vnic_dev.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. 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. a0 = vdev->fw_info_pa;
  337. /* only get fw_info once and cache it */
  338. err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
  339. }
  340. *fw_info = vdev->fw_info;
  341. return err;
  342. }
  343. int vnic_dev_hw_version(struct vnic_dev *vdev, enum vnic_dev_hw_version *hw_ver)
  344. {
  345. struct vnic_devcmd_fw_info *fw_info;
  346. int err;
  347. err = vnic_dev_fw_info(vdev, &fw_info);
  348. if (err)
  349. return err;
  350. if (strncmp(fw_info->hw_version, "A1", sizeof("A1")) == 0)
  351. *hw_ver = VNIC_DEV_HW_VER_A1;
  352. else if (strncmp(fw_info->hw_version, "A2", sizeof("A2")) == 0)
  353. *hw_ver = VNIC_DEV_HW_VER_A2;
  354. else
  355. *hw_ver = VNIC_DEV_HW_VER_UNKNOWN;
  356. return 0;
  357. }
  358. int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
  359. void *value)
  360. {
  361. u64 a0, a1;
  362. int wait = 1000;
  363. int err;
  364. a0 = offset;
  365. a1 = size;
  366. err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
  367. switch (size) {
  368. case 1: *(u8 *)value = (u8)a0; break;
  369. case 2: *(u16 *)value = (u16)a0; break;
  370. case 4: *(u32 *)value = (u32)a0; break;
  371. case 8: *(u64 *)value = a0; break;
  372. default: BUG(); break;
  373. }
  374. return err;
  375. }
  376. int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
  377. {
  378. u64 a0, a1;
  379. int wait = 1000;
  380. if (!vdev->stats) {
  381. vdev->stats = pci_alloc_consistent(vdev->pdev,
  382. sizeof(struct vnic_stats), &vdev->stats_pa);
  383. if (!vdev->stats)
  384. return -ENOMEM;
  385. }
  386. *stats = vdev->stats;
  387. a0 = vdev->stats_pa;
  388. a1 = sizeof(struct vnic_stats);
  389. return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
  390. }
  391. int vnic_dev_close(struct vnic_dev *vdev)
  392. {
  393. u64 a0 = 0, a1 = 0;
  394. int wait = 1000;
  395. return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
  396. }
  397. int vnic_dev_enable(struct vnic_dev *vdev)
  398. {
  399. u64 a0 = 0, a1 = 0;
  400. int wait = 1000;
  401. return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
  402. }
  403. int vnic_dev_disable(struct vnic_dev *vdev)
  404. {
  405. u64 a0 = 0, a1 = 0;
  406. int wait = 1000;
  407. return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
  408. }
  409. int vnic_dev_open(struct vnic_dev *vdev, int arg)
  410. {
  411. u64 a0 = (u32)arg, a1 = 0;
  412. int wait = 1000;
  413. return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
  414. }
  415. int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
  416. {
  417. u64 a0 = 0, a1 = 0;
  418. int wait = 1000;
  419. int err;
  420. *done = 0;
  421. err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
  422. if (err)
  423. return err;
  424. *done = (a0 == 0);
  425. return 0;
  426. }
  427. int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
  428. {
  429. u64 a0 = (u32)arg, a1 = 0;
  430. int wait = 1000;
  431. return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
  432. }
  433. int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
  434. {
  435. u64 a0 = 0, a1 = 0;
  436. int wait = 1000;
  437. int err;
  438. *done = 0;
  439. err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
  440. if (err)
  441. return err;
  442. *done = (a0 == 0);
  443. return 0;
  444. }
  445. int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
  446. {
  447. u64 a0 = (u32)arg, a1 = 0;
  448. int wait = 1000;
  449. int err;
  450. err = vnic_dev_cmd(vdev, CMD_HANG_RESET, &a0, &a1, wait);
  451. if (err == ERR_ECMDUNKNOWN) {
  452. err = vnic_dev_soft_reset(vdev, arg);
  453. if (err)
  454. return err;
  455. return vnic_dev_init(vdev, 0);
  456. }
  457. return err;
  458. }
  459. int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
  460. {
  461. u64 a0 = 0, a1 = 0;
  462. int wait = 1000;
  463. int err;
  464. *done = 0;
  465. err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS, &a0, &a1, wait);
  466. if (err) {
  467. if (err == ERR_ECMDUNKNOWN)
  468. return vnic_dev_soft_reset_done(vdev, done);
  469. return err;
  470. }
  471. *done = (a0 == 0);
  472. return 0;
  473. }
  474. int vnic_dev_hang_notify(struct vnic_dev *vdev)
  475. {
  476. u64 a0, a1;
  477. int wait = 1000;
  478. return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
  479. }
  480. int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
  481. {
  482. u64 a0, a1;
  483. int wait = 1000;
  484. int err, i;
  485. for (i = 0; i < ETH_ALEN; i++)
  486. mac_addr[i] = 0;
  487. err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
  488. if (err)
  489. return err;
  490. for (i = 0; i < ETH_ALEN; i++)
  491. mac_addr[i] = ((u8 *)&a0)[i];
  492. return 0;
  493. }
  494. int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
  495. int broadcast, int promisc, int allmulti)
  496. {
  497. u64 a0, a1 = 0;
  498. int wait = 1000;
  499. int err;
  500. a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
  501. (multicast ? CMD_PFILTER_MULTICAST : 0) |
  502. (broadcast ? CMD_PFILTER_BROADCAST : 0) |
  503. (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
  504. (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
  505. err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
  506. if (err)
  507. pr_err("Can't set packet filter\n");
  508. return err;
  509. }
  510. int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
  511. {
  512. u64 a0 = 0, a1 = 0;
  513. int wait = 1000;
  514. int err;
  515. int i;
  516. for (i = 0; i < ETH_ALEN; i++)
  517. ((u8 *)&a0)[i] = addr[i];
  518. err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  519. if (err)
  520. pr_err("Can't add addr [%pM], %d\n", addr, err);
  521. return err;
  522. }
  523. int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
  524. {
  525. u64 a0 = 0, a1 = 0;
  526. int wait = 1000;
  527. int err;
  528. int i;
  529. for (i = 0; i < ETH_ALEN; i++)
  530. ((u8 *)&a0)[i] = addr[i];
  531. err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
  532. if (err)
  533. pr_err("Can't del addr [%pM], %d\n", addr, err);
  534. return err;
  535. }
  536. int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
  537. u8 ig_vlan_rewrite_mode)
  538. {
  539. u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
  540. int wait = 1000;
  541. int err;
  542. err = vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, &a0, &a1, wait);
  543. if (err == ERR_ECMDUNKNOWN)
  544. return 0;
  545. return err;
  546. }
  547. int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
  548. void *notify_addr, dma_addr_t notify_pa, u16 intr)
  549. {
  550. u64 a0, a1;
  551. int wait = 1000;
  552. int r;
  553. memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
  554. vdev->notify = notify_addr;
  555. vdev->notify_pa = notify_pa;
  556. a0 = (u64)notify_pa;
  557. a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
  558. a1 += sizeof(struct vnic_devcmd_notify);
  559. r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  560. vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
  561. return r;
  562. }
  563. int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
  564. {
  565. void *notify_addr;
  566. dma_addr_t notify_pa;
  567. if (vdev->notify || vdev->notify_pa) {
  568. pr_err("notify block %p still allocated", vdev->notify);
  569. return -EINVAL;
  570. }
  571. notify_addr = pci_alloc_consistent(vdev->pdev,
  572. sizeof(struct vnic_devcmd_notify),
  573. &notify_pa);
  574. if (!notify_addr)
  575. return -ENOMEM;
  576. return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
  577. }
  578. int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
  579. {
  580. u64 a0, a1;
  581. int wait = 1000;
  582. int err;
  583. a0 = 0; /* paddr = 0 to unset notify buffer */
  584. a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
  585. a1 += sizeof(struct vnic_devcmd_notify);
  586. err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  587. vdev->notify = NULL;
  588. vdev->notify_pa = 0;
  589. vdev->notify_sz = 0;
  590. return err;
  591. }
  592. int vnic_dev_notify_unset(struct vnic_dev *vdev)
  593. {
  594. if (vdev->notify) {
  595. pci_free_consistent(vdev->pdev,
  596. sizeof(struct vnic_devcmd_notify),
  597. vdev->notify,
  598. vdev->notify_pa);
  599. }
  600. return vnic_dev_notify_unsetcmd(vdev);
  601. }
  602. static int vnic_dev_notify_ready(struct vnic_dev *vdev)
  603. {
  604. u32 *words;
  605. unsigned int nwords = vdev->notify_sz / 4;
  606. unsigned int i;
  607. u32 csum;
  608. if (!vdev->notify || !vdev->notify_sz)
  609. return 0;
  610. do {
  611. csum = 0;
  612. memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
  613. words = (u32 *)&vdev->notify_copy;
  614. for (i = 1; i < nwords; i++)
  615. csum += words[i];
  616. } while (csum != words[0]);
  617. return 1;
  618. }
  619. int vnic_dev_init(struct vnic_dev *vdev, int arg)
  620. {
  621. u64 a0 = (u32)arg, a1 = 0;
  622. int wait = 1000;
  623. int r = 0;
  624. if (vnic_dev_capable(vdev, CMD_INIT))
  625. r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
  626. else {
  627. vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
  628. if (a0 & CMD_INITF_DEFAULT_MAC) {
  629. /* Emulate these for old CMD_INIT_v1 which
  630. * didn't pass a0 so no CMD_INITF_*.
  631. */
  632. vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
  633. vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  634. }
  635. }
  636. return r;
  637. }
  638. int vnic_dev_init_done(struct vnic_dev *vdev, int *done, int *err)
  639. {
  640. u64 a0 = 0, a1 = 0;
  641. int wait = 1000;
  642. int ret;
  643. *done = 0;
  644. ret = vnic_dev_cmd(vdev, CMD_INIT_STATUS, &a0, &a1, wait);
  645. if (ret)
  646. return ret;
  647. *done = (a0 == 0);
  648. *err = (a0 == 0) ? (int)a1:0;
  649. return 0;
  650. }
  651. int vnic_dev_init_prov(struct vnic_dev *vdev, u8 *buf, u32 len)
  652. {
  653. u64 a0, a1 = len;
  654. int wait = 1000;
  655. dma_addr_t prov_pa;
  656. void *prov_buf;
  657. int ret;
  658. prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
  659. if (!prov_buf)
  660. return -ENOMEM;
  661. memcpy(prov_buf, buf, len);
  662. a0 = prov_pa;
  663. ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO, &a0, &a1, wait);
  664. pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
  665. return ret;
  666. }
  667. int vnic_dev_deinit(struct vnic_dev *vdev)
  668. {
  669. u64 a0 = 0, a1 = 0;
  670. int wait = 1000;
  671. return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
  672. }
  673. int vnic_dev_link_status(struct vnic_dev *vdev)
  674. {
  675. if (!vnic_dev_notify_ready(vdev))
  676. return 0;
  677. return vdev->notify_copy.link_state;
  678. }
  679. u32 vnic_dev_port_speed(struct vnic_dev *vdev)
  680. {
  681. if (!vnic_dev_notify_ready(vdev))
  682. return 0;
  683. return vdev->notify_copy.port_speed;
  684. }
  685. u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
  686. {
  687. if (!vnic_dev_notify_ready(vdev))
  688. return 0;
  689. return vdev->notify_copy.msglvl;
  690. }
  691. u32 vnic_dev_mtu(struct vnic_dev *vdev)
  692. {
  693. if (!vnic_dev_notify_ready(vdev))
  694. return 0;
  695. return vdev->notify_copy.mtu;
  696. }
  697. void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
  698. enum vnic_dev_intr_mode intr_mode)
  699. {
  700. vdev->intr_mode = intr_mode;
  701. }
  702. enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
  703. struct vnic_dev *vdev)
  704. {
  705. return vdev->intr_mode;
  706. }
  707. void vnic_dev_unregister(struct vnic_dev *vdev)
  708. {
  709. if (vdev) {
  710. if (vdev->notify)
  711. pci_free_consistent(vdev->pdev,
  712. sizeof(struct vnic_devcmd_notify),
  713. vdev->notify,
  714. vdev->notify_pa);
  715. if (vdev->stats)
  716. pci_free_consistent(vdev->pdev,
  717. sizeof(struct vnic_stats),
  718. vdev->stats, vdev->stats_pa);
  719. if (vdev->fw_info)
  720. pci_free_consistent(vdev->pdev,
  721. sizeof(struct vnic_devcmd_fw_info),
  722. vdev->fw_info, vdev->fw_info_pa);
  723. kfree(vdev);
  724. }
  725. }
  726. struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
  727. void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar,
  728. unsigned int num_bars)
  729. {
  730. if (!vdev) {
  731. vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
  732. if (!vdev)
  733. return NULL;
  734. }
  735. vdev->priv = priv;
  736. vdev->pdev = pdev;
  737. if (vnic_dev_discover_res(vdev, bar, num_bars))
  738. goto err_out;
  739. vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
  740. if (!vdev->devcmd)
  741. goto err_out;
  742. return vdev;
  743. err_out:
  744. vnic_dev_unregister(vdev);
  745. return NULL;
  746. }