vnic_dev.c 21 KB

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