lpfc_scsi.c 164 KB

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