fcoe_sysfs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * Copyright(c) 2011 - 2012 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/ctype.h>
  24. #include <scsi/fcoe_sysfs.h>
  25. #include <scsi/libfcoe.h>
  26. /*
  27. * OK to include local libfcoe.h for debug_logging, but cannot include
  28. * <scsi/libfcoe.h> otherwise non-netdev based fcoe solutions would have
  29. * have to include more than fcoe_sysfs.h.
  30. */
  31. #include "libfcoe.h"
  32. static atomic_t ctlr_num;
  33. static atomic_t fcf_num;
  34. /*
  35. * fcoe_fcf_dev_loss_tmo: the default number of seconds that fcoe sysfs
  36. * should insulate the loss of a fcf.
  37. */
  38. static unsigned int fcoe_fcf_dev_loss_tmo = 1800; /* seconds */
  39. module_param_named(fcf_dev_loss_tmo, fcoe_fcf_dev_loss_tmo,
  40. uint, S_IRUGO|S_IWUSR);
  41. MODULE_PARM_DESC(fcf_dev_loss_tmo,
  42. "Maximum number of seconds that libfcoe should"
  43. " insulate the loss of a fcf. Once this value is"
  44. " exceeded, the fcf is removed.");
  45. /*
  46. * These are used by the fcoe_*_show_function routines, they
  47. * are intentionally placed in the .c file as they're not intended
  48. * for use throughout the code.
  49. */
  50. #define fcoe_ctlr_id(x) \
  51. ((x)->id)
  52. #define fcoe_ctlr_work_q_name(x) \
  53. ((x)->work_q_name)
  54. #define fcoe_ctlr_work_q(x) \
  55. ((x)->work_q)
  56. #define fcoe_ctlr_devloss_work_q_name(x) \
  57. ((x)->devloss_work_q_name)
  58. #define fcoe_ctlr_devloss_work_q(x) \
  59. ((x)->devloss_work_q)
  60. #define fcoe_ctlr_mode(x) \
  61. ((x)->mode)
  62. #define fcoe_ctlr_fcf_dev_loss_tmo(x) \
  63. ((x)->fcf_dev_loss_tmo)
  64. #define fcoe_ctlr_link_fail(x) \
  65. ((x)->lesb.lesb_link_fail)
  66. #define fcoe_ctlr_vlink_fail(x) \
  67. ((x)->lesb.lesb_vlink_fail)
  68. #define fcoe_ctlr_miss_fka(x) \
  69. ((x)->lesb.lesb_miss_fka)
  70. #define fcoe_ctlr_symb_err(x) \
  71. ((x)->lesb.lesb_symb_err)
  72. #define fcoe_ctlr_err_block(x) \
  73. ((x)->lesb.lesb_err_block)
  74. #define fcoe_ctlr_fcs_error(x) \
  75. ((x)->lesb.lesb_fcs_error)
  76. #define fcoe_ctlr_enabled(x) \
  77. ((x)->enabled)
  78. #define fcoe_fcf_state(x) \
  79. ((x)->state)
  80. #define fcoe_fcf_fabric_name(x) \
  81. ((x)->fabric_name)
  82. #define fcoe_fcf_switch_name(x) \
  83. ((x)->switch_name)
  84. #define fcoe_fcf_fc_map(x) \
  85. ((x)->fc_map)
  86. #define fcoe_fcf_vfid(x) \
  87. ((x)->vfid)
  88. #define fcoe_fcf_mac(x) \
  89. ((x)->mac)
  90. #define fcoe_fcf_priority(x) \
  91. ((x)->priority)
  92. #define fcoe_fcf_fka_period(x) \
  93. ((x)->fka_period)
  94. #define fcoe_fcf_dev_loss_tmo(x) \
  95. ((x)->dev_loss_tmo)
  96. #define fcoe_fcf_selected(x) \
  97. ((x)->selected)
  98. #define fcoe_fcf_vlan_id(x) \
  99. ((x)->vlan_id)
  100. /*
  101. * dev_loss_tmo attribute
  102. */
  103. static int fcoe_str_to_dev_loss(const char *buf, unsigned long *val)
  104. {
  105. int ret;
  106. ret = kstrtoul(buf, 0, val);
  107. if (ret)
  108. return -EINVAL;
  109. /*
  110. * Check for overflow; dev_loss_tmo is u32
  111. */
  112. if (*val > UINT_MAX)
  113. return -EINVAL;
  114. return 0;
  115. }
  116. static int fcoe_fcf_set_dev_loss_tmo(struct fcoe_fcf_device *fcf,
  117. unsigned long val)
  118. {
  119. if ((fcf->state == FCOE_FCF_STATE_UNKNOWN) ||
  120. (fcf->state == FCOE_FCF_STATE_DISCONNECTED) ||
  121. (fcf->state == FCOE_FCF_STATE_DELETED))
  122. return -EBUSY;
  123. /*
  124. * Check for overflow; dev_loss_tmo is u32
  125. */
  126. if (val > UINT_MAX)
  127. return -EINVAL;
  128. fcoe_fcf_dev_loss_tmo(fcf) = val;
  129. return 0;
  130. }
  131. #define FCOE_DEVICE_ATTR(_prefix, _name, _mode, _show, _store) \
  132. struct device_attribute device_attr_fcoe_##_prefix##_##_name = \
  133. __ATTR(_name, _mode, _show, _store)
  134. #define fcoe_ctlr_show_function(field, format_string, sz, cast) \
  135. static ssize_t show_fcoe_ctlr_device_##field(struct device *dev, \
  136. struct device_attribute *attr, \
  137. char *buf) \
  138. { \
  139. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev); \
  140. if (ctlr->f->get_fcoe_ctlr_##field) \
  141. ctlr->f->get_fcoe_ctlr_##field(ctlr); \
  142. return snprintf(buf, sz, format_string, \
  143. cast fcoe_ctlr_##field(ctlr)); \
  144. }
  145. #define fcoe_fcf_show_function(field, format_string, sz, cast) \
  146. static ssize_t show_fcoe_fcf_device_##field(struct device *dev, \
  147. struct device_attribute *attr, \
  148. char *buf) \
  149. { \
  150. struct fcoe_fcf_device *fcf = dev_to_fcf(dev); \
  151. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf); \
  152. if (ctlr->f->get_fcoe_fcf_##field) \
  153. ctlr->f->get_fcoe_fcf_##field(fcf); \
  154. return snprintf(buf, sz, format_string, \
  155. cast fcoe_fcf_##field(fcf)); \
  156. }
  157. #define fcoe_ctlr_private_show_function(field, format_string, sz, cast) \
  158. static ssize_t show_fcoe_ctlr_device_##field(struct device *dev, \
  159. struct device_attribute *attr, \
  160. char *buf) \
  161. { \
  162. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev); \
  163. return snprintf(buf, sz, format_string, cast fcoe_ctlr_##field(ctlr)); \
  164. }
  165. #define fcoe_fcf_private_show_function(field, format_string, sz, cast) \
  166. static ssize_t show_fcoe_fcf_device_##field(struct device *dev, \
  167. struct device_attribute *attr, \
  168. char *buf) \
  169. { \
  170. struct fcoe_fcf_device *fcf = dev_to_fcf(dev); \
  171. return snprintf(buf, sz, format_string, cast fcoe_fcf_##field(fcf)); \
  172. }
  173. #define fcoe_ctlr_private_rd_attr(field, format_string, sz) \
  174. fcoe_ctlr_private_show_function(field, format_string, sz, ) \
  175. static FCOE_DEVICE_ATTR(ctlr, field, S_IRUGO, \
  176. show_fcoe_ctlr_device_##field, NULL)
  177. #define fcoe_ctlr_rd_attr(field, format_string, sz) \
  178. fcoe_ctlr_show_function(field, format_string, sz, ) \
  179. static FCOE_DEVICE_ATTR(ctlr, field, S_IRUGO, \
  180. show_fcoe_ctlr_device_##field, NULL)
  181. #define fcoe_fcf_rd_attr(field, format_string, sz) \
  182. fcoe_fcf_show_function(field, format_string, sz, ) \
  183. static FCOE_DEVICE_ATTR(fcf, field, S_IRUGO, \
  184. show_fcoe_fcf_device_##field, NULL)
  185. #define fcoe_fcf_private_rd_attr(field, format_string, sz) \
  186. fcoe_fcf_private_show_function(field, format_string, sz, ) \
  187. static FCOE_DEVICE_ATTR(fcf, field, S_IRUGO, \
  188. show_fcoe_fcf_device_##field, NULL)
  189. #define fcoe_ctlr_private_rd_attr_cast(field, format_string, sz, cast) \
  190. fcoe_ctlr_private_show_function(field, format_string, sz, (cast)) \
  191. static FCOE_DEVICE_ATTR(ctlr, field, S_IRUGO, \
  192. show_fcoe_ctlr_device_##field, NULL)
  193. #define fcoe_fcf_private_rd_attr_cast(field, format_string, sz, cast) \
  194. fcoe_fcf_private_show_function(field, format_string, sz, (cast)) \
  195. static FCOE_DEVICE_ATTR(fcf, field, S_IRUGO, \
  196. show_fcoe_fcf_device_##field, NULL)
  197. #define fcoe_enum_name_search(title, table_type, table) \
  198. static const char *get_fcoe_##title##_name(enum table_type table_key) \
  199. { \
  200. if (table_key < 0 || table_key >= ARRAY_SIZE(table)) \
  201. return NULL; \
  202. return table[table_key]; \
  203. }
  204. static char *fip_conn_type_names[] = {
  205. [ FIP_CONN_TYPE_UNKNOWN ] = "Unknown",
  206. [ FIP_CONN_TYPE_FABRIC ] = "Fabric",
  207. [ FIP_CONN_TYPE_VN2VN ] = "VN2VN",
  208. };
  209. fcoe_enum_name_search(ctlr_mode, fip_conn_type, fip_conn_type_names)
  210. static enum fip_conn_type fcoe_parse_mode(const char *buf)
  211. {
  212. int i;
  213. for (i = 0; i < ARRAY_SIZE(fip_conn_type_names); i++) {
  214. if (strcasecmp(buf, fip_conn_type_names[i]) == 0)
  215. return i;
  216. }
  217. return FIP_CONN_TYPE_UNKNOWN;
  218. }
  219. static char *fcf_state_names[] = {
  220. [ FCOE_FCF_STATE_UNKNOWN ] = "Unknown",
  221. [ FCOE_FCF_STATE_DISCONNECTED ] = "Disconnected",
  222. [ FCOE_FCF_STATE_CONNECTED ] = "Connected",
  223. };
  224. fcoe_enum_name_search(fcf_state, fcf_state, fcf_state_names)
  225. #define FCOE_FCF_STATE_MAX_NAMELEN 50
  226. static ssize_t show_fcf_state(struct device *dev,
  227. struct device_attribute *attr,
  228. char *buf)
  229. {
  230. struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
  231. const char *name;
  232. name = get_fcoe_fcf_state_name(fcf->state);
  233. if (!name)
  234. return -EINVAL;
  235. return snprintf(buf, FCOE_FCF_STATE_MAX_NAMELEN, "%s\n", name);
  236. }
  237. static FCOE_DEVICE_ATTR(fcf, state, S_IRUGO, show_fcf_state, NULL);
  238. #define FCOE_MAX_MODENAME_LEN 20
  239. static ssize_t show_ctlr_mode(struct device *dev,
  240. struct device_attribute *attr,
  241. char *buf)
  242. {
  243. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  244. const char *name;
  245. name = get_fcoe_ctlr_mode_name(ctlr->mode);
  246. if (!name)
  247. return -EINVAL;
  248. return snprintf(buf, FCOE_MAX_MODENAME_LEN,
  249. "%s\n", name);
  250. }
  251. static ssize_t store_ctlr_mode(struct device *dev,
  252. struct device_attribute *attr,
  253. const char *buf, size_t count)
  254. {
  255. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  256. char mode[FCOE_MAX_MODENAME_LEN + 1];
  257. if (count > FCOE_MAX_MODENAME_LEN)
  258. return -EINVAL;
  259. strncpy(mode, buf, count);
  260. if (mode[count - 1] == '\n')
  261. mode[count - 1] = '\0';
  262. else
  263. mode[count] = '\0';
  264. switch (ctlr->enabled) {
  265. case FCOE_CTLR_ENABLED:
  266. LIBFCOE_SYSFS_DBG(ctlr, "Cannot change mode when enabled.\n");
  267. return -EBUSY;
  268. case FCOE_CTLR_DISABLED:
  269. if (!ctlr->f->set_fcoe_ctlr_mode) {
  270. LIBFCOE_SYSFS_DBG(ctlr,
  271. "Mode change not supported by LLD.\n");
  272. return -ENOTSUPP;
  273. }
  274. ctlr->mode = fcoe_parse_mode(mode);
  275. if (ctlr->mode == FIP_CONN_TYPE_UNKNOWN) {
  276. LIBFCOE_SYSFS_DBG(ctlr, "Unknown mode %s provided.\n",
  277. buf);
  278. return -EINVAL;
  279. }
  280. ctlr->f->set_fcoe_ctlr_mode(ctlr);
  281. LIBFCOE_SYSFS_DBG(ctlr, "Mode changed to %s.\n", buf);
  282. return count;
  283. case FCOE_CTLR_UNUSED:
  284. default:
  285. LIBFCOE_SYSFS_DBG(ctlr, "Mode change not supported.\n");
  286. return -ENOTSUPP;
  287. };
  288. }
  289. static FCOE_DEVICE_ATTR(ctlr, mode, S_IRUGO | S_IWUSR,
  290. show_ctlr_mode, store_ctlr_mode);
  291. static ssize_t store_ctlr_enabled(struct device *dev,
  292. struct device_attribute *attr,
  293. const char *buf, size_t count)
  294. {
  295. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  296. int rc;
  297. switch (ctlr->enabled) {
  298. case FCOE_CTLR_ENABLED:
  299. if (*buf == '1')
  300. return count;
  301. ctlr->enabled = FCOE_CTLR_DISABLED;
  302. break;
  303. case FCOE_CTLR_DISABLED:
  304. if (*buf == '0')
  305. return count;
  306. ctlr->enabled = FCOE_CTLR_ENABLED;
  307. break;
  308. case FCOE_CTLR_UNUSED:
  309. return -ENOTSUPP;
  310. };
  311. rc = ctlr->f->set_fcoe_ctlr_enabled(ctlr);
  312. if (rc)
  313. return rc;
  314. return count;
  315. }
  316. static char *ctlr_enabled_state_names[] = {
  317. [ FCOE_CTLR_ENABLED ] = "1",
  318. [ FCOE_CTLR_DISABLED ] = "0",
  319. };
  320. fcoe_enum_name_search(ctlr_enabled_state, ctlr_enabled_state,
  321. ctlr_enabled_state_names)
  322. #define FCOE_CTLR_ENABLED_MAX_NAMELEN 50
  323. static ssize_t show_ctlr_enabled_state(struct device *dev,
  324. struct device_attribute *attr,
  325. char *buf)
  326. {
  327. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  328. const char *name;
  329. name = get_fcoe_ctlr_enabled_state_name(ctlr->enabled);
  330. if (!name)
  331. return -EINVAL;
  332. return snprintf(buf, FCOE_CTLR_ENABLED_MAX_NAMELEN,
  333. "%s\n", name);
  334. }
  335. static FCOE_DEVICE_ATTR(ctlr, enabled, S_IRUGO | S_IWUSR,
  336. show_ctlr_enabled_state,
  337. store_ctlr_enabled);
  338. static ssize_t
  339. store_private_fcoe_ctlr_fcf_dev_loss_tmo(struct device *dev,
  340. struct device_attribute *attr,
  341. const char *buf, size_t count)
  342. {
  343. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  344. struct fcoe_fcf_device *fcf;
  345. unsigned long val;
  346. int rc;
  347. rc = fcoe_str_to_dev_loss(buf, &val);
  348. if (rc)
  349. return rc;
  350. fcoe_ctlr_fcf_dev_loss_tmo(ctlr) = val;
  351. mutex_lock(&ctlr->lock);
  352. list_for_each_entry(fcf, &ctlr->fcfs, peers)
  353. fcoe_fcf_set_dev_loss_tmo(fcf, val);
  354. mutex_unlock(&ctlr->lock);
  355. return count;
  356. }
  357. fcoe_ctlr_private_show_function(fcf_dev_loss_tmo, "%d\n", 20, );
  358. static FCOE_DEVICE_ATTR(ctlr, fcf_dev_loss_tmo, S_IRUGO | S_IWUSR,
  359. show_fcoe_ctlr_device_fcf_dev_loss_tmo,
  360. store_private_fcoe_ctlr_fcf_dev_loss_tmo);
  361. /* Link Error Status Block (LESB) */
  362. fcoe_ctlr_rd_attr(link_fail, "%u\n", 20);
  363. fcoe_ctlr_rd_attr(vlink_fail, "%u\n", 20);
  364. fcoe_ctlr_rd_attr(miss_fka, "%u\n", 20);
  365. fcoe_ctlr_rd_attr(symb_err, "%u\n", 20);
  366. fcoe_ctlr_rd_attr(err_block, "%u\n", 20);
  367. fcoe_ctlr_rd_attr(fcs_error, "%u\n", 20);
  368. fcoe_fcf_private_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
  369. fcoe_fcf_private_rd_attr_cast(switch_name, "0x%llx\n", 20, unsigned long long);
  370. fcoe_fcf_private_rd_attr(priority, "%u\n", 20);
  371. fcoe_fcf_private_rd_attr(fc_map, "0x%x\n", 20);
  372. fcoe_fcf_private_rd_attr(vfid, "%u\n", 20);
  373. fcoe_fcf_private_rd_attr(mac, "%pM\n", 20);
  374. fcoe_fcf_private_rd_attr(fka_period, "%u\n", 20);
  375. fcoe_fcf_rd_attr(selected, "%u\n", 20);
  376. fcoe_fcf_rd_attr(vlan_id, "%u\n", 20);
  377. fcoe_fcf_private_show_function(dev_loss_tmo, "%d\n", 20, )
  378. static ssize_t
  379. store_fcoe_fcf_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
  380. const char *buf, size_t count)
  381. {
  382. struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
  383. unsigned long val;
  384. int rc;
  385. rc = fcoe_str_to_dev_loss(buf, &val);
  386. if (rc)
  387. return rc;
  388. rc = fcoe_fcf_set_dev_loss_tmo(fcf, val);
  389. if (rc)
  390. return rc;
  391. return count;
  392. }
  393. static FCOE_DEVICE_ATTR(fcf, dev_loss_tmo, S_IRUGO | S_IWUSR,
  394. show_fcoe_fcf_device_dev_loss_tmo,
  395. store_fcoe_fcf_dev_loss_tmo);
  396. static struct attribute *fcoe_ctlr_lesb_attrs[] = {
  397. &device_attr_fcoe_ctlr_link_fail.attr,
  398. &device_attr_fcoe_ctlr_vlink_fail.attr,
  399. &device_attr_fcoe_ctlr_miss_fka.attr,
  400. &device_attr_fcoe_ctlr_symb_err.attr,
  401. &device_attr_fcoe_ctlr_err_block.attr,
  402. &device_attr_fcoe_ctlr_fcs_error.attr,
  403. NULL,
  404. };
  405. static struct attribute_group fcoe_ctlr_lesb_attr_group = {
  406. .name = "lesb",
  407. .attrs = fcoe_ctlr_lesb_attrs,
  408. };
  409. static struct attribute *fcoe_ctlr_attrs[] = {
  410. &device_attr_fcoe_ctlr_fcf_dev_loss_tmo.attr,
  411. &device_attr_fcoe_ctlr_enabled.attr,
  412. &device_attr_fcoe_ctlr_mode.attr,
  413. NULL,
  414. };
  415. static struct attribute_group fcoe_ctlr_attr_group = {
  416. .attrs = fcoe_ctlr_attrs,
  417. };
  418. static const struct attribute_group *fcoe_ctlr_attr_groups[] = {
  419. &fcoe_ctlr_attr_group,
  420. &fcoe_ctlr_lesb_attr_group,
  421. NULL,
  422. };
  423. static struct attribute *fcoe_fcf_attrs[] = {
  424. &device_attr_fcoe_fcf_fabric_name.attr,
  425. &device_attr_fcoe_fcf_switch_name.attr,
  426. &device_attr_fcoe_fcf_dev_loss_tmo.attr,
  427. &device_attr_fcoe_fcf_fc_map.attr,
  428. &device_attr_fcoe_fcf_vfid.attr,
  429. &device_attr_fcoe_fcf_mac.attr,
  430. &device_attr_fcoe_fcf_priority.attr,
  431. &device_attr_fcoe_fcf_fka_period.attr,
  432. &device_attr_fcoe_fcf_state.attr,
  433. &device_attr_fcoe_fcf_selected.attr,
  434. &device_attr_fcoe_fcf_vlan_id.attr,
  435. NULL
  436. };
  437. static struct attribute_group fcoe_fcf_attr_group = {
  438. .attrs = fcoe_fcf_attrs,
  439. };
  440. static const struct attribute_group *fcoe_fcf_attr_groups[] = {
  441. &fcoe_fcf_attr_group,
  442. NULL,
  443. };
  444. static struct bus_type fcoe_bus_type;
  445. static int fcoe_bus_match(struct device *dev,
  446. struct device_driver *drv)
  447. {
  448. if (dev->bus == &fcoe_bus_type)
  449. return 1;
  450. return 0;
  451. }
  452. /**
  453. * fcoe_ctlr_device_release() - Release the FIP ctlr memory
  454. * @dev: Pointer to the FIP ctlr's embedded device
  455. *
  456. * Called when the last FIP ctlr reference is released.
  457. */
  458. static void fcoe_ctlr_device_release(struct device *dev)
  459. {
  460. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  461. kfree(ctlr);
  462. }
  463. /**
  464. * fcoe_fcf_device_release() - Release the FIP fcf memory
  465. * @dev: Pointer to the fcf's embedded device
  466. *
  467. * Called when the last FIP fcf reference is released.
  468. */
  469. static void fcoe_fcf_device_release(struct device *dev)
  470. {
  471. struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
  472. kfree(fcf);
  473. }
  474. static struct device_type fcoe_ctlr_device_type = {
  475. .name = "fcoe_ctlr",
  476. .groups = fcoe_ctlr_attr_groups,
  477. .release = fcoe_ctlr_device_release,
  478. };
  479. static struct device_type fcoe_fcf_device_type = {
  480. .name = "fcoe_fcf",
  481. .groups = fcoe_fcf_attr_groups,
  482. .release = fcoe_fcf_device_release,
  483. };
  484. static BUS_ATTR(ctlr_create, S_IWUSR, NULL, fcoe_ctlr_create_store);
  485. static BUS_ATTR(ctlr_destroy, S_IWUSR, NULL, fcoe_ctlr_destroy_store);
  486. static struct attribute *fcoe_bus_attrs[] = {
  487. &bus_attr_ctlr_create.attr,
  488. &bus_attr_ctlr_destroy.attr,
  489. NULL,
  490. };
  491. ATTRIBUTE_GROUPS(fcoe_bus);
  492. static struct bus_type fcoe_bus_type = {
  493. .name = "fcoe",
  494. .match = &fcoe_bus_match,
  495. .bus_groups = fcoe_bus_groups,
  496. };
  497. /**
  498. * fcoe_ctlr_device_flush_work() - Flush a FIP ctlr's workqueue
  499. * @ctlr: Pointer to the FIP ctlr whose workqueue is to be flushed
  500. */
  501. static void fcoe_ctlr_device_flush_work(struct fcoe_ctlr_device *ctlr)
  502. {
  503. if (!fcoe_ctlr_work_q(ctlr)) {
  504. printk(KERN_ERR
  505. "ERROR: FIP Ctlr '%d' attempted to flush work, "
  506. "when no workqueue created.\n", ctlr->id);
  507. dump_stack();
  508. return;
  509. }
  510. flush_workqueue(fcoe_ctlr_work_q(ctlr));
  511. }
  512. /**
  513. * fcoe_ctlr_device_queue_work() - Schedule work for a FIP ctlr's workqueue
  514. * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
  515. * @work: Work to queue for execution
  516. *
  517. * Return value:
  518. * 1 on success / 0 already queued / < 0 for error
  519. */
  520. static int fcoe_ctlr_device_queue_work(struct fcoe_ctlr_device *ctlr,
  521. struct work_struct *work)
  522. {
  523. if (unlikely(!fcoe_ctlr_work_q(ctlr))) {
  524. printk(KERN_ERR
  525. "ERROR: FIP Ctlr '%d' attempted to queue work, "
  526. "when no workqueue created.\n", ctlr->id);
  527. dump_stack();
  528. return -EINVAL;
  529. }
  530. return queue_work(fcoe_ctlr_work_q(ctlr), work);
  531. }
  532. /**
  533. * fcoe_ctlr_device_flush_devloss() - Flush a FIP ctlr's devloss workqueue
  534. * @ctlr: Pointer to FIP ctlr whose workqueue is to be flushed
  535. */
  536. static void fcoe_ctlr_device_flush_devloss(struct fcoe_ctlr_device *ctlr)
  537. {
  538. if (!fcoe_ctlr_devloss_work_q(ctlr)) {
  539. printk(KERN_ERR
  540. "ERROR: FIP Ctlr '%d' attempted to flush work, "
  541. "when no workqueue created.\n", ctlr->id);
  542. dump_stack();
  543. return;
  544. }
  545. flush_workqueue(fcoe_ctlr_devloss_work_q(ctlr));
  546. }
  547. /**
  548. * fcoe_ctlr_device_queue_devloss_work() - Schedule work for a FIP ctlr's devloss workqueue
  549. * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
  550. * @work: Work to queue for execution
  551. * @delay: jiffies to delay the work queuing
  552. *
  553. * Return value:
  554. * 1 on success / 0 already queued / < 0 for error
  555. */
  556. static int fcoe_ctlr_device_queue_devloss_work(struct fcoe_ctlr_device *ctlr,
  557. struct delayed_work *work,
  558. unsigned long delay)
  559. {
  560. if (unlikely(!fcoe_ctlr_devloss_work_q(ctlr))) {
  561. printk(KERN_ERR
  562. "ERROR: FIP Ctlr '%d' attempted to queue work, "
  563. "when no workqueue created.\n", ctlr->id);
  564. dump_stack();
  565. return -EINVAL;
  566. }
  567. return queue_delayed_work(fcoe_ctlr_devloss_work_q(ctlr), work, delay);
  568. }
  569. static int fcoe_fcf_device_match(struct fcoe_fcf_device *new,
  570. struct fcoe_fcf_device *old)
  571. {
  572. if (new->switch_name == old->switch_name &&
  573. new->fabric_name == old->fabric_name &&
  574. new->fc_map == old->fc_map &&
  575. ether_addr_equal(new->mac, old->mac))
  576. return 1;
  577. return 0;
  578. }
  579. /**
  580. * fcoe_ctlr_device_add() - Add a FIP ctlr to sysfs
  581. * @parent: The parent device to which the fcoe_ctlr instance
  582. * should be attached
  583. * @f: The LLD's FCoE sysfs function template pointer
  584. * @priv_size: Size to be allocated with the fcoe_ctlr_device for the LLD
  585. *
  586. * This routine allocates a FIP ctlr object with some additional memory
  587. * for the LLD. The FIP ctlr is initialized, added to sysfs and then
  588. * attributes are added to it.
  589. */
  590. struct fcoe_ctlr_device *fcoe_ctlr_device_add(struct device *parent,
  591. struct fcoe_sysfs_function_template *f,
  592. int priv_size)
  593. {
  594. struct fcoe_ctlr_device *ctlr;
  595. int error = 0;
  596. ctlr = kzalloc(sizeof(struct fcoe_ctlr_device) + priv_size,
  597. GFP_KERNEL);
  598. if (!ctlr)
  599. goto out;
  600. ctlr->id = atomic_inc_return(&ctlr_num) - 1;
  601. ctlr->f = f;
  602. ctlr->mode = FIP_CONN_TYPE_FABRIC;
  603. INIT_LIST_HEAD(&ctlr->fcfs);
  604. mutex_init(&ctlr->lock);
  605. ctlr->dev.parent = parent;
  606. ctlr->dev.bus = &fcoe_bus_type;
  607. ctlr->dev.type = &fcoe_ctlr_device_type;
  608. ctlr->fcf_dev_loss_tmo = fcoe_fcf_dev_loss_tmo;
  609. snprintf(ctlr->work_q_name, sizeof(ctlr->work_q_name),
  610. "ctlr_wq_%d", ctlr->id);
  611. ctlr->work_q = create_singlethread_workqueue(
  612. ctlr->work_q_name);
  613. if (!ctlr->work_q)
  614. goto out_del;
  615. snprintf(ctlr->devloss_work_q_name,
  616. sizeof(ctlr->devloss_work_q_name),
  617. "ctlr_dl_wq_%d", ctlr->id);
  618. ctlr->devloss_work_q = create_singlethread_workqueue(
  619. ctlr->devloss_work_q_name);
  620. if (!ctlr->devloss_work_q)
  621. goto out_del_q;
  622. dev_set_name(&ctlr->dev, "ctlr_%d", ctlr->id);
  623. error = device_register(&ctlr->dev);
  624. if (error)
  625. goto out_del_q2;
  626. return ctlr;
  627. out_del_q2:
  628. destroy_workqueue(ctlr->devloss_work_q);
  629. ctlr->devloss_work_q = NULL;
  630. out_del_q:
  631. destroy_workqueue(ctlr->work_q);
  632. ctlr->work_q = NULL;
  633. out_del:
  634. kfree(ctlr);
  635. out:
  636. return NULL;
  637. }
  638. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_add);
  639. /**
  640. * fcoe_ctlr_device_delete() - Delete a FIP ctlr and its subtree from sysfs
  641. * @ctlr: A pointer to the ctlr to be deleted
  642. *
  643. * Deletes a FIP ctlr and any fcfs attached
  644. * to it. Deleting fcfs will cause their childen
  645. * to be deleted as well.
  646. *
  647. * The ctlr is detached from sysfs and it's resources
  648. * are freed (work q), but the memory is not freed
  649. * until its last reference is released.
  650. *
  651. * This routine expects no locks to be held before
  652. * calling.
  653. *
  654. * TODO: Currently there are no callbacks to clean up LLD data
  655. * for a fcoe_fcf_device. LLDs must keep this in mind as they need
  656. * to clean up each of their LLD data for all fcoe_fcf_device before
  657. * calling fcoe_ctlr_device_delete.
  658. */
  659. void fcoe_ctlr_device_delete(struct fcoe_ctlr_device *ctlr)
  660. {
  661. struct fcoe_fcf_device *fcf, *next;
  662. /* Remove any attached fcfs */
  663. mutex_lock(&ctlr->lock);
  664. list_for_each_entry_safe(fcf, next,
  665. &ctlr->fcfs, peers) {
  666. list_del(&fcf->peers);
  667. fcf->state = FCOE_FCF_STATE_DELETED;
  668. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  669. }
  670. mutex_unlock(&ctlr->lock);
  671. fcoe_ctlr_device_flush_work(ctlr);
  672. destroy_workqueue(ctlr->devloss_work_q);
  673. ctlr->devloss_work_q = NULL;
  674. destroy_workqueue(ctlr->work_q);
  675. ctlr->work_q = NULL;
  676. device_unregister(&ctlr->dev);
  677. }
  678. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_delete);
  679. /**
  680. * fcoe_fcf_device_final_delete() - Final delete routine
  681. * @work: The FIP fcf's embedded work struct
  682. *
  683. * It is expected that the fcf has been removed from
  684. * the FIP ctlr's list before calling this routine.
  685. */
  686. static void fcoe_fcf_device_final_delete(struct work_struct *work)
  687. {
  688. struct fcoe_fcf_device *fcf =
  689. container_of(work, struct fcoe_fcf_device, delete_work);
  690. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  691. /*
  692. * Cancel any outstanding timers. These should really exist
  693. * only when rmmod'ing the LLDD and we're asking for
  694. * immediate termination of the rports
  695. */
  696. if (!cancel_delayed_work(&fcf->dev_loss_work))
  697. fcoe_ctlr_device_flush_devloss(ctlr);
  698. device_unregister(&fcf->dev);
  699. }
  700. /**
  701. * fip_timeout_deleted_fcf() - Delete a fcf when the devloss timer fires
  702. * @work: The FIP fcf's embedded work struct
  703. *
  704. * Removes the fcf from the FIP ctlr's list of fcfs and
  705. * queues the final deletion.
  706. */
  707. static void fip_timeout_deleted_fcf(struct work_struct *work)
  708. {
  709. struct fcoe_fcf_device *fcf =
  710. container_of(work, struct fcoe_fcf_device, dev_loss_work.work);
  711. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  712. mutex_lock(&ctlr->lock);
  713. /*
  714. * If the fcf is deleted or reconnected before the timer
  715. * fires the devloss queue will be flushed, but the state will
  716. * either be CONNECTED or DELETED. If that is the case we
  717. * cancel deleting the fcf.
  718. */
  719. if (fcf->state != FCOE_FCF_STATE_DISCONNECTED)
  720. goto out;
  721. dev_printk(KERN_ERR, &fcf->dev,
  722. "FIP fcf connection time out: removing fcf\n");
  723. list_del(&fcf->peers);
  724. fcf->state = FCOE_FCF_STATE_DELETED;
  725. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  726. out:
  727. mutex_unlock(&ctlr->lock);
  728. }
  729. /**
  730. * fcoe_fcf_device_delete() - Delete a FIP fcf
  731. * @fcf: Pointer to the fcf which is to be deleted
  732. *
  733. * Queues the FIP fcf on the devloss workqueue
  734. *
  735. * Expects the ctlr_attrs mutex to be held for fcf
  736. * state change.
  737. */
  738. void fcoe_fcf_device_delete(struct fcoe_fcf_device *fcf)
  739. {
  740. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  741. int timeout = fcf->dev_loss_tmo;
  742. if (fcf->state != FCOE_FCF_STATE_CONNECTED)
  743. return;
  744. fcf->state = FCOE_FCF_STATE_DISCONNECTED;
  745. /*
  746. * FCF will only be re-connected by the LLD calling
  747. * fcoe_fcf_device_add, and it should be setting up
  748. * priv then.
  749. */
  750. fcf->priv = NULL;
  751. fcoe_ctlr_device_queue_devloss_work(ctlr, &fcf->dev_loss_work,
  752. timeout * HZ);
  753. }
  754. EXPORT_SYMBOL_GPL(fcoe_fcf_device_delete);
  755. /**
  756. * fcoe_fcf_device_add() - Add a FCoE sysfs fcoe_fcf_device to the system
  757. * @ctlr: The fcoe_ctlr_device that will be the fcoe_fcf_device parent
  758. * @new_fcf: A temporary FCF used for lookups on the current list of fcfs
  759. *
  760. * Expects to be called with the ctlr->lock held
  761. */
  762. struct fcoe_fcf_device *fcoe_fcf_device_add(struct fcoe_ctlr_device *ctlr,
  763. struct fcoe_fcf_device *new_fcf)
  764. {
  765. struct fcoe_fcf_device *fcf;
  766. int error = 0;
  767. list_for_each_entry(fcf, &ctlr->fcfs, peers) {
  768. if (fcoe_fcf_device_match(new_fcf, fcf)) {
  769. if (fcf->state == FCOE_FCF_STATE_CONNECTED)
  770. return fcf;
  771. fcf->state = FCOE_FCF_STATE_CONNECTED;
  772. if (!cancel_delayed_work(&fcf->dev_loss_work))
  773. fcoe_ctlr_device_flush_devloss(ctlr);
  774. return fcf;
  775. }
  776. }
  777. fcf = kzalloc(sizeof(struct fcoe_fcf_device), GFP_ATOMIC);
  778. if (unlikely(!fcf))
  779. goto out;
  780. INIT_WORK(&fcf->delete_work, fcoe_fcf_device_final_delete);
  781. INIT_DELAYED_WORK(&fcf->dev_loss_work, fip_timeout_deleted_fcf);
  782. fcf->dev.parent = &ctlr->dev;
  783. fcf->dev.bus = &fcoe_bus_type;
  784. fcf->dev.type = &fcoe_fcf_device_type;
  785. fcf->id = atomic_inc_return(&fcf_num) - 1;
  786. fcf->state = FCOE_FCF_STATE_UNKNOWN;
  787. fcf->dev_loss_tmo = ctlr->fcf_dev_loss_tmo;
  788. dev_set_name(&fcf->dev, "fcf_%d", fcf->id);
  789. fcf->fabric_name = new_fcf->fabric_name;
  790. fcf->switch_name = new_fcf->switch_name;
  791. fcf->fc_map = new_fcf->fc_map;
  792. fcf->vfid = new_fcf->vfid;
  793. memcpy(fcf->mac, new_fcf->mac, ETH_ALEN);
  794. fcf->priority = new_fcf->priority;
  795. fcf->fka_period = new_fcf->fka_period;
  796. fcf->selected = new_fcf->selected;
  797. error = device_register(&fcf->dev);
  798. if (error)
  799. goto out_del;
  800. fcf->state = FCOE_FCF_STATE_CONNECTED;
  801. list_add_tail(&fcf->peers, &ctlr->fcfs);
  802. return fcf;
  803. out_del:
  804. kfree(fcf);
  805. out:
  806. return NULL;
  807. }
  808. EXPORT_SYMBOL_GPL(fcoe_fcf_device_add);
  809. int __init fcoe_sysfs_setup(void)
  810. {
  811. int error;
  812. atomic_set(&ctlr_num, 0);
  813. atomic_set(&fcf_num, 0);
  814. error = bus_register(&fcoe_bus_type);
  815. if (error)
  816. return error;
  817. return 0;
  818. }
  819. void __exit fcoe_sysfs_teardown(void)
  820. {
  821. bus_unregister(&fcoe_bus_type);
  822. }