lpfc_scsi.c 117 KB

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