scsi_transport_fc.c 98 KB

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