blob: 0033971b5b2b7197595fcaeaecd98ee4ae17d2f4 [file] [log] [blame]
Sony Chackoc70001a2012-11-28 04:34:26 +00001#include <linux/netdevice.h>
2#include <linux/if_vlan.h>
3#include <net/ip.h>
4#include <linux/ipv6.h>
5
6#include "qlcnic.h"
7
Sony Chackoc70001a2012-11-28 04:34:26 +00008#define TX_ETHER_PKT 0x01
9#define TX_TCP_PKT 0x02
10#define TX_UDP_PKT 0x03
11#define TX_IP_PKT 0x04
12#define TX_TCP_LSO 0x05
13#define TX_TCP_LSO6 0x06
14#define TX_TCPV6_PKT 0x0b
15#define TX_UDPV6_PKT 0x0c
16#define FLAGS_VLAN_TAGGED 0x10
17#define FLAGS_VLAN_OOB 0x40
18
19#define qlcnic_set_tx_vlan_tci(cmd_desc, v) \
20 (cmd_desc)->vlan_TCI = cpu_to_le16(v);
21#define qlcnic_set_cmd_desc_port(cmd_desc, var) \
22 ((cmd_desc)->port_ctxid |= ((var) & 0x0F))
23#define qlcnic_set_cmd_desc_ctxid(cmd_desc, var) \
24 ((cmd_desc)->port_ctxid |= ((var) << 4 & 0xF0))
25
26#define qlcnic_set_tx_port(_desc, _port) \
27 ((_desc)->port_ctxid = ((_port) & 0xf) | (((_port) << 4) & 0xf0))
28
29#define qlcnic_set_tx_flags_opcode(_desc, _flags, _opcode) \
30 ((_desc)->flags_opcode |= \
31 cpu_to_le16(((_flags) & 0x7f) | (((_opcode) & 0x3f) << 7)))
32
33#define qlcnic_set_tx_frags_len(_desc, _frags, _len) \
34 ((_desc)->nfrags__length = \
35 cpu_to_le32(((_frags) & 0xff) | (((_len) & 0xffffff) << 8)))
36
37/* owner bits of status_desc */
38#define STATUS_OWNER_HOST (0x1ULL << 56)
39#define STATUS_OWNER_PHANTOM (0x2ULL << 56)
40
41/* Status descriptor:
42 0-3 port, 4-7 status, 8-11 type, 12-27 total_length
43 28-43 reference_handle, 44-47 protocol, 48-52 pkt_offset
44 53-55 desc_cnt, 56-57 owner, 58-63 opcode
45 */
46#define qlcnic_get_sts_port(sts_data) \
47 ((sts_data) & 0x0F)
48#define qlcnic_get_sts_status(sts_data) \
49 (((sts_data) >> 4) & 0x0F)
50#define qlcnic_get_sts_type(sts_data) \
51 (((sts_data) >> 8) & 0x0F)
52#define qlcnic_get_sts_totallength(sts_data) \
53 (((sts_data) >> 12) & 0xFFFF)
54#define qlcnic_get_sts_refhandle(sts_data) \
55 (((sts_data) >> 28) & 0xFFFF)
56#define qlcnic_get_sts_prot(sts_data) \
57 (((sts_data) >> 44) & 0x0F)
58#define qlcnic_get_sts_pkt_offset(sts_data) \
59 (((sts_data) >> 48) & 0x1F)
60#define qlcnic_get_sts_desc_cnt(sts_data) \
61 (((sts_data) >> 53) & 0x7)
62#define qlcnic_get_sts_opcode(sts_data) \
63 (((sts_data) >> 58) & 0x03F)
64
65#define qlcnic_get_lro_sts_refhandle(sts_data) \
Sony Chacko5796bd02012-12-04 03:33:57 +000066 ((sts_data) & 0x07FFF)
Sony Chackoc70001a2012-11-28 04:34:26 +000067#define qlcnic_get_lro_sts_length(sts_data) \
68 (((sts_data) >> 16) & 0x0FFFF)
69#define qlcnic_get_lro_sts_l2_hdr_offset(sts_data) \
70 (((sts_data) >> 32) & 0x0FF)
71#define qlcnic_get_lro_sts_l4_hdr_offset(sts_data) \
72 (((sts_data) >> 40) & 0x0FF)
73#define qlcnic_get_lro_sts_timestamp(sts_data) \
74 (((sts_data) >> 48) & 0x1)
75#define qlcnic_get_lro_sts_type(sts_data) \
76 (((sts_data) >> 49) & 0x7)
77#define qlcnic_get_lro_sts_push_flag(sts_data) \
78 (((sts_data) >> 52) & 0x1)
79#define qlcnic_get_lro_sts_seq_number(sts_data) \
80 ((sts_data) & 0x0FFFFFFFF)
81#define qlcnic_get_lro_sts_mss(sts_data1) \
82 ((sts_data1 >> 32) & 0x0FFFF)
83
84/* opcode field in status_desc */
85#define QLCNIC_SYN_OFFLOAD 0x03
86#define QLCNIC_RXPKT_DESC 0x04
87#define QLCNIC_OLD_RXPKT_DESC 0x3f
88#define QLCNIC_RESPONSE_DESC 0x05
89#define QLCNIC_LRO_DESC 0x12
90
Sony Chacko4be41e92013-01-01 03:20:20 +000091#define QLCNIC_TX_POLL_BUDGET 128
92#define QLCNIC_TCP_HDR_SIZE 20
93#define QLCNIC_TCP_TS_OPTION_SIZE 12
94#define QLCNIC_FETCH_RING_ID(handle) ((handle) >> 63)
95#define QLCNIC_DESC_OWNER_FW cpu_to_le64(STATUS_OWNER_PHANTOM)
96
97#define QLCNIC_TCP_TS_HDR_SIZE (QLCNIC_TCP_HDR_SIZE + QLCNIC_TCP_TS_OPTION_SIZE)
98
Sony Chackoc70001a2012-11-28 04:34:26 +000099/* for status field in status_desc */
100#define STATUS_CKSUM_LOOP 0
101#define STATUS_CKSUM_OK 2
Sony Chackod17dd0d2012-11-28 04:34:27 +0000102
Sony Chacko4be41e92013-01-01 03:20:20 +0000103#define qlcnic_83xx_pktln(sts) ((sts >> 32) & 0x3FFF)
104#define qlcnic_83xx_hndl(sts) ((sts >> 48) & 0x7FFF)
105#define qlcnic_83xx_csum_status(sts) ((sts >> 39) & 7)
106#define qlcnic_83xx_opcode(sts) ((sts >> 42) & 0xF)
107#define qlcnic_83xx_vlan_tag(sts) (((sts) >> 48) & 0xFFFF)
108#define qlcnic_83xx_lro_pktln(sts) (((sts) >> 32) & 0x3FFF)
109#define qlcnic_83xx_l2_hdr_off(sts) (((sts) >> 16) & 0xFF)
110#define qlcnic_83xx_l4_hdr_off(sts) (((sts) >> 24) & 0xFF)
111#define qlcnic_83xx_pkt_cnt(sts) (((sts) >> 16) & 0x7)
112#define qlcnic_83xx_is_tstamp(sts) (((sts) >> 40) & 1)
113#define qlcnic_83xx_is_psh_bit(sts) (((sts) >> 41) & 1)
114#define qlcnic_83xx_is_ip_align(sts) (((sts) >> 46) & 1)
115#define qlcnic_83xx_has_vlan_tag(sts) (((sts) >> 47) & 1)
116
117struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *,
118 struct qlcnic_host_rds_ring *, u16, u16);
119
120inline void qlcnic_83xx_enable_tx_intr(struct qlcnic_adapter *adapter,
121 struct qlcnic_host_tx_ring *tx_ring)
122{
123 writel(0, tx_ring->crb_intr_mask);
124}
125
126inline void qlcnic_83xx_disable_tx_intr(struct qlcnic_adapter *adapter,
127 struct qlcnic_host_tx_ring *tx_ring)
128{
129 writel(1, tx_ring->crb_intr_mask);
130}
131
132static inline u8 qlcnic_mac_hash(u64 mac)
133{
134 return (u8)((mac & 0xff) ^ ((mac >> 40) & 0xff));
135}
136
137static inline u32 qlcnic_get_ref_handle(struct qlcnic_adapter *adapter,
138 u16 handle, u8 ring_id)
139{
140 if (adapter->pdev->device == PCI_DEVICE_ID_QLOGIC_QLE834X)
141 return handle | (ring_id << 15);
142 else
143 return handle;
144}
145
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000146void qlcnic_82xx_change_filter(struct qlcnic_adapter *adapter, u64 *uaddr,
147 __le16 vlan_id)
Sony Chackoc70001a2012-11-28 04:34:26 +0000148{
149 struct cmd_desc_type0 *hwdesc;
150 struct qlcnic_nic_req *req;
151 struct qlcnic_mac_req *mac_req;
152 struct qlcnic_vlan_req *vlan_req;
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000153 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
Sony Chackoc70001a2012-11-28 04:34:26 +0000154 u32 producer;
155 u64 word;
156
157 producer = tx_ring->producer;
158 hwdesc = &tx_ring->desc_head[tx_ring->producer];
159
160 req = (struct qlcnic_nic_req *)hwdesc;
161 memset(req, 0, sizeof(struct qlcnic_nic_req));
162 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
163
164 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
165 req->req_hdr = cpu_to_le64(word);
166
167 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
168 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
169 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
170
171 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
172 vlan_req->vlan_id = vlan_id;
173
174 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
175 smp_mb();
176}
177
Sony Chackod17dd0d2012-11-28 04:34:27 +0000178static void qlcnic_send_filter(struct qlcnic_adapter *adapter,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000179 struct cmd_desc_type0 *first_desc,
180 struct sk_buff *skb)
Sony Chackoc70001a2012-11-28 04:34:26 +0000181{
Sony Chackoc70001a2012-11-28 04:34:26 +0000182 struct qlcnic_filter *fil, *tmp_fil;
183 struct hlist_node *tmp_hnode, *n;
184 struct hlist_head *head;
Sony Chacko4be41e92013-01-01 03:20:20 +0000185 struct net_device *netdev = adapter->netdev;
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000186 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
Sony Chackoc70001a2012-11-28 04:34:26 +0000187 u64 src_addr = 0;
188 __le16 vlan_id = 0;
189 u8 hindex;
190
Sony Chacko4be41e92013-01-01 03:20:20 +0000191 if (ether_addr_equal(phdr->h_source, adapter->mac_addr))
Sony Chackoc70001a2012-11-28 04:34:26 +0000192 return;
193
Sony Chacko4be41e92013-01-01 03:20:20 +0000194 if (adapter->fhash.fnum >= adapter->fhash.fmax) {
195 adapter->stats.mac_filter_limit_overrun++;
196 netdev_info(netdev, "Can not add more than %d mac addresses\n",
197 adapter->fhash.fmax);
Sony Chackoc70001a2012-11-28 04:34:26 +0000198 return;
Sony Chacko4be41e92013-01-01 03:20:20 +0000199 }
Sony Chackoc70001a2012-11-28 04:34:26 +0000200
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000201 /* Only NPAR capable devices support vlan based learning */
Sony Chackoc70001a2012-11-28 04:34:26 +0000202 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
203 vlan_id = first_desc->vlan_TCI;
204 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
Sony Chacko4be41e92013-01-01 03:20:20 +0000205 hindex = qlcnic_mac_hash(src_addr) & (adapter->fhash.fbucket_size - 1);
Sony Chackoc70001a2012-11-28 04:34:26 +0000206 head = &(adapter->fhash.fhead[hindex]);
207
208 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
209 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000210 tmp_fil->vlan_id == vlan_id) {
Sony Chackod17dd0d2012-11-28 04:34:27 +0000211 if (jiffies > (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000212 qlcnic_change_filter(adapter, &src_addr,
213 vlan_id);
Sony Chackoc70001a2012-11-28 04:34:26 +0000214 tmp_fil->ftime = jiffies;
215 return;
216 }
217 }
218
219 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
220 if (!fil)
221 return;
222
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000223 qlcnic_change_filter(adapter, &src_addr, vlan_id);
Sony Chackoc70001a2012-11-28 04:34:26 +0000224 fil->ftime = jiffies;
225 fil->vlan_id = vlan_id;
226 memcpy(fil->faddr, &src_addr, ETH_ALEN);
227 spin_lock(&adapter->mac_learn_lock);
228 hlist_add_head(&(fil->fnode), head);
229 adapter->fhash.fnum++;
230 spin_unlock(&adapter->mac_learn_lock);
231}
232
Sony Chackod17dd0d2012-11-28 04:34:27 +0000233static int qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
234 struct cmd_desc_type0 *first_desc, struct sk_buff *skb)
Sony Chackoc70001a2012-11-28 04:34:26 +0000235{
Sony Chackod17dd0d2012-11-28 04:34:27 +0000236 u8 l4proto, opcode = 0, hdr_len = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000237 u16 flags = 0, vlan_tci = 0;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000238 int copied, offset, copy_len, size;
Sony Chackoc70001a2012-11-28 04:34:26 +0000239 struct cmd_desc_type0 *hwdesc;
240 struct vlan_ethhdr *vh;
241 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
242 u16 protocol = ntohs(skb->protocol);
243 u32 producer = tx_ring->producer;
244
245 if (protocol == ETH_P_8021Q) {
246 vh = (struct vlan_ethhdr *)skb->data;
247 flags = FLAGS_VLAN_TAGGED;
248 vlan_tci = ntohs(vh->h_vlan_TCI);
249 protocol = ntohs(vh->h_vlan_encapsulated_proto);
250 } else if (vlan_tx_tag_present(skb)) {
251 flags = FLAGS_VLAN_OOB;
252 vlan_tci = vlan_tx_tag_get(skb);
253 }
254 if (unlikely(adapter->pvid)) {
255 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
256 return -EIO;
257 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
258 goto set_flags;
259
260 flags = FLAGS_VLAN_OOB;
261 vlan_tci = adapter->pvid;
262 }
263set_flags:
264 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
265 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
266
267 if (*(skb->data) & BIT_0) {
268 flags |= BIT_0;
269 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
270 }
271 opcode = TX_ETHER_PKT;
272 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
Sony Chackod17dd0d2012-11-28 04:34:27 +0000273 skb_shinfo(skb)->gso_size > 0) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000274 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Sony Chackoc70001a2012-11-28 04:34:26 +0000275 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
276 first_desc->total_hdr_length = hdr_len;
Sony Chackoc70001a2012-11-28 04:34:26 +0000277 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
278
279 /* For LSO, we need to copy the MAC/IP/TCP headers into
280 * the descriptor ring */
281 copied = 0;
282 offset = 2;
283
284 if (flags & FLAGS_VLAN_OOB) {
285 first_desc->total_hdr_length += VLAN_HLEN;
286 first_desc->tcp_hdr_offset = VLAN_HLEN;
287 first_desc->ip_hdr_offset = VLAN_HLEN;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000288
Sony Chackoc70001a2012-11-28 04:34:26 +0000289 /* Only in case of TSO on vlan device */
290 flags |= FLAGS_VLAN_TAGGED;
291
292 /* Create a TSO vlan header template for firmware */
Sony Chackoc70001a2012-11-28 04:34:26 +0000293 hwdesc = &tx_ring->desc_head[producer];
294 tx_ring->cmd_buf_arr[producer].skb = NULL;
295
296 copy_len = min((int)sizeof(struct cmd_desc_type0) -
Sony Chackod17dd0d2012-11-28 04:34:27 +0000297 offset, hdr_len + VLAN_HLEN);
Sony Chackoc70001a2012-11-28 04:34:26 +0000298
299 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
300 skb_copy_from_linear_data(skb, vh, 12);
301 vh->h_vlan_proto = htons(ETH_P_8021Q);
302 vh->h_vlan_TCI = htons(vlan_tci);
303
304 skb_copy_from_linear_data_offset(skb, 12,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000305 (char *)vh + 16,
306 copy_len - 16);
Sony Chackoc70001a2012-11-28 04:34:26 +0000307 copied = copy_len - VLAN_HLEN;
308 offset = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000309 producer = get_next_index(producer, tx_ring->num_desc);
310 }
311
312 while (copied < hdr_len) {
Sony Chackod17dd0d2012-11-28 04:34:27 +0000313 size = (int)sizeof(struct cmd_desc_type0) - offset;
314 copy_len = min(size, (hdr_len - copied));
Sony Chackoc70001a2012-11-28 04:34:26 +0000315 hwdesc = &tx_ring->desc_head[producer];
316 tx_ring->cmd_buf_arr[producer].skb = NULL;
Sony Chackoc70001a2012-11-28 04:34:26 +0000317 skb_copy_from_linear_data_offset(skb, copied,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000318 (char *)hwdesc +
319 offset, copy_len);
Sony Chackoc70001a2012-11-28 04:34:26 +0000320 copied += copy_len;
321 offset = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000322 producer = get_next_index(producer, tx_ring->num_desc);
323 }
324
325 tx_ring->producer = producer;
326 smp_mb();
327 adapter->stats.lso_frames++;
328
329 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000330 if (protocol == ETH_P_IP) {
331 l4proto = ip_hdr(skb)->protocol;
332
333 if (l4proto == IPPROTO_TCP)
334 opcode = TX_TCP_PKT;
335 else if (l4proto == IPPROTO_UDP)
336 opcode = TX_UDP_PKT;
337 } else if (protocol == ETH_P_IPV6) {
338 l4proto = ipv6_hdr(skb)->nexthdr;
339
340 if (l4proto == IPPROTO_TCP)
341 opcode = TX_TCPV6_PKT;
342 else if (l4proto == IPPROTO_UDP)
343 opcode = TX_UDPV6_PKT;
344 }
345 }
346 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
347 first_desc->ip_hdr_offset += skb_network_offset(skb);
348 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
349
350 return 0;
351}
352
Sony Chackod17dd0d2012-11-28 04:34:27 +0000353static int qlcnic_map_tx_skb(struct pci_dev *pdev, struct sk_buff *skb,
354 struct qlcnic_cmd_buffer *pbuf)
Sony Chackoc70001a2012-11-28 04:34:26 +0000355{
356 struct qlcnic_skb_frag *nf;
357 struct skb_frag_struct *frag;
358 int i, nr_frags;
359 dma_addr_t map;
360
361 nr_frags = skb_shinfo(skb)->nr_frags;
362 nf = &pbuf->frag_array[0];
363
Sony Chackod17dd0d2012-11-28 04:34:27 +0000364 map = pci_map_single(pdev, skb->data, skb_headlen(skb),
365 PCI_DMA_TODEVICE);
Sony Chackoc70001a2012-11-28 04:34:26 +0000366 if (pci_dma_mapping_error(pdev, map))
367 goto out_err;
368
369 nf->dma = map;
370 nf->length = skb_headlen(skb);
371
372 for (i = 0; i < nr_frags; i++) {
373 frag = &skb_shinfo(skb)->frags[i];
374 nf = &pbuf->frag_array[i+1];
Sony Chackoc70001a2012-11-28 04:34:26 +0000375 map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
376 DMA_TO_DEVICE);
377 if (dma_mapping_error(&pdev->dev, map))
378 goto unwind;
379
380 nf->dma = map;
381 nf->length = skb_frag_size(frag);
382 }
383
384 return 0;
385
386unwind:
387 while (--i >= 0) {
388 nf = &pbuf->frag_array[i+1];
389 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
390 }
391
392 nf = &pbuf->frag_array[0];
393 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
394
395out_err:
396 return -ENOMEM;
397}
398
Sony Chackod17dd0d2012-11-28 04:34:27 +0000399static void qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
400 struct qlcnic_cmd_buffer *pbuf)
Sony Chackoc70001a2012-11-28 04:34:26 +0000401{
402 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
Sony Chackod17dd0d2012-11-28 04:34:27 +0000403 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Sony Chackoc70001a2012-11-28 04:34:26 +0000404
405 for (i = 0; i < nr_frags; i++) {
406 nf = &pbuf->frag_array[i+1];
407 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
408 }
409
410 nf = &pbuf->frag_array[0];
411 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
412 pbuf->skb = NULL;
413}
414
Sony Chackod17dd0d2012-11-28 04:34:27 +0000415static inline void qlcnic_clear_cmddesc(u64 *desc)
Sony Chackoc70001a2012-11-28 04:34:26 +0000416{
417 desc[0] = 0ULL;
418 desc[2] = 0ULL;
419 desc[7] = 0ULL;
420}
421
Sony Chackod17dd0d2012-11-28 04:34:27 +0000422netdev_tx_t qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Sony Chackoc70001a2012-11-28 04:34:26 +0000423{
424 struct qlcnic_adapter *adapter = netdev_priv(netdev);
425 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
426 struct qlcnic_cmd_buffer *pbuf;
427 struct qlcnic_skb_frag *buffrag;
428 struct cmd_desc_type0 *hwdesc, *first_desc;
429 struct pci_dev *pdev;
430 struct ethhdr *phdr;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000431 int i, k, frag_count, delta = 0;
432 u32 producer, num_txd;
Sony Chackoc70001a2012-11-28 04:34:26 +0000433
Sony Chackod17dd0d2012-11-28 04:34:27 +0000434 num_txd = tx_ring->num_desc;
Sony Chackoc70001a2012-11-28 04:34:26 +0000435
436 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
437 netif_stop_queue(netdev);
438 return NETDEV_TX_BUSY;
439 }
440
441 if (adapter->flags & QLCNIC_MACSPOOF) {
442 phdr = (struct ethhdr *)skb->data;
443 if (!ether_addr_equal(phdr->h_source, adapter->mac_addr))
444 goto drop_packet;
445 }
446
447 frag_count = skb_shinfo(skb)->nr_frags + 1;
448 /* 14 frags supported for normal packet and
449 * 32 frags supported for TSO packet
450 */
451 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000452 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
453 delta += skb_frag_size(&skb_shinfo(skb)->frags[i]);
454
455 if (!__pskb_pull_tail(skb, delta))
456 goto drop_packet;
457
458 frag_count = 1 + skb_shinfo(skb)->nr_frags;
459 }
460
461 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
462 netif_stop_queue(netdev);
Sony Chackod17dd0d2012-11-28 04:34:27 +0000463 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000464 netif_start_queue(netdev);
Sony Chackod17dd0d2012-11-28 04:34:27 +0000465 } else {
Sony Chackoc70001a2012-11-28 04:34:26 +0000466 adapter->stats.xmit_off++;
467 return NETDEV_TX_BUSY;
468 }
469 }
470
471 producer = tx_ring->producer;
472 pbuf = &tx_ring->cmd_buf_arr[producer];
Sony Chackoc70001a2012-11-28 04:34:26 +0000473 pdev = adapter->pdev;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000474 first_desc = &tx_ring->desc_head[producer];
475 hwdesc = &tx_ring->desc_head[producer];
Sony Chackoc70001a2012-11-28 04:34:26 +0000476 qlcnic_clear_cmddesc((u64 *)hwdesc);
477
478 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
479 adapter->stats.tx_dma_map_error++;
480 goto drop_packet;
481 }
482
483 pbuf->skb = skb;
484 pbuf->frag_count = frag_count;
485
486 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
487 qlcnic_set_tx_port(first_desc, adapter->portnum);
488
489 for (i = 0; i < frag_count; i++) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000490 k = i % 4;
491
492 if ((k == 0) && (i > 0)) {
493 /* move to next desc.*/
494 producer = get_next_index(producer, num_txd);
495 hwdesc = &tx_ring->desc_head[producer];
496 qlcnic_clear_cmddesc((u64 *)hwdesc);
497 tx_ring->cmd_buf_arr[producer].skb = NULL;
498 }
499
500 buffrag = &pbuf->frag_array[i];
Sony Chackoc70001a2012-11-28 04:34:26 +0000501 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
502 switch (k) {
503 case 0:
504 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
505 break;
506 case 1:
507 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
508 break;
509 case 2:
510 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
511 break;
512 case 3:
513 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
514 break;
515 }
516 }
517
518 tx_ring->producer = get_next_index(producer, num_txd);
519 smp_mb();
520
521 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
522 goto unwind_buff;
523
524 if (adapter->mac_learn)
Sony Chacko4be41e92013-01-01 03:20:20 +0000525 qlcnic_send_filter(adapter, first_desc, skb);
Sony Chackoc70001a2012-11-28 04:34:26 +0000526
527 adapter->stats.txbytes += skb->len;
528 adapter->stats.xmitcalled++;
529
530 qlcnic_update_cmd_producer(tx_ring);
531
532 return NETDEV_TX_OK;
533
534unwind_buff:
535 qlcnic_unmap_buffers(pdev, skb, pbuf);
536drop_packet:
537 adapter->stats.txdropped++;
538 dev_kfree_skb_any(skb);
539 return NETDEV_TX_OK;
540}
541
Sony Chacko7f9664522013-01-01 03:20:19 +0000542void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
Sony Chackoc70001a2012-11-28 04:34:26 +0000543{
544 struct net_device *netdev = adapter->netdev;
545
546 if (adapter->ahw->linkup && !linkup) {
547 netdev_info(netdev, "NIC Link is down\n");
548 adapter->ahw->linkup = 0;
549 if (netif_running(netdev)) {
550 netif_carrier_off(netdev);
551 netif_stop_queue(netdev);
552 }
553 } else if (!adapter->ahw->linkup && linkup) {
554 netdev_info(netdev, "NIC Link is up\n");
555 adapter->ahw->linkup = 1;
556 if (netif_running(netdev)) {
557 netif_carrier_on(netdev);
558 netif_wake_queue(netdev);
559 }
560 }
561}
562
Sony Chackod17dd0d2012-11-28 04:34:27 +0000563static int qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
564 struct qlcnic_host_rds_ring *rds_ring,
565 struct qlcnic_rx_buffer *buffer)
Sony Chackoc70001a2012-11-28 04:34:26 +0000566{
567 struct sk_buff *skb;
568 dma_addr_t dma;
569 struct pci_dev *pdev = adapter->pdev;
570
571 skb = netdev_alloc_skb(adapter->netdev, rds_ring->skb_size);
572 if (!skb) {
573 adapter->stats.skb_alloc_failure++;
574 return -ENOMEM;
575 }
576
577 skb_reserve(skb, NET_IP_ALIGN);
Sony Chacko4be41e92013-01-01 03:20:20 +0000578 dma = pci_map_single(pdev, skb->data,
579 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
Sony Chackoc70001a2012-11-28 04:34:26 +0000580
581 if (pci_dma_mapping_error(pdev, dma)) {
582 adapter->stats.rx_dma_map_error++;
583 dev_kfree_skb_any(skb);
584 return -ENOMEM;
585 }
586
587 buffer->skb = skb;
588 buffer->dma = dma;
589
590 return 0;
591}
592
593static void qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
Sony Chacko4be41e92013-01-01 03:20:20 +0000594 struct qlcnic_host_rds_ring *rds_ring,
595 u8 ring_id)
Sony Chackoc70001a2012-11-28 04:34:26 +0000596{
597 struct rcv_desc *pdesc;
598 struct qlcnic_rx_buffer *buffer;
599 int count = 0;
Sony Chacko4be41e92013-01-01 03:20:20 +0000600 uint32_t producer, handle;
Sony Chackoc70001a2012-11-28 04:34:26 +0000601 struct list_head *head;
602
603 if (!spin_trylock(&rds_ring->lock))
604 return;
605
606 producer = rds_ring->producer;
Sony Chackoc70001a2012-11-28 04:34:26 +0000607 head = &rds_ring->free_list;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000608 while (!list_empty(head)) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000609 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
610
611 if (!buffer->skb) {
612 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
613 break;
614 }
Sony Chackoc70001a2012-11-28 04:34:26 +0000615 count++;
616 list_del(&buffer->list);
617
618 /* make a rcv descriptor */
619 pdesc = &rds_ring->desc_head[producer];
Sony Chacko4be41e92013-01-01 03:20:20 +0000620 handle = qlcnic_get_ref_handle(adapter,
621 buffer->ref_handle, ring_id);
622 pdesc->reference_handle = cpu_to_le16(handle);
Sony Chackoc70001a2012-11-28 04:34:26 +0000623 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
624 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Sony Chackoc70001a2012-11-28 04:34:26 +0000625 producer = get_next_index(producer, rds_ring->num_desc);
626 }
Sony Chackoc70001a2012-11-28 04:34:26 +0000627 if (count) {
628 rds_ring->producer = producer;
629 writel((producer - 1) & (rds_ring->num_desc - 1),
Sony Chackod17dd0d2012-11-28 04:34:27 +0000630 rds_ring->crb_rcv_producer);
Sony Chackoc70001a2012-11-28 04:34:26 +0000631 }
632 spin_unlock(&rds_ring->lock);
633}
634
Sony Chacko4be41e92013-01-01 03:20:20 +0000635static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter,
636 struct qlcnic_host_tx_ring *tx_ring,
637 int budget)
Sony Chackoc70001a2012-11-28 04:34:26 +0000638{
639 u32 sw_consumer, hw_consumer;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000640 int i, done, count = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000641 struct qlcnic_cmd_buffer *buffer;
642 struct pci_dev *pdev = adapter->pdev;
643 struct net_device *netdev = adapter->netdev;
644 struct qlcnic_skb_frag *frag;
Sony Chackoc70001a2012-11-28 04:34:26 +0000645
646 if (!spin_trylock(&adapter->tx_clean_lock))
647 return 1;
648
649 sw_consumer = tx_ring->sw_consumer;
650 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
651
652 while (sw_consumer != hw_consumer) {
653 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
654 if (buffer->skb) {
655 frag = &buffer->frag_array[0];
656 pci_unmap_single(pdev, frag->dma, frag->length,
657 PCI_DMA_TODEVICE);
658 frag->dma = 0ULL;
659 for (i = 1; i < buffer->frag_count; i++) {
660 frag++;
661 pci_unmap_page(pdev, frag->dma, frag->length,
662 PCI_DMA_TODEVICE);
663 frag->dma = 0ULL;
664 }
Sony Chackoc70001a2012-11-28 04:34:26 +0000665 adapter->stats.xmitfinished++;
666 dev_kfree_skb_any(buffer->skb);
667 buffer->skb = NULL;
668 }
669
670 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
Sony Chacko4be41e92013-01-01 03:20:20 +0000671 if (++count >= budget)
Sony Chackoc70001a2012-11-28 04:34:26 +0000672 break;
673 }
674
675 if (count && netif_running(netdev)) {
676 tx_ring->sw_consumer = sw_consumer;
Sony Chackoc70001a2012-11-28 04:34:26 +0000677 smp_mb();
Sony Chackoc70001a2012-11-28 04:34:26 +0000678 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
679 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
680 netif_wake_queue(netdev);
681 adapter->stats.xmit_on++;
682 }
683 }
684 adapter->tx_timeo_cnt = 0;
685 }
686 /*
687 * If everything is freed up to consumer then check if the ring is full
688 * If the ring is full then check if more needs to be freed and
689 * schedule the call back again.
690 *
691 * This happens when there are 2 CPUs. One could be freeing and the
692 * other filling it. If the ring is full when we get out of here and
693 * the card has already interrupted the host then the host can miss the
694 * interrupt.
695 *
696 * There is still a possible race condition and the host could miss an
697 * interrupt. The card has to take care of this.
698 */
699 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
700 done = (sw_consumer == hw_consumer);
701 spin_unlock(&adapter->tx_clean_lock);
702
703 return done;
704}
705
706static int qlcnic_poll(struct napi_struct *napi, int budget)
707{
Sony Chacko4be41e92013-01-01 03:20:20 +0000708 int tx_complete, work_done;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000709 struct qlcnic_host_sds_ring *sds_ring;
710 struct qlcnic_adapter *adapter;
Sony Chackoc70001a2012-11-28 04:34:26 +0000711
Sony Chackod17dd0d2012-11-28 04:34:27 +0000712 sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
713 adapter = sds_ring->adapter;
Sony Chacko4be41e92013-01-01 03:20:20 +0000714 tx_complete = qlcnic_process_cmd_ring(adapter, adapter->tx_ring,
715 budget);
Sony Chackoc70001a2012-11-28 04:34:26 +0000716 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
Sony Chackoc70001a2012-11-28 04:34:26 +0000717 if ((work_done < budget) && tx_complete) {
718 napi_complete(&sds_ring->napi);
719 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
720 qlcnic_enable_int(sds_ring);
721 }
722
723 return work_done;
724}
725
726static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
727{
Sony Chackod17dd0d2012-11-28 04:34:27 +0000728 struct qlcnic_host_sds_ring *sds_ring;
729 struct qlcnic_adapter *adapter;
Sony Chackoc70001a2012-11-28 04:34:26 +0000730 int work_done;
731
Sony Chackod17dd0d2012-11-28 04:34:27 +0000732 sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
733 adapter = sds_ring->adapter;
734
Sony Chackoc70001a2012-11-28 04:34:26 +0000735 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
736
737 if (work_done < budget) {
738 napi_complete(&sds_ring->napi);
739 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
740 qlcnic_enable_int(sds_ring);
741 }
742
743 return work_done;
744}
745
Sony Chackod17dd0d2012-11-28 04:34:27 +0000746static void qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
747 struct qlcnic_fw_msg *msg)
Sony Chackoc70001a2012-11-28 04:34:26 +0000748{
749 u32 cable_OUI;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000750 u16 cable_len, link_speed;
751 u8 link_status, module, duplex, autoneg, lb_status = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000752 struct net_device *netdev = adapter->netdev;
753
Sony Chacko79788452012-12-04 03:33:53 +0000754 adapter->ahw->has_link_events = 1;
Sony Chackoc70001a2012-11-28 04:34:26 +0000755
756 cable_OUI = msg->body[1] & 0xffffffff;
757 cable_len = (msg->body[1] >> 32) & 0xffff;
758 link_speed = (msg->body[1] >> 48) & 0xffff;
759
760 link_status = msg->body[2] & 0xff;
761 duplex = (msg->body[2] >> 16) & 0xff;
762 autoneg = (msg->body[2] >> 24) & 0xff;
763 lb_status = (msg->body[2] >> 32) & 0x3;
764
765 module = (msg->body[2] >> 8) & 0xff;
766 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
Sony Chackod17dd0d2012-11-28 04:34:27 +0000767 dev_info(&netdev->dev,
768 "unsupported cable: OUI 0x%x, length %d\n",
769 cable_OUI, cable_len);
Sony Chackoc70001a2012-11-28 04:34:26 +0000770 else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
771 dev_info(&netdev->dev, "unsupported cable length %d\n",
Sony Chackod17dd0d2012-11-28 04:34:27 +0000772 cable_len);
Sony Chackoc70001a2012-11-28 04:34:26 +0000773
774 if (!link_status && (lb_status == QLCNIC_ILB_MODE ||
775 lb_status == QLCNIC_ELB_MODE))
776 adapter->ahw->loopback_state |= QLCNIC_LINKEVENT;
777
778 qlcnic_advert_link_change(adapter, link_status);
779
780 if (duplex == LINKEVENT_FULL_DUPLEX)
Sony Chacko79788452012-12-04 03:33:53 +0000781 adapter->ahw->link_duplex = DUPLEX_FULL;
Sony Chackoc70001a2012-11-28 04:34:26 +0000782 else
Sony Chacko79788452012-12-04 03:33:53 +0000783 adapter->ahw->link_duplex = DUPLEX_HALF;
Sony Chackoc70001a2012-11-28 04:34:26 +0000784
Sony Chacko79788452012-12-04 03:33:53 +0000785 adapter->ahw->module_type = module;
786 adapter->ahw->link_autoneg = autoneg;
Sony Chackoc70001a2012-11-28 04:34:26 +0000787
788 if (link_status) {
Sony Chacko79788452012-12-04 03:33:53 +0000789 adapter->ahw->link_speed = link_speed;
Sony Chackoc70001a2012-11-28 04:34:26 +0000790 } else {
Sony Chacko79788452012-12-04 03:33:53 +0000791 adapter->ahw->link_speed = SPEED_UNKNOWN;
792 adapter->ahw->link_duplex = DUPLEX_UNKNOWN;
Sony Chackoc70001a2012-11-28 04:34:26 +0000793 }
794}
795
Sony Chackod17dd0d2012-11-28 04:34:27 +0000796static void qlcnic_handle_fw_message(int desc_cnt, int index,
797 struct qlcnic_host_sds_ring *sds_ring)
Sony Chackoc70001a2012-11-28 04:34:26 +0000798{
799 struct qlcnic_fw_msg msg;
800 struct status_desc *desc;
801 struct qlcnic_adapter *adapter;
802 struct device *dev;
803 int i = 0, opcode, ret;
804
805 while (desc_cnt > 0 && i < 8) {
806 desc = &sds_ring->desc_head[index];
807 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
808 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
809
810 index = get_next_index(index, sds_ring->num_desc);
811 desc_cnt--;
812 }
813
814 adapter = sds_ring->adapter;
815 dev = &adapter->pdev->dev;
816 opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
817
818 switch (opcode) {
819 case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
820 qlcnic_handle_linkevent(adapter, &msg);
821 break;
822 case QLCNIC_C2H_OPCODE_CONFIG_LOOPBACK:
823 ret = (u32)(msg.body[1]);
824 switch (ret) {
825 case 0:
826 adapter->ahw->loopback_state |= QLCNIC_LB_RESPONSE;
827 break;
828 case 1:
829 dev_info(dev, "loopback already in progress\n");
Sony Chacko79788452012-12-04 03:33:53 +0000830 adapter->ahw->diag_cnt = -QLCNIC_TEST_IN_PROGRESS;
Sony Chackoc70001a2012-11-28 04:34:26 +0000831 break;
832 case 2:
833 dev_info(dev, "loopback cable is not connected\n");
Sony Chacko79788452012-12-04 03:33:53 +0000834 adapter->ahw->diag_cnt = -QLCNIC_LB_CABLE_NOT_CONN;
Sony Chackoc70001a2012-11-28 04:34:26 +0000835 break;
836 default:
Sony Chackod17dd0d2012-11-28 04:34:27 +0000837 dev_info(dev,
838 "loopback configure request failed, err %x\n",
839 ret);
Sony Chacko79788452012-12-04 03:33:53 +0000840 adapter->ahw->diag_cnt = -QLCNIC_UNDEFINED_ERROR;
Sony Chackoc70001a2012-11-28 04:34:26 +0000841 break;
842 }
843 break;
844 default:
845 break;
846 }
847}
848
Sony Chacko4be41e92013-01-01 03:20:20 +0000849struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
850 struct qlcnic_host_rds_ring *ring,
851 u16 index, u16 cksum)
Sony Chackoc70001a2012-11-28 04:34:26 +0000852{
853 struct qlcnic_rx_buffer *buffer;
854 struct sk_buff *skb;
855
Sony Chacko4be41e92013-01-01 03:20:20 +0000856 buffer = &ring->rx_buf_arr[index];
Sony Chackoc70001a2012-11-28 04:34:26 +0000857 if (unlikely(buffer->skb == NULL)) {
858 WARN_ON(1);
859 return NULL;
860 }
861
Sony Chacko4be41e92013-01-01 03:20:20 +0000862 pci_unmap_single(adapter->pdev, buffer->dma, ring->dma_size,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000863 PCI_DMA_FROMDEVICE);
Sony Chackoc70001a2012-11-28 04:34:26 +0000864
865 skb = buffer->skb;
Sony Chackoc70001a2012-11-28 04:34:26 +0000866 if (likely((adapter->netdev->features & NETIF_F_RXCSUM) &&
Sony Chackod17dd0d2012-11-28 04:34:27 +0000867 (cksum == STATUS_CKSUM_OK || cksum == STATUS_CKSUM_LOOP))) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000868 adapter->stats.csummed++;
869 skb->ip_summed = CHECKSUM_UNNECESSARY;
870 } else {
871 skb_checksum_none_assert(skb);
872 }
873
Sony Chacko4be41e92013-01-01 03:20:20 +0000874
Sony Chackoc70001a2012-11-28 04:34:26 +0000875 buffer->skb = NULL;
876
877 return skb;
878}
879
Sony Chackod17dd0d2012-11-28 04:34:27 +0000880static inline int qlcnic_check_rx_tagging(struct qlcnic_adapter *adapter,
881 struct sk_buff *skb, u16 *vlan_tag)
Sony Chackoc70001a2012-11-28 04:34:26 +0000882{
883 struct ethhdr *eth_hdr;
884
885 if (!__vlan_get_tag(skb, vlan_tag)) {
Sony Chackod17dd0d2012-11-28 04:34:27 +0000886 eth_hdr = (struct ethhdr *)skb->data;
Sony Chackoc70001a2012-11-28 04:34:26 +0000887 memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
888 skb_pull(skb, VLAN_HLEN);
889 }
890 if (!adapter->pvid)
891 return 0;
892
893 if (*vlan_tag == adapter->pvid) {
894 /* Outer vlan tag. Packet should follow non-vlan path */
895 *vlan_tag = 0xffff;
896 return 0;
897 }
898 if (adapter->flags & QLCNIC_TAGGING_ENABLED)
899 return 0;
900
901 return -EINVAL;
902}
903
904static struct qlcnic_rx_buffer *
905qlcnic_process_rcv(struct qlcnic_adapter *adapter,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000906 struct qlcnic_host_sds_ring *sds_ring, int ring,
907 u64 sts_data0)
Sony Chackoc70001a2012-11-28 04:34:26 +0000908{
909 struct net_device *netdev = adapter->netdev;
910 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
911 struct qlcnic_rx_buffer *buffer;
912 struct sk_buff *skb;
913 struct qlcnic_host_rds_ring *rds_ring;
914 int index, length, cksum, pkt_offset;
915 u16 vid = 0xffff;
916
917 if (unlikely(ring >= adapter->max_rds_rings))
918 return NULL;
919
920 rds_ring = &recv_ctx->rds_rings[ring];
921
922 index = qlcnic_get_sts_refhandle(sts_data0);
923 if (unlikely(index >= rds_ring->num_desc))
924 return NULL;
925
926 buffer = &rds_ring->rx_buf_arr[index];
Sony Chackoc70001a2012-11-28 04:34:26 +0000927 length = qlcnic_get_sts_totallength(sts_data0);
928 cksum = qlcnic_get_sts_status(sts_data0);
929 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
930
931 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
932 if (!skb)
933 return buffer;
934
935 if (length > rds_ring->skb_size)
936 skb_put(skb, rds_ring->skb_size);
937 else
938 skb_put(skb, length);
939
940 if (pkt_offset)
941 skb_pull(skb, pkt_offset);
942
943 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
944 adapter->stats.rxdropped++;
945 dev_kfree_skb(skb);
946 return buffer;
947 }
948
949 skb->protocol = eth_type_trans(skb, netdev);
950
951 if (vid != 0xffff)
952 __vlan_hwaccel_put_tag(skb, vid);
953
954 napi_gro_receive(&sds_ring->napi, skb);
955
956 adapter->stats.rx_pkts++;
957 adapter->stats.rxbytes += length;
958
959 return buffer;
960}
961
962#define QLC_TCP_HDR_SIZE 20
963#define QLC_TCP_TS_OPTION_SIZE 12
964#define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
965
966static struct qlcnic_rx_buffer *
967qlcnic_process_lro(struct qlcnic_adapter *adapter,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000968 int ring, u64 sts_data0, u64 sts_data1)
Sony Chackoc70001a2012-11-28 04:34:26 +0000969{
970 struct net_device *netdev = adapter->netdev;
971 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
972 struct qlcnic_rx_buffer *buffer;
973 struct sk_buff *skb;
974 struct qlcnic_host_rds_ring *rds_ring;
975 struct iphdr *iph;
Shahed Shaikh776e7bd2013-01-25 10:20:35 +0000976 struct ipv6hdr *ipv6h;
Sony Chackoc70001a2012-11-28 04:34:26 +0000977 struct tcphdr *th;
978 bool push, timestamp;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000979 int index, l2_hdr_offset, l4_hdr_offset;
980 u16 lro_length, length, data_offset, vid = 0xffff;
Sony Chackoc70001a2012-11-28 04:34:26 +0000981 u32 seq_number;
Sony Chackoc70001a2012-11-28 04:34:26 +0000982
983 if (unlikely(ring > adapter->max_rds_rings))
984 return NULL;
985
986 rds_ring = &recv_ctx->rds_rings[ring];
987
988 index = qlcnic_get_lro_sts_refhandle(sts_data0);
989 if (unlikely(index > rds_ring->num_desc))
990 return NULL;
991
992 buffer = &rds_ring->rx_buf_arr[index];
993
994 timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
995 lro_length = qlcnic_get_lro_sts_length(sts_data0);
996 l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
997 l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
998 push = qlcnic_get_lro_sts_push_flag(sts_data0);
999 seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1000
1001 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1002 if (!skb)
1003 return buffer;
1004
1005 if (timestamp)
1006 data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1007 else
1008 data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1009
1010 skb_put(skb, lro_length + data_offset);
Sony Chackoc70001a2012-11-28 04:34:26 +00001011 skb_pull(skb, l2_hdr_offset);
1012
1013 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1014 adapter->stats.rxdropped++;
1015 dev_kfree_skb(skb);
1016 return buffer;
1017 }
1018
1019 skb->protocol = eth_type_trans(skb, netdev);
Shahed Shaikh776e7bd2013-01-25 10:20:35 +00001020
1021 if (htons(skb->protocol) == ETH_P_IPV6) {
1022 ipv6h = (struct ipv6hdr *)skb->data;
1023 th = (struct tcphdr *)(skb->data + sizeof(struct ipv6hdr));
1024 length = (th->doff << 2) + lro_length;
1025 ipv6h->payload_len = htons(length);
1026 } else {
1027 iph = (struct iphdr *)skb->data;
1028 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1029 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1030 iph->tot_len = htons(length);
1031 iph->check = 0;
1032 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1033 }
1034
Sony Chackoc70001a2012-11-28 04:34:26 +00001035 th->psh = push;
1036 th->seq = htonl(seq_number);
Sony Chackoc70001a2012-11-28 04:34:26 +00001037 length = skb->len;
1038
1039 if (adapter->flags & QLCNIC_FW_LRO_MSS_CAP)
1040 skb_shinfo(skb)->gso_size = qlcnic_get_lro_sts_mss(sts_data1);
1041
1042 if (vid != 0xffff)
1043 __vlan_hwaccel_put_tag(skb, vid);
1044 netif_receive_skb(skb);
1045
1046 adapter->stats.lro_pkts++;
1047 adapter->stats.lrobytes += length;
1048
1049 return buffer;
1050}
1051
Sony Chackod17dd0d2012-11-28 04:34:27 +00001052int qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
Sony Chackoc70001a2012-11-28 04:34:26 +00001053{
Sony Chackod17dd0d2012-11-28 04:34:27 +00001054 struct qlcnic_host_rds_ring *rds_ring;
Sony Chackoc70001a2012-11-28 04:34:26 +00001055 struct qlcnic_adapter *adapter = sds_ring->adapter;
1056 struct list_head *cur;
1057 struct status_desc *desc;
1058 struct qlcnic_rx_buffer *rxbuf;
Sony Chacko4be41e92013-01-01 03:20:20 +00001059 int opcode, desc_cnt, count = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +00001060 u64 sts_data0, sts_data1;
Sony Chacko4be41e92013-01-01 03:20:20 +00001061 u8 ring;
Sony Chackoc70001a2012-11-28 04:34:26 +00001062 u32 consumer = sds_ring->consumer;
1063
1064 while (count < max) {
1065 desc = &sds_ring->desc_head[consumer];
1066 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1067
1068 if (!(sts_data0 & STATUS_OWNER_HOST))
1069 break;
1070
1071 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1072 opcode = qlcnic_get_sts_opcode(sts_data0);
Sony Chackoc70001a2012-11-28 04:34:26 +00001073 switch (opcode) {
1074 case QLCNIC_RXPKT_DESC:
1075 case QLCNIC_OLD_RXPKT_DESC:
1076 case QLCNIC_SYN_OFFLOAD:
1077 ring = qlcnic_get_sts_type(sts_data0);
Sony Chackod17dd0d2012-11-28 04:34:27 +00001078 rxbuf = qlcnic_process_rcv(adapter, sds_ring, ring,
1079 sts_data0);
Sony Chackoc70001a2012-11-28 04:34:26 +00001080 break;
1081 case QLCNIC_LRO_DESC:
1082 ring = qlcnic_get_lro_sts_type(sts_data0);
1083 sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1084 rxbuf = qlcnic_process_lro(adapter, ring, sts_data0,
1085 sts_data1);
1086 break;
1087 case QLCNIC_RESPONSE_DESC:
1088 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1089 default:
1090 goto skip;
1091 }
Sony Chackoc70001a2012-11-28 04:34:26 +00001092 WARN_ON(desc_cnt > 1);
1093
1094 if (likely(rxbuf))
1095 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1096 else
1097 adapter->stats.null_rxbuf++;
Sony Chackoc70001a2012-11-28 04:34:26 +00001098skip:
1099 for (; desc_cnt > 0; desc_cnt--) {
1100 desc = &sds_ring->desc_head[consumer];
Sony Chacko4be41e92013-01-01 03:20:20 +00001101 desc->status_desc_data[0] = QLCNIC_DESC_OWNER_FW;
Sony Chackoc70001a2012-11-28 04:34:26 +00001102 consumer = get_next_index(consumer, sds_ring->num_desc);
1103 }
1104 count++;
1105 }
1106
1107 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Sony Chackod17dd0d2012-11-28 04:34:27 +00001108 rds_ring = &adapter->recv_ctx->rds_rings[ring];
Sony Chackoc70001a2012-11-28 04:34:26 +00001109 if (!list_empty(&sds_ring->free_list[ring])) {
1110 list_for_each(cur, &sds_ring->free_list[ring]) {
Sony Chackod17dd0d2012-11-28 04:34:27 +00001111 rxbuf = list_entry(cur, struct qlcnic_rx_buffer,
1112 list);
Sony Chackoc70001a2012-11-28 04:34:26 +00001113 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1114 }
1115 spin_lock(&rds_ring->lock);
1116 list_splice_tail_init(&sds_ring->free_list[ring],
Sony Chackod17dd0d2012-11-28 04:34:27 +00001117 &rds_ring->free_list);
Sony Chackoc70001a2012-11-28 04:34:26 +00001118 spin_unlock(&rds_ring->lock);
1119 }
1120
Sony Chacko4be41e92013-01-01 03:20:20 +00001121 qlcnic_post_rx_buffers_nodb(adapter, rds_ring, ring);
Sony Chackoc70001a2012-11-28 04:34:26 +00001122 }
1123
1124 if (count) {
1125 sds_ring->consumer = consumer;
1126 writel(consumer, sds_ring->crb_sts_consumer);
1127 }
1128
1129 return count;
1130}
1131
Sony Chackod17dd0d2012-11-28 04:34:27 +00001132void qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter,
Sony Chacko4be41e92013-01-01 03:20:20 +00001133 struct qlcnic_host_rds_ring *rds_ring, u8 ring_id)
Sony Chackoc70001a2012-11-28 04:34:26 +00001134{
1135 struct rcv_desc *pdesc;
1136 struct qlcnic_rx_buffer *buffer;
1137 int count = 0;
Sony Chacko4be41e92013-01-01 03:20:20 +00001138 u32 producer, handle;
Sony Chackoc70001a2012-11-28 04:34:26 +00001139 struct list_head *head;
1140
1141 producer = rds_ring->producer;
Sony Chackoc70001a2012-11-28 04:34:26 +00001142 head = &rds_ring->free_list;
Sony Chackod17dd0d2012-11-28 04:34:27 +00001143
Sony Chackoc70001a2012-11-28 04:34:26 +00001144 while (!list_empty(head)) {
1145
1146 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1147
1148 if (!buffer->skb) {
1149 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1150 break;
1151 }
1152
1153 count++;
1154 list_del(&buffer->list);
1155
1156 /* make a rcv descriptor */
1157 pdesc = &rds_ring->desc_head[producer];
1158 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Sony Chacko4be41e92013-01-01 03:20:20 +00001159 handle = qlcnic_get_ref_handle(adapter, buffer->ref_handle,
1160 ring_id);
1161 pdesc->reference_handle = cpu_to_le16(handle);
Sony Chackoc70001a2012-11-28 04:34:26 +00001162 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
Sony Chackoc70001a2012-11-28 04:34:26 +00001163 producer = get_next_index(producer, rds_ring->num_desc);
1164 }
1165
1166 if (count) {
1167 rds_ring->producer = producer;
1168 writel((producer-1) & (rds_ring->num_desc-1),
Sony Chackod17dd0d2012-11-28 04:34:27 +00001169 rds_ring->crb_rcv_producer);
Sony Chackoc70001a2012-11-28 04:34:26 +00001170 }
1171}
1172
1173static void dump_skb(struct sk_buff *skb, struct qlcnic_adapter *adapter)
1174{
1175 int i;
1176 unsigned char *data = skb->data;
1177
Sony Chackod17dd0d2012-11-28 04:34:27 +00001178 pr_info(KERN_INFO "\n");
Sony Chackoc70001a2012-11-28 04:34:26 +00001179 for (i = 0; i < skb->len; i++) {
1180 QLCDB(adapter, DRV, "%02x ", data[i]);
1181 if ((i & 0x0f) == 8)
Sony Chackod17dd0d2012-11-28 04:34:27 +00001182 pr_info(KERN_INFO "\n");
Sony Chackoc70001a2012-11-28 04:34:26 +00001183 }
1184}
1185
1186static void qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter, int ring,
1187 u64 sts_data0)
1188{
1189 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1190 struct sk_buff *skb;
1191 struct qlcnic_host_rds_ring *rds_ring;
1192 int index, length, cksum, pkt_offset;
1193
1194 if (unlikely(ring >= adapter->max_rds_rings))
1195 return;
1196
1197 rds_ring = &recv_ctx->rds_rings[ring];
1198
1199 index = qlcnic_get_sts_refhandle(sts_data0);
1200 length = qlcnic_get_sts_totallength(sts_data0);
1201 if (unlikely(index >= rds_ring->num_desc))
1202 return;
1203
1204 cksum = qlcnic_get_sts_status(sts_data0);
1205 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1206
1207 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1208 if (!skb)
1209 return;
1210
1211 if (length > rds_ring->skb_size)
1212 skb_put(skb, rds_ring->skb_size);
1213 else
1214 skb_put(skb, length);
1215
1216 if (pkt_offset)
1217 skb_pull(skb, pkt_offset);
1218
1219 if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
Sony Chacko79788452012-12-04 03:33:53 +00001220 adapter->ahw->diag_cnt++;
Sony Chackoc70001a2012-11-28 04:34:26 +00001221 else
1222 dump_skb(skb, adapter);
1223
1224 dev_kfree_skb_any(skb);
1225 adapter->stats.rx_pkts++;
1226 adapter->stats.rxbytes += length;
1227
1228 return;
1229}
1230
Sony Chacko7e2cf4f2013-01-01 03:20:17 +00001231void qlcnic_82xx_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
Sony Chackoc70001a2012-11-28 04:34:26 +00001232{
1233 struct qlcnic_adapter *adapter = sds_ring->adapter;
1234 struct status_desc *desc;
1235 u64 sts_data0;
1236 int ring, opcode, desc_cnt;
1237
1238 u32 consumer = sds_ring->consumer;
1239
1240 desc = &sds_ring->desc_head[consumer];
1241 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1242
1243 if (!(sts_data0 & STATUS_OWNER_HOST))
1244 return;
1245
1246 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1247 opcode = qlcnic_get_sts_opcode(sts_data0);
1248 switch (opcode) {
1249 case QLCNIC_RESPONSE_DESC:
1250 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1251 break;
1252 default:
1253 ring = qlcnic_get_sts_type(sts_data0);
1254 qlcnic_process_rcv_diag(adapter, ring, sts_data0);
1255 break;
1256 }
1257
1258 for (; desc_cnt > 0; desc_cnt--) {
1259 desc = &sds_ring->desc_head[consumer];
1260 desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1261 consumer = get_next_index(consumer, sds_ring->num_desc);
1262 }
1263
1264 sds_ring->consumer = consumer;
1265 writel(consumer, sds_ring->crb_sts_consumer);
1266}
1267
Sony Chacko7e2cf4f2013-01-01 03:20:17 +00001268int qlcnic_82xx_napi_add(struct qlcnic_adapter *adapter,
1269 struct net_device *netdev)
Sony Chackoc70001a2012-11-28 04:34:26 +00001270{
Sony Chackod17dd0d2012-11-28 04:34:27 +00001271 int ring, max_sds_rings;
Sony Chackoc70001a2012-11-28 04:34:26 +00001272 struct qlcnic_host_sds_ring *sds_ring;
1273 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1274
1275 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
1276 return -ENOMEM;
1277
Sony Chackod17dd0d2012-11-28 04:34:27 +00001278 max_sds_rings = adapter->max_sds_rings;
1279
Sony Chackoc70001a2012-11-28 04:34:26 +00001280 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1281 sds_ring = &recv_ctx->sds_rings[ring];
Sony Chacko4be41e92013-01-01 03:20:20 +00001282 if (ring == adapter->max_sds_rings - 1)
Sony Chackoc70001a2012-11-28 04:34:26 +00001283 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
Sony Chackod17dd0d2012-11-28 04:34:27 +00001284 QLCNIC_NETDEV_WEIGHT / max_sds_rings);
Sony Chackoc70001a2012-11-28 04:34:26 +00001285 else
Sony Chackod17dd0d2012-11-28 04:34:27 +00001286 netif_napi_add(netdev, &sds_ring->napi, qlcnic_rx_poll,
1287 QLCNIC_NETDEV_WEIGHT*2);
Sony Chackoc70001a2012-11-28 04:34:26 +00001288 }
1289
Sony Chacko4be41e92013-01-01 03:20:20 +00001290 if (qlcnic_alloc_tx_rings(adapter, netdev)) {
1291 qlcnic_free_sds_rings(recv_ctx);
1292 return -ENOMEM;
1293 }
1294
Sony Chackoc70001a2012-11-28 04:34:26 +00001295 return 0;
1296}
1297
Sony Chacko4be41e92013-01-01 03:20:20 +00001298void qlcnic_82xx_napi_del(struct qlcnic_adapter *adapter)
Sony Chackoc70001a2012-11-28 04:34:26 +00001299{
1300 int ring;
1301 struct qlcnic_host_sds_ring *sds_ring;
1302 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1303
1304 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1305 sds_ring = &recv_ctx->sds_rings[ring];
1306 netif_napi_del(&sds_ring->napi);
1307 }
1308
1309 qlcnic_free_sds_rings(adapter->recv_ctx);
Sony Chacko4be41e92013-01-01 03:20:20 +00001310 qlcnic_free_tx_rings(adapter);
Sony Chackoc70001a2012-11-28 04:34:26 +00001311}
1312
Sony Chacko7e2cf4f2013-01-01 03:20:17 +00001313void qlcnic_82xx_napi_enable(struct qlcnic_adapter *adapter)
Sony Chackoc70001a2012-11-28 04:34:26 +00001314{
1315 int ring;
1316 struct qlcnic_host_sds_ring *sds_ring;
1317 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1318
1319 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1320 return;
1321
1322 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1323 sds_ring = &recv_ctx->sds_rings[ring];
1324 napi_enable(&sds_ring->napi);
1325 qlcnic_enable_int(sds_ring);
1326 }
1327}
1328
Sony Chacko7e2cf4f2013-01-01 03:20:17 +00001329void qlcnic_82xx_napi_disable(struct qlcnic_adapter *adapter)
Sony Chackoc70001a2012-11-28 04:34:26 +00001330{
1331 int ring;
1332 struct qlcnic_host_sds_ring *sds_ring;
1333 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1334
1335 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1336 return;
1337
1338 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1339 sds_ring = &recv_ctx->sds_rings[ring];
1340 qlcnic_disable_int(sds_ring);
1341 napi_synchronize(&sds_ring->napi);
1342 napi_disable(&sds_ring->napi);
1343 }
1344}
Sony Chacko4be41e92013-01-01 03:20:20 +00001345
1346static struct qlcnic_rx_buffer *
1347qlcnic_83xx_process_rcv(struct qlcnic_adapter *adapter,
1348 struct qlcnic_host_sds_ring *sds_ring,
1349 u8 ring, u64 sts_data[])
1350{
1351 struct net_device *netdev = adapter->netdev;
1352 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1353 struct qlcnic_rx_buffer *buffer;
1354 struct sk_buff *skb;
1355 struct qlcnic_host_rds_ring *rds_ring;
1356 int index, length, cksum;
1357 u16 vid = 0xffff;
1358
1359 if (unlikely(ring >= adapter->max_rds_rings))
1360 return NULL;
1361
1362 rds_ring = &recv_ctx->rds_rings[ring];
1363
1364 index = qlcnic_83xx_hndl(sts_data[0]);
1365 if (unlikely(index >= rds_ring->num_desc))
1366 return NULL;
1367
1368 buffer = &rds_ring->rx_buf_arr[index];
1369 length = qlcnic_83xx_pktln(sts_data[0]);
1370 cksum = qlcnic_83xx_csum_status(sts_data[1]);
1371 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1372 if (!skb)
1373 return buffer;
1374
1375 if (length > rds_ring->skb_size)
1376 skb_put(skb, rds_ring->skb_size);
1377 else
1378 skb_put(skb, length);
1379
1380 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1381 adapter->stats.rxdropped++;
1382 dev_kfree_skb(skb);
1383 return buffer;
1384 }
1385
1386 skb->protocol = eth_type_trans(skb, netdev);
1387
1388 if (vid != 0xffff)
1389 __vlan_hwaccel_put_tag(skb, vid);
1390
1391 napi_gro_receive(&sds_ring->napi, skb);
1392
1393 adapter->stats.rx_pkts++;
1394 adapter->stats.rxbytes += length;
1395
1396 return buffer;
1397}
1398
1399static struct qlcnic_rx_buffer *
1400qlcnic_83xx_process_lro(struct qlcnic_adapter *adapter,
1401 u8 ring, u64 sts_data[])
1402{
1403 struct net_device *netdev = adapter->netdev;
1404 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1405 struct qlcnic_rx_buffer *buffer;
1406 struct sk_buff *skb;
1407 struct qlcnic_host_rds_ring *rds_ring;
1408 struct iphdr *iph;
1409 struct ipv6hdr *ipv6h;
1410 struct tcphdr *th;
1411 bool push;
1412 int l2_hdr_offset, l4_hdr_offset;
1413 int index;
1414 u16 lro_length, length, data_offset;
1415 u16 vid = 0xffff;
1416
1417 if (unlikely(ring > adapter->max_rds_rings))
1418 return NULL;
1419
1420 rds_ring = &recv_ctx->rds_rings[ring];
1421
1422 index = qlcnic_83xx_hndl(sts_data[0]);
1423 if (unlikely(index > rds_ring->num_desc))
1424 return NULL;
1425
1426 buffer = &rds_ring->rx_buf_arr[index];
1427
1428 lro_length = qlcnic_83xx_lro_pktln(sts_data[0]);
1429 l2_hdr_offset = qlcnic_83xx_l2_hdr_off(sts_data[1]);
1430 l4_hdr_offset = qlcnic_83xx_l4_hdr_off(sts_data[1]);
1431 push = qlcnic_83xx_is_psh_bit(sts_data[1]);
1432
1433 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1434 if (!skb)
1435 return buffer;
1436 if (qlcnic_83xx_is_tstamp(sts_data[1]))
1437 data_offset = l4_hdr_offset + QLCNIC_TCP_TS_HDR_SIZE;
1438 else
1439 data_offset = l4_hdr_offset + QLCNIC_TCP_HDR_SIZE;
1440
1441 skb_put(skb, lro_length + data_offset);
1442 skb_pull(skb, l2_hdr_offset);
1443
1444 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1445 adapter->stats.rxdropped++;
1446 dev_kfree_skb(skb);
1447 return buffer;
1448 }
1449
1450 skb->protocol = eth_type_trans(skb, netdev);
1451 if (ntohs(skb->protocol) == ETH_P_IPV6) {
1452 ipv6h = (struct ipv6hdr *)skb->data;
1453 th = (struct tcphdr *)(skb->data + sizeof(struct ipv6hdr));
1454
1455 length = (th->doff << 2) + lro_length;
1456 ipv6h->payload_len = htons(length);
1457 } else {
1458 iph = (struct iphdr *)skb->data;
1459 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1460 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1461 iph->tot_len = htons(length);
1462 iph->check = 0;
1463 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1464 }
1465
1466 th->psh = push;
1467 length = skb->len;
1468
1469 if (vid != 0xffff)
1470 __vlan_hwaccel_put_tag(skb, vid);
1471
1472 netif_receive_skb(skb);
1473
1474 adapter->stats.lro_pkts++;
1475 adapter->stats.lrobytes += length;
1476 return buffer;
1477}
1478
1479static int qlcnic_83xx_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring,
1480 int max)
1481{
1482 struct qlcnic_host_rds_ring *rds_ring;
1483 struct qlcnic_adapter *adapter = sds_ring->adapter;
1484 struct list_head *cur;
1485 struct status_desc *desc;
1486 struct qlcnic_rx_buffer *rxbuf = NULL;
1487 u8 ring;
1488 u64 sts_data[2];
1489 int count = 0, opcode;
1490 u32 consumer = sds_ring->consumer;
1491
1492 while (count < max) {
1493 desc = &sds_ring->desc_head[consumer];
1494 sts_data[1] = le64_to_cpu(desc->status_desc_data[1]);
1495 opcode = qlcnic_83xx_opcode(sts_data[1]);
1496 if (!opcode)
1497 break;
1498 sts_data[0] = le64_to_cpu(desc->status_desc_data[0]);
1499 ring = QLCNIC_FETCH_RING_ID(sts_data[0]);
1500
1501 switch (opcode) {
1502 case QLC_83XX_REG_DESC:
1503 rxbuf = qlcnic_83xx_process_rcv(adapter, sds_ring,
1504 ring, sts_data);
1505 break;
1506 case QLC_83XX_LRO_DESC:
1507 rxbuf = qlcnic_83xx_process_lro(adapter, ring,
1508 sts_data);
1509 break;
1510 default:
1511 dev_info(&adapter->pdev->dev,
1512 "Unkonwn opcode: 0x%x\n", opcode);
1513 goto skip;
1514 }
1515
1516 if (likely(rxbuf))
1517 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1518 else
1519 adapter->stats.null_rxbuf++;
1520skip:
1521 desc = &sds_ring->desc_head[consumer];
1522 /* Reset the descriptor */
1523 desc->status_desc_data[1] = 0;
1524 consumer = get_next_index(consumer, sds_ring->num_desc);
1525 count++;
1526 }
1527 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1528 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1529 if (!list_empty(&sds_ring->free_list[ring])) {
1530 list_for_each(cur, &sds_ring->free_list[ring]) {
1531 rxbuf = list_entry(cur, struct qlcnic_rx_buffer,
1532 list);
1533 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1534 }
1535 spin_lock(&rds_ring->lock);
1536 list_splice_tail_init(&sds_ring->free_list[ring],
1537 &rds_ring->free_list);
1538 spin_unlock(&rds_ring->lock);
1539 }
1540 qlcnic_post_rx_buffers_nodb(adapter, rds_ring, ring);
1541 }
1542 if (count) {
1543 sds_ring->consumer = consumer;
1544 writel(consumer, sds_ring->crb_sts_consumer);
1545 }
1546 return count;
1547}
1548
1549static int qlcnic_83xx_poll(struct napi_struct *napi, int budget)
1550{
1551 int tx_complete;
1552 int work_done;
1553 struct qlcnic_host_sds_ring *sds_ring;
1554 struct qlcnic_adapter *adapter;
1555 struct qlcnic_host_tx_ring *tx_ring;
1556
1557 sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1558 adapter = sds_ring->adapter;
1559 /* tx ring count = 1 */
1560 tx_ring = adapter->tx_ring;
1561
1562 if (!(adapter->flags & QLCNIC_MSIX_ENABLED))
1563 qlcnic_83xx_process_aen(adapter);
1564
1565 tx_complete = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
1566 work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
1567 if ((work_done < budget) && tx_complete) {
1568 napi_complete(&sds_ring->napi);
1569 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1570 qlcnic_83xx_enable_intr(adapter, sds_ring);
1571 }
1572
1573 return work_done;
1574}
1575
1576static int qlcnic_83xx_msix_tx_poll(struct napi_struct *napi, int budget)
1577{
1578 int work_done;
1579 struct qlcnic_host_tx_ring *tx_ring;
1580 struct qlcnic_adapter *adapter;
1581
1582 budget = QLCNIC_TX_POLL_BUDGET;
1583 tx_ring = container_of(napi, struct qlcnic_host_tx_ring, napi);
1584 adapter = tx_ring->adapter;
1585 work_done = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
1586 if (work_done) {
1587 napi_complete(&tx_ring->napi);
1588 if (test_bit(__QLCNIC_DEV_UP , &adapter->state))
1589 qlcnic_83xx_enable_tx_intr(adapter, tx_ring);
1590 }
1591
1592 return work_done;
1593}
1594
1595static int qlcnic_83xx_rx_poll(struct napi_struct *napi, int budget)
1596{
1597 int work_done;
1598 struct qlcnic_host_sds_ring *sds_ring;
1599 struct qlcnic_adapter *adapter;
1600
1601 sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1602 adapter = sds_ring->adapter;
1603 work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
1604 if (work_done < budget) {
1605 napi_complete(&sds_ring->napi);
1606 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1607 qlcnic_83xx_enable_intr(adapter, sds_ring);
1608 }
1609
1610 return work_done;
1611}
1612
1613void qlcnic_83xx_napi_enable(struct qlcnic_adapter *adapter)
1614{
1615 int ring;
1616 struct qlcnic_host_sds_ring *sds_ring;
1617 struct qlcnic_host_tx_ring *tx_ring;
1618 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1619
1620 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1621 return;
1622
1623 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1624 sds_ring = &recv_ctx->sds_rings[ring];
1625 napi_enable(&sds_ring->napi);
1626 qlcnic_83xx_enable_intr(adapter, sds_ring);
1627 }
1628
1629 if (adapter->flags & QLCNIC_MSIX_ENABLED) {
1630 for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
1631 tx_ring = &adapter->tx_ring[ring];
1632 napi_enable(&tx_ring->napi);
1633 qlcnic_83xx_enable_tx_intr(adapter, tx_ring);
1634 }
1635 }
1636}
1637
1638void qlcnic_83xx_napi_disable(struct qlcnic_adapter *adapter)
1639{
1640 int ring;
1641 struct qlcnic_host_sds_ring *sds_ring;
1642 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1643 struct qlcnic_host_tx_ring *tx_ring;
1644
1645 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1646 return;
1647
1648 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1649 sds_ring = &recv_ctx->sds_rings[ring];
1650 writel(1, sds_ring->crb_intr_mask);
1651 napi_synchronize(&sds_ring->napi);
1652 napi_disable(&sds_ring->napi);
1653 }
1654
1655 if (adapter->flags & QLCNIC_MSIX_ENABLED) {
1656 for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
1657 tx_ring = &adapter->tx_ring[ring];
1658 qlcnic_83xx_disable_tx_intr(adapter, tx_ring);
1659 napi_synchronize(&tx_ring->napi);
1660 napi_disable(&tx_ring->napi);
1661 }
1662 }
1663}
1664
1665int qlcnic_83xx_napi_add(struct qlcnic_adapter *adapter,
1666 struct net_device *netdev)
1667{
1668 int ring, max_sds_rings;
1669 struct qlcnic_host_sds_ring *sds_ring;
1670 struct qlcnic_host_tx_ring *tx_ring;
1671 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1672
1673 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
1674 return -ENOMEM;
1675
1676 max_sds_rings = adapter->max_sds_rings;
1677 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1678 sds_ring = &recv_ctx->sds_rings[ring];
1679 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1680 netif_napi_add(netdev, &sds_ring->napi,
1681 qlcnic_83xx_rx_poll,
1682 QLCNIC_NETDEV_WEIGHT * 2);
1683 else
1684 netif_napi_add(netdev, &sds_ring->napi,
1685 qlcnic_83xx_poll,
1686 QLCNIC_NETDEV_WEIGHT / max_sds_rings);
1687 }
1688
1689 if (qlcnic_alloc_tx_rings(adapter, netdev)) {
1690 qlcnic_free_sds_rings(recv_ctx);
1691 return -ENOMEM;
1692 }
1693
1694 if (adapter->flags & QLCNIC_MSIX_ENABLED) {
1695 for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
1696 tx_ring = &adapter->tx_ring[ring];
1697 netif_napi_add(netdev, &tx_ring->napi,
1698 qlcnic_83xx_msix_tx_poll,
1699 QLCNIC_NETDEV_WEIGHT);
1700 }
1701 }
1702
1703 return 0;
1704}
1705
1706void qlcnic_83xx_napi_del(struct qlcnic_adapter *adapter)
1707{
1708 int ring;
1709 struct qlcnic_host_sds_ring *sds_ring;
1710 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1711 struct qlcnic_host_tx_ring *tx_ring;
1712
1713 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1714 sds_ring = &recv_ctx->sds_rings[ring];
1715 netif_napi_del(&sds_ring->napi);
1716 }
1717
1718 qlcnic_free_sds_rings(adapter->recv_ctx);
1719
1720 if ((adapter->flags & QLCNIC_MSIX_ENABLED)) {
1721 for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
1722 tx_ring = &adapter->tx_ring[ring];
1723 netif_napi_del(&tx_ring->napi);
1724 }
1725 }
1726
1727 qlcnic_free_tx_rings(adapter);
1728}
1729
1730void qlcnic_83xx_process_rcv_diag(struct qlcnic_adapter *adapter,
1731 int ring, u64 sts_data[])
1732{
1733 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1734 struct sk_buff *skb;
1735 struct qlcnic_host_rds_ring *rds_ring;
1736 int index, length;
1737
1738 if (unlikely(ring >= adapter->max_rds_rings))
1739 return;
1740
1741 rds_ring = &recv_ctx->rds_rings[ring];
1742 index = qlcnic_83xx_hndl(sts_data[0]);
1743 if (unlikely(index >= rds_ring->num_desc))
1744 return;
1745
1746 length = qlcnic_83xx_pktln(sts_data[0]);
1747
1748 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1749 if (!skb)
1750 return;
1751
1752 if (length > rds_ring->skb_size)
1753 skb_put(skb, rds_ring->skb_size);
1754 else
1755 skb_put(skb, length);
1756
1757 if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
1758 adapter->ahw->diag_cnt++;
1759 else
1760 dump_skb(skb, adapter);
1761
1762 dev_kfree_skb_any(skb);
1763 return;
1764}
1765
1766void qlcnic_83xx_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
1767{
1768 struct qlcnic_adapter *adapter = sds_ring->adapter;
1769 struct status_desc *desc;
1770 u64 sts_data[2];
1771 int ring, opcode;
1772 u32 consumer = sds_ring->consumer;
1773
1774 desc = &sds_ring->desc_head[consumer];
1775 sts_data[0] = le64_to_cpu(desc->status_desc_data[0]);
1776 sts_data[1] = le64_to_cpu(desc->status_desc_data[1]);
1777 opcode = qlcnic_83xx_opcode(sts_data[1]);
1778 if (!opcode)
1779 return;
1780
1781 ring = QLCNIC_FETCH_RING_ID(qlcnic_83xx_hndl(sts_data[0]));
1782 qlcnic_83xx_process_rcv_diag(adapter, ring, sts_data);
1783 desc = &sds_ring->desc_head[consumer];
1784 desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1785 consumer = get_next_index(consumer, sds_ring->num_desc);
1786 sds_ring->consumer = consumer;
1787 writel(consumer, sds_ring->crb_sts_consumer);
1788}