lpfc_scsi.c 93 KB

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