lpfc_scsi.c 151 KB

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