fcoe_sysfs.c 23 KB

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