vnic_dev.c 16 KB

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