scsi_transport_fc.c 57 KB

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