vnic_dev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * Copyright 2008 Cisco Systems, Inc. All rights reserved.
  3. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  4. *
  5. * This program is free software; you may redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  13. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  14. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. * SOFTWARE.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/pci.h>
  23. #include <linux/delay.h>
  24. #include <linux/if_ether.h>
  25. #include "vnic_resource.h"
  26. #include "vnic_devcmd.h"
  27. #include "vnic_dev.h"
  28. #include "vnic_stats.h"
  29. struct vnic_res {
  30. void __iomem *vaddr;
  31. unsigned int count;
  32. };
  33. struct vnic_dev {
  34. void *priv;
  35. struct pci_dev *pdev;
  36. struct vnic_res res[RES_TYPE_MAX];
  37. enum vnic_dev_intr_mode intr_mode;
  38. struct vnic_devcmd __iomem *devcmd;
  39. struct vnic_devcmd_notify *notify;
  40. struct vnic_devcmd_notify notify_copy;
  41. dma_addr_t notify_pa;
  42. u32 notify_sz;
  43. u32 *linkstatus;
  44. dma_addr_t linkstatus_pa;
  45. struct vnic_stats *stats;
  46. dma_addr_t stats_pa;
  47. struct vnic_devcmd_fw_info *fw_info;
  48. dma_addr_t fw_info_pa;
  49. };
  50. #define VNIC_MAX_RES_HDR_SIZE \
  51. (sizeof(struct vnic_resource_header) + \
  52. sizeof(struct vnic_resource) * RES_TYPE_MAX)
  53. #define VNIC_RES_STRIDE 128
  54. void *vnic_dev_priv(struct vnic_dev *vdev)
  55. {
  56. return vdev->priv;
  57. }
  58. static int vnic_dev_discover_res(struct vnic_dev *vdev,
  59. struct vnic_dev_bar *bar)
  60. {
  61. struct vnic_resource_header __iomem *rh;
  62. struct vnic_resource __iomem *r;
  63. u8 type;
  64. if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
  65. printk(KERN_ERR "vNIC BAR0 res hdr length error\n");
  66. return -EINVAL;
  67. }
  68. rh = bar->vaddr;
  69. if (!rh) {
  70. printk(KERN_ERR "vNIC BAR0 res hdr not mem-mapped\n");
  71. return -EINVAL;
  72. }
  73. if (ioread32(&rh->magic) != VNIC_RES_MAGIC ||
  74. ioread32(&rh->version) != VNIC_RES_VERSION) {
  75. printk(KERN_ERR "vNIC BAR0 res magic/version error "
  76. "exp (%lx/%lx) curr (%x/%x)\n",
  77. VNIC_RES_MAGIC, VNIC_RES_VERSION,
  78. ioread32(&rh->magic), ioread32(&rh->version));
  79. return -EINVAL;
  80. }
  81. r = (struct vnic_resource __iomem *)(rh + 1);
  82. while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
  83. u8 bar_num = ioread8(&r->bar);
  84. u32 bar_offset = ioread32(&r->bar_offset);
  85. u32 count = ioread32(&r->count);
  86. u32 len;
  87. r++;
  88. if (bar_num != 0) /* only mapping in BAR0 resources */
  89. continue;
  90. switch (type) {
  91. case RES_TYPE_WQ:
  92. case RES_TYPE_RQ:
  93. case RES_TYPE_CQ:
  94. case RES_TYPE_INTR_CTRL:
  95. /* each count is stride bytes long */
  96. len = count * VNIC_RES_STRIDE;
  97. if (len + bar_offset > bar->len) {
  98. printk(KERN_ERR "vNIC BAR0 resource %d "
  99. "out-of-bounds, offset 0x%x + "
  100. "size 0x%x > bar len 0x%lx\n",
  101. type, bar_offset,
  102. len,
  103. bar->len);
  104. return -EINVAL;
  105. }
  106. break;
  107. case RES_TYPE_INTR_PBA_LEGACY:
  108. case RES_TYPE_DEVCMD:
  109. len = count;
  110. break;
  111. default:
  112. continue;
  113. }
  114. vdev->res[type].count = count;
  115. vdev->res[type].vaddr = (char __iomem *)bar->vaddr + bar_offset;
  116. }
  117. return 0;
  118. }
  119. unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
  120. enum vnic_res_type type)
  121. {
  122. return vdev->res[type].count;
  123. }
  124. void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
  125. unsigned int index)
  126. {
  127. if (!vdev->res[type].vaddr)
  128. return NULL;
  129. switch (type) {
  130. case RES_TYPE_WQ:
  131. case RES_TYPE_RQ:
  132. case RES_TYPE_CQ:
  133. case RES_TYPE_INTR_CTRL:
  134. return (char __iomem *)vdev->res[type].vaddr +
  135. index * VNIC_RES_STRIDE;
  136. default:
  137. return (char __iomem *)vdev->res[type].vaddr;
  138. }
  139. }
  140. unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
  141. unsigned int desc_count, unsigned int desc_size)
  142. {
  143. /* The base address of the desc rings must be 512 byte aligned.
  144. * Descriptor count is aligned to groups of 32 descriptors. A
  145. * count of 0 means the maximum 4096 descriptors. Descriptor
  146. * size is aligned to 16 bytes.
  147. */
  148. unsigned int count_align = 32;
  149. unsigned int desc_align = 16;
  150. ring->base_align = 512;
  151. if (desc_count == 0)
  152. desc_count = 4096;
  153. ring->desc_count = ALIGN(desc_count, count_align);
  154. ring->desc_size = ALIGN(desc_size, desc_align);
  155. ring->size = ring->desc_count * ring->desc_size;
  156. ring->size_unaligned = ring->size + ring->base_align;
  157. return ring->size_unaligned;
  158. }
  159. void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
  160. {
  161. memset(ring->descs, 0, ring->size);
  162. }
  163. int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
  164. unsigned int desc_count, unsigned int desc_size)
  165. {
  166. vnic_dev_desc_ring_size(ring, desc_count, desc_size);
  167. ring->descs_unaligned = pci_alloc_consistent(vdev->pdev,
  168. ring->size_unaligned,
  169. &ring->base_addr_unaligned);
  170. if (!ring->descs_unaligned) {
  171. printk(KERN_ERR
  172. "Failed to allocate ring (size=%d), aborting\n",
  173. (int)ring->size);
  174. return -ENOMEM;
  175. }
  176. ring->base_addr = ALIGN(ring->base_addr_unaligned,
  177. ring->base_align);
  178. ring->descs = (u8 *)ring->descs_unaligned +
  179. (ring->base_addr - ring->base_addr_unaligned);
  180. vnic_dev_clear_desc_ring(ring);
  181. ring->desc_avail = ring->desc_count - 1;
  182. return 0;
  183. }
  184. void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
  185. {
  186. if (ring->descs) {
  187. pci_free_consistent(vdev->pdev,
  188. ring->size_unaligned,
  189. ring->descs_unaligned,
  190. ring->base_addr_unaligned);
  191. ring->descs = NULL;
  192. }
  193. }
  194. int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  195. u64 *a0, u64 *a1, int wait)
  196. {
  197. struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
  198. int delay;
  199. u32 status;
  200. int err;
  201. status = ioread32(&devcmd->status);
  202. if (status & STAT_BUSY) {
  203. printk(KERN_ERR "Busy devcmd %d\n", _CMD_N(cmd));
  204. return -EBUSY;
  205. }
  206. if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
  207. writeq(*a0, &devcmd->args[0]);
  208. writeq(*a1, &devcmd->args[1]);
  209. wmb();
  210. }
  211. iowrite32(cmd, &devcmd->cmd);
  212. if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
  213. return 0;
  214. for (delay = 0; delay < wait; delay++) {
  215. udelay(100);
  216. status = ioread32(&devcmd->status);
  217. if (!(status & STAT_BUSY)) {
  218. if (status & STAT_ERROR) {
  219. err = (int)readq(&devcmd->args[0]);
  220. if (err != ERR_ECMDUNKNOWN ||
  221. cmd != CMD_CAPABILITY)
  222. printk(KERN_ERR "Error %d devcmd %d\n",
  223. err, _CMD_N(cmd));
  224. return err;
  225. }
  226. if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
  227. rmb();
  228. *a0 = readq(&devcmd->args[0]);
  229. *a1 = readq(&devcmd->args[1]);
  230. }
  231. return 0;
  232. }
  233. }
  234. printk(KERN_ERR "Timedout devcmd %d\n", _CMD_N(cmd));
  235. return -ETIMEDOUT;
  236. }
  237. static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
  238. {
  239. u64 a0 = (u32)cmd, a1 = 0;
  240. int wait = 1000;
  241. int err;
  242. err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
  243. return !(err || a0);
  244. }
  245. int vnic_dev_fw_info(struct vnic_dev *vdev,
  246. struct vnic_devcmd_fw_info **fw_info)
  247. {
  248. u64 a0, a1 = 0;
  249. int wait = 1000;
  250. int err = 0;
  251. if (!vdev->fw_info) {
  252. vdev->fw_info = pci_alloc_consistent(vdev->pdev,
  253. sizeof(struct vnic_devcmd_fw_info),
  254. &vdev->fw_info_pa);
  255. if (!vdev->fw_info)
  256. return -ENOMEM;
  257. a0 = vdev->fw_info_pa;
  258. /* only get fw_info once and cache it */
  259. err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
  260. }
  261. *fw_info = vdev->fw_info;
  262. return err;
  263. }
  264. int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
  265. void *value)
  266. {
  267. u64 a0, a1;
  268. int wait = 1000;
  269. int err;
  270. a0 = offset;
  271. a1 = size;
  272. err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
  273. switch (size) {
  274. case 1: *(u8 *)value = (u8)a0; break;
  275. case 2: *(u16 *)value = (u16)a0; break;
  276. case 4: *(u32 *)value = (u32)a0; break;
  277. case 8: *(u64 *)value = a0; break;
  278. default: BUG(); break;
  279. }
  280. return err;
  281. }
  282. int vnic_dev_stats_clear(struct vnic_dev *vdev)
  283. {
  284. u64 a0 = 0, a1 = 0;
  285. int wait = 1000;
  286. return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
  287. }
  288. int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
  289. {
  290. u64 a0, a1;
  291. int wait = 1000;
  292. if (!vdev->stats) {
  293. vdev->stats = pci_alloc_consistent(vdev->pdev,
  294. sizeof(struct vnic_stats), &vdev->stats_pa);
  295. if (!vdev->stats)
  296. return -ENOMEM;
  297. }
  298. *stats = vdev->stats;
  299. a0 = vdev->stats_pa;
  300. a1 = sizeof(struct vnic_stats);
  301. return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
  302. }
  303. int vnic_dev_close(struct vnic_dev *vdev)
  304. {
  305. u64 a0 = 0, a1 = 0;
  306. int wait = 1000;
  307. return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
  308. }
  309. int vnic_dev_enable(struct vnic_dev *vdev)
  310. {
  311. u64 a0 = 0, a1 = 0;
  312. int wait = 1000;
  313. return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
  314. }
  315. int vnic_dev_disable(struct vnic_dev *vdev)
  316. {
  317. u64 a0 = 0, a1 = 0;
  318. int wait = 1000;
  319. return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
  320. }
  321. int vnic_dev_open(struct vnic_dev *vdev, int arg)
  322. {
  323. u64 a0 = (u32)arg, a1 = 0;
  324. int wait = 1000;
  325. return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
  326. }
  327. int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
  328. {
  329. u64 a0 = 0, a1 = 0;
  330. int wait = 1000;
  331. int err;
  332. *done = 0;
  333. err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
  334. if (err)
  335. return err;
  336. *done = (a0 == 0);
  337. return 0;
  338. }
  339. int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
  340. {
  341. u64 a0 = (u32)arg, a1 = 0;
  342. int wait = 1000;
  343. return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
  344. }
  345. int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
  346. {
  347. u64 a0 = 0, a1 = 0;
  348. int wait = 1000;
  349. int err;
  350. *done = 0;
  351. err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
  352. if (err)
  353. return err;
  354. *done = (a0 == 0);
  355. return 0;
  356. }
  357. int vnic_dev_hang_notify(struct vnic_dev *vdev)
  358. {
  359. u64 a0, a1;
  360. int wait = 1000;
  361. return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
  362. }
  363. int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
  364. {
  365. u64 a0, a1;
  366. int wait = 1000;
  367. int err, i;
  368. for (i = 0; i < ETH_ALEN; i++)
  369. mac_addr[i] = 0;
  370. err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
  371. if (err)
  372. return err;
  373. for (i = 0; i < ETH_ALEN; i++)
  374. mac_addr[i] = ((u8 *)&a0)[i];
  375. return 0;
  376. }
  377. void vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
  378. int broadcast, int promisc, int allmulti)
  379. {
  380. u64 a0, a1 = 0;
  381. int wait = 1000;
  382. int err;
  383. a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
  384. (multicast ? CMD_PFILTER_MULTICAST : 0) |
  385. (broadcast ? CMD_PFILTER_BROADCAST : 0) |
  386. (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
  387. (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
  388. err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
  389. if (err)
  390. printk(KERN_ERR "Can't set packet filter\n");
  391. }
  392. void vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
  393. {
  394. u64 a0 = 0, a1 = 0;
  395. int wait = 1000;
  396. int err;
  397. int i;
  398. for (i = 0; i < ETH_ALEN; i++)
  399. ((u8 *)&a0)[i] = addr[i];
  400. err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  401. if (err)
  402. printk(KERN_ERR "Can't add addr [%pM], %d\n", addr, err);
  403. }
  404. void vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
  405. {
  406. u64 a0 = 0, a1 = 0;
  407. int wait = 1000;
  408. int err;
  409. int i;
  410. for (i = 0; i < ETH_ALEN; i++)
  411. ((u8 *)&a0)[i] = addr[i];
  412. err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
  413. if (err)
  414. printk(KERN_ERR "Can't del addr [%pM], %d\n", addr, err);
  415. }
  416. int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
  417. {
  418. u64 a0, a1;
  419. int wait = 1000;
  420. int r;
  421. if (!vdev->notify) {
  422. vdev->notify = pci_alloc_consistent(vdev->pdev,
  423. sizeof(struct vnic_devcmd_notify),
  424. &vdev->notify_pa);
  425. if (!vdev->notify)
  426. return -ENOMEM;
  427. memset(vdev->notify, 0, sizeof(struct vnic_devcmd_notify));
  428. }
  429. a0 = vdev->notify_pa;
  430. a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
  431. a1 += sizeof(struct vnic_devcmd_notify);
  432. r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  433. vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
  434. return r;
  435. }
  436. void vnic_dev_notify_unset(struct vnic_dev *vdev)
  437. {
  438. u64 a0, a1;
  439. int wait = 1000;
  440. a0 = 0; /* paddr = 0 to unset notify buffer */
  441. a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
  442. a1 += sizeof(struct vnic_devcmd_notify);
  443. vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  444. vdev->notify_sz = 0;
  445. }
  446. static int vnic_dev_notify_ready(struct vnic_dev *vdev)
  447. {
  448. u32 *words;
  449. unsigned int nwords = vdev->notify_sz / 4;
  450. unsigned int i;
  451. u32 csum;
  452. if (!vdev->notify || !vdev->notify_sz)
  453. return 0;
  454. do {
  455. csum = 0;
  456. memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
  457. words = (u32 *)&vdev->notify_copy;
  458. for (i = 1; i < nwords; i++)
  459. csum += words[i];
  460. } while (csum != words[0]);
  461. return 1;
  462. }
  463. int vnic_dev_init(struct vnic_dev *vdev, int arg)
  464. {
  465. u64 a0 = (u32)arg, a1 = 0;
  466. int wait = 1000;
  467. int r = 0;
  468. if (vnic_dev_capable(vdev, CMD_INIT))
  469. r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
  470. else {
  471. vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
  472. if (a0 & CMD_INITF_DEFAULT_MAC) {
  473. // Emulate these for old CMD_INIT_v1 which
  474. // didn't pass a0 so no CMD_INITF_*.
  475. vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
  476. vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  477. }
  478. }
  479. return r;
  480. }
  481. int vnic_dev_link_status(struct vnic_dev *vdev)
  482. {
  483. if (vdev->linkstatus)
  484. return *vdev->linkstatus;
  485. if (!vnic_dev_notify_ready(vdev))
  486. return 0;
  487. return vdev->notify_copy.link_state;
  488. }
  489. u32 vnic_dev_port_speed(struct vnic_dev *vdev)
  490. {
  491. if (!vnic_dev_notify_ready(vdev))
  492. return 0;
  493. return vdev->notify_copy.port_speed;
  494. }
  495. u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
  496. {
  497. if (!vnic_dev_notify_ready(vdev))
  498. return 0;
  499. return vdev->notify_copy.msglvl;
  500. }
  501. u32 vnic_dev_mtu(struct vnic_dev *vdev)
  502. {
  503. if (!vnic_dev_notify_ready(vdev))
  504. return 0;
  505. return vdev->notify_copy.mtu;
  506. }
  507. void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
  508. enum vnic_dev_intr_mode intr_mode)
  509. {
  510. vdev->intr_mode = intr_mode;
  511. }
  512. enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
  513. struct vnic_dev *vdev)
  514. {
  515. return vdev->intr_mode;
  516. }
  517. void vnic_dev_unregister(struct vnic_dev *vdev)
  518. {
  519. if (vdev) {
  520. if (vdev->notify)
  521. pci_free_consistent(vdev->pdev,
  522. sizeof(struct vnic_devcmd_notify),
  523. vdev->notify,
  524. vdev->notify_pa);
  525. if (vdev->linkstatus)
  526. pci_free_consistent(vdev->pdev,
  527. sizeof(u32),
  528. vdev->linkstatus,
  529. vdev->linkstatus_pa);
  530. if (vdev->stats)
  531. pci_free_consistent(vdev->pdev,
  532. sizeof(struct vnic_dev),
  533. vdev->stats, vdev->stats_pa);
  534. if (vdev->fw_info)
  535. pci_free_consistent(vdev->pdev,
  536. sizeof(struct vnic_devcmd_fw_info),
  537. vdev->fw_info, vdev->fw_info_pa);
  538. kfree(vdev);
  539. }
  540. }
  541. struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
  542. void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar)
  543. {
  544. if (!vdev) {
  545. vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
  546. if (!vdev)
  547. return NULL;
  548. }
  549. vdev->priv = priv;
  550. vdev->pdev = pdev;
  551. if (vnic_dev_discover_res(vdev, bar))
  552. goto err_out;
  553. vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
  554. if (!vdev->devcmd)
  555. goto err_out;
  556. return vdev;
  557. err_out:
  558. vnic_dev_unregister(vdev);
  559. return NULL;
  560. }