vnic_dev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*
  2. * Copyright 2008-2010 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. enum vnic_proxy_type {
  30. PROXY_NONE,
  31. PROXY_BY_BDF,
  32. };
  33. struct vnic_res {
  34. void __iomem *vaddr;
  35. dma_addr_t bus_addr;
  36. unsigned int count;
  37. };
  38. struct vnic_dev {
  39. void *priv;
  40. struct pci_dev *pdev;
  41. struct vnic_res res[RES_TYPE_MAX];
  42. enum vnic_dev_intr_mode intr_mode;
  43. struct vnic_devcmd __iomem *devcmd;
  44. struct vnic_devcmd_notify *notify;
  45. struct vnic_devcmd_notify notify_copy;
  46. dma_addr_t notify_pa;
  47. u32 notify_sz;
  48. dma_addr_t linkstatus_pa;
  49. struct vnic_stats *stats;
  50. dma_addr_t stats_pa;
  51. struct vnic_devcmd_fw_info *fw_info;
  52. dma_addr_t fw_info_pa;
  53. enum vnic_proxy_type proxy;
  54. u32 proxy_index;
  55. u64 args[VNIC_DEVCMD_NARGS];
  56. };
  57. #define VNIC_MAX_RES_HDR_SIZE \
  58. (sizeof(struct vnic_resource_header) + \
  59. sizeof(struct vnic_resource) * RES_TYPE_MAX)
  60. #define VNIC_RES_STRIDE 128
  61. void *vnic_dev_priv(struct vnic_dev *vdev)
  62. {
  63. return vdev->priv;
  64. }
  65. static int vnic_dev_discover_res(struct vnic_dev *vdev,
  66. struct vnic_dev_bar *bar, unsigned int num_bars)
  67. {
  68. struct vnic_resource_header __iomem *rh;
  69. struct mgmt_barmap_hdr __iomem *mrh;
  70. struct vnic_resource __iomem *r;
  71. u8 type;
  72. if (num_bars == 0)
  73. return -EINVAL;
  74. if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
  75. pr_err("vNIC BAR0 res hdr length error\n");
  76. return -EINVAL;
  77. }
  78. rh = bar->vaddr;
  79. mrh = bar->vaddr;
  80. if (!rh) {
  81. pr_err("vNIC BAR0 res hdr not mem-mapped\n");
  82. return -EINVAL;
  83. }
  84. /* Check for mgmt vnic in addition to normal vnic */
  85. if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
  86. (ioread32(&rh->version) != VNIC_RES_VERSION)) {
  87. if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
  88. (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
  89. pr_err("vNIC BAR0 res magic/version error "
  90. "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
  91. VNIC_RES_MAGIC, VNIC_RES_VERSION,
  92. MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
  93. ioread32(&rh->magic), ioread32(&rh->version));
  94. return -EINVAL;
  95. }
  96. }
  97. if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
  98. r = (struct vnic_resource __iomem *)(mrh + 1);
  99. else
  100. r = (struct vnic_resource __iomem *)(rh + 1);
  101. while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
  102. u8 bar_num = ioread8(&r->bar);
  103. u32 bar_offset = ioread32(&r->bar_offset);
  104. u32 count = ioread32(&r->count);
  105. u32 len;
  106. r++;
  107. if (bar_num >= num_bars)
  108. continue;
  109. if (!bar[bar_num].len || !bar[bar_num].vaddr)
  110. continue;
  111. switch (type) {
  112. case RES_TYPE_WQ:
  113. case RES_TYPE_RQ:
  114. case RES_TYPE_CQ:
  115. case RES_TYPE_INTR_CTRL:
  116. /* each count is stride bytes long */
  117. len = count * VNIC_RES_STRIDE;
  118. if (len + bar_offset > bar[bar_num].len) {
  119. pr_err("vNIC BAR0 resource %d "
  120. "out-of-bounds, offset 0x%x + "
  121. "size 0x%x > bar len 0x%lx\n",
  122. type, bar_offset,
  123. len,
  124. bar[bar_num].len);
  125. return -EINVAL;
  126. }
  127. break;
  128. case RES_TYPE_INTR_PBA_LEGACY:
  129. case RES_TYPE_DEVCMD:
  130. len = count;
  131. break;
  132. default:
  133. continue;
  134. }
  135. vdev->res[type].count = count;
  136. vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
  137. bar_offset;
  138. vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
  139. }
  140. return 0;
  141. }
  142. unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
  143. enum vnic_res_type type)
  144. {
  145. return vdev->res[type].count;
  146. }
  147. void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
  148. unsigned int index)
  149. {
  150. if (!vdev->res[type].vaddr)
  151. return NULL;
  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 (char __iomem *)vdev->res[type].vaddr +
  158. index * VNIC_RES_STRIDE;
  159. default:
  160. return (char __iomem *)vdev->res[type].vaddr;
  161. }
  162. }
  163. static 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. pr_err("Failed to allocate ring (size=%d), aborting\n",
  195. (int)ring->size);
  196. return -ENOMEM;
  197. }
  198. ring->base_addr = ALIGN(ring->base_addr_unaligned,
  199. ring->base_align);
  200. ring->descs = (u8 *)ring->descs_unaligned +
  201. (ring->base_addr - ring->base_addr_unaligned);
  202. vnic_dev_clear_desc_ring(ring);
  203. ring->desc_avail = ring->desc_count - 1;
  204. return 0;
  205. }
  206. void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
  207. {
  208. if (ring->descs) {
  209. pci_free_consistent(vdev->pdev,
  210. ring->size_unaligned,
  211. ring->descs_unaligned,
  212. ring->base_addr_unaligned);
  213. ring->descs = NULL;
  214. }
  215. }
  216. static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  217. int wait)
  218. {
  219. struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
  220. unsigned int i;
  221. int delay;
  222. u32 status;
  223. int err;
  224. status = ioread32(&devcmd->status);
  225. if (status == 0xFFFFFFFF) {
  226. /* PCI-e target device is gone */
  227. return -ENODEV;
  228. }
  229. if (status & STAT_BUSY) {
  230. pr_err("Busy devcmd %d\n", _CMD_N(cmd));
  231. return -EBUSY;
  232. }
  233. if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
  234. for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
  235. writeq(vdev->args[i], &devcmd->args[i]);
  236. wmb();
  237. }
  238. iowrite32(cmd, &devcmd->cmd);
  239. if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
  240. return 0;
  241. for (delay = 0; delay < wait; delay++) {
  242. udelay(100);
  243. status = ioread32(&devcmd->status);
  244. if (status == 0xFFFFFFFF) {
  245. /* PCI-e target device is gone */
  246. return -ENODEV;
  247. }
  248. if (!(status & STAT_BUSY)) {
  249. if (status & STAT_ERROR) {
  250. err = (int)readq(&devcmd->args[0]);
  251. if (err != ERR_ECMDUNKNOWN ||
  252. cmd != CMD_CAPABILITY)
  253. pr_err("Error %d devcmd %d\n",
  254. err, _CMD_N(cmd));
  255. return err;
  256. }
  257. if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
  258. rmb();
  259. for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
  260. vdev->args[i] = readq(&devcmd->args[i]);
  261. }
  262. return 0;
  263. }
  264. }
  265. pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
  266. return -ETIMEDOUT;
  267. }
  268. static int vnic_dev_cmd_proxy_by_bdf(struct vnic_dev *vdev,
  269. enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
  270. {
  271. u32 status;
  272. int err;
  273. memset(vdev->args, 0, sizeof(vdev->args));
  274. vdev->args[0] = vdev->proxy_index; /* bdf */
  275. vdev->args[1] = cmd;
  276. vdev->args[2] = *a0;
  277. vdev->args[3] = *a1;
  278. err = _vnic_dev_cmd(vdev, CMD_PROXY_BY_BDF, wait);
  279. if (err)
  280. return err;
  281. status = (u32)vdev->args[0];
  282. if (status & STAT_ERROR) {
  283. err = (int)vdev->args[1];
  284. if (err != ERR_ECMDUNKNOWN ||
  285. cmd != CMD_CAPABILITY)
  286. pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
  287. return err;
  288. }
  289. *a0 = vdev->args[1];
  290. *a1 = vdev->args[2];
  291. return 0;
  292. }
  293. static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
  294. enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
  295. {
  296. int err;
  297. vdev->args[0] = *a0;
  298. vdev->args[1] = *a1;
  299. err = _vnic_dev_cmd(vdev, cmd, wait);
  300. *a0 = vdev->args[0];
  301. *a1 = vdev->args[1];
  302. return err;
  303. }
  304. int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  305. u64 *a0, u64 *a1, int wait)
  306. {
  307. memset(vdev->args, 0, sizeof(vdev->args));
  308. switch (vdev->proxy) {
  309. case PROXY_BY_BDF:
  310. return vnic_dev_cmd_proxy_by_bdf(vdev, cmd, a0, a1, wait);
  311. case PROXY_NONE:
  312. default:
  313. return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait);
  314. }
  315. }
  316. static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
  317. {
  318. u64 a0 = (u32)cmd, a1 = 0;
  319. int wait = 1000;
  320. int err;
  321. err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
  322. return !(err || a0);
  323. }
  324. int vnic_dev_fw_info(struct vnic_dev *vdev,
  325. struct vnic_devcmd_fw_info **fw_info)
  326. {
  327. u64 a0, a1 = 0;
  328. int wait = 1000;
  329. int err = 0;
  330. if (!vdev->fw_info) {
  331. vdev->fw_info = pci_alloc_consistent(vdev->pdev,
  332. sizeof(struct vnic_devcmd_fw_info),
  333. &vdev->fw_info_pa);
  334. if (!vdev->fw_info)
  335. return -ENOMEM;
  336. memset(vdev->fw_info, 0, sizeof(struct vnic_devcmd_fw_info));
  337. a0 = vdev->fw_info_pa;
  338. a1 = sizeof(struct vnic_devcmd_fw_info);
  339. /* only get fw_info once and cache it */
  340. err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
  341. if (err == ERR_ECMDUNKNOWN) {
  342. err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO_OLD,
  343. &a0, &a1, wait);
  344. }
  345. }
  346. *fw_info = vdev->fw_info;
  347. return err;
  348. }
  349. int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
  350. void *value)
  351. {
  352. u64 a0, a1;
  353. int wait = 1000;
  354. int err;
  355. a0 = offset;
  356. a1 = size;
  357. err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
  358. switch (size) {
  359. case 1: *(u8 *)value = (u8)a0; break;
  360. case 2: *(u16 *)value = (u16)a0; break;
  361. case 4: *(u32 *)value = (u32)a0; break;
  362. case 8: *(u64 *)value = a0; break;
  363. default: BUG(); break;
  364. }
  365. return err;
  366. }
  367. int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
  368. {
  369. u64 a0, a1;
  370. int wait = 1000;
  371. if (!vdev->stats) {
  372. vdev->stats = pci_alloc_consistent(vdev->pdev,
  373. sizeof(struct vnic_stats), &vdev->stats_pa);
  374. if (!vdev->stats)
  375. return -ENOMEM;
  376. }
  377. *stats = vdev->stats;
  378. a0 = vdev->stats_pa;
  379. a1 = sizeof(struct vnic_stats);
  380. return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
  381. }
  382. int vnic_dev_close(struct vnic_dev *vdev)
  383. {
  384. u64 a0 = 0, a1 = 0;
  385. int wait = 1000;
  386. return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
  387. }
  388. int vnic_dev_enable_wait(struct vnic_dev *vdev)
  389. {
  390. u64 a0 = 0, a1 = 0;
  391. int wait = 1000;
  392. int err;
  393. err = vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
  394. if (err == ERR_ECMDUNKNOWN)
  395. return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
  396. return err;
  397. }
  398. int vnic_dev_disable(struct vnic_dev *vdev)
  399. {
  400. u64 a0 = 0, a1 = 0;
  401. int wait = 1000;
  402. return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
  403. }
  404. int vnic_dev_open(struct vnic_dev *vdev, int arg)
  405. {
  406. u64 a0 = (u32)arg, a1 = 0;
  407. int wait = 1000;
  408. return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
  409. }
  410. int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
  411. {
  412. u64 a0 = 0, a1 = 0;
  413. int wait = 1000;
  414. int err;
  415. *done = 0;
  416. err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
  417. if (err)
  418. return err;
  419. *done = (a0 == 0);
  420. return 0;
  421. }
  422. static int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
  423. {
  424. u64 a0 = (u32)arg, a1 = 0;
  425. int wait = 1000;
  426. return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
  427. }
  428. static int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
  429. {
  430. u64 a0 = 0, a1 = 0;
  431. int wait = 1000;
  432. int err;
  433. *done = 0;
  434. err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
  435. if (err)
  436. return err;
  437. *done = (a0 == 0);
  438. return 0;
  439. }
  440. int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
  441. {
  442. u64 a0 = (u32)arg, a1 = 0;
  443. int wait = 1000;
  444. int err;
  445. err = vnic_dev_cmd(vdev, CMD_HANG_RESET, &a0, &a1, wait);
  446. if (err == ERR_ECMDUNKNOWN) {
  447. err = vnic_dev_soft_reset(vdev, arg);
  448. if (err)
  449. return err;
  450. return vnic_dev_init(vdev, 0);
  451. }
  452. return err;
  453. }
  454. int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
  455. {
  456. u64 a0 = 0, a1 = 0;
  457. int wait = 1000;
  458. int err;
  459. *done = 0;
  460. err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS, &a0, &a1, wait);
  461. if (err) {
  462. if (err == ERR_ECMDUNKNOWN)
  463. return vnic_dev_soft_reset_done(vdev, done);
  464. return err;
  465. }
  466. *done = (a0 == 0);
  467. return 0;
  468. }
  469. int vnic_dev_hang_notify(struct vnic_dev *vdev)
  470. {
  471. u64 a0, a1;
  472. int wait = 1000;
  473. return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
  474. }
  475. int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
  476. {
  477. u64 a0, a1;
  478. int wait = 1000;
  479. int err, i;
  480. for (i = 0; i < ETH_ALEN; i++)
  481. mac_addr[i] = 0;
  482. err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
  483. if (err)
  484. return err;
  485. for (i = 0; i < ETH_ALEN; i++)
  486. mac_addr[i] = ((u8 *)&a0)[i];
  487. return 0;
  488. }
  489. int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
  490. int broadcast, int promisc, int allmulti)
  491. {
  492. u64 a0, a1 = 0;
  493. int wait = 1000;
  494. int err;
  495. a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
  496. (multicast ? CMD_PFILTER_MULTICAST : 0) |
  497. (broadcast ? CMD_PFILTER_BROADCAST : 0) |
  498. (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
  499. (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
  500. err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
  501. if (err)
  502. pr_err("Can't set packet filter\n");
  503. return err;
  504. }
  505. int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
  506. {
  507. u64 a0 = 0, a1 = 0;
  508. int wait = 1000;
  509. int err;
  510. int i;
  511. for (i = 0; i < ETH_ALEN; i++)
  512. ((u8 *)&a0)[i] = addr[i];
  513. err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  514. if (err)
  515. pr_err("Can't add addr [%pM], %d\n", addr, err);
  516. return err;
  517. }
  518. int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
  519. {
  520. u64 a0 = 0, a1 = 0;
  521. int wait = 1000;
  522. int err;
  523. int i;
  524. for (i = 0; i < ETH_ALEN; i++)
  525. ((u8 *)&a0)[i] = addr[i];
  526. err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
  527. if (err)
  528. pr_err("Can't del addr [%pM], %d\n", addr, err);
  529. return err;
  530. }
  531. int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
  532. u8 ig_vlan_rewrite_mode)
  533. {
  534. u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
  535. int wait = 1000;
  536. int err;
  537. err = vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, &a0, &a1, wait);
  538. if (err == ERR_ECMDUNKNOWN)
  539. return 0;
  540. return err;
  541. }
  542. static int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
  543. void *notify_addr, dma_addr_t notify_pa, u16 intr)
  544. {
  545. u64 a0, a1;
  546. int wait = 1000;
  547. int r;
  548. memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
  549. vdev->notify = notify_addr;
  550. vdev->notify_pa = notify_pa;
  551. a0 = (u64)notify_pa;
  552. a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
  553. a1 += sizeof(struct vnic_devcmd_notify);
  554. r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  555. vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
  556. return r;
  557. }
  558. int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
  559. {
  560. void *notify_addr;
  561. dma_addr_t notify_pa;
  562. if (vdev->notify || vdev->notify_pa) {
  563. pr_err("notify block %p still allocated", vdev->notify);
  564. return -EINVAL;
  565. }
  566. notify_addr = pci_alloc_consistent(vdev->pdev,
  567. sizeof(struct vnic_devcmd_notify),
  568. &notify_pa);
  569. if (!notify_addr)
  570. return -ENOMEM;
  571. return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
  572. }
  573. static int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
  574. {
  575. u64 a0, a1;
  576. int wait = 1000;
  577. int err;
  578. a0 = 0; /* paddr = 0 to unset notify buffer */
  579. a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
  580. a1 += sizeof(struct vnic_devcmd_notify);
  581. err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  582. vdev->notify = NULL;
  583. vdev->notify_pa = 0;
  584. vdev->notify_sz = 0;
  585. return err;
  586. }
  587. int vnic_dev_notify_unset(struct vnic_dev *vdev)
  588. {
  589. if (vdev->notify) {
  590. pci_free_consistent(vdev->pdev,
  591. sizeof(struct vnic_devcmd_notify),
  592. vdev->notify,
  593. vdev->notify_pa);
  594. }
  595. return vnic_dev_notify_unsetcmd(vdev);
  596. }
  597. static int vnic_dev_notify_ready(struct vnic_dev *vdev)
  598. {
  599. u32 *words;
  600. unsigned int nwords = vdev->notify_sz / 4;
  601. unsigned int i;
  602. u32 csum;
  603. if (!vdev->notify || !vdev->notify_sz)
  604. return 0;
  605. do {
  606. csum = 0;
  607. memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
  608. words = (u32 *)&vdev->notify_copy;
  609. for (i = 1; i < nwords; i++)
  610. csum += words[i];
  611. } while (csum != words[0]);
  612. return 1;
  613. }
  614. int vnic_dev_init(struct vnic_dev *vdev, int arg)
  615. {
  616. u64 a0 = (u32)arg, a1 = 0;
  617. int wait = 1000;
  618. int r = 0;
  619. if (vnic_dev_capable(vdev, CMD_INIT))
  620. r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
  621. else {
  622. vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
  623. if (a0 & CMD_INITF_DEFAULT_MAC) {
  624. /* Emulate these for old CMD_INIT_v1 which
  625. * didn't pass a0 so no CMD_INITF_*.
  626. */
  627. vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
  628. vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  629. }
  630. }
  631. return r;
  632. }
  633. int vnic_dev_init_done(struct vnic_dev *vdev, int *done, int *err)
  634. {
  635. u64 a0 = 0, a1 = 0;
  636. int wait = 1000;
  637. int ret;
  638. *done = 0;
  639. ret = vnic_dev_cmd(vdev, CMD_INIT_STATUS, &a0, &a1, wait);
  640. if (ret)
  641. return ret;
  642. *done = (a0 == 0);
  643. *err = (a0 == 0) ? (int)a1:0;
  644. return 0;
  645. }
  646. int vnic_dev_init_prov(struct vnic_dev *vdev, u8 *buf, u32 len)
  647. {
  648. u64 a0, a1 = len;
  649. int wait = 1000;
  650. dma_addr_t prov_pa;
  651. void *prov_buf;
  652. int ret;
  653. prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
  654. if (!prov_buf)
  655. return -ENOMEM;
  656. memcpy(prov_buf, buf, len);
  657. a0 = prov_pa;
  658. ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO, &a0, &a1, wait);
  659. pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
  660. return ret;
  661. }
  662. int vnic_dev_deinit(struct vnic_dev *vdev)
  663. {
  664. u64 a0 = 0, a1 = 0;
  665. int wait = 1000;
  666. return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
  667. }
  668. int vnic_dev_link_status(struct vnic_dev *vdev)
  669. {
  670. if (!vnic_dev_notify_ready(vdev))
  671. return 0;
  672. return vdev->notify_copy.link_state;
  673. }
  674. u32 vnic_dev_port_speed(struct vnic_dev *vdev)
  675. {
  676. if (!vnic_dev_notify_ready(vdev))
  677. return 0;
  678. return vdev->notify_copy.port_speed;
  679. }
  680. u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
  681. {
  682. if (!vnic_dev_notify_ready(vdev))
  683. return 0;
  684. return vdev->notify_copy.msglvl;
  685. }
  686. u32 vnic_dev_mtu(struct vnic_dev *vdev)
  687. {
  688. if (!vnic_dev_notify_ready(vdev))
  689. return 0;
  690. return vdev->notify_copy.mtu;
  691. }
  692. void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
  693. enum vnic_dev_intr_mode intr_mode)
  694. {
  695. vdev->intr_mode = intr_mode;
  696. }
  697. enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
  698. struct vnic_dev *vdev)
  699. {
  700. return vdev->intr_mode;
  701. }
  702. void vnic_dev_unregister(struct vnic_dev *vdev)
  703. {
  704. if (vdev) {
  705. if (vdev->notify)
  706. pci_free_consistent(vdev->pdev,
  707. sizeof(struct vnic_devcmd_notify),
  708. vdev->notify,
  709. vdev->notify_pa);
  710. if (vdev->stats)
  711. pci_free_consistent(vdev->pdev,
  712. sizeof(struct vnic_stats),
  713. vdev->stats, vdev->stats_pa);
  714. if (vdev->fw_info)
  715. pci_free_consistent(vdev->pdev,
  716. sizeof(struct vnic_devcmd_fw_info),
  717. vdev->fw_info, vdev->fw_info_pa);
  718. kfree(vdev);
  719. }
  720. }
  721. struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
  722. void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar,
  723. unsigned int num_bars)
  724. {
  725. if (!vdev) {
  726. vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
  727. if (!vdev)
  728. return NULL;
  729. }
  730. vdev->priv = priv;
  731. vdev->pdev = pdev;
  732. if (vnic_dev_discover_res(vdev, bar, num_bars))
  733. goto err_out;
  734. vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
  735. if (!vdev->devcmd)
  736. goto err_out;
  737. return vdev;
  738. err_out:
  739. vnic_dev_unregister(vdev);
  740. return NULL;
  741. }