usnic_vnic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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/errno.h>
  34. #include <linux/module.h>
  35. #include <linux/pci.h>
  36. #include "usnic_ib.h"
  37. #include "vnic_resource.h"
  38. #include "usnic_log.h"
  39. #include "usnic_vnic.h"
  40. struct usnic_vnic {
  41. struct vnic_dev *vdev;
  42. struct vnic_dev_bar bar[PCI_NUM_RESOURCES];
  43. struct usnic_vnic_res_chunk chunks[USNIC_VNIC_RES_TYPE_MAX];
  44. spinlock_t res_lock;
  45. };
  46. static enum vnic_res_type _to_vnic_res_type(enum usnic_vnic_res_type res_type)
  47. {
  48. #define DEFINE_USNIC_VNIC_RES_AT(usnic_vnic_res_t, vnic_res_type, desc, val) \
  49. vnic_res_type,
  50. #define DEFINE_USNIC_VNIC_RES(usnic_vnic_res_t, vnic_res_type, desc) \
  51. vnic_res_type,
  52. static enum vnic_res_type usnic_vnic_type_2_vnic_type[] = {
  53. USNIC_VNIC_RES_TYPES};
  54. #undef DEFINE_USNIC_VNIC_RES
  55. #undef DEFINE_USNIC_VNIC_RES_AT
  56. if (res_type >= USNIC_VNIC_RES_TYPE_MAX)
  57. return RES_TYPE_MAX;
  58. return usnic_vnic_type_2_vnic_type[res_type];
  59. }
  60. const char *usnic_vnic_res_type_to_str(enum usnic_vnic_res_type res_type)
  61. {
  62. #define DEFINE_USNIC_VNIC_RES_AT(usnic_vnic_res_t, vnic_res_type, desc, val) \
  63. desc,
  64. #define DEFINE_USNIC_VNIC_RES(usnic_vnic_res_t, vnic_res_type, desc) \
  65. desc,
  66. static const char * const usnic_vnic_res_type_desc[] = {
  67. USNIC_VNIC_RES_TYPES};
  68. #undef DEFINE_USNIC_VNIC_RES
  69. #undef DEFINE_USNIC_VNIC_RES_AT
  70. if (res_type >= USNIC_VNIC_RES_TYPE_MAX)
  71. return "unknown";
  72. return usnic_vnic_res_type_desc[res_type];
  73. }
  74. const char *usnic_vnic_pci_name(struct usnic_vnic *vnic)
  75. {
  76. return pci_name(usnic_vnic_get_pdev(vnic));
  77. }
  78. int usnic_vnic_dump(struct usnic_vnic *vnic, char *buf,
  79. int buf_sz,
  80. void *hdr_obj,
  81. int (*printtitle)(void *, char*, int),
  82. int (*printcols)(char *, int),
  83. int (*printrow)(void *, char *, int))
  84. {
  85. struct usnic_vnic_res_chunk *chunk;
  86. struct usnic_vnic_res *res;
  87. struct vnic_dev_bar *bar0;
  88. int i, j, offset;
  89. offset = 0;
  90. bar0 = usnic_vnic_get_bar(vnic, 0);
  91. offset += scnprintf(buf + offset, buf_sz - offset,
  92. "VF:%hu BAR0 bus_addr=%pa vaddr=0x%p size=%ld ",
  93. usnic_vnic_get_index(vnic),
  94. &bar0->bus_addr,
  95. bar0->vaddr, bar0->len);
  96. if (printtitle)
  97. offset += printtitle(hdr_obj, buf + offset, buf_sz - offset);
  98. offset += scnprintf(buf + offset, buf_sz - offset, "\n");
  99. offset += scnprintf(buf + offset, buf_sz - offset,
  100. "|RES\t|CTRL_PIN\t\t|IN_USE\t");
  101. if (printcols)
  102. offset += printcols(buf + offset, buf_sz - offset);
  103. offset += scnprintf(buf + offset, buf_sz - offset, "\n");
  104. spin_lock(&vnic->res_lock);
  105. for (i = 0; i < ARRAY_SIZE(vnic->chunks); i++) {
  106. chunk = &vnic->chunks[i];
  107. for (j = 0; j < chunk->cnt; j++) {
  108. res = chunk->res[j];
  109. offset += scnprintf(buf + offset, buf_sz - offset,
  110. "|%s[%u]\t|0x%p\t|%u\t",
  111. usnic_vnic_res_type_to_str(res->type),
  112. res->vnic_idx, res->ctrl, !!res->owner);
  113. if (printrow) {
  114. offset += printrow(res->owner, buf + offset,
  115. buf_sz - offset);
  116. }
  117. offset += scnprintf(buf + offset, buf_sz - offset,
  118. "\n");
  119. }
  120. }
  121. spin_unlock(&vnic->res_lock);
  122. return offset;
  123. }
  124. void usnic_vnic_res_spec_update(struct usnic_vnic_res_spec *spec,
  125. enum usnic_vnic_res_type trgt_type,
  126. u16 cnt)
  127. {
  128. int i;
  129. for (i = 0; i < USNIC_VNIC_RES_TYPE_MAX; i++) {
  130. if (spec->resources[i].type == trgt_type) {
  131. spec->resources[i].cnt = cnt;
  132. return;
  133. }
  134. }
  135. WARN_ON(1);
  136. }
  137. int usnic_vnic_res_spec_satisfied(const struct usnic_vnic_res_spec *min_spec,
  138. struct usnic_vnic_res_spec *res_spec)
  139. {
  140. int found, i, j;
  141. for (i = 0; i < USNIC_VNIC_RES_TYPE_MAX; i++) {
  142. found = 0;
  143. for (j = 0; j < USNIC_VNIC_RES_TYPE_MAX; j++) {
  144. if (res_spec->resources[i].type !=
  145. min_spec->resources[i].type)
  146. continue;
  147. found = 1;
  148. if (min_spec->resources[i].cnt >
  149. res_spec->resources[i].cnt)
  150. return -EINVAL;
  151. break;
  152. }
  153. if (!found)
  154. return -EINVAL;
  155. }
  156. return 0;
  157. }
  158. int usnic_vnic_spec_dump(char *buf, int buf_sz,
  159. struct usnic_vnic_res_spec *res_spec)
  160. {
  161. enum usnic_vnic_res_type res_type;
  162. int res_cnt;
  163. int i;
  164. int offset = 0;
  165. for (i = 0; i < USNIC_VNIC_RES_TYPE_MAX; i++) {
  166. res_type = res_spec->resources[i].type;
  167. res_cnt = res_spec->resources[i].cnt;
  168. offset += scnprintf(buf + offset, buf_sz - offset,
  169. "Res: %s Cnt: %d ",
  170. usnic_vnic_res_type_to_str(res_type),
  171. res_cnt);
  172. }
  173. return offset;
  174. }
  175. int usnic_vnic_check_room(struct usnic_vnic *vnic,
  176. struct usnic_vnic_res_spec *res_spec)
  177. {
  178. int i;
  179. enum usnic_vnic_res_type res_type;
  180. int res_cnt;
  181. for (i = 0; i < USNIC_VNIC_RES_TYPE_MAX; i++) {
  182. res_type = res_spec->resources[i].type;
  183. res_cnt = res_spec->resources[i].cnt;
  184. if (res_type == USNIC_VNIC_RES_TYPE_EOL)
  185. break;
  186. if (res_cnt > usnic_vnic_res_free_cnt(vnic, res_type))
  187. return -EBUSY;
  188. }
  189. return 0;
  190. }
  191. int usnic_vnic_res_cnt(struct usnic_vnic *vnic,
  192. enum usnic_vnic_res_type type)
  193. {
  194. return vnic->chunks[type].cnt;
  195. }
  196. int usnic_vnic_res_free_cnt(struct usnic_vnic *vnic,
  197. enum usnic_vnic_res_type type)
  198. {
  199. return vnic->chunks[type].free_cnt;
  200. }
  201. struct usnic_vnic_res_chunk *
  202. usnic_vnic_get_resources(struct usnic_vnic *vnic, enum usnic_vnic_res_type type,
  203. int cnt, void *owner)
  204. {
  205. struct usnic_vnic_res_chunk *src, *ret;
  206. struct usnic_vnic_res *res;
  207. int i;
  208. if (usnic_vnic_res_free_cnt(vnic, type) < cnt || cnt < 0 || !owner)
  209. return ERR_PTR(-EINVAL);
  210. ret = kzalloc(sizeof(*ret), GFP_ATOMIC);
  211. if (!ret)
  212. return ERR_PTR(-ENOMEM);
  213. if (cnt > 0) {
  214. ret->res = kcalloc(cnt, sizeof(*(ret->res)), GFP_ATOMIC);
  215. if (!ret->res) {
  216. kfree(ret);
  217. return ERR_PTR(-ENOMEM);
  218. }
  219. spin_lock(&vnic->res_lock);
  220. src = &vnic->chunks[type];
  221. for (i = 0; i < src->cnt && ret->cnt < cnt; i++) {
  222. res = src->res[i];
  223. if (!res->owner) {
  224. src->free_cnt--;
  225. res->owner = owner;
  226. ret->res[ret->cnt++] = res;
  227. }
  228. }
  229. spin_unlock(&vnic->res_lock);
  230. }
  231. ret->type = type;
  232. ret->vnic = vnic;
  233. WARN_ON(ret->cnt != cnt);
  234. return ret;
  235. }
  236. void usnic_vnic_put_resources(struct usnic_vnic_res_chunk *chunk)
  237. {
  238. struct usnic_vnic_res *res;
  239. int i;
  240. struct usnic_vnic *vnic = chunk->vnic;
  241. if (chunk->cnt > 0) {
  242. spin_lock(&vnic->res_lock);
  243. while ((i = --chunk->cnt) >= 0) {
  244. res = chunk->res[i];
  245. chunk->res[i] = NULL;
  246. res->owner = NULL;
  247. vnic->chunks[res->type].free_cnt++;
  248. }
  249. spin_unlock(&vnic->res_lock);
  250. }
  251. kfree(chunk->res);
  252. kfree(chunk);
  253. }
  254. u16 usnic_vnic_get_index(struct usnic_vnic *vnic)
  255. {
  256. return usnic_vnic_get_pdev(vnic)->devfn - 1;
  257. }
  258. static int usnic_vnic_alloc_res_chunk(struct usnic_vnic *vnic,
  259. enum usnic_vnic_res_type type,
  260. struct usnic_vnic_res_chunk *chunk)
  261. {
  262. int cnt, err, i;
  263. struct usnic_vnic_res *res;
  264. cnt = vnic_dev_get_res_count(vnic->vdev, _to_vnic_res_type(type));
  265. if (cnt < 1) {
  266. usnic_err("Wrong res count with cnt %d\n", cnt);
  267. return -EINVAL;
  268. }
  269. chunk->cnt = chunk->free_cnt = cnt;
  270. chunk->res = kcalloc(cnt, sizeof(*(chunk->res)), GFP_KERNEL);
  271. if (!chunk->res)
  272. return -ENOMEM;
  273. for (i = 0; i < cnt; i++) {
  274. res = kzalloc(sizeof(*res), GFP_KERNEL);
  275. if (!res) {
  276. err = -ENOMEM;
  277. goto fail;
  278. }
  279. res->type = type;
  280. res->vnic_idx = i;
  281. res->vnic = vnic;
  282. res->ctrl = vnic_dev_get_res(vnic->vdev,
  283. _to_vnic_res_type(type), i);
  284. chunk->res[i] = res;
  285. }
  286. chunk->vnic = vnic;
  287. return 0;
  288. fail:
  289. for (i--; i >= 0; i--)
  290. kfree(chunk->res[i]);
  291. kfree(chunk->res);
  292. return err;
  293. }
  294. static void usnic_vnic_free_res_chunk(struct usnic_vnic_res_chunk *chunk)
  295. {
  296. int i;
  297. for (i = 0; i < chunk->cnt; i++)
  298. kfree(chunk->res[i]);
  299. kfree(chunk->res);
  300. }
  301. static int usnic_vnic_discover_resources(struct pci_dev *pdev,
  302. struct usnic_vnic *vnic)
  303. {
  304. enum usnic_vnic_res_type res_type;
  305. int i;
  306. int err = 0;
  307. for (i = 0; i < ARRAY_SIZE(vnic->bar); i++) {
  308. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  309. continue;
  310. vnic->bar[i].len = pci_resource_len(pdev, i);
  311. vnic->bar[i].vaddr = pci_iomap(pdev, i, vnic->bar[i].len);
  312. if (!vnic->bar[i].vaddr) {
  313. usnic_err("Cannot memory-map BAR %d, aborting\n",
  314. i);
  315. err = -ENODEV;
  316. goto out_clean_bar;
  317. }
  318. vnic->bar[i].bus_addr = pci_resource_start(pdev, i);
  319. }
  320. vnic->vdev = vnic_dev_register(NULL, pdev, pdev, vnic->bar,
  321. ARRAY_SIZE(vnic->bar));
  322. if (!vnic->vdev) {
  323. usnic_err("Failed to register device %s\n",
  324. pci_name(pdev));
  325. err = -EINVAL;
  326. goto out_clean_bar;
  327. }
  328. for (res_type = USNIC_VNIC_RES_TYPE_EOL + 1;
  329. res_type < USNIC_VNIC_RES_TYPE_MAX; res_type++) {
  330. err = usnic_vnic_alloc_res_chunk(vnic, res_type,
  331. &vnic->chunks[res_type]);
  332. if (err)
  333. goto out_clean_chunks;
  334. }
  335. return 0;
  336. out_clean_chunks:
  337. for (res_type--; res_type > USNIC_VNIC_RES_TYPE_EOL; res_type--)
  338. usnic_vnic_free_res_chunk(&vnic->chunks[res_type]);
  339. vnic_dev_unregister(vnic->vdev);
  340. out_clean_bar:
  341. for (i = 0; i < ARRAY_SIZE(vnic->bar); i++) {
  342. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  343. continue;
  344. if (!vnic->bar[i].vaddr)
  345. break;
  346. iounmap(vnic->bar[i].vaddr);
  347. }
  348. return err;
  349. }
  350. struct pci_dev *usnic_vnic_get_pdev(struct usnic_vnic *vnic)
  351. {
  352. return vnic_dev_get_pdev(vnic->vdev);
  353. }
  354. struct vnic_dev_bar *usnic_vnic_get_bar(struct usnic_vnic *vnic,
  355. int bar_num)
  356. {
  357. return (bar_num < ARRAY_SIZE(vnic->bar)) ? &vnic->bar[bar_num] : NULL;
  358. }
  359. static void usnic_vnic_release_resources(struct usnic_vnic *vnic)
  360. {
  361. int i;
  362. struct pci_dev *pdev;
  363. enum usnic_vnic_res_type res_type;
  364. pdev = usnic_vnic_get_pdev(vnic);
  365. for (res_type = USNIC_VNIC_RES_TYPE_EOL + 1;
  366. res_type < USNIC_VNIC_RES_TYPE_MAX; res_type++)
  367. usnic_vnic_free_res_chunk(&vnic->chunks[res_type]);
  368. vnic_dev_unregister(vnic->vdev);
  369. for (i = 0; i < ARRAY_SIZE(vnic->bar); i++) {
  370. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  371. continue;
  372. iounmap(vnic->bar[i].vaddr);
  373. }
  374. }
  375. struct usnic_vnic *usnic_vnic_alloc(struct pci_dev *pdev)
  376. {
  377. struct usnic_vnic *vnic;
  378. int err = 0;
  379. if (!pci_is_enabled(pdev)) {
  380. usnic_err("PCI dev %s is disabled\n", pci_name(pdev));
  381. return ERR_PTR(-EINVAL);
  382. }
  383. vnic = kzalloc(sizeof(*vnic), GFP_KERNEL);
  384. if (!vnic)
  385. return ERR_PTR(-ENOMEM);
  386. spin_lock_init(&vnic->res_lock);
  387. err = usnic_vnic_discover_resources(pdev, vnic);
  388. if (err) {
  389. usnic_err("Failed to discover %s resources with err %d\n",
  390. pci_name(pdev), err);
  391. goto out_free_vnic;
  392. }
  393. usnic_dbg("Allocated vnic for %s\n", usnic_vnic_pci_name(vnic));
  394. return vnic;
  395. out_free_vnic:
  396. kfree(vnic);
  397. return ERR_PTR(err);
  398. }
  399. void usnic_vnic_free(struct usnic_vnic *vnic)
  400. {
  401. usnic_vnic_release_resources(vnic);
  402. kfree(vnic);
  403. }