ibmvfc.c 132 KB

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