36 #include <linux/netdevice.h>
38 #include <linux/if_vlan.h>
42 #include <linux/prefetch.h>
43 #include <linux/export.h>
56 # define FL_PG_ORDER 0
58 # define FL_PG_ORDER (16 - PAGE_SHIFT)
62 #define RX_COPY_THRES 256
63 #define RX_PULL_LEN 128
69 #define RX_PKT_SKB_LEN 512
77 #define MAX_TX_RECLAIM 16
83 #define MAX_RX_REFILL 16U
89 #define RX_QCHECK_PERIOD (HZ / 2)
94 #define TX_QCHECK_PERIOD (HZ / 2)
99 #define MAX_TIMER_TX_RECLAIM 100
104 #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
110 #define FL_STARVE_THRES 4
117 #define ETHTXQ_STOP_THRES \
118 (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
124 #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
130 #define MAX_IMM_TX_PKT_LEN 128
135 #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
153 #define FL_MTU_SMALL 1500
154 #define FL_MTU_LARGE 9000
156 static inline unsigned int fl_mtu_bufsize(
struct adapter *
adapter,
164 #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
165 #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
201 static inline bool is_buf_mapped(
const struct rx_sw_desc *
d)
213 static inline unsigned int txq_avail(
const struct sge_txq *
q)
226 static inline unsigned int fl_cap(
const struct sge_fl *
fl)
231 static inline bool fl_starving(
const struct sge_fl *
fl)
246 si = skb_shinfo(skb);
249 for (fp = si->
frags; fp < end; fp++) {
250 *++addr = skb_frag_dma_map(dev, fp, 0, skb_frag_size(fp),
258 while (fp-- > si->
frags)
266 #ifdef CONFIG_NEED_DMA_MAP_STATE
267 static void unmap_skb(
struct device *dev,
const struct sk_buff *skb,
275 si = skb_shinfo(skb);
277 for (fp = si->
frags; fp < end; fp++)
289 static void deferred_unmap_destructor(
struct sk_buff *skb)
295 static void unmap_sgl(
struct device *dev,
const struct sk_buff *skb,
299 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
301 if (
likely(skb_headlen(skb)))
314 for (p = sgl->
sge; nfrags >= 2; nfrags -= 2) {
321 }
else if ((
u8 *)p == (
u8 *)q->
stat) {
324 }
else if ((
u8 *)p + 8 == (
u8 *)q->
stat) {
365 unsigned int n,
bool unmap)
368 unsigned int cidx = q->
cidx;
375 unmap_sgl(dev, d->
skb, d->
sgl, q);
380 if (++cidx == q->
size) {
391 static inline int reclaimable(
const struct sge_txq *q)
395 return hw_cidx < 0 ? hw_cidx + q->
size : hw_cidx;
408 static inline void reclaim_completed_tx(
struct adapter *adap,
struct sge_txq *q,
411 int avail = reclaimable(q);
421 free_tx_desc(adap, q, avail, unmap);
433 switch (rx_buf_size_idx) {
466 static void free_rx_bufs(
struct adapter *adap,
struct sge_fl *q,
int n)
471 if (is_buf_mapped(d))
473 get_buf_size(adap, d),
494 static void unmap_rx_buf(
struct adapter *adap,
struct sge_fl *q)
498 if (is_buf_mapped(d))
507 static inline void ring_fl_db(
struct adapter *adap,
struct sge_fl *q)
538 static unsigned int refill_fl(
struct adapter *adap,
struct sge_fl *q,
int n,
541 struct sge *s = &adap->
sge;
551 goto alloc_small_pages;
573 set_rx_sw_desc(sd, pg, mapping);
587 pg = __skb_alloc_page(gfp,
NULL);
601 set_rx_sw_desc(sd, pg, mapping);
619 adap->
sge.starving_fl);
625 static inline void __refill_fl(
struct adapter *adap,
struct sge_fl *
fl)
650 static void *alloc_ring(
struct device *dev,
size_t nelem,
size_t elem_size,
652 size_t stat_size,
int node)
654 size_t len = nelem * elem_size + stat_size;
661 s = kzalloc_node(nelem * sw_size,
GFP_KERNEL, node);
669 *(
void **)metadata = s;
681 static inline unsigned int sgl_len(
unsigned int n)
684 return (3 * n) / 2 + (n & 1) + 2;
694 static inline unsigned int flits_to_desc(
unsigned int n)
707 static inline int is_eth_imm(
const struct sk_buff *skb)
719 static inline unsigned int calc_tx_flits(
const struct sk_buff *skb)
726 flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
727 if (skb_shinfo(skb)->gso_size)
739 static inline unsigned int calc_tx_descs(
const struct sk_buff *skb)
741 return flits_to_desc(calc_tx_flits(skb));
761 static void write_sgl(
const struct sk_buff *skb,
struct sge_txq *q,
771 len = skb_headlen(skb) -
start;
782 if (
likely(--nfrags == 0))
791 for (i = (nfrags != si->
nr_frags); nfrags >= 2; nfrags -= 2, to++) {
823 static inline void ring_tx_db(
struct adapter *adap,
struct sge_txq *q,
int n)
846 static void inline_tx_skb(
const struct sk_buff *skb,
const struct sge_txq *q,
854 skb_copy_from_linear_data(skb, pos, skb->
len);
861 pos = (
void *)q->
desc + (skb->
len - left);
877 const struct iphdr *iph = ip_hdr(skb);
879 if (iph->version == 4) {
910 int start = skb_transport_offset(skb);
919 netif_tx_stop_queue(q->
txq);
923 static inline void txq_advance(
struct sge_txq *q,
unsigned int n)
943 unsigned int flits,
ndesc;
944 struct adapter *adap;
957 out_free: dev_kfree_skb(skb);
961 pi = netdev_priv(dev);
963 qidx = skb_get_queue_mapping(skb);
966 reclaim_completed_tx(adap, &q->
q,
true);
968 flits = calc_tx_flits(skb);
969 ndesc = flits_to_desc(flits);
970 credits = txq_avail(&q->
q) -
ndesc;
975 "%s: Tx ring %u full while queue awake!\n",
980 if (!is_eth_imm(skb) &&
992 wr = (
void *)&q->
q.desc[q->
q.pidx];
995 end = (
u64 *)wr + flits;
997 ssi = skb_shinfo(skb);
1001 int l3hdr_len = skb_network_header_len(skb);
1002 int eth_xtra_len = skb_network_offset(skb) -
ETH_HLEN;
1012 lso->
c.ipid_ofst =
htons(0);
1014 lso->
c.seqno_offset =
htonl(0);
1016 cpl = (
void *)(lso + 1);
1025 len = is_eth_imm(skb) ? skb->
len +
sizeof(*cpl) :
sizeof(*cpl);
1028 cpl = (
void *)(wr + 1);
1047 if (is_eth_imm(skb)) {
1048 inline_tx_skb(skb, &q->
q, cpl + 1);
1053 write_sgl(skb, &q->
q, (
struct ulptx_sgl *)(cpl + 1), end, 0,
1057 last_desc = q->
q.pidx + ndesc - 1;
1058 if (last_desc >= q->
q.size)
1059 last_desc -= q->
q.size;
1060 q->
q.sdesc[last_desc].skb =
skb;
1061 q->
q.sdesc[last_desc].sgl = (
struct ulptx_sgl *)(cpl + 1);
1064 txq_advance(&q->
q, ndesc);
1066 ring_tx_db(adap, &q->
q, ndesc);
1078 static inline void reclaim_completed_tx_imm(
struct sge_txq *q)
1096 static inline int is_imm(
const struct sk_buff *skb)
1113 reclaim_completed_tx_imm(&q->
q);
1141 spin_lock(&q->
sendq.lock);
1145 __skb_queue_tail(&q->
sendq, skb);
1146 spin_unlock(&q->
sendq.lock);
1151 inline_tx_skb(skb, &q->
q, wr);
1153 txq_advance(&q->
q, ndesc);
1155 ctrlq_check_stop(q, wr);
1157 ring_tx_db(q->
adap, &q->
q, ndesc);
1158 spin_unlock(&q->
sendq.lock);
1170 static void restart_ctrlq(
unsigned long data)
1173 unsigned int written = 0;
1176 spin_lock(&q->
sendq.lock);
1177 reclaim_completed_tx_imm(&q->
q);
1180 while ((skb = __skb_dequeue(&q->
sendq)) !=
NULL) {
1182 unsigned int ndesc = skb->
priority;
1188 spin_unlock(&q->
sendq.lock);
1191 inline_tx_skb(skb, &q->
q, wr);
1195 txq_advance(&q->
q, ndesc);
1197 unsigned long old = q->
q.stops;
1199 ctrlq_check_stop(q, wr);
1200 if (q->
q.stops != old) {
1201 spin_lock(&q->
sendq.lock);
1206 ring_tx_db(q->
adap, &q->
q, written);
1209 spin_lock(&q->
sendq.lock);
1212 ringdb:
if (written)
1213 ring_tx_db(q->
adap, &q->
q, written);
1214 spin_unlock(&q->
sendq.lock);
1229 ret = ctrl_xmit(&adap->
sge.ctrlq[0], skb);
1241 static inline int is_ofld_imm(
const struct sk_buff *skb)
1254 static inline unsigned int calc_tx_flits_ofld(
const struct sk_buff *skb)
1256 unsigned int flits,
cnt;
1258 if (is_ofld_imm(skb))
1261 flits = skb_transport_offset(skb) / 8
U;
1262 cnt = skb_shinfo(skb)->nr_frags;
1265 return flits + sgl_len(cnt);
1282 q->
adap->sge.txq_maperr);
1314 unsigned int written = 0;
1315 unsigned int flits,
ndesc;
1322 spin_unlock(&q->
sendq.lock);
1324 reclaim_completed_tx(q->
adap, &q->
q,
false);
1327 ndesc = flits_to_desc(flits);
1328 credits = txq_avail(&q->
q) -
ndesc;
1331 ofldtxq_stop(q, skb);
1333 pos = (
u64 *)&q->
q.desc[q->
q.pidx];
1334 if (is_ofld_imm(skb))
1335 inline_tx_skb(skb, &q->
q, pos);
1336 else if (map_skb(q->
adap->pdev_dev, skb,
1339 spin_lock(&q->
sendq.lock);
1342 int last_desc,
hdr_len = skb_transport_offset(skb);
1345 write_sgl(skb, &q->
q, (
void *)pos + hdr_len,
1346 pos + flits, hdr_len,
1348 #ifdef CONFIG_NEED_DMA_MAP_STATE
1352 last_desc = q->
q.pidx + ndesc - 1;
1353 if (last_desc >= q->
q.size)
1354 last_desc -= q->
q.size;
1355 q->
q.sdesc[last_desc].skb =
skb;
1358 txq_advance(&q->
q, ndesc);
1361 ring_tx_db(q->
adap, &q->
q, written);
1365 spin_lock(&q->
sendq.lock);
1366 __skb_unlink(skb, &q->
sendq);
1367 if (is_ofld_imm(skb))
1371 ring_tx_db(q->
adap, &q->
q, written);
1383 skb->
priority = calc_tx_flits_ofld(skb);
1384 spin_lock(&q->
sendq.lock);
1385 __skb_queue_tail(&q->
sendq, skb);
1386 if (q->
sendq.qlen == 1)
1388 spin_unlock(&q->
sendq.lock);
1398 static void restart_ofldq(
unsigned long data)
1402 spin_lock(&q->
sendq.lock);
1405 spin_unlock(&q->
sendq.lock);
1415 static inline unsigned int skb_txq(
const struct sk_buff *skb)
1427 static inline unsigned int is_ctrl_pkt(
const struct sk_buff *skb)
1432 static inline int ofld_send(
struct adapter *adap,
struct sk_buff *skb)
1434 unsigned int idx = skb_txq(skb);
1437 return ctrl_xmit(&adap->
sge.ctrlq[idx], skb);
1438 return ofld_xmit(&adap->
sge.ofldtxq[idx], skb);
1455 ret = ofld_send(adap, skb);
1474 static inline void copy_frags(
struct sk_buff *skb,
1480 __skb_fill_page_desc(skb, 0, gl->
frags[0].page,
1481 gl->
frags[0].offset + offset,
1482 gl->
frags[0].size - offset);
1483 skb_shinfo(skb)->nr_frags = gl->
nfrags;
1484 for (i = 1; i < gl->
nfrags; i++)
1485 __skb_fill_page_desc(skb, i, gl->
frags[i].page,
1486 gl->
frags[i].offset,
1503 unsigned int skb_len,
unsigned int pull_len)
1513 skb = dev_alloc_skb(gl->
tot_len);
1517 skb_copy_to_linear_data(skb, gl->
va, gl->
tot_len);
1519 skb = dev_alloc_skb(skb_len);
1522 __skb_put(skb, pull_len);
1523 skb_copy_to_linear_data(skb, gl->
va, pull_len);
1525 copy_frags(skb, gl, pull_len);
1541 static void t4_pktgl_free(
const struct pkt_gl *gl)
1554 static noinline int handle_trace_pkt(
struct adapter *adap,
1567 __skb_pull(skb,
sizeof(*p));
1568 skb_reset_mac_header(skb);
1578 struct adapter *adapter = rxq->
rspq.adap;
1579 struct sge *s = &adapter->
sge;
1586 rxq->
stats.rx_drops++;
1595 skb_record_rx_queue(skb, rxq->
rspq.idx);
1600 __vlan_hwaccel_put_tag(skb,
ntohs(pkt->
vlan));
1601 rxq->
stats.vlan_ex++;
1605 rxq->
stats.lro_pkts++;
1607 rxq->
stats.lro_merged++;
1609 rxq->
stats.rx_cso++;
1627 struct sge *s = &q->
adap->sge;
1630 return handle_trace_pkt(q->
adap, si);
1636 do_gro(rxq, si, pkt);
1643 rxq->
stats.rx_drops++;
1649 skb_record_rx_queue(skb, q->
idx);
1659 rxq->
stats.rx_cso++;
1662 skb->
csum = csum_unfold(c);
1664 rxq->
stats.rx_cso++;
1667 skb_checksum_none_assert(skb);
1670 __vlan_hwaccel_put_tag(skb,
ntohs(pkt->
vlan));
1671 rxq->
stats.vlan_ex++;
1692 static void restore_rx_bufs(
const struct pkt_gl *si,
struct sge_fl *q,
1717 static inline bool is_new_response(
const struct rsp_ctrl *
r,
1729 static inline void rspq_next(
struct sge_rspq *q)
1755 int budget_left = budget;
1758 struct adapter *adapter = q->
adap;
1759 struct sge *s = &adapter->
sge;
1761 while (
likely(budget_left)) {
1763 if (!is_new_response(rc, q))
1776 free_rx_bufs(q->
adap, &rxq->
fl, 1);
1784 for (frags = 0, fp = si.
frags; ; frags++, fp++) {
1785 rsd = &rxq->
fl.sdesc[rxq->
fl.cidx];
1786 bufsz = get_buf_size(adapter, rsd);
1793 unmap_rx_buf(q->
adap, &rxq->
fl);
1813 restore_rx_bufs(&si, &rxq->
fl, frags);
1830 if (q->
offset >= 0 && rxq->
fl.size - rxq->
fl.avail >= 16)
1831 __refill_fl(q->
adap, &rxq->
fl);
1832 return budget - budget_left;
1852 if (
likely(work_done < budget)) {
1871 napi_schedule(&q->
napi);
1879 static unsigned int process_intrq(
struct adapter *adap)
1885 spin_lock(&adap->
sge.intrq_lock);
1886 for (credits = 0; ; credits++) {
1888 if (!is_new_response(rc, q))
1895 qid -= adap->
sge.ingr_start;
1896 napi_schedule(&adap->
sge.ingr_map[qid]->napi);
1904 spin_unlock(&adap->
sge.intrq_lock);
1914 struct adapter *adap =
cookie;
1917 process_intrq(adap);
1928 struct adapter *adap =
cookie;
1949 return t4_intr_intx;
1952 static void sge_rx_timer_cb(
unsigned long data)
1955 unsigned int i, cnt[2];
1956 struct adapter *adap = (
struct adapter *)data;
1957 struct sge *s = &adap->
sge;
1959 for (i = 0; i <
ARRAY_SIZE(s->starving_fl); i++)
1960 for (m = s->starving_fl[i]; m; m &= m - 1) {
1968 if (fl_starving(fl)) {
1970 if (napi_reschedule(&rxq->
rspq.napi))
1981 for (i = 0; i < 2; i++)
1983 if (s->
idma_state[i] || cnt[i] == 0xffffffff)
1989 "SGE idma%u starvation detected for "
1990 "queue %lu\n", i, m & 0xffff);
1997 static void sge_tx_timer_cb(
unsigned long data)
2000 unsigned int i, budget;
2001 struct adapter *adap = (
struct adapter *)data;
2002 struct sge *s = &adap->
sge;
2004 for (i = 0; i <
ARRAY_SIZE(s->txq_maperr); i++)
2005 for (m = s->txq_maperr[i]; m; m &= m - 1) {
2020 __netif_tx_trylock(q->
txq)) {
2021 int avail = reclaimable(&q->
q);
2027 free_tx_desc(adap, &q->
q, avail,
true);
2031 __netif_tx_unlock(q->
txq);
2047 struct sge *s = &adap->
sge;
2048 struct port_info *pi = netdev_priv(dev);
2058 memset(&c, 0,
sizeof(c));
2095 ret = t4_wr_mbox(adap, adap->
fn, &c,
sizeof(c), &c);
2112 iq->
offset = fl ? 0 : -1;
2134 if (fl && fl->
desc) {
2144 static void init_txq(
struct adapter *adap,
struct sge_txq *q,
unsigned int id)
2152 adap->
sge.egr_map[
id - adap->
sge.egr_start] =
q;
2161 struct sge *s = &adap->
sge;
2162 struct port_info *pi = netdev_priv(dev);
2167 txq->
q.desc = alloc_ring(adap->
pdev_dev, txq->
q.size,
2169 &txq->
q.phys_addr, &txq->
q.sdesc, s->
stat_len,
2170 netdev_queue_numa_node_read(netdevq));
2174 memset(&c, 0,
sizeof(c));
2191 ret = t4_wr_mbox(adap, adap->
fn, &c,
sizeof(c), &c);
2194 txq->
q.sdesc =
NULL;
2196 nentries *
sizeof(
struct tx_desc),
2197 txq->
q.desc, txq->
q.phys_addr);
2211 unsigned int cmplqid)
2215 struct sge *s = &adap->
sge;
2216 struct port_info *pi = netdev_priv(dev);
2221 txq->
q.desc = alloc_ring(adap->
pdev_dev, nentries,
2222 sizeof(
struct tx_desc), 0, &txq->
q.phys_addr,
2245 ret = t4_wr_mbox(adap, adap->
fn, &c,
sizeof(c), &c);
2248 nentries *
sizeof(
struct tx_desc),
2249 txq->
q.desc, txq->
q.phys_addr);
2256 skb_queue_head_init(&txq->
sendq);
2267 struct sge *s = &adap->
sge;
2268 struct port_info *pi = netdev_priv(dev);
2273 txq->
q.desc = alloc_ring(adap->
pdev_dev, txq->
q.size,
2275 &txq->
q.phys_addr, &txq->
q.sdesc, s->
stat_len,
2280 memset(&c, 0,
sizeof(c));
2297 ret = t4_wr_mbox(adap, adap->
fn, &c,
sizeof(c), &c);
2300 txq->
q.sdesc =
NULL;
2302 nentries *
sizeof(
struct tx_desc),
2303 txq->
q.desc, txq->
q.phys_addr);
2310 skb_queue_head_init(&txq->
sendq);
2313 txq->mapping_err = 0;
2317 static void free_txq(
struct adapter *adap,
struct sge_txq *q)
2319 struct sge *s = &adap->
sge;
2329 static void free_rspq_fl(
struct adapter *adap,
struct sge_rspq *
rq,
2332 struct sge *s = &adap->
sge;
2333 unsigned int fl_id = fl ? fl->
cntxt_id : 0xffff;
2346 free_rx_bufs(adap, fl, fl->
avail);
2370 for (i = 0; i < adap->
sge.ethqsets; i++, eq++, etq++) {
2372 free_rspq_fl(adap, &eq->rspq, &eq->fl);
2376 free_tx_desc(adap, &etq->
q, etq->
q.in_use,
true);
2378 free_txq(adap, &etq->
q);
2383 for (i = 0; i < adap->
sge.ofldqsets; i++, oq++) {
2385 free_rspq_fl(adap, &oq->
rspq, &oq->
fl);
2387 for (i = 0, oq = adap->
sge.rdmarxq; i < adap->
sge.
rdmaqs; i++, oq++) {
2389 free_rspq_fl(adap, &oq->
rspq, &oq->
fl);
2400 free_tx_desc(adap, &q->
q, q->
q.in_use,
false);
2402 __skb_queue_purge(&q->
sendq);
2403 free_txq(adap, &q->
q);
2415 __skb_queue_purge(&cq->
sendq);
2416 free_txq(adap, &cq->
q);
2420 if (adap->
sge.fw_evtq.desc)
2421 free_rspq_fl(adap, &adap->
sge.fw_evtq,
NULL);
2423 if (adap->
sge.intrq.desc)
2424 free_rspq_fl(adap, &adap->
sge.intrq,
NULL);
2427 memset(adap->
sge.egr_map, 0,
sizeof(adap->
sge.egr_map));
2432 adap->
sge.ethtxq_rover = 0;
2448 struct sge *s = &adap->
sge;
2493 static int t4_sge_init_soft(
struct adapter *adap)
2495 struct sge *s = &adap->
sge;
2496 u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
2497 u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
2498 u32 ingress_rx_threshold;
2519 #define READ_FL_BUF(x) \
2520 t4_read_reg(adap, SGE_FL_BUFFER_SIZE0+(x)*sizeof(u32))
2530 (fl_large_pg != 0 && (fl_large_pg <= fl_small_pg ||
2531 (fl_large_pg & (fl_large_pg-1)) != 0))) {
2533 fl_small_pg, fl_large_pg);
2542 fl_small_mtu, fl_large_mtu);
2553 s->
timer_val[0] = core_ticks_to_us(adap,
2555 s->
timer_val[1] = core_ticks_to_us(adap,
2557 s->
timer_val[2] = core_ticks_to_us(adap,
2559 s->
timer_val[3] = core_ticks_to_us(adap,
2561 s->
timer_val[4] = core_ticks_to_us(adap,
2563 s->
timer_val[5] = core_ticks_to_us(adap,
2575 static int t4_sge_init_hard(
struct adapter *adap)
2577 struct sge *s = &adap->
sge;
2636 struct sge *s = &adap->
sge;
2651 ret = t4_sge_init_soft(adap);
2653 ret = t4_sge_init_hard(adap);