3838#include <linux/rtnetlink.h>
3939#include <linux/prefetch.h>
4040#include <net/ip6_checksum.h>
41+ #include <linux/ktime.h>
4142
4243#include "cq_enet_desc.h"
4344#include "vnic_dev.h"
@@ -72,6 +73,35 @@ MODULE_LICENSE("GPL");
7273MODULE_VERSION (DRV_VERSION );
7374MODULE_DEVICE_TABLE (pci , enic_id_table );
7475
76+ #define ENIC_LARGE_PKT_THRESHOLD 1000
77+ #define ENIC_MAX_COALESCE_TIMERS 10
78+ /* Interrupt moderation table, which will be used to decide the
79+ * coalescing timer values
80+ * {rx_rate in Mbps, mapping percentage of the range}
81+ */
82+ struct enic_intr_mod_table mod_table [ENIC_MAX_COALESCE_TIMERS + 1 ] = {
83+ {4000 , 0 },
84+ {4400 , 10 },
85+ {5060 , 20 },
86+ {5230 , 30 },
87+ {5540 , 40 },
88+ {5820 , 50 },
89+ {6120 , 60 },
90+ {6435 , 70 },
91+ {6745 , 80 },
92+ {7000 , 90 },
93+ {0xFFFFFFFF , 100 }
94+ };
95+
96+ /* This table helps the driver to pick different ranges for rx coalescing
97+ * timer depending on the link speed.
98+ */
99+ struct enic_intr_mod_range mod_range [ENIC_MAX_LINK_SPEEDS ] = {
100+ {0 , 0 }, /* 0 - 4 Gbps */
101+ {0 , 3 }, /* 4 - 10 Gbps */
102+ {3 , 6 }, /* 10 - 40 Gbps */
103+ };
104+
75105int enic_is_dynamic (struct enic * enic )
76106{
77107 return enic -> pdev -> device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN ;
@@ -979,13 +1009,23 @@ static int enic_rq_alloc_buf(struct vnic_rq *rq)
9791009 return 0 ;
9801010}
9811011
1012+ static void enic_intr_update_pkt_size (struct vnic_rx_bytes_counter * pkt_size ,
1013+ u32 pkt_len )
1014+ {
1015+ if (ENIC_LARGE_PKT_THRESHOLD <= pkt_len )
1016+ pkt_size -> large_pkt_bytes_cnt += pkt_len ;
1017+ else
1018+ pkt_size -> small_pkt_bytes_cnt += pkt_len ;
1019+ }
1020+
9821021static void enic_rq_indicate_buf (struct vnic_rq * rq ,
9831022 struct cq_desc * cq_desc , struct vnic_rq_buf * buf ,
9841023 int skipped , void * opaque )
9851024{
9861025 struct enic * enic = vnic_dev_priv (rq -> vdev );
9871026 struct net_device * netdev = enic -> netdev ;
9881027 struct sk_buff * skb ;
1028+ struct vnic_cq * cq = & enic -> cq [enic_cq_rq (enic , rq -> index )];
9891029
9901030 u8 type , color , eop , sop , ingress_port , vlan_stripped ;
9911031 u8 fcoe , fcoe_sof , fcoe_fc_crc_ok , fcoe_enc_error , fcoe_eof ;
@@ -1056,6 +1096,9 @@ static void enic_rq_indicate_buf(struct vnic_rq *rq,
10561096 napi_gro_receive (& enic -> napi [q_number ], skb );
10571097 else
10581098 netif_receive_skb (skb );
1099+ if (enic -> rx_coalesce_setting .use_adaptive_rx_coalesce )
1100+ enic_intr_update_pkt_size (& cq -> pkt_size_counter ,
1101+ bytes_written );
10591102 } else {
10601103
10611104 /* Buffer overflow
@@ -1134,6 +1177,64 @@ static int enic_poll(struct napi_struct *napi, int budget)
11341177 return rq_work_done ;
11351178}
11361179
1180+ static void enic_set_int_moderation (struct enic * enic , struct vnic_rq * rq )
1181+ {
1182+ unsigned int intr = enic_msix_rq_intr (enic , rq -> index );
1183+ struct vnic_cq * cq = & enic -> cq [enic_cq_rq (enic , rq -> index )];
1184+ u32 timer = cq -> tobe_rx_coal_timeval ;
1185+
1186+ if (cq -> tobe_rx_coal_timeval != cq -> cur_rx_coal_timeval ) {
1187+ vnic_intr_coalescing_timer_set (& enic -> intr [intr ], timer );
1188+ cq -> cur_rx_coal_timeval = cq -> tobe_rx_coal_timeval ;
1189+ }
1190+ }
1191+
1192+ static void enic_calc_int_moderation (struct enic * enic , struct vnic_rq * rq )
1193+ {
1194+ struct enic_rx_coal * rx_coal = & enic -> rx_coalesce_setting ;
1195+ struct vnic_cq * cq = & enic -> cq [enic_cq_rq (enic , rq -> index )];
1196+ struct vnic_rx_bytes_counter * pkt_size_counter = & cq -> pkt_size_counter ;
1197+ int index ;
1198+ u32 timer ;
1199+ u32 range_start ;
1200+ u32 traffic ;
1201+ u64 delta ;
1202+ ktime_t now = ktime_get ();
1203+
1204+ delta = ktime_us_delta (now , cq -> prev_ts );
1205+ if (delta < ENIC_AIC_TS_BREAK )
1206+ return ;
1207+ cq -> prev_ts = now ;
1208+
1209+ traffic = pkt_size_counter -> large_pkt_bytes_cnt +
1210+ pkt_size_counter -> small_pkt_bytes_cnt ;
1211+ /* The table takes Mbps
1212+ * traffic *= 8 => bits
1213+ * traffic *= (10^6 / delta) => bps
1214+ * traffic /= 10^6 => Mbps
1215+ *
1216+ * Combining, traffic *= (8 / delta)
1217+ */
1218+
1219+ traffic <<= 3 ;
1220+ traffic /= delta ;
1221+
1222+ for (index = 0 ; index < ENIC_MAX_COALESCE_TIMERS ; index ++ )
1223+ if (traffic < mod_table [index ].rx_rate )
1224+ break ;
1225+ range_start = (pkt_size_counter -> small_pkt_bytes_cnt >
1226+ pkt_size_counter -> large_pkt_bytes_cnt << 1 ) ?
1227+ rx_coal -> small_pkt_range_start :
1228+ rx_coal -> large_pkt_range_start ;
1229+ timer = range_start + ((rx_coal -> range_end - range_start ) *
1230+ mod_table [index ].range_percent / 100 );
1231+ /* Damping */
1232+ cq -> tobe_rx_coal_timeval = (timer + cq -> tobe_rx_coal_timeval ) >> 1 ;
1233+
1234+ pkt_size_counter -> large_pkt_bytes_cnt = 0 ;
1235+ pkt_size_counter -> small_pkt_bytes_cnt = 0 ;
1236+ }
1237+
11371238static int enic_poll_msix (struct napi_struct * napi , int budget )
11381239{
11391240 struct net_device * netdev = napi -> dev ;
@@ -1171,6 +1272,13 @@ static int enic_poll_msix(struct napi_struct *napi, int budget)
11711272
11721273 if (err )
11731274 work_done = work_to_do ;
1275+ if (enic -> rx_coalesce_setting .use_adaptive_rx_coalesce )
1276+ /* Call the function which refreshes
1277+ * the intr coalescing timer value based on
1278+ * the traffic. This is supported only in
1279+ * the case of MSI-x mode
1280+ */
1281+ enic_calc_int_moderation (enic , & enic -> rq [rq ]);
11741282
11751283 if (work_done < work_to_do ) {
11761284
@@ -1179,6 +1287,8 @@ static int enic_poll_msix(struct napi_struct *napi, int budget)
11791287 */
11801288
11811289 napi_complete (napi );
1290+ if (enic -> rx_coalesce_setting .use_adaptive_rx_coalesce )
1291+ enic_set_int_moderation (enic , & enic -> rq [rq ]);
11821292 vnic_intr_unmask (& enic -> intr [intr ]);
11831293 }
11841294
@@ -1314,6 +1424,42 @@ static void enic_synchronize_irqs(struct enic *enic)
13141424 }
13151425}
13161426
1427+ static void enic_set_rx_coal_setting (struct enic * enic )
1428+ {
1429+ unsigned int speed ;
1430+ int index = -1 ;
1431+ struct enic_rx_coal * rx_coal = & enic -> rx_coalesce_setting ;
1432+
1433+ /* If intr mode is not MSIX, do not do adaptive coalescing */
1434+ if (VNIC_DEV_INTR_MODE_MSIX != vnic_dev_get_intr_mode (enic -> vdev )) {
1435+ netdev_info (enic -> netdev , "INTR mode is not MSIX, Not initializing adaptive coalescing" );
1436+ return ;
1437+ }
1438+
1439+ /* 1. Read the link speed from fw
1440+ * 2. Pick the default range for the speed
1441+ * 3. Update it in enic->rx_coalesce_setting
1442+ */
1443+ speed = vnic_dev_port_speed (enic -> vdev );
1444+ if (ENIC_LINK_SPEED_10G < speed )
1445+ index = ENIC_LINK_40G_INDEX ;
1446+ else if (ENIC_LINK_SPEED_4G < speed )
1447+ index = ENIC_LINK_10G_INDEX ;
1448+ else
1449+ index = ENIC_LINK_4G_INDEX ;
1450+
1451+ rx_coal -> small_pkt_range_start = mod_range [index ].small_pkt_range_start ;
1452+ rx_coal -> large_pkt_range_start = mod_range [index ].large_pkt_range_start ;
1453+ rx_coal -> range_end = ENIC_RX_COALESCE_RANGE_END ;
1454+
1455+ /* Start with the value provided by UCSM */
1456+ for (index = 0 ; index < enic -> rq_count ; index ++ )
1457+ enic -> cq [index ].cur_rx_coal_timeval =
1458+ enic -> config .intr_timer_usec ;
1459+
1460+ rx_coal -> use_adaptive_rx_coalesce = 1 ;
1461+ }
1462+
13171463static int enic_dev_notify_set (struct enic * enic )
13181464{
13191465 int err ;
@@ -2231,6 +2377,7 @@ static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
22312377 enic -> notify_timer .function = enic_notify_timer ;
22322378 enic -> notify_timer .data = (unsigned long )enic ;
22332379
2380+ enic_set_rx_coal_setting (enic );
22342381 INIT_WORK (& enic -> reset , enic_reset );
22352382 INIT_WORK (& enic -> change_mtu_work , enic_change_mtu_work );
22362383
@@ -2250,6 +2397,9 @@ static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
22502397 }
22512398
22522399 enic -> tx_coalesce_usecs = enic -> config .intr_timer_usec ;
2400+ /* rx coalesce time already got initialized. This gets used
2401+ * if adaptive coal is turned off
2402+ */
22532403 enic -> rx_coalesce_usecs = enic -> tx_coalesce_usecs ;
22542404
22552405 if (enic_is_dynamic (enic ) || enic_is_sriov_vf (enic ))
0 commit comments