ibmvfc.c 111 KB

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