scsi_transport_fc.c 117 KB

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