42 #include <linux/types.h>
43 #include <linux/errno.h>
44 #include <linux/pci.h>
46 #include <linux/netdevice.h>
48 #include <linux/if_vlan.h>
52 #include <linux/tcp.h>
55 #include <linux/if_arp.h>
56 #include <linux/slab.h>
57 #include <linux/prefetch.h>
65 #define ETH_P_CPL5 0xf
68 #define SGE_FREELQ_N 2
69 #define SGE_CMDQ0_E_N 1024
70 #define SGE_CMDQ1_E_N 128
71 #define SGE_FREEL_SIZE 4096
72 #define SGE_JUMBO_FREEL_SIZE 512
73 #define SGE_FREEL_REFILL_THRESH 16
74 #define SGE_RESPQ_E_N 1024
75 #define SGE_INTRTIMER_NRES 1000
76 #define SGE_RX_SM_BUF_SIZE 1536
77 #define SGE_TX_DESC_MAX_PLEN 16384
79 #define SGE_RESPQ_REPLENISH_THRES (SGE_RESPQ_E_N / 4)
85 #define TX_RECLAIM_PERIOD (HZ / 4)
87 #define M_CMD_LEN 0x7fffffff
88 #define V_CMD_LEN(v) (v)
89 #define G_CMD_LEN(v) ((v) & M_CMD_LEN)
90 #define V_CMD_GEN1(v) ((v) << 31)
91 #define V_CMD_GEN2(v) (v)
92 #define F_CMD_DATAVALID (1 << 1)
93 #define F_CMD_SOP (1 << 2)
94 #define V_CMD_EOP(v) ((v) << 3)
99 #if defined(__BIG_ENDIAN_BITFIELD)
116 u32 Cmdq1CreditReturn : 5;
117 u32 Cmdq1DmaComplete : 5;
118 u32 Cmdq0CreditReturn : 5;
119 u32 Cmdq0DmaComplete : 5;
126 u32 GenerationBit : 1;
129 #elif defined(__LITTLE_ENDIAN_BITFIELD)
146 u32 GenerationBit : 1;
153 u32 Cmdq0DmaComplete : 5;
154 u32 Cmdq0CreditReturn : 5;
155 u32 Cmdq1DmaComplete : 5;
156 u32 Cmdq1CreditReturn : 5;
245 static void restart_sched(
unsigned long);
278 0x0, 0x7, 0x43, 0x0, 0x0, 0x0
284 static void tx_sched_stop(
struct sge *
sge)
292 __skb_queue_purge(&s->
p[s->
port].skbq);
300 unsigned int mtu,
unsigned int speed)
304 unsigned int max_avail_segs;
306 pr_debug(
"t1_sched_update_params mtu=%d speed=%d\n", mtu, speed);
313 unsigned long long drain = 1024ULL * p->
speed * (p->
mtu - 40);
325 max_avail_segs =
max(1
U, 4096 / (p->
mtu - 40));
328 max_avail_segs =
max(1
U, 9000 / (p->
mtu - 40));
331 pr_debug(
"t1_sched_update_parms: mtu %u speed %u max_avail %u "
332 "max_avail_segs %u drain_bits_per_1024ns %u\n", p->
mtu,
336 return max_avail_segs * (p->
mtu - 40);
345 void t1_sched_set_max_avail_bytes(
struct sge *sge,
unsigned int val)
359 void t1_sched_set_drain_bits_per_us(
struct sge *sge,
unsigned int port,
379 return timespec_to_ktime(
ts);
385 static int tx_sched_init(
struct sge *sge)
399 skb_queue_head_init(&s->
p[i].skbq);
411 static inline int sched_update_avail(
struct sge *sge)
416 long long delta_time_ns;
418 delta_time_ns = ktime_to_ns(ktime_sub(now, s->
last_updated));
420 pr_debug(
"sched_update_avail delta=%lld\n", delta_time_ns);
421 if (delta_time_ns < 15000)
426 unsigned int delta_avail;
446 unsigned int credits)
457 skbq = &s->
p[skb->
dev->if_port].skbq;
458 __skb_queue_tail(skbq, skb);
468 s->
port = (s->
port + 1) & (MAX_NPORTS - 1);
469 skbq = &s->
p[s->
port].skbq;
471 skb = skb_peek(skbq);
477 if (len <= s->
p[s->
port].avail) {
478 s->
p[s->
port].avail -= len;
480 __skb_unlink(skb, skbq);
486 if (update-- && sched_update_avail(sge))
493 if (s->
num && !skb) {
494 struct cmdQ *
q = &sge->cmdQ[0];
501 pr_debug(
"sched_skb ret %p\n", skb);
519 static void free_freelQ_buffers(
struct pci_dev *pdev,
struct freelQ *q)
529 dev_kfree_skb(ce->
skb);
531 if (++cidx == q->
size)
539 static void free_rx_resources(
struct sge *sge)
542 unsigned int size,
i;
544 if (sge->
respQ.entries) {
545 size =
sizeof(
struct respQ_e) * sge->
respQ.size;
547 sge->
respQ.dma_addr);
554 free_freelQ_buffers(pdev, q);
558 size =
sizeof(
struct freelQ_e) * q->
size;
569 static int alloc_rx_resources(
struct sge *sge,
struct sge_params *
p)
572 unsigned int size,
i;
580 size =
sizeof(
struct freelQ_e) * q->
size;
600 sge->
freelQ[!sge->jumbo_fl].dma_offset;
614 sge->
respQ.genbit = 1;
616 sge->
respQ.credits = 0;
617 size =
sizeof(
struct respQ_e) * sge->
respQ.size;
620 if (!sge->
respQ.entries)
625 free_rx_resources(sge);
632 static void free_cmdQ_buffers(
struct sge *sge,
struct cmdQ *q,
unsigned int n)
636 unsigned int cidx = q->
cidx;
653 if (++cidx == q->
size) {
666 static void free_tx_resources(
struct sge *sge)
669 unsigned int size,
i;
672 struct cmdQ *q = &sge->cmdQ[
i];
676 free_cmdQ_buffers(sge, q, q->
in_use);
680 size =
sizeof(
struct cmdQ_e) * q->
size;
690 static int alloc_tx_resources(
struct sge *sge,
struct sge_params *p)
693 unsigned int size,
i;
696 struct cmdQ *q = &sge->cmdQ[
i];
706 size =
sizeof(
struct cmdQ_e) * q->
size;
724 sge->cmdQ[0].stop_thres = sge->
adapter->params.nports *
729 free_tx_resources(sge);
734 u32 size,
int base_reg_lo,
735 int base_reg_hi,
int size_reg)
738 writel(addr >> 32, adapter->
regs + base_reg_hi);
747 struct sge *sge = adapter->
sge;
763 static void configure_sge(
struct sge *sge,
struct sge_params *p)
765 struct adapter *ap = sge->
adapter;
768 setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
770 setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
772 setup_ring_params(ap, sge->
freelQ[0].dma_addr,
775 setup_ring_params(ap, sge->
freelQ[1].dma_addr,
782 setup_ring_params(ap, sge->
respQ.dma_addr, sge->
respQ.size,
791 #if defined(__BIG_ENDIAN_BITFIELD)
804 static inline unsigned int jumbo_payload_capacity(
const struct sge *sge)
822 free_tx_resources(sge);
823 free_rx_resources(sge);
839 static void refill_free_list(
struct sge *sge,
struct freelQ *q)
855 mapping = pci_map_single(pdev, skb->
data, dma_len,
862 e->addr_lo = (
u32)mapping;
863 e->addr_hi = (
u64)mapping >> 32;
885 static void freelQs_empty(
struct sge *sge)
887 struct adapter *adapter = sge->
adapter;
891 refill_free_list(sge, &sge->
freelQ[0]);
892 refill_free_list(sge, &sge->
freelQ[1]);
894 if (sge->
freelQ[0].credits > (sge->
freelQ[0].size >> 2) &&
910 #define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
911 #define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
912 #define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
913 F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
954 struct adapter *adapter = sge->
adapter;
960 sge->
stats.respQ_empty++;
962 sge->
stats.respQ_overflow++;
963 pr_alert(
"%s: SGE response queue overflow\n",
967 sge->
stats.freelistQ_empty++;
971 sge->
stats.pkt_too_big++;
972 pr_alert(
"%s: SGE max packet size exceeded\n",
976 sge->
stats.pkt_mismatch++;
996 memset(ss, 0,
sizeof(*ss));
1017 static void recycle_fl_buf(
struct freelQ *
fl,
int idx)
1023 to->addr_lo = from->addr_lo;
1024 to->addr_hi = from->addr_hi;
1055 struct freelQ *fl,
unsigned int len)
1065 skb_reserve(skb, 2);
1067 pci_dma_sync_single_for_cpu(pdev,
1071 skb_copy_from_linear_data(ce->
skb, skb->
data, len);
1072 pci_dma_sync_single_for_device(pdev,
1076 recycle_fl_buf(fl, fl->
cidx);
1082 recycle_fl_buf(fl, fl->
cidx);
1104 static void unexpected_offload(
struct adapter *adapter,
struct freelQ *fl)
1111 pr_err(
"%s: unexpected offload packet, cmd %u\n",
1113 recycle_fl_buf(fl, fl->
cidx);
1126 static inline unsigned int compute_large_page_tx_descs(
struct sk_buff *skb)
1128 unsigned int count = 0;
1131 unsigned int nfrags = skb_shinfo(skb)->nr_frags;
1132 unsigned int i, len = skb_headlen(skb);
1137 for (i = 0; nfrags--; i++) {
1139 len = skb_frag_size(frag);
1155 static inline void write_tx_desc(
struct cmdQ_e *e,
dma_addr_t mapping,
1156 unsigned int len,
unsigned int gen,
1161 e->addr_lo = (
u32)mapping;
1162 e->addr_hi = (
u64)mapping >> 32;
1173 static inline unsigned int write_large_page_tx_descs(
unsigned int pidx,
1178 unsigned int *desc_len,
1179 unsigned int nfrags,
1183 struct cmdQ_e *
e1 = *
e;
1189 *gen, nfrags == 0 && *desc_len == 0);
1196 if (++pidx == q->
size) {
1214 static inline void write_tx_descs(
struct adapter *adapter,
struct sk_buff *skb,
1215 unsigned int pidx,
unsigned int gen,
1219 struct cmdQ_e *
e, *
e1;
1221 unsigned int i,
flags, first_desc_len, desc_len,
1222 nfrags = skb_shinfo(skb)->nr_frags;
1227 mapping = pci_map_single(adapter->
pdev, skb->
data,
1231 desc_len = skb_headlen(skb);
1238 e->addr_lo = (
u32)desc_mapping;
1239 e->addr_hi = (
u64)desc_mapping >> 32;
1246 desc_mapping += first_desc_len;
1247 desc_len -= first_desc_len;
1250 if (++pidx == q->
size) {
1256 pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
1257 &desc_mapping, &desc_len,
1261 write_tx_desc(e1, desc_mapping, desc_len, gen,
1269 for (i = 0; nfrags--; i++) {
1273 if (++pidx == q->
size) {
1280 mapping = skb_frag_dma_map(&adapter->
pdev->dev, frag, 0,
1283 desc_len = skb_frag_size(frag);
1285 pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
1286 &desc_mapping, &desc_len,
1289 write_tx_desc(e1, desc_mapping, desc_len, gen,
1303 static inline void reclaim_completed_tx(
struct sge *sge,
struct cmdQ *q)
1308 pr_debug(
"reclaim_completed_tx processed:%d cleaned:%d\n",
1310 free_cmdQ_buffers(sge, q, reclaim);
1319 static void restart_sched(
unsigned long arg)
1321 struct sge *sge = (
struct sge *) arg;
1322 struct adapter *adapter = sge->
adapter;
1323 struct cmdQ *q = &sge->cmdQ[0];
1325 unsigned int credits, queued_skb = 0;
1327 spin_lock(&q->
lock);
1328 reclaim_completed_tx(sge, q);
1331 pr_debug(
"restart_sched credits=%d\n", credits);
1332 while ((skb = sched_skb(sge,
NULL, credits)) !=
NULL) {
1333 unsigned int genbit, pidx,
count;
1334 count = 1 + skb_shinfo(skb)->nr_frags;
1335 count += compute_large_page_tx_descs(skb);
1344 write_tx_descs(adapter, skb, pidx, genbit, q);
1356 spin_unlock(&q->
lock);
1367 static void sge_rx(
struct sge *sge,
struct freelQ *fl,
unsigned int len)
1371 struct adapter *adapter = sge->
adapter;
1377 sge->
stats.rx_drops++;
1386 __skb_pull(skb,
sizeof(*p));
1389 dev = adapter->
port[p->
iff].dev;
1398 skb_checksum_none_assert(skb);
1402 __vlan_hwaccel_put_tag(skb,
ntohs(p->
vlan));
1411 static inline int enough_free_Tx_descs(
const struct cmdQ *q)
1422 static void restart_tx_queues(
struct sge *sge)
1424 struct adapter *adap = sge->
adapter;
1427 if (!enough_free_Tx_descs(&sge->cmdQ[0]))
1434 netif_running(nd)) {
1435 sge->
stats.cmdQ_restarted[2]++;
1436 netif_wake_queue(nd);
1445 static unsigned int update_tx_info(
struct adapter *adapter,
1449 struct sge *sge = adapter->
sge;
1450 struct cmdQ *cmdq = &sge->cmdQ[0];
1466 tasklet_hi_schedule(&sge->
tx_sched->sched_tsk);
1468 flags &= ~F_CMDQ0_ENABLE;
1472 restart_tx_queues(sge);
1483 struct sge *sge = adapter->
sge;
1487 unsigned int flags = 0;
1488 unsigned int cmdq_processed[
SGE_CMDQ_N] = {0, 0};
1490 while (done < budget && e->GenerationBit == q->
genbit) {
1491 flags |= e->Qsleeping;
1493 cmdq_processed[0] += e->Cmdq0CreditReturn;
1494 cmdq_processed[1] += e->Cmdq1CreditReturn;
1500 if (
unlikely((flags & F_CMDQ0_ENABLE) || cmdq_processed[0] > 64)) {
1501 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1502 cmdq_processed[0] = 0;
1505 if (
unlikely(cmdq_processed[1] > 16)) {
1506 sge->cmdQ[1].processed += cmdq_processed[1];
1507 cmdq_processed[1] = 0;
1510 if (
likely(e->DataValid)) {
1513 BUG_ON(!e->Sop || !e->Eop);
1515 unexpected_offload(adapter, fl);
1517 sge_rx(sge, fl, e->BufferLength);
1531 refill_free_list(sge, fl);
1533 sge->
stats.pure_rsps++;
1549 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1550 sge->cmdQ[1].processed += cmdq_processed[1];
1555 static inline int responses_pending(
const struct adapter *adapter)
1557 const struct respQ *
Q = &adapter->
sge->respQ;
1560 return e->GenerationBit == Q->
genbit;
1571 static int process_pure_responses(
struct adapter *adapter)
1573 struct sge *sge = adapter->
sge;
1576 const struct freelQ *fl = &sge->
freelQ[e->FreelistQid];
1577 unsigned int flags = 0;
1578 unsigned int cmdq_processed[
SGE_CMDQ_N] = {0, 0};
1585 flags |= e->Qsleeping;
1587 cmdq_processed[0] += e->Cmdq0CreditReturn;
1588 cmdq_processed[1] += e->Cmdq1CreditReturn;
1602 sge->
stats.pure_rsps++;
1603 }
while (e->GenerationBit == q->
genbit && !e->DataValid);
1605 flags = update_tx_info(adapter, flags, cmdq_processed[0]);
1606 sge->cmdQ[1].processed += cmdq_processed[1];
1608 return e->GenerationBit == q->
genbit;
1618 struct adapter *adapter =
container_of(napi,
struct adapter, napi);
1621 if (
likely(work_done < budget)) {
1631 struct adapter *adapter =
data;
1632 struct sge *sge = adapter->
sge;
1635 if (
likely(responses_pending(adapter))) {
1638 if (napi_schedule_prep(&adapter->
napi)) {
1639 if (process_pure_responses(adapter))
1645 napi_enable(&adapter->
napi);
1651 spin_lock(&adapter->async_lock);
1653 spin_unlock(&adapter->async_lock);
1656 sge->
stats.unhandled_irqs++;
1674 static int t1_sge_tx(
struct sk_buff *skb,
struct adapter *adapter,
1677 struct sge *sge = adapter->
sge;
1678 struct cmdQ *q = &sge->cmdQ[
qid];
1681 if (!spin_trylock(&q->
lock))
1684 reclaim_completed_tx(sge, q);
1688 count = 1 + skb_shinfo(skb)->nr_frags;
1689 count += compute_large_page_tx_descs(skb);
1693 if (!netif_queue_stopped(dev)) {
1694 netif_stop_queue(dev);
1696 sge->
stats.cmdQ_full[2]++;
1697 pr_err(
"%s: Tx ring full while queue awake!\n",
1700 spin_unlock(&q->
lock);
1705 netif_stop_queue(dev);
1707 sge->
stats.cmdQ_full[2]++;
1719 skb = sched_skb(sge, skb, credits);
1721 spin_unlock(&q->
lock);
1725 count = 1 + skb_shinfo(skb)->nr_frags;
1726 count += compute_large_page_tx_descs(skb);
1737 spin_unlock(&q->
lock);
1739 write_tx_descs(adapter, skb, pidx, genbit, q);
1758 if (use_sched_skb) {
1759 if (spin_trylock(&q->
lock)) {
1768 #define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))
1776 static inline int eth_hdr_len(
const void *
data)
1788 struct adapter *adapter = dev->
ml_priv;
1789 struct sge *sge = adapter->
sge;
1810 if (skb_shinfo(skb)->gso_size) {
1816 eth_type = skb_network_offset(skb) ==
ETH_HLEN ?
1825 skb_shinfo(skb)->gso_size));
1836 skb->
len > dev->
mtu + eth_hdr_len(skb->
data))) {
1837 pr_debug(
"%s: packet size %d hdr %d mtu%d\n", dev->
name,
1846 pr_debug(
"%s: unable to do udp checksum\n", dev->
name);
1867 cpl = (
struct cpl_tx_pkt *)__skb_push(skb,
sizeof(*cpl));
1885 ret = t1_sge_tx(skb, adapter, 0, dev);
1900 static void sge_tx_reclaim_cb(
unsigned long data)
1903 struct sge *sge = (
struct sge *)data;
1906 struct cmdQ *q = &sge->cmdQ[
i];
1908 if (!spin_trylock(&q->
lock))
1911 reclaim_completed_tx(sge, q);
1912 if (i == 0 && q->
in_use) {
1915 spin_unlock(&q->
lock);
1926 core_ticks_per_usec(sge->
adapter);
1937 if (alloc_rx_resources(sge, p))
1939 if (alloc_tx_resources(sge, p)) {
1940 free_rx_resources(sge);
1943 configure_sge(sge, p);
1980 refill_free_list(sge, &sge->
freelQ[0]);
1981 refill_free_list(sge, &sge->
freelQ[1]);
1996 static void espibug_workaround_t204(
unsigned long data)
1998 struct adapter *adapter = (
struct adapter *)data;
1999 struct sge *sge = adapter->
sge;
2000 unsigned int nports = adapter->
params.nports;
2009 for (i = 0; i < nports; i++) {
2012 if (!netif_running(adapter->
port[i].dev) ||
2013 netif_queue_stopped(adapter->
port[i].dev) ||
2014 !seop[
i] || ((seop[
i] & 0xfff) != 0) || !
skb)
2018 skb_copy_to_linear_data_offset(skb,
2022 skb_copy_to_linear_data_offset(skb,
2033 t1_sge_tx(skb, adapter, 0, adapter->
port[i].dev);
2039 static void espibug_workaround(
unsigned long data)
2041 struct adapter *adapter = (
struct adapter *)data;
2042 struct sge *sge = adapter->
sge;
2044 if (netif_running(adapter->
port[0].dev)) {
2048 if ((seop & 0xfff0fff) == 0xfff && skb) {
2050 skb_copy_to_linear_data_offset(skb,
2054 skb_copy_to_linear_data_offset(skb,
2065 t1_sge_tx(skb, adapter, 0, adapter->
port[0].dev);
2077 struct sge *sge = kzalloc(
sizeof(*sge),
GFP_KERNEL);
2101 if (adapter->
params.nports > 1) {