hns_ae_adapt.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /*
  2. * Copyright (c) 2014-2015 Hisilicon Limited.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/etherdevice.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/spinlock.h>
  12. #include "hnae.h"
  13. #include "hns_dsaf_mac.h"
  14. #include "hns_dsaf_main.h"
  15. #include "hns_dsaf_ppe.h"
  16. #include "hns_dsaf_rcb.h"
  17. #define AE_NAME_PORT_ID_IDX 6
  18. static struct hns_mac_cb *hns_get_mac_cb(struct hnae_handle *handle)
  19. {
  20. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  21. return vf_cb->mac_cb;
  22. }
  23. static struct dsaf_device *hns_ae_get_dsaf_dev(struct hnae_ae_dev *dev)
  24. {
  25. return container_of(dev, struct dsaf_device, ae_dev);
  26. }
  27. static struct hns_ppe_cb *hns_get_ppe_cb(struct hnae_handle *handle)
  28. {
  29. int ppe_index;
  30. struct ppe_common_cb *ppe_comm;
  31. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  32. ppe_comm = vf_cb->dsaf_dev->ppe_common[0];
  33. ppe_index = vf_cb->port_index;
  34. return &ppe_comm->ppe_cb[ppe_index];
  35. }
  36. static int hns_ae_get_q_num_per_vf(
  37. struct dsaf_device *dsaf_dev, int port)
  38. {
  39. return dsaf_dev->rcb_common[0]->max_q_per_vf;
  40. }
  41. static int hns_ae_get_vf_num_per_port(
  42. struct dsaf_device *dsaf_dev, int port)
  43. {
  44. return dsaf_dev->rcb_common[0]->max_vfn;
  45. }
  46. static struct ring_pair_cb *hns_ae_get_base_ring_pair(
  47. struct dsaf_device *dsaf_dev, int port)
  48. {
  49. struct rcb_common_cb *rcb_comm = dsaf_dev->rcb_common[0];
  50. int q_num = rcb_comm->max_q_per_vf;
  51. int vf_num = rcb_comm->max_vfn;
  52. return &rcb_comm->ring_pair_cb[port * q_num * vf_num];
  53. }
  54. static struct ring_pair_cb *hns_ae_get_ring_pair(struct hnae_queue *q)
  55. {
  56. return container_of(q, struct ring_pair_cb, q);
  57. }
  58. static struct hnae_handle *hns_ae_get_handle(struct hnae_ae_dev *dev,
  59. u32 port_id)
  60. {
  61. int vfnum_per_port;
  62. int qnum_per_vf;
  63. int i;
  64. struct dsaf_device *dsaf_dev;
  65. struct hnae_handle *ae_handle;
  66. struct ring_pair_cb *ring_pair_cb;
  67. struct hnae_vf_cb *vf_cb;
  68. dsaf_dev = hns_ae_get_dsaf_dev(dev);
  69. ring_pair_cb = hns_ae_get_base_ring_pair(dsaf_dev, port_id);
  70. vfnum_per_port = hns_ae_get_vf_num_per_port(dsaf_dev, port_id);
  71. qnum_per_vf = hns_ae_get_q_num_per_vf(dsaf_dev, port_id);
  72. vf_cb = kzalloc(sizeof(*vf_cb) +
  73. qnum_per_vf * sizeof(struct hnae_queue *), GFP_KERNEL);
  74. if (unlikely(!vf_cb)) {
  75. dev_err(dsaf_dev->dev, "malloc vf_cb fail!\n");
  76. ae_handle = ERR_PTR(-ENOMEM);
  77. goto handle_err;
  78. }
  79. ae_handle = &vf_cb->ae_handle;
  80. /* ae_handle Init */
  81. ae_handle->owner_dev = dsaf_dev->dev;
  82. ae_handle->dev = dev;
  83. ae_handle->q_num = qnum_per_vf;
  84. ae_handle->coal_param = HNAE_LOWEST_LATENCY_COAL_PARAM;
  85. /* find ring pair, and set vf id*/
  86. for (ae_handle->vf_id = 0;
  87. ae_handle->vf_id < vfnum_per_port; ae_handle->vf_id++) {
  88. if (!ring_pair_cb->used_by_vf)
  89. break;
  90. ring_pair_cb += qnum_per_vf;
  91. }
  92. if (ae_handle->vf_id >= vfnum_per_port) {
  93. dev_err(dsaf_dev->dev, "malloc queue fail!\n");
  94. ae_handle = ERR_PTR(-EINVAL);
  95. goto vf_id_err;
  96. }
  97. ae_handle->qs = (struct hnae_queue **)(&ae_handle->qs + 1);
  98. for (i = 0; i < qnum_per_vf; i++) {
  99. ae_handle->qs[i] = &ring_pair_cb->q;
  100. ae_handle->qs[i]->rx_ring.q = ae_handle->qs[i];
  101. ae_handle->qs[i]->tx_ring.q = ae_handle->qs[i];
  102. ring_pair_cb->used_by_vf = 1;
  103. ring_pair_cb++;
  104. }
  105. vf_cb->dsaf_dev = dsaf_dev;
  106. vf_cb->port_index = port_id;
  107. vf_cb->mac_cb = dsaf_dev->mac_cb[port_id];
  108. ae_handle->phy_if = vf_cb->mac_cb->phy_if;
  109. ae_handle->phy_dev = vf_cb->mac_cb->phy_dev;
  110. ae_handle->if_support = vf_cb->mac_cb->if_support;
  111. ae_handle->port_type = vf_cb->mac_cb->mac_type;
  112. ae_handle->media_type = vf_cb->mac_cb->media_type;
  113. ae_handle->dport_id = port_id;
  114. return ae_handle;
  115. vf_id_err:
  116. kfree(vf_cb);
  117. handle_err:
  118. return ae_handle;
  119. }
  120. static void hns_ae_put_handle(struct hnae_handle *handle)
  121. {
  122. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  123. int i;
  124. for (i = 0; i < handle->q_num; i++)
  125. hns_ae_get_ring_pair(handle->qs[i])->used_by_vf = 0;
  126. kfree(vf_cb);
  127. }
  128. static int hns_ae_wait_flow_down(struct hnae_handle *handle)
  129. {
  130. struct dsaf_device *dsaf_dev;
  131. struct hns_ppe_cb *ppe_cb;
  132. struct hnae_vf_cb *vf_cb;
  133. int ret;
  134. int i;
  135. for (i = 0; i < handle->q_num; i++) {
  136. ret = hns_rcb_wait_tx_ring_clean(handle->qs[i]);
  137. if (ret)
  138. return ret;
  139. }
  140. ppe_cb = hns_get_ppe_cb(handle);
  141. ret = hns_ppe_wait_tx_fifo_clean(ppe_cb);
  142. if (ret)
  143. return ret;
  144. dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
  145. if (!dsaf_dev)
  146. return -EINVAL;
  147. ret = hns_dsaf_wait_pkt_clean(dsaf_dev, handle->dport_id);
  148. if (ret)
  149. return ret;
  150. vf_cb = hns_ae_get_vf_cb(handle);
  151. ret = hns_mac_wait_fifo_clean(vf_cb->mac_cb);
  152. if (ret)
  153. return ret;
  154. mdelay(10);
  155. return 0;
  156. }
  157. static void hns_ae_ring_enable_all(struct hnae_handle *handle, int val)
  158. {
  159. int q_num = handle->q_num;
  160. int i;
  161. for (i = 0; i < q_num; i++)
  162. hns_rcb_ring_enable_hw(handle->qs[i], val);
  163. }
  164. static void hns_ae_init_queue(struct hnae_queue *q)
  165. {
  166. struct ring_pair_cb *ring =
  167. container_of(q, struct ring_pair_cb, q);
  168. hns_rcb_init_hw(ring);
  169. }
  170. static void hns_ae_fini_queue(struct hnae_queue *q)
  171. {
  172. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(q->handle);
  173. if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
  174. hns_rcb_reset_ring_hw(q);
  175. }
  176. static int hns_ae_set_mac_address(struct hnae_handle *handle, void *p)
  177. {
  178. int ret;
  179. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  180. if (!p || !is_valid_ether_addr((const u8 *)p)) {
  181. dev_err(handle->owner_dev, "is not valid ether addr !\n");
  182. return -EADDRNOTAVAIL;
  183. }
  184. ret = hns_mac_change_vf_addr(mac_cb, handle->vf_id, p);
  185. if (ret != 0) {
  186. dev_err(handle->owner_dev,
  187. "set_mac_address fail, ret=%d!\n", ret);
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static int hns_ae_add_uc_address(struct hnae_handle *handle,
  193. const unsigned char *addr)
  194. {
  195. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  196. if (mac_cb->mac_type != HNAE_PORT_SERVICE)
  197. return -ENOSPC;
  198. return hns_mac_add_uc_addr(mac_cb, handle->vf_id, addr);
  199. }
  200. static int hns_ae_rm_uc_address(struct hnae_handle *handle,
  201. const unsigned char *addr)
  202. {
  203. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  204. if (mac_cb->mac_type != HNAE_PORT_SERVICE)
  205. return -ENOSPC;
  206. return hns_mac_rm_uc_addr(mac_cb, handle->vf_id, addr);
  207. }
  208. static int hns_ae_set_multicast_one(struct hnae_handle *handle, void *addr)
  209. {
  210. int ret;
  211. char *mac_addr = (char *)addr;
  212. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  213. u8 port_num;
  214. assert(mac_cb);
  215. if (mac_cb->mac_type != HNAE_PORT_SERVICE)
  216. return 0;
  217. ret = hns_mac_set_multi(mac_cb, mac_cb->mac_id, mac_addr, true);
  218. if (ret) {
  219. dev_err(handle->owner_dev,
  220. "mac add mul_mac:%pM port%d fail, ret = %#x!\n",
  221. mac_addr, mac_cb->mac_id, ret);
  222. return ret;
  223. }
  224. ret = hns_mac_get_inner_port_num(mac_cb, handle->vf_id, &port_num);
  225. if (ret)
  226. return ret;
  227. ret = hns_mac_set_multi(mac_cb, port_num, mac_addr, true);
  228. if (ret)
  229. dev_err(handle->owner_dev,
  230. "mac add mul_mac:%pM port%d fail, ret = %#x!\n",
  231. mac_addr, DSAF_BASE_INNER_PORT_NUM, ret);
  232. return ret;
  233. }
  234. static int hns_ae_clr_multicast(struct hnae_handle *handle)
  235. {
  236. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  237. if (mac_cb->mac_type != HNAE_PORT_SERVICE)
  238. return 0;
  239. return hns_mac_clr_multicast(mac_cb, handle->vf_id);
  240. }
  241. static int hns_ae_set_mtu(struct hnae_handle *handle, int new_mtu)
  242. {
  243. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  244. struct hnae_queue *q;
  245. u32 rx_buf_size;
  246. int i, ret;
  247. /* when buf_size is 2048, max mtu is 6K for rx ring max bd num is 3. */
  248. if (!AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver)) {
  249. if (new_mtu <= BD_SIZE_2048_MAX_MTU)
  250. rx_buf_size = 2048;
  251. else
  252. rx_buf_size = 4096;
  253. } else {
  254. rx_buf_size = mac_cb->dsaf_dev->buf_size;
  255. }
  256. ret = hns_mac_set_mtu(mac_cb, new_mtu, rx_buf_size);
  257. if (!ret) {
  258. /* reinit ring buf_size */
  259. for (i = 0; i < handle->q_num; i++) {
  260. q = handle->qs[i];
  261. q->rx_ring.buf_size = rx_buf_size;
  262. hns_rcb_set_rx_ring_bs(q, rx_buf_size);
  263. }
  264. }
  265. return ret;
  266. }
  267. static void hns_ae_set_tso_stats(struct hnae_handle *handle, int enable)
  268. {
  269. struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
  270. hns_ppe_set_tso_enable(ppe_cb, enable);
  271. }
  272. static int hns_ae_start(struct hnae_handle *handle)
  273. {
  274. int ret;
  275. int k;
  276. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  277. ret = hns_mac_vm_config_bc_en(mac_cb, 0, true);
  278. if (ret)
  279. return ret;
  280. for (k = 0; k < handle->q_num; k++) {
  281. if (AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver))
  282. hns_rcb_int_clr_hw(handle->qs[k],
  283. RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
  284. else
  285. hns_rcbv2_int_clr_hw(handle->qs[k],
  286. RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
  287. }
  288. hns_ae_ring_enable_all(handle, 1);
  289. msleep(100);
  290. hns_mac_start(mac_cb);
  291. return 0;
  292. }
  293. static void hns_ae_stop(struct hnae_handle *handle)
  294. {
  295. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  296. /* just clean tx fbd, neednot rx fbd*/
  297. hns_rcb_wait_fbd_clean(handle->qs, handle->q_num, RCB_INT_FLAG_TX);
  298. msleep(20);
  299. hns_mac_stop(mac_cb);
  300. usleep_range(10000, 20000);
  301. hns_ae_ring_enable_all(handle, 0);
  302. /* clean rx fbd. */
  303. hns_rcb_wait_fbd_clean(handle->qs, handle->q_num, RCB_INT_FLAG_RX);
  304. (void)hns_mac_vm_config_bc_en(mac_cb, 0, false);
  305. }
  306. static void hns_ae_reset(struct hnae_handle *handle)
  307. {
  308. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  309. if (vf_cb->mac_cb->mac_type == HNAE_PORT_DEBUG) {
  310. hns_mac_reset(vf_cb->mac_cb);
  311. hns_ppe_reset_common(vf_cb->dsaf_dev, 0);
  312. }
  313. }
  314. static void hns_ae_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
  315. {
  316. u32 flag;
  317. if (is_tx_ring(ring))
  318. flag = RCB_INT_FLAG_TX;
  319. else
  320. flag = RCB_INT_FLAG_RX;
  321. hns_rcb_int_ctrl_hw(ring->q, flag, mask);
  322. }
  323. static void hns_aev2_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
  324. {
  325. u32 flag;
  326. if (is_tx_ring(ring))
  327. flag = RCB_INT_FLAG_TX;
  328. else
  329. flag = RCB_INT_FLAG_RX;
  330. hns_rcbv2_int_ctrl_hw(ring->q, flag, mask);
  331. }
  332. static int hns_ae_get_link_status(struct hnae_handle *handle)
  333. {
  334. u32 link_status;
  335. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  336. hns_mac_get_link_status(mac_cb, &link_status);
  337. return !!link_status;
  338. }
  339. static int hns_ae_get_mac_info(struct hnae_handle *handle,
  340. u8 *auto_neg, u16 *speed, u8 *duplex)
  341. {
  342. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  343. return hns_mac_get_port_info(mac_cb, auto_neg, speed, duplex);
  344. }
  345. static bool hns_ae_need_adjust_link(struct hnae_handle *handle, int speed,
  346. int duplex)
  347. {
  348. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  349. return hns_mac_need_adjust_link(mac_cb, speed, duplex);
  350. }
  351. static void hns_ae_adjust_link(struct hnae_handle *handle, int speed,
  352. int duplex)
  353. {
  354. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  355. switch (mac_cb->dsaf_dev->dsaf_ver) {
  356. case AE_VERSION_1:
  357. hns_mac_adjust_link(mac_cb, speed, duplex);
  358. break;
  359. case AE_VERSION_2:
  360. /* chip need to clear all pkt inside */
  361. hns_mac_disable(mac_cb, MAC_COMM_MODE_RX);
  362. if (hns_ae_wait_flow_down(handle)) {
  363. hns_mac_enable(mac_cb, MAC_COMM_MODE_RX);
  364. break;
  365. }
  366. hns_mac_adjust_link(mac_cb, speed, duplex);
  367. hns_mac_enable(mac_cb, MAC_COMM_MODE_RX);
  368. break;
  369. default:
  370. break;
  371. }
  372. return;
  373. }
  374. static void hns_ae_get_ring_bdnum_limit(struct hnae_queue *queue,
  375. u32 *uplimit)
  376. {
  377. *uplimit = HNS_RCB_RING_MAX_PENDING_BD;
  378. }
  379. static void hns_ae_get_pauseparam(struct hnae_handle *handle,
  380. u32 *auto_neg, u32 *rx_en, u32 *tx_en)
  381. {
  382. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  383. struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
  384. hns_mac_get_autoneg(mac_cb, auto_neg);
  385. hns_mac_get_pauseparam(mac_cb, rx_en, tx_en);
  386. /* Service port's pause feature is provided by DSAF, not mac */
  387. if (handle->port_type == HNAE_PORT_SERVICE)
  388. hns_dsaf_get_rx_mac_pause_en(dsaf_dev, mac_cb->mac_id, rx_en);
  389. }
  390. static int hns_ae_set_autoneg(struct hnae_handle *handle, u8 enable)
  391. {
  392. assert(handle);
  393. return hns_mac_set_autoneg(hns_get_mac_cb(handle), enable);
  394. }
  395. static void hns_ae_set_promisc_mode(struct hnae_handle *handle, u32 en)
  396. {
  397. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  398. hns_dsaf_set_promisc_mode(hns_ae_get_dsaf_dev(handle->dev), en);
  399. hns_mac_set_promisc(mac_cb, (u8)!!en);
  400. }
  401. static int hns_ae_get_autoneg(struct hnae_handle *handle)
  402. {
  403. u32 auto_neg;
  404. assert(handle);
  405. hns_mac_get_autoneg(hns_get_mac_cb(handle), &auto_neg);
  406. return auto_neg;
  407. }
  408. static int hns_ae_set_pauseparam(struct hnae_handle *handle,
  409. u32 autoneg, u32 rx_en, u32 tx_en)
  410. {
  411. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  412. struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
  413. int ret;
  414. ret = hns_mac_set_autoneg(mac_cb, autoneg);
  415. if (ret)
  416. return ret;
  417. /* Service port's pause feature is provided by DSAF, not mac */
  418. if (handle->port_type == HNAE_PORT_SERVICE) {
  419. ret = hns_dsaf_set_rx_mac_pause_en(dsaf_dev,
  420. mac_cb->mac_id, rx_en);
  421. if (ret)
  422. return ret;
  423. rx_en = 0;
  424. }
  425. return hns_mac_set_pauseparam(mac_cb, rx_en, tx_en);
  426. }
  427. static void hns_ae_get_coalesce_usecs(struct hnae_handle *handle,
  428. u32 *tx_usecs, u32 *rx_usecs)
  429. {
  430. struct ring_pair_cb *ring_pair =
  431. container_of(handle->qs[0], struct ring_pair_cb, q);
  432. *tx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
  433. ring_pair->port_id_in_comm);
  434. *rx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
  435. ring_pair->port_id_in_comm);
  436. }
  437. static void hns_ae_get_max_coalesced_frames(struct hnae_handle *handle,
  438. u32 *tx_frames, u32 *rx_frames)
  439. {
  440. struct ring_pair_cb *ring_pair =
  441. container_of(handle->qs[0], struct ring_pair_cb, q);
  442. struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
  443. if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
  444. handle->port_type == HNAE_PORT_DEBUG)
  445. *tx_frames = hns_rcb_get_rx_coalesced_frames(
  446. ring_pair->rcb_common, ring_pair->port_id_in_comm);
  447. else
  448. *tx_frames = hns_rcb_get_tx_coalesced_frames(
  449. ring_pair->rcb_common, ring_pair->port_id_in_comm);
  450. *rx_frames = hns_rcb_get_rx_coalesced_frames(ring_pair->rcb_common,
  451. ring_pair->port_id_in_comm);
  452. }
  453. static int hns_ae_set_coalesce_usecs(struct hnae_handle *handle,
  454. u32 timeout)
  455. {
  456. struct ring_pair_cb *ring_pair =
  457. container_of(handle->qs[0], struct ring_pair_cb, q);
  458. return hns_rcb_set_coalesce_usecs(
  459. ring_pair->rcb_common, ring_pair->port_id_in_comm, timeout);
  460. }
  461. static int hns_ae_set_coalesce_frames(struct hnae_handle *handle,
  462. u32 tx_frames, u32 rx_frames)
  463. {
  464. int ret;
  465. struct ring_pair_cb *ring_pair =
  466. container_of(handle->qs[0], struct ring_pair_cb, q);
  467. struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
  468. if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
  469. handle->port_type == HNAE_PORT_DEBUG) {
  470. if (tx_frames != rx_frames)
  471. return -EINVAL;
  472. return hns_rcb_set_rx_coalesced_frames(
  473. ring_pair->rcb_common,
  474. ring_pair->port_id_in_comm, rx_frames);
  475. } else {
  476. if (tx_frames != 1)
  477. return -EINVAL;
  478. ret = hns_rcb_set_tx_coalesced_frames(
  479. ring_pair->rcb_common,
  480. ring_pair->port_id_in_comm, tx_frames);
  481. if (ret)
  482. return ret;
  483. return hns_rcb_set_rx_coalesced_frames(
  484. ring_pair->rcb_common,
  485. ring_pair->port_id_in_comm, rx_frames);
  486. }
  487. }
  488. static void hns_ae_get_coalesce_range(struct hnae_handle *handle,
  489. u32 *tx_frames_low, u32 *rx_frames_low,
  490. u32 *tx_frames_high, u32 *rx_frames_high,
  491. u32 *tx_usecs_low, u32 *rx_usecs_low,
  492. u32 *tx_usecs_high, u32 *rx_usecs_high)
  493. {
  494. struct dsaf_device *dsaf_dev;
  495. assert(handle);
  496. dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
  497. *tx_frames_low = HNS_RCB_TX_FRAMES_LOW;
  498. *rx_frames_low = HNS_RCB_RX_FRAMES_LOW;
  499. if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
  500. handle->port_type == HNAE_PORT_DEBUG)
  501. *tx_frames_high =
  502. (dsaf_dev->desc_num - 1 > HNS_RCB_TX_FRAMES_HIGH) ?
  503. HNS_RCB_TX_FRAMES_HIGH : dsaf_dev->desc_num - 1;
  504. else
  505. *tx_frames_high = 1;
  506. *rx_frames_high = (dsaf_dev->desc_num - 1 > HNS_RCB_RX_FRAMES_HIGH) ?
  507. HNS_RCB_RX_FRAMES_HIGH : dsaf_dev->desc_num - 1;
  508. *tx_usecs_low = HNS_RCB_TX_USECS_LOW;
  509. *rx_usecs_low = HNS_RCB_RX_USECS_LOW;
  510. *tx_usecs_high = HNS_RCB_TX_USECS_HIGH;
  511. *rx_usecs_high = HNS_RCB_RX_USECS_HIGH;
  512. }
  513. static void hns_ae_update_stats(struct hnae_handle *handle,
  514. struct net_device_stats *net_stats)
  515. {
  516. int port;
  517. int idx;
  518. struct dsaf_device *dsaf_dev;
  519. struct hns_mac_cb *mac_cb;
  520. struct hns_ppe_cb *ppe_cb;
  521. struct hnae_queue *queue;
  522. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  523. u64 tx_bytes = 0, rx_bytes = 0, tx_packets = 0, rx_packets = 0;
  524. u64 rx_errors = 0, tx_errors = 0, tx_dropped = 0;
  525. u64 rx_missed_errors = 0;
  526. dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
  527. if (!dsaf_dev)
  528. return;
  529. port = vf_cb->port_index;
  530. ppe_cb = hns_get_ppe_cb(handle);
  531. mac_cb = hns_get_mac_cb(handle);
  532. for (idx = 0; idx < handle->q_num; idx++) {
  533. queue = handle->qs[idx];
  534. hns_rcb_update_stats(queue);
  535. tx_bytes += queue->tx_ring.stats.tx_bytes;
  536. tx_packets += queue->tx_ring.stats.tx_pkts;
  537. rx_bytes += queue->rx_ring.stats.rx_bytes;
  538. rx_packets += queue->rx_ring.stats.rx_pkts;
  539. rx_errors += queue->rx_ring.stats.err_pkt_len
  540. + queue->rx_ring.stats.l2_err
  541. + queue->rx_ring.stats.l3l4_csum_err;
  542. }
  543. hns_ppe_update_stats(ppe_cb);
  544. rx_missed_errors = ppe_cb->hw_stats.rx_drop_no_buf;
  545. tx_errors += ppe_cb->hw_stats.tx_err_checksum
  546. + ppe_cb->hw_stats.tx_err_fifo_empty;
  547. if (mac_cb->mac_type == HNAE_PORT_SERVICE) {
  548. hns_dsaf_update_stats(dsaf_dev, port);
  549. /* for port upline direction, i.e., rx. */
  550. rx_missed_errors += dsaf_dev->hw_stats[port].bp_drop;
  551. rx_missed_errors += dsaf_dev->hw_stats[port].pad_drop;
  552. rx_missed_errors += dsaf_dev->hw_stats[port].crc_false;
  553. /* for port downline direction, i.e., tx. */
  554. port = port + DSAF_PPE_INODE_BASE;
  555. hns_dsaf_update_stats(dsaf_dev, port);
  556. tx_dropped += dsaf_dev->hw_stats[port].bp_drop;
  557. tx_dropped += dsaf_dev->hw_stats[port].pad_drop;
  558. tx_dropped += dsaf_dev->hw_stats[port].crc_false;
  559. tx_dropped += dsaf_dev->hw_stats[port].rslt_drop;
  560. tx_dropped += dsaf_dev->hw_stats[port].vlan_drop;
  561. tx_dropped += dsaf_dev->hw_stats[port].stp_drop;
  562. }
  563. hns_mac_update_stats(mac_cb);
  564. rx_errors += mac_cb->hw_stats.rx_fifo_overrun_err;
  565. tx_errors += mac_cb->hw_stats.tx_bad_pkts
  566. + mac_cb->hw_stats.tx_fragment_err
  567. + mac_cb->hw_stats.tx_jabber_err
  568. + mac_cb->hw_stats.tx_underrun_err
  569. + mac_cb->hw_stats.tx_crc_err;
  570. net_stats->tx_bytes = tx_bytes;
  571. net_stats->tx_packets = tx_packets;
  572. net_stats->rx_bytes = rx_bytes;
  573. net_stats->rx_dropped = 0;
  574. net_stats->rx_packets = rx_packets;
  575. net_stats->rx_errors = rx_errors;
  576. net_stats->tx_errors = tx_errors;
  577. net_stats->tx_dropped = tx_dropped;
  578. net_stats->rx_missed_errors = rx_missed_errors;
  579. net_stats->rx_crc_errors = mac_cb->hw_stats.rx_fcs_err;
  580. net_stats->rx_frame_errors = mac_cb->hw_stats.rx_align_err;
  581. net_stats->rx_fifo_errors = mac_cb->hw_stats.rx_fifo_overrun_err;
  582. net_stats->rx_length_errors = mac_cb->hw_stats.rx_len_err;
  583. net_stats->multicast = mac_cb->hw_stats.rx_mc_pkts;
  584. }
  585. static void hns_ae_get_stats(struct hnae_handle *handle, u64 *data)
  586. {
  587. int idx;
  588. struct hns_mac_cb *mac_cb;
  589. struct hns_ppe_cb *ppe_cb;
  590. u64 *p = data;
  591. struct hnae_vf_cb *vf_cb;
  592. if (!handle || !data) {
  593. pr_err("hns_ae_get_stats NULL handle or data pointer!\n");
  594. return;
  595. }
  596. vf_cb = hns_ae_get_vf_cb(handle);
  597. mac_cb = hns_get_mac_cb(handle);
  598. ppe_cb = hns_get_ppe_cb(handle);
  599. for (idx = 0; idx < handle->q_num; idx++) {
  600. hns_rcb_get_stats(handle->qs[idx], p);
  601. p += hns_rcb_get_ring_sset_count((int)ETH_SS_STATS);
  602. }
  603. hns_ppe_get_stats(ppe_cb, p);
  604. p += hns_ppe_get_sset_count((int)ETH_SS_STATS);
  605. hns_mac_get_stats(mac_cb, p);
  606. p += hns_mac_get_sset_count(mac_cb, (int)ETH_SS_STATS);
  607. if (mac_cb->mac_type == HNAE_PORT_SERVICE)
  608. hns_dsaf_get_stats(vf_cb->dsaf_dev, p, vf_cb->port_index);
  609. }
  610. static void hns_ae_get_strings(struct hnae_handle *handle,
  611. u32 stringset, u8 *data)
  612. {
  613. int port;
  614. int idx;
  615. struct hns_mac_cb *mac_cb;
  616. struct hns_ppe_cb *ppe_cb;
  617. struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
  618. u8 *p = data;
  619. struct hnae_vf_cb *vf_cb;
  620. assert(handle);
  621. vf_cb = hns_ae_get_vf_cb(handle);
  622. port = vf_cb->port_index;
  623. mac_cb = hns_get_mac_cb(handle);
  624. ppe_cb = hns_get_ppe_cb(handle);
  625. for (idx = 0; idx < handle->q_num; idx++) {
  626. hns_rcb_get_strings(stringset, p, idx);
  627. p += ETH_GSTRING_LEN * hns_rcb_get_ring_sset_count(stringset);
  628. }
  629. hns_ppe_get_strings(ppe_cb, stringset, p);
  630. p += ETH_GSTRING_LEN * hns_ppe_get_sset_count(stringset);
  631. hns_mac_get_strings(mac_cb, stringset, p);
  632. p += ETH_GSTRING_LEN * hns_mac_get_sset_count(mac_cb, stringset);
  633. if (mac_cb->mac_type == HNAE_PORT_SERVICE)
  634. hns_dsaf_get_strings(stringset, p, port, dsaf_dev);
  635. }
  636. static int hns_ae_get_sset_count(struct hnae_handle *handle, int stringset)
  637. {
  638. u32 sset_count = 0;
  639. struct hns_mac_cb *mac_cb;
  640. struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
  641. assert(handle);
  642. mac_cb = hns_get_mac_cb(handle);
  643. sset_count += hns_rcb_get_ring_sset_count(stringset) * handle->q_num;
  644. sset_count += hns_ppe_get_sset_count(stringset);
  645. sset_count += hns_mac_get_sset_count(mac_cb, stringset);
  646. if (mac_cb->mac_type == HNAE_PORT_SERVICE)
  647. sset_count += hns_dsaf_get_sset_count(dsaf_dev, stringset);
  648. return sset_count;
  649. }
  650. static int hns_ae_config_loopback(struct hnae_handle *handle,
  651. enum hnae_loop loop, int en)
  652. {
  653. int ret;
  654. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  655. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  656. struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
  657. switch (loop) {
  658. case MAC_INTERNALLOOP_PHY:
  659. ret = 0;
  660. break;
  661. case MAC_INTERNALLOOP_SERDES:
  662. ret = dsaf_dev->misc_op->cfg_serdes_loopback(vf_cb->mac_cb,
  663. !!en);
  664. break;
  665. case MAC_INTERNALLOOP_MAC:
  666. ret = hns_mac_config_mac_loopback(vf_cb->mac_cb, loop, en);
  667. break;
  668. default:
  669. ret = -EINVAL;
  670. }
  671. return ret;
  672. }
  673. static void hns_ae_update_led_status(struct hnae_handle *handle)
  674. {
  675. struct hns_mac_cb *mac_cb;
  676. assert(handle);
  677. mac_cb = hns_get_mac_cb(handle);
  678. if (mac_cb->media_type != HNAE_MEDIA_TYPE_FIBER)
  679. return;
  680. hns_set_led_opt(mac_cb);
  681. }
  682. static int hns_ae_cpld_set_led_id(struct hnae_handle *handle,
  683. enum hnae_led_state status)
  684. {
  685. struct hns_mac_cb *mac_cb;
  686. assert(handle);
  687. mac_cb = hns_get_mac_cb(handle);
  688. return hns_cpld_led_set_id(mac_cb, status);
  689. }
  690. static void hns_ae_get_regs(struct hnae_handle *handle, void *data)
  691. {
  692. u32 *p = data;
  693. int i;
  694. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  695. struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
  696. hns_ppe_get_regs(ppe_cb, p);
  697. p += hns_ppe_get_regs_count();
  698. hns_rcb_get_common_regs(vf_cb->dsaf_dev->rcb_common[0], p);
  699. p += hns_rcb_get_common_regs_count();
  700. for (i = 0; i < handle->q_num; i++) {
  701. hns_rcb_get_ring_regs(handle->qs[i], p);
  702. p += hns_rcb_get_ring_regs_count();
  703. }
  704. hns_mac_get_regs(vf_cb->mac_cb, p);
  705. p += hns_mac_get_regs_count(vf_cb->mac_cb);
  706. if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
  707. hns_dsaf_get_regs(vf_cb->dsaf_dev, vf_cb->port_index, p);
  708. }
  709. static int hns_ae_get_regs_len(struct hnae_handle *handle)
  710. {
  711. u32 total_num;
  712. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  713. total_num = hns_ppe_get_regs_count();
  714. total_num += hns_rcb_get_common_regs_count();
  715. total_num += hns_rcb_get_ring_regs_count() * handle->q_num;
  716. total_num += hns_mac_get_regs_count(vf_cb->mac_cb);
  717. if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
  718. total_num += hns_dsaf_get_regs_count();
  719. return total_num;
  720. }
  721. static u32 hns_ae_get_rss_key_size(struct hnae_handle *handle)
  722. {
  723. return HNS_PPEV2_RSS_KEY_SIZE;
  724. }
  725. static u32 hns_ae_get_rss_indir_size(struct hnae_handle *handle)
  726. {
  727. return HNS_PPEV2_RSS_IND_TBL_SIZE;
  728. }
  729. static int hns_ae_get_rss(struct hnae_handle *handle, u32 *indir, u8 *key,
  730. u8 *hfunc)
  731. {
  732. struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
  733. /* currently we support only one type of hash function i.e. Toep hash */
  734. if (hfunc)
  735. *hfunc = ETH_RSS_HASH_TOP;
  736. /* get the RSS Key required by the user */
  737. if (key)
  738. memcpy(key, ppe_cb->rss_key, HNS_PPEV2_RSS_KEY_SIZE);
  739. /* update the current hash->queue mappings from the shadow RSS table */
  740. if (indir)
  741. memcpy(indir, ppe_cb->rss_indir_table,
  742. HNS_PPEV2_RSS_IND_TBL_SIZE * sizeof(*indir));
  743. return 0;
  744. }
  745. static int hns_ae_set_rss(struct hnae_handle *handle, const u32 *indir,
  746. const u8 *key, const u8 hfunc)
  747. {
  748. struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
  749. /* set the RSS Hash Key if specififed by the user */
  750. if (key) {
  751. memcpy(ppe_cb->rss_key, key, HNS_PPEV2_RSS_KEY_SIZE);
  752. hns_ppe_set_rss_key(ppe_cb, ppe_cb->rss_key);
  753. }
  754. if (indir) {
  755. /* update the shadow RSS table with user specified qids */
  756. memcpy(ppe_cb->rss_indir_table, indir,
  757. HNS_PPEV2_RSS_IND_TBL_SIZE * sizeof(*indir));
  758. /* now update the hardware */
  759. hns_ppe_set_indir_table(ppe_cb, ppe_cb->rss_indir_table);
  760. }
  761. return 0;
  762. }
  763. static struct hnae_ae_ops hns_dsaf_ops = {
  764. .get_handle = hns_ae_get_handle,
  765. .put_handle = hns_ae_put_handle,
  766. .init_queue = hns_ae_init_queue,
  767. .fini_queue = hns_ae_fini_queue,
  768. .start = hns_ae_start,
  769. .stop = hns_ae_stop,
  770. .reset = hns_ae_reset,
  771. .toggle_ring_irq = hns_ae_toggle_ring_irq,
  772. .get_status = hns_ae_get_link_status,
  773. .get_info = hns_ae_get_mac_info,
  774. .adjust_link = hns_ae_adjust_link,
  775. .need_adjust_link = hns_ae_need_adjust_link,
  776. .set_loopback = hns_ae_config_loopback,
  777. .get_ring_bdnum_limit = hns_ae_get_ring_bdnum_limit,
  778. .get_pauseparam = hns_ae_get_pauseparam,
  779. .set_autoneg = hns_ae_set_autoneg,
  780. .get_autoneg = hns_ae_get_autoneg,
  781. .set_pauseparam = hns_ae_set_pauseparam,
  782. .get_coalesce_usecs = hns_ae_get_coalesce_usecs,
  783. .get_max_coalesced_frames = hns_ae_get_max_coalesced_frames,
  784. .set_coalesce_usecs = hns_ae_set_coalesce_usecs,
  785. .set_coalesce_frames = hns_ae_set_coalesce_frames,
  786. .get_coalesce_range = hns_ae_get_coalesce_range,
  787. .set_promisc_mode = hns_ae_set_promisc_mode,
  788. .set_mac_addr = hns_ae_set_mac_address,
  789. .add_uc_addr = hns_ae_add_uc_address,
  790. .rm_uc_addr = hns_ae_rm_uc_address,
  791. .set_mc_addr = hns_ae_set_multicast_one,
  792. .clr_mc_addr = hns_ae_clr_multicast,
  793. .set_mtu = hns_ae_set_mtu,
  794. .update_stats = hns_ae_update_stats,
  795. .set_tso_stats = hns_ae_set_tso_stats,
  796. .get_stats = hns_ae_get_stats,
  797. .get_strings = hns_ae_get_strings,
  798. .get_sset_count = hns_ae_get_sset_count,
  799. .update_led_status = hns_ae_update_led_status,
  800. .set_led_id = hns_ae_cpld_set_led_id,
  801. .get_regs = hns_ae_get_regs,
  802. .get_regs_len = hns_ae_get_regs_len,
  803. .get_rss_key_size = hns_ae_get_rss_key_size,
  804. .get_rss_indir_size = hns_ae_get_rss_indir_size,
  805. .get_rss = hns_ae_get_rss,
  806. .set_rss = hns_ae_set_rss
  807. };
  808. int hns_dsaf_ae_init(struct dsaf_device *dsaf_dev)
  809. {
  810. struct hnae_ae_dev *ae_dev = &dsaf_dev->ae_dev;
  811. static atomic_t id = ATOMIC_INIT(-1);
  812. switch (dsaf_dev->dsaf_ver) {
  813. case AE_VERSION_1:
  814. hns_dsaf_ops.toggle_ring_irq = hns_ae_toggle_ring_irq;
  815. break;
  816. case AE_VERSION_2:
  817. hns_dsaf_ops.toggle_ring_irq = hns_aev2_toggle_ring_irq;
  818. break;
  819. default:
  820. break;
  821. }
  822. snprintf(ae_dev->name, AE_NAME_SIZE, "%s%d", DSAF_DEVICE_NAME,
  823. (int)atomic_inc_return(&id));
  824. ae_dev->ops = &hns_dsaf_ops;
  825. ae_dev->dev = dsaf_dev->dev;
  826. return hnae_ae_register(ae_dev, THIS_MODULE);
  827. }
  828. void hns_dsaf_ae_uninit(struct dsaf_device *dsaf_dev)
  829. {
  830. hnae_ae_unregister(&dsaf_dev->ae_dev);
  831. }