scsi_transport_fc.c 117 KB

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