lpfc_scsi.c 37 KB

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