vnic_dev.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright 2014 Cisco Systems, Inc. All rights reserved.
  3. #include <linux/kernel.h>
  4. #include <linux/errno.h>
  5. #include <linux/types.h>
  6. #include <linux/pci.h>
  7. #include <linux/delay.h>
  8. #include <linux/if_ether.h>
  9. #include <linux/slab.h>
  10. #include "vnic_resource.h"
  11. #include "vnic_devcmd.h"
  12. #include "vnic_dev.h"
  13. #include "vnic_stats.h"
  14. #include "vnic_wq.h"
  15. #define VNIC_DVCMD_TMO 10000 /* Devcmd Timeout value */
  16. #define VNIC_NOTIFY_INTR_MASK 0x0000ffff00000000ULL
  17. struct devcmd2_controller {
  18. struct vnic_wq_ctrl __iomem *wq_ctrl;
  19. struct vnic_dev_ring results_ring;
  20. struct vnic_wq wq;
  21. struct vnic_devcmd2 *cmd_ring;
  22. struct devcmd2_result *result;
  23. u16 next_result;
  24. u16 result_size;
  25. int color;
  26. };
  27. struct vnic_res {
  28. void __iomem *vaddr;
  29. unsigned int count;
  30. };
  31. struct vnic_dev {
  32. void *priv;
  33. struct pci_dev *pdev;
  34. struct vnic_res res[RES_TYPE_MAX];
  35. enum vnic_dev_intr_mode intr_mode;
  36. struct vnic_devcmd __iomem *devcmd;
  37. struct vnic_devcmd_notify *notify;
  38. struct vnic_devcmd_notify notify_copy;
  39. dma_addr_t notify_pa;
  40. struct vnic_stats *stats;
  41. dma_addr_t stats_pa;
  42. struct vnic_devcmd_fw_info *fw_info;
  43. dma_addr_t fw_info_pa;
  44. u64 args[VNIC_DEVCMD_NARGS];
  45. struct devcmd2_controller *devcmd2;
  46. int (*devcmd_rtn)(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  47. int wait);
  48. };
  49. #define VNIC_MAX_RES_HDR_SIZE \
  50. (sizeof(struct vnic_resource_header) + \
  51. sizeof(struct vnic_resource) * RES_TYPE_MAX)
  52. #define VNIC_RES_STRIDE 128
  53. void *svnic_dev_priv(struct vnic_dev *vdev)
  54. {
  55. return vdev->priv;
  56. }
  57. static int vnic_dev_discover_res(struct vnic_dev *vdev,
  58. struct vnic_dev_bar *bar, unsigned int num_bars)
  59. {
  60. struct vnic_resource_header __iomem *rh;
  61. struct vnic_resource __iomem *r;
  62. u8 type;
  63. if (num_bars == 0)
  64. return -EINVAL;
  65. if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
  66. pr_err("vNIC BAR0 res hdr length error\n");
  67. return -EINVAL;
  68. }
  69. rh = bar->vaddr;
  70. if (!rh) {
  71. pr_err("vNIC BAR0 res hdr not mem-mapped\n");
  72. return -EINVAL;
  73. }
  74. if (ioread32(&rh->magic) != VNIC_RES_MAGIC ||
  75. ioread32(&rh->version) != VNIC_RES_VERSION) {
  76. pr_err("vNIC BAR0 res magic/version error 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 >= num_bars)
  89. continue;
  90. if (!bar[bar_num].len || !bar[bar_num].vaddr)
  91. continue;
  92. switch (type) {
  93. case RES_TYPE_WQ:
  94. case RES_TYPE_RQ:
  95. case RES_TYPE_CQ:
  96. case RES_TYPE_INTR_CTRL:
  97. /* each count is stride bytes long */
  98. len = count * VNIC_RES_STRIDE;
  99. if (len + bar_offset > bar->len) {
  100. pr_err("vNIC BAR0 resource %d out-of-bounds, offset 0x%x + 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. case RES_TYPE_DEVCMD2:
  110. len = count;
  111. break;
  112. default:
  113. continue;
  114. }
  115. vdev->res[type].count = count;
  116. vdev->res[type].vaddr = (char __iomem *)bar->vaddr + bar_offset;
  117. }
  118. return 0;
  119. }
  120. unsigned int svnic_dev_get_res_count(struct vnic_dev *vdev,
  121. enum vnic_res_type type)
  122. {
  123. return vdev->res[type].count;
  124. }
  125. void __iomem *svnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
  126. unsigned int index)
  127. {
  128. if (!vdev->res[type].vaddr)
  129. return NULL;
  130. switch (type) {
  131. case RES_TYPE_WQ:
  132. case RES_TYPE_RQ:
  133. case RES_TYPE_CQ:
  134. case RES_TYPE_INTR_CTRL:
  135. return (char __iomem *)vdev->res[type].vaddr +
  136. index * VNIC_RES_STRIDE;
  137. default:
  138. return (char __iomem *)vdev->res[type].vaddr;
  139. }
  140. }
  141. unsigned int svnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
  142. unsigned int desc_count,
  143. unsigned int desc_size)
  144. {
  145. /* The base address of the desc rings must be 512 byte aligned.
  146. * Descriptor count is aligned to groups of 32 descriptors. A
  147. * count of 0 means the maximum 4096 descriptors. Descriptor
  148. * size is aligned to 16 bytes.
  149. */
  150. unsigned int count_align = 32;
  151. unsigned int desc_align = 16;
  152. ring->base_align = 512;
  153. if (desc_count == 0)
  154. desc_count = 4096;
  155. ring->desc_count = ALIGN(desc_count, count_align);
  156. ring->desc_size = ALIGN(desc_size, desc_align);
  157. ring->size = ring->desc_count * ring->desc_size;
  158. ring->size_unaligned = ring->size + ring->base_align;
  159. return ring->size_unaligned;
  160. }
  161. void svnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
  162. {
  163. memset(ring->descs, 0, ring->size);
  164. }
  165. int svnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
  166. unsigned int desc_count, unsigned int desc_size)
  167. {
  168. svnic_dev_desc_ring_size(ring, desc_count, desc_size);
  169. ring->descs_unaligned = dma_alloc_coherent(&vdev->pdev->dev,
  170. ring->size_unaligned, &ring->base_addr_unaligned,
  171. GFP_KERNEL);
  172. if (!ring->descs_unaligned) {
  173. pr_err("Failed to allocate ring (size=%d), aborting\n",
  174. (int)ring->size);
  175. return -ENOMEM;
  176. }
  177. ring->base_addr = ALIGN(ring->base_addr_unaligned,
  178. ring->base_align);
  179. ring->descs = (u8 *)ring->descs_unaligned +
  180. (ring->base_addr - ring->base_addr_unaligned);
  181. svnic_dev_clear_desc_ring(ring);
  182. ring->desc_avail = ring->desc_count - 1;
  183. return 0;
  184. }
  185. void svnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
  186. {
  187. if (ring->descs) {
  188. dma_free_coherent(&vdev->pdev->dev,
  189. ring->size_unaligned,
  190. ring->descs_unaligned,
  191. ring->base_addr_unaligned);
  192. ring->descs = NULL;
  193. }
  194. }
  195. static int _svnic_dev_cmd2(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  196. int wait)
  197. {
  198. struct devcmd2_controller *dc2c = vdev->devcmd2;
  199. struct devcmd2_result *result = NULL;
  200. unsigned int i;
  201. int delay;
  202. int err;
  203. u32 posted;
  204. u32 fetch_idx;
  205. u32 new_posted;
  206. u8 color;
  207. fetch_idx = ioread32(&dc2c->wq_ctrl->fetch_index);
  208. if (fetch_idx == 0xFFFFFFFF) { /* check for hardware gone */
  209. /* Hardware surprise removal: return error */
  210. return -ENODEV;
  211. }
  212. posted = ioread32(&dc2c->wq_ctrl->posted_index);
  213. if (posted == 0xFFFFFFFF) { /* check for hardware gone */
  214. /* Hardware surprise removal: return error */
  215. return -ENODEV;
  216. }
  217. new_posted = (posted + 1) % DEVCMD2_RING_SIZE;
  218. if (new_posted == fetch_idx) {
  219. pr_err("%s: wq is full while issuing devcmd2 command %d, fetch index: %u, posted index: %u\n",
  220. pci_name(vdev->pdev), _CMD_N(cmd), fetch_idx, posted);
  221. return -EBUSY;
  222. }
  223. dc2c->cmd_ring[posted].cmd = cmd;
  224. dc2c->cmd_ring[posted].flags = 0;
  225. if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
  226. dc2c->cmd_ring[posted].flags |= DEVCMD2_FNORESULT;
  227. if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
  228. for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
  229. dc2c->cmd_ring[posted].args[i] = vdev->args[i];
  230. }
  231. /* Adding write memory barrier prevents compiler and/or CPU
  232. * reordering, thus avoiding descriptor posting before
  233. * descriptor is initialized. Otherwise, hardware can read
  234. * stale descriptor fields.
  235. */
  236. wmb();
  237. iowrite32(new_posted, &dc2c->wq_ctrl->posted_index);
  238. if (dc2c->cmd_ring[posted].flags & DEVCMD2_FNORESULT)
  239. return 0;
  240. result = dc2c->result + dc2c->next_result;
  241. color = dc2c->color;
  242. /*
  243. * Increment next_result, after posting the devcmd, irrespective of
  244. * devcmd result, and it should be done only once.
  245. */
  246. dc2c->next_result++;
  247. if (dc2c->next_result == dc2c->result_size) {
  248. dc2c->next_result = 0;
  249. dc2c->color = dc2c->color ? 0 : 1;
  250. }
  251. for (delay = 0; delay < wait; delay++) {
  252. udelay(100);
  253. if (result->color == color) {
  254. if (result->error) {
  255. err = (int) result->error;
  256. if (err != ERR_ECMDUNKNOWN ||
  257. cmd != CMD_CAPABILITY)
  258. pr_err("Error %d devcmd %d\n",
  259. err, _CMD_N(cmd));
  260. return err;
  261. }
  262. if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
  263. for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
  264. vdev->args[i] = result->results[i];
  265. }
  266. return 0;
  267. }
  268. }
  269. pr_err("Timed out devcmd %d\n", _CMD_N(cmd));
  270. return -ETIMEDOUT;
  271. }
  272. static int svnic_dev_init_devcmd2(struct vnic_dev *vdev)
  273. {
  274. struct devcmd2_controller *dc2c = NULL;
  275. unsigned int fetch_idx;
  276. int ret;
  277. void __iomem *p;
  278. if (vdev->devcmd2)
  279. return 0;
  280. p = svnic_dev_get_res(vdev, RES_TYPE_DEVCMD2, 0);
  281. if (!p)
  282. return -ENODEV;
  283. dc2c = kzalloc_obj(*dc2c, GFP_ATOMIC);
  284. if (!dc2c)
  285. return -ENOMEM;
  286. vdev->devcmd2 = dc2c;
  287. dc2c->color = 1;
  288. dc2c->result_size = DEVCMD2_RING_SIZE;
  289. ret = vnic_wq_devcmd2_alloc(vdev,
  290. &dc2c->wq,
  291. DEVCMD2_RING_SIZE,
  292. DEVCMD2_DESC_SIZE);
  293. if (ret)
  294. goto err_free_devcmd2;
  295. fetch_idx = ioread32(&dc2c->wq.ctrl->fetch_index);
  296. if (fetch_idx == 0xFFFFFFFF) { /* check for hardware gone */
  297. /* Hardware surprise removal: reset fetch_index */
  298. fetch_idx = 0;
  299. }
  300. /*
  301. * Don't change fetch_index ever and
  302. * set posted_index same as fetch_index
  303. * when setting up the WQ for devcmd2.
  304. */
  305. vnic_wq_init_start(&dc2c->wq, 0, fetch_idx, fetch_idx, 0, 0);
  306. svnic_wq_enable(&dc2c->wq);
  307. ret = svnic_dev_alloc_desc_ring(vdev,
  308. &dc2c->results_ring,
  309. DEVCMD2_RING_SIZE,
  310. DEVCMD2_DESC_SIZE);
  311. if (ret)
  312. goto err_free_wq;
  313. dc2c->result = (struct devcmd2_result *) dc2c->results_ring.descs;
  314. dc2c->cmd_ring = (struct vnic_devcmd2 *) dc2c->wq.ring.descs;
  315. dc2c->wq_ctrl = dc2c->wq.ctrl;
  316. vdev->args[0] = (u64) dc2c->results_ring.base_addr | VNIC_PADDR_TARGET;
  317. vdev->args[1] = DEVCMD2_RING_SIZE;
  318. ret = _svnic_dev_cmd2(vdev, CMD_INITIALIZE_DEVCMD2, VNIC_DVCMD_TMO);
  319. if (ret < 0)
  320. goto err_free_desc_ring;
  321. vdev->devcmd_rtn = &_svnic_dev_cmd2;
  322. pr_info("DEVCMD2 Initialized.\n");
  323. return ret;
  324. err_free_desc_ring:
  325. svnic_dev_free_desc_ring(vdev, &dc2c->results_ring);
  326. err_free_wq:
  327. svnic_wq_disable(&dc2c->wq);
  328. svnic_wq_free(&dc2c->wq);
  329. err_free_devcmd2:
  330. kfree(dc2c);
  331. vdev->devcmd2 = NULL;
  332. return ret;
  333. } /* end of svnic_dev_init_devcmd2 */
  334. static void vnic_dev_deinit_devcmd2(struct vnic_dev *vdev)
  335. {
  336. struct devcmd2_controller *dc2c = vdev->devcmd2;
  337. vdev->devcmd2 = NULL;
  338. vdev->devcmd_rtn = NULL;
  339. svnic_dev_free_desc_ring(vdev, &dc2c->results_ring);
  340. svnic_wq_disable(&dc2c->wq);
  341. svnic_wq_free(&dc2c->wq);
  342. kfree(dc2c);
  343. }
  344. int svnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  345. u64 *a0, u64 *a1, int wait)
  346. {
  347. int err;
  348. memset(vdev->args, 0, sizeof(vdev->args));
  349. vdev->args[0] = *a0;
  350. vdev->args[1] = *a1;
  351. err = (*vdev->devcmd_rtn)(vdev, cmd, wait);
  352. *a0 = vdev->args[0];
  353. *a1 = vdev->args[1];
  354. return err;
  355. }
  356. int svnic_dev_fw_info(struct vnic_dev *vdev,
  357. struct vnic_devcmd_fw_info **fw_info)
  358. {
  359. u64 a0, a1 = 0;
  360. int wait = VNIC_DVCMD_TMO;
  361. int err = 0;
  362. if (!vdev->fw_info) {
  363. vdev->fw_info = dma_alloc_coherent(&vdev->pdev->dev,
  364. sizeof(struct vnic_devcmd_fw_info),
  365. &vdev->fw_info_pa, GFP_KERNEL);
  366. if (!vdev->fw_info)
  367. return -ENOMEM;
  368. a0 = vdev->fw_info_pa;
  369. /* only get fw_info once and cache it */
  370. err = svnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
  371. }
  372. *fw_info = vdev->fw_info;
  373. return err;
  374. }
  375. int svnic_dev_spec(struct vnic_dev *vdev, unsigned int offset,
  376. unsigned int size, void *value)
  377. {
  378. u64 a0, a1;
  379. int wait = VNIC_DVCMD_TMO;
  380. int err;
  381. a0 = offset;
  382. a1 = size;
  383. err = svnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
  384. switch (size) {
  385. case 1:
  386. *(u8 *)value = (u8)a0;
  387. break;
  388. case 2:
  389. *(u16 *)value = (u16)a0;
  390. break;
  391. case 4:
  392. *(u32 *)value = (u32)a0;
  393. break;
  394. case 8:
  395. *(u64 *)value = a0;
  396. break;
  397. default:
  398. BUG();
  399. break;
  400. }
  401. return err;
  402. }
  403. int svnic_dev_stats_clear(struct vnic_dev *vdev)
  404. {
  405. u64 a0 = 0, a1 = 0;
  406. int wait = VNIC_DVCMD_TMO;
  407. return svnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
  408. }
  409. int svnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
  410. {
  411. u64 a0, a1;
  412. int wait = VNIC_DVCMD_TMO;
  413. if (!vdev->stats) {
  414. vdev->stats = dma_alloc_coherent(&vdev->pdev->dev,
  415. sizeof(struct vnic_stats), &vdev->stats_pa, GFP_KERNEL);
  416. if (!vdev->stats)
  417. return -ENOMEM;
  418. }
  419. *stats = vdev->stats;
  420. a0 = vdev->stats_pa;
  421. a1 = sizeof(struct vnic_stats);
  422. return svnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
  423. }
  424. int svnic_dev_close(struct vnic_dev *vdev)
  425. {
  426. u64 a0 = 0, a1 = 0;
  427. int wait = VNIC_DVCMD_TMO;
  428. return svnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
  429. }
  430. int svnic_dev_enable_wait(struct vnic_dev *vdev)
  431. {
  432. u64 a0 = 0, a1 = 0;
  433. int wait = VNIC_DVCMD_TMO;
  434. int err = 0;
  435. err = svnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
  436. if (err == ERR_ECMDUNKNOWN)
  437. return svnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
  438. return err;
  439. }
  440. int svnic_dev_disable(struct vnic_dev *vdev)
  441. {
  442. u64 a0 = 0, a1 = 0;
  443. int wait = VNIC_DVCMD_TMO;
  444. return svnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
  445. }
  446. int svnic_dev_open(struct vnic_dev *vdev, int arg)
  447. {
  448. u64 a0 = (u32)arg, a1 = 0;
  449. int wait = VNIC_DVCMD_TMO;
  450. return svnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
  451. }
  452. int svnic_dev_open_done(struct vnic_dev *vdev, int *done)
  453. {
  454. u64 a0 = 0, a1 = 0;
  455. int wait = VNIC_DVCMD_TMO;
  456. int err;
  457. *done = 0;
  458. err = svnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
  459. if (err)
  460. return err;
  461. *done = (a0 == 0);
  462. return 0;
  463. }
  464. int svnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
  465. {
  466. u64 a0, a1;
  467. int wait = VNIC_DVCMD_TMO;
  468. if (!vdev->notify) {
  469. vdev->notify = dma_alloc_coherent(&vdev->pdev->dev,
  470. sizeof(struct vnic_devcmd_notify),
  471. &vdev->notify_pa, GFP_KERNEL);
  472. if (!vdev->notify)
  473. return -ENOMEM;
  474. }
  475. a0 = vdev->notify_pa;
  476. a1 = ((u64)intr << 32) & VNIC_NOTIFY_INTR_MASK;
  477. a1 += sizeof(struct vnic_devcmd_notify);
  478. return svnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  479. }
  480. void svnic_dev_notify_unset(struct vnic_dev *vdev)
  481. {
  482. u64 a0, a1;
  483. int wait = VNIC_DVCMD_TMO;
  484. a0 = 0; /* paddr = 0 to unset notify buffer */
  485. a1 = VNIC_NOTIFY_INTR_MASK; /* intr num = -1 to unreg for intr */
  486. a1 += sizeof(struct vnic_devcmd_notify);
  487. svnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  488. }
  489. static int vnic_dev_notify_ready(struct vnic_dev *vdev)
  490. {
  491. u32 *words;
  492. unsigned int nwords = sizeof(struct vnic_devcmd_notify) / 4;
  493. unsigned int i;
  494. u32 csum;
  495. if (!vdev->notify)
  496. return 0;
  497. do {
  498. csum = 0;
  499. memcpy(&vdev->notify_copy, vdev->notify,
  500. sizeof(struct vnic_devcmd_notify));
  501. words = (u32 *)&vdev->notify_copy;
  502. for (i = 1; i < nwords; i++)
  503. csum += words[i];
  504. } while (csum != words[0]);
  505. return 1;
  506. }
  507. int svnic_dev_init(struct vnic_dev *vdev, int arg)
  508. {
  509. u64 a0 = (u32)arg, a1 = 0;
  510. int wait = VNIC_DVCMD_TMO;
  511. return svnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
  512. }
  513. int svnic_dev_link_status(struct vnic_dev *vdev)
  514. {
  515. if (!vnic_dev_notify_ready(vdev))
  516. return 0;
  517. return vdev->notify_copy.link_state;
  518. }
  519. u32 svnic_dev_link_down_cnt(struct vnic_dev *vdev)
  520. {
  521. if (!vnic_dev_notify_ready(vdev))
  522. return 0;
  523. return vdev->notify_copy.link_down_cnt;
  524. }
  525. void svnic_dev_set_intr_mode(struct vnic_dev *vdev,
  526. enum vnic_dev_intr_mode intr_mode)
  527. {
  528. vdev->intr_mode = intr_mode;
  529. }
  530. enum vnic_dev_intr_mode svnic_dev_get_intr_mode(struct vnic_dev *vdev)
  531. {
  532. return vdev->intr_mode;
  533. }
  534. void svnic_dev_unregister(struct vnic_dev *vdev)
  535. {
  536. if (vdev) {
  537. if (vdev->notify)
  538. dma_free_coherent(&vdev->pdev->dev,
  539. sizeof(struct vnic_devcmd_notify),
  540. vdev->notify,
  541. vdev->notify_pa);
  542. if (vdev->stats)
  543. dma_free_coherent(&vdev->pdev->dev,
  544. sizeof(struct vnic_stats),
  545. vdev->stats, vdev->stats_pa);
  546. if (vdev->fw_info)
  547. dma_free_coherent(&vdev->pdev->dev,
  548. sizeof(struct vnic_devcmd_fw_info),
  549. vdev->fw_info, vdev->fw_info_pa);
  550. if (vdev->devcmd2)
  551. vnic_dev_deinit_devcmd2(vdev);
  552. kfree(vdev);
  553. }
  554. }
  555. struct vnic_dev *svnic_dev_alloc_discover(struct vnic_dev *vdev,
  556. void *priv,
  557. struct pci_dev *pdev,
  558. struct vnic_dev_bar *bar,
  559. unsigned int num_bars)
  560. {
  561. if (!vdev) {
  562. vdev = kzalloc_obj(struct vnic_dev, GFP_ATOMIC);
  563. if (!vdev)
  564. return NULL;
  565. }
  566. vdev->priv = priv;
  567. vdev->pdev = pdev;
  568. if (vnic_dev_discover_res(vdev, bar, num_bars))
  569. goto err_out;
  570. return vdev;
  571. err_out:
  572. svnic_dev_unregister(vdev);
  573. return NULL;
  574. } /* end of svnic_dev_alloc_discover */
  575. /*
  576. * fallback option is left to keep the interface common for other vnics.
  577. */
  578. int svnic_dev_cmd_init(struct vnic_dev *vdev, int fallback)
  579. {
  580. int err = -ENODEV;
  581. void __iomem *p;
  582. p = svnic_dev_get_res(vdev, RES_TYPE_DEVCMD2, 0);
  583. if (p)
  584. err = svnic_dev_init_devcmd2(vdev);
  585. else
  586. pr_err("DEVCMD2 resource not found.\n");
  587. return err;
  588. } /* end of svnic_dev_cmd_init */