fcoe_sysfs.c 28 KB

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