scsi_transport_fc.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146
  1. /*
  2. * FiberChannel transport specific attributes exported to sysfs.
  3. *
  4. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * ========
  21. *
  22. * Copyright (C) 2004-2007 James Smart, Emulex Corporation
  23. * Rewrite for host, target, device, and remote port attributes,
  24. * statistics, and service functions...
  25. * Add vports, etc
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/slab.h>
  31. #include <linux/delay.h>
  32. #include <linux/kernel.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi_transport.h>
  36. #include <scsi/scsi_transport_fc.h>
  37. #include <scsi/scsi_cmnd.h>
  38. #include <linux/netlink.h>
  39. #include <net/netlink.h>
  40. #include <scsi/scsi_netlink_fc.h>
  41. #include <scsi/scsi_bsg_fc.h>
  42. #include "scsi_priv.h"
  43. #include "scsi_transport_fc_internal.h"
  44. static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
  45. static void fc_vport_sched_delete(struct work_struct *work);
  46. static int fc_vport_setup(struct Scsi_Host *shost, int channel,
  47. struct device *pdev, struct fc_vport_identifiers *ids,
  48. struct fc_vport **vport);
  49. static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
  50. static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
  51. static void fc_bsg_remove(struct request_queue *);
  52. static void fc_bsg_goose_queue(struct fc_rport *);
  53. /*
  54. * Module Parameters
  55. */
  56. /*
  57. * dev_loss_tmo: the default number of seconds that the FC transport
  58. * should insulate the loss of a remote port.
  59. * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
  60. */
  61. static unsigned int fc_dev_loss_tmo = 60; /* seconds */
  62. module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
  63. MODULE_PARM_DESC(dev_loss_tmo,
  64. "Maximum number of seconds that the FC transport should"
  65. " insulate the loss of a remote port. Once this value is"
  66. " exceeded, the scsi target is removed. Value should be"
  67. " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
  68. " fast_io_fail_tmo is not set.");
  69. /*
  70. * Redefine so that we can have same named attributes in the
  71. * sdev/starget/host objects.
  72. */
  73. #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  74. struct device_attribute device_attr_##_prefix##_##_name = \
  75. __ATTR(_name,_mode,_show,_store)
  76. #define fc_enum_name_search(title, table_type, table) \
  77. static const char *get_fc_##title##_name(enum table_type table_key) \
  78. { \
  79. int i; \
  80. char *name = NULL; \
  81. \
  82. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  83. if (table[i].value == table_key) { \
  84. name = table[i].name; \
  85. break; \
  86. } \
  87. } \
  88. return name; \
  89. }
  90. #define fc_enum_name_match(title, table_type, table) \
  91. static int get_fc_##title##_match(const char *table_key, \
  92. enum table_type *value) \
  93. { \
  94. int i; \
  95. \
  96. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  97. if (strncmp(table_key, table[i].name, \
  98. table[i].matchlen) == 0) { \
  99. *value = table[i].value; \
  100. return 0; /* success */ \
  101. } \
  102. } \
  103. return 1; /* failure */ \
  104. }
  105. /* Convert fc_port_type values to ascii string name */
  106. static struct {
  107. enum fc_port_type value;
  108. char *name;
  109. } fc_port_type_names[] = {
  110. { FC_PORTTYPE_UNKNOWN, "Unknown" },
  111. { FC_PORTTYPE_OTHER, "Other" },
  112. { FC_PORTTYPE_NOTPRESENT, "Not Present" },
  113. { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
  114. { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
  115. { FC_PORTTYPE_LPORT, "LPort (private loop)" },
  116. { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
  117. { FC_PORTTYPE_NPIV, "NPIV VPORT" },
  118. };
  119. fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
  120. #define FC_PORTTYPE_MAX_NAMELEN 50
  121. /* Reuse fc_port_type enum function for vport_type */
  122. #define get_fc_vport_type_name get_fc_port_type_name
  123. /* Convert fc_host_event_code values to ascii string name */
  124. static const struct {
  125. enum fc_host_event_code value;
  126. char *name;
  127. } fc_host_event_code_names[] = {
  128. { FCH_EVT_LIP, "lip" },
  129. { FCH_EVT_LINKUP, "link_up" },
  130. { FCH_EVT_LINKDOWN, "link_down" },
  131. { FCH_EVT_LIPRESET, "lip_reset" },
  132. { FCH_EVT_RSCN, "rscn" },
  133. { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
  134. { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
  135. { FCH_EVT_PORT_ONLINE, "port_online" },
  136. { FCH_EVT_PORT_OFFLINE, "port_offline" },
  137. { FCH_EVT_PORT_FABRIC, "port_fabric" },
  138. { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
  139. { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
  140. };
  141. fc_enum_name_search(host_event_code, fc_host_event_code,
  142. fc_host_event_code_names)
  143. #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
  144. /* Convert fc_port_state values to ascii string name */
  145. static struct {
  146. enum fc_port_state value;
  147. char *name;
  148. } fc_port_state_names[] = {
  149. { FC_PORTSTATE_UNKNOWN, "Unknown" },
  150. { FC_PORTSTATE_NOTPRESENT, "Not Present" },
  151. { FC_PORTSTATE_ONLINE, "Online" },
  152. { FC_PORTSTATE_OFFLINE, "Offline" },
  153. { FC_PORTSTATE_BLOCKED, "Blocked" },
  154. { FC_PORTSTATE_BYPASSED, "Bypassed" },
  155. { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
  156. { FC_PORTSTATE_LINKDOWN, "Linkdown" },
  157. { FC_PORTSTATE_ERROR, "Error" },
  158. { FC_PORTSTATE_LOOPBACK, "Loopback" },
  159. { FC_PORTSTATE_DELETED, "Deleted" },
  160. };
  161. fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
  162. #define FC_PORTSTATE_MAX_NAMELEN 20
  163. /* Convert fc_vport_state values to ascii string name */
  164. static struct {
  165. enum fc_vport_state value;
  166. char *name;
  167. } fc_vport_state_names[] = {
  168. { FC_VPORT_UNKNOWN, "Unknown" },
  169. { FC_VPORT_ACTIVE, "Active" },
  170. { FC_VPORT_DISABLED, "Disabled" },
  171. { FC_VPORT_LINKDOWN, "Linkdown" },
  172. { FC_VPORT_INITIALIZING, "Initializing" },
  173. { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
  174. { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
  175. { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
  176. { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
  177. { FC_VPORT_FAILED, "VPort Failed" },
  178. };
  179. fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
  180. #define FC_VPORTSTATE_MAX_NAMELEN 24
  181. /* Reuse fc_vport_state enum function for vport_last_state */
  182. #define get_fc_vport_last_state_name get_fc_vport_state_name
  183. /* Convert fc_tgtid_binding_type values to ascii string name */
  184. static const struct {
  185. enum fc_tgtid_binding_type value;
  186. char *name;
  187. int matchlen;
  188. } fc_tgtid_binding_type_names[] = {
  189. { FC_TGTID_BIND_NONE, "none", 4 },
  190. { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
  191. { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
  192. { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
  193. };
  194. fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
  195. fc_tgtid_binding_type_names)
  196. fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
  197. fc_tgtid_binding_type_names)
  198. #define FC_BINDTYPE_MAX_NAMELEN 30
  199. #define fc_bitfield_name_search(title, table) \
  200. static ssize_t \
  201. get_fc_##title##_names(u32 table_key, char *buf) \
  202. { \
  203. char *prefix = ""; \
  204. ssize_t len = 0; \
  205. int i; \
  206. \
  207. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  208. if (table[i].value & table_key) { \
  209. len += sprintf(buf + len, "%s%s", \
  210. prefix, table[i].name); \
  211. prefix = ", "; \
  212. } \
  213. } \
  214. len += sprintf(buf + len, "\n"); \
  215. return len; \
  216. }
  217. /* Convert FC_COS bit values to ascii string name */
  218. static const struct {
  219. u32 value;
  220. char *name;
  221. } fc_cos_names[] = {
  222. { FC_COS_CLASS1, "Class 1" },
  223. { FC_COS_CLASS2, "Class 2" },
  224. { FC_COS_CLASS3, "Class 3" },
  225. { FC_COS_CLASS4, "Class 4" },
  226. { FC_COS_CLASS6, "Class 6" },
  227. };
  228. fc_bitfield_name_search(cos, fc_cos_names)
  229. /* Convert FC_PORTSPEED bit values to ascii string name */
  230. static const struct {
  231. u32 value;
  232. char *name;
  233. } fc_port_speed_names[] = {
  234. { FC_PORTSPEED_1GBIT, "1 Gbit" },
  235. { FC_PORTSPEED_2GBIT, "2 Gbit" },
  236. { FC_PORTSPEED_4GBIT, "4 Gbit" },
  237. { FC_PORTSPEED_10GBIT, "10 Gbit" },
  238. { FC_PORTSPEED_8GBIT, "8 Gbit" },
  239. { FC_PORTSPEED_16GBIT, "16 Gbit" },
  240. { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
  241. };
  242. fc_bitfield_name_search(port_speed, fc_port_speed_names)
  243. static int
  244. show_fc_fc4s (char *buf, u8 *fc4_list)
  245. {
  246. int i, len=0;
  247. for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
  248. len += sprintf(buf + len , "0x%02x ", *fc4_list);
  249. len += sprintf(buf + len, "\n");
  250. return len;
  251. }
  252. /* Convert FC_PORT_ROLE bit values to ascii string name */
  253. static const struct {
  254. u32 value;
  255. char *name;
  256. } fc_port_role_names[] = {
  257. { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
  258. { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
  259. { FC_PORT_ROLE_IP_PORT, "IP Port" },
  260. };
  261. fc_bitfield_name_search(port_roles, fc_port_role_names)
  262. /*
  263. * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
  264. */
  265. #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
  266. #define FC_WELLKNOWN_ROLE_MASK 0x00000f
  267. #define FC_FPORT_PORTID 0x00000e
  268. #define FC_FABCTLR_PORTID 0x00000d
  269. #define FC_DIRSRVR_PORTID 0x00000c
  270. #define FC_TIMESRVR_PORTID 0x00000b
  271. #define FC_MGMTSRVR_PORTID 0x00000a
  272. static void fc_timeout_deleted_rport(struct work_struct *work);
  273. static void fc_timeout_fail_rport_io(struct work_struct *work);
  274. static void fc_scsi_scan_rport(struct work_struct *work);
  275. /*
  276. * Attribute counts pre object type...
  277. * Increase these values if you add attributes
  278. */
  279. #define FC_STARGET_NUM_ATTRS 3
  280. #define FC_RPORT_NUM_ATTRS 10
  281. #define FC_VPORT_NUM_ATTRS 9
  282. #define FC_HOST_NUM_ATTRS 29
  283. struct fc_internal {
  284. struct scsi_transport_template t;
  285. struct fc_function_template *f;
  286. /*
  287. * For attributes : each object has :
  288. * An array of the actual attributes structures
  289. * An array of null-terminated pointers to the attribute
  290. * structures - used for mid-layer interaction.
  291. *
  292. * The attribute containers for the starget and host are are
  293. * part of the midlayer. As the remote port is specific to the
  294. * fc transport, we must provide the attribute container.
  295. */
  296. struct device_attribute private_starget_attrs[
  297. FC_STARGET_NUM_ATTRS];
  298. struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
  299. struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
  300. struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
  301. struct transport_container rport_attr_cont;
  302. struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
  303. struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
  304. struct transport_container vport_attr_cont;
  305. struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
  306. struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
  307. };
  308. #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
  309. static int fc_target_setup(struct transport_container *tc, struct device *dev,
  310. struct device *cdev)
  311. {
  312. struct scsi_target *starget = to_scsi_target(dev);
  313. struct fc_rport *rport = starget_to_rport(starget);
  314. /*
  315. * if parent is remote port, use values from remote port.
  316. * Otherwise, this host uses the fc_transport, but not the
  317. * remote port interface. As such, initialize to known non-values.
  318. */
  319. if (rport) {
  320. fc_starget_node_name(starget) = rport->node_name;
  321. fc_starget_port_name(starget) = rport->port_name;
  322. fc_starget_port_id(starget) = rport->port_id;
  323. } else {
  324. fc_starget_node_name(starget) = -1;
  325. fc_starget_port_name(starget) = -1;
  326. fc_starget_port_id(starget) = -1;
  327. }
  328. return 0;
  329. }
  330. static DECLARE_TRANSPORT_CLASS(fc_transport_class,
  331. "fc_transport",
  332. fc_target_setup,
  333. NULL,
  334. NULL);
  335. static int fc_host_setup(struct transport_container *tc, struct device *dev,
  336. struct device *cdev)
  337. {
  338. struct Scsi_Host *shost = dev_to_shost(dev);
  339. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  340. /*
  341. * Set default values easily detected by the midlayer as
  342. * failure cases. The scsi lldd is responsible for initializing
  343. * all transport attributes to valid values per host.
  344. */
  345. fc_host->node_name = -1;
  346. fc_host->port_name = -1;
  347. fc_host->permanent_port_name = -1;
  348. fc_host->supported_classes = FC_COS_UNSPECIFIED;
  349. memset(fc_host->supported_fc4s, 0,
  350. sizeof(fc_host->supported_fc4s));
  351. fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
  352. fc_host->maxframe_size = -1;
  353. fc_host->max_npiv_vports = 0;
  354. memset(fc_host->serial_number, 0,
  355. sizeof(fc_host->serial_number));
  356. memset(fc_host->manufacturer, 0,
  357. sizeof(fc_host->manufacturer));
  358. memset(fc_host->model, 0,
  359. sizeof(fc_host->model));
  360. memset(fc_host->model_description, 0,
  361. sizeof(fc_host->model_description));
  362. memset(fc_host->hardware_version, 0,
  363. sizeof(fc_host->hardware_version));
  364. memset(fc_host->driver_version, 0,
  365. sizeof(fc_host->driver_version));
  366. memset(fc_host->firmware_version, 0,
  367. sizeof(fc_host->firmware_version));
  368. memset(fc_host->optionrom_version, 0,
  369. sizeof(fc_host->optionrom_version));
  370. fc_host->port_id = -1;
  371. fc_host->port_type = FC_PORTTYPE_UNKNOWN;
  372. fc_host->port_state = FC_PORTSTATE_UNKNOWN;
  373. memset(fc_host->active_fc4s, 0,
  374. sizeof(fc_host->active_fc4s));
  375. fc_host->speed = FC_PORTSPEED_UNKNOWN;
  376. fc_host->fabric_name = -1;
  377. memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
  378. memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
  379. fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
  380. INIT_LIST_HEAD(&fc_host->rports);
  381. INIT_LIST_HEAD(&fc_host->rport_bindings);
  382. INIT_LIST_HEAD(&fc_host->vports);
  383. fc_host->next_rport_number = 0;
  384. fc_host->next_target_id = 0;
  385. fc_host->next_vport_number = 0;
  386. fc_host->npiv_vports_inuse = 0;
  387. snprintf(fc_host->work_q_name, sizeof(fc_host->work_q_name),
  388. "fc_wq_%d", shost->host_no);
  389. fc_host->work_q = alloc_workqueue(fc_host->work_q_name, 0, 0);
  390. if (!fc_host->work_q)
  391. return -ENOMEM;
  392. fc_host->dev_loss_tmo = fc_dev_loss_tmo;
  393. snprintf(fc_host->devloss_work_q_name,
  394. sizeof(fc_host->devloss_work_q_name),
  395. "fc_dl_%d", shost->host_no);
  396. fc_host->devloss_work_q =
  397. alloc_workqueue(fc_host->devloss_work_q_name, 0, 0);
  398. if (!fc_host->devloss_work_q) {
  399. destroy_workqueue(fc_host->work_q);
  400. fc_host->work_q = NULL;
  401. return -ENOMEM;
  402. }
  403. fc_bsg_hostadd(shost, fc_host);
  404. /* ignore any bsg add error - we just can't do sgio */
  405. return 0;
  406. }
  407. static int fc_host_remove(struct transport_container *tc, struct device *dev,
  408. struct device *cdev)
  409. {
  410. struct Scsi_Host *shost = dev_to_shost(dev);
  411. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  412. fc_bsg_remove(fc_host->rqst_q);
  413. return 0;
  414. }
  415. static DECLARE_TRANSPORT_CLASS(fc_host_class,
  416. "fc_host",
  417. fc_host_setup,
  418. fc_host_remove,
  419. NULL);
  420. /*
  421. * Setup and Remove actions for remote ports are handled
  422. * in the service functions below.
  423. */
  424. static DECLARE_TRANSPORT_CLASS(fc_rport_class,
  425. "fc_remote_ports",
  426. NULL,
  427. NULL,
  428. NULL);
  429. /*
  430. * Setup and Remove actions for virtual ports are handled
  431. * in the service functions below.
  432. */
  433. static DECLARE_TRANSPORT_CLASS(fc_vport_class,
  434. "fc_vports",
  435. NULL,
  436. NULL,
  437. NULL);
  438. /*
  439. * Netlink Infrastructure
  440. */
  441. static atomic_t fc_event_seq;
  442. /**
  443. * fc_get_event_number - Obtain the next sequential FC event number
  444. *
  445. * Notes:
  446. * We could have inlined this, but it would have required fc_event_seq to
  447. * be exposed. For now, live with the subroutine call.
  448. * Atomic used to avoid lock/unlock...
  449. */
  450. u32
  451. fc_get_event_number(void)
  452. {
  453. return atomic_add_return(1, &fc_event_seq);
  454. }
  455. EXPORT_SYMBOL(fc_get_event_number);
  456. /**
  457. * fc_host_post_event - called to post an even on an fc_host.
  458. * @shost: host the event occurred on
  459. * @event_number: fc event number obtained from get_fc_event_number()
  460. * @event_code: fc_host event being posted
  461. * @event_data: 32bits of data for the event being posted
  462. *
  463. * Notes:
  464. * This routine assumes no locks are held on entry.
  465. */
  466. void
  467. fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
  468. enum fc_host_event_code event_code, u32 event_data)
  469. {
  470. struct sk_buff *skb;
  471. struct nlmsghdr *nlh;
  472. struct fc_nl_event *event;
  473. const char *name;
  474. u32 len, skblen;
  475. int err;
  476. if (!scsi_nl_sock) {
  477. err = -ENOENT;
  478. goto send_fail;
  479. }
  480. len = FC_NL_MSGALIGN(sizeof(*event));
  481. skblen = NLMSG_SPACE(len);
  482. skb = alloc_skb(skblen, GFP_KERNEL);
  483. if (!skb) {
  484. err = -ENOBUFS;
  485. goto send_fail;
  486. }
  487. nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
  488. skblen - sizeof(*nlh), 0);
  489. if (!nlh) {
  490. err = -ENOBUFS;
  491. goto send_fail_skb;
  492. }
  493. event = NLMSG_DATA(nlh);
  494. INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
  495. FC_NL_ASYNC_EVENT, len);
  496. event->seconds = get_seconds();
  497. event->vendor_id = 0;
  498. event->host_no = shost->host_no;
  499. event->event_datalen = sizeof(u32); /* bytes */
  500. event->event_num = event_number;
  501. event->event_code = event_code;
  502. event->event_data = event_data;
  503. nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
  504. GFP_KERNEL);
  505. return;
  506. send_fail_skb:
  507. kfree_skb(skb);
  508. send_fail:
  509. name = get_fc_host_event_code_name(event_code);
  510. printk(KERN_WARNING
  511. "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
  512. __func__, shost->host_no,
  513. (name) ? name : "<unknown>", event_data, err);
  514. return;
  515. }
  516. EXPORT_SYMBOL(fc_host_post_event);
  517. /**
  518. * fc_host_post_vendor_event - called to post a vendor unique event on an fc_host
  519. * @shost: host the event occurred on
  520. * @event_number: fc event number obtained from get_fc_event_number()
  521. * @data_len: amount, in bytes, of vendor unique data
  522. * @data_buf: pointer to vendor unique data
  523. * @vendor_id: Vendor id
  524. *
  525. * Notes:
  526. * This routine assumes no locks are held on entry.
  527. */
  528. void
  529. fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
  530. u32 data_len, char * data_buf, u64 vendor_id)
  531. {
  532. struct sk_buff *skb;
  533. struct nlmsghdr *nlh;
  534. struct fc_nl_event *event;
  535. u32 len, skblen;
  536. int err;
  537. if (!scsi_nl_sock) {
  538. err = -ENOENT;
  539. goto send_vendor_fail;
  540. }
  541. len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
  542. skblen = NLMSG_SPACE(len);
  543. skb = alloc_skb(skblen, GFP_KERNEL);
  544. if (!skb) {
  545. err = -ENOBUFS;
  546. goto send_vendor_fail;
  547. }
  548. nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
  549. skblen - sizeof(*nlh), 0);
  550. if (!nlh) {
  551. err = -ENOBUFS;
  552. goto send_vendor_fail_skb;
  553. }
  554. event = NLMSG_DATA(nlh);
  555. INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
  556. FC_NL_ASYNC_EVENT, len);
  557. event->seconds = get_seconds();
  558. event->vendor_id = vendor_id;
  559. event->host_no = shost->host_no;
  560. event->event_datalen = data_len; /* bytes */
  561. event->event_num = event_number;
  562. event->event_code = FCH_EVT_VENDOR_UNIQUE;
  563. memcpy(&event->event_data, data_buf, data_len);
  564. nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
  565. GFP_KERNEL);
  566. return;
  567. send_vendor_fail_skb:
  568. kfree_skb(skb);
  569. send_vendor_fail:
  570. printk(KERN_WARNING
  571. "%s: Dropped Event : host %d vendor_unique - err %d\n",
  572. __func__, shost->host_no, err);
  573. return;
  574. }
  575. EXPORT_SYMBOL(fc_host_post_vendor_event);
  576. static __init int fc_transport_init(void)
  577. {
  578. int error;
  579. atomic_set(&fc_event_seq, 0);
  580. error = transport_class_register(&fc_host_class);
  581. if (error)
  582. return error;
  583. error = transport_class_register(&fc_vport_class);
  584. if (error)
  585. goto unreg_host_class;
  586. error = transport_class_register(&fc_rport_class);
  587. if (error)
  588. goto unreg_vport_class;
  589. error = transport_class_register(&fc_transport_class);
  590. if (error)
  591. goto unreg_rport_class;
  592. return 0;
  593. unreg_rport_class:
  594. transport_class_unregister(&fc_rport_class);
  595. unreg_vport_class:
  596. transport_class_unregister(&fc_vport_class);
  597. unreg_host_class:
  598. transport_class_unregister(&fc_host_class);
  599. return error;
  600. }
  601. static void __exit fc_transport_exit(void)
  602. {
  603. transport_class_unregister(&fc_transport_class);
  604. transport_class_unregister(&fc_rport_class);
  605. transport_class_unregister(&fc_host_class);
  606. transport_class_unregister(&fc_vport_class);
  607. }
  608. /*
  609. * FC Remote Port Attribute Management
  610. */
  611. #define fc_rport_show_function(field, format_string, sz, cast) \
  612. static ssize_t \
  613. show_fc_rport_##field (struct device *dev, \
  614. struct device_attribute *attr, char *buf) \
  615. { \
  616. struct fc_rport *rport = transport_class_to_rport(dev); \
  617. struct Scsi_Host *shost = rport_to_shost(rport); \
  618. struct fc_internal *i = to_fc_internal(shost->transportt); \
  619. if ((i->f->get_rport_##field) && \
  620. !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
  621. (rport->port_state == FC_PORTSTATE_DELETED) || \
  622. (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
  623. i->f->get_rport_##field(rport); \
  624. return snprintf(buf, sz, format_string, cast rport->field); \
  625. }
  626. #define fc_rport_store_function(field) \
  627. static ssize_t \
  628. store_fc_rport_##field(struct device *dev, \
  629. struct device_attribute *attr, \
  630. const char *buf, size_t count) \
  631. { \
  632. int val; \
  633. struct fc_rport *rport = transport_class_to_rport(dev); \
  634. struct Scsi_Host *shost = rport_to_shost(rport); \
  635. struct fc_internal *i = to_fc_internal(shost->transportt); \
  636. char *cp; \
  637. if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
  638. (rport->port_state == FC_PORTSTATE_DELETED) || \
  639. (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
  640. return -EBUSY; \
  641. val = simple_strtoul(buf, &cp, 0); \
  642. if (*cp && (*cp != '\n')) \
  643. return -EINVAL; \
  644. i->f->set_rport_##field(rport, val); \
  645. return count; \
  646. }
  647. #define fc_rport_rd_attr(field, format_string, sz) \
  648. fc_rport_show_function(field, format_string, sz, ) \
  649. static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
  650. show_fc_rport_##field, NULL)
  651. #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
  652. fc_rport_show_function(field, format_string, sz, (cast)) \
  653. static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
  654. show_fc_rport_##field, NULL)
  655. #define fc_rport_rw_attr(field, format_string, sz) \
  656. fc_rport_show_function(field, format_string, sz, ) \
  657. fc_rport_store_function(field) \
  658. static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
  659. show_fc_rport_##field, \
  660. store_fc_rport_##field)
  661. #define fc_private_rport_show_function(field, format_string, sz, cast) \
  662. static ssize_t \
  663. show_fc_rport_##field (struct device *dev, \
  664. struct device_attribute *attr, char *buf) \
  665. { \
  666. struct fc_rport *rport = transport_class_to_rport(dev); \
  667. return snprintf(buf, sz, format_string, cast rport->field); \
  668. }
  669. #define fc_private_rport_rd_attr(field, format_string, sz) \
  670. fc_private_rport_show_function(field, format_string, sz, ) \
  671. static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
  672. show_fc_rport_##field, NULL)
  673. #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
  674. fc_private_rport_show_function(field, format_string, sz, (cast)) \
  675. static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
  676. show_fc_rport_##field, NULL)
  677. #define fc_private_rport_rd_enum_attr(title, maxlen) \
  678. static ssize_t \
  679. show_fc_rport_##title (struct device *dev, \
  680. struct device_attribute *attr, char *buf) \
  681. { \
  682. struct fc_rport *rport = transport_class_to_rport(dev); \
  683. const char *name; \
  684. name = get_fc_##title##_name(rport->title); \
  685. if (!name) \
  686. return -EINVAL; \
  687. return snprintf(buf, maxlen, "%s\n", name); \
  688. } \
  689. static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
  690. show_fc_rport_##title, NULL)
  691. #define SETUP_RPORT_ATTRIBUTE_RD(field) \
  692. i->private_rport_attrs[count] = device_attr_rport_##field; \
  693. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  694. i->private_rport_attrs[count].store = NULL; \
  695. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  696. if (i->f->show_rport_##field) \
  697. count++
  698. #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
  699. i->private_rport_attrs[count] = device_attr_rport_##field; \
  700. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  701. i->private_rport_attrs[count].store = NULL; \
  702. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  703. count++
  704. #define SETUP_RPORT_ATTRIBUTE_RW(field) \
  705. i->private_rport_attrs[count] = device_attr_rport_##field; \
  706. if (!i->f->set_rport_##field) { \
  707. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  708. i->private_rport_attrs[count].store = NULL; \
  709. } \
  710. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  711. if (i->f->show_rport_##field) \
  712. count++
  713. #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
  714. { \
  715. i->private_rport_attrs[count] = device_attr_rport_##field; \
  716. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  717. count++; \
  718. }
  719. /* The FC Transport Remote Port Attributes: */
  720. /* Fixed Remote Port Attributes */
  721. fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
  722. static ssize_t
  723. show_fc_rport_supported_classes (struct device *dev,
  724. struct device_attribute *attr, char *buf)
  725. {
  726. struct fc_rport *rport = transport_class_to_rport(dev);
  727. if (rport->supported_classes == FC_COS_UNSPECIFIED)
  728. return snprintf(buf, 20, "unspecified\n");
  729. return get_fc_cos_names(rport->supported_classes, buf);
  730. }
  731. static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
  732. show_fc_rport_supported_classes, NULL);
  733. /* Dynamic Remote Port Attributes */
  734. /*
  735. * dev_loss_tmo attribute
  736. */
  737. static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
  738. {
  739. char *cp;
  740. *val = simple_strtoul(buf, &cp, 0);
  741. if ((*cp && (*cp != '\n')) || (*val < 0))
  742. return -EINVAL;
  743. /*
  744. * Check for overflow; dev_loss_tmo is u32
  745. */
  746. if (*val > UINT_MAX)
  747. return -EINVAL;
  748. return 0;
  749. }
  750. static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
  751. unsigned long val)
  752. {
  753. struct Scsi_Host *shost = rport_to_shost(rport);
  754. struct fc_internal *i = to_fc_internal(shost->transportt);
  755. if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
  756. (rport->port_state == FC_PORTSTATE_DELETED) ||
  757. (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  758. return -EBUSY;
  759. /*
  760. * Check for overflow; dev_loss_tmo is u32
  761. */
  762. if (val > UINT_MAX)
  763. return -EINVAL;
  764. /*
  765. * If fast_io_fail is off we have to cap
  766. * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
  767. */
  768. if (rport->fast_io_fail_tmo == -1 &&
  769. val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
  770. return -EINVAL;
  771. i->f->set_rport_dev_loss_tmo(rport, val);
  772. return 0;
  773. }
  774. fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
  775. static ssize_t
  776. store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
  777. const char *buf, size_t count)
  778. {
  779. struct fc_rport *rport = transport_class_to_rport(dev);
  780. unsigned long val;
  781. int rc;
  782. rc = fc_str_to_dev_loss(buf, &val);
  783. if (rc)
  784. return rc;
  785. rc = fc_rport_set_dev_loss_tmo(rport, val);
  786. if (rc)
  787. return rc;
  788. return count;
  789. }
  790. static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
  791. show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
  792. /* Private Remote Port Attributes */
  793. fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  794. fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  795. fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
  796. static ssize_t
  797. show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
  798. char *buf)
  799. {
  800. struct fc_rport *rport = transport_class_to_rport(dev);
  801. /* identify any roles that are port_id specific */
  802. if ((rport->port_id != -1) &&
  803. (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
  804. FC_WELLKNOWN_PORTID_MASK) {
  805. switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
  806. case FC_FPORT_PORTID:
  807. return snprintf(buf, 30, "Fabric Port\n");
  808. case FC_FABCTLR_PORTID:
  809. return snprintf(buf, 30, "Fabric Controller\n");
  810. case FC_DIRSRVR_PORTID:
  811. return snprintf(buf, 30, "Directory Server\n");
  812. case FC_TIMESRVR_PORTID:
  813. return snprintf(buf, 30, "Time Server\n");
  814. case FC_MGMTSRVR_PORTID:
  815. return snprintf(buf, 30, "Management Server\n");
  816. default:
  817. return snprintf(buf, 30, "Unknown Fabric Entity\n");
  818. }
  819. } else {
  820. if (rport->roles == FC_PORT_ROLE_UNKNOWN)
  821. return snprintf(buf, 20, "unknown\n");
  822. return get_fc_port_roles_names(rport->roles, buf);
  823. }
  824. }
  825. static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
  826. show_fc_rport_roles, NULL);
  827. fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  828. fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
  829. /*
  830. * fast_io_fail_tmo attribute
  831. */
  832. static ssize_t
  833. show_fc_rport_fast_io_fail_tmo (struct device *dev,
  834. struct device_attribute *attr, char *buf)
  835. {
  836. struct fc_rport *rport = transport_class_to_rport(dev);
  837. if (rport->fast_io_fail_tmo == -1)
  838. return snprintf(buf, 5, "off\n");
  839. return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
  840. }
  841. static ssize_t
  842. store_fc_rport_fast_io_fail_tmo(struct device *dev,
  843. struct device_attribute *attr, const char *buf,
  844. size_t count)
  845. {
  846. int val;
  847. char *cp;
  848. struct fc_rport *rport = transport_class_to_rport(dev);
  849. if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
  850. (rport->port_state == FC_PORTSTATE_DELETED) ||
  851. (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  852. return -EBUSY;
  853. if (strncmp(buf, "off", 3) == 0)
  854. rport->fast_io_fail_tmo = -1;
  855. else {
  856. val = simple_strtoul(buf, &cp, 0);
  857. if ((*cp && (*cp != '\n')) || (val < 0))
  858. return -EINVAL;
  859. /*
  860. * Cap fast_io_fail by dev_loss_tmo or
  861. * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
  862. */
  863. if ((val >= rport->dev_loss_tmo) ||
  864. (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
  865. return -EINVAL;
  866. rport->fast_io_fail_tmo = val;
  867. }
  868. return count;
  869. }
  870. static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
  871. show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
  872. /*
  873. * FC SCSI Target Attribute Management
  874. */
  875. /*
  876. * Note: in the target show function we recognize when the remote
  877. * port is in the hierarchy and do not allow the driver to get
  878. * involved in sysfs functions. The driver only gets involved if
  879. * it's the "old" style that doesn't use rports.
  880. */
  881. #define fc_starget_show_function(field, format_string, sz, cast) \
  882. static ssize_t \
  883. show_fc_starget_##field (struct device *dev, \
  884. struct device_attribute *attr, char *buf) \
  885. { \
  886. struct scsi_target *starget = transport_class_to_starget(dev); \
  887. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  888. struct fc_internal *i = to_fc_internal(shost->transportt); \
  889. struct fc_rport *rport = starget_to_rport(starget); \
  890. if (rport) \
  891. fc_starget_##field(starget) = rport->field; \
  892. else if (i->f->get_starget_##field) \
  893. i->f->get_starget_##field(starget); \
  894. return snprintf(buf, sz, format_string, \
  895. cast fc_starget_##field(starget)); \
  896. }
  897. #define fc_starget_rd_attr(field, format_string, sz) \
  898. fc_starget_show_function(field, format_string, sz, ) \
  899. static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
  900. show_fc_starget_##field, NULL)
  901. #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
  902. fc_starget_show_function(field, format_string, sz, (cast)) \
  903. static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
  904. show_fc_starget_##field, NULL)
  905. #define SETUP_STARGET_ATTRIBUTE_RD(field) \
  906. i->private_starget_attrs[count] = device_attr_starget_##field; \
  907. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  908. i->private_starget_attrs[count].store = NULL; \
  909. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  910. if (i->f->show_starget_##field) \
  911. count++
  912. #define SETUP_STARGET_ATTRIBUTE_RW(field) \
  913. i->private_starget_attrs[count] = device_attr_starget_##field; \
  914. if (!i->f->set_starget_##field) { \
  915. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  916. i->private_starget_attrs[count].store = NULL; \
  917. } \
  918. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  919. if (i->f->show_starget_##field) \
  920. count++
  921. /* The FC Transport SCSI Target Attributes: */
  922. fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  923. fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  924. fc_starget_rd_attr(port_id, "0x%06x\n", 20);
  925. /*
  926. * FC Virtual Port Attribute Management
  927. */
  928. #define fc_vport_show_function(field, format_string, sz, cast) \
  929. static ssize_t \
  930. show_fc_vport_##field (struct device *dev, \
  931. struct device_attribute *attr, char *buf) \
  932. { \
  933. struct fc_vport *vport = transport_class_to_vport(dev); \
  934. struct Scsi_Host *shost = vport_to_shost(vport); \
  935. struct fc_internal *i = to_fc_internal(shost->transportt); \
  936. if ((i->f->get_vport_##field) && \
  937. !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
  938. i->f->get_vport_##field(vport); \
  939. return snprintf(buf, sz, format_string, cast vport->field); \
  940. }
  941. #define fc_vport_store_function(field) \
  942. static ssize_t \
  943. store_fc_vport_##field(struct device *dev, \
  944. struct device_attribute *attr, \
  945. const char *buf, size_t count) \
  946. { \
  947. int val; \
  948. struct fc_vport *vport = transport_class_to_vport(dev); \
  949. struct Scsi_Host *shost = vport_to_shost(vport); \
  950. struct fc_internal *i = to_fc_internal(shost->transportt); \
  951. char *cp; \
  952. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
  953. return -EBUSY; \
  954. val = simple_strtoul(buf, &cp, 0); \
  955. if (*cp && (*cp != '\n')) \
  956. return -EINVAL; \
  957. i->f->set_vport_##field(vport, val); \
  958. return count; \
  959. }
  960. #define fc_vport_store_str_function(field, slen) \
  961. static ssize_t \
  962. store_fc_vport_##field(struct device *dev, \
  963. struct device_attribute *attr, \
  964. const char *buf, size_t count) \
  965. { \
  966. struct fc_vport *vport = transport_class_to_vport(dev); \
  967. struct Scsi_Host *shost = vport_to_shost(vport); \
  968. struct fc_internal *i = to_fc_internal(shost->transportt); \
  969. unsigned int cnt=count; \
  970. \
  971. /* count may include a LF at end of string */ \
  972. if (buf[cnt-1] == '\n') \
  973. cnt--; \
  974. if (cnt > ((slen) - 1)) \
  975. return -EINVAL; \
  976. memcpy(vport->field, buf, cnt); \
  977. i->f->set_vport_##field(vport); \
  978. return count; \
  979. }
  980. #define fc_vport_rd_attr(field, format_string, sz) \
  981. fc_vport_show_function(field, format_string, sz, ) \
  982. static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
  983. show_fc_vport_##field, NULL)
  984. #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
  985. fc_vport_show_function(field, format_string, sz, (cast)) \
  986. static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
  987. show_fc_vport_##field, NULL)
  988. #define fc_vport_rw_attr(field, format_string, sz) \
  989. fc_vport_show_function(field, format_string, sz, ) \
  990. fc_vport_store_function(field) \
  991. static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
  992. show_fc_vport_##field, \
  993. store_fc_vport_##field)
  994. #define fc_private_vport_show_function(field, format_string, sz, cast) \
  995. static ssize_t \
  996. show_fc_vport_##field (struct device *dev, \
  997. struct device_attribute *attr, char *buf) \
  998. { \
  999. struct fc_vport *vport = transport_class_to_vport(dev); \
  1000. return snprintf(buf, sz, format_string, cast vport->field); \
  1001. }
  1002. #define fc_private_vport_store_u32_function(field) \
  1003. static ssize_t \
  1004. store_fc_vport_##field(struct device *dev, \
  1005. struct device_attribute *attr, \
  1006. const char *buf, size_t count) \
  1007. { \
  1008. u32 val; \
  1009. struct fc_vport *vport = transport_class_to_vport(dev); \
  1010. char *cp; \
  1011. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
  1012. return -EBUSY; \
  1013. val = simple_strtoul(buf, &cp, 0); \
  1014. if (*cp && (*cp != '\n')) \
  1015. return -EINVAL; \
  1016. vport->field = val; \
  1017. return count; \
  1018. }
  1019. #define fc_private_vport_rd_attr(field, format_string, sz) \
  1020. fc_private_vport_show_function(field, format_string, sz, ) \
  1021. static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
  1022. show_fc_vport_##field, NULL)
  1023. #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
  1024. fc_private_vport_show_function(field, format_string, sz, (cast)) \
  1025. static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
  1026. show_fc_vport_##field, NULL)
  1027. #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
  1028. fc_private_vport_show_function(field, format_string, sz, ) \
  1029. fc_private_vport_store_u32_function(field) \
  1030. static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
  1031. show_fc_vport_##field, \
  1032. store_fc_vport_##field)
  1033. #define fc_private_vport_rd_enum_attr(title, maxlen) \
  1034. static ssize_t \
  1035. show_fc_vport_##title (struct device *dev, \
  1036. struct device_attribute *attr, \
  1037. char *buf) \
  1038. { \
  1039. struct fc_vport *vport = transport_class_to_vport(dev); \
  1040. const char *name; \
  1041. name = get_fc_##title##_name(vport->title); \
  1042. if (!name) \
  1043. return -EINVAL; \
  1044. return snprintf(buf, maxlen, "%s\n", name); \
  1045. } \
  1046. static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
  1047. show_fc_vport_##title, NULL)
  1048. #define SETUP_VPORT_ATTRIBUTE_RD(field) \
  1049. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1050. i->private_vport_attrs[count].attr.mode = S_IRUGO; \
  1051. i->private_vport_attrs[count].store = NULL; \
  1052. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1053. if (i->f->get_##field) \
  1054. count++
  1055. /* NOTE: Above MACRO differs: checks function not show bit */
  1056. #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
  1057. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1058. i->private_vport_attrs[count].attr.mode = S_IRUGO; \
  1059. i->private_vport_attrs[count].store = NULL; \
  1060. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1061. count++
  1062. #define SETUP_VPORT_ATTRIBUTE_WR(field) \
  1063. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1064. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1065. if (i->f->field) \
  1066. count++
  1067. /* NOTE: Above MACRO differs: checks function */
  1068. #define SETUP_VPORT_ATTRIBUTE_RW(field) \
  1069. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1070. if (!i->f->set_vport_##field) { \
  1071. i->private_vport_attrs[count].attr.mode = S_IRUGO; \
  1072. i->private_vport_attrs[count].store = NULL; \
  1073. } \
  1074. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1075. count++
  1076. /* NOTE: Above MACRO differs: does not check show bit */
  1077. #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
  1078. { \
  1079. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1080. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1081. count++; \
  1082. }
  1083. /* The FC Transport Virtual Port Attributes: */
  1084. /* Fixed Virtual Port Attributes */
  1085. /* Dynamic Virtual Port Attributes */
  1086. /* Private Virtual Port Attributes */
  1087. fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
  1088. fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
  1089. fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  1090. fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  1091. static ssize_t
  1092. show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
  1093. char *buf)
  1094. {
  1095. struct fc_vport *vport = transport_class_to_vport(dev);
  1096. if (vport->roles == FC_PORT_ROLE_UNKNOWN)
  1097. return snprintf(buf, 20, "unknown\n");
  1098. return get_fc_port_roles_names(vport->roles, buf);
  1099. }
  1100. static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
  1101. fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
  1102. fc_private_vport_show_function(symbolic_name, "%s\n",
  1103. FC_VPORT_SYMBOLIC_NAMELEN + 1, )
  1104. fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
  1105. static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
  1106. show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
  1107. static ssize_t
  1108. store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
  1109. const char *buf, size_t count)
  1110. {
  1111. struct fc_vport *vport = transport_class_to_vport(dev);
  1112. struct Scsi_Host *shost = vport_to_shost(vport);
  1113. unsigned long flags;
  1114. spin_lock_irqsave(shost->host_lock, flags);
  1115. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
  1116. spin_unlock_irqrestore(shost->host_lock, flags);
  1117. return -EBUSY;
  1118. }
  1119. vport->flags |= FC_VPORT_DELETING;
  1120. spin_unlock_irqrestore(shost->host_lock, flags);
  1121. fc_queue_work(shost, &vport->vport_delete_work);
  1122. return count;
  1123. }
  1124. static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
  1125. NULL, store_fc_vport_delete);
  1126. /*
  1127. * Enable/Disable vport
  1128. * Write "1" to disable, write "0" to enable
  1129. */
  1130. static ssize_t
  1131. store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
  1132. const char *buf,
  1133. size_t count)
  1134. {
  1135. struct fc_vport *vport = transport_class_to_vport(dev);
  1136. struct Scsi_Host *shost = vport_to_shost(vport);
  1137. struct fc_internal *i = to_fc_internal(shost->transportt);
  1138. int stat;
  1139. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
  1140. return -EBUSY;
  1141. if (*buf == '0') {
  1142. if (vport->vport_state != FC_VPORT_DISABLED)
  1143. return -EALREADY;
  1144. } else if (*buf == '1') {
  1145. if (vport->vport_state == FC_VPORT_DISABLED)
  1146. return -EALREADY;
  1147. } else
  1148. return -EINVAL;
  1149. stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
  1150. return stat ? stat : count;
  1151. }
  1152. static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
  1153. NULL, store_fc_vport_disable);
  1154. /*
  1155. * Host Attribute Management
  1156. */
  1157. #define fc_host_show_function(field, format_string, sz, cast) \
  1158. static ssize_t \
  1159. show_fc_host_##field (struct device *dev, \
  1160. struct device_attribute *attr, char *buf) \
  1161. { \
  1162. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1163. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1164. if (i->f->get_host_##field) \
  1165. i->f->get_host_##field(shost); \
  1166. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  1167. }
  1168. #define fc_host_store_function(field) \
  1169. static ssize_t \
  1170. store_fc_host_##field(struct device *dev, \
  1171. struct device_attribute *attr, \
  1172. const char *buf, size_t count) \
  1173. { \
  1174. int val; \
  1175. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1176. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1177. char *cp; \
  1178. \
  1179. val = simple_strtoul(buf, &cp, 0); \
  1180. if (*cp && (*cp != '\n')) \
  1181. return -EINVAL; \
  1182. i->f->set_host_##field(shost, val); \
  1183. return count; \
  1184. }
  1185. #define fc_host_store_str_function(field, slen) \
  1186. static ssize_t \
  1187. store_fc_host_##field(struct device *dev, \
  1188. struct device_attribute *attr, \
  1189. const char *buf, size_t count) \
  1190. { \
  1191. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1192. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1193. unsigned int cnt=count; \
  1194. \
  1195. /* count may include a LF at end of string */ \
  1196. if (buf[cnt-1] == '\n') \
  1197. cnt--; \
  1198. if (cnt > ((slen) - 1)) \
  1199. return -EINVAL; \
  1200. memcpy(fc_host_##field(shost), buf, cnt); \
  1201. i->f->set_host_##field(shost); \
  1202. return count; \
  1203. }
  1204. #define fc_host_rd_attr(field, format_string, sz) \
  1205. fc_host_show_function(field, format_string, sz, ) \
  1206. static FC_DEVICE_ATTR(host, field, S_IRUGO, \
  1207. show_fc_host_##field, NULL)
  1208. #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
  1209. fc_host_show_function(field, format_string, sz, (cast)) \
  1210. static FC_DEVICE_ATTR(host, field, S_IRUGO, \
  1211. show_fc_host_##field, NULL)
  1212. #define fc_host_rw_attr(field, format_string, sz) \
  1213. fc_host_show_function(field, format_string, sz, ) \
  1214. fc_host_store_function(field) \
  1215. static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
  1216. show_fc_host_##field, \
  1217. store_fc_host_##field)
  1218. #define fc_host_rd_enum_attr(title, maxlen) \
  1219. static ssize_t \
  1220. show_fc_host_##title (struct device *dev, \
  1221. struct device_attribute *attr, char *buf) \
  1222. { \
  1223. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1224. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1225. const char *name; \
  1226. if (i->f->get_host_##title) \
  1227. i->f->get_host_##title(shost); \
  1228. name = get_fc_##title##_name(fc_host_##title(shost)); \
  1229. if (!name) \
  1230. return -EINVAL; \
  1231. return snprintf(buf, maxlen, "%s\n", name); \
  1232. } \
  1233. static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
  1234. #define SETUP_HOST_ATTRIBUTE_RD(field) \
  1235. i->private_host_attrs[count] = device_attr_host_##field; \
  1236. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1237. i->private_host_attrs[count].store = NULL; \
  1238. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1239. if (i->f->show_host_##field) \
  1240. count++
  1241. #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
  1242. i->private_host_attrs[count] = device_attr_host_##field; \
  1243. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1244. i->private_host_attrs[count].store = NULL; \
  1245. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1246. count++
  1247. #define SETUP_HOST_ATTRIBUTE_RW(field) \
  1248. i->private_host_attrs[count] = device_attr_host_##field; \
  1249. if (!i->f->set_host_##field) { \
  1250. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1251. i->private_host_attrs[count].store = NULL; \
  1252. } \
  1253. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1254. if (i->f->show_host_##field) \
  1255. count++
  1256. #define fc_private_host_show_function(field, format_string, sz, cast) \
  1257. static ssize_t \
  1258. show_fc_host_##field (struct device *dev, \
  1259. struct device_attribute *attr, char *buf) \
  1260. { \
  1261. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1262. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  1263. }
  1264. #define fc_private_host_rd_attr(field, format_string, sz) \
  1265. fc_private_host_show_function(field, format_string, sz, ) \
  1266. static FC_DEVICE_ATTR(host, field, S_IRUGO, \
  1267. show_fc_host_##field, NULL)
  1268. #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
  1269. fc_private_host_show_function(field, format_string, sz, (cast)) \
  1270. static FC_DEVICE_ATTR(host, field, S_IRUGO, \
  1271. show_fc_host_##field, NULL)
  1272. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
  1273. i->private_host_attrs[count] = device_attr_host_##field; \
  1274. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1275. i->private_host_attrs[count].store = NULL; \
  1276. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1277. count++
  1278. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
  1279. { \
  1280. i->private_host_attrs[count] = device_attr_host_##field; \
  1281. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1282. count++; \
  1283. }
  1284. /* Fixed Host Attributes */
  1285. static ssize_t
  1286. show_fc_host_supported_classes (struct device *dev,
  1287. struct device_attribute *attr, char *buf)
  1288. {
  1289. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1290. if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
  1291. return snprintf(buf, 20, "unspecified\n");
  1292. return get_fc_cos_names(fc_host_supported_classes(shost), buf);
  1293. }
  1294. static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
  1295. show_fc_host_supported_classes, NULL);
  1296. static ssize_t
  1297. show_fc_host_supported_fc4s (struct device *dev,
  1298. struct device_attribute *attr, char *buf)
  1299. {
  1300. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1301. return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
  1302. }
  1303. static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
  1304. show_fc_host_supported_fc4s, NULL);
  1305. static ssize_t
  1306. show_fc_host_supported_speeds (struct device *dev,
  1307. struct device_attribute *attr, char *buf)
  1308. {
  1309. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1310. if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
  1311. return snprintf(buf, 20, "unknown\n");
  1312. return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
  1313. }
  1314. static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
  1315. show_fc_host_supported_speeds, NULL);
  1316. fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  1317. fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  1318. fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
  1319. unsigned long long);
  1320. fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
  1321. fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
  1322. fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
  1323. fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
  1324. fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
  1325. fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
  1326. fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
  1327. fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
  1328. fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
  1329. fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
  1330. /* Dynamic Host Attributes */
  1331. static ssize_t
  1332. show_fc_host_active_fc4s (struct device *dev,
  1333. struct device_attribute *attr, char *buf)
  1334. {
  1335. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1336. struct fc_internal *i = to_fc_internal(shost->transportt);
  1337. if (i->f->get_host_active_fc4s)
  1338. i->f->get_host_active_fc4s(shost);
  1339. return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
  1340. }
  1341. static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
  1342. show_fc_host_active_fc4s, NULL);
  1343. static ssize_t
  1344. show_fc_host_speed (struct device *dev,
  1345. struct device_attribute *attr, char *buf)
  1346. {
  1347. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1348. struct fc_internal *i = to_fc_internal(shost->transportt);
  1349. if (i->f->get_host_speed)
  1350. i->f->get_host_speed(shost);
  1351. if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
  1352. return snprintf(buf, 20, "unknown\n");
  1353. return get_fc_port_speed_names(fc_host_speed(shost), buf);
  1354. }
  1355. static FC_DEVICE_ATTR(host, speed, S_IRUGO,
  1356. show_fc_host_speed, NULL);
  1357. fc_host_rd_attr(port_id, "0x%06x\n", 20);
  1358. fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
  1359. fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  1360. fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
  1361. fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
  1362. fc_private_host_show_function(system_hostname, "%s\n",
  1363. FC_SYMBOLIC_NAME_SIZE + 1, )
  1364. fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
  1365. static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
  1366. show_fc_host_system_hostname, store_fc_host_system_hostname);
  1367. /* Private Host Attributes */
  1368. static ssize_t
  1369. show_fc_private_host_tgtid_bind_type(struct device *dev,
  1370. struct device_attribute *attr, char *buf)
  1371. {
  1372. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1373. const char *name;
  1374. name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
  1375. if (!name)
  1376. return -EINVAL;
  1377. return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
  1378. }
  1379. #define get_list_head_entry(pos, head, member) \
  1380. pos = list_entry((head)->next, typeof(*pos), member)
  1381. static ssize_t
  1382. store_fc_private_host_tgtid_bind_type(struct device *dev,
  1383. struct device_attribute *attr, const char *buf, size_t count)
  1384. {
  1385. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1386. struct fc_rport *rport;
  1387. enum fc_tgtid_binding_type val;
  1388. unsigned long flags;
  1389. if (get_fc_tgtid_bind_type_match(buf, &val))
  1390. return -EINVAL;
  1391. /* if changing bind type, purge all unused consistent bindings */
  1392. if (val != fc_host_tgtid_bind_type(shost)) {
  1393. spin_lock_irqsave(shost->host_lock, flags);
  1394. while (!list_empty(&fc_host_rport_bindings(shost))) {
  1395. get_list_head_entry(rport,
  1396. &fc_host_rport_bindings(shost), peers);
  1397. list_del(&rport->peers);
  1398. rport->port_state = FC_PORTSTATE_DELETED;
  1399. fc_queue_work(shost, &rport->rport_delete_work);
  1400. }
  1401. spin_unlock_irqrestore(shost->host_lock, flags);
  1402. }
  1403. fc_host_tgtid_bind_type(shost) = val;
  1404. return count;
  1405. }
  1406. static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
  1407. show_fc_private_host_tgtid_bind_type,
  1408. store_fc_private_host_tgtid_bind_type);
  1409. static ssize_t
  1410. store_fc_private_host_issue_lip(struct device *dev,
  1411. struct device_attribute *attr, const char *buf, size_t count)
  1412. {
  1413. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1414. struct fc_internal *i = to_fc_internal(shost->transportt);
  1415. int ret;
  1416. /* ignore any data value written to the attribute */
  1417. if (i->f->issue_fc_host_lip) {
  1418. ret = i->f->issue_fc_host_lip(shost);
  1419. return ret ? ret: count;
  1420. }
  1421. return -ENOENT;
  1422. }
  1423. static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
  1424. store_fc_private_host_issue_lip);
  1425. static ssize_t
  1426. store_fc_private_host_dev_loss_tmo(struct device *dev,
  1427. struct device_attribute *attr,
  1428. const char *buf, size_t count)
  1429. {
  1430. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1431. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1432. struct fc_rport *rport;
  1433. unsigned long val, flags;
  1434. int rc;
  1435. rc = fc_str_to_dev_loss(buf, &val);
  1436. if (rc)
  1437. return rc;
  1438. fc_host_dev_loss_tmo(shost) = val;
  1439. spin_lock_irqsave(shost->host_lock, flags);
  1440. list_for_each_entry(rport, &fc_host->rports, peers)
  1441. fc_rport_set_dev_loss_tmo(rport, val);
  1442. spin_unlock_irqrestore(shost->host_lock, flags);
  1443. return count;
  1444. }
  1445. fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
  1446. static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
  1447. show_fc_host_dev_loss_tmo,
  1448. store_fc_private_host_dev_loss_tmo);
  1449. fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
  1450. /*
  1451. * Host Statistics Management
  1452. */
  1453. /* Show a given an attribute in the statistics group */
  1454. static ssize_t
  1455. fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
  1456. {
  1457. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1458. struct fc_internal *i = to_fc_internal(shost->transportt);
  1459. struct fc_host_statistics *stats;
  1460. ssize_t ret = -ENOENT;
  1461. if (offset > sizeof(struct fc_host_statistics) ||
  1462. offset % sizeof(u64) != 0)
  1463. WARN_ON(1);
  1464. if (i->f->get_fc_host_stats) {
  1465. stats = (i->f->get_fc_host_stats)(shost);
  1466. if (stats)
  1467. ret = snprintf(buf, 20, "0x%llx\n",
  1468. (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
  1469. }
  1470. return ret;
  1471. }
  1472. /* generate a read-only statistics attribute */
  1473. #define fc_host_statistic(name) \
  1474. static ssize_t show_fcstat_##name(struct device *cd, \
  1475. struct device_attribute *attr, \
  1476. char *buf) \
  1477. { \
  1478. return fc_stat_show(cd, buf, \
  1479. offsetof(struct fc_host_statistics, name)); \
  1480. } \
  1481. static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
  1482. fc_host_statistic(seconds_since_last_reset);
  1483. fc_host_statistic(tx_frames);
  1484. fc_host_statistic(tx_words);
  1485. fc_host_statistic(rx_frames);
  1486. fc_host_statistic(rx_words);
  1487. fc_host_statistic(lip_count);
  1488. fc_host_statistic(nos_count);
  1489. fc_host_statistic(error_frames);
  1490. fc_host_statistic(dumped_frames);
  1491. fc_host_statistic(link_failure_count);
  1492. fc_host_statistic(loss_of_sync_count);
  1493. fc_host_statistic(loss_of_signal_count);
  1494. fc_host_statistic(prim_seq_protocol_err_count);
  1495. fc_host_statistic(invalid_tx_word_count);
  1496. fc_host_statistic(invalid_crc_count);
  1497. fc_host_statistic(fcp_input_requests);
  1498. fc_host_statistic(fcp_output_requests);
  1499. fc_host_statistic(fcp_control_requests);
  1500. fc_host_statistic(fcp_input_megabytes);
  1501. fc_host_statistic(fcp_output_megabytes);
  1502. static ssize_t
  1503. fc_reset_statistics(struct device *dev, struct device_attribute *attr,
  1504. const char *buf, size_t count)
  1505. {
  1506. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1507. struct fc_internal *i = to_fc_internal(shost->transportt);
  1508. /* ignore any data value written to the attribute */
  1509. if (i->f->reset_fc_host_stats) {
  1510. i->f->reset_fc_host_stats(shost);
  1511. return count;
  1512. }
  1513. return -ENOENT;
  1514. }
  1515. static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
  1516. fc_reset_statistics);
  1517. static struct attribute *fc_statistics_attrs[] = {
  1518. &device_attr_host_seconds_since_last_reset.attr,
  1519. &device_attr_host_tx_frames.attr,
  1520. &device_attr_host_tx_words.attr,
  1521. &device_attr_host_rx_frames.attr,
  1522. &device_attr_host_rx_words.attr,
  1523. &device_attr_host_lip_count.attr,
  1524. &device_attr_host_nos_count.attr,
  1525. &device_attr_host_error_frames.attr,
  1526. &device_attr_host_dumped_frames.attr,
  1527. &device_attr_host_link_failure_count.attr,
  1528. &device_attr_host_loss_of_sync_count.attr,
  1529. &device_attr_host_loss_of_signal_count.attr,
  1530. &device_attr_host_prim_seq_protocol_err_count.attr,
  1531. &device_attr_host_invalid_tx_word_count.attr,
  1532. &device_attr_host_invalid_crc_count.attr,
  1533. &device_attr_host_fcp_input_requests.attr,
  1534. &device_attr_host_fcp_output_requests.attr,
  1535. &device_attr_host_fcp_control_requests.attr,
  1536. &device_attr_host_fcp_input_megabytes.attr,
  1537. &device_attr_host_fcp_output_megabytes.attr,
  1538. &device_attr_host_reset_statistics.attr,
  1539. NULL
  1540. };
  1541. static struct attribute_group fc_statistics_group = {
  1542. .name = "statistics",
  1543. .attrs = fc_statistics_attrs,
  1544. };
  1545. /* Host Vport Attributes */
  1546. static int
  1547. fc_parse_wwn(const char *ns, u64 *nm)
  1548. {
  1549. unsigned int i, j;
  1550. u8 wwn[8];
  1551. memset(wwn, 0, sizeof(wwn));
  1552. /* Validate and store the new name */
  1553. for (i=0, j=0; i < 16; i++) {
  1554. int value;
  1555. value = hex_to_bin(*ns++);
  1556. if (value >= 0)
  1557. j = (j << 4) | value;
  1558. else
  1559. return -EINVAL;
  1560. if (i % 2) {
  1561. wwn[i/2] = j & 0xff;
  1562. j = 0;
  1563. }
  1564. }
  1565. *nm = wwn_to_u64(wwn);
  1566. return 0;
  1567. }
  1568. /*
  1569. * "Short-cut" sysfs variable to create a new vport on a FC Host.
  1570. * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
  1571. * will default to a NPIV-based FCP_Initiator; The WWNs are specified
  1572. * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
  1573. */
  1574. static ssize_t
  1575. store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
  1576. const char *buf, size_t count)
  1577. {
  1578. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1579. struct fc_vport_identifiers vid;
  1580. struct fc_vport *vport;
  1581. unsigned int cnt=count;
  1582. int stat;
  1583. memset(&vid, 0, sizeof(vid));
  1584. /* count may include a LF at end of string */
  1585. if (buf[cnt-1] == '\n')
  1586. cnt--;
  1587. /* validate we have enough characters for WWPN */
  1588. if ((cnt != (16+1+16)) || (buf[16] != ':'))
  1589. return -EINVAL;
  1590. stat = fc_parse_wwn(&buf[0], &vid.port_name);
  1591. if (stat)
  1592. return stat;
  1593. stat = fc_parse_wwn(&buf[17], &vid.node_name);
  1594. if (stat)
  1595. return stat;
  1596. vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
  1597. vid.vport_type = FC_PORTTYPE_NPIV;
  1598. /* vid.symbolic_name is already zero/NULL's */
  1599. vid.disable = false; /* always enabled */
  1600. /* we only allow support on Channel 0 !!! */
  1601. stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
  1602. return stat ? stat : count;
  1603. }
  1604. static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
  1605. store_fc_host_vport_create);
  1606. /*
  1607. * "Short-cut" sysfs variable to delete a vport on a FC Host.
  1608. * Vport is identified by a string containing "<WWPN>:<WWNN>".
  1609. * The WWNs are specified as hex characters, and may *not* contain
  1610. * any prefixes (e.g. 0x, x, etc)
  1611. */
  1612. static ssize_t
  1613. store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
  1614. const char *buf, size_t count)
  1615. {
  1616. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1617. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1618. struct fc_vport *vport;
  1619. u64 wwpn, wwnn;
  1620. unsigned long flags;
  1621. unsigned int cnt=count;
  1622. int stat, match;
  1623. /* count may include a LF at end of string */
  1624. if (buf[cnt-1] == '\n')
  1625. cnt--;
  1626. /* validate we have enough characters for WWPN */
  1627. if ((cnt != (16+1+16)) || (buf[16] != ':'))
  1628. return -EINVAL;
  1629. stat = fc_parse_wwn(&buf[0], &wwpn);
  1630. if (stat)
  1631. return stat;
  1632. stat = fc_parse_wwn(&buf[17], &wwnn);
  1633. if (stat)
  1634. return stat;
  1635. spin_lock_irqsave(shost->host_lock, flags);
  1636. match = 0;
  1637. /* we only allow support on Channel 0 !!! */
  1638. list_for_each_entry(vport, &fc_host->vports, peers) {
  1639. if ((vport->channel == 0) &&
  1640. (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
  1641. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
  1642. break;
  1643. vport->flags |= FC_VPORT_DELETING;
  1644. match = 1;
  1645. break;
  1646. }
  1647. }
  1648. spin_unlock_irqrestore(shost->host_lock, flags);
  1649. if (!match)
  1650. return -ENODEV;
  1651. stat = fc_vport_terminate(vport);
  1652. return stat ? stat : count;
  1653. }
  1654. static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
  1655. store_fc_host_vport_delete);
  1656. static int fc_host_match(struct attribute_container *cont,
  1657. struct device *dev)
  1658. {
  1659. struct Scsi_Host *shost;
  1660. struct fc_internal *i;
  1661. if (!scsi_is_host_device(dev))
  1662. return 0;
  1663. shost = dev_to_shost(dev);
  1664. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1665. != &fc_host_class.class)
  1666. return 0;
  1667. i = to_fc_internal(shost->transportt);
  1668. return &i->t.host_attrs.ac == cont;
  1669. }
  1670. static int fc_target_match(struct attribute_container *cont,
  1671. struct device *dev)
  1672. {
  1673. struct Scsi_Host *shost;
  1674. struct fc_internal *i;
  1675. if (!scsi_is_target_device(dev))
  1676. return 0;
  1677. shost = dev_to_shost(dev->parent);
  1678. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1679. != &fc_host_class.class)
  1680. return 0;
  1681. i = to_fc_internal(shost->transportt);
  1682. return &i->t.target_attrs.ac == cont;
  1683. }
  1684. static void fc_rport_dev_release(struct device *dev)
  1685. {
  1686. struct fc_rport *rport = dev_to_rport(dev);
  1687. put_device(dev->parent);
  1688. kfree(rport);
  1689. }
  1690. int scsi_is_fc_rport(const struct device *dev)
  1691. {
  1692. return dev->release == fc_rport_dev_release;
  1693. }
  1694. EXPORT_SYMBOL(scsi_is_fc_rport);
  1695. static int fc_rport_match(struct attribute_container *cont,
  1696. struct device *dev)
  1697. {
  1698. struct Scsi_Host *shost;
  1699. struct fc_internal *i;
  1700. if (!scsi_is_fc_rport(dev))
  1701. return 0;
  1702. shost = dev_to_shost(dev->parent);
  1703. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1704. != &fc_host_class.class)
  1705. return 0;
  1706. i = to_fc_internal(shost->transportt);
  1707. return &i->rport_attr_cont.ac == cont;
  1708. }
  1709. static void fc_vport_dev_release(struct device *dev)
  1710. {
  1711. struct fc_vport *vport = dev_to_vport(dev);
  1712. put_device(dev->parent); /* release kobj parent */
  1713. kfree(vport);
  1714. }
  1715. int scsi_is_fc_vport(const struct device *dev)
  1716. {
  1717. return dev->release == fc_vport_dev_release;
  1718. }
  1719. EXPORT_SYMBOL(scsi_is_fc_vport);
  1720. static int fc_vport_match(struct attribute_container *cont,
  1721. struct device *dev)
  1722. {
  1723. struct fc_vport *vport;
  1724. struct Scsi_Host *shost;
  1725. struct fc_internal *i;
  1726. if (!scsi_is_fc_vport(dev))
  1727. return 0;
  1728. vport = dev_to_vport(dev);
  1729. shost = vport_to_shost(vport);
  1730. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1731. != &fc_host_class.class)
  1732. return 0;
  1733. i = to_fc_internal(shost->transportt);
  1734. return &i->vport_attr_cont.ac == cont;
  1735. }
  1736. /**
  1737. * fc_timed_out - FC Transport I/O timeout intercept handler
  1738. * @scmd: The SCSI command which timed out
  1739. *
  1740. * This routine protects against error handlers getting invoked while a
  1741. * rport is in a blocked state, typically due to a temporarily loss of
  1742. * connectivity. If the error handlers are allowed to proceed, requests
  1743. * to abort i/o, reset the target, etc will likely fail as there is no way
  1744. * to communicate with the device to perform the requested function. These
  1745. * failures may result in the midlayer taking the device offline, requiring
  1746. * manual intervention to restore operation.
  1747. *
  1748. * This routine, called whenever an i/o times out, validates the state of
  1749. * the underlying rport. If the rport is blocked, it returns
  1750. * EH_RESET_TIMER, which will continue to reschedule the timeout.
  1751. * Eventually, either the device will return, or devloss_tmo will fire,
  1752. * and when the timeout then fires, it will be handled normally.
  1753. * If the rport is not blocked, normal error handling continues.
  1754. *
  1755. * Notes:
  1756. * This routine assumes no locks are held on entry.
  1757. */
  1758. static enum blk_eh_timer_return
  1759. fc_timed_out(struct scsi_cmnd *scmd)
  1760. {
  1761. struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
  1762. if (rport->port_state == FC_PORTSTATE_BLOCKED)
  1763. return BLK_EH_RESET_TIMER;
  1764. return BLK_EH_NOT_HANDLED;
  1765. }
  1766. /*
  1767. * Called by fc_user_scan to locate an rport on the shost that
  1768. * matches the channel and target id, and invoke scsi_scan_target()
  1769. * on the rport.
  1770. */
  1771. static void
  1772. fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, uint lun)
  1773. {
  1774. struct fc_rport *rport;
  1775. unsigned long flags;
  1776. spin_lock_irqsave(shost->host_lock, flags);
  1777. list_for_each_entry(rport, &fc_host_rports(shost), peers) {
  1778. if (rport->scsi_target_id == -1)
  1779. continue;
  1780. if (rport->port_state != FC_PORTSTATE_ONLINE)
  1781. continue;
  1782. if ((channel == rport->channel) &&
  1783. (id == rport->scsi_target_id)) {
  1784. spin_unlock_irqrestore(shost->host_lock, flags);
  1785. scsi_scan_target(&rport->dev, channel, id, lun, 1);
  1786. return;
  1787. }
  1788. }
  1789. spin_unlock_irqrestore(shost->host_lock, flags);
  1790. }
  1791. /*
  1792. * Called via sysfs scan routines. Necessary, as the FC transport
  1793. * wants to place all target objects below the rport object. So this
  1794. * routine must invoke the scsi_scan_target() routine with the rport
  1795. * object as the parent.
  1796. */
  1797. static int
  1798. fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, uint lun)
  1799. {
  1800. uint chlo, chhi;
  1801. uint tgtlo, tgthi;
  1802. if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
  1803. ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
  1804. ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
  1805. return -EINVAL;
  1806. if (channel == SCAN_WILD_CARD) {
  1807. chlo = 0;
  1808. chhi = shost->max_channel + 1;
  1809. } else {
  1810. chlo = channel;
  1811. chhi = channel + 1;
  1812. }
  1813. if (id == SCAN_WILD_CARD) {
  1814. tgtlo = 0;
  1815. tgthi = shost->max_id;
  1816. } else {
  1817. tgtlo = id;
  1818. tgthi = id + 1;
  1819. }
  1820. for ( ; chlo < chhi; chlo++)
  1821. for ( ; tgtlo < tgthi; tgtlo++)
  1822. fc_user_scan_tgt(shost, chlo, tgtlo, lun);
  1823. return 0;
  1824. }
  1825. static int fc_tsk_mgmt_response(struct Scsi_Host *shost, u64 nexus, u64 tm_id,
  1826. int result)
  1827. {
  1828. struct fc_internal *i = to_fc_internal(shost->transportt);
  1829. return i->f->tsk_mgmt_response(shost, nexus, tm_id, result);
  1830. }
  1831. static int fc_it_nexus_response(struct Scsi_Host *shost, u64 nexus, int result)
  1832. {
  1833. struct fc_internal *i = to_fc_internal(shost->transportt);
  1834. return i->f->it_nexus_response(shost, nexus, result);
  1835. }
  1836. struct scsi_transport_template *
  1837. fc_attach_transport(struct fc_function_template *ft)
  1838. {
  1839. int count;
  1840. struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
  1841. GFP_KERNEL);
  1842. if (unlikely(!i))
  1843. return NULL;
  1844. i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
  1845. i->t.target_attrs.ac.class = &fc_transport_class.class;
  1846. i->t.target_attrs.ac.match = fc_target_match;
  1847. i->t.target_size = sizeof(struct fc_starget_attrs);
  1848. transport_container_register(&i->t.target_attrs);
  1849. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  1850. i->t.host_attrs.ac.class = &fc_host_class.class;
  1851. i->t.host_attrs.ac.match = fc_host_match;
  1852. i->t.host_size = sizeof(struct fc_host_attrs);
  1853. if (ft->get_fc_host_stats)
  1854. i->t.host_attrs.statistics = &fc_statistics_group;
  1855. transport_container_register(&i->t.host_attrs);
  1856. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  1857. i->rport_attr_cont.ac.class = &fc_rport_class.class;
  1858. i->rport_attr_cont.ac.match = fc_rport_match;
  1859. transport_container_register(&i->rport_attr_cont);
  1860. i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
  1861. i->vport_attr_cont.ac.class = &fc_vport_class.class;
  1862. i->vport_attr_cont.ac.match = fc_vport_match;
  1863. transport_container_register(&i->vport_attr_cont);
  1864. i->f = ft;
  1865. /* Transport uses the shost workq for scsi scanning */
  1866. i->t.create_work_queue = 1;
  1867. i->t.eh_timed_out = fc_timed_out;
  1868. i->t.user_scan = fc_user_scan;
  1869. /* target-mode drivers' functions */
  1870. i->t.tsk_mgmt_response = fc_tsk_mgmt_response;
  1871. i->t.it_nexus_response = fc_it_nexus_response;
  1872. /*
  1873. * Setup SCSI Target Attributes.
  1874. */
  1875. count = 0;
  1876. SETUP_STARGET_ATTRIBUTE_RD(node_name);
  1877. SETUP_STARGET_ATTRIBUTE_RD(port_name);
  1878. SETUP_STARGET_ATTRIBUTE_RD(port_id);
  1879. BUG_ON(count > FC_STARGET_NUM_ATTRS);
  1880. i->starget_attrs[count] = NULL;
  1881. /*
  1882. * Setup SCSI Host Attributes.
  1883. */
  1884. count=0;
  1885. SETUP_HOST_ATTRIBUTE_RD(node_name);
  1886. SETUP_HOST_ATTRIBUTE_RD(port_name);
  1887. SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
  1888. SETUP_HOST_ATTRIBUTE_RD(supported_classes);
  1889. SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
  1890. SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
  1891. SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
  1892. if (ft->vport_create) {
  1893. SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
  1894. SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
  1895. }
  1896. SETUP_HOST_ATTRIBUTE_RD(serial_number);
  1897. SETUP_HOST_ATTRIBUTE_RD(manufacturer);
  1898. SETUP_HOST_ATTRIBUTE_RD(model);
  1899. SETUP_HOST_ATTRIBUTE_RD(model_description);
  1900. SETUP_HOST_ATTRIBUTE_RD(hardware_version);
  1901. SETUP_HOST_ATTRIBUTE_RD(driver_version);
  1902. SETUP_HOST_ATTRIBUTE_RD(firmware_version);
  1903. SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
  1904. SETUP_HOST_ATTRIBUTE_RD(port_id);
  1905. SETUP_HOST_ATTRIBUTE_RD(port_type);
  1906. SETUP_HOST_ATTRIBUTE_RD(port_state);
  1907. SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
  1908. SETUP_HOST_ATTRIBUTE_RD(speed);
  1909. SETUP_HOST_ATTRIBUTE_RD(fabric_name);
  1910. SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
  1911. SETUP_HOST_ATTRIBUTE_RW(system_hostname);
  1912. /* Transport-managed attributes */
  1913. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
  1914. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
  1915. if (ft->issue_fc_host_lip)
  1916. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
  1917. if (ft->vport_create)
  1918. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
  1919. if (ft->vport_delete)
  1920. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
  1921. BUG_ON(count > FC_HOST_NUM_ATTRS);
  1922. i->host_attrs[count] = NULL;
  1923. /*
  1924. * Setup Remote Port Attributes.
  1925. */
  1926. count=0;
  1927. SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
  1928. SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
  1929. SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
  1930. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
  1931. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
  1932. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
  1933. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
  1934. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
  1935. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
  1936. SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
  1937. BUG_ON(count > FC_RPORT_NUM_ATTRS);
  1938. i->rport_attrs[count] = NULL;
  1939. /*
  1940. * Setup Virtual Port Attributes.
  1941. */
  1942. count=0;
  1943. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
  1944. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
  1945. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
  1946. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
  1947. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
  1948. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
  1949. SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
  1950. SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
  1951. SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
  1952. BUG_ON(count > FC_VPORT_NUM_ATTRS);
  1953. i->vport_attrs[count] = NULL;
  1954. return &i->t;
  1955. }
  1956. EXPORT_SYMBOL(fc_attach_transport);
  1957. void fc_release_transport(struct scsi_transport_template *t)
  1958. {
  1959. struct fc_internal *i = to_fc_internal(t);
  1960. transport_container_unregister(&i->t.target_attrs);
  1961. transport_container_unregister(&i->t.host_attrs);
  1962. transport_container_unregister(&i->rport_attr_cont);
  1963. transport_container_unregister(&i->vport_attr_cont);
  1964. kfree(i);
  1965. }
  1966. EXPORT_SYMBOL(fc_release_transport);
  1967. /**
  1968. * fc_queue_work - Queue work to the fc_host workqueue.
  1969. * @shost: Pointer to Scsi_Host bound to fc_host.
  1970. * @work: Work to queue for execution.
  1971. *
  1972. * Return value:
  1973. * 1 - work queued for execution
  1974. * 0 - work is already queued
  1975. * -EINVAL - work queue doesn't exist
  1976. */
  1977. static int
  1978. fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
  1979. {
  1980. if (unlikely(!fc_host_work_q(shost))) {
  1981. printk(KERN_ERR
  1982. "ERROR: FC host '%s' attempted to queue work, "
  1983. "when no workqueue created.\n", shost->hostt->name);
  1984. dump_stack();
  1985. return -EINVAL;
  1986. }
  1987. return queue_work(fc_host_work_q(shost), work);
  1988. }
  1989. /**
  1990. * fc_flush_work - Flush a fc_host's workqueue.
  1991. * @shost: Pointer to Scsi_Host bound to fc_host.
  1992. */
  1993. static void
  1994. fc_flush_work(struct Scsi_Host *shost)
  1995. {
  1996. if (!fc_host_work_q(shost)) {
  1997. printk(KERN_ERR
  1998. "ERROR: FC host '%s' attempted to flush work, "
  1999. "when no workqueue created.\n", shost->hostt->name);
  2000. dump_stack();
  2001. return;
  2002. }
  2003. flush_workqueue(fc_host_work_q(shost));
  2004. }
  2005. /**
  2006. * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
  2007. * @shost: Pointer to Scsi_Host bound to fc_host.
  2008. * @work: Work to queue for execution.
  2009. * @delay: jiffies to delay the work queuing
  2010. *
  2011. * Return value:
  2012. * 1 on success / 0 already queued / < 0 for error
  2013. */
  2014. static int
  2015. fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
  2016. unsigned long delay)
  2017. {
  2018. if (unlikely(!fc_host_devloss_work_q(shost))) {
  2019. printk(KERN_ERR
  2020. "ERROR: FC host '%s' attempted to queue work, "
  2021. "when no workqueue created.\n", shost->hostt->name);
  2022. dump_stack();
  2023. return -EINVAL;
  2024. }
  2025. return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
  2026. }
  2027. /**
  2028. * fc_flush_devloss - Flush a fc_host's devloss workqueue.
  2029. * @shost: Pointer to Scsi_Host bound to fc_host.
  2030. */
  2031. static void
  2032. fc_flush_devloss(struct Scsi_Host *shost)
  2033. {
  2034. if (!fc_host_devloss_work_q(shost)) {
  2035. printk(KERN_ERR
  2036. "ERROR: FC host '%s' attempted to flush work, "
  2037. "when no workqueue created.\n", shost->hostt->name);
  2038. dump_stack();
  2039. return;
  2040. }
  2041. flush_workqueue(fc_host_devloss_work_q(shost));
  2042. }
  2043. /**
  2044. * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
  2045. * @shost: Which &Scsi_Host
  2046. *
  2047. * This routine is expected to be called immediately preceding the
  2048. * a driver's call to scsi_remove_host().
  2049. *
  2050. * WARNING: A driver utilizing the fc_transport, which fails to call
  2051. * this routine prior to scsi_remove_host(), will leave dangling
  2052. * objects in /sys/class/fc_remote_ports. Access to any of these
  2053. * objects can result in a system crash !!!
  2054. *
  2055. * Notes:
  2056. * This routine assumes no locks are held on entry.
  2057. */
  2058. void
  2059. fc_remove_host(struct Scsi_Host *shost)
  2060. {
  2061. struct fc_vport *vport = NULL, *next_vport = NULL;
  2062. struct fc_rport *rport = NULL, *next_rport = NULL;
  2063. struct workqueue_struct *work_q;
  2064. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2065. unsigned long flags;
  2066. spin_lock_irqsave(shost->host_lock, flags);
  2067. /* Remove any vports */
  2068. list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
  2069. fc_queue_work(shost, &vport->vport_delete_work);
  2070. /* Remove any remote ports */
  2071. list_for_each_entry_safe(rport, next_rport,
  2072. &fc_host->rports, peers) {
  2073. list_del(&rport->peers);
  2074. rport->port_state = FC_PORTSTATE_DELETED;
  2075. fc_queue_work(shost, &rport->rport_delete_work);
  2076. }
  2077. list_for_each_entry_safe(rport, next_rport,
  2078. &fc_host->rport_bindings, peers) {
  2079. list_del(&rport->peers);
  2080. rport->port_state = FC_PORTSTATE_DELETED;
  2081. fc_queue_work(shost, &rport->rport_delete_work);
  2082. }
  2083. spin_unlock_irqrestore(shost->host_lock, flags);
  2084. /* flush all scan work items */
  2085. scsi_flush_work(shost);
  2086. /* flush all stgt delete, and rport delete work items, then kill it */
  2087. if (fc_host->work_q) {
  2088. work_q = fc_host->work_q;
  2089. fc_host->work_q = NULL;
  2090. destroy_workqueue(work_q);
  2091. }
  2092. /* flush all devloss work items, then kill it */
  2093. if (fc_host->devloss_work_q) {
  2094. work_q = fc_host->devloss_work_q;
  2095. fc_host->devloss_work_q = NULL;
  2096. destroy_workqueue(work_q);
  2097. }
  2098. }
  2099. EXPORT_SYMBOL(fc_remove_host);
  2100. static void fc_terminate_rport_io(struct fc_rport *rport)
  2101. {
  2102. struct Scsi_Host *shost = rport_to_shost(rport);
  2103. struct fc_internal *i = to_fc_internal(shost->transportt);
  2104. /* Involve the LLDD if possible to terminate all io on the rport. */
  2105. if (i->f->terminate_rport_io)
  2106. i->f->terminate_rport_io(rport);
  2107. /*
  2108. * must unblock to flush queued IO. The caller will have set
  2109. * the port_state or flags, so that fc_remote_port_chkready will
  2110. * fail IO.
  2111. */
  2112. scsi_target_unblock(&rport->dev);
  2113. }
  2114. /**
  2115. * fc_starget_delete - called to delete the scsi descendants of an rport
  2116. * @work: remote port to be operated on.
  2117. *
  2118. * Deletes target and all sdevs.
  2119. */
  2120. static void
  2121. fc_starget_delete(struct work_struct *work)
  2122. {
  2123. struct fc_rport *rport =
  2124. container_of(work, struct fc_rport, stgt_delete_work);
  2125. fc_terminate_rport_io(rport);
  2126. scsi_remove_target(&rport->dev);
  2127. }
  2128. /**
  2129. * fc_rport_final_delete - finish rport termination and delete it.
  2130. * @work: remote port to be deleted.
  2131. */
  2132. static void
  2133. fc_rport_final_delete(struct work_struct *work)
  2134. {
  2135. struct fc_rport *rport =
  2136. container_of(work, struct fc_rport, rport_delete_work);
  2137. struct device *dev = &rport->dev;
  2138. struct Scsi_Host *shost = rport_to_shost(rport);
  2139. struct fc_internal *i = to_fc_internal(shost->transportt);
  2140. unsigned long flags;
  2141. int do_callback = 0;
  2142. fc_terminate_rport_io(rport);
  2143. /*
  2144. * if a scan is pending, flush the SCSI Host work_q so that
  2145. * that we can reclaim the rport scan work element.
  2146. */
  2147. if (rport->flags & FC_RPORT_SCAN_PENDING)
  2148. scsi_flush_work(shost);
  2149. /*
  2150. * Cancel any outstanding timers. These should really exist
  2151. * only when rmmod'ing the LLDD and we're asking for
  2152. * immediate termination of the rports
  2153. */
  2154. spin_lock_irqsave(shost->host_lock, flags);
  2155. if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
  2156. spin_unlock_irqrestore(shost->host_lock, flags);
  2157. if (!cancel_delayed_work(&rport->fail_io_work))
  2158. fc_flush_devloss(shost);
  2159. if (!cancel_delayed_work(&rport->dev_loss_work))
  2160. fc_flush_devloss(shost);
  2161. spin_lock_irqsave(shost->host_lock, flags);
  2162. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  2163. }
  2164. spin_unlock_irqrestore(shost->host_lock, flags);
  2165. /* Delete SCSI target and sdevs */
  2166. if (rport->scsi_target_id != -1)
  2167. fc_starget_delete(&rport->stgt_delete_work);
  2168. /*
  2169. * Notify the driver that the rport is now dead. The LLDD will
  2170. * also guarantee that any communication to the rport is terminated
  2171. *
  2172. * Avoid this call if we already called it when we preserved the
  2173. * rport for the binding.
  2174. */
  2175. spin_lock_irqsave(shost->host_lock, flags);
  2176. if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
  2177. (i->f->dev_loss_tmo_callbk)) {
  2178. rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
  2179. do_callback = 1;
  2180. }
  2181. spin_unlock_irqrestore(shost->host_lock, flags);
  2182. if (do_callback)
  2183. i->f->dev_loss_tmo_callbk(rport);
  2184. fc_bsg_remove(rport->rqst_q);
  2185. transport_remove_device(dev);
  2186. device_del(dev);
  2187. transport_destroy_device(dev);
  2188. put_device(&shost->shost_gendev); /* for fc_host->rport list */
  2189. put_device(dev); /* for self-reference */
  2190. }
  2191. /**
  2192. * fc_rport_create - allocates and creates a remote FC port.
  2193. * @shost: scsi host the remote port is connected to.
  2194. * @channel: Channel on shost port connected to.
  2195. * @ids: The world wide names, fc address, and FC4 port
  2196. * roles for the remote port.
  2197. *
  2198. * Allocates and creates the remoter port structure, including the
  2199. * class and sysfs creation.
  2200. *
  2201. * Notes:
  2202. * This routine assumes no locks are held on entry.
  2203. */
  2204. static struct fc_rport *
  2205. fc_rport_create(struct Scsi_Host *shost, int channel,
  2206. struct fc_rport_identifiers *ids)
  2207. {
  2208. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2209. struct fc_internal *fci = to_fc_internal(shost->transportt);
  2210. struct fc_rport *rport;
  2211. struct device *dev;
  2212. unsigned long flags;
  2213. int error;
  2214. size_t size;
  2215. size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
  2216. rport = kzalloc(size, GFP_KERNEL);
  2217. if (unlikely(!rport)) {
  2218. printk(KERN_ERR "%s: allocation failure\n", __func__);
  2219. return NULL;
  2220. }
  2221. rport->maxframe_size = -1;
  2222. rport->supported_classes = FC_COS_UNSPECIFIED;
  2223. rport->dev_loss_tmo = fc_host->dev_loss_tmo;
  2224. memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
  2225. memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
  2226. rport->port_id = ids->port_id;
  2227. rport->roles = ids->roles;
  2228. rport->port_state = FC_PORTSTATE_ONLINE;
  2229. if (fci->f->dd_fcrport_size)
  2230. rport->dd_data = &rport[1];
  2231. rport->channel = channel;
  2232. rport->fast_io_fail_tmo = -1;
  2233. INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
  2234. INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
  2235. INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
  2236. INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
  2237. INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
  2238. spin_lock_irqsave(shost->host_lock, flags);
  2239. rport->number = fc_host->next_rport_number++;
  2240. if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
  2241. rport->scsi_target_id = fc_host->next_target_id++;
  2242. else
  2243. rport->scsi_target_id = -1;
  2244. list_add_tail(&rport->peers, &fc_host->rports);
  2245. get_device(&shost->shost_gendev); /* for fc_host->rport list */
  2246. spin_unlock_irqrestore(shost->host_lock, flags);
  2247. dev = &rport->dev;
  2248. device_initialize(dev); /* takes self reference */
  2249. dev->parent = get_device(&shost->shost_gendev); /* parent reference */
  2250. dev->release = fc_rport_dev_release;
  2251. dev_set_name(dev, "rport-%d:%d-%d",
  2252. shost->host_no, channel, rport->number);
  2253. transport_setup_device(dev);
  2254. error = device_add(dev);
  2255. if (error) {
  2256. printk(KERN_ERR "FC Remote Port device_add failed\n");
  2257. goto delete_rport;
  2258. }
  2259. transport_add_device(dev);
  2260. transport_configure_device(dev);
  2261. fc_bsg_rportadd(shost, rport);
  2262. /* ignore any bsg add error - we just can't do sgio */
  2263. if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
  2264. /* initiate a scan of the target */
  2265. rport->flags |= FC_RPORT_SCAN_PENDING;
  2266. scsi_queue_work(shost, &rport->scan_work);
  2267. }
  2268. return rport;
  2269. delete_rport:
  2270. transport_destroy_device(dev);
  2271. spin_lock_irqsave(shost->host_lock, flags);
  2272. list_del(&rport->peers);
  2273. put_device(&shost->shost_gendev); /* for fc_host->rport list */
  2274. spin_unlock_irqrestore(shost->host_lock, flags);
  2275. put_device(dev->parent);
  2276. kfree(rport);
  2277. return NULL;
  2278. }
  2279. /**
  2280. * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
  2281. * @shost: scsi host the remote port is connected to.
  2282. * @channel: Channel on shost port connected to.
  2283. * @ids: The world wide names, fc address, and FC4 port
  2284. * roles for the remote port.
  2285. *
  2286. * The LLDD calls this routine to notify the transport of the existence
  2287. * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
  2288. * of the port, it's FC address (port_id), and the FC4 roles that are
  2289. * active for the port.
  2290. *
  2291. * For ports that are FCP targets (aka scsi targets), the FC transport
  2292. * maintains consistent target id bindings on behalf of the LLDD.
  2293. * A consistent target id binding is an assignment of a target id to
  2294. * a remote port identifier, which persists while the scsi host is
  2295. * attached. The remote port can disappear, then later reappear, and
  2296. * it's target id assignment remains the same. This allows for shifts
  2297. * in FC addressing (if binding by wwpn or wwnn) with no apparent
  2298. * changes to the scsi subsystem which is based on scsi host number and
  2299. * target id values. Bindings are only valid during the attachment of
  2300. * the scsi host. If the host detaches, then later re-attaches, target
  2301. * id bindings may change.
  2302. *
  2303. * This routine is responsible for returning a remote port structure.
  2304. * The routine will search the list of remote ports it maintains
  2305. * internally on behalf of consistent target id mappings. If found, the
  2306. * remote port structure will be reused. Otherwise, a new remote port
  2307. * structure will be allocated.
  2308. *
  2309. * Whenever a remote port is allocated, a new fc_remote_port class
  2310. * device is created.
  2311. *
  2312. * Should not be called from interrupt context.
  2313. *
  2314. * Notes:
  2315. * This routine assumes no locks are held on entry.
  2316. */
  2317. struct fc_rport *
  2318. fc_remote_port_add(struct Scsi_Host *shost, int channel,
  2319. struct fc_rport_identifiers *ids)
  2320. {
  2321. struct fc_internal *fci = to_fc_internal(shost->transportt);
  2322. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2323. struct fc_rport *rport;
  2324. unsigned long flags;
  2325. int match = 0;
  2326. /* ensure any stgt delete functions are done */
  2327. fc_flush_work(shost);
  2328. /*
  2329. * Search the list of "active" rports, for an rport that has been
  2330. * deleted, but we've held off the real delete while the target
  2331. * is in a "blocked" state.
  2332. */
  2333. spin_lock_irqsave(shost->host_lock, flags);
  2334. list_for_each_entry(rport, &fc_host->rports, peers) {
  2335. if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
  2336. (rport->channel == channel)) {
  2337. switch (fc_host->tgtid_bind_type) {
  2338. case FC_TGTID_BIND_BY_WWPN:
  2339. case FC_TGTID_BIND_NONE:
  2340. if (rport->port_name == ids->port_name)
  2341. match = 1;
  2342. break;
  2343. case FC_TGTID_BIND_BY_WWNN:
  2344. if (rport->node_name == ids->node_name)
  2345. match = 1;
  2346. break;
  2347. case FC_TGTID_BIND_BY_ID:
  2348. if (rport->port_id == ids->port_id)
  2349. match = 1;
  2350. break;
  2351. }
  2352. if (match) {
  2353. memcpy(&rport->node_name, &ids->node_name,
  2354. sizeof(rport->node_name));
  2355. memcpy(&rport->port_name, &ids->port_name,
  2356. sizeof(rport->port_name));
  2357. rport->port_id = ids->port_id;
  2358. rport->port_state = FC_PORTSTATE_ONLINE;
  2359. rport->roles = ids->roles;
  2360. spin_unlock_irqrestore(shost->host_lock, flags);
  2361. if (fci->f->dd_fcrport_size)
  2362. memset(rport->dd_data, 0,
  2363. fci->f->dd_fcrport_size);
  2364. /*
  2365. * If we were not a target, cancel the
  2366. * io terminate and rport timers, and
  2367. * we're done.
  2368. *
  2369. * If we were a target, but our new role
  2370. * doesn't indicate a target, leave the
  2371. * timers running expecting the role to
  2372. * change as the target fully logs in. If
  2373. * it doesn't, the target will be torn down.
  2374. *
  2375. * If we were a target, and our role shows
  2376. * we're still a target, cancel the timers
  2377. * and kick off a scan.
  2378. */
  2379. /* was a target, not in roles */
  2380. if ((rport->scsi_target_id != -1) &&
  2381. (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
  2382. return rport;
  2383. /*
  2384. * Stop the fail io and dev_loss timers.
  2385. * If they flush, the port_state will
  2386. * be checked and will NOOP the function.
  2387. */
  2388. if (!cancel_delayed_work(&rport->fail_io_work))
  2389. fc_flush_devloss(shost);
  2390. if (!cancel_delayed_work(&rport->dev_loss_work))
  2391. fc_flush_devloss(shost);
  2392. spin_lock_irqsave(shost->host_lock, flags);
  2393. rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
  2394. FC_RPORT_DEVLOSS_PENDING |
  2395. FC_RPORT_DEVLOSS_CALLBK_DONE);
  2396. spin_unlock_irqrestore(shost->host_lock, flags);
  2397. /* if target, initiate a scan */
  2398. if (rport->scsi_target_id != -1) {
  2399. scsi_target_unblock(&rport->dev);
  2400. spin_lock_irqsave(shost->host_lock,
  2401. flags);
  2402. rport->flags |= FC_RPORT_SCAN_PENDING;
  2403. scsi_queue_work(shost,
  2404. &rport->scan_work);
  2405. spin_unlock_irqrestore(shost->host_lock,
  2406. flags);
  2407. }
  2408. fc_bsg_goose_queue(rport);
  2409. return rport;
  2410. }
  2411. }
  2412. }
  2413. /*
  2414. * Search the bindings array
  2415. * Note: if never a FCP target, you won't be on this list
  2416. */
  2417. if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
  2418. /* search for a matching consistent binding */
  2419. list_for_each_entry(rport, &fc_host->rport_bindings,
  2420. peers) {
  2421. if (rport->channel != channel)
  2422. continue;
  2423. switch (fc_host->tgtid_bind_type) {
  2424. case FC_TGTID_BIND_BY_WWPN:
  2425. if (rport->port_name == ids->port_name)
  2426. match = 1;
  2427. break;
  2428. case FC_TGTID_BIND_BY_WWNN:
  2429. if (rport->node_name == ids->node_name)
  2430. match = 1;
  2431. break;
  2432. case FC_TGTID_BIND_BY_ID:
  2433. if (rport->port_id == ids->port_id)
  2434. match = 1;
  2435. break;
  2436. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  2437. break;
  2438. }
  2439. if (match) {
  2440. list_move_tail(&rport->peers, &fc_host->rports);
  2441. break;
  2442. }
  2443. }
  2444. if (match) {
  2445. memcpy(&rport->node_name, &ids->node_name,
  2446. sizeof(rport->node_name));
  2447. memcpy(&rport->port_name, &ids->port_name,
  2448. sizeof(rport->port_name));
  2449. rport->port_id = ids->port_id;
  2450. rport->roles = ids->roles;
  2451. rport->port_state = FC_PORTSTATE_ONLINE;
  2452. rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
  2453. if (fci->f->dd_fcrport_size)
  2454. memset(rport->dd_data, 0,
  2455. fci->f->dd_fcrport_size);
  2456. spin_unlock_irqrestore(shost->host_lock, flags);
  2457. if (ids->roles & FC_PORT_ROLE_FCP_TARGET) {
  2458. scsi_target_unblock(&rport->dev);
  2459. /* initiate a scan of the target */
  2460. spin_lock_irqsave(shost->host_lock, flags);
  2461. rport->flags |= FC_RPORT_SCAN_PENDING;
  2462. scsi_queue_work(shost, &rport->scan_work);
  2463. spin_unlock_irqrestore(shost->host_lock, flags);
  2464. }
  2465. return rport;
  2466. }
  2467. }
  2468. spin_unlock_irqrestore(shost->host_lock, flags);
  2469. /* No consistent binding found - create new remote port entry */
  2470. rport = fc_rport_create(shost, channel, ids);
  2471. return rport;
  2472. }
  2473. EXPORT_SYMBOL(fc_remote_port_add);
  2474. /**
  2475. * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
  2476. * @rport: The remote port that no longer exists
  2477. *
  2478. * The LLDD calls this routine to notify the transport that a remote
  2479. * port is no longer part of the topology. Note: Although a port
  2480. * may no longer be part of the topology, it may persist in the remote
  2481. * ports displayed by the fc_host. We do this under 2 conditions:
  2482. * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
  2483. * This allows the port to temporarily disappear, then reappear without
  2484. * disrupting the SCSI device tree attached to it. During the "blocked"
  2485. * period the port will still exist.
  2486. * 2) If the port was a scsi target and disappears for longer than we
  2487. * expect, we'll delete the port and the tear down the SCSI device tree
  2488. * attached to it. However, we want to semi-persist the target id assigned
  2489. * to that port if it eventually does exist. The port structure will
  2490. * remain (although with minimal information) so that the target id
  2491. * bindings remails.
  2492. *
  2493. * If the remote port is not an FCP Target, it will be fully torn down
  2494. * and deallocated, including the fc_remote_port class device.
  2495. *
  2496. * If the remote port is an FCP Target, the port will be placed in a
  2497. * temporary blocked state. From the LLDD's perspective, the rport no
  2498. * longer exists. From the SCSI midlayer's perspective, the SCSI target
  2499. * exists, but all sdevs on it are blocked from further I/O. The following
  2500. * is then expected.
  2501. *
  2502. * If the remote port does not return (signaled by a LLDD call to
  2503. * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
  2504. * scsi target is removed - killing all outstanding i/o and removing the
  2505. * scsi devices attached ot it. The port structure will be marked Not
  2506. * Present and be partially cleared, leaving only enough information to
  2507. * recognize the remote port relative to the scsi target id binding if
  2508. * it later appears. The port will remain as long as there is a valid
  2509. * binding (e.g. until the user changes the binding type or unloads the
  2510. * scsi host with the binding).
  2511. *
  2512. * If the remote port returns within the dev_loss_tmo value (and matches
  2513. * according to the target id binding type), the port structure will be
  2514. * reused. If it is no longer a SCSI target, the target will be torn
  2515. * down. If it continues to be a SCSI target, then the target will be
  2516. * unblocked (allowing i/o to be resumed), and a scan will be activated
  2517. * to ensure that all luns are detected.
  2518. *
  2519. * Called from normal process context only - cannot be called from interrupt.
  2520. *
  2521. * Notes:
  2522. * This routine assumes no locks are held on entry.
  2523. */
  2524. void
  2525. fc_remote_port_delete(struct fc_rport *rport)
  2526. {
  2527. struct Scsi_Host *shost = rport_to_shost(rport);
  2528. unsigned long timeout = rport->dev_loss_tmo;
  2529. unsigned long flags;
  2530. /*
  2531. * No need to flush the fc_host work_q's, as all adds are synchronous.
  2532. *
  2533. * We do need to reclaim the rport scan work element, so eventually
  2534. * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
  2535. * there's still a scan pending.
  2536. */
  2537. spin_lock_irqsave(shost->host_lock, flags);
  2538. if (rport->port_state != FC_PORTSTATE_ONLINE) {
  2539. spin_unlock_irqrestore(shost->host_lock, flags);
  2540. return;
  2541. }
  2542. /*
  2543. * In the past, we if this was not an FCP-Target, we would
  2544. * unconditionally just jump to deleting the rport.
  2545. * However, rports can be used as node containers by the LLDD,
  2546. * and its not appropriate to just terminate the rport at the
  2547. * first sign of a loss in connectivity. The LLDD may want to
  2548. * send ELS traffic to re-validate the login. If the rport is
  2549. * immediately deleted, it makes it inappropriate for a node
  2550. * container.
  2551. * So... we now unconditionally wait dev_loss_tmo before
  2552. * destroying an rport.
  2553. */
  2554. rport->port_state = FC_PORTSTATE_BLOCKED;
  2555. rport->flags |= FC_RPORT_DEVLOSS_PENDING;
  2556. spin_unlock_irqrestore(shost->host_lock, flags);
  2557. if (rport->roles & FC_PORT_ROLE_FCP_INITIATOR &&
  2558. shost->active_mode & MODE_TARGET)
  2559. fc_tgt_it_nexus_destroy(shost, (unsigned long)rport);
  2560. scsi_target_block(&rport->dev);
  2561. /* see if we need to kill io faster than waiting for device loss */
  2562. if ((rport->fast_io_fail_tmo != -1) &&
  2563. (rport->fast_io_fail_tmo < timeout))
  2564. fc_queue_devloss_work(shost, &rport->fail_io_work,
  2565. rport->fast_io_fail_tmo * HZ);
  2566. /* cap the length the devices can be blocked until they are deleted */
  2567. fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
  2568. }
  2569. EXPORT_SYMBOL(fc_remote_port_delete);
  2570. /**
  2571. * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
  2572. * @rport: The remote port that changed.
  2573. * @roles: New roles for this port.
  2574. *
  2575. * Description: The LLDD calls this routine to notify the transport that the
  2576. * roles on a remote port may have changed. The largest effect of this is
  2577. * if a port now becomes a FCP Target, it must be allocated a
  2578. * scsi target id. If the port is no longer a FCP target, any
  2579. * scsi target id value assigned to it will persist in case the
  2580. * role changes back to include FCP Target. No changes in the scsi
  2581. * midlayer will be invoked if the role changes (in the expectation
  2582. * that the role will be resumed. If it doesn't normal error processing
  2583. * will take place).
  2584. *
  2585. * Should not be called from interrupt context.
  2586. *
  2587. * Notes:
  2588. * This routine assumes no locks are held on entry.
  2589. */
  2590. void
  2591. fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
  2592. {
  2593. struct Scsi_Host *shost = rport_to_shost(rport);
  2594. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2595. unsigned long flags;
  2596. int create = 0;
  2597. int ret;
  2598. spin_lock_irqsave(shost->host_lock, flags);
  2599. if (roles & FC_PORT_ROLE_FCP_TARGET) {
  2600. if (rport->scsi_target_id == -1) {
  2601. rport->scsi_target_id = fc_host->next_target_id++;
  2602. create = 1;
  2603. } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
  2604. create = 1;
  2605. } else if (shost->active_mode & MODE_TARGET) {
  2606. ret = fc_tgt_it_nexus_create(shost, (unsigned long)rport,
  2607. (char *)&rport->node_name);
  2608. if (ret)
  2609. printk(KERN_ERR "FC Remore Port tgt nexus failed %d\n",
  2610. ret);
  2611. }
  2612. rport->roles = roles;
  2613. spin_unlock_irqrestore(shost->host_lock, flags);
  2614. if (create) {
  2615. /*
  2616. * There may have been a delete timer running on the
  2617. * port. Ensure that it is cancelled as we now know
  2618. * the port is an FCP Target.
  2619. * Note: we know the rport is exists and in an online
  2620. * state as the LLDD would not have had an rport
  2621. * reference to pass us.
  2622. *
  2623. * Take no action on the del_timer failure as the state
  2624. * machine state change will validate the
  2625. * transaction.
  2626. */
  2627. if (!cancel_delayed_work(&rport->fail_io_work))
  2628. fc_flush_devloss(shost);
  2629. if (!cancel_delayed_work(&rport->dev_loss_work))
  2630. fc_flush_devloss(shost);
  2631. spin_lock_irqsave(shost->host_lock, flags);
  2632. rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
  2633. FC_RPORT_DEVLOSS_PENDING |
  2634. FC_RPORT_DEVLOSS_CALLBK_DONE);
  2635. spin_unlock_irqrestore(shost->host_lock, flags);
  2636. /* ensure any stgt delete functions are done */
  2637. fc_flush_work(shost);
  2638. scsi_target_unblock(&rport->dev);
  2639. /* initiate a scan of the target */
  2640. spin_lock_irqsave(shost->host_lock, flags);
  2641. rport->flags |= FC_RPORT_SCAN_PENDING;
  2642. scsi_queue_work(shost, &rport->scan_work);
  2643. spin_unlock_irqrestore(shost->host_lock, flags);
  2644. }
  2645. }
  2646. EXPORT_SYMBOL(fc_remote_port_rolechg);
  2647. /**
  2648. * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
  2649. * @work: rport target that failed to reappear in the allotted time.
  2650. *
  2651. * Description: An attempt to delete a remote port blocks, and if it fails
  2652. * to return in the allotted time this gets called.
  2653. */
  2654. static void
  2655. fc_timeout_deleted_rport(struct work_struct *work)
  2656. {
  2657. struct fc_rport *rport =
  2658. container_of(work, struct fc_rport, dev_loss_work.work);
  2659. struct Scsi_Host *shost = rport_to_shost(rport);
  2660. struct fc_internal *i = to_fc_internal(shost->transportt);
  2661. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2662. unsigned long flags;
  2663. int do_callback = 0;
  2664. spin_lock_irqsave(shost->host_lock, flags);
  2665. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  2666. /*
  2667. * If the port is ONLINE, then it came back. If it was a SCSI
  2668. * target, validate it still is. If not, tear down the
  2669. * scsi_target on it.
  2670. */
  2671. if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
  2672. (rport->scsi_target_id != -1) &&
  2673. !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
  2674. dev_printk(KERN_ERR, &rport->dev,
  2675. "blocked FC remote port time out: no longer"
  2676. " a FCP target, removing starget\n");
  2677. spin_unlock_irqrestore(shost->host_lock, flags);
  2678. scsi_target_unblock(&rport->dev);
  2679. fc_queue_work(shost, &rport->stgt_delete_work);
  2680. return;
  2681. }
  2682. /* NOOP state - we're flushing workq's */
  2683. if (rport->port_state != FC_PORTSTATE_BLOCKED) {
  2684. spin_unlock_irqrestore(shost->host_lock, flags);
  2685. dev_printk(KERN_ERR, &rport->dev,
  2686. "blocked FC remote port time out: leaving"
  2687. " rport%s alone\n",
  2688. (rport->scsi_target_id != -1) ? " and starget" : "");
  2689. return;
  2690. }
  2691. if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
  2692. (rport->scsi_target_id == -1)) {
  2693. list_del(&rport->peers);
  2694. rport->port_state = FC_PORTSTATE_DELETED;
  2695. dev_printk(KERN_ERR, &rport->dev,
  2696. "blocked FC remote port time out: removing"
  2697. " rport%s\n",
  2698. (rport->scsi_target_id != -1) ? " and starget" : "");
  2699. fc_queue_work(shost, &rport->rport_delete_work);
  2700. spin_unlock_irqrestore(shost->host_lock, flags);
  2701. return;
  2702. }
  2703. dev_printk(KERN_ERR, &rport->dev,
  2704. "blocked FC remote port time out: removing target and "
  2705. "saving binding\n");
  2706. list_move_tail(&rport->peers, &fc_host->rport_bindings);
  2707. /*
  2708. * Note: We do not remove or clear the hostdata area. This allows
  2709. * host-specific target data to persist along with the
  2710. * scsi_target_id. It's up to the host to manage it's hostdata area.
  2711. */
  2712. /*
  2713. * Reinitialize port attributes that may change if the port comes back.
  2714. */
  2715. rport->maxframe_size = -1;
  2716. rport->supported_classes = FC_COS_UNSPECIFIED;
  2717. rport->roles = FC_PORT_ROLE_UNKNOWN;
  2718. rport->port_state = FC_PORTSTATE_NOTPRESENT;
  2719. rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
  2720. /*
  2721. * Pre-emptively kill I/O rather than waiting for the work queue
  2722. * item to teardown the starget. (FCOE libFC folks prefer this
  2723. * and to have the rport_port_id still set when it's done).
  2724. */
  2725. spin_unlock_irqrestore(shost->host_lock, flags);
  2726. fc_terminate_rport_io(rport);
  2727. spin_lock_irqsave(shost->host_lock, flags);
  2728. if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
  2729. /* remove the identifiers that aren't used in the consisting binding */
  2730. switch (fc_host->tgtid_bind_type) {
  2731. case FC_TGTID_BIND_BY_WWPN:
  2732. rport->node_name = -1;
  2733. rport->port_id = -1;
  2734. break;
  2735. case FC_TGTID_BIND_BY_WWNN:
  2736. rport->port_name = -1;
  2737. rport->port_id = -1;
  2738. break;
  2739. case FC_TGTID_BIND_BY_ID:
  2740. rport->node_name = -1;
  2741. rport->port_name = -1;
  2742. break;
  2743. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  2744. break;
  2745. }
  2746. /*
  2747. * As this only occurs if the remote port (scsi target)
  2748. * went away and didn't come back - we'll remove
  2749. * all attached scsi devices.
  2750. */
  2751. rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
  2752. fc_queue_work(shost, &rport->stgt_delete_work);
  2753. do_callback = 1;
  2754. }
  2755. spin_unlock_irqrestore(shost->host_lock, flags);
  2756. /*
  2757. * Notify the driver that the rport is now dead. The LLDD will
  2758. * also guarantee that any communication to the rport is terminated
  2759. *
  2760. * Note: we set the CALLBK_DONE flag above to correspond
  2761. */
  2762. if (do_callback && i->f->dev_loss_tmo_callbk)
  2763. i->f->dev_loss_tmo_callbk(rport);
  2764. }
  2765. /**
  2766. * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
  2767. * @work: rport to terminate io on.
  2768. *
  2769. * Notes: Only requests the failure of the io, not that all are flushed
  2770. * prior to returning.
  2771. */
  2772. static void
  2773. fc_timeout_fail_rport_io(struct work_struct *work)
  2774. {
  2775. struct fc_rport *rport =
  2776. container_of(work, struct fc_rport, fail_io_work.work);
  2777. if (rport->port_state != FC_PORTSTATE_BLOCKED)
  2778. return;
  2779. rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
  2780. fc_terminate_rport_io(rport);
  2781. }
  2782. /**
  2783. * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
  2784. * @work: remote port to be scanned.
  2785. */
  2786. static void
  2787. fc_scsi_scan_rport(struct work_struct *work)
  2788. {
  2789. struct fc_rport *rport =
  2790. container_of(work, struct fc_rport, scan_work);
  2791. struct Scsi_Host *shost = rport_to_shost(rport);
  2792. struct fc_internal *i = to_fc_internal(shost->transportt);
  2793. unsigned long flags;
  2794. if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
  2795. (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
  2796. !(i->f->disable_target_scan)) {
  2797. scsi_scan_target(&rport->dev, rport->channel,
  2798. rport->scsi_target_id, SCAN_WILD_CARD, 1);
  2799. }
  2800. spin_lock_irqsave(shost->host_lock, flags);
  2801. rport->flags &= ~FC_RPORT_SCAN_PENDING;
  2802. spin_unlock_irqrestore(shost->host_lock, flags);
  2803. }
  2804. /**
  2805. * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
  2806. * @cmnd: SCSI command that scsi_eh is trying to recover
  2807. *
  2808. * This routine can be called from a FC LLD scsi_eh callback. It
  2809. * blocks the scsi_eh thread until the fc_rport leaves the
  2810. * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
  2811. * necessary to avoid the scsi_eh failing recovery actions for blocked
  2812. * rports which would lead to offlined SCSI devices.
  2813. *
  2814. * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
  2815. * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
  2816. * passed back to scsi_eh.
  2817. */
  2818. int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
  2819. {
  2820. struct Scsi_Host *shost = cmnd->device->host;
  2821. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  2822. unsigned long flags;
  2823. spin_lock_irqsave(shost->host_lock, flags);
  2824. while (rport->port_state == FC_PORTSTATE_BLOCKED &&
  2825. !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
  2826. spin_unlock_irqrestore(shost->host_lock, flags);
  2827. msleep(1000);
  2828. spin_lock_irqsave(shost->host_lock, flags);
  2829. }
  2830. spin_unlock_irqrestore(shost->host_lock, flags);
  2831. if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
  2832. return FAST_IO_FAIL;
  2833. return 0;
  2834. }
  2835. EXPORT_SYMBOL(fc_block_scsi_eh);
  2836. /**
  2837. * fc_vport_setup - allocates and creates a FC virtual port.
  2838. * @shost: scsi host the virtual port is connected to.
  2839. * @channel: Channel on shost port connected to.
  2840. * @pdev: parent device for vport
  2841. * @ids: The world wide names, FC4 port roles, etc for
  2842. * the virtual port.
  2843. * @ret_vport: The pointer to the created vport.
  2844. *
  2845. * Allocates and creates the vport structure, calls the parent host
  2846. * to instantiate the vport, the completes w/ class and sysfs creation.
  2847. *
  2848. * Notes:
  2849. * This routine assumes no locks are held on entry.
  2850. */
  2851. static int
  2852. fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
  2853. struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
  2854. {
  2855. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2856. struct fc_internal *fci = to_fc_internal(shost->transportt);
  2857. struct fc_vport *vport;
  2858. struct device *dev;
  2859. unsigned long flags;
  2860. size_t size;
  2861. int error;
  2862. *ret_vport = NULL;
  2863. if ( ! fci->f->vport_create)
  2864. return -ENOENT;
  2865. size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
  2866. vport = kzalloc(size, GFP_KERNEL);
  2867. if (unlikely(!vport)) {
  2868. printk(KERN_ERR "%s: allocation failure\n", __func__);
  2869. return -ENOMEM;
  2870. }
  2871. vport->vport_state = FC_VPORT_UNKNOWN;
  2872. vport->vport_last_state = FC_VPORT_UNKNOWN;
  2873. vport->node_name = ids->node_name;
  2874. vport->port_name = ids->port_name;
  2875. vport->roles = ids->roles;
  2876. vport->vport_type = ids->vport_type;
  2877. if (fci->f->dd_fcvport_size)
  2878. vport->dd_data = &vport[1];
  2879. vport->shost = shost;
  2880. vport->channel = channel;
  2881. vport->flags = FC_VPORT_CREATING;
  2882. INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
  2883. spin_lock_irqsave(shost->host_lock, flags);
  2884. if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
  2885. spin_unlock_irqrestore(shost->host_lock, flags);
  2886. kfree(vport);
  2887. return -ENOSPC;
  2888. }
  2889. fc_host->npiv_vports_inuse++;
  2890. vport->number = fc_host->next_vport_number++;
  2891. list_add_tail(&vport->peers, &fc_host->vports);
  2892. get_device(&shost->shost_gendev); /* for fc_host->vport list */
  2893. spin_unlock_irqrestore(shost->host_lock, flags);
  2894. dev = &vport->dev;
  2895. device_initialize(dev); /* takes self reference */
  2896. dev->parent = get_device(pdev); /* takes parent reference */
  2897. dev->release = fc_vport_dev_release;
  2898. dev_set_name(dev, "vport-%d:%d-%d",
  2899. shost->host_no, channel, vport->number);
  2900. transport_setup_device(dev);
  2901. error = device_add(dev);
  2902. if (error) {
  2903. printk(KERN_ERR "FC Virtual Port device_add failed\n");
  2904. goto delete_vport;
  2905. }
  2906. transport_add_device(dev);
  2907. transport_configure_device(dev);
  2908. error = fci->f->vport_create(vport, ids->disable);
  2909. if (error) {
  2910. printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
  2911. goto delete_vport_all;
  2912. }
  2913. /*
  2914. * if the parent isn't the physical adapter's Scsi_Host, ensure
  2915. * the Scsi_Host at least contains ia symlink to the vport.
  2916. */
  2917. if (pdev != &shost->shost_gendev) {
  2918. error = sysfs_create_link(&shost->shost_gendev.kobj,
  2919. &dev->kobj, dev_name(dev));
  2920. if (error)
  2921. printk(KERN_ERR
  2922. "%s: Cannot create vport symlinks for "
  2923. "%s, err=%d\n",
  2924. __func__, dev_name(dev), error);
  2925. }
  2926. spin_lock_irqsave(shost->host_lock, flags);
  2927. vport->flags &= ~FC_VPORT_CREATING;
  2928. spin_unlock_irqrestore(shost->host_lock, flags);
  2929. dev_printk(KERN_NOTICE, pdev,
  2930. "%s created via shost%d channel %d\n", dev_name(dev),
  2931. shost->host_no, channel);
  2932. *ret_vport = vport;
  2933. return 0;
  2934. delete_vport_all:
  2935. transport_remove_device(dev);
  2936. device_del(dev);
  2937. delete_vport:
  2938. transport_destroy_device(dev);
  2939. spin_lock_irqsave(shost->host_lock, flags);
  2940. list_del(&vport->peers);
  2941. put_device(&shost->shost_gendev); /* for fc_host->vport list */
  2942. fc_host->npiv_vports_inuse--;
  2943. spin_unlock_irqrestore(shost->host_lock, flags);
  2944. put_device(dev->parent);
  2945. kfree(vport);
  2946. return error;
  2947. }
  2948. /**
  2949. * fc_vport_create - Admin App or LLDD requests creation of a vport
  2950. * @shost: scsi host the virtual port is connected to.
  2951. * @channel: channel on shost port connected to.
  2952. * @ids: The world wide names, FC4 port roles, etc for
  2953. * the virtual port.
  2954. *
  2955. * Notes:
  2956. * This routine assumes no locks are held on entry.
  2957. */
  2958. struct fc_vport *
  2959. fc_vport_create(struct Scsi_Host *shost, int channel,
  2960. struct fc_vport_identifiers *ids)
  2961. {
  2962. int stat;
  2963. struct fc_vport *vport;
  2964. stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
  2965. ids, &vport);
  2966. return stat ? NULL : vport;
  2967. }
  2968. EXPORT_SYMBOL(fc_vport_create);
  2969. /**
  2970. * fc_vport_terminate - Admin App or LLDD requests termination of a vport
  2971. * @vport: fc_vport to be terminated
  2972. *
  2973. * Calls the LLDD vport_delete() function, then deallocates and removes
  2974. * the vport from the shost and object tree.
  2975. *
  2976. * Notes:
  2977. * This routine assumes no locks are held on entry.
  2978. */
  2979. int
  2980. fc_vport_terminate(struct fc_vport *vport)
  2981. {
  2982. struct Scsi_Host *shost = vport_to_shost(vport);
  2983. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2984. struct fc_internal *i = to_fc_internal(shost->transportt);
  2985. struct device *dev = &vport->dev;
  2986. unsigned long flags;
  2987. int stat;
  2988. if (i->f->vport_delete)
  2989. stat = i->f->vport_delete(vport);
  2990. else
  2991. stat = -ENOENT;
  2992. spin_lock_irqsave(shost->host_lock, flags);
  2993. vport->flags &= ~FC_VPORT_DELETING;
  2994. if (!stat) {
  2995. vport->flags |= FC_VPORT_DELETED;
  2996. list_del(&vport->peers);
  2997. fc_host->npiv_vports_inuse--;
  2998. put_device(&shost->shost_gendev); /* for fc_host->vport list */
  2999. }
  3000. spin_unlock_irqrestore(shost->host_lock, flags);
  3001. if (stat)
  3002. return stat;
  3003. if (dev->parent != &shost->shost_gendev)
  3004. sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
  3005. transport_remove_device(dev);
  3006. device_del(dev);
  3007. transport_destroy_device(dev);
  3008. /*
  3009. * Removing our self-reference should mean our
  3010. * release function gets called, which will drop the remaining
  3011. * parent reference and free the data structure.
  3012. */
  3013. put_device(dev); /* for self-reference */
  3014. return 0; /* SUCCESS */
  3015. }
  3016. EXPORT_SYMBOL(fc_vport_terminate);
  3017. /**
  3018. * fc_vport_sched_delete - workq-based delete request for a vport
  3019. * @work: vport to be deleted.
  3020. */
  3021. static void
  3022. fc_vport_sched_delete(struct work_struct *work)
  3023. {
  3024. struct fc_vport *vport =
  3025. container_of(work, struct fc_vport, vport_delete_work);
  3026. int stat;
  3027. stat = fc_vport_terminate(vport);
  3028. if (stat)
  3029. dev_printk(KERN_ERR, vport->dev.parent,
  3030. "%s: %s could not be deleted created via "
  3031. "shost%d channel %d - error %d\n", __func__,
  3032. dev_name(&vport->dev), vport->shost->host_no,
  3033. vport->channel, stat);
  3034. }
  3035. /*
  3036. * BSG support
  3037. */
  3038. /**
  3039. * fc_destroy_bsgjob - routine to teardown/delete a fc bsg job
  3040. * @job: fc_bsg_job that is to be torn down
  3041. */
  3042. static void
  3043. fc_destroy_bsgjob(struct fc_bsg_job *job)
  3044. {
  3045. unsigned long flags;
  3046. spin_lock_irqsave(&job->job_lock, flags);
  3047. if (job->ref_cnt) {
  3048. spin_unlock_irqrestore(&job->job_lock, flags);
  3049. return;
  3050. }
  3051. spin_unlock_irqrestore(&job->job_lock, flags);
  3052. put_device(job->dev); /* release reference for the request */
  3053. kfree(job->request_payload.sg_list);
  3054. kfree(job->reply_payload.sg_list);
  3055. kfree(job);
  3056. }
  3057. /**
  3058. * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
  3059. * completed
  3060. * @job: fc_bsg_job that is complete
  3061. */
  3062. static void
  3063. fc_bsg_jobdone(struct fc_bsg_job *job)
  3064. {
  3065. struct request *req = job->req;
  3066. struct request *rsp = req->next_rq;
  3067. int err;
  3068. err = job->req->errors = job->reply->result;
  3069. if (err < 0)
  3070. /* we're only returning the result field in the reply */
  3071. job->req->sense_len = sizeof(uint32_t);
  3072. else
  3073. job->req->sense_len = job->reply_len;
  3074. /* we assume all request payload was transferred, residual == 0 */
  3075. req->resid_len = 0;
  3076. if (rsp) {
  3077. WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
  3078. /* set reply (bidi) residual */
  3079. rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
  3080. rsp->resid_len);
  3081. }
  3082. blk_complete_request(req);
  3083. }
  3084. /**
  3085. * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
  3086. * @rq: BSG request that holds the job to be destroyed
  3087. */
  3088. static void fc_bsg_softirq_done(struct request *rq)
  3089. {
  3090. struct fc_bsg_job *job = rq->special;
  3091. unsigned long flags;
  3092. spin_lock_irqsave(&job->job_lock, flags);
  3093. job->state_flags |= FC_RQST_STATE_DONE;
  3094. job->ref_cnt--;
  3095. spin_unlock_irqrestore(&job->job_lock, flags);
  3096. blk_end_request_all(rq, rq->errors);
  3097. fc_destroy_bsgjob(job);
  3098. }
  3099. /**
  3100. * fc_bsg_job_timeout - handler for when a bsg request timesout
  3101. * @req: request that timed out
  3102. */
  3103. static enum blk_eh_timer_return
  3104. fc_bsg_job_timeout(struct request *req)
  3105. {
  3106. struct fc_bsg_job *job = (void *) req->special;
  3107. struct Scsi_Host *shost = job->shost;
  3108. struct fc_internal *i = to_fc_internal(shost->transportt);
  3109. unsigned long flags;
  3110. int err = 0, done = 0;
  3111. if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
  3112. return BLK_EH_RESET_TIMER;
  3113. spin_lock_irqsave(&job->job_lock, flags);
  3114. if (job->state_flags & FC_RQST_STATE_DONE)
  3115. done = 1;
  3116. else
  3117. job->ref_cnt++;
  3118. spin_unlock_irqrestore(&job->job_lock, flags);
  3119. if (!done && i->f->bsg_timeout) {
  3120. /* call LLDD to abort the i/o as it has timed out */
  3121. err = i->f->bsg_timeout(job);
  3122. if (err == -EAGAIN) {
  3123. job->ref_cnt--;
  3124. return BLK_EH_RESET_TIMER;
  3125. } else if (err)
  3126. printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
  3127. "abort failed with status %d\n", err);
  3128. }
  3129. /* the blk_end_sync_io() doesn't check the error */
  3130. if (done)
  3131. return BLK_EH_NOT_HANDLED;
  3132. else
  3133. return BLK_EH_HANDLED;
  3134. }
  3135. static int
  3136. fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
  3137. {
  3138. size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
  3139. BUG_ON(!req->nr_phys_segments);
  3140. buf->sg_list = kzalloc(sz, GFP_KERNEL);
  3141. if (!buf->sg_list)
  3142. return -ENOMEM;
  3143. sg_init_table(buf->sg_list, req->nr_phys_segments);
  3144. buf->sg_cnt = blk_rq_map_sg(req->q, req, buf->sg_list);
  3145. buf->payload_len = blk_rq_bytes(req);
  3146. return 0;
  3147. }
  3148. /**
  3149. * fc_req_to_bsgjob - Allocate/create the fc_bsg_job structure for the
  3150. * bsg request
  3151. * @shost: SCSI Host corresponding to the bsg object
  3152. * @rport: (optional) FC Remote Port corresponding to the bsg object
  3153. * @req: BSG request that needs a job structure
  3154. */
  3155. static int
  3156. fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
  3157. struct request *req)
  3158. {
  3159. struct fc_internal *i = to_fc_internal(shost->transportt);
  3160. struct request *rsp = req->next_rq;
  3161. struct fc_bsg_job *job;
  3162. int ret;
  3163. BUG_ON(req->special);
  3164. job = kzalloc(sizeof(struct fc_bsg_job) + i->f->dd_bsg_size,
  3165. GFP_KERNEL);
  3166. if (!job)
  3167. return -ENOMEM;
  3168. /*
  3169. * Note: this is a bit silly.
  3170. * The request gets formatted as a SGIO v4 ioctl request, which
  3171. * then gets reformatted as a blk request, which then gets
  3172. * reformatted as a fc bsg request. And on completion, we have
  3173. * to wrap return results such that SGIO v4 thinks it was a scsi
  3174. * status. I hope this was all worth it.
  3175. */
  3176. req->special = job;
  3177. job->shost = shost;
  3178. job->rport = rport;
  3179. job->req = req;
  3180. if (i->f->dd_bsg_size)
  3181. job->dd_data = (void *)&job[1];
  3182. spin_lock_init(&job->job_lock);
  3183. job->request = (struct fc_bsg_request *)req->cmd;
  3184. job->request_len = req->cmd_len;
  3185. job->reply = req->sense;
  3186. job->reply_len = SCSI_SENSE_BUFFERSIZE; /* Size of sense buffer
  3187. * allocated */
  3188. if (req->bio) {
  3189. ret = fc_bsg_map_buffer(&job->request_payload, req);
  3190. if (ret)
  3191. goto failjob_rls_job;
  3192. }
  3193. if (rsp && rsp->bio) {
  3194. ret = fc_bsg_map_buffer(&job->reply_payload, rsp);
  3195. if (ret)
  3196. goto failjob_rls_rqst_payload;
  3197. }
  3198. job->job_done = fc_bsg_jobdone;
  3199. if (rport)
  3200. job->dev = &rport->dev;
  3201. else
  3202. job->dev = &shost->shost_gendev;
  3203. get_device(job->dev); /* take a reference for the request */
  3204. job->ref_cnt = 1;
  3205. return 0;
  3206. failjob_rls_rqst_payload:
  3207. kfree(job->request_payload.sg_list);
  3208. failjob_rls_job:
  3209. kfree(job);
  3210. return -ENOMEM;
  3211. }
  3212. enum fc_dispatch_result {
  3213. FC_DISPATCH_BREAK, /* on return, q is locked, break from q loop */
  3214. FC_DISPATCH_LOCKED, /* on return, q is locked, continue on */
  3215. FC_DISPATCH_UNLOCKED, /* on return, q is unlocked, continue on */
  3216. };
  3217. /**
  3218. * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
  3219. * @q: fc host request queue
  3220. * @shost: scsi host rport attached to
  3221. * @job: bsg job to be processed
  3222. */
  3223. static enum fc_dispatch_result
  3224. fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
  3225. struct fc_bsg_job *job)
  3226. {
  3227. struct fc_internal *i = to_fc_internal(shost->transportt);
  3228. int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
  3229. int ret;
  3230. /* Validate the host command */
  3231. switch (job->request->msgcode) {
  3232. case FC_BSG_HST_ADD_RPORT:
  3233. cmdlen += sizeof(struct fc_bsg_host_add_rport);
  3234. break;
  3235. case FC_BSG_HST_DEL_RPORT:
  3236. cmdlen += sizeof(struct fc_bsg_host_del_rport);
  3237. break;
  3238. case FC_BSG_HST_ELS_NOLOGIN:
  3239. cmdlen += sizeof(struct fc_bsg_host_els);
  3240. /* there better be a xmt and rcv payloads */
  3241. if ((!job->request_payload.payload_len) ||
  3242. (!job->reply_payload.payload_len)) {
  3243. ret = -EINVAL;
  3244. goto fail_host_msg;
  3245. }
  3246. break;
  3247. case FC_BSG_HST_CT:
  3248. cmdlen += sizeof(struct fc_bsg_host_ct);
  3249. /* there better be xmt and rcv payloads */
  3250. if ((!job->request_payload.payload_len) ||
  3251. (!job->reply_payload.payload_len)) {
  3252. ret = -EINVAL;
  3253. goto fail_host_msg;
  3254. }
  3255. break;
  3256. case FC_BSG_HST_VENDOR:
  3257. cmdlen += sizeof(struct fc_bsg_host_vendor);
  3258. if ((shost->hostt->vendor_id == 0L) ||
  3259. (job->request->rqst_data.h_vendor.vendor_id !=
  3260. shost->hostt->vendor_id)) {
  3261. ret = -ESRCH;
  3262. goto fail_host_msg;
  3263. }
  3264. break;
  3265. default:
  3266. ret = -EBADR;
  3267. goto fail_host_msg;
  3268. }
  3269. /* check if we really have all the request data needed */
  3270. if (job->request_len < cmdlen) {
  3271. ret = -ENOMSG;
  3272. goto fail_host_msg;
  3273. }
  3274. ret = i->f->bsg_request(job);
  3275. if (!ret)
  3276. return FC_DISPATCH_UNLOCKED;
  3277. fail_host_msg:
  3278. /* return the errno failure code as the only status */
  3279. BUG_ON(job->reply_len < sizeof(uint32_t));
  3280. job->reply->reply_payload_rcv_len = 0;
  3281. job->reply->result = ret;
  3282. job->reply_len = sizeof(uint32_t);
  3283. fc_bsg_jobdone(job);
  3284. return FC_DISPATCH_UNLOCKED;
  3285. }
  3286. /*
  3287. * fc_bsg_goose_queue - restart rport queue in case it was stopped
  3288. * @rport: rport to be restarted
  3289. */
  3290. static void
  3291. fc_bsg_goose_queue(struct fc_rport *rport)
  3292. {
  3293. if (!rport->rqst_q)
  3294. return;
  3295. /*
  3296. * This get/put dance makes no sense
  3297. */
  3298. get_device(&rport->dev);
  3299. blk_run_queue_async(rport->rqst_q);
  3300. put_device(&rport->dev);
  3301. }
  3302. /**
  3303. * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
  3304. * @q: rport request queue
  3305. * @shost: scsi host rport attached to
  3306. * @rport: rport request destined to
  3307. * @job: bsg job to be processed
  3308. */
  3309. static enum fc_dispatch_result
  3310. fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
  3311. struct fc_rport *rport, struct fc_bsg_job *job)
  3312. {
  3313. struct fc_internal *i = to_fc_internal(shost->transportt);
  3314. int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
  3315. int ret;
  3316. /* Validate the rport command */
  3317. switch (job->request->msgcode) {
  3318. case FC_BSG_RPT_ELS:
  3319. cmdlen += sizeof(struct fc_bsg_rport_els);
  3320. goto check_bidi;
  3321. case FC_BSG_RPT_CT:
  3322. cmdlen += sizeof(struct fc_bsg_rport_ct);
  3323. check_bidi:
  3324. /* there better be xmt and rcv payloads */
  3325. if ((!job->request_payload.payload_len) ||
  3326. (!job->reply_payload.payload_len)) {
  3327. ret = -EINVAL;
  3328. goto fail_rport_msg;
  3329. }
  3330. break;
  3331. default:
  3332. ret = -EBADR;
  3333. goto fail_rport_msg;
  3334. }
  3335. /* check if we really have all the request data needed */
  3336. if (job->request_len < cmdlen) {
  3337. ret = -ENOMSG;
  3338. goto fail_rport_msg;
  3339. }
  3340. ret = i->f->bsg_request(job);
  3341. if (!ret)
  3342. return FC_DISPATCH_UNLOCKED;
  3343. fail_rport_msg:
  3344. /* return the errno failure code as the only status */
  3345. BUG_ON(job->reply_len < sizeof(uint32_t));
  3346. job->reply->reply_payload_rcv_len = 0;
  3347. job->reply->result = ret;
  3348. job->reply_len = sizeof(uint32_t);
  3349. fc_bsg_jobdone(job);
  3350. return FC_DISPATCH_UNLOCKED;
  3351. }
  3352. /**
  3353. * fc_bsg_request_handler - generic handler for bsg requests
  3354. * @q: request queue to manage
  3355. * @shost: Scsi_Host related to the bsg object
  3356. * @rport: FC remote port related to the bsg object (optional)
  3357. * @dev: device structure for bsg object
  3358. */
  3359. static void
  3360. fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
  3361. struct fc_rport *rport, struct device *dev)
  3362. {
  3363. struct request *req;
  3364. struct fc_bsg_job *job;
  3365. enum fc_dispatch_result ret;
  3366. if (!get_device(dev))
  3367. return;
  3368. while (1) {
  3369. if (rport && (rport->port_state == FC_PORTSTATE_BLOCKED) &&
  3370. !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
  3371. break;
  3372. req = blk_fetch_request(q);
  3373. if (!req)
  3374. break;
  3375. if (rport && (rport->port_state != FC_PORTSTATE_ONLINE)) {
  3376. req->errors = -ENXIO;
  3377. spin_unlock_irq(q->queue_lock);
  3378. blk_end_request_all(req, -ENXIO);
  3379. spin_lock_irq(q->queue_lock);
  3380. continue;
  3381. }
  3382. spin_unlock_irq(q->queue_lock);
  3383. ret = fc_req_to_bsgjob(shost, rport, req);
  3384. if (ret) {
  3385. req->errors = ret;
  3386. blk_end_request_all(req, ret);
  3387. spin_lock_irq(q->queue_lock);
  3388. continue;
  3389. }
  3390. job = req->special;
  3391. /* check if we have the msgcode value at least */
  3392. if (job->request_len < sizeof(uint32_t)) {
  3393. BUG_ON(job->reply_len < sizeof(uint32_t));
  3394. job->reply->reply_payload_rcv_len = 0;
  3395. job->reply->result = -ENOMSG;
  3396. job->reply_len = sizeof(uint32_t);
  3397. fc_bsg_jobdone(job);
  3398. spin_lock_irq(q->queue_lock);
  3399. continue;
  3400. }
  3401. /* the dispatch routines will unlock the queue_lock */
  3402. if (rport)
  3403. ret = fc_bsg_rport_dispatch(q, shost, rport, job);
  3404. else
  3405. ret = fc_bsg_host_dispatch(q, shost, job);
  3406. /* did dispatcher hit state that can't process any more */
  3407. if (ret == FC_DISPATCH_BREAK)
  3408. break;
  3409. /* did dispatcher had released the lock */
  3410. if (ret == FC_DISPATCH_UNLOCKED)
  3411. spin_lock_irq(q->queue_lock);
  3412. }
  3413. spin_unlock_irq(q->queue_lock);
  3414. put_device(dev);
  3415. spin_lock_irq(q->queue_lock);
  3416. }
  3417. /**
  3418. * fc_bsg_host_handler - handler for bsg requests for a fc host
  3419. * @q: fc host request queue
  3420. */
  3421. static void
  3422. fc_bsg_host_handler(struct request_queue *q)
  3423. {
  3424. struct Scsi_Host *shost = q->queuedata;
  3425. fc_bsg_request_handler(q, shost, NULL, &shost->shost_gendev);
  3426. }
  3427. /**
  3428. * fc_bsg_rport_handler - handler for bsg requests for a fc rport
  3429. * @q: rport request queue
  3430. */
  3431. static void
  3432. fc_bsg_rport_handler(struct request_queue *q)
  3433. {
  3434. struct fc_rport *rport = q->queuedata;
  3435. struct Scsi_Host *shost = rport_to_shost(rport);
  3436. fc_bsg_request_handler(q, shost, rport, &rport->dev);
  3437. }
  3438. /**
  3439. * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
  3440. * @shost: shost for fc_host
  3441. * @fc_host: fc_host adding the structures to
  3442. */
  3443. static int
  3444. fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
  3445. {
  3446. struct device *dev = &shost->shost_gendev;
  3447. struct fc_internal *i = to_fc_internal(shost->transportt);
  3448. struct request_queue *q;
  3449. int err;
  3450. char bsg_name[20];
  3451. fc_host->rqst_q = NULL;
  3452. if (!i->f->bsg_request)
  3453. return -ENOTSUPP;
  3454. snprintf(bsg_name, sizeof(bsg_name),
  3455. "fc_host%d", shost->host_no);
  3456. q = __scsi_alloc_queue(shost, fc_bsg_host_handler);
  3457. if (!q) {
  3458. printk(KERN_ERR "fc_host%d: bsg interface failed to "
  3459. "initialize - no request queue\n",
  3460. shost->host_no);
  3461. return -ENOMEM;
  3462. }
  3463. q->queuedata = shost;
  3464. queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
  3465. blk_queue_softirq_done(q, fc_bsg_softirq_done);
  3466. blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
  3467. blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
  3468. err = bsg_register_queue(q, dev, bsg_name, NULL);
  3469. if (err) {
  3470. printk(KERN_ERR "fc_host%d: bsg interface failed to "
  3471. "initialize - register queue\n",
  3472. shost->host_no);
  3473. blk_cleanup_queue(q);
  3474. return err;
  3475. }
  3476. fc_host->rqst_q = q;
  3477. return 0;
  3478. }
  3479. /**
  3480. * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
  3481. * @shost: shost that rport is attached to
  3482. * @rport: rport that the bsg hooks are being attached to
  3483. */
  3484. static int
  3485. fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
  3486. {
  3487. struct device *dev = &rport->dev;
  3488. struct fc_internal *i = to_fc_internal(shost->transportt);
  3489. struct request_queue *q;
  3490. int err;
  3491. rport->rqst_q = NULL;
  3492. if (!i->f->bsg_request)
  3493. return -ENOTSUPP;
  3494. q = __scsi_alloc_queue(shost, fc_bsg_rport_handler);
  3495. if (!q) {
  3496. printk(KERN_ERR "%s: bsg interface failed to "
  3497. "initialize - no request queue\n",
  3498. dev->kobj.name);
  3499. return -ENOMEM;
  3500. }
  3501. q->queuedata = rport;
  3502. queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
  3503. blk_queue_softirq_done(q, fc_bsg_softirq_done);
  3504. blk_queue_rq_timed_out(q, fc_bsg_job_timeout);
  3505. blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
  3506. err = bsg_register_queue(q, dev, NULL, NULL);
  3507. if (err) {
  3508. printk(KERN_ERR "%s: bsg interface failed to "
  3509. "initialize - register queue\n",
  3510. dev->kobj.name);
  3511. blk_cleanup_queue(q);
  3512. return err;
  3513. }
  3514. rport->rqst_q = q;
  3515. return 0;
  3516. }
  3517. /**
  3518. * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
  3519. * @q: the request_queue that is to be torn down.
  3520. *
  3521. * Notes:
  3522. * Before unregistering the queue empty any requests that are blocked
  3523. *
  3524. *
  3525. */
  3526. static void
  3527. fc_bsg_remove(struct request_queue *q)
  3528. {
  3529. if (q) {
  3530. bsg_unregister_queue(q);
  3531. blk_cleanup_queue(q);
  3532. }
  3533. }
  3534. /* Original Author: Martin Hicks */
  3535. MODULE_AUTHOR("James Smart");
  3536. MODULE_DESCRIPTION("FC Transport Attributes");
  3537. MODULE_LICENSE("GPL");
  3538. module_init(fc_transport_init);
  3539. module_exit(fc_transport_exit);