vnic_dev.c 22 KB

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