ibmvfc.c 114 KB

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