lpfc_scsi.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2008 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_nl.h"
  33. #include "lpfc_disc.h"
  34. #include "lpfc_scsi.h"
  35. #include "lpfc.h"
  36. #include "lpfc_logmsg.h"
  37. #include "lpfc_crtn.h"
  38. #include "lpfc_vport.h"
  39. #define LPFC_RESET_WAIT 2
  40. #define LPFC_ABORT_WAIT 2
  41. /**
  42. * lpfc_update_stats: Update statistical data for the command completion.
  43. * @phba: Pointer to HBA object.
  44. * @lpfc_cmd: lpfc scsi command object pointer.
  45. *
  46. * This function is called when there is a command completion and this
  47. * function updates the statistical data for the command completion.
  48. **/
  49. static void
  50. lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  51. {
  52. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  53. struct lpfc_nodelist *pnode = rdata->pnode;
  54. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  55. unsigned long flags;
  56. struct Scsi_Host *shost = cmd->device->host;
  57. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  58. unsigned long latency;
  59. int i;
  60. if (cmd->result)
  61. return;
  62. spin_lock_irqsave(shost->host_lock, flags);
  63. if (!vport->stat_data_enabled ||
  64. vport->stat_data_blocked ||
  65. !pnode->lat_data ||
  66. (phba->bucket_type == LPFC_NO_BUCKET)) {
  67. spin_unlock_irqrestore(shost->host_lock, flags);
  68. return;
  69. }
  70. latency = jiffies_to_msecs(jiffies - lpfc_cmd->start_time);
  71. if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
  72. i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
  73. phba->bucket_step;
  74. if (i >= LPFC_MAX_BUCKET_COUNT)
  75. i = LPFC_MAX_BUCKET_COUNT;
  76. } else {
  77. for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
  78. if (latency <= (phba->bucket_base +
  79. ((1<<i)*phba->bucket_step)))
  80. break;
  81. }
  82. pnode->lat_data[i].cmd_count++;
  83. spin_unlock_irqrestore(shost->host_lock, flags);
  84. }
  85. /**
  86. * lpfc_send_sdev_queuedepth_change_event: Posts a queuedepth change
  87. * event.
  88. * @phba: Pointer to HBA context object.
  89. * @vport: Pointer to vport object.
  90. * @ndlp: Pointer to FC node associated with the target.
  91. * @lun: Lun number of the scsi device.
  92. * @old_val: Old value of the queue depth.
  93. * @new_val: New value of the queue depth.
  94. *
  95. * This function sends an event to the mgmt application indicating
  96. * there is a change in the scsi device queue depth.
  97. **/
  98. static void
  99. lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
  100. struct lpfc_vport *vport,
  101. struct lpfc_nodelist *ndlp,
  102. uint32_t lun,
  103. uint32_t old_val,
  104. uint32_t new_val)
  105. {
  106. struct lpfc_fast_path_event *fast_path_evt;
  107. unsigned long flags;
  108. fast_path_evt = lpfc_alloc_fast_evt(phba);
  109. if (!fast_path_evt)
  110. return;
  111. fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
  112. FC_REG_SCSI_EVENT;
  113. fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
  114. LPFC_EVENT_VARQUEDEPTH;
  115. /* Report all luns with change in queue depth */
  116. fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
  117. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  118. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
  119. &ndlp->nlp_portname, sizeof(struct lpfc_name));
  120. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
  121. &ndlp->nlp_nodename, sizeof(struct lpfc_name));
  122. }
  123. fast_path_evt->un.queue_depth_evt.oldval = old_val;
  124. fast_path_evt->un.queue_depth_evt.newval = new_val;
  125. fast_path_evt->vport = vport;
  126. fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
  127. spin_lock_irqsave(&phba->hbalock, flags);
  128. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  129. spin_unlock_irqrestore(&phba->hbalock, flags);
  130. lpfc_worker_wake_up(phba);
  131. return;
  132. }
  133. /*
  134. * This function is called with no lock held when there is a resource
  135. * error in driver or in firmware.
  136. */
  137. void
  138. lpfc_adjust_queue_depth(struct lpfc_hba *phba)
  139. {
  140. unsigned long flags;
  141. uint32_t evt_posted;
  142. spin_lock_irqsave(&phba->hbalock, flags);
  143. atomic_inc(&phba->num_rsrc_err);
  144. phba->last_rsrc_error_time = jiffies;
  145. if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
  146. spin_unlock_irqrestore(&phba->hbalock, flags);
  147. return;
  148. }
  149. phba->last_ramp_down_time = jiffies;
  150. spin_unlock_irqrestore(&phba->hbalock, flags);
  151. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  152. evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
  153. if (!evt_posted)
  154. phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
  155. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  156. if (!evt_posted)
  157. lpfc_worker_wake_up(phba);
  158. return;
  159. }
  160. /*
  161. * This function is called with no lock held when there is a successful
  162. * SCSI command completion.
  163. */
  164. static inline void
  165. lpfc_rampup_queue_depth(struct lpfc_vport *vport,
  166. struct scsi_device *sdev)
  167. {
  168. unsigned long flags;
  169. struct lpfc_hba *phba = vport->phba;
  170. uint32_t evt_posted;
  171. atomic_inc(&phba->num_cmd_success);
  172. if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
  173. return;
  174. spin_lock_irqsave(&phba->hbalock, flags);
  175. if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
  176. ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
  177. spin_unlock_irqrestore(&phba->hbalock, flags);
  178. return;
  179. }
  180. phba->last_ramp_up_time = jiffies;
  181. spin_unlock_irqrestore(&phba->hbalock, flags);
  182. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  183. evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
  184. if (!evt_posted)
  185. phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
  186. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  187. if (!evt_posted)
  188. lpfc_worker_wake_up(phba);
  189. return;
  190. }
  191. void
  192. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  193. {
  194. struct lpfc_vport **vports;
  195. struct Scsi_Host *shost;
  196. struct scsi_device *sdev;
  197. unsigned long new_queue_depth, old_queue_depth;
  198. unsigned long num_rsrc_err, num_cmd_success;
  199. int i;
  200. struct lpfc_rport_data *rdata;
  201. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  202. num_cmd_success = atomic_read(&phba->num_cmd_success);
  203. vports = lpfc_create_vport_work_array(phba);
  204. if (vports != NULL)
  205. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  206. shost = lpfc_shost_from_vport(vports[i]);
  207. shost_for_each_device(sdev, shost) {
  208. new_queue_depth =
  209. sdev->queue_depth * num_rsrc_err /
  210. (num_rsrc_err + num_cmd_success);
  211. if (!new_queue_depth)
  212. new_queue_depth = sdev->queue_depth - 1;
  213. else
  214. new_queue_depth = sdev->queue_depth -
  215. new_queue_depth;
  216. old_queue_depth = sdev->queue_depth;
  217. if (sdev->ordered_tags)
  218. scsi_adjust_queue_depth(sdev,
  219. MSG_ORDERED_TAG,
  220. new_queue_depth);
  221. else
  222. scsi_adjust_queue_depth(sdev,
  223. MSG_SIMPLE_TAG,
  224. new_queue_depth);
  225. rdata = sdev->hostdata;
  226. if (rdata)
  227. lpfc_send_sdev_queuedepth_change_event(
  228. phba, vports[i],
  229. rdata->pnode,
  230. sdev->lun, old_queue_depth,
  231. new_queue_depth);
  232. }
  233. }
  234. lpfc_destroy_vport_work_array(phba, vports);
  235. atomic_set(&phba->num_rsrc_err, 0);
  236. atomic_set(&phba->num_cmd_success, 0);
  237. }
  238. void
  239. lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
  240. {
  241. struct lpfc_vport **vports;
  242. struct Scsi_Host *shost;
  243. struct scsi_device *sdev;
  244. int i;
  245. struct lpfc_rport_data *rdata;
  246. vports = lpfc_create_vport_work_array(phba);
  247. if (vports != NULL)
  248. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  249. shost = lpfc_shost_from_vport(vports[i]);
  250. shost_for_each_device(sdev, shost) {
  251. if (vports[i]->cfg_lun_queue_depth <=
  252. sdev->queue_depth)
  253. continue;
  254. if (sdev->ordered_tags)
  255. scsi_adjust_queue_depth(sdev,
  256. MSG_ORDERED_TAG,
  257. sdev->queue_depth+1);
  258. else
  259. scsi_adjust_queue_depth(sdev,
  260. MSG_SIMPLE_TAG,
  261. sdev->queue_depth+1);
  262. rdata = sdev->hostdata;
  263. if (rdata)
  264. lpfc_send_sdev_queuedepth_change_event(
  265. phba, vports[i],
  266. rdata->pnode,
  267. sdev->lun,
  268. sdev->queue_depth - 1,
  269. sdev->queue_depth);
  270. }
  271. }
  272. lpfc_destroy_vport_work_array(phba, vports);
  273. atomic_set(&phba->num_rsrc_err, 0);
  274. atomic_set(&phba->num_cmd_success, 0);
  275. }
  276. /**
  277. * lpfc_scsi_dev_block: set all scsi hosts to block state.
  278. * @phba: Pointer to HBA context object.
  279. *
  280. * This function walks vport list and set each SCSI host to block state
  281. * by invoking fc_remote_port_delete() routine. This function is invoked
  282. * with EEH when device's PCI slot has been permanently disabled.
  283. **/
  284. void
  285. lpfc_scsi_dev_block(struct lpfc_hba *phba)
  286. {
  287. struct lpfc_vport **vports;
  288. struct Scsi_Host *shost;
  289. struct scsi_device *sdev;
  290. struct fc_rport *rport;
  291. int i;
  292. vports = lpfc_create_vport_work_array(phba);
  293. if (vports != NULL)
  294. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  295. shost = lpfc_shost_from_vport(vports[i]);
  296. shost_for_each_device(sdev, shost) {
  297. rport = starget_to_rport(scsi_target(sdev));
  298. fc_remote_port_delete(rport);
  299. }
  300. }
  301. lpfc_destroy_vport_work_array(phba, vports);
  302. }
  303. /*
  304. * This routine allocates a scsi buffer, which contains all the necessary
  305. * information needed to initiate a SCSI I/O. The non-DMAable buffer region
  306. * contains information to build the IOCB. The DMAable region contains
  307. * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
  308. * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
  309. * and the BPL BDE is setup in the IOCB.
  310. */
  311. static struct lpfc_scsi_buf *
  312. lpfc_new_scsi_buf(struct lpfc_vport *vport)
  313. {
  314. struct lpfc_hba *phba = vport->phba;
  315. struct lpfc_scsi_buf *psb;
  316. struct ulp_bde64 *bpl;
  317. IOCB_t *iocb;
  318. dma_addr_t pdma_phys_fcp_cmd;
  319. dma_addr_t pdma_phys_fcp_rsp;
  320. dma_addr_t pdma_phys_bpl;
  321. uint16_t iotag;
  322. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  323. if (!psb)
  324. return NULL;
  325. /*
  326. * Get memory from the pci pool to map the virt space to pci bus space
  327. * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
  328. * struct fcp_rsp and the number of bde's necessary to support the
  329. * sg_tablesize.
  330. */
  331. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
  332. &psb->dma_handle);
  333. if (!psb->data) {
  334. kfree(psb);
  335. return NULL;
  336. }
  337. /* Initialize virtual ptrs to dma_buf region. */
  338. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  339. /* Allocate iotag for psb->cur_iocbq. */
  340. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  341. if (iotag == 0) {
  342. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  343. psb->data, psb->dma_handle);
  344. kfree (psb);
  345. return NULL;
  346. }
  347. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  348. psb->fcp_cmnd = psb->data;
  349. psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
  350. psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
  351. sizeof(struct fcp_rsp);
  352. /* Initialize local short-hand pointers. */
  353. bpl = psb->fcp_bpl;
  354. pdma_phys_fcp_cmd = psb->dma_handle;
  355. pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
  356. pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
  357. sizeof(struct fcp_rsp);
  358. /*
  359. * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
  360. * list bdes. Initialize the first two and leave the rest for
  361. * queuecommand.
  362. */
  363. bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
  364. bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
  365. bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
  366. bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  367. bpl[0].tus.w = le32_to_cpu(bpl->tus.w);
  368. /* Setup the physical region for the FCP RSP */
  369. bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
  370. bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
  371. bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
  372. bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  373. bpl[1].tus.w = le32_to_cpu(bpl->tus.w);
  374. /*
  375. * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
  376. * initialize it with all known data now.
  377. */
  378. iocb = &psb->cur_iocbq.iocb;
  379. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  380. if (phba->sli_rev == 3) {
  381. /* fill in immediate fcp command BDE */
  382. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
  383. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  384. iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
  385. unsli3.fcp_ext.icd);
  386. iocb->un.fcpi64.bdl.addrHigh = 0;
  387. iocb->ulpBdeCount = 0;
  388. iocb->ulpLe = 0;
  389. /* fill in responce BDE */
  390. iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  391. iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
  392. sizeof(struct fcp_rsp);
  393. iocb->unsli3.fcp_ext.rbde.addrLow =
  394. putPaddrLow(pdma_phys_fcp_rsp);
  395. iocb->unsli3.fcp_ext.rbde.addrHigh =
  396. putPaddrHigh(pdma_phys_fcp_rsp);
  397. } else {
  398. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  399. iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  400. iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_bpl);
  401. iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_bpl);
  402. iocb->ulpBdeCount = 1;
  403. iocb->ulpLe = 1;
  404. }
  405. iocb->ulpClass = CLASS3;
  406. return psb;
  407. }
  408. static struct lpfc_scsi_buf*
  409. lpfc_get_scsi_buf(struct lpfc_hba * phba)
  410. {
  411. struct lpfc_scsi_buf * lpfc_cmd = NULL;
  412. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  413. unsigned long iflag = 0;
  414. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  415. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  416. if (lpfc_cmd) {
  417. lpfc_cmd->seg_cnt = 0;
  418. lpfc_cmd->nonsg_phys = 0;
  419. }
  420. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  421. return lpfc_cmd;
  422. }
  423. static void
  424. lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  425. {
  426. unsigned long iflag = 0;
  427. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  428. psb->pCmd = NULL;
  429. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  430. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  431. }
  432. static int
  433. lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  434. {
  435. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  436. struct scatterlist *sgel = NULL;
  437. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  438. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  439. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  440. struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
  441. dma_addr_t physaddr;
  442. uint32_t num_bde = 0;
  443. int nseg, datadir = scsi_cmnd->sc_data_direction;
  444. /*
  445. * There are three possibilities here - use scatter-gather segment, use
  446. * the single mapping, or neither. Start the lpfc command prep by
  447. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  448. * data bde entry.
  449. */
  450. bpl += 2;
  451. if (scsi_sg_count(scsi_cmnd)) {
  452. /*
  453. * The driver stores the segment count returned from pci_map_sg
  454. * because this a count of dma-mappings used to map the use_sg
  455. * pages. They are not guaranteed to be the same for those
  456. * architectures that implement an IOMMU.
  457. */
  458. nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
  459. scsi_sg_count(scsi_cmnd), datadir);
  460. if (unlikely(!nseg))
  461. return 1;
  462. lpfc_cmd->seg_cnt = nseg;
  463. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  464. printk(KERN_ERR "%s: Too many sg segments from "
  465. "dma_map_sg. Config %d, seg_cnt %d",
  466. __func__, phba->cfg_sg_seg_cnt,
  467. lpfc_cmd->seg_cnt);
  468. scsi_dma_unmap(scsi_cmnd);
  469. return 1;
  470. }
  471. /*
  472. * The driver established a maximum scatter-gather segment count
  473. * during probe that limits the number of sg elements in any
  474. * single scsi command. Just run through the seg_cnt and format
  475. * the bde's.
  476. * When using SLI-3 the driver will try to fit all the BDEs into
  477. * the IOCB. If it can't then the BDEs get added to a BPL as it
  478. * does for SLI-2 mode.
  479. */
  480. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  481. physaddr = sg_dma_address(sgel);
  482. if (phba->sli_rev == 3 &&
  483. nseg <= LPFC_EXT_DATA_BDE_COUNT) {
  484. data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  485. data_bde->tus.f.bdeSize = sg_dma_len(sgel);
  486. data_bde->addrLow = putPaddrLow(physaddr);
  487. data_bde->addrHigh = putPaddrHigh(physaddr);
  488. data_bde++;
  489. } else {
  490. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  491. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  492. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  493. bpl->addrLow =
  494. le32_to_cpu(putPaddrLow(physaddr));
  495. bpl->addrHigh =
  496. le32_to_cpu(putPaddrHigh(physaddr));
  497. bpl++;
  498. }
  499. }
  500. }
  501. /*
  502. * Finish initializing those IOCB fields that are dependent on the
  503. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  504. * explicitly reinitialized and for SLI-3 the extended bde count is
  505. * explicitly reinitialized since all iocb memory resources are reused.
  506. */
  507. if (phba->sli_rev == 3) {
  508. if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
  509. /*
  510. * The extended IOCB format can only fit 3 BDE or a BPL.
  511. * This I/O has more than 3 BDE so the 1st data bde will
  512. * be a BPL that is filled in here.
  513. */
  514. physaddr = lpfc_cmd->dma_handle;
  515. data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
  516. data_bde->tus.f.bdeSize = (num_bde *
  517. sizeof(struct ulp_bde64));
  518. physaddr += (sizeof(struct fcp_cmnd) +
  519. sizeof(struct fcp_rsp) +
  520. (2 * sizeof(struct ulp_bde64)));
  521. data_bde->addrHigh = putPaddrHigh(physaddr);
  522. data_bde->addrLow = putPaddrLow(physaddr);
  523. /* ebde count includes the responce bde and data bpl */
  524. iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
  525. } else {
  526. /* ebde count includes the responce bde and data bdes */
  527. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  528. }
  529. } else {
  530. iocb_cmd->un.fcpi64.bdl.bdeSize =
  531. ((num_bde + 2) * sizeof(struct ulp_bde64));
  532. }
  533. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  534. return 0;
  535. }
  536. /**
  537. * lpfc_send_scsi_error_event: Posts an event when there is SCSI error.
  538. * @phba: Pointer to hba context object.
  539. * @vport: Pointer to vport object.
  540. * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
  541. * @rsp_iocb: Pointer to response iocb object which reported error.
  542. *
  543. * This function posts an event when there is a SCSI command reporting
  544. * error from the scsi device.
  545. **/
  546. static void
  547. lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
  548. struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
  549. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  550. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  551. uint32_t resp_info = fcprsp->rspStatus2;
  552. uint32_t scsi_status = fcprsp->rspStatus3;
  553. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  554. struct lpfc_fast_path_event *fast_path_evt = NULL;
  555. struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
  556. unsigned long flags;
  557. /* If there is queuefull or busy condition send a scsi event */
  558. if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
  559. (cmnd->result == SAM_STAT_BUSY)) {
  560. fast_path_evt = lpfc_alloc_fast_evt(phba);
  561. if (!fast_path_evt)
  562. return;
  563. fast_path_evt->un.scsi_evt.event_type =
  564. FC_REG_SCSI_EVENT;
  565. fast_path_evt->un.scsi_evt.subcategory =
  566. (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
  567. LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
  568. fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
  569. memcpy(&fast_path_evt->un.scsi_evt.wwpn,
  570. &pnode->nlp_portname, sizeof(struct lpfc_name));
  571. memcpy(&fast_path_evt->un.scsi_evt.wwnn,
  572. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  573. } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
  574. ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
  575. fast_path_evt = lpfc_alloc_fast_evt(phba);
  576. if (!fast_path_evt)
  577. return;
  578. fast_path_evt->un.check_cond_evt.scsi_event.event_type =
  579. FC_REG_SCSI_EVENT;
  580. fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
  581. LPFC_EVENT_CHECK_COND;
  582. fast_path_evt->un.check_cond_evt.scsi_event.lun =
  583. cmnd->device->lun;
  584. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
  585. &pnode->nlp_portname, sizeof(struct lpfc_name));
  586. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
  587. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  588. fast_path_evt->un.check_cond_evt.sense_key =
  589. cmnd->sense_buffer[2] & 0xf;
  590. fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
  591. fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
  592. } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  593. fcpi_parm &&
  594. ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
  595. ((scsi_status == SAM_STAT_GOOD) &&
  596. !(resp_info & (RESID_UNDER | RESID_OVER))))) {
  597. /*
  598. * If status is good or resid does not match with fcp_param and
  599. * there is valid fcpi_parm, then there is a read_check error
  600. */
  601. fast_path_evt = lpfc_alloc_fast_evt(phba);
  602. if (!fast_path_evt)
  603. return;
  604. fast_path_evt->un.read_check_error.header.event_type =
  605. FC_REG_FABRIC_EVENT;
  606. fast_path_evt->un.read_check_error.header.subcategory =
  607. LPFC_EVENT_FCPRDCHKERR;
  608. memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
  609. &pnode->nlp_portname, sizeof(struct lpfc_name));
  610. memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
  611. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  612. fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
  613. fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
  614. fast_path_evt->un.read_check_error.fcpiparam =
  615. fcpi_parm;
  616. } else
  617. return;
  618. fast_path_evt->vport = vport;
  619. spin_lock_irqsave(&phba->hbalock, flags);
  620. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  621. spin_unlock_irqrestore(&phba->hbalock, flags);
  622. lpfc_worker_wake_up(phba);
  623. return;
  624. }
  625. static void
  626. lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
  627. {
  628. /*
  629. * There are only two special cases to consider. (1) the scsi command
  630. * requested scatter-gather usage or (2) the scsi command allocated
  631. * a request buffer, but did not request use_sg. There is a third
  632. * case, but it does not require resource deallocation.
  633. */
  634. if (psb->seg_cnt > 0)
  635. scsi_dma_unmap(psb->pCmd);
  636. }
  637. static void
  638. lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  639. struct lpfc_iocbq *rsp_iocb)
  640. {
  641. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  642. struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
  643. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  644. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  645. uint32_t resp_info = fcprsp->rspStatus2;
  646. uint32_t scsi_status = fcprsp->rspStatus3;
  647. uint32_t *lp;
  648. uint32_t host_status = DID_OK;
  649. uint32_t rsplen = 0;
  650. uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
  651. /*
  652. * If this is a task management command, there is no
  653. * scsi packet associated with this lpfc_cmd. The driver
  654. * consumes it.
  655. */
  656. if (fcpcmd->fcpCntl2) {
  657. scsi_status = 0;
  658. goto out;
  659. }
  660. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  661. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  662. if (snslen > SCSI_SENSE_BUFFERSIZE)
  663. snslen = SCSI_SENSE_BUFFERSIZE;
  664. if (resp_info & RSP_LEN_VALID)
  665. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  666. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  667. }
  668. lp = (uint32_t *)cmnd->sense_buffer;
  669. if (!scsi_status && (resp_info & RESID_UNDER))
  670. logit = LOG_FCP;
  671. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  672. "0730 FCP command x%x failed: x%x SNS x%x x%x "
  673. "Data: x%x x%x x%x x%x x%x\n",
  674. cmnd->cmnd[0], scsi_status,
  675. be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
  676. be32_to_cpu(fcprsp->rspResId),
  677. be32_to_cpu(fcprsp->rspSnsLen),
  678. be32_to_cpu(fcprsp->rspRspLen),
  679. fcprsp->rspInfo3);
  680. if (resp_info & RSP_LEN_VALID) {
  681. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  682. if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
  683. (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
  684. host_status = DID_ERROR;
  685. goto out;
  686. }
  687. }
  688. scsi_set_resid(cmnd, 0);
  689. if (resp_info & RESID_UNDER) {
  690. scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
  691. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  692. "0716 FCP Read Underrun, expected %d, "
  693. "residual %d Data: x%x x%x x%x\n",
  694. be32_to_cpu(fcpcmd->fcpDl),
  695. scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
  696. cmnd->underflow);
  697. /*
  698. * If there is an under run check if under run reported by
  699. * storage array is same as the under run reported by HBA.
  700. * If this is not same, there is a dropped frame.
  701. */
  702. if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  703. fcpi_parm &&
  704. (scsi_get_resid(cmnd) != fcpi_parm)) {
  705. lpfc_printf_vlog(vport, KERN_WARNING,
  706. LOG_FCP | LOG_FCP_ERROR,
  707. "0735 FCP Read Check Error "
  708. "and Underrun Data: x%x x%x x%x x%x\n",
  709. be32_to_cpu(fcpcmd->fcpDl),
  710. scsi_get_resid(cmnd), fcpi_parm,
  711. cmnd->cmnd[0]);
  712. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  713. host_status = DID_ERROR;
  714. }
  715. /*
  716. * The cmnd->underflow is the minimum number of bytes that must
  717. * be transfered for this command. Provided a sense condition
  718. * is not present, make sure the actual amount transferred is at
  719. * least the underflow value or fail.
  720. */
  721. if (!(resp_info & SNS_LEN_VALID) &&
  722. (scsi_status == SAM_STAT_GOOD) &&
  723. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
  724. < cmnd->underflow)) {
  725. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  726. "0717 FCP command x%x residual "
  727. "underrun converted to error "
  728. "Data: x%x x%x x%x\n",
  729. cmnd->cmnd[0], scsi_bufflen(cmnd),
  730. scsi_get_resid(cmnd), cmnd->underflow);
  731. host_status = DID_ERROR;
  732. }
  733. } else if (resp_info & RESID_OVER) {
  734. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  735. "0720 FCP command x%x residual overrun error. "
  736. "Data: x%x x%x \n", cmnd->cmnd[0],
  737. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  738. host_status = DID_ERROR;
  739. /*
  740. * Check SLI validation that all the transfer was actually done
  741. * (fcpi_parm should be zero). Apply check only to reads.
  742. */
  743. } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
  744. (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
  745. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
  746. "0734 FCP Read Check Error Data: "
  747. "x%x x%x x%x x%x\n",
  748. be32_to_cpu(fcpcmd->fcpDl),
  749. be32_to_cpu(fcprsp->rspResId),
  750. fcpi_parm, cmnd->cmnd[0]);
  751. host_status = DID_ERROR;
  752. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  753. }
  754. out:
  755. cmnd->result = ScsiResult(host_status, scsi_status);
  756. lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
  757. }
  758. static void
  759. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  760. struct lpfc_iocbq *pIocbOut)
  761. {
  762. struct lpfc_scsi_buf *lpfc_cmd =
  763. (struct lpfc_scsi_buf *) pIocbIn->context1;
  764. struct lpfc_vport *vport = pIocbIn->vport;
  765. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  766. struct lpfc_nodelist *pnode = rdata->pnode;
  767. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  768. int result;
  769. struct scsi_device *sdev, *tmp_sdev;
  770. int depth = 0;
  771. unsigned long flags;
  772. struct lpfc_fast_path_event *fast_path_evt;
  773. lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
  774. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  775. atomic_dec(&pnode->cmd_pending);
  776. if (lpfc_cmd->status) {
  777. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  778. (lpfc_cmd->result & IOERR_DRVR_MASK))
  779. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  780. else if (lpfc_cmd->status >= IOSTAT_CNT)
  781. lpfc_cmd->status = IOSTAT_DEFAULT;
  782. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  783. "0729 FCP cmd x%x failed <%d/%d> "
  784. "status: x%x result: x%x Data: x%x x%x\n",
  785. cmd->cmnd[0],
  786. cmd->device ? cmd->device->id : 0xffff,
  787. cmd->device ? cmd->device->lun : 0xffff,
  788. lpfc_cmd->status, lpfc_cmd->result,
  789. pIocbOut->iocb.ulpContext,
  790. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  791. switch (lpfc_cmd->status) {
  792. case IOSTAT_FCP_RSP_ERROR:
  793. /* Call FCP RSP handler to determine result */
  794. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  795. break;
  796. case IOSTAT_NPORT_BSY:
  797. case IOSTAT_FABRIC_BSY:
  798. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  799. fast_path_evt = lpfc_alloc_fast_evt(phba);
  800. if (!fast_path_evt)
  801. break;
  802. fast_path_evt->un.fabric_evt.event_type =
  803. FC_REG_FABRIC_EVENT;
  804. fast_path_evt->un.fabric_evt.subcategory =
  805. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  806. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  807. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  808. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  809. &pnode->nlp_portname,
  810. sizeof(struct lpfc_name));
  811. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  812. &pnode->nlp_nodename,
  813. sizeof(struct lpfc_name));
  814. }
  815. fast_path_evt->vport = vport;
  816. fast_path_evt->work_evt.evt =
  817. LPFC_EVT_FASTPATH_MGMT_EVT;
  818. spin_lock_irqsave(&phba->hbalock, flags);
  819. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  820. &phba->work_list);
  821. spin_unlock_irqrestore(&phba->hbalock, flags);
  822. lpfc_worker_wake_up(phba);
  823. break;
  824. case IOSTAT_LOCAL_REJECT:
  825. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  826. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  827. lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
  828. cmd->result = ScsiResult(DID_REQUEUE, 0);
  829. break;
  830. } /* else: fall through */
  831. default:
  832. cmd->result = ScsiResult(DID_ERROR, 0);
  833. break;
  834. }
  835. if (!pnode || !NLP_CHK_NODE_ACT(pnode)
  836. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  837. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
  838. SAM_STAT_BUSY);
  839. } else {
  840. cmd->result = ScsiResult(DID_OK, 0);
  841. }
  842. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  843. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  844. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  845. "0710 Iodone <%d/%d> cmd %p, error "
  846. "x%x SNS x%x x%x Data: x%x x%x\n",
  847. cmd->device->id, cmd->device->lun, cmd,
  848. cmd->result, *lp, *(lp + 3), cmd->retries,
  849. scsi_get_resid(cmd));
  850. }
  851. lpfc_update_stats(phba, lpfc_cmd);
  852. result = cmd->result;
  853. sdev = cmd->device;
  854. if (vport->cfg_max_scsicmpl_time &&
  855. time_after(jiffies, lpfc_cmd->start_time +
  856. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  857. spin_lock_irqsave(sdev->host->host_lock, flags);
  858. if ((pnode->cmd_qdepth > atomic_read(&pnode->cmd_pending) &&
  859. (atomic_read(&pnode->cmd_pending) > LPFC_MIN_TGT_QDEPTH) &&
  860. ((cmd->cmnd[0] == READ_10) || (cmd->cmnd[0] == WRITE_10))))
  861. pnode->cmd_qdepth = atomic_read(&pnode->cmd_pending);
  862. pnode->last_change_time = jiffies;
  863. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  864. } else if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
  865. time_after(jiffies, pnode->last_change_time +
  866. msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
  867. spin_lock_irqsave(sdev->host->host_lock, flags);
  868. pnode->cmd_qdepth += pnode->cmd_qdepth *
  869. LPFC_TGTQ_RAMPUP_PCENT / 100;
  870. if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
  871. pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
  872. pnode->last_change_time = jiffies;
  873. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  874. }
  875. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  876. cmd->scsi_done(cmd);
  877. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  878. /*
  879. * If there is a thread waiting for command completion
  880. * wake up the thread.
  881. */
  882. spin_lock_irqsave(sdev->host->host_lock, flags);
  883. lpfc_cmd->pCmd = NULL;
  884. if (lpfc_cmd->waitq)
  885. wake_up(lpfc_cmd->waitq);
  886. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  887. lpfc_release_scsi_buf(phba, lpfc_cmd);
  888. return;
  889. }
  890. if (!result)
  891. lpfc_rampup_queue_depth(vport, sdev);
  892. if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
  893. ((jiffies - pnode->last_ramp_up_time) >
  894. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  895. ((jiffies - pnode->last_q_full_time) >
  896. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  897. (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
  898. shost_for_each_device(tmp_sdev, sdev->host) {
  899. if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
  900. if (tmp_sdev->id != sdev->id)
  901. continue;
  902. if (tmp_sdev->ordered_tags)
  903. scsi_adjust_queue_depth(tmp_sdev,
  904. MSG_ORDERED_TAG,
  905. tmp_sdev->queue_depth+1);
  906. else
  907. scsi_adjust_queue_depth(tmp_sdev,
  908. MSG_SIMPLE_TAG,
  909. tmp_sdev->queue_depth+1);
  910. pnode->last_ramp_up_time = jiffies;
  911. }
  912. }
  913. lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
  914. 0xFFFFFFFF,
  915. sdev->queue_depth - 1, sdev->queue_depth);
  916. }
  917. /*
  918. * Check for queue full. If the lun is reporting queue full, then
  919. * back off the lun queue depth to prevent target overloads.
  920. */
  921. if (result == SAM_STAT_TASK_SET_FULL && pnode &&
  922. NLP_CHK_NODE_ACT(pnode)) {
  923. pnode->last_q_full_time = jiffies;
  924. shost_for_each_device(tmp_sdev, sdev->host) {
  925. if (tmp_sdev->id != sdev->id)
  926. continue;
  927. depth = scsi_track_queue_full(tmp_sdev,
  928. tmp_sdev->queue_depth - 1);
  929. }
  930. /*
  931. * The queue depth cannot be lowered any more.
  932. * Modify the returned error code to store
  933. * the final depth value set by
  934. * scsi_track_queue_full.
  935. */
  936. if (depth == -1)
  937. depth = sdev->host->cmd_per_lun;
  938. if (depth) {
  939. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  940. "0711 detected queue full - lun queue "
  941. "depth adjusted to %d.\n", depth);
  942. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  943. pnode, 0xFFFFFFFF,
  944. depth+1, depth);
  945. }
  946. }
  947. /*
  948. * If there is a thread waiting for command completion
  949. * wake up the thread.
  950. */
  951. spin_lock_irqsave(sdev->host->host_lock, flags);
  952. lpfc_cmd->pCmd = NULL;
  953. if (lpfc_cmd->waitq)
  954. wake_up(lpfc_cmd->waitq);
  955. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  956. lpfc_release_scsi_buf(phba, lpfc_cmd);
  957. }
  958. /**
  959. * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB.
  960. * @data: A pointer to the immediate command data portion of the IOCB.
  961. * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
  962. *
  963. * The routine copies the entire FCP command from @fcp_cmnd to @data while
  964. * byte swapping the data to big endian format for transmission on the wire.
  965. **/
  966. static void
  967. lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
  968. {
  969. int i, j;
  970. for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
  971. i += sizeof(uint32_t), j++) {
  972. ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
  973. }
  974. }
  975. static void
  976. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  977. struct lpfc_nodelist *pnode)
  978. {
  979. struct lpfc_hba *phba = vport->phba;
  980. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  981. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  982. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  983. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  984. int datadir = scsi_cmnd->sc_data_direction;
  985. char tag[2];
  986. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  987. return;
  988. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  989. /* clear task management bits */
  990. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  991. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  992. &lpfc_cmd->fcp_cmnd->fcp_lun);
  993. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  994. if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
  995. switch (tag[0]) {
  996. case HEAD_OF_QUEUE_TAG:
  997. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  998. break;
  999. case ORDERED_QUEUE_TAG:
  1000. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  1001. break;
  1002. default:
  1003. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  1004. break;
  1005. }
  1006. } else
  1007. fcp_cmnd->fcpCntl1 = 0;
  1008. /*
  1009. * There are three possibilities here - use scatter-gather segment, use
  1010. * the single mapping, or neither. Start the lpfc command prep by
  1011. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1012. * data bde entry.
  1013. */
  1014. if (scsi_sg_count(scsi_cmnd)) {
  1015. if (datadir == DMA_TO_DEVICE) {
  1016. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  1017. iocb_cmd->un.fcpi.fcpi_parm = 0;
  1018. iocb_cmd->ulpPU = 0;
  1019. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  1020. phba->fc4OutputRequests++;
  1021. } else {
  1022. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  1023. iocb_cmd->ulpPU = PARM_READ_CHECK;
  1024. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  1025. fcp_cmnd->fcpCntl3 = READ_DATA;
  1026. phba->fc4InputRequests++;
  1027. }
  1028. } else {
  1029. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  1030. iocb_cmd->un.fcpi.fcpi_parm = 0;
  1031. iocb_cmd->ulpPU = 0;
  1032. fcp_cmnd->fcpCntl3 = 0;
  1033. phba->fc4ControlRequests++;
  1034. }
  1035. if (phba->sli_rev == 3)
  1036. lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
  1037. /*
  1038. * Finish initializing those IOCB fields that are independent
  1039. * of the scsi_cmnd request_buffer
  1040. */
  1041. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  1042. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  1043. piocbq->iocb.ulpFCP2Rcvy = 1;
  1044. else
  1045. piocbq->iocb.ulpFCP2Rcvy = 0;
  1046. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  1047. piocbq->context1 = lpfc_cmd;
  1048. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  1049. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  1050. piocbq->vport = vport;
  1051. }
  1052. static int
  1053. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  1054. struct lpfc_scsi_buf *lpfc_cmd,
  1055. unsigned int lun,
  1056. uint8_t task_mgmt_cmd)
  1057. {
  1058. struct lpfc_iocbq *piocbq;
  1059. IOCB_t *piocb;
  1060. struct fcp_cmnd *fcp_cmnd;
  1061. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  1062. struct lpfc_nodelist *ndlp = rdata->pnode;
  1063. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
  1064. ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  1065. return 0;
  1066. piocbq = &(lpfc_cmd->cur_iocbq);
  1067. piocbq->vport = vport;
  1068. piocb = &piocbq->iocb;
  1069. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1070. /* Clear out any old data in the FCP command area */
  1071. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  1072. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  1073. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  1074. if (vport->phba->sli_rev == 3)
  1075. lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
  1076. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  1077. piocb->ulpContext = ndlp->nlp_rpi;
  1078. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  1079. piocb->ulpFCP2Rcvy = 1;
  1080. }
  1081. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  1082. /* ulpTimeout is only one byte */
  1083. if (lpfc_cmd->timeout > 0xff) {
  1084. /*
  1085. * Do not timeout the command at the firmware level.
  1086. * The driver will provide the timeout mechanism.
  1087. */
  1088. piocb->ulpTimeout = 0;
  1089. } else {
  1090. piocb->ulpTimeout = lpfc_cmd->timeout;
  1091. }
  1092. return 1;
  1093. }
  1094. static void
  1095. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  1096. struct lpfc_iocbq *cmdiocbq,
  1097. struct lpfc_iocbq *rspiocbq)
  1098. {
  1099. struct lpfc_scsi_buf *lpfc_cmd =
  1100. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  1101. if (lpfc_cmd)
  1102. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1103. return;
  1104. }
  1105. static int
  1106. lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
  1107. unsigned tgt_id, unsigned int lun,
  1108. struct lpfc_rport_data *rdata)
  1109. {
  1110. struct lpfc_hba *phba = vport->phba;
  1111. struct lpfc_iocbq *iocbq;
  1112. struct lpfc_iocbq *iocbqrsp;
  1113. int ret;
  1114. int status;
  1115. if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
  1116. return FAILED;
  1117. lpfc_cmd->rdata = rdata;
  1118. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
  1119. FCP_TARGET_RESET);
  1120. if (!status)
  1121. return FAILED;
  1122. iocbq = &lpfc_cmd->cur_iocbq;
  1123. iocbqrsp = lpfc_sli_get_iocbq(phba);
  1124. if (!iocbqrsp)
  1125. return FAILED;
  1126. /* Issue Target Reset to TGT <num> */
  1127. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1128. "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
  1129. tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
  1130. status = lpfc_sli_issue_iocb_wait(phba,
  1131. &phba->sli.ring[phba->sli.fcp_ring],
  1132. iocbq, iocbqrsp, lpfc_cmd->timeout);
  1133. if (status != IOCB_SUCCESS) {
  1134. if (status == IOCB_TIMEDOUT) {
  1135. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  1136. ret = TIMEOUT_ERROR;
  1137. } else
  1138. ret = FAILED;
  1139. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  1140. } else {
  1141. ret = SUCCESS;
  1142. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  1143. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  1144. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  1145. (lpfc_cmd->result & IOERR_DRVR_MASK))
  1146. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  1147. }
  1148. lpfc_sli_release_iocbq(phba, iocbqrsp);
  1149. return ret;
  1150. }
  1151. const char *
  1152. lpfc_info(struct Scsi_Host *host)
  1153. {
  1154. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  1155. struct lpfc_hba *phba = vport->phba;
  1156. int len;
  1157. static char lpfcinfobuf[384];
  1158. memset(lpfcinfobuf,0,384);
  1159. if (phba && phba->pcidev){
  1160. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  1161. len = strlen(lpfcinfobuf);
  1162. snprintf(lpfcinfobuf + len,
  1163. 384-len,
  1164. " on PCI bus %02x device %02x irq %d",
  1165. phba->pcidev->bus->number,
  1166. phba->pcidev->devfn,
  1167. phba->pcidev->irq);
  1168. len = strlen(lpfcinfobuf);
  1169. if (phba->Port[0]) {
  1170. snprintf(lpfcinfobuf + len,
  1171. 384-len,
  1172. " port %s",
  1173. phba->Port);
  1174. }
  1175. }
  1176. return lpfcinfobuf;
  1177. }
  1178. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  1179. {
  1180. unsigned long poll_tmo_expires =
  1181. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  1182. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  1183. mod_timer(&phba->fcp_poll_timer,
  1184. poll_tmo_expires);
  1185. }
  1186. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  1187. {
  1188. lpfc_poll_rearm_timer(phba);
  1189. }
  1190. void lpfc_poll_timeout(unsigned long ptr)
  1191. {
  1192. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  1193. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1194. lpfc_sli_poll_fcp_ring (phba);
  1195. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  1196. lpfc_poll_rearm_timer(phba);
  1197. }
  1198. }
  1199. static int
  1200. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  1201. {
  1202. struct Scsi_Host *shost = cmnd->device->host;
  1203. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1204. struct lpfc_hba *phba = vport->phba;
  1205. struct lpfc_sli *psli = &phba->sli;
  1206. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  1207. struct lpfc_nodelist *ndlp = rdata->pnode;
  1208. struct lpfc_scsi_buf *lpfc_cmd;
  1209. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1210. int err;
  1211. err = fc_remote_port_chkready(rport);
  1212. if (err) {
  1213. cmnd->result = err;
  1214. goto out_fail_command;
  1215. }
  1216. /*
  1217. * Catch race where our node has transitioned, but the
  1218. * transport is still transitioning.
  1219. */
  1220. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  1221. cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  1222. goto out_fail_command;
  1223. }
  1224. if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
  1225. goto out_host_busy;
  1226. lpfc_cmd = lpfc_get_scsi_buf(phba);
  1227. if (lpfc_cmd == NULL) {
  1228. lpfc_adjust_queue_depth(phba);
  1229. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1230. "0707 driver's buffer pool is empty, "
  1231. "IO busied\n");
  1232. goto out_host_busy;
  1233. }
  1234. lpfc_cmd->start_time = jiffies;
  1235. /*
  1236. * Store the midlayer's command structure for the completion phase
  1237. * and complete the command initialization.
  1238. */
  1239. lpfc_cmd->pCmd = cmnd;
  1240. lpfc_cmd->rdata = rdata;
  1241. lpfc_cmd->timeout = 0;
  1242. lpfc_cmd->start_time = jiffies;
  1243. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  1244. cmnd->scsi_done = done;
  1245. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  1246. if (err)
  1247. goto out_host_busy_free_buf;
  1248. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  1249. atomic_inc(&ndlp->cmd_pending);
  1250. err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
  1251. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  1252. if (err)
  1253. goto out_host_busy_free_buf;
  1254. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1255. lpfc_sli_poll_fcp_ring(phba);
  1256. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  1257. lpfc_poll_rearm_timer(phba);
  1258. }
  1259. return 0;
  1260. out_host_busy_free_buf:
  1261. atomic_dec(&ndlp->cmd_pending);
  1262. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  1263. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1264. out_host_busy:
  1265. return SCSI_MLQUEUE_HOST_BUSY;
  1266. out_fail_command:
  1267. done(cmnd);
  1268. return 0;
  1269. }
  1270. static void
  1271. lpfc_block_error_handler(struct scsi_cmnd *cmnd)
  1272. {
  1273. struct Scsi_Host *shost = cmnd->device->host;
  1274. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1275. spin_lock_irq(shost->host_lock);
  1276. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  1277. spin_unlock_irq(shost->host_lock);
  1278. msleep(1000);
  1279. spin_lock_irq(shost->host_lock);
  1280. }
  1281. spin_unlock_irq(shost->host_lock);
  1282. return;
  1283. }
  1284. static int
  1285. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  1286. {
  1287. struct Scsi_Host *shost = cmnd->device->host;
  1288. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1289. struct lpfc_hba *phba = vport->phba;
  1290. struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
  1291. struct lpfc_iocbq *iocb;
  1292. struct lpfc_iocbq *abtsiocb;
  1293. struct lpfc_scsi_buf *lpfc_cmd;
  1294. IOCB_t *cmd, *icmd;
  1295. int ret = SUCCESS;
  1296. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  1297. lpfc_block_error_handler(cmnd);
  1298. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  1299. BUG_ON(!lpfc_cmd);
  1300. /*
  1301. * If pCmd field of the corresponding lpfc_scsi_buf structure
  1302. * points to a different SCSI command, then the driver has
  1303. * already completed this command, but the midlayer did not
  1304. * see the completion before the eh fired. Just return
  1305. * SUCCESS.
  1306. */
  1307. iocb = &lpfc_cmd->cur_iocbq;
  1308. if (lpfc_cmd->pCmd != cmnd)
  1309. goto out;
  1310. BUG_ON(iocb->context1 != lpfc_cmd);
  1311. abtsiocb = lpfc_sli_get_iocbq(phba);
  1312. if (abtsiocb == NULL) {
  1313. ret = FAILED;
  1314. goto out;
  1315. }
  1316. /*
  1317. * The scsi command can not be in txq and it is in flight because the
  1318. * pCmd is still pointig at the SCSI command we have to abort. There
  1319. * is no need to search the txcmplq. Just send an abort to the FW.
  1320. */
  1321. cmd = &iocb->iocb;
  1322. icmd = &abtsiocb->iocb;
  1323. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  1324. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  1325. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  1326. icmd->ulpLe = 1;
  1327. icmd->ulpClass = cmd->ulpClass;
  1328. if (lpfc_is_link_up(phba))
  1329. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  1330. else
  1331. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  1332. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  1333. abtsiocb->vport = vport;
  1334. if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
  1335. lpfc_sli_release_iocbq(phba, abtsiocb);
  1336. ret = FAILED;
  1337. goto out;
  1338. }
  1339. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  1340. lpfc_sli_poll_fcp_ring (phba);
  1341. lpfc_cmd->waitq = &waitq;
  1342. /* Wait for abort to complete */
  1343. wait_event_timeout(waitq,
  1344. (lpfc_cmd->pCmd != cmnd),
  1345. (2*vport->cfg_devloss_tmo*HZ));
  1346. spin_lock_irq(shost->host_lock);
  1347. lpfc_cmd->waitq = NULL;
  1348. spin_unlock_irq(shost->host_lock);
  1349. if (lpfc_cmd->pCmd == cmnd) {
  1350. ret = FAILED;
  1351. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1352. "0748 abort handler timed out waiting "
  1353. "for abort to complete: ret %#x, ID %d, "
  1354. "LUN %d, snum %#lx\n",
  1355. ret, cmnd->device->id, cmnd->device->lun,
  1356. cmnd->serial_number);
  1357. }
  1358. out:
  1359. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1360. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  1361. "LUN %d snum %#lx\n", ret, cmnd->device->id,
  1362. cmnd->device->lun, cmnd->serial_number);
  1363. return ret;
  1364. }
  1365. static int
  1366. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  1367. {
  1368. struct Scsi_Host *shost = cmnd->device->host;
  1369. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1370. struct lpfc_hba *phba = vport->phba;
  1371. struct lpfc_scsi_buf *lpfc_cmd;
  1372. struct lpfc_iocbq *iocbq, *iocbqrsp;
  1373. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  1374. struct lpfc_nodelist *pnode = rdata->pnode;
  1375. unsigned long later;
  1376. int ret = SUCCESS;
  1377. int status;
  1378. int cnt;
  1379. struct lpfc_scsi_event_header scsi_event;
  1380. lpfc_block_error_handler(cmnd);
  1381. /*
  1382. * If target is not in a MAPPED state, delay the reset until
  1383. * target is rediscovered or devloss timeout expires.
  1384. */
  1385. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  1386. while (time_after(later, jiffies)) {
  1387. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  1388. return FAILED;
  1389. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  1390. break;
  1391. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  1392. rdata = cmnd->device->hostdata;
  1393. if (!rdata)
  1394. break;
  1395. pnode = rdata->pnode;
  1396. }
  1397. scsi_event.event_type = FC_REG_SCSI_EVENT;
  1398. scsi_event.subcategory = LPFC_EVENT_TGTRESET;
  1399. scsi_event.lun = 0;
  1400. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  1401. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1402. fc_host_post_vendor_event(shost,
  1403. fc_get_event_number(),
  1404. sizeof(scsi_event),
  1405. (char *)&scsi_event,
  1406. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  1407. if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
  1408. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1409. "0721 LUN Reset rport "
  1410. "failure: msec x%x rdata x%p\n",
  1411. jiffies_to_msecs(jiffies - later), rdata);
  1412. return FAILED;
  1413. }
  1414. lpfc_cmd = lpfc_get_scsi_buf(phba);
  1415. if (lpfc_cmd == NULL)
  1416. return FAILED;
  1417. lpfc_cmd->timeout = 60;
  1418. lpfc_cmd->rdata = rdata;
  1419. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
  1420. cmnd->device->lun,
  1421. FCP_TARGET_RESET);
  1422. if (!status) {
  1423. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1424. return FAILED;
  1425. }
  1426. iocbq = &lpfc_cmd->cur_iocbq;
  1427. /* get a buffer for this IOCB command response */
  1428. iocbqrsp = lpfc_sli_get_iocbq(phba);
  1429. if (iocbqrsp == NULL) {
  1430. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1431. return FAILED;
  1432. }
  1433. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1434. "0703 Issue target reset to TGT %d LUN %d "
  1435. "rpi x%x nlp_flag x%x\n", cmnd->device->id,
  1436. cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
  1437. status = lpfc_sli_issue_iocb_wait(phba,
  1438. &phba->sli.ring[phba->sli.fcp_ring],
  1439. iocbq, iocbqrsp, lpfc_cmd->timeout);
  1440. if (status == IOCB_TIMEDOUT) {
  1441. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  1442. ret = TIMEOUT_ERROR;
  1443. } else {
  1444. if (status != IOCB_SUCCESS)
  1445. ret = FAILED;
  1446. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1447. }
  1448. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1449. "0713 SCSI layer issued device reset (%d, %d) "
  1450. "return x%x status x%x result x%x\n",
  1451. cmnd->device->id, cmnd->device->lun, ret,
  1452. iocbqrsp->iocb.ulpStatus,
  1453. iocbqrsp->iocb.un.ulpWord[4]);
  1454. lpfc_sli_release_iocbq(phba, iocbqrsp);
  1455. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
  1456. LPFC_CTX_TGT);
  1457. if (cnt)
  1458. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  1459. cmnd->device->id, cmnd->device->lun,
  1460. LPFC_CTX_TGT);
  1461. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  1462. while (time_after(later, jiffies) && cnt) {
  1463. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  1464. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
  1465. cmnd->device->lun, LPFC_CTX_TGT);
  1466. }
  1467. if (cnt) {
  1468. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1469. "0719 device reset I/O flush failure: "
  1470. "cnt x%x\n", cnt);
  1471. ret = FAILED;
  1472. }
  1473. return ret;
  1474. }
  1475. static int
  1476. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  1477. {
  1478. struct Scsi_Host *shost = cmnd->device->host;
  1479. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1480. struct lpfc_hba *phba = vport->phba;
  1481. struct lpfc_nodelist *ndlp = NULL;
  1482. int match;
  1483. int ret = SUCCESS, status = SUCCESS, i;
  1484. int cnt;
  1485. struct lpfc_scsi_buf * lpfc_cmd;
  1486. unsigned long later;
  1487. struct lpfc_scsi_event_header scsi_event;
  1488. scsi_event.event_type = FC_REG_SCSI_EVENT;
  1489. scsi_event.subcategory = LPFC_EVENT_BUSRESET;
  1490. scsi_event.lun = 0;
  1491. memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
  1492. memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
  1493. fc_host_post_vendor_event(shost,
  1494. fc_get_event_number(),
  1495. sizeof(scsi_event),
  1496. (char *)&scsi_event,
  1497. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  1498. lpfc_block_error_handler(cmnd);
  1499. /*
  1500. * Since the driver manages a single bus device, reset all
  1501. * targets known to the driver. Should any target reset
  1502. * fail, this routine returns failure to the midlayer.
  1503. */
  1504. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  1505. /* Search for mapped node by target ID */
  1506. match = 0;
  1507. spin_lock_irq(shost->host_lock);
  1508. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  1509. if (!NLP_CHK_NODE_ACT(ndlp))
  1510. continue;
  1511. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  1512. ndlp->nlp_sid == i &&
  1513. ndlp->rport) {
  1514. match = 1;
  1515. break;
  1516. }
  1517. }
  1518. spin_unlock_irq(shost->host_lock);
  1519. if (!match)
  1520. continue;
  1521. lpfc_cmd = lpfc_get_scsi_buf(phba);
  1522. if (lpfc_cmd) {
  1523. lpfc_cmd->timeout = 60;
  1524. status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
  1525. cmnd->device->lun,
  1526. ndlp->rport->dd_data);
  1527. if (status != TIMEOUT_ERROR)
  1528. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1529. }
  1530. if (!lpfc_cmd || status != SUCCESS) {
  1531. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1532. "0700 Bus Reset on target %d failed\n",
  1533. i);
  1534. ret = FAILED;
  1535. }
  1536. }
  1537. /*
  1538. * All outstanding txcmplq I/Os should have been aborted by
  1539. * the targets. Unfortunately, some targets do not abide by
  1540. * this forcing the driver to double check.
  1541. */
  1542. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  1543. if (cnt)
  1544. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  1545. 0, 0, LPFC_CTX_HOST);
  1546. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  1547. while (time_after(later, jiffies) && cnt) {
  1548. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  1549. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  1550. }
  1551. if (cnt) {
  1552. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1553. "0715 Bus Reset I/O flush failure: "
  1554. "cnt x%x left x%x\n", cnt, i);
  1555. ret = FAILED;
  1556. }
  1557. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1558. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  1559. return ret;
  1560. }
  1561. static int
  1562. lpfc_slave_alloc(struct scsi_device *sdev)
  1563. {
  1564. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  1565. struct lpfc_hba *phba = vport->phba;
  1566. struct lpfc_scsi_buf *scsi_buf = NULL;
  1567. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1568. uint32_t total = 0, i;
  1569. uint32_t num_to_alloc = 0;
  1570. unsigned long flags;
  1571. if (!rport || fc_remote_port_chkready(rport))
  1572. return -ENXIO;
  1573. sdev->hostdata = rport->dd_data;
  1574. /*
  1575. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  1576. * available list of scsi buffers. Don't allocate more than the
  1577. * HBA limit conveyed to the midlayer via the host structure. The
  1578. * formula accounts for the lun_queue_depth + error handlers + 1
  1579. * extra. This list of scsi bufs exists for the lifetime of the driver.
  1580. */
  1581. total = phba->total_scsi_bufs;
  1582. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  1583. /* Allow some exchanges to be available always to complete discovery */
  1584. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  1585. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1586. "0704 At limitation of %d preallocated "
  1587. "command buffers\n", total);
  1588. return 0;
  1589. /* Allow some exchanges to be available always to complete discovery */
  1590. } else if (total + num_to_alloc >
  1591. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  1592. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1593. "0705 Allocation request of %d "
  1594. "command buffers will exceed max of %d. "
  1595. "Reducing allocation request to %d.\n",
  1596. num_to_alloc, phba->cfg_hba_queue_depth,
  1597. (phba->cfg_hba_queue_depth - total));
  1598. num_to_alloc = phba->cfg_hba_queue_depth - total;
  1599. }
  1600. for (i = 0; i < num_to_alloc; i++) {
  1601. scsi_buf = lpfc_new_scsi_buf(vport);
  1602. if (!scsi_buf) {
  1603. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  1604. "0706 Failed to allocate "
  1605. "command buffer\n");
  1606. break;
  1607. }
  1608. spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
  1609. phba->total_scsi_bufs++;
  1610. list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
  1611. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
  1612. }
  1613. return 0;
  1614. }
  1615. static int
  1616. lpfc_slave_configure(struct scsi_device *sdev)
  1617. {
  1618. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  1619. struct lpfc_hba *phba = vport->phba;
  1620. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  1621. if (sdev->tagged_supported)
  1622. scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
  1623. else
  1624. scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
  1625. /*
  1626. * Initialize the fc transport attributes for the target
  1627. * containing this scsi device. Also note that the driver's
  1628. * target pointer is stored in the starget_data for the
  1629. * driver's sysfs entry point functions.
  1630. */
  1631. rport->dev_loss_tmo = vport->cfg_devloss_tmo;
  1632. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1633. lpfc_sli_poll_fcp_ring(phba);
  1634. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  1635. lpfc_poll_rearm_timer(phba);
  1636. }
  1637. return 0;
  1638. }
  1639. static void
  1640. lpfc_slave_destroy(struct scsi_device *sdev)
  1641. {
  1642. sdev->hostdata = NULL;
  1643. return;
  1644. }
  1645. struct scsi_host_template lpfc_template = {
  1646. .module = THIS_MODULE,
  1647. .name = LPFC_DRIVER_NAME,
  1648. .info = lpfc_info,
  1649. .queuecommand = lpfc_queuecommand,
  1650. .eh_abort_handler = lpfc_abort_handler,
  1651. .eh_device_reset_handler= lpfc_device_reset_handler,
  1652. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  1653. .slave_alloc = lpfc_slave_alloc,
  1654. .slave_configure = lpfc_slave_configure,
  1655. .slave_destroy = lpfc_slave_destroy,
  1656. .scan_finished = lpfc_scan_finished,
  1657. .this_id = -1,
  1658. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  1659. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1660. .use_clustering = ENABLE_CLUSTERING,
  1661. .shost_attrs = lpfc_hba_attrs,
  1662. .max_sectors = 0xFFFF,
  1663. };
  1664. struct scsi_host_template lpfc_vport_template = {
  1665. .module = THIS_MODULE,
  1666. .name = LPFC_DRIVER_NAME,
  1667. .info = lpfc_info,
  1668. .queuecommand = lpfc_queuecommand,
  1669. .eh_abort_handler = lpfc_abort_handler,
  1670. .eh_device_reset_handler= lpfc_device_reset_handler,
  1671. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  1672. .slave_alloc = lpfc_slave_alloc,
  1673. .slave_configure = lpfc_slave_configure,
  1674. .slave_destroy = lpfc_slave_destroy,
  1675. .scan_finished = lpfc_scan_finished,
  1676. .this_id = -1,
  1677. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  1678. .cmd_per_lun = LPFC_CMD_PER_LUN,
  1679. .use_clustering = ENABLE_CLUSTERING,
  1680. .shost_attrs = lpfc_vport_attrs,
  1681. .max_sectors = 0xFFFF,
  1682. };