lpfc_scsi.c 152 KB

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