ibmvfc.c 135 KB

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