lpfc_scsi.c 152 KB

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