vnic_dev.c 20 KB

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