lpfc_scsi.c 41 KB

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