fcoe_sysfs.c 27 KB

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