fcoe_sysfs.c 23 KB

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