vnic_dev.c 15 KB

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