lpfc_scsi.c 36 KB

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