vnic_dev.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. PROXY_BY_INDEX,
  33. };
  34. struct vnic_res {
  35. void __iomem *vaddr;
  36. dma_addr_t bus_addr;
  37. unsigned int count;
  38. };
  39. struct vnic_intr_coal_timer_info {
  40. u32 mul;
  41. u32 div;
  42. u32 max_usec;
  43. };
  44. struct vnic_dev {
  45. void *priv;
  46. struct pci_dev *pdev;
  47. struct vnic_res res[RES_TYPE_MAX];
  48. enum vnic_dev_intr_mode intr_mode;
  49. struct vnic_devcmd __iomem *devcmd;
  50. struct vnic_devcmd_notify *notify;
  51. struct vnic_devcmd_notify notify_copy;
  52. dma_addr_t notify_pa;
  53. u32 notify_sz;
  54. dma_addr_t linkstatus_pa;
  55. struct vnic_stats *stats;
  56. dma_addr_t stats_pa;
  57. struct vnic_devcmd_fw_info *fw_info;
  58. dma_addr_t fw_info_pa;
  59. enum vnic_proxy_type proxy;
  60. u32 proxy_index;
  61. u64 args[VNIC_DEVCMD_NARGS];
  62. struct vnic_intr_coal_timer_info intr_coal_timer_info;
  63. };
  64. #define VNIC_MAX_RES_HDR_SIZE \
  65. (sizeof(struct vnic_resource_header) + \
  66. sizeof(struct vnic_resource) * RES_TYPE_MAX)
  67. #define VNIC_RES_STRIDE 128
  68. void *vnic_dev_priv(struct vnic_dev *vdev)
  69. {
  70. return vdev->priv;
  71. }
  72. static int vnic_dev_discover_res(struct vnic_dev *vdev,
  73. struct vnic_dev_bar *bar, unsigned int num_bars)
  74. {
  75. struct vnic_resource_header __iomem *rh;
  76. struct mgmt_barmap_hdr __iomem *mrh;
  77. struct vnic_resource __iomem *r;
  78. u8 type;
  79. if (num_bars == 0)
  80. return -EINVAL;
  81. if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
  82. pr_err("vNIC BAR0 res hdr length error\n");
  83. return -EINVAL;
  84. }
  85. rh = bar->vaddr;
  86. mrh = bar->vaddr;
  87. if (!rh) {
  88. pr_err("vNIC BAR0 res hdr not mem-mapped\n");
  89. return -EINVAL;
  90. }
  91. /* Check for mgmt vnic in addition to normal vnic */
  92. if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
  93. (ioread32(&rh->version) != VNIC_RES_VERSION)) {
  94. if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
  95. (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
  96. pr_err("vNIC BAR0 res magic/version error "
  97. "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
  98. VNIC_RES_MAGIC, VNIC_RES_VERSION,
  99. MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
  100. ioread32(&rh->magic), ioread32(&rh->version));
  101. return -EINVAL;
  102. }
  103. }
  104. if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
  105. r = (struct vnic_resource __iomem *)(mrh + 1);
  106. else
  107. r = (struct vnic_resource __iomem *)(rh + 1);
  108. while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
  109. u8 bar_num = ioread8(&r->bar);
  110. u32 bar_offset = ioread32(&r->bar_offset);
  111. u32 count = ioread32(&r->count);
  112. u32 len;
  113. r++;
  114. if (bar_num >= num_bars)
  115. continue;
  116. if (!bar[bar_num].len || !bar[bar_num].vaddr)
  117. continue;
  118. switch (type) {
  119. case RES_TYPE_WQ:
  120. case RES_TYPE_RQ:
  121. case RES_TYPE_CQ:
  122. case RES_TYPE_INTR_CTRL:
  123. /* each count is stride bytes long */
  124. len = count * VNIC_RES_STRIDE;
  125. if (len + bar_offset > bar[bar_num].len) {
  126. pr_err("vNIC BAR0 resource %d "
  127. "out-of-bounds, offset 0x%x + "
  128. "size 0x%x > bar len 0x%lx\n",
  129. type, bar_offset,
  130. len,
  131. bar[bar_num].len);
  132. return -EINVAL;
  133. }
  134. break;
  135. case RES_TYPE_INTR_PBA_LEGACY:
  136. case RES_TYPE_DEVCMD:
  137. len = count;
  138. break;
  139. default:
  140. continue;
  141. }
  142. vdev->res[type].count = count;
  143. vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
  144. bar_offset;
  145. vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
  146. }
  147. return 0;
  148. }
  149. unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
  150. enum vnic_res_type type)
  151. {
  152. return vdev->res[type].count;
  153. }
  154. EXPORT_SYMBOL(vnic_dev_get_res_count);
  155. void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
  156. unsigned int index)
  157. {
  158. if (!vdev->res[type].vaddr)
  159. return NULL;
  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 (char __iomem *)vdev->res[type].vaddr +
  166. index * VNIC_RES_STRIDE;
  167. default:
  168. return (char __iomem *)vdev->res[type].vaddr;
  169. }
  170. }
  171. EXPORT_SYMBOL(vnic_dev_get_res);
  172. static unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
  173. unsigned int desc_count, unsigned int desc_size)
  174. {
  175. /* The base address of the desc rings must be 512 byte aligned.
  176. * Descriptor count is aligned to groups of 32 descriptors. A
  177. * count of 0 means the maximum 4096 descriptors. Descriptor
  178. * size is aligned to 16 bytes.
  179. */
  180. unsigned int count_align = 32;
  181. unsigned int desc_align = 16;
  182. ring->base_align = 512;
  183. if (desc_count == 0)
  184. desc_count = 4096;
  185. ring->desc_count = ALIGN(desc_count, count_align);
  186. ring->desc_size = ALIGN(desc_size, desc_align);
  187. ring->size = ring->desc_count * ring->desc_size;
  188. ring->size_unaligned = ring->size + ring->base_align;
  189. return ring->size_unaligned;
  190. }
  191. void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
  192. {
  193. memset(ring->descs, 0, ring->size);
  194. }
  195. int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
  196. unsigned int desc_count, unsigned int desc_size)
  197. {
  198. vnic_dev_desc_ring_size(ring, desc_count, desc_size);
  199. ring->descs_unaligned = pci_alloc_consistent(vdev->pdev,
  200. ring->size_unaligned,
  201. &ring->base_addr_unaligned);
  202. if (!ring->descs_unaligned) {
  203. pr_err("Failed to allocate ring (size=%d), aborting\n",
  204. (int)ring->size);
  205. return -ENOMEM;
  206. }
  207. ring->base_addr = ALIGN(ring->base_addr_unaligned,
  208. ring->base_align);
  209. ring->descs = (u8 *)ring->descs_unaligned +
  210. (ring->base_addr - ring->base_addr_unaligned);
  211. vnic_dev_clear_desc_ring(ring);
  212. ring->desc_avail = ring->desc_count - 1;
  213. return 0;
  214. }
  215. void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
  216. {
  217. if (ring->descs) {
  218. pci_free_consistent(vdev->pdev,
  219. ring->size_unaligned,
  220. ring->descs_unaligned,
  221. ring->base_addr_unaligned);
  222. ring->descs = NULL;
  223. }
  224. }
  225. static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  226. int wait)
  227. {
  228. struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
  229. unsigned int i;
  230. int delay;
  231. u32 status;
  232. int err;
  233. status = ioread32(&devcmd->status);
  234. if (status == 0xFFFFFFFF) {
  235. /* PCI-e target device is gone */
  236. return -ENODEV;
  237. }
  238. if (status & STAT_BUSY) {
  239. pr_err("Busy devcmd %d\n", _CMD_N(cmd));
  240. return -EBUSY;
  241. }
  242. if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
  243. for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
  244. writeq(vdev->args[i], &devcmd->args[i]);
  245. wmb();
  246. }
  247. iowrite32(cmd, &devcmd->cmd);
  248. if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
  249. return 0;
  250. for (delay = 0; delay < wait; delay++) {
  251. udelay(100);
  252. status = ioread32(&devcmd->status);
  253. if (status == 0xFFFFFFFF) {
  254. /* PCI-e target device is gone */
  255. return -ENODEV;
  256. }
  257. if (!(status & STAT_BUSY)) {
  258. if (status & STAT_ERROR) {
  259. err = (int)readq(&devcmd->args[0]);
  260. if (err == ERR_EINVAL &&
  261. cmd == CMD_CAPABILITY)
  262. return -err;
  263. if (err != ERR_ECMDUNKNOWN ||
  264. cmd != CMD_CAPABILITY)
  265. pr_err("Error %d devcmd %d\n",
  266. err, _CMD_N(cmd));
  267. return -err;
  268. }
  269. if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
  270. rmb();
  271. for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
  272. vdev->args[i] = readq(&devcmd->args[i]);
  273. }
  274. return 0;
  275. }
  276. }
  277. pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
  278. return -ETIMEDOUT;
  279. }
  280. static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
  281. enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
  282. u64 *a0, u64 *a1, int wait)
  283. {
  284. u32 status;
  285. int err;
  286. memset(vdev->args, 0, sizeof(vdev->args));
  287. vdev->args[0] = vdev->proxy_index;
  288. vdev->args[1] = cmd;
  289. vdev->args[2] = *a0;
  290. vdev->args[3] = *a1;
  291. err = _vnic_dev_cmd(vdev, proxy_cmd, wait);
  292. if (err)
  293. return err;
  294. status = (u32)vdev->args[0];
  295. if (status & STAT_ERROR) {
  296. err = (int)vdev->args[1];
  297. if (err != ERR_ECMDUNKNOWN ||
  298. cmd != CMD_CAPABILITY)
  299. pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
  300. return err;
  301. }
  302. *a0 = vdev->args[1];
  303. *a1 = vdev->args[2];
  304. return 0;
  305. }
  306. static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
  307. enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
  308. {
  309. int err;
  310. vdev->args[0] = *a0;
  311. vdev->args[1] = *a1;
  312. err = _vnic_dev_cmd(vdev, cmd, wait);
  313. *a0 = vdev->args[0];
  314. *a1 = vdev->args[1];
  315. return err;
  316. }
  317. void vnic_dev_cmd_proxy_by_index_start(struct vnic_dev *vdev, u16 index)
  318. {
  319. vdev->proxy = PROXY_BY_INDEX;
  320. vdev->proxy_index = index;
  321. }
  322. void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev)
  323. {
  324. vdev->proxy = PROXY_NONE;
  325. vdev->proxy_index = 0;
  326. }
  327. int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  328. u64 *a0, u64 *a1, int wait)
  329. {
  330. memset(vdev->args, 0, sizeof(vdev->args));
  331. switch (vdev->proxy) {
  332. case PROXY_BY_INDEX:
  333. return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
  334. a0, a1, wait);
  335. case PROXY_BY_BDF:
  336. return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
  337. a0, a1, wait);
  338. case PROXY_NONE:
  339. default:
  340. return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait);
  341. }
  342. }
  343. static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
  344. {
  345. u64 a0 = (u32)cmd, a1 = 0;
  346. int wait = 1000;
  347. int err;
  348. err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
  349. return !(err || a0);
  350. }
  351. int vnic_dev_fw_info(struct vnic_dev *vdev,
  352. struct vnic_devcmd_fw_info **fw_info)
  353. {
  354. u64 a0, a1 = 0;
  355. int wait = 1000;
  356. int err = 0;
  357. if (!vdev->fw_info) {
  358. vdev->fw_info = pci_zalloc_consistent(vdev->pdev,
  359. sizeof(struct vnic_devcmd_fw_info),
  360. &vdev->fw_info_pa);
  361. if (!vdev->fw_info)
  362. return -ENOMEM;
  363. a0 = vdev->fw_info_pa;
  364. a1 = sizeof(struct vnic_devcmd_fw_info);
  365. /* only get fw_info once and cache it */
  366. if (vnic_dev_capable(vdev, CMD_MCPU_FW_INFO))
  367. err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO,
  368. &a0, &a1, wait);
  369. else
  370. err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO_OLD,
  371. &a0, &a1, wait);
  372. }
  373. *fw_info = vdev->fw_info;
  374. return err;
  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_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
  395. {
  396. u64 a0, a1;
  397. int wait = 1000;
  398. if (!vdev->stats) {
  399. vdev->stats = pci_alloc_consistent(vdev->pdev,
  400. sizeof(struct vnic_stats), &vdev->stats_pa);
  401. if (!vdev->stats)
  402. return -ENOMEM;
  403. }
  404. *stats = vdev->stats;
  405. a0 = vdev->stats_pa;
  406. a1 = sizeof(struct vnic_stats);
  407. return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
  408. }
  409. int vnic_dev_close(struct vnic_dev *vdev)
  410. {
  411. u64 a0 = 0, a1 = 0;
  412. int wait = 1000;
  413. return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
  414. }
  415. int vnic_dev_enable_wait(struct vnic_dev *vdev)
  416. {
  417. u64 a0 = 0, a1 = 0;
  418. int wait = 1000;
  419. if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
  420. return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
  421. else
  422. return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
  423. }
  424. int vnic_dev_disable(struct vnic_dev *vdev)
  425. {
  426. u64 a0 = 0, a1 = 0;
  427. int wait = 1000;
  428. return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
  429. }
  430. int vnic_dev_open(struct vnic_dev *vdev, int arg)
  431. {
  432. u64 a0 = (u32)arg, a1 = 0;
  433. int wait = 1000;
  434. return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
  435. }
  436. int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
  437. {
  438. u64 a0 = 0, a1 = 0;
  439. int wait = 1000;
  440. int err;
  441. *done = 0;
  442. err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
  443. if (err)
  444. return err;
  445. *done = (a0 == 0);
  446. return 0;
  447. }
  448. static int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
  449. {
  450. u64 a0 = (u32)arg, a1 = 0;
  451. int wait = 1000;
  452. return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
  453. }
  454. static int vnic_dev_soft_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_SOFT_RESET_STATUS, &a0, &a1, wait);
  461. if (err)
  462. return err;
  463. *done = (a0 == 0);
  464. return 0;
  465. }
  466. int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
  467. {
  468. u64 a0 = (u32)arg, a1 = 0;
  469. int wait = 1000;
  470. int err;
  471. if (vnic_dev_capable(vdev, CMD_HANG_RESET)) {
  472. return vnic_dev_cmd(vdev, CMD_HANG_RESET,
  473. &a0, &a1, wait);
  474. } else {
  475. err = vnic_dev_soft_reset(vdev, arg);
  476. if (err)
  477. return err;
  478. return vnic_dev_init(vdev, 0);
  479. }
  480. }
  481. int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
  482. {
  483. u64 a0 = 0, a1 = 0;
  484. int wait = 1000;
  485. int err;
  486. *done = 0;
  487. if (vnic_dev_capable(vdev, CMD_HANG_RESET_STATUS)) {
  488. err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS,
  489. &a0, &a1, wait);
  490. if (err)
  491. return err;
  492. } else {
  493. return vnic_dev_soft_reset_done(vdev, done);
  494. }
  495. *done = (a0 == 0);
  496. return 0;
  497. }
  498. int vnic_dev_hang_notify(struct vnic_dev *vdev)
  499. {
  500. u64 a0, a1;
  501. int wait = 1000;
  502. return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
  503. }
  504. int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
  505. {
  506. u64 a0, a1;
  507. int wait = 1000;
  508. int err, i;
  509. for (i = 0; i < ETH_ALEN; i++)
  510. mac_addr[i] = 0;
  511. err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
  512. if (err)
  513. return err;
  514. for (i = 0; i < ETH_ALEN; i++)
  515. mac_addr[i] = ((u8 *)&a0)[i];
  516. return 0;
  517. }
  518. int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
  519. int broadcast, int promisc, int allmulti)
  520. {
  521. u64 a0, a1 = 0;
  522. int wait = 1000;
  523. int err;
  524. a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
  525. (multicast ? CMD_PFILTER_MULTICAST : 0) |
  526. (broadcast ? CMD_PFILTER_BROADCAST : 0) |
  527. (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
  528. (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
  529. err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
  530. if (err)
  531. pr_err("Can't set packet filter\n");
  532. return err;
  533. }
  534. int vnic_dev_add_addr(struct vnic_dev *vdev, const u8 *addr)
  535. {
  536. u64 a0 = 0, a1 = 0;
  537. int wait = 1000;
  538. int err;
  539. int i;
  540. for (i = 0; i < ETH_ALEN; i++)
  541. ((u8 *)&a0)[i] = addr[i];
  542. err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  543. if (err)
  544. pr_err("Can't add addr [%pM], %d\n", addr, err);
  545. return err;
  546. }
  547. int vnic_dev_del_addr(struct vnic_dev *vdev, const u8 *addr)
  548. {
  549. u64 a0 = 0, a1 = 0;
  550. int wait = 1000;
  551. int err;
  552. int i;
  553. for (i = 0; i < ETH_ALEN; i++)
  554. ((u8 *)&a0)[i] = addr[i];
  555. err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
  556. if (err)
  557. pr_err("Can't del addr [%pM], %d\n", addr, err);
  558. return err;
  559. }
  560. int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
  561. u8 ig_vlan_rewrite_mode)
  562. {
  563. u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
  564. int wait = 1000;
  565. if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
  566. return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
  567. &a0, &a1, wait);
  568. else
  569. return 0;
  570. }
  571. static int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
  572. void *notify_addr, dma_addr_t notify_pa, u16 intr)
  573. {
  574. u64 a0, a1;
  575. int wait = 1000;
  576. int r;
  577. memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
  578. vdev->notify = notify_addr;
  579. vdev->notify_pa = notify_pa;
  580. a0 = (u64)notify_pa;
  581. a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
  582. a1 += sizeof(struct vnic_devcmd_notify);
  583. r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  584. vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
  585. return r;
  586. }
  587. int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
  588. {
  589. void *notify_addr;
  590. dma_addr_t notify_pa;
  591. if (vdev->notify || vdev->notify_pa) {
  592. pr_err("notify block %p still allocated", vdev->notify);
  593. return -EINVAL;
  594. }
  595. notify_addr = pci_alloc_consistent(vdev->pdev,
  596. sizeof(struct vnic_devcmd_notify),
  597. &notify_pa);
  598. if (!notify_addr)
  599. return -ENOMEM;
  600. return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
  601. }
  602. static int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
  603. {
  604. u64 a0, a1;
  605. int wait = 1000;
  606. int err;
  607. a0 = 0; /* paddr = 0 to unset notify buffer */
  608. a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
  609. a1 += sizeof(struct vnic_devcmd_notify);
  610. err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  611. vdev->notify = NULL;
  612. vdev->notify_pa = 0;
  613. vdev->notify_sz = 0;
  614. return err;
  615. }
  616. int vnic_dev_notify_unset(struct vnic_dev *vdev)
  617. {
  618. if (vdev->notify) {
  619. pci_free_consistent(vdev->pdev,
  620. sizeof(struct vnic_devcmd_notify),
  621. vdev->notify,
  622. vdev->notify_pa);
  623. }
  624. return vnic_dev_notify_unsetcmd(vdev);
  625. }
  626. static int vnic_dev_notify_ready(struct vnic_dev *vdev)
  627. {
  628. u32 *words;
  629. unsigned int nwords = vdev->notify_sz / 4;
  630. unsigned int i;
  631. u32 csum;
  632. if (!vdev->notify || !vdev->notify_sz)
  633. return 0;
  634. do {
  635. csum = 0;
  636. memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
  637. words = (u32 *)&vdev->notify_copy;
  638. for (i = 1; i < nwords; i++)
  639. csum += words[i];
  640. } while (csum != words[0]);
  641. return 1;
  642. }
  643. int vnic_dev_init(struct vnic_dev *vdev, int arg)
  644. {
  645. u64 a0 = (u32)arg, a1 = 0;
  646. int wait = 1000;
  647. int r = 0;
  648. if (vnic_dev_capable(vdev, CMD_INIT))
  649. r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
  650. else {
  651. vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
  652. if (a0 & CMD_INITF_DEFAULT_MAC) {
  653. /* Emulate these for old CMD_INIT_v1 which
  654. * didn't pass a0 so no CMD_INITF_*.
  655. */
  656. vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
  657. vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  658. }
  659. }
  660. return r;
  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. void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
  669. {
  670. /* Default: hardware intr coal timer is in units of 1.5 usecs */
  671. vdev->intr_coal_timer_info.mul = 2;
  672. vdev->intr_coal_timer_info.div = 3;
  673. vdev->intr_coal_timer_info.max_usec =
  674. vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
  675. }
  676. int vnic_dev_intr_coal_timer_info(struct vnic_dev *vdev)
  677. {
  678. int wait = 1000;
  679. int err;
  680. memset(vdev->args, 0, sizeof(vdev->args));
  681. if (vnic_dev_capable(vdev, CMD_INTR_COAL_CONVERT))
  682. err = _vnic_dev_cmd(vdev, CMD_INTR_COAL_CONVERT, wait);
  683. else
  684. err = ERR_ECMDUNKNOWN;
  685. /* Use defaults when firmware doesn't support the devcmd at all or
  686. * supports it for only specific hardware
  687. */
  688. if ((err == ERR_ECMDUNKNOWN) ||
  689. (!err && !(vdev->args[0] && vdev->args[1] && vdev->args[2]))) {
  690. pr_warn("Using default conversion factor for interrupt coalesce timer\n");
  691. vnic_dev_intr_coal_timer_info_default(vdev);
  692. return 0;
  693. }
  694. if (!err) {
  695. vdev->intr_coal_timer_info.mul = (u32) vdev->args[0];
  696. vdev->intr_coal_timer_info.div = (u32) vdev->args[1];
  697. vdev->intr_coal_timer_info.max_usec = (u32) vdev->args[2];
  698. }
  699. return err;
  700. }
  701. int vnic_dev_link_status(struct vnic_dev *vdev)
  702. {
  703. if (!vnic_dev_notify_ready(vdev))
  704. return 0;
  705. return vdev->notify_copy.link_state;
  706. }
  707. u32 vnic_dev_port_speed(struct vnic_dev *vdev)
  708. {
  709. if (!vnic_dev_notify_ready(vdev))
  710. return 0;
  711. return vdev->notify_copy.port_speed;
  712. }
  713. u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
  714. {
  715. if (!vnic_dev_notify_ready(vdev))
  716. return 0;
  717. return vdev->notify_copy.msglvl;
  718. }
  719. u32 vnic_dev_mtu(struct vnic_dev *vdev)
  720. {
  721. if (!vnic_dev_notify_ready(vdev))
  722. return 0;
  723. return vdev->notify_copy.mtu;
  724. }
  725. void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
  726. enum vnic_dev_intr_mode intr_mode)
  727. {
  728. vdev->intr_mode = intr_mode;
  729. }
  730. enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
  731. struct vnic_dev *vdev)
  732. {
  733. return vdev->intr_mode;
  734. }
  735. u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
  736. {
  737. return (usec * vdev->intr_coal_timer_info.mul) /
  738. vdev->intr_coal_timer_info.div;
  739. }
  740. u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
  741. {
  742. return (hw_cycles * vdev->intr_coal_timer_info.div) /
  743. vdev->intr_coal_timer_info.mul;
  744. }
  745. u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
  746. {
  747. return vdev->intr_coal_timer_info.max_usec;
  748. }
  749. void vnic_dev_unregister(struct vnic_dev *vdev)
  750. {
  751. if (vdev) {
  752. if (vdev->notify)
  753. pci_free_consistent(vdev->pdev,
  754. sizeof(struct vnic_devcmd_notify),
  755. vdev->notify,
  756. vdev->notify_pa);
  757. if (vdev->stats)
  758. pci_free_consistent(vdev->pdev,
  759. sizeof(struct vnic_stats),
  760. vdev->stats, vdev->stats_pa);
  761. if (vdev->fw_info)
  762. pci_free_consistent(vdev->pdev,
  763. sizeof(struct vnic_devcmd_fw_info),
  764. vdev->fw_info, vdev->fw_info_pa);
  765. kfree(vdev);
  766. }
  767. }
  768. EXPORT_SYMBOL(vnic_dev_unregister);
  769. struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
  770. void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar,
  771. unsigned int num_bars)
  772. {
  773. if (!vdev) {
  774. vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
  775. if (!vdev)
  776. return NULL;
  777. }
  778. vdev->priv = priv;
  779. vdev->pdev = pdev;
  780. if (vnic_dev_discover_res(vdev, bar, num_bars))
  781. goto err_out;
  782. vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
  783. if (!vdev->devcmd)
  784. goto err_out;
  785. return vdev;
  786. err_out:
  787. vnic_dev_unregister(vdev);
  788. return NULL;
  789. }
  790. EXPORT_SYMBOL(vnic_dev_register);
  791. struct pci_dev *vnic_dev_get_pdev(struct vnic_dev *vdev)
  792. {
  793. return vdev->pdev;
  794. }
  795. EXPORT_SYMBOL(vnic_dev_get_pdev);
  796. int vnic_dev_init_prov2(struct vnic_dev *vdev, u8 *buf, u32 len)
  797. {
  798. u64 a0, a1 = len;
  799. int wait = 1000;
  800. dma_addr_t prov_pa;
  801. void *prov_buf;
  802. int ret;
  803. prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
  804. if (!prov_buf)
  805. return -ENOMEM;
  806. memcpy(prov_buf, buf, len);
  807. a0 = prov_pa;
  808. ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO2, &a0, &a1, wait);
  809. pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
  810. return ret;
  811. }
  812. int vnic_dev_enable2(struct vnic_dev *vdev, int active)
  813. {
  814. u64 a0, a1 = 0;
  815. int wait = 1000;
  816. a0 = (active ? CMD_ENABLE2_ACTIVE : 0);
  817. return vnic_dev_cmd(vdev, CMD_ENABLE2, &a0, &a1, wait);
  818. }
  819. static int vnic_dev_cmd_status(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  820. int *status)
  821. {
  822. u64 a0 = cmd, a1 = 0;
  823. int wait = 1000;
  824. int ret;
  825. ret = vnic_dev_cmd(vdev, CMD_STATUS, &a0, &a1, wait);
  826. if (!ret)
  827. *status = (int)a0;
  828. return ret;
  829. }
  830. int vnic_dev_enable2_done(struct vnic_dev *vdev, int *status)
  831. {
  832. return vnic_dev_cmd_status(vdev, CMD_ENABLE2, status);
  833. }
  834. int vnic_dev_deinit_done(struct vnic_dev *vdev, int *status)
  835. {
  836. return vnic_dev_cmd_status(vdev, CMD_DEINIT, status);
  837. }
  838. int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
  839. {
  840. u64 a0, a1;
  841. int wait = 1000;
  842. int i;
  843. for (i = 0; i < ETH_ALEN; i++)
  844. ((u8 *)&a0)[i] = mac_addr[i];
  845. return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait);
  846. }
  847. /* vnic_dev_classifier: Add/Delete classifier entries
  848. * @vdev: vdev of the device
  849. * @cmd: CLSF_ADD for Add filter
  850. * CLSF_DEL for Delete filter
  851. * @entry: In case of ADD filter, the caller passes the RQ number in this
  852. * variable.
  853. *
  854. * This function stores the filter_id returned by the firmware in the
  855. * same variable before return;
  856. *
  857. * In case of DEL filter, the caller passes the RQ number. Return
  858. * value is irrelevant.
  859. * @data: filter data
  860. */
  861. int vnic_dev_classifier(struct vnic_dev *vdev, u8 cmd, u16 *entry,
  862. struct filter *data)
  863. {
  864. u64 a0, a1;
  865. int wait = 1000;
  866. dma_addr_t tlv_pa;
  867. int ret = -EINVAL;
  868. struct filter_tlv *tlv, *tlv_va;
  869. struct filter_action *action;
  870. u64 tlv_size;
  871. if (cmd == CLSF_ADD) {
  872. tlv_size = sizeof(struct filter) +
  873. sizeof(struct filter_action) +
  874. 2 * sizeof(struct filter_tlv);
  875. tlv_va = pci_alloc_consistent(vdev->pdev, tlv_size, &tlv_pa);
  876. if (!tlv_va)
  877. return -ENOMEM;
  878. tlv = tlv_va;
  879. a0 = tlv_pa;
  880. a1 = tlv_size;
  881. memset(tlv, 0, tlv_size);
  882. tlv->type = CLSF_TLV_FILTER;
  883. tlv->length = sizeof(struct filter);
  884. *(struct filter *)&tlv->val = *data;
  885. tlv = (struct filter_tlv *)((char *)tlv +
  886. sizeof(struct filter_tlv) +
  887. sizeof(struct filter));
  888. tlv->type = CLSF_TLV_ACTION;
  889. tlv->length = sizeof(struct filter_action);
  890. action = (struct filter_action *)&tlv->val;
  891. action->type = FILTER_ACTION_RQ_STEERING;
  892. action->u.rq_idx = *entry;
  893. ret = vnic_dev_cmd(vdev, CMD_ADD_FILTER, &a0, &a1, wait);
  894. *entry = (u16)a0;
  895. pci_free_consistent(vdev->pdev, tlv_size, tlv_va, tlv_pa);
  896. } else if (cmd == CLSF_DEL) {
  897. a0 = *entry;
  898. ret = vnic_dev_cmd(vdev, CMD_DEL_FILTER, &a0, &a1, wait);
  899. }
  900. return ret;
  901. }