ibmvfc.c 111 KB

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