fcoe_sysfs.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. destroy_workqueue(ctlr->devloss_work_q);
  722. destroy_workqueue(ctlr->work_q);
  723. put_device(&ctlr->dev);
  724. return NULL;
  725. }
  726. return ctlr;
  727. out_del_q:
  728. destroy_workqueue(ctlr->work_q);
  729. ctlr->work_q = NULL;
  730. out_del:
  731. kfree(ctlr);
  732. out:
  733. return NULL;
  734. }
  735. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_add);
  736. /**
  737. * fcoe_ctlr_device_delete() - Delete a FIP ctlr and its subtree from sysfs
  738. * @ctlr: A pointer to the ctlr to be deleted
  739. *
  740. * Deletes a FIP ctlr and any fcfs attached
  741. * to it. Deleting fcfs will cause their childen
  742. * to be deleted as well.
  743. *
  744. * The ctlr is detached from sysfs and it's resources
  745. * are freed (work q), but the memory is not freed
  746. * until its last reference is released.
  747. *
  748. * This routine expects no locks to be held before
  749. * calling.
  750. *
  751. * TODO: Currently there are no callbacks to clean up LLD data
  752. * for a fcoe_fcf_device. LLDs must keep this in mind as they need
  753. * to clean up each of their LLD data for all fcoe_fcf_device before
  754. * calling fcoe_ctlr_device_delete.
  755. */
  756. void fcoe_ctlr_device_delete(struct fcoe_ctlr_device *ctlr)
  757. {
  758. struct fcoe_fcf_device *fcf, *next;
  759. /* Remove any attached fcfs */
  760. mutex_lock(&ctlr->lock);
  761. list_for_each_entry_safe(fcf, next,
  762. &ctlr->fcfs, peers) {
  763. list_del(&fcf->peers);
  764. fcf->state = FCOE_FCF_STATE_DELETED;
  765. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  766. }
  767. mutex_unlock(&ctlr->lock);
  768. fcoe_ctlr_device_flush_work(ctlr);
  769. destroy_workqueue(ctlr->devloss_work_q);
  770. ctlr->devloss_work_q = NULL;
  771. destroy_workqueue(ctlr->work_q);
  772. ctlr->work_q = NULL;
  773. device_unregister(&ctlr->dev);
  774. }
  775. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_delete);
  776. /**
  777. * fcoe_fcf_device_final_delete() - Final delete routine
  778. * @work: The FIP fcf's embedded work struct
  779. *
  780. * It is expected that the fcf has been removed from
  781. * the FIP ctlr's list before calling this routine.
  782. */
  783. static void fcoe_fcf_device_final_delete(struct work_struct *work)
  784. {
  785. struct fcoe_fcf_device *fcf =
  786. container_of(work, struct fcoe_fcf_device, delete_work);
  787. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  788. /*
  789. * Cancel any outstanding timers. These should really exist
  790. * only when rmmod'ing the LLDD and we're asking for
  791. * immediate termination of the rports
  792. */
  793. if (!cancel_delayed_work(&fcf->dev_loss_work))
  794. fcoe_ctlr_device_flush_devloss(ctlr);
  795. device_unregister(&fcf->dev);
  796. }
  797. /**
  798. * fip_timeout_deleted_fcf() - Delete a fcf when the devloss timer fires
  799. * @work: The FIP fcf's embedded work struct
  800. *
  801. * Removes the fcf from the FIP ctlr's list of fcfs and
  802. * queues the final deletion.
  803. */
  804. static void fip_timeout_deleted_fcf(struct work_struct *work)
  805. {
  806. struct fcoe_fcf_device *fcf =
  807. container_of(work, struct fcoe_fcf_device, dev_loss_work.work);
  808. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  809. mutex_lock(&ctlr->lock);
  810. /*
  811. * If the fcf is deleted or reconnected before the timer
  812. * fires the devloss queue will be flushed, but the state will
  813. * either be CONNECTED or DELETED. If that is the case we
  814. * cancel deleting the fcf.
  815. */
  816. if (fcf->state != FCOE_FCF_STATE_DISCONNECTED)
  817. goto out;
  818. dev_printk(KERN_ERR, &fcf->dev,
  819. "FIP fcf connection time out: removing fcf\n");
  820. list_del(&fcf->peers);
  821. fcf->state = FCOE_FCF_STATE_DELETED;
  822. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  823. out:
  824. mutex_unlock(&ctlr->lock);
  825. }
  826. /**
  827. * fcoe_fcf_device_delete() - Delete a FIP fcf
  828. * @fcf: Pointer to the fcf which is to be deleted
  829. *
  830. * Queues the FIP fcf on the devloss workqueue
  831. *
  832. * Expects the ctlr_attrs mutex to be held for fcf
  833. * state change.
  834. */
  835. void fcoe_fcf_device_delete(struct fcoe_fcf_device *fcf)
  836. {
  837. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  838. int timeout = fcf->dev_loss_tmo;
  839. if (fcf->state != FCOE_FCF_STATE_CONNECTED)
  840. return;
  841. fcf->state = FCOE_FCF_STATE_DISCONNECTED;
  842. /*
  843. * FCF will only be re-connected by the LLD calling
  844. * fcoe_fcf_device_add, and it should be setting up
  845. * priv then.
  846. */
  847. fcf->priv = NULL;
  848. fcoe_ctlr_device_queue_devloss_work(ctlr, &fcf->dev_loss_work,
  849. timeout * HZ);
  850. }
  851. EXPORT_SYMBOL_GPL(fcoe_fcf_device_delete);
  852. /**
  853. * fcoe_fcf_device_add() - Add a FCoE sysfs fcoe_fcf_device to the system
  854. * @ctlr: The fcoe_ctlr_device that will be the fcoe_fcf_device parent
  855. * @new_fcf: A temporary FCF used for lookups on the current list of fcfs
  856. *
  857. * Expects to be called with the ctlr->lock held
  858. */
  859. struct fcoe_fcf_device *fcoe_fcf_device_add(struct fcoe_ctlr_device *ctlr,
  860. struct fcoe_fcf_device *new_fcf)
  861. {
  862. struct fcoe_fcf_device *fcf;
  863. int error = 0;
  864. list_for_each_entry(fcf, &ctlr->fcfs, peers) {
  865. if (fcoe_fcf_device_match(new_fcf, fcf)) {
  866. if (fcf->state == FCOE_FCF_STATE_CONNECTED)
  867. return fcf;
  868. fcf->state = FCOE_FCF_STATE_CONNECTED;
  869. if (!cancel_delayed_work(&fcf->dev_loss_work))
  870. fcoe_ctlr_device_flush_devloss(ctlr);
  871. return fcf;
  872. }
  873. }
  874. fcf = kzalloc(sizeof(struct fcoe_fcf_device), GFP_ATOMIC);
  875. if (unlikely(!fcf))
  876. goto out;
  877. INIT_WORK(&fcf->delete_work, fcoe_fcf_device_final_delete);
  878. INIT_DELAYED_WORK(&fcf->dev_loss_work, fip_timeout_deleted_fcf);
  879. fcf->dev.parent = &ctlr->dev;
  880. fcf->dev.bus = &fcoe_bus_type;
  881. fcf->dev.type = &fcoe_fcf_device_type;
  882. fcf->id = atomic_inc_return(&fcf_num) - 1;
  883. fcf->state = FCOE_FCF_STATE_UNKNOWN;
  884. fcf->dev_loss_tmo = ctlr->fcf_dev_loss_tmo;
  885. dev_set_name(&fcf->dev, "fcf_%d", fcf->id);
  886. fcf->fabric_name = new_fcf->fabric_name;
  887. fcf->switch_name = new_fcf->switch_name;
  888. fcf->fc_map = new_fcf->fc_map;
  889. fcf->vfid = new_fcf->vfid;
  890. memcpy(fcf->mac, new_fcf->mac, ETH_ALEN);
  891. fcf->priority = new_fcf->priority;
  892. fcf->fka_period = new_fcf->fka_period;
  893. fcf->selected = new_fcf->selected;
  894. error = device_register(&fcf->dev);
  895. if (error) {
  896. put_device(&fcf->dev);
  897. goto out;
  898. }
  899. fcf->state = FCOE_FCF_STATE_CONNECTED;
  900. list_add_tail(&fcf->peers, &ctlr->fcfs);
  901. return fcf;
  902. out:
  903. return NULL;
  904. }
  905. EXPORT_SYMBOL_GPL(fcoe_fcf_device_add);
  906. int __init fcoe_sysfs_setup(void)
  907. {
  908. atomic_set(&ctlr_num, 0);
  909. atomic_set(&fcf_num, 0);
  910. return bus_register(&fcoe_bus_type);
  911. }
  912. void __exit fcoe_sysfs_teardown(void)
  913. {
  914. bus_unregister(&fcoe_bus_type);
  915. }