vnic_dev.c 16 KB

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