lpfc_scsi.c 39 KB

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