sysfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /*
  2. * Copyright (c) 2012 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. /*#include "core_priv.h"*/
  33. #include "mlx4_ib.h"
  34. #include <linux/slab.h>
  35. #include <linux/string.h>
  36. #include <linux/stat.h>
  37. #include <rdma/ib_mad.h>
  38. /*show_admin_alias_guid returns the administratively assigned value of that GUID.
  39. * Values returned in buf parameter string:
  40. * 0 - requests opensm to assign a value.
  41. * ffffffffffffffff - delete this entry.
  42. * other - value assigned by administrator.
  43. */
  44. static ssize_t show_admin_alias_guid(struct device *dev,
  45. struct device_attribute *attr, char *buf)
  46. {
  47. struct mlx4_ib_iov_sysfs_attr *mlx4_ib_iov_dentry =
  48. container_of(attr, struct mlx4_ib_iov_sysfs_attr, dentry);
  49. struct mlx4_ib_iov_port *port = mlx4_ib_iov_dentry->ctx;
  50. struct mlx4_ib_dev *mdev = port->dev;
  51. __be64 sysadmin_ag_val;
  52. sysadmin_ag_val = mlx4_get_admin_guid(mdev->dev,
  53. mlx4_ib_iov_dentry->entry_num,
  54. port->num);
  55. return sprintf(buf, "%llx\n", be64_to_cpu(sysadmin_ag_val));
  56. }
  57. /* store_admin_alias_guid stores the (new) administratively assigned value of that GUID.
  58. * Values in buf parameter string:
  59. * 0 - requests opensm to assign a value.
  60. * 0xffffffffffffffff - delete this entry.
  61. * other - guid value assigned by the administrator.
  62. */
  63. static ssize_t store_admin_alias_guid(struct device *dev,
  64. struct device_attribute *attr,
  65. const char *buf, size_t count)
  66. {
  67. int record_num;/*0-15*/
  68. int guid_index_in_rec; /*0 - 7*/
  69. struct mlx4_ib_iov_sysfs_attr *mlx4_ib_iov_dentry =
  70. container_of(attr, struct mlx4_ib_iov_sysfs_attr, dentry);
  71. struct mlx4_ib_iov_port *port = mlx4_ib_iov_dentry->ctx;
  72. struct mlx4_ib_dev *mdev = port->dev;
  73. u64 sysadmin_ag_val;
  74. unsigned long flags;
  75. record_num = mlx4_ib_iov_dentry->entry_num / 8;
  76. guid_index_in_rec = mlx4_ib_iov_dentry->entry_num % 8;
  77. if (0 == record_num && 0 == guid_index_in_rec) {
  78. pr_err("GUID 0 block 0 is RO\n");
  79. return count;
  80. }
  81. spin_lock_irqsave(&mdev->sriov.alias_guid.ag_work_lock, flags);
  82. sscanf(buf, "%llx", &sysadmin_ag_val);
  83. *(__be64 *)&mdev->sriov.alias_guid.ports_guid[port->num - 1].
  84. all_rec_per_port[record_num].
  85. all_recs[GUID_REC_SIZE * guid_index_in_rec] =
  86. cpu_to_be64(sysadmin_ag_val);
  87. /* Change the state to be pending for update */
  88. mdev->sriov.alias_guid.ports_guid[port->num - 1].all_rec_per_port[record_num].status
  89. = MLX4_GUID_INFO_STATUS_IDLE ;
  90. mlx4_set_admin_guid(mdev->dev, cpu_to_be64(sysadmin_ag_val),
  91. mlx4_ib_iov_dentry->entry_num,
  92. port->num);
  93. /* set the record index */
  94. mdev->sriov.alias_guid.ports_guid[port->num - 1].all_rec_per_port[record_num].guid_indexes
  95. |= mlx4_ib_get_aguid_comp_mask_from_ix(guid_index_in_rec);
  96. spin_unlock_irqrestore(&mdev->sriov.alias_guid.ag_work_lock, flags);
  97. mlx4_ib_init_alias_guid_work(mdev, port->num - 1);
  98. return count;
  99. }
  100. static ssize_t show_port_gid(struct device *dev,
  101. struct device_attribute *attr,
  102. char *buf)
  103. {
  104. struct mlx4_ib_iov_sysfs_attr *mlx4_ib_iov_dentry =
  105. container_of(attr, struct mlx4_ib_iov_sysfs_attr, dentry);
  106. struct mlx4_ib_iov_port *port = mlx4_ib_iov_dentry->ctx;
  107. struct mlx4_ib_dev *mdev = port->dev;
  108. union ib_gid gid;
  109. ssize_t ret;
  110. ret = __mlx4_ib_query_gid(&mdev->ib_dev, port->num,
  111. mlx4_ib_iov_dentry->entry_num, &gid, 1);
  112. if (ret)
  113. return ret;
  114. ret = sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  115. be16_to_cpu(((__be16 *) gid.raw)[0]),
  116. be16_to_cpu(((__be16 *) gid.raw)[1]),
  117. be16_to_cpu(((__be16 *) gid.raw)[2]),
  118. be16_to_cpu(((__be16 *) gid.raw)[3]),
  119. be16_to_cpu(((__be16 *) gid.raw)[4]),
  120. be16_to_cpu(((__be16 *) gid.raw)[5]),
  121. be16_to_cpu(((__be16 *) gid.raw)[6]),
  122. be16_to_cpu(((__be16 *) gid.raw)[7]));
  123. return ret;
  124. }
  125. static ssize_t show_phys_port_pkey(struct device *dev,
  126. struct device_attribute *attr,
  127. char *buf)
  128. {
  129. struct mlx4_ib_iov_sysfs_attr *mlx4_ib_iov_dentry =
  130. container_of(attr, struct mlx4_ib_iov_sysfs_attr, dentry);
  131. struct mlx4_ib_iov_port *port = mlx4_ib_iov_dentry->ctx;
  132. struct mlx4_ib_dev *mdev = port->dev;
  133. u16 pkey;
  134. ssize_t ret;
  135. ret = __mlx4_ib_query_pkey(&mdev->ib_dev, port->num,
  136. mlx4_ib_iov_dentry->entry_num, &pkey, 1);
  137. if (ret)
  138. return ret;
  139. return sprintf(buf, "0x%04x\n", pkey);
  140. }
  141. #define DENTRY_REMOVE(_dentry) \
  142. do { \
  143. sysfs_remove_file((_dentry)->kobj, &(_dentry)->dentry.attr); \
  144. } while (0);
  145. static int create_sysfs_entry(void *_ctx, struct mlx4_ib_iov_sysfs_attr *_dentry,
  146. char *_name, struct kobject *_kobj,
  147. ssize_t (*show)(struct device *dev,
  148. struct device_attribute *attr,
  149. char *buf),
  150. ssize_t (*store)(struct device *dev,
  151. struct device_attribute *attr,
  152. const char *buf, size_t count)
  153. )
  154. {
  155. int ret = 0;
  156. struct mlx4_ib_iov_sysfs_attr *vdentry = _dentry;
  157. vdentry->ctx = _ctx;
  158. vdentry->dentry.show = show;
  159. vdentry->dentry.store = store;
  160. sysfs_attr_init(&vdentry->dentry.attr);
  161. vdentry->dentry.attr.name = vdentry->name;
  162. vdentry->dentry.attr.mode = 0;
  163. vdentry->kobj = _kobj;
  164. snprintf(vdentry->name, 15, "%s", _name);
  165. if (vdentry->dentry.store)
  166. vdentry->dentry.attr.mode |= S_IWUSR;
  167. if (vdentry->dentry.show)
  168. vdentry->dentry.attr.mode |= S_IRUGO;
  169. ret = sysfs_create_file(vdentry->kobj, &vdentry->dentry.attr);
  170. if (ret) {
  171. pr_err("failed to create %s\n", vdentry->dentry.attr.name);
  172. vdentry->ctx = NULL;
  173. return ret;
  174. }
  175. return ret;
  176. }
  177. int add_sysfs_port_mcg_attr(struct mlx4_ib_dev *device, int port_num,
  178. struct attribute *attr)
  179. {
  180. struct mlx4_ib_iov_port *port = &device->iov_ports[port_num - 1];
  181. int ret;
  182. ret = sysfs_create_file(port->mcgs_parent, attr);
  183. if (ret)
  184. pr_err("failed to create %s\n", attr->name);
  185. return ret;
  186. }
  187. void del_sysfs_port_mcg_attr(struct mlx4_ib_dev *device, int port_num,
  188. struct attribute *attr)
  189. {
  190. struct mlx4_ib_iov_port *port = &device->iov_ports[port_num - 1];
  191. sysfs_remove_file(port->mcgs_parent, attr);
  192. }
  193. static int add_port_entries(struct mlx4_ib_dev *device, int port_num)
  194. {
  195. int i;
  196. char buff[10];
  197. struct mlx4_ib_iov_port *port = NULL;
  198. int ret = 0 ;
  199. struct ib_port_attr attr;
  200. /* get the physical gid and pkey table sizes.*/
  201. ret = __mlx4_ib_query_port(&device->ib_dev, port_num, &attr, 1);
  202. if (ret)
  203. goto err;
  204. port = &device->iov_ports[port_num - 1];
  205. port->dev = device;
  206. port->num = port_num;
  207. /* Directory structure:
  208. * iov -
  209. * port num -
  210. * admin_guids
  211. * gids (operational)
  212. * mcg_table
  213. */
  214. port->dentr_ar = kzalloc(sizeof (struct mlx4_ib_iov_sysfs_attr_ar),
  215. GFP_KERNEL);
  216. if (!port->dentr_ar) {
  217. ret = -ENOMEM;
  218. goto err;
  219. }
  220. sprintf(buff, "%d", port_num);
  221. port->cur_port = kobject_create_and_add(buff,
  222. kobject_get(device->ports_parent));
  223. if (!port->cur_port) {
  224. ret = -ENOMEM;
  225. goto kobj_create_err;
  226. }
  227. /* admin GUIDs */
  228. port->admin_alias_parent = kobject_create_and_add("admin_guids",
  229. kobject_get(port->cur_port));
  230. if (!port->admin_alias_parent) {
  231. ret = -ENOMEM;
  232. goto err_admin_guids;
  233. }
  234. for (i = 0 ; i < attr.gid_tbl_len; i++) {
  235. sprintf(buff, "%d", i);
  236. port->dentr_ar->dentries[i].entry_num = i;
  237. ret = create_sysfs_entry(port, &port->dentr_ar->dentries[i],
  238. buff, port->admin_alias_parent,
  239. show_admin_alias_guid, store_admin_alias_guid);
  240. if (ret)
  241. goto err_admin_alias_parent;
  242. }
  243. /* gids subdirectory (operational gids) */
  244. port->gids_parent = kobject_create_and_add("gids",
  245. kobject_get(port->cur_port));
  246. if (!port->gids_parent) {
  247. ret = -ENOMEM;
  248. goto err_gids;
  249. }
  250. for (i = 0 ; i < attr.gid_tbl_len; i++) {
  251. sprintf(buff, "%d", i);
  252. port->dentr_ar->dentries[attr.gid_tbl_len + i].entry_num = i;
  253. ret = create_sysfs_entry(port,
  254. &port->dentr_ar->dentries[attr.gid_tbl_len + i],
  255. buff,
  256. port->gids_parent, show_port_gid, NULL);
  257. if (ret)
  258. goto err_gids_parent;
  259. }
  260. /* physical port pkey table */
  261. port->pkeys_parent =
  262. kobject_create_and_add("pkeys", kobject_get(port->cur_port));
  263. if (!port->pkeys_parent) {
  264. ret = -ENOMEM;
  265. goto err_pkeys;
  266. }
  267. for (i = 0 ; i < attr.pkey_tbl_len; i++) {
  268. sprintf(buff, "%d", i);
  269. port->dentr_ar->dentries[2 * attr.gid_tbl_len + i].entry_num = i;
  270. ret = create_sysfs_entry(port,
  271. &port->dentr_ar->dentries[2 * attr.gid_tbl_len + i],
  272. buff, port->pkeys_parent,
  273. show_phys_port_pkey, NULL);
  274. if (ret)
  275. goto err_pkeys_parent;
  276. }
  277. /* MCGs table */
  278. port->mcgs_parent =
  279. kobject_create_and_add("mcgs", kobject_get(port->cur_port));
  280. if (!port->mcgs_parent) {
  281. ret = -ENOMEM;
  282. goto err_mcgs;
  283. }
  284. return 0;
  285. err_mcgs:
  286. kobject_put(port->cur_port);
  287. err_pkeys_parent:
  288. kobject_put(port->pkeys_parent);
  289. err_pkeys:
  290. kobject_put(port->cur_port);
  291. err_gids_parent:
  292. kobject_put(port->gids_parent);
  293. err_gids:
  294. kobject_put(port->cur_port);
  295. err_admin_alias_parent:
  296. kobject_put(port->admin_alias_parent);
  297. err_admin_guids:
  298. kobject_put(port->cur_port);
  299. kobject_put(port->cur_port); /* once more for create_and_add buff */
  300. kobj_create_err:
  301. kobject_put(device->ports_parent);
  302. kfree(port->dentr_ar);
  303. err:
  304. pr_err("add_port_entries FAILED: for port:%d, error: %d\n",
  305. port_num, ret);
  306. return ret;
  307. }
  308. static void get_name(struct mlx4_ib_dev *dev, char *name, int i, int max)
  309. {
  310. char base_name[9];
  311. /* pci_name format is: bus:dev:func -> xxxx:yy:zz.n */
  312. strlcpy(name, pci_name(dev->dev->persist->pdev), max);
  313. strncpy(base_name, name, 8); /*till xxxx:yy:*/
  314. base_name[8] = '\0';
  315. /* with no ARI only 3 last bits are used so when the fn is higher than 8
  316. * need to add it to the dev num, so count in the last number will be
  317. * modulo 8 */
  318. sprintf(name, "%s%.2d.%d", base_name, (i/8), (i%8));
  319. }
  320. struct mlx4_port {
  321. struct kobject kobj;
  322. struct mlx4_ib_dev *dev;
  323. struct attribute_group pkey_group;
  324. struct attribute_group gid_group;
  325. struct device_attribute enable_smi_admin;
  326. struct device_attribute smi_enabled;
  327. int slave;
  328. u8 port_num;
  329. };
  330. static void mlx4_port_release(struct kobject *kobj)
  331. {
  332. struct mlx4_port *p = container_of(kobj, struct mlx4_port, kobj);
  333. struct attribute *a;
  334. int i;
  335. for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
  336. kfree(a);
  337. kfree(p->pkey_group.attrs);
  338. for (i = 0; (a = p->gid_group.attrs[i]); ++i)
  339. kfree(a);
  340. kfree(p->gid_group.attrs);
  341. kfree(p);
  342. }
  343. struct port_attribute {
  344. struct attribute attr;
  345. ssize_t (*show)(struct mlx4_port *, struct port_attribute *, char *buf);
  346. ssize_t (*store)(struct mlx4_port *, struct port_attribute *,
  347. const char *buf, size_t count);
  348. };
  349. static ssize_t port_attr_show(struct kobject *kobj,
  350. struct attribute *attr, char *buf)
  351. {
  352. struct port_attribute *port_attr =
  353. container_of(attr, struct port_attribute, attr);
  354. struct mlx4_port *p = container_of(kobj, struct mlx4_port, kobj);
  355. if (!port_attr->show)
  356. return -EIO;
  357. return port_attr->show(p, port_attr, buf);
  358. }
  359. static ssize_t port_attr_store(struct kobject *kobj,
  360. struct attribute *attr,
  361. const char *buf, size_t size)
  362. {
  363. struct port_attribute *port_attr =
  364. container_of(attr, struct port_attribute, attr);
  365. struct mlx4_port *p = container_of(kobj, struct mlx4_port, kobj);
  366. if (!port_attr->store)
  367. return -EIO;
  368. return port_attr->store(p, port_attr, buf, size);
  369. }
  370. static const struct sysfs_ops port_sysfs_ops = {
  371. .show = port_attr_show,
  372. .store = port_attr_store,
  373. };
  374. static struct kobj_type port_type = {
  375. .release = mlx4_port_release,
  376. .sysfs_ops = &port_sysfs_ops,
  377. };
  378. struct port_table_attribute {
  379. struct port_attribute attr;
  380. char name[8];
  381. int index;
  382. };
  383. static ssize_t show_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
  384. char *buf)
  385. {
  386. struct port_table_attribute *tab_attr =
  387. container_of(attr, struct port_table_attribute, attr);
  388. ssize_t ret = -ENODEV;
  389. if (p->dev->pkeys.virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index] >=
  390. (p->dev->dev->caps.pkey_table_len[p->port_num]))
  391. ret = sprintf(buf, "none\n");
  392. else
  393. ret = sprintf(buf, "%d\n",
  394. p->dev->pkeys.virt2phys_pkey[p->slave]
  395. [p->port_num - 1][tab_attr->index]);
  396. return ret;
  397. }
  398. static ssize_t store_port_pkey(struct mlx4_port *p, struct port_attribute *attr,
  399. const char *buf, size_t count)
  400. {
  401. struct port_table_attribute *tab_attr =
  402. container_of(attr, struct port_table_attribute, attr);
  403. int idx;
  404. int err;
  405. /* do not allow remapping Dom0 virtual pkey table */
  406. if (p->slave == mlx4_master_func_num(p->dev->dev))
  407. return -EINVAL;
  408. if (!strncasecmp(buf, "no", 2))
  409. idx = p->dev->dev->phys_caps.pkey_phys_table_len[p->port_num] - 1;
  410. else if (sscanf(buf, "%i", &idx) != 1 ||
  411. idx >= p->dev->dev->caps.pkey_table_len[p->port_num] ||
  412. idx < 0)
  413. return -EINVAL;
  414. p->dev->pkeys.virt2phys_pkey[p->slave][p->port_num - 1]
  415. [tab_attr->index] = idx;
  416. mlx4_sync_pkey_table(p->dev->dev, p->slave, p->port_num,
  417. tab_attr->index, idx);
  418. err = mlx4_gen_pkey_eqe(p->dev->dev, p->slave, p->port_num);
  419. if (err) {
  420. pr_err("mlx4_gen_pkey_eqe failed for slave %d,"
  421. " port %d, index %d\n", p->slave, p->port_num, idx);
  422. return err;
  423. }
  424. return count;
  425. }
  426. static ssize_t show_port_gid_idx(struct mlx4_port *p,
  427. struct port_attribute *attr, char *buf)
  428. {
  429. return sprintf(buf, "%d\n", p->slave);
  430. }
  431. static struct attribute **
  432. alloc_group_attrs(ssize_t (*show)(struct mlx4_port *,
  433. struct port_attribute *, char *buf),
  434. ssize_t (*store)(struct mlx4_port *, struct port_attribute *,
  435. const char *buf, size_t count),
  436. int len)
  437. {
  438. struct attribute **tab_attr;
  439. struct port_table_attribute *element;
  440. int i;
  441. tab_attr = kcalloc(1 + len, sizeof (struct attribute *), GFP_KERNEL);
  442. if (!tab_attr)
  443. return NULL;
  444. for (i = 0; i < len; i++) {
  445. element = kzalloc(sizeof (struct port_table_attribute),
  446. GFP_KERNEL);
  447. if (!element)
  448. goto err;
  449. if (snprintf(element->name, sizeof (element->name),
  450. "%d", i) >= sizeof (element->name)) {
  451. kfree(element);
  452. goto err;
  453. }
  454. sysfs_attr_init(&element->attr.attr);
  455. element->attr.attr.name = element->name;
  456. if (store) {
  457. element->attr.attr.mode = S_IWUSR | S_IRUGO;
  458. element->attr.store = store;
  459. } else
  460. element->attr.attr.mode = S_IRUGO;
  461. element->attr.show = show;
  462. element->index = i;
  463. tab_attr[i] = &element->attr.attr;
  464. }
  465. return tab_attr;
  466. err:
  467. while (--i >= 0)
  468. kfree(tab_attr[i]);
  469. kfree(tab_attr);
  470. return NULL;
  471. }
  472. static ssize_t sysfs_show_smi_enabled(struct device *dev,
  473. struct device_attribute *attr, char *buf)
  474. {
  475. struct mlx4_port *p =
  476. container_of(attr, struct mlx4_port, smi_enabled);
  477. ssize_t len = 0;
  478. if (mlx4_vf_smi_enabled(p->dev->dev, p->slave, p->port_num))
  479. len = sprintf(buf, "%d\n", 1);
  480. else
  481. len = sprintf(buf, "%d\n", 0);
  482. return len;
  483. }
  484. static ssize_t sysfs_show_enable_smi_admin(struct device *dev,
  485. struct device_attribute *attr,
  486. char *buf)
  487. {
  488. struct mlx4_port *p =
  489. container_of(attr, struct mlx4_port, enable_smi_admin);
  490. ssize_t len = 0;
  491. if (mlx4_vf_get_enable_smi_admin(p->dev->dev, p->slave, p->port_num))
  492. len = sprintf(buf, "%d\n", 1);
  493. else
  494. len = sprintf(buf, "%d\n", 0);
  495. return len;
  496. }
  497. static ssize_t sysfs_store_enable_smi_admin(struct device *dev,
  498. struct device_attribute *attr,
  499. const char *buf, size_t count)
  500. {
  501. struct mlx4_port *p =
  502. container_of(attr, struct mlx4_port, enable_smi_admin);
  503. int enable;
  504. if (sscanf(buf, "%i", &enable) != 1 ||
  505. enable < 0 || enable > 1)
  506. return -EINVAL;
  507. if (mlx4_vf_set_enable_smi_admin(p->dev->dev, p->slave, p->port_num, enable))
  508. return -EINVAL;
  509. return count;
  510. }
  511. static int add_vf_smi_entries(struct mlx4_port *p)
  512. {
  513. int is_eth = rdma_port_get_link_layer(&p->dev->ib_dev, p->port_num) ==
  514. IB_LINK_LAYER_ETHERNET;
  515. int ret;
  516. /* do not display entries if eth transport, or if master */
  517. if (is_eth || p->slave == mlx4_master_func_num(p->dev->dev))
  518. return 0;
  519. sysfs_attr_init(&p->smi_enabled.attr);
  520. p->smi_enabled.show = sysfs_show_smi_enabled;
  521. p->smi_enabled.store = NULL;
  522. p->smi_enabled.attr.name = "smi_enabled";
  523. p->smi_enabled.attr.mode = 0444;
  524. ret = sysfs_create_file(&p->kobj, &p->smi_enabled.attr);
  525. if (ret) {
  526. pr_err("failed to create smi_enabled\n");
  527. return ret;
  528. }
  529. sysfs_attr_init(&p->enable_smi_admin.attr);
  530. p->enable_smi_admin.show = sysfs_show_enable_smi_admin;
  531. p->enable_smi_admin.store = sysfs_store_enable_smi_admin;
  532. p->enable_smi_admin.attr.name = "enable_smi_admin";
  533. p->enable_smi_admin.attr.mode = 0644;
  534. ret = sysfs_create_file(&p->kobj, &p->enable_smi_admin.attr);
  535. if (ret) {
  536. pr_err("failed to create enable_smi_admin\n");
  537. sysfs_remove_file(&p->kobj, &p->smi_enabled.attr);
  538. return ret;
  539. }
  540. return 0;
  541. }
  542. static void remove_vf_smi_entries(struct mlx4_port *p)
  543. {
  544. int is_eth = rdma_port_get_link_layer(&p->dev->ib_dev, p->port_num) ==
  545. IB_LINK_LAYER_ETHERNET;
  546. if (is_eth || p->slave == mlx4_master_func_num(p->dev->dev))
  547. return;
  548. sysfs_remove_file(&p->kobj, &p->smi_enabled.attr);
  549. sysfs_remove_file(&p->kobj, &p->enable_smi_admin.attr);
  550. }
  551. static int add_port(struct mlx4_ib_dev *dev, int port_num, int slave)
  552. {
  553. struct mlx4_port *p;
  554. int i;
  555. int ret;
  556. int is_eth = rdma_port_get_link_layer(&dev->ib_dev, port_num) ==
  557. IB_LINK_LAYER_ETHERNET;
  558. p = kzalloc(sizeof *p, GFP_KERNEL);
  559. if (!p)
  560. return -ENOMEM;
  561. p->dev = dev;
  562. p->port_num = port_num;
  563. p->slave = slave;
  564. ret = kobject_init_and_add(&p->kobj, &port_type,
  565. kobject_get(dev->dev_ports_parent[slave]),
  566. "%d", port_num);
  567. if (ret)
  568. goto err_alloc;
  569. p->pkey_group.name = "pkey_idx";
  570. p->pkey_group.attrs =
  571. alloc_group_attrs(show_port_pkey,
  572. is_eth ? NULL : store_port_pkey,
  573. dev->dev->caps.pkey_table_len[port_num]);
  574. if (!p->pkey_group.attrs) {
  575. ret = -ENOMEM;
  576. goto err_alloc;
  577. }
  578. ret = sysfs_create_group(&p->kobj, &p->pkey_group);
  579. if (ret)
  580. goto err_free_pkey;
  581. p->gid_group.name = "gid_idx";
  582. p->gid_group.attrs = alloc_group_attrs(show_port_gid_idx, NULL, 1);
  583. if (!p->gid_group.attrs) {
  584. ret = -ENOMEM;
  585. goto err_free_pkey;
  586. }
  587. ret = sysfs_create_group(&p->kobj, &p->gid_group);
  588. if (ret)
  589. goto err_free_gid;
  590. ret = add_vf_smi_entries(p);
  591. if (ret)
  592. goto err_free_gid;
  593. list_add_tail(&p->kobj.entry, &dev->pkeys.pkey_port_list[slave]);
  594. return 0;
  595. err_free_gid:
  596. kfree(p->gid_group.attrs[0]);
  597. kfree(p->gid_group.attrs);
  598. err_free_pkey:
  599. for (i = 0; i < dev->dev->caps.pkey_table_len[port_num]; ++i)
  600. kfree(p->pkey_group.attrs[i]);
  601. kfree(p->pkey_group.attrs);
  602. err_alloc:
  603. kobject_put(dev->dev_ports_parent[slave]);
  604. kfree(p);
  605. return ret;
  606. }
  607. static int register_one_pkey_tree(struct mlx4_ib_dev *dev, int slave)
  608. {
  609. char name[32];
  610. int err;
  611. int port;
  612. struct kobject *p, *t;
  613. struct mlx4_port *mport;
  614. struct mlx4_active_ports actv_ports;
  615. get_name(dev, name, slave, sizeof name);
  616. dev->pkeys.device_parent[slave] =
  617. kobject_create_and_add(name, kobject_get(dev->iov_parent));
  618. if (!dev->pkeys.device_parent[slave]) {
  619. err = -ENOMEM;
  620. goto fail_dev;
  621. }
  622. INIT_LIST_HEAD(&dev->pkeys.pkey_port_list[slave]);
  623. dev->dev_ports_parent[slave] =
  624. kobject_create_and_add("ports",
  625. kobject_get(dev->pkeys.device_parent[slave]));
  626. if (!dev->dev_ports_parent[slave]) {
  627. err = -ENOMEM;
  628. goto err_ports;
  629. }
  630. actv_ports = mlx4_get_active_ports(dev->dev, slave);
  631. for (port = 1; port <= dev->dev->caps.num_ports; ++port) {
  632. if (!test_bit(port - 1, actv_ports.ports))
  633. continue;
  634. err = add_port(dev, port, slave);
  635. if (err)
  636. goto err_add;
  637. }
  638. return 0;
  639. err_add:
  640. list_for_each_entry_safe(p, t,
  641. &dev->pkeys.pkey_port_list[slave],
  642. entry) {
  643. list_del(&p->entry);
  644. mport = container_of(p, struct mlx4_port, kobj);
  645. sysfs_remove_group(p, &mport->pkey_group);
  646. sysfs_remove_group(p, &mport->gid_group);
  647. remove_vf_smi_entries(mport);
  648. kobject_put(p);
  649. }
  650. kobject_put(dev->dev_ports_parent[slave]);
  651. err_ports:
  652. kobject_put(dev->pkeys.device_parent[slave]);
  653. /* extra put for the device_parent create_and_add */
  654. kobject_put(dev->pkeys.device_parent[slave]);
  655. fail_dev:
  656. kobject_put(dev->iov_parent);
  657. return err;
  658. }
  659. static int register_pkey_tree(struct mlx4_ib_dev *device)
  660. {
  661. int i;
  662. if (!mlx4_is_master(device->dev))
  663. return 0;
  664. for (i = 0; i <= device->dev->persist->num_vfs; ++i)
  665. register_one_pkey_tree(device, i);
  666. return 0;
  667. }
  668. static void unregister_pkey_tree(struct mlx4_ib_dev *device)
  669. {
  670. int slave;
  671. struct kobject *p, *t;
  672. struct mlx4_port *port;
  673. if (!mlx4_is_master(device->dev))
  674. return;
  675. for (slave = device->dev->persist->num_vfs; slave >= 0; --slave) {
  676. list_for_each_entry_safe(p, t,
  677. &device->pkeys.pkey_port_list[slave],
  678. entry) {
  679. list_del(&p->entry);
  680. port = container_of(p, struct mlx4_port, kobj);
  681. sysfs_remove_group(p, &port->pkey_group);
  682. sysfs_remove_group(p, &port->gid_group);
  683. remove_vf_smi_entries(port);
  684. kobject_put(p);
  685. kobject_put(device->dev_ports_parent[slave]);
  686. }
  687. kobject_put(device->dev_ports_parent[slave]);
  688. kobject_put(device->pkeys.device_parent[slave]);
  689. kobject_put(device->pkeys.device_parent[slave]);
  690. kobject_put(device->iov_parent);
  691. }
  692. }
  693. int mlx4_ib_device_register_sysfs(struct mlx4_ib_dev *dev)
  694. {
  695. int i;
  696. int ret = 0;
  697. if (!mlx4_is_master(dev->dev))
  698. return 0;
  699. dev->iov_parent =
  700. kobject_create_and_add("iov",
  701. kobject_get(dev->ib_dev.ports_parent->parent));
  702. if (!dev->iov_parent) {
  703. ret = -ENOMEM;
  704. goto err;
  705. }
  706. dev->ports_parent =
  707. kobject_create_and_add("ports",
  708. kobject_get(dev->iov_parent));
  709. if (!dev->ports_parent) {
  710. ret = -ENOMEM;
  711. goto err_ports;
  712. }
  713. for (i = 1; i <= dev->ib_dev.phys_port_cnt; ++i) {
  714. ret = add_port_entries(dev, i);
  715. if (ret)
  716. goto err_add_entries;
  717. }
  718. ret = register_pkey_tree(dev);
  719. if (ret)
  720. goto err_add_entries;
  721. return 0;
  722. err_add_entries:
  723. kobject_put(dev->ports_parent);
  724. err_ports:
  725. kobject_put(dev->iov_parent);
  726. err:
  727. kobject_put(dev->ib_dev.ports_parent->parent);
  728. pr_err("mlx4_ib_device_register_sysfs error (%d)\n", ret);
  729. return ret;
  730. }
  731. static void unregister_alias_guid_tree(struct mlx4_ib_dev *device)
  732. {
  733. struct mlx4_ib_iov_port *p;
  734. int i;
  735. if (!mlx4_is_master(device->dev))
  736. return;
  737. for (i = 0; i < device->dev->caps.num_ports; i++) {
  738. p = &device->iov_ports[i];
  739. kobject_put(p->admin_alias_parent);
  740. kobject_put(p->gids_parent);
  741. kobject_put(p->pkeys_parent);
  742. kobject_put(p->mcgs_parent);
  743. kobject_put(p->cur_port);
  744. kobject_put(p->cur_port);
  745. kobject_put(p->cur_port);
  746. kobject_put(p->cur_port);
  747. kobject_put(p->cur_port);
  748. kobject_put(p->dev->ports_parent);
  749. kfree(p->dentr_ar);
  750. }
  751. }
  752. void mlx4_ib_device_unregister_sysfs(struct mlx4_ib_dev *device)
  753. {
  754. unregister_alias_guid_tree(device);
  755. unregister_pkey_tree(device);
  756. kobject_put(device->ports_parent);
  757. kobject_put(device->iov_parent);
  758. kobject_put(device->iov_parent);
  759. kobject_put(device->ib_dev.ports_parent->parent);
  760. }