ibmvfc.c 134 KB

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