scsi_transport_fc.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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 16
  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 (likely((ids->roles & FC_RPORT_ROLE_FCP_TARGET) &&
  1301. (fc_host_tgtid_bind_type(shost) != FC_TGTID_BIND_NONE))) {
  1302. /* search for a matching consistent binding */
  1303. list_for_each_entry(rport, &fc_host_rport_bindings(shost),
  1304. peers) {
  1305. if (rport->channel != channel)
  1306. continue;
  1307. switch (fc_host_tgtid_bind_type(shost)) {
  1308. case FC_TGTID_BIND_BY_WWPN:
  1309. if (rport->port_name == ids->port_name)
  1310. match = 1;
  1311. break;
  1312. case FC_TGTID_BIND_BY_WWNN:
  1313. if (rport->node_name == ids->node_name)
  1314. match = 1;
  1315. break;
  1316. case FC_TGTID_BIND_BY_ID:
  1317. if (rport->port_id == ids->port_id)
  1318. match = 1;
  1319. break;
  1320. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  1321. break;
  1322. }
  1323. if (match) {
  1324. list_move_tail(&rport->peers,
  1325. &fc_host_rports(shost));
  1326. break;
  1327. }
  1328. }
  1329. if (match) {
  1330. memcpy(&rport->node_name, &ids->node_name,
  1331. sizeof(rport->node_name));
  1332. memcpy(&rport->port_name, &ids->port_name,
  1333. sizeof(rport->port_name));
  1334. rport->port_id = ids->port_id;
  1335. rport->roles = ids->roles;
  1336. rport->port_state = FC_PORTSTATE_ONLINE;
  1337. spin_unlock_irqrestore(shost->host_lock, flags);
  1338. if (fci->f->dd_fcrport_size)
  1339. memset(rport->dd_data, 0,
  1340. fci->f->dd_fcrport_size);
  1341. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  1342. /* initiate a scan of the target */
  1343. scsi_queue_work(shost, &rport->scan_work);
  1344. return rport;
  1345. }
  1346. }
  1347. spin_unlock_irqrestore(shost->host_lock, flags);
  1348. /* No consistent binding found - create new remote port entry */
  1349. rport = fc_rport_create(shost, channel, ids);
  1350. return rport;
  1351. }
  1352. EXPORT_SYMBOL(fc_remote_port_add);
  1353. /*
  1354. * fc_rport_terminate - this routine tears down and deallocates a remote port.
  1355. * @rport: The remote port to be terminated
  1356. *
  1357. * Notes:
  1358. * This routine assumes no locks are held on entry.
  1359. */
  1360. static void
  1361. fc_rport_terminate(struct fc_rport *rport)
  1362. {
  1363. struct Scsi_Host *shost = rport_to_shost(rport);
  1364. struct device *dev = &rport->dev;
  1365. unsigned long flags;
  1366. fc_rport_tgt_remove(rport);
  1367. transport_remove_device(dev);
  1368. device_del(dev);
  1369. transport_destroy_device(dev);
  1370. spin_lock_irqsave(shost->host_lock, flags);
  1371. list_del(&rport->peers);
  1372. spin_unlock_irqrestore(shost->host_lock, flags);
  1373. put_device(&shost->shost_gendev);
  1374. }
  1375. /**
  1376. * fc_remote_port_delete - notifies the fc transport that a remote
  1377. * port is no longer in existence.
  1378. * @rport: The remote port that no longer exists
  1379. *
  1380. * The LLDD calls this routine to notify the transport that a remote
  1381. * port is no longer part of the topology. Note: Although a port
  1382. * may no longer be part of the topology, it may persist in the remote
  1383. * ports displayed by the fc_host. We do this under 2 conditions:
  1384. * - If the port was a scsi target, we delay its deletion by "blocking" it.
  1385. * This allows the port to temporarily disappear, then reappear without
  1386. * disrupting the SCSI device tree attached to it. During the "blocked"
  1387. * period the port will still exist.
  1388. * - If the port was a scsi target and disappears for longer than we
  1389. * expect, we'll delete the port and the tear down the SCSI device tree
  1390. * attached to it. However, we want to semi-persist the target id assigned
  1391. * to that port if it eventually does exist. The port structure will
  1392. * remain (although with minimal information) so that the target id
  1393. * bindings remails.
  1394. *
  1395. * If the remote port is not an FCP Target, it will be fully torn down
  1396. * and deallocated, including the fc_remote_port class device.
  1397. *
  1398. * If the remote port is an FCP Target, the port will be placed in a
  1399. * temporary blocked state. From the LLDD's perspective, the rport no
  1400. * longer exists. From the SCSI midlayer's perspective, the SCSI target
  1401. * exists, but all sdevs on it are blocked from further I/O. The following
  1402. * is then expected:
  1403. * If the remote port does not return (signaled by a LLDD call to
  1404. * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
  1405. * scsi target is removed - killing all outstanding i/o and removing the
  1406. * scsi devices attached ot it. The port structure will be marked Not
  1407. * Present and be partially cleared, leaving only enough information to
  1408. * recognize the remote port relative to the scsi target id binding if
  1409. * it later appears. The port will remain as long as there is a valid
  1410. * binding (e.g. until the user changes the binding type or unloads the
  1411. * scsi host with the binding).
  1412. *
  1413. * If the remote port returns within the dev_loss_tmo value (and matches
  1414. * according to the target id binding type), the port structure will be
  1415. * reused. If it is no longer a SCSI target, the target will be torn
  1416. * down. If it continues to be a SCSI target, then the target will be
  1417. * unblocked (allowing i/o to be resumed), and a scan will be activated
  1418. * to ensure that all luns are detected.
  1419. *
  1420. * Called from normal process context only - cannot be called from interrupt.
  1421. *
  1422. * Notes:
  1423. * This routine assumes no locks are held on entry.
  1424. **/
  1425. void
  1426. fc_remote_port_delete(struct fc_rport *rport)
  1427. {
  1428. int timeout = rport->dev_loss_tmo;
  1429. /* If no scsi target id mapping, delete it */
  1430. if (rport->scsi_target_id == -1) {
  1431. fc_rport_terminate(rport);
  1432. return;
  1433. }
  1434. scsi_target_block(&rport->dev);
  1435. /* cap the length the devices can be blocked until they are deleted */
  1436. schedule_delayed_work(&rport->dev_loss_work, timeout * HZ);
  1437. rport->port_state = FC_PORTSTATE_BLOCKED;
  1438. }
  1439. EXPORT_SYMBOL(fc_remote_port_delete);
  1440. /**
  1441. * fc_remote_port_rolechg - notifies the fc transport that the roles
  1442. * on a remote may have changed.
  1443. * @rport: The remote port that changed.
  1444. *
  1445. * The LLDD calls this routine to notify the transport that the roles
  1446. * on a remote port may have changed. The largest effect of this is
  1447. * if a port now becomes a FCP Target, it must be allocated a
  1448. * scsi target id. If the port is no longer a FCP target, any
  1449. * scsi target id value assigned to it will persist in case the
  1450. * role changes back to include FCP Target. No changes in the scsi
  1451. * midlayer will be invoked if the role changes (in the expectation
  1452. * that the role will be resumed. If it doesn't normal error processing
  1453. * will take place).
  1454. *
  1455. * Should not be called from interrupt context.
  1456. *
  1457. * Notes:
  1458. * This routine assumes no locks are held on entry.
  1459. **/
  1460. void
  1461. fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
  1462. {
  1463. struct Scsi_Host *shost = rport_to_shost(rport);
  1464. struct fc_host_attrs *fc_host =
  1465. (struct fc_host_attrs *)shost->shost_data;
  1466. unsigned long flags;
  1467. int create = 0;
  1468. spin_lock_irqsave(shost->host_lock, flags);
  1469. if (roles & FC_RPORT_ROLE_FCP_TARGET) {
  1470. if (rport->scsi_target_id == -1) {
  1471. rport->scsi_target_id = fc_host->next_target_id++;
  1472. create = 1;
  1473. } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
  1474. create = 1;
  1475. }
  1476. spin_unlock_irqrestore(shost->host_lock, flags);
  1477. rport->roles = roles;
  1478. if (create) {
  1479. /*
  1480. * There may have been a delete timer running on the
  1481. * port. Ensure that it is cancelled as we now know
  1482. * the port is an FCP Target.
  1483. * Note: we know the rport is exists and in an online
  1484. * state as the LLDD would not have had an rport
  1485. * reference to pass us.
  1486. *
  1487. * Take no action on the del_timer failure as the state
  1488. * machine state change will validate the
  1489. * transaction.
  1490. */
  1491. if (!cancel_delayed_work(&rport->dev_loss_work))
  1492. flush_scheduled_work();
  1493. /* initiate a scan of the target */
  1494. scsi_queue_work(shost, &rport->scan_work);
  1495. }
  1496. }
  1497. EXPORT_SYMBOL(fc_remote_port_rolechg);
  1498. /**
  1499. * fc_timeout_deleted_rport - Timeout handler for a deleted remote port that
  1500. * was a SCSI target (thus was blocked), and failed
  1501. * to return in the alloted time.
  1502. *
  1503. * @data: rport target that failed to reappear in the alloted time.
  1504. **/
  1505. static void
  1506. fc_timeout_deleted_rport(void *data)
  1507. {
  1508. struct fc_rport *rport = (struct fc_rport *)data;
  1509. struct Scsi_Host *shost = rport_to_shost(rport);
  1510. unsigned long flags;
  1511. spin_lock_irqsave(shost->host_lock, flags);
  1512. /*
  1513. * If the port is ONLINE, then it came back, but was no longer an
  1514. * FCP target. Thus we need to tear down the scsi_target on it.
  1515. */
  1516. if (rport->port_state == FC_PORTSTATE_ONLINE) {
  1517. spin_unlock_irqrestore(shost->host_lock, flags);
  1518. dev_printk(KERN_ERR, &rport->dev,
  1519. "blocked FC remote port time out: removing target\n");
  1520. fc_rport_tgt_remove(rport);
  1521. return;
  1522. }
  1523. if (rport->port_state != FC_PORTSTATE_BLOCKED) {
  1524. spin_unlock_irqrestore(shost->host_lock, flags);
  1525. dev_printk(KERN_ERR, &rport->dev,
  1526. "blocked FC remote port time out: leaving target alone\n");
  1527. return;
  1528. }
  1529. if (fc_host_tgtid_bind_type(shost) == FC_TGTID_BIND_NONE) {
  1530. spin_unlock_irqrestore(shost->host_lock, flags);
  1531. dev_printk(KERN_ERR, &rport->dev,
  1532. "blocked FC remote port time out: removing target\n");
  1533. fc_rport_terminate(rport);
  1534. return;
  1535. }
  1536. dev_printk(KERN_ERR, &rport->dev,
  1537. "blocked FC remote port time out: removing target and "
  1538. "saving binding\n");
  1539. list_move_tail(&rport->peers, &fc_host_rport_bindings(shost));
  1540. /*
  1541. * Note: We do not remove or clear the hostdata area. This allows
  1542. * host-specific target data to persist along with the
  1543. * scsi_target_id. It's up to the host to manage it's hostdata area.
  1544. */
  1545. /*
  1546. * Reinitialize port attributes that may change if the port comes back.
  1547. */
  1548. rport->maxframe_size = -1;
  1549. rport->supported_classes = FC_COS_UNSPECIFIED;
  1550. rport->roles = FC_RPORT_ROLE_UNKNOWN;
  1551. rport->port_state = FC_PORTSTATE_DELETED;
  1552. /* remove the identifiers that aren't used in the consisting binding */
  1553. switch (fc_host_tgtid_bind_type(shost)) {
  1554. case FC_TGTID_BIND_BY_WWPN:
  1555. rport->node_name = -1;
  1556. rport->port_id = -1;
  1557. break;
  1558. case FC_TGTID_BIND_BY_WWNN:
  1559. rport->port_name = -1;
  1560. rport->port_id = -1;
  1561. break;
  1562. case FC_TGTID_BIND_BY_ID:
  1563. rport->node_name = -1;
  1564. rport->port_name = -1;
  1565. break;
  1566. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  1567. break;
  1568. }
  1569. /*
  1570. * As this only occurs if the remote port (scsi target)
  1571. * went away and didn't come back - we'll remove
  1572. * all attached scsi devices.
  1573. *
  1574. * We'll schedule the shost work item to perform the actual removal
  1575. * to avoid recursion in the different flush calls if we perform
  1576. * the removal in each target - and there are lots of targets
  1577. * whose timeouts fire at the same time.
  1578. */
  1579. if ( !(fc_host_flags(shost) & FC_SHOST_RPORT_DEL_SCHEDULED)) {
  1580. fc_host_flags(shost) |= FC_SHOST_RPORT_DEL_SCHEDULED;
  1581. scsi_queue_work(shost, &fc_host_rport_del_work(shost));
  1582. }
  1583. spin_unlock_irqrestore(shost->host_lock, flags);
  1584. }
  1585. /**
  1586. * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
  1587. *
  1588. * Will unblock the target (in case it went away and has now come back),
  1589. * then invoke a scan.
  1590. *
  1591. * @data: remote port to be scanned.
  1592. **/
  1593. static void
  1594. fc_scsi_scan_rport(void *data)
  1595. {
  1596. struct fc_rport *rport = (struct fc_rport *)data;
  1597. scsi_target_unblock(&rport->dev);
  1598. scsi_scan_target(&rport->dev, rport->channel, rport->scsi_target_id,
  1599. SCAN_WILD_CARD, 1);
  1600. }
  1601. /**
  1602. * fc_shost_remove_rports - called to remove all rports that are marked
  1603. * as in a deleted (not connected) state.
  1604. *
  1605. * @data: shost whose rports are to be looked at
  1606. **/
  1607. static void
  1608. fc_shost_remove_rports(void *data)
  1609. {
  1610. struct Scsi_Host *shost = (struct Scsi_Host *)data;
  1611. struct fc_rport *rport, *next_rport;
  1612. unsigned long flags;
  1613. spin_lock_irqsave(shost->host_lock, flags);
  1614. while (fc_host_flags(shost) & FC_SHOST_RPORT_DEL_SCHEDULED) {
  1615. fc_host_flags(shost) &= ~FC_SHOST_RPORT_DEL_SCHEDULED;
  1616. restart_search:
  1617. list_for_each_entry_safe(rport, next_rport,
  1618. &fc_host_rport_bindings(shost), peers) {
  1619. if (rport->port_state == FC_PORTSTATE_DELETED) {
  1620. rport->port_state = FC_PORTSTATE_NOTPRESENT;
  1621. spin_unlock_irqrestore(shost->host_lock, flags);
  1622. fc_rport_tgt_remove(rport);
  1623. spin_lock_irqsave(shost->host_lock, flags);
  1624. goto restart_search;
  1625. }
  1626. }
  1627. }
  1628. spin_unlock_irqrestore(shost->host_lock, flags);
  1629. }
  1630. MODULE_AUTHOR("Martin Hicks");
  1631. MODULE_DESCRIPTION("FC Transport Attributes");
  1632. MODULE_LICENSE("GPL");
  1633. module_init(fc_transport_init);
  1634. module_exit(fc_transport_exit);