sxgbe_desc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /* 10G controller driver for Samsung SoCs
  2. *
  3. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Author: Siva Reddy Kallam <siva.kallam@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/bitops.h>
  14. #include <linux/export.h>
  15. #include <linux/io.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/phy.h>
  18. #include "sxgbe_common.h"
  19. #include "sxgbe_dma.h"
  20. #include "sxgbe_desc.h"
  21. /* DMA TX descriptor ring initialization */
  22. static void sxgbe_init_tx_desc(struct sxgbe_tx_norm_desc *p)
  23. {
  24. p->tdes23.tx_rd_des23.own_bit = 0;
  25. }
  26. static void sxgbe_tx_desc_enable_tse(struct sxgbe_tx_norm_desc *p, u8 is_tse,
  27. u32 total_hdr_len, u32 tcp_hdr_len,
  28. u32 tcp_payload_len)
  29. {
  30. p->tdes23.tx_rd_des23.tse_bit = is_tse;
  31. p->tdes23.tx_rd_des23.buf1_size = total_hdr_len;
  32. p->tdes23.tx_rd_des23.tcp_hdr_len = tcp_hdr_len / 4;
  33. p->tdes23.tx_rd_des23.tx_pkt_len.tcp_payload_len = tcp_payload_len;
  34. }
  35. /* Assign buffer lengths for descriptor */
  36. static void sxgbe_prepare_tx_desc(struct sxgbe_tx_norm_desc *p, u8 is_fd,
  37. int buf1_len, int pkt_len, int cksum)
  38. {
  39. p->tdes23.tx_rd_des23.first_desc = is_fd;
  40. p->tdes23.tx_rd_des23.buf1_size = buf1_len;
  41. p->tdes23.tx_rd_des23.tx_pkt_len.pkt_len.total_pkt_len = pkt_len;
  42. if (cksum)
  43. p->tdes23.tx_rd_des23.cksum_ctl = cic_full;
  44. }
  45. /* Set VLAN control information */
  46. static void sxgbe_tx_vlanctl_desc(struct sxgbe_tx_norm_desc *p, int vlan_ctl)
  47. {
  48. p->tdes23.tx_rd_des23.vlan_tag_ctl = vlan_ctl;
  49. }
  50. /* Set the owner of Normal descriptor */
  51. static void sxgbe_set_tx_owner(struct sxgbe_tx_norm_desc *p)
  52. {
  53. p->tdes23.tx_rd_des23.own_bit = 1;
  54. }
  55. /* Get the owner of Normal descriptor */
  56. static int sxgbe_get_tx_owner(struct sxgbe_tx_norm_desc *p)
  57. {
  58. return p->tdes23.tx_rd_des23.own_bit;
  59. }
  60. /* Invoked by the xmit function to close the tx descriptor */
  61. static void sxgbe_close_tx_desc(struct sxgbe_tx_norm_desc *p)
  62. {
  63. p->tdes23.tx_rd_des23.last_desc = 1;
  64. p->tdes23.tx_rd_des23.int_on_com = 1;
  65. }
  66. /* Clean the tx descriptor as soon as the tx irq is received */
  67. static void sxgbe_release_tx_desc(struct sxgbe_tx_norm_desc *p)
  68. {
  69. memset(p, 0, sizeof(*p));
  70. }
  71. /* Clear interrupt on tx frame completion. When this bit is
  72. * set an interrupt happens as soon as the frame is transmitted
  73. */
  74. static void sxgbe_clear_tx_ic(struct sxgbe_tx_norm_desc *p)
  75. {
  76. p->tdes23.tx_rd_des23.int_on_com = 0;
  77. }
  78. /* Last tx segment reports the transmit status */
  79. static int sxgbe_get_tx_ls(struct sxgbe_tx_norm_desc *p)
  80. {
  81. return p->tdes23.tx_rd_des23.last_desc;
  82. }
  83. /* Get the buffer size from the descriptor */
  84. static int sxgbe_get_tx_len(struct sxgbe_tx_norm_desc *p)
  85. {
  86. return p->tdes23.tx_rd_des23.buf1_size;
  87. }
  88. /* Set tx timestamp enable bit */
  89. static void sxgbe_tx_enable_tstamp(struct sxgbe_tx_norm_desc *p)
  90. {
  91. p->tdes23.tx_rd_des23.timestmp_enable = 1;
  92. }
  93. /* get tx timestamp status */
  94. static int sxgbe_get_tx_timestamp_status(struct sxgbe_tx_norm_desc *p)
  95. {
  96. return p->tdes23.tx_rd_des23.timestmp_enable;
  97. }
  98. /* TX Context Descripto Specific */
  99. static void sxgbe_tx_ctxt_desc_set_ctxt(struct sxgbe_tx_ctxt_desc *p)
  100. {
  101. p->ctxt_bit = 1;
  102. }
  103. /* Set the owner of TX context descriptor */
  104. static void sxgbe_tx_ctxt_desc_set_owner(struct sxgbe_tx_ctxt_desc *p)
  105. {
  106. p->own_bit = 1;
  107. }
  108. /* Get the owner of TX context descriptor */
  109. static int sxgbe_tx_ctxt_desc_get_owner(struct sxgbe_tx_ctxt_desc *p)
  110. {
  111. return p->own_bit;
  112. }
  113. /* Set TX mss in TX context Descriptor */
  114. static void sxgbe_tx_ctxt_desc_set_mss(struct sxgbe_tx_ctxt_desc *p, u16 mss)
  115. {
  116. p->maxseg_size = mss;
  117. }
  118. /* Get TX mss from TX context Descriptor */
  119. static int sxgbe_tx_ctxt_desc_get_mss(struct sxgbe_tx_ctxt_desc *p)
  120. {
  121. return p->maxseg_size;
  122. }
  123. /* Set TX tcmssv in TX context Descriptor */
  124. static void sxgbe_tx_ctxt_desc_set_tcmssv(struct sxgbe_tx_ctxt_desc *p)
  125. {
  126. p->tcmssv = 1;
  127. }
  128. /* Reset TX ostc in TX context Descriptor */
  129. static void sxgbe_tx_ctxt_desc_reset_ostc(struct sxgbe_tx_ctxt_desc *p)
  130. {
  131. p->ostc = 0;
  132. }
  133. /* Set IVLAN information */
  134. static void sxgbe_tx_ctxt_desc_set_ivlantag(struct sxgbe_tx_ctxt_desc *p,
  135. int is_ivlanvalid, int ivlan_tag,
  136. int ivlan_ctl)
  137. {
  138. if (is_ivlanvalid) {
  139. p->ivlan_tag_valid = is_ivlanvalid;
  140. p->ivlan_tag = ivlan_tag;
  141. p->ivlan_tag_ctl = ivlan_ctl;
  142. }
  143. }
  144. /* Return IVLAN Tag */
  145. static int sxgbe_tx_ctxt_desc_get_ivlantag(struct sxgbe_tx_ctxt_desc *p)
  146. {
  147. return p->ivlan_tag;
  148. }
  149. /* Set VLAN Tag */
  150. static void sxgbe_tx_ctxt_desc_set_vlantag(struct sxgbe_tx_ctxt_desc *p,
  151. int is_vlanvalid, int vlan_tag)
  152. {
  153. if (is_vlanvalid) {
  154. p->vltag_valid = is_vlanvalid;
  155. p->vlan_tag = vlan_tag;
  156. }
  157. }
  158. /* Return VLAN Tag */
  159. static int sxgbe_tx_ctxt_desc_get_vlantag(struct sxgbe_tx_ctxt_desc *p)
  160. {
  161. return p->vlan_tag;
  162. }
  163. /* Set Time stamp */
  164. static void sxgbe_tx_ctxt_desc_set_tstamp(struct sxgbe_tx_ctxt_desc *p,
  165. u8 ostc_enable, u64 tstamp)
  166. {
  167. if (ostc_enable) {
  168. p->ostc = ostc_enable;
  169. p->tstamp_lo = (u32) tstamp;
  170. p->tstamp_hi = (u32) (tstamp>>32);
  171. }
  172. }
  173. /* Close TX context descriptor */
  174. static void sxgbe_tx_ctxt_desc_close(struct sxgbe_tx_ctxt_desc *p)
  175. {
  176. p->own_bit = 1;
  177. }
  178. /* WB status of context descriptor */
  179. static int sxgbe_tx_ctxt_desc_get_cde(struct sxgbe_tx_ctxt_desc *p)
  180. {
  181. return p->ctxt_desc_err;
  182. }
  183. /* DMA RX descriptor ring initialization */
  184. static void sxgbe_init_rx_desc(struct sxgbe_rx_norm_desc *p, int disable_rx_ic,
  185. int mode, int end)
  186. {
  187. p->rdes23.rx_rd_des23.own_bit = 1;
  188. if (disable_rx_ic)
  189. p->rdes23.rx_rd_des23.int_on_com = disable_rx_ic;
  190. }
  191. /* Get RX own bit */
  192. static int sxgbe_get_rx_owner(struct sxgbe_rx_norm_desc *p)
  193. {
  194. return p->rdes23.rx_rd_des23.own_bit;
  195. }
  196. /* Set RX own bit */
  197. static void sxgbe_set_rx_owner(struct sxgbe_rx_norm_desc *p)
  198. {
  199. p->rdes23.rx_rd_des23.own_bit = 1;
  200. }
  201. /* Set Interrupt on completion bit */
  202. static void sxgbe_set_rx_int_on_com(struct sxgbe_rx_norm_desc *p)
  203. {
  204. p->rdes23.rx_rd_des23.int_on_com = 1;
  205. }
  206. /* Get the receive frame size */
  207. static int sxgbe_get_rx_frame_len(struct sxgbe_rx_norm_desc *p)
  208. {
  209. return p->rdes23.rx_wb_des23.pkt_len;
  210. }
  211. /* Return first Descriptor status */
  212. static int sxgbe_get_rx_fd_status(struct sxgbe_rx_norm_desc *p)
  213. {
  214. return p->rdes23.rx_wb_des23.first_desc;
  215. }
  216. /* Return Last Descriptor status */
  217. static int sxgbe_get_rx_ld_status(struct sxgbe_rx_norm_desc *p)
  218. {
  219. return p->rdes23.rx_wb_des23.last_desc;
  220. }
  221. /* Return the RX status looking at the WB fields */
  222. static int sxgbe_rx_wbstatus(struct sxgbe_rx_norm_desc *p,
  223. struct sxgbe_extra_stats *x, int *checksum)
  224. {
  225. int status = 0;
  226. *checksum = CHECKSUM_UNNECESSARY;
  227. if (p->rdes23.rx_wb_des23.err_summary) {
  228. switch (p->rdes23.rx_wb_des23.err_l2_type) {
  229. case RX_GMII_ERR:
  230. status = -EINVAL;
  231. x->rx_code_gmii_err++;
  232. break;
  233. case RX_WATCHDOG_ERR:
  234. status = -EINVAL;
  235. x->rx_watchdog_err++;
  236. break;
  237. case RX_CRC_ERR:
  238. status = -EINVAL;
  239. x->rx_crc_err++;
  240. break;
  241. case RX_GAINT_ERR:
  242. status = -EINVAL;
  243. x->rx_gaint_pkt_err++;
  244. break;
  245. case RX_IP_HDR_ERR:
  246. *checksum = CHECKSUM_NONE;
  247. x->ip_hdr_err++;
  248. break;
  249. case RX_PAYLOAD_ERR:
  250. *checksum = CHECKSUM_NONE;
  251. x->ip_payload_err++;
  252. break;
  253. case RX_OVERFLOW_ERR:
  254. status = -EINVAL;
  255. x->overflow_error++;
  256. break;
  257. default:
  258. pr_err("Invalid Error type\n");
  259. break;
  260. }
  261. } else {
  262. switch (p->rdes23.rx_wb_des23.err_l2_type) {
  263. case RX_LEN_PKT:
  264. x->len_pkt++;
  265. break;
  266. case RX_MACCTL_PKT:
  267. x->mac_ctl_pkt++;
  268. break;
  269. case RX_DCBCTL_PKT:
  270. x->dcb_ctl_pkt++;
  271. break;
  272. case RX_ARP_PKT:
  273. x->arp_pkt++;
  274. break;
  275. case RX_OAM_PKT:
  276. x->oam_pkt++;
  277. break;
  278. case RX_UNTAG_PKT:
  279. x->untag_okt++;
  280. break;
  281. case RX_OTHER_PKT:
  282. x->other_pkt++;
  283. break;
  284. case RX_SVLAN_PKT:
  285. x->svlan_tag_pkt++;
  286. break;
  287. case RX_CVLAN_PKT:
  288. x->cvlan_tag_pkt++;
  289. break;
  290. case RX_DVLAN_OCVLAN_ICVLAN_PKT:
  291. x->dvlan_ocvlan_icvlan_pkt++;
  292. break;
  293. case RX_DVLAN_OSVLAN_ISVLAN_PKT:
  294. x->dvlan_osvlan_isvlan_pkt++;
  295. break;
  296. case RX_DVLAN_OSVLAN_ICVLAN_PKT:
  297. x->dvlan_osvlan_icvlan_pkt++;
  298. break;
  299. case RX_DVLAN_OCVLAN_ISVLAN_PKT:
  300. x->dvlan_ocvlan_icvlan_pkt++;
  301. break;
  302. default:
  303. pr_err("Invalid L2 Packet type\n");
  304. break;
  305. }
  306. }
  307. /* L3/L4 Pkt type */
  308. switch (p->rdes23.rx_wb_des23.layer34_pkt_type) {
  309. case RX_NOT_IP_PKT:
  310. x->not_ip_pkt++;
  311. break;
  312. case RX_IPV4_TCP_PKT:
  313. x->ip4_tcp_pkt++;
  314. break;
  315. case RX_IPV4_UDP_PKT:
  316. x->ip4_udp_pkt++;
  317. break;
  318. case RX_IPV4_ICMP_PKT:
  319. x->ip4_icmp_pkt++;
  320. break;
  321. case RX_IPV4_UNKNOWN_PKT:
  322. x->ip4_unknown_pkt++;
  323. break;
  324. case RX_IPV6_TCP_PKT:
  325. x->ip6_tcp_pkt++;
  326. break;
  327. case RX_IPV6_UDP_PKT:
  328. x->ip6_udp_pkt++;
  329. break;
  330. case RX_IPV6_ICMP_PKT:
  331. x->ip6_icmp_pkt++;
  332. break;
  333. case RX_IPV6_UNKNOWN_PKT:
  334. x->ip6_unknown_pkt++;
  335. break;
  336. default:
  337. pr_err("Invalid L3/L4 Packet type\n");
  338. break;
  339. }
  340. /* Filter */
  341. if (p->rdes23.rx_wb_des23.vlan_filter_match)
  342. x->vlan_filter_match++;
  343. if (p->rdes23.rx_wb_des23.sa_filter_fail) {
  344. status = -EINVAL;
  345. x->sa_filter_fail++;
  346. }
  347. if (p->rdes23.rx_wb_des23.da_filter_fail) {
  348. status = -EINVAL;
  349. x->da_filter_fail++;
  350. }
  351. if (p->rdes23.rx_wb_des23.hash_filter_pass)
  352. x->hash_filter_pass++;
  353. if (p->rdes23.rx_wb_des23.l3_filter_match)
  354. x->l3_filter_match++;
  355. if (p->rdes23.rx_wb_des23.l4_filter_match)
  356. x->l4_filter_match++;
  357. return status;
  358. }
  359. /* Get own bit of context descriptor */
  360. static int sxgbe_get_rx_ctxt_owner(struct sxgbe_rx_ctxt_desc *p)
  361. {
  362. return p->own_bit;
  363. }
  364. /* Set own bit for context descriptor */
  365. static void sxgbe_set_ctxt_rx_owner(struct sxgbe_rx_ctxt_desc *p)
  366. {
  367. p->own_bit = 1;
  368. }
  369. /* Return the reception status looking at Context control information */
  370. static void sxgbe_rx_ctxt_wbstatus(struct sxgbe_rx_ctxt_desc *p,
  371. struct sxgbe_extra_stats *x)
  372. {
  373. if (p->tstamp_dropped)
  374. x->timestamp_dropped++;
  375. /* ptp */
  376. if (p->ptp_msgtype == RX_NO_PTP)
  377. x->rx_msg_type_no_ptp++;
  378. else if (p->ptp_msgtype == RX_PTP_SYNC)
  379. x->rx_ptp_type_sync++;
  380. else if (p->ptp_msgtype == RX_PTP_FOLLOW_UP)
  381. x->rx_ptp_type_follow_up++;
  382. else if (p->ptp_msgtype == RX_PTP_DELAY_REQ)
  383. x->rx_ptp_type_delay_req++;
  384. else if (p->ptp_msgtype == RX_PTP_DELAY_RESP)
  385. x->rx_ptp_type_delay_resp++;
  386. else if (p->ptp_msgtype == RX_PTP_PDELAY_REQ)
  387. x->rx_ptp_type_pdelay_req++;
  388. else if (p->ptp_msgtype == RX_PTP_PDELAY_RESP)
  389. x->rx_ptp_type_pdelay_resp++;
  390. else if (p->ptp_msgtype == RX_PTP_PDELAY_FOLLOW_UP)
  391. x->rx_ptp_type_pdelay_follow_up++;
  392. else if (p->ptp_msgtype == RX_PTP_ANNOUNCE)
  393. x->rx_ptp_announce++;
  394. else if (p->ptp_msgtype == RX_PTP_MGMT)
  395. x->rx_ptp_mgmt++;
  396. else if (p->ptp_msgtype == RX_PTP_SIGNAL)
  397. x->rx_ptp_signal++;
  398. else if (p->ptp_msgtype == RX_PTP_RESV_MSG)
  399. x->rx_ptp_resv_msg_type++;
  400. }
  401. /* Get rx timestamp status */
  402. static int sxgbe_get_rx_ctxt_tstamp_status(struct sxgbe_rx_ctxt_desc *p)
  403. {
  404. if ((p->tstamp_hi == 0xffffffff) && (p->tstamp_lo == 0xffffffff)) {
  405. pr_err("Time stamp corrupted\n");
  406. return 0;
  407. }
  408. return p->tstamp_available;
  409. }
  410. static u64 sxgbe_get_rx_timestamp(struct sxgbe_rx_ctxt_desc *p)
  411. {
  412. u64 ns;
  413. ns = p->tstamp_lo;
  414. ns |= ((u64)p->tstamp_hi) << 32;
  415. return ns;
  416. }
  417. static const struct sxgbe_desc_ops desc_ops = {
  418. .init_tx_desc = sxgbe_init_tx_desc,
  419. .tx_desc_enable_tse = sxgbe_tx_desc_enable_tse,
  420. .prepare_tx_desc = sxgbe_prepare_tx_desc,
  421. .tx_vlanctl_desc = sxgbe_tx_vlanctl_desc,
  422. .set_tx_owner = sxgbe_set_tx_owner,
  423. .get_tx_owner = sxgbe_get_tx_owner,
  424. .close_tx_desc = sxgbe_close_tx_desc,
  425. .release_tx_desc = sxgbe_release_tx_desc,
  426. .clear_tx_ic = sxgbe_clear_tx_ic,
  427. .get_tx_ls = sxgbe_get_tx_ls,
  428. .get_tx_len = sxgbe_get_tx_len,
  429. .tx_enable_tstamp = sxgbe_tx_enable_tstamp,
  430. .get_tx_timestamp_status = sxgbe_get_tx_timestamp_status,
  431. .tx_ctxt_desc_set_ctxt = sxgbe_tx_ctxt_desc_set_ctxt,
  432. .tx_ctxt_desc_set_owner = sxgbe_tx_ctxt_desc_set_owner,
  433. .get_tx_ctxt_owner = sxgbe_tx_ctxt_desc_get_owner,
  434. .tx_ctxt_desc_set_mss = sxgbe_tx_ctxt_desc_set_mss,
  435. .tx_ctxt_desc_get_mss = sxgbe_tx_ctxt_desc_get_mss,
  436. .tx_ctxt_desc_set_tcmssv = sxgbe_tx_ctxt_desc_set_tcmssv,
  437. .tx_ctxt_desc_reset_ostc = sxgbe_tx_ctxt_desc_reset_ostc,
  438. .tx_ctxt_desc_set_ivlantag = sxgbe_tx_ctxt_desc_set_ivlantag,
  439. .tx_ctxt_desc_get_ivlantag = sxgbe_tx_ctxt_desc_get_ivlantag,
  440. .tx_ctxt_desc_set_vlantag = sxgbe_tx_ctxt_desc_set_vlantag,
  441. .tx_ctxt_desc_get_vlantag = sxgbe_tx_ctxt_desc_get_vlantag,
  442. .tx_ctxt_set_tstamp = sxgbe_tx_ctxt_desc_set_tstamp,
  443. .close_tx_ctxt_desc = sxgbe_tx_ctxt_desc_close,
  444. .get_tx_ctxt_cde = sxgbe_tx_ctxt_desc_get_cde,
  445. .init_rx_desc = sxgbe_init_rx_desc,
  446. .get_rx_owner = sxgbe_get_rx_owner,
  447. .set_rx_owner = sxgbe_set_rx_owner,
  448. .set_rx_int_on_com = sxgbe_set_rx_int_on_com,
  449. .get_rx_frame_len = sxgbe_get_rx_frame_len,
  450. .get_rx_fd_status = sxgbe_get_rx_fd_status,
  451. .get_rx_ld_status = sxgbe_get_rx_ld_status,
  452. .rx_wbstatus = sxgbe_rx_wbstatus,
  453. .get_rx_ctxt_owner = sxgbe_get_rx_ctxt_owner,
  454. .set_rx_ctxt_owner = sxgbe_set_ctxt_rx_owner,
  455. .rx_ctxt_wbstatus = sxgbe_rx_ctxt_wbstatus,
  456. .get_rx_ctxt_tstamp_status = sxgbe_get_rx_ctxt_tstamp_status,
  457. .get_timestamp = sxgbe_get_rx_timestamp,
  458. };
  459. const struct sxgbe_desc_ops *sxgbe_get_desc_ops(void)
  460. {
  461. return &desc_ops;
  462. }