lpfc_scsi.c 153 KB

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