scsi_transport_fc.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  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-2005 James Smart, Emulex Corporation
  23. * Rewrite for host, target, device, and remote port attributes,
  24. * statistics, and service functions...
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_transport_fc.h>
  33. #include "scsi_priv.h"
  34. #define FC_PRINTK(x, l, f, a...) printk(l "scsi(%d:%d:%d:%d): " f, (x)->host->host_no, (x)->channel, (x)->id, (x)->lun , ##a)
  35. /*
  36. * Redefine so that we can have same named attributes in the
  37. * sdev/starget/host objects.
  38. */
  39. #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  40. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  41. __ATTR(_name,_mode,_show,_store)
  42. #define fc_enum_name_search(title, table_type, table) \
  43. static const char *get_fc_##title##_name(enum table_type table_key) \
  44. { \
  45. int i; \
  46. char *name = NULL; \
  47. \
  48. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  49. if (table[i].value == table_key) { \
  50. name = table[i].name; \
  51. break; \
  52. } \
  53. } \
  54. return name; \
  55. }
  56. #define fc_enum_name_match(title, table_type, table) \
  57. static int get_fc_##title##_match(const char *table_key, \
  58. enum table_type *value) \
  59. { \
  60. int i; \
  61. \
  62. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  63. if (strncmp(table_key, table[i].name, \
  64. table[i].matchlen) == 0) { \
  65. *value = table[i].value; \
  66. return 0; /* success */ \
  67. } \
  68. } \
  69. return 1; /* failure */ \
  70. }
  71. /* Convert fc_port_type values to ascii string name */
  72. static struct {
  73. enum fc_port_type value;
  74. char *name;
  75. } fc_port_type_names[] = {
  76. { FC_PORTTYPE_UNKNOWN, "Unknown" },
  77. { FC_PORTTYPE_OTHER, "Other" },
  78. { FC_PORTTYPE_NOTPRESENT, "Not Present" },
  79. { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
  80. { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
  81. { FC_PORTTYPE_LPORT, "LPort (private loop)" },
  82. { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
  83. };
  84. fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
  85. #define FC_PORTTYPE_MAX_NAMELEN 50
  86. /* Convert fc_port_state values to ascii string name */
  87. static struct {
  88. enum fc_port_state value;
  89. char *name;
  90. } fc_port_state_names[] = {
  91. { FC_PORTSTATE_UNKNOWN, "Unknown" },
  92. { FC_PORTSTATE_NOTPRESENT, "Not Present" },
  93. { FC_PORTSTATE_ONLINE, "Online" },
  94. { FC_PORTSTATE_OFFLINE, "Offline" },
  95. { FC_PORTSTATE_BLOCKED, "Blocked" },
  96. { FC_PORTSTATE_BYPASSED, "Bypassed" },
  97. { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
  98. { FC_PORTSTATE_LINKDOWN, "Linkdown" },
  99. { FC_PORTSTATE_ERROR, "Error" },
  100. { FC_PORTSTATE_LOOPBACK, "Loopback" },
  101. };
  102. fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
  103. #define FC_PORTSTATE_MAX_NAMELEN 20
  104. /* Convert fc_tgtid_binding_type values to ascii string name */
  105. static struct {
  106. enum fc_tgtid_binding_type value;
  107. char *name;
  108. int matchlen;
  109. } fc_tgtid_binding_type_names[] = {
  110. { FC_TGTID_BIND_NONE, "none", 4 },
  111. { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
  112. { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
  113. { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
  114. };
  115. fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
  116. fc_tgtid_binding_type_names)
  117. fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
  118. fc_tgtid_binding_type_names)
  119. #define FC_BINDTYPE_MAX_NAMELEN 30
  120. #define fc_bitfield_name_search(title, table) \
  121. static ssize_t \
  122. get_fc_##title##_names(u32 table_key, char *buf) \
  123. { \
  124. char *prefix = ""; \
  125. ssize_t len = 0; \
  126. int i; \
  127. \
  128. for (i = 0; i < sizeof(table)/sizeof(table[0]); i++) { \
  129. if (table[i].value & table_key) { \
  130. len += sprintf(buf + len, "%s%s", \
  131. prefix, table[i].name); \
  132. prefix = ", "; \
  133. } \
  134. } \
  135. len += sprintf(buf + len, "\n"); \
  136. return len; \
  137. }
  138. /* Convert FC_COS bit values to ascii string name */
  139. static struct {
  140. u32 value;
  141. char *name;
  142. } fc_cos_names[] = {
  143. { FC_COS_CLASS1, "Class 1" },
  144. { FC_COS_CLASS2, "Class 2" },
  145. { FC_COS_CLASS3, "Class 3" },
  146. { FC_COS_CLASS4, "Class 4" },
  147. { FC_COS_CLASS6, "Class 6" },
  148. };
  149. fc_bitfield_name_search(cos, fc_cos_names)
  150. /* Convert FC_PORTSPEED bit values to ascii string name */
  151. static struct {
  152. u32 value;
  153. char *name;
  154. } fc_port_speed_names[] = {
  155. { FC_PORTSPEED_1GBIT, "1 Gbit" },
  156. { FC_PORTSPEED_2GBIT, "2 Gbit" },
  157. { FC_PORTSPEED_4GBIT, "4 Gbit" },
  158. { FC_PORTSPEED_10GBIT, "10 Gbit" },
  159. { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
  160. };
  161. fc_bitfield_name_search(port_speed, fc_port_speed_names)
  162. static int
  163. show_fc_fc4s (char *buf, u8 *fc4_list)
  164. {
  165. int i, len=0;
  166. for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
  167. len += sprintf(buf + len , "0x%02x ", *fc4_list);
  168. len += sprintf(buf + len, "\n");
  169. return len;
  170. }
  171. /* Convert FC_RPORT_ROLE bit values to ascii string name */
  172. static struct {
  173. u32 value;
  174. char *name;
  175. } fc_remote_port_role_names[] = {
  176. { FC_RPORT_ROLE_FCP_TARGET, "FCP Target" },
  177. { FC_RPORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
  178. { FC_RPORT_ROLE_IP_PORT, "IP Port" },
  179. };
  180. fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
  181. /*
  182. * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
  183. */
  184. #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
  185. #define FC_WELLKNOWN_ROLE_MASK 0x00000f
  186. #define FC_FPORT_PORTID 0x00000e
  187. #define FC_FABCTLR_PORTID 0x00000d
  188. #define FC_DIRSRVR_PORTID 0x00000c
  189. #define FC_TIMESRVR_PORTID 0x00000b
  190. #define FC_MGMTSRVR_PORTID 0x00000a
  191. static void fc_timeout_blocked_rport(void *data);
  192. static void fc_scsi_scan_rport(void *data);
  193. static void fc_rport_terminate(struct fc_rport *rport);
  194. /*
  195. * Attribute counts pre object type...
  196. * Increase these values if you add attributes
  197. */
  198. #define FC_STARGET_NUM_ATTRS 3
  199. #define FC_RPORT_NUM_ATTRS 9
  200. #define FC_HOST_NUM_ATTRS 15
  201. struct fc_internal {
  202. struct scsi_transport_template t;
  203. struct fc_function_template *f;
  204. /*
  205. * For attributes : each object has :
  206. * An array of the actual attributes structures
  207. * An array of null-terminated pointers to the attribute
  208. * structures - used for mid-layer interaction.
  209. *
  210. * The attribute containers for the starget and host are are
  211. * part of the midlayer. As the remote port is specific to the
  212. * fc transport, we must provide the attribute container.
  213. */
  214. struct class_device_attribute private_starget_attrs[
  215. FC_STARGET_NUM_ATTRS];
  216. struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
  217. struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
  218. struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
  219. struct transport_container rport_attr_cont;
  220. struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
  221. struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
  222. };
  223. #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
  224. static int fc_target_setup(struct device *dev)
  225. {
  226. struct scsi_target *starget = to_scsi_target(dev);
  227. struct fc_rport *rport = starget_to_rport(starget);
  228. /*
  229. * if parent is remote port, use values from remote port.
  230. * Otherwise, this host uses the fc_transport, but not the
  231. * remote port interface. As such, initialize to known non-values.
  232. */
  233. if (rport) {
  234. fc_starget_node_name(starget) = rport->node_name;
  235. fc_starget_port_name(starget) = rport->port_name;
  236. fc_starget_port_id(starget) = rport->port_id;
  237. } else {
  238. fc_starget_node_name(starget) = -1;
  239. fc_starget_port_name(starget) = -1;
  240. fc_starget_port_id(starget) = -1;
  241. }
  242. return 0;
  243. }
  244. static DECLARE_TRANSPORT_CLASS(fc_transport_class,
  245. "fc_transport",
  246. fc_target_setup,
  247. NULL,
  248. NULL);
  249. static int fc_host_setup(struct device *dev)
  250. {
  251. struct Scsi_Host *shost = dev_to_shost(dev);
  252. /*
  253. * Set default values easily detected by the midlayer as
  254. * failure cases. The scsi lldd is responsible for initializing
  255. * all transport attributes to valid values per host.
  256. */
  257. fc_host_node_name(shost) = -1;
  258. fc_host_port_name(shost) = -1;
  259. fc_host_supported_classes(shost) = FC_COS_UNSPECIFIED;
  260. memset(fc_host_supported_fc4s(shost), 0,
  261. sizeof(fc_host_supported_fc4s(shost)));
  262. memset(fc_host_symbolic_name(shost), 0,
  263. sizeof(fc_host_symbolic_name(shost)));
  264. fc_host_supported_speeds(shost) = FC_PORTSPEED_UNKNOWN;
  265. fc_host_maxframe_size(shost) = -1;
  266. memset(fc_host_serial_number(shost), 0,
  267. sizeof(fc_host_serial_number(shost)));
  268. fc_host_port_id(shost) = -1;
  269. fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
  270. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  271. memset(fc_host_active_fc4s(shost), 0,
  272. sizeof(fc_host_active_fc4s(shost)));
  273. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  274. fc_host_fabric_name(shost) = -1;
  275. fc_host_tgtid_bind_type(shost) = FC_TGTID_BIND_BY_WWPN;
  276. INIT_LIST_HEAD(&fc_host_rports(shost));
  277. INIT_LIST_HEAD(&fc_host_rport_bindings(shost));
  278. fc_host_next_rport_number(shost) = 0;
  279. fc_host_next_target_id(shost) = 0;
  280. return 0;
  281. }
  282. static DECLARE_TRANSPORT_CLASS(fc_host_class,
  283. "fc_host",
  284. fc_host_setup,
  285. NULL,
  286. NULL);
  287. /*
  288. * Setup and Remove actions for remote ports are handled
  289. * in the service functions below.
  290. */
  291. static DECLARE_TRANSPORT_CLASS(fc_rport_class,
  292. "fc_remote_ports",
  293. NULL,
  294. NULL,
  295. NULL);
  296. /*
  297. * Module Parameters
  298. */
  299. /*
  300. * dev_loss_tmo: the default number of seconds that the FC transport
  301. * should insulate the loss of a remote port.
  302. * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
  303. */
  304. static unsigned int fc_dev_loss_tmo = SCSI_DEVICE_BLOCK_MAX_TIMEOUT;
  305. module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
  306. MODULE_PARM_DESC(dev_loss_tmo,
  307. "Maximum number of seconds that the FC transport should"
  308. " insulate the loss of a remote port. Once this value is"
  309. " exceeded, the scsi target is removed. Value should be"
  310. " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
  311. static __init int fc_transport_init(void)
  312. {
  313. int error = transport_class_register(&fc_host_class);
  314. if (error)
  315. return error;
  316. error = transport_class_register(&fc_rport_class);
  317. if (error)
  318. return error;
  319. return transport_class_register(&fc_transport_class);
  320. }
  321. static void __exit fc_transport_exit(void)
  322. {
  323. transport_class_unregister(&fc_transport_class);
  324. transport_class_unregister(&fc_rport_class);
  325. transport_class_unregister(&fc_host_class);
  326. }
  327. /*
  328. * FC Remote Port Attribute Management
  329. */
  330. #define fc_rport_show_function(field, format_string, sz, cast) \
  331. static ssize_t \
  332. show_fc_rport_##field (struct class_device *cdev, char *buf) \
  333. { \
  334. struct fc_rport *rport = transport_class_to_rport(cdev); \
  335. struct Scsi_Host *shost = rport_to_shost(rport); \
  336. struct fc_internal *i = to_fc_internal(shost->transportt); \
  337. if (i->f->get_rport_##field) \
  338. i->f->get_rport_##field(rport); \
  339. return snprintf(buf, sz, format_string, cast rport->field); \
  340. }
  341. #define fc_rport_store_function(field) \
  342. static ssize_t \
  343. store_fc_rport_##field(struct class_device *cdev, const char *buf, \
  344. size_t count) \
  345. { \
  346. int val; \
  347. struct fc_rport *rport = transport_class_to_rport(cdev); \
  348. struct Scsi_Host *shost = rport_to_shost(rport); \
  349. struct fc_internal *i = to_fc_internal(shost->transportt); \
  350. val = simple_strtoul(buf, NULL, 0); \
  351. i->f->set_rport_##field(rport, val); \
  352. return count; \
  353. }
  354. #define fc_rport_rd_attr(field, format_string, sz) \
  355. fc_rport_show_function(field, format_string, sz, ) \
  356. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  357. show_fc_rport_##field, NULL)
  358. #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
  359. fc_rport_show_function(field, format_string, sz, (cast)) \
  360. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  361. show_fc_rport_##field, NULL)
  362. #define fc_rport_rw_attr(field, format_string, sz) \
  363. fc_rport_show_function(field, format_string, sz, ) \
  364. fc_rport_store_function(field) \
  365. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
  366. show_fc_rport_##field, \
  367. store_fc_rport_##field)
  368. #define fc_private_rport_show_function(field, format_string, sz, cast) \
  369. static ssize_t \
  370. show_fc_rport_##field (struct class_device *cdev, char *buf) \
  371. { \
  372. struct fc_rport *rport = transport_class_to_rport(cdev); \
  373. return snprintf(buf, sz, format_string, cast rport->field); \
  374. }
  375. #define fc_private_rport_rd_attr(field, format_string, sz) \
  376. fc_private_rport_show_function(field, format_string, sz, ) \
  377. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  378. show_fc_rport_##field, NULL)
  379. #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
  380. fc_private_rport_show_function(field, format_string, sz, (cast)) \
  381. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  382. show_fc_rport_##field, NULL)
  383. #define fc_private_rport_rd_enum_attr(title, maxlen) \
  384. static ssize_t \
  385. show_fc_rport_##title (struct class_device *cdev, char *buf) \
  386. { \
  387. struct fc_rport *rport = transport_class_to_rport(cdev); \
  388. const char *name; \
  389. name = get_fc_##title##_name(rport->title); \
  390. if (!name) \
  391. return -EINVAL; \
  392. return snprintf(buf, maxlen, "%s\n", name); \
  393. } \
  394. static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
  395. show_fc_rport_##title, NULL)
  396. #define SETUP_RPORT_ATTRIBUTE_RD(field) \
  397. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  398. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  399. i->private_rport_attrs[count].store = NULL; \
  400. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  401. if (i->f->show_rport_##field) \
  402. count++
  403. #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
  404. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  405. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  406. i->private_rport_attrs[count].store = NULL; \
  407. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  408. count++
  409. #define SETUP_RPORT_ATTRIBUTE_RW(field) \
  410. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  411. if (!i->f->set_rport_##field) { \
  412. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  413. i->private_rport_attrs[count].store = NULL; \
  414. } \
  415. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  416. if (i->f->show_rport_##field) \
  417. count++
  418. /* The FC Transport Remote Port Attributes: */
  419. /* Fixed Remote Port Attributes */
  420. fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
  421. static ssize_t
  422. show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
  423. {
  424. struct fc_rport *rport = transport_class_to_rport(cdev);
  425. if (rport->supported_classes == FC_COS_UNSPECIFIED)
  426. return snprintf(buf, 20, "unspecified\n");
  427. return get_fc_cos_names(rport->supported_classes, buf);
  428. }
  429. static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
  430. show_fc_rport_supported_classes, NULL);
  431. /* Dynamic Remote Port Attributes */
  432. fc_rport_rw_attr(dev_loss_tmo, "%d\n", 20);
  433. /* Private Remote Port Attributes */
  434. fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  435. fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  436. fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
  437. static ssize_t
  438. show_fc_rport_roles (struct class_device *cdev, char *buf)
  439. {
  440. struct fc_rport *rport = transport_class_to_rport(cdev);
  441. /* identify any roles that are port_id specific */
  442. if ((rport->port_id != -1) &&
  443. (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
  444. FC_WELLKNOWN_PORTID_MASK) {
  445. switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
  446. case FC_FPORT_PORTID:
  447. return snprintf(buf, 30, "Fabric Port\n");
  448. case FC_FABCTLR_PORTID:
  449. return snprintf(buf, 30, "Fabric Controller\n");
  450. case FC_DIRSRVR_PORTID:
  451. return snprintf(buf, 30, "Directory Server\n");
  452. case FC_TIMESRVR_PORTID:
  453. return snprintf(buf, 30, "Time Server\n");
  454. case FC_MGMTSRVR_PORTID:
  455. return snprintf(buf, 30, "Management Server\n");
  456. default:
  457. return snprintf(buf, 30, "Unknown Fabric Entity\n");
  458. }
  459. } else {
  460. if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
  461. return snprintf(buf, 20, "unknown\n");
  462. return get_fc_remote_port_roles_names(rport->roles, buf);
  463. }
  464. }
  465. static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
  466. show_fc_rport_roles, NULL);
  467. fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  468. fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
  469. /*
  470. * FC SCSI Target Attribute Management
  471. */
  472. /*
  473. * Note: in the target show function we recognize when the remote
  474. * port is in the heirarchy and do not allow the driver to get
  475. * involved in sysfs functions. The driver only gets involved if
  476. * it's the "old" style that doesn't use rports.
  477. */
  478. #define fc_starget_show_function(field, format_string, sz, cast) \
  479. static ssize_t \
  480. show_fc_starget_##field (struct class_device *cdev, char *buf) \
  481. { \
  482. struct scsi_target *starget = transport_class_to_starget(cdev); \
  483. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  484. struct fc_internal *i = to_fc_internal(shost->transportt); \
  485. struct fc_rport *rport = starget_to_rport(starget); \
  486. if (rport) \
  487. fc_starget_##field(starget) = rport->field; \
  488. else if (i->f->get_starget_##field) \
  489. i->f->get_starget_##field(starget); \
  490. return snprintf(buf, sz, format_string, \
  491. cast fc_starget_##field(starget)); \
  492. }
  493. #define fc_starget_rd_attr(field, format_string, sz) \
  494. fc_starget_show_function(field, format_string, sz, ) \
  495. static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
  496. show_fc_starget_##field, NULL)
  497. #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
  498. fc_starget_show_function(field, format_string, sz, (cast)) \
  499. static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
  500. show_fc_starget_##field, NULL)
  501. #define SETUP_STARGET_ATTRIBUTE_RD(field) \
  502. i->private_starget_attrs[count] = class_device_attr_starget_##field; \
  503. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  504. i->private_starget_attrs[count].store = NULL; \
  505. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  506. if (i->f->show_starget_##field) \
  507. count++
  508. #define SETUP_STARGET_ATTRIBUTE_RW(field) \
  509. i->private_starget_attrs[count] = class_device_attr_starget_##field; \
  510. if (!i->f->set_starget_##field) { \
  511. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  512. i->private_starget_attrs[count].store = NULL; \
  513. } \
  514. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  515. if (i->f->show_starget_##field) \
  516. count++
  517. /* The FC Transport SCSI Target Attributes: */
  518. fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  519. fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  520. fc_starget_rd_attr(port_id, "0x%06x\n", 20);
  521. /*
  522. * Host Attribute Management
  523. */
  524. #define fc_host_show_function(field, format_string, sz, cast) \
  525. static ssize_t \
  526. show_fc_host_##field (struct class_device *cdev, char *buf) \
  527. { \
  528. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  529. struct fc_internal *i = to_fc_internal(shost->transportt); \
  530. if (i->f->get_host_##field) \
  531. i->f->get_host_##field(shost); \
  532. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  533. }
  534. #define fc_host_store_function(field) \
  535. static ssize_t \
  536. store_fc_host_##field(struct class_device *cdev, const char *buf, \
  537. size_t count) \
  538. { \
  539. int val; \
  540. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  541. struct fc_internal *i = to_fc_internal(shost->transportt); \
  542. \
  543. val = simple_strtoul(buf, NULL, 0); \
  544. i->f->set_host_##field(shost, val); \
  545. return count; \
  546. }
  547. #define fc_host_rd_attr(field, format_string, sz) \
  548. fc_host_show_function(field, format_string, sz, ) \
  549. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  550. show_fc_host_##field, NULL)
  551. #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
  552. fc_host_show_function(field, format_string, sz, (cast)) \
  553. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  554. show_fc_host_##field, NULL)
  555. #define fc_host_rw_attr(field, format_string, sz) \
  556. fc_host_show_function(field, format_string, sz, ) \
  557. fc_host_store_function(field) \
  558. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
  559. show_fc_host_##field, \
  560. store_fc_host_##field)
  561. #define fc_host_rd_enum_attr(title, maxlen) \
  562. static ssize_t \
  563. show_fc_host_##title (struct class_device *cdev, char *buf) \
  564. { \
  565. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  566. struct fc_internal *i = to_fc_internal(shost->transportt); \
  567. const char *name; \
  568. if (i->f->get_host_##title) \
  569. i->f->get_host_##title(shost); \
  570. name = get_fc_##title##_name(fc_host_##title(shost)); \
  571. if (!name) \
  572. return -EINVAL; \
  573. return snprintf(buf, maxlen, "%s\n", name); \
  574. } \
  575. static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
  576. #define SETUP_HOST_ATTRIBUTE_RD(field) \
  577. i->private_host_attrs[count] = class_device_attr_host_##field; \
  578. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  579. i->private_host_attrs[count].store = NULL; \
  580. i->host_attrs[count] = &i->private_host_attrs[count]; \
  581. if (i->f->show_host_##field) \
  582. count++
  583. #define SETUP_HOST_ATTRIBUTE_RW(field) \
  584. i->private_host_attrs[count] = class_device_attr_host_##field; \
  585. if (!i->f->set_host_##field) { \
  586. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  587. i->private_host_attrs[count].store = NULL; \
  588. } \
  589. i->host_attrs[count] = &i->private_host_attrs[count]; \
  590. if (i->f->show_host_##field) \
  591. count++
  592. #define fc_private_host_show_function(field, format_string, sz, cast) \
  593. static ssize_t \
  594. show_fc_host_##field (struct class_device *cdev, char *buf) \
  595. { \
  596. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  597. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  598. }
  599. #define fc_private_host_rd_attr(field, format_string, sz) \
  600. fc_private_host_show_function(field, format_string, sz, ) \
  601. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  602. show_fc_host_##field, NULL)
  603. #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
  604. fc_private_host_show_function(field, format_string, sz, (cast)) \
  605. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  606. show_fc_host_##field, NULL)
  607. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
  608. i->private_host_attrs[count] = class_device_attr_host_##field; \
  609. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  610. i->private_host_attrs[count].store = NULL; \
  611. i->host_attrs[count] = &i->private_host_attrs[count]; \
  612. count++
  613. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
  614. i->private_host_attrs[count] = class_device_attr_host_##field; \
  615. i->host_attrs[count] = &i->private_host_attrs[count]; \
  616. count++
  617. /* Fixed Host Attributes */
  618. static ssize_t
  619. show_fc_host_supported_classes (struct class_device *cdev, char *buf)
  620. {
  621. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  622. if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
  623. return snprintf(buf, 20, "unspecified\n");
  624. return get_fc_cos_names(fc_host_supported_classes(shost), buf);
  625. }
  626. static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
  627. show_fc_host_supported_classes, NULL);
  628. static ssize_t
  629. show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
  630. {
  631. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  632. return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
  633. }
  634. static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
  635. show_fc_host_supported_fc4s, NULL);
  636. static ssize_t
  637. show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
  638. {
  639. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  640. if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
  641. return snprintf(buf, 20, "unknown\n");
  642. return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
  643. }
  644. static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
  645. show_fc_host_supported_speeds, NULL);
  646. fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  647. fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  648. fc_private_host_rd_attr(symbolic_name, "%s\n", (FC_SYMBOLIC_NAME_SIZE +1));
  649. fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
  650. fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
  651. /* Dynamic Host Attributes */
  652. static ssize_t
  653. show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
  654. {
  655. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  656. struct fc_internal *i = to_fc_internal(shost->transportt);
  657. if (i->f->get_host_active_fc4s)
  658. i->f->get_host_active_fc4s(shost);
  659. return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
  660. }
  661. static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
  662. show_fc_host_active_fc4s, NULL);
  663. static ssize_t
  664. show_fc_host_speed (struct class_device *cdev, char *buf)
  665. {
  666. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  667. struct fc_internal *i = to_fc_internal(shost->transportt);
  668. if (i->f->get_host_speed)
  669. i->f->get_host_speed(shost);
  670. if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
  671. return snprintf(buf, 20, "unknown\n");
  672. return get_fc_port_speed_names(fc_host_speed(shost), buf);
  673. }
  674. static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
  675. show_fc_host_speed, NULL);
  676. fc_host_rd_attr(port_id, "0x%06x\n", 20);
  677. fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
  678. fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  679. fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
  680. /* Private Host Attributes */
  681. static ssize_t
  682. show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
  683. {
  684. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  685. const char *name;
  686. name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
  687. if (!name)
  688. return -EINVAL;
  689. return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
  690. }
  691. static ssize_t
  692. store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
  693. const char *buf, size_t count)
  694. {
  695. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  696. struct fc_rport *rport, *next_rport;
  697. enum fc_tgtid_binding_type val;
  698. unsigned long flags;
  699. if (get_fc_tgtid_bind_type_match(buf, &val))
  700. return -EINVAL;
  701. /* if changing bind type, purge all unused consistent bindings */
  702. if (val != fc_host_tgtid_bind_type(shost)) {
  703. spin_lock_irqsave(shost->host_lock, flags);
  704. list_for_each_entry_safe(rport, next_rport,
  705. &fc_host_rport_bindings(shost), peers)
  706. fc_rport_terminate(rport);
  707. spin_unlock_irqrestore(shost->host_lock, flags);
  708. }
  709. fc_host_tgtid_bind_type(shost) = val;
  710. return count;
  711. }
  712. static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
  713. show_fc_private_host_tgtid_bind_type,
  714. store_fc_private_host_tgtid_bind_type);
  715. /*
  716. * Host Statistics Management
  717. */
  718. /* Show a given an attribute in the statistics group */
  719. static ssize_t
  720. fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
  721. {
  722. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  723. struct fc_internal *i = to_fc_internal(shost->transportt);
  724. struct fc_host_statistics *stats;
  725. ssize_t ret = -ENOENT;
  726. if (offset > sizeof(struct fc_host_statistics) ||
  727. offset % sizeof(u64) != 0)
  728. WARN_ON(1);
  729. if (i->f->get_fc_host_stats) {
  730. stats = (i->f->get_fc_host_stats)(shost);
  731. if (stats)
  732. ret = snprintf(buf, 20, "0x%llx\n",
  733. (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
  734. }
  735. return ret;
  736. }
  737. /* generate a read-only statistics attribute */
  738. #define fc_host_statistic(name) \
  739. static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
  740. { \
  741. return fc_stat_show(cd, buf, \
  742. offsetof(struct fc_host_statistics, name)); \
  743. } \
  744. static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
  745. fc_host_statistic(seconds_since_last_reset);
  746. fc_host_statistic(tx_frames);
  747. fc_host_statistic(tx_words);
  748. fc_host_statistic(rx_frames);
  749. fc_host_statistic(rx_words);
  750. fc_host_statistic(lip_count);
  751. fc_host_statistic(nos_count);
  752. fc_host_statistic(error_frames);
  753. fc_host_statistic(dumped_frames);
  754. fc_host_statistic(link_failure_count);
  755. fc_host_statistic(loss_of_sync_count);
  756. fc_host_statistic(loss_of_signal_count);
  757. fc_host_statistic(prim_seq_protocol_err_count);
  758. fc_host_statistic(invalid_tx_word_count);
  759. fc_host_statistic(invalid_crc_count);
  760. fc_host_statistic(fcp_input_requests);
  761. fc_host_statistic(fcp_output_requests);
  762. fc_host_statistic(fcp_control_requests);
  763. fc_host_statistic(fcp_input_megabytes);
  764. fc_host_statistic(fcp_output_megabytes);
  765. static ssize_t
  766. fc_reset_statistics(struct class_device *cdev, const char *buf,
  767. size_t count)
  768. {
  769. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  770. struct fc_internal *i = to_fc_internal(shost->transportt);
  771. /* ignore any data value written to the attribute */
  772. if (i->f->reset_fc_host_stats) {
  773. i->f->reset_fc_host_stats(shost);
  774. return count;
  775. }
  776. return -ENOENT;
  777. }
  778. static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
  779. fc_reset_statistics);
  780. static struct attribute *fc_statistics_attrs[] = {
  781. &class_device_attr_host_seconds_since_last_reset.attr,
  782. &class_device_attr_host_tx_frames.attr,
  783. &class_device_attr_host_tx_words.attr,
  784. &class_device_attr_host_rx_frames.attr,
  785. &class_device_attr_host_rx_words.attr,
  786. &class_device_attr_host_lip_count.attr,
  787. &class_device_attr_host_nos_count.attr,
  788. &class_device_attr_host_error_frames.attr,
  789. &class_device_attr_host_dumped_frames.attr,
  790. &class_device_attr_host_link_failure_count.attr,
  791. &class_device_attr_host_loss_of_sync_count.attr,
  792. &class_device_attr_host_loss_of_signal_count.attr,
  793. &class_device_attr_host_prim_seq_protocol_err_count.attr,
  794. &class_device_attr_host_invalid_tx_word_count.attr,
  795. &class_device_attr_host_invalid_crc_count.attr,
  796. &class_device_attr_host_fcp_input_requests.attr,
  797. &class_device_attr_host_fcp_output_requests.attr,
  798. &class_device_attr_host_fcp_control_requests.attr,
  799. &class_device_attr_host_fcp_input_megabytes.attr,
  800. &class_device_attr_host_fcp_output_megabytes.attr,
  801. &class_device_attr_host_reset_statistics.attr,
  802. NULL
  803. };
  804. static struct attribute_group fc_statistics_group = {
  805. .name = "statistics",
  806. .attrs = fc_statistics_attrs,
  807. };
  808. static int fc_host_match(struct attribute_container *cont,
  809. struct device *dev)
  810. {
  811. struct Scsi_Host *shost;
  812. struct fc_internal *i;
  813. if (!scsi_is_host_device(dev))
  814. return 0;
  815. shost = dev_to_shost(dev);
  816. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  817. != &fc_host_class.class)
  818. return 0;
  819. i = to_fc_internal(shost->transportt);
  820. return &i->t.host_attrs.ac == cont;
  821. }
  822. static int fc_target_match(struct attribute_container *cont,
  823. struct device *dev)
  824. {
  825. struct Scsi_Host *shost;
  826. struct fc_internal *i;
  827. if (!scsi_is_target_device(dev))
  828. return 0;
  829. shost = dev_to_shost(dev->parent);
  830. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  831. != &fc_host_class.class)
  832. return 0;
  833. i = to_fc_internal(shost->transportt);
  834. return &i->t.target_attrs.ac == cont;
  835. }
  836. static void fc_rport_dev_release(struct device *dev)
  837. {
  838. struct fc_rport *rport = dev_to_rport(dev);
  839. put_device(dev->parent);
  840. kfree(rport);
  841. }
  842. int scsi_is_fc_rport(const struct device *dev)
  843. {
  844. return dev->release == fc_rport_dev_release;
  845. }
  846. EXPORT_SYMBOL(scsi_is_fc_rport);
  847. static int fc_rport_match(struct attribute_container *cont,
  848. struct device *dev)
  849. {
  850. struct Scsi_Host *shost;
  851. struct fc_internal *i;
  852. if (!scsi_is_fc_rport(dev))
  853. return 0;
  854. shost = dev_to_shost(dev->parent);
  855. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  856. != &fc_host_class.class)
  857. return 0;
  858. i = to_fc_internal(shost->transportt);
  859. return &i->rport_attr_cont.ac == cont;
  860. }
  861. struct scsi_transport_template *
  862. fc_attach_transport(struct fc_function_template *ft)
  863. {
  864. struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
  865. GFP_KERNEL);
  866. int count;
  867. if (unlikely(!i))
  868. return NULL;
  869. memset(i, 0, sizeof(struct fc_internal));
  870. i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
  871. i->t.target_attrs.ac.class = &fc_transport_class.class;
  872. i->t.target_attrs.ac.match = fc_target_match;
  873. i->t.target_size = sizeof(struct fc_starget_attrs);
  874. transport_container_register(&i->t.target_attrs);
  875. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  876. i->t.host_attrs.ac.class = &fc_host_class.class;
  877. i->t.host_attrs.ac.match = fc_host_match;
  878. i->t.host_size = sizeof(struct fc_host_attrs);
  879. if (ft->get_fc_host_stats)
  880. i->t.host_attrs.statistics = &fc_statistics_group;
  881. transport_container_register(&i->t.host_attrs);
  882. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  883. i->rport_attr_cont.ac.class = &fc_rport_class.class;
  884. i->rport_attr_cont.ac.match = fc_rport_match;
  885. transport_container_register(&i->rport_attr_cont);
  886. i->f = ft;
  887. /* Transport uses the shost workq for scsi scanning */
  888. i->t.create_work_queue = 1;
  889. /*
  890. * Setup SCSI Target Attributes.
  891. */
  892. count = 0;
  893. SETUP_STARGET_ATTRIBUTE_RD(node_name);
  894. SETUP_STARGET_ATTRIBUTE_RD(port_name);
  895. SETUP_STARGET_ATTRIBUTE_RD(port_id);
  896. BUG_ON(count > FC_STARGET_NUM_ATTRS);
  897. i->starget_attrs[count] = NULL;
  898. /*
  899. * Setup SCSI Host Attributes.
  900. */
  901. count=0;
  902. SETUP_HOST_ATTRIBUTE_RD(node_name);
  903. SETUP_HOST_ATTRIBUTE_RD(port_name);
  904. SETUP_HOST_ATTRIBUTE_RD(supported_classes);
  905. SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
  906. SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
  907. SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
  908. SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
  909. SETUP_HOST_ATTRIBUTE_RD(serial_number);
  910. SETUP_HOST_ATTRIBUTE_RD(port_id);
  911. SETUP_HOST_ATTRIBUTE_RD(port_type);
  912. SETUP_HOST_ATTRIBUTE_RD(port_state);
  913. SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
  914. SETUP_HOST_ATTRIBUTE_RD(speed);
  915. SETUP_HOST_ATTRIBUTE_RD(fabric_name);
  916. /* Transport-managed attributes */
  917. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
  918. BUG_ON(count > FC_HOST_NUM_ATTRS);
  919. i->host_attrs[count] = NULL;
  920. /*
  921. * Setup Remote Port Attributes.
  922. */
  923. count=0;
  924. SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
  925. SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
  926. SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
  927. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
  928. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
  929. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
  930. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
  931. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
  932. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
  933. BUG_ON(count > FC_RPORT_NUM_ATTRS);
  934. i->rport_attrs[count] = NULL;
  935. return &i->t;
  936. }
  937. EXPORT_SYMBOL(fc_attach_transport);
  938. void fc_release_transport(struct scsi_transport_template *t)
  939. {
  940. struct fc_internal *i = to_fc_internal(t);
  941. transport_container_unregister(&i->t.target_attrs);
  942. transport_container_unregister(&i->t.host_attrs);
  943. transport_container_unregister(&i->rport_attr_cont);
  944. kfree(i);
  945. }
  946. EXPORT_SYMBOL(fc_release_transport);
  947. /**
  948. * fc_remove_host - called to terminate any fc_transport-related elements
  949. * for a scsi host.
  950. * @rport: remote port to be unblocked.
  951. *
  952. * This routine is expected to be called immediately preceeding the
  953. * a driver's call to scsi_remove_host().
  954. *
  955. * WARNING: A driver utilizing the fc_transport, which fails to call
  956. * this routine prior to scsi_remote_host(), will leave dangling
  957. * objects in /sys/class/fc_remote_ports. Access to any of these
  958. * objects can result in a system crash !!!
  959. *
  960. * Notes:
  961. * This routine assumes no locks are held on entry.
  962. **/
  963. void
  964. fc_remove_host(struct Scsi_Host *shost)
  965. {
  966. struct fc_rport *rport, *next_rport;
  967. /* Remove any remote ports */
  968. list_for_each_entry_safe(rport, next_rport,
  969. &fc_host_rports(shost), peers)
  970. fc_rport_terminate(rport);
  971. list_for_each_entry_safe(rport, next_rport,
  972. &fc_host_rport_bindings(shost), peers)
  973. fc_rport_terminate(rport);
  974. }
  975. EXPORT_SYMBOL(fc_remove_host);
  976. /**
  977. * fc_rport_create - allocates and creates a remote FC port.
  978. * @shost: scsi host the remote port is connected to.
  979. * @channel: Channel on shost port connected to.
  980. * @ids: The world wide names, fc address, and FC4 port
  981. * roles for the remote port.
  982. *
  983. * Allocates and creates the remoter port structure, including the
  984. * class and sysfs creation.
  985. *
  986. * Notes:
  987. * This routine assumes no locks are held on entry.
  988. **/
  989. struct fc_rport *
  990. fc_rport_create(struct Scsi_Host *shost, int channel,
  991. struct fc_rport_identifiers *ids)
  992. {
  993. struct fc_host_attrs *fc_host =
  994. (struct fc_host_attrs *)shost->shost_data;
  995. struct fc_internal *fci = to_fc_internal(shost->transportt);
  996. struct fc_rport *rport;
  997. struct device *dev;
  998. unsigned long flags;
  999. int error;
  1000. size_t size;
  1001. size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
  1002. rport = kmalloc(size, GFP_KERNEL);
  1003. if (unlikely(!rport)) {
  1004. printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
  1005. return NULL;
  1006. }
  1007. memset(rport, 0, size);
  1008. rport->maxframe_size = -1;
  1009. rport->supported_classes = FC_COS_UNSPECIFIED;
  1010. rport->dev_loss_tmo = fc_dev_loss_tmo;
  1011. memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
  1012. memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
  1013. rport->port_id = ids->port_id;
  1014. rport->roles = ids->roles;
  1015. rport->port_state = FC_PORTSTATE_ONLINE;
  1016. if (fci->f->dd_fcrport_size)
  1017. rport->dd_data = &rport[1];
  1018. rport->channel = channel;
  1019. INIT_WORK(&rport->dev_loss_work, fc_timeout_blocked_rport, rport);
  1020. INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
  1021. spin_lock_irqsave(shost->host_lock, flags);
  1022. rport->number = fc_host->next_rport_number++;
  1023. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  1024. rport->scsi_target_id = fc_host->next_target_id++;
  1025. else
  1026. rport->scsi_target_id = -1;
  1027. list_add_tail(&rport->peers, &fc_host_rports(shost));
  1028. get_device(&shost->shost_gendev);
  1029. spin_unlock_irqrestore(shost->host_lock, flags);
  1030. dev = &rport->dev;
  1031. device_initialize(dev);
  1032. dev->parent = get_device(&shost->shost_gendev);
  1033. dev->release = fc_rport_dev_release;
  1034. sprintf(dev->bus_id, "rport-%d:%d-%d",
  1035. shost->host_no, channel, rport->number);
  1036. transport_setup_device(dev);
  1037. error = device_add(dev);
  1038. if (error) {
  1039. printk(KERN_ERR "FC Remote Port device_add failed\n");
  1040. goto delete_rport;
  1041. }
  1042. transport_add_device(dev);
  1043. transport_configure_device(dev);
  1044. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  1045. /* initiate a scan of the target */
  1046. scsi_queue_work(shost, &rport->scan_work);
  1047. return rport;
  1048. delete_rport:
  1049. transport_destroy_device(dev);
  1050. put_device(dev->parent);
  1051. spin_lock_irqsave(shost->host_lock, flags);
  1052. list_del(&rport->peers);
  1053. put_device(&shost->shost_gendev);
  1054. spin_unlock_irqrestore(shost->host_lock, flags);
  1055. put_device(dev->parent);
  1056. kfree(rport);
  1057. return NULL;
  1058. }
  1059. /**
  1060. * fc_remote_port_add - notifies the fc transport of the existence
  1061. * of a remote FC port.
  1062. * @shost: scsi host the remote port is connected to.
  1063. * @channel: Channel on shost port connected to.
  1064. * @ids: The world wide names, fc address, and FC4 port
  1065. * roles for the remote port.
  1066. *
  1067. * The LLDD calls this routine to notify the transport of the existence
  1068. * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
  1069. * of the port, it's FC address (port_id), and the FC4 roles that are
  1070. * active for the port.
  1071. *
  1072. * For ports that are FCP targets (aka scsi targets), the FC transport
  1073. * maintains consistent target id bindings on behalf of the LLDD.
  1074. * A consistent target id binding is an assignment of a target id to
  1075. * a remote port identifier, which persists while the scsi host is
  1076. * attached. The remote port can disappear, then later reappear, and
  1077. * it's target id assignment remains the same. This allows for shifts
  1078. * in FC addressing (if binding by wwpn or wwnn) with no apparent
  1079. * changes to the scsi subsystem which is based on scsi host number and
  1080. * target id values. Bindings are only valid during the attachment of
  1081. * the scsi host. If the host detaches, then later re-attaches, target
  1082. * id bindings may change.
  1083. *
  1084. * This routine is responsible for returning a remote port structure.
  1085. * The routine will search the list of remote ports it maintains
  1086. * internally on behalf of consistent target id mappings. If found, the
  1087. * remote port structure will be reused. Otherwise, a new remote port
  1088. * structure will be allocated.
  1089. *
  1090. * Whenever a remote port is allocated, a new fc_remote_port class
  1091. * device is created.
  1092. *
  1093. * Should not be called from interrupt context.
  1094. *
  1095. * Notes:
  1096. * This routine assumes no locks are held on entry.
  1097. **/
  1098. struct fc_rport *
  1099. fc_remote_port_add(struct Scsi_Host *shost, int channel,
  1100. struct fc_rport_identifiers *ids)
  1101. {
  1102. struct fc_rport *rport;
  1103. unsigned long flags;
  1104. int match = 0;
  1105. if (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
  1106. (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
  1107. /* search for a matching consistent binding */
  1108. spin_lock_irqsave(shost->host_lock, flags);
  1109. list_for_each_entry(rport, &fc_host_rport_bindings(shost),
  1110. peers) {
  1111. if (rport->channel != channel)
  1112. continue;
  1113. switch (fc_host_tgtid_bind_type(shost)) {
  1114. case FC_TGTID_BIND_BY_WWPN:
  1115. if (rport->port_name == ids->port_name)
  1116. match = 1;
  1117. break;
  1118. case FC_TGTID_BIND_BY_WWNN:
  1119. if (rport->node_name == ids->node_name)
  1120. match = 1;
  1121. break;
  1122. case FC_TGTID_BIND_BY_ID:
  1123. if (rport->port_id == ids->port_id)
  1124. match = 1;
  1125. break;
  1126. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  1127. break;
  1128. }
  1129. if (match) {
  1130. list_move_tail(&rport->peers,
  1131. &fc_host_rports(shost));
  1132. break;
  1133. }
  1134. }
  1135. spin_unlock_irqrestore(shost->host_lock, flags);
  1136. if (match) {
  1137. memcpy(&rport->node_name, &ids->node_name,
  1138. sizeof(rport->node_name));
  1139. memcpy(&rport->port_name, &ids->port_name,
  1140. sizeof(rport->port_name));
  1141. rport->port_id = ids->port_id;
  1142. rport->roles = ids->roles;
  1143. rport->port_state = FC_PORTSTATE_ONLINE;
  1144. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  1145. /* initiate a scan of the target */
  1146. scsi_queue_work(shost, &rport->scan_work);
  1147. return rport;
  1148. }
  1149. }
  1150. /* No consistent binding found - create new remote port entry */
  1151. rport = fc_rport_create(shost, channel, ids);
  1152. return rport;
  1153. }
  1154. EXPORT_SYMBOL(fc_remote_port_add);
  1155. /*
  1156. * fc_rport_tgt_remove - Removes the scsi target on the remote port
  1157. * @rport: The remote port to be operated on
  1158. */
  1159. static void
  1160. fc_rport_tgt_remove(struct fc_rport *rport)
  1161. {
  1162. struct Scsi_Host *shost = rport_to_shost(rport);
  1163. scsi_target_unblock(&rport->dev);
  1164. /* Stop anything on the workq */
  1165. if (!cancel_delayed_work(&rport->dev_loss_work))
  1166. flush_scheduled_work();
  1167. scsi_flush_work(shost);
  1168. scsi_remove_target(&rport->dev);
  1169. }
  1170. /*
  1171. * fc_rport_terminate - this routine tears down and deallocates a remote port.
  1172. * @rport: The remote port to be terminated
  1173. *
  1174. * Notes:
  1175. * This routine assumes no locks are held on entry.
  1176. */
  1177. static void
  1178. fc_rport_terminate(struct fc_rport *rport)
  1179. {
  1180. struct Scsi_Host *shost = rport_to_shost(rport);
  1181. struct device *dev = &rport->dev;
  1182. unsigned long flags;
  1183. fc_rport_tgt_remove(rport);
  1184. transport_remove_device(dev);
  1185. device_del(dev);
  1186. transport_destroy_device(dev);
  1187. spin_lock_irqsave(shost->host_lock, flags);
  1188. list_del(&rport->peers);
  1189. spin_unlock_irqrestore(shost->host_lock, flags);
  1190. put_device(&shost->shost_gendev);
  1191. }
  1192. /**
  1193. * fc_remote_port_delete - notifies the fc transport that a remote
  1194. * port is no longer in existence.
  1195. * @rport: The remote port that no longer exists
  1196. *
  1197. * The LLDD calls this routine to notify the transport that a remote
  1198. * port is no longer part of the topology. Note: Although a port
  1199. * may no longer be part of the topology, it may persist in the remote
  1200. * ports displayed by the fc_host. This is done so that target id
  1201. * mappings (managed via the remote port structures), are always visible
  1202. * as long as the mapping is valid, regardless of port state,
  1203. *
  1204. * If the remote port is not an FCP Target, it will be fully torn down
  1205. * and deallocated, including the fc_remote_port class device.
  1206. *
  1207. * If the remote port is an FCP Target, the port structure will be
  1208. * marked as Not Present, but will remain as long as there is a valid
  1209. * SCSI target id mapping associated with the port structure. Validity
  1210. * is determined by the binding type. If binding by wwpn, then the port
  1211. * structure is always valid and will not be deallocated until the host
  1212. * is removed. If binding by wwnn, then the port structure is valid
  1213. * until another port with the same node name is found in the topology.
  1214. * If binding by port id (fc address), then the port structure is valid
  1215. * valid until another port with the same address is identified.
  1216. *
  1217. * Called from interrupt or normal process context.
  1218. *
  1219. * Notes:
  1220. * This routine assumes no locks are held on entry.
  1221. **/
  1222. void
  1223. fc_remote_port_delete(struct fc_rport *rport)
  1224. {
  1225. struct Scsi_Host *shost = rport_to_shost(rport);
  1226. unsigned long flags;
  1227. /* If no scsi target id mapping or consistent binding type, delete it */
  1228. if ((rport->scsi_target_id == -1) ||
  1229. (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE)) {
  1230. fc_rport_terminate(rport);
  1231. return;
  1232. }
  1233. fc_rport_tgt_remove(rport);
  1234. spin_lock_irqsave(shost->host_lock, flags);
  1235. list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
  1236. spin_unlock_irqrestore(shost->host_lock, flags);
  1237. /*
  1238. * Note: We do not remove or clear the hostdata area. This allows
  1239. * host-specific target data to persist along with the
  1240. * scsi_target_id. It's up to the host to manage it's hostdata area.
  1241. */
  1242. /*
  1243. * Reinitialize port attributes that may change if the port comes back.
  1244. */
  1245. rport->maxframe_size = -1;
  1246. rport->supported_classes = FC_COS_UNSPECIFIED;
  1247. rport->roles = FC_RPORT_ROLE_UNKNOWN;
  1248. rport->port_state = FC_PORTSTATE_NOTPRESENT;
  1249. /* remove the identifiers that aren't used in the consisting binding */
  1250. switch (fc_host_tgtid_bind_type(shost)) {
  1251. case FC_TGTID_BIND_BY_WWPN:
  1252. rport->node_name = -1;
  1253. rport->port_id = -1;
  1254. break;
  1255. case FC_TGTID_BIND_BY_WWNN:
  1256. rport->port_name = -1;
  1257. rport->port_id = -1;
  1258. break;
  1259. case FC_TGTID_BIND_BY_ID:
  1260. rport->node_name = -1;
  1261. rport->port_name = -1;
  1262. break;
  1263. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  1264. break;
  1265. }
  1266. }
  1267. EXPORT_SYMBOL(fc_remote_port_delete);
  1268. /**
  1269. * fc_remote_port_rolechg - notifies the fc transport that the roles
  1270. * on a remote may have changed.
  1271. * @rport: The remote port that changed.
  1272. *
  1273. * The LLDD calls this routine to notify the transport that the roles
  1274. * on a remote port may have changed. The largest effect of this is
  1275. * if a port now becomes a FCP Target, it must be allocated a
  1276. * scsi target id. If the port is no longer a FCP target, any
  1277. * scsi target id value assigned to it will persist in case the
  1278. * role changes back to include FCP Target. No changes in the scsi
  1279. * midlayer will be invoked if the role changes (in the expectation
  1280. * that the role will be resumed. If it doesn't normal error processing
  1281. * will take place).
  1282. *
  1283. * Should not be called from interrupt context.
  1284. *
  1285. * Notes:
  1286. * This routine assumes no locks are held on entry.
  1287. **/
  1288. void
  1289. fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
  1290. {
  1291. struct Scsi_Host *shost = rport_to_shost(rport);
  1292. struct fc_host_attrs *fc_host =
  1293. (struct fc_host_attrs *)shost->shost_data;
  1294. unsigned long flags;
  1295. int create = 0;
  1296. rport->roles = roles;
  1297. spin_lock_irqsave(shost->host_lock, flags);
  1298. if ((rport->scsi_target_id == -1) &&
  1299. (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
  1300. rport->scsi_target_id = fc_host->next_target_id++;
  1301. create = 1;
  1302. }
  1303. spin_unlock_irqrestore(shost->host_lock, flags);
  1304. if (create)
  1305. /* initiate a scan of the target */
  1306. scsi_queue_work(shost, &rport->scan_work);
  1307. }
  1308. EXPORT_SYMBOL(fc_remote_port_rolechg);
  1309. /**
  1310. * fc_timeout_blocked_rport - Timeout handler for blocked remote port
  1311. * that fails to return in the alloted time.
  1312. * @data: scsi target that failed to reappear in the alloted time.
  1313. **/
  1314. static void
  1315. fc_timeout_blocked_rport(void *data)
  1316. {
  1317. struct fc_rport *rport = (struct fc_rport *)data;
  1318. rport->port_state = FC_PORTSTATE_OFFLINE;
  1319. dev_printk(KERN_ERR, &rport->dev,
  1320. "blocked FC remote port time out: removing target\n");
  1321. /*
  1322. * As this only occurs if the remote port (scsi target)
  1323. * went away and didn't come back - we'll remove
  1324. * all attached scsi devices.
  1325. */
  1326. scsi_target_unblock(&rport->dev);
  1327. scsi_remove_target(&rport->dev);
  1328. }
  1329. /**
  1330. * fc_remote_port_block - temporarily block any scsi traffic to a remote port.
  1331. * @rport: remote port to be blocked.
  1332. *
  1333. * scsi lldd's with a FC transport call this routine to temporarily stop
  1334. * all scsi traffic to a remote port. If the port is not a SCSI target,
  1335. * no action is taken. If the port is a SCSI target, all attached devices
  1336. * are placed into a SDEV_BLOCK state and a timer is started. The timer is
  1337. * represents the maximum amount of time the port may be blocked. If the
  1338. * timer expires, the port is considered non-existent and the attached
  1339. * scsi devices will be removed.
  1340. *
  1341. * Called from interrupt or normal process context.
  1342. *
  1343. * Returns zero if successful or error if not
  1344. *
  1345. * Notes:
  1346. * This routine assumes no locks are held on entry.
  1347. *
  1348. * The timeout and timer types are extracted from the fc transport
  1349. * attributes from the caller's rport pointer.
  1350. **/
  1351. int
  1352. fc_remote_port_block(struct fc_rport *rport)
  1353. {
  1354. int timeout = rport->dev_loss_tmo;
  1355. struct work_struct *work = &rport->dev_loss_work;
  1356. if (timeout < 0 || timeout > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
  1357. return -EINVAL;
  1358. scsi_target_block(&rport->dev);
  1359. /* cap the length the devices can be blocked */
  1360. schedule_delayed_work(work, timeout * HZ);
  1361. rport->port_state = FC_PORTSTATE_BLOCKED;
  1362. return 0;
  1363. }
  1364. EXPORT_SYMBOL(fc_remote_port_block);
  1365. /**
  1366. * fc_remote_port_unblock - restart any blocked scsi traffic to a remote port.
  1367. * @rport: remote port to be unblocked.
  1368. *
  1369. * scsi lld's with a FC transport call this routine to restart IO to all
  1370. * devices associated with the caller's scsi target following a fc_target_block
  1371. * request. Called from interrupt or normal process context.
  1372. *
  1373. * Notes:
  1374. * This routine assumes no locks are held on entry.
  1375. **/
  1376. void
  1377. fc_remote_port_unblock(struct fc_rport *rport)
  1378. {
  1379. struct work_struct *work = &rport->dev_loss_work;
  1380. struct Scsi_Host *shost = rport_to_shost(rport);
  1381. /*
  1382. * Stop the target timer first. Take no action on the del_timer
  1383. * failure as the state machine state change will validate the
  1384. * transaction.
  1385. */
  1386. if (!cancel_delayed_work(work))
  1387. flush_scheduled_work();
  1388. if (rport->port_state == FC_PORTSTATE_OFFLINE)
  1389. /*
  1390. * initiate a scan of the target as the target has
  1391. * been torn down.
  1392. */
  1393. scsi_queue_work(shost, &rport->scan_work);
  1394. else
  1395. scsi_target_unblock(&rport->dev);
  1396. rport->port_state = FC_PORTSTATE_ONLINE;
  1397. }
  1398. EXPORT_SYMBOL(fc_remote_port_unblock);
  1399. /**
  1400. * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
  1401. * @data: remote port to be scanned.
  1402. **/
  1403. static void
  1404. fc_scsi_scan_rport(void *data)
  1405. {
  1406. struct fc_rport *rport = (struct fc_rport *)data;
  1407. scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
  1408. SCAN_WILD_CARD, 1);
  1409. }
  1410. MODULE_AUTHOR("Martin Hicks");
  1411. MODULE_DESCRIPTION("FC Transport Attributes");
  1412. MODULE_LICENSE("GPL");
  1413. module_init(fc_transport_init);
  1414. module_exit(fc_transport_exit);