[SCSI] lpfc 8.3.6 : FCoE Protocol Fixes
[linux-2.6-block.git] / drivers / scsi / lpfc / lpfc_scsi.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
d8e93df1 4 * Copyright (C) 2004-2009 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>
22#include <linux/interrupt.h>
a90f5684 23#include <linux/delay.h>
e2a0a9d6 24#include <asm/unaligned.h>
dea3101e 25
26#include <scsi/scsi.h>
27#include <scsi/scsi_device.h>
e2a0a9d6 28#include <scsi/scsi_eh.h>
dea3101e 29#include <scsi/scsi_host.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi_transport_fc.h>
32
33#include "lpfc_version.h"
da0436e9 34#include "lpfc_hw4.h"
dea3101e 35#include "lpfc_hw.h"
36#include "lpfc_sli.h"
da0436e9 37#include "lpfc_sli4.h"
ea2151b4 38#include "lpfc_nl.h"
dea3101e 39#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_logmsg.h"
43#include "lpfc_crtn.h"
92d7f7b0 44#include "lpfc_vport.h"
dea3101e 45
46#define LPFC_RESET_WAIT 2
47#define LPFC_ABORT_WAIT 2
48
e2a0a9d6
JS
49int _dump_buf_done;
50
51static char *dif_op_str[] = {
52 "SCSI_PROT_NORMAL",
53 "SCSI_PROT_READ_INSERT",
54 "SCSI_PROT_WRITE_STRIP",
55 "SCSI_PROT_READ_STRIP",
56 "SCSI_PROT_WRITE_INSERT",
57 "SCSI_PROT_READ_PASS",
58 "SCSI_PROT_WRITE_PASS",
e2a0a9d6 59};
da0436e9
JS
60static void
61lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
e2a0a9d6
JS
62
63static void
6a9c52cf 64lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
e2a0a9d6
JS
65{
66 void *src, *dst;
67 struct scatterlist *sgde = scsi_sglist(cmnd);
68
69 if (!_dump_buf_data) {
6a9c52cf
JS
70 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
71 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
e2a0a9d6
JS
72 __func__);
73 return;
74 }
75
76
77 if (!sgde) {
6a9c52cf
JS
78 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
79 "9051 BLKGRD: ERROR: data scatterlist is null\n");
e2a0a9d6
JS
80 return;
81 }
82
83 dst = (void *) _dump_buf_data;
84 while (sgde) {
85 src = sg_virt(sgde);
86 memcpy(dst, src, sgde->length);
87 dst += sgde->length;
88 sgde = sg_next(sgde);
89 }
90}
91
92static void
6a9c52cf 93lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
e2a0a9d6
JS
94{
95 void *src, *dst;
96 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
97
98 if (!_dump_buf_dif) {
6a9c52cf
JS
99 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
100 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
e2a0a9d6
JS
101 __func__);
102 return;
103 }
104
105 if (!sgde) {
6a9c52cf
JS
106 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
107 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
e2a0a9d6
JS
108 return;
109 }
110
111 dst = _dump_buf_dif;
112 while (sgde) {
113 src = sg_virt(sgde);
114 memcpy(dst, src, sgde->length);
115 dst += sgde->length;
116 sgde = sg_next(sgde);
117 }
118}
119
f1126688
JS
120/**
121 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
122 * @phba: Pointer to HBA object.
123 * @lpfc_cmd: lpfc scsi command object pointer.
124 *
125 * This function is called from the lpfc_prep_task_mgmt_cmd function to
126 * set the last bit in the response sge entry.
127 **/
128static void
129lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
130 struct lpfc_scsi_buf *lpfc_cmd)
131{
132 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
133 if (sgl) {
134 sgl += 1;
135 sgl->word2 = le32_to_cpu(sgl->word2);
136 bf_set(lpfc_sli4_sge_last, sgl, 1);
137 sgl->word2 = cpu_to_le32(sgl->word2);
138 }
139}
140
ea2151b4 141/**
3621a710 142 * lpfc_update_stats - Update statistical data for the command completion
ea2151b4
JS
143 * @phba: Pointer to HBA object.
144 * @lpfc_cmd: lpfc scsi command object pointer.
145 *
146 * This function is called when there is a command completion and this
147 * function updates the statistical data for the command completion.
148 **/
149static void
150lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
151{
152 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
153 struct lpfc_nodelist *pnode = rdata->pnode;
154 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
155 unsigned long flags;
156 struct Scsi_Host *shost = cmd->device->host;
157 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
158 unsigned long latency;
159 int i;
160
161 if (cmd->result)
162 return;
163
9f1e1b50
JS
164 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
165
ea2151b4
JS
166 spin_lock_irqsave(shost->host_lock, flags);
167 if (!vport->stat_data_enabled ||
168 vport->stat_data_blocked ||
169 !pnode->lat_data ||
170 (phba->bucket_type == LPFC_NO_BUCKET)) {
171 spin_unlock_irqrestore(shost->host_lock, flags);
172 return;
173 }
ea2151b4
JS
174
175 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
176 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
177 phba->bucket_step;
9f1e1b50
JS
178 /* check array subscript bounds */
179 if (i < 0)
180 i = 0;
181 else if (i >= LPFC_MAX_BUCKET_COUNT)
182 i = LPFC_MAX_BUCKET_COUNT - 1;
ea2151b4
JS
183 } else {
184 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
185 if (latency <= (phba->bucket_base +
186 ((1<<i)*phba->bucket_step)))
187 break;
188 }
189
190 pnode->lat_data[i].cmd_count++;
191 spin_unlock_irqrestore(shost->host_lock, flags);
192}
193
ea2151b4 194/**
3621a710 195 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
ea2151b4
JS
196 * @phba: Pointer to HBA context object.
197 * @vport: Pointer to vport object.
198 * @ndlp: Pointer to FC node associated with the target.
199 * @lun: Lun number of the scsi device.
200 * @old_val: Old value of the queue depth.
201 * @new_val: New value of the queue depth.
202 *
203 * This function sends an event to the mgmt application indicating
204 * there is a change in the scsi device queue depth.
205 **/
206static void
207lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
208 struct lpfc_vport *vport,
209 struct lpfc_nodelist *ndlp,
210 uint32_t lun,
211 uint32_t old_val,
212 uint32_t new_val)
213{
214 struct lpfc_fast_path_event *fast_path_evt;
215 unsigned long flags;
216
217 fast_path_evt = lpfc_alloc_fast_evt(phba);
218 if (!fast_path_evt)
219 return;
220
221 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
222 FC_REG_SCSI_EVENT;
223 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
224 LPFC_EVENT_VARQUEDEPTH;
225
226 /* Report all luns with change in queue depth */
227 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
228 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
229 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
230 &ndlp->nlp_portname, sizeof(struct lpfc_name));
231 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
232 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
233 }
234
235 fast_path_evt->un.queue_depth_evt.oldval = old_val;
236 fast_path_evt->un.queue_depth_evt.newval = new_val;
237 fast_path_evt->vport = vport;
238
239 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
240 spin_lock_irqsave(&phba->hbalock, flags);
241 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
242 spin_unlock_irqrestore(&phba->hbalock, flags);
243 lpfc_worker_wake_up(phba);
244
245 return;
246}
247
9bad7671 248/**
3621a710 249 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
9bad7671
JS
250 * @phba: The Hba for which this call is being executed.
251 *
252 * This routine is called when there is resource error in driver or firmware.
253 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
254 * posts at most 1 event each second. This routine wakes up worker thread of
255 * @phba to process WORKER_RAM_DOWN_EVENT event.
256 *
257 * This routine should be called with no lock held.
258 **/
92d7f7b0 259void
eaf15d5b 260lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
92d7f7b0
JS
261{
262 unsigned long flags;
5e9d9b82 263 uint32_t evt_posted;
92d7f7b0
JS
264
265 spin_lock_irqsave(&phba->hbalock, flags);
266 atomic_inc(&phba->num_rsrc_err);
267 phba->last_rsrc_error_time = jiffies;
268
269 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
270 spin_unlock_irqrestore(&phba->hbalock, flags);
271 return;
272 }
273
274 phba->last_ramp_down_time = jiffies;
275
276 spin_unlock_irqrestore(&phba->hbalock, flags);
277
278 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
279 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
280 if (!evt_posted)
92d7f7b0 281 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
92d7f7b0
JS
282 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
283
5e9d9b82
JS
284 if (!evt_posted)
285 lpfc_worker_wake_up(phba);
92d7f7b0
JS
286 return;
287}
288
9bad7671 289/**
3621a710 290 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
9bad7671
JS
291 * @phba: The Hba for which this call is being executed.
292 *
293 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
294 * post at most 1 event every 5 minute after last_ramp_up_time or
295 * last_rsrc_error_time. This routine wakes up worker thread of @phba
296 * to process WORKER_RAM_DOWN_EVENT event.
297 *
298 * This routine should be called with no lock held.
299 **/
92d7f7b0 300static inline void
3de2a653 301lpfc_rampup_queue_depth(struct lpfc_vport *vport,
a257bf90 302 uint32_t queue_depth)
92d7f7b0
JS
303{
304 unsigned long flags;
3de2a653 305 struct lpfc_hba *phba = vport->phba;
5e9d9b82 306 uint32_t evt_posted;
92d7f7b0
JS
307 atomic_inc(&phba->num_cmd_success);
308
a257bf90 309 if (vport->cfg_lun_queue_depth <= queue_depth)
92d7f7b0 310 return;
92d7f7b0
JS
311 spin_lock_irqsave(&phba->hbalock, flags);
312 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
313 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
314 spin_unlock_irqrestore(&phba->hbalock, flags);
315 return;
316 }
92d7f7b0
JS
317 phba->last_ramp_up_time = jiffies;
318 spin_unlock_irqrestore(&phba->hbalock, flags);
319
320 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
321 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
322 if (!evt_posted)
92d7f7b0 323 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
92d7f7b0
JS
324 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
325
5e9d9b82
JS
326 if (!evt_posted)
327 lpfc_worker_wake_up(phba);
328 return;
92d7f7b0
JS
329}
330
9bad7671 331/**
3621a710 332 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
9bad7671
JS
333 * @phba: The Hba for which this call is being executed.
334 *
335 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
336 * thread.This routine reduces queue depth for all scsi device on each vport
337 * associated with @phba.
338 **/
92d7f7b0
JS
339void
340lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
341{
549e55cd
JS
342 struct lpfc_vport **vports;
343 struct Scsi_Host *shost;
92d7f7b0 344 struct scsi_device *sdev;
ea2151b4 345 unsigned long new_queue_depth, old_queue_depth;
92d7f7b0 346 unsigned long num_rsrc_err, num_cmd_success;
549e55cd 347 int i;
ea2151b4 348 struct lpfc_rport_data *rdata;
92d7f7b0
JS
349
350 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
351 num_cmd_success = atomic_read(&phba->num_cmd_success);
352
549e55cd
JS
353 vports = lpfc_create_vport_work_array(phba);
354 if (vports != NULL)
21e9a0a5 355 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
356 shost = lpfc_shost_from_vport(vports[i]);
357 shost_for_each_device(sdev, shost) {
92d7f7b0 358 new_queue_depth =
549e55cd
JS
359 sdev->queue_depth * num_rsrc_err /
360 (num_rsrc_err + num_cmd_success);
361 if (!new_queue_depth)
362 new_queue_depth = sdev->queue_depth - 1;
363 else
364 new_queue_depth = sdev->queue_depth -
365 new_queue_depth;
ea2151b4 366 old_queue_depth = sdev->queue_depth;
549e55cd
JS
367 if (sdev->ordered_tags)
368 scsi_adjust_queue_depth(sdev,
369 MSG_ORDERED_TAG,
370 new_queue_depth);
371 else
372 scsi_adjust_queue_depth(sdev,
373 MSG_SIMPLE_TAG,
374 new_queue_depth);
ea2151b4
JS
375 rdata = sdev->hostdata;
376 if (rdata)
377 lpfc_send_sdev_queuedepth_change_event(
378 phba, vports[i],
379 rdata->pnode,
380 sdev->lun, old_queue_depth,
381 new_queue_depth);
549e55cd 382 }
92d7f7b0 383 }
09372820 384 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
385 atomic_set(&phba->num_rsrc_err, 0);
386 atomic_set(&phba->num_cmd_success, 0);
387}
388
9bad7671 389/**
3621a710 390 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
9bad7671
JS
391 * @phba: The Hba for which this call is being executed.
392 *
393 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
394 * thread.This routine increases queue depth for all scsi device on each vport
395 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
396 * num_cmd_success to zero.
397 **/
92d7f7b0
JS
398void
399lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
400{
549e55cd
JS
401 struct lpfc_vport **vports;
402 struct Scsi_Host *shost;
92d7f7b0 403 struct scsi_device *sdev;
549e55cd 404 int i;
ea2151b4 405 struct lpfc_rport_data *rdata;
549e55cd
JS
406
407 vports = lpfc_create_vport_work_array(phba);
408 if (vports != NULL)
21e9a0a5 409 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
410 shost = lpfc_shost_from_vport(vports[i]);
411 shost_for_each_device(sdev, shost) {
97eab634
JS
412 if (vports[i]->cfg_lun_queue_depth <=
413 sdev->queue_depth)
414 continue;
549e55cd
JS
415 if (sdev->ordered_tags)
416 scsi_adjust_queue_depth(sdev,
417 MSG_ORDERED_TAG,
418 sdev->queue_depth+1);
419 else
420 scsi_adjust_queue_depth(sdev,
421 MSG_SIMPLE_TAG,
422 sdev->queue_depth+1);
ea2151b4
JS
423 rdata = sdev->hostdata;
424 if (rdata)
425 lpfc_send_sdev_queuedepth_change_event(
426 phba, vports[i],
427 rdata->pnode,
428 sdev->lun,
429 sdev->queue_depth - 1,
430 sdev->queue_depth);
549e55cd 431 }
92d7f7b0 432 }
09372820 433 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
434 atomic_set(&phba->num_rsrc_err, 0);
435 atomic_set(&phba->num_cmd_success, 0);
436}
437
a8e497d5 438/**
3621a710 439 * lpfc_scsi_dev_block - set all scsi hosts to block state
a8e497d5
JS
440 * @phba: Pointer to HBA context object.
441 *
442 * This function walks vport list and set each SCSI host to block state
443 * by invoking fc_remote_port_delete() routine. This function is invoked
444 * with EEH when device's PCI slot has been permanently disabled.
445 **/
446void
447lpfc_scsi_dev_block(struct lpfc_hba *phba)
448{
449 struct lpfc_vport **vports;
450 struct Scsi_Host *shost;
451 struct scsi_device *sdev;
452 struct fc_rport *rport;
453 int i;
454
455 vports = lpfc_create_vport_work_array(phba);
456 if (vports != NULL)
21e9a0a5 457 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
a8e497d5
JS
458 shost = lpfc_shost_from_vport(vports[i]);
459 shost_for_each_device(sdev, shost) {
460 rport = starget_to_rport(scsi_target(sdev));
461 fc_remote_port_delete(rport);
462 }
463 }
464 lpfc_destroy_vport_work_array(phba, vports);
465}
466
9bad7671 467/**
3772a991 468 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
9bad7671 469 * @vport: The virtual port for which this call being executed.
3772a991 470 * @num_to_allocate: The requested number of buffers to allocate.
9bad7671 471 *
3772a991
JS
472 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
473 * the scsi buffer contains all the necessary information needed to initiate
474 * a SCSI I/O. The non-DMAable buffer region contains information to build
475 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
476 * and the initial BPL. In addition to allocating memory, the FCP CMND and
477 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
9bad7671
JS
478 *
479 * Return codes:
3772a991
JS
480 * int - number of scsi buffers that were allocated.
481 * 0 = failure, less than num_to_alloc is a partial failure.
9bad7671 482 **/
3772a991
JS
483static int
484lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea3101e 485{
2e0fef85 486 struct lpfc_hba *phba = vport->phba;
dea3101e 487 struct lpfc_scsi_buf *psb;
488 struct ulp_bde64 *bpl;
489 IOCB_t *iocb;
34b02dcd
JS
490 dma_addr_t pdma_phys_fcp_cmd;
491 dma_addr_t pdma_phys_fcp_rsp;
492 dma_addr_t pdma_phys_bpl;
604a3e30 493 uint16_t iotag;
3772a991 494 int bcnt;
dea3101e 495
3772a991
JS
496 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
497 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
498 if (!psb)
499 break;
dea3101e 500
3772a991
JS
501 /*
502 * Get memory from the pci pool to map the virt space to pci
503 * bus space for an I/O. The DMA buffer includes space for the
504 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
505 * necessary to support the sg_tablesize.
506 */
507 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
508 GFP_KERNEL, &psb->dma_handle);
509 if (!psb->data) {
510 kfree(psb);
511 break;
512 }
513
514 /* Initialize virtual ptrs to dma_buf region. */
515 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
516
517 /* Allocate iotag for psb->cur_iocbq. */
518 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
519 if (iotag == 0) {
520 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
521 psb->data, psb->dma_handle);
522 kfree(psb);
523 break;
524 }
525 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
526
527 psb->fcp_cmnd = psb->data;
528 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
529 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
34b02dcd 530 sizeof(struct fcp_rsp);
dea3101e 531
3772a991
JS
532 /* Initialize local short-hand pointers. */
533 bpl = psb->fcp_bpl;
534 pdma_phys_fcp_cmd = psb->dma_handle;
535 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
536 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
537 sizeof(struct fcp_rsp);
538
539 /*
540 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
541 * are sg list bdes. Initialize the first two and leave the
542 * rest for queuecommand.
543 */
544 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
545 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
546 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
547 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
548 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
549
550 /* Setup the physical region for the FCP RSP */
551 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
552 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
553 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
554 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
555 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
556
557 /*
558 * Since the IOCB for the FCP I/O is built into this
559 * lpfc_scsi_buf, initialize it with all known data now.
560 */
561 iocb = &psb->cur_iocbq.iocb;
562 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
563 if ((phba->sli_rev == 3) &&
564 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
565 /* fill in immediate fcp command BDE */
566 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
567 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
568 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
569 unsli3.fcp_ext.icd);
570 iocb->un.fcpi64.bdl.addrHigh = 0;
571 iocb->ulpBdeCount = 0;
572 iocb->ulpLe = 0;
573 /* fill in responce BDE */
574 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
575 BUFF_TYPE_BDE_64;
576 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
577 sizeof(struct fcp_rsp);
578 iocb->unsli3.fcp_ext.rbde.addrLow =
579 putPaddrLow(pdma_phys_fcp_rsp);
580 iocb->unsli3.fcp_ext.rbde.addrHigh =
581 putPaddrHigh(pdma_phys_fcp_rsp);
582 } else {
583 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
584 iocb->un.fcpi64.bdl.bdeSize =
585 (2 * sizeof(struct ulp_bde64));
586 iocb->un.fcpi64.bdl.addrLow =
587 putPaddrLow(pdma_phys_bpl);
588 iocb->un.fcpi64.bdl.addrHigh =
589 putPaddrHigh(pdma_phys_bpl);
590 iocb->ulpBdeCount = 1;
591 iocb->ulpLe = 1;
592 }
593 iocb->ulpClass = CLASS3;
594 psb->status = IOSTAT_SUCCESS;
da0436e9
JS
595 /* Put it back into the SCSI buffer list */
596 lpfc_release_scsi_buf_s4(phba, psb);
dea3101e 597
34b02dcd 598 }
dea3101e 599
3772a991 600 return bcnt;
dea3101e 601}
602
da0436e9
JS
603/**
604 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
605 * @phba: pointer to lpfc hba data structure.
606 * @axri: pointer to the fcp xri abort wcqe structure.
607 *
608 * This routine is invoked by the worker thread to process a SLI4 fast-path
609 * FCP aborted xri.
610 **/
611void
612lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
613 struct sli4_wcqe_xri_aborted *axri)
614{
615 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
616 struct lpfc_scsi_buf *psb, *next_psb;
617 unsigned long iflag = 0;
618
619 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock, iflag);
620 list_for_each_entry_safe(psb, next_psb,
621 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
622 if (psb->cur_iocbq.sli4_xritag == xri) {
623 list_del(&psb->list);
624 psb->status = IOSTAT_SUCCESS;
625 spin_unlock_irqrestore(
626 &phba->sli4_hba.abts_scsi_buf_list_lock,
627 iflag);
628 lpfc_release_scsi_buf_s4(phba, psb);
629 return;
630 }
631 }
632 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
633 iflag);
634}
635
636/**
637 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
638 * @phba: pointer to lpfc hba data structure.
639 *
640 * This routine walks the list of scsi buffers that have been allocated and
641 * repost them to the HBA by using SGL block post. This is needed after a
642 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
643 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
644 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
645 *
646 * Returns: 0 = success, non-zero failure.
647 **/
648int
649lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
650{
651 struct lpfc_scsi_buf *psb;
652 int index, status, bcnt = 0, rcnt = 0, rc = 0;
653 LIST_HEAD(sblist);
654
655 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
656 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
657 if (psb) {
658 /* Remove from SCSI buffer list */
659 list_del(&psb->list);
660 /* Add it to a local SCSI buffer list */
661 list_add_tail(&psb->list, &sblist);
662 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
663 bcnt = rcnt;
664 rcnt = 0;
665 }
666 } else
667 /* A hole present in the XRI array, need to skip */
668 bcnt = rcnt;
669
670 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
671 /* End of XRI array for SCSI buffer, complete */
672 bcnt = rcnt;
673
674 /* Continue until collect up to a nembed page worth of sgls */
675 if (bcnt == 0)
676 continue;
677 /* Now, post the SCSI buffer list sgls as a block */
678 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
679 /* Reset SCSI buffer count for next round of posting */
680 bcnt = 0;
681 while (!list_empty(&sblist)) {
682 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
683 list);
684 if (status) {
685 /* Put this back on the abort scsi list */
686 psb->status = IOSTAT_LOCAL_REJECT;
687 psb->result = IOERR_ABORT_REQUESTED;
688 rc++;
689 } else
690 psb->status = IOSTAT_SUCCESS;
691 /* Put it back into the SCSI buffer list */
692 lpfc_release_scsi_buf_s4(phba, psb);
693 }
694 }
695 return rc;
696}
697
698/**
699 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
700 * @vport: The virtual port for which this call being executed.
701 * @num_to_allocate: The requested number of buffers to allocate.
702 *
703 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
704 * the scsi buffer contains all the necessary information needed to initiate
705 * a SCSI I/O.
706 *
707 * Return codes:
708 * int - number of scsi buffers that were allocated.
709 * 0 = failure, less than num_to_alloc is a partial failure.
710 **/
711static int
712lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
713{
714 struct lpfc_hba *phba = vport->phba;
715 struct lpfc_scsi_buf *psb;
716 struct sli4_sge *sgl;
717 IOCB_t *iocb;
718 dma_addr_t pdma_phys_fcp_cmd;
719 dma_addr_t pdma_phys_fcp_rsp;
720 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
721 uint16_t iotag, last_xritag = NO_XRI;
722 int status = 0, index;
723 int bcnt;
724 int non_sequential_xri = 0;
725 int rc = 0;
726 LIST_HEAD(sblist);
727
728 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
729 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
730 if (!psb)
731 break;
732
733 /*
734 * Get memory from the pci pool to map the virt space to pci bus
735 * space for an I/O. The DMA buffer includes space for the
736 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
737 * necessary to support the sg_tablesize.
738 */
739 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
740 GFP_KERNEL, &psb->dma_handle);
741 if (!psb->data) {
742 kfree(psb);
743 break;
744 }
745
746 /* Initialize virtual ptrs to dma_buf region. */
747 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
748
749 /* Allocate iotag for psb->cur_iocbq. */
750 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
751 if (iotag == 0) {
752 kfree(psb);
753 break;
754 }
755
756 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
757 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
758 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
759 psb->data, psb->dma_handle);
760 kfree(psb);
761 break;
762 }
763 if (last_xritag != NO_XRI
764 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
765 non_sequential_xri = 1;
766 } else
767 list_add_tail(&psb->list, &sblist);
768 last_xritag = psb->cur_iocbq.sli4_xritag;
769
770 index = phba->sli4_hba.scsi_xri_cnt++;
771 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
772
773 psb->fcp_bpl = psb->data;
774 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
775 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
776 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
777 sizeof(struct fcp_cmnd));
778
779 /* Initialize local short-hand pointers. */
780 sgl = (struct sli4_sge *)psb->fcp_bpl;
781 pdma_phys_bpl = psb->dma_handle;
782 pdma_phys_fcp_cmd =
783 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
784 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
785 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
786
787 /*
788 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
789 * are sg list bdes. Initialize the first two and leave the
790 * rest for queuecommand.
791 */
792 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
793 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
794 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_cmnd));
795 bf_set(lpfc_sli4_sge_last, sgl, 0);
796 sgl->word2 = cpu_to_le32(sgl->word2);
797 sgl->word3 = cpu_to_le32(sgl->word3);
798 sgl++;
799
800 /* Setup the physical region for the FCP RSP */
801 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
802 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
803 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_rsp));
804 bf_set(lpfc_sli4_sge_last, sgl, 1);
805 sgl->word2 = cpu_to_le32(sgl->word2);
806 sgl->word3 = cpu_to_le32(sgl->word3);
807
808 /*
809 * Since the IOCB for the FCP I/O is built into this
810 * lpfc_scsi_buf, initialize it with all known data now.
811 */
812 iocb = &psb->cur_iocbq.iocb;
813 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
814 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
815 /* setting the BLP size to 2 * sizeof BDE may not be correct.
816 * We are setting the bpl to point to out sgl. An sgl's
817 * entries are 16 bytes, a bpl entries are 12 bytes.
818 */
819 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
820 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
821 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
822 iocb->ulpBdeCount = 1;
823 iocb->ulpLe = 1;
824 iocb->ulpClass = CLASS3;
825 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
826 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
827 else
828 pdma_phys_bpl1 = 0;
829 psb->dma_phys_bpl = pdma_phys_bpl;
830 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
831 if (non_sequential_xri) {
832 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
833 pdma_phys_bpl1,
834 psb->cur_iocbq.sli4_xritag);
835 if (status) {
836 /* Put this back on the abort scsi list */
837 psb->status = IOSTAT_LOCAL_REJECT;
838 psb->result = IOERR_ABORT_REQUESTED;
839 rc++;
840 } else
841 psb->status = IOSTAT_SUCCESS;
842 /* Put it back into the SCSI buffer list */
843 lpfc_release_scsi_buf_s4(phba, psb);
844 break;
845 }
846 }
847 if (bcnt) {
848 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
849 /* Reset SCSI buffer count for next round of posting */
850 while (!list_empty(&sblist)) {
851 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
852 list);
853 if (status) {
854 /* Put this back on the abort scsi list */
855 psb->status = IOSTAT_LOCAL_REJECT;
856 psb->result = IOERR_ABORT_REQUESTED;
857 rc++;
858 } else
859 psb->status = IOSTAT_SUCCESS;
860 /* Put it back into the SCSI buffer list */
861 lpfc_release_scsi_buf_s4(phba, psb);
862 }
863 }
864
865 return bcnt + non_sequential_xri - rc;
866}
867
9bad7671 868/**
3772a991
JS
869 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
870 * @vport: The virtual port for which this call being executed.
871 * @num_to_allocate: The requested number of buffers to allocate.
872 *
873 * This routine wraps the actual SCSI buffer allocator function pointer from
874 * the lpfc_hba struct.
875 *
876 * Return codes:
877 * int - number of scsi buffers that were allocated.
878 * 0 = failure, less than num_to_alloc is a partial failure.
879 **/
880static inline int
881lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
882{
883 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
884}
885
886/**
887 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
888 * @phba: The HBA for which this call is being executed.
9bad7671
JS
889 *
890 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
891 * and returns to caller.
892 *
893 * Return codes:
894 * NULL - Error
895 * Pointer to lpfc_scsi_buf - Success
896 **/
455c53ec 897static struct lpfc_scsi_buf*
875fbdfe 898lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea3101e 899{
0bd4ca25
JSEC
900 struct lpfc_scsi_buf * lpfc_cmd = NULL;
901 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
875fbdfe 902 unsigned long iflag = 0;
0bd4ca25 903
875fbdfe 904 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 905 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1dcb58e5
JS
906 if (lpfc_cmd) {
907 lpfc_cmd->seg_cnt = 0;
908 lpfc_cmd->nonsg_phys = 0;
e2a0a9d6 909 lpfc_cmd->prot_seg_cnt = 0;
1dcb58e5 910 }
875fbdfe 911 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
0bd4ca25
JSEC
912 return lpfc_cmd;
913}
dea3101e 914
9bad7671 915/**
3772a991 916 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
9bad7671
JS
917 * @phba: The Hba for which this call is being executed.
918 * @psb: The scsi buffer which is being released.
919 *
920 * This routine releases @psb scsi buffer by adding it to tail of @phba
921 * lpfc_scsi_buf_list list.
922 **/
0bd4ca25 923static void
3772a991 924lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
0bd4ca25 925{
875fbdfe 926 unsigned long iflag = 0;
dea3101e 927
875fbdfe 928 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 929 psb->pCmd = NULL;
dea3101e 930 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
875fbdfe 931 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea3101e 932}
933
da0436e9
JS
934/**
935 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
936 * @phba: The Hba for which this call is being executed.
937 * @psb: The scsi buffer which is being released.
938 *
939 * This routine releases @psb scsi buffer by adding it to tail of @phba
940 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
941 * and cannot be reused for at least RA_TOV amount of time if it was
942 * aborted.
943 **/
944static void
945lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
946{
947 unsigned long iflag = 0;
948
949 if (psb->status == IOSTAT_LOCAL_REJECT
950 && psb->result == IOERR_ABORT_REQUESTED) {
951 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
952 iflag);
953 psb->pCmd = NULL;
954 list_add_tail(&psb->list,
955 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
956 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
957 iflag);
958 } else {
959
960 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
961 psb->pCmd = NULL;
962 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
963 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
964 }
965}
966
9bad7671 967/**
3772a991
JS
968 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
969 * @phba: The Hba for which this call is being executed.
970 * @psb: The scsi buffer which is being released.
971 *
972 * This routine releases @psb scsi buffer by adding it to tail of @phba
973 * lpfc_scsi_buf_list list.
974 **/
975static void
976lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
977{
978
979 phba->lpfc_release_scsi_buf(phba, psb);
980}
981
982/**
983 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
9bad7671
JS
984 * @phba: The Hba for which this call is being executed.
985 * @lpfc_cmd: The scsi buffer which is going to be mapped.
986 *
987 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3772a991
JS
988 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
989 * through sg elements and format the bdea. This routine also initializes all
990 * IOCB fields which are dependent on scsi command request buffer.
9bad7671
JS
991 *
992 * Return codes:
993 * 1 - Error
994 * 0 - Success
995 **/
dea3101e 996static int
3772a991 997lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea3101e 998{
999 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1000 struct scatterlist *sgel = NULL;
1001 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1002 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1003 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
34b02dcd 1004 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea3101e 1005 dma_addr_t physaddr;
34b02dcd 1006 uint32_t num_bde = 0;
a0b4f78f 1007 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea3101e 1008
1009 /*
1010 * There are three possibilities here - use scatter-gather segment, use
1011 * the single mapping, or neither. Start the lpfc command prep by
1012 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1013 * data bde entry.
1014 */
1015 bpl += 2;
c59fd9eb 1016 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 1017 /*
1018 * The driver stores the segment count returned from pci_map_sg
1019 * because this a count of dma-mappings used to map the use_sg
1020 * pages. They are not guaranteed to be the same for those
1021 * architectures that implement an IOMMU.
1022 */
dea3101e 1023
c59fd9eb
FT
1024 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1025 scsi_sg_count(scsi_cmnd), datadir);
1026 if (unlikely(!nseg))
1027 return 1;
1028
a0b4f78f 1029 lpfc_cmd->seg_cnt = nseg;
dea3101e 1030 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
6a9c52cf
JS
1031 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1032 "9064 BLKGRD: %s: Too many sg segments from "
e2a0a9d6 1033 "dma_map_sg. Config %d, seg_cnt %d\n",
cadbd4a5 1034 __func__, phba->cfg_sg_seg_cnt,
dea3101e 1035 lpfc_cmd->seg_cnt);
a0b4f78f 1036 scsi_dma_unmap(scsi_cmnd);
dea3101e 1037 return 1;
1038 }
1039
1040 /*
1041 * The driver established a maximum scatter-gather segment count
1042 * during probe that limits the number of sg elements in any
1043 * single scsi command. Just run through the seg_cnt and format
1044 * the bde's.
34b02dcd
JS
1045 * When using SLI-3 the driver will try to fit all the BDEs into
1046 * the IOCB. If it can't then the BDEs get added to a BPL as it
1047 * does for SLI-2 mode.
dea3101e 1048 */
34b02dcd 1049 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea3101e 1050 physaddr = sg_dma_address(sgel);
34b02dcd 1051 if (phba->sli_rev == 3 &&
e2a0a9d6 1052 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
34b02dcd
JS
1053 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1054 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1055 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1056 data_bde->addrLow = putPaddrLow(physaddr);
1057 data_bde->addrHigh = putPaddrHigh(physaddr);
1058 data_bde++;
1059 } else {
1060 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1061 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1062 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1063 bpl->addrLow =
1064 le32_to_cpu(putPaddrLow(physaddr));
1065 bpl->addrHigh =
1066 le32_to_cpu(putPaddrHigh(physaddr));
1067 bpl++;
1068 }
dea3101e 1069 }
c59fd9eb 1070 }
dea3101e 1071
1072 /*
1073 * Finish initializing those IOCB fields that are dependent on the
34b02dcd
JS
1074 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1075 * explicitly reinitialized and for SLI-3 the extended bde count is
1076 * explicitly reinitialized since all iocb memory resources are reused.
dea3101e 1077 */
e2a0a9d6
JS
1078 if (phba->sli_rev == 3 &&
1079 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
34b02dcd
JS
1080 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1081 /*
1082 * The extended IOCB format can only fit 3 BDE or a BPL.
1083 * This I/O has more than 3 BDE so the 1st data bde will
1084 * be a BPL that is filled in here.
1085 */
1086 physaddr = lpfc_cmd->dma_handle;
1087 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1088 data_bde->tus.f.bdeSize = (num_bde *
1089 sizeof(struct ulp_bde64));
1090 physaddr += (sizeof(struct fcp_cmnd) +
1091 sizeof(struct fcp_rsp) +
1092 (2 * sizeof(struct ulp_bde64)));
1093 data_bde->addrHigh = putPaddrHigh(physaddr);
1094 data_bde->addrLow = putPaddrLow(physaddr);
1095 /* ebde count includes the responce bde and data bpl */
1096 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1097 } else {
1098 /* ebde count includes the responce bde and data bdes */
1099 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1100 }
1101 } else {
1102 iocb_cmd->un.fcpi64.bdl.bdeSize =
1103 ((num_bde + 2) * sizeof(struct ulp_bde64));
1104 }
09372820 1105 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
e2a0a9d6
JS
1106
1107 /*
1108 * Due to difference in data length between DIF/non-DIF paths,
1109 * we need to set word 4 of IOCB here
1110 */
a257bf90 1111 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
e2a0a9d6
JS
1112 return 0;
1113}
1114
1115/*
1116 * Given a scsi cmnd, determine the BlockGuard profile to be used
1117 * with the cmd
1118 */
1119static int
6a9c52cf 1120lpfc_sc_to_sli_prof(struct lpfc_hba *phba, struct scsi_cmnd *sc)
e2a0a9d6
JS
1121{
1122 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1123 uint8_t ret_prof = LPFC_PROF_INVALID;
1124
1125 if (guard_type == SHOST_DIX_GUARD_IP) {
1126 switch (scsi_get_prot_op(sc)) {
1127 case SCSI_PROT_READ_INSERT:
1128 case SCSI_PROT_WRITE_STRIP:
1129 ret_prof = LPFC_PROF_AST2;
1130 break;
1131
1132 case SCSI_PROT_READ_STRIP:
1133 case SCSI_PROT_WRITE_INSERT:
1134 ret_prof = LPFC_PROF_A1;
1135 break;
1136
c6af4042
MP
1137 case SCSI_PROT_READ_PASS:
1138 case SCSI_PROT_WRITE_PASS:
e2a0a9d6
JS
1139 ret_prof = LPFC_PROF_AST1;
1140 break;
1141
e2a0a9d6
JS
1142 case SCSI_PROT_NORMAL:
1143 default:
6a9c52cf
JS
1144 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1145 "9063 BLKGRD:Bad op/guard:%d/%d combination\n",
e2a0a9d6
JS
1146 scsi_get_prot_op(sc), guard_type);
1147 break;
1148
1149 }
1150 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1151 switch (scsi_get_prot_op(sc)) {
1152 case SCSI_PROT_READ_STRIP:
1153 case SCSI_PROT_WRITE_INSERT:
1154 ret_prof = LPFC_PROF_A1;
1155 break;
1156
1157 case SCSI_PROT_READ_PASS:
1158 case SCSI_PROT_WRITE_PASS:
1159 ret_prof = LPFC_PROF_C1;
1160 break;
1161
e2a0a9d6
JS
1162 case SCSI_PROT_READ_INSERT:
1163 case SCSI_PROT_WRITE_STRIP:
1164 case SCSI_PROT_NORMAL:
1165 default:
6a9c52cf
JS
1166 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1167 "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
e2a0a9d6
JS
1168 scsi_get_prot_op(sc), guard_type);
1169 break;
1170 }
1171 } else {
1172 /* unsupported format */
1173 BUG();
1174 }
1175
1176 return ret_prof;
1177}
1178
1179struct scsi_dif_tuple {
1180 __be16 guard_tag; /* Checksum */
1181 __be16 app_tag; /* Opaque storage */
1182 __be32 ref_tag; /* Target LBA or indirect LBA */
1183};
1184
1185static inline unsigned
1186lpfc_cmd_blksize(struct scsi_cmnd *sc)
1187{
1188 return sc->device->sector_size;
1189}
1190
1191/**
1192 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1193 * @sc: in: SCSI command
3621a710
JS
1194 * @apptagmask: out: app tag mask
1195 * @apptagval: out: app tag value
1196 * @reftag: out: ref tag (reference tag)
e2a0a9d6
JS
1197 *
1198 * Description:
98a1708d
MO
1199 * Extract DIF parameters from the command if possible. Otherwise,
1200 * use default parameters.
e2a0a9d6
JS
1201 *
1202 **/
1203static inline void
1204lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1205 uint16_t *apptagval, uint32_t *reftag)
1206{
1207 struct scsi_dif_tuple *spt;
1208 unsigned char op = scsi_get_prot_op(sc);
1209 unsigned int protcnt = scsi_prot_sg_count(sc);
1210 static int cnt;
1211
1212 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
c6af4042 1213 op == SCSI_PROT_WRITE_PASS)) {
e2a0a9d6
JS
1214
1215 cnt++;
1216 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1217 scsi_prot_sglist(sc)[0].offset;
1218 *apptagmask = 0;
1219 *apptagval = 0;
1220 *reftag = cpu_to_be32(spt->ref_tag);
1221
1222 } else {
1223 /* SBC defines ref tag to be lower 32bits of LBA */
1224 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1225 *apptagmask = 0;
1226 *apptagval = 0;
1227 }
1228}
1229
1230/*
1231 * This function sets up buffer list for protection groups of
1232 * type LPFC_PG_TYPE_NO_DIF
1233 *
1234 * This is usually used when the HBA is instructed to generate
1235 * DIFs and insert them into data stream (or strip DIF from
1236 * incoming data stream)
1237 *
1238 * The buffer list consists of just one protection group described
1239 * below:
1240 * +-------------------------+
1241 * start of prot group --> | PDE_1 |
1242 * +-------------------------+
1243 * | Data BDE |
1244 * +-------------------------+
1245 * |more Data BDE's ... (opt)|
1246 * +-------------------------+
1247 *
1248 * @sc: pointer to scsi command we're working on
1249 * @bpl: pointer to buffer list for protection groups
1250 * @datacnt: number of segments of data that have been dma mapped
1251 *
1252 * Note: Data s/g buffers have been dma mapped
1253 */
1254static int
1255lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1256 struct ulp_bde64 *bpl, int datasegcnt)
1257{
1258 struct scatterlist *sgde = NULL; /* s/g data entry */
1259 struct lpfc_pde *pde1 = NULL;
1260 dma_addr_t physaddr;
1261 int i = 0, num_bde = 0;
1262 int datadir = sc->sc_data_direction;
1263 int prof = LPFC_PROF_INVALID;
1264 unsigned blksize;
1265 uint32_t reftag;
1266 uint16_t apptagmask, apptagval;
1267
1268 pde1 = (struct lpfc_pde *) bpl;
6a9c52cf 1269 prof = lpfc_sc_to_sli_prof(phba, sc);
e2a0a9d6
JS
1270
1271 if (prof == LPFC_PROF_INVALID)
1272 goto out;
1273
1274 /* extract some info from the scsi command for PDE1*/
1275 blksize = lpfc_cmd_blksize(sc);
1276 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1277
1278 /* setup PDE1 with what we have */
1279 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1280 BG_EC_STOP_ERR);
1281 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1282
1283 num_bde++;
1284 bpl++;
1285
1286 /* assumption: caller has already run dma_map_sg on command data */
1287 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1288 physaddr = sg_dma_address(sgde);
1289 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1290 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1291 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1292 if (datadir == DMA_TO_DEVICE)
1293 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1294 else
1295 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1296 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1297 bpl++;
1298 num_bde++;
1299 }
1300
1301out:
1302 return num_bde;
1303}
1304
1305/*
1306 * This function sets up buffer list for protection groups of
1307 * type LPFC_PG_TYPE_DIF_BUF
1308 *
1309 * This is usually used when DIFs are in their own buffers,
1310 * separate from the data. The HBA can then by instructed
1311 * to place the DIFs in the outgoing stream. For read operations,
1312 * The HBA could extract the DIFs and place it in DIF buffers.
1313 *
1314 * The buffer list for this type consists of one or more of the
1315 * protection groups described below:
1316 * +-------------------------+
1317 * start of first prot group --> | PDE_1 |
1318 * +-------------------------+
1319 * | PDE_3 (Prot BDE) |
1320 * +-------------------------+
1321 * | Data BDE |
1322 * +-------------------------+
1323 * |more Data BDE's ... (opt)|
1324 * +-------------------------+
1325 * start of new prot group --> | PDE_1 |
1326 * +-------------------------+
1327 * | ... |
1328 * +-------------------------+
1329 *
1330 * @sc: pointer to scsi command we're working on
1331 * @bpl: pointer to buffer list for protection groups
1332 * @datacnt: number of segments of data that have been dma mapped
1333 * @protcnt: number of segment of protection data that have been dma mapped
1334 *
1335 * Note: It is assumed that both data and protection s/g buffers have been
1336 * mapped for DMA
1337 */
1338static int
1339lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1340 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1341{
1342 struct scatterlist *sgde = NULL; /* s/g data entry */
1343 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1344 struct lpfc_pde *pde1 = NULL;
1345 struct ulp_bde64 *prot_bde = NULL;
1346 dma_addr_t dataphysaddr, protphysaddr;
1347 unsigned short curr_data = 0, curr_prot = 0;
1348 unsigned int split_offset, protgroup_len;
1349 unsigned int protgrp_blks, protgrp_bytes;
1350 unsigned int remainder, subtotal;
1351 int prof = LPFC_PROF_INVALID;
1352 int datadir = sc->sc_data_direction;
1353 unsigned char pgdone = 0, alldone = 0;
1354 unsigned blksize;
1355 uint32_t reftag;
1356 uint16_t apptagmask, apptagval;
1357 int num_bde = 0;
1358
1359 sgpe = scsi_prot_sglist(sc);
1360 sgde = scsi_sglist(sc);
1361
1362 if (!sgpe || !sgde) {
1363 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1364 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1365 sgpe, sgde);
1366 return 0;
1367 }
1368
6a9c52cf 1369 prof = lpfc_sc_to_sli_prof(phba, sc);
e2a0a9d6
JS
1370 if (prof == LPFC_PROF_INVALID)
1371 goto out;
1372
1373 /* extract some info from the scsi command for PDE1*/
1374 blksize = lpfc_cmd_blksize(sc);
1375 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1376
1377 split_offset = 0;
1378 do {
1379 /* setup the first PDE_1 */
1380 pde1 = (struct lpfc_pde *) bpl;
1381
1382 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1383 BG_EC_STOP_ERR);
1384 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1385
1386 num_bde++;
1387 bpl++;
1388
1389 /* setup the first BDE that points to protection buffer */
1390 prot_bde = (struct ulp_bde64 *) bpl;
1391 protphysaddr = sg_dma_address(sgpe);
1392 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1393 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1394 protgroup_len = sg_dma_len(sgpe);
1395
1396
1397 /* must be integer multiple of the DIF block length */
1398 BUG_ON(protgroup_len % 8);
1399
1400 protgrp_blks = protgroup_len / 8;
1401 protgrp_bytes = protgrp_blks * blksize;
1402
1403 prot_bde->tus.f.bdeSize = protgroup_len;
1404 if (datadir == DMA_TO_DEVICE)
1405 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1406 else
1407 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1408 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1409
1410 curr_prot++;
1411 num_bde++;
1412
1413 /* setup BDE's for data blocks associated with DIF data */
1414 pgdone = 0;
1415 subtotal = 0; /* total bytes processed for current prot grp */
1416 while (!pgdone) {
1417 if (!sgde) {
6a9c52cf
JS
1418 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1419 "9065 BLKGRD:%s Invalid data segment\n",
e2a0a9d6
JS
1420 __func__);
1421 return 0;
1422 }
1423 bpl++;
1424 dataphysaddr = sg_dma_address(sgde) + split_offset;
1425 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1426 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1427
1428 remainder = sg_dma_len(sgde) - split_offset;
1429
1430 if ((subtotal + remainder) <= protgrp_bytes) {
1431 /* we can use this whole buffer */
1432 bpl->tus.f.bdeSize = remainder;
1433 split_offset = 0;
1434
1435 if ((subtotal + remainder) == protgrp_bytes)
1436 pgdone = 1;
1437 } else {
1438 /* must split this buffer with next prot grp */
1439 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1440 split_offset += bpl->tus.f.bdeSize;
1441 }
1442
1443 subtotal += bpl->tus.f.bdeSize;
1444
1445 if (datadir == DMA_TO_DEVICE)
1446 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1447 else
1448 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1449 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1450
1451 num_bde++;
1452 curr_data++;
1453
1454 if (split_offset)
1455 break;
1456
1457 /* Move to the next s/g segment if possible */
1458 sgde = sg_next(sgde);
1459 }
1460
1461 /* are we done ? */
1462 if (curr_prot == protcnt) {
1463 alldone = 1;
1464 } else if (curr_prot < protcnt) {
1465 /* advance to next prot buffer */
1466 sgpe = sg_next(sgpe);
1467 bpl++;
1468
1469 /* update the reference tag */
1470 reftag += protgrp_blks;
1471 } else {
1472 /* if we're here, we have a bug */
6a9c52cf
JS
1473 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1474 "9054 BLKGRD: bug in %s\n", __func__);
e2a0a9d6
JS
1475 }
1476
1477 } while (!alldone);
1478
1479out:
1480
1481
1482 return num_bde;
1483}
1484/*
1485 * Given a SCSI command that supports DIF, determine composition of protection
1486 * groups involved in setting up buffer lists
1487 *
1488 * Returns:
1489 * for DIF (for both read and write)
1490 * */
1491static int
1492lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1493{
1494 int ret = LPFC_PG_TYPE_INVALID;
1495 unsigned char op = scsi_get_prot_op(sc);
1496
1497 switch (op) {
1498 case SCSI_PROT_READ_STRIP:
1499 case SCSI_PROT_WRITE_INSERT:
1500 ret = LPFC_PG_TYPE_NO_DIF;
1501 break;
1502 case SCSI_PROT_READ_INSERT:
1503 case SCSI_PROT_WRITE_STRIP:
1504 case SCSI_PROT_READ_PASS:
1505 case SCSI_PROT_WRITE_PASS:
e2a0a9d6
JS
1506 ret = LPFC_PG_TYPE_DIF_BUF;
1507 break;
1508 default:
1509 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1510 "9021 Unsupported protection op:%d\n", op);
1511 break;
1512 }
1513
1514 return ret;
1515}
1516
1517/*
1518 * This is the protection/DIF aware version of
1519 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1520 * two functions eventually, but for now, it's here
1521 */
1522static int
1523lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1524 struct lpfc_scsi_buf *lpfc_cmd)
1525{
1526 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1527 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1528 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1529 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1530 uint32_t num_bde = 0;
1531 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1532 int prot_group_type = 0;
1533 int diflen, fcpdl;
1534 unsigned blksize;
1535
1536 /*
1537 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1538 * fcp_rsp regions to the first data bde entry
1539 */
1540 bpl += 2;
1541 if (scsi_sg_count(scsi_cmnd)) {
1542 /*
1543 * The driver stores the segment count returned from pci_map_sg
1544 * because this a count of dma-mappings used to map the use_sg
1545 * pages. They are not guaranteed to be the same for those
1546 * architectures that implement an IOMMU.
1547 */
1548 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1549 scsi_sglist(scsi_cmnd),
1550 scsi_sg_count(scsi_cmnd), datadir);
1551 if (unlikely(!datasegcnt))
1552 return 1;
1553
1554 lpfc_cmd->seg_cnt = datasegcnt;
1555 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
6a9c52cf
JS
1556 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1557 "9067 BLKGRD: %s: Too many sg segments"
1558 " from dma_map_sg. Config %d, seg_cnt"
1559 " %d\n",
e2a0a9d6
JS
1560 __func__, phba->cfg_sg_seg_cnt,
1561 lpfc_cmd->seg_cnt);
1562 scsi_dma_unmap(scsi_cmnd);
1563 return 1;
1564 }
1565
1566 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1567
1568 switch (prot_group_type) {
1569 case LPFC_PG_TYPE_NO_DIF:
1570 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1571 datasegcnt);
1572 /* we shoud have 2 or more entries in buffer list */
1573 if (num_bde < 2)
1574 goto err;
1575 break;
1576 case LPFC_PG_TYPE_DIF_BUF:{
1577 /*
1578 * This type indicates that protection buffers are
1579 * passed to the driver, so that needs to be prepared
1580 * for DMA
1581 */
1582 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1583 scsi_prot_sglist(scsi_cmnd),
1584 scsi_prot_sg_count(scsi_cmnd), datadir);
1585 if (unlikely(!protsegcnt)) {
1586 scsi_dma_unmap(scsi_cmnd);
1587 return 1;
1588 }
1589
1590 lpfc_cmd->prot_seg_cnt = protsegcnt;
1591 if (lpfc_cmd->prot_seg_cnt
1592 > phba->cfg_prot_sg_seg_cnt) {
6a9c52cf
JS
1593 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1594 "9068 BLKGRD: %s: Too many prot sg "
1595 "segments from dma_map_sg. Config %d,"
e2a0a9d6
JS
1596 "prot_seg_cnt %d\n", __func__,
1597 phba->cfg_prot_sg_seg_cnt,
1598 lpfc_cmd->prot_seg_cnt);
1599 dma_unmap_sg(&phba->pcidev->dev,
1600 scsi_prot_sglist(scsi_cmnd),
1601 scsi_prot_sg_count(scsi_cmnd),
1602 datadir);
1603 scsi_dma_unmap(scsi_cmnd);
1604 return 1;
1605 }
1606
1607 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1608 datasegcnt, protsegcnt);
1609 /* we shoud have 3 or more entries in buffer list */
1610 if (num_bde < 3)
1611 goto err;
1612 break;
1613 }
1614 case LPFC_PG_TYPE_INVALID:
1615 default:
1616 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1617 "9022 Unexpected protection group %i\n",
1618 prot_group_type);
1619 return 1;
1620 }
1621 }
1622
1623 /*
1624 * Finish initializing those IOCB fields that are dependent on the
1625 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1626 * reinitialized since all iocb memory resources are used many times
1627 * for transmit, receive, and continuation bpl's.
1628 */
1629 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1630 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1631 iocb_cmd->ulpBdeCount = 1;
1632 iocb_cmd->ulpLe = 1;
1633
1634 fcpdl = scsi_bufflen(scsi_cmnd);
1635
1636 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1637 /*
1638 * We are in DIF Type 1 mode
1639 * Every data block has a 8 byte DIF (trailer)
1640 * attached to it. Must ajust FCP data length
1641 */
1642 blksize = lpfc_cmd_blksize(scsi_cmnd);
1643 diflen = (fcpdl / blksize) * 8;
1644 fcpdl += diflen;
1645 }
1646 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1647
1648 /*
1649 * Due to difference in data length between DIF/non-DIF paths,
1650 * we need to set word 4 of IOCB here
1651 */
1652 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1653
dea3101e 1654 return 0;
e2a0a9d6
JS
1655err:
1656 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1657 "9023 Could not setup all needed BDE's"
1658 "prot_group_type=%d, num_bde=%d\n",
1659 prot_group_type, num_bde);
1660 return 1;
1661}
1662
1663/*
1664 * This function checks for BlockGuard errors detected by
1665 * the HBA. In case of errors, the ASC/ASCQ fields in the
1666 * sense buffer will be set accordingly, paired with
1667 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1668 * detected corruption.
1669 *
1670 * Returns:
1671 * 0 - No error found
1672 * 1 - BlockGuard error found
1673 * -1 - Internal error (bad profile, ...etc)
1674 */
1675static int
1676lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1677 struct lpfc_iocbq *pIocbOut)
1678{
1679 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1680 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1681 int ret = 0;
1682 uint32_t bghm = bgf->bghm;
1683 uint32_t bgstat = bgf->bgstat;
1684 uint64_t failing_sector = 0;
1685
6a9c52cf
JS
1686 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1687 " 0x%x lba 0x%llx blk cnt 0x%x "
e2a0a9d6 1688 "bgstat=0x%x bghm=0x%x\n",
87b5c328 1689 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
83096ebf 1690 blk_rq_sectors(cmd->request), bgstat, bghm);
e2a0a9d6
JS
1691
1692 spin_lock(&_dump_buf_lock);
1693 if (!_dump_buf_done) {
6a9c52cf
JS
1694 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
1695 " Data for %u blocks to debugfs\n",
e2a0a9d6 1696 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
6a9c52cf 1697 lpfc_debug_save_data(phba, cmd);
e2a0a9d6
JS
1698
1699 /* If we have a prot sgl, save the DIF buffer */
1700 if (lpfc_prot_group_type(phba, cmd) ==
1701 LPFC_PG_TYPE_DIF_BUF) {
6a9c52cf
JS
1702 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1703 "Saving DIF for %u blocks to debugfs\n",
1704 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1705 lpfc_debug_save_dif(phba, cmd);
e2a0a9d6
JS
1706 }
1707
1708 _dump_buf_done = 1;
1709 }
1710 spin_unlock(&_dump_buf_lock);
1711
1712 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1713 cmd->result = ScsiResult(DID_ERROR, 0);
6a9c52cf
JS
1714 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1715 " BlockGuard profile. bgstat:0x%x\n",
1716 bgstat);
e2a0a9d6
JS
1717 ret = (-1);
1718 goto out;
1719 }
1720
1721 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1722 cmd->result = ScsiResult(DID_ERROR, 0);
6a9c52cf
JS
1723 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1724 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
e2a0a9d6
JS
1725 bgstat);
1726 ret = (-1);
1727 goto out;
1728 }
1729
1730 if (lpfc_bgs_get_guard_err(bgstat)) {
1731 ret = 1;
1732
1733 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1734 0x10, 0x1);
1c9fbafc 1735 cmd->result = DRIVER_SENSE << 24
e2a0a9d6
JS
1736 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1737 phba->bg_guard_err_cnt++;
6a9c52cf
JS
1738 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1739 "9055 BLKGRD: guard_tag error\n");
e2a0a9d6
JS
1740 }
1741
1742 if (lpfc_bgs_get_reftag_err(bgstat)) {
1743 ret = 1;
1744
1745 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1746 0x10, 0x3);
1c9fbafc 1747 cmd->result = DRIVER_SENSE << 24
e2a0a9d6
JS
1748 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1749
1750 phba->bg_reftag_err_cnt++;
6a9c52cf
JS
1751 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1752 "9056 BLKGRD: ref_tag error\n");
e2a0a9d6
JS
1753 }
1754
1755 if (lpfc_bgs_get_apptag_err(bgstat)) {
1756 ret = 1;
1757
1758 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1759 0x10, 0x2);
1c9fbafc 1760 cmd->result = DRIVER_SENSE << 24
e2a0a9d6
JS
1761 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1762
1763 phba->bg_apptag_err_cnt++;
6a9c52cf
JS
1764 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1765 "9061 BLKGRD: app_tag error\n");
e2a0a9d6
JS
1766 }
1767
1768 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1769 /*
1770 * setup sense data descriptor 0 per SPC-4 as an information
1771 * field, and put the failing LBA in it
1772 */
1773 cmd->sense_buffer[8] = 0; /* Information */
1774 cmd->sense_buffer[9] = 0xa; /* Add. length */
2344b5b6 1775 bghm /= cmd->device->sector_size;
e2a0a9d6
JS
1776
1777 failing_sector = scsi_get_lba(cmd);
1778 failing_sector += bghm;
1779
1780 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1781 }
1782
1783 if (!ret) {
1784 /* No error was reported - problem in FW? */
1785 cmd->result = ScsiResult(DID_ERROR, 0);
6a9c52cf
JS
1786 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1787 "9057 BLKGRD: no errors reported!\n");
e2a0a9d6
JS
1788 }
1789
1790out:
1791 return ret;
dea3101e 1792}
1793
da0436e9
JS
1794/**
1795 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1796 * @phba: The Hba for which this call is being executed.
1797 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1798 *
1799 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1800 * field of @lpfc_cmd for device with SLI-4 interface spec.
1801 *
1802 * Return codes:
1803 * 1 - Error
1804 * 0 - Success
1805 **/
1806static int
1807lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1808{
1809 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1810 struct scatterlist *sgel = NULL;
1811 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1812 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1813 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1814 dma_addr_t physaddr;
1815 uint32_t num_bde = 0;
1816 uint32_t dma_len;
1817 uint32_t dma_offset = 0;
1818 int nseg;
1819
1820 /*
1821 * There are three possibilities here - use scatter-gather segment, use
1822 * the single mapping, or neither. Start the lpfc command prep by
1823 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1824 * data bde entry.
1825 */
1826 if (scsi_sg_count(scsi_cmnd)) {
1827 /*
1828 * The driver stores the segment count returned from pci_map_sg
1829 * because this a count of dma-mappings used to map the use_sg
1830 * pages. They are not guaranteed to be the same for those
1831 * architectures that implement an IOMMU.
1832 */
1833
1834 nseg = scsi_dma_map(scsi_cmnd);
1835 if (unlikely(!nseg))
1836 return 1;
1837 sgl += 1;
1838 /* clear the last flag in the fcp_rsp map entry */
1839 sgl->word2 = le32_to_cpu(sgl->word2);
1840 bf_set(lpfc_sli4_sge_last, sgl, 0);
1841 sgl->word2 = cpu_to_le32(sgl->word2);
1842 sgl += 1;
1843
1844 lpfc_cmd->seg_cnt = nseg;
1845 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
6a9c52cf
JS
1846 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
1847 " %s: Too many sg segments from "
1848 "dma_map_sg. Config %d, seg_cnt %d\n",
1849 __func__, phba->cfg_sg_seg_cnt,
da0436e9
JS
1850 lpfc_cmd->seg_cnt);
1851 scsi_dma_unmap(scsi_cmnd);
1852 return 1;
1853 }
1854
1855 /*
1856 * The driver established a maximum scatter-gather segment count
1857 * during probe that limits the number of sg elements in any
1858 * single scsi command. Just run through the seg_cnt and format
1859 * the sge's.
1860 * When using SLI-3 the driver will try to fit all the BDEs into
1861 * the IOCB. If it can't then the BDEs get added to a BPL as it
1862 * does for SLI-2 mode.
1863 */
1864 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1865 physaddr = sg_dma_address(sgel);
1866 dma_len = sg_dma_len(sgel);
1867 bf_set(lpfc_sli4_sge_len, sgl, sg_dma_len(sgel));
1868 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1869 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1870 if ((num_bde + 1) == nseg)
1871 bf_set(lpfc_sli4_sge_last, sgl, 1);
1872 else
1873 bf_set(lpfc_sli4_sge_last, sgl, 0);
1874 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1875 sgl->word2 = cpu_to_le32(sgl->word2);
1876 sgl->word3 = cpu_to_le32(sgl->word3);
1877 dma_offset += dma_len;
1878 sgl++;
1879 }
1880 } else {
1881 sgl += 1;
1882 /* clear the last flag in the fcp_rsp map entry */
1883 sgl->word2 = le32_to_cpu(sgl->word2);
1884 bf_set(lpfc_sli4_sge_last, sgl, 1);
1885 sgl->word2 = cpu_to_le32(sgl->word2);
1886 }
1887
1888 /*
1889 * Finish initializing those IOCB fields that are dependent on the
1890 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1891 * explicitly reinitialized.
1892 * all iocb memory resources are reused.
1893 */
1894 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1895
1896 /*
1897 * Due to difference in data length between DIF/non-DIF paths,
1898 * we need to set word 4 of IOCB here
1899 */
1900 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1901 return 0;
1902}
1903
3772a991
JS
1904/**
1905 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1906 * @phba: The Hba for which this call is being executed.
1907 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1908 *
1909 * This routine wraps the actual DMA mapping function pointer from the
1910 * lpfc_hba struct.
1911 *
1912 * Return codes:
1913 * 1 - Error
1914 * 0 - Success
1915 **/
1916static inline int
1917lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1918{
1919 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1920}
1921
ea2151b4 1922/**
3621a710 1923 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
ea2151b4
JS
1924 * @phba: Pointer to hba context object.
1925 * @vport: Pointer to vport object.
1926 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1927 * @rsp_iocb: Pointer to response iocb object which reported error.
1928 *
1929 * This function posts an event when there is a SCSI command reporting
1930 * error from the scsi device.
1931 **/
1932static void
1933lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1934 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1935 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1936 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1937 uint32_t resp_info = fcprsp->rspStatus2;
1938 uint32_t scsi_status = fcprsp->rspStatus3;
1939 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1940 struct lpfc_fast_path_event *fast_path_evt = NULL;
1941 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1942 unsigned long flags;
1943
1944 /* If there is queuefull or busy condition send a scsi event */
1945 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1946 (cmnd->result == SAM_STAT_BUSY)) {
1947 fast_path_evt = lpfc_alloc_fast_evt(phba);
1948 if (!fast_path_evt)
1949 return;
1950 fast_path_evt->un.scsi_evt.event_type =
1951 FC_REG_SCSI_EVENT;
1952 fast_path_evt->un.scsi_evt.subcategory =
1953 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1954 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1955 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1956 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1957 &pnode->nlp_portname, sizeof(struct lpfc_name));
1958 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1959 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1960 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1961 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1962 fast_path_evt = lpfc_alloc_fast_evt(phba);
1963 if (!fast_path_evt)
1964 return;
1965 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1966 FC_REG_SCSI_EVENT;
1967 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1968 LPFC_EVENT_CHECK_COND;
1969 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1970 cmnd->device->lun;
1971 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1972 &pnode->nlp_portname, sizeof(struct lpfc_name));
1973 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
1974 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1975 fast_path_evt->un.check_cond_evt.sense_key =
1976 cmnd->sense_buffer[2] & 0xf;
1977 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
1978 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
1979 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1980 fcpi_parm &&
1981 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
1982 ((scsi_status == SAM_STAT_GOOD) &&
1983 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
1984 /*
1985 * If status is good or resid does not match with fcp_param and
1986 * there is valid fcpi_parm, then there is a read_check error
1987 */
1988 fast_path_evt = lpfc_alloc_fast_evt(phba);
1989 if (!fast_path_evt)
1990 return;
1991 fast_path_evt->un.read_check_error.header.event_type =
1992 FC_REG_FABRIC_EVENT;
1993 fast_path_evt->un.read_check_error.header.subcategory =
1994 LPFC_EVENT_FCPRDCHKERR;
1995 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
1996 &pnode->nlp_portname, sizeof(struct lpfc_name));
1997 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
1998 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1999 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2000 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2001 fast_path_evt->un.read_check_error.fcpiparam =
2002 fcpi_parm;
2003 } else
2004 return;
2005
2006 fast_path_evt->vport = vport;
2007 spin_lock_irqsave(&phba->hbalock, flags);
2008 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2009 spin_unlock_irqrestore(&phba->hbalock, flags);
2010 lpfc_worker_wake_up(phba);
2011 return;
2012}
9bad7671
JS
2013
2014/**
f1126688 2015 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3772a991 2016 * @phba: The HBA for which this call is being executed.
9bad7671
JS
2017 * @psb: The scsi buffer which is going to be un-mapped.
2018 *
2019 * This routine does DMA un-mapping of scatter gather list of scsi command
3772a991 2020 * field of @lpfc_cmd for device with SLI-3 interface spec.
9bad7671 2021 **/
bcf4dbfa 2022static void
f1126688 2023lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
bcf4dbfa
JS
2024{
2025 /*
2026 * There are only two special cases to consider. (1) the scsi command
2027 * requested scatter-gather usage or (2) the scsi command allocated
2028 * a request buffer, but did not request use_sg. There is a third
2029 * case, but it does not require resource deallocation.
2030 */
a0b4f78f
FT
2031 if (psb->seg_cnt > 0)
2032 scsi_dma_unmap(psb->pCmd);
e2a0a9d6
JS
2033 if (psb->prot_seg_cnt > 0)
2034 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2035 scsi_prot_sg_count(psb->pCmd),
2036 psb->pCmd->sc_data_direction);
bcf4dbfa
JS
2037}
2038
9bad7671 2039/**
3621a710 2040 * lpfc_handler_fcp_err - FCP response handler
9bad7671
JS
2041 * @vport: The virtual port for which this call is being executed.
2042 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2043 * @rsp_iocb: The response IOCB which contains FCP error.
2044 *
2045 * This routine is called to process response IOCB with status field
2046 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2047 * based upon SCSI and FCP error.
2048 **/
dea3101e 2049static void
2e0fef85
JS
2050lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2051 struct lpfc_iocbq *rsp_iocb)
dea3101e 2052{
2053 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2054 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2055 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
7054a606 2056 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea3101e 2057 uint32_t resp_info = fcprsp->rspStatus2;
2058 uint32_t scsi_status = fcprsp->rspStatus3;
c7743956 2059 uint32_t *lp;
dea3101e 2060 uint32_t host_status = DID_OK;
2061 uint32_t rsplen = 0;
c7743956 2062 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea3101e 2063
ea2151b4 2064
dea3101e 2065 /*
2066 * If this is a task management command, there is no
2067 * scsi packet associated with this lpfc_cmd. The driver
2068 * consumes it.
2069 */
2070 if (fcpcmd->fcpCntl2) {
2071 scsi_status = 0;
2072 goto out;
2073 }
2074
6a9c52cf
JS
2075 if (resp_info & RSP_LEN_VALID) {
2076 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2077 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
2078 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
2079 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2080 "2719 Invalid response length: "
2081 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2082 cmnd->device->id,
2083 cmnd->device->lun, cmnd->cmnd[0],
2084 rsplen);
2085 host_status = DID_ERROR;
2086 goto out;
2087 }
2088 }
2089
c7743956
JS
2090 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2091 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2092 if (snslen > SCSI_SENSE_BUFFERSIZE)
2093 snslen = SCSI_SENSE_BUFFERSIZE;
2094
2095 if (resp_info & RSP_LEN_VALID)
2096 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2097 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2098 }
2099 lp = (uint32_t *)cmnd->sense_buffer;
2100
2101 if (!scsi_status && (resp_info & RESID_UNDER))
2102 logit = LOG_FCP;
2103
e8b62011 2104 lpfc_printf_vlog(vport, KERN_WARNING, logit,
e2a0a9d6 2105 "9024 FCP command x%x failed: x%x SNS x%x x%x "
e8b62011
JS
2106 "Data: x%x x%x x%x x%x x%x\n",
2107 cmnd->cmnd[0], scsi_status,
2108 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2109 be32_to_cpu(fcprsp->rspResId),
2110 be32_to_cpu(fcprsp->rspSnsLen),
2111 be32_to_cpu(fcprsp->rspRspLen),
2112 fcprsp->rspInfo3);
dea3101e 2113
a0b4f78f 2114 scsi_set_resid(cmnd, 0);
dea3101e 2115 if (resp_info & RESID_UNDER) {
a0b4f78f 2116 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea3101e 2117
e8b62011 2118 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
e2a0a9d6 2119 "9025 FCP Read Underrun, expected %d, "
e8b62011
JS
2120 "residual %d Data: x%x x%x x%x\n",
2121 be32_to_cpu(fcpcmd->fcpDl),
2122 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2123 cmnd->underflow);
dea3101e 2124
7054a606
JS
2125 /*
2126 * If there is an under run check if under run reported by
2127 * storage array is same as the under run reported by HBA.
2128 * If this is not same, there is a dropped frame.
2129 */
2130 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2131 fcpi_parm &&
a0b4f78f 2132 (scsi_get_resid(cmnd) != fcpi_parm)) {
e8b62011
JS
2133 lpfc_printf_vlog(vport, KERN_WARNING,
2134 LOG_FCP | LOG_FCP_ERROR,
e2a0a9d6 2135 "9026 FCP Read Check Error "
e8b62011
JS
2136 "and Underrun Data: x%x x%x x%x x%x\n",
2137 be32_to_cpu(fcpcmd->fcpDl),
2138 scsi_get_resid(cmnd), fcpi_parm,
2139 cmnd->cmnd[0]);
a0b4f78f 2140 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
7054a606
JS
2141 host_status = DID_ERROR;
2142 }
dea3101e 2143 /*
2144 * The cmnd->underflow is the minimum number of bytes that must
2145 * be transfered for this command. Provided a sense condition
2146 * is not present, make sure the actual amount transferred is at
2147 * least the underflow value or fail.
2148 */
2149 if (!(resp_info & SNS_LEN_VALID) &&
2150 (scsi_status == SAM_STAT_GOOD) &&
a0b4f78f
FT
2151 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2152 < cmnd->underflow)) {
e8b62011 2153 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
e2a0a9d6 2154 "9027 FCP command x%x residual "
e8b62011
JS
2155 "underrun converted to error "
2156 "Data: x%x x%x x%x\n",
66dbfbe6 2157 cmnd->cmnd[0], scsi_bufflen(cmnd),
e8b62011 2158 scsi_get_resid(cmnd), cmnd->underflow);
dea3101e 2159 host_status = DID_ERROR;
2160 }
2161 } else if (resp_info & RESID_OVER) {
e8b62011 2162 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
e2a0a9d6 2163 "9028 FCP command x%x residual overrun error. "
e4e74273 2164 "Data: x%x x%x\n", cmnd->cmnd[0],
e8b62011 2165 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea3101e 2166 host_status = DID_ERROR;
2167
2168 /*
2169 * Check SLI validation that all the transfer was actually done
2170 * (fcpi_parm should be zero). Apply check only to reads.
2171 */
2172 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
2173 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
e8b62011 2174 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
e2a0a9d6 2175 "9029 FCP Read Check Error Data: "
e8b62011
JS
2176 "x%x x%x x%x x%x\n",
2177 be32_to_cpu(fcpcmd->fcpDl),
2178 be32_to_cpu(fcprsp->rspResId),
2179 fcpi_parm, cmnd->cmnd[0]);
dea3101e 2180 host_status = DID_ERROR;
a0b4f78f 2181 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea3101e 2182 }
2183
2184 out:
2185 cmnd->result = ScsiResult(host_status, scsi_status);
ea2151b4 2186 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea3101e 2187}
2188
9bad7671 2189/**
3621a710 2190 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
9bad7671
JS
2191 * @phba: The Hba for which this call is being executed.
2192 * @pIocbIn: The command IOCBQ for the scsi cmnd.
3772a991 2193 * @pIocbOut: The response IOCBQ for the scsi cmnd.
9bad7671
JS
2194 *
2195 * This routine assigns scsi command result by looking into response IOCB
2196 * status field appropriately. This routine handles QUEUE FULL condition as
2197 * well by ramping down device queue depth.
2198 **/
dea3101e 2199static void
2200lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2201 struct lpfc_iocbq *pIocbOut)
2202{
2203 struct lpfc_scsi_buf *lpfc_cmd =
2204 (struct lpfc_scsi_buf *) pIocbIn->context1;
2e0fef85 2205 struct lpfc_vport *vport = pIocbIn->vport;
dea3101e 2206 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2207 struct lpfc_nodelist *pnode = rdata->pnode;
2208 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
445cf4f4 2209 int result;
a257bf90 2210 struct scsi_device *tmp_sdev;
445cf4f4 2211 int depth = 0;
fa61a54e 2212 unsigned long flags;
ea2151b4 2213 struct lpfc_fast_path_event *fast_path_evt;
a257bf90
JS
2214 struct Scsi_Host *shost = cmd->device->host;
2215 uint32_t queue_depth, scsi_id;
dea3101e 2216
2217 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2218 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
109f6ed0
JS
2219 if (pnode && NLP_CHK_NODE_ACT(pnode))
2220 atomic_dec(&pnode->cmd_pending);
dea3101e 2221
2222 if (lpfc_cmd->status) {
2223 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2224 (lpfc_cmd->result & IOERR_DRVR_MASK))
2225 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2226 else if (lpfc_cmd->status >= IOSTAT_CNT)
2227 lpfc_cmd->status = IOSTAT_DEFAULT;
2228
e8b62011 2229 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
e2a0a9d6 2230 "9030 FCP cmd x%x failed <%d/%d> "
e8b62011
JS
2231 "status: x%x result: x%x Data: x%x x%x\n",
2232 cmd->cmnd[0],
2233 cmd->device ? cmd->device->id : 0xffff,
2234 cmd->device ? cmd->device->lun : 0xffff,
2235 lpfc_cmd->status, lpfc_cmd->result,
2236 pIocbOut->iocb.ulpContext,
2237 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea3101e 2238
2239 switch (lpfc_cmd->status) {
2240 case IOSTAT_FCP_RSP_ERROR:
2241 /* Call FCP RSP handler to determine result */
2e0fef85 2242 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea3101e 2243 break;
2244 case IOSTAT_NPORT_BSY:
2245 case IOSTAT_FABRIC_BSY:
0f1f53a7 2246 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
ea2151b4
JS
2247 fast_path_evt = lpfc_alloc_fast_evt(phba);
2248 if (!fast_path_evt)
2249 break;
2250 fast_path_evt->un.fabric_evt.event_type =
2251 FC_REG_FABRIC_EVENT;
2252 fast_path_evt->un.fabric_evt.subcategory =
2253 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2254 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2255 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2256 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2257 &pnode->nlp_portname,
2258 sizeof(struct lpfc_name));
2259 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2260 &pnode->nlp_nodename,
2261 sizeof(struct lpfc_name));
2262 }
2263 fast_path_evt->vport = vport;
2264 fast_path_evt->work_evt.evt =
2265 LPFC_EVT_FASTPATH_MGMT_EVT;
2266 spin_lock_irqsave(&phba->hbalock, flags);
2267 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2268 &phba->work_list);
2269 spin_unlock_irqrestore(&phba->hbalock, flags);
2270 lpfc_worker_wake_up(phba);
dea3101e 2271 break;
92d7f7b0 2272 case IOSTAT_LOCAL_REJECT:
d7c255b2 2273 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
92d7f7b0 2274 lpfc_cmd->result == IOERR_NO_RESOURCES ||
d7c255b2 2275 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
92d7f7b0 2276 cmd->result = ScsiResult(DID_REQUEUE, 0);
58da1ffb 2277 break;
e2a0a9d6
JS
2278 }
2279
2280 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2281 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2282 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2283 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2284 /*
2285 * This is a response for a BG enabled
2286 * cmd. Parse BG error
2287 */
2288 lpfc_parse_bg_err(phba, lpfc_cmd,
2289 pIocbOut);
2290 break;
2291 } else {
2292 lpfc_printf_vlog(vport, KERN_WARNING,
2293 LOG_BG,
2294 "9031 non-zero BGSTAT "
6a9c52cf 2295 "on unprotected cmd\n");
e2a0a9d6
JS
2296 }
2297 }
2298
2299 /* else: fall through */
dea3101e 2300 default:
2301 cmd->result = ScsiResult(DID_ERROR, 0);
2302 break;
2303 }
2304
58da1ffb 2305 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
19a7b4ae 2306 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
0f1f53a7
JS
2307 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2308 SAM_STAT_BUSY);
dea3101e 2309 } else {
2310 cmd->result = ScsiResult(DID_OK, 0);
2311 }
2312
2313 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2314 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2315
e8b62011
JS
2316 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2317 "0710 Iodone <%d/%d> cmd %p, error "
2318 "x%x SNS x%x x%x Data: x%x x%x\n",
2319 cmd->device->id, cmd->device->lun, cmd,
2320 cmd->result, *lp, *(lp + 3), cmd->retries,
2321 scsi_get_resid(cmd));
dea3101e 2322 }
2323
ea2151b4 2324 lpfc_update_stats(phba, lpfc_cmd);
445cf4f4 2325 result = cmd->result;
977b5a0a
JS
2326 if (vport->cfg_max_scsicmpl_time &&
2327 time_after(jiffies, lpfc_cmd->start_time +
2328 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
a257bf90 2329 spin_lock_irqsave(shost->host_lock, flags);
109f6ed0
JS
2330 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2331 if (pnode->cmd_qdepth >
2332 atomic_read(&pnode->cmd_pending) &&
2333 (atomic_read(&pnode->cmd_pending) >
2334 LPFC_MIN_TGT_QDEPTH) &&
2335 ((cmd->cmnd[0] == READ_10) ||
2336 (cmd->cmnd[0] == WRITE_10)))
2337 pnode->cmd_qdepth =
2338 atomic_read(&pnode->cmd_pending);
2339
2340 pnode->last_change_time = jiffies;
2341 }
a257bf90 2342 spin_unlock_irqrestore(shost->host_lock, flags);
109f6ed0
JS
2343 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2344 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
977b5a0a 2345 time_after(jiffies, pnode->last_change_time +
109f6ed0 2346 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
a257bf90 2347 spin_lock_irqsave(shost->host_lock, flags);
109f6ed0
JS
2348 pnode->cmd_qdepth += pnode->cmd_qdepth *
2349 LPFC_TGTQ_RAMPUP_PCENT / 100;
2350 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
2351 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2352 pnode->last_change_time = jiffies;
a257bf90 2353 spin_unlock_irqrestore(shost->host_lock, flags);
109f6ed0 2354 }
977b5a0a
JS
2355 }
2356
1dcb58e5 2357 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
a257bf90
JS
2358
2359 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2360 queue_depth = cmd->device->queue_depth;
2361 scsi_id = cmd->device->id;
0bd4ca25
JSEC
2362 cmd->scsi_done(cmd);
2363
b808608b 2364 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
fa61a54e
JS
2365 /*
2366 * If there is a thread waiting for command completion
2367 * wake up the thread.
2368 */
a257bf90 2369 spin_lock_irqsave(shost->host_lock, flags);
495a714c 2370 lpfc_cmd->pCmd = NULL;
fa61a54e
JS
2371 if (lpfc_cmd->waitq)
2372 wake_up(lpfc_cmd->waitq);
a257bf90 2373 spin_unlock_irqrestore(shost->host_lock, flags);
b808608b
JW
2374 lpfc_release_scsi_buf(phba, lpfc_cmd);
2375 return;
2376 }
2377
92d7f7b0
JS
2378
2379 if (!result)
a257bf90 2380 lpfc_rampup_queue_depth(vport, queue_depth);
92d7f7b0 2381
58da1ffb 2382 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
445cf4f4
JSEC
2383 ((jiffies - pnode->last_ramp_up_time) >
2384 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
2385 ((jiffies - pnode->last_q_full_time) >
2386 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
a257bf90
JS
2387 (vport->cfg_lun_queue_depth > queue_depth)) {
2388 shost_for_each_device(tmp_sdev, shost) {
3de2a653 2389 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
a257bf90 2390 if (tmp_sdev->id != scsi_id)
445cf4f4
JSEC
2391 continue;
2392 if (tmp_sdev->ordered_tags)
2393 scsi_adjust_queue_depth(tmp_sdev,
2394 MSG_ORDERED_TAG,
2395 tmp_sdev->queue_depth+1);
2396 else
2397 scsi_adjust_queue_depth(tmp_sdev,
2398 MSG_SIMPLE_TAG,
2399 tmp_sdev->queue_depth+1);
2400
2401 pnode->last_ramp_up_time = jiffies;
2402 }
2403 }
ea2151b4
JS
2404 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
2405 0xFFFFFFFF,
a257bf90 2406 queue_depth , queue_depth + 1);
445cf4f4
JSEC
2407 }
2408
2409 /*
2410 * Check for queue full. If the lun is reporting queue full, then
2411 * back off the lun queue depth to prevent target overloads.
2412 */
58da1ffb
JS
2413 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2414 NLP_CHK_NODE_ACT(pnode)) {
445cf4f4
JSEC
2415 pnode->last_q_full_time = jiffies;
2416
a257bf90
JS
2417 shost_for_each_device(tmp_sdev, shost) {
2418 if (tmp_sdev->id != scsi_id)
445cf4f4
JSEC
2419 continue;
2420 depth = scsi_track_queue_full(tmp_sdev,
2421 tmp_sdev->queue_depth - 1);
2422 }
2423 /*
2e0fef85 2424 * The queue depth cannot be lowered any more.
445cf4f4
JSEC
2425 * Modify the returned error code to store
2426 * the final depth value set by
2427 * scsi_track_queue_full.
2428 */
2429 if (depth == -1)
a257bf90 2430 depth = shost->cmd_per_lun;
445cf4f4
JSEC
2431
2432 if (depth) {
e8b62011
JS
2433 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2434 "0711 detected queue full - lun queue "
2435 "depth adjusted to %d.\n", depth);
ea2151b4
JS
2436 lpfc_send_sdev_queuedepth_change_event(phba, vport,
2437 pnode, 0xFFFFFFFF,
2438 depth+1, depth);
445cf4f4
JSEC
2439 }
2440 }
2441
fa61a54e
JS
2442 /*
2443 * If there is a thread waiting for command completion
2444 * wake up the thread.
2445 */
a257bf90 2446 spin_lock_irqsave(shost->host_lock, flags);
495a714c 2447 lpfc_cmd->pCmd = NULL;
fa61a54e
JS
2448 if (lpfc_cmd->waitq)
2449 wake_up(lpfc_cmd->waitq);
a257bf90 2450 spin_unlock_irqrestore(shost->host_lock, flags);
fa61a54e 2451
0bd4ca25 2452 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 2453}
2454
34b02dcd 2455/**
3621a710 2456 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
34b02dcd
JS
2457 * @data: A pointer to the immediate command data portion of the IOCB.
2458 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2459 *
2460 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2461 * byte swapping the data to big endian format for transmission on the wire.
2462 **/
2463static void
2464lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2465{
2466 int i, j;
2467 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2468 i += sizeof(uint32_t), j++) {
2469 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2470 }
2471}
2472
9bad7671 2473/**
f1126688 2474 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
9bad7671
JS
2475 * @vport: The virtual port for which this call is being executed.
2476 * @lpfc_cmd: The scsi command which needs to send.
2477 * @pnode: Pointer to lpfc_nodelist.
2478 *
2479 * This routine initializes fcp_cmnd and iocb data structure from scsi command
3772a991 2480 * to transfer for device with SLI3 interface spec.
9bad7671 2481 **/
dea3101e 2482static void
f1126688 2483lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2e0fef85 2484 struct lpfc_nodelist *pnode)
dea3101e 2485{
2e0fef85 2486 struct lpfc_hba *phba = vport->phba;
dea3101e 2487 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2488 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2489 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2490 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2491 int datadir = scsi_cmnd->sc_data_direction;
7e2b19fb 2492 char tag[2];
dea3101e 2493
58da1ffb
JS
2494 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2495 return;
2496
dea3101e 2497 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
69859dc4
JSEC
2498 /* clear task management bits */
2499 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea3101e 2500
91886523
JSEC
2501 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2502 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 2503
2504 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2505
7e2b19fb
JS
2506 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2507 switch (tag[0]) {
dea3101e 2508 case HEAD_OF_QUEUE_TAG:
2509 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2510 break;
2511 case ORDERED_QUEUE_TAG:
2512 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2513 break;
2514 default:
2515 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2516 break;
2517 }
2518 } else
2519 fcp_cmnd->fcpCntl1 = 0;
2520
2521 /*
2522 * There are three possibilities here - use scatter-gather segment, use
2523 * the single mapping, or neither. Start the lpfc command prep by
2524 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2525 * data bde entry.
2526 */
a0b4f78f 2527 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 2528 if (datadir == DMA_TO_DEVICE) {
2529 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
3772a991
JS
2530 if (phba->sli_rev < LPFC_SLI_REV4) {
2531 iocb_cmd->un.fcpi.fcpi_parm = 0;
2532 iocb_cmd->ulpPU = 0;
2533 } else
2534 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea3101e 2535 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2536 phba->fc4OutputRequests++;
2537 } else {
2538 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2539 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea3101e 2540 fcp_cmnd->fcpCntl3 = READ_DATA;
2541 phba->fc4InputRequests++;
2542 }
2543 } else {
2544 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2545 iocb_cmd->un.fcpi.fcpi_parm = 0;
2546 iocb_cmd->ulpPU = 0;
2547 fcp_cmnd->fcpCntl3 = 0;
2548 phba->fc4ControlRequests++;
2549 }
e2a0a9d6
JS
2550 if (phba->sli_rev == 3 &&
2551 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
34b02dcd 2552 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 2553 /*
2554 * Finish initializing those IOCB fields that are independent
2555 * of the scsi_cmnd request_buffer
2556 */
2557 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2558 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2559 piocbq->iocb.ulpFCP2Rcvy = 1;
09372820
JS
2560 else
2561 piocbq->iocb.ulpFCP2Rcvy = 0;
dea3101e 2562
2563 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2564 piocbq->context1 = lpfc_cmd;
2565 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2566 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2e0fef85 2567 piocbq->vport = vport;
dea3101e 2568}
2569
da0436e9 2570/**
f1126688 2571 * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
9bad7671
JS
2572 * @vport: The virtual port for which this call is being executed.
2573 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2574 * @lun: Logical unit number.
2575 * @task_mgmt_cmd: SCSI task management command.
2576 *
3772a991
JS
2577 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2578 * for device with SLI-3 interface spec.
9bad7671
JS
2579 *
2580 * Return codes:
2581 * 0 - Error
2582 * 1 - Success
2583 **/
dea3101e 2584static int
f1126688 2585lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea3101e 2586 struct lpfc_scsi_buf *lpfc_cmd,
420b630d 2587 unsigned int lun,
dea3101e 2588 uint8_t task_mgmt_cmd)
2589{
dea3101e 2590 struct lpfc_iocbq *piocbq;
2591 IOCB_t *piocb;
2592 struct fcp_cmnd *fcp_cmnd;
0b18ac42 2593 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea3101e 2594 struct lpfc_nodelist *ndlp = rdata->pnode;
2595
58da1ffb
JS
2596 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2597 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea3101e 2598 return 0;
dea3101e 2599
dea3101e 2600 piocbq = &(lpfc_cmd->cur_iocbq);
2e0fef85
JS
2601 piocbq->vport = vport;
2602
dea3101e 2603 piocb = &piocbq->iocb;
2604
2605 fcp_cmnd = lpfc_cmd->fcp_cmnd;
34b02dcd
JS
2606 /* Clear out any old data in the FCP command area */
2607 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2608 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea3101e 2609 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
e2a0a9d6
JS
2610 if (vport->phba->sli_rev == 3 &&
2611 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
34b02dcd 2612 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 2613 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea3101e 2614 piocb->ulpContext = ndlp->nlp_rpi;
2615 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2616 piocb->ulpFCP2Rcvy = 1;
2617 }
2618 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2619
2620 /* ulpTimeout is only one byte */
2621 if (lpfc_cmd->timeout > 0xff) {
2622 /*
2623 * Do not timeout the command at the firmware level.
2624 * The driver will provide the timeout mechanism.
2625 */
2626 piocb->ulpTimeout = 0;
f1126688 2627 } else
dea3101e 2628 piocb->ulpTimeout = lpfc_cmd->timeout;
da0436e9 2629
f1126688
JS
2630 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2631 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
3772a991 2632
f1126688 2633 return 1;
3772a991
JS
2634}
2635
2636/**
2637 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2638 * @phba: The hba struct for which this call is being executed.
2639 * @dev_grp: The HBA PCI-Device group number.
2640 *
2641 * This routine sets up the SCSI interface API function jump table in @phba
2642 * struct.
2643 * Returns: 0 - success, -ENODEV - failure.
2644 **/
2645int
2646lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2647{
2648
f1126688
JS
2649 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2650 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2651 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2652
3772a991
JS
2653 switch (dev_grp) {
2654 case LPFC_PCI_DEV_LP:
2655 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2656 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
3772a991
JS
2657 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2658 break;
da0436e9
JS
2659 case LPFC_PCI_DEV_OC:
2660 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2661 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
da0436e9
JS
2662 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2663 break;
3772a991
JS
2664 default:
2665 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2666 "1418 Invalid HBA PCI-device group: 0x%x\n",
2667 dev_grp);
2668 return -ENODEV;
2669 break;
2670 }
2671 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2672 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2673 return 0;
2674}
2675
9bad7671 2676/**
3621a710 2677 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
9bad7671
JS
2678 * @phba: The Hba for which this call is being executed.
2679 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2680 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2681 *
2682 * This routine is IOCB completion routine for device reset and target reset
2683 * routine. This routine release scsi buffer associated with lpfc_cmd.
2684 **/
7054a606
JS
2685static void
2686lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2687 struct lpfc_iocbq *cmdiocbq,
2688 struct lpfc_iocbq *rspiocbq)
2689{
2690 struct lpfc_scsi_buf *lpfc_cmd =
2691 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2692 if (lpfc_cmd)
2693 lpfc_release_scsi_buf(phba, lpfc_cmd);
2694 return;
2695}
2696
9bad7671 2697/**
3621a710 2698 * lpfc_info - Info entry point of scsi_host_template data structure
9bad7671
JS
2699 * @host: The scsi host for which this call is being executed.
2700 *
2701 * This routine provides module information about hba.
2702 *
2703 * Reutrn code:
2704 * Pointer to char - Success.
2705 **/
dea3101e 2706const char *
2707lpfc_info(struct Scsi_Host *host)
2708{
2e0fef85
JS
2709 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2710 struct lpfc_hba *phba = vport->phba;
dea3101e 2711 int len;
2712 static char lpfcinfobuf[384];
2713
2714 memset(lpfcinfobuf,0,384);
2715 if (phba && phba->pcidev){
2716 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2717 len = strlen(lpfcinfobuf);
2718 snprintf(lpfcinfobuf + len,
2719 384-len,
2720 " on PCI bus %02x device %02x irq %d",
2721 phba->pcidev->bus->number,
2722 phba->pcidev->devfn,
2723 phba->pcidev->irq);
2724 len = strlen(lpfcinfobuf);
2725 if (phba->Port[0]) {
2726 snprintf(lpfcinfobuf + len,
2727 384-len,
2728 " port %s",
2729 phba->Port);
2730 }
2731 }
2732 return lpfcinfobuf;
2733}
2734
9bad7671 2735/**
3621a710 2736 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
9bad7671
JS
2737 * @phba: The Hba for which this call is being executed.
2738 *
2739 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2740 * The default value of cfg_poll_tmo is 10 milliseconds.
2741 **/
875fbdfe
JSEC
2742static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2743{
2744 unsigned long poll_tmo_expires =
2745 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2746
2747 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2748 mod_timer(&phba->fcp_poll_timer,
2749 poll_tmo_expires);
2750}
2751
9bad7671 2752/**
3621a710 2753 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
9bad7671
JS
2754 * @phba: The Hba for which this call is being executed.
2755 *
2756 * This routine starts the fcp_poll_timer of @phba.
2757 **/
875fbdfe
JSEC
2758void lpfc_poll_start_timer(struct lpfc_hba * phba)
2759{
2760 lpfc_poll_rearm_timer(phba);
2761}
2762
9bad7671 2763/**
3621a710 2764 * lpfc_poll_timeout - Restart polling timer
9bad7671
JS
2765 * @ptr: Map to lpfc_hba data structure pointer.
2766 *
2767 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2768 * and FCP Ring interrupt is disable.
2769 **/
2770
875fbdfe
JSEC
2771void lpfc_poll_timeout(unsigned long ptr)
2772{
2e0fef85 2773 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
875fbdfe
JSEC
2774
2775 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
2776 lpfc_sli_handle_fast_ring_event(phba,
2777 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2778
875fbdfe
JSEC
2779 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2780 lpfc_poll_rearm_timer(phba);
2781 }
875fbdfe
JSEC
2782}
2783
9bad7671 2784/**
3621a710 2785 * lpfc_queuecommand - scsi_host_template queuecommand entry point
9bad7671
JS
2786 * @cmnd: Pointer to scsi_cmnd data structure.
2787 * @done: Pointer to done routine.
2788 *
2789 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2790 * This routine prepares an IOCB from scsi command and provides to firmware.
2791 * The @done callback is invoked after driver finished processing the command.
2792 *
2793 * Return value :
2794 * 0 - Success
2795 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2796 **/
dea3101e 2797static int
2798lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2799{
2e0fef85
JS
2800 struct Scsi_Host *shost = cmnd->device->host;
2801 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2802 struct lpfc_hba *phba = vport->phba;
dea3101e 2803 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
2804 struct lpfc_nodelist *ndlp = rdata->pnode;
0bd4ca25 2805 struct lpfc_scsi_buf *lpfc_cmd;
19a7b4ae 2806 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
19a7b4ae 2807 int err;
dea3101e 2808
19a7b4ae
JSEC
2809 err = fc_remote_port_chkready(rport);
2810 if (err) {
2811 cmnd->result = err;
dea3101e 2812 goto out_fail_command;
2813 }
2814
e2a0a9d6
JS
2815 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2816 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2817
6a9c52cf
JS
2818 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2819 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
2820 " op:%02x str=%s without registering for"
2821 " BlockGuard - Rejecting command\n",
e2a0a9d6
JS
2822 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2823 dif_op_str[scsi_get_prot_op(cmnd)]);
2824 goto out_fail_command;
2825 }
2826
dea3101e 2827 /*
19a7b4ae
JSEC
2828 * Catch race where our node has transitioned, but the
2829 * transport is still transitioning.
dea3101e 2830 */
b522d7d4
JS
2831 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2832 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2833 goto out_fail_command;
2834 }
109f6ed0
JS
2835 if (vport->cfg_max_scsicmpl_time &&
2836 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
977b5a0a 2837 goto out_host_busy;
a93ce024 2838
ed957684 2839 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 2840 if (lpfc_cmd == NULL) {
eaf15d5b 2841 lpfc_rampdown_queue_depth(phba);
92d7f7b0 2842
e8b62011
JS
2843 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2844 "0707 driver's buffer pool is empty, "
2845 "IO busied\n");
dea3101e 2846 goto out_host_busy;
2847 }
2848
2849 /*
2850 * Store the midlayer's command structure for the completion phase
2851 * and complete the command initialization.
2852 */
2853 lpfc_cmd->pCmd = cmnd;
2854 lpfc_cmd->rdata = rdata;
2855 lpfc_cmd->timeout = 0;
977b5a0a 2856 lpfc_cmd->start_time = jiffies;
dea3101e 2857 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2858 cmnd->scsi_done = done;
2859
e2a0a9d6 2860 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
6a9c52cf
JS
2861 if (vport->phba->cfg_enable_bg) {
2862 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6
JS
2863 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2864 "str=%s\n",
2865 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2866 dif_op_str[scsi_get_prot_op(cmnd)]);
6a9c52cf 2867 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 2868 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
e4e74273 2869 "%02x %02x %02x %02x %02x\n",
e2a0a9d6
JS
2870 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2871 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2872 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2873 cmnd->cmnd[9]);
6a9c52cf
JS
2874 if (cmnd->cmnd[0] == READ_10)
2875 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 2876 "9035 BLKGRD: READ @ sector %llu, "
83096ebf
TH
2877 "count %u\n",
2878 (unsigned long long)scsi_get_lba(cmnd),
2879 blk_rq_sectors(cmnd->request));
6a9c52cf
JS
2880 else if (cmnd->cmnd[0] == WRITE_10)
2881 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 2882 "9036 BLKGRD: WRITE @ sector %llu, "
83096ebf 2883 "count %u cmd=%p\n",
87b5c328 2884 (unsigned long long)scsi_get_lba(cmnd),
83096ebf 2885 blk_rq_sectors(cmnd->request),
e2a0a9d6 2886 cmnd);
6a9c52cf 2887 }
e2a0a9d6
JS
2888
2889 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2890 } else {
6a9c52cf 2891 if (vport->phba->cfg_enable_bg) {
e2a0a9d6 2892 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
6a9c52cf
JS
2893 "9038 BLKGRD: rcvd unprotected cmd:"
2894 "%02x op:%02x str=%s\n",
2895 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2896 dif_op_str[scsi_get_prot_op(cmnd)]);
2897 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2898 "9039 BLKGRD: CDB: %02x %02x %02x "
2899 "%02x %02x %02x %02x %02x %02x %02x\n",
2900 cmnd->cmnd[0], cmnd->cmnd[1],
2901 cmnd->cmnd[2], cmnd->cmnd[3],
2902 cmnd->cmnd[4], cmnd->cmnd[5],
2903 cmnd->cmnd[6], cmnd->cmnd[7],
2904 cmnd->cmnd[8], cmnd->cmnd[9]);
2905 if (cmnd->cmnd[0] == READ_10)
2906 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2907 "9040 dbg: READ @ sector %llu, "
2908 "count %u\n",
2909 (unsigned long long)scsi_get_lba(cmnd),
83096ebf 2910 blk_rq_sectors(cmnd->request));
6a9c52cf
JS
2911 else if (cmnd->cmnd[0] == WRITE_10)
2912 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 2913 "9041 dbg: WRITE @ sector %llu, "
83096ebf 2914 "count %u cmd=%p\n",
87b5c328 2915 (unsigned long long)scsi_get_lba(cmnd),
83096ebf 2916 blk_rq_sectors(cmnd->request), cmnd);
6a9c52cf
JS
2917 else
2918 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 2919 "9042 dbg: parser not implemented\n");
6a9c52cf 2920 }
e2a0a9d6
JS
2921 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2922 }
2923
dea3101e 2924 if (err)
2925 goto out_host_busy_free_buf;
2926
2e0fef85 2927 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea3101e 2928
977b5a0a 2929 atomic_inc(&ndlp->cmd_pending);
3772a991 2930 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
92d7f7b0 2931 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
eaf15d5b
JS
2932 if (err) {
2933 atomic_dec(&ndlp->cmd_pending);
dea3101e 2934 goto out_host_busy_free_buf;
eaf15d5b 2935 }
875fbdfe 2936 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
2937 spin_unlock(shost->host_lock);
2938 lpfc_sli_handle_fast_ring_event(phba,
2939 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2940
2941 spin_lock(shost->host_lock);
875fbdfe
JSEC
2942 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2943 lpfc_poll_rearm_timer(phba);
2944 }
2945
dea3101e 2946 return 0;
2947
2948 out_host_busy_free_buf:
bcf4dbfa 2949 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25 2950 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 2951 out_host_busy:
2952 return SCSI_MLQUEUE_HOST_BUSY;
2953
2954 out_fail_command:
2955 done(cmnd);
2956 return 0;
2957}
2958
9bad7671 2959/**
3621a710 2960 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
9bad7671
JS
2961 * @cmnd: Pointer to scsi_cmnd data structure.
2962 *
2963 * This routine aborts @cmnd pending in base driver.
2964 *
2965 * Return code :
2966 * 0x2003 - Error
2967 * 0x2002 - Success
2968 **/
dea3101e 2969static int
63c59c3b 2970lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea3101e 2971{
2e0fef85
JS
2972 struct Scsi_Host *shost = cmnd->device->host;
2973 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2974 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
2975 struct lpfc_iocbq *iocb;
2976 struct lpfc_iocbq *abtsiocb;
dea3101e 2977 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 2978 IOCB_t *cmd, *icmd;
0bd4ca25 2979 int ret = SUCCESS;
fa61a54e 2980 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
dea3101e 2981
65d430fa 2982 fc_block_scsi_eh(cmnd);
0bd4ca25
JSEC
2983 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
2984 BUG_ON(!lpfc_cmd);
dea3101e 2985
0bd4ca25
JSEC
2986 /*
2987 * If pCmd field of the corresponding lpfc_scsi_buf structure
2988 * points to a different SCSI command, then the driver has
2989 * already completed this command, but the midlayer did not
2990 * see the completion before the eh fired. Just return
2991 * SUCCESS.
2992 */
2993 iocb = &lpfc_cmd->cur_iocbq;
2994 if (lpfc_cmd->pCmd != cmnd)
2995 goto out;
dea3101e 2996
0bd4ca25 2997 BUG_ON(iocb->context1 != lpfc_cmd);
dea3101e 2998
0bd4ca25
JSEC
2999 abtsiocb = lpfc_sli_get_iocbq(phba);
3000 if (abtsiocb == NULL) {
3001 ret = FAILED;
dea3101e 3002 goto out;
3003 }
3004
dea3101e 3005 /*
0bd4ca25
JSEC
3006 * The scsi command can not be in txq and it is in flight because the
3007 * pCmd is still pointig at the SCSI command we have to abort. There
3008 * is no need to search the txcmplq. Just send an abort to the FW.
dea3101e 3009 */
dea3101e 3010
0bd4ca25
JSEC
3011 cmd = &iocb->iocb;
3012 icmd = &abtsiocb->iocb;
3013 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3014 icmd->un.acxri.abortContextTag = cmd->ulpContext;
3772a991
JS
3015 if (phba->sli_rev == LPFC_SLI_REV4)
3016 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3017 else
3018 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 3019
0bd4ca25
JSEC
3020 icmd->ulpLe = 1;
3021 icmd->ulpClass = cmd->ulpClass;
2e0fef85 3022 if (lpfc_is_link_up(phba))
0bd4ca25
JSEC
3023 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3024 else
3025 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 3026
0bd4ca25 3027 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2e0fef85 3028 abtsiocb->vport = vport;
3772a991
JS
3029 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3030 IOCB_ERROR) {
0bd4ca25
JSEC
3031 lpfc_sli_release_iocbq(phba, abtsiocb);
3032 ret = FAILED;
3033 goto out;
3034 }
dea3101e 3035
875fbdfe 3036 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
45ed1190
JS
3037 lpfc_sli_handle_fast_ring_event(phba,
3038 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
875fbdfe 3039
fa61a54e 3040 lpfc_cmd->waitq = &waitq;
0bd4ca25 3041 /* Wait for abort to complete */
fa61a54e
JS
3042 wait_event_timeout(waitq,
3043 (lpfc_cmd->pCmd != cmnd),
3044 (2*vport->cfg_devloss_tmo*HZ));
875fbdfe 3045
fa61a54e
JS
3046 spin_lock_irq(shost->host_lock);
3047 lpfc_cmd->waitq = NULL;
3048 spin_unlock_irq(shost->host_lock);
dea3101e 3049
0bd4ca25
JSEC
3050 if (lpfc_cmd->pCmd == cmnd) {
3051 ret = FAILED;
e8b62011
JS
3052 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3053 "0748 abort handler timed out waiting "
3054 "for abort to complete: ret %#x, ID %d, "
3055 "LUN %d, snum %#lx\n",
3056 ret, cmnd->device->id, cmnd->device->lun,
3057 cmnd->serial_number);
dea3101e 3058 }
3059
3060 out:
e8b62011
JS
3061 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3062 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3063 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3064 cmnd->device->lun, cmnd->serial_number);
63c59c3b 3065 return ret;
8fa728a2
JG
3066}
3067
bbb9d180
JS
3068static char *
3069lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3070{
3071 switch (task_mgmt_cmd) {
3072 case FCP_ABORT_TASK_SET:
3073 return "ABORT_TASK_SET";
3074 case FCP_CLEAR_TASK_SET:
3075 return "FCP_CLEAR_TASK_SET";
3076 case FCP_BUS_RESET:
3077 return "FCP_BUS_RESET";
3078 case FCP_LUN_RESET:
3079 return "FCP_LUN_RESET";
3080 case FCP_TARGET_RESET:
3081 return "FCP_TARGET_RESET";
3082 case FCP_CLEAR_ACA:
3083 return "FCP_CLEAR_ACA";
3084 case FCP_TERMINATE_TASK:
3085 return "FCP_TERMINATE_TASK";
3086 default:
3087 return "unknown";
3088 }
3089}
3090
9bad7671 3091/**
bbb9d180
JS
3092 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3093 * @vport: The virtual port for which this call is being executed.
3094 * @rdata: Pointer to remote port local data
3095 * @tgt_id: Target ID of remote device.
3096 * @lun_id: Lun number for the TMF
3097 * @task_mgmt_cmd: type of TMF to send
9bad7671 3098 *
bbb9d180
JS
3099 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3100 * a remote port.
9bad7671 3101 *
bbb9d180
JS
3102 * Return Code:
3103 * 0x2003 - Error
3104 * 0x2002 - Success.
9bad7671 3105 **/
dea3101e 3106static int
bbb9d180
JS
3107lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3108 unsigned tgt_id, unsigned int lun_id,
3109 uint8_t task_mgmt_cmd)
dea3101e 3110{
2e0fef85 3111 struct lpfc_hba *phba = vport->phba;
0bd4ca25 3112 struct lpfc_scsi_buf *lpfc_cmd;
bbb9d180
JS
3113 struct lpfc_iocbq *iocbq;
3114 struct lpfc_iocbq *iocbqrsp;
3115 int ret;
915caaaf 3116 int status;
dea3101e 3117
bbb9d180 3118 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
915caaaf 3119 return FAILED;
bbb9d180 3120
2e0fef85 3121 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 3122 if (lpfc_cmd == NULL)
915caaaf 3123 return FAILED;
dea3101e 3124 lpfc_cmd->timeout = 60;
0b18ac42 3125 lpfc_cmd->rdata = rdata;
dea3101e 3126
bbb9d180
JS
3127 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3128 task_mgmt_cmd);
915caaaf
JS
3129 if (!status) {
3130 lpfc_release_scsi_buf(phba, lpfc_cmd);
3131 return FAILED;
3132 }
dea3101e 3133
bbb9d180 3134 iocbq = &lpfc_cmd->cur_iocbq;
0bd4ca25 3135 iocbqrsp = lpfc_sli_get_iocbq(phba);
915caaaf
JS
3136 if (iocbqrsp == NULL) {
3137 lpfc_release_scsi_buf(phba, lpfc_cmd);
3138 return FAILED;
3139 }
bbb9d180 3140
e8b62011 3141 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
bbb9d180
JS
3142 "0702 Issue %s to TGT %d LUN %d "
3143 "rpi x%x nlp_flag x%x\n",
3144 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3145 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
3146
3772a991 3147 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
915caaaf 3148 iocbq, iocbqrsp, lpfc_cmd->timeout);
bbb9d180
JS
3149 if (status != IOCB_SUCCESS) {
3150 if (status == IOCB_TIMEDOUT) {
3151 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3152 ret = TIMEOUT_ERROR;
3153 } else
915caaaf 3154 ret = FAILED;
bbb9d180
JS
3155 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3156 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3157 "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3158 lpfc_taskmgmt_name(task_mgmt_cmd),
3159 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
915caaaf 3160 iocbqrsp->iocb.un.ulpWord[4]);
bbb9d180
JS
3161 } else
3162 ret = SUCCESS;
3163
6175c02a 3164 lpfc_sli_release_iocbq(phba, iocbqrsp);
bbb9d180
JS
3165
3166 if (ret != TIMEOUT_ERROR)
3167 lpfc_release_scsi_buf(phba, lpfc_cmd);
3168
3169 return ret;
3170}
3171
3172/**
3173 * lpfc_chk_tgt_mapped -
3174 * @vport: The virtual port to check on
3175 * @cmnd: Pointer to scsi_cmnd data structure.
3176 *
3177 * This routine delays until the scsi target (aka rport) for the
3178 * command exists (is present and logged in) or we declare it non-existent.
3179 *
3180 * Return code :
3181 * 0x2003 - Error
3182 * 0x2002 - Success
3183 **/
3184static int
3185lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3186{
3187 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3188 struct lpfc_nodelist *pnode = rdata->pnode;
3189 unsigned long later;
3190
3191 /*
3192 * If target is not in a MAPPED state, delay until
3193 * target is rediscovered or devloss timeout expires.
3194 */
3195 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3196 while (time_after(later, jiffies)) {
3197 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3198 return FAILED;
3199 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3200 return SUCCESS;
3201 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3202 rdata = cmnd->device->hostdata;
3203 if (!rdata)
3204 return FAILED;
3205 pnode = rdata->pnode;
3206 }
3207 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3208 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3209 return FAILED;
3210 return SUCCESS;
3211}
3212
3213/**
3214 * lpfc_reset_flush_io_context -
3215 * @vport: The virtual port (scsi_host) for the flush context
3216 * @tgt_id: If aborting by Target contect - specifies the target id
3217 * @lun_id: If aborting by Lun context - specifies the lun id
3218 * @context: specifies the context level to flush at.
3219 *
3220 * After a reset condition via TMF, we need to flush orphaned i/o
3221 * contexts from the adapter. This routine aborts any contexts
3222 * outstanding, then waits for their completions. The wait is
3223 * bounded by devloss_tmo though.
3224 *
3225 * Return code :
3226 * 0x2003 - Error
3227 * 0x2002 - Success
3228 **/
3229static int
3230lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3231 uint64_t lun_id, lpfc_ctx_cmd context)
3232{
3233 struct lpfc_hba *phba = vport->phba;
3234 unsigned long later;
3235 int cnt;
3236
3237 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
6175c02a 3238 if (cnt)
51ef4c26 3239 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
bbb9d180 3240 tgt_id, lun_id, context);
915caaaf
JS
3241 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3242 while (time_after(later, jiffies) && cnt) {
3243 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
bbb9d180 3244 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
dea3101e 3245 }
dea3101e 3246 if (cnt) {
e8b62011 3247 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
bbb9d180
JS
3248 "0724 I/O flush failure for context %s : cnt x%x\n",
3249 ((context == LPFC_CTX_LUN) ? "LUN" :
3250 ((context == LPFC_CTX_TGT) ? "TGT" :
3251 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3252 cnt);
3253 return FAILED;
dea3101e 3254 }
bbb9d180
JS
3255 return SUCCESS;
3256}
3257
3258/**
3259 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
3260 * @cmnd: Pointer to scsi_cmnd data structure.
3261 *
3262 * This routine does a device reset by sending a LUN_RESET task management
3263 * command.
3264 *
3265 * Return code :
3266 * 0x2003 - Error
3267 * 0x2002 - Success
3268 **/
3269static int
3270lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
3271{
3272 struct Scsi_Host *shost = cmnd->device->host;
3273 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3274 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3275 struct lpfc_nodelist *pnode = rdata->pnode;
3276 unsigned tgt_id = cmnd->device->id;
3277 unsigned int lun_id = cmnd->device->lun;
3278 struct lpfc_scsi_event_header scsi_event;
3279 int status;
3280
65d430fa 3281 fc_block_scsi_eh(cmnd);
bbb9d180
JS
3282
3283 status = lpfc_chk_tgt_mapped(vport, cmnd);
3284 if (status == FAILED) {
3285 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3286 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3287 return FAILED;
3288 }
3289
3290 scsi_event.event_type = FC_REG_SCSI_EVENT;
3291 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3292 scsi_event.lun = lun_id;
3293 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3294 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3295
3296 fc_host_post_vendor_event(shost, fc_get_event_number(),
3297 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3298
3299 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3300 FCP_LUN_RESET);
3301
3302 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3303 "0713 SCSI layer issued Device Reset (%d, %d) "
3304 "return x%x\n", tgt_id, lun_id, status);
3305
3306 /*
3307 * We have to clean up i/o as : they may be orphaned by the TMF;
3308 * or if the TMF failed, they may be in an indeterminate state.
3309 * So, continue on.
3310 * We will report success if all the i/o aborts successfully.
3311 */
3312 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3313 LPFC_CTX_LUN);
3314 return status;
3315}
3316
3317/**
3318 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3319 * @cmnd: Pointer to scsi_cmnd data structure.
3320 *
3321 * This routine does a target reset by sending a TARGET_RESET task management
3322 * command.
3323 *
3324 * Return code :
3325 * 0x2003 - Error
3326 * 0x2002 - Success
3327 **/
3328static int
3329lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3330{
3331 struct Scsi_Host *shost = cmnd->device->host;
3332 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3333 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3334 struct lpfc_nodelist *pnode = rdata->pnode;
3335 unsigned tgt_id = cmnd->device->id;
3336 unsigned int lun_id = cmnd->device->lun;
3337 struct lpfc_scsi_event_header scsi_event;
3338 int status;
3339
65d430fa 3340 fc_block_scsi_eh(cmnd);
bbb9d180
JS
3341
3342 status = lpfc_chk_tgt_mapped(vport, cmnd);
3343 if (status == FAILED) {
3344 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3345 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3346 return FAILED;
3347 }
3348
3349 scsi_event.event_type = FC_REG_SCSI_EVENT;
3350 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3351 scsi_event.lun = 0;
3352 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3353 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3354
3355 fc_host_post_vendor_event(shost, fc_get_event_number(),
3356 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3357
3358 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3359 FCP_TARGET_RESET);
3360
3361 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3362 "0723 SCSI layer issued Target Reset (%d, %d) "
3363 "return x%x\n", tgt_id, lun_id, status);
3364
3365 /*
3366 * We have to clean up i/o as : they may be orphaned by the TMF;
3367 * or if the TMF failed, they may be in an indeterminate state.
3368 * So, continue on.
3369 * We will report success if all the i/o aborts successfully.
3370 */
3371 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3372 LPFC_CTX_TGT);
3373 return status;
dea3101e 3374}
3375
9bad7671 3376/**
3621a710 3377 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
9bad7671
JS
3378 * @cmnd: Pointer to scsi_cmnd data structure.
3379 *
bbb9d180
JS
3380 * This routine does target reset to all targets on @cmnd->device->host.
3381 * This emulates Parallel SCSI Bus Reset Semantics.
9bad7671 3382 *
bbb9d180
JS
3383 * Return code :
3384 * 0x2003 - Error
3385 * 0x2002 - Success
9bad7671 3386 **/
94d0e7b8 3387static int
7054a606 3388lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 3389{
2e0fef85
JS
3390 struct Scsi_Host *shost = cmnd->device->host;
3391 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea3101e 3392 struct lpfc_nodelist *ndlp = NULL;
ea2151b4 3393 struct lpfc_scsi_event_header scsi_event;
bbb9d180
JS
3394 int match;
3395 int ret = SUCCESS, status, i;
ea2151b4
JS
3396
3397 scsi_event.event_type = FC_REG_SCSI_EVENT;
3398 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3399 scsi_event.lun = 0;
3400 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3401 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3402
bbb9d180
JS
3403 fc_host_post_vendor_event(shost, fc_get_event_number(),
3404 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea3101e 3405
65d430fa 3406 fc_block_scsi_eh(cmnd);
bbb9d180 3407
dea3101e 3408 /*
3409 * Since the driver manages a single bus device, reset all
3410 * targets known to the driver. Should any target reset
3411 * fail, this routine returns failure to the midlayer.
3412 */
e17da18e 3413 for (i = 0; i < LPFC_MAX_TARGET; i++) {
685f0bf7 3414 /* Search for mapped node by target ID */
dea3101e 3415 match = 0;
2e0fef85
JS
3416 spin_lock_irq(shost->host_lock);
3417 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
3418 if (!NLP_CHK_NODE_ACT(ndlp))
3419 continue;
685f0bf7 3420 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
915caaaf 3421 ndlp->nlp_sid == i &&
685f0bf7 3422 ndlp->rport) {
dea3101e 3423 match = 1;
3424 break;
3425 }
3426 }
2e0fef85 3427 spin_unlock_irq(shost->host_lock);
dea3101e 3428 if (!match)
3429 continue;
bbb9d180
JS
3430
3431 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3432 i, 0, FCP_TARGET_RESET);
3433
3434 if (status != SUCCESS) {
e8b62011
JS
3435 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3436 "0700 Bus Reset on target %d failed\n",
3437 i);
915caaaf 3438 ret = FAILED;
dea3101e 3439 }
3440 }
6175c02a 3441 /*
bbb9d180
JS
3442 * We have to clean up i/o as : they may be orphaned by the TMFs
3443 * above; or if any of the TMFs failed, they may be in an
3444 * indeterminate state.
3445 * We will report success if all the i/o aborts successfully.
6175c02a 3446 */
bbb9d180
JS
3447
3448 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3449 if (status != SUCCESS)
0bd4ca25 3450 ret = FAILED;
bbb9d180 3451
e8b62011
JS
3452 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3453 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea3101e 3454 return ret;
3455}
3456
9bad7671 3457/**
3621a710 3458 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
9bad7671
JS
3459 * @sdev: Pointer to scsi_device.
3460 *
3461 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3462 * globally available list of scsi buffers. This routine also makes sure scsi
3463 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3464 * of scsi buffer exists for the lifetime of the driver.
3465 *
3466 * Return codes:
3467 * non-0 - Error
3468 * 0 - Success
3469 **/
dea3101e 3470static int
3471lpfc_slave_alloc(struct scsi_device *sdev)
3472{
2e0fef85
JS
3473 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3474 struct lpfc_hba *phba = vport->phba;
19a7b4ae 3475 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3772a991 3476 uint32_t total = 0;
dea3101e 3477 uint32_t num_to_alloc = 0;
3772a991 3478 int num_allocated = 0;
dea3101e 3479
19a7b4ae 3480 if (!rport || fc_remote_port_chkready(rport))
dea3101e 3481 return -ENXIO;
3482
19a7b4ae 3483 sdev->hostdata = rport->dd_data;
dea3101e 3484
3485 /*
3486 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3487 * available list of scsi buffers. Don't allocate more than the
a784efbf
JSEC
3488 * HBA limit conveyed to the midlayer via the host structure. The
3489 * formula accounts for the lun_queue_depth + error handlers + 1
3490 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea3101e 3491 */
3492 total = phba->total_scsi_bufs;
3de2a653 3493 num_to_alloc = vport->cfg_lun_queue_depth + 2;
92d7f7b0
JS
3494
3495 /* Allow some exchanges to be available always to complete discovery */
3496 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
3497 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3498 "0704 At limitation of %d preallocated "
3499 "command buffers\n", total);
dea3101e 3500 return 0;
92d7f7b0
JS
3501 /* Allow some exchanges to be available always to complete discovery */
3502 } else if (total + num_to_alloc >
3503 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
3504 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3505 "0705 Allocation request of %d "
3506 "command buffers will exceed max of %d. "
3507 "Reducing allocation request to %d.\n",
3508 num_to_alloc, phba->cfg_hba_queue_depth,
3509 (phba->cfg_hba_queue_depth - total));
dea3101e 3510 num_to_alloc = phba->cfg_hba_queue_depth - total;
3511 }
3772a991
JS
3512 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3513 if (num_to_alloc != num_allocated) {
3514 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3515 "0708 Allocation request of %d "
3516 "command buffers did not succeed. "
3517 "Allocated %d buffers.\n",
3518 num_to_alloc, num_allocated);
dea3101e 3519 }
3520 return 0;
3521}
3522
9bad7671 3523/**
3621a710 3524 * lpfc_slave_configure - scsi_host_template slave_configure entry point
9bad7671
JS
3525 * @sdev: Pointer to scsi_device.
3526 *
3527 * This routine configures following items
3528 * - Tag command queuing support for @sdev if supported.
3529 * - Dev loss time out value of fc_rport.
3530 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3531 *
3532 * Return codes:
3533 * 0 - Success
3534 **/
dea3101e 3535static int
3536lpfc_slave_configure(struct scsi_device *sdev)
3537{
2e0fef85
JS
3538 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3539 struct lpfc_hba *phba = vport->phba;
3540 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea3101e 3541
3542 if (sdev->tagged_supported)
3de2a653 3543 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 3544 else
3de2a653 3545 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 3546
3547 /*
3548 * Initialize the fc transport attributes for the target
3549 * containing this scsi device. Also note that the driver's
3550 * target pointer is stored in the starget_data for the
3551 * driver's sysfs entry point functions.
3552 */
3de2a653 3553 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea3101e 3554
875fbdfe 3555 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
3556 lpfc_sli_handle_fast_ring_event(phba,
3557 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
875fbdfe
JSEC
3558 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3559 lpfc_poll_rearm_timer(phba);
3560 }
3561
dea3101e 3562 return 0;
3563}
3564
9bad7671 3565/**
3621a710 3566 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
9bad7671
JS
3567 * @sdev: Pointer to scsi_device.
3568 *
3569 * This routine sets @sdev hostatdata filed to null.
3570 **/
dea3101e 3571static void
3572lpfc_slave_destroy(struct scsi_device *sdev)
3573{
3574 sdev->hostdata = NULL;
3575 return;
3576}
3577
92d7f7b0 3578
dea3101e 3579struct scsi_host_template lpfc_template = {
3580 .module = THIS_MODULE,
3581 .name = LPFC_DRIVER_NAME,
3582 .info = lpfc_info,
3583 .queuecommand = lpfc_queuecommand,
3584 .eh_abort_handler = lpfc_abort_handler,
bbb9d180
JS
3585 .eh_device_reset_handler = lpfc_device_reset_handler,
3586 .eh_target_reset_handler = lpfc_target_reset_handler,
7054a606 3587 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea3101e 3588 .slave_alloc = lpfc_slave_alloc,
3589 .slave_configure = lpfc_slave_configure,
3590 .slave_destroy = lpfc_slave_destroy,
47a8617c 3591 .scan_finished = lpfc_scan_finished,
dea3101e 3592 .this_id = -1,
83108bd3 3593 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea3101e 3594 .cmd_per_lun = LPFC_CMD_PER_LUN,
3595 .use_clustering = ENABLE_CLUSTERING,
2e0fef85 3596 .shost_attrs = lpfc_hba_attrs,
564b2960 3597 .max_sectors = 0xFFFF,
f1c3b0fc 3598 .vendor_id = LPFC_NL_VENDOR_ID,
dea3101e 3599};
3de2a653
JS
3600
3601struct scsi_host_template lpfc_vport_template = {
3602 .module = THIS_MODULE,
3603 .name = LPFC_DRIVER_NAME,
3604 .info = lpfc_info,
3605 .queuecommand = lpfc_queuecommand,
3606 .eh_abort_handler = lpfc_abort_handler,
bbb9d180
JS
3607 .eh_device_reset_handler = lpfc_device_reset_handler,
3608 .eh_target_reset_handler = lpfc_target_reset_handler,
3de2a653
JS
3609 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3610 .slave_alloc = lpfc_slave_alloc,
3611 .slave_configure = lpfc_slave_configure,
3612 .slave_destroy = lpfc_slave_destroy,
3613 .scan_finished = lpfc_scan_finished,
3614 .this_id = -1,
83108bd3 3615 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3de2a653
JS
3616 .cmd_per_lun = LPFC_CMD_PER_LUN,
3617 .use_clustering = ENABLE_CLUSTERING,
3618 .shost_attrs = lpfc_vport_attrs,
3619 .max_sectors = 0xFFFF,
3620};