lpfc_scsi.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972
  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 <asm/unaligned.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_device.h>
  27. #include <scsi/scsi_eh.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include "lpfc_version.h"
  32. #include "lpfc_hw.h"
  33. #include "lpfc_sli.h"
  34. #include "lpfc_nl.h"
  35. #include "lpfc_disc.h"
  36. #include "lpfc_scsi.h"
  37. #include "lpfc.h"
  38. #include "lpfc_logmsg.h"
  39. #include "lpfc_crtn.h"
  40. #include "lpfc_vport.h"
  41. #define LPFC_RESET_WAIT 2
  42. #define LPFC_ABORT_WAIT 2
  43. int _dump_buf_done;
  44. static char *dif_op_str[] = {
  45. "SCSI_PROT_NORMAL",
  46. "SCSI_PROT_READ_INSERT",
  47. "SCSI_PROT_WRITE_STRIP",
  48. "SCSI_PROT_READ_STRIP",
  49. "SCSI_PROT_WRITE_INSERT",
  50. "SCSI_PROT_READ_PASS",
  51. "SCSI_PROT_WRITE_PASS",
  52. "SCSI_PROT_READ_CONVERT",
  53. "SCSI_PROT_WRITE_CONVERT"
  54. };
  55. static void
  56. lpfc_debug_save_data(struct scsi_cmnd *cmnd)
  57. {
  58. void *src, *dst;
  59. struct scatterlist *sgde = scsi_sglist(cmnd);
  60. if (!_dump_buf_data) {
  61. printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
  62. __func__);
  63. return;
  64. }
  65. if (!sgde) {
  66. printk(KERN_ERR "BLKGRD ERROR: data scatterlist is null\n");
  67. return;
  68. }
  69. dst = (void *) _dump_buf_data;
  70. while (sgde) {
  71. src = sg_virt(sgde);
  72. memcpy(dst, src, sgde->length);
  73. dst += sgde->length;
  74. sgde = sg_next(sgde);
  75. }
  76. }
  77. static void
  78. lpfc_debug_save_dif(struct scsi_cmnd *cmnd)
  79. {
  80. void *src, *dst;
  81. struct scatterlist *sgde = scsi_prot_sglist(cmnd);
  82. if (!_dump_buf_dif) {
  83. printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
  84. __func__);
  85. return;
  86. }
  87. if (!sgde) {
  88. printk(KERN_ERR "BLKGRD ERROR: prot scatterlist is null\n");
  89. return;
  90. }
  91. dst = _dump_buf_dif;
  92. while (sgde) {
  93. src = sg_virt(sgde);
  94. memcpy(dst, src, sgde->length);
  95. dst += sgde->length;
  96. sgde = sg_next(sgde);
  97. }
  98. }
  99. /**
  100. * lpfc_update_stats - Update statistical data for the command completion
  101. * @phba: Pointer to HBA object.
  102. * @lpfc_cmd: lpfc scsi command object pointer.
  103. *
  104. * This function is called when there is a command completion and this
  105. * function updates the statistical data for the command completion.
  106. **/
  107. static void
  108. lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  109. {
  110. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  111. struct lpfc_nodelist *pnode = rdata->pnode;
  112. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  113. unsigned long flags;
  114. struct Scsi_Host *shost = cmd->device->host;
  115. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  116. unsigned long latency;
  117. int i;
  118. if (cmd->result)
  119. return;
  120. latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
  121. spin_lock_irqsave(shost->host_lock, flags);
  122. if (!vport->stat_data_enabled ||
  123. vport->stat_data_blocked ||
  124. !pnode->lat_data ||
  125. (phba->bucket_type == LPFC_NO_BUCKET)) {
  126. spin_unlock_irqrestore(shost->host_lock, flags);
  127. return;
  128. }
  129. if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
  130. i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
  131. phba->bucket_step;
  132. /* check array subscript bounds */
  133. if (i < 0)
  134. i = 0;
  135. else if (i >= LPFC_MAX_BUCKET_COUNT)
  136. i = LPFC_MAX_BUCKET_COUNT - 1;
  137. } else {
  138. for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
  139. if (latency <= (phba->bucket_base +
  140. ((1<<i)*phba->bucket_step)))
  141. break;
  142. }
  143. pnode->lat_data[i].cmd_count++;
  144. spin_unlock_irqrestore(shost->host_lock, flags);
  145. }
  146. /**
  147. * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
  148. * @phba: Pointer to HBA context object.
  149. * @vport: Pointer to vport object.
  150. * @ndlp: Pointer to FC node associated with the target.
  151. * @lun: Lun number of the scsi device.
  152. * @old_val: Old value of the queue depth.
  153. * @new_val: New value of the queue depth.
  154. *
  155. * This function sends an event to the mgmt application indicating
  156. * there is a change in the scsi device queue depth.
  157. **/
  158. static void
  159. lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
  160. struct lpfc_vport *vport,
  161. struct lpfc_nodelist *ndlp,
  162. uint32_t lun,
  163. uint32_t old_val,
  164. uint32_t new_val)
  165. {
  166. struct lpfc_fast_path_event *fast_path_evt;
  167. unsigned long flags;
  168. fast_path_evt = lpfc_alloc_fast_evt(phba);
  169. if (!fast_path_evt)
  170. return;
  171. fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
  172. FC_REG_SCSI_EVENT;
  173. fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
  174. LPFC_EVENT_VARQUEDEPTH;
  175. /* Report all luns with change in queue depth */
  176. fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
  177. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  178. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
  179. &ndlp->nlp_portname, sizeof(struct lpfc_name));
  180. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
  181. &ndlp->nlp_nodename, sizeof(struct lpfc_name));
  182. }
  183. fast_path_evt->un.queue_depth_evt.oldval = old_val;
  184. fast_path_evt->un.queue_depth_evt.newval = new_val;
  185. fast_path_evt->vport = vport;
  186. fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
  187. spin_lock_irqsave(&phba->hbalock, flags);
  188. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  189. spin_unlock_irqrestore(&phba->hbalock, flags);
  190. lpfc_worker_wake_up(phba);
  191. return;
  192. }
  193. /**
  194. * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
  195. * @phba: The Hba for which this call is being executed.
  196. *
  197. * This routine is called when there is resource error in driver or firmware.
  198. * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
  199. * posts at most 1 event each second. This routine wakes up worker thread of
  200. * @phba to process WORKER_RAM_DOWN_EVENT event.
  201. *
  202. * This routine should be called with no lock held.
  203. **/
  204. void
  205. lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
  206. {
  207. unsigned long flags;
  208. uint32_t evt_posted;
  209. spin_lock_irqsave(&phba->hbalock, flags);
  210. atomic_inc(&phba->num_rsrc_err);
  211. phba->last_rsrc_error_time = jiffies;
  212. if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
  213. spin_unlock_irqrestore(&phba->hbalock, flags);
  214. return;
  215. }
  216. phba->last_ramp_down_time = jiffies;
  217. spin_unlock_irqrestore(&phba->hbalock, flags);
  218. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  219. evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
  220. if (!evt_posted)
  221. phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
  222. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  223. if (!evt_posted)
  224. lpfc_worker_wake_up(phba);
  225. return;
  226. }
  227. /**
  228. * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
  229. * @phba: The Hba for which this call is being executed.
  230. *
  231. * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
  232. * post at most 1 event every 5 minute after last_ramp_up_time or
  233. * last_rsrc_error_time. This routine wakes up worker thread of @phba
  234. * to process WORKER_RAM_DOWN_EVENT event.
  235. *
  236. * This routine should be called with no lock held.
  237. **/
  238. static inline void
  239. lpfc_rampup_queue_depth(struct lpfc_vport *vport,
  240. struct scsi_device *sdev)
  241. {
  242. unsigned long flags;
  243. struct lpfc_hba *phba = vport->phba;
  244. uint32_t evt_posted;
  245. atomic_inc(&phba->num_cmd_success);
  246. if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
  247. return;
  248. spin_lock_irqsave(&phba->hbalock, flags);
  249. if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
  250. ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
  251. spin_unlock_irqrestore(&phba->hbalock, flags);
  252. return;
  253. }
  254. phba->last_ramp_up_time = jiffies;
  255. spin_unlock_irqrestore(&phba->hbalock, flags);
  256. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  257. evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
  258. if (!evt_posted)
  259. phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
  260. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  261. if (!evt_posted)
  262. lpfc_worker_wake_up(phba);
  263. return;
  264. }
  265. /**
  266. * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
  267. * @phba: The Hba for which this call is being executed.
  268. *
  269. * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
  270. * thread.This routine reduces queue depth for all scsi device on each vport
  271. * associated with @phba.
  272. **/
  273. void
  274. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  275. {
  276. struct lpfc_vport **vports;
  277. struct Scsi_Host *shost;
  278. struct scsi_device *sdev;
  279. unsigned long new_queue_depth, old_queue_depth;
  280. unsigned long num_rsrc_err, num_cmd_success;
  281. int i;
  282. struct lpfc_rport_data *rdata;
  283. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  284. num_cmd_success = atomic_read(&phba->num_cmd_success);
  285. vports = lpfc_create_vport_work_array(phba);
  286. if (vports != NULL)
  287. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  288. shost = lpfc_shost_from_vport(vports[i]);
  289. shost_for_each_device(sdev, shost) {
  290. new_queue_depth =
  291. sdev->queue_depth * num_rsrc_err /
  292. (num_rsrc_err + num_cmd_success);
  293. if (!new_queue_depth)
  294. new_queue_depth = sdev->queue_depth - 1;
  295. else
  296. new_queue_depth = sdev->queue_depth -
  297. new_queue_depth;
  298. old_queue_depth = sdev->queue_depth;
  299. if (sdev->ordered_tags)
  300. scsi_adjust_queue_depth(sdev,
  301. MSG_ORDERED_TAG,
  302. new_queue_depth);
  303. else
  304. scsi_adjust_queue_depth(sdev,
  305. MSG_SIMPLE_TAG,
  306. new_queue_depth);
  307. rdata = sdev->hostdata;
  308. if (rdata)
  309. lpfc_send_sdev_queuedepth_change_event(
  310. phba, vports[i],
  311. rdata->pnode,
  312. sdev->lun, old_queue_depth,
  313. new_queue_depth);
  314. }
  315. }
  316. lpfc_destroy_vport_work_array(phba, vports);
  317. atomic_set(&phba->num_rsrc_err, 0);
  318. atomic_set(&phba->num_cmd_success, 0);
  319. }
  320. /**
  321. * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
  322. * @phba: The Hba for which this call is being executed.
  323. *
  324. * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
  325. * thread.This routine increases queue depth for all scsi device on each vport
  326. * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
  327. * num_cmd_success to zero.
  328. **/
  329. void
  330. lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
  331. {
  332. struct lpfc_vport **vports;
  333. struct Scsi_Host *shost;
  334. struct scsi_device *sdev;
  335. int i;
  336. struct lpfc_rport_data *rdata;
  337. vports = lpfc_create_vport_work_array(phba);
  338. if (vports != NULL)
  339. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  340. shost = lpfc_shost_from_vport(vports[i]);
  341. shost_for_each_device(sdev, shost) {
  342. if (vports[i]->cfg_lun_queue_depth <=
  343. sdev->queue_depth)
  344. continue;
  345. if (sdev->ordered_tags)
  346. scsi_adjust_queue_depth(sdev,
  347. MSG_ORDERED_TAG,
  348. sdev->queue_depth+1);
  349. else
  350. scsi_adjust_queue_depth(sdev,
  351. MSG_SIMPLE_TAG,
  352. sdev->queue_depth+1);
  353. rdata = sdev->hostdata;
  354. if (rdata)
  355. lpfc_send_sdev_queuedepth_change_event(
  356. phba, vports[i],
  357. rdata->pnode,
  358. sdev->lun,
  359. sdev->queue_depth - 1,
  360. sdev->queue_depth);
  361. }
  362. }
  363. lpfc_destroy_vport_work_array(phba, vports);
  364. atomic_set(&phba->num_rsrc_err, 0);
  365. atomic_set(&phba->num_cmd_success, 0);
  366. }
  367. /**
  368. * lpfc_scsi_dev_block - set all scsi hosts to block state
  369. * @phba: Pointer to HBA context object.
  370. *
  371. * This function walks vport list and set each SCSI host to block state
  372. * by invoking fc_remote_port_delete() routine. This function is invoked
  373. * with EEH when device's PCI slot has been permanently disabled.
  374. **/
  375. void
  376. lpfc_scsi_dev_block(struct lpfc_hba *phba)
  377. {
  378. struct lpfc_vport **vports;
  379. struct Scsi_Host *shost;
  380. struct scsi_device *sdev;
  381. struct fc_rport *rport;
  382. int i;
  383. vports = lpfc_create_vport_work_array(phba);
  384. if (vports != NULL)
  385. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  386. shost = lpfc_shost_from_vport(vports[i]);
  387. shost_for_each_device(sdev, shost) {
  388. rport = starget_to_rport(scsi_target(sdev));
  389. fc_remote_port_delete(rport);
  390. }
  391. }
  392. lpfc_destroy_vport_work_array(phba, vports);
  393. }
  394. /**
  395. * lpfc_new_scsi_buf - Scsi buffer allocator
  396. * @vport: The virtual port for which this call being executed.
  397. *
  398. * This routine allocates a scsi buffer, which contains all the necessary
  399. * information needed to initiate a SCSI I/O. The non-DMAable buffer region
  400. * contains information to build the IOCB. The DMAable region contains
  401. * memory for the FCP CMND, FCP RSP, and the initial BPL. In addition to
  402. * allocating memory, the FCP CMND and FCP RSP BDEs are setup in the BPL
  403. * and the BPL BDE is setup in the IOCB.
  404. *
  405. * Return codes:
  406. * NULL - Error
  407. * Pointer to lpfc_scsi_buf data structure - Success
  408. **/
  409. static struct lpfc_scsi_buf *
  410. lpfc_new_scsi_buf(struct lpfc_vport *vport)
  411. {
  412. struct lpfc_hba *phba = vport->phba;
  413. struct lpfc_scsi_buf *psb;
  414. struct ulp_bde64 *bpl;
  415. IOCB_t *iocb;
  416. dma_addr_t pdma_phys_fcp_cmd;
  417. dma_addr_t pdma_phys_fcp_rsp;
  418. dma_addr_t pdma_phys_bpl;
  419. uint16_t iotag;
  420. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  421. if (!psb)
  422. return NULL;
  423. /*
  424. * Get memory from the pci pool to map the virt space to pci bus space
  425. * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
  426. * struct fcp_rsp and the number of bde's necessary to support the
  427. * sg_tablesize.
  428. */
  429. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
  430. &psb->dma_handle);
  431. if (!psb->data) {
  432. kfree(psb);
  433. return NULL;
  434. }
  435. /* Initialize virtual ptrs to dma_buf region. */
  436. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  437. /* Allocate iotag for psb->cur_iocbq. */
  438. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  439. if (iotag == 0) {
  440. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  441. psb->data, psb->dma_handle);
  442. kfree (psb);
  443. return NULL;
  444. }
  445. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  446. psb->fcp_cmnd = psb->data;
  447. psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
  448. psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
  449. sizeof(struct fcp_rsp);
  450. /* Initialize local short-hand pointers. */
  451. bpl = psb->fcp_bpl;
  452. pdma_phys_fcp_cmd = psb->dma_handle;
  453. pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
  454. pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
  455. sizeof(struct fcp_rsp);
  456. /*
  457. * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
  458. * list bdes. Initialize the first two and leave the rest for
  459. * queuecommand.
  460. */
  461. bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
  462. bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
  463. bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
  464. bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  465. bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
  466. /* Setup the physical region for the FCP RSP */
  467. bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
  468. bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
  469. bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
  470. bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  471. bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
  472. /*
  473. * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
  474. * initialize it with all known data now.
  475. */
  476. iocb = &psb->cur_iocbq.iocb;
  477. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  478. if ((phba->sli_rev == 3) &&
  479. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  480. /* fill in immediate fcp command BDE */
  481. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
  482. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  483. iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
  484. unsli3.fcp_ext.icd);
  485. iocb->un.fcpi64.bdl.addrHigh = 0;
  486. iocb->ulpBdeCount = 0;
  487. iocb->ulpLe = 0;
  488. /* fill in responce BDE */
  489. iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  490. iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
  491. sizeof(struct fcp_rsp);
  492. iocb->unsli3.fcp_ext.rbde.addrLow =
  493. putPaddrLow(pdma_phys_fcp_rsp);
  494. iocb->unsli3.fcp_ext.rbde.addrHigh =
  495. putPaddrHigh(pdma_phys_fcp_rsp);
  496. } else {
  497. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  498. iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  499. iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_bpl);
  500. iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_bpl);
  501. iocb->ulpBdeCount = 1;
  502. iocb->ulpLe = 1;
  503. }
  504. iocb->ulpClass = CLASS3;
  505. return psb;
  506. }
  507. /**
  508. * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list list of Hba
  509. * @phba: The Hba for which this call is being executed.
  510. *
  511. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  512. * and returns to caller.
  513. *
  514. * Return codes:
  515. * NULL - Error
  516. * Pointer to lpfc_scsi_buf - Success
  517. **/
  518. static struct lpfc_scsi_buf*
  519. lpfc_get_scsi_buf(struct lpfc_hba * phba)
  520. {
  521. struct lpfc_scsi_buf * lpfc_cmd = NULL;
  522. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  523. unsigned long iflag = 0;
  524. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  525. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  526. if (lpfc_cmd) {
  527. lpfc_cmd->seg_cnt = 0;
  528. lpfc_cmd->nonsg_phys = 0;
  529. lpfc_cmd->prot_seg_cnt = 0;
  530. }
  531. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  532. return lpfc_cmd;
  533. }
  534. /**
  535. * lpfc_release_scsi_buf - Return a scsi buffer back to hba's lpfc_scsi_buf_list
  536. * @phba: The Hba for which this call is being executed.
  537. * @psb: The scsi buffer which is being released.
  538. *
  539. * This routine releases @psb scsi buffer by adding it to tail of @phba
  540. * lpfc_scsi_buf_list list.
  541. **/
  542. static void
  543. lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  544. {
  545. unsigned long iflag = 0;
  546. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  547. psb->pCmd = NULL;
  548. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  549. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  550. }
  551. /**
  552. * lpfc_scsi_prep_dma_buf - Routine to do DMA mapping for scsi buffer
  553. * @phba: The Hba for which this call is being executed.
  554. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  555. *
  556. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  557. * field of @lpfc_cmd. This routine scans through sg elements and format the
  558. * bdea. This routine also initializes all IOCB fields which are dependent on
  559. * scsi command request buffer.
  560. *
  561. * Return codes:
  562. * 1 - Error
  563. * 0 - Success
  564. **/
  565. static int
  566. lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  567. {
  568. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  569. struct scatterlist *sgel = NULL;
  570. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  571. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  572. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  573. struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
  574. dma_addr_t physaddr;
  575. uint32_t num_bde = 0;
  576. int nseg, datadir = scsi_cmnd->sc_data_direction;
  577. /*
  578. * There are three possibilities here - use scatter-gather segment, use
  579. * the single mapping, or neither. Start the lpfc command prep by
  580. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  581. * data bde entry.
  582. */
  583. bpl += 2;
  584. if (scsi_sg_count(scsi_cmnd)) {
  585. /*
  586. * The driver stores the segment count returned from pci_map_sg
  587. * because this a count of dma-mappings used to map the use_sg
  588. * pages. They are not guaranteed to be the same for those
  589. * architectures that implement an IOMMU.
  590. */
  591. nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
  592. scsi_sg_count(scsi_cmnd), datadir);
  593. if (unlikely(!nseg))
  594. return 1;
  595. lpfc_cmd->seg_cnt = nseg;
  596. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  597. printk(KERN_ERR "%s: Too many sg segments from "
  598. "dma_map_sg. Config %d, seg_cnt %d\n",
  599. __func__, phba->cfg_sg_seg_cnt,
  600. lpfc_cmd->seg_cnt);
  601. scsi_dma_unmap(scsi_cmnd);
  602. return 1;
  603. }
  604. /*
  605. * The driver established a maximum scatter-gather segment count
  606. * during probe that limits the number of sg elements in any
  607. * single scsi command. Just run through the seg_cnt and format
  608. * the bde's.
  609. * When using SLI-3 the driver will try to fit all the BDEs into
  610. * the IOCB. If it can't then the BDEs get added to a BPL as it
  611. * does for SLI-2 mode.
  612. */
  613. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  614. physaddr = sg_dma_address(sgel);
  615. if (phba->sli_rev == 3 &&
  616. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  617. nseg <= LPFC_EXT_DATA_BDE_COUNT) {
  618. data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  619. data_bde->tus.f.bdeSize = sg_dma_len(sgel);
  620. data_bde->addrLow = putPaddrLow(physaddr);
  621. data_bde->addrHigh = putPaddrHigh(physaddr);
  622. data_bde++;
  623. } else {
  624. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  625. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  626. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  627. bpl->addrLow =
  628. le32_to_cpu(putPaddrLow(physaddr));
  629. bpl->addrHigh =
  630. le32_to_cpu(putPaddrHigh(physaddr));
  631. bpl++;
  632. }
  633. }
  634. }
  635. /*
  636. * Finish initializing those IOCB fields that are dependent on the
  637. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  638. * explicitly reinitialized and for SLI-3 the extended bde count is
  639. * explicitly reinitialized since all iocb memory resources are reused.
  640. */
  641. if (phba->sli_rev == 3 &&
  642. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  643. if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
  644. /*
  645. * The extended IOCB format can only fit 3 BDE or a BPL.
  646. * This I/O has more than 3 BDE so the 1st data bde will
  647. * be a BPL that is filled in here.
  648. */
  649. physaddr = lpfc_cmd->dma_handle;
  650. data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
  651. data_bde->tus.f.bdeSize = (num_bde *
  652. sizeof(struct ulp_bde64));
  653. physaddr += (sizeof(struct fcp_cmnd) +
  654. sizeof(struct fcp_rsp) +
  655. (2 * sizeof(struct ulp_bde64)));
  656. data_bde->addrHigh = putPaddrHigh(physaddr);
  657. data_bde->addrLow = putPaddrLow(physaddr);
  658. /* ebde count includes the responce bde and data bpl */
  659. iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
  660. } else {
  661. /* ebde count includes the responce bde and data bdes */
  662. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  663. }
  664. } else {
  665. iocb_cmd->un.fcpi64.bdl.bdeSize =
  666. ((num_bde + 2) * sizeof(struct ulp_bde64));
  667. }
  668. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  669. /*
  670. * Due to difference in data length between DIF/non-DIF paths,
  671. * we need to set word 4 of IOCB here
  672. */
  673. iocb_cmd->un.fcpi.fcpi_parm = le32_to_cpu(scsi_bufflen(scsi_cmnd));
  674. return 0;
  675. }
  676. /*
  677. * Given a scsi cmnd, determine the BlockGuard profile to be used
  678. * with the cmd
  679. */
  680. static int
  681. lpfc_sc_to_sli_prof(struct scsi_cmnd *sc)
  682. {
  683. uint8_t guard_type = scsi_host_get_guard(sc->device->host);
  684. uint8_t ret_prof = LPFC_PROF_INVALID;
  685. if (guard_type == SHOST_DIX_GUARD_IP) {
  686. switch (scsi_get_prot_op(sc)) {
  687. case SCSI_PROT_READ_INSERT:
  688. case SCSI_PROT_WRITE_STRIP:
  689. ret_prof = LPFC_PROF_AST2;
  690. break;
  691. case SCSI_PROT_READ_STRIP:
  692. case SCSI_PROT_WRITE_INSERT:
  693. ret_prof = LPFC_PROF_A1;
  694. break;
  695. case SCSI_PROT_READ_CONVERT:
  696. case SCSI_PROT_WRITE_CONVERT:
  697. ret_prof = LPFC_PROF_AST1;
  698. break;
  699. case SCSI_PROT_READ_PASS:
  700. case SCSI_PROT_WRITE_PASS:
  701. case SCSI_PROT_NORMAL:
  702. default:
  703. printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
  704. scsi_get_prot_op(sc), guard_type);
  705. break;
  706. }
  707. } else if (guard_type == SHOST_DIX_GUARD_CRC) {
  708. switch (scsi_get_prot_op(sc)) {
  709. case SCSI_PROT_READ_STRIP:
  710. case SCSI_PROT_WRITE_INSERT:
  711. ret_prof = LPFC_PROF_A1;
  712. break;
  713. case SCSI_PROT_READ_PASS:
  714. case SCSI_PROT_WRITE_PASS:
  715. ret_prof = LPFC_PROF_C1;
  716. break;
  717. case SCSI_PROT_READ_CONVERT:
  718. case SCSI_PROT_WRITE_CONVERT:
  719. case SCSI_PROT_READ_INSERT:
  720. case SCSI_PROT_WRITE_STRIP:
  721. case SCSI_PROT_NORMAL:
  722. default:
  723. printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
  724. scsi_get_prot_op(sc), guard_type);
  725. break;
  726. }
  727. } else {
  728. /* unsupported format */
  729. BUG();
  730. }
  731. return ret_prof;
  732. }
  733. struct scsi_dif_tuple {
  734. __be16 guard_tag; /* Checksum */
  735. __be16 app_tag; /* Opaque storage */
  736. __be32 ref_tag; /* Target LBA or indirect LBA */
  737. };
  738. static inline unsigned
  739. lpfc_cmd_blksize(struct scsi_cmnd *sc)
  740. {
  741. return sc->device->sector_size;
  742. }
  743. /**
  744. * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
  745. * @sc: in: SCSI command
  746. * @apptagmask: out: app tag mask
  747. * @apptagval: out: app tag value
  748. * @reftag: out: ref tag (reference tag)
  749. *
  750. * Description:
  751. * Extract DIF paramters from the command if possible. Otherwise,
  752. * use default paratmers.
  753. *
  754. **/
  755. static inline void
  756. lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
  757. uint16_t *apptagval, uint32_t *reftag)
  758. {
  759. struct scsi_dif_tuple *spt;
  760. unsigned char op = scsi_get_prot_op(sc);
  761. unsigned int protcnt = scsi_prot_sg_count(sc);
  762. static int cnt;
  763. if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
  764. op == SCSI_PROT_WRITE_PASS ||
  765. op == SCSI_PROT_WRITE_CONVERT)) {
  766. cnt++;
  767. spt = page_address(sg_page(scsi_prot_sglist(sc))) +
  768. scsi_prot_sglist(sc)[0].offset;
  769. *apptagmask = 0;
  770. *apptagval = 0;
  771. *reftag = cpu_to_be32(spt->ref_tag);
  772. } else {
  773. /* SBC defines ref tag to be lower 32bits of LBA */
  774. *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
  775. *apptagmask = 0;
  776. *apptagval = 0;
  777. }
  778. }
  779. /*
  780. * This function sets up buffer list for protection groups of
  781. * type LPFC_PG_TYPE_NO_DIF
  782. *
  783. * This is usually used when the HBA is instructed to generate
  784. * DIFs and insert them into data stream (or strip DIF from
  785. * incoming data stream)
  786. *
  787. * The buffer list consists of just one protection group described
  788. * below:
  789. * +-------------------------+
  790. * start of prot group --> | PDE_1 |
  791. * +-------------------------+
  792. * | Data BDE |
  793. * +-------------------------+
  794. * |more Data BDE's ... (opt)|
  795. * +-------------------------+
  796. *
  797. * @sc: pointer to scsi command we're working on
  798. * @bpl: pointer to buffer list for protection groups
  799. * @datacnt: number of segments of data that have been dma mapped
  800. *
  801. * Note: Data s/g buffers have been dma mapped
  802. */
  803. static int
  804. lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  805. struct ulp_bde64 *bpl, int datasegcnt)
  806. {
  807. struct scatterlist *sgde = NULL; /* s/g data entry */
  808. struct lpfc_pde *pde1 = NULL;
  809. dma_addr_t physaddr;
  810. int i = 0, num_bde = 0;
  811. int datadir = sc->sc_data_direction;
  812. int prof = LPFC_PROF_INVALID;
  813. unsigned blksize;
  814. uint32_t reftag;
  815. uint16_t apptagmask, apptagval;
  816. pde1 = (struct lpfc_pde *) bpl;
  817. prof = lpfc_sc_to_sli_prof(sc);
  818. if (prof == LPFC_PROF_INVALID)
  819. goto out;
  820. /* extract some info from the scsi command for PDE1*/
  821. blksize = lpfc_cmd_blksize(sc);
  822. lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
  823. /* setup PDE1 with what we have */
  824. lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
  825. BG_EC_STOP_ERR);
  826. lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
  827. num_bde++;
  828. bpl++;
  829. /* assumption: caller has already run dma_map_sg on command data */
  830. scsi_for_each_sg(sc, sgde, datasegcnt, i) {
  831. physaddr = sg_dma_address(sgde);
  832. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  833. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  834. bpl->tus.f.bdeSize = sg_dma_len(sgde);
  835. if (datadir == DMA_TO_DEVICE)
  836. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  837. else
  838. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  839. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  840. bpl++;
  841. num_bde++;
  842. }
  843. out:
  844. return num_bde;
  845. }
  846. /*
  847. * This function sets up buffer list for protection groups of
  848. * type LPFC_PG_TYPE_DIF_BUF
  849. *
  850. * This is usually used when DIFs are in their own buffers,
  851. * separate from the data. The HBA can then by instructed
  852. * to place the DIFs in the outgoing stream. For read operations,
  853. * The HBA could extract the DIFs and place it in DIF buffers.
  854. *
  855. * The buffer list for this type consists of one or more of the
  856. * protection groups described below:
  857. * +-------------------------+
  858. * start of first prot group --> | PDE_1 |
  859. * +-------------------------+
  860. * | PDE_3 (Prot BDE) |
  861. * +-------------------------+
  862. * | Data BDE |
  863. * +-------------------------+
  864. * |more Data BDE's ... (opt)|
  865. * +-------------------------+
  866. * start of new prot group --> | PDE_1 |
  867. * +-------------------------+
  868. * | ... |
  869. * +-------------------------+
  870. *
  871. * @sc: pointer to scsi command we're working on
  872. * @bpl: pointer to buffer list for protection groups
  873. * @datacnt: number of segments of data that have been dma mapped
  874. * @protcnt: number of segment of protection data that have been dma mapped
  875. *
  876. * Note: It is assumed that both data and protection s/g buffers have been
  877. * mapped for DMA
  878. */
  879. static int
  880. lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  881. struct ulp_bde64 *bpl, int datacnt, int protcnt)
  882. {
  883. struct scatterlist *sgde = NULL; /* s/g data entry */
  884. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  885. struct lpfc_pde *pde1 = NULL;
  886. struct ulp_bde64 *prot_bde = NULL;
  887. dma_addr_t dataphysaddr, protphysaddr;
  888. unsigned short curr_data = 0, curr_prot = 0;
  889. unsigned int split_offset, protgroup_len;
  890. unsigned int protgrp_blks, protgrp_bytes;
  891. unsigned int remainder, subtotal;
  892. int prof = LPFC_PROF_INVALID;
  893. int datadir = sc->sc_data_direction;
  894. unsigned char pgdone = 0, alldone = 0;
  895. unsigned blksize;
  896. uint32_t reftag;
  897. uint16_t apptagmask, apptagval;
  898. int num_bde = 0;
  899. sgpe = scsi_prot_sglist(sc);
  900. sgde = scsi_sglist(sc);
  901. if (!sgpe || !sgde) {
  902. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  903. "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
  904. sgpe, sgde);
  905. return 0;
  906. }
  907. prof = lpfc_sc_to_sli_prof(sc);
  908. if (prof == LPFC_PROF_INVALID)
  909. goto out;
  910. /* extract some info from the scsi command for PDE1*/
  911. blksize = lpfc_cmd_blksize(sc);
  912. lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
  913. split_offset = 0;
  914. do {
  915. /* setup the first PDE_1 */
  916. pde1 = (struct lpfc_pde *) bpl;
  917. lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
  918. BG_EC_STOP_ERR);
  919. lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
  920. num_bde++;
  921. bpl++;
  922. /* setup the first BDE that points to protection buffer */
  923. prot_bde = (struct ulp_bde64 *) bpl;
  924. protphysaddr = sg_dma_address(sgpe);
  925. prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
  926. prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
  927. protgroup_len = sg_dma_len(sgpe);
  928. /* must be integer multiple of the DIF block length */
  929. BUG_ON(protgroup_len % 8);
  930. protgrp_blks = protgroup_len / 8;
  931. protgrp_bytes = protgrp_blks * blksize;
  932. prot_bde->tus.f.bdeSize = protgroup_len;
  933. if (datadir == DMA_TO_DEVICE)
  934. prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  935. else
  936. prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  937. prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
  938. curr_prot++;
  939. num_bde++;
  940. /* setup BDE's for data blocks associated with DIF data */
  941. pgdone = 0;
  942. subtotal = 0; /* total bytes processed for current prot grp */
  943. while (!pgdone) {
  944. if (!sgde) {
  945. printk(KERN_ERR "%s Invalid data segment\n",
  946. __func__);
  947. return 0;
  948. }
  949. bpl++;
  950. dataphysaddr = sg_dma_address(sgde) + split_offset;
  951. bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
  952. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
  953. remainder = sg_dma_len(sgde) - split_offset;
  954. if ((subtotal + remainder) <= protgrp_bytes) {
  955. /* we can use this whole buffer */
  956. bpl->tus.f.bdeSize = remainder;
  957. split_offset = 0;
  958. if ((subtotal + remainder) == protgrp_bytes)
  959. pgdone = 1;
  960. } else {
  961. /* must split this buffer with next prot grp */
  962. bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
  963. split_offset += bpl->tus.f.bdeSize;
  964. }
  965. subtotal += bpl->tus.f.bdeSize;
  966. if (datadir == DMA_TO_DEVICE)
  967. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  968. else
  969. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  970. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  971. num_bde++;
  972. curr_data++;
  973. if (split_offset)
  974. break;
  975. /* Move to the next s/g segment if possible */
  976. sgde = sg_next(sgde);
  977. }
  978. /* are we done ? */
  979. if (curr_prot == protcnt) {
  980. alldone = 1;
  981. } else if (curr_prot < protcnt) {
  982. /* advance to next prot buffer */
  983. sgpe = sg_next(sgpe);
  984. bpl++;
  985. /* update the reference tag */
  986. reftag += protgrp_blks;
  987. } else {
  988. /* if we're here, we have a bug */
  989. printk(KERN_ERR "BLKGRD: bug in %s\n", __func__);
  990. }
  991. } while (!alldone);
  992. out:
  993. return num_bde;
  994. }
  995. /*
  996. * Given a SCSI command that supports DIF, determine composition of protection
  997. * groups involved in setting up buffer lists
  998. *
  999. * Returns:
  1000. * for DIF (for both read and write)
  1001. * */
  1002. static int
  1003. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  1004. {
  1005. int ret = LPFC_PG_TYPE_INVALID;
  1006. unsigned char op = scsi_get_prot_op(sc);
  1007. switch (op) {
  1008. case SCSI_PROT_READ_STRIP:
  1009. case SCSI_PROT_WRITE_INSERT:
  1010. ret = LPFC_PG_TYPE_NO_DIF;
  1011. break;
  1012. case SCSI_PROT_READ_INSERT:
  1013. case SCSI_PROT_WRITE_STRIP:
  1014. case SCSI_PROT_READ_PASS:
  1015. case SCSI_PROT_WRITE_PASS:
  1016. case SCSI_PROT_WRITE_CONVERT:
  1017. case SCSI_PROT_READ_CONVERT:
  1018. ret = LPFC_PG_TYPE_DIF_BUF;
  1019. break;
  1020. default:
  1021. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1022. "9021 Unsupported protection op:%d\n", op);
  1023. break;
  1024. }
  1025. return ret;
  1026. }
  1027. /*
  1028. * This is the protection/DIF aware version of
  1029. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  1030. * two functions eventually, but for now, it's here
  1031. */
  1032. static int
  1033. lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
  1034. struct lpfc_scsi_buf *lpfc_cmd)
  1035. {
  1036. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1037. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1038. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  1039. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1040. uint32_t num_bde = 0;
  1041. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  1042. int prot_group_type = 0;
  1043. int diflen, fcpdl;
  1044. unsigned blksize;
  1045. /*
  1046. * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
  1047. * fcp_rsp regions to the first data bde entry
  1048. */
  1049. bpl += 2;
  1050. if (scsi_sg_count(scsi_cmnd)) {
  1051. /*
  1052. * The driver stores the segment count returned from pci_map_sg
  1053. * because this a count of dma-mappings used to map the use_sg
  1054. * pages. They are not guaranteed to be the same for those
  1055. * architectures that implement an IOMMU.
  1056. */
  1057. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  1058. scsi_sglist(scsi_cmnd),
  1059. scsi_sg_count(scsi_cmnd), datadir);
  1060. if (unlikely(!datasegcnt))
  1061. return 1;
  1062. lpfc_cmd->seg_cnt = datasegcnt;
  1063. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1064. printk(KERN_ERR "%s: Too many sg segments from "
  1065. "dma_map_sg. Config %d, seg_cnt %d\n",
  1066. __func__, phba->cfg_sg_seg_cnt,
  1067. lpfc_cmd->seg_cnt);
  1068. scsi_dma_unmap(scsi_cmnd);
  1069. return 1;
  1070. }
  1071. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  1072. switch (prot_group_type) {
  1073. case LPFC_PG_TYPE_NO_DIF:
  1074. num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
  1075. datasegcnt);
  1076. /* we shoud have 2 or more entries in buffer list */
  1077. if (num_bde < 2)
  1078. goto err;
  1079. break;
  1080. case LPFC_PG_TYPE_DIF_BUF:{
  1081. /*
  1082. * This type indicates that protection buffers are
  1083. * passed to the driver, so that needs to be prepared
  1084. * for DMA
  1085. */
  1086. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  1087. scsi_prot_sglist(scsi_cmnd),
  1088. scsi_prot_sg_count(scsi_cmnd), datadir);
  1089. if (unlikely(!protsegcnt)) {
  1090. scsi_dma_unmap(scsi_cmnd);
  1091. return 1;
  1092. }
  1093. lpfc_cmd->prot_seg_cnt = protsegcnt;
  1094. if (lpfc_cmd->prot_seg_cnt
  1095. > phba->cfg_prot_sg_seg_cnt) {
  1096. printk(KERN_ERR "%s: Too many prot sg segments "
  1097. "from dma_map_sg. Config %d,"
  1098. "prot_seg_cnt %d\n", __func__,
  1099. phba->cfg_prot_sg_seg_cnt,
  1100. lpfc_cmd->prot_seg_cnt);
  1101. dma_unmap_sg(&phba->pcidev->dev,
  1102. scsi_prot_sglist(scsi_cmnd),
  1103. scsi_prot_sg_count(scsi_cmnd),
  1104. datadir);
  1105. scsi_dma_unmap(scsi_cmnd);
  1106. return 1;
  1107. }
  1108. num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
  1109. datasegcnt, protsegcnt);
  1110. /* we shoud have 3 or more entries in buffer list */
  1111. if (num_bde < 3)
  1112. goto err;
  1113. break;
  1114. }
  1115. case LPFC_PG_TYPE_INVALID:
  1116. default:
  1117. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1118. "9022 Unexpected protection group %i\n",
  1119. prot_group_type);
  1120. return 1;
  1121. }
  1122. }
  1123. /*
  1124. * Finish initializing those IOCB fields that are dependent on the
  1125. * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
  1126. * reinitialized since all iocb memory resources are used many times
  1127. * for transmit, receive, and continuation bpl's.
  1128. */
  1129. iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  1130. iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
  1131. iocb_cmd->ulpBdeCount = 1;
  1132. iocb_cmd->ulpLe = 1;
  1133. fcpdl = scsi_bufflen(scsi_cmnd);
  1134. if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
  1135. /*
  1136. * We are in DIF Type 1 mode
  1137. * Every data block has a 8 byte DIF (trailer)
  1138. * attached to it. Must ajust FCP data length
  1139. */
  1140. blksize = lpfc_cmd_blksize(scsi_cmnd);
  1141. diflen = (fcpdl / blksize) * 8;
  1142. fcpdl += diflen;
  1143. }
  1144. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  1145. /*
  1146. * Due to difference in data length between DIF/non-DIF paths,
  1147. * we need to set word 4 of IOCB here
  1148. */
  1149. iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
  1150. return 0;
  1151. err:
  1152. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1153. "9023 Could not setup all needed BDE's"
  1154. "prot_group_type=%d, num_bde=%d\n",
  1155. prot_group_type, num_bde);
  1156. return 1;
  1157. }
  1158. /*
  1159. * This function checks for BlockGuard errors detected by
  1160. * the HBA. In case of errors, the ASC/ASCQ fields in the
  1161. * sense buffer will be set accordingly, paired with
  1162. * ILLEGAL_REQUEST to signal to the kernel that the HBA
  1163. * detected corruption.
  1164. *
  1165. * Returns:
  1166. * 0 - No error found
  1167. * 1 - BlockGuard error found
  1168. * -1 - Internal error (bad profile, ...etc)
  1169. */
  1170. static int
  1171. lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
  1172. struct lpfc_iocbq *pIocbOut)
  1173. {
  1174. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  1175. struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
  1176. int ret = 0;
  1177. uint32_t bghm = bgf->bghm;
  1178. uint32_t bgstat = bgf->bgstat;
  1179. uint64_t failing_sector = 0;
  1180. printk(KERN_ERR "BG ERROR in cmd 0x%x lba 0x%llx blk cnt 0x%lx "
  1181. "bgstat=0x%x bghm=0x%x\n",
  1182. cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
  1183. cmd->request->nr_sectors, bgstat, bghm);
  1184. spin_lock(&_dump_buf_lock);
  1185. if (!_dump_buf_done) {
  1186. printk(KERN_ERR "Saving Data for %u blocks to debugfs\n",
  1187. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1188. lpfc_debug_save_data(cmd);
  1189. /* If we have a prot sgl, save the DIF buffer */
  1190. if (lpfc_prot_group_type(phba, cmd) ==
  1191. LPFC_PG_TYPE_DIF_BUF) {
  1192. printk(KERN_ERR "Saving DIF for %u blocks to debugfs\n",
  1193. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1194. lpfc_debug_save_dif(cmd);
  1195. }
  1196. _dump_buf_done = 1;
  1197. }
  1198. spin_unlock(&_dump_buf_lock);
  1199. if (lpfc_bgs_get_invalid_prof(bgstat)) {
  1200. cmd->result = ScsiResult(DID_ERROR, 0);
  1201. printk(KERN_ERR "Invalid BlockGuard profile. bgstat:0x%x\n",
  1202. bgstat);
  1203. ret = (-1);
  1204. goto out;
  1205. }
  1206. if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
  1207. cmd->result = ScsiResult(DID_ERROR, 0);
  1208. printk(KERN_ERR "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
  1209. bgstat);
  1210. ret = (-1);
  1211. goto out;
  1212. }
  1213. if (lpfc_bgs_get_guard_err(bgstat)) {
  1214. ret = 1;
  1215. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1216. 0x10, 0x1);
  1217. cmd->result = DRIVER_SENSE << 24
  1218. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1219. phba->bg_guard_err_cnt++;
  1220. printk(KERN_ERR "BLKGRD: guard_tag error\n");
  1221. }
  1222. if (lpfc_bgs_get_reftag_err(bgstat)) {
  1223. ret = 1;
  1224. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1225. 0x10, 0x3);
  1226. cmd->result = DRIVER_SENSE << 24
  1227. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1228. phba->bg_reftag_err_cnt++;
  1229. printk(KERN_ERR "BLKGRD: ref_tag error\n");
  1230. }
  1231. if (lpfc_bgs_get_apptag_err(bgstat)) {
  1232. ret = 1;
  1233. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1234. 0x10, 0x2);
  1235. cmd->result = DRIVER_SENSE << 24
  1236. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1237. phba->bg_apptag_err_cnt++;
  1238. printk(KERN_ERR "BLKGRD: app_tag error\n");
  1239. }
  1240. if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
  1241. /*
  1242. * setup sense data descriptor 0 per SPC-4 as an information
  1243. * field, and put the failing LBA in it
  1244. */
  1245. cmd->sense_buffer[8] = 0; /* Information */
  1246. cmd->sense_buffer[9] = 0xa; /* Add. length */
  1247. bghm /= cmd->device->sector_size;
  1248. failing_sector = scsi_get_lba(cmd);
  1249. failing_sector += bghm;
  1250. put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
  1251. }
  1252. if (!ret) {
  1253. /* No error was reported - problem in FW? */
  1254. cmd->result = ScsiResult(DID_ERROR, 0);
  1255. printk(KERN_ERR "BLKGRD: no errors reported!\n");
  1256. }
  1257. out:
  1258. return ret;
  1259. }
  1260. /**
  1261. * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
  1262. * @phba: Pointer to hba context object.
  1263. * @vport: Pointer to vport object.
  1264. * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
  1265. * @rsp_iocb: Pointer to response iocb object which reported error.
  1266. *
  1267. * This function posts an event when there is a SCSI command reporting
  1268. * error from the scsi device.
  1269. **/
  1270. static void
  1271. lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
  1272. struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
  1273. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  1274. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  1275. uint32_t resp_info = fcprsp->rspStatus2;
  1276. uint32_t scsi_status = fcprsp->rspStatus3;
  1277. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  1278. struct lpfc_fast_path_event *fast_path_evt = NULL;
  1279. struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
  1280. unsigned long flags;
  1281. /* If there is queuefull or busy condition send a scsi event */
  1282. if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
  1283. (cmnd->result == SAM_STAT_BUSY)) {
  1284. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1285. if (!fast_path_evt)
  1286. return;
  1287. fast_path_evt->un.scsi_evt.event_type =
  1288. FC_REG_SCSI_EVENT;
  1289. fast_path_evt->un.scsi_evt.subcategory =
  1290. (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
  1291. LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
  1292. fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
  1293. memcpy(&fast_path_evt->un.scsi_evt.wwpn,
  1294. &pnode->nlp_portname, sizeof(struct lpfc_name));
  1295. memcpy(&fast_path_evt->un.scsi_evt.wwnn,
  1296. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1297. } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
  1298. ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
  1299. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1300. if (!fast_path_evt)
  1301. return;
  1302. fast_path_evt->un.check_cond_evt.scsi_event.event_type =
  1303. FC_REG_SCSI_EVENT;
  1304. fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
  1305. LPFC_EVENT_CHECK_COND;
  1306. fast_path_evt->un.check_cond_evt.scsi_event.lun =
  1307. cmnd->device->lun;
  1308. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
  1309. &pnode->nlp_portname, sizeof(struct lpfc_name));
  1310. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
  1311. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1312. fast_path_evt->un.check_cond_evt.sense_key =
  1313. cmnd->sense_buffer[2] & 0xf;
  1314. fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
  1315. fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
  1316. } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  1317. fcpi_parm &&
  1318. ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
  1319. ((scsi_status == SAM_STAT_GOOD) &&
  1320. !(resp_info & (RESID_UNDER | RESID_OVER))))) {
  1321. /*
  1322. * If status is good or resid does not match with fcp_param and
  1323. * there is valid fcpi_parm, then there is a read_check error
  1324. */
  1325. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1326. if (!fast_path_evt)
  1327. return;
  1328. fast_path_evt->un.read_check_error.header.event_type =
  1329. FC_REG_FABRIC_EVENT;
  1330. fast_path_evt->un.read_check_error.header.subcategory =
  1331. LPFC_EVENT_FCPRDCHKERR;
  1332. memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
  1333. &pnode->nlp_portname, sizeof(struct lpfc_name));
  1334. memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
  1335. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  1336. fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
  1337. fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
  1338. fast_path_evt->un.read_check_error.fcpiparam =
  1339. fcpi_parm;
  1340. } else
  1341. return;
  1342. fast_path_evt->vport = vport;
  1343. spin_lock_irqsave(&phba->hbalock, flags);
  1344. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  1345. spin_unlock_irqrestore(&phba->hbalock, flags);
  1346. lpfc_worker_wake_up(phba);
  1347. return;
  1348. }
  1349. /**
  1350. * lpfc_scsi_unprep_dma_buf - Routine to un-map DMA mapping of scatter gather
  1351. * @phba: The Hba for which this call is being executed.
  1352. * @psb: The scsi buffer which is going to be un-mapped.
  1353. *
  1354. * This routine does DMA un-mapping of scatter gather list of scsi command
  1355. * field of @lpfc_cmd.
  1356. **/
  1357. static void
  1358. lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
  1359. {
  1360. /*
  1361. * There are only two special cases to consider. (1) the scsi command
  1362. * requested scatter-gather usage or (2) the scsi command allocated
  1363. * a request buffer, but did not request use_sg. There is a third
  1364. * case, but it does not require resource deallocation.
  1365. */
  1366. if (psb->seg_cnt > 0)
  1367. scsi_dma_unmap(psb->pCmd);
  1368. if (psb->prot_seg_cnt > 0)
  1369. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
  1370. scsi_prot_sg_count(psb->pCmd),
  1371. psb->pCmd->sc_data_direction);
  1372. }
  1373. /**
  1374. * lpfc_handler_fcp_err - FCP response handler
  1375. * @vport: The virtual port for which this call is being executed.
  1376. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  1377. * @rsp_iocb: The response IOCB which contains FCP error.
  1378. *
  1379. * This routine is called to process response IOCB with status field
  1380. * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
  1381. * based upon SCSI and FCP error.
  1382. **/
  1383. static void
  1384. lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  1385. struct lpfc_iocbq *rsp_iocb)
  1386. {
  1387. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  1388. struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
  1389. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  1390. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  1391. uint32_t resp_info = fcprsp->rspStatus2;
  1392. uint32_t scsi_status = fcprsp->rspStatus3;
  1393. uint32_t *lp;
  1394. uint32_t host_status = DID_OK;
  1395. uint32_t rsplen = 0;
  1396. uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
  1397. /*
  1398. * If this is a task management command, there is no
  1399. * scsi packet associated with this lpfc_cmd. The driver
  1400. * consumes it.
  1401. */
  1402. if (fcpcmd->fcpCntl2) {
  1403. scsi_status = 0;
  1404. goto out;
  1405. }
  1406. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  1407. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  1408. if (snslen > SCSI_SENSE_BUFFERSIZE)
  1409. snslen = SCSI_SENSE_BUFFERSIZE;
  1410. if (resp_info & RSP_LEN_VALID)
  1411. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  1412. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  1413. }
  1414. lp = (uint32_t *)cmnd->sense_buffer;
  1415. if (!scsi_status && (resp_info & RESID_UNDER))
  1416. logit = LOG_FCP;
  1417. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  1418. "9024 FCP command x%x failed: x%x SNS x%x x%x "
  1419. "Data: x%x x%x x%x x%x x%x\n",
  1420. cmnd->cmnd[0], scsi_status,
  1421. be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
  1422. be32_to_cpu(fcprsp->rspResId),
  1423. be32_to_cpu(fcprsp->rspSnsLen),
  1424. be32_to_cpu(fcprsp->rspRspLen),
  1425. fcprsp->rspInfo3);
  1426. if (resp_info & RSP_LEN_VALID) {
  1427. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  1428. if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
  1429. (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
  1430. host_status = DID_ERROR;
  1431. goto out;
  1432. }
  1433. }
  1434. scsi_set_resid(cmnd, 0);
  1435. if (resp_info & RESID_UNDER) {
  1436. scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
  1437. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1438. "9025 FCP Read Underrun, expected %d, "
  1439. "residual %d Data: x%x x%x x%x\n",
  1440. be32_to_cpu(fcpcmd->fcpDl),
  1441. scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
  1442. cmnd->underflow);
  1443. /*
  1444. * If there is an under run check if under run reported by
  1445. * storage array is same as the under run reported by HBA.
  1446. * If this is not same, there is a dropped frame.
  1447. */
  1448. if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  1449. fcpi_parm &&
  1450. (scsi_get_resid(cmnd) != fcpi_parm)) {
  1451. lpfc_printf_vlog(vport, KERN_WARNING,
  1452. LOG_FCP | LOG_FCP_ERROR,
  1453. "9026 FCP Read Check Error "
  1454. "and Underrun Data: x%x x%x x%x x%x\n",
  1455. be32_to_cpu(fcpcmd->fcpDl),
  1456. scsi_get_resid(cmnd), fcpi_parm,
  1457. cmnd->cmnd[0]);
  1458. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  1459. host_status = DID_ERROR;
  1460. }
  1461. /*
  1462. * The cmnd->underflow is the minimum number of bytes that must
  1463. * be transfered for this command. Provided a sense condition
  1464. * is not present, make sure the actual amount transferred is at
  1465. * least the underflow value or fail.
  1466. */
  1467. if (!(resp_info & SNS_LEN_VALID) &&
  1468. (scsi_status == SAM_STAT_GOOD) &&
  1469. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
  1470. < cmnd->underflow)) {
  1471. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1472. "9027 FCP command x%x residual "
  1473. "underrun converted to error "
  1474. "Data: x%x x%x x%x\n",
  1475. cmnd->cmnd[0], scsi_bufflen(cmnd),
  1476. scsi_get_resid(cmnd), cmnd->underflow);
  1477. host_status = DID_ERROR;
  1478. }
  1479. } else if (resp_info & RESID_OVER) {
  1480. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1481. "9028 FCP command x%x residual overrun error. "
  1482. "Data: x%x x%x \n", cmnd->cmnd[0],
  1483. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  1484. host_status = DID_ERROR;
  1485. /*
  1486. * Check SLI validation that all the transfer was actually done
  1487. * (fcpi_parm should be zero). Apply check only to reads.
  1488. */
  1489. } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
  1490. (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
  1491. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
  1492. "9029 FCP Read Check Error Data: "
  1493. "x%x x%x x%x x%x\n",
  1494. be32_to_cpu(fcpcmd->fcpDl),
  1495. be32_to_cpu(fcprsp->rspResId),
  1496. fcpi_parm, cmnd->cmnd[0]);
  1497. host_status = DID_ERROR;
  1498. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  1499. }
  1500. out:
  1501. cmnd->result = ScsiResult(host_status, scsi_status);
  1502. lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
  1503. }
  1504. /**
  1505. * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
  1506. * @phba: The Hba for which this call is being executed.
  1507. * @pIocbIn: The command IOCBQ for the scsi cmnd.
  1508. * @pIocbOut: The response IOCBQ for the scsi cmnd .
  1509. *
  1510. * This routine assigns scsi command result by looking into response IOCB
  1511. * status field appropriately. This routine handles QUEUE FULL condition as
  1512. * well by ramping down device queue depth.
  1513. **/
  1514. static void
  1515. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  1516. struct lpfc_iocbq *pIocbOut)
  1517. {
  1518. struct lpfc_scsi_buf *lpfc_cmd =
  1519. (struct lpfc_scsi_buf *) pIocbIn->context1;
  1520. struct lpfc_vport *vport = pIocbIn->vport;
  1521. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  1522. struct lpfc_nodelist *pnode = rdata->pnode;
  1523. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  1524. int result;
  1525. struct scsi_device *sdev, *tmp_sdev;
  1526. int depth = 0;
  1527. unsigned long flags;
  1528. struct lpfc_fast_path_event *fast_path_evt;
  1529. lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
  1530. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  1531. if (pnode && NLP_CHK_NODE_ACT(pnode))
  1532. atomic_dec(&pnode->cmd_pending);
  1533. if (lpfc_cmd->status) {
  1534. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  1535. (lpfc_cmd->result & IOERR_DRVR_MASK))
  1536. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  1537. else if (lpfc_cmd->status >= IOSTAT_CNT)
  1538. lpfc_cmd->status = IOSTAT_DEFAULT;
  1539. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1540. "9030 FCP cmd x%x failed <%d/%d> "
  1541. "status: x%x result: x%x Data: x%x x%x\n",
  1542. cmd->cmnd[0],
  1543. cmd->device ? cmd->device->id : 0xffff,
  1544. cmd->device ? cmd->device->lun : 0xffff,
  1545. lpfc_cmd->status, lpfc_cmd->result,
  1546. pIocbOut->iocb.ulpContext,
  1547. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  1548. switch (lpfc_cmd->status) {
  1549. case IOSTAT_FCP_RSP_ERROR:
  1550. /* Call FCP RSP handler to determine result */
  1551. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  1552. break;
  1553. case IOSTAT_NPORT_BSY:
  1554. case IOSTAT_FABRIC_BSY:
  1555. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  1556. fast_path_evt = lpfc_alloc_fast_evt(phba);
  1557. if (!fast_path_evt)
  1558. break;
  1559. fast_path_evt->un.fabric_evt.event_type =
  1560. FC_REG_FABRIC_EVENT;
  1561. fast_path_evt->un.fabric_evt.subcategory =
  1562. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  1563. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  1564. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  1565. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  1566. &pnode->nlp_portname,
  1567. sizeof(struct lpfc_name));
  1568. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  1569. &pnode->nlp_nodename,
  1570. sizeof(struct lpfc_name));
  1571. }
  1572. fast_path_evt->vport = vport;
  1573. fast_path_evt->work_evt.evt =
  1574. LPFC_EVT_FASTPATH_MGMT_EVT;
  1575. spin_lock_irqsave(&phba->hbalock, flags);
  1576. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  1577. &phba->work_list);
  1578. spin_unlock_irqrestore(&phba->hbalock, flags);
  1579. lpfc_worker_wake_up(phba);
  1580. break;
  1581. case IOSTAT_LOCAL_REJECT:
  1582. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  1583. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  1584. lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
  1585. cmd->result = ScsiResult(DID_REQUEUE, 0);
  1586. break;
  1587. }
  1588. if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
  1589. lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
  1590. pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
  1591. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  1592. /*
  1593. * This is a response for a BG enabled
  1594. * cmd. Parse BG error
  1595. */
  1596. lpfc_parse_bg_err(phba, lpfc_cmd,
  1597. pIocbOut);
  1598. break;
  1599. } else {
  1600. lpfc_printf_vlog(vport, KERN_WARNING,
  1601. LOG_BG,
  1602. "9031 non-zero BGSTAT "
  1603. "on unprotected cmd");
  1604. }
  1605. }
  1606. /* else: fall through */
  1607. default:
  1608. cmd->result = ScsiResult(DID_ERROR, 0);
  1609. break;
  1610. }
  1611. if (!pnode || !NLP_CHK_NODE_ACT(pnode)
  1612. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  1613. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
  1614. SAM_STAT_BUSY);
  1615. } else {
  1616. cmd->result = ScsiResult(DID_OK, 0);
  1617. }
  1618. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  1619. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  1620. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1621. "0710 Iodone <%d/%d> cmd %p, error "
  1622. "x%x SNS x%x x%x Data: x%x x%x\n",
  1623. cmd->device->id, cmd->device->lun, cmd,
  1624. cmd->result, *lp, *(lp + 3), cmd->retries,
  1625. scsi_get_resid(cmd));
  1626. }
  1627. lpfc_update_stats(phba, lpfc_cmd);
  1628. result = cmd->result;
  1629. sdev = cmd->device;
  1630. if (vport->cfg_max_scsicmpl_time &&
  1631. time_after(jiffies, lpfc_cmd->start_time +
  1632. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  1633. spin_lock_irqsave(sdev->host->host_lock, flags);
  1634. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  1635. if (pnode->cmd_qdepth >
  1636. atomic_read(&pnode->cmd_pending) &&
  1637. (atomic_read(&pnode->cmd_pending) >
  1638. LPFC_MIN_TGT_QDEPTH) &&
  1639. ((cmd->cmnd[0] == READ_10) ||
  1640. (cmd->cmnd[0] == WRITE_10)))
  1641. pnode->cmd_qdepth =
  1642. atomic_read(&pnode->cmd_pending);
  1643. pnode->last_change_time = jiffies;
  1644. }
  1645. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  1646. } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  1647. if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
  1648. time_after(jiffies, pnode->last_change_time +
  1649. msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
  1650. spin_lock_irqsave(sdev->host->host_lock, flags);
  1651. pnode->cmd_qdepth += pnode->cmd_qdepth *
  1652. LPFC_TGTQ_RAMPUP_PCENT / 100;
  1653. if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
  1654. pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
  1655. pnode->last_change_time = jiffies;
  1656. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  1657. }
  1658. }
  1659. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  1660. cmd->scsi_done(cmd);
  1661. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1662. /*
  1663. * If there is a thread waiting for command completion
  1664. * wake up the thread.
  1665. */
  1666. spin_lock_irqsave(sdev->host->host_lock, flags);
  1667. lpfc_cmd->pCmd = NULL;
  1668. if (lpfc_cmd->waitq)
  1669. wake_up(lpfc_cmd->waitq);
  1670. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  1671. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1672. return;
  1673. }
  1674. if (!result)
  1675. lpfc_rampup_queue_depth(vport, sdev);
  1676. if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
  1677. ((jiffies - pnode->last_ramp_up_time) >
  1678. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  1679. ((jiffies - pnode->last_q_full_time) >
  1680. LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
  1681. (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
  1682. shost_for_each_device(tmp_sdev, sdev->host) {
  1683. if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
  1684. if (tmp_sdev->id != sdev->id)
  1685. continue;
  1686. if (tmp_sdev->ordered_tags)
  1687. scsi_adjust_queue_depth(tmp_sdev,
  1688. MSG_ORDERED_TAG,
  1689. tmp_sdev->queue_depth+1);
  1690. else
  1691. scsi_adjust_queue_depth(tmp_sdev,
  1692. MSG_SIMPLE_TAG,
  1693. tmp_sdev->queue_depth+1);
  1694. pnode->last_ramp_up_time = jiffies;
  1695. }
  1696. }
  1697. lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
  1698. 0xFFFFFFFF,
  1699. sdev->queue_depth - 1, sdev->queue_depth);
  1700. }
  1701. /*
  1702. * Check for queue full. If the lun is reporting queue full, then
  1703. * back off the lun queue depth to prevent target overloads.
  1704. */
  1705. if (result == SAM_STAT_TASK_SET_FULL && pnode &&
  1706. NLP_CHK_NODE_ACT(pnode)) {
  1707. pnode->last_q_full_time = jiffies;
  1708. shost_for_each_device(tmp_sdev, sdev->host) {
  1709. if (tmp_sdev->id != sdev->id)
  1710. continue;
  1711. depth = scsi_track_queue_full(tmp_sdev,
  1712. tmp_sdev->queue_depth - 1);
  1713. }
  1714. /*
  1715. * The queue depth cannot be lowered any more.
  1716. * Modify the returned error code to store
  1717. * the final depth value set by
  1718. * scsi_track_queue_full.
  1719. */
  1720. if (depth == -1)
  1721. depth = sdev->host->cmd_per_lun;
  1722. if (depth) {
  1723. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  1724. "0711 detected queue full - lun queue "
  1725. "depth adjusted to %d.\n", depth);
  1726. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  1727. pnode, 0xFFFFFFFF,
  1728. depth+1, depth);
  1729. }
  1730. }
  1731. /*
  1732. * If there is a thread waiting for command completion
  1733. * wake up the thread.
  1734. */
  1735. spin_lock_irqsave(sdev->host->host_lock, flags);
  1736. lpfc_cmd->pCmd = NULL;
  1737. if (lpfc_cmd->waitq)
  1738. wake_up(lpfc_cmd->waitq);
  1739. spin_unlock_irqrestore(sdev->host->host_lock, flags);
  1740. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1741. }
  1742. /**
  1743. * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
  1744. * @data: A pointer to the immediate command data portion of the IOCB.
  1745. * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
  1746. *
  1747. * The routine copies the entire FCP command from @fcp_cmnd to @data while
  1748. * byte swapping the data to big endian format for transmission on the wire.
  1749. **/
  1750. static void
  1751. lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
  1752. {
  1753. int i, j;
  1754. for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
  1755. i += sizeof(uint32_t), j++) {
  1756. ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
  1757. }
  1758. }
  1759. /**
  1760. * lpfc_scsi_prep_cmnd - Routine to convert scsi cmnd to FCP information unit
  1761. * @vport: The virtual port for which this call is being executed.
  1762. * @lpfc_cmd: The scsi command which needs to send.
  1763. * @pnode: Pointer to lpfc_nodelist.
  1764. *
  1765. * This routine initializes fcp_cmnd and iocb data structure from scsi command
  1766. * to transfer.
  1767. **/
  1768. static void
  1769. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  1770. struct lpfc_nodelist *pnode)
  1771. {
  1772. struct lpfc_hba *phba = vport->phba;
  1773. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1774. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1775. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1776. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  1777. int datadir = scsi_cmnd->sc_data_direction;
  1778. char tag[2];
  1779. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  1780. return;
  1781. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  1782. /* clear task management bits */
  1783. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  1784. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  1785. &lpfc_cmd->fcp_cmnd->fcp_lun);
  1786. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  1787. if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
  1788. switch (tag[0]) {
  1789. case HEAD_OF_QUEUE_TAG:
  1790. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  1791. break;
  1792. case ORDERED_QUEUE_TAG:
  1793. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  1794. break;
  1795. default:
  1796. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  1797. break;
  1798. }
  1799. } else
  1800. fcp_cmnd->fcpCntl1 = 0;
  1801. /*
  1802. * There are three possibilities here - use scatter-gather segment, use
  1803. * the single mapping, or neither. Start the lpfc command prep by
  1804. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1805. * data bde entry.
  1806. */
  1807. if (scsi_sg_count(scsi_cmnd)) {
  1808. if (datadir == DMA_TO_DEVICE) {
  1809. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  1810. iocb_cmd->un.fcpi.fcpi_parm = 0;
  1811. iocb_cmd->ulpPU = 0;
  1812. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  1813. phba->fc4OutputRequests++;
  1814. } else {
  1815. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  1816. iocb_cmd->ulpPU = PARM_READ_CHECK;
  1817. fcp_cmnd->fcpCntl3 = READ_DATA;
  1818. phba->fc4InputRequests++;
  1819. }
  1820. } else {
  1821. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  1822. iocb_cmd->un.fcpi.fcpi_parm = 0;
  1823. iocb_cmd->ulpPU = 0;
  1824. fcp_cmnd->fcpCntl3 = 0;
  1825. phba->fc4ControlRequests++;
  1826. }
  1827. if (phba->sli_rev == 3 &&
  1828. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  1829. lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
  1830. /*
  1831. * Finish initializing those IOCB fields that are independent
  1832. * of the scsi_cmnd request_buffer
  1833. */
  1834. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  1835. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  1836. piocbq->iocb.ulpFCP2Rcvy = 1;
  1837. else
  1838. piocbq->iocb.ulpFCP2Rcvy = 0;
  1839. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  1840. piocbq->context1 = lpfc_cmd;
  1841. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  1842. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  1843. piocbq->vport = vport;
  1844. }
  1845. /**
  1846. * lpfc_scsi_prep_task_mgmt_cmnd - Convert scsi TM cmnd to FCP information unit
  1847. * @vport: The virtual port for which this call is being executed.
  1848. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  1849. * @lun: Logical unit number.
  1850. * @task_mgmt_cmd: SCSI task management command.
  1851. *
  1852. * This routine creates FCP information unit corresponding to @task_mgmt_cmd.
  1853. *
  1854. * Return codes:
  1855. * 0 - Error
  1856. * 1 - Success
  1857. **/
  1858. static int
  1859. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  1860. struct lpfc_scsi_buf *lpfc_cmd,
  1861. unsigned int lun,
  1862. uint8_t task_mgmt_cmd)
  1863. {
  1864. struct lpfc_iocbq *piocbq;
  1865. IOCB_t *piocb;
  1866. struct fcp_cmnd *fcp_cmnd;
  1867. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  1868. struct lpfc_nodelist *ndlp = rdata->pnode;
  1869. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
  1870. ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  1871. return 0;
  1872. piocbq = &(lpfc_cmd->cur_iocbq);
  1873. piocbq->vport = vport;
  1874. piocb = &piocbq->iocb;
  1875. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1876. /* Clear out any old data in the FCP command area */
  1877. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  1878. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  1879. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  1880. if (vport->phba->sli_rev == 3 &&
  1881. !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  1882. lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
  1883. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  1884. piocb->ulpContext = ndlp->nlp_rpi;
  1885. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  1886. piocb->ulpFCP2Rcvy = 1;
  1887. }
  1888. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  1889. /* ulpTimeout is only one byte */
  1890. if (lpfc_cmd->timeout > 0xff) {
  1891. /*
  1892. * Do not timeout the command at the firmware level.
  1893. * The driver will provide the timeout mechanism.
  1894. */
  1895. piocb->ulpTimeout = 0;
  1896. } else {
  1897. piocb->ulpTimeout = lpfc_cmd->timeout;
  1898. }
  1899. return 1;
  1900. }
  1901. /**
  1902. * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
  1903. * @phba: The Hba for which this call is being executed.
  1904. * @cmdiocbq: Pointer to lpfc_iocbq data structure.
  1905. * @rspiocbq: Pointer to lpfc_iocbq data structure.
  1906. *
  1907. * This routine is IOCB completion routine for device reset and target reset
  1908. * routine. This routine release scsi buffer associated with lpfc_cmd.
  1909. **/
  1910. static void
  1911. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  1912. struct lpfc_iocbq *cmdiocbq,
  1913. struct lpfc_iocbq *rspiocbq)
  1914. {
  1915. struct lpfc_scsi_buf *lpfc_cmd =
  1916. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  1917. if (lpfc_cmd)
  1918. lpfc_release_scsi_buf(phba, lpfc_cmd);
  1919. return;
  1920. }
  1921. /**
  1922. * lpfc_scsi_tgt_reset - Target reset handler
  1923. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure
  1924. * @vport: The virtual port for which this call is being executed.
  1925. * @tgt_id: Target ID.
  1926. * @lun: Lun number.
  1927. * @rdata: Pointer to lpfc_rport_data.
  1928. *
  1929. * This routine issues a TARGET RESET iocb to reset a target with @tgt_id ID.
  1930. *
  1931. * Return Code:
  1932. * 0x2003 - Error
  1933. * 0x2002 - Success.
  1934. **/
  1935. static int
  1936. lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
  1937. unsigned tgt_id, unsigned int lun,
  1938. struct lpfc_rport_data *rdata)
  1939. {
  1940. struct lpfc_hba *phba = vport->phba;
  1941. struct lpfc_iocbq *iocbq;
  1942. struct lpfc_iocbq *iocbqrsp;
  1943. int ret;
  1944. int status;
  1945. if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
  1946. return FAILED;
  1947. lpfc_cmd->rdata = rdata;
  1948. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
  1949. FCP_TARGET_RESET);
  1950. if (!status)
  1951. return FAILED;
  1952. iocbq = &lpfc_cmd->cur_iocbq;
  1953. iocbqrsp = lpfc_sli_get_iocbq(phba);
  1954. if (!iocbqrsp)
  1955. return FAILED;
  1956. /* Issue Target Reset to TGT <num> */
  1957. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  1958. "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
  1959. tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
  1960. status = lpfc_sli_issue_iocb_wait(phba,
  1961. &phba->sli.ring[phba->sli.fcp_ring],
  1962. iocbq, iocbqrsp, lpfc_cmd->timeout);
  1963. if (status != IOCB_SUCCESS) {
  1964. if (status == IOCB_TIMEDOUT) {
  1965. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  1966. ret = TIMEOUT_ERROR;
  1967. } else
  1968. ret = FAILED;
  1969. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  1970. } else {
  1971. ret = SUCCESS;
  1972. lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
  1973. lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
  1974. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  1975. (lpfc_cmd->result & IOERR_DRVR_MASK))
  1976. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  1977. }
  1978. lpfc_sli_release_iocbq(phba, iocbqrsp);
  1979. return ret;
  1980. }
  1981. /**
  1982. * lpfc_info - Info entry point of scsi_host_template data structure
  1983. * @host: The scsi host for which this call is being executed.
  1984. *
  1985. * This routine provides module information about hba.
  1986. *
  1987. * Reutrn code:
  1988. * Pointer to char - Success.
  1989. **/
  1990. const char *
  1991. lpfc_info(struct Scsi_Host *host)
  1992. {
  1993. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  1994. struct lpfc_hba *phba = vport->phba;
  1995. int len;
  1996. static char lpfcinfobuf[384];
  1997. memset(lpfcinfobuf,0,384);
  1998. if (phba && phba->pcidev){
  1999. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  2000. len = strlen(lpfcinfobuf);
  2001. snprintf(lpfcinfobuf + len,
  2002. 384-len,
  2003. " on PCI bus %02x device %02x irq %d",
  2004. phba->pcidev->bus->number,
  2005. phba->pcidev->devfn,
  2006. phba->pcidev->irq);
  2007. len = strlen(lpfcinfobuf);
  2008. if (phba->Port[0]) {
  2009. snprintf(lpfcinfobuf + len,
  2010. 384-len,
  2011. " port %s",
  2012. phba->Port);
  2013. }
  2014. }
  2015. return lpfcinfobuf;
  2016. }
  2017. /**
  2018. * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
  2019. * @phba: The Hba for which this call is being executed.
  2020. *
  2021. * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
  2022. * The default value of cfg_poll_tmo is 10 milliseconds.
  2023. **/
  2024. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  2025. {
  2026. unsigned long poll_tmo_expires =
  2027. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  2028. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  2029. mod_timer(&phba->fcp_poll_timer,
  2030. poll_tmo_expires);
  2031. }
  2032. /**
  2033. * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
  2034. * @phba: The Hba for which this call is being executed.
  2035. *
  2036. * This routine starts the fcp_poll_timer of @phba.
  2037. **/
  2038. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  2039. {
  2040. lpfc_poll_rearm_timer(phba);
  2041. }
  2042. /**
  2043. * lpfc_poll_timeout - Restart polling timer
  2044. * @ptr: Map to lpfc_hba data structure pointer.
  2045. *
  2046. * This routine restarts fcp_poll timer, when FCP ring polling is enable
  2047. * and FCP Ring interrupt is disable.
  2048. **/
  2049. void lpfc_poll_timeout(unsigned long ptr)
  2050. {
  2051. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2052. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2053. lpfc_sli_poll_fcp_ring (phba);
  2054. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2055. lpfc_poll_rearm_timer(phba);
  2056. }
  2057. }
  2058. /**
  2059. * lpfc_queuecommand - scsi_host_template queuecommand entry point
  2060. * @cmnd: Pointer to scsi_cmnd data structure.
  2061. * @done: Pointer to done routine.
  2062. *
  2063. * Driver registers this routine to scsi midlayer to submit a @cmd to process.
  2064. * This routine prepares an IOCB from scsi command and provides to firmware.
  2065. * The @done callback is invoked after driver finished processing the command.
  2066. *
  2067. * Return value :
  2068. * 0 - Success
  2069. * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
  2070. **/
  2071. static int
  2072. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  2073. {
  2074. struct Scsi_Host *shost = cmnd->device->host;
  2075. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2076. struct lpfc_hba *phba = vport->phba;
  2077. struct lpfc_sli *psli = &phba->sli;
  2078. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2079. struct lpfc_nodelist *ndlp = rdata->pnode;
  2080. struct lpfc_scsi_buf *lpfc_cmd;
  2081. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  2082. int err;
  2083. err = fc_remote_port_chkready(rport);
  2084. if (err) {
  2085. cmnd->result = err;
  2086. goto out_fail_command;
  2087. }
  2088. if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  2089. scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  2090. printk(KERN_ERR "BLKGRD ERROR: rcvd protected cmd:%02x op:%02x "
  2091. "str=%s without registering for BlockGuard - "
  2092. "Rejecting command\n",
  2093. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2094. dif_op_str[scsi_get_prot_op(cmnd)]);
  2095. goto out_fail_command;
  2096. }
  2097. /*
  2098. * Catch race where our node has transitioned, but the
  2099. * transport is still transitioning.
  2100. */
  2101. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  2102. cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  2103. goto out_fail_command;
  2104. }
  2105. if (vport->cfg_max_scsicmpl_time &&
  2106. (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
  2107. goto out_host_busy;
  2108. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2109. if (lpfc_cmd == NULL) {
  2110. lpfc_rampdown_queue_depth(phba);
  2111. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2112. "0707 driver's buffer pool is empty, "
  2113. "IO busied\n");
  2114. goto out_host_busy;
  2115. }
  2116. /*
  2117. * Store the midlayer's command structure for the completion phase
  2118. * and complete the command initialization.
  2119. */
  2120. lpfc_cmd->pCmd = cmnd;
  2121. lpfc_cmd->rdata = rdata;
  2122. lpfc_cmd->timeout = 0;
  2123. lpfc_cmd->start_time = jiffies;
  2124. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  2125. cmnd->scsi_done = done;
  2126. if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  2127. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2128. "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
  2129. "str=%s\n",
  2130. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2131. dif_op_str[scsi_get_prot_op(cmnd)]);
  2132. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2133. "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
  2134. "%02x %02x %02x %02x %02x \n",
  2135. cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
  2136. cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
  2137. cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
  2138. cmnd->cmnd[9]);
  2139. if (cmnd->cmnd[0] == READ_10)
  2140. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2141. "9035 BLKGRD: READ @ sector %llu, "
  2142. "count %lu\n",
  2143. (unsigned long long)scsi_get_lba(cmnd),
  2144. cmnd->request->nr_sectors);
  2145. else if (cmnd->cmnd[0] == WRITE_10)
  2146. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2147. "9036 BLKGRD: WRITE @ sector %llu, "
  2148. "count %lu cmd=%p\n",
  2149. (unsigned long long)scsi_get_lba(cmnd),
  2150. cmnd->request->nr_sectors,
  2151. cmnd);
  2152. err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  2153. } else {
  2154. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2155. "9038 BLKGRD: rcvd unprotected cmd:%02x op:%02x"
  2156. " str=%s\n",
  2157. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2158. dif_op_str[scsi_get_prot_op(cmnd)]);
  2159. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2160. "9039 BLKGRD: CDB: %02x %02x %02x %02x %02x "
  2161. "%02x %02x %02x %02x %02x \n",
  2162. cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
  2163. cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
  2164. cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
  2165. cmnd->cmnd[9]);
  2166. if (cmnd->cmnd[0] == READ_10)
  2167. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2168. "9040 dbg: READ @ sector %llu, "
  2169. "count %lu\n",
  2170. (unsigned long long)scsi_get_lba(cmnd),
  2171. cmnd->request->nr_sectors);
  2172. else if (cmnd->cmnd[0] == WRITE_10)
  2173. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2174. "9041 dbg: WRITE @ sector %llu, "
  2175. "count %lu cmd=%p\n",
  2176. (unsigned long long)scsi_get_lba(cmnd),
  2177. cmnd->request->nr_sectors, cmnd);
  2178. else
  2179. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2180. "9042 dbg: parser not implemented\n");
  2181. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  2182. }
  2183. if (err)
  2184. goto out_host_busy_free_buf;
  2185. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  2186. atomic_inc(&ndlp->cmd_pending);
  2187. err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
  2188. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  2189. if (err) {
  2190. atomic_dec(&ndlp->cmd_pending);
  2191. goto out_host_busy_free_buf;
  2192. }
  2193. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2194. lpfc_sli_poll_fcp_ring(phba);
  2195. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2196. lpfc_poll_rearm_timer(phba);
  2197. }
  2198. return 0;
  2199. out_host_busy_free_buf:
  2200. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  2201. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2202. out_host_busy:
  2203. return SCSI_MLQUEUE_HOST_BUSY;
  2204. out_fail_command:
  2205. done(cmnd);
  2206. return 0;
  2207. }
  2208. /**
  2209. * lpfc_block_error_handler - Routine to block error handler
  2210. * @cmnd: Pointer to scsi_cmnd data structure.
  2211. *
  2212. * This routine blocks execution till fc_rport state is not FC_PORSTAT_BLCOEKD.
  2213. **/
  2214. static void
  2215. lpfc_block_error_handler(struct scsi_cmnd *cmnd)
  2216. {
  2217. struct Scsi_Host *shost = cmnd->device->host;
  2218. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  2219. spin_lock_irq(shost->host_lock);
  2220. while (rport->port_state == FC_PORTSTATE_BLOCKED) {
  2221. spin_unlock_irq(shost->host_lock);
  2222. msleep(1000);
  2223. spin_lock_irq(shost->host_lock);
  2224. }
  2225. spin_unlock_irq(shost->host_lock);
  2226. return;
  2227. }
  2228. /**
  2229. * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
  2230. * @cmnd: Pointer to scsi_cmnd data structure.
  2231. *
  2232. * This routine aborts @cmnd pending in base driver.
  2233. *
  2234. * Return code :
  2235. * 0x2003 - Error
  2236. * 0x2002 - Success
  2237. **/
  2238. static int
  2239. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  2240. {
  2241. struct Scsi_Host *shost = cmnd->device->host;
  2242. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2243. struct lpfc_hba *phba = vport->phba;
  2244. struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
  2245. struct lpfc_iocbq *iocb;
  2246. struct lpfc_iocbq *abtsiocb;
  2247. struct lpfc_scsi_buf *lpfc_cmd;
  2248. IOCB_t *cmd, *icmd;
  2249. int ret = SUCCESS;
  2250. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  2251. lpfc_block_error_handler(cmnd);
  2252. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  2253. BUG_ON(!lpfc_cmd);
  2254. /*
  2255. * If pCmd field of the corresponding lpfc_scsi_buf structure
  2256. * points to a different SCSI command, then the driver has
  2257. * already completed this command, but the midlayer did not
  2258. * see the completion before the eh fired. Just return
  2259. * SUCCESS.
  2260. */
  2261. iocb = &lpfc_cmd->cur_iocbq;
  2262. if (lpfc_cmd->pCmd != cmnd)
  2263. goto out;
  2264. BUG_ON(iocb->context1 != lpfc_cmd);
  2265. abtsiocb = lpfc_sli_get_iocbq(phba);
  2266. if (abtsiocb == NULL) {
  2267. ret = FAILED;
  2268. goto out;
  2269. }
  2270. /*
  2271. * The scsi command can not be in txq and it is in flight because the
  2272. * pCmd is still pointig at the SCSI command we have to abort. There
  2273. * is no need to search the txcmplq. Just send an abort to the FW.
  2274. */
  2275. cmd = &iocb->iocb;
  2276. icmd = &abtsiocb->iocb;
  2277. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  2278. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  2279. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  2280. icmd->ulpLe = 1;
  2281. icmd->ulpClass = cmd->ulpClass;
  2282. if (lpfc_is_link_up(phba))
  2283. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  2284. else
  2285. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  2286. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  2287. abtsiocb->vport = vport;
  2288. if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
  2289. lpfc_sli_release_iocbq(phba, abtsiocb);
  2290. ret = FAILED;
  2291. goto out;
  2292. }
  2293. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2294. lpfc_sli_poll_fcp_ring (phba);
  2295. lpfc_cmd->waitq = &waitq;
  2296. /* Wait for abort to complete */
  2297. wait_event_timeout(waitq,
  2298. (lpfc_cmd->pCmd != cmnd),
  2299. (2*vport->cfg_devloss_tmo*HZ));
  2300. spin_lock_irq(shost->host_lock);
  2301. lpfc_cmd->waitq = NULL;
  2302. spin_unlock_irq(shost->host_lock);
  2303. if (lpfc_cmd->pCmd == cmnd) {
  2304. ret = FAILED;
  2305. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2306. "0748 abort handler timed out waiting "
  2307. "for abort to complete: ret %#x, ID %d, "
  2308. "LUN %d, snum %#lx\n",
  2309. ret, cmnd->device->id, cmnd->device->lun,
  2310. cmnd->serial_number);
  2311. }
  2312. out:
  2313. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2314. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  2315. "LUN %d snum %#lx\n", ret, cmnd->device->id,
  2316. cmnd->device->lun, cmnd->serial_number);
  2317. return ret;
  2318. }
  2319. /**
  2320. * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
  2321. * @cmnd: Pointer to scsi_cmnd data structure.
  2322. *
  2323. * This routine does a device reset by sending a TARGET_RESET task management
  2324. * command.
  2325. *
  2326. * Return code :
  2327. * 0x2003 - Error
  2328. * 0x2002 - Success
  2329. **/
  2330. static int
  2331. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  2332. {
  2333. struct Scsi_Host *shost = cmnd->device->host;
  2334. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2335. struct lpfc_hba *phba = vport->phba;
  2336. struct lpfc_scsi_buf *lpfc_cmd;
  2337. struct lpfc_iocbq *iocbq, *iocbqrsp;
  2338. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2339. struct lpfc_nodelist *pnode = rdata->pnode;
  2340. unsigned long later;
  2341. int ret = SUCCESS;
  2342. int status;
  2343. int cnt;
  2344. struct lpfc_scsi_event_header scsi_event;
  2345. lpfc_block_error_handler(cmnd);
  2346. /*
  2347. * If target is not in a MAPPED state, delay the reset until
  2348. * target is rediscovered or devloss timeout expires.
  2349. */
  2350. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2351. while (time_after(later, jiffies)) {
  2352. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2353. return FAILED;
  2354. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  2355. break;
  2356. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  2357. rdata = cmnd->device->hostdata;
  2358. if (!rdata)
  2359. break;
  2360. pnode = rdata->pnode;
  2361. }
  2362. scsi_event.event_type = FC_REG_SCSI_EVENT;
  2363. scsi_event.subcategory = LPFC_EVENT_TGTRESET;
  2364. scsi_event.lun = 0;
  2365. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  2366. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  2367. fc_host_post_vendor_event(shost,
  2368. fc_get_event_number(),
  2369. sizeof(scsi_event),
  2370. (char *)&scsi_event,
  2371. LPFC_NL_VENDOR_ID);
  2372. if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
  2373. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2374. "0721 LUN Reset rport "
  2375. "failure: msec x%x rdata x%p\n",
  2376. jiffies_to_msecs(jiffies - later), rdata);
  2377. return FAILED;
  2378. }
  2379. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2380. if (lpfc_cmd == NULL)
  2381. return FAILED;
  2382. lpfc_cmd->timeout = 60;
  2383. lpfc_cmd->rdata = rdata;
  2384. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
  2385. cmnd->device->lun,
  2386. FCP_TARGET_RESET);
  2387. if (!status) {
  2388. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2389. return FAILED;
  2390. }
  2391. iocbq = &lpfc_cmd->cur_iocbq;
  2392. /* get a buffer for this IOCB command response */
  2393. iocbqrsp = lpfc_sli_get_iocbq(phba);
  2394. if (iocbqrsp == NULL) {
  2395. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2396. return FAILED;
  2397. }
  2398. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2399. "0703 Issue target reset to TGT %d LUN %d "
  2400. "rpi x%x nlp_flag x%x\n", cmnd->device->id,
  2401. cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
  2402. status = lpfc_sli_issue_iocb_wait(phba,
  2403. &phba->sli.ring[phba->sli.fcp_ring],
  2404. iocbq, iocbqrsp, lpfc_cmd->timeout);
  2405. if (status == IOCB_TIMEDOUT) {
  2406. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  2407. ret = TIMEOUT_ERROR;
  2408. } else {
  2409. if (status != IOCB_SUCCESS)
  2410. ret = FAILED;
  2411. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2412. }
  2413. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2414. "0713 SCSI layer issued device reset (%d, %d) "
  2415. "return x%x status x%x result x%x\n",
  2416. cmnd->device->id, cmnd->device->lun, ret,
  2417. iocbqrsp->iocb.ulpStatus,
  2418. iocbqrsp->iocb.un.ulpWord[4]);
  2419. lpfc_sli_release_iocbq(phba, iocbqrsp);
  2420. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
  2421. LPFC_CTX_TGT);
  2422. if (cnt)
  2423. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  2424. cmnd->device->id, cmnd->device->lun,
  2425. LPFC_CTX_TGT);
  2426. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2427. while (time_after(later, jiffies) && cnt) {
  2428. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  2429. cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
  2430. cmnd->device->lun, LPFC_CTX_TGT);
  2431. }
  2432. if (cnt) {
  2433. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2434. "0719 device reset I/O flush failure: "
  2435. "cnt x%x\n", cnt);
  2436. ret = FAILED;
  2437. }
  2438. return ret;
  2439. }
  2440. /**
  2441. * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
  2442. * @cmnd: Pointer to scsi_cmnd data structure.
  2443. *
  2444. * This routine does target reset to all target on @cmnd->device->host.
  2445. *
  2446. * Return Code:
  2447. * 0x2003 - Error
  2448. * 0x2002 - Success
  2449. **/
  2450. static int
  2451. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  2452. {
  2453. struct Scsi_Host *shost = cmnd->device->host;
  2454. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2455. struct lpfc_hba *phba = vport->phba;
  2456. struct lpfc_nodelist *ndlp = NULL;
  2457. int match;
  2458. int ret = SUCCESS, status = SUCCESS, i;
  2459. int cnt;
  2460. struct lpfc_scsi_buf * lpfc_cmd;
  2461. unsigned long later;
  2462. struct lpfc_scsi_event_header scsi_event;
  2463. scsi_event.event_type = FC_REG_SCSI_EVENT;
  2464. scsi_event.subcategory = LPFC_EVENT_BUSRESET;
  2465. scsi_event.lun = 0;
  2466. memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
  2467. memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
  2468. fc_host_post_vendor_event(shost,
  2469. fc_get_event_number(),
  2470. sizeof(scsi_event),
  2471. (char *)&scsi_event,
  2472. LPFC_NL_VENDOR_ID);
  2473. lpfc_block_error_handler(cmnd);
  2474. /*
  2475. * Since the driver manages a single bus device, reset all
  2476. * targets known to the driver. Should any target reset
  2477. * fail, this routine returns failure to the midlayer.
  2478. */
  2479. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  2480. /* Search for mapped node by target ID */
  2481. match = 0;
  2482. spin_lock_irq(shost->host_lock);
  2483. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  2484. if (!NLP_CHK_NODE_ACT(ndlp))
  2485. continue;
  2486. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  2487. ndlp->nlp_sid == i &&
  2488. ndlp->rport) {
  2489. match = 1;
  2490. break;
  2491. }
  2492. }
  2493. spin_unlock_irq(shost->host_lock);
  2494. if (!match)
  2495. continue;
  2496. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2497. if (lpfc_cmd) {
  2498. lpfc_cmd->timeout = 60;
  2499. status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
  2500. cmnd->device->lun,
  2501. ndlp->rport->dd_data);
  2502. if (status != TIMEOUT_ERROR)
  2503. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2504. }
  2505. if (!lpfc_cmd || status != SUCCESS) {
  2506. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2507. "0700 Bus Reset on target %d failed\n",
  2508. i);
  2509. ret = FAILED;
  2510. }
  2511. }
  2512. /*
  2513. * All outstanding txcmplq I/Os should have been aborted by
  2514. * the targets. Unfortunately, some targets do not abide by
  2515. * this forcing the driver to double check.
  2516. */
  2517. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  2518. if (cnt)
  2519. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  2520. 0, 0, LPFC_CTX_HOST);
  2521. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2522. while (time_after(later, jiffies) && cnt) {
  2523. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  2524. cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
  2525. }
  2526. if (cnt) {
  2527. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2528. "0715 Bus Reset I/O flush failure: "
  2529. "cnt x%x left x%x\n", cnt, i);
  2530. ret = FAILED;
  2531. }
  2532. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2533. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  2534. return ret;
  2535. }
  2536. /**
  2537. * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
  2538. * @sdev: Pointer to scsi_device.
  2539. *
  2540. * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
  2541. * globally available list of scsi buffers. This routine also makes sure scsi
  2542. * buffer is not allocated more than HBA limit conveyed to midlayer. This list
  2543. * of scsi buffer exists for the lifetime of the driver.
  2544. *
  2545. * Return codes:
  2546. * non-0 - Error
  2547. * 0 - Success
  2548. **/
  2549. static int
  2550. lpfc_slave_alloc(struct scsi_device *sdev)
  2551. {
  2552. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  2553. struct lpfc_hba *phba = vport->phba;
  2554. struct lpfc_scsi_buf *scsi_buf = NULL;
  2555. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2556. uint32_t total = 0, i;
  2557. uint32_t num_to_alloc = 0;
  2558. unsigned long flags;
  2559. if (!rport || fc_remote_port_chkready(rport))
  2560. return -ENXIO;
  2561. sdev->hostdata = rport->dd_data;
  2562. /*
  2563. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  2564. * available list of scsi buffers. Don't allocate more than the
  2565. * HBA limit conveyed to the midlayer via the host structure. The
  2566. * formula accounts for the lun_queue_depth + error handlers + 1
  2567. * extra. This list of scsi bufs exists for the lifetime of the driver.
  2568. */
  2569. total = phba->total_scsi_bufs;
  2570. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  2571. /* Allow some exchanges to be available always to complete discovery */
  2572. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  2573. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2574. "0704 At limitation of %d preallocated "
  2575. "command buffers\n", total);
  2576. return 0;
  2577. /* Allow some exchanges to be available always to complete discovery */
  2578. } else if (total + num_to_alloc >
  2579. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  2580. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2581. "0705 Allocation request of %d "
  2582. "command buffers will exceed max of %d. "
  2583. "Reducing allocation request to %d.\n",
  2584. num_to_alloc, phba->cfg_hba_queue_depth,
  2585. (phba->cfg_hba_queue_depth - total));
  2586. num_to_alloc = phba->cfg_hba_queue_depth - total;
  2587. }
  2588. for (i = 0; i < num_to_alloc; i++) {
  2589. scsi_buf = lpfc_new_scsi_buf(vport);
  2590. if (!scsi_buf) {
  2591. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2592. "0706 Failed to allocate "
  2593. "command buffer\n");
  2594. break;
  2595. }
  2596. spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
  2597. phba->total_scsi_bufs++;
  2598. list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
  2599. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
  2600. }
  2601. return 0;
  2602. }
  2603. /**
  2604. * lpfc_slave_configure - scsi_host_template slave_configure entry point
  2605. * @sdev: Pointer to scsi_device.
  2606. *
  2607. * This routine configures following items
  2608. * - Tag command queuing support for @sdev if supported.
  2609. * - Dev loss time out value of fc_rport.
  2610. * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
  2611. *
  2612. * Return codes:
  2613. * 0 - Success
  2614. **/
  2615. static int
  2616. lpfc_slave_configure(struct scsi_device *sdev)
  2617. {
  2618. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  2619. struct lpfc_hba *phba = vport->phba;
  2620. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  2621. if (sdev->tagged_supported)
  2622. scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
  2623. else
  2624. scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
  2625. /*
  2626. * Initialize the fc transport attributes for the target
  2627. * containing this scsi device. Also note that the driver's
  2628. * target pointer is stored in the starget_data for the
  2629. * driver's sysfs entry point functions.
  2630. */
  2631. rport->dev_loss_tmo = vport->cfg_devloss_tmo;
  2632. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2633. lpfc_sli_poll_fcp_ring(phba);
  2634. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2635. lpfc_poll_rearm_timer(phba);
  2636. }
  2637. return 0;
  2638. }
  2639. /**
  2640. * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
  2641. * @sdev: Pointer to scsi_device.
  2642. *
  2643. * This routine sets @sdev hostatdata filed to null.
  2644. **/
  2645. static void
  2646. lpfc_slave_destroy(struct scsi_device *sdev)
  2647. {
  2648. sdev->hostdata = NULL;
  2649. return;
  2650. }
  2651. struct scsi_host_template lpfc_template = {
  2652. .module = THIS_MODULE,
  2653. .name = LPFC_DRIVER_NAME,
  2654. .info = lpfc_info,
  2655. .queuecommand = lpfc_queuecommand,
  2656. .eh_abort_handler = lpfc_abort_handler,
  2657. .eh_device_reset_handler= lpfc_device_reset_handler,
  2658. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  2659. .slave_alloc = lpfc_slave_alloc,
  2660. .slave_configure = lpfc_slave_configure,
  2661. .slave_destroy = lpfc_slave_destroy,
  2662. .scan_finished = lpfc_scan_finished,
  2663. .this_id = -1,
  2664. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  2665. .cmd_per_lun = LPFC_CMD_PER_LUN,
  2666. .use_clustering = ENABLE_CLUSTERING,
  2667. .shost_attrs = lpfc_hba_attrs,
  2668. .max_sectors = 0xFFFF,
  2669. };
  2670. struct scsi_host_template lpfc_vport_template = {
  2671. .module = THIS_MODULE,
  2672. .name = LPFC_DRIVER_NAME,
  2673. .info = lpfc_info,
  2674. .queuecommand = lpfc_queuecommand,
  2675. .eh_abort_handler = lpfc_abort_handler,
  2676. .eh_device_reset_handler= lpfc_device_reset_handler,
  2677. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  2678. .slave_alloc = lpfc_slave_alloc,
  2679. .slave_configure = lpfc_slave_configure,
  2680. .slave_destroy = lpfc_slave_destroy,
  2681. .scan_finished = lpfc_scan_finished,
  2682. .this_id = -1,
  2683. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  2684. .cmd_per_lun = LPFC_CMD_PER_LUN,
  2685. .use_clustering = ENABLE_CLUSTERING,
  2686. .shost_attrs = lpfc_vport_attrs,
  2687. .max_sectors = 0xFFFF,
  2688. };