[SCSI] lpfc 8.3.45: Incorporated support of a low-latency io path
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_scsi.c
CommitLineData
d85296cf 1/*******************************************************************
dea3101e 2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
92c13f29 4 * Copyright (C) 2004-2013 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * 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. *
dea3101e 20 *******************************************************************/
dea3101e 21#include <linux/pci.h>
5a0e3ad6 22#include <linux/slab.h>
dea3101e 23#include <linux/interrupt.h>
09703660 24#include <linux/export.h>
a90f5684 25#include <linux/delay.h>
e2a0a9d6 26#include <asm/unaligned.h>
737d4248
JS
27#include <linux/crc-t10dif.h>
28#include <net/checksum.h>
dea3101e 29
30#include <scsi/scsi.h>
31#include <scsi/scsi_device.h>
e2a0a9d6 32#include <scsi/scsi_eh.h>
dea3101e 33#include <scsi/scsi_host.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi_transport_fc.h>
36
37#include "lpfc_version.h"
da0436e9 38#include "lpfc_hw4.h"
dea3101e 39#include "lpfc_hw.h"
40#include "lpfc_sli.h"
da0436e9 41#include "lpfc_sli4.h"
ea2151b4 42#include "lpfc_nl.h"
dea3101e 43#include "lpfc_disc.h"
dea3101e 44#include "lpfc.h"
9a6b09c0 45#include "lpfc_scsi.h"
dea3101e 46#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
92d7f7b0 48#include "lpfc_vport.h"
dea3101e 49
50#define LPFC_RESET_WAIT 2
51#define LPFC_ABORT_WAIT 2
52
737d4248 53int _dump_buf_done = 1;
e2a0a9d6
JS
54
55static char *dif_op_str[] = {
9a6b09c0
JS
56 "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
f9bb2da1
JS
65struct 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
1ba981fd
JS
71static struct lpfc_rport_data *
72lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
73{
74 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
75
76 if (vport->phba->cfg_EnableXLane)
77 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
78 else
79 return (struct lpfc_rport_data *)sdev->hostdata;
80}
81
da0436e9
JS
82static void
83lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
1c6f4ef5
JS
84static void
85lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
9c6aa9d7
JS
86static int
87lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
e2a0a9d6
JS
88
89static void
6a9c52cf 90lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
e2a0a9d6
JS
91{
92 void *src, *dst;
93 struct scatterlist *sgde = scsi_sglist(cmnd);
94
95 if (!_dump_buf_data) {
6a9c52cf
JS
96 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
97 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
e2a0a9d6
JS
98 __func__);
99 return;
100 }
101
102
103 if (!sgde) {
6a9c52cf
JS
104 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
105 "9051 BLKGRD: ERROR: data scatterlist is null\n");
e2a0a9d6
JS
106 return;
107 }
108
109 dst = (void *) _dump_buf_data;
110 while (sgde) {
111 src = sg_virt(sgde);
112 memcpy(dst, src, sgde->length);
113 dst += sgde->length;
114 sgde = sg_next(sgde);
115 }
116}
117
118static void
6a9c52cf 119lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
e2a0a9d6
JS
120{
121 void *src, *dst;
122 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
123
124 if (!_dump_buf_dif) {
6a9c52cf
JS
125 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
126 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
e2a0a9d6
JS
127 __func__);
128 return;
129 }
130
131 if (!sgde) {
6a9c52cf
JS
132 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
133 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
e2a0a9d6
JS
134 return;
135 }
136
137 dst = _dump_buf_dif;
138 while (sgde) {
139 src = sg_virt(sgde);
140 memcpy(dst, src, sgde->length);
141 dst += sgde->length;
142 sgde = sg_next(sgde);
143 }
144}
145
9c6aa9d7
JS
146static inline unsigned
147lpfc_cmd_blksize(struct scsi_cmnd *sc)
148{
149 return sc->device->sector_size;
150}
151
152#define LPFC_CHECK_PROTECT_GUARD 1
153#define LPFC_CHECK_PROTECT_REF 2
154static inline unsigned
155lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
156{
157 return 1;
158}
159
160static inline unsigned
161lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
162{
163 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
164 return 0;
165 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
166 return 1;
167 return 0;
168}
169
f1126688
JS
170/**
171 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
172 * @phba: Pointer to HBA object.
173 * @lpfc_cmd: lpfc scsi command object pointer.
174 *
175 * This function is called from the lpfc_prep_task_mgmt_cmd function to
176 * set the last bit in the response sge entry.
177 **/
178static void
179lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
180 struct lpfc_scsi_buf *lpfc_cmd)
181{
182 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
183 if (sgl) {
184 sgl += 1;
185 sgl->word2 = le32_to_cpu(sgl->word2);
186 bf_set(lpfc_sli4_sge_last, sgl, 1);
187 sgl->word2 = cpu_to_le32(sgl->word2);
188 }
189}
190
ea2151b4 191/**
3621a710 192 * lpfc_update_stats - Update statistical data for the command completion
ea2151b4
JS
193 * @phba: Pointer to HBA object.
194 * @lpfc_cmd: lpfc scsi command object pointer.
195 *
196 * This function is called when there is a command completion and this
197 * function updates the statistical data for the command completion.
198 **/
199static void
200lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
201{
202 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
203 struct lpfc_nodelist *pnode = rdata->pnode;
204 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
205 unsigned long flags;
206 struct Scsi_Host *shost = cmd->device->host;
207 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
208 unsigned long latency;
209 int i;
210
211 if (cmd->result)
212 return;
213
9f1e1b50
JS
214 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
215
ea2151b4
JS
216 spin_lock_irqsave(shost->host_lock, flags);
217 if (!vport->stat_data_enabled ||
218 vport->stat_data_blocked ||
5989b8d4 219 !pnode ||
ea2151b4
JS
220 !pnode->lat_data ||
221 (phba->bucket_type == LPFC_NO_BUCKET)) {
222 spin_unlock_irqrestore(shost->host_lock, flags);
223 return;
224 }
ea2151b4
JS
225
226 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
227 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
228 phba->bucket_step;
9f1e1b50
JS
229 /* check array subscript bounds */
230 if (i < 0)
231 i = 0;
232 else if (i >= LPFC_MAX_BUCKET_COUNT)
233 i = LPFC_MAX_BUCKET_COUNT - 1;
ea2151b4
JS
234 } else {
235 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
236 if (latency <= (phba->bucket_base +
237 ((1<<i)*phba->bucket_step)))
238 break;
239 }
240
241 pnode->lat_data[i].cmd_count++;
242 spin_unlock_irqrestore(shost->host_lock, flags);
243}
244
ea2151b4 245/**
3621a710 246 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
ea2151b4
JS
247 * @phba: Pointer to HBA context object.
248 * @vport: Pointer to vport object.
249 * @ndlp: Pointer to FC node associated with the target.
250 * @lun: Lun number of the scsi device.
251 * @old_val: Old value of the queue depth.
252 * @new_val: New value of the queue depth.
253 *
254 * This function sends an event to the mgmt application indicating
255 * there is a change in the scsi device queue depth.
256 **/
257static void
258lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
259 struct lpfc_vport *vport,
260 struct lpfc_nodelist *ndlp,
261 uint32_t lun,
262 uint32_t old_val,
263 uint32_t new_val)
264{
265 struct lpfc_fast_path_event *fast_path_evt;
266 unsigned long flags;
267
268 fast_path_evt = lpfc_alloc_fast_evt(phba);
269 if (!fast_path_evt)
270 return;
271
272 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
273 FC_REG_SCSI_EVENT;
274 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
275 LPFC_EVENT_VARQUEDEPTH;
276
277 /* Report all luns with change in queue depth */
278 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
279 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
280 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
281 &ndlp->nlp_portname, sizeof(struct lpfc_name));
282 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
283 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
284 }
285
286 fast_path_evt->un.queue_depth_evt.oldval = old_val;
287 fast_path_evt->un.queue_depth_evt.newval = new_val;
288 fast_path_evt->vport = vport;
289
290 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
291 spin_lock_irqsave(&phba->hbalock, flags);
292 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
293 spin_unlock_irqrestore(&phba->hbalock, flags);
294 lpfc_worker_wake_up(phba);
295
296 return;
297}
298
5ffc266e
JS
299/**
300 * lpfc_change_queue_depth - Alter scsi device queue depth
301 * @sdev: Pointer the scsi device on which to change the queue depth.
302 * @qdepth: New queue depth to set the sdev to.
303 * @reason: The reason for the queue depth change.
304 *
305 * This function is called by the midlayer and the LLD to alter the queue
306 * depth for a scsi device. This function sets the queue depth to the new
307 * value and sends an event out to log the queue depth change.
308 **/
309int
310lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
311{
312 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
313 struct lpfc_hba *phba = vport->phba;
314 struct lpfc_rport_data *rdata;
315 unsigned long new_queue_depth, old_queue_depth;
316
317 old_queue_depth = sdev->queue_depth;
318 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
319 new_queue_depth = sdev->queue_depth;
1ba981fd 320 rdata = lpfc_rport_data_from_scsi_device(sdev);
5ffc266e
JS
321 if (rdata)
322 lpfc_send_sdev_queuedepth_change_event(phba, vport,
323 rdata->pnode, sdev->lun,
324 old_queue_depth,
325 new_queue_depth);
326 return sdev->queue_depth;
327}
328
fe8f7f9c
JS
329/**
330 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
331 * @sdev: Pointer the scsi device whose queue depth is to change
332 * @tag_type: Identifier for queue tag type
333 */
334static int
335lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
336{
337 if (sdev->tagged_supported) {
338 scsi_set_tag_type(sdev, tag_type);
339 if (tag_type)
340 scsi_activate_tcq(sdev, sdev->queue_depth);
341 else
342 scsi_deactivate_tcq(sdev, sdev->queue_depth);
343 } else
344 tag_type = 0;
345
346 return tag_type;
347}
348
9bad7671 349/**
3621a710 350 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
9bad7671
JS
351 * @phba: The Hba for which this call is being executed.
352 *
353 * This routine is called when there is resource error in driver or firmware.
354 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
355 * posts at most 1 event each second. This routine wakes up worker thread of
356 * @phba to process WORKER_RAM_DOWN_EVENT event.
357 *
358 * This routine should be called with no lock held.
359 **/
92d7f7b0 360void
eaf15d5b 361lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
92d7f7b0
JS
362{
363 unsigned long flags;
5e9d9b82 364 uint32_t evt_posted;
92d7f7b0
JS
365
366 spin_lock_irqsave(&phba->hbalock, flags);
367 atomic_inc(&phba->num_rsrc_err);
368 phba->last_rsrc_error_time = jiffies;
369
370 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
371 spin_unlock_irqrestore(&phba->hbalock, flags);
372 return;
373 }
374
375 phba->last_ramp_down_time = jiffies;
376
377 spin_unlock_irqrestore(&phba->hbalock, flags);
378
379 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
380 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
381 if (!evt_posted)
92d7f7b0 382 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
92d7f7b0
JS
383 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
384
5e9d9b82
JS
385 if (!evt_posted)
386 lpfc_worker_wake_up(phba);
92d7f7b0
JS
387 return;
388}
389
9bad7671 390/**
3621a710 391 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
9bad7671
JS
392 * @phba: The Hba for which this call is being executed.
393 *
394 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
395 * post at most 1 event every 5 minute after last_ramp_up_time or
396 * last_rsrc_error_time. This routine wakes up worker thread of @phba
397 * to process WORKER_RAM_DOWN_EVENT event.
398 *
399 * This routine should be called with no lock held.
400 **/
92d7f7b0 401static inline void
3de2a653 402lpfc_rampup_queue_depth(struct lpfc_vport *vport,
a257bf90 403 uint32_t queue_depth)
92d7f7b0
JS
404{
405 unsigned long flags;
3de2a653 406 struct lpfc_hba *phba = vport->phba;
5e9d9b82 407 uint32_t evt_posted;
92d7f7b0
JS
408 atomic_inc(&phba->num_cmd_success);
409
a257bf90 410 if (vport->cfg_lun_queue_depth <= queue_depth)
92d7f7b0 411 return;
92d7f7b0 412 spin_lock_irqsave(&phba->hbalock, flags);
5ffc266e
JS
413 if (time_before(jiffies,
414 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
415 time_before(jiffies,
416 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
92d7f7b0
JS
417 spin_unlock_irqrestore(&phba->hbalock, flags);
418 return;
419 }
92d7f7b0
JS
420 phba->last_ramp_up_time = jiffies;
421 spin_unlock_irqrestore(&phba->hbalock, flags);
422
423 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
424 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
425 if (!evt_posted)
92d7f7b0 426 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
92d7f7b0
JS
427 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
428
5e9d9b82
JS
429 if (!evt_posted)
430 lpfc_worker_wake_up(phba);
431 return;
92d7f7b0
JS
432}
433
9bad7671 434/**
3621a710 435 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
9bad7671
JS
436 * @phba: The Hba for which this call is being executed.
437 *
438 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
439 * thread.This routine reduces queue depth for all scsi device on each vport
440 * associated with @phba.
441 **/
92d7f7b0
JS
442void
443lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
444{
549e55cd
JS
445 struct lpfc_vport **vports;
446 struct Scsi_Host *shost;
92d7f7b0 447 struct scsi_device *sdev;
5ffc266e 448 unsigned long new_queue_depth;
92d7f7b0 449 unsigned long num_rsrc_err, num_cmd_success;
549e55cd 450 int i;
92d7f7b0
JS
451
452 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
453 num_cmd_success = atomic_read(&phba->num_cmd_success);
454
75ad83a4
JS
455 /*
456 * The error and success command counters are global per
457 * driver instance. If another handler has already
458 * operated on this error event, just exit.
459 */
460 if (num_rsrc_err == 0)
461 return;
462
549e55cd
JS
463 vports = lpfc_create_vport_work_array(phba);
464 if (vports != NULL)
21e9a0a5 465 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
466 shost = lpfc_shost_from_vport(vports[i]);
467 shost_for_each_device(sdev, shost) {
92d7f7b0 468 new_queue_depth =
549e55cd
JS
469 sdev->queue_depth * num_rsrc_err /
470 (num_rsrc_err + num_cmd_success);
471 if (!new_queue_depth)
472 new_queue_depth = sdev->queue_depth - 1;
473 else
474 new_queue_depth = sdev->queue_depth -
475 new_queue_depth;
5ffc266e
JS
476 lpfc_change_queue_depth(sdev, new_queue_depth,
477 SCSI_QDEPTH_DEFAULT);
549e55cd 478 }
92d7f7b0 479 }
09372820 480 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
481 atomic_set(&phba->num_rsrc_err, 0);
482 atomic_set(&phba->num_cmd_success, 0);
483}
484
9bad7671 485/**
3621a710 486 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
9bad7671
JS
487 * @phba: The Hba for which this call is being executed.
488 *
489 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
490 * thread.This routine increases queue depth for all scsi device on each vport
491 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
492 * num_cmd_success to zero.
493 **/
92d7f7b0
JS
494void
495lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
496{
549e55cd
JS
497 struct lpfc_vport **vports;
498 struct Scsi_Host *shost;
92d7f7b0 499 struct scsi_device *sdev;
549e55cd
JS
500 int i;
501
502 vports = lpfc_create_vport_work_array(phba);
503 if (vports != NULL)
21e9a0a5 504 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
505 shost = lpfc_shost_from_vport(vports[i]);
506 shost_for_each_device(sdev, shost) {
97eab634
JS
507 if (vports[i]->cfg_lun_queue_depth <=
508 sdev->queue_depth)
509 continue;
5ffc266e
JS
510 lpfc_change_queue_depth(sdev,
511 sdev->queue_depth+1,
512 SCSI_QDEPTH_RAMP_UP);
549e55cd 513 }
92d7f7b0 514 }
09372820 515 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
516 atomic_set(&phba->num_rsrc_err, 0);
517 atomic_set(&phba->num_cmd_success, 0);
518}
519
a8e497d5 520/**
3621a710 521 * lpfc_scsi_dev_block - set all scsi hosts to block state
a8e497d5
JS
522 * @phba: Pointer to HBA context object.
523 *
524 * This function walks vport list and set each SCSI host to block state
525 * by invoking fc_remote_port_delete() routine. This function is invoked
526 * with EEH when device's PCI slot has been permanently disabled.
527 **/
528void
529lpfc_scsi_dev_block(struct lpfc_hba *phba)
530{
531 struct lpfc_vport **vports;
532 struct Scsi_Host *shost;
533 struct scsi_device *sdev;
534 struct fc_rport *rport;
535 int i;
536
537 vports = lpfc_create_vport_work_array(phba);
538 if (vports != NULL)
21e9a0a5 539 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
a8e497d5
JS
540 shost = lpfc_shost_from_vport(vports[i]);
541 shost_for_each_device(sdev, shost) {
542 rport = starget_to_rport(scsi_target(sdev));
543 fc_remote_port_delete(rport);
544 }
545 }
546 lpfc_destroy_vport_work_array(phba, vports);
547}
548
9bad7671 549/**
3772a991 550 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
9bad7671 551 * @vport: The virtual port for which this call being executed.
3772a991 552 * @num_to_allocate: The requested number of buffers to allocate.
9bad7671 553 *
3772a991
JS
554 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
555 * the scsi buffer contains all the necessary information needed to initiate
556 * a SCSI I/O. The non-DMAable buffer region contains information to build
557 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
558 * and the initial BPL. In addition to allocating memory, the FCP CMND and
559 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
9bad7671
JS
560 *
561 * Return codes:
3772a991
JS
562 * int - number of scsi buffers that were allocated.
563 * 0 = failure, less than num_to_alloc is a partial failure.
9bad7671 564 **/
3772a991
JS
565static int
566lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea3101e 567{
2e0fef85 568 struct lpfc_hba *phba = vport->phba;
dea3101e 569 struct lpfc_scsi_buf *psb;
570 struct ulp_bde64 *bpl;
571 IOCB_t *iocb;
34b02dcd
JS
572 dma_addr_t pdma_phys_fcp_cmd;
573 dma_addr_t pdma_phys_fcp_rsp;
574 dma_addr_t pdma_phys_bpl;
604a3e30 575 uint16_t iotag;
96f7077f
JS
576 int bcnt, bpl_size;
577
578 bpl_size = phba->cfg_sg_dma_buf_size -
579 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
580
581 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
582 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
583 num_to_alloc, phba->cfg_sg_dma_buf_size,
584 (int)sizeof(struct fcp_cmnd),
585 (int)sizeof(struct fcp_rsp), bpl_size);
dea3101e 586
3772a991
JS
587 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
588 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
589 if (!psb)
590 break;
dea3101e 591
3772a991
JS
592 /*
593 * Get memory from the pci pool to map the virt space to pci
594 * bus space for an I/O. The DMA buffer includes space for the
595 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
596 * necessary to support the sg_tablesize.
597 */
598 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
599 GFP_KERNEL, &psb->dma_handle);
600 if (!psb->data) {
601 kfree(psb);
602 break;
603 }
604
605 /* Initialize virtual ptrs to dma_buf region. */
606 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
607
608 /* Allocate iotag for psb->cur_iocbq. */
609 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
610 if (iotag == 0) {
611 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
612 psb->data, psb->dma_handle);
613 kfree(psb);
614 break;
615 }
616 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
617
618 psb->fcp_cmnd = psb->data;
619 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
620 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
34b02dcd 621 sizeof(struct fcp_rsp);
dea3101e 622
3772a991
JS
623 /* Initialize local short-hand pointers. */
624 bpl = psb->fcp_bpl;
625 pdma_phys_fcp_cmd = psb->dma_handle;
626 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
627 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
628 sizeof(struct fcp_rsp);
629
630 /*
631 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
632 * are sg list bdes. Initialize the first two and leave the
633 * rest for queuecommand.
634 */
635 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
636 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
637 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
638 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
639 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
640
641 /* Setup the physical region for the FCP RSP */
642 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
643 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
644 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
645 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
646 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
647
648 /*
649 * Since the IOCB for the FCP I/O is built into this
650 * lpfc_scsi_buf, initialize it with all known data now.
651 */
652 iocb = &psb->cur_iocbq.iocb;
653 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
654 if ((phba->sli_rev == 3) &&
655 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
656 /* fill in immediate fcp command BDE */
657 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
658 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
659 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
660 unsli3.fcp_ext.icd);
661 iocb->un.fcpi64.bdl.addrHigh = 0;
662 iocb->ulpBdeCount = 0;
663 iocb->ulpLe = 0;
25985edc 664 /* fill in response BDE */
3772a991
JS
665 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
666 BUFF_TYPE_BDE_64;
667 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
668 sizeof(struct fcp_rsp);
669 iocb->unsli3.fcp_ext.rbde.addrLow =
670 putPaddrLow(pdma_phys_fcp_rsp);
671 iocb->unsli3.fcp_ext.rbde.addrHigh =
672 putPaddrHigh(pdma_phys_fcp_rsp);
673 } else {
674 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
675 iocb->un.fcpi64.bdl.bdeSize =
676 (2 * sizeof(struct ulp_bde64));
677 iocb->un.fcpi64.bdl.addrLow =
678 putPaddrLow(pdma_phys_bpl);
679 iocb->un.fcpi64.bdl.addrHigh =
680 putPaddrHigh(pdma_phys_bpl);
681 iocb->ulpBdeCount = 1;
682 iocb->ulpLe = 1;
683 }
684 iocb->ulpClass = CLASS3;
685 psb->status = IOSTAT_SUCCESS;
da0436e9 686 /* Put it back into the SCSI buffer list */
eee8877e 687 psb->cur_iocbq.context1 = psb;
1c6f4ef5 688 lpfc_release_scsi_buf_s3(phba, psb);
dea3101e 689
34b02dcd 690 }
dea3101e 691
3772a991 692 return bcnt;
dea3101e 693}
694
1151e3ec
JS
695/**
696 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
697 * @vport: pointer to lpfc vport data structure.
698 *
699 * This routine is invoked by the vport cleanup for deletions and the cleanup
700 * for an ndlp on removal.
701 **/
702void
703lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
704{
705 struct lpfc_hba *phba = vport->phba;
706 struct lpfc_scsi_buf *psb, *next_psb;
707 unsigned long iflag = 0;
708
709 spin_lock_irqsave(&phba->hbalock, iflag);
710 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
711 list_for_each_entry_safe(psb, next_psb,
712 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
713 if (psb->rdata && psb->rdata->pnode
714 && psb->rdata->pnode->vport == vport)
715 psb->rdata = NULL;
716 }
717 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
718 spin_unlock_irqrestore(&phba->hbalock, iflag);
719}
720
da0436e9
JS
721/**
722 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
723 * @phba: pointer to lpfc hba data structure.
724 * @axri: pointer to the fcp xri abort wcqe structure.
725 *
726 * This routine is invoked by the worker thread to process a SLI4 fast-path
727 * FCP aborted xri.
728 **/
729void
730lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
731 struct sli4_wcqe_xri_aborted *axri)
732{
733 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
19ca7609 734 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
da0436e9
JS
735 struct lpfc_scsi_buf *psb, *next_psb;
736 unsigned long iflag = 0;
0f65ff68
JS
737 struct lpfc_iocbq *iocbq;
738 int i;
19ca7609
JS
739 struct lpfc_nodelist *ndlp;
740 int rrq_empty = 0;
589a52d6 741 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
da0436e9 742
0f65ff68
JS
743 spin_lock_irqsave(&phba->hbalock, iflag);
744 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
da0436e9
JS
745 list_for_each_entry_safe(psb, next_psb,
746 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
747 if (psb->cur_iocbq.sli4_xritag == xri) {
748 list_del(&psb->list);
341af102 749 psb->exch_busy = 0;
da0436e9 750 psb->status = IOSTAT_SUCCESS;
0f65ff68
JS
751 spin_unlock(
752 &phba->sli4_hba.abts_scsi_buf_list_lock);
1151e3ec
JS
753 if (psb->rdata && psb->rdata->pnode)
754 ndlp = psb->rdata->pnode;
755 else
756 ndlp = NULL;
757
19ca7609 758 rrq_empty = list_empty(&phba->active_rrq_list);
0f65ff68 759 spin_unlock_irqrestore(&phba->hbalock, iflag);
cb69f7de 760 if (ndlp) {
ee0f4fe1
JS
761 lpfc_set_rrq_active(phba, ndlp,
762 psb->cur_iocbq.sli4_lxritag, rxid, 1);
cb69f7de
JS
763 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
764 }
da0436e9 765 lpfc_release_scsi_buf_s4(phba, psb);
19ca7609
JS
766 if (rrq_empty)
767 lpfc_worker_wake_up(phba);
da0436e9
JS
768 return;
769 }
770 }
0f65ff68
JS
771 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
772 for (i = 1; i <= phba->sli.last_iotag; i++) {
773 iocbq = phba->sli.iocbq_lookup[i];
774
775 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
776 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
777 continue;
778 if (iocbq->sli4_xritag != xri)
779 continue;
780 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
781 psb->exch_busy = 0;
782 spin_unlock_irqrestore(&phba->hbalock, iflag);
0e9bb8d7 783 if (!list_empty(&pring->txq))
589a52d6 784 lpfc_worker_wake_up(phba);
0f65ff68
JS
785 return;
786
787 }
788 spin_unlock_irqrestore(&phba->hbalock, iflag);
da0436e9
JS
789}
790
791/**
8a9d2e80 792 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
da0436e9 793 * @phba: pointer to lpfc hba data structure.
8a9d2e80 794 * @post_sblist: pointer to the scsi buffer list.
da0436e9 795 *
8a9d2e80
JS
796 * This routine walks a list of scsi buffers that was passed in. It attempts
797 * to construct blocks of scsi buffer sgls which contains contiguous xris and
798 * uses the non-embedded SGL block post mailbox commands to post to the port.
799 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
800 * embedded SGL post mailbox command for posting. The @post_sblist passed in
801 * must be local list, thus no lock is needed when manipulate the list.
da0436e9 802 *
8a9d2e80 803 * Returns: 0 = failure, non-zero number of successfully posted buffers.
da0436e9
JS
804 **/
805int
8a9d2e80
JS
806lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
807 struct list_head *post_sblist, int sb_count)
da0436e9 808{
8a9d2e80 809 struct lpfc_scsi_buf *psb, *psb_next;
96f7077f 810 int status, sgl_size;
8a9d2e80
JS
811 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
812 dma_addr_t pdma_phys_bpl1;
813 int last_xritag = NO_XRI;
814 LIST_HEAD(prep_sblist);
815 LIST_HEAD(blck_sblist);
816 LIST_HEAD(scsi_sblist);
817
818 /* sanity check */
819 if (sb_count <= 0)
820 return -EINVAL;
821
96f7077f
JS
822 sgl_size = phba->cfg_sg_dma_buf_size -
823 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
824
8a9d2e80
JS
825 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
826 list_del_init(&psb->list);
827 block_cnt++;
828 if ((last_xritag != NO_XRI) &&
829 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
830 /* a hole in xri block, form a sgl posting block */
831 list_splice_init(&prep_sblist, &blck_sblist);
832 post_cnt = block_cnt - 1;
833 /* prepare list for next posting block */
834 list_add_tail(&psb->list, &prep_sblist);
835 block_cnt = 1;
836 } else {
837 /* prepare list for next posting block */
838 list_add_tail(&psb->list, &prep_sblist);
839 /* enough sgls for non-embed sgl mbox command */
840 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
841 list_splice_init(&prep_sblist, &blck_sblist);
842 post_cnt = block_cnt;
843 block_cnt = 0;
da0436e9 844 }
8a9d2e80
JS
845 }
846 num_posting++;
847 last_xritag = psb->cur_iocbq.sli4_xritag;
da0436e9 848
8a9d2e80
JS
849 /* end of repost sgl list condition for SCSI buffers */
850 if (num_posting == sb_count) {
851 if (post_cnt == 0) {
852 /* last sgl posting block */
853 list_splice_init(&prep_sblist, &blck_sblist);
854 post_cnt = block_cnt;
855 } else if (block_cnt == 1) {
856 /* last single sgl with non-contiguous xri */
96f7077f 857 if (sgl_size > SGL_PAGE_SIZE)
8a9d2e80
JS
858 pdma_phys_bpl1 = psb->dma_phys_bpl +
859 SGL_PAGE_SIZE;
860 else
861 pdma_phys_bpl1 = 0;
862 status = lpfc_sli4_post_sgl(phba,
863 psb->dma_phys_bpl,
864 pdma_phys_bpl1,
865 psb->cur_iocbq.sli4_xritag);
866 if (status) {
867 /* failure, put on abort scsi list */
868 psb->exch_busy = 1;
869 } else {
870 /* success, put on SCSI buffer list */
871 psb->exch_busy = 0;
872 psb->status = IOSTAT_SUCCESS;
873 num_posted++;
874 }
875 /* success, put on SCSI buffer sgl list */
876 list_add_tail(&psb->list, &scsi_sblist);
877 }
878 }
da0436e9 879
8a9d2e80
JS
880 /* continue until a nembed page worth of sgls */
881 if (post_cnt == 0)
da0436e9 882 continue;
8a9d2e80
JS
883
884 /* post block of SCSI buffer list sgls */
885 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
886 post_cnt);
887
888 /* don't reset xirtag due to hole in xri block */
889 if (block_cnt == 0)
890 last_xritag = NO_XRI;
891
892 /* reset SCSI buffer post count for next round of posting */
893 post_cnt = 0;
894
895 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
896 while (!list_empty(&blck_sblist)) {
897 list_remove_head(&blck_sblist, psb,
898 struct lpfc_scsi_buf, list);
da0436e9 899 if (status) {
8a9d2e80 900 /* failure, put on abort scsi list */
341af102 901 psb->exch_busy = 1;
341af102 902 } else {
8a9d2e80 903 /* success, put on SCSI buffer list */
341af102 904 psb->exch_busy = 0;
da0436e9 905 psb->status = IOSTAT_SUCCESS;
8a9d2e80 906 num_posted++;
341af102 907 }
8a9d2e80 908 list_add_tail(&psb->list, &scsi_sblist);
da0436e9
JS
909 }
910 }
8a9d2e80
JS
911 /* Push SCSI buffers with sgl posted to the availble list */
912 while (!list_empty(&scsi_sblist)) {
913 list_remove_head(&scsi_sblist, psb,
914 struct lpfc_scsi_buf, list);
915 lpfc_release_scsi_buf_s4(phba, psb);
916 }
917 return num_posted;
918}
919
920/**
921 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
922 * @phba: pointer to lpfc hba data structure.
923 *
924 * This routine walks the list of scsi buffers that have been allocated and
925 * repost them to the port by using SGL block post. This is needed after a
926 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
927 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
928 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
929 *
930 * Returns: 0 = success, non-zero failure.
931 **/
932int
933lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
934{
935 LIST_HEAD(post_sblist);
936 int num_posted, rc = 0;
937
938 /* get all SCSI buffers need to repost to a local list */
a40fc5f0 939 spin_lock_irq(&phba->scsi_buf_list_get_lock);
164cecd1 940 spin_lock(&phba->scsi_buf_list_put_lock);
a40fc5f0
JS
941 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
942 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
164cecd1 943 spin_unlock(&phba->scsi_buf_list_put_lock);
a40fc5f0 944 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
8a9d2e80
JS
945
946 /* post the list of scsi buffer sgls to port if available */
947 if (!list_empty(&post_sblist)) {
948 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
949 phba->sli4_hba.scsi_xri_cnt);
950 /* failed to post any scsi buffer, return error */
951 if (num_posted == 0)
952 rc = -EIO;
953 }
da0436e9
JS
954 return rc;
955}
956
957/**
958 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
959 * @vport: The virtual port for which this call being executed.
960 * @num_to_allocate: The requested number of buffers to allocate.
961 *
8a9d2e80 962 * This routine allocates scsi buffers for device with SLI-4 interface spec,
da0436e9 963 * the scsi buffer contains all the necessary information needed to initiate
8a9d2e80
JS
964 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
965 * them on a list, it post them to the port by using SGL block post.
da0436e9
JS
966 *
967 * Return codes:
8a9d2e80 968 * int - number of scsi buffers that were allocated and posted.
da0436e9
JS
969 * 0 = failure, less than num_to_alloc is a partial failure.
970 **/
971static int
972lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
973{
974 struct lpfc_hba *phba = vport->phba;
975 struct lpfc_scsi_buf *psb;
976 struct sli4_sge *sgl;
977 IOCB_t *iocb;
978 dma_addr_t pdma_phys_fcp_cmd;
979 dma_addr_t pdma_phys_fcp_rsp;
96f7077f 980 dma_addr_t pdma_phys_bpl;
8a9d2e80 981 uint16_t iotag, lxri = 0;
96f7077f 982 int bcnt, num_posted, sgl_size;
8a9d2e80
JS
983 LIST_HEAD(prep_sblist);
984 LIST_HEAD(post_sblist);
985 LIST_HEAD(scsi_sblist);
da0436e9 986
96f7077f
JS
987 sgl_size = phba->cfg_sg_dma_buf_size -
988 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
989
990 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
991 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
992 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
993 (int)sizeof(struct fcp_cmnd),
994 (int)sizeof(struct fcp_rsp));
995
da0436e9
JS
996 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
997 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
998 if (!psb)
999 break;
da0436e9 1000 /*
8a9d2e80
JS
1001 * Get memory from the pci pool to map the virt space to
1002 * pci bus space for an I/O. The DMA buffer includes space
1003 * for the struct fcp_cmnd, struct fcp_rsp and the number
1004 * of bde's necessary to support the sg_tablesize.
da0436e9
JS
1005 */
1006 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
1007 GFP_KERNEL, &psb->dma_handle);
1008 if (!psb->data) {
1009 kfree(psb);
1010 break;
1011 }
da0436e9
JS
1012 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
1013
092cb034
JS
1014 /*
1015 * 4K Page alignment is CRITICAL to BlockGuard, double check
1016 * to be sure.
1017 */
1018 if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
1019 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
96f7077f
JS
1020 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1021 psb->data, psb->dma_handle);
1022 kfree(psb);
1023 break;
1024 }
1025
f7bc6434
JS
1026
1027 lxri = lpfc_sli4_next_xritag(phba);
1028 if (lxri == NO_XRI) {
b92938b4 1029 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
f7bc6434 1030 psb->data, psb->dma_handle);
da0436e9
JS
1031 kfree(psb);
1032 break;
1033 }
1034
f7bc6434
JS
1035 /* Allocate iotag for psb->cur_iocbq. */
1036 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
1037 if (iotag == 0) {
da0436e9 1038 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
f7bc6434 1039 psb->data, psb->dma_handle);
da0436e9 1040 kfree(psb);
f7bc6434
JS
1041 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1042 "3368 Failed to allocated IOTAG for"
1043 " XRI:0x%x\n", lxri);
1044 lpfc_sli4_free_xri(phba, lxri);
da0436e9
JS
1045 break;
1046 }
6d368e53
JS
1047 psb->cur_iocbq.sli4_lxritag = lxri;
1048 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
da0436e9 1049 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
da0436e9 1050 psb->fcp_bpl = psb->data;
96f7077f 1051 psb->fcp_cmnd = (psb->data + sgl_size);
da0436e9
JS
1052 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1053 sizeof(struct fcp_cmnd));
1054
1055 /* Initialize local short-hand pointers. */
1056 sgl = (struct sli4_sge *)psb->fcp_bpl;
1057 pdma_phys_bpl = psb->dma_handle;
96f7077f 1058 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
da0436e9
JS
1059 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1060
1061 /*
8a9d2e80
JS
1062 * The first two bdes are the FCP_CMD and FCP_RSP.
1063 * The balance are sg list bdes. Initialize the
1064 * first two and leave the rest for queuecommand.
da0436e9
JS
1065 */
1066 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1067 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
0558056c 1068 sgl->word2 = le32_to_cpu(sgl->word2);
da0436e9
JS
1069 bf_set(lpfc_sli4_sge_last, sgl, 0);
1070 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74 1071 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
da0436e9
JS
1072 sgl++;
1073
1074 /* Setup the physical region for the FCP RSP */
1075 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1076 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
0558056c 1077 sgl->word2 = le32_to_cpu(sgl->word2);
da0436e9
JS
1078 bf_set(lpfc_sli4_sge_last, sgl, 1);
1079 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74 1080 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
da0436e9
JS
1081
1082 /*
1083 * Since the IOCB for the FCP I/O is built into this
1084 * lpfc_scsi_buf, initialize it with all known data now.
1085 */
1086 iocb = &psb->cur_iocbq.iocb;
1087 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1088 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1089 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1090 * We are setting the bpl to point to out sgl. An sgl's
1091 * entries are 16 bytes, a bpl entries are 12 bytes.
1092 */
1093 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1094 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1095 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1096 iocb->ulpBdeCount = 1;
1097 iocb->ulpLe = 1;
1098 iocb->ulpClass = CLASS3;
8a9d2e80 1099 psb->cur_iocbq.context1 = psb;
da0436e9 1100 psb->dma_phys_bpl = pdma_phys_bpl;
da0436e9 1101
8a9d2e80
JS
1102 /* add the scsi buffer to a post list */
1103 list_add_tail(&psb->list, &post_sblist);
a40fc5f0 1104 spin_lock_irq(&phba->scsi_buf_list_get_lock);
8a9d2e80 1105 phba->sli4_hba.scsi_xri_cnt++;
a40fc5f0 1106 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
8a9d2e80
JS
1107 }
1108 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1109 "3021 Allocate %d out of %d requested new SCSI "
1110 "buffers\n", bcnt, num_to_alloc);
1111
1112 /* post the list of scsi buffer sgls to port if available */
1113 if (!list_empty(&post_sblist))
1114 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1115 &post_sblist, bcnt);
1116 else
1117 num_posted = 0;
1118
1119 return num_posted;
da0436e9
JS
1120}
1121
9bad7671 1122/**
3772a991
JS
1123 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1124 * @vport: The virtual port for which this call being executed.
1125 * @num_to_allocate: The requested number of buffers to allocate.
1126 *
1127 * This routine wraps the actual SCSI buffer allocator function pointer from
1128 * the lpfc_hba struct.
1129 *
1130 * Return codes:
1131 * int - number of scsi buffers that were allocated.
1132 * 0 = failure, less than num_to_alloc is a partial failure.
1133 **/
1134static inline int
1135lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1136{
1137 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1138}
1139
1140/**
19ca7609 1141 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
3772a991 1142 * @phba: The HBA for which this call is being executed.
9bad7671
JS
1143 *
1144 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1145 * and returns to caller.
1146 *
1147 * Return codes:
1148 * NULL - Error
1149 * Pointer to lpfc_scsi_buf - Success
1150 **/
455c53ec 1151static struct lpfc_scsi_buf*
19ca7609 1152lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 1153{
0bd4ca25 1154 struct lpfc_scsi_buf * lpfc_cmd = NULL;
a40fc5f0 1155 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
164cecd1 1156 unsigned long iflag = 0;
a40fc5f0 1157
164cecd1 1158 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
a40fc5f0
JS
1159 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1160 list);
1161 if (!lpfc_cmd) {
164cecd1 1162 spin_lock(&phba->scsi_buf_list_put_lock);
a40fc5f0
JS
1163 list_splice(&phba->lpfc_scsi_buf_list_put,
1164 &phba->lpfc_scsi_buf_list_get);
1165 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1166 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1167 struct lpfc_scsi_buf, list);
164cecd1 1168 spin_unlock(&phba->scsi_buf_list_put_lock);
1dcb58e5 1169 }
164cecd1 1170 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
0bd4ca25
JSEC
1171 return lpfc_cmd;
1172}
19ca7609
JS
1173/**
1174 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1175 * @phba: The HBA for which this call is being executed.
1176 *
1177 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1178 * and returns to caller.
1179 *
1180 * Return codes:
1181 * NULL - Error
1182 * Pointer to lpfc_scsi_buf - Success
1183 **/
1184static struct lpfc_scsi_buf*
1185lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1186{
3be30e0e 1187 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
164cecd1 1188 unsigned long iflag = 0;
19ca7609
JS
1189 int found = 0;
1190
164cecd1 1191 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
3be30e0e
JS
1192 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1193 &phba->lpfc_scsi_buf_list_get, list) {
19ca7609 1194 if (lpfc_test_rrq_active(phba, ndlp,
ee0f4fe1 1195 lpfc_cmd->cur_iocbq.sli4_lxritag))
1151e3ec
JS
1196 continue;
1197 list_del(&lpfc_cmd->list);
19ca7609 1198 found = 1;
1151e3ec 1199 break;
19ca7609 1200 }
a40fc5f0 1201 if (!found) {
164cecd1 1202 spin_lock(&phba->scsi_buf_list_put_lock);
a40fc5f0
JS
1203 list_splice(&phba->lpfc_scsi_buf_list_put,
1204 &phba->lpfc_scsi_buf_list_get);
1205 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
164cecd1 1206 spin_unlock(&phba->scsi_buf_list_put_lock);
3be30e0e
JS
1207 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1208 &phba->lpfc_scsi_buf_list_get, list) {
a40fc5f0
JS
1209 if (lpfc_test_rrq_active(
1210 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1211 continue;
1212 list_del(&lpfc_cmd->list);
1213 found = 1;
1214 break;
1215 }
1216 }
164cecd1 1217 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
1151e3ec
JS
1218 if (!found)
1219 return NULL;
a40fc5f0 1220 return lpfc_cmd;
19ca7609
JS
1221}
1222/**
1223 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1224 * @phba: The HBA for which this call is being executed.
1225 *
1226 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1227 * and returns to caller.
1228 *
1229 * Return codes:
1230 * NULL - Error
1231 * Pointer to lpfc_scsi_buf - Success
1232 **/
1233static struct lpfc_scsi_buf*
1234lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1235{
1236 return phba->lpfc_get_scsi_buf(phba, ndlp);
1237}
dea3101e 1238
9bad7671 1239/**
3772a991 1240 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
9bad7671
JS
1241 * @phba: The Hba for which this call is being executed.
1242 * @psb: The scsi buffer which is being released.
1243 *
1244 * This routine releases @psb scsi buffer by adding it to tail of @phba
1245 * lpfc_scsi_buf_list list.
1246 **/
0bd4ca25 1247static void
3772a991 1248lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
0bd4ca25 1249{
875fbdfe 1250 unsigned long iflag = 0;
dea3101e 1251
a40fc5f0
JS
1252 psb->seg_cnt = 0;
1253 psb->nonsg_phys = 0;
1254 psb->prot_seg_cnt = 0;
1255
1256 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
0bd4ca25 1257 psb->pCmd = NULL;
6a485eb9 1258 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
a40fc5f0
JS
1259 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1260 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea3101e 1261}
1262
da0436e9
JS
1263/**
1264 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1265 * @phba: The Hba for which this call is being executed.
1266 * @psb: The scsi buffer which is being released.
1267 *
1268 * This routine releases @psb scsi buffer by adding it to tail of @phba
1269 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1270 * and cannot be reused for at least RA_TOV amount of time if it was
1271 * aborted.
1272 **/
1273static void
1274lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1275{
1276 unsigned long iflag = 0;
1277
a40fc5f0
JS
1278 psb->seg_cnt = 0;
1279 psb->nonsg_phys = 0;
1280 psb->prot_seg_cnt = 0;
1281
341af102 1282 if (psb->exch_busy) {
da0436e9
JS
1283 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1284 iflag);
1285 psb->pCmd = NULL;
1286 list_add_tail(&psb->list,
1287 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1288 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1289 iflag);
1290 } else {
da0436e9 1291 psb->pCmd = NULL;
6a485eb9 1292 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
a40fc5f0
JS
1293 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1294 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1295 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
da0436e9
JS
1296 }
1297}
1298
9bad7671 1299/**
3772a991
JS
1300 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1301 * @phba: The Hba for which this call is being executed.
1302 * @psb: The scsi buffer which is being released.
1303 *
1304 * This routine releases @psb scsi buffer by adding it to tail of @phba
1305 * lpfc_scsi_buf_list list.
1306 **/
1307static void
1308lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1309{
1310
1311 phba->lpfc_release_scsi_buf(phba, psb);
1312}
1313
1314/**
1315 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
9bad7671
JS
1316 * @phba: The Hba for which this call is being executed.
1317 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1318 *
1319 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3772a991
JS
1320 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1321 * through sg elements and format the bdea. This routine also initializes all
1322 * IOCB fields which are dependent on scsi command request buffer.
9bad7671
JS
1323 *
1324 * Return codes:
1325 * 1 - Error
1326 * 0 - Success
1327 **/
dea3101e 1328static int
3772a991 1329lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea3101e 1330{
1331 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1332 struct scatterlist *sgel = NULL;
1333 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1334 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
0f65ff68 1335 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea3101e 1336 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
34b02dcd 1337 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea3101e 1338 dma_addr_t physaddr;
34b02dcd 1339 uint32_t num_bde = 0;
a0b4f78f 1340 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea3101e 1341
1342 /*
1343 * There are three possibilities here - use scatter-gather segment, use
1344 * the single mapping, or neither. Start the lpfc command prep by
1345 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1346 * data bde entry.
1347 */
1348 bpl += 2;
c59fd9eb 1349 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 1350 /*
1351 * The driver stores the segment count returned from pci_map_sg
1352 * because this a count of dma-mappings used to map the use_sg
1353 * pages. They are not guaranteed to be the same for those
1354 * architectures that implement an IOMMU.
1355 */
dea3101e 1356
c59fd9eb
FT
1357 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1358 scsi_sg_count(scsi_cmnd), datadir);
1359 if (unlikely(!nseg))
1360 return 1;
1361
a0b4f78f 1362 lpfc_cmd->seg_cnt = nseg;
dea3101e 1363 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
6a9c52cf
JS
1364 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1365 "9064 BLKGRD: %s: Too many sg segments from "
e2a0a9d6 1366 "dma_map_sg. Config %d, seg_cnt %d\n",
cadbd4a5 1367 __func__, phba->cfg_sg_seg_cnt,
dea3101e 1368 lpfc_cmd->seg_cnt);
96f7077f 1369 lpfc_cmd->seg_cnt = 0;
a0b4f78f 1370 scsi_dma_unmap(scsi_cmnd);
dea3101e 1371 return 1;
1372 }
1373
1374 /*
1375 * The driver established a maximum scatter-gather segment count
1376 * during probe that limits the number of sg elements in any
1377 * single scsi command. Just run through the seg_cnt and format
1378 * the bde's.
34b02dcd
JS
1379 * When using SLI-3 the driver will try to fit all the BDEs into
1380 * the IOCB. If it can't then the BDEs get added to a BPL as it
1381 * does for SLI-2 mode.
dea3101e 1382 */
34b02dcd 1383 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea3101e 1384 physaddr = sg_dma_address(sgel);
34b02dcd 1385 if (phba->sli_rev == 3 &&
e2a0a9d6 1386 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
0f65ff68 1387 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
34b02dcd
JS
1388 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1389 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1390 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1391 data_bde->addrLow = putPaddrLow(physaddr);
1392 data_bde->addrHigh = putPaddrHigh(physaddr);
1393 data_bde++;
1394 } else {
1395 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1396 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1397 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1398 bpl->addrLow =
1399 le32_to_cpu(putPaddrLow(physaddr));
1400 bpl->addrHigh =
1401 le32_to_cpu(putPaddrHigh(physaddr));
1402 bpl++;
1403 }
dea3101e 1404 }
c59fd9eb 1405 }
dea3101e 1406
1407 /*
1408 * Finish initializing those IOCB fields that are dependent on the
34b02dcd
JS
1409 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1410 * explicitly reinitialized and for SLI-3 the extended bde count is
1411 * explicitly reinitialized since all iocb memory resources are reused.
dea3101e 1412 */
e2a0a9d6 1413 if (phba->sli_rev == 3 &&
0f65ff68
JS
1414 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1415 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
34b02dcd
JS
1416 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1417 /*
1418 * The extended IOCB format can only fit 3 BDE or a BPL.
1419 * This I/O has more than 3 BDE so the 1st data bde will
1420 * be a BPL that is filled in here.
1421 */
1422 physaddr = lpfc_cmd->dma_handle;
1423 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1424 data_bde->tus.f.bdeSize = (num_bde *
1425 sizeof(struct ulp_bde64));
1426 physaddr += (sizeof(struct fcp_cmnd) +
1427 sizeof(struct fcp_rsp) +
1428 (2 * sizeof(struct ulp_bde64)));
1429 data_bde->addrHigh = putPaddrHigh(physaddr);
1430 data_bde->addrLow = putPaddrLow(physaddr);
25985edc 1431 /* ebde count includes the response bde and data bpl */
34b02dcd
JS
1432 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1433 } else {
25985edc 1434 /* ebde count includes the response bde and data bdes */
34b02dcd
JS
1435 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1436 }
1437 } else {
1438 iocb_cmd->un.fcpi64.bdl.bdeSize =
1439 ((num_bde + 2) * sizeof(struct ulp_bde64));
0f65ff68 1440 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
34b02dcd 1441 }
09372820 1442 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
e2a0a9d6
JS
1443
1444 /*
1445 * Due to difference in data length between DIF/non-DIF paths,
1446 * we need to set word 4 of IOCB here
1447 */
a257bf90 1448 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
e2a0a9d6
JS
1449 return 0;
1450}
1451
f9bb2da1 1452#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
acd6859b 1453
9a6b09c0
JS
1454/* Return if if error injection is detected by Initiator */
1455#define BG_ERR_INIT 0x1
1456/* Return if if error injection is detected by Target */
1457#define BG_ERR_TGT 0x2
1458/* Return if if swapping CSUM<-->CRC is required for error injection */
1459#define BG_ERR_SWAP 0x10
1460/* Return if disabling Guard/Ref/App checking is required for error injection */
1461#define BG_ERR_CHECK 0x20
acd6859b
JS
1462
1463/**
1464 * lpfc_bg_err_inject - Determine if we should inject an error
1465 * @phba: The Hba for which this call is being executed.
f9bb2da1
JS
1466 * @sc: The SCSI command to examine
1467 * @reftag: (out) BlockGuard reference tag for transmitted data
1468 * @apptag: (out) BlockGuard application tag for transmitted data
1469 * @new_guard (in) Value to replace CRC with if needed
1470 *
9a6b09c0 1471 * Returns BG_ERR_* bit mask or 0 if request ignored
acd6859b 1472 **/
f9bb2da1
JS
1473static int
1474lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1475 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1476{
1477 struct scatterlist *sgpe; /* s/g prot entry */
1478 struct scatterlist *sgde; /* s/g data entry */
9a6b09c0 1479 struct lpfc_scsi_buf *lpfc_cmd = NULL;
acd6859b 1480 struct scsi_dif_tuple *src = NULL;
4ac9b226
JS
1481 struct lpfc_nodelist *ndlp;
1482 struct lpfc_rport_data *rdata;
f9bb2da1
JS
1483 uint32_t op = scsi_get_prot_op(sc);
1484 uint32_t blksize;
1485 uint32_t numblks;
1486 sector_t lba;
1487 int rc = 0;
acd6859b 1488 int blockoff = 0;
f9bb2da1
JS
1489
1490 if (op == SCSI_PROT_NORMAL)
1491 return 0;
1492
acd6859b
JS
1493 sgpe = scsi_prot_sglist(sc);
1494 sgde = scsi_sglist(sc);
f9bb2da1 1495 lba = scsi_get_lba(sc);
4ac9b226
JS
1496
1497 /* First check if we need to match the LBA */
f9bb2da1
JS
1498 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1499 blksize = lpfc_cmd_blksize(sc);
1500 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1501
1502 /* Make sure we have the right LBA if one is specified */
1503 if ((phba->lpfc_injerr_lba < lba) ||
1504 (phba->lpfc_injerr_lba >= (lba + numblks)))
1505 return 0;
acd6859b
JS
1506 if (sgpe) {
1507 blockoff = phba->lpfc_injerr_lba - lba;
1508 numblks = sg_dma_len(sgpe) /
1509 sizeof(struct scsi_dif_tuple);
1510 if (numblks < blockoff)
1511 blockoff = numblks;
acd6859b 1512 }
f9bb2da1
JS
1513 }
1514
4ac9b226 1515 /* Next check if we need to match the remote NPortID or WWPN */
1ba981fd 1516 rdata = lpfc_rport_data_from_scsi_device(sc->device);
4ac9b226
JS
1517 if (rdata && rdata->pnode) {
1518 ndlp = rdata->pnode;
1519
1520 /* Make sure we have the right NPortID if one is specified */
1521 if (phba->lpfc_injerr_nportid &&
1522 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1523 return 0;
1524
1525 /*
1526 * Make sure we have the right WWPN if one is specified.
1527 * wwn[0] should be a non-zero NAA in a good WWPN.
1528 */
1529 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1530 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1531 sizeof(struct lpfc_name)) != 0))
1532 return 0;
1533 }
1534
1535 /* Setup a ptr to the protection data if the SCSI host provides it */
1536 if (sgpe) {
1537 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1538 src += blockoff;
1539 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1540 }
1541
f9bb2da1
JS
1542 /* Should we change the Reference Tag */
1543 if (reftag) {
acd6859b
JS
1544 if (phba->lpfc_injerr_wref_cnt) {
1545 switch (op) {
1546 case SCSI_PROT_WRITE_PASS:
9a6b09c0
JS
1547 if (src) {
1548 /*
1549 * For WRITE_PASS, force the error
1550 * to be sent on the wire. It should
1551 * be detected by the Target.
1552 * If blockoff != 0 error will be
1553 * inserted in middle of the IO.
1554 */
acd6859b
JS
1555
1556 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1557 "9076 BLKGRD: Injecting reftag error: "
1558 "write lba x%lx + x%x oldrefTag x%x\n",
1559 (unsigned long)lba, blockoff,
9a6b09c0 1560 be32_to_cpu(src->ref_tag));
f9bb2da1 1561
acd6859b 1562 /*
9a6b09c0
JS
1563 * Save the old ref_tag so we can
1564 * restore it on completion.
acd6859b 1565 */
9a6b09c0
JS
1566 if (lpfc_cmd) {
1567 lpfc_cmd->prot_data_type =
1568 LPFC_INJERR_REFTAG;
1569 lpfc_cmd->prot_data_segment =
1570 src;
1571 lpfc_cmd->prot_data =
1572 src->ref_tag;
1573 }
1574 src->ref_tag = cpu_to_be32(0xDEADBEEF);
acd6859b 1575 phba->lpfc_injerr_wref_cnt--;
4ac9b226
JS
1576 if (phba->lpfc_injerr_wref_cnt == 0) {
1577 phba->lpfc_injerr_nportid = 0;
1578 phba->lpfc_injerr_lba =
1579 LPFC_INJERR_LBA_OFF;
1580 memset(&phba->lpfc_injerr_wwpn,
1581 0, sizeof(struct lpfc_name));
1582 }
9a6b09c0
JS
1583 rc = BG_ERR_TGT | BG_ERR_CHECK;
1584
acd6859b
JS
1585 break;
1586 }
1587 /* Drop thru */
9a6b09c0 1588 case SCSI_PROT_WRITE_INSERT:
acd6859b 1589 /*
9a6b09c0
JS
1590 * For WRITE_INSERT, force the error
1591 * to be sent on the wire. It should be
1592 * detected by the Target.
acd6859b 1593 */
9a6b09c0 1594 /* DEADBEEF will be the reftag on the wire */
acd6859b
JS
1595 *reftag = 0xDEADBEEF;
1596 phba->lpfc_injerr_wref_cnt--;
4ac9b226
JS
1597 if (phba->lpfc_injerr_wref_cnt == 0) {
1598 phba->lpfc_injerr_nportid = 0;
1599 phba->lpfc_injerr_lba =
1600 LPFC_INJERR_LBA_OFF;
1601 memset(&phba->lpfc_injerr_wwpn,
1602 0, sizeof(struct lpfc_name));
1603 }
9a6b09c0 1604 rc = BG_ERR_TGT | BG_ERR_CHECK;
acd6859b
JS
1605
1606 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
9a6b09c0 1607 "9078 BLKGRD: Injecting reftag error: "
acd6859b
JS
1608 "write lba x%lx\n", (unsigned long)lba);
1609 break;
9a6b09c0 1610 case SCSI_PROT_WRITE_STRIP:
acd6859b 1611 /*
9a6b09c0
JS
1612 * For WRITE_STRIP and WRITE_PASS,
1613 * force the error on data
1614 * being copied from SLI-Host to SLI-Port.
acd6859b 1615 */
f9bb2da1
JS
1616 *reftag = 0xDEADBEEF;
1617 phba->lpfc_injerr_wref_cnt--;
4ac9b226
JS
1618 if (phba->lpfc_injerr_wref_cnt == 0) {
1619 phba->lpfc_injerr_nportid = 0;
1620 phba->lpfc_injerr_lba =
1621 LPFC_INJERR_LBA_OFF;
1622 memset(&phba->lpfc_injerr_wwpn,
1623 0, sizeof(struct lpfc_name));
1624 }
9a6b09c0 1625 rc = BG_ERR_INIT;
f9bb2da1
JS
1626
1627 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
9a6b09c0 1628 "9077 BLKGRD: Injecting reftag error: "
f9bb2da1 1629 "write lba x%lx\n", (unsigned long)lba);
acd6859b 1630 break;
f9bb2da1 1631 }
acd6859b
JS
1632 }
1633 if (phba->lpfc_injerr_rref_cnt) {
1634 switch (op) {
1635 case SCSI_PROT_READ_INSERT:
acd6859b
JS
1636 case SCSI_PROT_READ_STRIP:
1637 case SCSI_PROT_READ_PASS:
1638 /*
1639 * For READ_STRIP and READ_PASS, force the
1640 * error on data being read off the wire. It
1641 * should force an IO error to the driver.
1642 */
f9bb2da1
JS
1643 *reftag = 0xDEADBEEF;
1644 phba->lpfc_injerr_rref_cnt--;
4ac9b226
JS
1645 if (phba->lpfc_injerr_rref_cnt == 0) {
1646 phba->lpfc_injerr_nportid = 0;
1647 phba->lpfc_injerr_lba =
1648 LPFC_INJERR_LBA_OFF;
1649 memset(&phba->lpfc_injerr_wwpn,
1650 0, sizeof(struct lpfc_name));
1651 }
acd6859b 1652 rc = BG_ERR_INIT;
f9bb2da1
JS
1653
1654 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
acd6859b 1655 "9079 BLKGRD: Injecting reftag error: "
f9bb2da1 1656 "read lba x%lx\n", (unsigned long)lba);
acd6859b 1657 break;
f9bb2da1
JS
1658 }
1659 }
1660 }
1661
1662 /* Should we change the Application Tag */
1663 if (apptag) {
acd6859b
JS
1664 if (phba->lpfc_injerr_wapp_cnt) {
1665 switch (op) {
1666 case SCSI_PROT_WRITE_PASS:
4ac9b226 1667 if (src) {
9a6b09c0
JS
1668 /*
1669 * For WRITE_PASS, force the error
1670 * to be sent on the wire. It should
1671 * be detected by the Target.
1672 * If blockoff != 0 error will be
1673 * inserted in middle of the IO.
1674 */
1675
acd6859b
JS
1676 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1677 "9080 BLKGRD: Injecting apptag error: "
1678 "write lba x%lx + x%x oldappTag x%x\n",
1679 (unsigned long)lba, blockoff,
9a6b09c0 1680 be16_to_cpu(src->app_tag));
acd6859b
JS
1681
1682 /*
9a6b09c0
JS
1683 * Save the old app_tag so we can
1684 * restore it on completion.
acd6859b 1685 */
9a6b09c0
JS
1686 if (lpfc_cmd) {
1687 lpfc_cmd->prot_data_type =
1688 LPFC_INJERR_APPTAG;
1689 lpfc_cmd->prot_data_segment =
1690 src;
1691 lpfc_cmd->prot_data =
1692 src->app_tag;
1693 }
1694 src->app_tag = cpu_to_be16(0xDEAD);
acd6859b 1695 phba->lpfc_injerr_wapp_cnt--;
4ac9b226
JS
1696 if (phba->lpfc_injerr_wapp_cnt == 0) {
1697 phba->lpfc_injerr_nportid = 0;
1698 phba->lpfc_injerr_lba =
1699 LPFC_INJERR_LBA_OFF;
1700 memset(&phba->lpfc_injerr_wwpn,
1701 0, sizeof(struct lpfc_name));
1702 }
9a6b09c0 1703 rc = BG_ERR_TGT | BG_ERR_CHECK;
acd6859b
JS
1704 break;
1705 }
1706 /* Drop thru */
9a6b09c0 1707 case SCSI_PROT_WRITE_INSERT:
acd6859b 1708 /*
9a6b09c0
JS
1709 * For WRITE_INSERT, force the
1710 * error to be sent on the wire. It should be
1711 * detected by the Target.
acd6859b 1712 */
9a6b09c0 1713 /* DEAD will be the apptag on the wire */
acd6859b
JS
1714 *apptag = 0xDEAD;
1715 phba->lpfc_injerr_wapp_cnt--;
4ac9b226
JS
1716 if (phba->lpfc_injerr_wapp_cnt == 0) {
1717 phba->lpfc_injerr_nportid = 0;
1718 phba->lpfc_injerr_lba =
1719 LPFC_INJERR_LBA_OFF;
1720 memset(&phba->lpfc_injerr_wwpn,
1721 0, sizeof(struct lpfc_name));
1722 }
9a6b09c0 1723 rc = BG_ERR_TGT | BG_ERR_CHECK;
f9bb2da1 1724
acd6859b 1725 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
9a6b09c0 1726 "0813 BLKGRD: Injecting apptag error: "
acd6859b
JS
1727 "write lba x%lx\n", (unsigned long)lba);
1728 break;
9a6b09c0 1729 case SCSI_PROT_WRITE_STRIP:
acd6859b 1730 /*
9a6b09c0
JS
1731 * For WRITE_STRIP and WRITE_PASS,
1732 * force the error on data
1733 * being copied from SLI-Host to SLI-Port.
acd6859b 1734 */
f9bb2da1
JS
1735 *apptag = 0xDEAD;
1736 phba->lpfc_injerr_wapp_cnt--;
4ac9b226
JS
1737 if (phba->lpfc_injerr_wapp_cnt == 0) {
1738 phba->lpfc_injerr_nportid = 0;
1739 phba->lpfc_injerr_lba =
1740 LPFC_INJERR_LBA_OFF;
1741 memset(&phba->lpfc_injerr_wwpn,
1742 0, sizeof(struct lpfc_name));
1743 }
9a6b09c0 1744 rc = BG_ERR_INIT;
f9bb2da1
JS
1745
1746 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
9a6b09c0 1747 "0812 BLKGRD: Injecting apptag error: "
f9bb2da1 1748 "write lba x%lx\n", (unsigned long)lba);
acd6859b 1749 break;
f9bb2da1 1750 }
acd6859b
JS
1751 }
1752 if (phba->lpfc_injerr_rapp_cnt) {
1753 switch (op) {
1754 case SCSI_PROT_READ_INSERT:
acd6859b
JS
1755 case SCSI_PROT_READ_STRIP:
1756 case SCSI_PROT_READ_PASS:
1757 /*
1758 * For READ_STRIP and READ_PASS, force the
1759 * error on data being read off the wire. It
1760 * should force an IO error to the driver.
1761 */
f9bb2da1
JS
1762 *apptag = 0xDEAD;
1763 phba->lpfc_injerr_rapp_cnt--;
4ac9b226
JS
1764 if (phba->lpfc_injerr_rapp_cnt == 0) {
1765 phba->lpfc_injerr_nportid = 0;
1766 phba->lpfc_injerr_lba =
1767 LPFC_INJERR_LBA_OFF;
1768 memset(&phba->lpfc_injerr_wwpn,
1769 0, sizeof(struct lpfc_name));
1770 }
acd6859b 1771 rc = BG_ERR_INIT;
f9bb2da1
JS
1772
1773 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
acd6859b 1774 "0814 BLKGRD: Injecting apptag error: "
f9bb2da1 1775 "read lba x%lx\n", (unsigned long)lba);
acd6859b 1776 break;
f9bb2da1
JS
1777 }
1778 }
1779 }
1780
acd6859b 1781
f9bb2da1 1782 /* Should we change the Guard Tag */
acd6859b
JS
1783 if (new_guard) {
1784 if (phba->lpfc_injerr_wgrd_cnt) {
1785 switch (op) {
1786 case SCSI_PROT_WRITE_PASS:
9a6b09c0 1787 rc = BG_ERR_CHECK;
acd6859b 1788 /* Drop thru */
9a6b09c0
JS
1789
1790 case SCSI_PROT_WRITE_INSERT:
acd6859b 1791 /*
9a6b09c0
JS
1792 * For WRITE_INSERT, force the
1793 * error to be sent on the wire. It should be
1794 * detected by the Target.
acd6859b
JS
1795 */
1796 phba->lpfc_injerr_wgrd_cnt--;
4ac9b226
JS
1797 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1798 phba->lpfc_injerr_nportid = 0;
1799 phba->lpfc_injerr_lba =
1800 LPFC_INJERR_LBA_OFF;
1801 memset(&phba->lpfc_injerr_wwpn,
1802 0, sizeof(struct lpfc_name));
1803 }
f9bb2da1 1804
9a6b09c0 1805 rc |= BG_ERR_TGT | BG_ERR_SWAP;
acd6859b 1806 /* Signals the caller to swap CRC->CSUM */
f9bb2da1 1807
acd6859b 1808 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
9a6b09c0 1809 "0817 BLKGRD: Injecting guard error: "
acd6859b
JS
1810 "write lba x%lx\n", (unsigned long)lba);
1811 break;
9a6b09c0 1812 case SCSI_PROT_WRITE_STRIP:
acd6859b 1813 /*
9a6b09c0
JS
1814 * For WRITE_STRIP and WRITE_PASS,
1815 * force the error on data
1816 * being copied from SLI-Host to SLI-Port.
acd6859b
JS
1817 */
1818 phba->lpfc_injerr_wgrd_cnt--;
4ac9b226
JS
1819 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1820 phba->lpfc_injerr_nportid = 0;
1821 phba->lpfc_injerr_lba =
1822 LPFC_INJERR_LBA_OFF;
1823 memset(&phba->lpfc_injerr_wwpn,
1824 0, sizeof(struct lpfc_name));
1825 }
f9bb2da1 1826
9a6b09c0 1827 rc = BG_ERR_INIT | BG_ERR_SWAP;
acd6859b
JS
1828 /* Signals the caller to swap CRC->CSUM */
1829
1830 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
9a6b09c0 1831 "0816 BLKGRD: Injecting guard error: "
acd6859b
JS
1832 "write lba x%lx\n", (unsigned long)lba);
1833 break;
1834 }
1835 }
1836 if (phba->lpfc_injerr_rgrd_cnt) {
1837 switch (op) {
1838 case SCSI_PROT_READ_INSERT:
acd6859b
JS
1839 case SCSI_PROT_READ_STRIP:
1840 case SCSI_PROT_READ_PASS:
1841 /*
1842 * For READ_STRIP and READ_PASS, force the
1843 * error on data being read off the wire. It
1844 * should force an IO error to the driver.
1845 */
acd6859b 1846 phba->lpfc_injerr_rgrd_cnt--;
4ac9b226
JS
1847 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1848 phba->lpfc_injerr_nportid = 0;
1849 phba->lpfc_injerr_lba =
1850 LPFC_INJERR_LBA_OFF;
1851 memset(&phba->lpfc_injerr_wwpn,
1852 0, sizeof(struct lpfc_name));
1853 }
acd6859b 1854
9a6b09c0 1855 rc = BG_ERR_INIT | BG_ERR_SWAP;
acd6859b
JS
1856 /* Signals the caller to swap CRC->CSUM */
1857
1858 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1859 "0818 BLKGRD: Injecting guard error: "
1860 "read lba x%lx\n", (unsigned long)lba);
1861 }
f9bb2da1
JS
1862 }
1863 }
acd6859b 1864
f9bb2da1
JS
1865 return rc;
1866}
1867#endif
1868
acd6859b
JS
1869/**
1870 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1871 * the specified SCSI command.
1872 * @phba: The Hba for which this call is being executed.
6c8eea54
JS
1873 * @sc: The SCSI command to examine
1874 * @txopt: (out) BlockGuard operation for transmitted data
1875 * @rxopt: (out) BlockGuard operation for received data
1876 *
1877 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1878 *
acd6859b 1879 **/
e2a0a9d6 1880static int
6c8eea54
JS
1881lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1882 uint8_t *txop, uint8_t *rxop)
e2a0a9d6 1883{
6c8eea54 1884 uint8_t ret = 0;
e2a0a9d6 1885
9c6aa9d7 1886 if (lpfc_cmd_guard_csum(sc)) {
e2a0a9d6
JS
1887 switch (scsi_get_prot_op(sc)) {
1888 case SCSI_PROT_READ_INSERT:
1889 case SCSI_PROT_WRITE_STRIP:
6c8eea54 1890 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
4ac9b226 1891 *txop = BG_OP_IN_CSUM_OUT_NODIF;
e2a0a9d6
JS
1892 break;
1893
1894 case SCSI_PROT_READ_STRIP:
1895 case SCSI_PROT_WRITE_INSERT:
6c8eea54 1896 *rxop = BG_OP_IN_CRC_OUT_NODIF;
4ac9b226 1897 *txop = BG_OP_IN_NODIF_OUT_CRC;
e2a0a9d6
JS
1898 break;
1899
c6af4042
MP
1900 case SCSI_PROT_READ_PASS:
1901 case SCSI_PROT_WRITE_PASS:
6c8eea54 1902 *rxop = BG_OP_IN_CRC_OUT_CSUM;
4ac9b226 1903 *txop = BG_OP_IN_CSUM_OUT_CRC;
e2a0a9d6
JS
1904 break;
1905
e2a0a9d6
JS
1906 case SCSI_PROT_NORMAL:
1907 default:
6a9c52cf 1908 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7c56b9fd
JS
1909 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1910 scsi_get_prot_op(sc));
6c8eea54 1911 ret = 1;
e2a0a9d6
JS
1912 break;
1913
1914 }
7c56b9fd 1915 } else {
e2a0a9d6
JS
1916 switch (scsi_get_prot_op(sc)) {
1917 case SCSI_PROT_READ_STRIP:
1918 case SCSI_PROT_WRITE_INSERT:
6c8eea54 1919 *rxop = BG_OP_IN_CRC_OUT_NODIF;
4ac9b226 1920 *txop = BG_OP_IN_NODIF_OUT_CRC;
e2a0a9d6
JS
1921 break;
1922
1923 case SCSI_PROT_READ_PASS:
1924 case SCSI_PROT_WRITE_PASS:
6c8eea54 1925 *rxop = BG_OP_IN_CRC_OUT_CRC;
4ac9b226 1926 *txop = BG_OP_IN_CRC_OUT_CRC;
e2a0a9d6
JS
1927 break;
1928
e2a0a9d6
JS
1929 case SCSI_PROT_READ_INSERT:
1930 case SCSI_PROT_WRITE_STRIP:
7c56b9fd 1931 *rxop = BG_OP_IN_NODIF_OUT_CRC;
4ac9b226 1932 *txop = BG_OP_IN_CRC_OUT_NODIF;
7c56b9fd
JS
1933 break;
1934
e2a0a9d6
JS
1935 case SCSI_PROT_NORMAL:
1936 default:
6a9c52cf 1937 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7c56b9fd
JS
1938 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1939 scsi_get_prot_op(sc));
6c8eea54 1940 ret = 1;
e2a0a9d6
JS
1941 break;
1942 }
e2a0a9d6
JS
1943 }
1944
6c8eea54 1945 return ret;
e2a0a9d6
JS
1946}
1947
acd6859b
JS
1948#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1949/**
1950 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1951 * the specified SCSI command in order to force a guard tag error.
1952 * @phba: The Hba for which this call is being executed.
1953 * @sc: The SCSI command to examine
1954 * @txopt: (out) BlockGuard operation for transmitted data
1955 * @rxopt: (out) BlockGuard operation for received data
1956 *
1957 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1958 *
1959 **/
1960static int
1961lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1962 uint8_t *txop, uint8_t *rxop)
1963{
acd6859b
JS
1964 uint8_t ret = 0;
1965
9c6aa9d7 1966 if (lpfc_cmd_guard_csum(sc)) {
acd6859b
JS
1967 switch (scsi_get_prot_op(sc)) {
1968 case SCSI_PROT_READ_INSERT:
1969 case SCSI_PROT_WRITE_STRIP:
acd6859b 1970 *rxop = BG_OP_IN_NODIF_OUT_CRC;
4ac9b226 1971 *txop = BG_OP_IN_CRC_OUT_NODIF;
acd6859b
JS
1972 break;
1973
1974 case SCSI_PROT_READ_STRIP:
1975 case SCSI_PROT_WRITE_INSERT:
acd6859b 1976 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
4ac9b226 1977 *txop = BG_OP_IN_NODIF_OUT_CSUM;
acd6859b
JS
1978 break;
1979
1980 case SCSI_PROT_READ_PASS:
1981 case SCSI_PROT_WRITE_PASS:
4ac9b226 1982 *rxop = BG_OP_IN_CSUM_OUT_CRC;
9a6b09c0 1983 *txop = BG_OP_IN_CRC_OUT_CSUM;
acd6859b
JS
1984 break;
1985
1986 case SCSI_PROT_NORMAL:
1987 default:
1988 break;
1989
1990 }
1991 } else {
1992 switch (scsi_get_prot_op(sc)) {
1993 case SCSI_PROT_READ_STRIP:
1994 case SCSI_PROT_WRITE_INSERT:
acd6859b 1995 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
4ac9b226 1996 *txop = BG_OP_IN_NODIF_OUT_CSUM;
acd6859b
JS
1997 break;
1998
1999 case SCSI_PROT_READ_PASS:
2000 case SCSI_PROT_WRITE_PASS:
4ac9b226 2001 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
9a6b09c0 2002 *txop = BG_OP_IN_CSUM_OUT_CSUM;
acd6859b
JS
2003 break;
2004
2005 case SCSI_PROT_READ_INSERT:
2006 case SCSI_PROT_WRITE_STRIP:
acd6859b 2007 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
4ac9b226 2008 *txop = BG_OP_IN_CSUM_OUT_NODIF;
acd6859b
JS
2009 break;
2010
2011 case SCSI_PROT_NORMAL:
2012 default:
2013 break;
2014 }
2015 }
2016
2017 return ret;
2018}
2019#endif
2020
2021/**
2022 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
2023 * @phba: The Hba for which this call is being executed.
2024 * @sc: pointer to scsi command we're working on
2025 * @bpl: pointer to buffer list for protection groups
2026 * @datacnt: number of segments of data that have been dma mapped
2027 *
2028 * This function sets up BPL buffer list for protection groups of
e2a0a9d6
JS
2029 * type LPFC_PG_TYPE_NO_DIF
2030 *
2031 * This is usually used when the HBA is instructed to generate
2032 * DIFs and insert them into data stream (or strip DIF from
2033 * incoming data stream)
2034 *
2035 * The buffer list consists of just one protection group described
2036 * below:
2037 * +-------------------------+
6c8eea54
JS
2038 * start of prot group --> | PDE_5 |
2039 * +-------------------------+
2040 * | PDE_6 |
e2a0a9d6
JS
2041 * +-------------------------+
2042 * | Data BDE |
2043 * +-------------------------+
2044 * |more Data BDE's ... (opt)|
2045 * +-------------------------+
2046 *
e2a0a9d6
JS
2047 *
2048 * Note: Data s/g buffers have been dma mapped
acd6859b
JS
2049 *
2050 * Returns the number of BDEs added to the BPL.
2051 **/
e2a0a9d6
JS
2052static int
2053lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2054 struct ulp_bde64 *bpl, int datasegcnt)
2055{
2056 struct scatterlist *sgde = NULL; /* s/g data entry */
6c8eea54
JS
2057 struct lpfc_pde5 *pde5 = NULL;
2058 struct lpfc_pde6 *pde6 = NULL;
e2a0a9d6 2059 dma_addr_t physaddr;
6c8eea54 2060 int i = 0, num_bde = 0, status;
e2a0a9d6 2061 int datadir = sc->sc_data_direction;
0829a19a 2062#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
acd6859b 2063 uint32_t rc;
0829a19a 2064#endif
acd6859b 2065 uint32_t checking = 1;
e2a0a9d6 2066 uint32_t reftag;
7c56b9fd 2067 unsigned blksize;
6c8eea54 2068 uint8_t txop, rxop;
e2a0a9d6 2069
6c8eea54
JS
2070 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2071 if (status)
e2a0a9d6
JS
2072 goto out;
2073
6c8eea54 2074 /* extract some info from the scsi command for pde*/
e2a0a9d6 2075 blksize = lpfc_cmd_blksize(sc);
acd6859b 2076 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
e2a0a9d6 2077
f9bb2da1 2078#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4ac9b226 2079 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
acd6859b 2080 if (rc) {
9a6b09c0 2081 if (rc & BG_ERR_SWAP)
acd6859b 2082 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
9a6b09c0 2083 if (rc & BG_ERR_CHECK)
acd6859b
JS
2084 checking = 0;
2085 }
f9bb2da1
JS
2086#endif
2087
6c8eea54
JS
2088 /* setup PDE5 with what we have */
2089 pde5 = (struct lpfc_pde5 *) bpl;
2090 memset(pde5, 0, sizeof(struct lpfc_pde5));
2091 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
6c8eea54 2092
bc73905a 2093 /* Endianness conversion if necessary for PDE5 */
589a52d6 2094 pde5->word0 = cpu_to_le32(pde5->word0);
7c56b9fd 2095 pde5->reftag = cpu_to_le32(reftag);
589a52d6 2096
6c8eea54
JS
2097 /* advance bpl and increment bde count */
2098 num_bde++;
2099 bpl++;
2100 pde6 = (struct lpfc_pde6 *) bpl;
2101
2102 /* setup PDE6 with the rest of the info */
2103 memset(pde6, 0, sizeof(struct lpfc_pde6));
2104 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2105 bf_set(pde6_optx, pde6, txop);
2106 bf_set(pde6_oprx, pde6, rxop);
a6887e28
JS
2107
2108 /*
2109 * We only need to check the data on READs, for WRITEs
2110 * protection data is automatically generated, not checked.
2111 */
6c8eea54 2112 if (datadir == DMA_FROM_DEVICE) {
9c6aa9d7 2113 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
a6887e28
JS
2114 bf_set(pde6_ce, pde6, checking);
2115 else
2116 bf_set(pde6_ce, pde6, 0);
2117
9c6aa9d7 2118 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
a6887e28
JS
2119 bf_set(pde6_re, pde6, checking);
2120 else
2121 bf_set(pde6_re, pde6, 0);
6c8eea54
JS
2122 }
2123 bf_set(pde6_ai, pde6, 1);
7c56b9fd
JS
2124 bf_set(pde6_ae, pde6, 0);
2125 bf_set(pde6_apptagval, pde6, 0);
e2a0a9d6 2126
bc73905a 2127 /* Endianness conversion if necessary for PDE6 */
589a52d6
JS
2128 pde6->word0 = cpu_to_le32(pde6->word0);
2129 pde6->word1 = cpu_to_le32(pde6->word1);
2130 pde6->word2 = cpu_to_le32(pde6->word2);
2131
6c8eea54 2132 /* advance bpl and increment bde count */
e2a0a9d6
JS
2133 num_bde++;
2134 bpl++;
2135
2136 /* assumption: caller has already run dma_map_sg on command data */
2137 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2138 physaddr = sg_dma_address(sgde);
2139 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2140 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2141 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2142 if (datadir == DMA_TO_DEVICE)
2143 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2144 else
2145 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2146 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2147 bpl++;
2148 num_bde++;
2149 }
2150
2151out:
2152 return num_bde;
2153}
2154
acd6859b
JS
2155/**
2156 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2157 * @phba: The Hba for which this call is being executed.
2158 * @sc: pointer to scsi command we're working on
2159 * @bpl: pointer to buffer list for protection groups
2160 * @datacnt: number of segments of data that have been dma mapped
2161 * @protcnt: number of segment of protection data that have been dma mapped
2162 *
2163 * This function sets up BPL buffer list for protection groups of
2164 * type LPFC_PG_TYPE_DIF
e2a0a9d6
JS
2165 *
2166 * This is usually used when DIFs are in their own buffers,
2167 * separate from the data. The HBA can then by instructed
2168 * to place the DIFs in the outgoing stream. For read operations,
2169 * The HBA could extract the DIFs and place it in DIF buffers.
2170 *
2171 * The buffer list for this type consists of one or more of the
2172 * protection groups described below:
2173 * +-------------------------+
6c8eea54 2174 * start of first prot group --> | PDE_5 |
e2a0a9d6 2175 * +-------------------------+
6c8eea54
JS
2176 * | PDE_6 |
2177 * +-------------------------+
2178 * | PDE_7 (Prot BDE) |
e2a0a9d6
JS
2179 * +-------------------------+
2180 * | Data BDE |
2181 * +-------------------------+
2182 * |more Data BDE's ... (opt)|
2183 * +-------------------------+
6c8eea54 2184 * start of new prot group --> | PDE_5 |
e2a0a9d6
JS
2185 * +-------------------------+
2186 * | ... |
2187 * +-------------------------+
2188 *
e2a0a9d6
JS
2189 * Note: It is assumed that both data and protection s/g buffers have been
2190 * mapped for DMA
acd6859b
JS
2191 *
2192 * Returns the number of BDEs added to the BPL.
2193 **/
e2a0a9d6
JS
2194static int
2195lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2196 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2197{
2198 struct scatterlist *sgde = NULL; /* s/g data entry */
2199 struct scatterlist *sgpe = NULL; /* s/g prot entry */
6c8eea54
JS
2200 struct lpfc_pde5 *pde5 = NULL;
2201 struct lpfc_pde6 *pde6 = NULL;
7f86059a 2202 struct lpfc_pde7 *pde7 = NULL;
e2a0a9d6
JS
2203 dma_addr_t dataphysaddr, protphysaddr;
2204 unsigned short curr_data = 0, curr_prot = 0;
7f86059a
JS
2205 unsigned int split_offset;
2206 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
e2a0a9d6
JS
2207 unsigned int protgrp_blks, protgrp_bytes;
2208 unsigned int remainder, subtotal;
6c8eea54 2209 int status;
e2a0a9d6
JS
2210 int datadir = sc->sc_data_direction;
2211 unsigned char pgdone = 0, alldone = 0;
2212 unsigned blksize;
0829a19a 2213#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
acd6859b 2214 uint32_t rc;
0829a19a 2215#endif
acd6859b 2216 uint32_t checking = 1;
e2a0a9d6 2217 uint32_t reftag;
6c8eea54 2218 uint8_t txop, rxop;
e2a0a9d6
JS
2219 int num_bde = 0;
2220
2221 sgpe = scsi_prot_sglist(sc);
2222 sgde = scsi_sglist(sc);
2223
2224 if (!sgpe || !sgde) {
2225 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
acd6859b
JS
2226 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2227 sgpe, sgde);
2228 return 0;
2229 }
2230
2231 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2232 if (status)
2233 goto out;
2234
2235 /* extract some info from the scsi command */
2236 blksize = lpfc_cmd_blksize(sc);
2237 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2238
2239#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4ac9b226 2240 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
acd6859b 2241 if (rc) {
9a6b09c0 2242 if (rc & BG_ERR_SWAP)
acd6859b 2243 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
9a6b09c0 2244 if (rc & BG_ERR_CHECK)
acd6859b
JS
2245 checking = 0;
2246 }
2247#endif
2248
2249 split_offset = 0;
2250 do {
96f7077f
JS
2251 /* Check to see if we ran out of space */
2252 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2253 return num_bde + 3;
2254
acd6859b
JS
2255 /* setup PDE5 with what we have */
2256 pde5 = (struct lpfc_pde5 *) bpl;
2257 memset(pde5, 0, sizeof(struct lpfc_pde5));
2258 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2259
2260 /* Endianness conversion if necessary for PDE5 */
2261 pde5->word0 = cpu_to_le32(pde5->word0);
2262 pde5->reftag = cpu_to_le32(reftag);
2263
2264 /* advance bpl and increment bde count */
2265 num_bde++;
2266 bpl++;
2267 pde6 = (struct lpfc_pde6 *) bpl;
2268
2269 /* setup PDE6 with the rest of the info */
2270 memset(pde6, 0, sizeof(struct lpfc_pde6));
2271 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2272 bf_set(pde6_optx, pde6, txop);
2273 bf_set(pde6_oprx, pde6, rxop);
a6887e28 2274
9c6aa9d7 2275 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
a6887e28
JS
2276 bf_set(pde6_ce, pde6, checking);
2277 else
2278 bf_set(pde6_ce, pde6, 0);
2279
9c6aa9d7 2280 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
a6887e28
JS
2281 bf_set(pde6_re, pde6, checking);
2282 else
2283 bf_set(pde6_re, pde6, 0);
2284
acd6859b
JS
2285 bf_set(pde6_ai, pde6, 1);
2286 bf_set(pde6_ae, pde6, 0);
2287 bf_set(pde6_apptagval, pde6, 0);
2288
2289 /* Endianness conversion if necessary for PDE6 */
2290 pde6->word0 = cpu_to_le32(pde6->word0);
2291 pde6->word1 = cpu_to_le32(pde6->word1);
2292 pde6->word2 = cpu_to_le32(pde6->word2);
2293
2294 /* advance bpl and increment bde count */
2295 num_bde++;
2296 bpl++;
2297
2298 /* setup the first BDE that points to protection buffer */
2299 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2300 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2301
2302 /* must be integer multiple of the DIF block length */
2303 BUG_ON(protgroup_len % 8);
2304
2305 pde7 = (struct lpfc_pde7 *) bpl;
2306 memset(pde7, 0, sizeof(struct lpfc_pde7));
2307 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2308
2309 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2310 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
2311
2312 protgrp_blks = protgroup_len / 8;
2313 protgrp_bytes = protgrp_blks * blksize;
2314
2315 /* check if this pde is crossing the 4K boundary; if so split */
2316 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2317 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2318 protgroup_offset += protgroup_remainder;
2319 protgrp_blks = protgroup_remainder / 8;
2320 protgrp_bytes = protgrp_blks * blksize;
2321 } else {
2322 protgroup_offset = 0;
2323 curr_prot++;
2324 }
2325
2326 num_bde++;
2327
2328 /* setup BDE's for data blocks associated with DIF data */
2329 pgdone = 0;
2330 subtotal = 0; /* total bytes processed for current prot grp */
2331 while (!pgdone) {
96f7077f
JS
2332 /* Check to see if we ran out of space */
2333 if (num_bde >= phba->cfg_total_seg_cnt)
2334 return num_bde + 1;
2335
acd6859b
JS
2336 if (!sgde) {
2337 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2338 "9065 BLKGRD:%s Invalid data segment\n",
2339 __func__);
2340 return 0;
2341 }
2342 bpl++;
2343 dataphysaddr = sg_dma_address(sgde) + split_offset;
2344 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2345 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2346
2347 remainder = sg_dma_len(sgde) - split_offset;
2348
2349 if ((subtotal + remainder) <= protgrp_bytes) {
2350 /* we can use this whole buffer */
2351 bpl->tus.f.bdeSize = remainder;
2352 split_offset = 0;
2353
2354 if ((subtotal + remainder) == protgrp_bytes)
2355 pgdone = 1;
2356 } else {
2357 /* must split this buffer with next prot grp */
2358 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2359 split_offset += bpl->tus.f.bdeSize;
2360 }
2361
2362 subtotal += bpl->tus.f.bdeSize;
2363
2364 if (datadir == DMA_TO_DEVICE)
2365 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2366 else
2367 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2368 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2369
2370 num_bde++;
2371 curr_data++;
2372
2373 if (split_offset)
2374 break;
2375
2376 /* Move to the next s/g segment if possible */
2377 sgde = sg_next(sgde);
2378
2379 }
2380
2381 if (protgroup_offset) {
2382 /* update the reference tag */
2383 reftag += protgrp_blks;
2384 bpl++;
2385 continue;
2386 }
2387
2388 /* are we done ? */
2389 if (curr_prot == protcnt) {
2390 alldone = 1;
2391 } else if (curr_prot < protcnt) {
2392 /* advance to next prot buffer */
2393 sgpe = sg_next(sgpe);
2394 bpl++;
2395
2396 /* update the reference tag */
2397 reftag += protgrp_blks;
2398 } else {
2399 /* if we're here, we have a bug */
2400 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2401 "9054 BLKGRD: bug in %s\n", __func__);
2402 }
2403
2404 } while (!alldone);
2405out:
2406
2407 return num_bde;
2408}
2409
2410/**
2411 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2412 * @phba: The Hba for which this call is being executed.
2413 * @sc: pointer to scsi command we're working on
2414 * @sgl: pointer to buffer list for protection groups
2415 * @datacnt: number of segments of data that have been dma mapped
2416 *
2417 * This function sets up SGL buffer list for protection groups of
2418 * type LPFC_PG_TYPE_NO_DIF
2419 *
2420 * This is usually used when the HBA is instructed to generate
2421 * DIFs and insert them into data stream (or strip DIF from
2422 * incoming data stream)
2423 *
2424 * The buffer list consists of just one protection group described
2425 * below:
2426 * +-------------------------+
2427 * start of prot group --> | DI_SEED |
2428 * +-------------------------+
2429 * | Data SGE |
2430 * +-------------------------+
2431 * |more Data SGE's ... (opt)|
2432 * +-------------------------+
2433 *
2434 *
2435 * Note: Data s/g buffers have been dma mapped
2436 *
2437 * Returns the number of SGEs added to the SGL.
2438 **/
2439static int
2440lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2441 struct sli4_sge *sgl, int datasegcnt)
2442{
2443 struct scatterlist *sgde = NULL; /* s/g data entry */
2444 struct sli4_sge_diseed *diseed = NULL;
2445 dma_addr_t physaddr;
2446 int i = 0, num_sge = 0, status;
acd6859b
JS
2447 uint32_t reftag;
2448 unsigned blksize;
2449 uint8_t txop, rxop;
0829a19a 2450#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
acd6859b 2451 uint32_t rc;
0829a19a 2452#endif
acd6859b
JS
2453 uint32_t checking = 1;
2454 uint32_t dma_len;
2455 uint32_t dma_offset = 0;
2456
2457 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2458 if (status)
2459 goto out;
2460
2461 /* extract some info from the scsi command for pde*/
2462 blksize = lpfc_cmd_blksize(sc);
2463 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2464
2465#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4ac9b226 2466 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
acd6859b 2467 if (rc) {
9a6b09c0 2468 if (rc & BG_ERR_SWAP)
acd6859b 2469 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
9a6b09c0 2470 if (rc & BG_ERR_CHECK)
acd6859b
JS
2471 checking = 0;
2472 }
2473#endif
2474
2475 /* setup DISEED with what we have */
2476 diseed = (struct sli4_sge_diseed *) sgl;
2477 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2478 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2479
2480 /* Endianness conversion if necessary */
2481 diseed->ref_tag = cpu_to_le32(reftag);
2482 diseed->ref_tag_tran = diseed->ref_tag;
2483
a6887e28
JS
2484 /*
2485 * We only need to check the data on READs, for WRITEs
2486 * protection data is automatically generated, not checked.
2487 */
2488 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
9c6aa9d7 2489 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
a6887e28
JS
2490 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2491 else
2492 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2493
9c6aa9d7 2494 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
a6887e28
JS
2495 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2496 else
2497 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2498 }
2499
acd6859b
JS
2500 /* setup DISEED with the rest of the info */
2501 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2502 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
a6887e28 2503
acd6859b
JS
2504 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2505 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2506
2507 /* Endianness conversion if necessary for DISEED */
2508 diseed->word2 = cpu_to_le32(diseed->word2);
2509 diseed->word3 = cpu_to_le32(diseed->word3);
2510
2511 /* advance bpl and increment sge count */
2512 num_sge++;
2513 sgl++;
2514
2515 /* assumption: caller has already run dma_map_sg on command data */
2516 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2517 physaddr = sg_dma_address(sgde);
2518 dma_len = sg_dma_len(sgde);
2519 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2520 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2521 if ((i + 1) == datasegcnt)
2522 bf_set(lpfc_sli4_sge_last, sgl, 1);
2523 else
2524 bf_set(lpfc_sli4_sge_last, sgl, 0);
2525 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2526 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2527
2528 sgl->sge_len = cpu_to_le32(dma_len);
2529 dma_offset += dma_len;
2530
2531 sgl++;
2532 num_sge++;
2533 }
2534
2535out:
2536 return num_sge;
2537}
2538
2539/**
2540 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2541 * @phba: The Hba for which this call is being executed.
2542 * @sc: pointer to scsi command we're working on
2543 * @sgl: pointer to buffer list for protection groups
2544 * @datacnt: number of segments of data that have been dma mapped
2545 * @protcnt: number of segment of protection data that have been dma mapped
2546 *
2547 * This function sets up SGL buffer list for protection groups of
2548 * type LPFC_PG_TYPE_DIF
2549 *
2550 * This is usually used when DIFs are in their own buffers,
2551 * separate from the data. The HBA can then by instructed
2552 * to place the DIFs in the outgoing stream. For read operations,
2553 * The HBA could extract the DIFs and place it in DIF buffers.
2554 *
2555 * The buffer list for this type consists of one or more of the
2556 * protection groups described below:
2557 * +-------------------------+
2558 * start of first prot group --> | DISEED |
2559 * +-------------------------+
2560 * | DIF (Prot SGE) |
2561 * +-------------------------+
2562 * | Data SGE |
2563 * +-------------------------+
2564 * |more Data SGE's ... (opt)|
2565 * +-------------------------+
2566 * start of new prot group --> | DISEED |
2567 * +-------------------------+
2568 * | ... |
2569 * +-------------------------+
2570 *
2571 * Note: It is assumed that both data and protection s/g buffers have been
2572 * mapped for DMA
2573 *
2574 * Returns the number of SGEs added to the SGL.
2575 **/
2576static int
2577lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2578 struct sli4_sge *sgl, int datacnt, int protcnt)
2579{
2580 struct scatterlist *sgde = NULL; /* s/g data entry */
2581 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2582 struct sli4_sge_diseed *diseed = NULL;
2583 dma_addr_t dataphysaddr, protphysaddr;
2584 unsigned short curr_data = 0, curr_prot = 0;
2585 unsigned int split_offset;
2586 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2587 unsigned int protgrp_blks, protgrp_bytes;
2588 unsigned int remainder, subtotal;
2589 int status;
2590 unsigned char pgdone = 0, alldone = 0;
2591 unsigned blksize;
2592 uint32_t reftag;
2593 uint8_t txop, rxop;
2594 uint32_t dma_len;
0829a19a 2595#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
acd6859b 2596 uint32_t rc;
0829a19a 2597#endif
acd6859b
JS
2598 uint32_t checking = 1;
2599 uint32_t dma_offset = 0;
2600 int num_sge = 0;
2601
2602 sgpe = scsi_prot_sglist(sc);
2603 sgde = scsi_sglist(sc);
2604
2605 if (!sgpe || !sgde) {
2606 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2607 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
e2a0a9d6
JS
2608 sgpe, sgde);
2609 return 0;
2610 }
2611
6c8eea54
JS
2612 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2613 if (status)
e2a0a9d6
JS
2614 goto out;
2615
6c8eea54 2616 /* extract some info from the scsi command */
e2a0a9d6 2617 blksize = lpfc_cmd_blksize(sc);
acd6859b 2618 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
e2a0a9d6 2619
f9bb2da1 2620#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4ac9b226 2621 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
acd6859b 2622 if (rc) {
9a6b09c0 2623 if (rc & BG_ERR_SWAP)
acd6859b 2624 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
9a6b09c0 2625 if (rc & BG_ERR_CHECK)
acd6859b
JS
2626 checking = 0;
2627 }
f9bb2da1
JS
2628#endif
2629
e2a0a9d6
JS
2630 split_offset = 0;
2631 do {
96f7077f
JS
2632 /* Check to see if we ran out of space */
2633 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2634 return num_sge + 3;
2635
acd6859b
JS
2636 /* setup DISEED with what we have */
2637 diseed = (struct sli4_sge_diseed *) sgl;
2638 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2639 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2640
2641 /* Endianness conversion if necessary */
2642 diseed->ref_tag = cpu_to_le32(reftag);
2643 diseed->ref_tag_tran = diseed->ref_tag;
2644
9c6aa9d7 2645 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
a6887e28
JS
2646 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2647
2648 } else {
2649 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2650 /*
2651 * When in this mode, the hardware will replace
2652 * the guard tag from the host with a
2653 * newly generated good CRC for the wire.
2654 * Switch to raw mode here to avoid this
2655 * behavior. What the host sends gets put on the wire.
2656 */
2657 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2658 txop = BG_OP_RAW_MODE;
2659 rxop = BG_OP_RAW_MODE;
2660 }
2661 }
2662
2663
9c6aa9d7 2664 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
a6887e28
JS
2665 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2666 else
2667 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2668
acd6859b
JS
2669 /* setup DISEED with the rest of the info */
2670 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2671 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
a6887e28 2672
acd6859b
JS
2673 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2674 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2675
2676 /* Endianness conversion if necessary for DISEED */
2677 diseed->word2 = cpu_to_le32(diseed->word2);
2678 diseed->word3 = cpu_to_le32(diseed->word3);
2679
2680 /* advance sgl and increment bde count */
2681 num_sge++;
2682 sgl++;
e2a0a9d6
JS
2683
2684 /* setup the first BDE that points to protection buffer */
7f86059a
JS
2685 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2686 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
e2a0a9d6 2687
e2a0a9d6
JS
2688 /* must be integer multiple of the DIF block length */
2689 BUG_ON(protgroup_len % 8);
2690
acd6859b
JS
2691 /* Now setup DIF SGE */
2692 sgl->word2 = 0;
2693 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2694 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2695 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2696 sgl->word2 = cpu_to_le32(sgl->word2);
7f86059a 2697
e2a0a9d6
JS
2698 protgrp_blks = protgroup_len / 8;
2699 protgrp_bytes = protgrp_blks * blksize;
2700
acd6859b
JS
2701 /* check if DIF SGE is crossing the 4K boundary; if so split */
2702 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2703 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
7f86059a
JS
2704 protgroup_offset += protgroup_remainder;
2705 protgrp_blks = protgroup_remainder / 8;
7c56b9fd 2706 protgrp_bytes = protgrp_blks * blksize;
7f86059a
JS
2707 } else {
2708 protgroup_offset = 0;
2709 curr_prot++;
2710 }
e2a0a9d6 2711
acd6859b 2712 num_sge++;
e2a0a9d6 2713
acd6859b 2714 /* setup SGE's for data blocks associated with DIF data */
e2a0a9d6
JS
2715 pgdone = 0;
2716 subtotal = 0; /* total bytes processed for current prot grp */
2717 while (!pgdone) {
96f7077f
JS
2718 /* Check to see if we ran out of space */
2719 if (num_sge >= phba->cfg_total_seg_cnt)
2720 return num_sge + 1;
2721
e2a0a9d6 2722 if (!sgde) {
6a9c52cf 2723 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
acd6859b 2724 "9086 BLKGRD:%s Invalid data segment\n",
e2a0a9d6
JS
2725 __func__);
2726 return 0;
2727 }
acd6859b 2728 sgl++;
e2a0a9d6 2729 dataphysaddr = sg_dma_address(sgde) + split_offset;
e2a0a9d6
JS
2730
2731 remainder = sg_dma_len(sgde) - split_offset;
2732
2733 if ((subtotal + remainder) <= protgrp_bytes) {
2734 /* we can use this whole buffer */
acd6859b 2735 dma_len = remainder;
e2a0a9d6
JS
2736 split_offset = 0;
2737
2738 if ((subtotal + remainder) == protgrp_bytes)
2739 pgdone = 1;
2740 } else {
2741 /* must split this buffer with next prot grp */
acd6859b
JS
2742 dma_len = protgrp_bytes - subtotal;
2743 split_offset += dma_len;
e2a0a9d6
JS
2744 }
2745
acd6859b 2746 subtotal += dma_len;
e2a0a9d6 2747
acd6859b
JS
2748 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2749 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2750 bf_set(lpfc_sli4_sge_last, sgl, 0);
2751 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2752 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
e2a0a9d6 2753
acd6859b
JS
2754 sgl->sge_len = cpu_to_le32(dma_len);
2755 dma_offset += dma_len;
2756
2757 num_sge++;
e2a0a9d6
JS
2758 curr_data++;
2759
2760 if (split_offset)
2761 break;
2762
2763 /* Move to the next s/g segment if possible */
2764 sgde = sg_next(sgde);
2765 }
2766
7f86059a
JS
2767 if (protgroup_offset) {
2768 /* update the reference tag */
2769 reftag += protgrp_blks;
acd6859b 2770 sgl++;
7f86059a
JS
2771 continue;
2772 }
2773
e2a0a9d6
JS
2774 /* are we done ? */
2775 if (curr_prot == protcnt) {
acd6859b 2776 bf_set(lpfc_sli4_sge_last, sgl, 1);
e2a0a9d6
JS
2777 alldone = 1;
2778 } else if (curr_prot < protcnt) {
2779 /* advance to next prot buffer */
2780 sgpe = sg_next(sgpe);
acd6859b 2781 sgl++;
e2a0a9d6
JS
2782
2783 /* update the reference tag */
2784 reftag += protgrp_blks;
2785 } else {
2786 /* if we're here, we have a bug */
6a9c52cf 2787 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
acd6859b 2788 "9085 BLKGRD: bug in %s\n", __func__);
e2a0a9d6
JS
2789 }
2790
2791 } while (!alldone);
acd6859b 2792
e2a0a9d6
JS
2793out:
2794
acd6859b 2795 return num_sge;
e2a0a9d6 2796}
7f86059a 2797
acd6859b
JS
2798/**
2799 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2800 * @phba: The Hba for which this call is being executed.
2801 * @sc: pointer to scsi command we're working on
2802 *
e2a0a9d6
JS
2803 * Given a SCSI command that supports DIF, determine composition of protection
2804 * groups involved in setting up buffer lists
2805 *
acd6859b
JS
2806 * Returns: Protection group type (with or without DIF)
2807 *
2808 **/
e2a0a9d6
JS
2809static int
2810lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2811{
2812 int ret = LPFC_PG_TYPE_INVALID;
2813 unsigned char op = scsi_get_prot_op(sc);
2814
2815 switch (op) {
2816 case SCSI_PROT_READ_STRIP:
2817 case SCSI_PROT_WRITE_INSERT:
2818 ret = LPFC_PG_TYPE_NO_DIF;
2819 break;
2820 case SCSI_PROT_READ_INSERT:
2821 case SCSI_PROT_WRITE_STRIP:
2822 case SCSI_PROT_READ_PASS:
2823 case SCSI_PROT_WRITE_PASS:
e2a0a9d6
JS
2824 ret = LPFC_PG_TYPE_DIF_BUF;
2825 break;
2826 default:
9c6aa9d7
JS
2827 if (phba)
2828 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2829 "9021 Unsupported protection op:%d\n",
2830 op);
e2a0a9d6
JS
2831 break;
2832 }
e2a0a9d6
JS
2833 return ret;
2834}
2835
a6887e28
JS
2836/**
2837 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2838 * @phba: The Hba for which this call is being executed.
2839 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2840 *
2841 * Adjust the data length to account for how much data
2842 * is actually on the wire.
2843 *
2844 * returns the adjusted data length
2845 **/
2846static int
2847lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2848 struct lpfc_scsi_buf *lpfc_cmd)
2849{
2850 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2851 int fcpdl;
2852
2853 fcpdl = scsi_bufflen(sc);
2854
2855 /* Check if there is protection data on the wire */
2856 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
9c6aa9d7 2857 /* Read check for protection data */
a6887e28
JS
2858 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2859 return fcpdl;
2860
2861 } else {
9c6aa9d7 2862 /* Write check for protection data */
a6887e28
JS
2863 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2864 return fcpdl;
2865 }
2866
2867 /*
2868 * If we are in DIF Type 1 mode every data block has a 8 byte
9c6aa9d7
JS
2869 * DIF (trailer) attached to it. Must ajust FCP data length
2870 * to account for the protection data.
a6887e28 2871 */
9c6aa9d7 2872 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
a6887e28
JS
2873
2874 return fcpdl;
2875}
2876
acd6859b
JS
2877/**
2878 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2879 * @phba: The Hba for which this call is being executed.
2880 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2881 *
e2a0a9d6
JS
2882 * This is the protection/DIF aware version of
2883 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2884 * two functions eventually, but for now, it's here
acd6859b 2885 **/
e2a0a9d6 2886static int
acd6859b 2887lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
e2a0a9d6
JS
2888 struct lpfc_scsi_buf *lpfc_cmd)
2889{
2890 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2891 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2892 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2893 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2894 uint32_t num_bde = 0;
2895 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2896 int prot_group_type = 0;
a6887e28 2897 int fcpdl;
e2a0a9d6
JS
2898
2899 /*
2900 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2901 * fcp_rsp regions to the first data bde entry
2902 */
2903 bpl += 2;
2904 if (scsi_sg_count(scsi_cmnd)) {
2905 /*
2906 * The driver stores the segment count returned from pci_map_sg
2907 * because this a count of dma-mappings used to map the use_sg
2908 * pages. They are not guaranteed to be the same for those
2909 * architectures that implement an IOMMU.
2910 */
2911 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2912 scsi_sglist(scsi_cmnd),
2913 scsi_sg_count(scsi_cmnd), datadir);
2914 if (unlikely(!datasegcnt))
2915 return 1;
2916
2917 lpfc_cmd->seg_cnt = datasegcnt;
96f7077f
JS
2918
2919 /* First check if data segment count from SCSI Layer is good */
2920 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2921 goto err;
e2a0a9d6
JS
2922
2923 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2924
2925 switch (prot_group_type) {
2926 case LPFC_PG_TYPE_NO_DIF:
96f7077f
JS
2927
2928 /* Here we need to add a PDE5 and PDE6 to the count */
2929 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2930 goto err;
2931
e2a0a9d6
JS
2932 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2933 datasegcnt);
c9404c9c 2934 /* we should have 2 or more entries in buffer list */
e2a0a9d6
JS
2935 if (num_bde < 2)
2936 goto err;
2937 break;
96f7077f
JS
2938
2939 case LPFC_PG_TYPE_DIF_BUF:
e2a0a9d6
JS
2940 /*
2941 * This type indicates that protection buffers are
2942 * passed to the driver, so that needs to be prepared
2943 * for DMA
2944 */
2945 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2946 scsi_prot_sglist(scsi_cmnd),
2947 scsi_prot_sg_count(scsi_cmnd), datadir);
2948 if (unlikely(!protsegcnt)) {
2949 scsi_dma_unmap(scsi_cmnd);
2950 return 1;
2951 }
2952
2953 lpfc_cmd->prot_seg_cnt = protsegcnt;
96f7077f
JS
2954
2955 /*
2956 * There is a minimun of 4 BPLs used for every
2957 * protection data segment.
2958 */
2959 if ((lpfc_cmd->prot_seg_cnt * 4) >
2960 (phba->cfg_total_seg_cnt - 2))
2961 goto err;
e2a0a9d6
JS
2962
2963 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2964 datasegcnt, protsegcnt);
c9404c9c 2965 /* we should have 3 or more entries in buffer list */
96f7077f
JS
2966 if ((num_bde < 3) ||
2967 (num_bde > phba->cfg_total_seg_cnt))
e2a0a9d6
JS
2968 goto err;
2969 break;
96f7077f 2970
e2a0a9d6
JS
2971 case LPFC_PG_TYPE_INVALID:
2972 default:
96f7077f
JS
2973 scsi_dma_unmap(scsi_cmnd);
2974 lpfc_cmd->seg_cnt = 0;
2975
e2a0a9d6
JS
2976 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2977 "9022 Unexpected protection group %i\n",
2978 prot_group_type);
2979 return 1;
2980 }
2981 }
2982
2983 /*
2984 * Finish initializing those IOCB fields that are dependent on the
2985 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2986 * reinitialized since all iocb memory resources are used many times
2987 * for transmit, receive, and continuation bpl's.
2988 */
2989 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2990 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2991 iocb_cmd->ulpBdeCount = 1;
2992 iocb_cmd->ulpLe = 1;
2993
a6887e28 2994 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
e2a0a9d6
JS
2995 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2996
2997 /*
2998 * Due to difference in data length between DIF/non-DIF paths,
2999 * we need to set word 4 of IOCB here
3000 */
3001 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3002
dea3101e 3003 return 0;
e2a0a9d6 3004err:
96f7077f
JS
3005 if (lpfc_cmd->seg_cnt)
3006 scsi_dma_unmap(scsi_cmnd);
3007 if (lpfc_cmd->prot_seg_cnt)
3008 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3009 scsi_prot_sg_count(scsi_cmnd),
3010 scsi_cmnd->sc_data_direction);
3011
e2a0a9d6 3012 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
96f7077f
JS
3013 "9023 Cannot setup S/G List for HBA"
3014 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
3015 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3016 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
e2a0a9d6 3017 prot_group_type, num_bde);
96f7077f
JS
3018
3019 lpfc_cmd->seg_cnt = 0;
3020 lpfc_cmd->prot_seg_cnt = 0;
e2a0a9d6
JS
3021 return 1;
3022}
3023
737d4248
JS
3024/*
3025 * This function calcuates the T10 DIF guard tag
3026 * on the specified data using a CRC algorithmn
3027 * using crc_t10dif.
3028 */
3029uint16_t
3030lpfc_bg_crc(uint8_t *data, int count)
3031{
3032 uint16_t crc = 0;
3033 uint16_t x;
3034
3035 crc = crc_t10dif(data, count);
3036 x = cpu_to_be16(crc);
3037 return x;
3038}
3039
3040/*
3041 * This function calcuates the T10 DIF guard tag
3042 * on the specified data using a CSUM algorithmn
3043 * using ip_compute_csum.
3044 */
3045uint16_t
3046lpfc_bg_csum(uint8_t *data, int count)
3047{
3048 uint16_t ret;
3049
3050 ret = ip_compute_csum(data, count);
3051 return ret;
3052}
3053
3054/*
3055 * This function examines the protection data to try to determine
3056 * what type of T10-DIF error occurred.
3057 */
3058void
3059lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3060{
3061 struct scatterlist *sgpe; /* s/g prot entry */
3062 struct scatterlist *sgde; /* s/g data entry */
3063 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3064 struct scsi_dif_tuple *src = NULL;
3065 uint8_t *data_src = NULL;
3066 uint16_t guard_tag, guard_type;
3067 uint16_t start_app_tag, app_tag;
3068 uint32_t start_ref_tag, ref_tag;
3069 int prot, protsegcnt;
3070 int err_type, len, data_len;
3071 int chk_ref, chk_app, chk_guard;
3072 uint16_t sum;
3073 unsigned blksize;
3074
3075 err_type = BGS_GUARD_ERR_MASK;
3076 sum = 0;
3077 guard_tag = 0;
3078
3079 /* First check to see if there is protection data to examine */
3080 prot = scsi_get_prot_op(cmd);
3081 if ((prot == SCSI_PROT_READ_STRIP) ||
3082 (prot == SCSI_PROT_WRITE_INSERT) ||
3083 (prot == SCSI_PROT_NORMAL))
3084 goto out;
3085
3086 /* Currently the driver just supports ref_tag and guard_tag checking */
3087 chk_ref = 1;
3088 chk_app = 0;
3089 chk_guard = 0;
3090
3091 /* Setup a ptr to the protection data provided by the SCSI host */
3092 sgpe = scsi_prot_sglist(cmd);
3093 protsegcnt = lpfc_cmd->prot_seg_cnt;
3094
3095 if (sgpe && protsegcnt) {
3096
3097 /*
3098 * We will only try to verify guard tag if the segment
3099 * data length is a multiple of the blksize.
3100 */
3101 sgde = scsi_sglist(cmd);
3102 blksize = lpfc_cmd_blksize(cmd);
3103 data_src = (uint8_t *)sg_virt(sgde);
3104 data_len = sgde->length;
3105 if ((data_len & (blksize - 1)) == 0)
3106 chk_guard = 1;
3107 guard_type = scsi_host_get_guard(cmd->device->host);
3108
e85d8f9f 3109 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
a6887e28 3110 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
737d4248 3111 start_app_tag = src->app_tag;
737d4248
JS
3112 len = sgpe->length;
3113 while (src && protsegcnt) {
3114 while (len) {
3115
3116 /*
3117 * First check to see if a protection data
3118 * check is valid
3119 */
3120 if ((src->ref_tag == 0xffffffff) ||
3121 (src->app_tag == 0xffff)) {
3122 start_ref_tag++;
3123 goto skipit;
3124 }
3125
9c6aa9d7 3126 /* First Guard Tag checking */
737d4248
JS
3127 if (chk_guard) {
3128 guard_tag = src->guard_tag;
9c6aa9d7 3129 if (lpfc_cmd_guard_csum(cmd))
737d4248
JS
3130 sum = lpfc_bg_csum(data_src,
3131 blksize);
3132 else
3133 sum = lpfc_bg_crc(data_src,
3134 blksize);
3135 if ((guard_tag != sum)) {
3136 err_type = BGS_GUARD_ERR_MASK;
3137 goto out;
3138 }
3139 }
9c6aa9d7
JS
3140
3141 /* Reference Tag checking */
3142 ref_tag = be32_to_cpu(src->ref_tag);
3143 if (chk_ref && (ref_tag != start_ref_tag)) {
3144 err_type = BGS_REFTAG_ERR_MASK;
3145 goto out;
3146 }
3147 start_ref_tag++;
3148
3149 /* App Tag checking */
3150 app_tag = src->app_tag;
3151 if (chk_app && (app_tag != start_app_tag)) {
3152 err_type = BGS_APPTAG_ERR_MASK;
3153 goto out;
3154 }
737d4248
JS
3155skipit:
3156 len -= sizeof(struct scsi_dif_tuple);
3157 if (len < 0)
3158 len = 0;
3159 src++;
3160
3161 data_src += blksize;
3162 data_len -= blksize;
3163
3164 /*
3165 * Are we at the end of the Data segment?
3166 * The data segment is only used for Guard
3167 * tag checking.
3168 */
3169 if (chk_guard && (data_len == 0)) {
3170 chk_guard = 0;
3171 sgde = sg_next(sgde);
3172 if (!sgde)
3173 goto out;
3174
3175 data_src = (uint8_t *)sg_virt(sgde);
3176 data_len = sgde->length;
3177 if ((data_len & (blksize - 1)) == 0)
3178 chk_guard = 1;
3179 }
3180 }
3181
3182 /* Goto the next Protection data segment */
3183 sgpe = sg_next(sgpe);
3184 if (sgpe) {
3185 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3186 len = sgpe->length;
3187 } else {
3188 src = NULL;
3189 }
3190 protsegcnt--;
3191 }
3192 }
3193out:
3194 if (err_type == BGS_GUARD_ERR_MASK) {
3195 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3196 0x10, 0x1);
3197 cmd->result = DRIVER_SENSE << 24
3198 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3199 phba->bg_guard_err_cnt++;
3200 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3201 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3202 (unsigned long)scsi_get_lba(cmd),
3203 sum, guard_tag);
3204
3205 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3206 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3207 0x10, 0x3);
3208 cmd->result = DRIVER_SENSE << 24
3209 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3210
3211 phba->bg_reftag_err_cnt++;
3212 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3213 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3214 (unsigned long)scsi_get_lba(cmd),
3215 ref_tag, start_ref_tag);
3216
3217 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3218 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3219 0x10, 0x2);
3220 cmd->result = DRIVER_SENSE << 24
3221 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3222
3223 phba->bg_apptag_err_cnt++;
3224 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3225 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3226 (unsigned long)scsi_get_lba(cmd),
3227 app_tag, start_app_tag);
3228 }
3229}
3230
3231
e2a0a9d6
JS
3232/*
3233 * This function checks for BlockGuard errors detected by
3234 * the HBA. In case of errors, the ASC/ASCQ fields in the
3235 * sense buffer will be set accordingly, paired with
3236 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3237 * detected corruption.
3238 *
3239 * Returns:
3240 * 0 - No error found
3241 * 1 - BlockGuard error found
3242 * -1 - Internal error (bad profile, ...etc)
3243 */
3244static int
3245lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3246 struct lpfc_iocbq *pIocbOut)
3247{
3248 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3249 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3250 int ret = 0;
3251 uint32_t bghm = bgf->bghm;
3252 uint32_t bgstat = bgf->bgstat;
3253 uint64_t failing_sector = 0;
3254
e2a0a9d6
JS
3255 spin_lock(&_dump_buf_lock);
3256 if (!_dump_buf_done) {
6a9c52cf
JS
3257 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3258 " Data for %u blocks to debugfs\n",
e2a0a9d6 3259 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
6a9c52cf 3260 lpfc_debug_save_data(phba, cmd);
e2a0a9d6
JS
3261
3262 /* If we have a prot sgl, save the DIF buffer */
3263 if (lpfc_prot_group_type(phba, cmd) ==
3264 LPFC_PG_TYPE_DIF_BUF) {
6a9c52cf
JS
3265 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3266 "Saving DIF for %u blocks to debugfs\n",
3267 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3268 lpfc_debug_save_dif(phba, cmd);
e2a0a9d6
JS
3269 }
3270
3271 _dump_buf_done = 1;
3272 }
3273 spin_unlock(&_dump_buf_lock);
3274
3275 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3276 cmd->result = ScsiResult(DID_ERROR, 0);
737d4248
JS
3277 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3278 "9072 BLKGRD: Invalid BG Profile in cmd"
3279 " 0x%x lba 0x%llx blk cnt 0x%x "
3280 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3281 (unsigned long long)scsi_get_lba(cmd),
3282 blk_rq_sectors(cmd->request), bgstat, bghm);
e2a0a9d6
JS
3283 ret = (-1);
3284 goto out;
3285 }
3286
3287 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3288 cmd->result = ScsiResult(DID_ERROR, 0);
737d4248
JS
3289 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3290 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3291 " 0x%x lba 0x%llx blk cnt 0x%x "
3292 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3293 (unsigned long long)scsi_get_lba(cmd),
3294 blk_rq_sectors(cmd->request), bgstat, bghm);
e2a0a9d6
JS
3295 ret = (-1);
3296 goto out;
3297 }
3298
3299 if (lpfc_bgs_get_guard_err(bgstat)) {
3300 ret = 1;
3301
3302 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3303 0x10, 0x1);
1c9fbafc 3304 cmd->result = DRIVER_SENSE << 24
e2a0a9d6
JS
3305 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3306 phba->bg_guard_err_cnt++;
737d4248
JS
3307 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3308 "9055 BLKGRD: Guard Tag error in cmd"
3309 " 0x%x lba 0x%llx blk cnt 0x%x "
3310 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3311 (unsigned long long)scsi_get_lba(cmd),
3312 blk_rq_sectors(cmd->request), bgstat, bghm);
e2a0a9d6
JS
3313 }
3314
3315 if (lpfc_bgs_get_reftag_err(bgstat)) {
3316 ret = 1;
3317
3318 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3319 0x10, 0x3);
1c9fbafc 3320 cmd->result = DRIVER_SENSE << 24
e2a0a9d6
JS
3321 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3322
3323 phba->bg_reftag_err_cnt++;
737d4248
JS
3324 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3325 "9056 BLKGRD: Ref Tag error in cmd"
3326 " 0x%x lba 0x%llx blk cnt 0x%x "
3327 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3328 (unsigned long long)scsi_get_lba(cmd),
3329 blk_rq_sectors(cmd->request), bgstat, bghm);
e2a0a9d6
JS
3330 }
3331
3332 if (lpfc_bgs_get_apptag_err(bgstat)) {
3333 ret = 1;
3334
3335 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3336 0x10, 0x2);
1c9fbafc 3337 cmd->result = DRIVER_SENSE << 24
e2a0a9d6
JS
3338 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3339
3340 phba->bg_apptag_err_cnt++;
737d4248
JS
3341 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3342 "9061 BLKGRD: App Tag error in cmd"
3343 " 0x%x lba 0x%llx blk cnt 0x%x "
3344 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3345 (unsigned long long)scsi_get_lba(cmd),
3346 blk_rq_sectors(cmd->request), bgstat, bghm);
e2a0a9d6
JS
3347 }
3348
3349 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3350 /*
3351 * setup sense data descriptor 0 per SPC-4 as an information
7c56b9fd
JS
3352 * field, and put the failing LBA in it.
3353 * This code assumes there was also a guard/app/ref tag error
3354 * indication.
e2a0a9d6 3355 */
7c56b9fd
JS
3356 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3357 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3358 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3359 cmd->sense_buffer[10] = 0x80; /* Validity bit */
acd6859b
JS
3360
3361 /* bghm is a "on the wire" FC frame based count */
3362 switch (scsi_get_prot_op(cmd)) {
3363 case SCSI_PROT_READ_INSERT:
3364 case SCSI_PROT_WRITE_STRIP:
3365 bghm /= cmd->device->sector_size;
3366 break;
3367 case SCSI_PROT_READ_STRIP:
3368 case SCSI_PROT_WRITE_INSERT:
3369 case SCSI_PROT_READ_PASS:
3370 case SCSI_PROT_WRITE_PASS:
3371 bghm /= (cmd->device->sector_size +
3372 sizeof(struct scsi_dif_tuple));
3373 break;
3374 }
e2a0a9d6
JS
3375
3376 failing_sector = scsi_get_lba(cmd);
3377 failing_sector += bghm;
3378
7c56b9fd
JS
3379 /* Descriptor Information */
3380 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
e2a0a9d6
JS
3381 }
3382
3383 if (!ret) {
3384 /* No error was reported - problem in FW? */
737d4248
JS
3385 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3386 "9057 BLKGRD: Unknown error in cmd"
3387 " 0x%x lba 0x%llx blk cnt 0x%x "
3388 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3389 (unsigned long long)scsi_get_lba(cmd),
3390 blk_rq_sectors(cmd->request), bgstat, bghm);
3391
3392 /* Calcuate what type of error it was */
3393 lpfc_calc_bg_err(phba, lpfc_cmd);
e2a0a9d6 3394 }
e2a0a9d6
JS
3395out:
3396 return ret;
dea3101e 3397}
3398
da0436e9
JS
3399/**
3400 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3401 * @phba: The Hba for which this call is being executed.
3402 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3403 *
3404 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3405 * field of @lpfc_cmd for device with SLI-4 interface spec.
3406 *
3407 * Return codes:
6c8eea54
JS
3408 * 1 - Error
3409 * 0 - Success
da0436e9
JS
3410 **/
3411static int
3412lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3413{
3414 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3415 struct scatterlist *sgel = NULL;
3416 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3417 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
fedd3b7b 3418 struct sli4_sge *first_data_sgl;
da0436e9
JS
3419 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3420 dma_addr_t physaddr;
3421 uint32_t num_bde = 0;
3422 uint32_t dma_len;
3423 uint32_t dma_offset = 0;
3424 int nseg;
fedd3b7b 3425 struct ulp_bde64 *bde;
da0436e9
JS
3426
3427 /*
3428 * There are three possibilities here - use scatter-gather segment, use
3429 * the single mapping, or neither. Start the lpfc command prep by
3430 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3431 * data bde entry.
3432 */
3433 if (scsi_sg_count(scsi_cmnd)) {
3434 /*
3435 * The driver stores the segment count returned from pci_map_sg
3436 * because this a count of dma-mappings used to map the use_sg
3437 * pages. They are not guaranteed to be the same for those
3438 * architectures that implement an IOMMU.
3439 */
3440
3441 nseg = scsi_dma_map(scsi_cmnd);
3442 if (unlikely(!nseg))
3443 return 1;
3444 sgl += 1;
3445 /* clear the last flag in the fcp_rsp map entry */
3446 sgl->word2 = le32_to_cpu(sgl->word2);
3447 bf_set(lpfc_sli4_sge_last, sgl, 0);
3448 sgl->word2 = cpu_to_le32(sgl->word2);
3449 sgl += 1;
fedd3b7b 3450 first_data_sgl = sgl;
da0436e9
JS
3451 lpfc_cmd->seg_cnt = nseg;
3452 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
6a9c52cf
JS
3453 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3454 " %s: Too many sg segments from "
3455 "dma_map_sg. Config %d, seg_cnt %d\n",
3456 __func__, phba->cfg_sg_seg_cnt,
da0436e9 3457 lpfc_cmd->seg_cnt);
96f7077f 3458 lpfc_cmd->seg_cnt = 0;
da0436e9
JS
3459 scsi_dma_unmap(scsi_cmnd);
3460 return 1;
3461 }
3462
3463 /*
3464 * The driver established a maximum scatter-gather segment count
3465 * during probe that limits the number of sg elements in any
3466 * single scsi command. Just run through the seg_cnt and format
3467 * the sge's.
3468 * When using SLI-3 the driver will try to fit all the BDEs into
3469 * the IOCB. If it can't then the BDEs get added to a BPL as it
3470 * does for SLI-2 mode.
3471 */
3472 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3473 physaddr = sg_dma_address(sgel);
3474 dma_len = sg_dma_len(sgel);
da0436e9
JS
3475 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3476 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
0558056c 3477 sgl->word2 = le32_to_cpu(sgl->word2);
da0436e9
JS
3478 if ((num_bde + 1) == nseg)
3479 bf_set(lpfc_sli4_sge_last, sgl, 1);
3480 else
3481 bf_set(lpfc_sli4_sge_last, sgl, 0);
3482 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
f9bb2da1 3483 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
da0436e9 3484 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74 3485 sgl->sge_len = cpu_to_le32(dma_len);
da0436e9
JS
3486 dma_offset += dma_len;
3487 sgl++;
3488 }
fedd3b7b
JS
3489 /* setup the performance hint (first data BDE) if enabled */
3490 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3491 bde = (struct ulp_bde64 *)
3492 &(iocb_cmd->unsli3.sli3Words[5]);
3493 bde->addrLow = first_data_sgl->addr_lo;
3494 bde->addrHigh = first_data_sgl->addr_hi;
3495 bde->tus.f.bdeSize =
3496 le32_to_cpu(first_data_sgl->sge_len);
3497 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3498 bde->tus.w = cpu_to_le32(bde->tus.w);
3499 }
da0436e9
JS
3500 } else {
3501 sgl += 1;
3502 /* clear the last flag in the fcp_rsp map entry */
3503 sgl->word2 = le32_to_cpu(sgl->word2);
3504 bf_set(lpfc_sli4_sge_last, sgl, 1);
3505 sgl->word2 = cpu_to_le32(sgl->word2);
3506 }
3507
3508 /*
3509 * Finish initializing those IOCB fields that are dependent on the
3510 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3511 * explicitly reinitialized.
3512 * all iocb memory resources are reused.
3513 */
3514 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3515
3516 /*
3517 * Due to difference in data length between DIF/non-DIF paths,
3518 * we need to set word 4 of IOCB here
3519 */
3520 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1ba981fd
JS
3521
3522 /*
3523 * If the OAS driver feature is enabled and the lun is enabled for
3524 * OAS, set the oas iocb related flags.
3525 */
3526 if ((phba->cfg_EnableXLane) && ((struct lpfc_device_data *)
3527 scsi_cmnd->device->hostdata)->oas_enabled)
3528 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_OAS;
da0436e9
JS
3529 return 0;
3530}
3531
acd6859b
JS
3532/**
3533 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3534 * @phba: The Hba for which this call is being executed.
3535 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3536 *
3537 * This is the protection/DIF aware version of
3538 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3539 * two functions eventually, but for now, it's here
3540 **/
3541static int
3542lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3543 struct lpfc_scsi_buf *lpfc_cmd)
3544{
3545 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3546 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3547 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3548 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
96f7077f 3549 uint32_t num_sge = 0;
acd6859b
JS
3550 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3551 int prot_group_type = 0;
3552 int fcpdl;
3553
3554 /*
3555 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
96f7077f 3556 * fcp_rsp regions to the first data sge entry
acd6859b
JS
3557 */
3558 if (scsi_sg_count(scsi_cmnd)) {
3559 /*
3560 * The driver stores the segment count returned from pci_map_sg
3561 * because this a count of dma-mappings used to map the use_sg
3562 * pages. They are not guaranteed to be the same for those
3563 * architectures that implement an IOMMU.
3564 */
3565 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3566 scsi_sglist(scsi_cmnd),
3567 scsi_sg_count(scsi_cmnd), datadir);
3568 if (unlikely(!datasegcnt))
3569 return 1;
3570
3571 sgl += 1;
3572 /* clear the last flag in the fcp_rsp map entry */
3573 sgl->word2 = le32_to_cpu(sgl->word2);
3574 bf_set(lpfc_sli4_sge_last, sgl, 0);
3575 sgl->word2 = cpu_to_le32(sgl->word2);
3576
3577 sgl += 1;
3578 lpfc_cmd->seg_cnt = datasegcnt;
96f7077f
JS
3579
3580 /* First check if data segment count from SCSI Layer is good */
3581 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3582 goto err;
acd6859b
JS
3583
3584 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3585
3586 switch (prot_group_type) {
3587 case LPFC_PG_TYPE_NO_DIF:
96f7077f
JS
3588 /* Here we need to add a DISEED to the count */
3589 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3590 goto err;
3591
3592 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
acd6859b 3593 datasegcnt);
96f7077f 3594
acd6859b 3595 /* we should have 2 or more entries in buffer list */
96f7077f 3596 if (num_sge < 2)
acd6859b
JS
3597 goto err;
3598 break;
96f7077f
JS
3599
3600 case LPFC_PG_TYPE_DIF_BUF:
acd6859b
JS
3601 /*
3602 * This type indicates that protection buffers are
3603 * passed to the driver, so that needs to be prepared
3604 * for DMA
3605 */
3606 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3607 scsi_prot_sglist(scsi_cmnd),
3608 scsi_prot_sg_count(scsi_cmnd), datadir);
3609 if (unlikely(!protsegcnt)) {
3610 scsi_dma_unmap(scsi_cmnd);
3611 return 1;
3612 }
3613
3614 lpfc_cmd->prot_seg_cnt = protsegcnt;
96f7077f
JS
3615 /*
3616 * There is a minimun of 3 SGEs used for every
3617 * protection data segment.
3618 */
3619 if ((lpfc_cmd->prot_seg_cnt * 3) >
3620 (phba->cfg_total_seg_cnt - 2))
3621 goto err;
acd6859b 3622
96f7077f 3623 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
acd6859b 3624 datasegcnt, protsegcnt);
96f7077f 3625
acd6859b 3626 /* we should have 3 or more entries in buffer list */
96f7077f
JS
3627 if ((num_sge < 3) ||
3628 (num_sge > phba->cfg_total_seg_cnt))
acd6859b
JS
3629 goto err;
3630 break;
96f7077f 3631
acd6859b
JS
3632 case LPFC_PG_TYPE_INVALID:
3633 default:
96f7077f
JS
3634 scsi_dma_unmap(scsi_cmnd);
3635 lpfc_cmd->seg_cnt = 0;
3636
acd6859b
JS
3637 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3638 "9083 Unexpected protection group %i\n",
3639 prot_group_type);
3640 return 1;
3641 }
3642 }
3643
8012cc38
JS
3644 switch (scsi_get_prot_op(scsi_cmnd)) {
3645 case SCSI_PROT_WRITE_STRIP:
3646 case SCSI_PROT_READ_STRIP:
3647 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3648 break;
3649 case SCSI_PROT_WRITE_INSERT:
3650 case SCSI_PROT_READ_INSERT:
3651 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3652 break;
3653 case SCSI_PROT_WRITE_PASS:
3654 case SCSI_PROT_READ_PASS:
3655 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3656 break;
3657 }
3658
acd6859b 3659 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
acd6859b
JS
3660 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3661
3662 /*
3663 * Due to difference in data length between DIF/non-DIF paths,
3664 * we need to set word 4 of IOCB here
3665 */
3666 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
acd6859b
JS
3667
3668 return 0;
3669err:
96f7077f
JS
3670 if (lpfc_cmd->seg_cnt)
3671 scsi_dma_unmap(scsi_cmnd);
3672 if (lpfc_cmd->prot_seg_cnt)
3673 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3674 scsi_prot_sg_count(scsi_cmnd),
3675 scsi_cmnd->sc_data_direction);
3676
acd6859b 3677 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
96f7077f
JS
3678 "9084 Cannot setup S/G List for HBA"
3679 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3680 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3681 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3682 prot_group_type, num_sge);
3683
3684 lpfc_cmd->seg_cnt = 0;
3685 lpfc_cmd->prot_seg_cnt = 0;
acd6859b
JS
3686 return 1;
3687}
3688
3772a991
JS
3689/**
3690 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3691 * @phba: The Hba for which this call is being executed.
3692 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3693 *
3694 * This routine wraps the actual DMA mapping function pointer from the
3695 * lpfc_hba struct.
3696 *
3697 * Return codes:
6c8eea54
JS
3698 * 1 - Error
3699 * 0 - Success
3772a991
JS
3700 **/
3701static inline int
3702lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3703{
3704 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3705}
3706
acd6859b
JS
3707/**
3708 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3709 * using BlockGuard.
3710 * @phba: The Hba for which this call is being executed.
3711 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3712 *
3713 * This routine wraps the actual DMA mapping function pointer from the
3714 * lpfc_hba struct.
3715 *
3716 * Return codes:
3717 * 1 - Error
3718 * 0 - Success
3719 **/
3720static inline int
3721lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3722{
3723 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3724}
3725
ea2151b4 3726/**
3621a710 3727 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
ea2151b4
JS
3728 * @phba: Pointer to hba context object.
3729 * @vport: Pointer to vport object.
3730 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3731 * @rsp_iocb: Pointer to response iocb object which reported error.
3732 *
3733 * This function posts an event when there is a SCSI command reporting
3734 * error from the scsi device.
3735 **/
3736static void
3737lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3738 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3739 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3740 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3741 uint32_t resp_info = fcprsp->rspStatus2;
3742 uint32_t scsi_status = fcprsp->rspStatus3;
3743 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3744 struct lpfc_fast_path_event *fast_path_evt = NULL;
3745 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3746 unsigned long flags;
3747
5989b8d4
JS
3748 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3749 return;
3750
ea2151b4
JS
3751 /* If there is queuefull or busy condition send a scsi event */
3752 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3753 (cmnd->result == SAM_STAT_BUSY)) {
3754 fast_path_evt = lpfc_alloc_fast_evt(phba);
3755 if (!fast_path_evt)
3756 return;
3757 fast_path_evt->un.scsi_evt.event_type =
3758 FC_REG_SCSI_EVENT;
3759 fast_path_evt->un.scsi_evt.subcategory =
3760 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3761 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3762 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3763 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3764 &pnode->nlp_portname, sizeof(struct lpfc_name));
3765 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3766 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3767 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3768 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3769 fast_path_evt = lpfc_alloc_fast_evt(phba);
3770 if (!fast_path_evt)
3771 return;
3772 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3773 FC_REG_SCSI_EVENT;
3774 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3775 LPFC_EVENT_CHECK_COND;
3776 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3777 cmnd->device->lun;
3778 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3779 &pnode->nlp_portname, sizeof(struct lpfc_name));
3780 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3781 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3782 fast_path_evt->un.check_cond_evt.sense_key =
3783 cmnd->sense_buffer[2] & 0xf;
3784 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3785 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3786 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3787 fcpi_parm &&
3788 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3789 ((scsi_status == SAM_STAT_GOOD) &&
3790 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3791 /*
3792 * If status is good or resid does not match with fcp_param and
3793 * there is valid fcpi_parm, then there is a read_check error
3794 */
3795 fast_path_evt = lpfc_alloc_fast_evt(phba);
3796 if (!fast_path_evt)
3797 return;
3798 fast_path_evt->un.read_check_error.header.event_type =
3799 FC_REG_FABRIC_EVENT;
3800 fast_path_evt->un.read_check_error.header.subcategory =
3801 LPFC_EVENT_FCPRDCHKERR;
3802 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3803 &pnode->nlp_portname, sizeof(struct lpfc_name));
3804 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3805 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3806 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3807 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3808 fast_path_evt->un.read_check_error.fcpiparam =
3809 fcpi_parm;
3810 } else
3811 return;
3812
3813 fast_path_evt->vport = vport;
3814 spin_lock_irqsave(&phba->hbalock, flags);
3815 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3816 spin_unlock_irqrestore(&phba->hbalock, flags);
3817 lpfc_worker_wake_up(phba);
3818 return;
3819}
9bad7671
JS
3820
3821/**
f1126688 3822 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3772a991 3823 * @phba: The HBA for which this call is being executed.
9bad7671
JS
3824 * @psb: The scsi buffer which is going to be un-mapped.
3825 *
3826 * This routine does DMA un-mapping of scatter gather list of scsi command
3772a991 3827 * field of @lpfc_cmd for device with SLI-3 interface spec.
9bad7671 3828 **/
bcf4dbfa 3829static void
f1126688 3830lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
bcf4dbfa
JS
3831{
3832 /*
3833 * There are only two special cases to consider. (1) the scsi command
3834 * requested scatter-gather usage or (2) the scsi command allocated
3835 * a request buffer, but did not request use_sg. There is a third
3836 * case, but it does not require resource deallocation.
3837 */
a0b4f78f
FT
3838 if (psb->seg_cnt > 0)
3839 scsi_dma_unmap(psb->pCmd);
e2a0a9d6
JS
3840 if (psb->prot_seg_cnt > 0)
3841 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3842 scsi_prot_sg_count(psb->pCmd),
3843 psb->pCmd->sc_data_direction);
bcf4dbfa
JS
3844}
3845
9bad7671 3846/**
3621a710 3847 * lpfc_handler_fcp_err - FCP response handler
9bad7671
JS
3848 * @vport: The virtual port for which this call is being executed.
3849 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3850 * @rsp_iocb: The response IOCB which contains FCP error.
3851 *
3852 * This routine is called to process response IOCB with status field
3853 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3854 * based upon SCSI and FCP error.
3855 **/
dea3101e 3856static void
2e0fef85
JS
3857lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3858 struct lpfc_iocbq *rsp_iocb)
dea3101e 3859{
3860 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3861 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3862 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
7054a606 3863 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea3101e 3864 uint32_t resp_info = fcprsp->rspStatus2;
3865 uint32_t scsi_status = fcprsp->rspStatus3;
c7743956 3866 uint32_t *lp;
dea3101e 3867 uint32_t host_status = DID_OK;
3868 uint32_t rsplen = 0;
c7743956 3869 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea3101e 3870
ea2151b4 3871
dea3101e 3872 /*
3873 * If this is a task management command, there is no
3874 * scsi packet associated with this lpfc_cmd. The driver
3875 * consumes it.
3876 */
3877 if (fcpcmd->fcpCntl2) {
3878 scsi_status = 0;
3879 goto out;
3880 }
3881
6a9c52cf
JS
3882 if (resp_info & RSP_LEN_VALID) {
3883 rsplen = be32_to_cpu(fcprsp->rspRspLen);
e40a02c1 3884 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
6a9c52cf
JS
3885 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3886 "2719 Invalid response length: "
3887 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3888 cmnd->device->id,
3889 cmnd->device->lun, cmnd->cmnd[0],
3890 rsplen);
3891 host_status = DID_ERROR;
3892 goto out;
3893 }
e40a02c1
JS
3894 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3895 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3896 "2757 Protocol failure detected during "
3897 "processing of FCP I/O op: "
3898 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3899 cmnd->device->id,
3900 cmnd->device->lun, cmnd->cmnd[0],
3901 fcprsp->rspInfo3);
3902 host_status = DID_ERROR;
3903 goto out;
3904 }
6a9c52cf
JS
3905 }
3906
c7743956
JS
3907 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3908 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3909 if (snslen > SCSI_SENSE_BUFFERSIZE)
3910 snslen = SCSI_SENSE_BUFFERSIZE;
3911
3912 if (resp_info & RSP_LEN_VALID)
3913 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3914 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3915 }
3916 lp = (uint32_t *)cmnd->sense_buffer;
3917
aa1c7ee7
JS
3918 /* special handling for under run conditions */
3919 if (!scsi_status && (resp_info & RESID_UNDER)) {
3920 /* don't log under runs if fcp set... */
3921 if (vport->cfg_log_verbose & LOG_FCP)
3922 logit = LOG_FCP_ERROR;
3923 /* unless operator says so */
3924 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3925 logit = LOG_FCP_UNDER;
3926 }
c7743956 3927
e8b62011 3928 lpfc_printf_vlog(vport, KERN_WARNING, logit,
e2a0a9d6 3929 "9024 FCP command x%x failed: x%x SNS x%x x%x "
e8b62011
JS
3930 "Data: x%x x%x x%x x%x x%x\n",
3931 cmnd->cmnd[0], scsi_status,
3932 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3933 be32_to_cpu(fcprsp->rspResId),
3934 be32_to_cpu(fcprsp->rspSnsLen),
3935 be32_to_cpu(fcprsp->rspRspLen),
3936 fcprsp->rspInfo3);
dea3101e 3937
a0b4f78f 3938 scsi_set_resid(cmnd, 0);
dea3101e 3939 if (resp_info & RESID_UNDER) {
a0b4f78f 3940 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea3101e 3941
73d91e50 3942 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
e2a0a9d6 3943 "9025 FCP Read Underrun, expected %d, "
e8b62011
JS
3944 "residual %d Data: x%x x%x x%x\n",
3945 be32_to_cpu(fcpcmd->fcpDl),
3946 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3947 cmnd->underflow);
dea3101e 3948
7054a606
JS
3949 /*
3950 * If there is an under run check if under run reported by
3951 * storage array is same as the under run reported by HBA.
3952 * If this is not same, there is a dropped frame.
3953 */
3954 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3955 fcpi_parm &&
a0b4f78f 3956 (scsi_get_resid(cmnd) != fcpi_parm)) {
e8b62011
JS
3957 lpfc_printf_vlog(vport, KERN_WARNING,
3958 LOG_FCP | LOG_FCP_ERROR,
e2a0a9d6 3959 "9026 FCP Read Check Error "
e8b62011
JS
3960 "and Underrun Data: x%x x%x x%x x%x\n",
3961 be32_to_cpu(fcpcmd->fcpDl),
3962 scsi_get_resid(cmnd), fcpi_parm,
3963 cmnd->cmnd[0]);
a0b4f78f 3964 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
7054a606
JS
3965 host_status = DID_ERROR;
3966 }
dea3101e 3967 /*
3968 * The cmnd->underflow is the minimum number of bytes that must
25985edc 3969 * be transferred for this command. Provided a sense condition
dea3101e 3970 * is not present, make sure the actual amount transferred is at
3971 * least the underflow value or fail.
3972 */
3973 if (!(resp_info & SNS_LEN_VALID) &&
3974 (scsi_status == SAM_STAT_GOOD) &&
a0b4f78f
FT
3975 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3976 < cmnd->underflow)) {
e8b62011 3977 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
e2a0a9d6 3978 "9027 FCP command x%x residual "
e8b62011
JS
3979 "underrun converted to error "
3980 "Data: x%x x%x x%x\n",
66dbfbe6 3981 cmnd->cmnd[0], scsi_bufflen(cmnd),
e8b62011 3982 scsi_get_resid(cmnd), cmnd->underflow);
dea3101e 3983 host_status = DID_ERROR;
3984 }
3985 } else if (resp_info & RESID_OVER) {
e8b62011 3986 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
e2a0a9d6 3987 "9028 FCP command x%x residual overrun error. "
e4e74273 3988 "Data: x%x x%x\n", cmnd->cmnd[0],
e8b62011 3989 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea3101e 3990 host_status = DID_ERROR;
3991
3992 /*
3993 * Check SLI validation that all the transfer was actually done
26373d23 3994 * (fcpi_parm should be zero). Apply check only to reads.
dea3101e 3995 */
26373d23 3996 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
e8b62011 3997 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
26373d23 3998 "9029 FCP Read Check Error Data: "
eee8877e 3999 "x%x x%x x%x x%x x%x\n",
e8b62011
JS
4000 be32_to_cpu(fcpcmd->fcpDl),
4001 be32_to_cpu(fcprsp->rspResId),
eee8877e
JS
4002 fcpi_parm, cmnd->cmnd[0], scsi_status);
4003 switch (scsi_status) {
4004 case SAM_STAT_GOOD:
4005 case SAM_STAT_CHECK_CONDITION:
4006 /* Fabric dropped a data frame. Fail any successful
4007 * command in which we detected dropped frames.
4008 * A status of good or some check conditions could
4009 * be considered a successful command.
4010 */
4011 host_status = DID_ERROR;
4012 break;
4013 }
a0b4f78f 4014 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea3101e 4015 }
4016
4017 out:
4018 cmnd->result = ScsiResult(host_status, scsi_status);
ea2151b4 4019 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea3101e 4020}
4021
9bad7671 4022/**
3621a710 4023 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
9bad7671
JS
4024 * @phba: The Hba for which this call is being executed.
4025 * @pIocbIn: The command IOCBQ for the scsi cmnd.
3772a991 4026 * @pIocbOut: The response IOCBQ for the scsi cmnd.
9bad7671
JS
4027 *
4028 * This routine assigns scsi command result by looking into response IOCB
4029 * status field appropriately. This routine handles QUEUE FULL condition as
4030 * well by ramping down device queue depth.
4031 **/
dea3101e 4032static void
4033lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4034 struct lpfc_iocbq *pIocbOut)
4035{
4036 struct lpfc_scsi_buf *lpfc_cmd =
4037 (struct lpfc_scsi_buf *) pIocbIn->context1;
2e0fef85 4038 struct lpfc_vport *vport = pIocbIn->vport;
dea3101e 4039 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4040 struct lpfc_nodelist *pnode = rdata->pnode;
75baf696 4041 struct scsi_cmnd *cmd;
445cf4f4 4042 int result;
a257bf90 4043 struct scsi_device *tmp_sdev;
5ffc266e 4044 int depth;
fa61a54e 4045 unsigned long flags;
ea2151b4 4046 struct lpfc_fast_path_event *fast_path_evt;
75baf696 4047 struct Scsi_Host *shost;
a257bf90 4048 uint32_t queue_depth, scsi_id;
73d91e50 4049 uint32_t logit = LOG_FCP;
dea3101e 4050
75baf696
JS
4051 /* Sanity check on return of outstanding command */
4052 if (!(lpfc_cmd->pCmd))
4053 return;
4054 cmd = lpfc_cmd->pCmd;
4055 shost = cmd->device->host;
4056
e3d2b802 4057 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea3101e 4058 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
341af102
JS
4059 /* pick up SLI4 exhange busy status from HBA */
4060 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4061
9a6b09c0
JS
4062#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4063 if (lpfc_cmd->prot_data_type) {
4064 struct scsi_dif_tuple *src = NULL;
4065
4066 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4067 /*
4068 * Used to restore any changes to protection
4069 * data for error injection.
4070 */
4071 switch (lpfc_cmd->prot_data_type) {
4072 case LPFC_INJERR_REFTAG:
4073 src->ref_tag =
4074 lpfc_cmd->prot_data;
4075 break;
4076 case LPFC_INJERR_APPTAG:
4077 src->app_tag =
4078 (uint16_t)lpfc_cmd->prot_data;
4079 break;
4080 case LPFC_INJERR_GUARD:
4081 src->guard_tag =
4082 (uint16_t)lpfc_cmd->prot_data;
4083 break;
4084 default:
4085 break;
4086 }
4087
4088 lpfc_cmd->prot_data = 0;
4089 lpfc_cmd->prot_data_type = 0;
4090 lpfc_cmd->prot_data_segment = NULL;
4091 }
4092#endif
109f6ed0
JS
4093 if (pnode && NLP_CHK_NODE_ACT(pnode))
4094 atomic_dec(&pnode->cmd_pending);
dea3101e 4095
4096 if (lpfc_cmd->status) {
4097 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4098 (lpfc_cmd->result & IOERR_DRVR_MASK))
4099 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4100 else if (lpfc_cmd->status >= IOSTAT_CNT)
4101 lpfc_cmd->status = IOSTAT_DEFAULT;
aa1c7ee7
JS
4102 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4103 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4104 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4105 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
73d91e50
JS
4106 logit = 0;
4107 else
4108 logit = LOG_FCP | LOG_FCP_UNDER;
4109 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4110 "9030 FCP cmd x%x failed <%d/%d> "
5a0d80fc
JS
4111 "status: x%x result: x%x "
4112 "sid: x%x did: x%x oxid: x%x "
4113 "Data: x%x x%x\n",
73d91e50
JS
4114 cmd->cmnd[0],
4115 cmd->device ? cmd->device->id : 0xffff,
4116 cmd->device ? cmd->device->lun : 0xffff,
4117 lpfc_cmd->status, lpfc_cmd->result,
3bf41ba9
JS
4118 vport->fc_myDID,
4119 (pnode) ? pnode->nlp_DID : 0,
5a0d80fc
JS
4120 phba->sli_rev == LPFC_SLI_REV4 ?
4121 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
73d91e50
JS
4122 pIocbOut->iocb.ulpContext,
4123 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea3101e 4124
4125 switch (lpfc_cmd->status) {
4126 case IOSTAT_FCP_RSP_ERROR:
4127 /* Call FCP RSP handler to determine result */
2e0fef85 4128 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea3101e 4129 break;
4130 case IOSTAT_NPORT_BSY:
4131 case IOSTAT_FABRIC_BSY:
0f1f53a7 4132 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
ea2151b4
JS
4133 fast_path_evt = lpfc_alloc_fast_evt(phba);
4134 if (!fast_path_evt)
4135 break;
4136 fast_path_evt->un.fabric_evt.event_type =
4137 FC_REG_FABRIC_EVENT;
4138 fast_path_evt->un.fabric_evt.subcategory =
4139 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4140 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4141 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4142 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4143 &pnode->nlp_portname,
4144 sizeof(struct lpfc_name));
4145 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4146 &pnode->nlp_nodename,
4147 sizeof(struct lpfc_name));
4148 }
4149 fast_path_evt->vport = vport;
4150 fast_path_evt->work_evt.evt =
4151 LPFC_EVT_FASTPATH_MGMT_EVT;
4152 spin_lock_irqsave(&phba->hbalock, flags);
4153 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4154 &phba->work_list);
4155 spin_unlock_irqrestore(&phba->hbalock, flags);
4156 lpfc_worker_wake_up(phba);
dea3101e 4157 break;
92d7f7b0 4158 case IOSTAT_LOCAL_REJECT:
1151e3ec 4159 case IOSTAT_REMOTE_STOP:
ab56dc2e
JS
4160 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4161 lpfc_cmd->result ==
4162 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4163 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4164 lpfc_cmd->result ==
4165 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4166 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4167 break;
4168 }
d7c255b2 4169 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
92d7f7b0 4170 lpfc_cmd->result == IOERR_NO_RESOURCES ||
b92938b4
JS
4171 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4172 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
92d7f7b0 4173 cmd->result = ScsiResult(DID_REQUEUE, 0);
58da1ffb 4174 break;
e2a0a9d6 4175 }
e2a0a9d6
JS
4176 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4177 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4178 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4179 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4180 /*
4181 * This is a response for a BG enabled
4182 * cmd. Parse BG error
4183 */
4184 lpfc_parse_bg_err(phba, lpfc_cmd,
4185 pIocbOut);
4186 break;
4187 } else {
4188 lpfc_printf_vlog(vport, KERN_WARNING,
4189 LOG_BG,
4190 "9031 non-zero BGSTAT "
6a9c52cf 4191 "on unprotected cmd\n");
e2a0a9d6
JS
4192 }
4193 }
1151e3ec
JS
4194 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4195 && (phba->sli_rev == LPFC_SLI_REV4)
4196 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4197 /* This IO was aborted by the target, we don't
4198 * know the rxid and because we did not send the
4199 * ABTS we cannot generate and RRQ.
4200 */
4201 lpfc_set_rrq_active(phba, pnode,
ee0f4fe1
JS
4202 lpfc_cmd->cur_iocbq.sli4_lxritag,
4203 0, 0);
1151e3ec 4204 }
e2a0a9d6 4205 /* else: fall through */
dea3101e 4206 default:
4207 cmd->result = ScsiResult(DID_ERROR, 0);
4208 break;
4209 }
4210
58da1ffb 4211 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
19a7b4ae 4212 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
0f1f53a7
JS
4213 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4214 SAM_STAT_BUSY);
ab56dc2e 4215 } else
dea3101e 4216 cmd->result = ScsiResult(DID_OK, 0);
dea3101e 4217
4218 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4219 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4220
e8b62011
JS
4221 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4222 "0710 Iodone <%d/%d> cmd %p, error "
4223 "x%x SNS x%x x%x Data: x%x x%x\n",
4224 cmd->device->id, cmd->device->lun, cmd,
4225 cmd->result, *lp, *(lp + 3), cmd->retries,
4226 scsi_get_resid(cmd));
dea3101e 4227 }
4228
ea2151b4 4229 lpfc_update_stats(phba, lpfc_cmd);
445cf4f4 4230 result = cmd->result;
977b5a0a
JS
4231 if (vport->cfg_max_scsicmpl_time &&
4232 time_after(jiffies, lpfc_cmd->start_time +
4233 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
a257bf90 4234 spin_lock_irqsave(shost->host_lock, flags);
109f6ed0
JS
4235 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4236 if (pnode->cmd_qdepth >
4237 atomic_read(&pnode->cmd_pending) &&
4238 (atomic_read(&pnode->cmd_pending) >
4239 LPFC_MIN_TGT_QDEPTH) &&
4240 ((cmd->cmnd[0] == READ_10) ||
4241 (cmd->cmnd[0] == WRITE_10)))
4242 pnode->cmd_qdepth =
4243 atomic_read(&pnode->cmd_pending);
4244
4245 pnode->last_change_time = jiffies;
4246 }
a257bf90 4247 spin_unlock_irqrestore(shost->host_lock, flags);
109f6ed0 4248 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
7dc517df 4249 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
977b5a0a 4250 time_after(jiffies, pnode->last_change_time +
109f6ed0 4251 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
a257bf90 4252 spin_lock_irqsave(shost->host_lock, flags);
7dc517df
JS
4253 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4254 / 100;
4255 depth = depth ? depth : 1;
4256 pnode->cmd_qdepth += depth;
4257 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4258 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
109f6ed0 4259 pnode->last_change_time = jiffies;
a257bf90 4260 spin_unlock_irqrestore(shost->host_lock, flags);
109f6ed0 4261 }
977b5a0a
JS
4262 }
4263
1dcb58e5 4264 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
a257bf90
JS
4265
4266 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4267 queue_depth = cmd->device->queue_depth;
4268 scsi_id = cmd->device->id;
0bd4ca25
JSEC
4269 cmd->scsi_done(cmd);
4270
b808608b 4271 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
876dd7d0 4272 spin_lock_irqsave(&phba->hbalock, flags);
92e3af66 4273 lpfc_cmd->pCmd = NULL;
876dd7d0 4274 spin_unlock_irqrestore(&phba->hbalock, flags);
92e3af66 4275
fa61a54e
JS
4276 /*
4277 * If there is a thread waiting for command completion
4278 * wake up the thread.
4279 */
a257bf90 4280 spin_lock_irqsave(shost->host_lock, flags);
fa61a54e
JS
4281 if (lpfc_cmd->waitq)
4282 wake_up(lpfc_cmd->waitq);
a257bf90 4283 spin_unlock_irqrestore(shost->host_lock, flags);
b808608b
JW
4284 lpfc_release_scsi_buf(phba, lpfc_cmd);
4285 return;
4286 }
4287
92d7f7b0 4288 if (!result)
a257bf90 4289 lpfc_rampup_queue_depth(vport, queue_depth);
92d7f7b0 4290
445cf4f4
JSEC
4291 /*
4292 * Check for queue full. If the lun is reporting queue full, then
4293 * back off the lun queue depth to prevent target overloads.
4294 */
58da1ffb
JS
4295 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4296 NLP_CHK_NODE_ACT(pnode)) {
a257bf90
JS
4297 shost_for_each_device(tmp_sdev, shost) {
4298 if (tmp_sdev->id != scsi_id)
445cf4f4
JSEC
4299 continue;
4300 depth = scsi_track_queue_full(tmp_sdev,
5ffc266e
JS
4301 tmp_sdev->queue_depth-1);
4302 if (depth <= 0)
4303 continue;
e8b62011
JS
4304 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4305 "0711 detected queue full - lun queue "
4306 "depth adjusted to %d.\n", depth);
ea2151b4 4307 lpfc_send_sdev_queuedepth_change_event(phba, vport,
5ffc266e
JS
4308 pnode,
4309 tmp_sdev->lun,
4310 depth+1, depth);
445cf4f4
JSEC
4311 }
4312 }
4313
876dd7d0 4314 spin_lock_irqsave(&phba->hbalock, flags);
92e3af66 4315 lpfc_cmd->pCmd = NULL;
876dd7d0 4316 spin_unlock_irqrestore(&phba->hbalock, flags);
92e3af66 4317
fa61a54e
JS
4318 /*
4319 * If there is a thread waiting for command completion
4320 * wake up the thread.
4321 */
a257bf90 4322 spin_lock_irqsave(shost->host_lock, flags);
fa61a54e
JS
4323 if (lpfc_cmd->waitq)
4324 wake_up(lpfc_cmd->waitq);
a257bf90 4325 spin_unlock_irqrestore(shost->host_lock, flags);
fa61a54e 4326
0bd4ca25 4327 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 4328}
4329
34b02dcd 4330/**
3621a710 4331 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
34b02dcd
JS
4332 * @data: A pointer to the immediate command data portion of the IOCB.
4333 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4334 *
4335 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4336 * byte swapping the data to big endian format for transmission on the wire.
4337 **/
4338static void
4339lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4340{
4341 int i, j;
4342 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4343 i += sizeof(uint32_t), j++) {
4344 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4345 }
4346}
4347
9bad7671 4348/**
f1126688 4349 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
9bad7671
JS
4350 * @vport: The virtual port for which this call is being executed.
4351 * @lpfc_cmd: The scsi command which needs to send.
4352 * @pnode: Pointer to lpfc_nodelist.
4353 *
4354 * This routine initializes fcp_cmnd and iocb data structure from scsi command
3772a991 4355 * to transfer for device with SLI3 interface spec.
9bad7671 4356 **/
dea3101e 4357static void
f1126688 4358lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2e0fef85 4359 struct lpfc_nodelist *pnode)
dea3101e 4360{
2e0fef85 4361 struct lpfc_hba *phba = vport->phba;
dea3101e 4362 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4363 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4364 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4365 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4366 int datadir = scsi_cmnd->sc_data_direction;
7e2b19fb 4367 char tag[2];
027140ea
JS
4368 uint8_t *ptr;
4369 bool sli4;
98bbf5f7 4370 uint32_t fcpdl;
dea3101e 4371
58da1ffb
JS
4372 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4373 return;
4374
dea3101e 4375 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
69859dc4
JSEC
4376 /* clear task management bits */
4377 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea3101e 4378
91886523
JSEC
4379 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4380 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 4381
027140ea
JS
4382 ptr = &fcp_cmnd->fcpCdb[0];
4383 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4384 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4385 ptr += scsi_cmnd->cmd_len;
4386 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4387 }
4388
7e2b19fb
JS
4389 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4390 switch (tag[0]) {
dea3101e 4391 case HEAD_OF_QUEUE_TAG:
4392 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4393 break;
4394 case ORDERED_QUEUE_TAG:
4395 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4396 break;
4397 default:
4398 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4399 break;
4400 }
4401 } else
fe8f7f9c 4402 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea3101e 4403
027140ea
JS
4404 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4405
dea3101e 4406 /*
4407 * There are three possibilities here - use scatter-gather segment, use
4408 * the single mapping, or neither. Start the lpfc command prep by
4409 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4410 * data bde entry.
4411 */
a0b4f78f 4412 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 4413 if (datadir == DMA_TO_DEVICE) {
4414 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
182ba753 4415 iocb_cmd->ulpPU = PARM_READ_CHECK;
3cb01c57
JS
4416 if (vport->cfg_first_burst_size &&
4417 (pnode->nlp_flag & NLP_FIRSTBURST)) {
98bbf5f7
JS
4418 fcpdl = scsi_bufflen(scsi_cmnd);
4419 if (fcpdl < vport->cfg_first_burst_size)
4420 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4421 else
4422 piocbq->iocb.un.fcpi.fcpi_XRdy =
4423 vport->cfg_first_burst_size;
3cb01c57 4424 }
dea3101e 4425 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4426 phba->fc4OutputRequests++;
4427 } else {
4428 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4429 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea3101e 4430 fcp_cmnd->fcpCntl3 = READ_DATA;
4431 phba->fc4InputRequests++;
4432 }
4433 } else {
4434 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4435 iocb_cmd->un.fcpi.fcpi_parm = 0;
4436 iocb_cmd->ulpPU = 0;
4437 fcp_cmnd->fcpCntl3 = 0;
4438 phba->fc4ControlRequests++;
4439 }
e2a0a9d6
JS
4440 if (phba->sli_rev == 3 &&
4441 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
34b02dcd 4442 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 4443 /*
4444 * Finish initializing those IOCB fields that are independent
4445 * of the scsi_cmnd request_buffer
4446 */
4447 piocbq->iocb.ulpContext = pnode->nlp_rpi;
027140ea 4448 if (sli4)
6d368e53
JS
4449 piocbq->iocb.ulpContext =
4450 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea3101e 4451 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4452 piocbq->iocb.ulpFCP2Rcvy = 1;
09372820
JS
4453 else
4454 piocbq->iocb.ulpFCP2Rcvy = 0;
dea3101e 4455
4456 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4457 piocbq->context1 = lpfc_cmd;
4458 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4459 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2e0fef85 4460 piocbq->vport = vport;
dea3101e 4461}
4462
da0436e9 4463/**
6d368e53 4464 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
9bad7671
JS
4465 * @vport: The virtual port for which this call is being executed.
4466 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4467 * @lun: Logical unit number.
4468 * @task_mgmt_cmd: SCSI task management command.
4469 *
3772a991
JS
4470 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4471 * for device with SLI-3 interface spec.
9bad7671
JS
4472 *
4473 * Return codes:
4474 * 0 - Error
4475 * 1 - Success
4476 **/
dea3101e 4477static int
f1126688 4478lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea3101e 4479 struct lpfc_scsi_buf *lpfc_cmd,
420b630d 4480 unsigned int lun,
dea3101e 4481 uint8_t task_mgmt_cmd)
4482{
dea3101e 4483 struct lpfc_iocbq *piocbq;
4484 IOCB_t *piocb;
4485 struct fcp_cmnd *fcp_cmnd;
0b18ac42 4486 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea3101e 4487 struct lpfc_nodelist *ndlp = rdata->pnode;
4488
58da1ffb
JS
4489 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4490 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea3101e 4491 return 0;
dea3101e 4492
dea3101e 4493 piocbq = &(lpfc_cmd->cur_iocbq);
2e0fef85
JS
4494 piocbq->vport = vport;
4495
dea3101e 4496 piocb = &piocbq->iocb;
4497
4498 fcp_cmnd = lpfc_cmd->fcp_cmnd;
34b02dcd
JS
4499 /* Clear out any old data in the FCP command area */
4500 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4501 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea3101e 4502 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
e2a0a9d6
JS
4503 if (vport->phba->sli_rev == 3 &&
4504 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
34b02dcd 4505 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 4506 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea3101e 4507 piocb->ulpContext = ndlp->nlp_rpi;
6d368e53
JS
4508 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4509 piocb->ulpContext =
4510 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4511 }
53151bbb 4512 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
dea3101e 4513 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4514
4515 /* ulpTimeout is only one byte */
4516 if (lpfc_cmd->timeout > 0xff) {
4517 /*
4518 * Do not timeout the command at the firmware level.
4519 * The driver will provide the timeout mechanism.
4520 */
4521 piocb->ulpTimeout = 0;
f1126688 4522 } else
dea3101e 4523 piocb->ulpTimeout = lpfc_cmd->timeout;
da0436e9 4524
f1126688
JS
4525 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4526 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
3772a991 4527
f1126688 4528 return 1;
3772a991
JS
4529}
4530
4531/**
25985edc 4532 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
3772a991
JS
4533 * @phba: The hba struct for which this call is being executed.
4534 * @dev_grp: The HBA PCI-Device group number.
4535 *
4536 * This routine sets up the SCSI interface API function jump table in @phba
4537 * struct.
4538 * Returns: 0 - success, -ENODEV - failure.
4539 **/
4540int
4541lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4542{
4543
f1126688
JS
4544 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4545 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
f1126688 4546
3772a991
JS
4547 switch (dev_grp) {
4548 case LPFC_PCI_DEV_LP:
4549 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4550 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
acd6859b 4551 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
3772a991 4552 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
19ca7609 4553 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
3772a991 4554 break;
da0436e9
JS
4555 case LPFC_PCI_DEV_OC:
4556 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4557 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
acd6859b 4558 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
da0436e9 4559 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
19ca7609 4560 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
da0436e9 4561 break;
3772a991
JS
4562 default:
4563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4564 "1418 Invalid HBA PCI-device group: 0x%x\n",
4565 dev_grp);
4566 return -ENODEV;
4567 break;
4568 }
3772a991 4569 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
84d1b006 4570 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3772a991
JS
4571 return 0;
4572}
4573
9bad7671 4574/**
3621a710 4575 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
9bad7671
JS
4576 * @phba: The Hba for which this call is being executed.
4577 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4578 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4579 *
4580 * This routine is IOCB completion routine for device reset and target reset
4581 * routine. This routine release scsi buffer associated with lpfc_cmd.
4582 **/
7054a606
JS
4583static void
4584lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4585 struct lpfc_iocbq *cmdiocbq,
4586 struct lpfc_iocbq *rspiocbq)
4587{
4588 struct lpfc_scsi_buf *lpfc_cmd =
4589 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4590 if (lpfc_cmd)
4591 lpfc_release_scsi_buf(phba, lpfc_cmd);
4592 return;
4593}
4594
9bad7671 4595/**
3621a710 4596 * lpfc_info - Info entry point of scsi_host_template data structure
9bad7671
JS
4597 * @host: The scsi host for which this call is being executed.
4598 *
4599 * This routine provides module information about hba.
4600 *
4601 * Reutrn code:
4602 * Pointer to char - Success.
4603 **/
dea3101e 4604const char *
4605lpfc_info(struct Scsi_Host *host)
4606{
2e0fef85
JS
4607 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4608 struct lpfc_hba *phba = vport->phba;
8b68cd52 4609 int len, link_speed = 0;
dea3101e 4610 static char lpfcinfobuf[384];
4611
4612 memset(lpfcinfobuf,0,384);
4613 if (phba && phba->pcidev){
4614 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4615 len = strlen(lpfcinfobuf);
4616 snprintf(lpfcinfobuf + len,
4617 384-len,
4618 " on PCI bus %02x device %02x irq %d",
4619 phba->pcidev->bus->number,
4620 phba->pcidev->devfn,
4621 phba->pcidev->irq);
4622 len = strlen(lpfcinfobuf);
4623 if (phba->Port[0]) {
4624 snprintf(lpfcinfobuf + len,
4625 384-len,
4626 " port %s",
4627 phba->Port);
4628 }
65467b6b 4629 len = strlen(lpfcinfobuf);
8b68cd52
JS
4630 if (phba->sli_rev <= LPFC_SLI_REV3) {
4631 link_speed = lpfc_sli_port_speed_get(phba);
4632 } else {
4633 if (phba->sli4_hba.link_state.logical_speed)
4634 link_speed =
4635 phba->sli4_hba.link_state.logical_speed;
4636 else
4637 link_speed = phba->sli4_hba.link_state.speed;
65467b6b 4638 }
8b68cd52
JS
4639 if (link_speed != 0)
4640 snprintf(lpfcinfobuf + len, 384-len,
4641 " Logical Link Speed: %d Mbps", link_speed);
dea3101e 4642 }
4643 return lpfcinfobuf;
4644}
4645
9bad7671 4646/**
3621a710 4647 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
9bad7671
JS
4648 * @phba: The Hba for which this call is being executed.
4649 *
4650 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4651 * The default value of cfg_poll_tmo is 10 milliseconds.
4652 **/
875fbdfe
JSEC
4653static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4654{
4655 unsigned long poll_tmo_expires =
4656 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4657
0e9bb8d7 4658 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
875fbdfe
JSEC
4659 mod_timer(&phba->fcp_poll_timer,
4660 poll_tmo_expires);
4661}
4662
9bad7671 4663/**
3621a710 4664 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
9bad7671
JS
4665 * @phba: The Hba for which this call is being executed.
4666 *
4667 * This routine starts the fcp_poll_timer of @phba.
4668 **/
875fbdfe
JSEC
4669void lpfc_poll_start_timer(struct lpfc_hba * phba)
4670{
4671 lpfc_poll_rearm_timer(phba);
4672}
4673
9bad7671 4674/**
3621a710 4675 * lpfc_poll_timeout - Restart polling timer
9bad7671
JS
4676 * @ptr: Map to lpfc_hba data structure pointer.
4677 *
4678 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4679 * and FCP Ring interrupt is disable.
4680 **/
4681
875fbdfe
JSEC
4682void lpfc_poll_timeout(unsigned long ptr)
4683{
2e0fef85 4684 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
875fbdfe
JSEC
4685
4686 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
4687 lpfc_sli_handle_fast_ring_event(phba,
4688 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4689
875fbdfe
JSEC
4690 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4691 lpfc_poll_rearm_timer(phba);
4692 }
875fbdfe
JSEC
4693}
4694
9bad7671 4695/**
3621a710 4696 * lpfc_queuecommand - scsi_host_template queuecommand entry point
9bad7671
JS
4697 * @cmnd: Pointer to scsi_cmnd data structure.
4698 * @done: Pointer to done routine.
4699 *
4700 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4701 * This routine prepares an IOCB from scsi command and provides to firmware.
4702 * The @done callback is invoked after driver finished processing the command.
4703 *
4704 * Return value :
4705 * 0 - Success
4706 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4707 **/
dea3101e 4708static int
b9a7c631 4709lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea3101e 4710{
2e0fef85
JS
4711 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4712 struct lpfc_hba *phba = vport->phba;
1ba981fd 4713 struct lpfc_rport_data *rdata;
1c6f4ef5 4714 struct lpfc_nodelist *ndlp;
0bd4ca25 4715 struct lpfc_scsi_buf *lpfc_cmd;
19a7b4ae 4716 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
19a7b4ae 4717 int err;
dea3101e 4718
1ba981fd 4719 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
19a7b4ae
JSEC
4720 err = fc_remote_port_chkready(rport);
4721 if (err) {
4722 cmnd->result = err;
dea3101e 4723 goto out_fail_command;
4724 }
1c6f4ef5 4725 ndlp = rdata->pnode;
dea3101e 4726
bf08611b 4727 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
acd6859b 4728 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
e2a0a9d6 4729
6a9c52cf
JS
4730 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4731 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4732 " op:%02x str=%s without registering for"
4733 " BlockGuard - Rejecting command\n",
e2a0a9d6
JS
4734 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4735 dif_op_str[scsi_get_prot_op(cmnd)]);
4736 goto out_fail_command;
4737 }
4738
dea3101e 4739 /*
19a7b4ae
JSEC
4740 * Catch race where our node has transitioned, but the
4741 * transport is still transitioning.
dea3101e 4742 */
6b415f5d
JS
4743 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4744 goto out_tgt_busy;
7dc517df 4745 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
3496343d 4746 goto out_tgt_busy;
a93ce024 4747
19ca7609 4748 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea3101e 4749 if (lpfc_cmd == NULL) {
eaf15d5b 4750 lpfc_rampdown_queue_depth(phba);
92d7f7b0 4751
e8b62011
JS
4752 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4753 "0707 driver's buffer pool is empty, "
4754 "IO busied\n");
dea3101e 4755 goto out_host_busy;
4756 }
4757
4758 /*
4759 * Store the midlayer's command structure for the completion phase
4760 * and complete the command initialization.
4761 */
4762 lpfc_cmd->pCmd = cmnd;
4763 lpfc_cmd->rdata = rdata;
4764 lpfc_cmd->timeout = 0;
977b5a0a 4765 lpfc_cmd->start_time = jiffies;
dea3101e 4766 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea3101e 4767
e2a0a9d6 4768 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
6a9c52cf 4769 if (vport->phba->cfg_enable_bg) {
737d4248
JS
4770 lpfc_printf_vlog(vport,
4771 KERN_INFO, LOG_SCSI_CMD,
2613470a
JS
4772 "9033 BLKGRD: rcvd %s cmd:x%x "
4773 "sector x%llx cnt %u pt %x\n",
4774 dif_op_str[scsi_get_prot_op(cmnd)],
4775 cmnd->cmnd[0],
4776 (unsigned long long)scsi_get_lba(cmnd),
4777 blk_rq_sectors(cmnd->request),
4778 (cmnd->cmnd[1]>>5));
6a9c52cf 4779 }
e2a0a9d6
JS
4780 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4781 } else {
6a9c52cf 4782 if (vport->phba->cfg_enable_bg) {
737d4248
JS
4783 lpfc_printf_vlog(vport,
4784 KERN_INFO, LOG_SCSI_CMD,
2613470a
JS
4785 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4786 "x%x sector x%llx cnt %u pt %x\n",
4787 cmnd->cmnd[0],
4788 (unsigned long long)scsi_get_lba(cmnd),
9a6b09c0 4789 blk_rq_sectors(cmnd->request),
2613470a 4790 (cmnd->cmnd[1]>>5));
6a9c52cf 4791 }
e2a0a9d6
JS
4792 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4793 }
4794
dea3101e 4795 if (err)
4796 goto out_host_busy_free_buf;
4797
2e0fef85 4798 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea3101e 4799
977b5a0a 4800 atomic_inc(&ndlp->cmd_pending);
3772a991 4801 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
92d7f7b0 4802 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
eaf15d5b
JS
4803 if (err) {
4804 atomic_dec(&ndlp->cmd_pending);
76f96b6d
JS
4805 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4806 "3376 FCP could not issue IOCB err %x"
4807 "FCP cmd x%x <%d/%d> "
4808 "sid: x%x did: x%x oxid: x%x "
4809 "Data: x%x x%x x%x x%x\n",
4810 err, cmnd->cmnd[0],
4811 cmnd->device ? cmnd->device->id : 0xffff,
4812 cmnd->device ? cmnd->device->lun : 0xffff,
4813 vport->fc_myDID, ndlp->nlp_DID,
4814 phba->sli_rev == LPFC_SLI_REV4 ?
4815 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4816 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4817 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4818 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4819 (uint32_t)
4820 (cmnd->request->timeout / 1000));
4821
4822
dea3101e 4823 goto out_host_busy_free_buf;
eaf15d5b 4824 }
875fbdfe 4825 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
4826 lpfc_sli_handle_fast_ring_event(phba,
4827 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4828
875fbdfe
JSEC
4829 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4830 lpfc_poll_rearm_timer(phba);
4831 }
4832
dea3101e 4833 return 0;
4834
4835 out_host_busy_free_buf:
bcf4dbfa 4836 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25 4837 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 4838 out_host_busy:
4839 return SCSI_MLQUEUE_HOST_BUSY;
4840
3496343d
MC
4841 out_tgt_busy:
4842 return SCSI_MLQUEUE_TARGET_BUSY;
4843
dea3101e 4844 out_fail_command:
b9a7c631 4845 cmnd->scsi_done(cmnd);
dea3101e 4846 return 0;
4847}
4848
f281233d 4849
9bad7671 4850/**
3621a710 4851 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
9bad7671
JS
4852 * @cmnd: Pointer to scsi_cmnd data structure.
4853 *
4854 * This routine aborts @cmnd pending in base driver.
4855 *
4856 * Return code :
4857 * 0x2003 - Error
4858 * 0x2002 - Success
4859 **/
dea3101e 4860static int
63c59c3b 4861lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea3101e 4862{
2e0fef85
JS
4863 struct Scsi_Host *shost = cmnd->device->host;
4864 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4865 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
4866 struct lpfc_iocbq *iocb;
4867 struct lpfc_iocbq *abtsiocb;
dea3101e 4868 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 4869 IOCB_t *cmd, *icmd;
3a70730a 4870 int ret = SUCCESS, status = 0;
876dd7d0 4871 unsigned long flags;
fa61a54e 4872 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
dea3101e 4873
3a70730a 4874 status = fc_block_scsi_eh(cmnd);
908e18e4 4875 if (status != 0 && status != SUCCESS)
3a70730a 4876 return status;
4f2e66c6 4877
876dd7d0 4878 spin_lock_irqsave(&phba->hbalock, flags);
4f2e66c6
JS
4879 /* driver queued commands are in process of being flushed */
4880 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
876dd7d0 4881 spin_unlock_irqrestore(&phba->hbalock, flags);
4f2e66c6
JS
4882 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4883 "3168 SCSI Layer abort requested I/O has been "
4884 "flushed by LLD.\n");
4885 return FAILED;
4886 }
4887
0bd4ca25 4888 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
92e3af66 4889 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
876dd7d0 4890 spin_unlock_irqrestore(&phba->hbalock, flags);
eee8877e
JS
4891 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4892 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
5cd049a5 4893 "x%x ID %d LUN %d\n",
3a70730a 4894 SUCCESS, cmnd->device->id, cmnd->device->lun);
eee8877e
JS
4895 return SUCCESS;
4896 }
dea3101e 4897
4f2e66c6
JS
4898 iocb = &lpfc_cmd->cur_iocbq;
4899 /* the command is in process of being cancelled */
4900 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
876dd7d0 4901 spin_unlock_irqrestore(&phba->hbalock, flags);
4f2e66c6
JS
4902 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4903 "3169 SCSI Layer abort requested I/O has been "
4904 "cancelled by LLD.\n");
4905 return FAILED;
4906 }
0bd4ca25
JSEC
4907 /*
4908 * If pCmd field of the corresponding lpfc_scsi_buf structure
4909 * points to a different SCSI command, then the driver has
4910 * already completed this command, but the midlayer did not
4f2e66c6 4911 * see the completion before the eh fired. Just return SUCCESS.
0bd4ca25 4912 */
4f2e66c6
JS
4913 if (lpfc_cmd->pCmd != cmnd) {
4914 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4915 "3170 SCSI Layer abort requested I/O has been "
4916 "completed by LLD.\n");
4917 goto out_unlock;
4918 }
dea3101e 4919
0bd4ca25 4920 BUG_ON(iocb->context1 != lpfc_cmd);
dea3101e 4921
4f2e66c6 4922 abtsiocb = __lpfc_sli_get_iocbq(phba);
0bd4ca25
JSEC
4923 if (abtsiocb == NULL) {
4924 ret = FAILED;
4f2e66c6 4925 goto out_unlock;
dea3101e 4926 }
4927
afbd8d88
JS
4928 /* Indicate the IO is being aborted by the driver. */
4929 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4930
dea3101e 4931 /*
0bd4ca25
JSEC
4932 * The scsi command can not be in txq and it is in flight because the
4933 * pCmd is still pointig at the SCSI command we have to abort. There
4934 * is no need to search the txcmplq. Just send an abort to the FW.
dea3101e 4935 */
dea3101e 4936
0bd4ca25
JSEC
4937 cmd = &iocb->iocb;
4938 icmd = &abtsiocb->iocb;
4939 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4940 icmd->un.acxri.abortContextTag = cmd->ulpContext;
3772a991
JS
4941 if (phba->sli_rev == LPFC_SLI_REV4)
4942 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4943 else
4944 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 4945
0bd4ca25
JSEC
4946 icmd->ulpLe = 1;
4947 icmd->ulpClass = cmd->ulpClass;
5ffc266e
JS
4948
4949 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4950 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
341af102 4951 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 4952
2e0fef85 4953 if (lpfc_is_link_up(phba))
0bd4ca25
JSEC
4954 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4955 else
4956 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 4957
0bd4ca25 4958 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2e0fef85 4959 abtsiocb->vport = vport;
4f2e66c6 4960 /* no longer need the lock after this point */
876dd7d0 4961 spin_unlock_irqrestore(&phba->hbalock, flags);
4f2e66c6 4962
3772a991
JS
4963 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4964 IOCB_ERROR) {
0bd4ca25
JSEC
4965 lpfc_sli_release_iocbq(phba, abtsiocb);
4966 ret = FAILED;
4967 goto out;
4968 }
dea3101e 4969
875fbdfe 4970 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
45ed1190
JS
4971 lpfc_sli_handle_fast_ring_event(phba,
4972 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
875fbdfe 4973
fa61a54e 4974 lpfc_cmd->waitq = &waitq;
0bd4ca25 4975 /* Wait for abort to complete */
fa61a54e
JS
4976 wait_event_timeout(waitq,
4977 (lpfc_cmd->pCmd != cmnd),
256ec0d0 4978 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
fa61a54e 4979 lpfc_cmd->waitq = NULL;
dea3101e 4980
0bd4ca25
JSEC
4981 if (lpfc_cmd->pCmd == cmnd) {
4982 ret = FAILED;
e8b62011
JS
4983 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4984 "0748 abort handler timed out waiting "
247ca945
JS
4985 "for abortng I/O (xri:x%x) to complete: "
4986 "ret %#x, ID %d, LUN %d\n",
4987 iocb->sli4_xritag, ret,
4988 cmnd->device->id, cmnd->device->lun);
dea3101e 4989 }
4f2e66c6 4990 goto out;
dea3101e 4991
4f2e66c6 4992out_unlock:
876dd7d0 4993 spin_unlock_irqrestore(&phba->hbalock, flags);
4f2e66c6 4994out:
e8b62011
JS
4995 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4996 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
5cd049a5
CH
4997 "LUN %d\n", ret, cmnd->device->id,
4998 cmnd->device->lun);
63c59c3b 4999 return ret;
8fa728a2
JG
5000}
5001
bbb9d180
JS
5002static char *
5003lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
5004{
5005 switch (task_mgmt_cmd) {
5006 case FCP_ABORT_TASK_SET:
5007 return "ABORT_TASK_SET";
5008 case FCP_CLEAR_TASK_SET:
5009 return "FCP_CLEAR_TASK_SET";
5010 case FCP_BUS_RESET:
5011 return "FCP_BUS_RESET";
5012 case FCP_LUN_RESET:
5013 return "FCP_LUN_RESET";
5014 case FCP_TARGET_RESET:
5015 return "FCP_TARGET_RESET";
5016 case FCP_CLEAR_ACA:
5017 return "FCP_CLEAR_ACA";
5018 case FCP_TERMINATE_TASK:
5019 return "FCP_TERMINATE_TASK";
5020 default:
5021 return "unknown";
5022 }
5023}
5024
53151bbb
JS
5025
5026/**
5027 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
5028 * @vport: The virtual port for which this call is being executed.
5029 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
5030 *
5031 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
5032 *
5033 * Return code :
5034 * 0x2003 - Error
5035 * 0x2002 - Success
5036 **/
5037static int
5038lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
5039{
5040 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
5041 uint32_t rsp_info;
5042 uint32_t rsp_len;
5043 uint8_t rsp_info_code;
5044 int ret = FAILED;
5045
5046
5047 if (fcprsp == NULL)
5048 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5049 "0703 fcp_rsp is missing\n");
5050 else {
5051 rsp_info = fcprsp->rspStatus2;
5052 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
5053 rsp_info_code = fcprsp->rspInfo3;
5054
5055
5056 lpfc_printf_vlog(vport, KERN_INFO,
5057 LOG_FCP,
5058 "0706 fcp_rsp valid 0x%x,"
5059 " rsp len=%d code 0x%x\n",
5060 rsp_info,
5061 rsp_len, rsp_info_code);
5062
5063 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
5064 switch (rsp_info_code) {
5065 case RSP_NO_FAILURE:
5066 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5067 "0715 Task Mgmt No Failure\n");
5068 ret = SUCCESS;
5069 break;
5070 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
5071 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5072 "0716 Task Mgmt Target "
5073 "reject\n");
5074 break;
5075 case RSP_TM_NOT_COMPLETED: /* TM failed */
5076 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5077 "0717 Task Mgmt Target "
5078 "failed TM\n");
5079 break;
5080 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5081 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5082 "0718 Task Mgmt to invalid "
5083 "LUN\n");
5084 break;
5085 }
5086 }
5087 }
5088 return ret;
5089}
5090
5091
9bad7671 5092/**
bbb9d180
JS
5093 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5094 * @vport: The virtual port for which this call is being executed.
5095 * @rdata: Pointer to remote port local data
5096 * @tgt_id: Target ID of remote device.
5097 * @lun_id: Lun number for the TMF
5098 * @task_mgmt_cmd: type of TMF to send
9bad7671 5099 *
bbb9d180
JS
5100 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5101 * a remote port.
9bad7671 5102 *
bbb9d180
JS
5103 * Return Code:
5104 * 0x2003 - Error
5105 * 0x2002 - Success.
9bad7671 5106 **/
dea3101e 5107static int
bbb9d180
JS
5108lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
5109 unsigned tgt_id, unsigned int lun_id,
5110 uint8_t task_mgmt_cmd)
dea3101e 5111{
2e0fef85 5112 struct lpfc_hba *phba = vport->phba;
0bd4ca25 5113 struct lpfc_scsi_buf *lpfc_cmd;
bbb9d180
JS
5114 struct lpfc_iocbq *iocbq;
5115 struct lpfc_iocbq *iocbqrsp;
5989b8d4 5116 struct lpfc_nodelist *pnode = rdata->pnode;
bbb9d180 5117 int ret;
915caaaf 5118 int status;
dea3101e 5119
5989b8d4 5120 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
915caaaf 5121 return FAILED;
bbb9d180 5122
19ca7609 5123 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
dea3101e 5124 if (lpfc_cmd == NULL)
915caaaf 5125 return FAILED;
0c411222 5126 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
0b18ac42 5127 lpfc_cmd->rdata = rdata;
dea3101e 5128
bbb9d180
JS
5129 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5130 task_mgmt_cmd);
915caaaf
JS
5131 if (!status) {
5132 lpfc_release_scsi_buf(phba, lpfc_cmd);
5133 return FAILED;
5134 }
dea3101e 5135
bbb9d180 5136 iocbq = &lpfc_cmd->cur_iocbq;
0bd4ca25 5137 iocbqrsp = lpfc_sli_get_iocbq(phba);
915caaaf
JS
5138 if (iocbqrsp == NULL) {
5139 lpfc_release_scsi_buf(phba, lpfc_cmd);
5140 return FAILED;
5141 }
5a0916b4 5142 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
bbb9d180 5143
e8b62011 5144 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
bbb9d180 5145 "0702 Issue %s to TGT %d LUN %d "
6d368e53 5146 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
bbb9d180 5147 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
6d368e53
JS
5148 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5149 iocbq->iocb_flag);
bbb9d180 5150
3772a991 5151 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
915caaaf 5152 iocbq, iocbqrsp, lpfc_cmd->timeout);
53151bbb
JS
5153 if ((status != IOCB_SUCCESS) ||
5154 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
bbb9d180 5155 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
6d368e53
JS
5156 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5157 "iocb_flag x%x\n",
bbb9d180
JS
5158 lpfc_taskmgmt_name(task_mgmt_cmd),
5159 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
6d368e53
JS
5160 iocbqrsp->iocb.un.ulpWord[4],
5161 iocbq->iocb_flag);
53151bbb
JS
5162 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5163 if (status == IOCB_SUCCESS) {
5164 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5165 /* Something in the FCP_RSP was invalid.
5166 * Check conditions */
5167 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5168 else
5169 ret = FAILED;
5170 } else if (status == IOCB_TIMEDOUT) {
5171 ret = TIMEOUT_ERROR;
5172 } else {
5173 ret = FAILED;
5174 }
5175 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5176 } else
bbb9d180
JS
5177 ret = SUCCESS;
5178
6175c02a 5179 lpfc_sli_release_iocbq(phba, iocbqrsp);
bbb9d180
JS
5180
5181 if (ret != TIMEOUT_ERROR)
5182 lpfc_release_scsi_buf(phba, lpfc_cmd);
5183
5184 return ret;
5185}
5186
5187/**
5188 * lpfc_chk_tgt_mapped -
5189 * @vport: The virtual port to check on
5190 * @cmnd: Pointer to scsi_cmnd data structure.
5191 *
5192 * This routine delays until the scsi target (aka rport) for the
5193 * command exists (is present and logged in) or we declare it non-existent.
5194 *
5195 * Return code :
5196 * 0x2003 - Error
5197 * 0x2002 - Success
5198 **/
5199static int
5200lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5201{
1ba981fd 5202 struct lpfc_rport_data *rdata;
1c6f4ef5 5203 struct lpfc_nodelist *pnode;
bbb9d180
JS
5204 unsigned long later;
5205
1ba981fd 5206 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
1c6f4ef5
JS
5207 if (!rdata) {
5208 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5209 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5210 return FAILED;
5211 }
5212 pnode = rdata->pnode;
bbb9d180
JS
5213 /*
5214 * If target is not in a MAPPED state, delay until
5215 * target is rediscovered or devloss timeout expires.
5216 */
5217 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5218 while (time_after(later, jiffies)) {
5219 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5220 return FAILED;
5221 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5222 return SUCCESS;
5223 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1ba981fd 5224 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
bbb9d180
JS
5225 if (!rdata)
5226 return FAILED;
5227 pnode = rdata->pnode;
5228 }
5229 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5230 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5231 return FAILED;
5232 return SUCCESS;
5233}
5234
5235/**
5236 * lpfc_reset_flush_io_context -
5237 * @vport: The virtual port (scsi_host) for the flush context
5238 * @tgt_id: If aborting by Target contect - specifies the target id
5239 * @lun_id: If aborting by Lun context - specifies the lun id
5240 * @context: specifies the context level to flush at.
5241 *
5242 * After a reset condition via TMF, we need to flush orphaned i/o
5243 * contexts from the adapter. This routine aborts any contexts
5244 * outstanding, then waits for their completions. The wait is
5245 * bounded by devloss_tmo though.
5246 *
5247 * Return code :
5248 * 0x2003 - Error
5249 * 0x2002 - Success
5250 **/
5251static int
5252lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5253 uint64_t lun_id, lpfc_ctx_cmd context)
5254{
5255 struct lpfc_hba *phba = vport->phba;
5256 unsigned long later;
5257 int cnt;
5258
5259 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
6175c02a 5260 if (cnt)
51ef4c26 5261 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
bbb9d180 5262 tgt_id, lun_id, context);
915caaaf
JS
5263 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5264 while (time_after(later, jiffies) && cnt) {
5265 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
bbb9d180 5266 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
dea3101e 5267 }
dea3101e 5268 if (cnt) {
e8b62011 5269 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
bbb9d180
JS
5270 "0724 I/O flush failure for context %s : cnt x%x\n",
5271 ((context == LPFC_CTX_LUN) ? "LUN" :
5272 ((context == LPFC_CTX_TGT) ? "TGT" :
5273 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5274 cnt);
5275 return FAILED;
dea3101e 5276 }
bbb9d180
JS
5277 return SUCCESS;
5278}
5279
5280/**
5281 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
5282 * @cmnd: Pointer to scsi_cmnd data structure.
5283 *
5284 * This routine does a device reset by sending a LUN_RESET task management
5285 * command.
5286 *
5287 * Return code :
5288 * 0x2003 - Error
5289 * 0x2002 - Success
5290 **/
5291static int
5292lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
5293{
5294 struct Scsi_Host *shost = cmnd->device->host;
5295 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1ba981fd 5296 struct lpfc_rport_data *rdata;
1c6f4ef5 5297 struct lpfc_nodelist *pnode;
bbb9d180
JS
5298 unsigned tgt_id = cmnd->device->id;
5299 unsigned int lun_id = cmnd->device->lun;
5300 struct lpfc_scsi_event_header scsi_event;
53151bbb 5301 int status;
bbb9d180 5302
1ba981fd 5303 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
1c6f4ef5
JS
5304 if (!rdata) {
5305 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5306 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5307 return FAILED;
5308 }
5309 pnode = rdata->pnode;
589a52d6 5310 status = fc_block_scsi_eh(cmnd);
908e18e4 5311 if (status != 0 && status != SUCCESS)
589a52d6 5312 return status;
bbb9d180
JS
5313
5314 status = lpfc_chk_tgt_mapped(vport, cmnd);
5315 if (status == FAILED) {
5316 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5317 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5318 return FAILED;
5319 }
5320
5321 scsi_event.event_type = FC_REG_SCSI_EVENT;
5322 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5323 scsi_event.lun = lun_id;
5324 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5325 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5326
5327 fc_host_post_vendor_event(shost, fc_get_event_number(),
5328 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5329
5330 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5331 FCP_LUN_RESET);
5332
5333 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5334 "0713 SCSI layer issued Device Reset (%d, %d) "
5335 "return x%x\n", tgt_id, lun_id, status);
5336
5337 /*
5338 * We have to clean up i/o as : they may be orphaned by the TMF;
5339 * or if the TMF failed, they may be in an indeterminate state.
5340 * So, continue on.
5341 * We will report success if all the i/o aborts successfully.
5342 */
53151bbb
JS
5343 if (status == SUCCESS)
5344 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
bbb9d180 5345 LPFC_CTX_LUN);
53151bbb
JS
5346
5347 return status;
bbb9d180
JS
5348}
5349
5350/**
5351 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5352 * @cmnd: Pointer to scsi_cmnd data structure.
5353 *
5354 * This routine does a target reset by sending a TARGET_RESET task management
5355 * command.
5356 *
5357 * Return code :
5358 * 0x2003 - Error
5359 * 0x2002 - Success
5360 **/
5361static int
5362lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5363{
5364 struct Scsi_Host *shost = cmnd->device->host;
5365 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1ba981fd 5366 struct lpfc_rport_data *rdata;
1c6f4ef5 5367 struct lpfc_nodelist *pnode;
bbb9d180
JS
5368 unsigned tgt_id = cmnd->device->id;
5369 unsigned int lun_id = cmnd->device->lun;
5370 struct lpfc_scsi_event_header scsi_event;
53151bbb 5371 int status;
bbb9d180 5372
1ba981fd 5373 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
1c6f4ef5
JS
5374 if (!rdata) {
5375 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5376 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5377 return FAILED;
5378 }
5379 pnode = rdata->pnode;
589a52d6 5380 status = fc_block_scsi_eh(cmnd);
908e18e4 5381 if (status != 0 && status != SUCCESS)
589a52d6 5382 return status;
bbb9d180
JS
5383
5384 status = lpfc_chk_tgt_mapped(vport, cmnd);
5385 if (status == FAILED) {
5386 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5387 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5388 return FAILED;
5389 }
5390
5391 scsi_event.event_type = FC_REG_SCSI_EVENT;
5392 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5393 scsi_event.lun = 0;
5394 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5395 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5396
5397 fc_host_post_vendor_event(shost, fc_get_event_number(),
5398 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5399
5400 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5401 FCP_TARGET_RESET);
5402
5403 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5404 "0723 SCSI layer issued Target Reset (%d, %d) "
5405 "return x%x\n", tgt_id, lun_id, status);
5406
5407 /*
5408 * We have to clean up i/o as : they may be orphaned by the TMF;
5409 * or if the TMF failed, they may be in an indeterminate state.
5410 * So, continue on.
5411 * We will report success if all the i/o aborts successfully.
5412 */
53151bbb
JS
5413 if (status == SUCCESS)
5414 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3a70730a 5415 LPFC_CTX_TGT);
53151bbb 5416 return status;
dea3101e 5417}
5418
9bad7671 5419/**
3621a710 5420 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
9bad7671
JS
5421 * @cmnd: Pointer to scsi_cmnd data structure.
5422 *
bbb9d180
JS
5423 * This routine does target reset to all targets on @cmnd->device->host.
5424 * This emulates Parallel SCSI Bus Reset Semantics.
9bad7671 5425 *
bbb9d180
JS
5426 * Return code :
5427 * 0x2003 - Error
5428 * 0x2002 - Success
9bad7671 5429 **/
94d0e7b8 5430static int
7054a606 5431lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 5432{
2e0fef85
JS
5433 struct Scsi_Host *shost = cmnd->device->host;
5434 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea3101e 5435 struct lpfc_nodelist *ndlp = NULL;
ea2151b4 5436 struct lpfc_scsi_event_header scsi_event;
bbb9d180
JS
5437 int match;
5438 int ret = SUCCESS, status, i;
ea2151b4
JS
5439
5440 scsi_event.event_type = FC_REG_SCSI_EVENT;
5441 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5442 scsi_event.lun = 0;
5443 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5444 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5445
bbb9d180
JS
5446 fc_host_post_vendor_event(shost, fc_get_event_number(),
5447 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea3101e 5448
bf08611b 5449 status = fc_block_scsi_eh(cmnd);
908e18e4 5450 if (status != 0 && status != SUCCESS)
bf08611b 5451 return status;
bbb9d180 5452
dea3101e 5453 /*
5454 * Since the driver manages a single bus device, reset all
5455 * targets known to the driver. Should any target reset
5456 * fail, this routine returns failure to the midlayer.
5457 */
e17da18e 5458 for (i = 0; i < LPFC_MAX_TARGET; i++) {
685f0bf7 5459 /* Search for mapped node by target ID */
dea3101e 5460 match = 0;
2e0fef85
JS
5461 spin_lock_irq(shost->host_lock);
5462 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
5463 if (!NLP_CHK_NODE_ACT(ndlp))
5464 continue;
a6571c6e
JS
5465 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5466 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5467 continue;
685f0bf7 5468 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
915caaaf 5469 ndlp->nlp_sid == i &&
685f0bf7 5470 ndlp->rport) {
dea3101e 5471 match = 1;
5472 break;
5473 }
5474 }
2e0fef85 5475 spin_unlock_irq(shost->host_lock);
dea3101e 5476 if (!match)
5477 continue;
bbb9d180
JS
5478
5479 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5480 i, 0, FCP_TARGET_RESET);
5481
5482 if (status != SUCCESS) {
e8b62011
JS
5483 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5484 "0700 Bus Reset on target %d failed\n",
5485 i);
915caaaf 5486 ret = FAILED;
dea3101e 5487 }
5488 }
6175c02a 5489 /*
bbb9d180
JS
5490 * We have to clean up i/o as : they may be orphaned by the TMFs
5491 * above; or if any of the TMFs failed, they may be in an
5492 * indeterminate state.
5493 * We will report success if all the i/o aborts successfully.
6175c02a 5494 */
bbb9d180
JS
5495
5496 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5497 if (status != SUCCESS)
0bd4ca25 5498 ret = FAILED;
bbb9d180 5499
e8b62011
JS
5500 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5501 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea3101e 5502 return ret;
5503}
5504
27b01b82
JS
5505/**
5506 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5507 * @cmnd: Pointer to scsi_cmnd data structure.
5508 *
5509 * This routine does host reset to the adaptor port. It brings the HBA
5510 * offline, performs a board restart, and then brings the board back online.
5511 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5512 * reject all outstanding SCSI commands to the host and error returned
5513 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5514 * of error handling, it will only return error if resetting of the adapter
5515 * is not successful; in all other cases, will return success.
5516 *
5517 * Return code :
5518 * 0x2003 - Error
5519 * 0x2002 - Success
5520 **/
5521static int
5522lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5523{
5524 struct Scsi_Host *shost = cmnd->device->host;
5525 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5526 struct lpfc_hba *phba = vport->phba;
5527 int rc, ret = SUCCESS;
5528
a88dbb6a
JS
5529 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5530 "3172 SCSI layer issued Host Reset Data:\n");
5531
618a5230 5532 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
27b01b82
JS
5533 lpfc_offline(phba);
5534 rc = lpfc_sli_brdrestart(phba);
5535 if (rc)
5536 ret = FAILED;
a88dbb6a
JS
5537 rc = lpfc_online(phba);
5538 if (rc)
5539 ret = FAILED;
27b01b82
JS
5540 lpfc_unblock_mgmt_io(phba);
5541
a88dbb6a
JS
5542 if (ret == FAILED) {
5543 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5544 "3323 Failed host reset, bring it offline\n");
5545 lpfc_sli4_offline_eratt(phba);
5546 }
27b01b82
JS
5547 return ret;
5548}
5549
9bad7671 5550/**
3621a710 5551 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
9bad7671
JS
5552 * @sdev: Pointer to scsi_device.
5553 *
5554 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5555 * globally available list of scsi buffers. This routine also makes sure scsi
5556 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5557 * of scsi buffer exists for the lifetime of the driver.
5558 *
5559 * Return codes:
5560 * non-0 - Error
5561 * 0 - Success
5562 **/
dea3101e 5563static int
5564lpfc_slave_alloc(struct scsi_device *sdev)
5565{
2e0fef85
JS
5566 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5567 struct lpfc_hba *phba = vport->phba;
19a7b4ae 5568 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3772a991 5569 uint32_t total = 0;
dea3101e 5570 uint32_t num_to_alloc = 0;
3772a991 5571 int num_allocated = 0;
d7c47992 5572 uint32_t sdev_cnt;
1ba981fd
JS
5573 struct lpfc_device_data *device_data;
5574 unsigned long flags;
5575 struct lpfc_name target_wwpn;
dea3101e 5576
19a7b4ae 5577 if (!rport || fc_remote_port_chkready(rport))
dea3101e 5578 return -ENXIO;
5579
1ba981fd
JS
5580 if (phba->cfg_EnableXLane) {
5581
5582 /*
5583 * Check to see if the device data structure for the lun
5584 * exists. If not, create one.
5585 */
5586
5587 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5588 spin_lock_irqsave(&phba->devicelock, flags);
5589 device_data = __lpfc_get_device_data(phba,
5590 &phba->luns,
5591 &vport->fc_portname,
5592 &target_wwpn,
5593 sdev->lun);
5594 if (!device_data) {
5595 spin_unlock_irqrestore(&phba->devicelock, flags);
5596 device_data = lpfc_create_device_data(phba,
5597 &vport->fc_portname,
5598 &target_wwpn,
5599 sdev->lun, true);
5600 if (!device_data)
5601 return -ENOMEM;
5602 spin_lock_irqsave(&phba->devicelock, flags);
5603 list_add_tail(&device_data->listentry, &phba->luns);
5604 }
5605 device_data->rport_data = rport->dd_data;
5606 device_data->available = true;
5607 spin_unlock_irqrestore(&phba->devicelock, flags);
5608 sdev->hostdata = device_data;
5609 } else {
5610 sdev->hostdata = rport->dd_data;
5611 }
d7c47992 5612 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea3101e 5613
5614 /*
5615 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5616 * available list of scsi buffers. Don't allocate more than the
a784efbf
JSEC
5617 * HBA limit conveyed to the midlayer via the host structure. The
5618 * formula accounts for the lun_queue_depth + error handlers + 1
5619 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea3101e 5620 */
5621 total = phba->total_scsi_bufs;
3de2a653 5622 num_to_alloc = vport->cfg_lun_queue_depth + 2;
92d7f7b0 5623
d7c47992
JS
5624 /* If allocated buffers are enough do nothing */
5625 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5626 return 0;
5627
92d7f7b0
JS
5628 /* Allow some exchanges to be available always to complete discovery */
5629 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
5630 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5631 "0704 At limitation of %d preallocated "
5632 "command buffers\n", total);
dea3101e 5633 return 0;
92d7f7b0
JS
5634 /* Allow some exchanges to be available always to complete discovery */
5635 } else if (total + num_to_alloc >
5636 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
5637 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5638 "0705 Allocation request of %d "
5639 "command buffers will exceed max of %d. "
5640 "Reducing allocation request to %d.\n",
5641 num_to_alloc, phba->cfg_hba_queue_depth,
5642 (phba->cfg_hba_queue_depth - total));
dea3101e 5643 num_to_alloc = phba->cfg_hba_queue_depth - total;
5644 }
3772a991
JS
5645 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5646 if (num_to_alloc != num_allocated) {
96f7077f
JS
5647 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5648 "0708 Allocation request of %d "
5649 "command buffers did not succeed. "
5650 "Allocated %d buffers.\n",
5651 num_to_alloc, num_allocated);
dea3101e 5652 }
1c6f4ef5
JS
5653 if (num_allocated > 0)
5654 phba->total_scsi_bufs += num_allocated;
dea3101e 5655 return 0;
5656}
5657
9bad7671 5658/**
3621a710 5659 * lpfc_slave_configure - scsi_host_template slave_configure entry point
9bad7671
JS
5660 * @sdev: Pointer to scsi_device.
5661 *
5662 * This routine configures following items
5663 * - Tag command queuing support for @sdev if supported.
9bad7671
JS
5664 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5665 *
5666 * Return codes:
5667 * 0 - Success
5668 **/
dea3101e 5669static int
5670lpfc_slave_configure(struct scsi_device *sdev)
5671{
2e0fef85
JS
5672 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5673 struct lpfc_hba *phba = vport->phba;
dea3101e 5674
5675 if (sdev->tagged_supported)
3de2a653 5676 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 5677 else
3de2a653 5678 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 5679
875fbdfe 5680 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
5681 lpfc_sli_handle_fast_ring_event(phba,
5682 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
875fbdfe
JSEC
5683 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5684 lpfc_poll_rearm_timer(phba);
5685 }
5686
dea3101e 5687 return 0;
5688}
5689
9bad7671 5690/**
3621a710 5691 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
9bad7671
JS
5692 * @sdev: Pointer to scsi_device.
5693 *
5694 * This routine sets @sdev hostatdata filed to null.
5695 **/
dea3101e 5696static void
5697lpfc_slave_destroy(struct scsi_device *sdev)
5698{
d7c47992
JS
5699 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5700 struct lpfc_hba *phba = vport->phba;
1ba981fd
JS
5701 unsigned long flags;
5702 struct lpfc_device_data *device_data = sdev->hostdata;
5703
d7c47992 5704 atomic_dec(&phba->sdev_cnt);
1ba981fd
JS
5705 if ((phba->cfg_EnableXLane) && (device_data)) {
5706 spin_lock_irqsave(&phba->devicelock, flags);
5707 device_data->available = false;
5708 if (!device_data->oas_enabled)
5709 lpfc_delete_device_data(phba, device_data);
5710 spin_unlock_irqrestore(&phba->devicelock, flags);
5711 }
dea3101e 5712 sdev->hostdata = NULL;
5713 return;
5714}
5715
1ba981fd
JS
5716/**
5717 * lpfc_create_device_data - creates and initializes device data structure for OAS
5718 * @pha: Pointer to host bus adapter structure.
5719 * @vport_wwpn: Pointer to vport's wwpn information
5720 * @target_wwpn: Pointer to target's wwpn information
5721 * @lun: Lun on target
5722 * @atomic_create: Flag to indicate if memory should be allocated using the
5723 * GFP_ATOMIC flag or not.
5724 *
5725 * This routine creates a device data structure which will contain identifying
5726 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5727 * whether or not the corresponding lun is available by the system,
5728 * and pointer to the rport data.
5729 *
5730 * Return codes:
5731 * NULL - Error
5732 * Pointer to lpfc_device_data - Success
5733 **/
5734struct lpfc_device_data*
5735lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5736 struct lpfc_name *target_wwpn, uint64_t lun,
5737 bool atomic_create)
5738{
5739
5740 struct lpfc_device_data *lun_info;
5741 int memory_flags;
5742
5743 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5744 !(phba->cfg_EnableXLane))
5745 return NULL;
5746
5747 /* Attempt to create the device data to contain lun info */
5748
5749 if (atomic_create)
5750 memory_flags = GFP_ATOMIC;
5751 else
5752 memory_flags = GFP_KERNEL;
5753 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5754 if (!lun_info)
5755 return NULL;
5756 INIT_LIST_HEAD(&lun_info->listentry);
5757 lun_info->rport_data = NULL;
5758 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5759 sizeof(struct lpfc_name));
5760 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5761 sizeof(struct lpfc_name));
5762 lun_info->device_id.lun = lun;
5763 lun_info->oas_enabled = false;
5764 lun_info->available = false;
5765 return lun_info;
5766}
5767
5768/**
5769 * lpfc_delete_device_data - frees a device data structure for OAS
5770 * @pha: Pointer to host bus adapter structure.
5771 * @lun_info: Pointer to device data structure to free.
5772 *
5773 * This routine frees the previously allocated device data structure passed.
5774 *
5775 **/
5776void
5777lpfc_delete_device_data(struct lpfc_hba *phba,
5778 struct lpfc_device_data *lun_info)
5779{
5780
5781 if (unlikely(!phba) || !lun_info ||
5782 !(phba->cfg_EnableXLane))
5783 return;
5784
5785 if (!list_empty(&lun_info->listentry))
5786 list_del(&lun_info->listentry);
5787 mempool_free(lun_info, phba->device_data_mem_pool);
5788 return;
5789}
5790
5791/**
5792 * __lpfc_get_device_data - returns the device data for the specified lun
5793 * @pha: Pointer to host bus adapter structure.
5794 * @list: Point to list to search.
5795 * @vport_wwpn: Pointer to vport's wwpn information
5796 * @target_wwpn: Pointer to target's wwpn information
5797 * @lun: Lun on target
5798 *
5799 * This routine searches the list passed for the specified lun's device data.
5800 * This function does not hold locks, it is the responsibility of the caller
5801 * to ensure the proper lock is held before calling the function.
5802 *
5803 * Return codes:
5804 * NULL - Error
5805 * Pointer to lpfc_device_data - Success
5806 **/
5807struct lpfc_device_data*
5808__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5809 struct lpfc_name *vport_wwpn,
5810 struct lpfc_name *target_wwpn, uint64_t lun)
5811{
5812
5813 struct lpfc_device_data *lun_info;
5814
5815 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
5816 !phba->cfg_EnableXLane)
5817 return NULL;
5818
5819 /* Check to see if the lun is already enabled for OAS. */
5820
5821 list_for_each_entry(lun_info, list, listentry) {
5822 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5823 sizeof(struct lpfc_name)) == 0) &&
5824 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5825 sizeof(struct lpfc_name)) == 0) &&
5826 (lun_info->device_id.lun == lun))
5827 return lun_info;
5828 }
5829
5830 return NULL;
5831}
5832
5833/**
5834 * lpfc_find_next_oas_lun - searches for the next oas lun
5835 * @pha: Pointer to host bus adapter structure.
5836 * @vport_wwpn: Pointer to vport's wwpn information
5837 * @target_wwpn: Pointer to target's wwpn information
5838 * @starting_lun: Pointer to the lun to start searching for
5839 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5840 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5841 * @found_lun: Pointer to the found lun.
5842 * @found_lun_status: Pointer to status of the found lun.
5843 *
5844 * This routine searches the luns list for the specified lun
5845 * or the first lun for the vport/target. If the vport wwpn contains
5846 * a zero value then a specific vport is not specified. In this case
5847 * any vport which contains the lun will be considered a match. If the
5848 * target wwpn contains a zero value then a specific target is not specified.
5849 * In this case any target which contains the lun will be considered a
5850 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5851 * are returned. The function will also return the next lun if available.
5852 * If the next lun is not found, starting_lun parameter will be set to
5853 * NO_MORE_OAS_LUN.
5854 *
5855 * Return codes:
5856 * non-0 - Error
5857 * 0 - Success
5858 **/
5859bool
5860lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5861 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5862 struct lpfc_name *found_vport_wwpn,
5863 struct lpfc_name *found_target_wwpn,
5864 uint64_t *found_lun,
5865 uint32_t *found_lun_status)
5866{
5867
5868 unsigned long flags;
5869 struct lpfc_device_data *lun_info;
5870 struct lpfc_device_id *device_id;
5871 uint64_t lun;
5872 bool found = false;
5873
5874 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5875 !starting_lun || !found_vport_wwpn ||
5876 !found_target_wwpn || !found_lun || !found_lun_status ||
5877 (*starting_lun == NO_MORE_OAS_LUN) ||
5878 !phba->cfg_EnableXLane)
5879 return false;
5880
5881 lun = *starting_lun;
5882 *found_lun = NO_MORE_OAS_LUN;
5883 *starting_lun = NO_MORE_OAS_LUN;
5884
5885 /* Search for lun or the lun closet in value */
5886
5887 spin_lock_irqsave(&phba->devicelock, flags);
5888 list_for_each_entry(lun_info, &phba->luns, listentry) {
5889 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5890 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5891 sizeof(struct lpfc_name)) == 0)) &&
5892 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5893 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5894 sizeof(struct lpfc_name)) == 0)) &&
5895 (lun_info->oas_enabled)) {
5896 device_id = &lun_info->device_id;
5897 if ((!found) &&
5898 ((lun == FIND_FIRST_OAS_LUN) ||
5899 (device_id->lun == lun))) {
5900 *found_lun = device_id->lun;
5901 memcpy(found_vport_wwpn,
5902 &device_id->vport_wwpn,
5903 sizeof(struct lpfc_name));
5904 memcpy(found_target_wwpn,
5905 &device_id->target_wwpn,
5906 sizeof(struct lpfc_name));
5907 if (lun_info->available)
5908 *found_lun_status =
5909 OAS_LUN_STATUS_EXISTS;
5910 else
5911 *found_lun_status = 0;
5912 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5913 memset(vport_wwpn, 0x0,
5914 sizeof(struct lpfc_name));
5915 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5916 memset(target_wwpn, 0x0,
5917 sizeof(struct lpfc_name));
5918 found = true;
5919 } else if (found) {
5920 *starting_lun = device_id->lun;
5921 memcpy(vport_wwpn, &device_id->vport_wwpn,
5922 sizeof(struct lpfc_name));
5923 memcpy(target_wwpn, &device_id->target_wwpn,
5924 sizeof(struct lpfc_name));
5925 break;
5926 }
5927 }
5928 }
5929 spin_unlock_irqrestore(&phba->devicelock, flags);
5930 return found;
5931}
5932
5933/**
5934 * lpfc_enable_oas_lun - enables a lun for OAS operations
5935 * @pha: Pointer to host bus adapter structure.
5936 * @vport_wwpn: Pointer to vport's wwpn information
5937 * @target_wwpn: Pointer to target's wwpn information
5938 * @lun: Lun
5939 *
5940 * This routine enables a lun for oas operations. The routines does so by
5941 * doing the following :
5942 *
5943 * 1) Checks to see if the device data for the lun has been created.
5944 * 2) If found, sets the OAS enabled flag if not set and returns.
5945 * 3) Otherwise, creates a device data structure.
5946 * 4) If successfully created, indicates the device data is for an OAS lun,
5947 * indicates the lun is not available and add to the list of luns.
5948 *
5949 * Return codes:
5950 * false - Error
5951 * true - Success
5952 **/
5953bool
5954lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5955 struct lpfc_name *target_wwpn, uint64_t lun)
5956{
5957
5958 struct lpfc_device_data *lun_info;
5959 unsigned long flags;
5960
5961 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5962 !phba->cfg_EnableXLane)
5963 return false;
5964
5965 spin_lock_irqsave(&phba->devicelock, flags);
5966
5967 /* Check to see if the device data for the lun has been created */
5968 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5969 target_wwpn, lun);
5970 if (lun_info) {
5971 if (!lun_info->oas_enabled)
5972 lun_info->oas_enabled = true;
5973 spin_unlock_irqrestore(&phba->devicelock, flags);
5974 return true;
5975 }
5976
5977 /* Create an lun info structure and add to list of luns */
5978 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
5979 false);
5980 if (lun_info) {
5981 lun_info->oas_enabled = true;
5982 lun_info->available = false;
5983 list_add_tail(&lun_info->listentry, &phba->luns);
5984 spin_unlock_irqrestore(&phba->devicelock, flags);
5985 return true;
5986 }
5987 spin_unlock_irqrestore(&phba->devicelock, flags);
5988 return false;
5989}
5990
5991/**
5992 * lpfc_disable_oas_lun - disables a lun for OAS operations
5993 * @pha: Pointer to host bus adapter structure.
5994 * @vport_wwpn: Pointer to vport's wwpn information
5995 * @target_wwpn: Pointer to target's wwpn information
5996 * @lun: Lun
5997 *
5998 * This routine disables a lun for oas operations. The routines does so by
5999 * doing the following :
6000 *
6001 * 1) Checks to see if the device data for the lun is created.
6002 * 2) If present, clears the flag indicating this lun is for OAS.
6003 * 3) If the lun is not available by the system, the device data is
6004 * freed.
6005 *
6006 * Return codes:
6007 * false - Error
6008 * true - Success
6009 **/
6010bool
6011lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6012 struct lpfc_name *target_wwpn, uint64_t lun)
6013{
6014
6015 struct lpfc_device_data *lun_info;
6016 unsigned long flags;
6017
6018 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
6019 !phba->cfg_EnableXLane)
6020 return false;
6021
6022 spin_lock_irqsave(&phba->devicelock, flags);
6023
6024 /* Check to see if the lun is available. */
6025 lun_info = __lpfc_get_device_data(phba,
6026 &phba->luns, vport_wwpn,
6027 target_wwpn, lun);
6028 if (lun_info) {
6029 lun_info->oas_enabled = false;
6030 if (!lun_info->available)
6031 lpfc_delete_device_data(phba, lun_info);
6032 spin_unlock_irqrestore(&phba->devicelock, flags);
6033 return true;
6034 }
6035
6036 spin_unlock_irqrestore(&phba->devicelock, flags);
6037 return false;
6038}
92d7f7b0 6039
dea3101e 6040struct scsi_host_template lpfc_template = {
6041 .module = THIS_MODULE,
6042 .name = LPFC_DRIVER_NAME,
6043 .info = lpfc_info,
6044 .queuecommand = lpfc_queuecommand,
6045 .eh_abort_handler = lpfc_abort_handler,
bbb9d180
JS
6046 .eh_device_reset_handler = lpfc_device_reset_handler,
6047 .eh_target_reset_handler = lpfc_target_reset_handler,
7054a606 6048 .eh_bus_reset_handler = lpfc_bus_reset_handler,
27b01b82 6049 .eh_host_reset_handler = lpfc_host_reset_handler,
dea3101e 6050 .slave_alloc = lpfc_slave_alloc,
6051 .slave_configure = lpfc_slave_configure,
6052 .slave_destroy = lpfc_slave_destroy,
47a8617c 6053 .scan_finished = lpfc_scan_finished,
dea3101e 6054 .this_id = -1,
83108bd3 6055 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea3101e 6056 .cmd_per_lun = LPFC_CMD_PER_LUN,
6057 .use_clustering = ENABLE_CLUSTERING,
2e0fef85 6058 .shost_attrs = lpfc_hba_attrs,
564b2960 6059 .max_sectors = 0xFFFF,
f1c3b0fc 6060 .vendor_id = LPFC_NL_VENDOR_ID,
5ffc266e 6061 .change_queue_depth = lpfc_change_queue_depth,
fe8f7f9c 6062 .change_queue_type = lpfc_change_queue_type,
dea3101e 6063};
3de2a653
JS
6064
6065struct scsi_host_template lpfc_vport_template = {
6066 .module = THIS_MODULE,
6067 .name = LPFC_DRIVER_NAME,
6068 .info = lpfc_info,
6069 .queuecommand = lpfc_queuecommand,
6070 .eh_abort_handler = lpfc_abort_handler,
bbb9d180
JS
6071 .eh_device_reset_handler = lpfc_device_reset_handler,
6072 .eh_target_reset_handler = lpfc_target_reset_handler,
3de2a653
JS
6073 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6074 .slave_alloc = lpfc_slave_alloc,
6075 .slave_configure = lpfc_slave_configure,
6076 .slave_destroy = lpfc_slave_destroy,
6077 .scan_finished = lpfc_scan_finished,
6078 .this_id = -1,
83108bd3 6079 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3de2a653
JS
6080 .cmd_per_lun = LPFC_CMD_PER_LUN,
6081 .use_clustering = ENABLE_CLUSTERING,
6082 .shost_attrs = lpfc_vport_attrs,
6083 .max_sectors = 0xFFFF,
5ffc266e 6084 .change_queue_depth = lpfc_change_queue_depth,
fe8f7f9c 6085 .change_queue_type = lpfc_change_queue_type,
3de2a653 6086};
This page took 1.461303 seconds and 5 git commands to generate.