lpfc_scsi.c 36 KB

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