lpfc_scsi.c 153 KB

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