scsi_transport_fc.c 115 KB

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