lpfc_scsi.c 153 KB

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