ibmvfc.c 107 KB

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