lpfc_scsi.c 36 KB

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