lpfc_scsi.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2005 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  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. *
  20. *******************************************************************/
  21. #include <linux/pci.h>
  22. #include <linux/interrupt.h>
  23. #include <scsi/scsi.h>
  24. #include <scsi/scsi_device.h>
  25. #include <scsi/scsi_host.h>
  26. #include <scsi/scsi_tcq.h>
  27. #include <scsi/scsi_transport_fc.h>
  28. #include "lpfc_version.h"
  29. #include "lpfc_hw.h"
  30. #include "lpfc_sli.h"
  31. #include "lpfc_disc.h"
  32. #include "lpfc_scsi.h"
  33. #include "lpfc.h"
  34. #include "lpfc_logmsg.h"
  35. #include "lpfc_crtn.h"
  36. #define LPFC_RESET_WAIT 2
  37. #define LPFC_ABORT_WAIT 2
  38. /*
  39. * This routine allocates a scsi buffer, which contains all the necessary
  40. * information needed to initiate a SCSI I/O. The non-DMAable buffer region
  41. * contains information to build the IOCB. The DMAable region contains
  42. * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
  43. * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
  44. * and the BPL BDE is setup in the IOCB.
  45. */
  46. static struct lpfc_scsi_buf *
  47. lpfc_get_scsi_buf(struct lpfc_hba * phba)
  48. {
  49. struct lpfc_scsi_buf *psb;
  50. struct ulp_bde64 *bpl;
  51. IOCB_t *iocb;
  52. dma_addr_t pdma_phys;
  53. psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  54. if (!psb)
  55. return NULL;
  56. memset(psb, 0, sizeof (struct lpfc_scsi_buf));
  57. psb->scsi_hba = phba;
  58. /*
  59. * Get memory from the pci pool to map the virt space to pci bus space
  60. * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
  61. * struct fcp_rsp and the number of bde's necessary to support the
  62. * sg_tablesize.
  63. */
  64. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
  65. &psb->dma_handle);
  66. if (!psb->data) {
  67. kfree(psb);
  68. return NULL;
  69. }
  70. /* Initialize virtual ptrs to dma_buf region. */
  71. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  72. psb->fcp_cmnd = psb->data;
  73. psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
  74. psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
  75. sizeof(struct fcp_rsp);
  76. /* Initialize local short-hand pointers. */
  77. bpl = psb->fcp_bpl;
  78. pdma_phys = psb->dma_handle;
  79. /*
  80. * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
  81. * list bdes. Initialize the first two and leave the rest for
  82. * queuecommand.
  83. */
  84. bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
  85. bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
  86. bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
  87. bpl->tus.f.bdeFlags = BUFF_USE_CMND;
  88. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  89. bpl++;
  90. /* Setup the physical region for the FCP RSP */
  91. pdma_phys += sizeof (struct fcp_cmnd);
  92. bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
  93. bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
  94. bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
  95. bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
  96. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  97. /*
  98. * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
  99. * initialize it with all known data now.
  100. */
  101. pdma_phys += (sizeof (struct fcp_rsp));
  102. iocb = &psb->cur_iocbq.iocb;
  103. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  104. iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
  105. iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
  106. iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
  107. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
  108. iocb->ulpBdeCount = 1;
  109. iocb->ulpClass = CLASS3;
  110. return psb;
  111. }
  112. static void
  113. lpfc_free_scsi_buf(struct lpfc_scsi_buf * psb)
  114. {
  115. struct lpfc_hba *phba = psb->scsi_hba;
  116. /*
  117. * There are only two special cases to consider. (1) the scsi command
  118. * requested scatter-gather usage or (2) the scsi command allocated
  119. * a request buffer, but did not request use_sg. There is a third
  120. * case, but it does not require resource deallocation.
  121. */
  122. if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
  123. dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
  124. psb->seg_cnt, psb->pCmd->sc_data_direction);
  125. } else {
  126. if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
  127. dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
  128. psb->pCmd->request_bufflen,
  129. psb->pCmd->sc_data_direction);
  130. }
  131. }
  132. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  133. }
  134. static int
  135. lpfc_scsi_prep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
  136. {
  137. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  138. struct scatterlist *sgel = NULL;
  139. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  140. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  141. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  142. dma_addr_t physaddr;
  143. uint32_t i, num_bde = 0;
  144. int datadir = scsi_cmnd->sc_data_direction;
  145. int dma_error;
  146. /*
  147. * There are three possibilities here - use scatter-gather segment, use
  148. * the single mapping, or neither. Start the lpfc command prep by
  149. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  150. * data bde entry.
  151. */
  152. bpl += 2;
  153. if (scsi_cmnd->use_sg) {
  154. /*
  155. * The driver stores the segment count returned from pci_map_sg
  156. * because this a count of dma-mappings used to map the use_sg
  157. * pages. They are not guaranteed to be the same for those
  158. * architectures that implement an IOMMU.
  159. */
  160. sgel = (struct scatterlist *)scsi_cmnd->request_buffer;
  161. lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
  162. scsi_cmnd->use_sg, datadir);
  163. if (lpfc_cmd->seg_cnt == 0)
  164. return 1;
  165. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  166. printk(KERN_ERR "%s: Too many sg segments from "
  167. "dma_map_sg. Config %d, seg_cnt %d",
  168. __FUNCTION__, phba->cfg_sg_seg_cnt,
  169. lpfc_cmd->seg_cnt);
  170. dma_unmap_sg(&phba->pcidev->dev, sgel,
  171. lpfc_cmd->seg_cnt, datadir);
  172. return 1;
  173. }
  174. /*
  175. * The driver established a maximum scatter-gather segment count
  176. * during probe that limits the number of sg elements in any
  177. * single scsi command. Just run through the seg_cnt and format
  178. * the bde's.
  179. */
  180. for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
  181. physaddr = sg_dma_address(sgel);
  182. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  183. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  184. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  185. if (datadir == DMA_TO_DEVICE)
  186. bpl->tus.f.bdeFlags = 0;
  187. else
  188. bpl->tus.f.bdeFlags = BUFF_USE_RCV;
  189. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  190. bpl++;
  191. sgel++;
  192. num_bde++;
  193. }
  194. } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
  195. physaddr = dma_map_single(&phba->pcidev->dev,
  196. scsi_cmnd->request_buffer,
  197. scsi_cmnd->request_bufflen,
  198. datadir);
  199. dma_error = dma_mapping_error(physaddr);
  200. if (dma_error) {
  201. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  202. "%d:0718 Unable to dma_map_single "
  203. "request_buffer: x%x\n",
  204. phba->brd_no, dma_error);
  205. return 1;
  206. }
  207. lpfc_cmd->nonsg_phys = physaddr;
  208. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  209. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  210. bpl->tus.f.bdeSize = scsi_cmnd->request_bufflen;
  211. if (datadir == DMA_TO_DEVICE)
  212. bpl->tus.f.bdeFlags = 0;
  213. else
  214. bpl->tus.f.bdeFlags = BUFF_USE_RCV;
  215. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  216. num_bde = 1;
  217. bpl++;
  218. }
  219. /*
  220. * Finish initializing those IOCB fields that are dependent on the
  221. * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
  222. * reinitialized since all iocb memory resources are used many times
  223. * for transmit, receive, and continuation bpl's.
  224. */
  225. iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
  226. iocb_cmd->un.fcpi64.bdl.bdeSize +=
  227. (num_bde * sizeof (struct ulp_bde64));
  228. iocb_cmd->ulpBdeCount = 1;
  229. iocb_cmd->ulpLe = 1;
  230. fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
  231. return 0;
  232. }
  233. static void
  234. lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
  235. {
  236. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  237. struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
  238. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  239. struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
  240. uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
  241. uint32_t resp_info = fcprsp->rspStatus2;
  242. uint32_t scsi_status = fcprsp->rspStatus3;
  243. uint32_t host_status = DID_OK;
  244. uint32_t rsplen = 0;
  245. /*
  246. * If this is a task management command, there is no
  247. * scsi packet associated with this lpfc_cmd. The driver
  248. * consumes it.
  249. */
  250. if (fcpcmd->fcpCntl2) {
  251. scsi_status = 0;
  252. goto out;
  253. }
  254. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  255. "%d:0730 FCP command failed: RSP "
  256. "Data: x%x x%x x%x x%x x%x x%x\n",
  257. phba->brd_no, resp_info, scsi_status,
  258. be32_to_cpu(fcprsp->rspResId),
  259. be32_to_cpu(fcprsp->rspSnsLen),
  260. be32_to_cpu(fcprsp->rspRspLen),
  261. fcprsp->rspInfo3);
  262. if (resp_info & RSP_LEN_VALID) {
  263. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  264. if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
  265. (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
  266. host_status = DID_ERROR;
  267. goto out;
  268. }
  269. }
  270. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  271. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  272. if (snslen > SCSI_SENSE_BUFFERSIZE)
  273. snslen = SCSI_SENSE_BUFFERSIZE;
  274. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  275. }
  276. cmnd->resid = 0;
  277. if (resp_info & RESID_UNDER) {
  278. cmnd->resid = be32_to_cpu(fcprsp->rspResId);
  279. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  280. "%d:0716 FCP Read Underrun, expected %d, "
  281. "residual %d Data: x%x x%x x%x\n", phba->brd_no,
  282. be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
  283. fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
  284. /*
  285. * The cmnd->underflow is the minimum number of bytes that must
  286. * be transfered for this command. Provided a sense condition
  287. * is not present, make sure the actual amount transferred is at
  288. * least the underflow value or fail.
  289. */
  290. if (!(resp_info & SNS_LEN_VALID) &&
  291. (scsi_status == SAM_STAT_GOOD) &&
  292. (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
  293. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  294. "%d:0717 FCP command x%x residual "
  295. "underrun converted to error "
  296. "Data: x%x x%x x%x\n", phba->brd_no,
  297. cmnd->cmnd[0], cmnd->request_bufflen,
  298. cmnd->resid, cmnd->underflow);
  299. host_status = DID_ERROR;
  300. }
  301. } else if (resp_info & RESID_OVER) {
  302. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  303. "%d:0720 FCP command x%x residual "
  304. "overrun error. Data: x%x x%x \n",
  305. phba->brd_no, cmnd->cmnd[0],
  306. cmnd->request_bufflen, cmnd->resid);
  307. host_status = DID_ERROR;
  308. /*
  309. * Check SLI validation that all the transfer was actually done
  310. * (fcpi_parm should be zero). Apply check only to reads.
  311. */
  312. } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
  313. (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
  314. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  315. "%d:0734 FCP Read Check Error Data: "
  316. "x%x x%x x%x x%x\n", phba->brd_no,
  317. be32_to_cpu(fcpcmd->fcpDl),
  318. be32_to_cpu(fcprsp->rspResId),
  319. fcpi_parm, cmnd->cmnd[0]);
  320. host_status = DID_ERROR;
  321. cmnd->resid = cmnd->request_bufflen;
  322. }
  323. out:
  324. cmnd->result = ScsiResult(host_status, scsi_status);
  325. }
  326. static void
  327. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  328. struct lpfc_iocbq *pIocbOut)
  329. {
  330. struct lpfc_scsi_buf *lpfc_cmd =
  331. (struct lpfc_scsi_buf *) pIocbIn->context1;
  332. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  333. struct lpfc_nodelist *pnode = rdata->pnode;
  334. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  335. unsigned long iflag;
  336. lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
  337. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  338. if (lpfc_cmd->status) {
  339. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  340. (lpfc_cmd->result & IOERR_DRVR_MASK))
  341. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  342. else if (lpfc_cmd->status >= IOSTAT_CNT)
  343. lpfc_cmd->status = IOSTAT_DEFAULT;
  344. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  345. "%d:0729 FCP cmd x%x failed <%d/%d> status: "
  346. "x%x result: x%x Data: x%x x%x\n",
  347. phba->brd_no, cmd->cmnd[0], cmd->device->id,
  348. cmd->device->lun, lpfc_cmd->status,
  349. lpfc_cmd->result, pIocbOut->iocb.ulpContext,
  350. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  351. switch (lpfc_cmd->status) {
  352. case IOSTAT_FCP_RSP_ERROR:
  353. /* Call FCP RSP handler to determine result */
  354. lpfc_handle_fcp_err(lpfc_cmd);
  355. break;
  356. case IOSTAT_NPORT_BSY:
  357. case IOSTAT_FABRIC_BSY:
  358. cmd->result = ScsiResult(DID_BUS_BUSY, 0);
  359. break;
  360. default:
  361. cmd->result = ScsiResult(DID_ERROR, 0);
  362. break;
  363. }
  364. if ((pnode == NULL )
  365. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  366. cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
  367. } else {
  368. cmd->result = ScsiResult(DID_OK, 0);
  369. }
  370. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  371. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  372. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  373. "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
  374. "SNS x%x x%x Data: x%x x%x\n",
  375. phba->brd_no, cmd->device->id,
  376. cmd->device->lun, cmd, cmd->result,
  377. *lp, *(lp + 3), cmd->retries, cmd->resid);
  378. }
  379. spin_lock_irqsave(phba->host->host_lock, iflag);
  380. lpfc_free_scsi_buf(lpfc_cmd);
  381. cmd->host_scribble = NULL;
  382. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  383. cmd->scsi_done(cmd);
  384. }
  385. static void
  386. lpfc_scsi_prep_cmnd(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd,
  387. struct lpfc_nodelist *pnode)
  388. {
  389. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  390. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  391. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  392. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  393. int datadir = scsi_cmnd->sc_data_direction;
  394. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  395. /* clear task management bits */
  396. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  397. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  398. &lpfc_cmd->fcp_cmnd->fcp_lun);
  399. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  400. if (scsi_cmnd->device->tagged_supported) {
  401. switch (scsi_cmnd->tag) {
  402. case HEAD_OF_QUEUE_TAG:
  403. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  404. break;
  405. case ORDERED_QUEUE_TAG:
  406. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  407. break;
  408. default:
  409. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  410. break;
  411. }
  412. } else
  413. fcp_cmnd->fcpCntl1 = 0;
  414. /*
  415. * There are three possibilities here - use scatter-gather segment, use
  416. * the single mapping, or neither. Start the lpfc command prep by
  417. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  418. * data bde entry.
  419. */
  420. if (scsi_cmnd->use_sg) {
  421. if (datadir == DMA_TO_DEVICE) {
  422. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  423. iocb_cmd->un.fcpi.fcpi_parm = 0;
  424. iocb_cmd->ulpPU = 0;
  425. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  426. phba->fc4OutputRequests++;
  427. } else {
  428. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  429. iocb_cmd->ulpPU = PARM_READ_CHECK;
  430. iocb_cmd->un.fcpi.fcpi_parm =
  431. scsi_cmnd->request_bufflen;
  432. fcp_cmnd->fcpCntl3 = READ_DATA;
  433. phba->fc4InputRequests++;
  434. }
  435. } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
  436. if (datadir == DMA_TO_DEVICE) {
  437. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  438. iocb_cmd->un.fcpi.fcpi_parm = 0;
  439. iocb_cmd->ulpPU = 0;
  440. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  441. phba->fc4OutputRequests++;
  442. } else {
  443. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  444. iocb_cmd->ulpPU = PARM_READ_CHECK;
  445. iocb_cmd->un.fcpi.fcpi_parm =
  446. scsi_cmnd->request_bufflen;
  447. fcp_cmnd->fcpCntl3 = READ_DATA;
  448. phba->fc4InputRequests++;
  449. }
  450. } else {
  451. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  452. iocb_cmd->un.fcpi.fcpi_parm = 0;
  453. iocb_cmd->ulpPU = 0;
  454. fcp_cmnd->fcpCntl3 = 0;
  455. phba->fc4ControlRequests++;
  456. }
  457. /*
  458. * Finish initializing those IOCB fields that are independent
  459. * of the scsi_cmnd request_buffer
  460. */
  461. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  462. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  463. piocbq->iocb.ulpFCP2Rcvy = 1;
  464. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  465. piocbq->context1 = lpfc_cmd;
  466. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  467. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  468. }
  469. static int
  470. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
  471. struct lpfc_scsi_buf *lpfc_cmd,
  472. uint8_t task_mgmt_cmd)
  473. {
  474. struct lpfc_sli *psli;
  475. struct lpfc_iocbq *piocbq;
  476. IOCB_t *piocb;
  477. struct fcp_cmnd *fcp_cmnd;
  478. struct scsi_device *scsi_dev = lpfc_cmd->pCmd->device;
  479. struct lpfc_rport_data *rdata = scsi_dev->hostdata;
  480. struct lpfc_nodelist *ndlp = rdata->pnode;
  481. if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
  482. return 0;
  483. }
  484. psli = &phba->sli;
  485. piocbq = &(lpfc_cmd->cur_iocbq);
  486. piocb = &piocbq->iocb;
  487. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  488. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  489. &lpfc_cmd->fcp_cmnd->fcp_lun);
  490. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  491. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  492. piocb->ulpContext = ndlp->nlp_rpi;
  493. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  494. piocb->ulpFCP2Rcvy = 1;
  495. }
  496. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  497. /* ulpTimeout is only one byte */
  498. if (lpfc_cmd->timeout > 0xff) {
  499. /*
  500. * Do not timeout the command at the firmware level.
  501. * The driver will provide the timeout mechanism.
  502. */
  503. piocb->ulpTimeout = 0;
  504. } else {
  505. piocb->ulpTimeout = lpfc_cmd->timeout;
  506. }
  507. lpfc_cmd->rdata = rdata;
  508. switch (task_mgmt_cmd) {
  509. case FCP_LUN_RESET:
  510. /* Issue LUN Reset to TGT <num> LUN <num> */
  511. lpfc_printf_log(phba,
  512. KERN_INFO,
  513. LOG_FCP,
  514. "%d:0703 Issue LUN Reset to TGT %d LUN %d "
  515. "Data: x%x x%x\n",
  516. phba->brd_no,
  517. scsi_dev->id, scsi_dev->lun,
  518. ndlp->nlp_rpi, ndlp->nlp_flag);
  519. break;
  520. case FCP_ABORT_TASK_SET:
  521. /* Issue Abort Task Set to TGT <num> LUN <num> */
  522. lpfc_printf_log(phba,
  523. KERN_INFO,
  524. LOG_FCP,
  525. "%d:0701 Issue Abort Task Set to TGT %d LUN %d "
  526. "Data: x%x x%x\n",
  527. phba->brd_no,
  528. scsi_dev->id, scsi_dev->lun,
  529. ndlp->nlp_rpi, ndlp->nlp_flag);
  530. break;
  531. case FCP_TARGET_RESET:
  532. /* Issue Target Reset to TGT <num> */
  533. lpfc_printf_log(phba,
  534. KERN_INFO,
  535. LOG_FCP,
  536. "%d:0702 Issue Target Reset to TGT %d "
  537. "Data: x%x x%x\n",
  538. phba->brd_no,
  539. scsi_dev->id, ndlp->nlp_rpi,
  540. ndlp->nlp_flag);
  541. break;
  542. }
  543. return (1);
  544. }
  545. static int
  546. lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba)
  547. {
  548. struct lpfc_iocbq *iocbq;
  549. struct lpfc_iocbq *iocbqrsp = NULL;
  550. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  551. int ret;
  552. ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
  553. if (!ret)
  554. return FAILED;
  555. lpfc_cmd->scsi_hba = phba;
  556. iocbq = &lpfc_cmd->cur_iocbq;
  557. list_remove_head(lpfc_iocb_list, iocbqrsp, struct lpfc_iocbq, list);
  558. if (!iocbqrsp)
  559. return FAILED;
  560. memset(iocbqrsp, 0, sizeof (struct lpfc_iocbq));
  561. iocbq->iocb_flag |= LPFC_IO_POLL;
  562. ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
  563. &phba->sli.ring[phba->sli.fcp_ring],
  564. iocbq, SLI_IOCB_HIGH_PRIORITY,
  565. iocbqrsp,
  566. lpfc_cmd->timeout);
  567. if (ret != IOCB_SUCCESS) {
  568. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  569. ret = FAILED;
  570. } else {
  571. ret = SUCCESS;
  572. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  573. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  574. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  575. (lpfc_cmd->result & IOERR_DRVR_MASK))
  576. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  577. }
  578. /*
  579. * All outstanding txcmplq I/Os should have been aborted by the target.
  580. * Unfortunately, some targets do not abide by this forcing the driver
  581. * to double check.
  582. */
  583. lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
  584. lpfc_cmd->pCmd->device->id,
  585. lpfc_cmd->pCmd->device->lun, 0, LPFC_CTX_TGT);
  586. /* Return response IOCB to free list. */
  587. list_add_tail(&iocbqrsp->list, lpfc_iocb_list);
  588. return ret;
  589. }
  590. static void
  591. lpfc_scsi_cmd_iocb_cleanup (struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  592. struct lpfc_iocbq *pIocbOut)
  593. {
  594. unsigned long iflag;
  595. struct lpfc_scsi_buf *lpfc_cmd =
  596. (struct lpfc_scsi_buf *) pIocbIn->context1;
  597. spin_lock_irqsave(phba->host->host_lock, iflag);
  598. lpfc_free_scsi_buf(lpfc_cmd);
  599. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  600. }
  601. static void
  602. lpfc_scsi_cmd_iocb_cmpl_aborted(struct lpfc_hba *phba,
  603. struct lpfc_iocbq *pIocbIn,
  604. struct lpfc_iocbq *pIocbOut)
  605. {
  606. struct scsi_cmnd *ml_cmd =
  607. ((struct lpfc_scsi_buf *) pIocbIn->context1)->pCmd;
  608. lpfc_scsi_cmd_iocb_cleanup (phba, pIocbIn, pIocbOut);
  609. ml_cmd->host_scribble = NULL;
  610. }
  611. const char *
  612. lpfc_info(struct Scsi_Host *host)
  613. {
  614. struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata[0];
  615. int len;
  616. static char lpfcinfobuf[384];
  617. memset(lpfcinfobuf,0,384);
  618. if (phba && phba->pcidev){
  619. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  620. len = strlen(lpfcinfobuf);
  621. snprintf(lpfcinfobuf + len,
  622. 384-len,
  623. " on PCI bus %02x device %02x irq %d",
  624. phba->pcidev->bus->number,
  625. phba->pcidev->devfn,
  626. phba->pcidev->irq);
  627. len = strlen(lpfcinfobuf);
  628. if (phba->Port[0]) {
  629. snprintf(lpfcinfobuf + len,
  630. 384-len,
  631. " port %s",
  632. phba->Port);
  633. }
  634. }
  635. return lpfcinfobuf;
  636. }
  637. static int
  638. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  639. {
  640. struct lpfc_hba *phba =
  641. (struct lpfc_hba *) cmnd->device->host->hostdata[0];
  642. struct lpfc_sli *psli = &phba->sli;
  643. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  644. struct lpfc_nodelist *ndlp = rdata->pnode;
  645. struct lpfc_scsi_buf *lpfc_cmd = NULL;
  646. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  647. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  648. int err;
  649. err = fc_remote_port_chkready(rport);
  650. if (err) {
  651. cmnd->result = err;
  652. goto out_fail_command;
  653. }
  654. /*
  655. * Catch race where our node has transitioned, but the
  656. * transport is still transitioning.
  657. */
  658. if (!ndlp) {
  659. cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
  660. goto out_fail_command;
  661. }
  662. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  663. if (lpfc_cmd == NULL) {
  664. printk(KERN_WARNING "%s: No buffer available - list empty, "
  665. "total count %d\n", __FUNCTION__, phba->total_scsi_bufs);
  666. goto out_host_busy;
  667. }
  668. /*
  669. * Store the midlayer's command structure for the completion phase
  670. * and complete the command initialization.
  671. */
  672. lpfc_cmd->pCmd = cmnd;
  673. lpfc_cmd->rdata = rdata;
  674. lpfc_cmd->timeout = 0;
  675. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  676. cmnd->scsi_done = done;
  677. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  678. if (err)
  679. goto out_host_busy_free_buf;
  680. lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
  681. err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
  682. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  683. if (err)
  684. goto out_host_busy_free_buf;
  685. return 0;
  686. out_host_busy_free_buf:
  687. lpfc_free_scsi_buf(lpfc_cmd);
  688. cmnd->host_scribble = NULL;
  689. out_host_busy:
  690. return SCSI_MLQUEUE_HOST_BUSY;
  691. out_fail_command:
  692. done(cmnd);
  693. return 0;
  694. }
  695. static int
  696. __lpfc_abort_handler(struct scsi_cmnd *cmnd)
  697. {
  698. struct lpfc_hba *phba =
  699. (struct lpfc_hba *)cmnd->device->host->hostdata[0];
  700. struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
  701. struct lpfc_iocbq *iocb, *next_iocb;
  702. struct lpfc_iocbq *abtsiocb = NULL;
  703. struct lpfc_scsi_buf *lpfc_cmd;
  704. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  705. IOCB_t *cmd, *icmd;
  706. unsigned long snum;
  707. unsigned int id, lun;
  708. unsigned int loop_count = 0;
  709. int ret = IOCB_SUCCESS;
  710. /*
  711. * If the host_scribble data area is NULL, then the driver has already
  712. * completed this command, but the midlayer did not see the completion
  713. * before the eh fired. Just return SUCCESS.
  714. */
  715. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  716. if (!lpfc_cmd)
  717. return SUCCESS;
  718. /* save these now since lpfc_cmd can be freed */
  719. id = lpfc_cmd->pCmd->device->id;
  720. lun = lpfc_cmd->pCmd->device->lun;
  721. snum = lpfc_cmd->pCmd->serial_number;
  722. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  723. cmd = &iocb->iocb;
  724. if (iocb->context1 != lpfc_cmd)
  725. continue;
  726. list_del_init(&iocb->list);
  727. pring->txq_cnt--;
  728. if (!iocb->iocb_cmpl) {
  729. list_add_tail(&iocb->list, lpfc_iocb_list);
  730. }
  731. else {
  732. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  733. cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  734. lpfc_scsi_cmd_iocb_cmpl_aborted(phba, iocb, iocb);
  735. }
  736. goto out;
  737. }
  738. list_remove_head(lpfc_iocb_list, abtsiocb, struct lpfc_iocbq, list);
  739. if (abtsiocb == NULL)
  740. return FAILED;
  741. memset(abtsiocb, 0, sizeof (struct lpfc_iocbq));
  742. /*
  743. * The scsi command was not in the txq. Check the txcmplq and if it is
  744. * found, send an abort to the FW.
  745. */
  746. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
  747. if (iocb->context1 != lpfc_cmd)
  748. continue;
  749. iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl_aborted;
  750. cmd = &iocb->iocb;
  751. icmd = &abtsiocb->iocb;
  752. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  753. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  754. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  755. icmd->ulpLe = 1;
  756. icmd->ulpClass = cmd->ulpClass;
  757. if (phba->hba_state >= LPFC_LINK_UP)
  758. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  759. else
  760. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  761. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  762. if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) ==
  763. IOCB_ERROR) {
  764. list_add_tail(&abtsiocb->list, lpfc_iocb_list);
  765. ret = IOCB_ERROR;
  766. break;
  767. }
  768. /* Wait for abort to complete */
  769. while (cmnd->host_scribble)
  770. {
  771. spin_unlock_irq(phba->host->host_lock);
  772. set_current_state(TASK_UNINTERRUPTIBLE);
  773. schedule_timeout(LPFC_ABORT_WAIT*HZ);
  774. spin_lock_irq(phba->host->host_lock);
  775. if (++loop_count
  776. > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
  777. break;
  778. }
  779. if(cmnd->host_scribble) {
  780. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  781. "%d:0748 abort handler timed "
  782. "out waiting for abort to "
  783. "complete. Data: "
  784. "x%x x%x x%x x%lx\n",
  785. phba->brd_no, ret, id, lun, snum);
  786. cmnd->host_scribble = NULL;
  787. iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cleanup;
  788. ret = IOCB_ERROR;
  789. }
  790. break;
  791. }
  792. out:
  793. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  794. "%d:0749 SCSI layer issued abort device "
  795. "Data: x%x x%x x%x x%lx\n",
  796. phba->brd_no, ret, id, lun, snum);
  797. return ret == IOCB_SUCCESS ? SUCCESS : FAILED;
  798. }
  799. static int
  800. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  801. {
  802. int rc;
  803. spin_lock_irq(cmnd->device->host->host_lock);
  804. rc = __lpfc_abort_handler(cmnd);
  805. spin_unlock_irq(cmnd->device->host->host_lock);
  806. return rc;
  807. }
  808. static int
  809. __lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
  810. {
  811. struct Scsi_Host *shost = cmnd->device->host;
  812. struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
  813. struct lpfc_sli *psli = &phba->sli;
  814. struct lpfc_scsi_buf *lpfc_cmd = NULL;
  815. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  816. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  817. struct lpfc_iocbq *iocbq, *iocbqrsp = NULL;
  818. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  819. struct lpfc_nodelist *pnode = rdata->pnode;
  820. int ret = FAILED;
  821. int cnt, loopcnt;
  822. /*
  823. * If target is not in a MAPPED state, delay the reset until
  824. * target is rediscovered or nodev timeout expires.
  825. */
  826. while ( 1 ) {
  827. if (!pnode)
  828. break;
  829. if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
  830. spin_unlock_irq(phba->host->host_lock);
  831. set_current_state(TASK_UNINTERRUPTIBLE);
  832. schedule_timeout( HZ/2);
  833. spin_lock_irq(phba->host->host_lock);
  834. }
  835. if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
  836. break;
  837. }
  838. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  839. if (lpfc_cmd == NULL)
  840. goto out;
  841. lpfc_cmd->pCmd = cmnd;
  842. lpfc_cmd->timeout = 60;
  843. lpfc_cmd->scsi_hba = phba;
  844. ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
  845. if (!ret)
  846. goto out_free_scsi_buf;
  847. iocbq = &lpfc_cmd->cur_iocbq;
  848. /* get a buffer for this IOCB command response */
  849. list_remove_head(lpfc_iocb_list, iocbqrsp, struct lpfc_iocbq, list);
  850. if (iocbqrsp == NULL)
  851. goto out_free_scsi_buf;
  852. memset(iocbqrsp, 0, sizeof (struct lpfc_iocbq));
  853. iocbq->iocb_flag |= LPFC_IO_POLL;
  854. iocbq->iocb_cmpl = lpfc_sli_wake_iocb_high_priority;
  855. ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
  856. &phba->sli.ring[psli->fcp_ring],
  857. iocbq, 0, iocbqrsp, 60);
  858. if (ret == IOCB_SUCCESS)
  859. ret = SUCCESS;
  860. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  861. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  862. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT)
  863. if (lpfc_cmd->result & IOERR_DRVR_MASK)
  864. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  865. /*
  866. * All outstanding txcmplq I/Os should have been aborted by the target.
  867. * Unfortunately, some targets do not abide by this forcing the driver
  868. * to double check.
  869. */
  870. lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
  871. cmnd->device->id, cmnd->device->lun, 0,
  872. LPFC_CTX_LUN);
  873. loopcnt = 0;
  874. while((cnt = lpfc_sli_sum_iocb(phba,
  875. &phba->sli.ring[phba->sli.fcp_ring],
  876. cmnd->device->id, cmnd->device->lun,
  877. LPFC_CTX_LUN))) {
  878. spin_unlock_irq(phba->host->host_lock);
  879. set_current_state(TASK_UNINTERRUPTIBLE);
  880. schedule_timeout(LPFC_RESET_WAIT*HZ);
  881. spin_lock_irq(phba->host->host_lock);
  882. if (++loopcnt
  883. > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
  884. break;
  885. }
  886. if (cnt) {
  887. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  888. "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
  889. phba->brd_no, cnt);
  890. }
  891. list_add_tail(&iocbqrsp->list, lpfc_iocb_list);
  892. out_free_scsi_buf:
  893. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  894. "%d:0713 SCSI layer issued LUN reset (%d, %d) "
  895. "Data: x%x x%x x%x\n",
  896. phba->brd_no, lpfc_cmd->pCmd->device->id,
  897. lpfc_cmd->pCmd->device->lun, ret, lpfc_cmd->status,
  898. lpfc_cmd->result);
  899. lpfc_free_scsi_buf(lpfc_cmd);
  900. out:
  901. return ret;
  902. }
  903. static int
  904. lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
  905. {
  906. int rc;
  907. spin_lock_irq(cmnd->device->host->host_lock);
  908. rc = __lpfc_reset_lun_handler(cmnd);
  909. spin_unlock_irq(cmnd->device->host->host_lock);
  910. return rc;
  911. }
  912. /*
  913. * Note: midlayer calls this function with the host_lock held
  914. */
  915. static int
  916. __lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
  917. {
  918. struct Scsi_Host *shost = cmnd->device->host;
  919. struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
  920. struct lpfc_nodelist *ndlp = NULL;
  921. int match;
  922. int ret = FAILED, i, err_count = 0;
  923. int cnt, loopcnt;
  924. unsigned int midlayer_id = 0;
  925. struct lpfc_scsi_buf * lpfc_cmd = NULL;
  926. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  927. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  928. if (lpfc_cmd == NULL)
  929. goto out;
  930. /* The lpfc_cmd storage is reused. Set all loop invariants. */
  931. lpfc_cmd->timeout = 60;
  932. lpfc_cmd->pCmd = cmnd;
  933. lpfc_cmd->scsi_hba = phba;
  934. /*
  935. * Since the driver manages a single bus device, reset all
  936. * targets known to the driver. Should any target reset
  937. * fail, this routine returns failure to the midlayer.
  938. */
  939. midlayer_id = cmnd->device->id;
  940. for (i = 0; i < MAX_FCP_TARGET; i++) {
  941. /* Search the mapped list for this target ID */
  942. match = 0;
  943. list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
  944. if ((i == ndlp->nlp_sid) && ndlp->rport) {
  945. match = 1;
  946. break;
  947. }
  948. }
  949. if (!match)
  950. continue;
  951. lpfc_cmd->pCmd->device->id = i;
  952. lpfc_cmd->pCmd->device->hostdata = ndlp->rport->dd_data;
  953. ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
  954. if (ret != SUCCESS) {
  955. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  956. "%d:0713 Bus Reset on target %d failed\n",
  957. phba->brd_no, i);
  958. err_count++;
  959. }
  960. }
  961. cmnd->device->id = midlayer_id;
  962. loopcnt = 0;
  963. while((cnt = lpfc_sli_sum_iocb(phba,
  964. &phba->sli.ring[phba->sli.fcp_ring],
  965. 0, 0, LPFC_CTX_HOST))) {
  966. spin_unlock_irq(phba->host->host_lock);
  967. set_current_state(TASK_UNINTERRUPTIBLE);
  968. schedule_timeout(LPFC_RESET_WAIT*HZ);
  969. spin_lock_irq(phba->host->host_lock);
  970. if (++loopcnt
  971. > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
  972. break;
  973. }
  974. if (cnt) {
  975. /* flush all outstanding commands on the host */
  976. i = lpfc_sli_abort_iocb(phba,
  977. &phba->sli.ring[phba->sli.fcp_ring], 0, 0, 0,
  978. LPFC_CTX_HOST);
  979. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  980. "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
  981. phba->brd_no, cnt, i);
  982. }
  983. if (!err_count)
  984. ret = SUCCESS;
  985. lpfc_free_scsi_buf(lpfc_cmd);
  986. lpfc_printf_log(phba,
  987. KERN_ERR,
  988. LOG_FCP,
  989. "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
  990. phba->brd_no, ret);
  991. out:
  992. return ret;
  993. }
  994. static int
  995. lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
  996. {
  997. int rc;
  998. spin_lock_irq(cmnd->device->host->host_lock);
  999. rc = __lpfc_reset_bus_handler(cmnd);
  1000. spin_unlock_irq(cmnd->device->host->host_lock);
  1001. return rc;
  1002. }
  1003. static int
  1004. lpfc_slave_alloc(struct scsi_device *sdev)
  1005. {
  1006. struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata[0];
  1007. struct lpfc_scsi_buf *scsi_buf = NULL;
  1008. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1009. uint32_t total = 0, i;
  1010. uint32_t num_to_alloc = 0;
  1011. unsigned long flags;
  1012. if (!rport || fc_remote_port_chkready(rport))
  1013. return -ENXIO;
  1014. sdev->hostdata = rport->dd_data;
  1015. /*
  1016. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  1017. * available list of scsi buffers. Don't allocate more than the
  1018. * HBA limit conveyed to the midlayer via the host structure. Note
  1019. * that this list of scsi bufs exists for the lifetime of the driver.
  1020. */
  1021. total = phba->total_scsi_bufs;
  1022. num_to_alloc = LPFC_CMD_PER_LUN;
  1023. if (total >= phba->cfg_hba_queue_depth) {
  1024. printk(KERN_WARNING "%s, At config limitation of "
  1025. "%d allocated scsi_bufs\n", __FUNCTION__, total);
  1026. return 0;
  1027. } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
  1028. num_to_alloc = phba->cfg_hba_queue_depth - total;
  1029. }
  1030. for (i = 0; i < num_to_alloc; i++) {
  1031. scsi_buf = lpfc_get_scsi_buf(phba);
  1032. if (!scsi_buf) {
  1033. printk(KERN_ERR "%s, failed to allocate "
  1034. "scsi_buf\n", __FUNCTION__);
  1035. break;
  1036. }
  1037. spin_lock_irqsave(phba->host->host_lock, flags);
  1038. phba->total_scsi_bufs++;
  1039. list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
  1040. spin_unlock_irqrestore(phba->host->host_lock, flags);
  1041. }
  1042. return 0;
  1043. }
  1044. static int
  1045. lpfc_slave_configure(struct scsi_device *sdev)
  1046. {
  1047. struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata[0];
  1048. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  1049. if (sdev->tagged_supported)
  1050. scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
  1051. else
  1052. scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
  1053. /*
  1054. * Initialize the fc transport attributes for the target
  1055. * containing this scsi device. Also note that the driver's
  1056. * target pointer is stored in the starget_data for the
  1057. * driver's sysfs entry point functions.
  1058. */
  1059. rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
  1060. return 0;
  1061. }
  1062. static void
  1063. lpfc_slave_destroy(struct scsi_device *sdev)
  1064. {
  1065. sdev->hostdata = NULL;
  1066. return;
  1067. }
  1068. struct scsi_host_template lpfc_template = {
  1069. .module = THIS_MODULE,
  1070. .name = LPFC_DRIVER_NAME,
  1071. .info = lpfc_info,
  1072. .queuecommand = lpfc_queuecommand,
  1073. .eh_abort_handler = lpfc_abort_handler,
  1074. .eh_device_reset_handler= lpfc_reset_lun_handler,
  1075. .eh_bus_reset_handler = lpfc_reset_bus_handler,
  1076. .slave_alloc = lpfc_slave_alloc,
  1077. .slave_configure = lpfc_slave_configure,
  1078. .slave_destroy = lpfc_slave_destroy,
  1079. .this_id = -1,
  1080. .sg_tablesize = LPFC_SG_SEG_CNT,
  1081. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1082. .use_clustering = ENABLE_CLUSTERING,
  1083. .shost_attrs = lpfc_host_attrs,
  1084. .max_sectors = 0xFFFF,
  1085. };