scsi_transport_fc.c 58 KB

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