lpfc_scsi.c 42 KB

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