lpfc_scsi.c 37 KB

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