scsi_transport_fc.c 117 KB

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