This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_scsiport.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Enterprise Fibre Channel Host Bus Adapters.                     *
4  * Refer to the README file included with this package for         *
5  * driver version and adapter support.                             *
6  * Copyright (C) 2004 Emulex Corporation.                          *
7  * www.emulex.com                                                  *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of the GNU General Public License     *
11  * as published by the Free Software Foundation; either version 2  *
12  * of the License, or (at your option) any later version.          *
13  *                                                                 *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of  *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
17  * GNU General Public License for more details, a copy of which    *
18  * can be found in the file COPYING included with this package.    *
19  *******************************************************************/
20
21 /*
22  * $Id: lpfc_scsiport.c 1.208 2004/12/03 11:27:34EST sf_support Exp  $
23  */
24 #include <linux/version.h>
25 #include <linux/spinlock.h>
26 #include <linux/pci.h>
27 #include <linux/blkdev.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_transport_fc.h>
33
34 #include "lpfc_hw.h"
35 #include "lpfc_sli.h"
36 #include "lpfc_mem.h"
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_fcp.h"
42 #include "lpfc_crtn.h"
43
44 /* This routine allocates a scsi buffer, which contains all the necessary
45  * information needed to initiate a SCSI I/O. The non-DMAable region of
46  * the buffer contains the area to build the IOCB. The DMAable region contains
47  * the memory for the FCP CMND, FCP RSP, and the inital BPL.
48  * In addition to allocating memeory, the FCP CMND and FCP RSP BDEs are setup
49  * in the BPL and the BPL BDE is setup in the IOCB.
50  */
51 struct lpfc_scsi_buf *
52 lpfc_get_scsi_buf(struct lpfc_hba * phba, int gfp_flags)
53 {
54         struct lpfc_scsi_buf *psb;
55         struct ulp_bde64 *bpl;
56         IOCB_t *cmd;
57         uint8_t *ptr;
58         dma_addr_t pdma_phys;
59
60         psb = mempool_alloc(phba->scsibuf_mem_pool, gfp_flags);
61         if (!psb)
62                 return NULL;
63
64         memset(psb, 0, sizeof (struct lpfc_scsi_buf));
65
66         /* Get a SCSI DMA extention for an I/O */
67         /*
68          * The DMA buffer for struct fcp_cmnd, struct fcp_rsp and BPL use
69          * lpfc_scsi_dma_ext_pool with size LPFC_SCSI_DMA_EXT_SIZE
70          *
71          *
72          *    The size of struct fcp_cmnd  = 32 bytes.
73          *    The size of struct fcp_rsp   = 160 bytes.
74          *    The size of struct ulp_bde64 = 12 bytes and driver can only
75          *    support LPFC_SCSI_INITIAL_BPL_SIZE (3) S/G segments for scsi data.
76          *    One struct ulp_bde64 is used for each of the struct fcp_cmnd and
77          *    struct fcp_rsp
78          *
79          *    Total usage for each I/O use 32 + 160 + (2 * 12) +
80          *    (4 * 12) = 264 bytes.
81          */
82
83         INIT_LIST_HEAD(&psb->dma_ext.list);
84
85         psb->dma_ext.virt = pci_pool_alloc(phba->lpfc_scsi_dma_ext_pool,
86                                            GFP_ATOMIC, &psb->dma_ext.phys);
87         if (!psb->dma_ext.virt) {
88                 mempool_free(psb, phba->scsibuf_mem_pool);
89                 return NULL;
90         }
91
92         /* Save virtual ptrs to FCP Command, Response, and BPL */
93         ptr = (uint8_t *) psb->dma_ext.virt;
94
95         memset(ptr, 0, LPFC_SCSI_DMA_EXT_SIZE);
96         psb->fcp_cmnd = (struct fcp_cmnd *) ptr;
97         ptr += sizeof (struct fcp_cmnd);
98         psb->fcp_rsp = (struct fcp_rsp *) ptr;
99         ptr += (sizeof (struct fcp_rsp));
100         psb->fcp_bpl = (struct ulp_bde64 *) ptr;
101         psb->scsi_hba = phba;
102
103         /* Since this is for a FCP cmd, the first 2 BDEs in the BPL are always
104          * the FCP CMND and FCP RSP, so lets just set it up right here.
105          */
106         bpl = psb->fcp_bpl;
107         /* ptr points to physical address of FCP CMD */
108         pdma_phys = psb->dma_ext.phys;
109         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
110         bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
111         bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
112         bpl->tus.f.bdeFlags = BUFF_USE_CMND;
113         bpl->tus.w = le32_to_cpu(bpl->tus.w);
114         bpl++;
115
116         /* Setup FCP RSP */
117         pdma_phys += sizeof (struct fcp_cmnd);
118         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
119         bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
120         bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
121         bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
122         bpl->tus.w = le32_to_cpu(bpl->tus.w);
123         bpl++;
124
125         /* Since the IOCB for the FCP I/O is built into the struct
126          * lpfc_scsi_buf, lets setup what we can right here.
127          */
128         pdma_phys += (sizeof (struct fcp_rsp));
129         cmd = &psb->cur_iocbq.iocb;
130         cmd->un.fcpi64.bdl.ulpIoTag32 = 0;
131         cmd->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
132         cmd->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
133         cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
134         cmd->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
135         cmd->ulpBdeCount = 1;
136         cmd->ulpClass = CLASS3;
137
138         return (psb);
139 }
140
141 static void
142 lpfc_free_scsi_buf(struct lpfc_scsi_buf * psb)
143 {
144         struct lpfc_hba *phba = psb->scsi_hba;
145         struct lpfc_dmabuf *pbpl, *next_bpl;
146
147         /*
148          * There are only two special cases to consider.  (1) the scsi command
149          * requested scatter-gather usage or (2) the scsi command allocated
150          * a request buffer, but did not request use_sg.  There is a third
151          * case, but it does not require resource deallocation.
152          */
153
154         if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
155                 /*
156                  * Since the segment count is nonzero, the scsi command 
157                  * requested scatter-gather usage and the driver allocated
158                  * addition memory buffers to chain BPLs.  Traverse this list
159                  * and release those resource before freeing the parent 
160                  * structure.
161                  */
162                 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer, 
163                                 psb->seg_cnt, psb->pCmd->sc_data_direction);
164
165                 list_for_each_entry_safe(pbpl, next_bpl, 
166                                                 &psb->dma_ext.list, list) {
167                         lpfc_mbuf_free(phba, pbpl->virt, pbpl->phys);
168                         list_del(&pbpl->list);
169                         kfree(pbpl);
170                 }
171         } else {
172                  if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
173                         /*
174                          * Since either the segment count or the use_sg
175                          * value is zero, the scsi command did not request 
176                          * scatter-gather usage and no additional buffers were
177                          * required.  Just unmap the dma single resource.
178                          */
179                         dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
180                                                 psb->pCmd->request_bufflen,
181                                                 psb->pCmd->sc_data_direction);
182                  }
183         }
184
185         /*
186          * Release the pci pool resource and clean up the scsi buffer.  Neither
187          * are required now that the IO has completed.
188          */
189         pci_pool_free(phba->lpfc_scsi_dma_ext_pool, psb->dma_ext.virt,
190                                                          psb->dma_ext.phys);
191         mempool_free(psb, phba->scsibuf_mem_pool);
192 }
193
194 static int
195 lpfc_os_prep_io(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
196 {
197         struct fcp_cmnd *fcp_cmnd;
198         struct ulp_bde64 *topbpl = NULL;
199         struct ulp_bde64 *bpl;
200         struct lpfc_dmabuf *bmp;
201         struct lpfc_dmabuf *head_bmp;
202         IOCB_t *cmd;
203         struct scsi_cmnd *cmnd;
204         struct scatterlist *sgel = NULL;
205         struct scatterlist *sgel_begin = NULL;
206         dma_addr_t physaddr;
207         uint32_t i;
208         uint32_t num_bmps = 1, num_bde = 0, max_bde;
209         uint16_t use_sg;
210         int datadir;
211         int dma_error;
212
213         bpl = lpfc_cmd->fcp_bpl;
214         fcp_cmnd = lpfc_cmd->fcp_cmnd;
215
216         bpl += 2;               /* Bump past FCP CMND and FCP RSP */
217         max_bde = LPFC_SCSI_INITIAL_BPL_SIZE - 1;
218
219         cmnd = lpfc_cmd->pCmd;
220         cmd = &lpfc_cmd->cur_iocbq.iocb;
221
222         /* These are needed if we chain BPLs */
223         head_bmp = &(lpfc_cmd->dma_ext);
224         use_sg = cmnd->use_sg;
225
226         /*
227          * Fill in the FCP CMND
228          */
229         memcpy(&fcp_cmnd->fcpCdb[0], cmnd->cmnd, 16);
230
231         if (cmnd->device->tagged_supported) {
232                 switch (cmnd->tag) {
233                 case HEAD_OF_QUEUE_TAG:
234                         fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
235                         break;
236                 case ORDERED_QUEUE_TAG:
237                         fcp_cmnd->fcpCntl1 = ORDERED_Q;
238                         break;
239                 default:
240                         fcp_cmnd->fcpCntl1 = SIMPLE_Q;
241                         break;
242                 }
243         } else {
244                 fcp_cmnd->fcpCntl1 = 0;
245         }
246
247         datadir = cmnd->sc_data_direction;
248
249         if (use_sg) {
250                 /*
251                  * Get a local pointer to the scatter-gather list.  The
252                  * scatter-gather list head must be preserved since
253                  * sgel is incremented in the loop.  The driver must store
254                  * the segment count returned from pci_map_sg for calls to
255                  * pci_unmap_sg later on because the use_sg field in the
256                  * scsi_cmd is a count of physical memory pages, whereas the
257                  * seg_cnt is a count of dma-mappings used by the MMIO to
258                  * map the use_sg pages.  They are not the same in most
259                  * cases for those architectures that implement an MMIO.
260                  */
261                 sgel = (struct scatterlist *)cmnd->request_buffer;
262                 sgel_begin = sgel;
263                 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
264                                                 use_sg, datadir);
265
266                 /* return error if we cannot map sg list */
267                 if (lpfc_cmd->seg_cnt == 0)
268                         return 1;
269
270                 /* scatter-gather list case */
271                 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
272                         /* Check to see if current BPL is full of BDEs */
273                         /* If this is last BDE and there is one left in */
274                         /* current BPL, use it.                         */
275                         if (num_bde == max_bde) {
276                                 bmp = kmalloc(sizeof (struct lpfc_dmabuf),
277                                               GFP_ATOMIC);
278                                 if (bmp == 0) {
279                                         goto error_out;
280                                 }
281                                 memset(bmp, 0, sizeof (struct lpfc_dmabuf));
282                                 bmp->virt =
283                                     lpfc_mbuf_alloc(phba, 0, &bmp->phys);
284                                 if (!bmp->virt) {
285                                         kfree(bmp);
286                                         goto error_out;
287                                 }
288                                 max_bde = ((1024 / sizeof(struct ulp_bde64))-3);
289                                 /* Fill in continuation entry to next bpl */
290                                 bpl->addrHigh =
291                                     le32_to_cpu(putPaddrHigh(bmp->phys));
292                                 bpl->addrLow =
293                                     le32_to_cpu(putPaddrLow(bmp->phys));
294                                 bpl->tus.f.bdeFlags = BPL64_SIZE_WORD;
295                                 num_bde++;
296                                 if (num_bmps == 1) {
297                                         cmd->un.fcpi64.bdl.bdeSize += (num_bde *
298                                                 sizeof (struct ulp_bde64));
299                                 } else {
300                                         topbpl->tus.f.bdeSize = (num_bde *
301                                                 sizeof (struct ulp_bde64));
302                                         topbpl->tus.w =
303                                             le32_to_cpu(topbpl->tus.w);
304                                 }
305                                 topbpl = bpl;
306                                 bpl = (struct ulp_bde64 *) bmp->virt;
307                                 list_add(&bmp->list, &head_bmp->list);
308                                 num_bde = 0;
309                                 num_bmps++;
310                         }
311
312                         physaddr = sg_dma_address(sgel);
313
314                         bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
315                         bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
316                         bpl->tus.f.bdeSize = sg_dma_len(sgel);
317                         if (datadir == DMA_TO_DEVICE)
318                                 bpl->tus.f.bdeFlags = 0;
319                         else
320                                 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
321                         bpl->tus.w = le32_to_cpu(bpl->tus.w);
322                         bpl++;
323                         sgel++;
324                         num_bde++;
325                 }               /* end for loop */
326
327                 if (datadir == DMA_TO_DEVICE) {
328                         cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
329                         fcp_cmnd->fcpCntl3 = WRITE_DATA;
330
331                         phba->fc4OutputRequests++;
332                 } else {
333                         cmd->ulpCommand = CMD_FCP_IREAD64_CR;
334                         cmd->ulpPU = PARM_READ_CHECK;
335                         cmd->un.fcpi.fcpi_parm = cmnd->request_bufflen;
336                         fcp_cmnd->fcpCntl3 = READ_DATA;
337
338                         phba->fc4InputRequests++;
339                 }
340         } else if (cmnd->request_buffer && cmnd->request_bufflen) {
341                 physaddr = dma_map_single(&phba->pcidev->dev,
342                                           cmnd->request_buffer,
343                                           cmnd->request_bufflen,
344                                           datadir);
345                         dma_error = dma_mapping_error(physaddr);
346                         if (dma_error){
347                                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
348                                         "%d:0718 Unable to dma_map_single "
349                                         "request_buffer: x%x\n",
350                                         phba->brd_no, dma_error);
351                                 return 1;
352                         }
353
354                 /* no scatter-gather list case */
355                 lpfc_cmd->nonsg_phys = physaddr;
356                 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
357                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
358                 bpl->tus.f.bdeSize = cmnd->request_bufflen;
359                 if (datadir == DMA_TO_DEVICE) {
360                         cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
361                         fcp_cmnd->fcpCntl3 = WRITE_DATA;
362                         bpl->tus.f.bdeFlags = 0;
363
364                         phba->fc4OutputRequests++;
365                 } else {
366                         cmd->ulpCommand = CMD_FCP_IREAD64_CR;
367                         cmd->ulpPU = PARM_READ_CHECK;
368                         cmd->un.fcpi.fcpi_parm = cmnd->request_bufflen;
369                         fcp_cmnd->fcpCntl3 = READ_DATA;
370                         bpl->tus.f.bdeFlags = BUFF_USE_RCV;
371
372                         phba->fc4InputRequests++;
373                 }
374                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
375                 num_bde = 1;
376                 bpl++;
377         } else {
378                 cmd->ulpCommand = CMD_FCP_ICMND64_CR;
379                 cmd->un.fcpi.fcpi_parm = 0;
380                 fcp_cmnd->fcpCntl3 = 0;
381
382                 phba->fc4ControlRequests++;
383         }
384
385         bpl->addrHigh = 0;
386         bpl->addrLow = 0;
387         bpl->tus.w = 0;
388         if (num_bmps == 1) {
389                 cmd->un.fcpi64.bdl.bdeSize +=
390                         (num_bde * sizeof (struct ulp_bde64));
391         } else {
392                 topbpl->tus.f.bdeSize = (num_bde * sizeof (struct ulp_bde64));
393                 topbpl->tus.w = le32_to_cpu(topbpl->tus.w);
394         }
395         cmd->ulpBdeCount = 1;
396         cmd->ulpLe = 1;         /* Set the LE bit in the iocb */
397
398         /* set the Data Length field in the FCP CMND accordingly */
399         fcp_cmnd->fcpDl = be32_to_cpu(cmnd->request_bufflen);
400
401         return 0;
402
403 error_out:
404         /*
405          * Allocation of a chained BPL failed, unmap the sg list and return
406          * error.  This will ultimately cause lpfc_free_scsi_buf to be called
407          * which will handle the rest of the cleanup.  Set seg_cnt back to zero
408          * to avoid double unmaps of the sg resources.
409          */
410         dma_unmap_sg(&phba->pcidev->dev, sgel_begin, lpfc_cmd->seg_cnt,
411                         datadir);
412         lpfc_cmd->seg_cnt = 0;
413         return 1;
414 }
415
416 static void
417 lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
418 {
419         struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
420         struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
421         struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
422         struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
423         uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
424         uint32_t resp_info = fcprsp->rspStatus2;
425         uint32_t scsi_status = fcprsp->rspStatus3;
426         uint32_t host_status = DID_OK;
427         uint32_t rsplen = 0;
428
429         /*
430          *  If this is a task management command, there is no
431          *  scsi packet associated with this lpfc_cmd.  The driver
432          *  consumes it.
433          */
434         if (fcpcmd->fcpCntl2) {
435                 scsi_status = 0;
436                 goto out;
437         }
438
439         lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
440                         "%d:0730 FCP command failed: RSP "
441                         "Data: x%x x%x x%x x%x x%x x%x\n",
442                         phba->brd_no, resp_info, scsi_status,
443                         be32_to_cpu(fcprsp->rspResId),
444                         be32_to_cpu(fcprsp->rspSnsLen),
445                         be32_to_cpu(fcprsp->rspRspLen),
446                         fcprsp->rspInfo3);
447
448         if (resp_info & RSP_LEN_VALID) {
449                 rsplen = be32_to_cpu(fcprsp->rspRspLen);
450                 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
451                     (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
452                         host_status = DID_ERROR;
453                         goto out;
454                 }
455         }
456
457         if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
458                 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
459                 if (snslen > SCSI_SENSE_BUFFERSIZE)
460                         snslen = SCSI_SENSE_BUFFERSIZE;
461
462                 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
463         }
464
465         cmnd->resid = 0;
466         if (resp_info & RESID_UNDER) {
467                 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
468
469                 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
470                                 "%d:0716 FCP Read Underrun, expected %d, "
471                                 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
472                                 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
473                                 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
474
475                 /*
476                  * The cmnd->underflow is the minimum number of bytes that must
477                  * be transfered for this command.  Provided a sense condition is
478                  * not present, make sure the actual amount transferred is at
479                  * least the underflow value or fail.
480                  */
481                 if (!(resp_info & SNS_LEN_VALID) &&
482                     (scsi_status == SAM_STAT_GOOD) &&
483                     (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
484                         lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
485                                         "%d:0717 FCP command x%x residual "
486                                         "underrun converted to error "
487                                         "Data: x%x x%x x%x\n", phba->brd_no,
488                                         cmnd->cmnd[0], cmnd->request_bufflen,
489                                         cmnd->resid, cmnd->underflow);
490
491                         host_status = DID_ERROR;
492                 }
493         } else if (resp_info & RESID_OVER) {
494                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
495                                 "%d:0720 FCP command x%x residual "
496                                 "overrun error. Data: x%x x%x \n",
497                                 phba->brd_no, cmnd->cmnd[0], 
498                                 cmnd->request_bufflen, cmnd->resid);
499                 host_status = DID_ERROR;
500
501         /*
502          * Check SLI validation that all the transfer was actually done
503          * (fcpi_parm should be zero). Apply check only to reads.
504          */
505         } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
506                         (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
507                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
508                         "%d:0734 FCP Read Check Error Data: "
509                         "x%x x%x x%x x%x\n", phba->brd_no,
510                         be32_to_cpu(fcpcmd->fcpDl),
511                         be32_to_cpu(fcprsp->rspResId),
512                         fcpi_parm, cmnd->cmnd[0]);
513                 host_status = DID_ERROR;
514                 cmnd->resid = cmnd->request_bufflen;
515         }
516
517  out:
518         cmnd->result = ScsiResult(host_status, scsi_status);
519 }
520
521 static void
522 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
523                         struct lpfc_iocbq *pIocbOut)
524 {
525         struct lpfc_scsi_buf *lpfc_cmd =
526                 (struct lpfc_scsi_buf *) pIocbIn->context1;
527         struct lpfc_target *target = lpfc_cmd->target;
528         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
529
530         lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
531         lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
532
533         pci_dma_sync_single_for_cpu(phba->pcidev, lpfc_cmd->dma_ext.phys,
534                         LPFC_BPL_SIZE, PCI_DMA_FROMDEVICE);
535
536         target->iodonecnt++;
537
538         if (lpfc_cmd->status) {
539                 target->errorcnt++;
540
541                 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
542                     (lpfc_cmd->result & IOERR_DRVR_MASK))
543                         lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
544                 else if (lpfc_cmd->status >= IOSTAT_CNT)
545                         lpfc_cmd->status = IOSTAT_DEFAULT;
546
547                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
548                                 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
549                                 "x%x result: x%x Data: x%x x%x\n",
550                                 phba->brd_no, cmd->cmnd[0], cmd->device->id,
551                                 cmd->device->lun, lpfc_cmd->status,
552                                 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
553                                 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
554
555                 switch (lpfc_cmd->status) {
556                 case IOSTAT_FCP_RSP_ERROR:
557                         /* Call FCP RSP handler to determine result */
558                         lpfc_handle_fcp_err(lpfc_cmd);
559                         break;
560                 case IOSTAT_NPORT_BSY:
561                 case IOSTAT_FABRIC_BSY:
562                         cmd->result = ScsiResult(DID_BUS_BUSY, 0);
563                         break;
564                 default:
565                         cmd->result = ScsiResult(DID_ERROR, 0);
566                         break;
567                 }
568
569                 if (target->pnode) {
570                         if(target->pnode->nlp_state != NLP_STE_MAPPED_NODE)
571                                 cmd->result = ScsiResult(DID_BUS_BUSY,
572                                         SAM_STAT_BUSY);
573                 }
574                 else {
575                         cmd->result = ScsiResult(DID_NO_CONNECT, 0);
576                 }
577         } else {
578                 cmd->result = ScsiResult(DID_OK, 0);
579         }
580
581         if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
582                 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
583
584                 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
585                                 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
586                                 "SNS x%x x%x Data: x%x x%x\n",
587                                 phba->brd_no, cmd->device->id,
588                                 cmd->device->lun, cmd, cmd->result,
589                                 *lp, *(lp + 3), cmd->retries, cmd->resid);
590         }
591
592         lpfc_free_scsi_buf(lpfc_cmd);
593         cmd->host_scribble = NULL;
594         cmd->scsi_done(cmd);
595 }
596
597 static int
598 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
599                              struct lpfc_scsi_buf *lpfc_cmd,
600                              uint8_t task_mgmt_cmd)
601 {
602
603         struct lpfc_sli *psli;
604         struct lpfc_iocbq *piocbq;
605         IOCB_t *piocb;
606         struct fcp_cmnd *fcp_cmnd;
607         struct scsi_device *scsi_dev = lpfc_cmd->pCmd->device;
608         struct lpfc_target *target = scsi_dev->hostdata;
609         struct lpfc_nodelist *ndlp = target->pnode;
610
611         if ((ndlp == 0) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
612                 return 0;
613         }
614
615         /* allocate an iocb command */
616         psli = &phba->sli;
617         piocbq = &(lpfc_cmd->cur_iocbq);
618         piocb = &piocbq->iocb;
619
620
621         fcp_cmnd = lpfc_cmd->fcp_cmnd;
622         putLunHigh(fcp_cmnd->fcpLunMsl, 0);
623         putLunLow(fcp_cmnd->fcpLunLsl, lpfc_cmd->pCmd->device->lun)
624         fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
625
626         piocb->ulpIoTag =
627             lpfc_sli_next_iotag(phba, &phba->sli.ring[psli->fcp_ring]);
628         piocb->ulpCommand = CMD_FCP_ICMND64_CR;
629
630         piocb->ulpContext = ndlp->nlp_rpi;
631         if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
632                 piocb->ulpFCP2Rcvy = 1;
633         }
634         piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
635
636         /* ulpTimeout is only one byte */
637         if (lpfc_cmd->timeout > 0xff) {
638                 /*
639                  * Do not timeout the command at the firmware level.
640                  * The driver will provide the timeout mechanism.
641                  */
642                 piocb->ulpTimeout = 0;
643         } else {
644                 piocb->ulpTimeout = lpfc_cmd->timeout;
645         }
646
647         lpfc_cmd->target = target;
648
649         switch (task_mgmt_cmd) {
650         case FCP_LUN_RESET:
651                 /* Issue LUN Reset to TGT <num> LUN <num> */
652                 lpfc_printf_log(phba,
653                                 KERN_INFO,
654                                 LOG_FCP,
655                                 "%d:0703 Issue LUN Reset to TGT %d LUN %d "
656                                 "Data: x%x x%x\n",
657                                 phba->brd_no,
658                                 scsi_dev->id, scsi_dev->lun,
659                                 ndlp->nlp_rpi, ndlp->nlp_flag);
660
661                 break;
662         case FCP_ABORT_TASK_SET:
663                 /* Issue Abort Task Set to TGT <num> LUN <num> */
664                 lpfc_printf_log(phba,
665                                 KERN_INFO,
666                                 LOG_FCP,
667                                 "%d:0701 Issue Abort Task Set to TGT %d LUN %d "
668                                 "Data: x%x x%x\n",
669                                 phba->brd_no,
670                                 scsi_dev->id, scsi_dev->lun,
671                                 ndlp->nlp_rpi, ndlp->nlp_flag);
672
673                 break;
674         case FCP_TARGET_RESET:
675                 /* Issue Target Reset to TGT <num> */
676                 lpfc_printf_log(phba,
677                                 KERN_INFO,
678                                 LOG_FCP,
679                                 "%d:0702 Issue Target Reset to TGT %d "
680                                 "Data: x%x x%x\n",
681                                 phba->brd_no,
682                                 scsi_dev->id, ndlp->nlp_rpi,
683                                 ndlp->nlp_flag);
684                 break;
685         }
686
687         return (1);
688 }
689
690 static int
691 lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba)
692 {
693         struct lpfc_iocbq *piocbq, *piocbqrsp;
694         struct lpfc_sli *psli;
695         int ret;
696
697         ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
698         if (!ret)
699                 return FAILED;
700
701         lpfc_cmd->scsi_hba = phba;
702         psli = &phba->sli;
703         piocbq = &lpfc_cmd->cur_iocbq;
704         piocbqrsp = mempool_alloc(phba->iocb_mem_pool,
705                                   GFP_ATOMIC);
706         if (!piocbqrsp)
707                 return FAILED;
708
709         /* First flush all outstanding commands on the txq for the target */
710         lpfc_sli_abort_iocb_tgt(phba, &phba->sli.ring[phba->sli.fcp_ring],
711                                 lpfc_cmd->pCmd->device->id, LPFC_ABORT_TXQ);
712
713         memset(piocbqrsp, 0, sizeof (struct lpfc_iocbq));
714
715         piocbq->iocb_flag |= LPFC_IO_POLL;
716
717         ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
718                      &phba->sli.ring[psli->fcp_ring],
719                      piocbq, SLI_IOCB_HIGH_PRIORITY,
720                      piocbqrsp,
721                      lpfc_cmd->timeout);
722         if (ret != IOCB_SUCCESS) {
723                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
724         } else {
725                 lpfc_cmd->result = piocbqrsp->iocb.un.ulpWord[4];
726                 lpfc_cmd->status = piocbqrsp->iocb.ulpStatus;
727                 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
728                         (lpfc_cmd->result & IOERR_DRVR_MASK))
729                                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
730         }
731
732         /* At this point in time, target reset completion, all outstanding
733          * txcmplq I/Os should have been aborted by the target.
734          * Unfortunately, all targets do not abide by this so we need
735          * to help it out a bit.
736          */
737         lpfc_sli_abort_iocb_tgt(phba, &phba->sli.ring[phba->sli.fcp_ring],
738                                 lpfc_cmd->pCmd->device->id, LPFC_ABORT_ALLQ);
739
740         /* Done with piocbqrsp, return to free list. */
741         mempool_free(piocbqrsp, phba->iocb_mem_pool);
742         return ret;
743 }
744
745
746 int
747 lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
748 {
749         struct Scsi_Host *shost = cmnd->device->host;
750         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
751         struct lpfc_target *target;
752         int ret = FAILED, i, err_count = 0;
753         int cnt, loopcnt;
754         struct scsi_cmnd *lnx_cmd;
755         struct scsi_device *scsi_dev;
756         struct lpfc_scsi_buf * lpfc_cmd;
757
758         lpfc_cmd = lpfc_get_scsi_buf(phba, GFP_ATOMIC);
759         if (!lpfc_cmd)
760                 goto out;
761
762         lnx_cmd = kmalloc(sizeof(struct scsi_cmnd), GFP_ATOMIC);
763         if (!lnx_cmd)
764                 goto out_free_scsi_buf;
765
766         scsi_dev = kmalloc(sizeof(struct scsi_device), GFP_ATOMIC);
767         if (!scsi_dev)
768                 goto out_free_lnx_cmd;
769
770         /* The lpfc_cmd storage is reused.  Set all loop invariants. */
771         lpfc_cmd->timeout = cmnd->timeout;
772         if (!cmnd->timeout)
773                  lpfc_cmd->timeout = LPFC_DRVR_TIMEOUT;
774
775         lpfc_cmd->pCmd                  = lnx_cmd;
776         lpfc_cmd->scsi_hba              = phba;
777         lpfc_cmd->pCmd->device          = scsi_dev;
778         lpfc_cmd->pCmd->device->channel = 0;
779         lpfc_cmd->pCmd->device->lun     = 0;
780
781         /*
782          * Since the driver manages a single bus device, reset all
783          * targets known to the driver.  Should any target reset
784          * fail, this routine returns failure to the midlayer.
785          */
786         for (i = 0; i < MAX_FCP_TARGET; i++) {
787                 target = phba->device_queue_hash[i];
788                 if (!target)
789                         continue;
790
791                 lpfc_cmd->pCmd->device->id = i;
792                 lpfc_cmd->pCmd->device->hostdata = target;
793                 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
794                 if (ret != IOCB_SUCCESS) {
795                         lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
796                                 "%d:0713 Bus Reset on target %d failed\n",
797                                 phba->brd_no, i);
798                         err_count++;
799                 }
800         }
801
802         loopcnt = 0;
803         while((cnt = lpfc_sli_sum_iocb_host(phba,
804                                 &phba->sli.ring[phba->sli.fcp_ring]))) {
805                 spin_unlock_irq(phba->host->host_lock);
806 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,6)
807                 mdelay(50);
808 #else
809                 msleep(50);
810 #endif
811                 spin_lock_irq(phba->host->host_lock);
812
813                 /* 50ms * 100 = 5 sec
814                  * wait upto 5 seconds for all I/Os for this HBA to cmpl
815                  */
816                 if(++loopcnt >= 100)
817                         break;
818         }
819
820         if(cnt) {
821                 /* flush all outstanding commands on the host */
822                 i = lpfc_sli_abort_iocb_host(phba,
823                                 &phba->sli.ring[phba->sli.fcp_ring],
824                                 LPFC_ABORT_ALLQ);
825
826                 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
827                    "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
828                    phba->brd_no, cnt, i);
829         }
830         if (!err_count)
831                 ret = SUCCESS;
832
833 out_free_lnx_cmd:
834         kfree(lnx_cmd);
835 out_free_scsi_buf:
836         lpfc_free_scsi_buf(lpfc_cmd);
837         lpfc_printf_log(phba,
838                         KERN_ERR,
839                         LOG_FCP,
840                         "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
841                         phba->brd_no, ret);
842 out:
843         return ret;
844 }
845
846
847 int
848 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
849 {
850         struct lpfc_hba *phba =
851                 (struct lpfc_hba *) cmnd->device->host->hostdata[0];
852         struct lpfc_sli *psli = &phba->sli;
853         struct lpfc_target *targetp = cmnd->device->hostdata;
854         struct lpfc_nodelist *ndlp;
855         struct lpfc_iocbq *piocbq;
856         struct lpfc_scsi_buf *lpfc_cmd;
857         IOCB_t *piocb;
858         int err = 0;
859         uint16_t nlp_state;
860
861         targetp->qcmdcnt++;
862
863         /*
864          * The target pointer is guaranteed not to be NULL because the driver
865          * only clears the device->hostdata field in lpfc_slave_destroy.  This
866          * approach guarantees no further IO calls on this target.
867          */
868         ndlp =  targetp->pnode;
869         if (!ndlp) {
870                 if(phba->fc_flag & FC_UNLOADING) {
871                         /* Driver is in process of unloading */
872                         cmnd->result = ScsiResult(DID_BAD_TARGET, 0);
873                 }
874                 else {
875                         cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
876                 }
877                 goto out_fail_command;
878         }
879
880         nlp_state = ndlp->nlp_state;
881
882         /*
883          * A Fibre Channel is present and functioning only when the node state
884          * is MAPPED.  Any other state is a failure.
885          */
886         if (nlp_state != NLP_STE_MAPPED_NODE) {
887                 if ((nlp_state == NLP_STE_UNMAPPED_NODE) ||
888                     (nlp_state == NLP_STE_UNUSED_NODE)) {
889                         cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
890                         goto out_fail_command;
891                 }
892                 /*
893                  * The device is most likely recovered and the driver
894                  * needs a bit more time to finish.  Ask the midlayer
895                  * to retry.
896                  */
897                 goto out_host_busy;
898         }
899
900         lpfc_cmd = lpfc_get_scsi_buf(phba, GFP_ATOMIC);
901         if (!lpfc_cmd)
902                 goto out_host_busy;
903
904         /*
905          * Store the midlayer's command structure for the completion phase
906          * and complete the command initialization.
907          */
908         cmnd->scsi_done = done;
909         cmnd->host_scribble = (unsigned char *)lpfc_cmd;
910
911         lpfc_cmd->target = targetp;
912         lpfc_cmd->timeout = 0;
913         lpfc_cmd->pCmd = cmnd;
914         putLunHigh(lpfc_cmd->fcp_cmnd->fcpLunMsl, lpfc_cmd->pCmd->device->lun);
915         putLunLow(lpfc_cmd->fcp_cmnd->fcpLunLsl, lpfc_cmd->pCmd->device->lun);
916
917         err = lpfc_os_prep_io(phba, lpfc_cmd);
918         if (err)
919                 goto out_host_busy_free_buf;
920
921         piocbq = &(lpfc_cmd->cur_iocbq);
922         piocb = &piocbq->iocb;
923         piocb->ulpTimeout = lpfc_cmd->timeout;
924         piocbq->context1 = lpfc_cmd;
925         piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
926
927         piocbq->iocb.ulpContext = ndlp->nlp_rpi;
928         if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
929                 piocbq->iocb.ulpFCP2Rcvy = 1;
930         }
931
932         piocbq->iocb.ulpClass = (ndlp->nlp_fcp_info & 0x0f);
933
934         pci_dma_sync_single_for_device(phba->pcidev, lpfc_cmd->dma_ext.phys,
935                         LPFC_SCSI_DMA_EXT_SIZE, PCI_DMA_TODEVICE);
936
937         err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring], piocbq,
938                                  SLI_IOCB_RET_IOCB);
939         if (err)
940                 goto out_host_busy_free_buf;
941         return 0;
942
943  out_host_busy_free_buf:
944         lpfc_free_scsi_buf(lpfc_cmd);
945  out_host_busy:
946         targetp->iodonecnt++;
947         targetp->errorcnt++;
948         return SCSI_MLQUEUE_HOST_BUSY;
949
950  out_fail_command:
951         targetp->iodonecnt++;
952         targetp->errorcnt++;
953         done(cmnd);
954         return 0;
955 }
956
957 int
958 lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
959 {
960         struct Scsi_Host *shost = cmnd->device->host;
961         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
962         struct lpfc_sli *psli = &phba->sli;
963         struct lpfc_scsi_buf *lpfc_cmd;
964         struct lpfc_iocbq *piocbq, *piocbqrsp = NULL;
965         int ret = FAILED;
966         int cnt, loopcnt;
967
968         lpfc_cmd = lpfc_get_scsi_buf(phba, GFP_ATOMIC);
969         if (!lpfc_cmd)
970                 goto out;
971
972         lpfc_cmd->pCmd = cmnd;
973         lpfc_cmd->timeout = 60; /* set command timeout to 60 seconds */
974         lpfc_cmd->scsi_hba = phba;
975
976         ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
977         if (!ret)
978                 goto out_free_scsi_buf;
979
980         piocbq = &lpfc_cmd->cur_iocbq;
981
982         /* get a buffer for this IOCB command response */
983         piocbqrsp = mempool_alloc(phba->iocb_mem_pool, GFP_ATOMIC);
984         if(!piocbqrsp)
985                 goto out_free_scsi_buf;
986
987         /* First flush all outstanding commands on the txq for the lun */
988         lpfc_sli_abort_iocb_lun(phba,
989                                 &phba->sli.ring[phba->sli.fcp_ring],
990                                 cmnd->device->id,
991                                 cmnd->device->lun, LPFC_ABORT_TXQ);
992
993         memset(piocbqrsp, 0, sizeof (struct lpfc_iocbq));
994
995         piocbq->iocb_flag |= LPFC_IO_POLL;
996         piocbq->iocb_cmpl = lpfc_sli_wake_iocb_high_priority;
997
998         ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
999                      &phba->sli.ring[psli->fcp_ring],
1000                      piocbq, 0,
1001                      piocbqrsp, 60);
1002         if (ret == IOCB_SUCCESS)
1003                 ret = SUCCESS;
1004
1005         lpfc_cmd->result = piocbqrsp->iocb.un.ulpWord[4];
1006         lpfc_cmd->status = piocbqrsp->iocb.ulpStatus;
1007         if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT)
1008                 if (lpfc_cmd->result & IOERR_DRVR_MASK)
1009                         lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
1010
1011         /* At this point in time, lun reset completion, all outstanding
1012          * txcmplq I/Os should have been aborted by the target.
1013          * Unfortunately, all targets do not abide by this so we need
1014          * to help it out a bit.
1015          */
1016         lpfc_sli_abort_iocb_lun(phba,
1017                                 &phba->sli.ring[phba->sli.fcp_ring],
1018                                 cmnd->device->id,
1019                                 cmnd->device->lun, LPFC_ABORT_ALLQ);
1020
1021         loopcnt = 0;
1022         while((cnt = lpfc_sli_sum_iocb_lun(phba,
1023                                 &phba->sli.ring[phba->sli.fcp_ring],
1024                                 cmnd->device->id,
1025                                 cmnd->device->lun))) {
1026                 spin_unlock_irq(phba->host->host_lock);
1027 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,6)
1028                 mdelay(50);
1029 #else
1030                 msleep(50);
1031 #endif
1032                 spin_lock_irq(phba->host->host_lock);
1033
1034                 /* 50ms * 100 = 5 sec
1035                  * wait upto 5 seconds for all I/Os for this lun to cmpl
1036                  */
1037                 if(++loopcnt >= 100)
1038                         break;
1039         }
1040
1041         if(cnt) {
1042                 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1043                         "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1044                         phba->brd_no, cnt);
1045         }
1046
1047         mempool_free(piocbqrsp, phba->iocb_mem_pool);
1048
1049 out_free_scsi_buf:
1050         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1051                         "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1052                         "Data: x%x x%x x%x\n",
1053                         phba->brd_no, lpfc_cmd->pCmd->device->id,
1054                         lpfc_cmd->pCmd->device->lun, ret, lpfc_cmd->status,
1055                         lpfc_cmd->result);
1056         lpfc_free_scsi_buf(lpfc_cmd);
1057 out:
1058         return ret;
1059 }
1060
1061
1062 int
1063 lpfc_abort_handler(struct scsi_cmnd *cmnd)
1064 {
1065         struct lpfc_hba *phba =
1066                         (struct lpfc_hba *)cmnd->device->host->hostdata[0];
1067         struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
1068         struct lpfc_iocbq *iocb, *next_iocb, *abtsiocbp;
1069         struct lpfc_scsi_buf *lpfc_cmd;
1070         IOCB_t *cmd, *icmd;
1071         unsigned long snum;
1072         unsigned int id, lun;
1073         int ret = IOCB_ERROR;
1074
1075         /* Returns SUCESS if command aborted, else FAILED */
1076
1077         lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1078         if (!lpfc_cmd)
1079                 return(FAILED);
1080
1081         /* save these now since lpfc_cmd can be freed */
1082         id   = lpfc_cmd->pCmd->device->id;
1083         lun  = lpfc_cmd->pCmd->device->lun;
1084         snum = lpfc_cmd->pCmd->serial_number;
1085
1086         /* Search the txq first. */
1087         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
1088                 cmd = &iocb->iocb;
1089                 if (iocb->context1 != lpfc_cmd)
1090                         continue;
1091
1092                 list_del_init(&iocb->list);
1093                 pring->txq_cnt--;
1094                 if (!iocb->iocb_cmpl) {
1095                         mempool_free(iocb, phba->iocb_mem_pool);
1096                 }
1097                 else {
1098                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1099                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1100                         (iocb->iocb_cmpl)(phba, iocb, iocb);
1101                 }
1102                 ret = IOCB_SUCCESS;
1103                 goto out;
1104         }
1105
1106         abtsiocbp = mempool_alloc(phba->iocb_mem_pool, GFP_ATOMIC);
1107         if (!abtsiocbp)
1108                 goto out;
1109         memset(abtsiocbp, 0, sizeof (struct lpfc_iocbq));
1110
1111         /*
1112          * The scsi command was not in the txq.  Check the txcmplq and if it is
1113          * found, send an abort to the FW. 
1114          */
1115         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1116                 if (iocb->context1 != lpfc_cmd)
1117                         continue;
1118
1119                 iocb->iocb_cmpl = NULL;
1120                 cmd = &iocb->iocb;
1121                 icmd = &abtsiocbp->iocb;
1122                 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1123                 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1124                 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
1125
1126                 icmd->ulpLe = 1;
1127                 icmd->ulpClass = cmd->ulpClass;
1128                 icmd->ulpIoTag = lpfc_sli_next_iotag(phba, pring);
1129                 if (phba->hba_state >= LPFC_LINK_UP)
1130                         icmd->ulpCommand = CMD_ABORT_XRI_CN;
1131                 else
1132                         icmd->ulpCommand = CMD_CLOSE_XRI_CN;
1133
1134                 /* set up an iotag  */
1135                 icmd->ulpIoTag = lpfc_sli_next_iotag(phba, pring);
1136
1137                 if (lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0) ==
1138                                                                 IOCB_ERROR) {
1139                         mempool_free(abtsiocbp, phba->iocb_mem_pool);
1140                         break;
1141                 }
1142
1143                 /*
1144                  * The rsp ring completion will remove IOCB from txcmplq when
1145                  * abort is read by HBA.
1146                  */
1147                 ret = IOCB_SUCCESS;
1148                 break;
1149         }
1150
1151  out:
1152         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1153                         "%d:0749 SCSI layer issued abort device "
1154                         "Data: x%x x%x x%x x%lx\n",
1155                         phba->brd_no, ret, id, lun, snum);
1156
1157         return (ret == IOCB_SUCCESS ? SUCCESS : FAILED);
1158 }
1159
1160 #if defined(FC_TRANS_VER1) || defined(FC_TRANS_265_BLKPATCH)
1161 void
1162 lpfc_target_unblock(struct lpfc_hba *phba, struct lpfc_target *targetp)
1163 {
1164 #if defined(FC_TRANS_VER1)
1165         /*
1166          * This code to be removed once block/unblock and the new
1167          * dicovery state machine are fully debugged.
1168          */
1169         if (!targetp || !targetp->starget) {
1170 #else
1171         if (!targetp) {
1172 #endif
1173                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY | LOG_FCP,
1174                         "%d:0262 Cannot unblock scsi target\n", phba->brd_no);
1175
1176                 return;
1177         }
1178
1179         /* Unblock IO to target scsi id <sid> to NPort <nlp_DID> */
1180         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY | LOG_FCP,
1181                         "%d:0258 Unblocking IO to Target scsi id x%x  "
1182                         "NPort pointer x%p\n",
1183                         phba->brd_no, targetp->scsi_id, targetp->pnode);
1184
1185         spin_unlock_irq(phba->host->host_lock);
1186
1187 #if defined(FC_TRANS_VER1)
1188         fc_target_unblock(targetp->starget);
1189 #elif defined(FC_TRANS_265_BLKPATCH)
1190         fc_target_unblock(phba->host, targetp->scsi_id,
1191                           &targetp->dev_loss_timer);
1192 #endif
1193         spin_lock_irq(phba->host->host_lock);
1194         targetp->blocked--;
1195 }
1196
1197 void
1198 lpfc_target_block(struct lpfc_hba *phba, struct lpfc_target *targetp)
1199 {
1200 #if defined(FC_TRANS_VER1)
1201         /*
1202          * This code to be removed once block/unblock and the new
1203          * dicovery state machine are fully debugged.
1204          */
1205         if (!targetp || !targetp->starget) {
1206 #else
1207         if (!targetp) {
1208 #endif
1209                 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY | LOG_FCP,
1210                                 "%d:0263 Cannot block scsi target."
1211                                 " target ptr x%p\n",
1212                                 phba->brd_no, targetp);
1213                 return;
1214         }
1215
1216         /* Block all IO to target scsi id <sid> to NPort <nlp_DID> */
1217         lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY | LOG_FCP,
1218                         "%d:0259 Blocking IO to Target scsi id x%x"
1219                         " NPort pointer x%p\n",
1220                         phba->brd_no, targetp->scsi_id, targetp->pnode);
1221
1222         spin_unlock_irq(phba->host->host_lock);
1223 #if defined(FC_TRANS_VER1)
1224         fc_target_block(targetp->starget);
1225 #elif defined(FC_TRANS_265_BLKPATCH)
1226         fc_target_block(phba->host, targetp->scsi_id, &targetp->dev_loss_timer,
1227                         phba->cfg_nodev_tmo);
1228 #endif
1229         spin_lock_irq(phba->host->host_lock);
1230         targetp->blocked++;
1231 }
1232 #endif
1233
1234 #if defined(FC_TRANS_VER1) || defined(FC_TRANS_265_BLKPATCH)
1235 int
1236 lpfc_target_remove(struct lpfc_hba *phba, struct lpfc_target *targetp)
1237 {
1238         struct scsi_device *sdev;
1239         struct Scsi_Host   *shost = phba->host;
1240
1241         /* This is only called if scsi target (targetp->starget) is valid */
1242         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY | LOG_FCP,
1243                         "%d:0260 Remove Target scsi id x%x\n",
1244                         phba->brd_no, targetp->scsi_id);
1245
1246         /* If this target is blocked, we must unblock it first */
1247         if (targetp->blocked)
1248                 lpfc_target_unblock(phba, targetp);
1249
1250         /* Remove all associated devices for this target */
1251         if (phba->cfg_scsi_hotplug) {
1252 top:
1253                 list_for_each_entry(sdev, &shost->__devices, siblings) {
1254                         if (sdev->channel == 0
1255                             && sdev->id == targetp->scsi_id) {
1256                                 spin_unlock_irq(shost->host_lock);
1257                                 scsi_device_get(sdev);
1258                                 scsi_remove_device(sdev);
1259                                 scsi_device_put(sdev);
1260                                 spin_lock_irq(shost->host_lock);
1261                                 goto top;
1262                         }
1263                 }
1264         }
1265
1266         return 0;
1267 }
1268
1269 int
1270 lpfc_target_add(struct lpfc_hba *phba, struct lpfc_target *targetp)
1271 {
1272         struct Scsi_Host   *shost;
1273
1274         if(!phba->cfg_scsi_hotplug)
1275                 return 1;
1276
1277         /* This is only called if scsi target (targetp->starget) is valid */
1278
1279         lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY | LOG_FCP,
1280                         "%d:0261 Adding Target scsi id x%x\n",
1281                         phba->brd_no, targetp->scsi_id);
1282
1283         shost = phba->host;
1284
1285 #ifdef USE_SCAN_TARGET
1286         lpfc_discq_post_event(phba, targetp, NULL, LPFC_EVT_SCAN);
1287 #else
1288         /*
1289          * The driver discovered a new target.  Call the midlayer and get this
1290          * target's luns added into the device list.
1291          * Since we are going to scan the entire host, kick off a timer to 
1292          * do this so we can possibly consolidate multiple target scans into
1293          * one scsi host scan.
1294          */
1295         mod_timer(&phba->fc_scantmo, jiffies + HZ);
1296         phba->fc_flag |= FC_SCSI_SCAN_TMO;
1297 #endif
1298         return 0;
1299 }
1300 #endif