usnic_ib_sysfs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (c) 2013, Cisco Systems, Inc. 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. * 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. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/errno.h>
  36. #include <rdma/ib_user_verbs.h>
  37. #include <rdma/ib_addr.h>
  38. #include "usnic_common_util.h"
  39. #include "usnic_ib.h"
  40. #include "usnic_ib_qp_grp.h"
  41. #include "usnic_vnic.h"
  42. #include "usnic_ib_verbs.h"
  43. #include "usnic_log.h"
  44. static ssize_t usnic_ib_show_board(struct device *device,
  45. struct device_attribute *attr,
  46. char *buf)
  47. {
  48. struct usnic_ib_dev *us_ibdev =
  49. container_of(device, struct usnic_ib_dev, ib_dev.dev);
  50. unsigned short subsystem_device_id;
  51. mutex_lock(&us_ibdev->usdev_lock);
  52. subsystem_device_id = us_ibdev->pdev->subsystem_device;
  53. mutex_unlock(&us_ibdev->usdev_lock);
  54. return scnprintf(buf, PAGE_SIZE, "%hu\n", subsystem_device_id);
  55. }
  56. /*
  57. * Report the configuration for this PF
  58. */
  59. static ssize_t
  60. usnic_ib_show_config(struct device *device, struct device_attribute *attr,
  61. char *buf)
  62. {
  63. struct usnic_ib_dev *us_ibdev;
  64. char *ptr;
  65. unsigned left;
  66. unsigned n;
  67. enum usnic_vnic_res_type res_type;
  68. us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
  69. /* Buffer space limit is 1 page */
  70. ptr = buf;
  71. left = PAGE_SIZE;
  72. mutex_lock(&us_ibdev->usdev_lock);
  73. if (kref_read(&us_ibdev->vf_cnt) > 0) {
  74. char *busname;
  75. /*
  76. * bus name seems to come with annoying prefix.
  77. * Remove it if it is predictable
  78. */
  79. busname = us_ibdev->pdev->bus->name;
  80. if (strncmp(busname, "PCI Bus ", 8) == 0)
  81. busname += 8;
  82. n = scnprintf(ptr, left,
  83. "%s: %s:%d.%d, %s, %pM, %u VFs\n Per VF:",
  84. us_ibdev->ib_dev.name,
  85. busname,
  86. PCI_SLOT(us_ibdev->pdev->devfn),
  87. PCI_FUNC(us_ibdev->pdev->devfn),
  88. netdev_name(us_ibdev->netdev),
  89. us_ibdev->ufdev->mac,
  90. kref_read(&us_ibdev->vf_cnt));
  91. UPDATE_PTR_LEFT(n, ptr, left);
  92. for (res_type = USNIC_VNIC_RES_TYPE_EOL;
  93. res_type < USNIC_VNIC_RES_TYPE_MAX;
  94. res_type++) {
  95. if (us_ibdev->vf_res_cnt[res_type] == 0)
  96. continue;
  97. n = scnprintf(ptr, left, " %d %s%s",
  98. us_ibdev->vf_res_cnt[res_type],
  99. usnic_vnic_res_type_to_str(res_type),
  100. (res_type < (USNIC_VNIC_RES_TYPE_MAX - 1)) ?
  101. "," : "");
  102. UPDATE_PTR_LEFT(n, ptr, left);
  103. }
  104. n = scnprintf(ptr, left, "\n");
  105. UPDATE_PTR_LEFT(n, ptr, left);
  106. } else {
  107. n = scnprintf(ptr, left, "%s: no VFs\n",
  108. us_ibdev->ib_dev.name);
  109. UPDATE_PTR_LEFT(n, ptr, left);
  110. }
  111. mutex_unlock(&us_ibdev->usdev_lock);
  112. return ptr - buf;
  113. }
  114. static ssize_t
  115. usnic_ib_show_iface(struct device *device, struct device_attribute *attr,
  116. char *buf)
  117. {
  118. struct usnic_ib_dev *us_ibdev;
  119. us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
  120. return scnprintf(buf, PAGE_SIZE, "%s\n",
  121. netdev_name(us_ibdev->netdev));
  122. }
  123. static ssize_t
  124. usnic_ib_show_max_vf(struct device *device, struct device_attribute *attr,
  125. char *buf)
  126. {
  127. struct usnic_ib_dev *us_ibdev;
  128. us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
  129. return scnprintf(buf, PAGE_SIZE, "%u\n",
  130. kref_read(&us_ibdev->vf_cnt));
  131. }
  132. static ssize_t
  133. usnic_ib_show_qp_per_vf(struct device *device, struct device_attribute *attr,
  134. char *buf)
  135. {
  136. struct usnic_ib_dev *us_ibdev;
  137. int qp_per_vf;
  138. us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
  139. qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
  140. us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
  141. return scnprintf(buf, PAGE_SIZE,
  142. "%d\n", qp_per_vf);
  143. }
  144. static ssize_t
  145. usnic_ib_show_cq_per_vf(struct device *device, struct device_attribute *attr,
  146. char *buf)
  147. {
  148. struct usnic_ib_dev *us_ibdev;
  149. us_ibdev = container_of(device, struct usnic_ib_dev, ib_dev.dev);
  150. return scnprintf(buf, PAGE_SIZE, "%d\n",
  151. us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ]);
  152. }
  153. static DEVICE_ATTR(board_id, S_IRUGO, usnic_ib_show_board, NULL);
  154. static DEVICE_ATTR(config, S_IRUGO, usnic_ib_show_config, NULL);
  155. static DEVICE_ATTR(iface, S_IRUGO, usnic_ib_show_iface, NULL);
  156. static DEVICE_ATTR(max_vf, S_IRUGO, usnic_ib_show_max_vf, NULL);
  157. static DEVICE_ATTR(qp_per_vf, S_IRUGO, usnic_ib_show_qp_per_vf, NULL);
  158. static DEVICE_ATTR(cq_per_vf, S_IRUGO, usnic_ib_show_cq_per_vf, NULL);
  159. static struct device_attribute *usnic_class_attributes[] = {
  160. &dev_attr_board_id,
  161. &dev_attr_config,
  162. &dev_attr_iface,
  163. &dev_attr_max_vf,
  164. &dev_attr_qp_per_vf,
  165. &dev_attr_cq_per_vf,
  166. };
  167. struct qpn_attribute {
  168. struct attribute attr;
  169. ssize_t (*show)(struct usnic_ib_qp_grp *, char *buf);
  170. };
  171. /*
  172. * Definitions for supporting QPN entries in sysfs
  173. */
  174. static ssize_t
  175. usnic_ib_qpn_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  176. {
  177. struct usnic_ib_qp_grp *qp_grp;
  178. struct qpn_attribute *qpn_attr;
  179. qp_grp = container_of(kobj, struct usnic_ib_qp_grp, kobj);
  180. qpn_attr = container_of(attr, struct qpn_attribute, attr);
  181. return qpn_attr->show(qp_grp, buf);
  182. }
  183. static const struct sysfs_ops usnic_ib_qpn_sysfs_ops = {
  184. .show = usnic_ib_qpn_attr_show
  185. };
  186. #define QPN_ATTR_RO(NAME) \
  187. struct qpn_attribute qpn_attr_##NAME = __ATTR_RO(NAME)
  188. static ssize_t context_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
  189. {
  190. return scnprintf(buf, PAGE_SIZE, "0x%p\n", qp_grp->ctx);
  191. }
  192. static ssize_t summary_show(struct usnic_ib_qp_grp *qp_grp, char *buf)
  193. {
  194. int i, j, n;
  195. int left;
  196. char *ptr;
  197. struct usnic_vnic_res_chunk *res_chunk;
  198. struct usnic_vnic_res *vnic_res;
  199. left = PAGE_SIZE;
  200. ptr = buf;
  201. n = scnprintf(ptr, left,
  202. "QPN: %d State: (%s) PID: %u VF Idx: %hu ",
  203. qp_grp->ibqp.qp_num,
  204. usnic_ib_qp_grp_state_to_string(qp_grp->state),
  205. qp_grp->owner_pid,
  206. usnic_vnic_get_index(qp_grp->vf->vnic));
  207. UPDATE_PTR_LEFT(n, ptr, left);
  208. for (i = 0; qp_grp->res_chunk_list[i]; i++) {
  209. res_chunk = qp_grp->res_chunk_list[i];
  210. for (j = 0; j < res_chunk->cnt; j++) {
  211. vnic_res = res_chunk->res[j];
  212. n = scnprintf(ptr, left, "%s[%d] ",
  213. usnic_vnic_res_type_to_str(vnic_res->type),
  214. vnic_res->vnic_idx);
  215. UPDATE_PTR_LEFT(n, ptr, left);
  216. }
  217. }
  218. n = scnprintf(ptr, left, "\n");
  219. UPDATE_PTR_LEFT(n, ptr, left);
  220. return ptr - buf;
  221. }
  222. static QPN_ATTR_RO(context);
  223. static QPN_ATTR_RO(summary);
  224. static struct attribute *usnic_ib_qpn_default_attrs[] = {
  225. &qpn_attr_context.attr,
  226. &qpn_attr_summary.attr,
  227. NULL
  228. };
  229. static struct kobj_type usnic_ib_qpn_type = {
  230. .sysfs_ops = &usnic_ib_qpn_sysfs_ops,
  231. .default_attrs = usnic_ib_qpn_default_attrs
  232. };
  233. int usnic_ib_sysfs_register_usdev(struct usnic_ib_dev *us_ibdev)
  234. {
  235. int i;
  236. int err;
  237. for (i = 0; i < ARRAY_SIZE(usnic_class_attributes); ++i) {
  238. err = device_create_file(&us_ibdev->ib_dev.dev,
  239. usnic_class_attributes[i]);
  240. if (err) {
  241. usnic_err("Failed to create device file %d for %s eith err %d",
  242. i, us_ibdev->ib_dev.name, err);
  243. return -EINVAL;
  244. }
  245. }
  246. /* create kernel object for looking at individual QPs */
  247. kobject_get(&us_ibdev->ib_dev.dev.kobj);
  248. us_ibdev->qpn_kobj = kobject_create_and_add("qpn",
  249. &us_ibdev->ib_dev.dev.kobj);
  250. if (us_ibdev->qpn_kobj == NULL) {
  251. kobject_put(&us_ibdev->ib_dev.dev.kobj);
  252. return -ENOMEM;
  253. }
  254. return 0;
  255. }
  256. void usnic_ib_sysfs_unregister_usdev(struct usnic_ib_dev *us_ibdev)
  257. {
  258. int i;
  259. for (i = 0; i < ARRAY_SIZE(usnic_class_attributes); ++i) {
  260. device_remove_file(&us_ibdev->ib_dev.dev,
  261. usnic_class_attributes[i]);
  262. }
  263. kobject_put(us_ibdev->qpn_kobj);
  264. }
  265. void usnic_ib_sysfs_qpn_add(struct usnic_ib_qp_grp *qp_grp)
  266. {
  267. struct usnic_ib_dev *us_ibdev;
  268. int err;
  269. us_ibdev = qp_grp->vf->pf;
  270. err = kobject_init_and_add(&qp_grp->kobj, &usnic_ib_qpn_type,
  271. kobject_get(us_ibdev->qpn_kobj),
  272. "%d", qp_grp->grp_id);
  273. if (err) {
  274. kobject_put(us_ibdev->qpn_kobj);
  275. return;
  276. }
  277. }
  278. void usnic_ib_sysfs_qpn_remove(struct usnic_ib_qp_grp *qp_grp)
  279. {
  280. struct usnic_ib_dev *us_ibdev;
  281. us_ibdev = qp_grp->vf->pf;
  282. kobject_put(&qp_grp->kobj);
  283. kobject_put(us_ibdev->qpn_kobj);
  284. }