scsi_transport_fc.c 63 KB

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