scsi_transport_fc.c 98 KB

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