ibmvfc.c 115 KB

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