fcoe_sysfs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. bool enabled;
  297. int rc;
  298. if (*buf == '1')
  299. enabled = true;
  300. else if (*buf == '0')
  301. enabled = false;
  302. else
  303. return -EINVAL;
  304. switch (ctlr->enabled) {
  305. case FCOE_CTLR_ENABLED:
  306. if (enabled)
  307. return count;
  308. ctlr->enabled = FCOE_CTLR_DISABLED;
  309. break;
  310. case FCOE_CTLR_DISABLED:
  311. if (!enabled)
  312. return count;
  313. ctlr->enabled = FCOE_CTLR_ENABLED;
  314. break;
  315. case FCOE_CTLR_UNUSED:
  316. return -ENOTSUPP;
  317. };
  318. rc = ctlr->f->set_fcoe_ctlr_enabled(ctlr);
  319. if (rc)
  320. return rc;
  321. return count;
  322. }
  323. static char *ctlr_enabled_state_names[] = {
  324. [ FCOE_CTLR_ENABLED ] = "1",
  325. [ FCOE_CTLR_DISABLED ] = "0",
  326. };
  327. fcoe_enum_name_search(ctlr_enabled_state, ctlr_enabled_state,
  328. ctlr_enabled_state_names)
  329. #define FCOE_CTLR_ENABLED_MAX_NAMELEN 50
  330. static ssize_t show_ctlr_enabled_state(struct device *dev,
  331. struct device_attribute *attr,
  332. char *buf)
  333. {
  334. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  335. const char *name;
  336. name = get_fcoe_ctlr_enabled_state_name(ctlr->enabled);
  337. if (!name)
  338. return -EINVAL;
  339. return snprintf(buf, FCOE_CTLR_ENABLED_MAX_NAMELEN,
  340. "%s\n", name);
  341. }
  342. static FCOE_DEVICE_ATTR(ctlr, enabled, S_IRUGO | S_IWUSR,
  343. show_ctlr_enabled_state,
  344. store_ctlr_enabled);
  345. static ssize_t store_ctlr_fip_resp(struct device *dev,
  346. struct device_attribute *attr,
  347. const char *buf, size_t count)
  348. {
  349. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  350. struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr);
  351. mutex_lock(&fip->ctlr_mutex);
  352. if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
  353. if (buf[0] == '1') {
  354. fip->fip_resp = 1;
  355. mutex_unlock(&fip->ctlr_mutex);
  356. return count;
  357. }
  358. if (buf[0] == '0') {
  359. fip->fip_resp = 0;
  360. mutex_unlock(&fip->ctlr_mutex);
  361. return count;
  362. }
  363. }
  364. mutex_unlock(&fip->ctlr_mutex);
  365. return -EINVAL;
  366. }
  367. static ssize_t show_ctlr_fip_resp(struct device *dev,
  368. struct device_attribute *attr,
  369. char *buf)
  370. {
  371. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  372. struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr);
  373. return sprintf(buf, "%d\n", fip->fip_resp ? 1 : 0);
  374. }
  375. static FCOE_DEVICE_ATTR(ctlr, fip_vlan_responder, S_IRUGO | S_IWUSR,
  376. show_ctlr_fip_resp,
  377. store_ctlr_fip_resp);
  378. static ssize_t
  379. fcoe_ctlr_var_store(u32 *var, const char *buf, size_t count)
  380. {
  381. int err;
  382. unsigned long v;
  383. err = kstrtoul(buf, 10, &v);
  384. if (err || v > UINT_MAX)
  385. return -EINVAL;
  386. *var = v;
  387. return count;
  388. }
  389. static ssize_t store_ctlr_r_a_tov(struct device *dev,
  390. struct device_attribute *attr,
  391. const char *buf, size_t count)
  392. {
  393. struct fcoe_ctlr_device *ctlr_dev = dev_to_ctlr(dev);
  394. struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
  395. if (ctlr_dev->enabled == FCOE_CTLR_ENABLED)
  396. return -EBUSY;
  397. if (ctlr_dev->enabled == FCOE_CTLR_DISABLED)
  398. return fcoe_ctlr_var_store(&ctlr->lp->r_a_tov, buf, count);
  399. return -ENOTSUPP;
  400. }
  401. static ssize_t show_ctlr_r_a_tov(struct device *dev,
  402. struct device_attribute *attr,
  403. char *buf)
  404. {
  405. struct fcoe_ctlr_device *ctlr_dev = dev_to_ctlr(dev);
  406. struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
  407. return sprintf(buf, "%d\n", ctlr->lp->r_a_tov);
  408. }
  409. static FCOE_DEVICE_ATTR(ctlr, r_a_tov, S_IRUGO | S_IWUSR,
  410. show_ctlr_r_a_tov, store_ctlr_r_a_tov);
  411. static ssize_t store_ctlr_e_d_tov(struct device *dev,
  412. struct device_attribute *attr,
  413. const char *buf, size_t count)
  414. {
  415. struct fcoe_ctlr_device *ctlr_dev = dev_to_ctlr(dev);
  416. struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
  417. if (ctlr_dev->enabled == FCOE_CTLR_ENABLED)
  418. return -EBUSY;
  419. if (ctlr_dev->enabled == FCOE_CTLR_DISABLED)
  420. return fcoe_ctlr_var_store(&ctlr->lp->e_d_tov, buf, count);
  421. return -ENOTSUPP;
  422. }
  423. static ssize_t show_ctlr_e_d_tov(struct device *dev,
  424. struct device_attribute *attr,
  425. char *buf)
  426. {
  427. struct fcoe_ctlr_device *ctlr_dev = dev_to_ctlr(dev);
  428. struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
  429. return sprintf(buf, "%d\n", ctlr->lp->e_d_tov);
  430. }
  431. static FCOE_DEVICE_ATTR(ctlr, e_d_tov, S_IRUGO | S_IWUSR,
  432. show_ctlr_e_d_tov, store_ctlr_e_d_tov);
  433. static ssize_t
  434. store_private_fcoe_ctlr_fcf_dev_loss_tmo(struct device *dev,
  435. struct device_attribute *attr,
  436. const char *buf, size_t count)
  437. {
  438. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  439. struct fcoe_fcf_device *fcf;
  440. unsigned long val;
  441. int rc;
  442. rc = fcoe_str_to_dev_loss(buf, &val);
  443. if (rc)
  444. return rc;
  445. fcoe_ctlr_fcf_dev_loss_tmo(ctlr) = val;
  446. mutex_lock(&ctlr->lock);
  447. list_for_each_entry(fcf, &ctlr->fcfs, peers)
  448. fcoe_fcf_set_dev_loss_tmo(fcf, val);
  449. mutex_unlock(&ctlr->lock);
  450. return count;
  451. }
  452. fcoe_ctlr_private_show_function(fcf_dev_loss_tmo, "%d\n", 20, );
  453. static FCOE_DEVICE_ATTR(ctlr, fcf_dev_loss_tmo, S_IRUGO | S_IWUSR,
  454. show_fcoe_ctlr_device_fcf_dev_loss_tmo,
  455. store_private_fcoe_ctlr_fcf_dev_loss_tmo);
  456. /* Link Error Status Block (LESB) */
  457. fcoe_ctlr_rd_attr(link_fail, "%u\n", 20);
  458. fcoe_ctlr_rd_attr(vlink_fail, "%u\n", 20);
  459. fcoe_ctlr_rd_attr(miss_fka, "%u\n", 20);
  460. fcoe_ctlr_rd_attr(symb_err, "%u\n", 20);
  461. fcoe_ctlr_rd_attr(err_block, "%u\n", 20);
  462. fcoe_ctlr_rd_attr(fcs_error, "%u\n", 20);
  463. fcoe_fcf_private_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
  464. fcoe_fcf_private_rd_attr_cast(switch_name, "0x%llx\n", 20, unsigned long long);
  465. fcoe_fcf_private_rd_attr(priority, "%u\n", 20);
  466. fcoe_fcf_private_rd_attr(fc_map, "0x%x\n", 20);
  467. fcoe_fcf_private_rd_attr(vfid, "%u\n", 20);
  468. fcoe_fcf_private_rd_attr(mac, "%pM\n", 20);
  469. fcoe_fcf_private_rd_attr(fka_period, "%u\n", 20);
  470. fcoe_fcf_rd_attr(selected, "%u\n", 20);
  471. fcoe_fcf_rd_attr(vlan_id, "%u\n", 20);
  472. fcoe_fcf_private_show_function(dev_loss_tmo, "%d\n", 20, )
  473. static ssize_t
  474. store_fcoe_fcf_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
  475. const char *buf, size_t count)
  476. {
  477. struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
  478. unsigned long val;
  479. int rc;
  480. rc = fcoe_str_to_dev_loss(buf, &val);
  481. if (rc)
  482. return rc;
  483. rc = fcoe_fcf_set_dev_loss_tmo(fcf, val);
  484. if (rc)
  485. return rc;
  486. return count;
  487. }
  488. static FCOE_DEVICE_ATTR(fcf, dev_loss_tmo, S_IRUGO | S_IWUSR,
  489. show_fcoe_fcf_device_dev_loss_tmo,
  490. store_fcoe_fcf_dev_loss_tmo);
  491. static struct attribute *fcoe_ctlr_lesb_attrs[] = {
  492. &device_attr_fcoe_ctlr_link_fail.attr,
  493. &device_attr_fcoe_ctlr_vlink_fail.attr,
  494. &device_attr_fcoe_ctlr_miss_fka.attr,
  495. &device_attr_fcoe_ctlr_symb_err.attr,
  496. &device_attr_fcoe_ctlr_err_block.attr,
  497. &device_attr_fcoe_ctlr_fcs_error.attr,
  498. NULL,
  499. };
  500. static struct attribute_group fcoe_ctlr_lesb_attr_group = {
  501. .name = "lesb",
  502. .attrs = fcoe_ctlr_lesb_attrs,
  503. };
  504. static struct attribute *fcoe_ctlr_attrs[] = {
  505. &device_attr_fcoe_ctlr_fip_vlan_responder.attr,
  506. &device_attr_fcoe_ctlr_fcf_dev_loss_tmo.attr,
  507. &device_attr_fcoe_ctlr_r_a_tov.attr,
  508. &device_attr_fcoe_ctlr_e_d_tov.attr,
  509. &device_attr_fcoe_ctlr_enabled.attr,
  510. &device_attr_fcoe_ctlr_mode.attr,
  511. NULL,
  512. };
  513. static struct attribute_group fcoe_ctlr_attr_group = {
  514. .attrs = fcoe_ctlr_attrs,
  515. };
  516. static const struct attribute_group *fcoe_ctlr_attr_groups[] = {
  517. &fcoe_ctlr_attr_group,
  518. &fcoe_ctlr_lesb_attr_group,
  519. NULL,
  520. };
  521. static struct attribute *fcoe_fcf_attrs[] = {
  522. &device_attr_fcoe_fcf_fabric_name.attr,
  523. &device_attr_fcoe_fcf_switch_name.attr,
  524. &device_attr_fcoe_fcf_dev_loss_tmo.attr,
  525. &device_attr_fcoe_fcf_fc_map.attr,
  526. &device_attr_fcoe_fcf_vfid.attr,
  527. &device_attr_fcoe_fcf_mac.attr,
  528. &device_attr_fcoe_fcf_priority.attr,
  529. &device_attr_fcoe_fcf_fka_period.attr,
  530. &device_attr_fcoe_fcf_state.attr,
  531. &device_attr_fcoe_fcf_selected.attr,
  532. &device_attr_fcoe_fcf_vlan_id.attr,
  533. NULL
  534. };
  535. static struct attribute_group fcoe_fcf_attr_group = {
  536. .attrs = fcoe_fcf_attrs,
  537. };
  538. static const struct attribute_group *fcoe_fcf_attr_groups[] = {
  539. &fcoe_fcf_attr_group,
  540. NULL,
  541. };
  542. static struct bus_type fcoe_bus_type;
  543. static int fcoe_bus_match(struct device *dev,
  544. struct device_driver *drv)
  545. {
  546. if (dev->bus == &fcoe_bus_type)
  547. return 1;
  548. return 0;
  549. }
  550. /**
  551. * fcoe_ctlr_device_release() - Release the FIP ctlr memory
  552. * @dev: Pointer to the FIP ctlr's embedded device
  553. *
  554. * Called when the last FIP ctlr reference is released.
  555. */
  556. static void fcoe_ctlr_device_release(struct device *dev)
  557. {
  558. struct fcoe_ctlr_device *ctlr = dev_to_ctlr(dev);
  559. kfree(ctlr);
  560. }
  561. /**
  562. * fcoe_fcf_device_release() - Release the FIP fcf memory
  563. * @dev: Pointer to the fcf's embedded device
  564. *
  565. * Called when the last FIP fcf reference is released.
  566. */
  567. static void fcoe_fcf_device_release(struct device *dev)
  568. {
  569. struct fcoe_fcf_device *fcf = dev_to_fcf(dev);
  570. kfree(fcf);
  571. }
  572. static const struct device_type fcoe_ctlr_device_type = {
  573. .name = "fcoe_ctlr",
  574. .groups = fcoe_ctlr_attr_groups,
  575. .release = fcoe_ctlr_device_release,
  576. };
  577. static const struct device_type fcoe_fcf_device_type = {
  578. .name = "fcoe_fcf",
  579. .groups = fcoe_fcf_attr_groups,
  580. .release = fcoe_fcf_device_release,
  581. };
  582. static BUS_ATTR(ctlr_create, S_IWUSR, NULL, fcoe_ctlr_create_store);
  583. static BUS_ATTR(ctlr_destroy, S_IWUSR, NULL, fcoe_ctlr_destroy_store);
  584. static struct attribute *fcoe_bus_attrs[] = {
  585. &bus_attr_ctlr_create.attr,
  586. &bus_attr_ctlr_destroy.attr,
  587. NULL,
  588. };
  589. ATTRIBUTE_GROUPS(fcoe_bus);
  590. static struct bus_type fcoe_bus_type = {
  591. .name = "fcoe",
  592. .match = &fcoe_bus_match,
  593. .bus_groups = fcoe_bus_groups,
  594. };
  595. /**
  596. * fcoe_ctlr_device_flush_work() - Flush a FIP ctlr's workqueue
  597. * @ctlr: Pointer to the FIP ctlr whose workqueue is to be flushed
  598. */
  599. static void fcoe_ctlr_device_flush_work(struct fcoe_ctlr_device *ctlr)
  600. {
  601. if (!fcoe_ctlr_work_q(ctlr)) {
  602. printk(KERN_ERR
  603. "ERROR: FIP Ctlr '%d' attempted to flush work, "
  604. "when no workqueue created.\n", ctlr->id);
  605. dump_stack();
  606. return;
  607. }
  608. flush_workqueue(fcoe_ctlr_work_q(ctlr));
  609. }
  610. /**
  611. * fcoe_ctlr_device_queue_work() - Schedule work for a FIP ctlr's workqueue
  612. * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
  613. * @work: Work to queue for execution
  614. *
  615. * Return value:
  616. * 1 on success / 0 already queued / < 0 for error
  617. */
  618. static int fcoe_ctlr_device_queue_work(struct fcoe_ctlr_device *ctlr,
  619. struct work_struct *work)
  620. {
  621. if (unlikely(!fcoe_ctlr_work_q(ctlr))) {
  622. printk(KERN_ERR
  623. "ERROR: FIP Ctlr '%d' attempted to queue work, "
  624. "when no workqueue created.\n", ctlr->id);
  625. dump_stack();
  626. return -EINVAL;
  627. }
  628. return queue_work(fcoe_ctlr_work_q(ctlr), work);
  629. }
  630. /**
  631. * fcoe_ctlr_device_flush_devloss() - Flush a FIP ctlr's devloss workqueue
  632. * @ctlr: Pointer to FIP ctlr whose workqueue is to be flushed
  633. */
  634. static void fcoe_ctlr_device_flush_devloss(struct fcoe_ctlr_device *ctlr)
  635. {
  636. if (!fcoe_ctlr_devloss_work_q(ctlr)) {
  637. printk(KERN_ERR
  638. "ERROR: FIP Ctlr '%d' attempted to flush work, "
  639. "when no workqueue created.\n", ctlr->id);
  640. dump_stack();
  641. return;
  642. }
  643. flush_workqueue(fcoe_ctlr_devloss_work_q(ctlr));
  644. }
  645. /**
  646. * fcoe_ctlr_device_queue_devloss_work() - Schedule work for a FIP ctlr's devloss workqueue
  647. * @ctlr: Pointer to the FIP ctlr who owns the devloss workqueue
  648. * @work: Work to queue for execution
  649. * @delay: jiffies to delay the work queuing
  650. *
  651. * Return value:
  652. * 1 on success / 0 already queued / < 0 for error
  653. */
  654. static int fcoe_ctlr_device_queue_devloss_work(struct fcoe_ctlr_device *ctlr,
  655. struct delayed_work *work,
  656. unsigned long delay)
  657. {
  658. if (unlikely(!fcoe_ctlr_devloss_work_q(ctlr))) {
  659. printk(KERN_ERR
  660. "ERROR: FIP Ctlr '%d' attempted to queue work, "
  661. "when no workqueue created.\n", ctlr->id);
  662. dump_stack();
  663. return -EINVAL;
  664. }
  665. return queue_delayed_work(fcoe_ctlr_devloss_work_q(ctlr), work, delay);
  666. }
  667. static int fcoe_fcf_device_match(struct fcoe_fcf_device *new,
  668. struct fcoe_fcf_device *old)
  669. {
  670. if (new->switch_name == old->switch_name &&
  671. new->fabric_name == old->fabric_name &&
  672. new->fc_map == old->fc_map &&
  673. ether_addr_equal(new->mac, old->mac))
  674. return 1;
  675. return 0;
  676. }
  677. /**
  678. * fcoe_ctlr_device_add() - Add a FIP ctlr to sysfs
  679. * @parent: The parent device to which the fcoe_ctlr instance
  680. * should be attached
  681. * @f: The LLD's FCoE sysfs function template pointer
  682. * @priv_size: Size to be allocated with the fcoe_ctlr_device for the LLD
  683. *
  684. * This routine allocates a FIP ctlr object with some additional memory
  685. * for the LLD. The FIP ctlr is initialized, added to sysfs and then
  686. * attributes are added to it.
  687. */
  688. struct fcoe_ctlr_device *fcoe_ctlr_device_add(struct device *parent,
  689. struct fcoe_sysfs_function_template *f,
  690. int priv_size)
  691. {
  692. struct fcoe_ctlr_device *ctlr;
  693. int error = 0;
  694. ctlr = kzalloc(sizeof(struct fcoe_ctlr_device) + priv_size,
  695. GFP_KERNEL);
  696. if (!ctlr)
  697. goto out;
  698. ctlr->id = atomic_inc_return(&ctlr_num) - 1;
  699. ctlr->f = f;
  700. ctlr->mode = FIP_CONN_TYPE_FABRIC;
  701. INIT_LIST_HEAD(&ctlr->fcfs);
  702. mutex_init(&ctlr->lock);
  703. ctlr->dev.parent = parent;
  704. ctlr->dev.bus = &fcoe_bus_type;
  705. ctlr->dev.type = &fcoe_ctlr_device_type;
  706. ctlr->fcf_dev_loss_tmo = fcoe_fcf_dev_loss_tmo;
  707. snprintf(ctlr->work_q_name, sizeof(ctlr->work_q_name),
  708. "ctlr_wq_%d", ctlr->id);
  709. ctlr->work_q = create_singlethread_workqueue(
  710. ctlr->work_q_name);
  711. if (!ctlr->work_q)
  712. goto out_del;
  713. snprintf(ctlr->devloss_work_q_name,
  714. sizeof(ctlr->devloss_work_q_name),
  715. "ctlr_dl_wq_%d", ctlr->id);
  716. ctlr->devloss_work_q = create_singlethread_workqueue(
  717. ctlr->devloss_work_q_name);
  718. if (!ctlr->devloss_work_q)
  719. goto out_del_q;
  720. dev_set_name(&ctlr->dev, "ctlr_%d", ctlr->id);
  721. error = device_register(&ctlr->dev);
  722. if (error)
  723. goto out_del_q2;
  724. return ctlr;
  725. out_del_q2:
  726. destroy_workqueue(ctlr->devloss_work_q);
  727. ctlr->devloss_work_q = NULL;
  728. out_del_q:
  729. destroy_workqueue(ctlr->work_q);
  730. ctlr->work_q = NULL;
  731. out_del:
  732. kfree(ctlr);
  733. out:
  734. return NULL;
  735. }
  736. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_add);
  737. /**
  738. * fcoe_ctlr_device_delete() - Delete a FIP ctlr and its subtree from sysfs
  739. * @ctlr: A pointer to the ctlr to be deleted
  740. *
  741. * Deletes a FIP ctlr and any fcfs attached
  742. * to it. Deleting fcfs will cause their childen
  743. * to be deleted as well.
  744. *
  745. * The ctlr is detached from sysfs and it's resources
  746. * are freed (work q), but the memory is not freed
  747. * until its last reference is released.
  748. *
  749. * This routine expects no locks to be held before
  750. * calling.
  751. *
  752. * TODO: Currently there are no callbacks to clean up LLD data
  753. * for a fcoe_fcf_device. LLDs must keep this in mind as they need
  754. * to clean up each of their LLD data for all fcoe_fcf_device before
  755. * calling fcoe_ctlr_device_delete.
  756. */
  757. void fcoe_ctlr_device_delete(struct fcoe_ctlr_device *ctlr)
  758. {
  759. struct fcoe_fcf_device *fcf, *next;
  760. /* Remove any attached fcfs */
  761. mutex_lock(&ctlr->lock);
  762. list_for_each_entry_safe(fcf, next,
  763. &ctlr->fcfs, peers) {
  764. list_del(&fcf->peers);
  765. fcf->state = FCOE_FCF_STATE_DELETED;
  766. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  767. }
  768. mutex_unlock(&ctlr->lock);
  769. fcoe_ctlr_device_flush_work(ctlr);
  770. destroy_workqueue(ctlr->devloss_work_q);
  771. ctlr->devloss_work_q = NULL;
  772. destroy_workqueue(ctlr->work_q);
  773. ctlr->work_q = NULL;
  774. device_unregister(&ctlr->dev);
  775. }
  776. EXPORT_SYMBOL_GPL(fcoe_ctlr_device_delete);
  777. /**
  778. * fcoe_fcf_device_final_delete() - Final delete routine
  779. * @work: The FIP fcf's embedded work struct
  780. *
  781. * It is expected that the fcf has been removed from
  782. * the FIP ctlr's list before calling this routine.
  783. */
  784. static void fcoe_fcf_device_final_delete(struct work_struct *work)
  785. {
  786. struct fcoe_fcf_device *fcf =
  787. container_of(work, struct fcoe_fcf_device, delete_work);
  788. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  789. /*
  790. * Cancel any outstanding timers. These should really exist
  791. * only when rmmod'ing the LLDD and we're asking for
  792. * immediate termination of the rports
  793. */
  794. if (!cancel_delayed_work(&fcf->dev_loss_work))
  795. fcoe_ctlr_device_flush_devloss(ctlr);
  796. device_unregister(&fcf->dev);
  797. }
  798. /**
  799. * fip_timeout_deleted_fcf() - Delete a fcf when the devloss timer fires
  800. * @work: The FIP fcf's embedded work struct
  801. *
  802. * Removes the fcf from the FIP ctlr's list of fcfs and
  803. * queues the final deletion.
  804. */
  805. static void fip_timeout_deleted_fcf(struct work_struct *work)
  806. {
  807. struct fcoe_fcf_device *fcf =
  808. container_of(work, struct fcoe_fcf_device, dev_loss_work.work);
  809. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  810. mutex_lock(&ctlr->lock);
  811. /*
  812. * If the fcf is deleted or reconnected before the timer
  813. * fires the devloss queue will be flushed, but the state will
  814. * either be CONNECTED or DELETED. If that is the case we
  815. * cancel deleting the fcf.
  816. */
  817. if (fcf->state != FCOE_FCF_STATE_DISCONNECTED)
  818. goto out;
  819. dev_printk(KERN_ERR, &fcf->dev,
  820. "FIP fcf connection time out: removing fcf\n");
  821. list_del(&fcf->peers);
  822. fcf->state = FCOE_FCF_STATE_DELETED;
  823. fcoe_ctlr_device_queue_work(ctlr, &fcf->delete_work);
  824. out:
  825. mutex_unlock(&ctlr->lock);
  826. }
  827. /**
  828. * fcoe_fcf_device_delete() - Delete a FIP fcf
  829. * @fcf: Pointer to the fcf which is to be deleted
  830. *
  831. * Queues the FIP fcf on the devloss workqueue
  832. *
  833. * Expects the ctlr_attrs mutex to be held for fcf
  834. * state change.
  835. */
  836. void fcoe_fcf_device_delete(struct fcoe_fcf_device *fcf)
  837. {
  838. struct fcoe_ctlr_device *ctlr = fcoe_fcf_dev_to_ctlr_dev(fcf);
  839. int timeout = fcf->dev_loss_tmo;
  840. if (fcf->state != FCOE_FCF_STATE_CONNECTED)
  841. return;
  842. fcf->state = FCOE_FCF_STATE_DISCONNECTED;
  843. /*
  844. * FCF will only be re-connected by the LLD calling
  845. * fcoe_fcf_device_add, and it should be setting up
  846. * priv then.
  847. */
  848. fcf->priv = NULL;
  849. fcoe_ctlr_device_queue_devloss_work(ctlr, &fcf->dev_loss_work,
  850. timeout * HZ);
  851. }
  852. EXPORT_SYMBOL_GPL(fcoe_fcf_device_delete);
  853. /**
  854. * fcoe_fcf_device_add() - Add a FCoE sysfs fcoe_fcf_device to the system
  855. * @ctlr: The fcoe_ctlr_device that will be the fcoe_fcf_device parent
  856. * @new_fcf: A temporary FCF used for lookups on the current list of fcfs
  857. *
  858. * Expects to be called with the ctlr->lock held
  859. */
  860. struct fcoe_fcf_device *fcoe_fcf_device_add(struct fcoe_ctlr_device *ctlr,
  861. struct fcoe_fcf_device *new_fcf)
  862. {
  863. struct fcoe_fcf_device *fcf;
  864. int error = 0;
  865. list_for_each_entry(fcf, &ctlr->fcfs, peers) {
  866. if (fcoe_fcf_device_match(new_fcf, fcf)) {
  867. if (fcf->state == FCOE_FCF_STATE_CONNECTED)
  868. return fcf;
  869. fcf->state = FCOE_FCF_STATE_CONNECTED;
  870. if (!cancel_delayed_work(&fcf->dev_loss_work))
  871. fcoe_ctlr_device_flush_devloss(ctlr);
  872. return fcf;
  873. }
  874. }
  875. fcf = kzalloc(sizeof(struct fcoe_fcf_device), GFP_ATOMIC);
  876. if (unlikely(!fcf))
  877. goto out;
  878. INIT_WORK(&fcf->delete_work, fcoe_fcf_device_final_delete);
  879. INIT_DELAYED_WORK(&fcf->dev_loss_work, fip_timeout_deleted_fcf);
  880. fcf->dev.parent = &ctlr->dev;
  881. fcf->dev.bus = &fcoe_bus_type;
  882. fcf->dev.type = &fcoe_fcf_device_type;
  883. fcf->id = atomic_inc_return(&fcf_num) - 1;
  884. fcf->state = FCOE_FCF_STATE_UNKNOWN;
  885. fcf->dev_loss_tmo = ctlr->fcf_dev_loss_tmo;
  886. dev_set_name(&fcf->dev, "fcf_%d", fcf->id);
  887. fcf->fabric_name = new_fcf->fabric_name;
  888. fcf->switch_name = new_fcf->switch_name;
  889. fcf->fc_map = new_fcf->fc_map;
  890. fcf->vfid = new_fcf->vfid;
  891. memcpy(fcf->mac, new_fcf->mac, ETH_ALEN);
  892. fcf->priority = new_fcf->priority;
  893. fcf->fka_period = new_fcf->fka_period;
  894. fcf->selected = new_fcf->selected;
  895. error = device_register(&fcf->dev);
  896. if (error)
  897. goto out_del;
  898. fcf->state = FCOE_FCF_STATE_CONNECTED;
  899. list_add_tail(&fcf->peers, &ctlr->fcfs);
  900. return fcf;
  901. out_del:
  902. kfree(fcf);
  903. out:
  904. return NULL;
  905. }
  906. EXPORT_SYMBOL_GPL(fcoe_fcf_device_add);
  907. int __init fcoe_sysfs_setup(void)
  908. {
  909. int error;
  910. atomic_set(&ctlr_num, 0);
  911. atomic_set(&fcf_num, 0);
  912. error = bus_register(&fcoe_bus_type);
  913. if (error)
  914. return error;
  915. return 0;
  916. }
  917. void __exit fcoe_sysfs_teardown(void)
  918. {
  919. bus_unregister(&fcoe_bus_type);
  920. }