fcoe_sysfs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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.");
  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.");
  272. return -ENOTSUPP;
  273. }
  274. ctlr->mode = fcoe_parse_mode(mode);
  275. if (ctlr->mode == FIP_CONN_TYPE_UNKNOWN) {
  276. LIBFCOE_SYSFS_DBG(ctlr,
  277. "Unknown mode %s provided.", buf);
  278. return -EINVAL;
  279. }
  280. ctlr->f->set_fcoe_ctlr_mode(ctlr);
  281. LIBFCOE_SYSFS_DBG(ctlr, "Mode changed to %s.", buf);
  282. return count;
  283. case FCOE_CTLR_UNUSED:
  284. default:
  285. LIBFCOE_SYSFS_DBG(ctlr, "Mode change not supported.");
  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. 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. 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. 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. struct bus_attribute fcoe_bus_attr_group[] = {
  485. __ATTR(ctlr_create, S_IWUSR, NULL, fcoe_ctlr_create_store),
  486. __ATTR(ctlr_destroy, S_IWUSR, NULL, fcoe_ctlr_destroy_store),
  487. __ATTR_NULL
  488. };
  489. struct bus_type fcoe_bus_type = {
  490. .name = "fcoe",
  491. .match = &fcoe_bus_match,
  492. .bus_attrs = fcoe_bus_attr_group,
  493. };
  494. /**
  495. * fcoe_ctlr_device_flush_work() - Flush a FIP ctlr's workqueue
  496. * @ctlr: Pointer to the FIP ctlr whose workqueue is to be flushed
  497. */
  498. void fcoe_ctlr_device_flush_work(struct fcoe_ctlr_device *ctlr)
  499. {
  500. if (!fcoe_ctlr_work_q(ctlr)) {
  501. printk(KERN_ERR
  502. "ERROR: FIP Ctlr '%d' attempted to flush work, "
  503. "when no workqueue created.\n", ctlr->id);
  504. dump_stack();
  505. return;
  506. }
  507. flush_workqueue(fcoe_ctlr_work_q(ctlr));
  508. }
  509. /**
  510. * fcoe_ctlr_device_queue_work() - Schedule work for a FIP ctlr's workqueue
  511. * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
  512. * @work: Work to queue for execution
  513. *
  514. * Return value:
  515. * 1 on success / 0 already queued / < 0 for error
  516. */
  517. int fcoe_ctlr_device_queue_work(struct fcoe_ctlr_device *ctlr,
  518. struct work_struct *work)
  519. {
  520. if (unlikely(!fcoe_ctlr_work_q(ctlr))) {
  521. printk(KERN_ERR
  522. "ERROR: FIP Ctlr '%d' attempted to queue work, "
  523. "when no workqueue created.\n", ctlr->id);
  524. dump_stack();
  525. return -EINVAL;
  526. }
  527. return queue_work(fcoe_ctlr_work_q(ctlr), work);
  528. }
  529. /**
  530. * fcoe_ctlr_device_flush_devloss() - Flush a FIP ctlr's devloss workqueue
  531. * @ctlr: Pointer to FIP ctlr whose workqueue is to be flushed
  532. */
  533. void fcoe_ctlr_device_flush_devloss(struct fcoe_ctlr_device *ctlr)
  534. {
  535. if (!fcoe_ctlr_devloss_work_q(ctlr)) {
  536. printk(KERN_ERR
  537. "ERROR: FIP Ctlr '%d' attempted to flush work, "
  538. "when no workqueue created.\n", ctlr->id);
  539. dump_stack();
  540. return;
  541. }
  542. flush_workqueue(fcoe_ctlr_devloss_work_q(ctlr));
  543. }
  544. /**
  545. * fcoe_ctlr_device_queue_devloss_work() - Schedule work for a FIP ctlr's devloss workqueue
  546. * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
  547. * @work: Work to queue for execution
  548. * @delay: jiffies to delay the work queuing
  549. *
  550. * Return value:
  551. * 1 on success / 0 already queued / < 0 for error
  552. */
  553. int fcoe_ctlr_device_queue_devloss_work(struct fcoe_ctlr_device *ctlr,
  554. struct delayed_work *work,
  555. unsigned long delay)
  556. {
  557. if (unlikely(!fcoe_ctlr_devloss_work_q(ctlr))) {
  558. printk(KERN_ERR
  559. "ERROR: FIP Ctlr '%d' attempted to queue work, "
  560. "when no workqueue created.\n", ctlr->id);
  561. dump_stack();
  562. return -EINVAL;
  563. }
  564. return queue_delayed_work(fcoe_ctlr_devloss_work_q(ctlr), work, delay);
  565. }
  566. static int fcoe_fcf_device_match(struct fcoe_fcf_device *new,
  567. struct fcoe_fcf_device *old)
  568. {
  569. if (new->switch_name == old->switch_name &&
  570. new->fabric_name == old->fabric_name &&
  571. new->fc_map == old->fc_map &&
  572. compare_ether_addr(new->mac, old->mac) == 0)
  573. return 1;
  574. return 0;
  575. }
  576. /**
  577. * fcoe_ctlr_device_add() - Add a FIP ctlr to sysfs
  578. * @parent: The parent device to which the fcoe_ctlr instance
  579. * should be attached
  580. * @f: The LLD's FCoE sysfs function template pointer
  581. * @priv_size: Size to be allocated with the fcoe_ctlr_device for the LLD
  582. *
  583. * This routine allocates a FIP ctlr object with some additional memory
  584. * for the LLD. The FIP ctlr is initialized, added to sysfs and then
  585. * attributes are added to it.
  586. */
  587. struct fcoe_ctlr_device *fcoe_ctlr_device_add(struct device *parent,
  588. struct fcoe_sysfs_function_template *f,
  589. int priv_size)
  590. {
  591. struct fcoe_ctlr_device *ctlr;
  592. int error = 0;
  593. ctlr = kzalloc(sizeof(struct fcoe_ctlr_device) + priv_size,
  594. GFP_KERNEL);
  595. if (!ctlr)
  596. goto out;
  597. ctlr->id = atomic_inc_return(&ctlr_num) - 1;
  598. ctlr->f = f;
  599. ctlr->mode = FIP_CONN_TYPE_FABRIC;
  600. INIT_LIST_HEAD(&ctlr->fcfs);
  601. mutex_init(&ctlr->lock);
  602. ctlr->dev.parent = parent;
  603. ctlr->dev.bus = &fcoe_bus_type;
  604. ctlr->dev.type = &fcoe_ctlr_device_type;
  605. ctlr->fcf_dev_loss_tmo = fcoe_fcf_dev_loss_tmo;
  606. snprintf(ctlr->work_q_name, sizeof(ctlr->work_q_name),
  607. "ctlr_wq_%d", ctlr->id);
  608. ctlr->work_q = create_singlethread_workqueue(
  609. ctlr->work_q_name);
  610. if (!ctlr->work_q)
  611. goto out_del;
  612. snprintf(ctlr->devloss_work_q_name,
  613. sizeof(ctlr->devloss_work_q_name),
  614. "ctlr_dl_wq_%d", ctlr->id);
  615. ctlr->devloss_work_q = create_singlethread_workqueue(
  616. ctlr->devloss_work_q_name);
  617. if (!ctlr->devloss_work_q)
  618. goto out_del_q;
  619. dev_set_name(&ctlr->dev, "ctlr_%d", ctlr->id);
  620. error = device_register(&ctlr->dev);
  621. if (error)
  622. goto out_del_q2;
  623. return ctlr;
  624. out_del_q2:
  625. destroy_workqueue(ctlr->devloss_work_q);
  626. ctlr->devloss_work_q = NULL;
  627. out_del_q:
  628. destroy_workqueue(ctlr->work_q);
  629. ctlr->work_q = NULL;
  630. out_del:
  631. kfree(ctlr);
  632. out:
  633. return NULL;
  634. }
  635. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_add);
  636. /**
  637. * fcoe_ctlr_device_delete() - Delete a FIP ctlr and its subtree from sysfs
  638. * @ctlr: A pointer to the ctlr to be deleted
  639. *
  640. * Deletes a FIP ctlr and any fcfs attached
  641. * to it. Deleting fcfs will cause their childen
  642. * to be deleted as well.
  643. *
  644. * The ctlr is detached from sysfs and it's resources
  645. * are freed (work q), but the memory is not freed
  646. * until its last reference is released.
  647. *
  648. * This routine expects no locks to be held before
  649. * calling.
  650. *
  651. * TODO: Currently there are no callbacks to clean up LLD data
  652. * for a fcoe_fcf_device. LLDs must keep this in mind as they need
  653. * to clean up each of their LLD data for all fcoe_fcf_device before
  654. * calling fcoe_ctlr_device_delete.
  655. */
  656. void fcoe_ctlr_device_delete(struct fcoe_ctlr_device *ctlr)
  657. {
  658. struct fcoe_fcf_device *fcf, *next;
  659. /* Remove any attached fcfs */
  660. mutex_lock(&ctlr->lock);
  661. list_for_each_entry_safe(fcf, next,
  662. &ctlr->fcfs, peers) {
  663. list_del(&fcf->peers);
  664. fcf->state = FCOE_FCF_STATE_DELETED;
  665. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  666. }
  667. mutex_unlock(&ctlr->lock);
  668. fcoe_ctlr_device_flush_work(ctlr);
  669. destroy_workqueue(ctlr->devloss_work_q);
  670. ctlr->devloss_work_q = NULL;
  671. destroy_workqueue(ctlr->work_q);
  672. ctlr->work_q = NULL;
  673. device_unregister(&ctlr->dev);
  674. }
  675. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_delete);
  676. /**
  677. * fcoe_fcf_device_final_delete() - Final delete routine
  678. * @work: The FIP fcf's embedded work struct
  679. *
  680. * It is expected that the fcf has been removed from
  681. * the FIP ctlr's list before calling this routine.
  682. */
  683. static void fcoe_fcf_device_final_delete(struct work_struct *work)
  684. {
  685. struct fcoe_fcf_device *fcf =
  686. container_of(work, struct fcoe_fcf_device, delete_work);
  687. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  688. /*
  689. * Cancel any outstanding timers. These should really exist
  690. * only when rmmod'ing the LLDD and we're asking for
  691. * immediate termination of the rports
  692. */
  693. if (!cancel_delayed_work(&fcf->dev_loss_work))
  694. fcoe_ctlr_device_flush_devloss(ctlr);
  695. device_unregister(&fcf->dev);
  696. }
  697. /**
  698. * fip_timeout_deleted_fcf() - Delete a fcf when the devloss timer fires
  699. * @work: The FIP fcf's embedded work struct
  700. *
  701. * Removes the fcf from the FIP ctlr's list of fcfs and
  702. * queues the final deletion.
  703. */
  704. static void fip_timeout_deleted_fcf(struct work_struct *work)
  705. {
  706. struct fcoe_fcf_device *fcf =
  707. container_of(work, struct fcoe_fcf_device, dev_loss_work.work);
  708. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  709. mutex_lock(&ctlr->lock);
  710. /*
  711. * If the fcf is deleted or reconnected before the timer
  712. * fires the devloss queue will be flushed, but the state will
  713. * either be CONNECTED or DELETED. If that is the case we
  714. * cancel deleting the fcf.
  715. */
  716. if (fcf->state != FCOE_FCF_STATE_DISCONNECTED)
  717. goto out;
  718. dev_printk(KERN_ERR, &fcf->dev,
  719. "FIP fcf connection time out: removing fcf\n");
  720. list_del(&fcf->peers);
  721. fcf->state = FCOE_FCF_STATE_DELETED;
  722. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  723. out:
  724. mutex_unlock(&ctlr->lock);
  725. }
  726. /**
  727. * fcoe_fcf_device_delete() - Delete a FIP fcf
  728. * @fcf: Pointer to the fcf which is to be deleted
  729. *
  730. * Queues the FIP fcf on the devloss workqueue
  731. *
  732. * Expects the ctlr_attrs mutex to be held for fcf
  733. * state change.
  734. */
  735. void fcoe_fcf_device_delete(struct fcoe_fcf_device *fcf)
  736. {
  737. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  738. int timeout = fcf->dev_loss_tmo;
  739. if (fcf->state != FCOE_FCF_STATE_CONNECTED)
  740. return;
  741. fcf->state = FCOE_FCF_STATE_DISCONNECTED;
  742. /*
  743. * FCF will only be re-connected by the LLD calling
  744. * fcoe_fcf_device_add, and it should be setting up
  745. * priv then.
  746. */
  747. fcf->priv = NULL;
  748. fcoe_ctlr_device_queue_devloss_work(ctlr, &fcf->dev_loss_work,
  749. timeout * HZ);
  750. }
  751. EXPORT_SYMBOL_GPL(fcoe_fcf_device_delete);
  752. /**
  753. * fcoe_fcf_device_add() - Add a FCoE sysfs fcoe_fcf_device to the system
  754. * @ctlr: The fcoe_ctlr_device that will be the fcoe_fcf_device parent
  755. * @new_fcf: A temporary FCF used for lookups on the current list of fcfs
  756. *
  757. * Expects to be called with the ctlr->lock held
  758. */
  759. struct fcoe_fcf_device *fcoe_fcf_device_add(struct fcoe_ctlr_device *ctlr,
  760. struct fcoe_fcf_device *new_fcf)
  761. {
  762. struct fcoe_fcf_device *fcf;
  763. int error = 0;
  764. list_for_each_entry(fcf, &ctlr->fcfs, peers) {
  765. if (fcoe_fcf_device_match(new_fcf, fcf)) {
  766. if (fcf->state == FCOE_FCF_STATE_CONNECTED)
  767. return fcf;
  768. fcf->state = FCOE_FCF_STATE_CONNECTED;
  769. if (!cancel_delayed_work(&fcf->dev_loss_work))
  770. fcoe_ctlr_device_flush_devloss(ctlr);
  771. return fcf;
  772. }
  773. }
  774. fcf = kzalloc(sizeof(struct fcoe_fcf_device), GFP_ATOMIC);
  775. if (unlikely(!fcf))
  776. goto out;
  777. INIT_WORK(&fcf->delete_work, fcoe_fcf_device_final_delete);
  778. INIT_DELAYED_WORK(&fcf->dev_loss_work, fip_timeout_deleted_fcf);
  779. fcf->dev.parent = &ctlr->dev;
  780. fcf->dev.bus = &fcoe_bus_type;
  781. fcf->dev.type = &fcoe_fcf_device_type;
  782. fcf->id = atomic_inc_return(&fcf_num) - 1;
  783. fcf->state = FCOE_FCF_STATE_UNKNOWN;
  784. fcf->dev_loss_tmo = ctlr->fcf_dev_loss_tmo;
  785. dev_set_name(&fcf->dev, "fcf_%d", fcf->id);
  786. fcf->fabric_name = new_fcf->fabric_name;
  787. fcf->switch_name = new_fcf->switch_name;
  788. fcf->fc_map = new_fcf->fc_map;
  789. fcf->vfid = new_fcf->vfid;
  790. memcpy(fcf->mac, new_fcf->mac, ETH_ALEN);
  791. fcf->priority = new_fcf->priority;
  792. fcf->fka_period = new_fcf->fka_period;
  793. fcf->selected = new_fcf->selected;
  794. error = device_register(&fcf->dev);
  795. if (error)
  796. goto out_del;
  797. fcf->state = FCOE_FCF_STATE_CONNECTED;
  798. list_add_tail(&fcf->peers, &ctlr->fcfs);
  799. return fcf;
  800. out_del:
  801. kfree(fcf);
  802. out:
  803. return NULL;
  804. }
  805. EXPORT_SYMBOL_GPL(fcoe_fcf_device_add);
  806. int __init fcoe_sysfs_setup(void)
  807. {
  808. int error;
  809. atomic_set(&ctlr_num, 0);
  810. atomic_set(&fcf_num, 0);
  811. error = bus_register(&fcoe_bus_type);
  812. if (error)
  813. return error;
  814. return 0;
  815. }
  816. void __exit fcoe_sysfs_teardown(void)
  817. {
  818. bus_unregister(&fcoe_bus_type);
  819. }