lpfc_scsi.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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_hba *phba,
  75. struct scsi_device *sdev)
  76. {
  77. unsigned long flags;
  78. atomic_inc(&phba->num_cmd_success);
  79. if (phba->cfg_lun_queue_depth <= sdev->queue_depth)
  80. return;
  81. spin_lock_irqsave(&phba->hbalock, flags);
  82. if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
  83. ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
  84. spin_unlock_irqrestore(&phba->hbalock, flags);
  85. return;
  86. }
  87. phba->last_ramp_up_time = jiffies;
  88. spin_unlock_irqrestore(&phba->hbalock, flags);
  89. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  90. if ((phba->pport->work_port_events &
  91. WORKER_RAMP_UP_QUEUE) == 0) {
  92. phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
  93. }
  94. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  95. spin_lock_irqsave(&phba->hbalock, flags);
  96. if (phba->work_wait)
  97. wake_up(phba->work_wait);
  98. spin_unlock_irqrestore(&phba->hbalock, flags);
  99. }
  100. void
  101. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  102. {
  103. struct lpfc_vport *vport;
  104. struct Scsi_Host *host;
  105. struct scsi_device *sdev;
  106. unsigned long new_queue_depth;
  107. unsigned long num_rsrc_err, num_cmd_success;
  108. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  109. num_cmd_success = atomic_read(&phba->num_cmd_success);
  110. spin_lock_irq(&phba->hbalock);
  111. list_for_each_entry(vport, &phba->port_list, listentry) {
  112. host = lpfc_shost_from_vport(vport);
  113. if (!scsi_host_get(host))
  114. continue;
  115. spin_unlock_irq(&phba->hbalock);
  116. shost_for_each_device(sdev, host) {
  117. new_queue_depth = sdev->queue_depth * num_rsrc_err /
  118. (num_rsrc_err + num_cmd_success);
  119. if (!new_queue_depth)
  120. new_queue_depth = sdev->queue_depth - 1;
  121. else
  122. new_queue_depth =
  123. sdev->queue_depth - new_queue_depth;
  124. if (sdev->ordered_tags)
  125. scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG,
  126. new_queue_depth);
  127. else
  128. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG,
  129. new_queue_depth);
  130. }
  131. spin_lock_irq(&phba->hbalock);
  132. scsi_host_put(host);
  133. }
  134. spin_unlock_irq(&phba->hbalock);
  135. atomic_set(&phba->num_rsrc_err, 0);
  136. atomic_set(&phba->num_cmd_success, 0);
  137. }
  138. void
  139. lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
  140. {
  141. struct lpfc_vport *vport;
  142. struct Scsi_Host *host;
  143. struct scsi_device *sdev;
  144. spin_lock_irq(&phba->hbalock);
  145. list_for_each_entry(vport, &phba->port_list, listentry) {
  146. host = lpfc_shost_from_vport(vport);
  147. if (!scsi_host_get(host))
  148. continue;
  149. spin_unlock_irq(&phba->hbalock);
  150. shost_for_each_device(sdev, host) {
  151. if (sdev->ordered_tags)
  152. scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG,
  153. sdev->queue_depth+1);
  154. else
  155. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG,
  156. sdev->queue_depth+1);
  157. }
  158. spin_lock_irq(&phba->hbalock);
  159. scsi_host_put(host);
  160. }
  161. spin_unlock_irq(&phba->hbalock);
  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. struct lpfc_hba *phba = vport->phba;
  365. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  366. uint32_t vpi = vport->vpi;
  367. uint32_t resp_info = fcprsp->rspStatus2;
  368. uint32_t scsi_status = fcprsp->rspStatus3;
  369. uint32_t *lp;
  370. uint32_t host_status = DID_OK;
  371. uint32_t rsplen = 0;
  372. uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
  373. /*
  374. * If this is a task management command, there is no
  375. * scsi packet associated with this lpfc_cmd. The driver
  376. * consumes it.
  377. */
  378. if (fcpcmd->fcpCntl2) {
  379. scsi_status = 0;
  380. goto out;
  381. }
  382. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  383. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  384. if (snslen > SCSI_SENSE_BUFFERSIZE)
  385. snslen = SCSI_SENSE_BUFFERSIZE;
  386. if (resp_info & RSP_LEN_VALID)
  387. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  388. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  389. }
  390. lp = (uint32_t *)cmnd->sense_buffer;
  391. if (!scsi_status && (resp_info & RESID_UNDER))
  392. logit = LOG_FCP;
  393. lpfc_printf_log(phba, KERN_WARNING, logit,
  394. "%d (%d):0730 FCP command x%x failed: x%x SNS x%x x%x "
  395. "Data: x%x x%x x%x x%x x%x\n",
  396. phba->brd_no, vpi, cmnd->cmnd[0], scsi_status,
  397. be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
  398. be32_to_cpu(fcprsp->rspResId),
  399. be32_to_cpu(fcprsp->rspSnsLen),
  400. be32_to_cpu(fcprsp->rspRspLen),
  401. fcprsp->rspInfo3);
  402. if (resp_info & RSP_LEN_VALID) {
  403. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  404. if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
  405. (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
  406. host_status = DID_ERROR;
  407. goto out;
  408. }
  409. }
  410. scsi_set_resid(cmnd, 0);
  411. if (resp_info & RESID_UNDER) {
  412. scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
  413. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  414. "%d (%d):0716 FCP Read Underrun, expected %d, "
  415. "residual %d Data: x%x x%x x%x\n",
  416. phba->brd_no, vpi, be32_to_cpu(fcpcmd->fcpDl),
  417. scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
  418. cmnd->underflow);
  419. /*
  420. * If there is an under run check if under run reported by
  421. * storage array is same as the under run reported by HBA.
  422. * If this is not same, there is a dropped frame.
  423. */
  424. if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  425. fcpi_parm &&
  426. (scsi_get_resid(cmnd) != fcpi_parm)) {
  427. lpfc_printf_log(phba, KERN_WARNING,
  428. LOG_FCP | LOG_FCP_ERROR,
  429. "%d (%d):0735 FCP Read Check Error "
  430. "and Underrun Data: x%x x%x x%x x%x\n",
  431. phba->brd_no, vpi,
  432. be32_to_cpu(fcpcmd->fcpDl),
  433. scsi_get_resid(cmnd), fcpi_parm,
  434. cmnd->cmnd[0]);
  435. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  436. host_status = DID_ERROR;
  437. }
  438. /*
  439. * The cmnd->underflow is the minimum number of bytes that must
  440. * be transfered for this command. Provided a sense condition
  441. * is not present, make sure the actual amount transferred is at
  442. * least the underflow value or fail.
  443. */
  444. if (!(resp_info & SNS_LEN_VALID) &&
  445. (scsi_status == SAM_STAT_GOOD) &&
  446. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
  447. < cmnd->underflow)) {
  448. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  449. "%d (%d):0717 FCP command x%x residual "
  450. "underrun converted to error "
  451. "Data: x%x x%x x%x\n",
  452. phba->brd_no, vpi, cmnd->cmnd[0],
  453. scsi_bufflen(cmnd),
  454. scsi_get_resid(cmnd), cmnd->underflow);
  455. host_status = DID_ERROR;
  456. }
  457. } else if (resp_info & RESID_OVER) {
  458. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  459. "%d (%d):0720 FCP command x%x residual "
  460. "overrun error. Data: x%x x%x \n",
  461. phba->brd_no, vpi, cmnd->cmnd[0],
  462. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  463. host_status = DID_ERROR;
  464. /*
  465. * Check SLI validation that all the transfer was actually done
  466. * (fcpi_parm should be zero). Apply check only to reads.
  467. */
  468. } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
  469. (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
  470. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
  471. "%d (%d):0734 FCP Read Check Error Data: "
  472. "x%x x%x x%x x%x\n",
  473. phba->brd_no, vpi,
  474. be32_to_cpu(fcpcmd->fcpDl),
  475. be32_to_cpu(fcprsp->rspResId),
  476. fcpi_parm, cmnd->cmnd[0]);
  477. host_status = DID_ERROR;
  478. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  479. }
  480. out:
  481. cmnd->result = ScsiResult(host_status, scsi_status);
  482. }
  483. static void
  484. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  485. struct lpfc_iocbq *pIocbOut)
  486. {
  487. struct lpfc_scsi_buf *lpfc_cmd =
  488. (struct lpfc_scsi_buf *) pIocbIn->context1;
  489. struct lpfc_vport *vport = pIocbIn->vport;
  490. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  491. struct lpfc_nodelist *pnode = rdata->pnode;
  492. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  493. uint32_t vpi = (lpfc_cmd->cur_iocbq.vport
  494. ? lpfc_cmd->cur_iocbq.vport->vpi
  495. : 0);
  496. int result;
  497. struct scsi_device *sdev, *tmp_sdev;
  498. int depth = 0;
  499. lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
  500. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  501. if (lpfc_cmd->status) {
  502. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  503. (lpfc_cmd->result & IOERR_DRVR_MASK))
  504. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  505. else if (lpfc_cmd->status >= IOSTAT_CNT)
  506. lpfc_cmd->status = IOSTAT_DEFAULT;
  507. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  508. "%d (%d):0729 FCP cmd x%x failed <%d/%d> "
  509. "status: x%x result: x%x Data: x%x x%x\n",
  510. phba->brd_no, vpi, cmd->cmnd[0],
  511. cmd->device ? cmd->device->id : 0xffff,
  512. cmd->device ? cmd->device->lun : 0xffff,
  513. lpfc_cmd->status, lpfc_cmd->result,
  514. pIocbOut->iocb.ulpContext,
  515. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  516. switch (lpfc_cmd->status) {
  517. case IOSTAT_FCP_RSP_ERROR:
  518. /* Call FCP RSP handler to determine result */
  519. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  520. break;
  521. case IOSTAT_NPORT_BSY:
  522. case IOSTAT_FABRIC_BSY:
  523. cmd->result = ScsiResult(DID_BUS_BUSY, 0);
  524. break;
  525. case IOSTAT_LOCAL_REJECT:
  526. if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
  527. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  528. lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
  529. cmd->result = ScsiResult(DID_REQUEUE, 0);
  530. break;
  531. } /* else: fall through */
  532. default:
  533. cmd->result = ScsiResult(DID_ERROR, 0);
  534. break;
  535. }
  536. if ((pnode == NULL )
  537. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  538. cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
  539. } else {
  540. cmd->result = ScsiResult(DID_OK, 0);
  541. }
  542. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  543. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  544. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  545. "%d (%d):0710 Iodone <%d/%d> cmd %p, error "
  546. "x%x SNS x%x x%x Data: x%x x%x\n",
  547. phba->brd_no, vpi, cmd->device->id,
  548. cmd->device->lun, cmd, cmd->result,
  549. *lp, *(lp + 3), cmd->retries,
  550. scsi_get_resid(cmd));
  551. }
  552. result = cmd->result;
  553. sdev = cmd->device;
  554. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  555. cmd->scsi_done(cmd);
  556. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  557. lpfc_release_scsi_buf(phba, lpfc_cmd);
  558. return;
  559. }
  560. if (!result)
  561. lpfc_rampup_queue_depth(phba, sdev);
  562. if (!result && pnode != NULL &&
  563. ((jiffies - pnode->last_ramp_up_time) >
  564. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  565. ((jiffies - pnode->last_q_full_time) >
  566. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  567. (phba->cfg_lun_queue_depth > sdev->queue_depth)) {
  568. shost_for_each_device(tmp_sdev, sdev->host) {
  569. if (phba->cfg_lun_queue_depth > tmp_sdev->queue_depth) {
  570. if (tmp_sdev->id != sdev->id)
  571. continue;
  572. if (tmp_sdev->ordered_tags)
  573. scsi_adjust_queue_depth(tmp_sdev,
  574. MSG_ORDERED_TAG,
  575. tmp_sdev->queue_depth+1);
  576. else
  577. scsi_adjust_queue_depth(tmp_sdev,
  578. MSG_SIMPLE_TAG,
  579. tmp_sdev->queue_depth+1);
  580. pnode->last_ramp_up_time = jiffies;
  581. }
  582. }
  583. }
  584. /*
  585. * Check for queue full. If the lun is reporting queue full, then
  586. * back off the lun queue depth to prevent target overloads.
  587. */
  588. if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
  589. pnode->last_q_full_time = jiffies;
  590. shost_for_each_device(tmp_sdev, sdev->host) {
  591. if (tmp_sdev->id != sdev->id)
  592. continue;
  593. depth = scsi_track_queue_full(tmp_sdev,
  594. tmp_sdev->queue_depth - 1);
  595. }
  596. /*
  597. * The queue depth cannot be lowered any more.
  598. * Modify the returned error code to store
  599. * the final depth value set by
  600. * scsi_track_queue_full.
  601. */
  602. if (depth == -1)
  603. depth = sdev->host->cmd_per_lun;
  604. if (depth) {
  605. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  606. "%d (%d):0711 detected queue full - "
  607. "lun queue depth adjusted to %d.\n",
  608. phba->brd_no, vpi, depth);
  609. }
  610. }
  611. lpfc_release_scsi_buf(phba, lpfc_cmd);
  612. }
  613. static void
  614. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  615. struct lpfc_nodelist *pnode)
  616. {
  617. struct lpfc_hba *phba = vport->phba;
  618. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  619. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  620. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  621. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  622. int datadir = scsi_cmnd->sc_data_direction;
  623. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  624. /* clear task management bits */
  625. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  626. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  627. &lpfc_cmd->fcp_cmnd->fcp_lun);
  628. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  629. if (scsi_cmnd->device->tagged_supported) {
  630. switch (scsi_cmnd->tag) {
  631. case HEAD_OF_QUEUE_TAG:
  632. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  633. break;
  634. case ORDERED_QUEUE_TAG:
  635. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  636. break;
  637. default:
  638. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  639. break;
  640. }
  641. } else
  642. fcp_cmnd->fcpCntl1 = 0;
  643. /*
  644. * There are three possibilities here - use scatter-gather segment, use
  645. * the single mapping, or neither. Start the lpfc command prep by
  646. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  647. * data bde entry.
  648. */
  649. if (scsi_sg_count(scsi_cmnd)) {
  650. if (datadir == DMA_TO_DEVICE) {
  651. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  652. iocb_cmd->un.fcpi.fcpi_parm = 0;
  653. iocb_cmd->ulpPU = 0;
  654. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  655. phba->fc4OutputRequests++;
  656. } else {
  657. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  658. iocb_cmd->ulpPU = PARM_READ_CHECK;
  659. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  660. fcp_cmnd->fcpCntl3 = READ_DATA;
  661. phba->fc4InputRequests++;
  662. }
  663. } else {
  664. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  665. iocb_cmd->un.fcpi.fcpi_parm = 0;
  666. iocb_cmd->ulpPU = 0;
  667. fcp_cmnd->fcpCntl3 = 0;
  668. phba->fc4ControlRequests++;
  669. }
  670. /*
  671. * Finish initializing those IOCB fields that are independent
  672. * of the scsi_cmnd request_buffer
  673. */
  674. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  675. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  676. piocbq->iocb.ulpFCP2Rcvy = 1;
  677. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  678. piocbq->context1 = lpfc_cmd;
  679. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  680. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  681. piocbq->vport = vport;
  682. }
  683. static int
  684. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  685. struct lpfc_scsi_buf *lpfc_cmd,
  686. unsigned int lun,
  687. uint8_t task_mgmt_cmd)
  688. {
  689. struct lpfc_iocbq *piocbq;
  690. IOCB_t *piocb;
  691. struct fcp_cmnd *fcp_cmnd;
  692. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  693. struct lpfc_nodelist *ndlp = rdata->pnode;
  694. if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
  695. return 0;
  696. }
  697. piocbq = &(lpfc_cmd->cur_iocbq);
  698. piocbq->vport = vport;
  699. piocb = &piocbq->iocb;
  700. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  701. int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
  702. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  703. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  704. piocb->ulpContext = ndlp->nlp_rpi;
  705. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  706. piocb->ulpFCP2Rcvy = 1;
  707. }
  708. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  709. /* ulpTimeout is only one byte */
  710. if (lpfc_cmd->timeout > 0xff) {
  711. /*
  712. * Do not timeout the command at the firmware level.
  713. * The driver will provide the timeout mechanism.
  714. */
  715. piocb->ulpTimeout = 0;
  716. } else {
  717. piocb->ulpTimeout = lpfc_cmd->timeout;
  718. }
  719. return 1;
  720. }
  721. static void
  722. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  723. struct lpfc_iocbq *cmdiocbq,
  724. struct lpfc_iocbq *rspiocbq)
  725. {
  726. struct lpfc_scsi_buf *lpfc_cmd =
  727. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  728. if (lpfc_cmd)
  729. lpfc_release_scsi_buf(phba, lpfc_cmd);
  730. return;
  731. }
  732. static int
  733. lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
  734. unsigned tgt_id, unsigned int lun,
  735. struct lpfc_rport_data *rdata)
  736. {
  737. struct lpfc_hba *phba = vport->phba;
  738. struct lpfc_iocbq *iocbq;
  739. struct lpfc_iocbq *iocbqrsp;
  740. int ret;
  741. if (!rdata->pnode)
  742. return FAILED;
  743. lpfc_cmd->rdata = rdata;
  744. ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
  745. FCP_TARGET_RESET);
  746. if (!ret)
  747. return FAILED;
  748. iocbq = &lpfc_cmd->cur_iocbq;
  749. iocbqrsp = lpfc_sli_get_iocbq(phba);
  750. if (!iocbqrsp)
  751. return FAILED;
  752. /* Issue Target Reset to TGT <num> */
  753. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  754. "%d (%d):0702 Issue Target Reset to TGT %d "
  755. "Data: x%x x%x\n",
  756. phba->brd_no, vport->vpi, tgt_id,
  757. rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
  758. ret = lpfc_sli_issue_iocb_wait(phba,
  759. &phba->sli.ring[phba->sli.fcp_ring],
  760. iocbq, iocbqrsp, lpfc_cmd->timeout);
  761. if (ret != IOCB_SUCCESS) {
  762. if (ret == IOCB_TIMEDOUT)
  763. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  764. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  765. } else {
  766. ret = SUCCESS;
  767. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  768. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  769. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  770. (lpfc_cmd->result & IOERR_DRVR_MASK))
  771. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  772. }
  773. lpfc_sli_release_iocbq(phba, iocbqrsp);
  774. return ret;
  775. }
  776. const char *
  777. lpfc_info(struct Scsi_Host *host)
  778. {
  779. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  780. struct lpfc_hba *phba = vport->phba;
  781. int len;
  782. static char lpfcinfobuf[384];
  783. memset(lpfcinfobuf,0,384);
  784. if (phba && phba->pcidev){
  785. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  786. len = strlen(lpfcinfobuf);
  787. snprintf(lpfcinfobuf + len,
  788. 384-len,
  789. " on PCI bus %02x device %02x irq %d",
  790. phba->pcidev->bus->number,
  791. phba->pcidev->devfn,
  792. phba->pcidev->irq);
  793. len = strlen(lpfcinfobuf);
  794. if (phba->Port[0]) {
  795. snprintf(lpfcinfobuf + len,
  796. 384-len,
  797. " port %s",
  798. phba->Port);
  799. }
  800. }
  801. return lpfcinfobuf;
  802. }
  803. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  804. {
  805. unsigned long poll_tmo_expires =
  806. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  807. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  808. mod_timer(&phba->fcp_poll_timer,
  809. poll_tmo_expires);
  810. }
  811. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  812. {
  813. lpfc_poll_rearm_timer(phba);
  814. }
  815. void lpfc_poll_timeout(unsigned long ptr)
  816. {
  817. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  818. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  819. lpfc_sli_poll_fcp_ring (phba);
  820. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  821. lpfc_poll_rearm_timer(phba);
  822. }
  823. }
  824. static int
  825. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  826. {
  827. struct Scsi_Host *shost = cmnd->device->host;
  828. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  829. struct lpfc_hba *phba = vport->phba;
  830. struct lpfc_sli *psli = &phba->sli;
  831. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  832. struct lpfc_nodelist *ndlp = rdata->pnode;
  833. struct lpfc_scsi_buf *lpfc_cmd;
  834. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  835. int err;
  836. err = fc_remote_port_chkready(rport);
  837. if (err) {
  838. cmnd->result = err;
  839. goto out_fail_command;
  840. }
  841. /*
  842. * Catch race where our node has transitioned, but the
  843. * transport is still transitioning.
  844. */
  845. if (!ndlp) {
  846. cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
  847. goto out_fail_command;
  848. }
  849. lpfc_cmd = lpfc_get_scsi_buf(phba);
  850. if (lpfc_cmd == NULL) {
  851. lpfc_adjust_queue_depth(phba);
  852. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  853. "%d (%d):0707 driver's buffer pool is empty, "
  854. "IO busied\n",
  855. phba->brd_no, vport->vpi);
  856. goto out_host_busy;
  857. }
  858. /*
  859. * Store the midlayer's command structure for the completion phase
  860. * and complete the command initialization.
  861. */
  862. lpfc_cmd->pCmd = cmnd;
  863. lpfc_cmd->rdata = rdata;
  864. lpfc_cmd->timeout = 0;
  865. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  866. cmnd->scsi_done = done;
  867. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  868. if (err)
  869. goto out_host_busy_free_buf;
  870. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  871. err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
  872. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  873. if (err)
  874. goto out_host_busy_free_buf;
  875. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  876. lpfc_sli_poll_fcp_ring(phba);
  877. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  878. lpfc_poll_rearm_timer(phba);
  879. }
  880. return 0;
  881. out_host_busy_free_buf:
  882. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  883. lpfc_release_scsi_buf(phba, lpfc_cmd);
  884. out_host_busy:
  885. return SCSI_MLQUEUE_HOST_BUSY;
  886. out_fail_command:
  887. done(cmnd);
  888. return 0;
  889. }
  890. static void
  891. lpfc_block_error_handler(struct scsi_cmnd *cmnd)
  892. {
  893. struct Scsi_Host *shost = cmnd->device->host;
  894. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  895. spin_lock_irq(shost->host_lock);
  896. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  897. spin_unlock_irq(shost->host_lock);
  898. msleep(1000);
  899. spin_lock_irq(shost->host_lock);
  900. }
  901. spin_unlock_irq(shost->host_lock);
  902. return;
  903. }
  904. static int
  905. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  906. {
  907. struct Scsi_Host *shost = cmnd->device->host;
  908. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  909. struct lpfc_hba *phba = vport->phba;
  910. struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
  911. struct lpfc_iocbq *iocb;
  912. struct lpfc_iocbq *abtsiocb;
  913. struct lpfc_scsi_buf *lpfc_cmd;
  914. IOCB_t *cmd, *icmd;
  915. unsigned int loop_count = 0;
  916. int ret = SUCCESS;
  917. lpfc_block_error_handler(cmnd);
  918. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  919. BUG_ON(!lpfc_cmd);
  920. /*
  921. * If pCmd field of the corresponding lpfc_scsi_buf structure
  922. * points to a different SCSI command, then the driver has
  923. * already completed this command, but the midlayer did not
  924. * see the completion before the eh fired. Just return
  925. * SUCCESS.
  926. */
  927. iocb = &lpfc_cmd->cur_iocbq;
  928. if (lpfc_cmd->pCmd != cmnd)
  929. goto out;
  930. BUG_ON(iocb->context1 != lpfc_cmd);
  931. abtsiocb = lpfc_sli_get_iocbq(phba);
  932. if (abtsiocb == NULL) {
  933. ret = FAILED;
  934. goto out;
  935. }
  936. /*
  937. * The scsi command can not be in txq and it is in flight because the
  938. * pCmd is still pointig at the SCSI command we have to abort. There
  939. * is no need to search the txcmplq. Just send an abort to the FW.
  940. */
  941. cmd = &iocb->iocb;
  942. icmd = &abtsiocb->iocb;
  943. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  944. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  945. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  946. icmd->ulpLe = 1;
  947. icmd->ulpClass = cmd->ulpClass;
  948. if (lpfc_is_link_up(phba))
  949. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  950. else
  951. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  952. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  953. abtsiocb->vport = vport;
  954. if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
  955. lpfc_sli_release_iocbq(phba, abtsiocb);
  956. ret = FAILED;
  957. goto out;
  958. }
  959. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  960. lpfc_sli_poll_fcp_ring (phba);
  961. /* Wait for abort to complete */
  962. while (lpfc_cmd->pCmd == cmnd)
  963. {
  964. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  965. lpfc_sli_poll_fcp_ring (phba);
  966. schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
  967. if (++loop_count
  968. > (2 * phba->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
  969. break;
  970. }
  971. if (lpfc_cmd->pCmd == cmnd) {
  972. ret = FAILED;
  973. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  974. "%d (%d):0748 abort handler timed out waiting "
  975. "for abort to complete: ret %#x, ID %d, "
  976. "LUN %d, snum %#lx\n",
  977. phba->brd_no, vport->vpi, ret,
  978. cmnd->device->id, cmnd->device->lun,
  979. cmnd->serial_number);
  980. }
  981. out:
  982. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  983. "%d (%d):0749 SCSI Layer I/O Abort Request "
  984. "Status x%x ID %d LUN %d snum %#lx\n",
  985. phba->brd_no, vport->vpi, ret, cmnd->device->id,
  986. cmnd->device->lun, cmnd->serial_number);
  987. return ret;
  988. }
  989. static int
  990. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  991. {
  992. struct Scsi_Host *shost = cmnd->device->host;
  993. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  994. struct lpfc_hba *phba = vport->phba;
  995. struct lpfc_scsi_buf *lpfc_cmd;
  996. struct lpfc_iocbq *iocbq, *iocbqrsp;
  997. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  998. struct lpfc_nodelist *pnode = rdata->pnode;
  999. uint32_t cmd_result = 0, cmd_status = 0;
  1000. int ret = FAILED;
  1001. int iocb_status = IOCB_SUCCESS;
  1002. int cnt, loopcnt;
  1003. lpfc_block_error_handler(cmnd);
  1004. loopcnt = 0;
  1005. /*
  1006. * If target is not in a MAPPED state, delay the reset until
  1007. * target is rediscovered or devloss timeout expires.
  1008. */
  1009. while (1) {
  1010. if (!pnode)
  1011. goto out;
  1012. if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
  1013. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  1014. loopcnt++;
  1015. rdata = cmnd->device->hostdata;
  1016. if (!rdata ||
  1017. (loopcnt > ((phba->cfg_devloss_tmo * 2) + 1))) {
  1018. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1019. "%d (%d):0721 LUN Reset rport "
  1020. "failure: cnt x%x rdata x%p\n",
  1021. phba->brd_no, vport->vpi,
  1022. loopcnt, rdata);
  1023. goto out;
  1024. }
  1025. pnode = rdata->pnode;
  1026. if (!pnode)
  1027. goto out;
  1028. }
  1029. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  1030. break;
  1031. }
  1032. lpfc_cmd = lpfc_get_scsi_buf(phba);
  1033. if (lpfc_cmd == NULL)
  1034. goto out;
  1035. lpfc_cmd->timeout = 60;
  1036. lpfc_cmd->rdata = rdata;
  1037. ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
  1038. FCP_TARGET_RESET);
  1039. if (!ret)
  1040. goto out_free_scsi_buf;
  1041. iocbq = &lpfc_cmd->cur_iocbq;
  1042. /* get a buffer for this IOCB command response */
  1043. iocbqrsp = lpfc_sli_get_iocbq(phba);
  1044. if (iocbqrsp == NULL)
  1045. goto out_free_scsi_buf;
  1046. lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
  1047. "%d (%d):0703 Issue target reset to TGT %d LUN %d "
  1048. "rpi x%x nlp_flag x%x\n",
  1049. phba->brd_no, vport->vpi, cmnd->device->id,
  1050. cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
  1051. iocb_status = lpfc_sli_issue_iocb_wait(phba,
  1052. &phba->sli.ring[phba->sli.fcp_ring],
  1053. iocbq, iocbqrsp, lpfc_cmd->timeout);
  1054. if (iocb_status == IOCB_TIMEDOUT)
  1055. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  1056. if (iocb_status == IOCB_SUCCESS)
  1057. ret = SUCCESS;
  1058. else
  1059. ret = iocb_status;
  1060. cmd_result = iocbqrsp->iocb.un.ulpWord[4];
  1061. cmd_status = iocbqrsp->iocb.ulpStatus;
  1062. lpfc_sli_release_iocbq(phba, iocbqrsp);
  1063. /*
  1064. * All outstanding txcmplq I/Os should have been aborted by the device.
  1065. * Unfortunately, some targets do not abide by this forcing the driver
  1066. * to double check.
  1067. */
  1068. cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
  1069. cmnd->device->id, cmnd->device->lun,
  1070. LPFC_CTX_LUN);
  1071. if (cnt)
  1072. lpfc_sli_abort_iocb(phba,
  1073. &phba->sli.ring[phba->sli.fcp_ring],
  1074. cmnd->device->id, cmnd->device->lun,
  1075. 0, LPFC_CTX_LUN);
  1076. loopcnt = 0;
  1077. while(cnt) {
  1078. schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
  1079. if (++loopcnt
  1080. > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
  1081. break;
  1082. cnt = lpfc_sli_sum_iocb(phba,
  1083. &phba->sli.ring[phba->sli.fcp_ring],
  1084. cmnd->device->id, cmnd->device->lun,
  1085. LPFC_CTX_LUN);
  1086. }
  1087. if (cnt) {
  1088. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1089. "%d (%d):0719 device reset I/O flush failure: "
  1090. "cnt x%x\n",
  1091. phba->brd_no, vport->vpi, cnt);
  1092. ret = FAILED;
  1093. }
  1094. out_free_scsi_buf:
  1095. if (iocb_status != IOCB_TIMEDOUT) {
  1096. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1097. }
  1098. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1099. "%d (%d):0713 SCSI layer issued device reset (%d, %d) "
  1100. "return x%x status x%x result x%x\n",
  1101. phba->brd_no, vport->vpi, cmnd->device->id,
  1102. cmnd->device->lun, ret, cmd_status, cmd_result);
  1103. out:
  1104. return ret;
  1105. }
  1106. static int
  1107. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  1108. {
  1109. struct Scsi_Host *shost = cmnd->device->host;
  1110. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1111. struct lpfc_hba *phba = vport->phba;
  1112. struct lpfc_nodelist *ndlp = NULL;
  1113. int match;
  1114. int ret = FAILED, i, err_count = 0;
  1115. int cnt, loopcnt;
  1116. struct lpfc_scsi_buf * lpfc_cmd;
  1117. lpfc_block_error_handler(cmnd);
  1118. lpfc_cmd = lpfc_get_scsi_buf(phba);
  1119. if (lpfc_cmd == NULL)
  1120. goto out;
  1121. /* The lpfc_cmd storage is reused. Set all loop invariants. */
  1122. lpfc_cmd->timeout = 60;
  1123. /*
  1124. * Since the driver manages a single bus device, reset all
  1125. * targets known to the driver. Should any target reset
  1126. * fail, this routine returns failure to the midlayer.
  1127. */
  1128. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  1129. /* Search for mapped node by target ID */
  1130. match = 0;
  1131. spin_lock_irq(shost->host_lock);
  1132. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  1133. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  1134. i == ndlp->nlp_sid &&
  1135. ndlp->rport) {
  1136. match = 1;
  1137. break;
  1138. }
  1139. }
  1140. spin_unlock_irq(shost->host_lock);
  1141. if (!match)
  1142. continue;
  1143. ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
  1144. cmnd->device->lun,
  1145. ndlp->rport->dd_data);
  1146. if (ret != SUCCESS) {
  1147. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1148. "%d (%d):0700 Bus Reset on target %d "
  1149. "failed\n",
  1150. phba->brd_no, vport->vpi, i);
  1151. err_count++;
  1152. break;
  1153. }
  1154. }
  1155. if (ret != IOCB_TIMEDOUT)
  1156. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1157. if (err_count == 0)
  1158. ret = SUCCESS;
  1159. else
  1160. ret = FAILED;
  1161. /*
  1162. * All outstanding txcmplq I/Os should have been aborted by
  1163. * the targets. Unfortunately, some targets do not abide by
  1164. * this forcing the driver to double check.
  1165. */
  1166. cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
  1167. 0, 0, LPFC_CTX_HOST);
  1168. if (cnt)
  1169. lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
  1170. 0, 0, 0, LPFC_CTX_HOST);
  1171. loopcnt = 0;
  1172. while(cnt) {
  1173. schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
  1174. if (++loopcnt
  1175. > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
  1176. break;
  1177. cnt = lpfc_sli_sum_iocb(phba,
  1178. &phba->sli.ring[phba->sli.fcp_ring],
  1179. 0, 0, LPFC_CTX_HOST);
  1180. }
  1181. if (cnt) {
  1182. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1183. "%d (%d):0715 Bus Reset I/O flush failure: "
  1184. "cnt x%x left x%x\n",
  1185. phba->brd_no, vport->vpi, cnt, i);
  1186. ret = FAILED;
  1187. }
  1188. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1189. "%d (%d):0714 SCSI layer issued Bus Reset Data: x%x\n",
  1190. phba->brd_no, vport->vpi, ret);
  1191. out:
  1192. return ret;
  1193. }
  1194. static int
  1195. lpfc_slave_alloc(struct scsi_device *sdev)
  1196. {
  1197. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  1198. struct lpfc_hba *phba = vport->phba;
  1199. struct lpfc_scsi_buf *scsi_buf = NULL;
  1200. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1201. uint32_t total = 0, i;
  1202. uint32_t num_to_alloc = 0;
  1203. unsigned long flags;
  1204. if (!rport || fc_remote_port_chkready(rport))
  1205. return -ENXIO;
  1206. sdev->hostdata = rport->dd_data;
  1207. /*
  1208. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  1209. * available list of scsi buffers. Don't allocate more than the
  1210. * HBA limit conveyed to the midlayer via the host structure. The
  1211. * formula accounts for the lun_queue_depth + error handlers + 1
  1212. * extra. This list of scsi bufs exists for the lifetime of the driver.
  1213. */
  1214. total = phba->total_scsi_bufs;
  1215. num_to_alloc = phba->cfg_lun_queue_depth + 2;
  1216. /* Allow some exchanges to be available always to complete discovery */
  1217. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  1218. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  1219. "%d (%d):0704 At limitation of %d "
  1220. "preallocated command buffers\n",
  1221. phba->brd_no, vport->vpi, total);
  1222. return 0;
  1223. /* Allow some exchanges to be available always to complete discovery */
  1224. } else if (total + num_to_alloc >
  1225. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  1226. lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
  1227. "%d (%d):0705 Allocation request of %d "
  1228. "command buffers will exceed max of %d. "
  1229. "Reducing allocation request to %d.\n",
  1230. phba->brd_no, vport->vpi, num_to_alloc,
  1231. phba->cfg_hba_queue_depth,
  1232. (phba->cfg_hba_queue_depth - total));
  1233. num_to_alloc = phba->cfg_hba_queue_depth - total;
  1234. }
  1235. for (i = 0; i < num_to_alloc; i++) {
  1236. scsi_buf = lpfc_new_scsi_buf(vport);
  1237. if (!scsi_buf) {
  1238. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1239. "%d (%d):0706 Failed to allocate "
  1240. "command buffer\n",
  1241. phba->brd_no, vport->vpi);
  1242. break;
  1243. }
  1244. spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
  1245. phba->total_scsi_bufs++;
  1246. list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
  1247. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
  1248. }
  1249. return 0;
  1250. }
  1251. static int
  1252. lpfc_slave_configure(struct scsi_device *sdev)
  1253. {
  1254. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  1255. struct lpfc_hba *phba = vport->phba;
  1256. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  1257. if (sdev->tagged_supported)
  1258. scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
  1259. else
  1260. scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
  1261. /*
  1262. * Initialize the fc transport attributes for the target
  1263. * containing this scsi device. Also note that the driver's
  1264. * target pointer is stored in the starget_data for the
  1265. * driver's sysfs entry point functions.
  1266. */
  1267. rport->dev_loss_tmo = phba->cfg_devloss_tmo;
  1268. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1269. lpfc_sli_poll_fcp_ring(phba);
  1270. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  1271. lpfc_poll_rearm_timer(phba);
  1272. }
  1273. return 0;
  1274. }
  1275. static void
  1276. lpfc_slave_destroy(struct scsi_device *sdev)
  1277. {
  1278. sdev->hostdata = NULL;
  1279. return;
  1280. }
  1281. struct scsi_host_template lpfc_template = {
  1282. .module = THIS_MODULE,
  1283. .name = LPFC_DRIVER_NAME,
  1284. .info = lpfc_info,
  1285. .queuecommand = lpfc_queuecommand,
  1286. .eh_abort_handler = lpfc_abort_handler,
  1287. .eh_device_reset_handler= lpfc_device_reset_handler,
  1288. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  1289. .slave_alloc = lpfc_slave_alloc,
  1290. .slave_configure = lpfc_slave_configure,
  1291. .slave_destroy = lpfc_slave_destroy,
  1292. .scan_finished = lpfc_scan_finished,
  1293. .this_id = -1,
  1294. .sg_tablesize = LPFC_SG_SEG_CNT,
  1295. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1296. .use_clustering = ENABLE_CLUSTERING,
  1297. .shost_attrs = lpfc_hba_attrs,
  1298. .max_sectors = 0xFFFF,
  1299. };