vnic_dev.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
  155. unsigned int index)
  156. {
  157. if (!vdev->res[type].vaddr)
  158. return NULL;
  159. switch (type) {
  160. case RES_TYPE_WQ:
  161. case RES_TYPE_RQ:
  162. case RES_TYPE_CQ:
  163. case RES_TYPE_INTR_CTRL:
  164. return (char __iomem *)vdev->res[type].vaddr +
  165. index * VNIC_RES_STRIDE;
  166. default:
  167. return (char __iomem *)vdev->res[type].vaddr;
  168. }
  169. }
  170. static unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
  171. unsigned int desc_count, unsigned int desc_size)
  172. {
  173. /* The base address of the desc rings must be 512 byte aligned.
  174. * Descriptor count is aligned to groups of 32 descriptors. A
  175. * count of 0 means the maximum 4096 descriptors. Descriptor
  176. * size is aligned to 16 bytes.
  177. */
  178. unsigned int count_align = 32;
  179. unsigned int desc_align = 16;
  180. ring->base_align = 512;
  181. if (desc_count == 0)
  182. desc_count = 4096;
  183. ring->desc_count = ALIGN(desc_count, count_align);
  184. ring->desc_size = ALIGN(desc_size, desc_align);
  185. ring->size = ring->desc_count * ring->desc_size;
  186. ring->size_unaligned = ring->size + ring->base_align;
  187. return ring->size_unaligned;
  188. }
  189. void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
  190. {
  191. memset(ring->descs, 0, ring->size);
  192. }
  193. int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
  194. unsigned int desc_count, unsigned int desc_size)
  195. {
  196. vnic_dev_desc_ring_size(ring, desc_count, desc_size);
  197. ring->descs_unaligned = pci_alloc_consistent(vdev->pdev,
  198. ring->size_unaligned,
  199. &ring->base_addr_unaligned);
  200. if (!ring->descs_unaligned) {
  201. pr_err("Failed to allocate ring (size=%d), aborting\n",
  202. (int)ring->size);
  203. return -ENOMEM;
  204. }
  205. ring->base_addr = ALIGN(ring->base_addr_unaligned,
  206. ring->base_align);
  207. ring->descs = (u8 *)ring->descs_unaligned +
  208. (ring->base_addr - ring->base_addr_unaligned);
  209. vnic_dev_clear_desc_ring(ring);
  210. ring->desc_avail = ring->desc_count - 1;
  211. return 0;
  212. }
  213. void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
  214. {
  215. if (ring->descs) {
  216. pci_free_consistent(vdev->pdev,
  217. ring->size_unaligned,
  218. ring->descs_unaligned,
  219. ring->base_addr_unaligned);
  220. ring->descs = NULL;
  221. }
  222. }
  223. static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  224. int wait)
  225. {
  226. struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
  227. unsigned int i;
  228. int delay;
  229. u32 status;
  230. int err;
  231. status = ioread32(&devcmd->status);
  232. if (status == 0xFFFFFFFF) {
  233. /* PCI-e target device is gone */
  234. return -ENODEV;
  235. }
  236. if (status & STAT_BUSY) {
  237. pr_err("Busy devcmd %d\n", _CMD_N(cmd));
  238. return -EBUSY;
  239. }
  240. if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
  241. for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
  242. writeq(vdev->args[i], &devcmd->args[i]);
  243. wmb();
  244. }
  245. iowrite32(cmd, &devcmd->cmd);
  246. if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
  247. return 0;
  248. for (delay = 0; delay < wait; delay++) {
  249. udelay(100);
  250. status = ioread32(&devcmd->status);
  251. if (status == 0xFFFFFFFF) {
  252. /* PCI-e target device is gone */
  253. return -ENODEV;
  254. }
  255. if (!(status & STAT_BUSY)) {
  256. if (status & STAT_ERROR) {
  257. err = (int)readq(&devcmd->args[0]);
  258. if (err != ERR_ECMDUNKNOWN ||
  259. cmd != CMD_CAPABILITY)
  260. pr_err("Error %d devcmd %d\n",
  261. err, _CMD_N(cmd));
  262. return err;
  263. }
  264. if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
  265. rmb();
  266. for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
  267. vdev->args[i] = readq(&devcmd->args[i]);
  268. }
  269. return 0;
  270. }
  271. }
  272. pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
  273. return -ETIMEDOUT;
  274. }
  275. static int vnic_dev_cmd_proxy(struct vnic_dev *vdev,
  276. enum vnic_devcmd_cmd proxy_cmd, enum vnic_devcmd_cmd cmd,
  277. 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;
  283. vdev->args[1] = cmd;
  284. vdev->args[2] = *a0;
  285. vdev->args[3] = *a1;
  286. err = _vnic_dev_cmd(vdev, proxy_cmd, 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_index_start(struct vnic_dev *vdev, u16 index)
  313. {
  314. vdev->proxy = PROXY_BY_INDEX;
  315. vdev->proxy_index = index;
  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_INDEX:
  328. return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_INDEX, cmd,
  329. a0, a1, wait);
  330. case PROXY_BY_BDF:
  331. return vnic_dev_cmd_proxy(vdev, CMD_PROXY_BY_BDF, cmd,
  332. a0, a1, wait);
  333. case PROXY_NONE:
  334. default:
  335. return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait);
  336. }
  337. }
  338. static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
  339. {
  340. u64 a0 = (u32)cmd, a1 = 0;
  341. int wait = 1000;
  342. int err;
  343. err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
  344. return !(err || a0);
  345. }
  346. int vnic_dev_fw_info(struct vnic_dev *vdev,
  347. struct vnic_devcmd_fw_info **fw_info)
  348. {
  349. u64 a0, a1 = 0;
  350. int wait = 1000;
  351. int err = 0;
  352. if (!vdev->fw_info) {
  353. vdev->fw_info = pci_alloc_consistent(vdev->pdev,
  354. sizeof(struct vnic_devcmd_fw_info),
  355. &vdev->fw_info_pa);
  356. if (!vdev->fw_info)
  357. return -ENOMEM;
  358. memset(vdev->fw_info, 0, sizeof(struct vnic_devcmd_fw_info));
  359. a0 = vdev->fw_info_pa;
  360. a1 = sizeof(struct vnic_devcmd_fw_info);
  361. /* only get fw_info once and cache it */
  362. if (vnic_dev_capable(vdev, CMD_MCPU_FW_INFO))
  363. err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO,
  364. &a0, &a1, wait);
  365. else
  366. err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO_OLD,
  367. &a0, &a1, wait);
  368. }
  369. *fw_info = vdev->fw_info;
  370. return err;
  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_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
  391. {
  392. u64 a0, a1;
  393. int wait = 1000;
  394. if (!vdev->stats) {
  395. vdev->stats = pci_alloc_consistent(vdev->pdev,
  396. sizeof(struct vnic_stats), &vdev->stats_pa);
  397. if (!vdev->stats)
  398. return -ENOMEM;
  399. }
  400. *stats = vdev->stats;
  401. a0 = vdev->stats_pa;
  402. a1 = sizeof(struct vnic_stats);
  403. return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
  404. }
  405. int vnic_dev_close(struct vnic_dev *vdev)
  406. {
  407. u64 a0 = 0, a1 = 0;
  408. int wait = 1000;
  409. return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
  410. }
  411. int vnic_dev_enable_wait(struct vnic_dev *vdev)
  412. {
  413. u64 a0 = 0, a1 = 0;
  414. int wait = 1000;
  415. if (vnic_dev_capable(vdev, CMD_ENABLE_WAIT))
  416. return vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
  417. else
  418. return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
  419. }
  420. int vnic_dev_disable(struct vnic_dev *vdev)
  421. {
  422. u64 a0 = 0, a1 = 0;
  423. int wait = 1000;
  424. return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
  425. }
  426. int vnic_dev_open(struct vnic_dev *vdev, int arg)
  427. {
  428. u64 a0 = (u32)arg, a1 = 0;
  429. int wait = 1000;
  430. return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
  431. }
  432. int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
  433. {
  434. u64 a0 = 0, a1 = 0;
  435. int wait = 1000;
  436. int err;
  437. *done = 0;
  438. err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
  439. if (err)
  440. return err;
  441. *done = (a0 == 0);
  442. return 0;
  443. }
  444. static int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
  445. {
  446. u64 a0 = (u32)arg, a1 = 0;
  447. int wait = 1000;
  448. return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
  449. }
  450. static int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
  451. {
  452. u64 a0 = 0, a1 = 0;
  453. int wait = 1000;
  454. int err;
  455. *done = 0;
  456. err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
  457. if (err)
  458. return err;
  459. *done = (a0 == 0);
  460. return 0;
  461. }
  462. int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
  463. {
  464. u64 a0 = (u32)arg, a1 = 0;
  465. int wait = 1000;
  466. int err;
  467. if (vnic_dev_capable(vdev, CMD_HANG_RESET)) {
  468. return vnic_dev_cmd(vdev, CMD_HANG_RESET,
  469. &a0, &a1, wait);
  470. } else {
  471. err = vnic_dev_soft_reset(vdev, arg);
  472. if (err)
  473. return err;
  474. return vnic_dev_init(vdev, 0);
  475. }
  476. }
  477. int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
  478. {
  479. u64 a0 = 0, a1 = 0;
  480. int wait = 1000;
  481. int err;
  482. *done = 0;
  483. if (vnic_dev_capable(vdev, CMD_HANG_RESET_STATUS)) {
  484. err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS,
  485. &a0, &a1, wait);
  486. if (err)
  487. return err;
  488. } else {
  489. return vnic_dev_soft_reset_done(vdev, done);
  490. }
  491. *done = (a0 == 0);
  492. return 0;
  493. }
  494. int vnic_dev_hang_notify(struct vnic_dev *vdev)
  495. {
  496. u64 a0, a1;
  497. int wait = 1000;
  498. return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
  499. }
  500. int vnic_dev_get_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
  501. {
  502. u64 a0, a1;
  503. int wait = 1000;
  504. int err, i;
  505. for (i = 0; i < ETH_ALEN; i++)
  506. mac_addr[i] = 0;
  507. err = vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
  508. if (err)
  509. return err;
  510. for (i = 0; i < ETH_ALEN; i++)
  511. mac_addr[i] = ((u8 *)&a0)[i];
  512. return 0;
  513. }
  514. int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
  515. int broadcast, int promisc, int allmulti)
  516. {
  517. u64 a0, a1 = 0;
  518. int wait = 1000;
  519. int err;
  520. a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
  521. (multicast ? CMD_PFILTER_MULTICAST : 0) |
  522. (broadcast ? CMD_PFILTER_BROADCAST : 0) |
  523. (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
  524. (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
  525. err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
  526. if (err)
  527. pr_err("Can't set packet filter\n");
  528. return err;
  529. }
  530. int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
  531. {
  532. u64 a0 = 0, a1 = 0;
  533. int wait = 1000;
  534. int err;
  535. int i;
  536. for (i = 0; i < ETH_ALEN; i++)
  537. ((u8 *)&a0)[i] = addr[i];
  538. err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  539. if (err)
  540. pr_err("Can't add addr [%pM], %d\n", addr, err);
  541. return err;
  542. }
  543. int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
  544. {
  545. u64 a0 = 0, a1 = 0;
  546. int wait = 1000;
  547. int err;
  548. int i;
  549. for (i = 0; i < ETH_ALEN; i++)
  550. ((u8 *)&a0)[i] = addr[i];
  551. err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
  552. if (err)
  553. pr_err("Can't del addr [%pM], %d\n", addr, err);
  554. return err;
  555. }
  556. int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
  557. u8 ig_vlan_rewrite_mode)
  558. {
  559. u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
  560. int wait = 1000;
  561. if (vnic_dev_capable(vdev, CMD_IG_VLAN_REWRITE_MODE))
  562. return vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE,
  563. &a0, &a1, wait);
  564. else
  565. return 0;
  566. }
  567. static int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
  568. void *notify_addr, dma_addr_t notify_pa, u16 intr)
  569. {
  570. u64 a0, a1;
  571. int wait = 1000;
  572. int r;
  573. memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
  574. vdev->notify = notify_addr;
  575. vdev->notify_pa = notify_pa;
  576. a0 = (u64)notify_pa;
  577. a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
  578. a1 += sizeof(struct vnic_devcmd_notify);
  579. r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  580. vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
  581. return r;
  582. }
  583. int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
  584. {
  585. void *notify_addr;
  586. dma_addr_t notify_pa;
  587. if (vdev->notify || vdev->notify_pa) {
  588. pr_err("notify block %p still allocated", vdev->notify);
  589. return -EINVAL;
  590. }
  591. notify_addr = pci_alloc_consistent(vdev->pdev,
  592. sizeof(struct vnic_devcmd_notify),
  593. &notify_pa);
  594. if (!notify_addr)
  595. return -ENOMEM;
  596. return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
  597. }
  598. static int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
  599. {
  600. u64 a0, a1;
  601. int wait = 1000;
  602. int err;
  603. a0 = 0; /* paddr = 0 to unset notify buffer */
  604. a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
  605. a1 += sizeof(struct vnic_devcmd_notify);
  606. err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
  607. vdev->notify = NULL;
  608. vdev->notify_pa = 0;
  609. vdev->notify_sz = 0;
  610. return err;
  611. }
  612. int vnic_dev_notify_unset(struct vnic_dev *vdev)
  613. {
  614. if (vdev->notify) {
  615. pci_free_consistent(vdev->pdev,
  616. sizeof(struct vnic_devcmd_notify),
  617. vdev->notify,
  618. vdev->notify_pa);
  619. }
  620. return vnic_dev_notify_unsetcmd(vdev);
  621. }
  622. static int vnic_dev_notify_ready(struct vnic_dev *vdev)
  623. {
  624. u32 *words;
  625. unsigned int nwords = vdev->notify_sz / 4;
  626. unsigned int i;
  627. u32 csum;
  628. if (!vdev->notify || !vdev->notify_sz)
  629. return 0;
  630. do {
  631. csum = 0;
  632. memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
  633. words = (u32 *)&vdev->notify_copy;
  634. for (i = 1; i < nwords; i++)
  635. csum += words[i];
  636. } while (csum != words[0]);
  637. return 1;
  638. }
  639. int vnic_dev_init(struct vnic_dev *vdev, int arg)
  640. {
  641. u64 a0 = (u32)arg, a1 = 0;
  642. int wait = 1000;
  643. int r = 0;
  644. if (vnic_dev_capable(vdev, CMD_INIT))
  645. r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
  646. else {
  647. vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
  648. if (a0 & CMD_INITF_DEFAULT_MAC) {
  649. /* Emulate these for old CMD_INIT_v1 which
  650. * didn't pass a0 so no CMD_INITF_*.
  651. */
  652. vnic_dev_cmd(vdev, CMD_GET_MAC_ADDR, &a0, &a1, wait);
  653. vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
  654. }
  655. }
  656. return r;
  657. }
  658. int vnic_dev_deinit(struct vnic_dev *vdev)
  659. {
  660. u64 a0 = 0, a1 = 0;
  661. int wait = 1000;
  662. return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
  663. }
  664. void vnic_dev_intr_coal_timer_info_default(struct vnic_dev *vdev)
  665. {
  666. /* Default: hardware intr coal timer is in units of 1.5 usecs */
  667. vdev->intr_coal_timer_info.mul = 2;
  668. vdev->intr_coal_timer_info.div = 3;
  669. vdev->intr_coal_timer_info.max_usec =
  670. vnic_dev_intr_coal_timer_hw_to_usec(vdev, 0xffff);
  671. }
  672. int vnic_dev_intr_coal_timer_info(struct vnic_dev *vdev)
  673. {
  674. int wait = 1000;
  675. int err;
  676. memset(vdev->args, 0, sizeof(vdev->args));
  677. if (vnic_dev_capable(vdev, CMD_INTR_COAL_CONVERT))
  678. err = _vnic_dev_cmd(vdev, CMD_INTR_COAL_CONVERT, wait);
  679. else
  680. err = ERR_ECMDUNKNOWN;
  681. /* Use defaults when firmware doesn't support the devcmd at all or
  682. * supports it for only specific hardware
  683. */
  684. if ((err == ERR_ECMDUNKNOWN) ||
  685. (!err && !(vdev->args[0] && vdev->args[1] && vdev->args[2]))) {
  686. pr_warning("Using default conversion factor for "
  687. "interrupt coalesce timer\n");
  688. vnic_dev_intr_coal_timer_info_default(vdev);
  689. return 0;
  690. }
  691. if (!err) {
  692. vdev->intr_coal_timer_info.mul = (u32) vdev->args[0];
  693. vdev->intr_coal_timer_info.div = (u32) vdev->args[1];
  694. vdev->intr_coal_timer_info.max_usec = (u32) vdev->args[2];
  695. }
  696. return err;
  697. }
  698. int vnic_dev_link_status(struct vnic_dev *vdev)
  699. {
  700. if (!vnic_dev_notify_ready(vdev))
  701. return 0;
  702. return vdev->notify_copy.link_state;
  703. }
  704. u32 vnic_dev_port_speed(struct vnic_dev *vdev)
  705. {
  706. if (!vnic_dev_notify_ready(vdev))
  707. return 0;
  708. return vdev->notify_copy.port_speed;
  709. }
  710. u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
  711. {
  712. if (!vnic_dev_notify_ready(vdev))
  713. return 0;
  714. return vdev->notify_copy.msglvl;
  715. }
  716. u32 vnic_dev_mtu(struct vnic_dev *vdev)
  717. {
  718. if (!vnic_dev_notify_ready(vdev))
  719. return 0;
  720. return vdev->notify_copy.mtu;
  721. }
  722. void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
  723. enum vnic_dev_intr_mode intr_mode)
  724. {
  725. vdev->intr_mode = intr_mode;
  726. }
  727. enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
  728. struct vnic_dev *vdev)
  729. {
  730. return vdev->intr_mode;
  731. }
  732. u32 vnic_dev_intr_coal_timer_usec_to_hw(struct vnic_dev *vdev, u32 usec)
  733. {
  734. return (usec * vdev->intr_coal_timer_info.mul) /
  735. vdev->intr_coal_timer_info.div;
  736. }
  737. u32 vnic_dev_intr_coal_timer_hw_to_usec(struct vnic_dev *vdev, u32 hw_cycles)
  738. {
  739. return (hw_cycles * vdev->intr_coal_timer_info.div) /
  740. vdev->intr_coal_timer_info.mul;
  741. }
  742. u32 vnic_dev_get_intr_coal_timer_max(struct vnic_dev *vdev)
  743. {
  744. return vdev->intr_coal_timer_info.max_usec;
  745. }
  746. void vnic_dev_unregister(struct vnic_dev *vdev)
  747. {
  748. if (vdev) {
  749. if (vdev->notify)
  750. pci_free_consistent(vdev->pdev,
  751. sizeof(struct vnic_devcmd_notify),
  752. vdev->notify,
  753. vdev->notify_pa);
  754. if (vdev->stats)
  755. pci_free_consistent(vdev->pdev,
  756. sizeof(struct vnic_stats),
  757. vdev->stats, vdev->stats_pa);
  758. if (vdev->fw_info)
  759. pci_free_consistent(vdev->pdev,
  760. sizeof(struct vnic_devcmd_fw_info),
  761. vdev->fw_info, vdev->fw_info_pa);
  762. kfree(vdev);
  763. }
  764. }
  765. struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
  766. void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar,
  767. unsigned int num_bars)
  768. {
  769. if (!vdev) {
  770. vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
  771. if (!vdev)
  772. return NULL;
  773. }
  774. vdev->priv = priv;
  775. vdev->pdev = pdev;
  776. if (vnic_dev_discover_res(vdev, bar, num_bars))
  777. goto err_out;
  778. vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
  779. if (!vdev->devcmd)
  780. goto err_out;
  781. return vdev;
  782. err_out:
  783. vnic_dev_unregister(vdev);
  784. return NULL;
  785. }
  786. int vnic_dev_init_prov2(struct vnic_dev *vdev, u8 *buf, u32 len)
  787. {
  788. u64 a0, a1 = len;
  789. int wait = 1000;
  790. dma_addr_t prov_pa;
  791. void *prov_buf;
  792. int ret;
  793. prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
  794. if (!prov_buf)
  795. return -ENOMEM;
  796. memcpy(prov_buf, buf, len);
  797. a0 = prov_pa;
  798. ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO2, &a0, &a1, wait);
  799. pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
  800. return ret;
  801. }
  802. int vnic_dev_enable2(struct vnic_dev *vdev, int active)
  803. {
  804. u64 a0, a1 = 0;
  805. int wait = 1000;
  806. a0 = (active ? CMD_ENABLE2_ACTIVE : 0);
  807. return vnic_dev_cmd(vdev, CMD_ENABLE2, &a0, &a1, wait);
  808. }
  809. static int vnic_dev_cmd_status(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
  810. int *status)
  811. {
  812. u64 a0 = cmd, a1 = 0;
  813. int wait = 1000;
  814. int ret;
  815. ret = vnic_dev_cmd(vdev, CMD_STATUS, &a0, &a1, wait);
  816. if (!ret)
  817. *status = (int)a0;
  818. return ret;
  819. }
  820. int vnic_dev_enable2_done(struct vnic_dev *vdev, int *status)
  821. {
  822. return vnic_dev_cmd_status(vdev, CMD_ENABLE2, status);
  823. }
  824. int vnic_dev_deinit_done(struct vnic_dev *vdev, int *status)
  825. {
  826. return vnic_dev_cmd_status(vdev, CMD_DEINIT, status);
  827. }
  828. int vnic_dev_set_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
  829. {
  830. u64 a0, a1;
  831. int wait = 1000;
  832. int i;
  833. for (i = 0; i < ETH_ALEN; i++)
  834. ((u8 *)&a0)[i] = mac_addr[i];
  835. return vnic_dev_cmd(vdev, CMD_SET_MAC_ADDR, &a0, &a1, wait);
  836. }