scsi_transport_fc.c 56 KB

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