usnic_vnic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 < 1 || !owner)
  209. return ERR_PTR(-EINVAL);
  210. ret = kzalloc(sizeof(*ret), GFP_ATOMIC);
  211. if (!ret) {
  212. usnic_err("Failed to allocate chunk for %s - Out of memory\n",
  213. usnic_vnic_pci_name(vnic));
  214. return ERR_PTR(-ENOMEM);
  215. }
  216. ret->res = kzalloc(sizeof(*(ret->res))*cnt, GFP_ATOMIC);
  217. if (!ret->res) {
  218. usnic_err("Failed to allocate resources for %s. Out of memory\n",
  219. usnic_vnic_pci_name(vnic));
  220. kfree(ret);
  221. return ERR_PTR(-ENOMEM);
  222. }
  223. spin_lock(&vnic->res_lock);
  224. src = &vnic->chunks[type];
  225. for (i = 0; i < src->cnt && ret->cnt < cnt; i++) {
  226. res = src->res[i];
  227. if (!res->owner) {
  228. src->free_cnt--;
  229. res->owner = owner;
  230. ret->res[ret->cnt++] = res;
  231. }
  232. }
  233. spin_unlock(&vnic->res_lock);
  234. ret->type = type;
  235. ret->vnic = vnic;
  236. WARN_ON(ret->cnt != cnt);
  237. return ret;
  238. }
  239. void usnic_vnic_put_resources(struct usnic_vnic_res_chunk *chunk)
  240. {
  241. struct usnic_vnic_res *res;
  242. int i;
  243. struct usnic_vnic *vnic = chunk->vnic;
  244. spin_lock(&vnic->res_lock);
  245. while ((i = --chunk->cnt) >= 0) {
  246. res = chunk->res[i];
  247. chunk->res[i] = NULL;
  248. res->owner = NULL;
  249. vnic->chunks[res->type].free_cnt++;
  250. }
  251. spin_unlock(&vnic->res_lock);
  252. kfree(chunk->res);
  253. kfree(chunk);
  254. }
  255. u16 usnic_vnic_get_index(struct usnic_vnic *vnic)
  256. {
  257. return usnic_vnic_get_pdev(vnic)->devfn - 1;
  258. }
  259. static int usnic_vnic_alloc_res_chunk(struct usnic_vnic *vnic,
  260. enum usnic_vnic_res_type type,
  261. struct usnic_vnic_res_chunk *chunk)
  262. {
  263. int cnt, err, i;
  264. struct usnic_vnic_res *res;
  265. cnt = vnic_dev_get_res_count(vnic->vdev, _to_vnic_res_type(type));
  266. if (cnt < 1)
  267. return -EINVAL;
  268. chunk->cnt = chunk->free_cnt = cnt;
  269. chunk->res = kzalloc(sizeof(*(chunk->res))*cnt, GFP_KERNEL);
  270. if (!chunk->res)
  271. return -ENOMEM;
  272. for (i = 0; i < cnt; i++) {
  273. res = kzalloc(sizeof(*res), GFP_KERNEL);
  274. if (!res) {
  275. err = -ENOMEM;
  276. goto fail;
  277. }
  278. res->type = type;
  279. res->vnic_idx = i;
  280. res->vnic = vnic;
  281. res->ctrl = vnic_dev_get_res(vnic->vdev,
  282. _to_vnic_res_type(type), i);
  283. chunk->res[i] = res;
  284. }
  285. chunk->vnic = vnic;
  286. return 0;
  287. fail:
  288. for (i--; i >= 0; i--)
  289. kfree(chunk->res[i]);
  290. kfree(chunk->res);
  291. return err;
  292. }
  293. static void usnic_vnic_free_res_chunk(struct usnic_vnic_res_chunk *chunk)
  294. {
  295. int i;
  296. for (i = 0; i < chunk->cnt; i++)
  297. kfree(chunk->res[i]);
  298. kfree(chunk->res);
  299. }
  300. static int usnic_vnic_discover_resources(struct pci_dev *pdev,
  301. struct usnic_vnic *vnic)
  302. {
  303. enum usnic_vnic_res_type res_type;
  304. int i;
  305. int err = 0;
  306. for (i = 0; i < ARRAY_SIZE(vnic->bar); i++) {
  307. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  308. continue;
  309. vnic->bar[i].len = pci_resource_len(pdev, i);
  310. vnic->bar[i].vaddr = pci_iomap(pdev, i, vnic->bar[i].len);
  311. if (!vnic->bar[i].vaddr) {
  312. usnic_err("Cannot memory-map BAR %d, aborting\n",
  313. i);
  314. err = -ENODEV;
  315. goto out_clean_bar;
  316. }
  317. vnic->bar[i].bus_addr = pci_resource_start(pdev, i);
  318. }
  319. vnic->vdev = vnic_dev_register(NULL, pdev, pdev, vnic->bar,
  320. ARRAY_SIZE(vnic->bar));
  321. if (!vnic->vdev) {
  322. usnic_err("Failed to register device %s\n",
  323. pci_name(pdev));
  324. err = -EINVAL;
  325. goto out_clean_bar;
  326. }
  327. for (res_type = USNIC_VNIC_RES_TYPE_EOL + 1;
  328. res_type < USNIC_VNIC_RES_TYPE_MAX; res_type++) {
  329. err = usnic_vnic_alloc_res_chunk(vnic, res_type,
  330. &vnic->chunks[res_type]);
  331. if (err) {
  332. usnic_err("Failed to alloc res %s with err %d\n",
  333. usnic_vnic_res_type_to_str(res_type),
  334. err);
  335. goto out_clean_chunks;
  336. }
  337. }
  338. return 0;
  339. out_clean_chunks:
  340. for (res_type--; res_type > USNIC_VNIC_RES_TYPE_EOL; res_type--)
  341. usnic_vnic_free_res_chunk(&vnic->chunks[res_type]);
  342. vnic_dev_unregister(vnic->vdev);
  343. out_clean_bar:
  344. for (i = 0; i < ARRAY_SIZE(vnic->bar); i++) {
  345. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  346. continue;
  347. if (!vnic->bar[i].vaddr)
  348. break;
  349. iounmap(vnic->bar[i].vaddr);
  350. }
  351. return err;
  352. }
  353. struct pci_dev *usnic_vnic_get_pdev(struct usnic_vnic *vnic)
  354. {
  355. return vnic_dev_get_pdev(vnic->vdev);
  356. }
  357. struct vnic_dev_bar *usnic_vnic_get_bar(struct usnic_vnic *vnic,
  358. int bar_num)
  359. {
  360. return (bar_num < ARRAY_SIZE(vnic->bar)) ? &vnic->bar[bar_num] : NULL;
  361. }
  362. static void usnic_vnic_release_resources(struct usnic_vnic *vnic)
  363. {
  364. int i;
  365. struct pci_dev *pdev;
  366. enum usnic_vnic_res_type res_type;
  367. pdev = usnic_vnic_get_pdev(vnic);
  368. for (res_type = USNIC_VNIC_RES_TYPE_EOL + 1;
  369. res_type < USNIC_VNIC_RES_TYPE_MAX; res_type++)
  370. usnic_vnic_free_res_chunk(&vnic->chunks[res_type]);
  371. vnic_dev_unregister(vnic->vdev);
  372. for (i = 0; i < ARRAY_SIZE(vnic->bar); i++) {
  373. if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
  374. continue;
  375. iounmap(vnic->bar[i].vaddr);
  376. }
  377. }
  378. struct usnic_vnic *usnic_vnic_alloc(struct pci_dev *pdev)
  379. {
  380. struct usnic_vnic *vnic;
  381. int err = 0;
  382. if (!pci_is_enabled(pdev)) {
  383. usnic_err("PCI dev %s is disabled\n", pci_name(pdev));
  384. return ERR_PTR(-EINVAL);
  385. }
  386. vnic = kzalloc(sizeof(*vnic), GFP_KERNEL);
  387. if (!vnic) {
  388. usnic_err("Failed to alloc vnic for %s - out of memory\n",
  389. pci_name(pdev));
  390. return ERR_PTR(-ENOMEM);
  391. }
  392. spin_lock_init(&vnic->res_lock);
  393. err = usnic_vnic_discover_resources(pdev, vnic);
  394. if (err) {
  395. usnic_err("Failed to discover %s resources with err %d\n",
  396. pci_name(pdev), err);
  397. goto out_free_vnic;
  398. }
  399. usnic_dbg("Allocated vnic for %s\n", usnic_vnic_pci_name(vnic));
  400. return vnic;
  401. out_free_vnic:
  402. kfree(vnic);
  403. return ERR_PTR(err);
  404. }
  405. void usnic_vnic_free(struct usnic_vnic *vnic)
  406. {
  407. usnic_vnic_release_resources(vnic);
  408. kfree(vnic);
  409. }