ibmvfc.c 124 KB

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