scsi_transport_fc.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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 int fc_user_scan(struct Scsi_Host *shost, uint channel,
  928. uint id, uint lun)
  929. {
  930. struct fc_rport *rport;
  931. list_for_each_entry(rport, &fc_host_rports(shost), peers) {
  932. if (rport->scsi_target_id == -1)
  933. continue;
  934. if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
  935. (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
  936. scsi_scan_target(&rport->dev, rport->channel,
  937. rport->scsi_target_id, lun, 1);
  938. }
  939. }
  940. return 0;
  941. }
  942. struct scsi_transport_template *
  943. fc_attach_transport(struct fc_function_template *ft)
  944. {
  945. struct fc_internal *i = kmalloc(sizeof(struct fc_internal),
  946. GFP_KERNEL);
  947. int count;
  948. if (unlikely(!i))
  949. return NULL;
  950. memset(i, 0, sizeof(struct fc_internal));
  951. i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
  952. i->t.target_attrs.ac.class = &fc_transport_class.class;
  953. i->t.target_attrs.ac.match = fc_target_match;
  954. i->t.target_size = sizeof(struct fc_starget_attrs);
  955. transport_container_register(&i->t.target_attrs);
  956. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  957. i->t.host_attrs.ac.class = &fc_host_class.class;
  958. i->t.host_attrs.ac.match = fc_host_match;
  959. i->t.host_size = sizeof(struct fc_host_attrs);
  960. if (ft->get_fc_host_stats)
  961. i->t.host_attrs.statistics = &fc_statistics_group;
  962. transport_container_register(&i->t.host_attrs);
  963. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  964. i->rport_attr_cont.ac.class = &fc_rport_class.class;
  965. i->rport_attr_cont.ac.match = fc_rport_match;
  966. transport_container_register(&i->rport_attr_cont);
  967. i->f = ft;
  968. /* Transport uses the shost workq for scsi scanning */
  969. i->t.create_work_queue = 1;
  970. i->t.user_scan = fc_user_scan;
  971. /*
  972. * Setup SCSI Target Attributes.
  973. */
  974. count = 0;
  975. SETUP_STARGET_ATTRIBUTE_RD(node_name);
  976. SETUP_STARGET_ATTRIBUTE_RD(port_name);
  977. SETUP_STARGET_ATTRIBUTE_RD(port_id);
  978. BUG_ON(count > FC_STARGET_NUM_ATTRS);
  979. i->starget_attrs[count] = NULL;
  980. /*
  981. * Setup SCSI Host Attributes.
  982. */
  983. count=0;
  984. SETUP_HOST_ATTRIBUTE_RD(node_name);
  985. SETUP_HOST_ATTRIBUTE_RD(port_name);
  986. SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
  987. SETUP_HOST_ATTRIBUTE_RD(supported_classes);
  988. SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
  989. SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
  990. SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
  991. SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
  992. SETUP_HOST_ATTRIBUTE_RD(serial_number);
  993. SETUP_HOST_ATTRIBUTE_RD(port_id);
  994. SETUP_HOST_ATTRIBUTE_RD(port_type);
  995. SETUP_HOST_ATTRIBUTE_RD(port_state);
  996. SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
  997. SETUP_HOST_ATTRIBUTE_RD(speed);
  998. SETUP_HOST_ATTRIBUTE_RD(fabric_name);
  999. /* Transport-managed attributes */
  1000. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
  1001. if (ft->issue_fc_host_lip)
  1002. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
  1003. BUG_ON(count > FC_HOST_NUM_ATTRS);
  1004. i->host_attrs[count] = NULL;
  1005. /*
  1006. * Setup Remote Port Attributes.
  1007. */
  1008. count=0;
  1009. SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
  1010. SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
  1011. SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
  1012. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
  1013. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
  1014. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
  1015. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
  1016. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
  1017. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
  1018. BUG_ON(count > FC_RPORT_NUM_ATTRS);
  1019. i->rport_attrs[count] = NULL;
  1020. return &i->t;
  1021. }
  1022. EXPORT_SYMBOL(fc_attach_transport);
  1023. void fc_release_transport(struct scsi_transport_template *t)
  1024. {
  1025. struct fc_internal *i = to_fc_internal(t);
  1026. transport_container_unregister(&i->t.target_attrs);
  1027. transport_container_unregister(&i->t.host_attrs);
  1028. transport_container_unregister(&i->rport_attr_cont);
  1029. kfree(i);
  1030. }
  1031. EXPORT_SYMBOL(fc_release_transport);
  1032. /**
  1033. * fc_remove_host - called to terminate any fc_transport-related elements
  1034. * for a scsi host.
  1035. * @rport: remote port to be unblocked.
  1036. *
  1037. * This routine is expected to be called immediately preceeding the
  1038. * a driver's call to scsi_remove_host().
  1039. *
  1040. * WARNING: A driver utilizing the fc_transport, which fails to call
  1041. * this routine prior to scsi_remote_host(), will leave dangling
  1042. * objects in /sys/class/fc_remote_ports. Access to any of these
  1043. * objects can result in a system crash !!!
  1044. *
  1045. * Notes:
  1046. * This routine assumes no locks are held on entry.
  1047. **/
  1048. void
  1049. fc_remove_host(struct Scsi_Host *shost)
  1050. {
  1051. struct fc_rport *rport, *next_rport;
  1052. /* Remove any remote ports */
  1053. list_for_each_entry_safe(rport, next_rport,
  1054. &fc_host_rports(shost), peers)
  1055. fc_rport_terminate(rport);
  1056. list_for_each_entry_safe(rport, next_rport,
  1057. &fc_host_rport_bindings(shost), peers)
  1058. fc_rport_terminate(rport);
  1059. }
  1060. EXPORT_SYMBOL(fc_remove_host);
  1061. /*
  1062. * fc_rport_tgt_remove - Removes the scsi target on the remote port
  1063. * @rport: The remote port to be operated on
  1064. */
  1065. static void
  1066. fc_rport_tgt_remove(struct fc_rport *rport)
  1067. {
  1068. struct Scsi_Host *shost = rport_to_shost(rport);
  1069. scsi_target_unblock(&rport->dev);
  1070. /* Stop anything on the workq */
  1071. if (!cancel_delayed_work(&rport->dev_loss_work))
  1072. flush_scheduled_work();
  1073. scsi_flush_work(shost);
  1074. scsi_remove_target(&rport->dev);
  1075. }
  1076. /**
  1077. * fc_rport_create - allocates and creates a remote FC port.
  1078. * @shost: scsi host the remote port is connected to.
  1079. * @channel: Channel on shost port connected to.
  1080. * @ids: The world wide names, fc address, and FC4 port
  1081. * roles for the remote port.
  1082. *
  1083. * Allocates and creates the remoter port structure, including the
  1084. * class and sysfs creation.
  1085. *
  1086. * Notes:
  1087. * This routine assumes no locks are held on entry.
  1088. **/
  1089. struct fc_rport *
  1090. fc_rport_create(struct Scsi_Host *shost, int channel,
  1091. struct fc_rport_identifiers *ids)
  1092. {
  1093. struct fc_host_attrs *fc_host =
  1094. (struct fc_host_attrs *)shost->shost_data;
  1095. struct fc_internal *fci = to_fc_internal(shost->transportt);
  1096. struct fc_rport *rport;
  1097. struct device *dev;
  1098. unsigned long flags;
  1099. int error;
  1100. size_t size;
  1101. size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
  1102. rport = kmalloc(size, GFP_KERNEL);
  1103. if (unlikely(!rport)) {
  1104. printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
  1105. return NULL;
  1106. }
  1107. memset(rport, 0, size);
  1108. rport->maxframe_size = -1;
  1109. rport->supported_classes = FC_COS_UNSPECIFIED;
  1110. rport->dev_loss_tmo = fc_dev_loss_tmo;
  1111. memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
  1112. memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
  1113. rport->port_id = ids->port_id;
  1114. rport->roles = ids->roles;
  1115. rport->port_state = FC_PORTSTATE_ONLINE;
  1116. if (fci->f->dd_fcrport_size)
  1117. rport->dd_data = &rport[1];
  1118. rport->channel = channel;
  1119. INIT_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport, rport);
  1120. INIT_WORK(&rport->scan_work, fc_scsi_scan_rport, rport);
  1121. spin_lock_irqsave(shost->host_lock, flags);
  1122. rport->number = fc_host->next_rport_number++;
  1123. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  1124. rport->scsi_target_id = fc_host->next_target_id++;
  1125. else
  1126. rport->scsi_target_id = -1;
  1127. list_add_tail(&rport->peers, &fc_host_rports(shost));
  1128. get_device(&shost->shost_gendev);
  1129. spin_unlock_irqrestore(shost->host_lock, flags);
  1130. dev = &rport->dev;
  1131. device_initialize(dev);
  1132. dev->parent = get_device(&shost->shost_gendev);
  1133. dev->release = fc_rport_dev_release;
  1134. sprintf(dev->bus_id, "rport-%d:%d-%d",
  1135. shost->host_no, channel, rport->number);
  1136. transport_setup_device(dev);
  1137. error = device_add(dev);
  1138. if (error) {
  1139. printk(KERN_ERR "FC Remote Port device_add failed\n");
  1140. goto delete_rport;
  1141. }
  1142. transport_add_device(dev);
  1143. transport_configure_device(dev);
  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. delete_rport:
  1149. transport_destroy_device(dev);
  1150. put_device(dev->parent);
  1151. spin_lock_irqsave(shost->host_lock, flags);
  1152. list_del(&rport->peers);
  1153. put_device(&shost->shost_gendev);
  1154. spin_unlock_irqrestore(shost->host_lock, flags);
  1155. put_device(dev->parent);
  1156. kfree(rport);
  1157. return NULL;
  1158. }
  1159. /**
  1160. * fc_remote_port_add - notifies the fc transport of the existence
  1161. * of a remote FC port.
  1162. * @shost: scsi host the remote port is connected to.
  1163. * @channel: Channel on shost port connected to.
  1164. * @ids: The world wide names, fc address, and FC4 port
  1165. * roles for the remote port.
  1166. *
  1167. * The LLDD calls this routine to notify the transport of the existence
  1168. * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
  1169. * of the port, it's FC address (port_id), and the FC4 roles that are
  1170. * active for the port.
  1171. *
  1172. * For ports that are FCP targets (aka scsi targets), the FC transport
  1173. * maintains consistent target id bindings on behalf of the LLDD.
  1174. * A consistent target id binding is an assignment of a target id to
  1175. * a remote port identifier, which persists while the scsi host is
  1176. * attached. The remote port can disappear, then later reappear, and
  1177. * it's target id assignment remains the same. This allows for shifts
  1178. * in FC addressing (if binding by wwpn or wwnn) with no apparent
  1179. * changes to the scsi subsystem which is based on scsi host number and
  1180. * target id values. Bindings are only valid during the attachment of
  1181. * the scsi host. If the host detaches, then later re-attaches, target
  1182. * id bindings may change.
  1183. *
  1184. * This routine is responsible for returning a remote port structure.
  1185. * The routine will search the list of remote ports it maintains
  1186. * internally on behalf of consistent target id mappings. If found, the
  1187. * remote port structure will be reused. Otherwise, a new remote port
  1188. * structure will be allocated.
  1189. *
  1190. * Whenever a remote port is allocated, a new fc_remote_port class
  1191. * device is created.
  1192. *
  1193. * Should not be called from interrupt context.
  1194. *
  1195. * Notes:
  1196. * This routine assumes no locks are held on entry.
  1197. **/
  1198. struct fc_rport *
  1199. fc_remote_port_add(struct Scsi_Host *shost, int channel,
  1200. struct fc_rport_identifiers *ids)
  1201. {
  1202. struct fc_internal *fci = to_fc_internal(shost->transportt);
  1203. struct fc_rport *rport;
  1204. unsigned long flags;
  1205. int match = 0;
  1206. /*
  1207. * Search the list of "active" rports, for an rport that has been
  1208. * deleted, but we've held off the real delete while the target
  1209. * is in a "blocked" state.
  1210. */
  1211. spin_lock_irqsave(shost->host_lock, flags);
  1212. list_for_each_entry(rport, &fc_host_rports(shost), peers) {
  1213. if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
  1214. (rport->channel == channel)) {
  1215. switch (fc_host_tgtid_bind_type(shost)) {
  1216. case FC_TGTID_BIND_BY_WWPN:
  1217. case FC_TGTID_BIND_NONE:
  1218. if (rport->port_name == ids->port_name)
  1219. match = 1;
  1220. break;
  1221. case FC_TGTID_BIND_BY_WWNN:
  1222. if (rport->node_name == ids->node_name)
  1223. match = 1;
  1224. break;
  1225. case FC_TGTID_BIND_BY_ID:
  1226. if (rport->port_id == ids->port_id)
  1227. match = 1;
  1228. break;
  1229. }
  1230. if (match) {
  1231. struct work_struct *work =
  1232. &rport->dev_loss_work;
  1233. memcpy(&rport->node_name, &ids->node_name,
  1234. sizeof(rport->node_name));
  1235. memcpy(&rport->port_name, &ids->port_name,
  1236. sizeof(rport->port_name));
  1237. rport->port_id = ids->port_id;
  1238. rport->port_state = FC_PORTSTATE_ONLINE;
  1239. rport->roles = ids->roles;
  1240. spin_unlock_irqrestore(shost->host_lock, flags);
  1241. if (fci->f->dd_fcrport_size)
  1242. memset(rport->dd_data, 0,
  1243. fci->f->dd_fcrport_size);
  1244. /*
  1245. * If we were blocked, we were a target.
  1246. * If no longer a target, we leave the timer
  1247. * running in case the port changes roles
  1248. * prior to the timer expiring. If the timer
  1249. * fires, the target will be torn down.
  1250. */
  1251. if (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET))
  1252. return rport;
  1253. /* restart the target */
  1254. /*
  1255. * Stop the target timer first. Take no action
  1256. * on the del_timer failure as the state
  1257. * machine state change will validate the
  1258. * transaction.
  1259. */
  1260. if (!cancel_delayed_work(work))
  1261. flush_scheduled_work();
  1262. /* initiate a scan of the target */
  1263. scsi_queue_work(shost, &rport->scan_work);
  1264. return rport;
  1265. }
  1266. }
  1267. }
  1268. /* Search the bindings array */
  1269. if (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
  1270. (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
  1271. /* search for a matching consistent binding */
  1272. list_for_each_entry(rport, &fc_host_rport_bindings(shost),
  1273. peers) {
  1274. if (rport->channel != channel)
  1275. continue;
  1276. switch (fc_host_tgtid_bind_type(shost)) {
  1277. case FC_TGTID_BIND_BY_WWPN:
  1278. if (rport->port_name == ids->port_name)
  1279. match = 1;
  1280. break;
  1281. case FC_TGTID_BIND_BY_WWNN:
  1282. if (rport->node_name == ids->node_name)
  1283. match = 1;
  1284. break;
  1285. case FC_TGTID_BIND_BY_ID:
  1286. if (rport->port_id == ids->port_id)
  1287. match = 1;
  1288. break;
  1289. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  1290. break;
  1291. }
  1292. if (match) {
  1293. list_move_tail(&rport->peers,
  1294. &fc_host_rports(shost));
  1295. break;
  1296. }
  1297. }
  1298. if (match) {
  1299. memcpy(&rport->node_name, &ids->node_name,
  1300. sizeof(rport->node_name));
  1301. memcpy(&rport->port_name, &ids->port_name,
  1302. sizeof(rport->port_name));
  1303. rport->port_id = ids->port_id;
  1304. rport->roles = ids->roles;
  1305. rport->port_state = FC_PORTSTATE_ONLINE;
  1306. spin_unlock_irqrestore(shost->host_lock, flags);
  1307. if (fci->f->dd_fcrport_size)
  1308. memset(rport->dd_data, 0,
  1309. fci->f->dd_fcrport_size);
  1310. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  1311. /* initiate a scan of the target */
  1312. scsi_queue_work(shost, &rport->scan_work);
  1313. return rport;
  1314. }
  1315. }
  1316. spin_unlock_irqrestore(shost->host_lock, flags);
  1317. /* No consistent binding found - create new remote port entry */
  1318. rport = fc_rport_create(shost, channel, ids);
  1319. return rport;
  1320. }
  1321. EXPORT_SYMBOL(fc_remote_port_add);
  1322. /*
  1323. * fc_rport_terminate - this routine tears down and deallocates a remote port.
  1324. * @rport: The remote port to be terminated
  1325. *
  1326. * Notes:
  1327. * This routine assumes no locks are held on entry.
  1328. */
  1329. static void
  1330. fc_rport_terminate(struct fc_rport *rport)
  1331. {
  1332. struct Scsi_Host *shost = rport_to_shost(rport);
  1333. struct device *dev = &rport->dev;
  1334. unsigned long flags;
  1335. fc_rport_tgt_remove(rport);
  1336. transport_remove_device(dev);
  1337. device_del(dev);
  1338. transport_destroy_device(dev);
  1339. spin_lock_irqsave(shost->host_lock, flags);
  1340. list_del(&rport->peers);
  1341. spin_unlock_irqrestore(shost->host_lock, flags);
  1342. put_device(&shost->shost_gendev);
  1343. }
  1344. /**
  1345. * fc_remote_port_delete - notifies the fc transport that a remote
  1346. * port is no longer in existence.
  1347. * @rport: The remote port that no longer exists
  1348. *
  1349. * The LLDD calls this routine to notify the transport that a remote
  1350. * port is no longer part of the topology. Note: Although a port
  1351. * may no longer be part of the topology, it may persist in the remote
  1352. * ports displayed by the fc_host. We do this under 2 conditions:
  1353. * - If the port was a scsi target, we delay its deletion by "blocking" it.
  1354. * This allows the port to temporarily disappear, then reappear without
  1355. * disrupting the SCSI device tree attached to it. During the "blocked"
  1356. * period the port will still exist.
  1357. * - If the port was a scsi target and disappears for longer than we
  1358. * expect, we'll delete the port and the tear down the SCSI device tree
  1359. * attached to it. However, we want to semi-persist the target id assigned
  1360. * to that port if it eventually does exist. The port structure will
  1361. * remain (although with minimal information) so that the target id
  1362. * bindings remails.
  1363. *
  1364. * If the remote port is not an FCP Target, it will be fully torn down
  1365. * and deallocated, including the fc_remote_port class device.
  1366. *
  1367. * If the remote port is an FCP Target, the port will be placed in a
  1368. * temporary blocked state. From the LLDD's perspective, the rport no
  1369. * longer exists. From the SCSI midlayer's perspective, the SCSI target
  1370. * exists, but all sdevs on it are blocked from further I/O. The following
  1371. * is then expected:
  1372. * If the remote port does not return (signaled by a LLDD call to
  1373. * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
  1374. * scsi target is removed - killing all outstanding i/o and removing the
  1375. * scsi devices attached ot it. The port structure will be marked Not
  1376. * Present and be partially cleared, leaving only enough information to
  1377. * recognize the remote port relative to the scsi target id binding if
  1378. * it later appears. The port will remain as long as there is a valid
  1379. * binding (e.g. until the user changes the binding type or unloads the
  1380. * scsi host with the binding).
  1381. *
  1382. * If the remote port returns within the dev_loss_tmo value (and matches
  1383. * according to the target id binding type), the port structure will be
  1384. * reused. If it is no longer a SCSI target, the target will be torn
  1385. * down. If it continues to be a SCSI target, then the target will be
  1386. * unblocked (allowing i/o to be resumed), and a scan will be activated
  1387. * to ensure that all luns are detected.
  1388. *
  1389. * Called from normal process context only - cannot be called from interrupt.
  1390. *
  1391. * Notes:
  1392. * This routine assumes no locks are held on entry.
  1393. **/
  1394. void
  1395. fc_remote_port_delete(struct fc_rport *rport)
  1396. {
  1397. int timeout = rport->dev_loss_tmo;
  1398. /* If no scsi target id mapping, delete it */
  1399. if (rport->scsi_target_id == -1) {
  1400. fc_rport_terminate(rport);
  1401. return;
  1402. }
  1403. scsi_target_block(&rport->dev);
  1404. /* cap the length the devices can be blocked until they are deleted */
  1405. schedule_delayed_work(&rport->dev_loss_work, timeout * HZ);
  1406. rport->port_state = FC_PORTSTATE_BLOCKED;
  1407. }
  1408. EXPORT_SYMBOL(fc_remote_port_delete);
  1409. /**
  1410. * fc_remote_port_rolechg - notifies the fc transport that the roles
  1411. * on a remote may have changed.
  1412. * @rport: The remote port that changed.
  1413. *
  1414. * The LLDD calls this routine to notify the transport that the roles
  1415. * on a remote port may have changed. The largest effect of this is
  1416. * if a port now becomes a FCP Target, it must be allocated a
  1417. * scsi target id. If the port is no longer a FCP target, any
  1418. * scsi target id value assigned to it will persist in case the
  1419. * role changes back to include FCP Target. No changes in the scsi
  1420. * midlayer will be invoked if the role changes (in the expectation
  1421. * that the role will be resumed. If it doesn't normal error processing
  1422. * will take place).
  1423. *
  1424. * Should not be called from interrupt context.
  1425. *
  1426. * Notes:
  1427. * This routine assumes no locks are held on entry.
  1428. **/
  1429. void
  1430. fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
  1431. {
  1432. struct Scsi_Host *shost = rport_to_shost(rport);
  1433. struct fc_host_attrs *fc_host =
  1434. (struct fc_host_attrs *)shost->shost_data;
  1435. unsigned long flags;
  1436. int create = 0;
  1437. spin_lock_irqsave(shost->host_lock, flags);
  1438. if (roles & FC_RPORT_ROLE_FCP_TARGET) {
  1439. if (rport->scsi_target_id == -1) {
  1440. rport->scsi_target_id = fc_host->next_target_id++;
  1441. create = 1;
  1442. } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
  1443. create = 1;
  1444. }
  1445. spin_unlock_irqrestore(shost->host_lock, flags);
  1446. rport->roles = roles;
  1447. if (create) {
  1448. /*
  1449. * There may have been a delete timer running on the
  1450. * port. Ensure that it is cancelled as we now know
  1451. * the port is an FCP Target.
  1452. * Note: we know the rport is exists and in an online
  1453. * state as the LLDD would not have had an rport
  1454. * reference to pass us.
  1455. *
  1456. * Take no action on the del_timer failure as the state
  1457. * machine state change will validate the
  1458. * transaction.
  1459. */
  1460. if (!cancel_delayed_work(&rport->dev_loss_work))
  1461. flush_scheduled_work();
  1462. /* initiate a scan of the target */
  1463. scsi_queue_work(shost, &rport->scan_work);
  1464. }
  1465. }
  1466. EXPORT_SYMBOL(fc_remote_port_rolechg);
  1467. /**
  1468. * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
  1469. * was a SCSI target (thus was blocked), and failed
  1470. * to return in the alloted time.
  1471. *
  1472. * @data: rport target that failed to reappear in the alloted time.
  1473. **/
  1474. static void
  1475. fc_timeout_deleted_rport(void *data)
  1476. {
  1477. struct fc_rport *rport = (struct fc_rport *)data;
  1478. struct Scsi_Host *shost = rport_to_shost(rport);
  1479. unsigned long flags;
  1480. spin_lock_irqsave(shost->host_lock, flags);
  1481. /*
  1482. * If the port is ONLINE, then it came back, but was no longer an
  1483. * FCP target. Thus we need to tear down the scsi_target on it.
  1484. */
  1485. if (rport->port_state == FC_PORTSTATE_ONLINE) {
  1486. spin_unlock_irqrestore(shost->host_lock, flags);
  1487. dev_printk(KERN_ERR, &rport->dev,
  1488. "blocked FC remote port time out: removing target\n");
  1489. fc_rport_tgt_remove(rport);
  1490. return;
  1491. }
  1492. if (rport->port_state != FC_PORTSTATE_BLOCKED) {
  1493. spin_unlock_irqrestore(shost->host_lock, flags);
  1494. dev_printk(KERN_ERR, &rport->dev,
  1495. "blocked FC remote port time out: leaving target alone\n");
  1496. return;
  1497. }
  1498. if (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE) {
  1499. spin_unlock_irqrestore(shost->host_lock, flags);
  1500. dev_printk(KERN_ERR, &rport->dev,
  1501. "blocked FC remote port time out: removing target\n");
  1502. fc_rport_terminate(rport);
  1503. return;
  1504. }
  1505. dev_printk(KERN_ERR, &rport->dev,
  1506. "blocked FC remote port time out: removing target and "
  1507. "saving binding\n");
  1508. list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
  1509. /*
  1510. * Note: We do not remove or clear the hostdata area. This allows
  1511. * host-specific target data to persist along with the
  1512. * scsi_target_id. It's up to the host to manage it's hostdata area.
  1513. */
  1514. /*
  1515. * Reinitialize port attributes that may change if the port comes back.
  1516. */
  1517. rport->maxframe_size = -1;
  1518. rport->supported_classes = FC_COS_UNSPECIFIED;
  1519. rport->roles = FC_RPORT_ROLE_UNKNOWN;
  1520. rport->port_state = FC_PORTSTATE_DELETED;
  1521. /* remove the identifiers that aren't used in the consisting binding */
  1522. switch (fc_host_tgtid_bind_type(shost)) {
  1523. case FC_TGTID_BIND_BY_WWPN:
  1524. rport->node_name = -1;
  1525. rport->port_id = -1;
  1526. break;
  1527. case FC_TGTID_BIND_BY_WWNN:
  1528. rport->port_name = -1;
  1529. rport->port_id = -1;
  1530. break;
  1531. case FC_TGTID_BIND_BY_ID:
  1532. rport->node_name = -1;
  1533. rport->port_name = -1;
  1534. break;
  1535. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  1536. break;
  1537. }
  1538. /*
  1539. * As this only occurs if the remote port (scsi target)
  1540. * went away and didn't come back - we'll remove
  1541. * all attached scsi devices.
  1542. *
  1543. * We'll schedule the shost work item to perform the actual removal
  1544. * to avoid recursion in the different flush calls if we perform
  1545. * the removal in each target - and there are lots of targets
  1546. * whose timeouts fire at the same time.
  1547. */
  1548. if ( !(fc_host_flags(shost) & FC_SHOST_RPORT_DEL_SCHEDULED)) {
  1549. fc_host_flags(shost) |= FC_SHOST_RPORT_DEL_SCHEDULED;
  1550. scsi_queue_work(shost, &fc_host_rport_del_work(shost));
  1551. }
  1552. spin_unlock_irqrestore(shost->host_lock, flags);
  1553. }
  1554. /**
  1555. * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
  1556. *
  1557. * Will unblock the target (in case it went away and has now come back),
  1558. * then invoke a scan.
  1559. *
  1560. * @data: remote port to be scanned.
  1561. **/
  1562. static void
  1563. fc_scsi_scan_rport(void *data)
  1564. {
  1565. struct fc_rport *rport = (struct fc_rport *)data;
  1566. scsi_target_unblock(&rport->dev);
  1567. scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
  1568. SCAN_WILD_CARD, 1);
  1569. }
  1570. /**
  1571. * fc_shost_remove_rports - called to remove all rports that are marked
  1572. * as in a deleted (not connected) state.
  1573. *
  1574. * @data: shost whose rports are to be looked at
  1575. **/
  1576. static void
  1577. fc_shost_remove_rports(void *data)
  1578. {
  1579. struct Scsi_Host *shost = (struct Scsi_Host *)data;
  1580. struct fc_rport *rport, *next_rport;
  1581. unsigned long flags;
  1582. spin_lock_irqsave(shost->host_lock, flags);
  1583. while (fc_host_flags(shost) & FC_SHOST_RPORT_DEL_SCHEDULED) {
  1584. fc_host_flags(shost) &= ~FC_SHOST_RPORT_DEL_SCHEDULED;
  1585. restart_search:
  1586. list_for_each_entry_safe(rport, next_rport,
  1587. &fc_host_rport_bindings(shost), peers) {
  1588. if (rport->port_state == FC_PORTSTATE_DELETED) {
  1589. rport->port_state = FC_PORTSTATE_NOTPRESENT;
  1590. spin_unlock_irqrestore(shost->host_lock, flags);
  1591. fc_rport_tgt_remove(rport);
  1592. spin_lock_irqsave(shost->host_lock, flags);
  1593. goto restart_search;
  1594. }
  1595. }
  1596. }
  1597. spin_unlock_irqrestore(shost->host_lock, flags);
  1598. }
  1599. MODULE_AUTHOR("Martin Hicks");
  1600. MODULE_DESCRIPTION("FC Transport Attributes");
  1601. MODULE_LICENSE("GPL");
  1602. module_init(fc_transport_init);
  1603. module_exit(fc_transport_exit);