usnic_ib_sysfs.c 8.9 KB

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