lpfc_scsi.c 109 KB

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