lpfc_scsi.c 36 KB

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