scsi_transport_fc.c 56 KB

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