i40e_configfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/configfs.h>
  27. #include "i40e.h"
  28. #if IS_ENABLED(CONFIG_CONFIGFS_FS)
  29. /**
  30. * configfs structure for i40e
  31. *
  32. * This file adds code for configfs support for the i40e driver. This sets
  33. * up a filesystem under /sys/kernel/config in which configuration changes
  34. * can be made for the driver's netdevs.
  35. *
  36. * The initialization in this code creates the "i40e" entry in the configfs
  37. * system. After that, the user needs to use mkdir to create configurations
  38. * for specific netdev ports; for example "mkdir eth3". This code will verify
  39. * that such a netdev exists and that it is owned by i40e.
  40. *
  41. **/
  42. struct i40e_cfgfs_vsi {
  43. struct config_item item;
  44. struct i40e_vsi *vsi;
  45. };
  46. static inline struct i40e_cfgfs_vsi *to_i40e_cfgfs_vsi(struct config_item *item)
  47. {
  48. return item ? container_of(item, struct i40e_cfgfs_vsi, item) : NULL;
  49. }
  50. static struct configfs_attribute i40e_cfgfs_vsi_attr_min_bw = {
  51. .ca_owner = THIS_MODULE,
  52. .ca_name = "min_bw",
  53. .ca_mode = S_IRUGO | S_IWUSR,
  54. };
  55. static struct configfs_attribute i40e_cfgfs_vsi_attr_max_bw = {
  56. .ca_owner = THIS_MODULE,
  57. .ca_name = "max_bw",
  58. .ca_mode = S_IRUGO | S_IWUSR,
  59. };
  60. static struct configfs_attribute i40e_cfgfs_vsi_attr_commit = {
  61. .ca_owner = THIS_MODULE,
  62. .ca_name = "commit",
  63. .ca_mode = S_IRUGO | S_IWUSR,
  64. };
  65. static struct configfs_attribute i40e_cfgfs_vsi_attr_port_count = {
  66. .ca_owner = THIS_MODULE,
  67. .ca_name = "ports",
  68. .ca_mode = S_IRUGO | S_IWUSR,
  69. };
  70. static struct configfs_attribute i40e_cfgfs_vsi_attr_part_count = {
  71. .ca_owner = THIS_MODULE,
  72. .ca_name = "partitions",
  73. .ca_mode = S_IRUGO | S_IWUSR,
  74. };
  75. static struct configfs_attribute *i40e_cfgfs_vsi_attrs[] = {
  76. &i40e_cfgfs_vsi_attr_min_bw,
  77. &i40e_cfgfs_vsi_attr_max_bw,
  78. &i40e_cfgfs_vsi_attr_commit,
  79. &i40e_cfgfs_vsi_attr_port_count,
  80. &i40e_cfgfs_vsi_attr_part_count,
  81. NULL,
  82. };
  83. /**
  84. * i40e_cfgfs_vsi_attr_show - Show a VSI's NPAR BW partition info
  85. * @item: A pointer back to the configfs item created on driver load
  86. * @attr: A pointer to this item's configuration attribute
  87. * @page: A pointer to the output buffer
  88. **/
  89. static ssize_t i40e_cfgfs_vsi_attr_show(struct config_item *item,
  90. struct configfs_attribute *attr,
  91. char *page)
  92. {
  93. struct i40e_cfgfs_vsi *i40e_cfgfs_vsi = to_i40e_cfgfs_vsi(item);
  94. struct i40e_pf *pf = i40e_cfgfs_vsi->vsi->back;
  95. ssize_t count;
  96. if (i40e_cfgfs_vsi->vsi != pf->vsi[pf->lan_vsi])
  97. return 0;
  98. if (strncmp(attr->ca_name, "min_bw", 6) == 0)
  99. count = sprintf(page, "%s %s %d%%\n",
  100. i40e_cfgfs_vsi->vsi->netdev->name,
  101. (pf->npar_min_bw & I40E_ALT_BW_RELATIVE_MASK) ?
  102. "Relative Min BW" : "Absolute Min BW",
  103. pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK);
  104. else if (strncmp(attr->ca_name, "max_bw", 6) == 0)
  105. count = sprintf(page, "%s %s %d%%\n",
  106. i40e_cfgfs_vsi->vsi->netdev->name,
  107. (pf->npar_max_bw & I40E_ALT_BW_RELATIVE_MASK) ?
  108. "Relative Max BW" : "Absolute Max BW",
  109. pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK);
  110. else if (strncmp(attr->ca_name, "ports", 5) == 0)
  111. count = sprintf(page, "%d\n",
  112. pf->hw.num_ports);
  113. else if (strncmp(attr->ca_name, "partitions", 10) == 0)
  114. count = sprintf(page, "%d\n",
  115. pf->hw.num_partitions);
  116. else
  117. return 0;
  118. return count;
  119. }
  120. /**
  121. * i40e_cfgfs_vsi_attr_store - Show a VSI's NPAR BW partition info
  122. * @item: A pointer back to the configfs item created on driver load
  123. * @attr: A pointer to this item's configuration attribute
  124. * @page: A pointer to the user input buffer holding the user input values
  125. **/
  126. static ssize_t i40e_cfgfs_vsi_attr_store(struct config_item *item,
  127. struct configfs_attribute *attr,
  128. const char *page, size_t count)
  129. {
  130. struct i40e_cfgfs_vsi *i40e_cfgfs_vsi = to_i40e_cfgfs_vsi(item);
  131. struct i40e_pf *pf = i40e_cfgfs_vsi->vsi->back;
  132. char *p = (char *)page;
  133. int rc;
  134. unsigned long tmp;
  135. if (i40e_cfgfs_vsi->vsi != pf->vsi[pf->lan_vsi])
  136. return 0;
  137. if (!p || (*p && (*p == '\n')))
  138. return -EINVAL;
  139. rc = kstrtoul(p, 10, &tmp);
  140. if (rc)
  141. return rc;
  142. if (tmp > 100)
  143. return -ERANGE;
  144. if (strncmp(attr->ca_name, "min_bw", 6) == 0) {
  145. if (tmp > (pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK))
  146. return -ERANGE;
  147. /* Preserve the valid and relative BW bits - the rest is
  148. * don't care.
  149. */
  150. pf->npar_min_bw &= (I40E_ALT_BW_RELATIVE_MASK |
  151. I40E_ALT_BW_VALID_MASK);
  152. pf->npar_min_bw |= (tmp & I40E_ALT_BW_VALUE_MASK);
  153. i40e_set_npar_bw_setting(pf);
  154. } else if (strncmp(attr->ca_name, "max_bw", 6) == 0) {
  155. if (tmp < 1 ||
  156. tmp < (pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK))
  157. return -ERANGE;
  158. /* Preserve the valid and relative BW bits - the rest is
  159. * don't care.
  160. */
  161. pf->npar_max_bw &= (I40E_ALT_BW_RELATIVE_MASK |
  162. I40E_ALT_BW_VALID_MASK);
  163. pf->npar_max_bw |= (tmp & I40E_ALT_BW_VALUE_MASK);
  164. i40e_set_npar_bw_setting(pf);
  165. } else if (strncmp(attr->ca_name, "commit", 6) == 0 && tmp == 1) {
  166. if (i40e_commit_npar_bw_setting(pf))
  167. return -EIO;
  168. }
  169. return count;
  170. }
  171. /**
  172. * i40e_cfgfs_vsi_release - Free up the configuration item memory
  173. * @item: A pointer back to the configfs item created on driver load
  174. **/
  175. static void i40e_cfgfs_vsi_release(struct config_item *item)
  176. {
  177. kfree(to_i40e_cfgfs_vsi(item));
  178. }
  179. static struct configfs_item_operations i40e_cfgfs_vsi_item_ops = {
  180. .release = i40e_cfgfs_vsi_release,
  181. .show_attribute = i40e_cfgfs_vsi_attr_show,
  182. .store_attribute = i40e_cfgfs_vsi_attr_store,
  183. };
  184. static struct config_item_type i40e_cfgfs_vsi_type = {
  185. .ct_item_ops = &i40e_cfgfs_vsi_item_ops,
  186. .ct_attrs = i40e_cfgfs_vsi_attrs,
  187. .ct_owner = THIS_MODULE,
  188. };
  189. struct i40e_cfgfs_group {
  190. struct config_group group;
  191. };
  192. /**
  193. * to_i40e_cfgfs_group - Get the group pointer from the config item
  194. * @item: A pointer back to the configfs item created on driver load
  195. **/
  196. static inline struct i40e_cfgfs_group *
  197. to_i40e_cfgfs_group(struct config_item *item)
  198. {
  199. return item ? container_of(to_config_group(item),
  200. struct i40e_cfgfs_group, group) : NULL;
  201. }
  202. /**
  203. * i40e_cfgfs_group_make_item - Create the configfs item with group container
  204. * @group: A pointer to our configfs group
  205. * @name: A pointer to the nume of the device we're looking for
  206. **/
  207. static struct config_item *
  208. i40e_cfgfs_group_make_item(struct config_group *group, const char *name)
  209. {
  210. struct i40e_cfgfs_vsi *i40e_cfgfs_vsi;
  211. struct net_device *netdev;
  212. struct i40e_netdev_priv *np;
  213. read_lock(&dev_base_lock);
  214. netdev = first_net_device(&init_net);
  215. while (netdev) {
  216. if (strncmp(netdev->name, name, sizeof(netdev->name)) == 0)
  217. break;
  218. netdev = next_net_device(netdev);
  219. }
  220. read_unlock(&dev_base_lock);
  221. if (!netdev)
  222. return ERR_PTR(-ENODEV);
  223. /* is this netdev owned by i40e? */
  224. if (netdev->netdev_ops->ndo_open != i40e_open)
  225. return ERR_PTR(-EACCES);
  226. i40e_cfgfs_vsi = kzalloc(sizeof(*i40e_cfgfs_vsi), GFP_KERNEL);
  227. if (!i40e_cfgfs_vsi)
  228. return ERR_PTR(-ENOMEM);
  229. np = netdev_priv(netdev);
  230. i40e_cfgfs_vsi->vsi = np->vsi;
  231. config_item_init_type_name(&i40e_cfgfs_vsi->item, name,
  232. &i40e_cfgfs_vsi_type);
  233. return &i40e_cfgfs_vsi->item;
  234. }
  235. static struct configfs_attribute i40e_cfgfs_group_attr_description = {
  236. .ca_owner = THIS_MODULE,
  237. .ca_name = "description",
  238. .ca_mode = S_IRUGO,
  239. };
  240. static struct configfs_attribute *i40e_cfgfs_group_attrs[] = {
  241. &i40e_cfgfs_group_attr_description,
  242. NULL,
  243. };
  244. static ssize_t i40e_cfgfs_group_attr_show(struct config_item *item,
  245. struct configfs_attribute *attr,
  246. char *page)
  247. {
  248. return sprintf(page,
  249. "i40e\n"
  250. "\n"
  251. "This subsystem allows the modification of network port configurations.\n"
  252. "To start, use the name of the network port to be configured in a 'mkdir'\n"
  253. "command, e.g. 'mkdir eth3'.\n");
  254. }
  255. static void i40e_cfgfs_group_release(struct config_item *item)
  256. {
  257. kfree(to_i40e_cfgfs_group(item));
  258. }
  259. static struct configfs_item_operations i40e_cfgfs_group_item_ops = {
  260. .release = i40e_cfgfs_group_release,
  261. .show_attribute = i40e_cfgfs_group_attr_show,
  262. };
  263. /* Note that, since no extra work is required on ->drop_item(),
  264. * no ->drop_item() is provided.
  265. */
  266. static struct configfs_group_operations i40e_cfgfs_group_ops = {
  267. .make_item = i40e_cfgfs_group_make_item,
  268. };
  269. static struct config_item_type i40e_cfgfs_group_type = {
  270. .ct_item_ops = &i40e_cfgfs_group_item_ops,
  271. .ct_group_ops = &i40e_cfgfs_group_ops,
  272. .ct_attrs = i40e_cfgfs_group_attrs,
  273. .ct_owner = THIS_MODULE,
  274. };
  275. static struct configfs_subsystem i40e_cfgfs_group_subsys = {
  276. .su_group = {
  277. .cg_item = {
  278. .ci_namebuf = "i40e",
  279. .ci_type = &i40e_cfgfs_group_type,
  280. },
  281. },
  282. };
  283. /**
  284. * i40e_configfs_init - Initialize configfs support for our driver
  285. **/
  286. int i40e_configfs_init(void)
  287. {
  288. int ret;
  289. struct configfs_subsystem *subsys;
  290. subsys = &i40e_cfgfs_group_subsys;
  291. config_group_init(&subsys->su_group);
  292. mutex_init(&subsys->su_mutex);
  293. ret = configfs_register_subsystem(subsys);
  294. if (ret) {
  295. pr_err("Error %d while registering configfs subsystem %s\n",
  296. ret, subsys->su_group.cg_item.ci_namebuf);
  297. return ret;
  298. }
  299. return 0;
  300. }
  301. /**
  302. * i40e_configfs_init - Bail out - unregister configfs subsystem and release
  303. **/
  304. void i40e_configfs_exit(void)
  305. {
  306. configfs_unregister_subsystem(&i40e_cfgfs_group_subsys);
  307. }
  308. #endif /* IS_ENABLED(CONFIG_CONFIGFS_FS) */