ibmvfc.c 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974
  1. /*
  2. * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
  3. *
  4. * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
  5. *
  6. * Copyright (C) IBM Corporation, 2008
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmapool.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kthread.h>
  30. #include <linux/slab.h>
  31. #include <linux/of.h>
  32. #include <linux/pm.h>
  33. #include <linux/stringify.h>
  34. #include <asm/firmware.h>
  35. #include <asm/irq.h>
  36. #include <asm/vio.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_cmnd.h>
  39. #include <scsi/scsi_host.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_tcq.h>
  42. #include <scsi/scsi_transport_fc.h>
  43. #include <scsi/scsi_bsg_fc.h>
  44. #include "ibmvfc.h"
  45. static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
  46. static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
  47. static unsigned int max_lun = IBMVFC_MAX_LUN;
  48. static unsigned int max_targets = IBMVFC_MAX_TARGETS;
  49. static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
  50. static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
  51. static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
  52. static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
  53. static LIST_HEAD(ibmvfc_head);
  54. static DEFINE_SPINLOCK(ibmvfc_driver_lock);
  55. static struct scsi_transport_template *ibmvfc_transport_template;
  56. MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
  57. MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
  58. MODULE_LICENSE("GPL");
  59. MODULE_VERSION(IBMVFC_DRIVER_VERSION);
  60. module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
  61. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
  62. "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
  63. module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
  64. MODULE_PARM_DESC(default_timeout,
  65. "Default timeout in seconds for initialization and EH commands. "
  66. "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
  67. module_param_named(max_requests, max_requests, uint, S_IRUGO);
  68. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
  69. "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
  70. module_param_named(max_lun, max_lun, uint, S_IRUGO);
  71. MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
  72. "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
  73. module_param_named(max_targets, max_targets, uint, S_IRUGO);
  74. MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
  75. "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
  76. module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
  77. MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
  78. "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
  79. module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
  80. MODULE_PARM_DESC(debug, "Enable driver debug information. "
  81. "[Default=" __stringify(IBMVFC_DEBUG) "]");
  82. module_param_named(log_level, log_level, uint, 0);
  83. MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
  84. "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
  85. static const struct {
  86. u16 status;
  87. u16 error;
  88. u8 result;
  89. u8 retry;
  90. int log;
  91. char *name;
  92. } cmd_status [] = {
  93. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
  94. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
  95. { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
  96. { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
  97. { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
  98. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
  99. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
  100. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
  101. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
  102. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
  103. { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
  104. { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
  105. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
  106. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
  107. { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
  108. { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
  109. { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
  110. { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
  111. { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
  112. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
  113. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
  114. { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
  115. { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
  116. { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
  117. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
  118. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
  119. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
  120. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
  121. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
  122. { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
  123. { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
  124. { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
  125. { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
  126. { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
  127. { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
  128. { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
  129. };
  130. static void ibmvfc_npiv_login(struct ibmvfc_host *);
  131. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
  132. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
  133. static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
  134. static void ibmvfc_npiv_logout(struct ibmvfc_host *);
  135. static const char *unknown_error = "unknown error";
  136. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  137. /**
  138. * ibmvfc_trc_start - Log a start trace entry
  139. * @evt: ibmvfc event struct
  140. *
  141. **/
  142. static void ibmvfc_trc_start(struct ibmvfc_event *evt)
  143. {
  144. struct ibmvfc_host *vhost = evt->vhost;
  145. struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
  146. struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
  147. struct ibmvfc_trace_entry *entry;
  148. entry = &vhost->trace[vhost->trace_index++];
  149. entry->evt = evt;
  150. entry->time = jiffies;
  151. entry->fmt = evt->crq.format;
  152. entry->type = IBMVFC_TRC_START;
  153. switch (entry->fmt) {
  154. case IBMVFC_CMD_FORMAT:
  155. entry->op_code = vfc_cmd->iu.cdb[0];
  156. entry->scsi_id = vfc_cmd->tgt_scsi_id;
  157. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  158. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  159. entry->u.start.xfer_len = vfc_cmd->iu.xfer_len;
  160. break;
  161. case IBMVFC_MAD_FORMAT:
  162. entry->op_code = mad->opcode;
  163. break;
  164. default:
  165. break;
  166. };
  167. }
  168. /**
  169. * ibmvfc_trc_end - Log an end trace entry
  170. * @evt: ibmvfc event struct
  171. *
  172. **/
  173. static void ibmvfc_trc_end(struct ibmvfc_event *evt)
  174. {
  175. struct ibmvfc_host *vhost = evt->vhost;
  176. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  177. struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
  178. struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
  179. entry->evt = evt;
  180. entry->time = jiffies;
  181. entry->fmt = evt->crq.format;
  182. entry->type = IBMVFC_TRC_END;
  183. switch (entry->fmt) {
  184. case IBMVFC_CMD_FORMAT:
  185. entry->op_code = vfc_cmd->iu.cdb[0];
  186. entry->scsi_id = vfc_cmd->tgt_scsi_id;
  187. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  188. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  189. entry->u.end.status = vfc_cmd->status;
  190. entry->u.end.error = vfc_cmd->error;
  191. entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
  192. entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
  193. entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
  194. break;
  195. case IBMVFC_MAD_FORMAT:
  196. entry->op_code = mad->opcode;
  197. entry->u.end.status = mad->status;
  198. break;
  199. default:
  200. break;
  201. };
  202. }
  203. #else
  204. #define ibmvfc_trc_start(evt) do { } while (0)
  205. #define ibmvfc_trc_end(evt) do { } while (0)
  206. #endif
  207. /**
  208. * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
  209. * @status: status / error class
  210. * @error: error
  211. *
  212. * Return value:
  213. * index into cmd_status / -EINVAL on failure
  214. **/
  215. static int ibmvfc_get_err_index(u16 status, u16 error)
  216. {
  217. int i;
  218. for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
  219. if ((cmd_status[i].status & status) == cmd_status[i].status &&
  220. cmd_status[i].error == error)
  221. return i;
  222. return -EINVAL;
  223. }
  224. /**
  225. * ibmvfc_get_cmd_error - Find the error description for the fcp response
  226. * @status: status / error class
  227. * @error: error
  228. *
  229. * Return value:
  230. * error description string
  231. **/
  232. static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
  233. {
  234. int rc = ibmvfc_get_err_index(status, error);
  235. if (rc >= 0)
  236. return cmd_status[rc].name;
  237. return unknown_error;
  238. }
  239. /**
  240. * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
  241. * @vfc_cmd: ibmvfc command struct
  242. *
  243. * Return value:
  244. * SCSI result value to return for completed command
  245. **/
  246. static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
  247. {
  248. int err;
  249. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  250. int fc_rsp_len = rsp->fcp_rsp_len;
  251. if ((rsp->flags & FCP_RSP_LEN_VALID) &&
  252. ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
  253. rsp->data.info.rsp_code))
  254. return DID_ERROR << 16;
  255. err = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
  256. if (err >= 0)
  257. return rsp->scsi_status | (cmd_status[err].result << 16);
  258. return rsp->scsi_status | (DID_ERROR << 16);
  259. }
  260. /**
  261. * ibmvfc_retry_cmd - Determine if error status is retryable
  262. * @status: status / error class
  263. * @error: error
  264. *
  265. * Return value:
  266. * 1 if error should be retried / 0 if it should not
  267. **/
  268. static int ibmvfc_retry_cmd(u16 status, u16 error)
  269. {
  270. int rc = ibmvfc_get_err_index(status, error);
  271. if (rc >= 0)
  272. return cmd_status[rc].retry;
  273. return 1;
  274. }
  275. static const char *unknown_fc_explain = "unknown fc explain";
  276. static const struct {
  277. u16 fc_explain;
  278. char *name;
  279. } ls_explain [] = {
  280. { 0x00, "no additional explanation" },
  281. { 0x01, "service parameter error - options" },
  282. { 0x03, "service parameter error - initiator control" },
  283. { 0x05, "service parameter error - recipient control" },
  284. { 0x07, "service parameter error - received data field size" },
  285. { 0x09, "service parameter error - concurrent seq" },
  286. { 0x0B, "service parameter error - credit" },
  287. { 0x0D, "invalid N_Port/F_Port_Name" },
  288. { 0x0E, "invalid node/Fabric Name" },
  289. { 0x0F, "invalid common service parameters" },
  290. { 0x11, "invalid association header" },
  291. { 0x13, "association header required" },
  292. { 0x15, "invalid originator S_ID" },
  293. { 0x17, "invalid OX_ID-RX-ID combination" },
  294. { 0x19, "command (request) already in progress" },
  295. { 0x1E, "N_Port Login requested" },
  296. { 0x1F, "Invalid N_Port_ID" },
  297. };
  298. static const struct {
  299. u16 fc_explain;
  300. char *name;
  301. } gs_explain [] = {
  302. { 0x00, "no additional explanation" },
  303. { 0x01, "port identifier not registered" },
  304. { 0x02, "port name not registered" },
  305. { 0x03, "node name not registered" },
  306. { 0x04, "class of service not registered" },
  307. { 0x06, "initial process associator not registered" },
  308. { 0x07, "FC-4 TYPEs not registered" },
  309. { 0x08, "symbolic port name not registered" },
  310. { 0x09, "symbolic node name not registered" },
  311. { 0x0A, "port type not registered" },
  312. { 0xF0, "authorization exception" },
  313. { 0xF1, "authentication exception" },
  314. { 0xF2, "data base full" },
  315. { 0xF3, "data base empty" },
  316. { 0xF4, "processing request" },
  317. { 0xF5, "unable to verify connection" },
  318. { 0xF6, "devices not in a common zone" },
  319. };
  320. /**
  321. * ibmvfc_get_ls_explain - Return the FC Explain description text
  322. * @status: FC Explain status
  323. *
  324. * Returns:
  325. * error string
  326. **/
  327. static const char *ibmvfc_get_ls_explain(u16 status)
  328. {
  329. int i;
  330. for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
  331. if (ls_explain[i].fc_explain == status)
  332. return ls_explain[i].name;
  333. return unknown_fc_explain;
  334. }
  335. /**
  336. * ibmvfc_get_gs_explain - Return the FC Explain description text
  337. * @status: FC Explain status
  338. *
  339. * Returns:
  340. * error string
  341. **/
  342. static const char *ibmvfc_get_gs_explain(u16 status)
  343. {
  344. int i;
  345. for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
  346. if (gs_explain[i].fc_explain == status)
  347. return gs_explain[i].name;
  348. return unknown_fc_explain;
  349. }
  350. static const struct {
  351. enum ibmvfc_fc_type fc_type;
  352. char *name;
  353. } fc_type [] = {
  354. { IBMVFC_FABRIC_REJECT, "fabric reject" },
  355. { IBMVFC_PORT_REJECT, "port reject" },
  356. { IBMVFC_LS_REJECT, "ELS reject" },
  357. { IBMVFC_FABRIC_BUSY, "fabric busy" },
  358. { IBMVFC_PORT_BUSY, "port busy" },
  359. { IBMVFC_BASIC_REJECT, "basic reject" },
  360. };
  361. static const char *unknown_fc_type = "unknown fc type";
  362. /**
  363. * ibmvfc_get_fc_type - Return the FC Type description text
  364. * @status: FC Type error status
  365. *
  366. * Returns:
  367. * error string
  368. **/
  369. static const char *ibmvfc_get_fc_type(u16 status)
  370. {
  371. int i;
  372. for (i = 0; i < ARRAY_SIZE(fc_type); i++)
  373. if (fc_type[i].fc_type == status)
  374. return fc_type[i].name;
  375. return unknown_fc_type;
  376. }
  377. /**
  378. * ibmvfc_set_tgt_action - Set the next init action for the target
  379. * @tgt: ibmvfc target struct
  380. * @action: action to perform
  381. *
  382. **/
  383. static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
  384. enum ibmvfc_target_action action)
  385. {
  386. switch (tgt->action) {
  387. case IBMVFC_TGT_ACTION_DEL_RPORT:
  388. if (action == IBMVFC_TGT_ACTION_DELETED_RPORT)
  389. tgt->action = action;
  390. case IBMVFC_TGT_ACTION_DELETED_RPORT:
  391. break;
  392. default:
  393. if (action == IBMVFC_TGT_ACTION_DEL_RPORT)
  394. tgt->add_rport = 0;
  395. tgt->action = action;
  396. break;
  397. }
  398. }
  399. /**
  400. * ibmvfc_set_host_state - Set the state for the host
  401. * @vhost: ibmvfc host struct
  402. * @state: state to set host to
  403. *
  404. * Returns:
  405. * 0 if state changed / non-zero if not changed
  406. **/
  407. static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
  408. enum ibmvfc_host_state state)
  409. {
  410. int rc = 0;
  411. switch (vhost->state) {
  412. case IBMVFC_HOST_OFFLINE:
  413. rc = -EINVAL;
  414. break;
  415. default:
  416. vhost->state = state;
  417. break;
  418. };
  419. return rc;
  420. }
  421. /**
  422. * ibmvfc_set_host_action - Set the next init action for the host
  423. * @vhost: ibmvfc host struct
  424. * @action: action to perform
  425. *
  426. **/
  427. static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
  428. enum ibmvfc_host_action action)
  429. {
  430. switch (action) {
  431. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  432. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
  433. vhost->action = action;
  434. break;
  435. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  436. if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
  437. vhost->action = action;
  438. break;
  439. case IBMVFC_HOST_ACTION_INIT_WAIT:
  440. if (vhost->action == IBMVFC_HOST_ACTION_INIT)
  441. vhost->action = action;
  442. break;
  443. case IBMVFC_HOST_ACTION_QUERY:
  444. switch (vhost->action) {
  445. case IBMVFC_HOST_ACTION_INIT_WAIT:
  446. case IBMVFC_HOST_ACTION_NONE:
  447. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  448. vhost->action = action;
  449. break;
  450. default:
  451. break;
  452. };
  453. break;
  454. case IBMVFC_HOST_ACTION_TGT_INIT:
  455. if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
  456. vhost->action = action;
  457. break;
  458. case IBMVFC_HOST_ACTION_INIT:
  459. case IBMVFC_HOST_ACTION_TGT_DEL:
  460. switch (vhost->action) {
  461. case IBMVFC_HOST_ACTION_RESET:
  462. case IBMVFC_HOST_ACTION_REENABLE:
  463. break;
  464. default:
  465. vhost->action = action;
  466. break;
  467. };
  468. break;
  469. case IBMVFC_HOST_ACTION_LOGO:
  470. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  471. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  472. case IBMVFC_HOST_ACTION_NONE:
  473. case IBMVFC_HOST_ACTION_RESET:
  474. case IBMVFC_HOST_ACTION_REENABLE:
  475. default:
  476. vhost->action = action;
  477. break;
  478. };
  479. }
  480. /**
  481. * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
  482. * @vhost: ibmvfc host struct
  483. *
  484. * Return value:
  485. * nothing
  486. **/
  487. static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
  488. {
  489. if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
  490. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  491. scsi_block_requests(vhost->host);
  492. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  493. }
  494. } else
  495. vhost->reinit = 1;
  496. wake_up(&vhost->work_wait_q);
  497. }
  498. /**
  499. * ibmvfc_link_down - Handle a link down event from the adapter
  500. * @vhost: ibmvfc host struct
  501. * @state: ibmvfc host state to enter
  502. *
  503. **/
  504. static void ibmvfc_link_down(struct ibmvfc_host *vhost,
  505. enum ibmvfc_host_state state)
  506. {
  507. struct ibmvfc_target *tgt;
  508. ENTER;
  509. scsi_block_requests(vhost->host);
  510. list_for_each_entry(tgt, &vhost->targets, queue)
  511. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  512. ibmvfc_set_host_state(vhost, state);
  513. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  514. vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
  515. wake_up(&vhost->work_wait_q);
  516. LEAVE;
  517. }
  518. /**
  519. * ibmvfc_init_host - Start host initialization
  520. * @vhost: ibmvfc host struct
  521. *
  522. * Return value:
  523. * nothing
  524. **/
  525. static void ibmvfc_init_host(struct ibmvfc_host *vhost)
  526. {
  527. struct ibmvfc_target *tgt;
  528. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  529. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  530. dev_err(vhost->dev,
  531. "Host initialization retries exceeded. Taking adapter offline\n");
  532. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  533. return;
  534. }
  535. }
  536. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  537. memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
  538. vhost->async_crq.cur = 0;
  539. list_for_each_entry(tgt, &vhost->targets, queue)
  540. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  541. scsi_block_requests(vhost->host);
  542. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  543. vhost->job_step = ibmvfc_npiv_login;
  544. wake_up(&vhost->work_wait_q);
  545. }
  546. }
  547. /**
  548. * ibmvfc_send_crq - Send a CRQ
  549. * @vhost: ibmvfc host struct
  550. * @word1: the first 64 bits of the data
  551. * @word2: the second 64 bits of the data
  552. *
  553. * Return value:
  554. * 0 on success / other on failure
  555. **/
  556. static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
  557. {
  558. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  559. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  560. }
  561. /**
  562. * ibmvfc_send_crq_init - Send a CRQ init message
  563. * @vhost: ibmvfc host struct
  564. *
  565. * Return value:
  566. * 0 on success / other on failure
  567. **/
  568. static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
  569. {
  570. ibmvfc_dbg(vhost, "Sending CRQ init\n");
  571. return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
  572. }
  573. /**
  574. * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
  575. * @vhost: ibmvfc host struct
  576. *
  577. * Return value:
  578. * 0 on success / other on failure
  579. **/
  580. static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
  581. {
  582. ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
  583. return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
  584. }
  585. /**
  586. * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
  587. * @vhost: ibmvfc host struct
  588. *
  589. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  590. * the crq with the hypervisor.
  591. **/
  592. static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
  593. {
  594. long rc = 0;
  595. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  596. struct ibmvfc_crq_queue *crq = &vhost->crq;
  597. ibmvfc_dbg(vhost, "Releasing CRQ\n");
  598. free_irq(vdev->irq, vhost);
  599. tasklet_kill(&vhost->tasklet);
  600. do {
  601. if (rc)
  602. msleep(100);
  603. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  604. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  605. vhost->state = IBMVFC_NO_CRQ;
  606. vhost->logged_in = 0;
  607. dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  608. free_page((unsigned long)crq->msgs);
  609. }
  610. /**
  611. * ibmvfc_reenable_crq_queue - reenables the CRQ
  612. * @vhost: ibmvfc host struct
  613. *
  614. * Return value:
  615. * 0 on success / other on failure
  616. **/
  617. static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
  618. {
  619. int rc = 0;
  620. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  621. /* Re-enable the CRQ */
  622. do {
  623. if (rc)
  624. msleep(100);
  625. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  626. } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
  627. if (rc)
  628. dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
  629. return rc;
  630. }
  631. /**
  632. * ibmvfc_reset_crq - resets a crq after a failure
  633. * @vhost: ibmvfc host struct
  634. *
  635. * Return value:
  636. * 0 on success / other on failure
  637. **/
  638. static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
  639. {
  640. int rc = 0;
  641. unsigned long flags;
  642. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  643. struct ibmvfc_crq_queue *crq = &vhost->crq;
  644. /* Close the CRQ */
  645. do {
  646. if (rc)
  647. msleep(100);
  648. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  649. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  650. spin_lock_irqsave(vhost->host->host_lock, flags);
  651. vhost->state = IBMVFC_NO_CRQ;
  652. vhost->logged_in = 0;
  653. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  654. /* Clean out the queue */
  655. memset(crq->msgs, 0, PAGE_SIZE);
  656. crq->cur = 0;
  657. /* And re-open it again */
  658. rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  659. crq->msg_token, PAGE_SIZE);
  660. if (rc == H_CLOSED)
  661. /* Adapter is good, but other end is not ready */
  662. dev_warn(vhost->dev, "Partner adapter not ready\n");
  663. else if (rc != 0)
  664. dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
  665. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  666. return rc;
  667. }
  668. /**
  669. * ibmvfc_valid_event - Determines if event is valid.
  670. * @pool: event_pool that contains the event
  671. * @evt: ibmvfc event to be checked for validity
  672. *
  673. * Return value:
  674. * 1 if event is valid / 0 if event is not valid
  675. **/
  676. static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
  677. struct ibmvfc_event *evt)
  678. {
  679. int index = evt - pool->events;
  680. if (index < 0 || index >= pool->size) /* outside of bounds */
  681. return 0;
  682. if (evt != pool->events + index) /* unaligned */
  683. return 0;
  684. return 1;
  685. }
  686. /**
  687. * ibmvfc_free_event - Free the specified event
  688. * @evt: ibmvfc_event to be freed
  689. *
  690. **/
  691. static void ibmvfc_free_event(struct ibmvfc_event *evt)
  692. {
  693. struct ibmvfc_host *vhost = evt->vhost;
  694. struct ibmvfc_event_pool *pool = &vhost->pool;
  695. BUG_ON(!ibmvfc_valid_event(pool, evt));
  696. BUG_ON(atomic_inc_return(&evt->free) != 1);
  697. list_add_tail(&evt->queue, &vhost->free);
  698. }
  699. /**
  700. * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
  701. * @evt: ibmvfc event struct
  702. *
  703. * This function does not setup any error status, that must be done
  704. * before this function gets called.
  705. **/
  706. static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
  707. {
  708. struct scsi_cmnd *cmnd = evt->cmnd;
  709. if (cmnd) {
  710. scsi_dma_unmap(cmnd);
  711. cmnd->scsi_done(cmnd);
  712. }
  713. if (evt->eh_comp)
  714. complete(evt->eh_comp);
  715. ibmvfc_free_event(evt);
  716. }
  717. /**
  718. * ibmvfc_fail_request - Fail request with specified error code
  719. * @evt: ibmvfc event struct
  720. * @error_code: error code to fail request with
  721. *
  722. * Return value:
  723. * none
  724. **/
  725. static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
  726. {
  727. if (evt->cmnd) {
  728. evt->cmnd->result = (error_code << 16);
  729. evt->done = ibmvfc_scsi_eh_done;
  730. } else
  731. evt->xfer_iu->mad_common.status = IBMVFC_MAD_DRIVER_FAILED;
  732. list_del(&evt->queue);
  733. del_timer(&evt->timer);
  734. ibmvfc_trc_end(evt);
  735. evt->done(evt);
  736. }
  737. /**
  738. * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
  739. * @vhost: ibmvfc host struct
  740. * @error_code: error code to fail requests with
  741. *
  742. * Return value:
  743. * none
  744. **/
  745. static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
  746. {
  747. struct ibmvfc_event *evt, *pos;
  748. ibmvfc_dbg(vhost, "Purging all requests\n");
  749. list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
  750. ibmvfc_fail_request(evt, error_code);
  751. }
  752. /**
  753. * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
  754. * @vhost: struct ibmvfc host to reset
  755. **/
  756. static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
  757. {
  758. ibmvfc_purge_requests(vhost, DID_ERROR);
  759. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  760. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  761. }
  762. /**
  763. * __ibmvfc_reset_host - Reset the connection to the server (no locking)
  764. * @vhost: struct ibmvfc host to reset
  765. **/
  766. static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
  767. {
  768. if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
  769. !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  770. scsi_block_requests(vhost->host);
  771. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
  772. vhost->job_step = ibmvfc_npiv_logout;
  773. wake_up(&vhost->work_wait_q);
  774. } else
  775. ibmvfc_hard_reset_host(vhost);
  776. }
  777. /**
  778. * ibmvfc_reset_host - Reset the connection to the server
  779. * @vhost: ibmvfc host struct
  780. **/
  781. static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
  782. {
  783. unsigned long flags;
  784. spin_lock_irqsave(vhost->host->host_lock, flags);
  785. __ibmvfc_reset_host(vhost);
  786. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  787. }
  788. /**
  789. * ibmvfc_retry_host_init - Retry host initialization if allowed
  790. * @vhost: ibmvfc host struct
  791. *
  792. * Returns: 1 if init will be retried / 0 if not
  793. *
  794. **/
  795. static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
  796. {
  797. int retry = 0;
  798. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  799. vhost->delay_init = 1;
  800. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  801. dev_err(vhost->dev,
  802. "Host initialization retries exceeded. Taking adapter offline\n");
  803. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  804. } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
  805. __ibmvfc_reset_host(vhost);
  806. else {
  807. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  808. retry = 1;
  809. }
  810. }
  811. wake_up(&vhost->work_wait_q);
  812. return retry;
  813. }
  814. /**
  815. * __ibmvfc_get_target - Find the specified scsi_target (no locking)
  816. * @starget: scsi target struct
  817. *
  818. * Return value:
  819. * ibmvfc_target struct / NULL if not found
  820. **/
  821. static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
  822. {
  823. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  824. struct ibmvfc_host *vhost = shost_priv(shost);
  825. struct ibmvfc_target *tgt;
  826. list_for_each_entry(tgt, &vhost->targets, queue)
  827. if (tgt->target_id == starget->id) {
  828. kref_get(&tgt->kref);
  829. return tgt;
  830. }
  831. return NULL;
  832. }
  833. /**
  834. * ibmvfc_get_target - Find the specified scsi_target
  835. * @starget: scsi target struct
  836. *
  837. * Return value:
  838. * ibmvfc_target struct / NULL if not found
  839. **/
  840. static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
  841. {
  842. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  843. struct ibmvfc_target *tgt;
  844. unsigned long flags;
  845. spin_lock_irqsave(shost->host_lock, flags);
  846. tgt = __ibmvfc_get_target(starget);
  847. spin_unlock_irqrestore(shost->host_lock, flags);
  848. return tgt;
  849. }
  850. /**
  851. * ibmvfc_get_host_speed - Get host port speed
  852. * @shost: scsi host struct
  853. *
  854. * Return value:
  855. * none
  856. **/
  857. static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
  858. {
  859. struct ibmvfc_host *vhost = shost_priv(shost);
  860. unsigned long flags;
  861. spin_lock_irqsave(shost->host_lock, flags);
  862. if (vhost->state == IBMVFC_ACTIVE) {
  863. switch (vhost->login_buf->resp.link_speed / 100) {
  864. case 1:
  865. fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
  866. break;
  867. case 2:
  868. fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
  869. break;
  870. case 4:
  871. fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
  872. break;
  873. case 8:
  874. fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
  875. break;
  876. case 10:
  877. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  878. break;
  879. case 16:
  880. fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
  881. break;
  882. default:
  883. ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
  884. vhost->login_buf->resp.link_speed / 100);
  885. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  886. break;
  887. }
  888. } else
  889. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  890. spin_unlock_irqrestore(shost->host_lock, flags);
  891. }
  892. /**
  893. * ibmvfc_get_host_port_state - Get host port state
  894. * @shost: scsi host struct
  895. *
  896. * Return value:
  897. * none
  898. **/
  899. static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
  900. {
  901. struct ibmvfc_host *vhost = shost_priv(shost);
  902. unsigned long flags;
  903. spin_lock_irqsave(shost->host_lock, flags);
  904. switch (vhost->state) {
  905. case IBMVFC_INITIALIZING:
  906. case IBMVFC_ACTIVE:
  907. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  908. break;
  909. case IBMVFC_LINK_DOWN:
  910. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  911. break;
  912. case IBMVFC_LINK_DEAD:
  913. case IBMVFC_HOST_OFFLINE:
  914. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  915. break;
  916. case IBMVFC_HALTED:
  917. fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
  918. break;
  919. case IBMVFC_NO_CRQ:
  920. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  921. break;
  922. default:
  923. ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
  924. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  925. break;
  926. }
  927. spin_unlock_irqrestore(shost->host_lock, flags);
  928. }
  929. /**
  930. * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
  931. * @rport: rport struct
  932. * @timeout: timeout value
  933. *
  934. * Return value:
  935. * none
  936. **/
  937. static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  938. {
  939. if (timeout)
  940. rport->dev_loss_tmo = timeout;
  941. else
  942. rport->dev_loss_tmo = 1;
  943. }
  944. /**
  945. * ibmvfc_release_tgt - Free memory allocated for a target
  946. * @kref: kref struct
  947. *
  948. **/
  949. static void ibmvfc_release_tgt(struct kref *kref)
  950. {
  951. struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
  952. kfree(tgt);
  953. }
  954. /**
  955. * ibmvfc_get_starget_node_name - Get SCSI target's node name
  956. * @starget: scsi target struct
  957. *
  958. * Return value:
  959. * none
  960. **/
  961. static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
  962. {
  963. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  964. fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
  965. if (tgt)
  966. kref_put(&tgt->kref, ibmvfc_release_tgt);
  967. }
  968. /**
  969. * ibmvfc_get_starget_port_name - Get SCSI target's port name
  970. * @starget: scsi target struct
  971. *
  972. * Return value:
  973. * none
  974. **/
  975. static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
  976. {
  977. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  978. fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
  979. if (tgt)
  980. kref_put(&tgt->kref, ibmvfc_release_tgt);
  981. }
  982. /**
  983. * ibmvfc_get_starget_port_id - Get SCSI target's port ID
  984. * @starget: scsi target struct
  985. *
  986. * Return value:
  987. * none
  988. **/
  989. static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
  990. {
  991. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  992. fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
  993. if (tgt)
  994. kref_put(&tgt->kref, ibmvfc_release_tgt);
  995. }
  996. /**
  997. * ibmvfc_wait_while_resetting - Wait while the host resets
  998. * @vhost: ibmvfc host struct
  999. *
  1000. * Return value:
  1001. * 0 on success / other on failure
  1002. **/
  1003. static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
  1004. {
  1005. long timeout = wait_event_timeout(vhost->init_wait_q,
  1006. ((vhost->state == IBMVFC_ACTIVE ||
  1007. vhost->state == IBMVFC_HOST_OFFLINE ||
  1008. vhost->state == IBMVFC_LINK_DEAD) &&
  1009. vhost->action == IBMVFC_HOST_ACTION_NONE),
  1010. (init_timeout * HZ));
  1011. return timeout ? 0 : -EIO;
  1012. }
  1013. /**
  1014. * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
  1015. * @shost: scsi host struct
  1016. *
  1017. * Return value:
  1018. * 0 on success / other on failure
  1019. **/
  1020. static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
  1021. {
  1022. struct ibmvfc_host *vhost = shost_priv(shost);
  1023. dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
  1024. ibmvfc_reset_host(vhost);
  1025. return ibmvfc_wait_while_resetting(vhost);
  1026. }
  1027. /**
  1028. * ibmvfc_gather_partition_info - Gather info about the LPAR
  1029. *
  1030. * Return value:
  1031. * none
  1032. **/
  1033. static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
  1034. {
  1035. struct device_node *rootdn;
  1036. const char *name;
  1037. const unsigned int *num;
  1038. rootdn = of_find_node_by_path("/");
  1039. if (!rootdn)
  1040. return;
  1041. name = of_get_property(rootdn, "ibm,partition-name", NULL);
  1042. if (name)
  1043. strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
  1044. num = of_get_property(rootdn, "ibm,partition-no", NULL);
  1045. if (num)
  1046. vhost->partition_number = *num;
  1047. of_node_put(rootdn);
  1048. }
  1049. /**
  1050. * ibmvfc_set_login_info - Setup info for NPIV login
  1051. * @vhost: ibmvfc host struct
  1052. *
  1053. * Return value:
  1054. * none
  1055. **/
  1056. static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
  1057. {
  1058. struct ibmvfc_npiv_login *login_info = &vhost->login_info;
  1059. struct device_node *of_node = vhost->dev->of_node;
  1060. const char *location;
  1061. memset(login_info, 0, sizeof(*login_info));
  1062. login_info->ostype = IBMVFC_OS_LINUX;
  1063. login_info->max_dma_len = IBMVFC_MAX_SECTORS << 9;
  1064. login_info->max_payload = sizeof(struct ibmvfc_fcp_cmd_iu);
  1065. login_info->max_response = sizeof(struct ibmvfc_fcp_rsp);
  1066. login_info->partition_num = vhost->partition_number;
  1067. login_info->vfc_frame_version = 1;
  1068. login_info->fcp_version = 3;
  1069. login_info->flags = IBMVFC_FLUSH_ON_HALT;
  1070. if (vhost->client_migrated)
  1071. login_info->flags |= IBMVFC_CLIENT_MIGRATED;
  1072. login_info->max_cmds = max_requests + IBMVFC_NUM_INTERNAL_REQ;
  1073. login_info->capabilities = IBMVFC_CAN_MIGRATE;
  1074. login_info->async.va = vhost->async_crq.msg_token;
  1075. login_info->async.len = vhost->async_crq.size * sizeof(*vhost->async_crq.msgs);
  1076. strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
  1077. strncpy(login_info->device_name,
  1078. dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
  1079. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1080. location = location ? location : dev_name(vhost->dev);
  1081. strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
  1082. }
  1083. /**
  1084. * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
  1085. * @vhost: ibmvfc host who owns the event pool
  1086. *
  1087. * Returns zero on success.
  1088. **/
  1089. static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
  1090. {
  1091. int i;
  1092. struct ibmvfc_event_pool *pool = &vhost->pool;
  1093. ENTER;
  1094. pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
  1095. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  1096. if (!pool->events)
  1097. return -ENOMEM;
  1098. pool->iu_storage = dma_alloc_coherent(vhost->dev,
  1099. pool->size * sizeof(*pool->iu_storage),
  1100. &pool->iu_token, 0);
  1101. if (!pool->iu_storage) {
  1102. kfree(pool->events);
  1103. return -ENOMEM;
  1104. }
  1105. for (i = 0; i < pool->size; ++i) {
  1106. struct ibmvfc_event *evt = &pool->events[i];
  1107. atomic_set(&evt->free, 1);
  1108. evt->crq.valid = 0x80;
  1109. evt->crq.ioba = pool->iu_token + (sizeof(*evt->xfer_iu) * i);
  1110. evt->xfer_iu = pool->iu_storage + i;
  1111. evt->vhost = vhost;
  1112. evt->ext_list = NULL;
  1113. list_add_tail(&evt->queue, &vhost->free);
  1114. }
  1115. LEAVE;
  1116. return 0;
  1117. }
  1118. /**
  1119. * ibmvfc_free_event_pool - Frees memory of the event pool of a host
  1120. * @vhost: ibmvfc host who owns the event pool
  1121. *
  1122. **/
  1123. static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
  1124. {
  1125. int i;
  1126. struct ibmvfc_event_pool *pool = &vhost->pool;
  1127. ENTER;
  1128. for (i = 0; i < pool->size; ++i) {
  1129. list_del(&pool->events[i].queue);
  1130. BUG_ON(atomic_read(&pool->events[i].free) != 1);
  1131. if (pool->events[i].ext_list)
  1132. dma_pool_free(vhost->sg_pool,
  1133. pool->events[i].ext_list,
  1134. pool->events[i].ext_list_token);
  1135. }
  1136. kfree(pool->events);
  1137. dma_free_coherent(vhost->dev,
  1138. pool->size * sizeof(*pool->iu_storage),
  1139. pool->iu_storage, pool->iu_token);
  1140. LEAVE;
  1141. }
  1142. /**
  1143. * ibmvfc_get_event - Gets the next free event in pool
  1144. * @vhost: ibmvfc host struct
  1145. *
  1146. * Returns a free event from the pool.
  1147. **/
  1148. static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
  1149. {
  1150. struct ibmvfc_event *evt;
  1151. BUG_ON(list_empty(&vhost->free));
  1152. evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
  1153. atomic_set(&evt->free, 0);
  1154. list_del(&evt->queue);
  1155. return evt;
  1156. }
  1157. /**
  1158. * ibmvfc_init_event - Initialize fields in an event struct that are always
  1159. * required.
  1160. * @evt: The event
  1161. * @done: Routine to call when the event is responded to
  1162. * @format: SRP or MAD format
  1163. **/
  1164. static void ibmvfc_init_event(struct ibmvfc_event *evt,
  1165. void (*done) (struct ibmvfc_event *), u8 format)
  1166. {
  1167. evt->cmnd = NULL;
  1168. evt->sync_iu = NULL;
  1169. evt->crq.format = format;
  1170. evt->done = done;
  1171. evt->eh_comp = NULL;
  1172. }
  1173. /**
  1174. * ibmvfc_map_sg_list - Initialize scatterlist
  1175. * @scmd: scsi command struct
  1176. * @nseg: number of scatterlist segments
  1177. * @md: memory descriptor list to initialize
  1178. **/
  1179. static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
  1180. struct srp_direct_buf *md)
  1181. {
  1182. int i;
  1183. struct scatterlist *sg;
  1184. scsi_for_each_sg(scmd, sg, nseg, i) {
  1185. md[i].va = sg_dma_address(sg);
  1186. md[i].len = sg_dma_len(sg);
  1187. md[i].key = 0;
  1188. }
  1189. }
  1190. /**
  1191. * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
  1192. * @scmd: Scsi_Cmnd with the scatterlist
  1193. * @evt: ibmvfc event struct
  1194. * @vfc_cmd: vfc_cmd that contains the memory descriptor
  1195. * @dev: device for which to map dma memory
  1196. *
  1197. * Returns:
  1198. * 0 on success / non-zero on failure
  1199. **/
  1200. static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
  1201. struct ibmvfc_event *evt,
  1202. struct ibmvfc_cmd *vfc_cmd, struct device *dev)
  1203. {
  1204. int sg_mapped;
  1205. struct srp_direct_buf *data = &vfc_cmd->ioba;
  1206. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  1207. sg_mapped = scsi_dma_map(scmd);
  1208. if (!sg_mapped) {
  1209. vfc_cmd->flags |= IBMVFC_NO_MEM_DESC;
  1210. return 0;
  1211. } else if (unlikely(sg_mapped < 0)) {
  1212. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1213. scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
  1214. return sg_mapped;
  1215. }
  1216. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  1217. vfc_cmd->flags |= IBMVFC_WRITE;
  1218. vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
  1219. } else {
  1220. vfc_cmd->flags |= IBMVFC_READ;
  1221. vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
  1222. }
  1223. if (sg_mapped == 1) {
  1224. ibmvfc_map_sg_list(scmd, sg_mapped, data);
  1225. return 0;
  1226. }
  1227. vfc_cmd->flags |= IBMVFC_SCATTERLIST;
  1228. if (!evt->ext_list) {
  1229. evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
  1230. &evt->ext_list_token);
  1231. if (!evt->ext_list) {
  1232. scsi_dma_unmap(scmd);
  1233. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1234. scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
  1235. return -ENOMEM;
  1236. }
  1237. }
  1238. ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
  1239. data->va = evt->ext_list_token;
  1240. data->len = sg_mapped * sizeof(struct srp_direct_buf);
  1241. data->key = 0;
  1242. return 0;
  1243. }
  1244. /**
  1245. * ibmvfc_timeout - Internal command timeout handler
  1246. * @evt: struct ibmvfc_event that timed out
  1247. *
  1248. * Called when an internally generated command times out
  1249. **/
  1250. static void ibmvfc_timeout(struct ibmvfc_event *evt)
  1251. {
  1252. struct ibmvfc_host *vhost = evt->vhost;
  1253. dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
  1254. ibmvfc_reset_host(vhost);
  1255. }
  1256. /**
  1257. * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
  1258. * @evt: event to be sent
  1259. * @vhost: ibmvfc host struct
  1260. * @timeout: timeout in seconds - 0 means do not time command
  1261. *
  1262. * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
  1263. **/
  1264. static int ibmvfc_send_event(struct ibmvfc_event *evt,
  1265. struct ibmvfc_host *vhost, unsigned long timeout)
  1266. {
  1267. u64 *crq_as_u64 = (u64 *) &evt->crq;
  1268. int rc;
  1269. /* Copy the IU into the transfer area */
  1270. *evt->xfer_iu = evt->iu;
  1271. if (evt->crq.format == IBMVFC_CMD_FORMAT)
  1272. evt->xfer_iu->cmd.tag = (u64)evt;
  1273. else if (evt->crq.format == IBMVFC_MAD_FORMAT)
  1274. evt->xfer_iu->mad_common.tag = (u64)evt;
  1275. else
  1276. BUG();
  1277. list_add_tail(&evt->queue, &vhost->sent);
  1278. init_timer(&evt->timer);
  1279. if (timeout) {
  1280. evt->timer.data = (unsigned long) evt;
  1281. evt->timer.expires = jiffies + (timeout * HZ);
  1282. evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
  1283. add_timer(&evt->timer);
  1284. }
  1285. mb();
  1286. if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
  1287. list_del(&evt->queue);
  1288. del_timer(&evt->timer);
  1289. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  1290. * Firmware will send a CRQ with a transport event (0xFF) to
  1291. * tell this client what has happened to the transport. This
  1292. * will be handled in ibmvfc_handle_crq()
  1293. */
  1294. if (rc == H_CLOSED) {
  1295. if (printk_ratelimit())
  1296. dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
  1297. if (evt->cmnd)
  1298. scsi_dma_unmap(evt->cmnd);
  1299. ibmvfc_free_event(evt);
  1300. return SCSI_MLQUEUE_HOST_BUSY;
  1301. }
  1302. dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
  1303. if (evt->cmnd) {
  1304. evt->cmnd->result = DID_ERROR << 16;
  1305. evt->done = ibmvfc_scsi_eh_done;
  1306. } else
  1307. evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
  1308. evt->done(evt);
  1309. } else
  1310. ibmvfc_trc_start(evt);
  1311. return 0;
  1312. }
  1313. /**
  1314. * ibmvfc_log_error - Log an error for the failed command if appropriate
  1315. * @evt: ibmvfc event to log
  1316. *
  1317. **/
  1318. static void ibmvfc_log_error(struct ibmvfc_event *evt)
  1319. {
  1320. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1321. struct ibmvfc_host *vhost = evt->vhost;
  1322. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1323. struct scsi_cmnd *cmnd = evt->cmnd;
  1324. const char *err = unknown_error;
  1325. int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
  1326. int logerr = 0;
  1327. int rsp_code = 0;
  1328. if (index >= 0) {
  1329. logerr = cmd_status[index].log;
  1330. err = cmd_status[index].name;
  1331. }
  1332. if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
  1333. return;
  1334. if (rsp->flags & FCP_RSP_LEN_VALID)
  1335. rsp_code = rsp->data.info.rsp_code;
  1336. scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
  1337. "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
  1338. cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
  1339. rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
  1340. }
  1341. /**
  1342. * ibmvfc_relogin - Log back into the specified device
  1343. * @sdev: scsi device struct
  1344. *
  1345. **/
  1346. static void ibmvfc_relogin(struct scsi_device *sdev)
  1347. {
  1348. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1349. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1350. struct ibmvfc_target *tgt;
  1351. list_for_each_entry(tgt, &vhost->targets, queue) {
  1352. if (rport == tgt->rport) {
  1353. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  1354. break;
  1355. }
  1356. }
  1357. ibmvfc_reinit_host(vhost);
  1358. }
  1359. /**
  1360. * ibmvfc_scsi_done - Handle responses from commands
  1361. * @evt: ibmvfc event to be handled
  1362. *
  1363. * Used as a callback when sending scsi cmds.
  1364. **/
  1365. static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
  1366. {
  1367. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1368. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1369. struct scsi_cmnd *cmnd = evt->cmnd;
  1370. u32 rsp_len = 0;
  1371. u32 sense_len = rsp->fcp_sense_len;
  1372. if (cmnd) {
  1373. if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
  1374. scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
  1375. else if (rsp->flags & FCP_RESID_UNDER)
  1376. scsi_set_resid(cmnd, rsp->fcp_resid);
  1377. else
  1378. scsi_set_resid(cmnd, 0);
  1379. if (vfc_cmd->status) {
  1380. cmnd->result = ibmvfc_get_err_result(vfc_cmd);
  1381. if (rsp->flags & FCP_RSP_LEN_VALID)
  1382. rsp_len = rsp->fcp_rsp_len;
  1383. if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
  1384. sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
  1385. if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
  1386. memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
  1387. if ((vfc_cmd->status & IBMVFC_VIOS_FAILURE) && (vfc_cmd->error == IBMVFC_PLOGI_REQUIRED))
  1388. ibmvfc_relogin(cmnd->device);
  1389. if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
  1390. cmnd->result = (DID_ERROR << 16);
  1391. ibmvfc_log_error(evt);
  1392. }
  1393. if (!cmnd->result &&
  1394. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
  1395. cmnd->result = (DID_ERROR << 16);
  1396. scsi_dma_unmap(cmnd);
  1397. cmnd->scsi_done(cmnd);
  1398. }
  1399. if (evt->eh_comp)
  1400. complete(evt->eh_comp);
  1401. ibmvfc_free_event(evt);
  1402. }
  1403. /**
  1404. * ibmvfc_host_chkready - Check if the host can accept commands
  1405. * @vhost: struct ibmvfc host
  1406. *
  1407. * Returns:
  1408. * 1 if host can accept command / 0 if not
  1409. **/
  1410. static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
  1411. {
  1412. int result = 0;
  1413. switch (vhost->state) {
  1414. case IBMVFC_LINK_DEAD:
  1415. case IBMVFC_HOST_OFFLINE:
  1416. result = DID_NO_CONNECT << 16;
  1417. break;
  1418. case IBMVFC_NO_CRQ:
  1419. case IBMVFC_INITIALIZING:
  1420. case IBMVFC_HALTED:
  1421. case IBMVFC_LINK_DOWN:
  1422. result = DID_REQUEUE << 16;
  1423. break;
  1424. case IBMVFC_ACTIVE:
  1425. result = 0;
  1426. break;
  1427. };
  1428. return result;
  1429. }
  1430. /**
  1431. * ibmvfc_queuecommand - The queuecommand function of the scsi template
  1432. * @cmnd: struct scsi_cmnd to be executed
  1433. * @done: Callback function to be called when cmnd is completed
  1434. *
  1435. * Returns:
  1436. * 0 on success / other on failure
  1437. **/
  1438. static int ibmvfc_queuecommand(struct scsi_cmnd *cmnd,
  1439. void (*done) (struct scsi_cmnd *))
  1440. {
  1441. struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
  1442. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1443. struct ibmvfc_cmd *vfc_cmd;
  1444. struct ibmvfc_event *evt;
  1445. u8 tag[2];
  1446. int rc;
  1447. if (unlikely((rc = fc_remote_port_chkready(rport))) ||
  1448. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1449. cmnd->result = rc;
  1450. done(cmnd);
  1451. return 0;
  1452. }
  1453. cmnd->result = (DID_OK << 16);
  1454. evt = ibmvfc_get_event(vhost);
  1455. ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
  1456. evt->cmnd = cmnd;
  1457. cmnd->scsi_done = done;
  1458. vfc_cmd = &evt->iu.cmd;
  1459. memset(vfc_cmd, 0, sizeof(*vfc_cmd));
  1460. vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
  1461. vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
  1462. vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
  1463. vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
  1464. vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
  1465. vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
  1466. vfc_cmd->tgt_scsi_id = rport->port_id;
  1467. vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
  1468. int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
  1469. memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
  1470. if (scsi_populate_tag_msg(cmnd, tag)) {
  1471. vfc_cmd->task_tag = tag[1];
  1472. switch (tag[0]) {
  1473. case MSG_SIMPLE_TAG:
  1474. vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
  1475. break;
  1476. case MSG_HEAD_TAG:
  1477. vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
  1478. break;
  1479. case MSG_ORDERED_TAG:
  1480. vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
  1481. break;
  1482. };
  1483. }
  1484. if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
  1485. return ibmvfc_send_event(evt, vhost, 0);
  1486. ibmvfc_free_event(evt);
  1487. if (rc == -ENOMEM)
  1488. return SCSI_MLQUEUE_HOST_BUSY;
  1489. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1490. scmd_printk(KERN_ERR, cmnd,
  1491. "Failed to map DMA buffer for command. rc=%d\n", rc);
  1492. cmnd->result = DID_ERROR << 16;
  1493. done(cmnd);
  1494. return 0;
  1495. }
  1496. /**
  1497. * ibmvfc_sync_completion - Signal that a synchronous command has completed
  1498. * @evt: ibmvfc event struct
  1499. *
  1500. **/
  1501. static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
  1502. {
  1503. /* copy the response back */
  1504. if (evt->sync_iu)
  1505. *evt->sync_iu = *evt->xfer_iu;
  1506. complete(&evt->comp);
  1507. }
  1508. /**
  1509. * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
  1510. * @evt: struct ibmvfc_event
  1511. *
  1512. **/
  1513. static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
  1514. {
  1515. struct ibmvfc_host *vhost = evt->vhost;
  1516. ibmvfc_free_event(evt);
  1517. vhost->aborting_passthru = 0;
  1518. dev_info(vhost->dev, "Passthru command cancelled\n");
  1519. }
  1520. /**
  1521. * ibmvfc_bsg_timeout - Handle a BSG timeout
  1522. * @job: struct fc_bsg_job that timed out
  1523. *
  1524. * Returns:
  1525. * 0 on success / other on failure
  1526. **/
  1527. static int ibmvfc_bsg_timeout(struct fc_bsg_job *job)
  1528. {
  1529. struct ibmvfc_host *vhost = shost_priv(job->shost);
  1530. unsigned long port_id = (unsigned long)job->dd_data;
  1531. struct ibmvfc_event *evt;
  1532. struct ibmvfc_tmf *tmf;
  1533. unsigned long flags;
  1534. int rc;
  1535. ENTER;
  1536. spin_lock_irqsave(vhost->host->host_lock, flags);
  1537. if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
  1538. __ibmvfc_reset_host(vhost);
  1539. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1540. return 0;
  1541. }
  1542. vhost->aborting_passthru = 1;
  1543. evt = ibmvfc_get_event(vhost);
  1544. ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
  1545. tmf = &evt->iu.tmf;
  1546. memset(tmf, 0, sizeof(*tmf));
  1547. tmf->common.version = 1;
  1548. tmf->common.opcode = IBMVFC_TMF_MAD;
  1549. tmf->common.length = sizeof(*tmf);
  1550. tmf->scsi_id = port_id;
  1551. tmf->cancel_key = IBMVFC_PASSTHRU_CANCEL_KEY;
  1552. tmf->my_cancel_key = IBMVFC_INTERNAL_CANCEL_KEY;
  1553. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1554. if (rc != 0) {
  1555. vhost->aborting_passthru = 0;
  1556. dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
  1557. rc = -EIO;
  1558. } else
  1559. dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
  1560. port_id);
  1561. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1562. LEAVE;
  1563. return rc;
  1564. }
  1565. /**
  1566. * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
  1567. * @vhost: struct ibmvfc_host to send command
  1568. * @port_id: port ID to send command
  1569. *
  1570. * Returns:
  1571. * 0 on success / other on failure
  1572. **/
  1573. static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
  1574. {
  1575. struct ibmvfc_port_login *plogi;
  1576. struct ibmvfc_target *tgt;
  1577. struct ibmvfc_event *evt;
  1578. union ibmvfc_iu rsp_iu;
  1579. unsigned long flags;
  1580. int rc = 0, issue_login = 1;
  1581. ENTER;
  1582. spin_lock_irqsave(vhost->host->host_lock, flags);
  1583. list_for_each_entry(tgt, &vhost->targets, queue) {
  1584. if (tgt->scsi_id == port_id) {
  1585. issue_login = 0;
  1586. break;
  1587. }
  1588. }
  1589. if (!issue_login)
  1590. goto unlock_out;
  1591. if (unlikely((rc = ibmvfc_host_chkready(vhost))))
  1592. goto unlock_out;
  1593. evt = ibmvfc_get_event(vhost);
  1594. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1595. plogi = &evt->iu.plogi;
  1596. memset(plogi, 0, sizeof(*plogi));
  1597. plogi->common.version = 1;
  1598. plogi->common.opcode = IBMVFC_PORT_LOGIN;
  1599. plogi->common.length = sizeof(*plogi);
  1600. plogi->scsi_id = port_id;
  1601. evt->sync_iu = &rsp_iu;
  1602. init_completion(&evt->comp);
  1603. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1604. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1605. if (rc)
  1606. return -EIO;
  1607. wait_for_completion(&evt->comp);
  1608. if (rsp_iu.plogi.common.status)
  1609. rc = -EIO;
  1610. spin_lock_irqsave(vhost->host->host_lock, flags);
  1611. ibmvfc_free_event(evt);
  1612. unlock_out:
  1613. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1614. LEAVE;
  1615. return rc;
  1616. }
  1617. /**
  1618. * ibmvfc_bsg_request - Handle a BSG request
  1619. * @job: struct fc_bsg_job to be executed
  1620. *
  1621. * Returns:
  1622. * 0 on success / other on failure
  1623. **/
  1624. static int ibmvfc_bsg_request(struct fc_bsg_job *job)
  1625. {
  1626. struct ibmvfc_host *vhost = shost_priv(job->shost);
  1627. struct fc_rport *rport = job->rport;
  1628. struct ibmvfc_passthru_mad *mad;
  1629. struct ibmvfc_event *evt;
  1630. union ibmvfc_iu rsp_iu;
  1631. unsigned long flags, port_id = -1;
  1632. unsigned int code = job->request->msgcode;
  1633. int rc = 0, req_seg, rsp_seg, issue_login = 0;
  1634. u32 fc_flags, rsp_len;
  1635. ENTER;
  1636. job->reply->reply_payload_rcv_len = 0;
  1637. if (rport)
  1638. port_id = rport->port_id;
  1639. switch (code) {
  1640. case FC_BSG_HST_ELS_NOLOGIN:
  1641. port_id = (job->request->rqst_data.h_els.port_id[0] << 16) |
  1642. (job->request->rqst_data.h_els.port_id[1] << 8) |
  1643. job->request->rqst_data.h_els.port_id[2];
  1644. case FC_BSG_RPT_ELS:
  1645. fc_flags = IBMVFC_FC_ELS;
  1646. break;
  1647. case FC_BSG_HST_CT:
  1648. issue_login = 1;
  1649. port_id = (job->request->rqst_data.h_ct.port_id[0] << 16) |
  1650. (job->request->rqst_data.h_ct.port_id[1] << 8) |
  1651. job->request->rqst_data.h_ct.port_id[2];
  1652. case FC_BSG_RPT_CT:
  1653. fc_flags = IBMVFC_FC_CT_IU;
  1654. break;
  1655. default:
  1656. return -ENOTSUPP;
  1657. };
  1658. if (port_id == -1)
  1659. return -EINVAL;
  1660. if (!mutex_trylock(&vhost->passthru_mutex))
  1661. return -EBUSY;
  1662. job->dd_data = (void *)port_id;
  1663. req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
  1664. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1665. if (!req_seg) {
  1666. mutex_unlock(&vhost->passthru_mutex);
  1667. return -ENOMEM;
  1668. }
  1669. rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
  1670. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1671. if (!rsp_seg) {
  1672. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  1673. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1674. mutex_unlock(&vhost->passthru_mutex);
  1675. return -ENOMEM;
  1676. }
  1677. if (req_seg > 1 || rsp_seg > 1) {
  1678. rc = -EINVAL;
  1679. goto out;
  1680. }
  1681. if (issue_login)
  1682. rc = ibmvfc_bsg_plogi(vhost, port_id);
  1683. spin_lock_irqsave(vhost->host->host_lock, flags);
  1684. if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
  1685. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1686. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1687. goto out;
  1688. }
  1689. evt = ibmvfc_get_event(vhost);
  1690. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1691. mad = &evt->iu.passthru;
  1692. memset(mad, 0, sizeof(*mad));
  1693. mad->common.version = 1;
  1694. mad->common.opcode = IBMVFC_PASSTHRU;
  1695. mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
  1696. mad->cmd_ioba.va = (u64)evt->crq.ioba +
  1697. offsetof(struct ibmvfc_passthru_mad, iu);
  1698. mad->cmd_ioba.len = sizeof(mad->iu);
  1699. mad->iu.cmd_len = job->request_payload.payload_len;
  1700. mad->iu.rsp_len = job->reply_payload.payload_len;
  1701. mad->iu.flags = fc_flags;
  1702. mad->iu.cancel_key = IBMVFC_PASSTHRU_CANCEL_KEY;
  1703. mad->iu.cmd.va = sg_dma_address(job->request_payload.sg_list);
  1704. mad->iu.cmd.len = sg_dma_len(job->request_payload.sg_list);
  1705. mad->iu.rsp.va = sg_dma_address(job->reply_payload.sg_list);
  1706. mad->iu.rsp.len = sg_dma_len(job->reply_payload.sg_list);
  1707. mad->iu.scsi_id = port_id;
  1708. mad->iu.tag = (u64)evt;
  1709. rsp_len = mad->iu.rsp.len;
  1710. evt->sync_iu = &rsp_iu;
  1711. init_completion(&evt->comp);
  1712. rc = ibmvfc_send_event(evt, vhost, 0);
  1713. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1714. if (rc) {
  1715. rc = -EIO;
  1716. goto out;
  1717. }
  1718. wait_for_completion(&evt->comp);
  1719. if (rsp_iu.passthru.common.status)
  1720. rc = -EIO;
  1721. else
  1722. job->reply->reply_payload_rcv_len = rsp_len;
  1723. spin_lock_irqsave(vhost->host->host_lock, flags);
  1724. ibmvfc_free_event(evt);
  1725. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1726. job->reply->result = rc;
  1727. job->job_done(job);
  1728. rc = 0;
  1729. out:
  1730. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  1731. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1732. dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
  1733. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1734. mutex_unlock(&vhost->passthru_mutex);
  1735. LEAVE;
  1736. return rc;
  1737. }
  1738. /**
  1739. * ibmvfc_reset_device - Reset the device with the specified reset type
  1740. * @sdev: scsi device to reset
  1741. * @type: reset type
  1742. * @desc: reset type description for log messages
  1743. *
  1744. * Returns:
  1745. * 0 on success / other on failure
  1746. **/
  1747. static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
  1748. {
  1749. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1750. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1751. struct ibmvfc_cmd *tmf;
  1752. struct ibmvfc_event *evt = NULL;
  1753. union ibmvfc_iu rsp_iu;
  1754. struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
  1755. int rsp_rc = -EBUSY;
  1756. unsigned long flags;
  1757. int rsp_code = 0;
  1758. spin_lock_irqsave(vhost->host->host_lock, flags);
  1759. if (vhost->state == IBMVFC_ACTIVE) {
  1760. evt = ibmvfc_get_event(vhost);
  1761. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  1762. tmf = &evt->iu.cmd;
  1763. memset(tmf, 0, sizeof(*tmf));
  1764. tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
  1765. tmf->resp.len = sizeof(tmf->rsp);
  1766. tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
  1767. tmf->payload_len = sizeof(tmf->iu);
  1768. tmf->resp_len = sizeof(tmf->rsp);
  1769. tmf->cancel_key = (unsigned long)sdev->hostdata;
  1770. tmf->tgt_scsi_id = rport->port_id;
  1771. int_to_scsilun(sdev->lun, &tmf->iu.lun);
  1772. tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
  1773. tmf->iu.tmf_flags = type;
  1774. evt->sync_iu = &rsp_iu;
  1775. init_completion(&evt->comp);
  1776. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1777. }
  1778. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1779. if (rsp_rc != 0) {
  1780. sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
  1781. desc, rsp_rc);
  1782. return -EIO;
  1783. }
  1784. sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
  1785. wait_for_completion(&evt->comp);
  1786. if (rsp_iu.cmd.status)
  1787. rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
  1788. if (rsp_code) {
  1789. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  1790. rsp_code = fc_rsp->data.info.rsp_code;
  1791. sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
  1792. "flags: %x fcp_rsp: %x, scsi_status: %x\n",
  1793. desc, ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
  1794. rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
  1795. fc_rsp->scsi_status);
  1796. rsp_rc = -EIO;
  1797. } else
  1798. sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
  1799. spin_lock_irqsave(vhost->host->host_lock, flags);
  1800. ibmvfc_free_event(evt);
  1801. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1802. return rsp_rc;
  1803. }
  1804. /**
  1805. * ibmvfc_match_rport - Match function for specified remote port
  1806. * @evt: ibmvfc event struct
  1807. * @device: device to match (rport)
  1808. *
  1809. * Returns:
  1810. * 1 if event matches rport / 0 if event does not match rport
  1811. **/
  1812. static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
  1813. {
  1814. struct fc_rport *cmd_rport;
  1815. if (evt->cmnd) {
  1816. cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
  1817. if (cmd_rport == rport)
  1818. return 1;
  1819. }
  1820. return 0;
  1821. }
  1822. /**
  1823. * ibmvfc_match_target - Match function for specified target
  1824. * @evt: ibmvfc event struct
  1825. * @device: device to match (starget)
  1826. *
  1827. * Returns:
  1828. * 1 if event matches starget / 0 if event does not match starget
  1829. **/
  1830. static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
  1831. {
  1832. if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
  1833. return 1;
  1834. return 0;
  1835. }
  1836. /**
  1837. * ibmvfc_match_lun - Match function for specified LUN
  1838. * @evt: ibmvfc event struct
  1839. * @device: device to match (sdev)
  1840. *
  1841. * Returns:
  1842. * 1 if event matches sdev / 0 if event does not match sdev
  1843. **/
  1844. static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
  1845. {
  1846. if (evt->cmnd && evt->cmnd->device == device)
  1847. return 1;
  1848. return 0;
  1849. }
  1850. /**
  1851. * ibmvfc_wait_for_ops - Wait for ops to complete
  1852. * @vhost: ibmvfc host struct
  1853. * @device: device to match (starget or sdev)
  1854. * @match: match function
  1855. *
  1856. * Returns:
  1857. * SUCCESS / FAILED
  1858. **/
  1859. static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
  1860. int (*match) (struct ibmvfc_event *, void *))
  1861. {
  1862. struct ibmvfc_event *evt;
  1863. DECLARE_COMPLETION_ONSTACK(comp);
  1864. int wait;
  1865. unsigned long flags;
  1866. signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
  1867. ENTER;
  1868. do {
  1869. wait = 0;
  1870. spin_lock_irqsave(vhost->host->host_lock, flags);
  1871. list_for_each_entry(evt, &vhost->sent, queue) {
  1872. if (match(evt, device)) {
  1873. evt->eh_comp = &comp;
  1874. wait++;
  1875. }
  1876. }
  1877. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1878. if (wait) {
  1879. timeout = wait_for_completion_timeout(&comp, timeout);
  1880. if (!timeout) {
  1881. wait = 0;
  1882. spin_lock_irqsave(vhost->host->host_lock, flags);
  1883. list_for_each_entry(evt, &vhost->sent, queue) {
  1884. if (match(evt, device)) {
  1885. evt->eh_comp = NULL;
  1886. wait++;
  1887. }
  1888. }
  1889. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1890. if (wait)
  1891. dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
  1892. LEAVE;
  1893. return wait ? FAILED : SUCCESS;
  1894. }
  1895. }
  1896. } while (wait);
  1897. LEAVE;
  1898. return SUCCESS;
  1899. }
  1900. /**
  1901. * ibmvfc_cancel_all - Cancel all outstanding commands to the device
  1902. * @sdev: scsi device to cancel commands
  1903. * @type: type of error recovery being performed
  1904. *
  1905. * This sends a cancel to the VIOS for the specified device. This does
  1906. * NOT send any abort to the actual device. That must be done separately.
  1907. *
  1908. * Returns:
  1909. * 0 on success / other on failure
  1910. **/
  1911. static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
  1912. {
  1913. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1914. struct scsi_target *starget = scsi_target(sdev);
  1915. struct fc_rport *rport = starget_to_rport(starget);
  1916. struct ibmvfc_tmf *tmf;
  1917. struct ibmvfc_event *evt, *found_evt;
  1918. union ibmvfc_iu rsp;
  1919. int rsp_rc = -EBUSY;
  1920. unsigned long flags;
  1921. u16 status;
  1922. ENTER;
  1923. spin_lock_irqsave(vhost->host->host_lock, flags);
  1924. found_evt = NULL;
  1925. list_for_each_entry(evt, &vhost->sent, queue) {
  1926. if (evt->cmnd && evt->cmnd->device == sdev) {
  1927. found_evt = evt;
  1928. break;
  1929. }
  1930. }
  1931. if (!found_evt) {
  1932. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1933. sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
  1934. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1935. return 0;
  1936. }
  1937. if (vhost->state == IBMVFC_ACTIVE) {
  1938. evt = ibmvfc_get_event(vhost);
  1939. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1940. tmf = &evt->iu.tmf;
  1941. memset(tmf, 0, sizeof(*tmf));
  1942. tmf->common.version = 1;
  1943. tmf->common.opcode = IBMVFC_TMF_MAD;
  1944. tmf->common.length = sizeof(*tmf);
  1945. tmf->scsi_id = rport->port_id;
  1946. int_to_scsilun(sdev->lun, &tmf->lun);
  1947. tmf->flags = (type | IBMVFC_TMF_LUA_VALID);
  1948. tmf->cancel_key = (unsigned long)sdev->hostdata;
  1949. tmf->my_cancel_key = (unsigned long)starget->hostdata;
  1950. evt->sync_iu = &rsp;
  1951. init_completion(&evt->comp);
  1952. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1953. }
  1954. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1955. if (rsp_rc != 0) {
  1956. sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
  1957. return -EIO;
  1958. }
  1959. sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
  1960. wait_for_completion(&evt->comp);
  1961. status = rsp.mad_common.status;
  1962. spin_lock_irqsave(vhost->host->host_lock, flags);
  1963. ibmvfc_free_event(evt);
  1964. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1965. if (status != IBMVFC_MAD_SUCCESS) {
  1966. sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
  1967. return -EIO;
  1968. }
  1969. sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
  1970. return 0;
  1971. }
  1972. /**
  1973. * ibmvfc_match_key - Match function for specified cancel key
  1974. * @evt: ibmvfc event struct
  1975. * @key: cancel key to match
  1976. *
  1977. * Returns:
  1978. * 1 if event matches key / 0 if event does not match key
  1979. **/
  1980. static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
  1981. {
  1982. unsigned long cancel_key = (unsigned long)key;
  1983. if (evt->crq.format == IBMVFC_CMD_FORMAT &&
  1984. evt->iu.cmd.cancel_key == cancel_key)
  1985. return 1;
  1986. return 0;
  1987. }
  1988. /**
  1989. * ibmvfc_abort_task_set - Abort outstanding commands to the device
  1990. * @sdev: scsi device to abort commands
  1991. *
  1992. * This sends an Abort Task Set to the VIOS for the specified device. This does
  1993. * NOT send any cancel to the VIOS. That must be done separately.
  1994. *
  1995. * Returns:
  1996. * 0 on success / other on failure
  1997. **/
  1998. static int ibmvfc_abort_task_set(struct scsi_device *sdev)
  1999. {
  2000. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2001. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2002. struct ibmvfc_cmd *tmf;
  2003. struct ibmvfc_event *evt, *found_evt;
  2004. union ibmvfc_iu rsp_iu;
  2005. struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
  2006. int rc, rsp_rc = -EBUSY;
  2007. unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
  2008. int rsp_code = 0;
  2009. spin_lock_irqsave(vhost->host->host_lock, flags);
  2010. found_evt = NULL;
  2011. list_for_each_entry(evt, &vhost->sent, queue) {
  2012. if (evt->cmnd && evt->cmnd->device == sdev) {
  2013. found_evt = evt;
  2014. break;
  2015. }
  2016. }
  2017. if (!found_evt) {
  2018. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  2019. sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
  2020. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2021. return 0;
  2022. }
  2023. if (vhost->state == IBMVFC_ACTIVE) {
  2024. evt = ibmvfc_get_event(vhost);
  2025. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  2026. tmf = &evt->iu.cmd;
  2027. memset(tmf, 0, sizeof(*tmf));
  2028. tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
  2029. tmf->resp.len = sizeof(tmf->rsp);
  2030. tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
  2031. tmf->payload_len = sizeof(tmf->iu);
  2032. tmf->resp_len = sizeof(tmf->rsp);
  2033. tmf->cancel_key = (unsigned long)sdev->hostdata;
  2034. tmf->tgt_scsi_id = rport->port_id;
  2035. int_to_scsilun(sdev->lun, &tmf->iu.lun);
  2036. tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
  2037. tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
  2038. evt->sync_iu = &rsp_iu;
  2039. init_completion(&evt->comp);
  2040. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  2041. }
  2042. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2043. if (rsp_rc != 0) {
  2044. sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
  2045. return -EIO;
  2046. }
  2047. sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
  2048. timeout = wait_for_completion_timeout(&evt->comp, timeout);
  2049. if (!timeout) {
  2050. rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
  2051. if (!rc) {
  2052. rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
  2053. if (rc == SUCCESS)
  2054. rc = 0;
  2055. }
  2056. if (rc) {
  2057. sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
  2058. ibmvfc_reset_host(vhost);
  2059. rsp_rc = 0;
  2060. goto out;
  2061. }
  2062. }
  2063. if (rsp_iu.cmd.status)
  2064. rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
  2065. if (rsp_code) {
  2066. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  2067. rsp_code = fc_rsp->data.info.rsp_code;
  2068. sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
  2069. "flags: %x fcp_rsp: %x, scsi_status: %x\n",
  2070. ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
  2071. rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
  2072. fc_rsp->scsi_status);
  2073. rsp_rc = -EIO;
  2074. } else
  2075. sdev_printk(KERN_INFO, sdev, "Abort successful\n");
  2076. out:
  2077. spin_lock_irqsave(vhost->host->host_lock, flags);
  2078. ibmvfc_free_event(evt);
  2079. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2080. return rsp_rc;
  2081. }
  2082. /**
  2083. * ibmvfc_eh_abort_handler - Abort a command
  2084. * @cmd: scsi command to abort
  2085. *
  2086. * Returns:
  2087. * SUCCESS / FAILED
  2088. **/
  2089. static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
  2090. {
  2091. struct scsi_device *sdev = cmd->device;
  2092. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2093. int cancel_rc, abort_rc;
  2094. int rc = FAILED;
  2095. ENTER;
  2096. fc_block_scsi_eh(cmd);
  2097. ibmvfc_wait_while_resetting(vhost);
  2098. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
  2099. abort_rc = ibmvfc_abort_task_set(sdev);
  2100. if (!cancel_rc && !abort_rc)
  2101. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2102. LEAVE;
  2103. return rc;
  2104. }
  2105. /**
  2106. * ibmvfc_eh_device_reset_handler - Reset a single LUN
  2107. * @cmd: scsi command struct
  2108. *
  2109. * Returns:
  2110. * SUCCESS / FAILED
  2111. **/
  2112. static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
  2113. {
  2114. struct scsi_device *sdev = cmd->device;
  2115. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2116. int cancel_rc, reset_rc;
  2117. int rc = FAILED;
  2118. ENTER;
  2119. fc_block_scsi_eh(cmd);
  2120. ibmvfc_wait_while_resetting(vhost);
  2121. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
  2122. reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
  2123. if (!cancel_rc && !reset_rc)
  2124. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2125. LEAVE;
  2126. return rc;
  2127. }
  2128. /**
  2129. * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
  2130. * @sdev: scsi device struct
  2131. * @data: return code
  2132. *
  2133. **/
  2134. static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
  2135. {
  2136. unsigned long *rc = data;
  2137. *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
  2138. }
  2139. /**
  2140. * ibmvfc_eh_target_reset_handler - Reset the target
  2141. * @cmd: scsi command struct
  2142. *
  2143. * Returns:
  2144. * SUCCESS / FAILED
  2145. **/
  2146. static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
  2147. {
  2148. struct scsi_device *sdev = cmd->device;
  2149. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2150. struct scsi_target *starget = scsi_target(sdev);
  2151. int reset_rc;
  2152. int rc = FAILED;
  2153. unsigned long cancel_rc = 0;
  2154. ENTER;
  2155. fc_block_scsi_eh(cmd);
  2156. ibmvfc_wait_while_resetting(vhost);
  2157. starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
  2158. reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
  2159. if (!cancel_rc && !reset_rc)
  2160. rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
  2161. LEAVE;
  2162. return rc;
  2163. }
  2164. /**
  2165. * ibmvfc_eh_host_reset_handler - Reset the connection to the server
  2166. * @cmd: struct scsi_cmnd having problems
  2167. *
  2168. **/
  2169. static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
  2170. {
  2171. int rc;
  2172. struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
  2173. fc_block_scsi_eh(cmd);
  2174. dev_err(vhost->dev, "Resetting connection due to error recovery\n");
  2175. rc = ibmvfc_issue_fc_host_lip(vhost->host);
  2176. return rc ? FAILED : SUCCESS;
  2177. }
  2178. /**
  2179. * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
  2180. * @rport: rport struct
  2181. *
  2182. * Return value:
  2183. * none
  2184. **/
  2185. static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
  2186. {
  2187. struct Scsi_Host *shost = rport_to_shost(rport);
  2188. struct ibmvfc_host *vhost = shost_priv(shost);
  2189. struct fc_rport *dev_rport;
  2190. struct scsi_device *sdev;
  2191. unsigned long rc;
  2192. ENTER;
  2193. shost_for_each_device(sdev, shost) {
  2194. dev_rport = starget_to_rport(scsi_target(sdev));
  2195. if (dev_rport != rport)
  2196. continue;
  2197. ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
  2198. ibmvfc_abort_task_set(sdev);
  2199. }
  2200. rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
  2201. if (rc == FAILED)
  2202. ibmvfc_issue_fc_host_lip(shost);
  2203. LEAVE;
  2204. }
  2205. static const struct ibmvfc_async_desc ae_desc [] = {
  2206. { IBMVFC_AE_ELS_PLOGI, "PLOGI", IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2207. { IBMVFC_AE_ELS_LOGO, "LOGO", IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2208. { IBMVFC_AE_ELS_PRLO, "PRLO", IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2209. { IBMVFC_AE_SCN_NPORT, "N-Port SCN", IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2210. { IBMVFC_AE_SCN_GROUP, "Group SCN", IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2211. { IBMVFC_AE_SCN_DOMAIN, "Domain SCN", IBMVFC_DEFAULT_LOG_LEVEL },
  2212. { IBMVFC_AE_SCN_FABRIC, "Fabric SCN", IBMVFC_DEFAULT_LOG_LEVEL },
  2213. { IBMVFC_AE_LINK_UP, "Link Up", IBMVFC_DEFAULT_LOG_LEVEL },
  2214. { IBMVFC_AE_LINK_DOWN, "Link Down", IBMVFC_DEFAULT_LOG_LEVEL },
  2215. { IBMVFC_AE_LINK_DEAD, "Link Dead", IBMVFC_DEFAULT_LOG_LEVEL },
  2216. { IBMVFC_AE_HALT, "Halt", IBMVFC_DEFAULT_LOG_LEVEL },
  2217. { IBMVFC_AE_RESUME, "Resume", IBMVFC_DEFAULT_LOG_LEVEL },
  2218. { IBMVFC_AE_ADAPTER_FAILED, "Adapter Failed", IBMVFC_DEFAULT_LOG_LEVEL },
  2219. };
  2220. static const struct ibmvfc_async_desc unknown_ae = {
  2221. 0, "Unknown async", IBMVFC_DEFAULT_LOG_LEVEL
  2222. };
  2223. /**
  2224. * ibmvfc_get_ae_desc - Get text description for async event
  2225. * @ae: async event
  2226. *
  2227. **/
  2228. static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
  2229. {
  2230. int i;
  2231. for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
  2232. if (ae_desc[i].ae == ae)
  2233. return &ae_desc[i];
  2234. return &unknown_ae;
  2235. }
  2236. static const struct {
  2237. enum ibmvfc_ae_link_state state;
  2238. const char *desc;
  2239. } link_desc [] = {
  2240. { IBMVFC_AE_LS_LINK_UP, " link up" },
  2241. { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
  2242. { IBMVFC_AE_LS_LINK_DOWN, " link down" },
  2243. { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
  2244. };
  2245. /**
  2246. * ibmvfc_get_link_state - Get text description for link state
  2247. * @state: link state
  2248. *
  2249. **/
  2250. static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
  2251. {
  2252. int i;
  2253. for (i = 0; i < ARRAY_SIZE(link_desc); i++)
  2254. if (link_desc[i].state == state)
  2255. return link_desc[i].desc;
  2256. return "";
  2257. }
  2258. /**
  2259. * ibmvfc_handle_async - Handle an async event from the adapter
  2260. * @crq: crq to process
  2261. * @vhost: ibmvfc host struct
  2262. *
  2263. **/
  2264. static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
  2265. struct ibmvfc_host *vhost)
  2266. {
  2267. const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(crq->event);
  2268. struct ibmvfc_target *tgt;
  2269. ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
  2270. " node_name: %llx%s\n", desc->desc, crq->scsi_id, crq->wwpn, crq->node_name,
  2271. ibmvfc_get_link_state(crq->link_state));
  2272. switch (crq->event) {
  2273. case IBMVFC_AE_RESUME:
  2274. switch (crq->link_state) {
  2275. case IBMVFC_AE_LS_LINK_DOWN:
  2276. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2277. break;
  2278. case IBMVFC_AE_LS_LINK_DEAD:
  2279. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2280. break;
  2281. case IBMVFC_AE_LS_LINK_UP:
  2282. case IBMVFC_AE_LS_LINK_BOUNCED:
  2283. default:
  2284. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2285. vhost->delay_init = 1;
  2286. __ibmvfc_reset_host(vhost);
  2287. break;
  2288. };
  2289. break;
  2290. case IBMVFC_AE_LINK_UP:
  2291. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2292. vhost->delay_init = 1;
  2293. __ibmvfc_reset_host(vhost);
  2294. break;
  2295. case IBMVFC_AE_SCN_FABRIC:
  2296. case IBMVFC_AE_SCN_DOMAIN:
  2297. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2298. vhost->delay_init = 1;
  2299. __ibmvfc_reset_host(vhost);
  2300. break;
  2301. case IBMVFC_AE_SCN_NPORT:
  2302. case IBMVFC_AE_SCN_GROUP:
  2303. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2304. ibmvfc_reinit_host(vhost);
  2305. break;
  2306. case IBMVFC_AE_ELS_LOGO:
  2307. case IBMVFC_AE_ELS_PRLO:
  2308. case IBMVFC_AE_ELS_PLOGI:
  2309. list_for_each_entry(tgt, &vhost->targets, queue) {
  2310. if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
  2311. break;
  2312. if (crq->scsi_id && tgt->scsi_id != crq->scsi_id)
  2313. continue;
  2314. if (crq->wwpn && tgt->ids.port_name != crq->wwpn)
  2315. continue;
  2316. if (crq->node_name && tgt->ids.node_name != crq->node_name)
  2317. continue;
  2318. if (tgt->need_login && crq->event == IBMVFC_AE_ELS_LOGO)
  2319. tgt->logo_rcvd = 1;
  2320. if (!tgt->need_login || crq->event == IBMVFC_AE_ELS_PLOGI) {
  2321. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2322. ibmvfc_reinit_host(vhost);
  2323. }
  2324. }
  2325. break;
  2326. case IBMVFC_AE_LINK_DOWN:
  2327. case IBMVFC_AE_ADAPTER_FAILED:
  2328. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2329. break;
  2330. case IBMVFC_AE_LINK_DEAD:
  2331. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2332. break;
  2333. case IBMVFC_AE_HALT:
  2334. ibmvfc_link_down(vhost, IBMVFC_HALTED);
  2335. break;
  2336. default:
  2337. dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
  2338. break;
  2339. };
  2340. }
  2341. /**
  2342. * ibmvfc_handle_crq - Handles and frees received events in the CRQ
  2343. * @crq: Command/Response queue
  2344. * @vhost: ibmvfc host struct
  2345. *
  2346. **/
  2347. static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
  2348. {
  2349. long rc;
  2350. struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
  2351. switch (crq->valid) {
  2352. case IBMVFC_CRQ_INIT_RSP:
  2353. switch (crq->format) {
  2354. case IBMVFC_CRQ_INIT:
  2355. dev_info(vhost->dev, "Partner initialized\n");
  2356. /* Send back a response */
  2357. rc = ibmvfc_send_crq_init_complete(vhost);
  2358. if (rc == 0)
  2359. ibmvfc_init_host(vhost);
  2360. else
  2361. dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
  2362. break;
  2363. case IBMVFC_CRQ_INIT_COMPLETE:
  2364. dev_info(vhost->dev, "Partner initialization complete\n");
  2365. ibmvfc_init_host(vhost);
  2366. break;
  2367. default:
  2368. dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
  2369. }
  2370. return;
  2371. case IBMVFC_CRQ_XPORT_EVENT:
  2372. vhost->state = IBMVFC_NO_CRQ;
  2373. vhost->logged_in = 0;
  2374. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  2375. if (crq->format == IBMVFC_PARTITION_MIGRATED) {
  2376. /* We need to re-setup the interpartition connection */
  2377. dev_info(vhost->dev, "Re-enabling adapter\n");
  2378. vhost->client_migrated = 1;
  2379. ibmvfc_purge_requests(vhost, DID_REQUEUE);
  2380. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2381. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
  2382. } else {
  2383. dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
  2384. ibmvfc_purge_requests(vhost, DID_ERROR);
  2385. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2386. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  2387. }
  2388. return;
  2389. case IBMVFC_CRQ_CMD_RSP:
  2390. break;
  2391. default:
  2392. dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
  2393. return;
  2394. }
  2395. if (crq->format == IBMVFC_ASYNC_EVENT)
  2396. return;
  2397. /* The only kind of payload CRQs we should get are responses to
  2398. * things we send. Make sure this response is to something we
  2399. * actually sent
  2400. */
  2401. if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
  2402. dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
  2403. crq->ioba);
  2404. return;
  2405. }
  2406. if (unlikely(atomic_read(&evt->free))) {
  2407. dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
  2408. crq->ioba);
  2409. return;
  2410. }
  2411. del_timer(&evt->timer);
  2412. list_del(&evt->queue);
  2413. ibmvfc_trc_end(evt);
  2414. evt->done(evt);
  2415. }
  2416. /**
  2417. * ibmvfc_scan_finished - Check if the device scan is done.
  2418. * @shost: scsi host struct
  2419. * @time: current elapsed time
  2420. *
  2421. * Returns:
  2422. * 0 if scan is not done / 1 if scan is done
  2423. **/
  2424. static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  2425. {
  2426. unsigned long flags;
  2427. struct ibmvfc_host *vhost = shost_priv(shost);
  2428. int done = 0;
  2429. spin_lock_irqsave(shost->host_lock, flags);
  2430. if (time >= (init_timeout * HZ)) {
  2431. dev_info(vhost->dev, "Scan taking longer than %d seconds, "
  2432. "continuing initialization\n", init_timeout);
  2433. done = 1;
  2434. }
  2435. if (vhost->scan_complete)
  2436. done = 1;
  2437. spin_unlock_irqrestore(shost->host_lock, flags);
  2438. return done;
  2439. }
  2440. /**
  2441. * ibmvfc_slave_alloc - Setup the device's task set value
  2442. * @sdev: struct scsi_device device to configure
  2443. *
  2444. * Set the device's task set value so that error handling works as
  2445. * expected.
  2446. *
  2447. * Returns:
  2448. * 0 on success / -ENXIO if device does not exist
  2449. **/
  2450. static int ibmvfc_slave_alloc(struct scsi_device *sdev)
  2451. {
  2452. struct Scsi_Host *shost = sdev->host;
  2453. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2454. struct ibmvfc_host *vhost = shost_priv(shost);
  2455. unsigned long flags = 0;
  2456. if (!rport || fc_remote_port_chkready(rport))
  2457. return -ENXIO;
  2458. spin_lock_irqsave(shost->host_lock, flags);
  2459. sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
  2460. spin_unlock_irqrestore(shost->host_lock, flags);
  2461. return 0;
  2462. }
  2463. /**
  2464. * ibmvfc_target_alloc - Setup the target's task set value
  2465. * @starget: struct scsi_target
  2466. *
  2467. * Set the target's task set value so that error handling works as
  2468. * expected.
  2469. *
  2470. * Returns:
  2471. * 0 on success / -ENXIO if device does not exist
  2472. **/
  2473. static int ibmvfc_target_alloc(struct scsi_target *starget)
  2474. {
  2475. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2476. struct ibmvfc_host *vhost = shost_priv(shost);
  2477. unsigned long flags = 0;
  2478. spin_lock_irqsave(shost->host_lock, flags);
  2479. starget->hostdata = (void *)(unsigned long)vhost->task_set++;
  2480. spin_unlock_irqrestore(shost->host_lock, flags);
  2481. return 0;
  2482. }
  2483. /**
  2484. * ibmvfc_slave_configure - Configure the device
  2485. * @sdev: struct scsi_device device to configure
  2486. *
  2487. * Enable allow_restart for a device if it is a disk. Adjust the
  2488. * queue_depth here also.
  2489. *
  2490. * Returns:
  2491. * 0
  2492. **/
  2493. static int ibmvfc_slave_configure(struct scsi_device *sdev)
  2494. {
  2495. struct Scsi_Host *shost = sdev->host;
  2496. unsigned long flags = 0;
  2497. spin_lock_irqsave(shost->host_lock, flags);
  2498. if (sdev->type == TYPE_DISK)
  2499. sdev->allow_restart = 1;
  2500. if (sdev->tagged_supported) {
  2501. scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
  2502. scsi_activate_tcq(sdev, sdev->queue_depth);
  2503. } else
  2504. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  2505. spin_unlock_irqrestore(shost->host_lock, flags);
  2506. return 0;
  2507. }
  2508. /**
  2509. * ibmvfc_change_queue_depth - Change the device's queue depth
  2510. * @sdev: scsi device struct
  2511. * @qdepth: depth to set
  2512. * @reason: calling context
  2513. *
  2514. * Return value:
  2515. * actual depth set
  2516. **/
  2517. static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth,
  2518. int reason)
  2519. {
  2520. if (reason != SCSI_QDEPTH_DEFAULT)
  2521. return -EOPNOTSUPP;
  2522. if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
  2523. qdepth = IBMVFC_MAX_CMDS_PER_LUN;
  2524. scsi_adjust_queue_depth(sdev, 0, qdepth);
  2525. return sdev->queue_depth;
  2526. }
  2527. /**
  2528. * ibmvfc_change_queue_type - Change the device's queue type
  2529. * @sdev: scsi device struct
  2530. * @tag_type: type of tags to use
  2531. *
  2532. * Return value:
  2533. * actual queue type set
  2534. **/
  2535. static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
  2536. {
  2537. if (sdev->tagged_supported) {
  2538. scsi_set_tag_type(sdev, tag_type);
  2539. if (tag_type)
  2540. scsi_activate_tcq(sdev, sdev->queue_depth);
  2541. else
  2542. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  2543. } else
  2544. tag_type = 0;
  2545. return tag_type;
  2546. }
  2547. static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
  2548. struct device_attribute *attr, char *buf)
  2549. {
  2550. struct Scsi_Host *shost = class_to_shost(dev);
  2551. struct ibmvfc_host *vhost = shost_priv(shost);
  2552. return snprintf(buf, PAGE_SIZE, "%s\n",
  2553. vhost->login_buf->resp.partition_name);
  2554. }
  2555. static ssize_t ibmvfc_show_host_device_name(struct device *dev,
  2556. struct device_attribute *attr, char *buf)
  2557. {
  2558. struct Scsi_Host *shost = class_to_shost(dev);
  2559. struct ibmvfc_host *vhost = shost_priv(shost);
  2560. return snprintf(buf, PAGE_SIZE, "%s\n",
  2561. vhost->login_buf->resp.device_name);
  2562. }
  2563. static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
  2564. struct device_attribute *attr, char *buf)
  2565. {
  2566. struct Scsi_Host *shost = class_to_shost(dev);
  2567. struct ibmvfc_host *vhost = shost_priv(shost);
  2568. return snprintf(buf, PAGE_SIZE, "%s\n",
  2569. vhost->login_buf->resp.port_loc_code);
  2570. }
  2571. static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
  2572. struct device_attribute *attr, char *buf)
  2573. {
  2574. struct Scsi_Host *shost = class_to_shost(dev);
  2575. struct ibmvfc_host *vhost = shost_priv(shost);
  2576. return snprintf(buf, PAGE_SIZE, "%s\n",
  2577. vhost->login_buf->resp.drc_name);
  2578. }
  2579. static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
  2580. struct device_attribute *attr, char *buf)
  2581. {
  2582. struct Scsi_Host *shost = class_to_shost(dev);
  2583. struct ibmvfc_host *vhost = shost_priv(shost);
  2584. return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
  2585. }
  2586. static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
  2587. struct device_attribute *attr, char *buf)
  2588. {
  2589. struct Scsi_Host *shost = class_to_shost(dev);
  2590. struct ibmvfc_host *vhost = shost_priv(shost);
  2591. return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
  2592. }
  2593. /**
  2594. * ibmvfc_show_log_level - Show the adapter's error logging level
  2595. * @dev: class device struct
  2596. * @buf: buffer
  2597. *
  2598. * Return value:
  2599. * number of bytes printed to buffer
  2600. **/
  2601. static ssize_t ibmvfc_show_log_level(struct device *dev,
  2602. struct device_attribute *attr, char *buf)
  2603. {
  2604. struct Scsi_Host *shost = class_to_shost(dev);
  2605. struct ibmvfc_host *vhost = shost_priv(shost);
  2606. unsigned long flags = 0;
  2607. int len;
  2608. spin_lock_irqsave(shost->host_lock, flags);
  2609. len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
  2610. spin_unlock_irqrestore(shost->host_lock, flags);
  2611. return len;
  2612. }
  2613. /**
  2614. * ibmvfc_store_log_level - Change the adapter's error logging level
  2615. * @dev: class device struct
  2616. * @buf: buffer
  2617. *
  2618. * Return value:
  2619. * number of bytes printed to buffer
  2620. **/
  2621. static ssize_t ibmvfc_store_log_level(struct device *dev,
  2622. struct device_attribute *attr,
  2623. const char *buf, size_t count)
  2624. {
  2625. struct Scsi_Host *shost = class_to_shost(dev);
  2626. struct ibmvfc_host *vhost = shost_priv(shost);
  2627. unsigned long flags = 0;
  2628. spin_lock_irqsave(shost->host_lock, flags);
  2629. vhost->log_level = simple_strtoul(buf, NULL, 10);
  2630. spin_unlock_irqrestore(shost->host_lock, flags);
  2631. return strlen(buf);
  2632. }
  2633. static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
  2634. static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
  2635. static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
  2636. static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
  2637. static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
  2638. static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
  2639. static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
  2640. ibmvfc_show_log_level, ibmvfc_store_log_level);
  2641. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  2642. /**
  2643. * ibmvfc_read_trace - Dump the adapter trace
  2644. * @filp: open sysfs file
  2645. * @kobj: kobject struct
  2646. * @bin_attr: bin_attribute struct
  2647. * @buf: buffer
  2648. * @off: offset
  2649. * @count: buffer size
  2650. *
  2651. * Return value:
  2652. * number of bytes printed to buffer
  2653. **/
  2654. static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
  2655. struct bin_attribute *bin_attr,
  2656. char *buf, loff_t off, size_t count)
  2657. {
  2658. struct device *dev = container_of(kobj, struct device, kobj);
  2659. struct Scsi_Host *shost = class_to_shost(dev);
  2660. struct ibmvfc_host *vhost = shost_priv(shost);
  2661. unsigned long flags = 0;
  2662. int size = IBMVFC_TRACE_SIZE;
  2663. char *src = (char *)vhost->trace;
  2664. if (off > size)
  2665. return 0;
  2666. if (off + count > size) {
  2667. size -= off;
  2668. count = size;
  2669. }
  2670. spin_lock_irqsave(shost->host_lock, flags);
  2671. memcpy(buf, &src[off], count);
  2672. spin_unlock_irqrestore(shost->host_lock, flags);
  2673. return count;
  2674. }
  2675. static struct bin_attribute ibmvfc_trace_attr = {
  2676. .attr = {
  2677. .name = "trace",
  2678. .mode = S_IRUGO,
  2679. },
  2680. .size = 0,
  2681. .read = ibmvfc_read_trace,
  2682. };
  2683. #endif
  2684. static struct device_attribute *ibmvfc_attrs[] = {
  2685. &dev_attr_partition_name,
  2686. &dev_attr_device_name,
  2687. &dev_attr_port_loc_code,
  2688. &dev_attr_drc_name,
  2689. &dev_attr_npiv_version,
  2690. &dev_attr_capabilities,
  2691. &dev_attr_log_level,
  2692. NULL
  2693. };
  2694. static struct scsi_host_template driver_template = {
  2695. .module = THIS_MODULE,
  2696. .name = "IBM POWER Virtual FC Adapter",
  2697. .proc_name = IBMVFC_NAME,
  2698. .queuecommand = ibmvfc_queuecommand,
  2699. .eh_abort_handler = ibmvfc_eh_abort_handler,
  2700. .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
  2701. .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
  2702. .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
  2703. .slave_alloc = ibmvfc_slave_alloc,
  2704. .slave_configure = ibmvfc_slave_configure,
  2705. .target_alloc = ibmvfc_target_alloc,
  2706. .scan_finished = ibmvfc_scan_finished,
  2707. .change_queue_depth = ibmvfc_change_queue_depth,
  2708. .change_queue_type = ibmvfc_change_queue_type,
  2709. .cmd_per_lun = 16,
  2710. .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
  2711. .this_id = -1,
  2712. .sg_tablesize = SG_ALL,
  2713. .max_sectors = IBMVFC_MAX_SECTORS,
  2714. .use_clustering = ENABLE_CLUSTERING,
  2715. .shost_attrs = ibmvfc_attrs,
  2716. };
  2717. /**
  2718. * ibmvfc_next_async_crq - Returns the next entry in async queue
  2719. * @vhost: ibmvfc host struct
  2720. *
  2721. * Returns:
  2722. * Pointer to next entry in queue / NULL if empty
  2723. **/
  2724. static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
  2725. {
  2726. struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
  2727. struct ibmvfc_async_crq *crq;
  2728. crq = &async_crq->msgs[async_crq->cur];
  2729. if (crq->valid & 0x80) {
  2730. if (++async_crq->cur == async_crq->size)
  2731. async_crq->cur = 0;
  2732. rmb();
  2733. } else
  2734. crq = NULL;
  2735. return crq;
  2736. }
  2737. /**
  2738. * ibmvfc_next_crq - Returns the next entry in message queue
  2739. * @vhost: ibmvfc host struct
  2740. *
  2741. * Returns:
  2742. * Pointer to next entry in queue / NULL if empty
  2743. **/
  2744. static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
  2745. {
  2746. struct ibmvfc_crq_queue *queue = &vhost->crq;
  2747. struct ibmvfc_crq *crq;
  2748. crq = &queue->msgs[queue->cur];
  2749. if (crq->valid & 0x80) {
  2750. if (++queue->cur == queue->size)
  2751. queue->cur = 0;
  2752. rmb();
  2753. } else
  2754. crq = NULL;
  2755. return crq;
  2756. }
  2757. /**
  2758. * ibmvfc_interrupt - Interrupt handler
  2759. * @irq: number of irq to handle, not used
  2760. * @dev_instance: ibmvfc_host that received interrupt
  2761. *
  2762. * Returns:
  2763. * IRQ_HANDLED
  2764. **/
  2765. static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
  2766. {
  2767. struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
  2768. unsigned long flags;
  2769. spin_lock_irqsave(vhost->host->host_lock, flags);
  2770. vio_disable_interrupts(to_vio_dev(vhost->dev));
  2771. tasklet_schedule(&vhost->tasklet);
  2772. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2773. return IRQ_HANDLED;
  2774. }
  2775. /**
  2776. * ibmvfc_tasklet - Interrupt handler tasklet
  2777. * @data: ibmvfc host struct
  2778. *
  2779. * Returns:
  2780. * Nothing
  2781. **/
  2782. static void ibmvfc_tasklet(void *data)
  2783. {
  2784. struct ibmvfc_host *vhost = data;
  2785. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  2786. struct ibmvfc_crq *crq;
  2787. struct ibmvfc_async_crq *async;
  2788. unsigned long flags;
  2789. int done = 0;
  2790. spin_lock_irqsave(vhost->host->host_lock, flags);
  2791. while (!done) {
  2792. /* Pull all the valid messages off the async CRQ */
  2793. while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  2794. ibmvfc_handle_async(async, vhost);
  2795. async->valid = 0;
  2796. wmb();
  2797. }
  2798. /* Pull all the valid messages off the CRQ */
  2799. while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  2800. ibmvfc_handle_crq(crq, vhost);
  2801. crq->valid = 0;
  2802. wmb();
  2803. }
  2804. vio_enable_interrupts(vdev);
  2805. if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  2806. vio_disable_interrupts(vdev);
  2807. ibmvfc_handle_async(async, vhost);
  2808. async->valid = 0;
  2809. wmb();
  2810. } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  2811. vio_disable_interrupts(vdev);
  2812. ibmvfc_handle_crq(crq, vhost);
  2813. crq->valid = 0;
  2814. wmb();
  2815. } else
  2816. done = 1;
  2817. }
  2818. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2819. }
  2820. /**
  2821. * ibmvfc_init_tgt - Set the next init job step for the target
  2822. * @tgt: ibmvfc target struct
  2823. * @job_step: job step to perform
  2824. *
  2825. **/
  2826. static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
  2827. void (*job_step) (struct ibmvfc_target *))
  2828. {
  2829. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
  2830. tgt->job_step = job_step;
  2831. wake_up(&tgt->vhost->work_wait_q);
  2832. }
  2833. /**
  2834. * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
  2835. * @tgt: ibmvfc target struct
  2836. * @job_step: initialization job step
  2837. *
  2838. * Returns: 1 if step will be retried / 0 if not
  2839. *
  2840. **/
  2841. static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
  2842. void (*job_step) (struct ibmvfc_target *))
  2843. {
  2844. if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
  2845. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2846. wake_up(&tgt->vhost->work_wait_q);
  2847. return 0;
  2848. } else
  2849. ibmvfc_init_tgt(tgt, job_step);
  2850. return 1;
  2851. }
  2852. /* Defined in FC-LS */
  2853. static const struct {
  2854. int code;
  2855. int retry;
  2856. int logged_in;
  2857. } prli_rsp [] = {
  2858. { 0, 1, 0 },
  2859. { 1, 0, 1 },
  2860. { 2, 1, 0 },
  2861. { 3, 1, 0 },
  2862. { 4, 0, 0 },
  2863. { 5, 0, 0 },
  2864. { 6, 0, 1 },
  2865. { 7, 0, 0 },
  2866. { 8, 1, 0 },
  2867. };
  2868. /**
  2869. * ibmvfc_get_prli_rsp - Find PRLI response index
  2870. * @flags: PRLI response flags
  2871. *
  2872. **/
  2873. static int ibmvfc_get_prli_rsp(u16 flags)
  2874. {
  2875. int i;
  2876. int code = (flags & 0x0f00) >> 8;
  2877. for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
  2878. if (prli_rsp[i].code == code)
  2879. return i;
  2880. return 0;
  2881. }
  2882. /**
  2883. * ibmvfc_tgt_prli_done - Completion handler for Process Login
  2884. * @evt: ibmvfc event struct
  2885. *
  2886. **/
  2887. static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
  2888. {
  2889. struct ibmvfc_target *tgt = evt->tgt;
  2890. struct ibmvfc_host *vhost = evt->vhost;
  2891. struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
  2892. struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
  2893. u32 status = rsp->common.status;
  2894. int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
  2895. vhost->discovery_threads--;
  2896. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2897. switch (status) {
  2898. case IBMVFC_MAD_SUCCESS:
  2899. tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
  2900. parms->type, parms->flags, parms->service_parms);
  2901. if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
  2902. index = ibmvfc_get_prli_rsp(parms->flags);
  2903. if (prli_rsp[index].logged_in) {
  2904. if (parms->flags & IBMVFC_PRLI_EST_IMG_PAIR) {
  2905. tgt->need_login = 0;
  2906. tgt->ids.roles = 0;
  2907. if (parms->service_parms & IBMVFC_PRLI_TARGET_FUNC)
  2908. tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
  2909. if (parms->service_parms & IBMVFC_PRLI_INITIATOR_FUNC)
  2910. tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
  2911. tgt->add_rport = 1;
  2912. } else
  2913. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2914. } else if (prli_rsp[index].retry)
  2915. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2916. else
  2917. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2918. } else
  2919. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2920. break;
  2921. case IBMVFC_MAD_DRIVER_FAILED:
  2922. break;
  2923. case IBMVFC_MAD_CRQ_ERROR:
  2924. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2925. break;
  2926. case IBMVFC_MAD_FAILED:
  2927. default:
  2928. if ((rsp->status & IBMVFC_VIOS_FAILURE) && rsp->error == IBMVFC_PLOGI_REQUIRED)
  2929. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  2930. else if (tgt->logo_rcvd)
  2931. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  2932. else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
  2933. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2934. else
  2935. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2936. tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
  2937. ibmvfc_get_cmd_error(rsp->status, rsp->error),
  2938. rsp->status, rsp->error, status);
  2939. break;
  2940. };
  2941. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2942. ibmvfc_free_event(evt);
  2943. wake_up(&vhost->work_wait_q);
  2944. }
  2945. /**
  2946. * ibmvfc_tgt_send_prli - Send a process login
  2947. * @tgt: ibmvfc target struct
  2948. *
  2949. **/
  2950. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
  2951. {
  2952. struct ibmvfc_process_login *prli;
  2953. struct ibmvfc_host *vhost = tgt->vhost;
  2954. struct ibmvfc_event *evt;
  2955. if (vhost->discovery_threads >= disc_threads)
  2956. return;
  2957. kref_get(&tgt->kref);
  2958. evt = ibmvfc_get_event(vhost);
  2959. vhost->discovery_threads++;
  2960. ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
  2961. evt->tgt = tgt;
  2962. prli = &evt->iu.prli;
  2963. memset(prli, 0, sizeof(*prli));
  2964. prli->common.version = 1;
  2965. prli->common.opcode = IBMVFC_PROCESS_LOGIN;
  2966. prli->common.length = sizeof(*prli);
  2967. prli->scsi_id = tgt->scsi_id;
  2968. prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
  2969. prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
  2970. prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
  2971. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  2972. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  2973. vhost->discovery_threads--;
  2974. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2975. kref_put(&tgt->kref, ibmvfc_release_tgt);
  2976. } else
  2977. tgt_dbg(tgt, "Sent process login\n");
  2978. }
  2979. /**
  2980. * ibmvfc_tgt_plogi_done - Completion handler for Port Login
  2981. * @evt: ibmvfc event struct
  2982. *
  2983. **/
  2984. static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
  2985. {
  2986. struct ibmvfc_target *tgt = evt->tgt;
  2987. struct ibmvfc_host *vhost = evt->vhost;
  2988. struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
  2989. u32 status = rsp->common.status;
  2990. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  2991. vhost->discovery_threads--;
  2992. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2993. switch (status) {
  2994. case IBMVFC_MAD_SUCCESS:
  2995. tgt_dbg(tgt, "Port Login succeeded\n");
  2996. if (tgt->ids.port_name &&
  2997. tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
  2998. vhost->reinit = 1;
  2999. tgt_dbg(tgt, "Port re-init required\n");
  3000. break;
  3001. }
  3002. tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
  3003. tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
  3004. tgt->ids.port_id = tgt->scsi_id;
  3005. memcpy(&tgt->service_parms, &rsp->service_parms,
  3006. sizeof(tgt->service_parms));
  3007. memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
  3008. sizeof(tgt->service_parms_change));
  3009. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
  3010. break;
  3011. case IBMVFC_MAD_DRIVER_FAILED:
  3012. break;
  3013. case IBMVFC_MAD_CRQ_ERROR:
  3014. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3015. break;
  3016. case IBMVFC_MAD_FAILED:
  3017. default:
  3018. if (ibmvfc_retry_cmd(rsp->status, rsp->error))
  3019. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3020. else
  3021. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3022. tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3023. ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
  3024. ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
  3025. ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
  3026. break;
  3027. };
  3028. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3029. ibmvfc_free_event(evt);
  3030. wake_up(&vhost->work_wait_q);
  3031. }
  3032. /**
  3033. * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
  3034. * @tgt: ibmvfc target struct
  3035. *
  3036. **/
  3037. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
  3038. {
  3039. struct ibmvfc_port_login *plogi;
  3040. struct ibmvfc_host *vhost = tgt->vhost;
  3041. struct ibmvfc_event *evt;
  3042. if (vhost->discovery_threads >= disc_threads)
  3043. return;
  3044. kref_get(&tgt->kref);
  3045. tgt->logo_rcvd = 0;
  3046. evt = ibmvfc_get_event(vhost);
  3047. vhost->discovery_threads++;
  3048. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3049. ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
  3050. evt->tgt = tgt;
  3051. plogi = &evt->iu.plogi;
  3052. memset(plogi, 0, sizeof(*plogi));
  3053. plogi->common.version = 1;
  3054. plogi->common.opcode = IBMVFC_PORT_LOGIN;
  3055. plogi->common.length = sizeof(*plogi);
  3056. plogi->scsi_id = tgt->scsi_id;
  3057. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3058. vhost->discovery_threads--;
  3059. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3060. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3061. } else
  3062. tgt_dbg(tgt, "Sent port login\n");
  3063. }
  3064. /**
  3065. * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
  3066. * @evt: ibmvfc event struct
  3067. *
  3068. **/
  3069. static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
  3070. {
  3071. struct ibmvfc_target *tgt = evt->tgt;
  3072. struct ibmvfc_host *vhost = evt->vhost;
  3073. struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
  3074. u32 status = rsp->common.status;
  3075. vhost->discovery_threads--;
  3076. ibmvfc_free_event(evt);
  3077. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3078. switch (status) {
  3079. case IBMVFC_MAD_SUCCESS:
  3080. tgt_dbg(tgt, "Implicit Logout succeeded\n");
  3081. break;
  3082. case IBMVFC_MAD_DRIVER_FAILED:
  3083. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3084. wake_up(&vhost->work_wait_q);
  3085. return;
  3086. case IBMVFC_MAD_FAILED:
  3087. default:
  3088. tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
  3089. break;
  3090. };
  3091. if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
  3092. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
  3093. else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
  3094. tgt->scsi_id != tgt->new_scsi_id)
  3095. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3096. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3097. wake_up(&vhost->work_wait_q);
  3098. }
  3099. /**
  3100. * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
  3101. * @tgt: ibmvfc target struct
  3102. *
  3103. **/
  3104. static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
  3105. {
  3106. struct ibmvfc_implicit_logout *mad;
  3107. struct ibmvfc_host *vhost = tgt->vhost;
  3108. struct ibmvfc_event *evt;
  3109. if (vhost->discovery_threads >= disc_threads)
  3110. return;
  3111. kref_get(&tgt->kref);
  3112. evt = ibmvfc_get_event(vhost);
  3113. vhost->discovery_threads++;
  3114. ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
  3115. evt->tgt = tgt;
  3116. mad = &evt->iu.implicit_logout;
  3117. memset(mad, 0, sizeof(*mad));
  3118. mad->common.version = 1;
  3119. mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
  3120. mad->common.length = sizeof(*mad);
  3121. mad->old_scsi_id = tgt->scsi_id;
  3122. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3123. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3124. vhost->discovery_threads--;
  3125. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3126. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3127. } else
  3128. tgt_dbg(tgt, "Sent Implicit Logout\n");
  3129. }
  3130. /**
  3131. * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
  3132. * @mad: ibmvfc passthru mad struct
  3133. * @tgt: ibmvfc target struct
  3134. *
  3135. * Returns:
  3136. * 1 if PLOGI needed / 0 if PLOGI not needed
  3137. **/
  3138. static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
  3139. struct ibmvfc_target *tgt)
  3140. {
  3141. if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
  3142. sizeof(tgt->ids.port_name)))
  3143. return 1;
  3144. if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
  3145. sizeof(tgt->ids.node_name)))
  3146. return 1;
  3147. if (mad->fc_iu.response[6] != tgt->scsi_id)
  3148. return 1;
  3149. return 0;
  3150. }
  3151. /**
  3152. * ibmvfc_tgt_adisc_done - Completion handler for ADISC
  3153. * @evt: ibmvfc event struct
  3154. *
  3155. **/
  3156. static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
  3157. {
  3158. struct ibmvfc_target *tgt = evt->tgt;
  3159. struct ibmvfc_host *vhost = evt->vhost;
  3160. struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
  3161. u32 status = mad->common.status;
  3162. u8 fc_reason, fc_explain;
  3163. vhost->discovery_threads--;
  3164. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3165. del_timer(&tgt->timer);
  3166. switch (status) {
  3167. case IBMVFC_MAD_SUCCESS:
  3168. tgt_dbg(tgt, "ADISC succeeded\n");
  3169. if (ibmvfc_adisc_needs_plogi(mad, tgt))
  3170. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3171. break;
  3172. case IBMVFC_MAD_DRIVER_FAILED:
  3173. break;
  3174. case IBMVFC_MAD_FAILED:
  3175. default:
  3176. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3177. fc_reason = (mad->fc_iu.response[1] & 0x00ff0000) >> 16;
  3178. fc_explain = (mad->fc_iu.response[1] & 0x0000ff00) >> 8;
  3179. tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3180. ibmvfc_get_cmd_error(mad->iu.status, mad->iu.error),
  3181. mad->iu.status, mad->iu.error,
  3182. ibmvfc_get_fc_type(fc_reason), fc_reason,
  3183. ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
  3184. break;
  3185. };
  3186. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3187. ibmvfc_free_event(evt);
  3188. wake_up(&vhost->work_wait_q);
  3189. }
  3190. /**
  3191. * ibmvfc_init_passthru - Initialize an event struct for FC passthru
  3192. * @evt: ibmvfc event struct
  3193. *
  3194. **/
  3195. static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
  3196. {
  3197. struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
  3198. memset(mad, 0, sizeof(*mad));
  3199. mad->common.version = 1;
  3200. mad->common.opcode = IBMVFC_PASSTHRU;
  3201. mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
  3202. mad->cmd_ioba.va = (u64)evt->crq.ioba +
  3203. offsetof(struct ibmvfc_passthru_mad, iu);
  3204. mad->cmd_ioba.len = sizeof(mad->iu);
  3205. mad->iu.cmd_len = sizeof(mad->fc_iu.payload);
  3206. mad->iu.rsp_len = sizeof(mad->fc_iu.response);
  3207. mad->iu.cmd.va = (u64)evt->crq.ioba +
  3208. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  3209. offsetof(struct ibmvfc_passthru_fc_iu, payload);
  3210. mad->iu.cmd.len = sizeof(mad->fc_iu.payload);
  3211. mad->iu.rsp.va = (u64)evt->crq.ioba +
  3212. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  3213. offsetof(struct ibmvfc_passthru_fc_iu, response);
  3214. mad->iu.rsp.len = sizeof(mad->fc_iu.response);
  3215. }
  3216. /**
  3217. * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
  3218. * @evt: ibmvfc event struct
  3219. *
  3220. * Just cleanup this event struct. Everything else is handled by
  3221. * the ADISC completion handler. If the ADISC never actually comes
  3222. * back, we still have the timer running on the ADISC event struct
  3223. * which will fire and cause the CRQ to get reset.
  3224. *
  3225. **/
  3226. static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
  3227. {
  3228. struct ibmvfc_host *vhost = evt->vhost;
  3229. struct ibmvfc_target *tgt = evt->tgt;
  3230. tgt_dbg(tgt, "ADISC cancel complete\n");
  3231. vhost->abort_threads--;
  3232. ibmvfc_free_event(evt);
  3233. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3234. wake_up(&vhost->work_wait_q);
  3235. }
  3236. /**
  3237. * ibmvfc_adisc_timeout - Handle an ADISC timeout
  3238. * @tgt: ibmvfc target struct
  3239. *
  3240. * If an ADISC times out, send a cancel. If the cancel times
  3241. * out, reset the CRQ. When the ADISC comes back as cancelled,
  3242. * log back into the target.
  3243. **/
  3244. static void ibmvfc_adisc_timeout(struct ibmvfc_target *tgt)
  3245. {
  3246. struct ibmvfc_host *vhost = tgt->vhost;
  3247. struct ibmvfc_event *evt;
  3248. struct ibmvfc_tmf *tmf;
  3249. unsigned long flags;
  3250. int rc;
  3251. tgt_dbg(tgt, "ADISC timeout\n");
  3252. spin_lock_irqsave(vhost->host->host_lock, flags);
  3253. if (vhost->abort_threads >= disc_threads ||
  3254. tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
  3255. vhost->state != IBMVFC_INITIALIZING ||
  3256. vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
  3257. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3258. return;
  3259. }
  3260. vhost->abort_threads++;
  3261. kref_get(&tgt->kref);
  3262. evt = ibmvfc_get_event(vhost);
  3263. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
  3264. evt->tgt = tgt;
  3265. tmf = &evt->iu.tmf;
  3266. memset(tmf, 0, sizeof(*tmf));
  3267. tmf->common.version = 1;
  3268. tmf->common.opcode = IBMVFC_TMF_MAD;
  3269. tmf->common.length = sizeof(*tmf);
  3270. tmf->scsi_id = tgt->scsi_id;
  3271. tmf->cancel_key = tgt->cancel_key;
  3272. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  3273. if (rc) {
  3274. tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
  3275. vhost->abort_threads--;
  3276. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3277. __ibmvfc_reset_host(vhost);
  3278. } else
  3279. tgt_dbg(tgt, "Attempting to cancel ADISC\n");
  3280. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3281. }
  3282. /**
  3283. * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
  3284. * @tgt: ibmvfc target struct
  3285. *
  3286. * When sending an ADISC we end up with two timers running. The
  3287. * first timer is the timer in the ibmvfc target struct. If this
  3288. * fires, we send a cancel to the target. The second timer is the
  3289. * timer on the ibmvfc event for the ADISC, which is longer. If that
  3290. * fires, it means the ADISC timed out and our attempt to cancel it
  3291. * also failed, so we need to reset the CRQ.
  3292. **/
  3293. static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
  3294. {
  3295. struct ibmvfc_passthru_mad *mad;
  3296. struct ibmvfc_host *vhost = tgt->vhost;
  3297. struct ibmvfc_event *evt;
  3298. if (vhost->discovery_threads >= disc_threads)
  3299. return;
  3300. kref_get(&tgt->kref);
  3301. evt = ibmvfc_get_event(vhost);
  3302. vhost->discovery_threads++;
  3303. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
  3304. evt->tgt = tgt;
  3305. ibmvfc_init_passthru(evt);
  3306. mad = &evt->iu.passthru;
  3307. mad->iu.flags = IBMVFC_FC_ELS;
  3308. mad->iu.scsi_id = tgt->scsi_id;
  3309. mad->iu.cancel_key = tgt->cancel_key;
  3310. mad->fc_iu.payload[0] = IBMVFC_ADISC;
  3311. memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
  3312. sizeof(vhost->login_buf->resp.port_name));
  3313. memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
  3314. sizeof(vhost->login_buf->resp.node_name));
  3315. mad->fc_iu.payload[6] = vhost->login_buf->resp.scsi_id & 0x00ffffff;
  3316. if (timer_pending(&tgt->timer))
  3317. mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
  3318. else {
  3319. tgt->timer.data = (unsigned long) tgt;
  3320. tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
  3321. tgt->timer.function = (void (*)(unsigned long))ibmvfc_adisc_timeout;
  3322. add_timer(&tgt->timer);
  3323. }
  3324. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3325. if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
  3326. vhost->discovery_threads--;
  3327. del_timer(&tgt->timer);
  3328. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3329. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3330. } else
  3331. tgt_dbg(tgt, "Sent ADISC\n");
  3332. }
  3333. /**
  3334. * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
  3335. * @evt: ibmvfc event struct
  3336. *
  3337. **/
  3338. static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
  3339. {
  3340. struct ibmvfc_target *tgt = evt->tgt;
  3341. struct ibmvfc_host *vhost = evt->vhost;
  3342. struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
  3343. u32 status = rsp->common.status;
  3344. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3345. vhost->discovery_threads--;
  3346. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3347. switch (status) {
  3348. case IBMVFC_MAD_SUCCESS:
  3349. tgt_dbg(tgt, "Query Target succeeded\n");
  3350. tgt->new_scsi_id = rsp->scsi_id;
  3351. if (rsp->scsi_id != tgt->scsi_id)
  3352. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  3353. else
  3354. ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
  3355. break;
  3356. case IBMVFC_MAD_DRIVER_FAILED:
  3357. break;
  3358. case IBMVFC_MAD_CRQ_ERROR:
  3359. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  3360. break;
  3361. case IBMVFC_MAD_FAILED:
  3362. default:
  3363. if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
  3364. rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
  3365. rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
  3366. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3367. else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
  3368. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  3369. else
  3370. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3371. tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3372. ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
  3373. ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
  3374. ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
  3375. break;
  3376. };
  3377. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3378. ibmvfc_free_event(evt);
  3379. wake_up(&vhost->work_wait_q);
  3380. }
  3381. /**
  3382. * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
  3383. * @tgt: ibmvfc target struct
  3384. *
  3385. **/
  3386. static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
  3387. {
  3388. struct ibmvfc_query_tgt *query_tgt;
  3389. struct ibmvfc_host *vhost = tgt->vhost;
  3390. struct ibmvfc_event *evt;
  3391. if (vhost->discovery_threads >= disc_threads)
  3392. return;
  3393. kref_get(&tgt->kref);
  3394. evt = ibmvfc_get_event(vhost);
  3395. vhost->discovery_threads++;
  3396. evt->tgt = tgt;
  3397. ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
  3398. query_tgt = &evt->iu.query_tgt;
  3399. memset(query_tgt, 0, sizeof(*query_tgt));
  3400. query_tgt->common.version = 1;
  3401. query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
  3402. query_tgt->common.length = sizeof(*query_tgt);
  3403. query_tgt->wwpn = tgt->ids.port_name;
  3404. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3405. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3406. vhost->discovery_threads--;
  3407. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3408. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3409. } else
  3410. tgt_dbg(tgt, "Sent Query Target\n");
  3411. }
  3412. /**
  3413. * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
  3414. * @vhost: ibmvfc host struct
  3415. * @scsi_id: SCSI ID to allocate target for
  3416. *
  3417. * Returns:
  3418. * 0 on success / other on failure
  3419. **/
  3420. static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
  3421. {
  3422. struct ibmvfc_target *tgt;
  3423. unsigned long flags;
  3424. spin_lock_irqsave(vhost->host->host_lock, flags);
  3425. list_for_each_entry(tgt, &vhost->targets, queue) {
  3426. if (tgt->scsi_id == scsi_id) {
  3427. if (tgt->need_login)
  3428. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  3429. goto unlock_out;
  3430. }
  3431. }
  3432. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3433. tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
  3434. if (!tgt) {
  3435. dev_err(vhost->dev, "Target allocation failure for scsi id %08llx\n",
  3436. scsi_id);
  3437. return -ENOMEM;
  3438. }
  3439. memset(tgt, 0, sizeof(*tgt));
  3440. tgt->scsi_id = scsi_id;
  3441. tgt->new_scsi_id = scsi_id;
  3442. tgt->vhost = vhost;
  3443. tgt->need_login = 1;
  3444. tgt->cancel_key = vhost->task_set++;
  3445. init_timer(&tgt->timer);
  3446. kref_init(&tgt->kref);
  3447. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  3448. spin_lock_irqsave(vhost->host->host_lock, flags);
  3449. list_add_tail(&tgt->queue, &vhost->targets);
  3450. unlock_out:
  3451. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3452. return 0;
  3453. }
  3454. /**
  3455. * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
  3456. * @vhost: ibmvfc host struct
  3457. *
  3458. * Returns:
  3459. * 0 on success / other on failure
  3460. **/
  3461. static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
  3462. {
  3463. int i, rc;
  3464. for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
  3465. rc = ibmvfc_alloc_target(vhost,
  3466. vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
  3467. return rc;
  3468. }
  3469. /**
  3470. * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
  3471. * @evt: ibmvfc event struct
  3472. *
  3473. **/
  3474. static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
  3475. {
  3476. struct ibmvfc_host *vhost = evt->vhost;
  3477. struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
  3478. u32 mad_status = rsp->common.status;
  3479. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3480. switch (mad_status) {
  3481. case IBMVFC_MAD_SUCCESS:
  3482. ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
  3483. vhost->num_targets = rsp->num_written;
  3484. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
  3485. break;
  3486. case IBMVFC_MAD_FAILED:
  3487. level += ibmvfc_retry_host_init(vhost);
  3488. ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
  3489. ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
  3490. break;
  3491. case IBMVFC_MAD_DRIVER_FAILED:
  3492. break;
  3493. default:
  3494. dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
  3495. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3496. break;
  3497. }
  3498. ibmvfc_free_event(evt);
  3499. wake_up(&vhost->work_wait_q);
  3500. }
  3501. /**
  3502. * ibmvfc_discover_targets - Send Discover Targets MAD
  3503. * @vhost: ibmvfc host struct
  3504. *
  3505. **/
  3506. static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
  3507. {
  3508. struct ibmvfc_discover_targets *mad;
  3509. struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
  3510. ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
  3511. mad = &evt->iu.discover_targets;
  3512. memset(mad, 0, sizeof(*mad));
  3513. mad->common.version = 1;
  3514. mad->common.opcode = IBMVFC_DISC_TARGETS;
  3515. mad->common.length = sizeof(*mad);
  3516. mad->bufflen = vhost->disc_buf_sz;
  3517. mad->buffer.va = vhost->disc_buf_dma;
  3518. mad->buffer.len = vhost->disc_buf_sz;
  3519. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  3520. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3521. ibmvfc_dbg(vhost, "Sent discover targets\n");
  3522. else
  3523. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3524. }
  3525. /**
  3526. * ibmvfc_npiv_login_done - Completion handler for NPIV Login
  3527. * @evt: ibmvfc event struct
  3528. *
  3529. **/
  3530. static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
  3531. {
  3532. struct ibmvfc_host *vhost = evt->vhost;
  3533. u32 mad_status = evt->xfer_iu->npiv_login.common.status;
  3534. struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
  3535. unsigned int npiv_max_sectors;
  3536. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3537. switch (mad_status) {
  3538. case IBMVFC_MAD_SUCCESS:
  3539. ibmvfc_free_event(evt);
  3540. break;
  3541. case IBMVFC_MAD_FAILED:
  3542. if (ibmvfc_retry_cmd(rsp->status, rsp->error))
  3543. level += ibmvfc_retry_host_init(vhost);
  3544. else
  3545. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3546. ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
  3547. ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
  3548. ibmvfc_free_event(evt);
  3549. return;
  3550. case IBMVFC_MAD_CRQ_ERROR:
  3551. ibmvfc_retry_host_init(vhost);
  3552. case IBMVFC_MAD_DRIVER_FAILED:
  3553. ibmvfc_free_event(evt);
  3554. return;
  3555. default:
  3556. dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
  3557. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3558. ibmvfc_free_event(evt);
  3559. return;
  3560. }
  3561. vhost->client_migrated = 0;
  3562. if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
  3563. dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
  3564. rsp->flags);
  3565. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3566. wake_up(&vhost->work_wait_q);
  3567. return;
  3568. }
  3569. if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
  3570. dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
  3571. rsp->max_cmds);
  3572. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3573. wake_up(&vhost->work_wait_q);
  3574. return;
  3575. }
  3576. vhost->logged_in = 1;
  3577. npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
  3578. dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
  3579. rsp->partition_name, rsp->device_name, rsp->port_loc_code,
  3580. rsp->drc_name, npiv_max_sectors);
  3581. fc_host_fabric_name(vhost->host) = rsp->node_name;
  3582. fc_host_node_name(vhost->host) = rsp->node_name;
  3583. fc_host_port_name(vhost->host) = rsp->port_name;
  3584. fc_host_port_id(vhost->host) = rsp->scsi_id;
  3585. fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
  3586. fc_host_supported_classes(vhost->host) = 0;
  3587. if (rsp->service_parms.class1_parms[0] & 0x80000000)
  3588. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
  3589. if (rsp->service_parms.class2_parms[0] & 0x80000000)
  3590. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
  3591. if (rsp->service_parms.class3_parms[0] & 0x80000000)
  3592. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
  3593. fc_host_maxframe_size(vhost->host) =
  3594. rsp->service_parms.common.bb_rcv_sz & 0x0fff;
  3595. vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
  3596. vhost->host->max_sectors = npiv_max_sectors;
  3597. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  3598. wake_up(&vhost->work_wait_q);
  3599. }
  3600. /**
  3601. * ibmvfc_npiv_login - Sends NPIV login
  3602. * @vhost: ibmvfc host struct
  3603. *
  3604. **/
  3605. static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
  3606. {
  3607. struct ibmvfc_npiv_login_mad *mad;
  3608. struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
  3609. ibmvfc_gather_partition_info(vhost);
  3610. ibmvfc_set_login_info(vhost);
  3611. ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
  3612. memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
  3613. mad = &evt->iu.npiv_login;
  3614. memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
  3615. mad->common.version = 1;
  3616. mad->common.opcode = IBMVFC_NPIV_LOGIN;
  3617. mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
  3618. mad->buffer.va = vhost->login_buf_dma;
  3619. mad->buffer.len = sizeof(*vhost->login_buf);
  3620. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  3621. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3622. ibmvfc_dbg(vhost, "Sent NPIV login\n");
  3623. else
  3624. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3625. };
  3626. /**
  3627. * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
  3628. * @vhost: ibmvfc host struct
  3629. *
  3630. **/
  3631. static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
  3632. {
  3633. struct ibmvfc_host *vhost = evt->vhost;
  3634. u32 mad_status = evt->xfer_iu->npiv_logout.common.status;
  3635. ibmvfc_free_event(evt);
  3636. switch (mad_status) {
  3637. case IBMVFC_MAD_SUCCESS:
  3638. if (list_empty(&vhost->sent) &&
  3639. vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
  3640. ibmvfc_init_host(vhost);
  3641. return;
  3642. }
  3643. break;
  3644. case IBMVFC_MAD_FAILED:
  3645. case IBMVFC_MAD_NOT_SUPPORTED:
  3646. case IBMVFC_MAD_CRQ_ERROR:
  3647. case IBMVFC_MAD_DRIVER_FAILED:
  3648. default:
  3649. ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
  3650. break;
  3651. }
  3652. ibmvfc_hard_reset_host(vhost);
  3653. }
  3654. /**
  3655. * ibmvfc_npiv_logout - Issue an NPIV Logout
  3656. * @vhost: ibmvfc host struct
  3657. *
  3658. **/
  3659. static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
  3660. {
  3661. struct ibmvfc_npiv_logout_mad *mad;
  3662. struct ibmvfc_event *evt;
  3663. evt = ibmvfc_get_event(vhost);
  3664. ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
  3665. mad = &evt->iu.npiv_logout;
  3666. memset(mad, 0, sizeof(*mad));
  3667. mad->common.version = 1;
  3668. mad->common.opcode = IBMVFC_NPIV_LOGOUT;
  3669. mad->common.length = sizeof(struct ibmvfc_npiv_logout_mad);
  3670. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
  3671. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3672. ibmvfc_dbg(vhost, "Sent NPIV logout\n");
  3673. else
  3674. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3675. }
  3676. /**
  3677. * ibmvfc_dev_init_to_do - Is there target initialization work to do?
  3678. * @vhost: ibmvfc host struct
  3679. *
  3680. * Returns:
  3681. * 1 if work to do / 0 if not
  3682. **/
  3683. static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
  3684. {
  3685. struct ibmvfc_target *tgt;
  3686. list_for_each_entry(tgt, &vhost->targets, queue) {
  3687. if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
  3688. tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  3689. return 1;
  3690. }
  3691. return 0;
  3692. }
  3693. /**
  3694. * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
  3695. * @vhost: ibmvfc host struct
  3696. *
  3697. * Returns:
  3698. * 1 if work to do / 0 if not
  3699. **/
  3700. static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  3701. {
  3702. struct ibmvfc_target *tgt;
  3703. if (kthread_should_stop())
  3704. return 1;
  3705. switch (vhost->action) {
  3706. case IBMVFC_HOST_ACTION_NONE:
  3707. case IBMVFC_HOST_ACTION_INIT_WAIT:
  3708. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  3709. return 0;
  3710. case IBMVFC_HOST_ACTION_TGT_INIT:
  3711. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  3712. if (vhost->discovery_threads == disc_threads)
  3713. return 0;
  3714. list_for_each_entry(tgt, &vhost->targets, queue)
  3715. if (tgt->action == IBMVFC_TGT_ACTION_INIT)
  3716. return 1;
  3717. list_for_each_entry(tgt, &vhost->targets, queue)
  3718. if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  3719. return 0;
  3720. return 1;
  3721. case IBMVFC_HOST_ACTION_LOGO:
  3722. case IBMVFC_HOST_ACTION_INIT:
  3723. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  3724. case IBMVFC_HOST_ACTION_TGT_DEL:
  3725. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  3726. case IBMVFC_HOST_ACTION_QUERY:
  3727. case IBMVFC_HOST_ACTION_RESET:
  3728. case IBMVFC_HOST_ACTION_REENABLE:
  3729. default:
  3730. break;
  3731. };
  3732. return 1;
  3733. }
  3734. /**
  3735. * ibmvfc_work_to_do - Is there task level work to do?
  3736. * @vhost: ibmvfc host struct
  3737. *
  3738. * Returns:
  3739. * 1 if work to do / 0 if not
  3740. **/
  3741. static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  3742. {
  3743. unsigned long flags;
  3744. int rc;
  3745. spin_lock_irqsave(vhost->host->host_lock, flags);
  3746. rc = __ibmvfc_work_to_do(vhost);
  3747. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3748. return rc;
  3749. }
  3750. /**
  3751. * ibmvfc_log_ae - Log async events if necessary
  3752. * @vhost: ibmvfc host struct
  3753. * @events: events to log
  3754. *
  3755. **/
  3756. static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
  3757. {
  3758. if (events & IBMVFC_AE_RSCN)
  3759. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
  3760. if ((events & IBMVFC_AE_LINKDOWN) &&
  3761. vhost->state >= IBMVFC_HALTED)
  3762. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
  3763. if ((events & IBMVFC_AE_LINKUP) &&
  3764. vhost->state == IBMVFC_INITIALIZING)
  3765. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
  3766. }
  3767. /**
  3768. * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
  3769. * @tgt: ibmvfc target struct
  3770. *
  3771. **/
  3772. static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
  3773. {
  3774. struct ibmvfc_host *vhost = tgt->vhost;
  3775. struct fc_rport *rport;
  3776. unsigned long flags;
  3777. tgt_dbg(tgt, "Adding rport\n");
  3778. rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
  3779. spin_lock_irqsave(vhost->host->host_lock, flags);
  3780. if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  3781. tgt_dbg(tgt, "Deleting rport\n");
  3782. list_del(&tgt->queue);
  3783. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  3784. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3785. fc_remote_port_delete(rport);
  3786. del_timer_sync(&tgt->timer);
  3787. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3788. return;
  3789. } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
  3790. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3791. return;
  3792. }
  3793. if (rport) {
  3794. tgt_dbg(tgt, "rport add succeeded\n");
  3795. tgt->rport = rport;
  3796. rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
  3797. rport->supported_classes = 0;
  3798. tgt->target_id = rport->scsi_target_id;
  3799. if (tgt->service_parms.class1_parms[0] & 0x80000000)
  3800. rport->supported_classes |= FC_COS_CLASS1;
  3801. if (tgt->service_parms.class2_parms[0] & 0x80000000)
  3802. rport->supported_classes |= FC_COS_CLASS2;
  3803. if (tgt->service_parms.class3_parms[0] & 0x80000000)
  3804. rport->supported_classes |= FC_COS_CLASS3;
  3805. if (rport->rqst_q)
  3806. blk_queue_max_segments(rport->rqst_q, 1);
  3807. } else
  3808. tgt_dbg(tgt, "rport add failed\n");
  3809. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3810. }
  3811. /**
  3812. * ibmvfc_do_work - Do task level work
  3813. * @vhost: ibmvfc host struct
  3814. *
  3815. **/
  3816. static void ibmvfc_do_work(struct ibmvfc_host *vhost)
  3817. {
  3818. struct ibmvfc_target *tgt;
  3819. unsigned long flags;
  3820. struct fc_rport *rport;
  3821. int rc;
  3822. ibmvfc_log_ae(vhost, vhost->events_to_log);
  3823. spin_lock_irqsave(vhost->host->host_lock, flags);
  3824. vhost->events_to_log = 0;
  3825. switch (vhost->action) {
  3826. case IBMVFC_HOST_ACTION_NONE:
  3827. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  3828. case IBMVFC_HOST_ACTION_INIT_WAIT:
  3829. break;
  3830. case IBMVFC_HOST_ACTION_RESET:
  3831. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  3832. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3833. rc = ibmvfc_reset_crq(vhost);
  3834. spin_lock_irqsave(vhost->host->host_lock, flags);
  3835. if (rc == H_CLOSED)
  3836. vio_enable_interrupts(to_vio_dev(vhost->dev));
  3837. else if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
  3838. (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
  3839. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3840. dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
  3841. }
  3842. break;
  3843. case IBMVFC_HOST_ACTION_REENABLE:
  3844. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  3845. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3846. rc = ibmvfc_reenable_crq_queue(vhost);
  3847. spin_lock_irqsave(vhost->host->host_lock, flags);
  3848. if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
  3849. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3850. dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
  3851. }
  3852. break;
  3853. case IBMVFC_HOST_ACTION_LOGO:
  3854. vhost->job_step(vhost);
  3855. break;
  3856. case IBMVFC_HOST_ACTION_INIT:
  3857. BUG_ON(vhost->state != IBMVFC_INITIALIZING);
  3858. if (vhost->delay_init) {
  3859. vhost->delay_init = 0;
  3860. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3861. ssleep(15);
  3862. return;
  3863. } else
  3864. vhost->job_step(vhost);
  3865. break;
  3866. case IBMVFC_HOST_ACTION_QUERY:
  3867. list_for_each_entry(tgt, &vhost->targets, queue)
  3868. ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
  3869. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
  3870. break;
  3871. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  3872. list_for_each_entry(tgt, &vhost->targets, queue) {
  3873. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  3874. tgt->job_step(tgt);
  3875. break;
  3876. }
  3877. }
  3878. if (!ibmvfc_dev_init_to_do(vhost))
  3879. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  3880. break;
  3881. case IBMVFC_HOST_ACTION_TGT_DEL:
  3882. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  3883. list_for_each_entry(tgt, &vhost->targets, queue) {
  3884. if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  3885. tgt_dbg(tgt, "Deleting rport\n");
  3886. rport = tgt->rport;
  3887. tgt->rport = NULL;
  3888. list_del(&tgt->queue);
  3889. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  3890. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3891. if (rport)
  3892. fc_remote_port_delete(rport);
  3893. del_timer_sync(&tgt->timer);
  3894. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3895. return;
  3896. }
  3897. }
  3898. if (vhost->state == IBMVFC_INITIALIZING) {
  3899. if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
  3900. if (vhost->reinit) {
  3901. vhost->reinit = 0;
  3902. scsi_block_requests(vhost->host);
  3903. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  3904. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3905. } else {
  3906. ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
  3907. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  3908. wake_up(&vhost->init_wait_q);
  3909. schedule_work(&vhost->rport_add_work_q);
  3910. vhost->init_retries = 0;
  3911. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3912. scsi_unblock_requests(vhost->host);
  3913. }
  3914. return;
  3915. } else {
  3916. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  3917. vhost->job_step = ibmvfc_discover_targets;
  3918. }
  3919. } else {
  3920. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  3921. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3922. scsi_unblock_requests(vhost->host);
  3923. wake_up(&vhost->init_wait_q);
  3924. return;
  3925. }
  3926. break;
  3927. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  3928. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
  3929. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3930. ibmvfc_alloc_targets(vhost);
  3931. spin_lock_irqsave(vhost->host->host_lock, flags);
  3932. break;
  3933. case IBMVFC_HOST_ACTION_TGT_INIT:
  3934. list_for_each_entry(tgt, &vhost->targets, queue) {
  3935. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  3936. tgt->job_step(tgt);
  3937. break;
  3938. }
  3939. }
  3940. if (!ibmvfc_dev_init_to_do(vhost))
  3941. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
  3942. break;
  3943. default:
  3944. break;
  3945. };
  3946. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3947. }
  3948. /**
  3949. * ibmvfc_work - Do task level work
  3950. * @data: ibmvfc host struct
  3951. *
  3952. * Returns:
  3953. * zero
  3954. **/
  3955. static int ibmvfc_work(void *data)
  3956. {
  3957. struct ibmvfc_host *vhost = data;
  3958. int rc;
  3959. set_user_nice(current, -20);
  3960. while (1) {
  3961. rc = wait_event_interruptible(vhost->work_wait_q,
  3962. ibmvfc_work_to_do(vhost));
  3963. BUG_ON(rc);
  3964. if (kthread_should_stop())
  3965. break;
  3966. ibmvfc_do_work(vhost);
  3967. }
  3968. ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
  3969. return 0;
  3970. }
  3971. /**
  3972. * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
  3973. * @vhost: ibmvfc host struct
  3974. *
  3975. * Allocates a page for messages, maps it for dma, and registers
  3976. * the crq with the hypervisor.
  3977. *
  3978. * Return value:
  3979. * zero on success / other on failure
  3980. **/
  3981. static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
  3982. {
  3983. int rc, retrc = -ENOMEM;
  3984. struct device *dev = vhost->dev;
  3985. struct vio_dev *vdev = to_vio_dev(dev);
  3986. struct ibmvfc_crq_queue *crq = &vhost->crq;
  3987. ENTER;
  3988. crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
  3989. if (!crq->msgs)
  3990. return -ENOMEM;
  3991. crq->size = PAGE_SIZE / sizeof(*crq->msgs);
  3992. crq->msg_token = dma_map_single(dev, crq->msgs,
  3993. PAGE_SIZE, DMA_BIDIRECTIONAL);
  3994. if (dma_mapping_error(dev, crq->msg_token))
  3995. goto map_failed;
  3996. retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  3997. crq->msg_token, PAGE_SIZE);
  3998. if (rc == H_RESOURCE)
  3999. /* maybe kexecing and resource is busy. try a reset */
  4000. retrc = rc = ibmvfc_reset_crq(vhost);
  4001. if (rc == H_CLOSED)
  4002. dev_warn(dev, "Partner adapter not ready\n");
  4003. else if (rc) {
  4004. dev_warn(dev, "Error %d opening adapter\n", rc);
  4005. goto reg_crq_failed;
  4006. }
  4007. retrc = 0;
  4008. tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
  4009. if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
  4010. dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
  4011. goto req_irq_failed;
  4012. }
  4013. if ((rc = vio_enable_interrupts(vdev))) {
  4014. dev_err(dev, "Error %d enabling interrupts\n", rc);
  4015. goto req_irq_failed;
  4016. }
  4017. crq->cur = 0;
  4018. LEAVE;
  4019. return retrc;
  4020. req_irq_failed:
  4021. tasklet_kill(&vhost->tasklet);
  4022. do {
  4023. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  4024. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  4025. reg_crq_failed:
  4026. dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  4027. map_failed:
  4028. free_page((unsigned long)crq->msgs);
  4029. return retrc;
  4030. }
  4031. /**
  4032. * ibmvfc_free_mem - Free memory for vhost
  4033. * @vhost: ibmvfc host struct
  4034. *
  4035. * Return value:
  4036. * none
  4037. **/
  4038. static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
  4039. {
  4040. struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
  4041. ENTER;
  4042. mempool_destroy(vhost->tgt_pool);
  4043. kfree(vhost->trace);
  4044. dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
  4045. vhost->disc_buf_dma);
  4046. dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
  4047. vhost->login_buf, vhost->login_buf_dma);
  4048. dma_pool_destroy(vhost->sg_pool);
  4049. dma_unmap_single(vhost->dev, async_q->msg_token,
  4050. async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
  4051. free_page((unsigned long)async_q->msgs);
  4052. LEAVE;
  4053. }
  4054. /**
  4055. * ibmvfc_alloc_mem - Allocate memory for vhost
  4056. * @vhost: ibmvfc host struct
  4057. *
  4058. * Return value:
  4059. * 0 on success / non-zero on failure
  4060. **/
  4061. static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
  4062. {
  4063. struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
  4064. struct device *dev = vhost->dev;
  4065. ENTER;
  4066. async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
  4067. if (!async_q->msgs) {
  4068. dev_err(dev, "Couldn't allocate async queue.\n");
  4069. goto nomem;
  4070. }
  4071. async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
  4072. async_q->msg_token = dma_map_single(dev, async_q->msgs,
  4073. async_q->size * sizeof(*async_q->msgs),
  4074. DMA_BIDIRECTIONAL);
  4075. if (dma_mapping_error(dev, async_q->msg_token)) {
  4076. dev_err(dev, "Failed to map async queue\n");
  4077. goto free_async_crq;
  4078. }
  4079. vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
  4080. SG_ALL * sizeof(struct srp_direct_buf),
  4081. sizeof(struct srp_direct_buf), 0);
  4082. if (!vhost->sg_pool) {
  4083. dev_err(dev, "Failed to allocate sg pool\n");
  4084. goto unmap_async_crq;
  4085. }
  4086. vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
  4087. &vhost->login_buf_dma, GFP_KERNEL);
  4088. if (!vhost->login_buf) {
  4089. dev_err(dev, "Couldn't allocate NPIV login buffer\n");
  4090. goto free_sg_pool;
  4091. }
  4092. vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
  4093. vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
  4094. &vhost->disc_buf_dma, GFP_KERNEL);
  4095. if (!vhost->disc_buf) {
  4096. dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
  4097. goto free_login_buffer;
  4098. }
  4099. vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
  4100. sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
  4101. if (!vhost->trace)
  4102. goto free_disc_buffer;
  4103. vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
  4104. sizeof(struct ibmvfc_target));
  4105. if (!vhost->tgt_pool) {
  4106. dev_err(dev, "Couldn't allocate target memory pool\n");
  4107. goto free_trace;
  4108. }
  4109. LEAVE;
  4110. return 0;
  4111. free_trace:
  4112. kfree(vhost->trace);
  4113. free_disc_buffer:
  4114. dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
  4115. vhost->disc_buf_dma);
  4116. free_login_buffer:
  4117. dma_free_coherent(dev, sizeof(*vhost->login_buf),
  4118. vhost->login_buf, vhost->login_buf_dma);
  4119. free_sg_pool:
  4120. dma_pool_destroy(vhost->sg_pool);
  4121. unmap_async_crq:
  4122. dma_unmap_single(dev, async_q->msg_token,
  4123. async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
  4124. free_async_crq:
  4125. free_page((unsigned long)async_q->msgs);
  4126. nomem:
  4127. LEAVE;
  4128. return -ENOMEM;
  4129. }
  4130. /**
  4131. * ibmvfc_rport_add_thread - Worker thread for rport adds
  4132. * @work: work struct
  4133. *
  4134. **/
  4135. static void ibmvfc_rport_add_thread(struct work_struct *work)
  4136. {
  4137. struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
  4138. rport_add_work_q);
  4139. struct ibmvfc_target *tgt;
  4140. struct fc_rport *rport;
  4141. unsigned long flags;
  4142. int did_work;
  4143. ENTER;
  4144. spin_lock_irqsave(vhost->host->host_lock, flags);
  4145. do {
  4146. did_work = 0;
  4147. if (vhost->state != IBMVFC_ACTIVE)
  4148. break;
  4149. list_for_each_entry(tgt, &vhost->targets, queue) {
  4150. if (tgt->add_rport) {
  4151. did_work = 1;
  4152. tgt->add_rport = 0;
  4153. kref_get(&tgt->kref);
  4154. rport = tgt->rport;
  4155. if (!rport) {
  4156. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4157. ibmvfc_tgt_add_rport(tgt);
  4158. } else if (get_device(&rport->dev)) {
  4159. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4160. tgt_dbg(tgt, "Setting rport roles\n");
  4161. fc_remote_port_rolechg(rport, tgt->ids.roles);
  4162. put_device(&rport->dev);
  4163. }
  4164. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4165. spin_lock_irqsave(vhost->host->host_lock, flags);
  4166. break;
  4167. }
  4168. }
  4169. } while(did_work);
  4170. if (vhost->state == IBMVFC_ACTIVE)
  4171. vhost->scan_complete = 1;
  4172. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4173. LEAVE;
  4174. }
  4175. /**
  4176. * ibmvfc_probe - Adapter hot plug add entry point
  4177. * @vdev: vio device struct
  4178. * @id: vio device id struct
  4179. *
  4180. * Return value:
  4181. * 0 on success / non-zero on failure
  4182. **/
  4183. static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  4184. {
  4185. struct ibmvfc_host *vhost;
  4186. struct Scsi_Host *shost;
  4187. struct device *dev = &vdev->dev;
  4188. int rc = -ENOMEM;
  4189. ENTER;
  4190. shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
  4191. if (!shost) {
  4192. dev_err(dev, "Couldn't allocate host data\n");
  4193. goto out;
  4194. }
  4195. shost->transportt = ibmvfc_transport_template;
  4196. shost->can_queue = max_requests;
  4197. shost->max_lun = max_lun;
  4198. shost->max_id = max_targets;
  4199. shost->max_sectors = IBMVFC_MAX_SECTORS;
  4200. shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
  4201. shost->unique_id = shost->host_no;
  4202. vhost = shost_priv(shost);
  4203. INIT_LIST_HEAD(&vhost->sent);
  4204. INIT_LIST_HEAD(&vhost->free);
  4205. INIT_LIST_HEAD(&vhost->targets);
  4206. sprintf(vhost->name, IBMVFC_NAME);
  4207. vhost->host = shost;
  4208. vhost->dev = dev;
  4209. vhost->partition_number = -1;
  4210. vhost->log_level = log_level;
  4211. vhost->task_set = 1;
  4212. strcpy(vhost->partition_name, "UNKNOWN");
  4213. init_waitqueue_head(&vhost->work_wait_q);
  4214. init_waitqueue_head(&vhost->init_wait_q);
  4215. INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
  4216. mutex_init(&vhost->passthru_mutex);
  4217. if ((rc = ibmvfc_alloc_mem(vhost)))
  4218. goto free_scsi_host;
  4219. vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
  4220. shost->host_no);
  4221. if (IS_ERR(vhost->work_thread)) {
  4222. dev_err(dev, "Couldn't create kernel thread: %ld\n",
  4223. PTR_ERR(vhost->work_thread));
  4224. goto free_host_mem;
  4225. }
  4226. if ((rc = ibmvfc_init_crq(vhost))) {
  4227. dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
  4228. goto kill_kthread;
  4229. }
  4230. if ((rc = ibmvfc_init_event_pool(vhost))) {
  4231. dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
  4232. goto release_crq;
  4233. }
  4234. if ((rc = scsi_add_host(shost, dev)))
  4235. goto release_event_pool;
  4236. fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
  4237. if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
  4238. &ibmvfc_trace_attr))) {
  4239. dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
  4240. goto remove_shost;
  4241. }
  4242. if (shost_to_fc_host(shost)->rqst_q)
  4243. blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
  4244. dev_set_drvdata(dev, vhost);
  4245. spin_lock(&ibmvfc_driver_lock);
  4246. list_add_tail(&vhost->queue, &ibmvfc_head);
  4247. spin_unlock(&ibmvfc_driver_lock);
  4248. ibmvfc_send_crq_init(vhost);
  4249. scsi_scan_host(shost);
  4250. return 0;
  4251. remove_shost:
  4252. scsi_remove_host(shost);
  4253. release_event_pool:
  4254. ibmvfc_free_event_pool(vhost);
  4255. release_crq:
  4256. ibmvfc_release_crq_queue(vhost);
  4257. kill_kthread:
  4258. kthread_stop(vhost->work_thread);
  4259. free_host_mem:
  4260. ibmvfc_free_mem(vhost);
  4261. free_scsi_host:
  4262. scsi_host_put(shost);
  4263. out:
  4264. LEAVE;
  4265. return rc;
  4266. }
  4267. /**
  4268. * ibmvfc_remove - Adapter hot plug remove entry point
  4269. * @vdev: vio device struct
  4270. *
  4271. * Return value:
  4272. * 0
  4273. **/
  4274. static int ibmvfc_remove(struct vio_dev *vdev)
  4275. {
  4276. struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
  4277. unsigned long flags;
  4278. ENTER;
  4279. ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
  4280. spin_lock_irqsave(vhost->host->host_lock, flags);
  4281. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  4282. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4283. ibmvfc_wait_while_resetting(vhost);
  4284. ibmvfc_release_crq_queue(vhost);
  4285. kthread_stop(vhost->work_thread);
  4286. fc_remove_host(vhost->host);
  4287. scsi_remove_host(vhost->host);
  4288. spin_lock_irqsave(vhost->host->host_lock, flags);
  4289. ibmvfc_purge_requests(vhost, DID_ERROR);
  4290. ibmvfc_free_event_pool(vhost);
  4291. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4292. ibmvfc_free_mem(vhost);
  4293. spin_lock(&ibmvfc_driver_lock);
  4294. list_del(&vhost->queue);
  4295. spin_unlock(&ibmvfc_driver_lock);
  4296. scsi_host_put(vhost->host);
  4297. LEAVE;
  4298. return 0;
  4299. }
  4300. /**
  4301. * ibmvfc_resume - Resume from suspend
  4302. * @dev: device struct
  4303. *
  4304. * We may have lost an interrupt across suspend/resume, so kick the
  4305. * interrupt handler
  4306. *
  4307. */
  4308. static int ibmvfc_resume(struct device *dev)
  4309. {
  4310. unsigned long flags;
  4311. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  4312. struct vio_dev *vdev = to_vio_dev(dev);
  4313. spin_lock_irqsave(vhost->host->host_lock, flags);
  4314. vio_disable_interrupts(vdev);
  4315. tasklet_schedule(&vhost->tasklet);
  4316. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4317. return 0;
  4318. }
  4319. /**
  4320. * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
  4321. * @vdev: vio device struct
  4322. *
  4323. * Return value:
  4324. * Number of bytes the driver will need to DMA map at the same time in
  4325. * order to perform well.
  4326. */
  4327. static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
  4328. {
  4329. unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
  4330. return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
  4331. }
  4332. static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
  4333. {"fcp", "IBM,vfc-client"},
  4334. { "", "" }
  4335. };
  4336. MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
  4337. static struct dev_pm_ops ibmvfc_pm_ops = {
  4338. .resume = ibmvfc_resume
  4339. };
  4340. static struct vio_driver ibmvfc_driver = {
  4341. .id_table = ibmvfc_device_table,
  4342. .probe = ibmvfc_probe,
  4343. .remove = ibmvfc_remove,
  4344. .get_desired_dma = ibmvfc_get_desired_dma,
  4345. .driver = {
  4346. .name = IBMVFC_NAME,
  4347. .owner = THIS_MODULE,
  4348. .pm = &ibmvfc_pm_ops,
  4349. }
  4350. };
  4351. static struct fc_function_template ibmvfc_transport_functions = {
  4352. .show_host_fabric_name = 1,
  4353. .show_host_node_name = 1,
  4354. .show_host_port_name = 1,
  4355. .show_host_supported_classes = 1,
  4356. .show_host_port_type = 1,
  4357. .show_host_port_id = 1,
  4358. .show_host_maxframe_size = 1,
  4359. .get_host_port_state = ibmvfc_get_host_port_state,
  4360. .show_host_port_state = 1,
  4361. .get_host_speed = ibmvfc_get_host_speed,
  4362. .show_host_speed = 1,
  4363. .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
  4364. .terminate_rport_io = ibmvfc_terminate_rport_io,
  4365. .show_rport_maxframe_size = 1,
  4366. .show_rport_supported_classes = 1,
  4367. .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
  4368. .show_rport_dev_loss_tmo = 1,
  4369. .get_starget_node_name = ibmvfc_get_starget_node_name,
  4370. .show_starget_node_name = 1,
  4371. .get_starget_port_name = ibmvfc_get_starget_port_name,
  4372. .show_starget_port_name = 1,
  4373. .get_starget_port_id = ibmvfc_get_starget_port_id,
  4374. .show_starget_port_id = 1,
  4375. .bsg_request = ibmvfc_bsg_request,
  4376. .bsg_timeout = ibmvfc_bsg_timeout,
  4377. };
  4378. /**
  4379. * ibmvfc_module_init - Initialize the ibmvfc module
  4380. *
  4381. * Return value:
  4382. * 0 on success / other on failure
  4383. **/
  4384. static int __init ibmvfc_module_init(void)
  4385. {
  4386. int rc;
  4387. if (!firmware_has_feature(FW_FEATURE_VIO))
  4388. return -ENODEV;
  4389. printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
  4390. IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
  4391. ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
  4392. if (!ibmvfc_transport_template)
  4393. return -ENOMEM;
  4394. rc = vio_register_driver(&ibmvfc_driver);
  4395. if (rc)
  4396. fc_release_transport(ibmvfc_transport_template);
  4397. return rc;
  4398. }
  4399. /**
  4400. * ibmvfc_module_exit - Teardown the ibmvfc module
  4401. *
  4402. * Return value:
  4403. * nothing
  4404. **/
  4405. static void __exit ibmvfc_module_exit(void)
  4406. {
  4407. vio_unregister_driver(&ibmvfc_driver);
  4408. fc_release_transport(ibmvfc_transport_template);
  4409. }
  4410. module_init(ibmvfc_module_init);
  4411. module_exit(ibmvfc_module_exit);