scsi_transport_fc.c 59 KB

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