fcoe_sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. if (table_key < 0 || table_key >= ARRAY_SIZE(table)) \
  191. return NULL; \
  192. return table[table_key]; \
  193. }
  194. static char *fip_conn_type_names[] = {
  195. [ FIP_CONN_TYPE_UNKNOWN ] = "Unknown",
  196. [ FIP_CONN_TYPE_FABRIC ] = "Fabric",
  197. [ FIP_CONN_TYPE_VN2VN ] = "VN2VN",
  198. };
  199. fcoe_enum_name_search(ctlr_mode, fip_conn_type, fip_conn_type_names)
  200. #define FCOE_CTLR_MODE_MAX_NAMELEN 50
  201. static char *fcf_state_names[] = {
  202. [ FCOE_FCF_STATE_UNKNOWN ] = "Unknown",
  203. [ FCOE_FCF_STATE_DISCONNECTED ] = "Disconnected",
  204. [ FCOE_FCF_STATE_CONNECTED ] = "Connected",
  205. };
  206. fcoe_enum_name_search(fcf_state, fcf_state, fcf_state_names)
  207. #define FCOE_FCF_STATE_MAX_NAMELEN 50
  208. static ssize_t show_fcf_state(struct device *dev,
  209. struct device_attribute *attr,
  210. char *buf)
  211. {
  212. struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
  213. const char *name;
  214. name = get_fcoe_fcf_state_name(fcf->state);
  215. if (!name)
  216. return -EINVAL;
  217. return snprintf(buf, FCOE_FCF_STATE_MAX_NAMELEN, "%s\n", name);
  218. }
  219. static FCOE_DEVICE_ATTR(fcf, state, S_IRUGO, show_fcf_state, NULL);
  220. #define FCOE_MAX_MODENAME_LEN 20
  221. static ssize_t show_ctlr_mode(struct device *dev,
  222. struct device_attribute *attr,
  223. char *buf)
  224. {
  225. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  226. const char *name;
  227. if (ctlr->f->get_fcoe_ctlr_mode)
  228. ctlr->f->get_fcoe_ctlr_mode(ctlr);
  229. name = get_fcoe_ctlr_mode_name(ctlr->mode);
  230. if (!name)
  231. return -EINVAL;
  232. return snprintf(buf, FCOE_CTLR_MODE_MAX_NAMELEN,
  233. "%s\n", name);
  234. }
  235. static FCOE_DEVICE_ATTR(ctlr, mode, S_IRUGO,
  236. show_ctlr_mode, NULL);
  237. static ssize_t
  238. store_private_fcoe_ctlr_fcf_dev_loss_tmo(struct device *dev,
  239. struct device_attribute *attr,
  240. const char *buf, size_t count)
  241. {
  242. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  243. struct fcoe_fcf_device *fcf;
  244. unsigned long val;
  245. int rc;
  246. rc = fcoe_str_to_dev_loss(buf, &val);
  247. if (rc)
  248. return rc;
  249. fcoe_ctlr_fcf_dev_loss_tmo(ctlr) = val;
  250. mutex_lock(&ctlr->lock);
  251. list_for_each_entry(fcf, &ctlr->fcfs, peers)
  252. fcoe_fcf_set_dev_loss_tmo(fcf, val);
  253. mutex_unlock(&ctlr->lock);
  254. return count;
  255. }
  256. fcoe_ctlr_private_show_function(fcf_dev_loss_tmo, "%d\n", 20, );
  257. static FCOE_DEVICE_ATTR(ctlr, fcf_dev_loss_tmo, S_IRUGO | S_IWUSR,
  258. show_fcoe_ctlr_device_fcf_dev_loss_tmo,
  259. store_private_fcoe_ctlr_fcf_dev_loss_tmo);
  260. /* Link Error Status Block (LESB) */
  261. fcoe_ctlr_rd_attr(link_fail, "%u\n", 20);
  262. fcoe_ctlr_rd_attr(vlink_fail, "%u\n", 20);
  263. fcoe_ctlr_rd_attr(miss_fka, "%u\n", 20);
  264. fcoe_ctlr_rd_attr(symb_err, "%u\n", 20);
  265. fcoe_ctlr_rd_attr(err_block, "%u\n", 20);
  266. fcoe_ctlr_rd_attr(fcs_error, "%u\n", 20);
  267. fcoe_fcf_private_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
  268. fcoe_fcf_private_rd_attr_cast(switch_name, "0x%llx\n", 20, unsigned long long);
  269. fcoe_fcf_private_rd_attr(priority, "%u\n", 20);
  270. fcoe_fcf_private_rd_attr(fc_map, "0x%x\n", 20);
  271. fcoe_fcf_private_rd_attr(vfid, "%u\n", 20);
  272. fcoe_fcf_private_rd_attr(mac, "%pM\n", 20);
  273. fcoe_fcf_private_rd_attr(fka_period, "%u\n", 20);
  274. fcoe_fcf_rd_attr(selected, "%u\n", 20);
  275. fcoe_fcf_rd_attr(vlan_id, "%u\n", 20);
  276. fcoe_fcf_private_show_function(dev_loss_tmo, "%d\n", 20, )
  277. static ssize_t
  278. store_fcoe_fcf_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
  279. const char *buf, size_t count)
  280. {
  281. struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
  282. unsigned long val;
  283. int rc;
  284. rc = fcoe_str_to_dev_loss(buf, &val);
  285. if (rc)
  286. return rc;
  287. rc = fcoe_fcf_set_dev_loss_tmo(fcf, val);
  288. if (rc)
  289. return rc;
  290. return count;
  291. }
  292. static FCOE_DEVICE_ATTR(fcf, dev_loss_tmo, S_IRUGO | S_IWUSR,
  293. show_fcoe_fcf_device_dev_loss_tmo,
  294. store_fcoe_fcf_dev_loss_tmo);
  295. static struct attribute *fcoe_ctlr_lesb_attrs[] = {
  296. &device_attr_fcoe_ctlr_link_fail.attr,
  297. &device_attr_fcoe_ctlr_vlink_fail.attr,
  298. &device_attr_fcoe_ctlr_miss_fka.attr,
  299. &device_attr_fcoe_ctlr_symb_err.attr,
  300. &device_attr_fcoe_ctlr_err_block.attr,
  301. &device_attr_fcoe_ctlr_fcs_error.attr,
  302. NULL,
  303. };
  304. static struct attribute_group fcoe_ctlr_lesb_attr_group = {
  305. .name = "lesb",
  306. .attrs = fcoe_ctlr_lesb_attrs,
  307. };
  308. static struct attribute *fcoe_ctlr_attrs[] = {
  309. &device_attr_fcoe_ctlr_fcf_dev_loss_tmo.attr,
  310. &device_attr_fcoe_ctlr_mode.attr,
  311. NULL,
  312. };
  313. static struct attribute_group fcoe_ctlr_attr_group = {
  314. .attrs = fcoe_ctlr_attrs,
  315. };
  316. static const struct attribute_group *fcoe_ctlr_attr_groups[] = {
  317. &fcoe_ctlr_attr_group,
  318. &fcoe_ctlr_lesb_attr_group,
  319. NULL,
  320. };
  321. static struct attribute *fcoe_fcf_attrs[] = {
  322. &device_attr_fcoe_fcf_fabric_name.attr,
  323. &device_attr_fcoe_fcf_switch_name.attr,
  324. &device_attr_fcoe_fcf_dev_loss_tmo.attr,
  325. &device_attr_fcoe_fcf_fc_map.attr,
  326. &device_attr_fcoe_fcf_vfid.attr,
  327. &device_attr_fcoe_fcf_mac.attr,
  328. &device_attr_fcoe_fcf_priority.attr,
  329. &device_attr_fcoe_fcf_fka_period.attr,
  330. &device_attr_fcoe_fcf_state.attr,
  331. &device_attr_fcoe_fcf_selected.attr,
  332. &device_attr_fcoe_fcf_vlan_id.attr,
  333. NULL
  334. };
  335. static struct attribute_group fcoe_fcf_attr_group = {
  336. .attrs = fcoe_fcf_attrs,
  337. };
  338. static const struct attribute_group *fcoe_fcf_attr_groups[] = {
  339. &fcoe_fcf_attr_group,
  340. NULL,
  341. };
  342. struct bus_type fcoe_bus_type;
  343. static int fcoe_bus_match(struct device *dev,
  344. struct device_driver *drv)
  345. {
  346. if (dev->bus == &fcoe_bus_type)
  347. return 1;
  348. return 0;
  349. }
  350. /**
  351. * fcoe_ctlr_device_release() - Release the FIP ctlr memory
  352. * @dev: Pointer to the FIP ctlr's embedded device
  353. *
  354. * Called when the last FIP ctlr reference is released.
  355. */
  356. static void fcoe_ctlr_device_release(struct device *dev)
  357. {
  358. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  359. kfree(ctlr);
  360. }
  361. /**
  362. * fcoe_fcf_device_release() - Release the FIP fcf memory
  363. * @dev: Pointer to the fcf's embedded device
  364. *
  365. * Called when the last FIP fcf reference is released.
  366. */
  367. static void fcoe_fcf_device_release(struct device *dev)
  368. {
  369. struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
  370. kfree(fcf);
  371. }
  372. struct device_type fcoe_ctlr_device_type = {
  373. .name = "fcoe_ctlr",
  374. .groups = fcoe_ctlr_attr_groups,
  375. .release = fcoe_ctlr_device_release,
  376. };
  377. struct device_type fcoe_fcf_device_type = {
  378. .name = "fcoe_fcf",
  379. .groups = fcoe_fcf_attr_groups,
  380. .release = fcoe_fcf_device_release,
  381. };
  382. struct bus_type fcoe_bus_type = {
  383. .name = "fcoe",
  384. .match = &fcoe_bus_match,
  385. };
  386. /**
  387. * fcoe_ctlr_device_flush_work() - Flush a FIP ctlr's workqueue
  388. * @ctlr: Pointer to the FIP ctlr whose workqueue is to be flushed
  389. */
  390. void fcoe_ctlr_device_flush_work(struct fcoe_ctlr_device *ctlr)
  391. {
  392. if (!fcoe_ctlr_work_q(ctlr)) {
  393. printk(KERN_ERR
  394. "ERROR: FIP Ctlr '%d' attempted to flush work, "
  395. "when no workqueue created.\n", ctlr->id);
  396. dump_stack();
  397. return;
  398. }
  399. flush_workqueue(fcoe_ctlr_work_q(ctlr));
  400. }
  401. /**
  402. * fcoe_ctlr_device_queue_work() - Schedule work for a FIP ctlr's workqueue
  403. * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
  404. * @work: Work to queue for execution
  405. *
  406. * Return value:
  407. * 1 on success / 0 already queued / < 0 for error
  408. */
  409. int fcoe_ctlr_device_queue_work(struct fcoe_ctlr_device *ctlr,
  410. struct work_struct *work)
  411. {
  412. if (unlikely(!fcoe_ctlr_work_q(ctlr))) {
  413. printk(KERN_ERR
  414. "ERROR: FIP Ctlr '%d' attempted to queue work, "
  415. "when no workqueue created.\n", ctlr->id);
  416. dump_stack();
  417. return -EINVAL;
  418. }
  419. return queue_work(fcoe_ctlr_work_q(ctlr), work);
  420. }
  421. /**
  422. * fcoe_ctlr_device_flush_devloss() - Flush a FIP ctlr's devloss workqueue
  423. * @ctlr: Pointer to FIP ctlr whose workqueue is to be flushed
  424. */
  425. void fcoe_ctlr_device_flush_devloss(struct fcoe_ctlr_device *ctlr)
  426. {
  427. if (!fcoe_ctlr_devloss_work_q(ctlr)) {
  428. printk(KERN_ERR
  429. "ERROR: FIP Ctlr '%d' attempted to flush work, "
  430. "when no workqueue created.\n", ctlr->id);
  431. dump_stack();
  432. return;
  433. }
  434. flush_workqueue(fcoe_ctlr_devloss_work_q(ctlr));
  435. }
  436. /**
  437. * fcoe_ctlr_device_queue_devloss_work() - Schedule work for a FIP ctlr's devloss workqueue
  438. * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
  439. * @work: Work to queue for execution
  440. * @delay: jiffies to delay the work queuing
  441. *
  442. * Return value:
  443. * 1 on success / 0 already queued / < 0 for error
  444. */
  445. int fcoe_ctlr_device_queue_devloss_work(struct fcoe_ctlr_device *ctlr,
  446. struct delayed_work *work,
  447. unsigned long delay)
  448. {
  449. if (unlikely(!fcoe_ctlr_devloss_work_q(ctlr))) {
  450. printk(KERN_ERR
  451. "ERROR: FIP Ctlr '%d' attempted to queue work, "
  452. "when no workqueue created.\n", ctlr->id);
  453. dump_stack();
  454. return -EINVAL;
  455. }
  456. return queue_delayed_work(fcoe_ctlr_devloss_work_q(ctlr), work, delay);
  457. }
  458. static int fcoe_fcf_device_match(struct fcoe_fcf_device *new,
  459. struct fcoe_fcf_device *old)
  460. {
  461. if (new->switch_name == old->switch_name &&
  462. new->fabric_name == old->fabric_name &&
  463. new->fc_map == old->fc_map &&
  464. compare_ether_addr(new->mac, old->mac) == 0)
  465. return 1;
  466. return 0;
  467. }
  468. /**
  469. * fcoe_ctlr_device_add() - Add a FIP ctlr to sysfs
  470. * @parent: The parent device to which the fcoe_ctlr instance
  471. * should be attached
  472. * @f: The LLD's FCoE sysfs function template pointer
  473. * @priv_size: Size to be allocated with the fcoe_ctlr_device for the LLD
  474. *
  475. * This routine allocates a FIP ctlr object with some additional memory
  476. * for the LLD. The FIP ctlr is initialized, added to sysfs and then
  477. * attributes are added to it.
  478. */
  479. struct fcoe_ctlr_device *fcoe_ctlr_device_add(struct device *parent,
  480. struct fcoe_sysfs_function_template *f,
  481. int priv_size)
  482. {
  483. struct fcoe_ctlr_device *ctlr;
  484. int error = 0;
  485. ctlr = kzalloc(sizeof(struct fcoe_ctlr_device) + priv_size,
  486. GFP_KERNEL);
  487. if (!ctlr)
  488. goto out;
  489. ctlr->id = atomic_inc_return(&ctlr_num) - 1;
  490. ctlr->f = f;
  491. INIT_LIST_HEAD(&ctlr->fcfs);
  492. mutex_init(&ctlr->lock);
  493. ctlr->dev.parent = parent;
  494. ctlr->dev.bus = &fcoe_bus_type;
  495. ctlr->dev.type = &fcoe_ctlr_device_type;
  496. ctlr->fcf_dev_loss_tmo = fcoe_fcf_dev_loss_tmo;
  497. snprintf(ctlr->work_q_name, sizeof(ctlr->work_q_name),
  498. "ctlr_wq_%d", ctlr->id);
  499. ctlr->work_q = create_singlethread_workqueue(
  500. ctlr->work_q_name);
  501. if (!ctlr->work_q)
  502. goto out_del;
  503. snprintf(ctlr->devloss_work_q_name,
  504. sizeof(ctlr->devloss_work_q_name),
  505. "ctlr_dl_wq_%d", ctlr->id);
  506. ctlr->devloss_work_q = create_singlethread_workqueue(
  507. ctlr->devloss_work_q_name);
  508. if (!ctlr->devloss_work_q)
  509. goto out_del_q;
  510. dev_set_name(&ctlr->dev, "ctlr_%d", ctlr->id);
  511. error = device_register(&ctlr->dev);
  512. if (error)
  513. goto out_del_q2;
  514. return ctlr;
  515. out_del_q2:
  516. destroy_workqueue(ctlr->devloss_work_q);
  517. ctlr->devloss_work_q = NULL;
  518. out_del_q:
  519. destroy_workqueue(ctlr->work_q);
  520. ctlr->work_q = NULL;
  521. out_del:
  522. kfree(ctlr);
  523. out:
  524. return NULL;
  525. }
  526. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_add);
  527. /**
  528. * fcoe_ctlr_device_delete() - Delete a FIP ctlr and its subtree from sysfs
  529. * @ctlr: A pointer to the ctlr to be deleted
  530. *
  531. * Deletes a FIP ctlr and any fcfs attached
  532. * to it. Deleting fcfs will cause their childen
  533. * to be deleted as well.
  534. *
  535. * The ctlr is detached from sysfs and it's resources
  536. * are freed (work q), but the memory is not freed
  537. * until its last reference is released.
  538. *
  539. * This routine expects no locks to be held before
  540. * calling.
  541. *
  542. * TODO: Currently there are no callbacks to clean up LLD data
  543. * for a fcoe_fcf_device. LLDs must keep this in mind as they need
  544. * to clean up each of their LLD data for all fcoe_fcf_device before
  545. * calling fcoe_ctlr_device_delete.
  546. */
  547. void fcoe_ctlr_device_delete(struct fcoe_ctlr_device *ctlr)
  548. {
  549. struct fcoe_fcf_device *fcf, *next;
  550. /* Remove any attached fcfs */
  551. mutex_lock(&ctlr->lock);
  552. list_for_each_entry_safe(fcf, next,
  553. &ctlr->fcfs, peers) {
  554. list_del(&fcf->peers);
  555. fcf->state = FCOE_FCF_STATE_DELETED;
  556. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  557. }
  558. mutex_unlock(&ctlr->lock);
  559. fcoe_ctlr_device_flush_work(ctlr);
  560. destroy_workqueue(ctlr->devloss_work_q);
  561. ctlr->devloss_work_q = NULL;
  562. destroy_workqueue(ctlr->work_q);
  563. ctlr->work_q = NULL;
  564. device_unregister(&ctlr->dev);
  565. }
  566. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_delete);
  567. /**
  568. * fcoe_fcf_device_final_delete() - Final delete routine
  569. * @work: The FIP fcf's embedded work struct
  570. *
  571. * It is expected that the fcf has been removed from
  572. * the FIP ctlr's list before calling this routine.
  573. */
  574. static void fcoe_fcf_device_final_delete(struct work_struct *work)
  575. {
  576. struct fcoe_fcf_device *fcf =
  577. container_of(work, struct fcoe_fcf_device, delete_work);
  578. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  579. /*
  580. * Cancel any outstanding timers. These should really exist
  581. * only when rmmod'ing the LLDD and we're asking for
  582. * immediate termination of the rports
  583. */
  584. if (!cancel_delayed_work(&fcf->dev_loss_work))
  585. fcoe_ctlr_device_flush_devloss(ctlr);
  586. device_unregister(&fcf->dev);
  587. }
  588. /**
  589. * fip_timeout_deleted_fcf() - Delete a fcf when the devloss timer fires
  590. * @work: The FIP fcf's embedded work struct
  591. *
  592. * Removes the fcf from the FIP ctlr's list of fcfs and
  593. * queues the final deletion.
  594. */
  595. static void fip_timeout_deleted_fcf(struct work_struct *work)
  596. {
  597. struct fcoe_fcf_device *fcf =
  598. container_of(work, struct fcoe_fcf_device, dev_loss_work.work);
  599. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  600. mutex_lock(&ctlr->lock);
  601. /*
  602. * If the fcf is deleted or reconnected before the timer
  603. * fires the devloss queue will be flushed, but the state will
  604. * either be CONNECTED or DELETED. If that is the case we
  605. * cancel deleting the fcf.
  606. */
  607. if (fcf->state != FCOE_FCF_STATE_DISCONNECTED)
  608. goto out;
  609. dev_printk(KERN_ERR, &fcf->dev,
  610. "FIP fcf connection time out: removing fcf\n");
  611. list_del(&fcf->peers);
  612. fcf->state = FCOE_FCF_STATE_DELETED;
  613. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  614. out:
  615. mutex_unlock(&ctlr->lock);
  616. }
  617. /**
  618. * fcoe_fcf_device_delete() - Delete a FIP fcf
  619. * @fcf: Pointer to the fcf which is to be deleted
  620. *
  621. * Queues the FIP fcf on the devloss workqueue
  622. *
  623. * Expects the ctlr_attrs mutex to be held for fcf
  624. * state change.
  625. */
  626. void fcoe_fcf_device_delete(struct fcoe_fcf_device *fcf)
  627. {
  628. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  629. int timeout = fcf->dev_loss_tmo;
  630. if (fcf->state != FCOE_FCF_STATE_CONNECTED)
  631. return;
  632. fcf->state = FCOE_FCF_STATE_DISCONNECTED;
  633. /*
  634. * FCF will only be re-connected by the LLD calling
  635. * fcoe_fcf_device_add, and it should be setting up
  636. * priv then.
  637. */
  638. fcf->priv = NULL;
  639. fcoe_ctlr_device_queue_devloss_work(ctlr, &fcf->dev_loss_work,
  640. timeout * HZ);
  641. }
  642. EXPORT_SYMBOL_GPL(fcoe_fcf_device_delete);
  643. /**
  644. * fcoe_fcf_device_add() - Add a FCoE sysfs fcoe_fcf_device to the system
  645. * @ctlr: The fcoe_ctlr_device that will be the fcoe_fcf_device parent
  646. * @new_fcf: A temporary FCF used for lookups on the current list of fcfs
  647. *
  648. * Expects to be called with the ctlr->lock held
  649. */
  650. struct fcoe_fcf_device *fcoe_fcf_device_add(struct fcoe_ctlr_device *ctlr,
  651. struct fcoe_fcf_device *new_fcf)
  652. {
  653. struct fcoe_fcf_device *fcf;
  654. int error = 0;
  655. list_for_each_entry(fcf, &ctlr->fcfs, peers) {
  656. if (fcoe_fcf_device_match(new_fcf, fcf)) {
  657. if (fcf->state == FCOE_FCF_STATE_CONNECTED)
  658. return fcf;
  659. fcf->state = FCOE_FCF_STATE_CONNECTED;
  660. if (!cancel_delayed_work(&fcf->dev_loss_work))
  661. fcoe_ctlr_device_flush_devloss(ctlr);
  662. return fcf;
  663. }
  664. }
  665. fcf = kzalloc(sizeof(struct fcoe_fcf_device), GFP_ATOMIC);
  666. if (unlikely(!fcf))
  667. goto out;
  668. INIT_WORK(&fcf->delete_work, fcoe_fcf_device_final_delete);
  669. INIT_DELAYED_WORK(&fcf->dev_loss_work, fip_timeout_deleted_fcf);
  670. fcf->dev.parent = &ctlr->dev;
  671. fcf->dev.bus = &fcoe_bus_type;
  672. fcf->dev.type = &fcoe_fcf_device_type;
  673. fcf->id = atomic_inc_return(&fcf_num) - 1;
  674. fcf->state = FCOE_FCF_STATE_UNKNOWN;
  675. fcf->dev_loss_tmo = ctlr->fcf_dev_loss_tmo;
  676. dev_set_name(&fcf->dev, "fcf_%d", fcf->id);
  677. fcf->fabric_name = new_fcf->fabric_name;
  678. fcf->switch_name = new_fcf->switch_name;
  679. fcf->fc_map = new_fcf->fc_map;
  680. fcf->vfid = new_fcf->vfid;
  681. memcpy(fcf->mac, new_fcf->mac, ETH_ALEN);
  682. fcf->priority = new_fcf->priority;
  683. fcf->fka_period = new_fcf->fka_period;
  684. fcf->selected = new_fcf->selected;
  685. error = device_register(&fcf->dev);
  686. if (error)
  687. goto out_del;
  688. fcf->state = FCOE_FCF_STATE_CONNECTED;
  689. list_add_tail(&fcf->peers, &ctlr->fcfs);
  690. return fcf;
  691. out_del:
  692. kfree(fcf);
  693. out:
  694. return NULL;
  695. }
  696. EXPORT_SYMBOL_GPL(fcoe_fcf_device_add);
  697. int __init fcoe_sysfs_setup(void)
  698. {
  699. int error;
  700. atomic_set(&ctlr_num, 0);
  701. atomic_set(&fcf_num, 0);
  702. error = bus_register(&fcoe_bus_type);
  703. if (error)
  704. return error;
  705. return 0;
  706. }
  707. void __exit fcoe_sysfs_teardown(void)
  708. {
  709. bus_unregister(&fcoe_bus_type);
  710. }