xref: /linux/drivers/net/ethernet/qlogic/qlcnic/qlcnic_io.c (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
1 /*
2  * QLogic qlcnic NIC Driver
3  * Copyright (c) 2009-2013 QLogic Corporation
4  *
5  * See LICENSE.qlcnic for copyright and licensing details.
6  */
7 
8 #include <linux/netdevice.h>
9 #include <linux/if_vlan.h>
10 #include <net/ip.h>
11 #include <linux/ipv6.h>
12 #include <net/checksum.h>
13 
14 #include "qlcnic.h"
15 
16 #define TX_ETHER_PKT	0x01
17 #define TX_TCP_PKT	0x02
18 #define TX_UDP_PKT	0x03
19 #define TX_IP_PKT	0x04
20 #define TX_TCP_LSO	0x05
21 #define TX_TCP_LSO6	0x06
22 #define TX_TCPV6_PKT	0x0b
23 #define TX_UDPV6_PKT	0x0c
24 #define FLAGS_VLAN_TAGGED	0x10
25 #define FLAGS_VLAN_OOB		0x40
26 
27 #define qlcnic_set_tx_vlan_tci(cmd_desc, v)	\
28 	(cmd_desc)->vlan_TCI = cpu_to_le16(v);
29 #define qlcnic_set_cmd_desc_port(cmd_desc, var)	\
30 	((cmd_desc)->port_ctxid |= ((var) & 0x0F))
31 #define qlcnic_set_cmd_desc_ctxid(cmd_desc, var)	\
32 	((cmd_desc)->port_ctxid |= ((var) << 4 & 0xF0))
33 
34 #define qlcnic_set_tx_port(_desc, _port) \
35 	((_desc)->port_ctxid = ((_port) & 0xf) | (((_port) << 4) & 0xf0))
36 
37 #define qlcnic_set_tx_flags_opcode(_desc, _flags, _opcode) \
38 	((_desc)->flags_opcode |= \
39 	cpu_to_le16(((_flags) & 0x7f) | (((_opcode) & 0x3f) << 7)))
40 
41 #define qlcnic_set_tx_frags_len(_desc, _frags, _len) \
42 	((_desc)->nfrags__length = \
43 	cpu_to_le32(((_frags) & 0xff) | (((_len) & 0xffffff) << 8)))
44 
45 /* owner bits of status_desc */
46 #define STATUS_OWNER_HOST	(0x1ULL << 56)
47 #define STATUS_OWNER_PHANTOM	(0x2ULL << 56)
48 
49 /* Status descriptor:
50    0-3 port, 4-7 status, 8-11 type, 12-27 total_length
51    28-43 reference_handle, 44-47 protocol, 48-52 pkt_offset
52    53-55 desc_cnt, 56-57 owner, 58-63 opcode
53  */
54 #define qlcnic_get_sts_port(sts_data)	\
55 	((sts_data) & 0x0F)
56 #define qlcnic_get_sts_status(sts_data)	\
57 	(((sts_data) >> 4) & 0x0F)
58 #define qlcnic_get_sts_type(sts_data)	\
59 	(((sts_data) >> 8) & 0x0F)
60 #define qlcnic_get_sts_totallength(sts_data)	\
61 	(((sts_data) >> 12) & 0xFFFF)
62 #define qlcnic_get_sts_refhandle(sts_data)	\
63 	(((sts_data) >> 28) & 0xFFFF)
64 #define qlcnic_get_sts_prot(sts_data)	\
65 	(((sts_data) >> 44) & 0x0F)
66 #define qlcnic_get_sts_pkt_offset(sts_data)	\
67 	(((sts_data) >> 48) & 0x1F)
68 #define qlcnic_get_sts_desc_cnt(sts_data)	\
69 	(((sts_data) >> 53) & 0x7)
70 #define qlcnic_get_sts_opcode(sts_data)	\
71 	(((sts_data) >> 58) & 0x03F)
72 
73 #define qlcnic_get_lro_sts_refhandle(sts_data) 	\
74 	((sts_data) & 0x07FFF)
75 #define qlcnic_get_lro_sts_length(sts_data)	\
76 	(((sts_data) >> 16) & 0x0FFFF)
77 #define qlcnic_get_lro_sts_l2_hdr_offset(sts_data)	\
78 	(((sts_data) >> 32) & 0x0FF)
79 #define qlcnic_get_lro_sts_l4_hdr_offset(sts_data)	\
80 	(((sts_data) >> 40) & 0x0FF)
81 #define qlcnic_get_lro_sts_timestamp(sts_data)	\
82 	(((sts_data) >> 48) & 0x1)
83 #define qlcnic_get_lro_sts_type(sts_data)	\
84 	(((sts_data) >> 49) & 0x7)
85 #define qlcnic_get_lro_sts_push_flag(sts_data)		\
86 	(((sts_data) >> 52) & 0x1)
87 #define qlcnic_get_lro_sts_seq_number(sts_data)		\
88 	((sts_data) & 0x0FFFFFFFF)
89 #define qlcnic_get_lro_sts_mss(sts_data1)		\
90 	((sts_data1 >> 32) & 0x0FFFF)
91 
92 #define qlcnic_83xx_get_lro_sts_mss(sts) ((sts) & 0xffff)
93 
94 /* opcode field in status_desc */
95 #define QLCNIC_SYN_OFFLOAD	0x03
96 #define QLCNIC_RXPKT_DESC  	0x04
97 #define QLCNIC_OLD_RXPKT_DESC	0x3f
98 #define QLCNIC_RESPONSE_DESC	0x05
99 #define QLCNIC_LRO_DESC  	0x12
100 
101 #define QLCNIC_TX_POLL_BUDGET		128
102 #define QLCNIC_TCP_HDR_SIZE		20
103 #define QLCNIC_TCP_TS_OPTION_SIZE	12
104 #define QLCNIC_FETCH_RING_ID(handle)	((handle) >> 63)
105 #define QLCNIC_DESC_OWNER_FW		cpu_to_le64(STATUS_OWNER_PHANTOM)
106 
107 #define QLCNIC_TCP_TS_HDR_SIZE (QLCNIC_TCP_HDR_SIZE + QLCNIC_TCP_TS_OPTION_SIZE)
108 
109 /* for status field in status_desc */
110 #define STATUS_CKSUM_LOOP	0
111 #define STATUS_CKSUM_OK		2
112 
113 #define qlcnic_83xx_pktln(sts)		((sts >> 32) & 0x3FFF)
114 #define qlcnic_83xx_hndl(sts)		((sts >> 48) & 0x7FFF)
115 #define qlcnic_83xx_csum_status(sts)	((sts >> 39) & 7)
116 #define qlcnic_83xx_opcode(sts)	((sts >> 42) & 0xF)
117 #define qlcnic_83xx_vlan_tag(sts)	(((sts) >> 48) & 0xFFFF)
118 #define qlcnic_83xx_lro_pktln(sts)	(((sts) >> 32) & 0x3FFF)
119 #define qlcnic_83xx_l2_hdr_off(sts)	(((sts) >> 16) & 0xFF)
120 #define qlcnic_83xx_l4_hdr_off(sts)	(((sts) >> 24) & 0xFF)
121 #define qlcnic_83xx_pkt_cnt(sts)	(((sts) >> 16) & 0x7)
122 #define qlcnic_83xx_is_tstamp(sts)	(((sts) >> 40) & 1)
123 #define qlcnic_83xx_is_psh_bit(sts)	(((sts) >> 41) & 1)
124 #define qlcnic_83xx_is_ip_align(sts)	(((sts) >> 46) & 1)
125 #define qlcnic_83xx_has_vlan_tag(sts)	(((sts) >> 47) & 1)
126 
127 struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *,
128 				     struct qlcnic_host_rds_ring *, u16, u16);
129 
130 inline void qlcnic_enable_tx_intr(struct qlcnic_adapter *adapter,
131 				  struct qlcnic_host_tx_ring *tx_ring)
132 {
133 	if (qlcnic_check_multi_tx(adapter) &&
134 	    !adapter->ahw->diag_test)
135 		writel(0x0, tx_ring->crb_intr_mask);
136 }
137 
138 
139 static inline void qlcnic_disable_tx_int(struct qlcnic_adapter *adapter,
140 					 struct qlcnic_host_tx_ring *tx_ring)
141 {
142 	if (qlcnic_check_multi_tx(adapter) &&
143 	    !adapter->ahw->diag_test)
144 		writel(1, tx_ring->crb_intr_mask);
145 }
146 
147 inline void qlcnic_83xx_enable_tx_intr(struct qlcnic_adapter *adapter,
148 				       struct qlcnic_host_tx_ring *tx_ring)
149 {
150 	writel(0, tx_ring->crb_intr_mask);
151 }
152 
153 inline void qlcnic_83xx_disable_tx_intr(struct qlcnic_adapter *adapter,
154 					struct qlcnic_host_tx_ring *tx_ring)
155 {
156 	writel(1, tx_ring->crb_intr_mask);
157 }
158 
159 static inline u8 qlcnic_mac_hash(u64 mac)
160 {
161 	return (u8)((mac & 0xff) ^ ((mac >> 40) & 0xff));
162 }
163 
164 static inline u32 qlcnic_get_ref_handle(struct qlcnic_adapter *adapter,
165 					u16 handle, u8 ring_id)
166 {
167 	if (qlcnic_83xx_check(adapter))
168 		return handle | (ring_id << 15);
169 	else
170 		return handle;
171 }
172 
173 static inline int qlcnic_82xx_is_lb_pkt(u64 sts_data)
174 {
175 	return (qlcnic_get_sts_status(sts_data) == STATUS_CKSUM_LOOP) ? 1 : 0;
176 }
177 
178 static void qlcnic_delete_rx_list_mac(struct qlcnic_adapter *adapter,
179 				      struct qlcnic_filter *fil,
180 				      void *addr, u16 vlan_id)
181 {
182 	int ret;
183 	u8 op;
184 
185 	op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
186 	ret = qlcnic_sre_macaddr_change(adapter, addr, vlan_id, op);
187 	if (ret)
188 		return;
189 
190 	op = vlan_id ? QLCNIC_MAC_VLAN_DEL : QLCNIC_MAC_DEL;
191 	ret = qlcnic_sre_macaddr_change(adapter, addr, vlan_id, op);
192 	if (!ret) {
193 		hlist_del(&fil->fnode);
194 		adapter->rx_fhash.fnum--;
195 	}
196 }
197 
198 static struct qlcnic_filter *qlcnic_find_mac_filter(struct hlist_head *head,
199 						    void *addr, u16 vlan_id)
200 {
201 	struct qlcnic_filter *tmp_fil = NULL;
202 	struct hlist_node *n;
203 
204 	hlist_for_each_entry_safe(tmp_fil, n, head, fnode) {
205 		if (!memcmp(tmp_fil->faddr, addr, ETH_ALEN) &&
206 		    tmp_fil->vlan_id == vlan_id)
207 			return tmp_fil;
208 	}
209 
210 	return NULL;
211 }
212 
213 void qlcnic_add_lb_filter(struct qlcnic_adapter *adapter, struct sk_buff *skb,
214 			  int loopback_pkt, u16 vlan_id)
215 {
216 	struct ethhdr *phdr = (struct ethhdr *)(skb->data);
217 	struct qlcnic_filter *fil, *tmp_fil;
218 	struct hlist_head *head;
219 	unsigned long time;
220 	u64 src_addr = 0;
221 	u8 hindex, op;
222 	int ret;
223 
224 	memcpy(&src_addr, phdr->h_source, ETH_ALEN);
225 	hindex = qlcnic_mac_hash(src_addr) &
226 		 (adapter->fhash.fbucket_size - 1);
227 
228 	if (loopback_pkt) {
229 		if (adapter->rx_fhash.fnum >= adapter->rx_fhash.fmax)
230 			return;
231 
232 		head = &(adapter->rx_fhash.fhead[hindex]);
233 
234 		tmp_fil = qlcnic_find_mac_filter(head, &src_addr, vlan_id);
235 		if (tmp_fil) {
236 			time = tmp_fil->ftime;
237 			if (time_after(jiffies, QLCNIC_READD_AGE * HZ + time))
238 				tmp_fil->ftime = jiffies;
239 			return;
240 		}
241 
242 		fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
243 		if (!fil)
244 			return;
245 
246 		fil->ftime = jiffies;
247 		memcpy(fil->faddr, &src_addr, ETH_ALEN);
248 		fil->vlan_id = vlan_id;
249 		spin_lock(&adapter->rx_mac_learn_lock);
250 		hlist_add_head(&(fil->fnode), head);
251 		adapter->rx_fhash.fnum++;
252 		spin_unlock(&adapter->rx_mac_learn_lock);
253 	} else {
254 		head = &adapter->fhash.fhead[hindex];
255 
256 		spin_lock(&adapter->mac_learn_lock);
257 
258 		tmp_fil = qlcnic_find_mac_filter(head, &src_addr, vlan_id);
259 		if (tmp_fil) {
260 			op = vlan_id ? QLCNIC_MAC_VLAN_DEL : QLCNIC_MAC_DEL;
261 			ret = qlcnic_sre_macaddr_change(adapter,
262 							(u8 *)&src_addr,
263 							vlan_id, op);
264 			if (!ret) {
265 				hlist_del(&tmp_fil->fnode);
266 				adapter->fhash.fnum--;
267 			}
268 
269 			spin_unlock(&adapter->mac_learn_lock);
270 
271 			return;
272 		}
273 
274 		spin_unlock(&adapter->mac_learn_lock);
275 
276 		head = &adapter->rx_fhash.fhead[hindex];
277 
278 		spin_lock(&adapter->rx_mac_learn_lock);
279 
280 		tmp_fil = qlcnic_find_mac_filter(head, &src_addr, vlan_id);
281 		if (tmp_fil)
282 			qlcnic_delete_rx_list_mac(adapter, tmp_fil, &src_addr,
283 						  vlan_id);
284 
285 		spin_unlock(&adapter->rx_mac_learn_lock);
286 	}
287 }
288 
289 void qlcnic_82xx_change_filter(struct qlcnic_adapter *adapter, u64 *uaddr,
290 			       u16 vlan_id)
291 {
292 	struct cmd_desc_type0 *hwdesc;
293 	struct qlcnic_nic_req *req;
294 	struct qlcnic_mac_req *mac_req;
295 	struct qlcnic_vlan_req *vlan_req;
296 	struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
297 	u32 producer;
298 	u64 word;
299 
300 	producer = tx_ring->producer;
301 	hwdesc = &tx_ring->desc_head[tx_ring->producer];
302 
303 	req = (struct qlcnic_nic_req *)hwdesc;
304 	memset(req, 0, sizeof(struct qlcnic_nic_req));
305 	req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
306 
307 	word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
308 	req->req_hdr = cpu_to_le64(word);
309 
310 	mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
311 	mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
312 	memcpy(mac_req->mac_addr, uaddr, ETH_ALEN);
313 
314 	vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
315 	vlan_req->vlan_id = cpu_to_le16(vlan_id);
316 
317 	tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
318 	smp_mb();
319 }
320 
321 static void qlcnic_send_filter(struct qlcnic_adapter *adapter,
322 			       struct cmd_desc_type0 *first_desc,
323 			       struct sk_buff *skb)
324 {
325 	struct qlcnic_filter *fil, *tmp_fil;
326 	struct hlist_node *n;
327 	struct hlist_head *head;
328 	struct net_device *netdev = adapter->netdev;
329 	struct ethhdr *phdr = (struct ethhdr *)(skb->data);
330 	u64 src_addr = 0;
331 	u16 vlan_id = 0;
332 	u8 hindex;
333 
334 	if (ether_addr_equal(phdr->h_source, adapter->mac_addr))
335 		return;
336 
337 	if (adapter->fhash.fnum >= adapter->fhash.fmax) {
338 		adapter->stats.mac_filter_limit_overrun++;
339 		netdev_info(netdev, "Can not add more than %d mac addresses\n",
340 			    adapter->fhash.fmax);
341 		return;
342 	}
343 
344 	memcpy(&src_addr, phdr->h_source, ETH_ALEN);
345 	hindex = qlcnic_mac_hash(src_addr) & (adapter->fhash.fbucket_size - 1);
346 	head = &(adapter->fhash.fhead[hindex]);
347 
348 	hlist_for_each_entry_safe(tmp_fil, n, head, fnode) {
349 		if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
350 		    tmp_fil->vlan_id == vlan_id) {
351 			if (jiffies > (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
352 				qlcnic_change_filter(adapter, &src_addr,
353 						     vlan_id);
354 			tmp_fil->ftime = jiffies;
355 			return;
356 		}
357 	}
358 
359 	fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
360 	if (!fil)
361 		return;
362 
363 	qlcnic_change_filter(adapter, &src_addr, vlan_id);
364 	fil->ftime = jiffies;
365 	fil->vlan_id = vlan_id;
366 	memcpy(fil->faddr, &src_addr, ETH_ALEN);
367 	spin_lock(&adapter->mac_learn_lock);
368 	hlist_add_head(&(fil->fnode), head);
369 	adapter->fhash.fnum++;
370 	spin_unlock(&adapter->mac_learn_lock);
371 }
372 
373 static int qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
374 			 struct cmd_desc_type0 *first_desc, struct sk_buff *skb,
375 			 struct qlcnic_host_tx_ring *tx_ring)
376 {
377 	u8 l4proto, opcode = 0, hdr_len = 0;
378 	u16 flags = 0, vlan_tci = 0;
379 	int copied, offset, copy_len, size;
380 	struct cmd_desc_type0 *hwdesc;
381 	struct vlan_ethhdr *vh;
382 	u16 protocol = ntohs(skb->protocol);
383 	u32 producer = tx_ring->producer;
384 
385 	if (protocol == ETH_P_8021Q) {
386 		vh = (struct vlan_ethhdr *)skb->data;
387 		flags = FLAGS_VLAN_TAGGED;
388 		vlan_tci = ntohs(vh->h_vlan_TCI);
389 		protocol = ntohs(vh->h_vlan_encapsulated_proto);
390 	} else if (vlan_tx_tag_present(skb)) {
391 		flags = FLAGS_VLAN_OOB;
392 		vlan_tci = vlan_tx_tag_get(skb);
393 	}
394 	if (unlikely(adapter->tx_pvid)) {
395 		if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
396 			return -EIO;
397 		if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
398 			goto set_flags;
399 
400 		flags = FLAGS_VLAN_OOB;
401 		vlan_tci = adapter->tx_pvid;
402 	}
403 set_flags:
404 	qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
405 	qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
406 
407 	if (*(skb->data) & BIT_0) {
408 		flags |= BIT_0;
409 		memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
410 	}
411 	opcode = TX_ETHER_PKT;
412 	if (skb_is_gso(skb)) {
413 		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
414 		first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
415 		first_desc->total_hdr_length = hdr_len;
416 		opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
417 
418 		/* For LSO, we need to copy the MAC/IP/TCP headers into
419 		* the descriptor ring */
420 		copied = 0;
421 		offset = 2;
422 
423 		if (flags & FLAGS_VLAN_OOB) {
424 			first_desc->total_hdr_length += VLAN_HLEN;
425 			first_desc->tcp_hdr_offset = VLAN_HLEN;
426 			first_desc->ip_hdr_offset = VLAN_HLEN;
427 
428 			/* Only in case of TSO on vlan device */
429 			flags |= FLAGS_VLAN_TAGGED;
430 
431 			/* Create a TSO vlan header template for firmware */
432 			hwdesc = &tx_ring->desc_head[producer];
433 			tx_ring->cmd_buf_arr[producer].skb = NULL;
434 
435 			copy_len = min((int)sizeof(struct cmd_desc_type0) -
436 				       offset, hdr_len + VLAN_HLEN);
437 
438 			vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
439 			skb_copy_from_linear_data(skb, vh, 12);
440 			vh->h_vlan_proto = htons(ETH_P_8021Q);
441 			vh->h_vlan_TCI = htons(vlan_tci);
442 
443 			skb_copy_from_linear_data_offset(skb, 12,
444 							 (char *)vh + 16,
445 							 copy_len - 16);
446 			copied = copy_len - VLAN_HLEN;
447 			offset = 0;
448 			producer = get_next_index(producer, tx_ring->num_desc);
449 		}
450 
451 		while (copied < hdr_len) {
452 			size = (int)sizeof(struct cmd_desc_type0) - offset;
453 			copy_len = min(size, (hdr_len - copied));
454 			hwdesc = &tx_ring->desc_head[producer];
455 			tx_ring->cmd_buf_arr[producer].skb = NULL;
456 			skb_copy_from_linear_data_offset(skb, copied,
457 							 (char *)hwdesc +
458 							 offset, copy_len);
459 			copied += copy_len;
460 			offset = 0;
461 			producer = get_next_index(producer, tx_ring->num_desc);
462 		}
463 
464 		tx_ring->producer = producer;
465 		smp_mb();
466 		adapter->stats.lso_frames++;
467 
468 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
469 		if (protocol == ETH_P_IP) {
470 			l4proto = ip_hdr(skb)->protocol;
471 
472 			if (l4proto == IPPROTO_TCP)
473 				opcode = TX_TCP_PKT;
474 			else if (l4proto == IPPROTO_UDP)
475 				opcode = TX_UDP_PKT;
476 		} else if (protocol == ETH_P_IPV6) {
477 			l4proto = ipv6_hdr(skb)->nexthdr;
478 
479 			if (l4proto == IPPROTO_TCP)
480 				opcode = TX_TCPV6_PKT;
481 			else if (l4proto == IPPROTO_UDP)
482 				opcode = TX_UDPV6_PKT;
483 		}
484 	}
485 	first_desc->tcp_hdr_offset += skb_transport_offset(skb);
486 	first_desc->ip_hdr_offset += skb_network_offset(skb);
487 	qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
488 
489 	return 0;
490 }
491 
492 static int qlcnic_map_tx_skb(struct pci_dev *pdev, struct sk_buff *skb,
493 			     struct qlcnic_cmd_buffer *pbuf)
494 {
495 	struct qlcnic_skb_frag *nf;
496 	struct skb_frag_struct *frag;
497 	int i, nr_frags;
498 	dma_addr_t map;
499 
500 	nr_frags = skb_shinfo(skb)->nr_frags;
501 	nf = &pbuf->frag_array[0];
502 
503 	map = pci_map_single(pdev, skb->data, skb_headlen(skb),
504 			     PCI_DMA_TODEVICE);
505 	if (pci_dma_mapping_error(pdev, map))
506 		goto out_err;
507 
508 	nf->dma = map;
509 	nf->length = skb_headlen(skb);
510 
511 	for (i = 0; i < nr_frags; i++) {
512 		frag = &skb_shinfo(skb)->frags[i];
513 		nf = &pbuf->frag_array[i+1];
514 		map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
515 				       DMA_TO_DEVICE);
516 		if (dma_mapping_error(&pdev->dev, map))
517 			goto unwind;
518 
519 		nf->dma = map;
520 		nf->length = skb_frag_size(frag);
521 	}
522 
523 	return 0;
524 
525 unwind:
526 	while (--i >= 0) {
527 		nf = &pbuf->frag_array[i+1];
528 		pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
529 	}
530 
531 	nf = &pbuf->frag_array[0];
532 	pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
533 
534 out_err:
535 	return -ENOMEM;
536 }
537 
538 static void qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
539 				 struct qlcnic_cmd_buffer *pbuf)
540 {
541 	struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
542 	int i, nr_frags = skb_shinfo(skb)->nr_frags;
543 
544 	for (i = 0; i < nr_frags; i++) {
545 		nf = &pbuf->frag_array[i+1];
546 		pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
547 	}
548 
549 	nf = &pbuf->frag_array[0];
550 	pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
551 	pbuf->skb = NULL;
552 }
553 
554 static inline void qlcnic_clear_cmddesc(u64 *desc)
555 {
556 	desc[0] = 0ULL;
557 	desc[2] = 0ULL;
558 	desc[7] = 0ULL;
559 }
560 
561 netdev_tx_t qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
562 {
563 	struct qlcnic_adapter *adapter = netdev_priv(netdev);
564 	struct qlcnic_host_tx_ring *tx_ring;
565 	struct qlcnic_cmd_buffer *pbuf;
566 	struct qlcnic_skb_frag *buffrag;
567 	struct cmd_desc_type0 *hwdesc, *first_desc;
568 	struct pci_dev *pdev;
569 	struct ethhdr *phdr;
570 	int i, k, frag_count, delta = 0;
571 	u32 producer, num_txd;
572 
573 	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
574 		netif_tx_stop_all_queues(netdev);
575 		return NETDEV_TX_BUSY;
576 	}
577 
578 	if (adapter->flags & QLCNIC_MACSPOOF) {
579 		phdr = (struct ethhdr *)skb->data;
580 		if (!ether_addr_equal(phdr->h_source, adapter->mac_addr))
581 			goto drop_packet;
582 	}
583 
584 	tx_ring = &adapter->tx_ring[skb_get_queue_mapping(skb)];
585 	num_txd = tx_ring->num_desc;
586 
587 	frag_count = skb_shinfo(skb)->nr_frags + 1;
588 
589 	/* 14 frags supported for normal packet and
590 	 * 32 frags supported for TSO packet
591 	 */
592 	if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
593 		for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
594 			delta += skb_frag_size(&skb_shinfo(skb)->frags[i]);
595 
596 		if (!__pskb_pull_tail(skb, delta))
597 			goto drop_packet;
598 
599 		frag_count = 1 + skb_shinfo(skb)->nr_frags;
600 	}
601 
602 	if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
603 		netif_tx_stop_queue(tx_ring->txq);
604 		if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
605 			netif_tx_start_queue(tx_ring->txq);
606 		} else {
607 			tx_ring->tx_stats.xmit_off++;
608 			return NETDEV_TX_BUSY;
609 		}
610 	}
611 
612 	producer = tx_ring->producer;
613 	pbuf = &tx_ring->cmd_buf_arr[producer];
614 	pdev = adapter->pdev;
615 	first_desc = &tx_ring->desc_head[producer];
616 	hwdesc = &tx_ring->desc_head[producer];
617 	qlcnic_clear_cmddesc((u64 *)hwdesc);
618 
619 	if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
620 		adapter->stats.tx_dma_map_error++;
621 		goto drop_packet;
622 	}
623 
624 	pbuf->skb = skb;
625 	pbuf->frag_count = frag_count;
626 
627 	qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
628 	qlcnic_set_tx_port(first_desc, adapter->portnum);
629 
630 	for (i = 0; i < frag_count; i++) {
631 		k = i % 4;
632 
633 		if ((k == 0) && (i > 0)) {
634 			/* move to next desc.*/
635 			producer = get_next_index(producer, num_txd);
636 			hwdesc = &tx_ring->desc_head[producer];
637 			qlcnic_clear_cmddesc((u64 *)hwdesc);
638 			tx_ring->cmd_buf_arr[producer].skb = NULL;
639 		}
640 
641 		buffrag = &pbuf->frag_array[i];
642 		hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
643 		switch (k) {
644 		case 0:
645 			hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
646 			break;
647 		case 1:
648 			hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
649 			break;
650 		case 2:
651 			hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
652 			break;
653 		case 3:
654 			hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
655 			break;
656 		}
657 	}
658 
659 	tx_ring->producer = get_next_index(producer, num_txd);
660 	smp_mb();
661 
662 	if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb, tx_ring)))
663 		goto unwind_buff;
664 
665 	if (adapter->drv_mac_learn)
666 		qlcnic_send_filter(adapter, first_desc, skb);
667 
668 	tx_ring->tx_stats.tx_bytes += skb->len;
669 	tx_ring->tx_stats.xmit_called++;
670 
671 	qlcnic_update_cmd_producer(tx_ring);
672 
673 	return NETDEV_TX_OK;
674 
675 unwind_buff:
676 	qlcnic_unmap_buffers(pdev, skb, pbuf);
677 drop_packet:
678 	adapter->stats.txdropped++;
679 	dev_kfree_skb_any(skb);
680 	return NETDEV_TX_OK;
681 }
682 
683 void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
684 {
685 	struct net_device *netdev = adapter->netdev;
686 
687 	if (adapter->ahw->linkup && !linkup) {
688 		netdev_info(netdev, "NIC Link is down\n");
689 		adapter->ahw->linkup = 0;
690 		netif_carrier_off(netdev);
691 	} else if (!adapter->ahw->linkup && linkup) {
692 		netdev_info(netdev, "NIC Link is up\n");
693 		adapter->ahw->linkup = 1;
694 		netif_carrier_on(netdev);
695 	}
696 }
697 
698 static int qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
699 			       struct qlcnic_host_rds_ring *rds_ring,
700 			       struct qlcnic_rx_buffer *buffer)
701 {
702 	struct sk_buff *skb;
703 	dma_addr_t dma;
704 	struct pci_dev *pdev = adapter->pdev;
705 
706 	skb = netdev_alloc_skb(adapter->netdev, rds_ring->skb_size);
707 	if (!skb) {
708 		adapter->stats.skb_alloc_failure++;
709 		return -ENOMEM;
710 	}
711 
712 	skb_reserve(skb, NET_IP_ALIGN);
713 	dma = pci_map_single(pdev, skb->data,
714 			     rds_ring->dma_size, PCI_DMA_FROMDEVICE);
715 
716 	if (pci_dma_mapping_error(pdev, dma)) {
717 		adapter->stats.rx_dma_map_error++;
718 		dev_kfree_skb_any(skb);
719 		return -ENOMEM;
720 	}
721 
722 	buffer->skb = skb;
723 	buffer->dma = dma;
724 
725 	return 0;
726 }
727 
728 static void qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
729 					struct qlcnic_host_rds_ring *rds_ring,
730 					u8 ring_id)
731 {
732 	struct rcv_desc *pdesc;
733 	struct qlcnic_rx_buffer *buffer;
734 	int  count = 0;
735 	uint32_t producer, handle;
736 	struct list_head *head;
737 
738 	if (!spin_trylock(&rds_ring->lock))
739 		return;
740 
741 	producer = rds_ring->producer;
742 	head = &rds_ring->free_list;
743 	while (!list_empty(head)) {
744 		buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
745 
746 		if (!buffer->skb) {
747 			if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
748 				break;
749 		}
750 		count++;
751 		list_del(&buffer->list);
752 
753 		/* make a rcv descriptor  */
754 		pdesc = &rds_ring->desc_head[producer];
755 		handle = qlcnic_get_ref_handle(adapter,
756 					       buffer->ref_handle, ring_id);
757 		pdesc->reference_handle = cpu_to_le16(handle);
758 		pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
759 		pdesc->addr_buffer = cpu_to_le64(buffer->dma);
760 		producer = get_next_index(producer, rds_ring->num_desc);
761 	}
762 	if (count) {
763 		rds_ring->producer = producer;
764 		writel((producer - 1) & (rds_ring->num_desc - 1),
765 		       rds_ring->crb_rcv_producer);
766 	}
767 	spin_unlock(&rds_ring->lock);
768 }
769 
770 static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter,
771 				   struct qlcnic_host_tx_ring *tx_ring,
772 				   int budget)
773 {
774 	u32 sw_consumer, hw_consumer;
775 	int i, done, count = 0;
776 	struct qlcnic_cmd_buffer *buffer;
777 	struct pci_dev *pdev = adapter->pdev;
778 	struct net_device *netdev = adapter->netdev;
779 	struct qlcnic_skb_frag *frag;
780 
781 	if (!spin_trylock(&adapter->tx_clean_lock))
782 		return 1;
783 
784 	sw_consumer = tx_ring->sw_consumer;
785 	hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
786 
787 	while (sw_consumer != hw_consumer) {
788 		buffer = &tx_ring->cmd_buf_arr[sw_consumer];
789 		if (buffer->skb) {
790 			frag = &buffer->frag_array[0];
791 			pci_unmap_single(pdev, frag->dma, frag->length,
792 					 PCI_DMA_TODEVICE);
793 			frag->dma = 0ULL;
794 			for (i = 1; i < buffer->frag_count; i++) {
795 				frag++;
796 				pci_unmap_page(pdev, frag->dma, frag->length,
797 					       PCI_DMA_TODEVICE);
798 				frag->dma = 0ULL;
799 			}
800 			tx_ring->tx_stats.xmit_finished++;
801 			dev_kfree_skb_any(buffer->skb);
802 			buffer->skb = NULL;
803 		}
804 
805 		sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
806 		if (++count >= budget)
807 			break;
808 	}
809 
810 	if (count && netif_running(netdev)) {
811 		tx_ring->sw_consumer = sw_consumer;
812 		smp_mb();
813 		if (netif_tx_queue_stopped(tx_ring->txq) &&
814 		    netif_carrier_ok(netdev)) {
815 			if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
816 				netif_tx_wake_queue(tx_ring->txq);
817 				tx_ring->tx_stats.xmit_on++;
818 			}
819 		}
820 		adapter->tx_timeo_cnt = 0;
821 	}
822 	/*
823 	 * If everything is freed up to consumer then check if the ring is full
824 	 * If the ring is full then check if more needs to be freed and
825 	 * schedule the call back again.
826 	 *
827 	 * This happens when there are 2 CPUs. One could be freeing and the
828 	 * other filling it. If the ring is full when we get out of here and
829 	 * the card has already interrupted the host then the host can miss the
830 	 * interrupt.
831 	 *
832 	 * There is still a possible race condition and the host could miss an
833 	 * interrupt. The card has to take care of this.
834 	 */
835 	hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
836 	done = (sw_consumer == hw_consumer);
837 	spin_unlock(&adapter->tx_clean_lock);
838 
839 	return done;
840 }
841 
842 static int qlcnic_poll(struct napi_struct *napi, int budget)
843 {
844 	int tx_complete, work_done;
845 	struct qlcnic_host_sds_ring *sds_ring;
846 	struct qlcnic_adapter *adapter;
847 	struct qlcnic_host_tx_ring *tx_ring;
848 
849 	sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
850 	adapter = sds_ring->adapter;
851 	tx_ring = sds_ring->tx_ring;
852 
853 	tx_complete = qlcnic_process_cmd_ring(adapter, tx_ring,
854 					      budget);
855 	work_done = qlcnic_process_rcv_ring(sds_ring, budget);
856 	if ((work_done < budget) && tx_complete) {
857 		napi_complete(&sds_ring->napi);
858 		if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
859 			qlcnic_enable_int(sds_ring);
860 			qlcnic_enable_tx_intr(adapter, tx_ring);
861 		}
862 	}
863 
864 	return work_done;
865 }
866 
867 static int qlcnic_tx_poll(struct napi_struct *napi, int budget)
868 {
869 	struct qlcnic_host_tx_ring *tx_ring;
870 	struct qlcnic_adapter *adapter;
871 	int work_done;
872 
873 	tx_ring = container_of(napi, struct qlcnic_host_tx_ring, napi);
874 	adapter = tx_ring->adapter;
875 
876 	work_done = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
877 	if (work_done) {
878 		napi_complete(&tx_ring->napi);
879 		if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
880 			qlcnic_enable_tx_intr(adapter, tx_ring);
881 	}
882 
883 	return work_done;
884 }
885 
886 static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
887 {
888 	struct qlcnic_host_sds_ring *sds_ring;
889 	struct qlcnic_adapter *adapter;
890 	int work_done;
891 
892 	sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
893 	adapter = sds_ring->adapter;
894 
895 	work_done = qlcnic_process_rcv_ring(sds_ring, budget);
896 
897 	if (work_done < budget) {
898 		napi_complete(&sds_ring->napi);
899 		if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
900 			qlcnic_enable_int(sds_ring);
901 	}
902 
903 	return work_done;
904 }
905 
906 static void qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
907 				    struct qlcnic_fw_msg *msg)
908 {
909 	u32 cable_OUI;
910 	u16 cable_len, link_speed;
911 	u8  link_status, module, duplex, autoneg, lb_status = 0;
912 	struct net_device *netdev = adapter->netdev;
913 
914 	adapter->ahw->has_link_events = 1;
915 
916 	cable_OUI = msg->body[1] & 0xffffffff;
917 	cable_len = (msg->body[1] >> 32) & 0xffff;
918 	link_speed = (msg->body[1] >> 48) & 0xffff;
919 
920 	link_status = msg->body[2] & 0xff;
921 	duplex = (msg->body[2] >> 16) & 0xff;
922 	autoneg = (msg->body[2] >> 24) & 0xff;
923 	lb_status = (msg->body[2] >> 32) & 0x3;
924 
925 	module = (msg->body[2] >> 8) & 0xff;
926 	if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
927 		dev_info(&netdev->dev,
928 			 "unsupported cable: OUI 0x%x, length %d\n",
929 			 cable_OUI, cable_len);
930 	else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
931 		dev_info(&netdev->dev, "unsupported cable length %d\n",
932 			 cable_len);
933 
934 	if (!link_status && (lb_status == QLCNIC_ILB_MODE ||
935 	    lb_status == QLCNIC_ELB_MODE))
936 		adapter->ahw->loopback_state |= QLCNIC_LINKEVENT;
937 
938 	qlcnic_advert_link_change(adapter, link_status);
939 
940 	if (duplex == LINKEVENT_FULL_DUPLEX)
941 		adapter->ahw->link_duplex = DUPLEX_FULL;
942 	else
943 		adapter->ahw->link_duplex = DUPLEX_HALF;
944 
945 	adapter->ahw->module_type = module;
946 	adapter->ahw->link_autoneg = autoneg;
947 
948 	if (link_status) {
949 		adapter->ahw->link_speed = link_speed;
950 	} else {
951 		adapter->ahw->link_speed = SPEED_UNKNOWN;
952 		adapter->ahw->link_duplex = DUPLEX_UNKNOWN;
953 	}
954 }
955 
956 static void qlcnic_handle_fw_message(int desc_cnt, int index,
957 				     struct qlcnic_host_sds_ring *sds_ring)
958 {
959 	struct qlcnic_fw_msg msg;
960 	struct status_desc *desc;
961 	struct qlcnic_adapter *adapter;
962 	struct device *dev;
963 	int i = 0, opcode, ret;
964 
965 	while (desc_cnt > 0 && i < 8) {
966 		desc = &sds_ring->desc_head[index];
967 		msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
968 		msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
969 
970 		index = get_next_index(index, sds_ring->num_desc);
971 		desc_cnt--;
972 	}
973 
974 	adapter = sds_ring->adapter;
975 	dev = &adapter->pdev->dev;
976 	opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
977 
978 	switch (opcode) {
979 	case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
980 		qlcnic_handle_linkevent(adapter, &msg);
981 		break;
982 	case QLCNIC_C2H_OPCODE_CONFIG_LOOPBACK:
983 		ret = (u32)(msg.body[1]);
984 		switch (ret) {
985 		case 0:
986 			adapter->ahw->loopback_state |= QLCNIC_LB_RESPONSE;
987 			break;
988 		case 1:
989 			dev_info(dev, "loopback already in progress\n");
990 			adapter->ahw->diag_cnt = -EINPROGRESS;
991 			break;
992 		case 2:
993 			dev_info(dev, "loopback cable is not connected\n");
994 			adapter->ahw->diag_cnt = -ENODEV;
995 			break;
996 		default:
997 			dev_info(dev,
998 				 "loopback configure request failed, err %x\n",
999 				 ret);
1000 			adapter->ahw->diag_cnt = -EIO;
1001 			break;
1002 		}
1003 		break;
1004 	case QLCNIC_C2H_OPCODE_GET_DCB_AEN:
1005 		qlcnic_dcb_aen_handler(adapter->dcb, (void *)&msg);
1006 		break;
1007 	default:
1008 		break;
1009 	}
1010 }
1011 
1012 struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
1013 				     struct qlcnic_host_rds_ring *ring,
1014 				     u16 index, u16 cksum)
1015 {
1016 	struct qlcnic_rx_buffer *buffer;
1017 	struct sk_buff *skb;
1018 
1019 	buffer = &ring->rx_buf_arr[index];
1020 	if (unlikely(buffer->skb == NULL)) {
1021 		WARN_ON(1);
1022 		return NULL;
1023 	}
1024 
1025 	pci_unmap_single(adapter->pdev, buffer->dma, ring->dma_size,
1026 			 PCI_DMA_FROMDEVICE);
1027 
1028 	skb = buffer->skb;
1029 	if (likely((adapter->netdev->features & NETIF_F_RXCSUM) &&
1030 		   (cksum == STATUS_CKSUM_OK || cksum == STATUS_CKSUM_LOOP))) {
1031 		adapter->stats.csummed++;
1032 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1033 	} else {
1034 		skb_checksum_none_assert(skb);
1035 	}
1036 
1037 
1038 	buffer->skb = NULL;
1039 
1040 	return skb;
1041 }
1042 
1043 static inline int qlcnic_check_rx_tagging(struct qlcnic_adapter *adapter,
1044 					  struct sk_buff *skb, u16 *vlan_tag)
1045 {
1046 	struct ethhdr *eth_hdr;
1047 
1048 	if (!__vlan_get_tag(skb, vlan_tag)) {
1049 		eth_hdr = (struct ethhdr *)skb->data;
1050 		memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
1051 		skb_pull(skb, VLAN_HLEN);
1052 	}
1053 	if (!adapter->rx_pvid)
1054 		return 0;
1055 
1056 	if (*vlan_tag == adapter->rx_pvid) {
1057 		/* Outer vlan tag. Packet should follow non-vlan path */
1058 		*vlan_tag = 0xffff;
1059 		return 0;
1060 	}
1061 	if (adapter->flags & QLCNIC_TAGGING_ENABLED)
1062 		return 0;
1063 
1064 	return -EINVAL;
1065 }
1066 
1067 static struct qlcnic_rx_buffer *
1068 qlcnic_process_rcv(struct qlcnic_adapter *adapter,
1069 		   struct qlcnic_host_sds_ring *sds_ring, int ring,
1070 		   u64 sts_data0)
1071 {
1072 	struct net_device *netdev = adapter->netdev;
1073 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1074 	struct qlcnic_rx_buffer *buffer;
1075 	struct sk_buff *skb;
1076 	struct qlcnic_host_rds_ring *rds_ring;
1077 	int index, length, cksum, pkt_offset, is_lb_pkt;
1078 	u16 vid = 0xffff, t_vid;
1079 
1080 	if (unlikely(ring >= adapter->max_rds_rings))
1081 		return NULL;
1082 
1083 	rds_ring = &recv_ctx->rds_rings[ring];
1084 
1085 	index = qlcnic_get_sts_refhandle(sts_data0);
1086 	if (unlikely(index >= rds_ring->num_desc))
1087 		return NULL;
1088 
1089 	buffer = &rds_ring->rx_buf_arr[index];
1090 	length = qlcnic_get_sts_totallength(sts_data0);
1091 	cksum  = qlcnic_get_sts_status(sts_data0);
1092 	pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1093 
1094 	skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1095 	if (!skb)
1096 		return buffer;
1097 
1098 	if (adapter->drv_mac_learn &&
1099 	    (adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
1100 		t_vid = 0;
1101 		is_lb_pkt = qlcnic_82xx_is_lb_pkt(sts_data0);
1102 		qlcnic_add_lb_filter(adapter, skb, is_lb_pkt, t_vid);
1103 	}
1104 
1105 	if (length > rds_ring->skb_size)
1106 		skb_put(skb, rds_ring->skb_size);
1107 	else
1108 		skb_put(skb, length);
1109 
1110 	if (pkt_offset)
1111 		skb_pull(skb, pkt_offset);
1112 
1113 	if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1114 		adapter->stats.rxdropped++;
1115 		dev_kfree_skb(skb);
1116 		return buffer;
1117 	}
1118 
1119 	skb->protocol = eth_type_trans(skb, netdev);
1120 
1121 	if (vid != 0xffff)
1122 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1123 
1124 	napi_gro_receive(&sds_ring->napi, skb);
1125 
1126 	adapter->stats.rx_pkts++;
1127 	adapter->stats.rxbytes += length;
1128 
1129 	return buffer;
1130 }
1131 
1132 #define QLC_TCP_HDR_SIZE            20
1133 #define QLC_TCP_TS_OPTION_SIZE      12
1134 #define QLC_TCP_TS_HDR_SIZE         (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
1135 
1136 static struct qlcnic_rx_buffer *
1137 qlcnic_process_lro(struct qlcnic_adapter *adapter,
1138 		   int ring, u64 sts_data0, u64 sts_data1)
1139 {
1140 	struct net_device *netdev = adapter->netdev;
1141 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1142 	struct qlcnic_rx_buffer *buffer;
1143 	struct sk_buff *skb;
1144 	struct qlcnic_host_rds_ring *rds_ring;
1145 	struct iphdr *iph;
1146 	struct ipv6hdr *ipv6h;
1147 	struct tcphdr *th;
1148 	bool push, timestamp;
1149 	int index, l2_hdr_offset, l4_hdr_offset, is_lb_pkt;
1150 	u16 lro_length, length, data_offset, t_vid, vid = 0xffff;
1151 	u32 seq_number;
1152 
1153 	if (unlikely(ring > adapter->max_rds_rings))
1154 		return NULL;
1155 
1156 	rds_ring = &recv_ctx->rds_rings[ring];
1157 
1158 	index = qlcnic_get_lro_sts_refhandle(sts_data0);
1159 	if (unlikely(index > rds_ring->num_desc))
1160 		return NULL;
1161 
1162 	buffer = &rds_ring->rx_buf_arr[index];
1163 
1164 	timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1165 	lro_length = qlcnic_get_lro_sts_length(sts_data0);
1166 	l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1167 	l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1168 	push = qlcnic_get_lro_sts_push_flag(sts_data0);
1169 	seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1170 
1171 	skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1172 	if (!skb)
1173 		return buffer;
1174 
1175 	if (adapter->drv_mac_learn &&
1176 	    (adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
1177 		t_vid = 0;
1178 		is_lb_pkt = qlcnic_82xx_is_lb_pkt(sts_data0);
1179 		qlcnic_add_lb_filter(adapter, skb, is_lb_pkt, t_vid);
1180 	}
1181 
1182 	if (timestamp)
1183 		data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1184 	else
1185 		data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1186 
1187 	skb_put(skb, lro_length + data_offset);
1188 	skb_pull(skb, l2_hdr_offset);
1189 
1190 	if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1191 		adapter->stats.rxdropped++;
1192 		dev_kfree_skb(skb);
1193 		return buffer;
1194 	}
1195 
1196 	skb->protocol = eth_type_trans(skb, netdev);
1197 
1198 	if (ntohs(skb->protocol) == ETH_P_IPV6) {
1199 		ipv6h = (struct ipv6hdr *)skb->data;
1200 		th = (struct tcphdr *)(skb->data + sizeof(struct ipv6hdr));
1201 		length = (th->doff << 2) + lro_length;
1202 		ipv6h->payload_len = htons(length);
1203 	} else {
1204 		iph = (struct iphdr *)skb->data;
1205 		th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1206 		length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1207 		csum_replace2(&iph->check, iph->tot_len, htons(length));
1208 		iph->tot_len = htons(length);
1209 	}
1210 
1211 	th->psh = push;
1212 	th->seq = htonl(seq_number);
1213 	length = skb->len;
1214 
1215 	if (adapter->flags & QLCNIC_FW_LRO_MSS_CAP) {
1216 		skb_shinfo(skb)->gso_size = qlcnic_get_lro_sts_mss(sts_data1);
1217 		if (skb->protocol == htons(ETH_P_IPV6))
1218 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1219 		else
1220 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1221 	}
1222 
1223 	if (vid != 0xffff)
1224 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1225 	netif_receive_skb(skb);
1226 
1227 	adapter->stats.lro_pkts++;
1228 	adapter->stats.lrobytes += length;
1229 
1230 	return buffer;
1231 }
1232 
1233 int qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
1234 {
1235 	struct qlcnic_host_rds_ring *rds_ring;
1236 	struct qlcnic_adapter *adapter = sds_ring->adapter;
1237 	struct list_head *cur;
1238 	struct status_desc *desc;
1239 	struct qlcnic_rx_buffer *rxbuf;
1240 	int opcode, desc_cnt, count = 0;
1241 	u64 sts_data0, sts_data1;
1242 	u8 ring;
1243 	u32 consumer = sds_ring->consumer;
1244 
1245 	while (count < max) {
1246 		desc = &sds_ring->desc_head[consumer];
1247 		sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1248 
1249 		if (!(sts_data0 & STATUS_OWNER_HOST))
1250 			break;
1251 
1252 		desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1253 		opcode = qlcnic_get_sts_opcode(sts_data0);
1254 		switch (opcode) {
1255 		case QLCNIC_RXPKT_DESC:
1256 		case QLCNIC_OLD_RXPKT_DESC:
1257 		case QLCNIC_SYN_OFFLOAD:
1258 			ring = qlcnic_get_sts_type(sts_data0);
1259 			rxbuf = qlcnic_process_rcv(adapter, sds_ring, ring,
1260 						   sts_data0);
1261 			break;
1262 		case QLCNIC_LRO_DESC:
1263 			ring = qlcnic_get_lro_sts_type(sts_data0);
1264 			sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1265 			rxbuf = qlcnic_process_lro(adapter, ring, sts_data0,
1266 						   sts_data1);
1267 			break;
1268 		case QLCNIC_RESPONSE_DESC:
1269 			qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1270 		default:
1271 			goto skip;
1272 		}
1273 		WARN_ON(desc_cnt > 1);
1274 
1275 		if (likely(rxbuf))
1276 			list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1277 		else
1278 			adapter->stats.null_rxbuf++;
1279 skip:
1280 		for (; desc_cnt > 0; desc_cnt--) {
1281 			desc = &sds_ring->desc_head[consumer];
1282 			desc->status_desc_data[0] = QLCNIC_DESC_OWNER_FW;
1283 			consumer = get_next_index(consumer, sds_ring->num_desc);
1284 		}
1285 		count++;
1286 	}
1287 
1288 	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1289 		rds_ring = &adapter->recv_ctx->rds_rings[ring];
1290 		if (!list_empty(&sds_ring->free_list[ring])) {
1291 			list_for_each(cur, &sds_ring->free_list[ring]) {
1292 				rxbuf = list_entry(cur, struct qlcnic_rx_buffer,
1293 						   list);
1294 				qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1295 			}
1296 			spin_lock(&rds_ring->lock);
1297 			list_splice_tail_init(&sds_ring->free_list[ring],
1298 					      &rds_ring->free_list);
1299 			spin_unlock(&rds_ring->lock);
1300 		}
1301 
1302 		qlcnic_post_rx_buffers_nodb(adapter, rds_ring, ring);
1303 	}
1304 
1305 	if (count) {
1306 		sds_ring->consumer = consumer;
1307 		writel(consumer, sds_ring->crb_sts_consumer);
1308 	}
1309 
1310 	return count;
1311 }
1312 
1313 void qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter,
1314 			    struct qlcnic_host_rds_ring *rds_ring, u8 ring_id)
1315 {
1316 	struct rcv_desc *pdesc;
1317 	struct qlcnic_rx_buffer *buffer;
1318 	int count = 0;
1319 	u32 producer, handle;
1320 	struct list_head *head;
1321 
1322 	producer = rds_ring->producer;
1323 	head = &rds_ring->free_list;
1324 
1325 	while (!list_empty(head)) {
1326 
1327 		buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1328 
1329 		if (!buffer->skb) {
1330 			if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1331 				break;
1332 		}
1333 
1334 		count++;
1335 		list_del(&buffer->list);
1336 
1337 		/* make a rcv descriptor  */
1338 		pdesc = &rds_ring->desc_head[producer];
1339 		pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1340 		handle = qlcnic_get_ref_handle(adapter, buffer->ref_handle,
1341 					       ring_id);
1342 		pdesc->reference_handle = cpu_to_le16(handle);
1343 		pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1344 		producer = get_next_index(producer, rds_ring->num_desc);
1345 	}
1346 
1347 	if (count) {
1348 		rds_ring->producer = producer;
1349 		writel((producer-1) & (rds_ring->num_desc-1),
1350 		       rds_ring->crb_rcv_producer);
1351 	}
1352 }
1353 
1354 static void dump_skb(struct sk_buff *skb, struct qlcnic_adapter *adapter)
1355 {
1356 	int i;
1357 	unsigned char *data = skb->data;
1358 
1359 	pr_info(KERN_INFO "\n");
1360 	for (i = 0; i < skb->len; i++) {
1361 		QLCDB(adapter, DRV, "%02x ", data[i]);
1362 		if ((i & 0x0f) == 8)
1363 			pr_info(KERN_INFO "\n");
1364 	}
1365 }
1366 
1367 static void qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter, int ring,
1368 				    u64 sts_data0)
1369 {
1370 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1371 	struct sk_buff *skb;
1372 	struct qlcnic_host_rds_ring *rds_ring;
1373 	int index, length, cksum, pkt_offset;
1374 
1375 	if (unlikely(ring >= adapter->max_rds_rings))
1376 		return;
1377 
1378 	rds_ring = &recv_ctx->rds_rings[ring];
1379 
1380 	index = qlcnic_get_sts_refhandle(sts_data0);
1381 	length = qlcnic_get_sts_totallength(sts_data0);
1382 	if (unlikely(index >= rds_ring->num_desc))
1383 		return;
1384 
1385 	cksum  = qlcnic_get_sts_status(sts_data0);
1386 	pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1387 
1388 	skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1389 	if (!skb)
1390 		return;
1391 
1392 	if (length > rds_ring->skb_size)
1393 		skb_put(skb, rds_ring->skb_size);
1394 	else
1395 		skb_put(skb, length);
1396 
1397 	if (pkt_offset)
1398 		skb_pull(skb, pkt_offset);
1399 
1400 	if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
1401 		adapter->ahw->diag_cnt++;
1402 	else
1403 		dump_skb(skb, adapter);
1404 
1405 	dev_kfree_skb_any(skb);
1406 	adapter->stats.rx_pkts++;
1407 	adapter->stats.rxbytes += length;
1408 
1409 	return;
1410 }
1411 
1412 void qlcnic_82xx_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
1413 {
1414 	struct qlcnic_adapter *adapter = sds_ring->adapter;
1415 	struct status_desc *desc;
1416 	u64 sts_data0;
1417 	int ring, opcode, desc_cnt;
1418 
1419 	u32 consumer = sds_ring->consumer;
1420 
1421 	desc = &sds_ring->desc_head[consumer];
1422 	sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1423 
1424 	if (!(sts_data0 & STATUS_OWNER_HOST))
1425 		return;
1426 
1427 	desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1428 	opcode = qlcnic_get_sts_opcode(sts_data0);
1429 	switch (opcode) {
1430 	case QLCNIC_RESPONSE_DESC:
1431 		qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1432 		break;
1433 	default:
1434 		ring = qlcnic_get_sts_type(sts_data0);
1435 		qlcnic_process_rcv_diag(adapter, ring, sts_data0);
1436 		break;
1437 	}
1438 
1439 	for (; desc_cnt > 0; desc_cnt--) {
1440 		desc = &sds_ring->desc_head[consumer];
1441 		desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1442 		consumer = get_next_index(consumer, sds_ring->num_desc);
1443 	}
1444 
1445 	sds_ring->consumer = consumer;
1446 	writel(consumer, sds_ring->crb_sts_consumer);
1447 }
1448 
1449 int qlcnic_82xx_napi_add(struct qlcnic_adapter *adapter,
1450 			 struct net_device *netdev)
1451 {
1452 	int ring;
1453 	struct qlcnic_host_sds_ring *sds_ring;
1454 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1455 	struct qlcnic_host_tx_ring *tx_ring;
1456 
1457 	if (qlcnic_alloc_sds_rings(recv_ctx, adapter->drv_sds_rings))
1458 		return -ENOMEM;
1459 
1460 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1461 		sds_ring = &recv_ctx->sds_rings[ring];
1462 		if (qlcnic_check_multi_tx(adapter) &&
1463 		    !adapter->ahw->diag_test &&
1464 		    (adapter->drv_tx_rings > QLCNIC_SINGLE_RING)) {
1465 			netif_napi_add(netdev, &sds_ring->napi, qlcnic_rx_poll,
1466 				       NAPI_POLL_WEIGHT);
1467 		} else {
1468 			if (ring == (adapter->drv_sds_rings - 1))
1469 				netif_napi_add(netdev, &sds_ring->napi,
1470 					       qlcnic_poll,
1471 					       NAPI_POLL_WEIGHT);
1472 			else
1473 				netif_napi_add(netdev, &sds_ring->napi,
1474 					       qlcnic_rx_poll,
1475 					       NAPI_POLL_WEIGHT);
1476 		}
1477 	}
1478 
1479 	if (qlcnic_alloc_tx_rings(adapter, netdev)) {
1480 		qlcnic_free_sds_rings(recv_ctx);
1481 		return -ENOMEM;
1482 	}
1483 
1484 	if (qlcnic_check_multi_tx(adapter) && !adapter->ahw->diag_test) {
1485 		for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1486 			tx_ring = &adapter->tx_ring[ring];
1487 			netif_napi_add(netdev, &tx_ring->napi, qlcnic_tx_poll,
1488 				       NAPI_POLL_WEIGHT);
1489 		}
1490 	}
1491 
1492 	return 0;
1493 }
1494 
1495 void qlcnic_82xx_napi_del(struct qlcnic_adapter *adapter)
1496 {
1497 	int ring;
1498 	struct qlcnic_host_sds_ring *sds_ring;
1499 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1500 	struct qlcnic_host_tx_ring *tx_ring;
1501 
1502 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1503 		sds_ring = &recv_ctx->sds_rings[ring];
1504 		netif_napi_del(&sds_ring->napi);
1505 	}
1506 
1507 	qlcnic_free_sds_rings(adapter->recv_ctx);
1508 
1509 	if (qlcnic_check_multi_tx(adapter) && !adapter->ahw->diag_test) {
1510 		for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1511 			tx_ring = &adapter->tx_ring[ring];
1512 			netif_napi_del(&tx_ring->napi);
1513 		}
1514 	}
1515 
1516 	qlcnic_free_tx_rings(adapter);
1517 }
1518 
1519 void qlcnic_82xx_napi_enable(struct qlcnic_adapter *adapter)
1520 {
1521 	int ring;
1522 	struct qlcnic_host_sds_ring *sds_ring;
1523 	struct qlcnic_host_tx_ring *tx_ring;
1524 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1525 
1526 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1527 		return;
1528 
1529 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1530 		sds_ring = &recv_ctx->sds_rings[ring];
1531 		napi_enable(&sds_ring->napi);
1532 		qlcnic_enable_int(sds_ring);
1533 	}
1534 
1535 	if (qlcnic_check_multi_tx(adapter) &&
1536 	    (adapter->flags & QLCNIC_MSIX_ENABLED) &&
1537 	    !adapter->ahw->diag_test &&
1538 	    (adapter->drv_tx_rings > QLCNIC_SINGLE_RING)) {
1539 		for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1540 			tx_ring = &adapter->tx_ring[ring];
1541 			napi_enable(&tx_ring->napi);
1542 			qlcnic_enable_tx_intr(adapter, tx_ring);
1543 		}
1544 	}
1545 }
1546 
1547 void qlcnic_82xx_napi_disable(struct qlcnic_adapter *adapter)
1548 {
1549 	int ring;
1550 	struct qlcnic_host_sds_ring *sds_ring;
1551 	struct qlcnic_host_tx_ring *tx_ring;
1552 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1553 
1554 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1555 		return;
1556 
1557 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1558 		sds_ring = &recv_ctx->sds_rings[ring];
1559 		qlcnic_disable_int(sds_ring);
1560 		napi_synchronize(&sds_ring->napi);
1561 		napi_disable(&sds_ring->napi);
1562 	}
1563 
1564 	if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
1565 	    !adapter->ahw->diag_test &&
1566 	    qlcnic_check_multi_tx(adapter)) {
1567 		for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1568 			tx_ring = &adapter->tx_ring[ring];
1569 			qlcnic_disable_tx_int(adapter, tx_ring);
1570 			napi_synchronize(&tx_ring->napi);
1571 			napi_disable(&tx_ring->napi);
1572 		}
1573 	}
1574 }
1575 
1576 #define QLC_83XX_NORMAL_LB_PKT	(1ULL << 36)
1577 #define QLC_83XX_LRO_LB_PKT	(1ULL << 46)
1578 
1579 static inline int qlcnic_83xx_is_lb_pkt(u64 sts_data, int lro_pkt)
1580 {
1581 	if (lro_pkt)
1582 		return (sts_data & QLC_83XX_LRO_LB_PKT) ? 1 : 0;
1583 	else
1584 		return (sts_data & QLC_83XX_NORMAL_LB_PKT) ? 1 : 0;
1585 }
1586 
1587 static struct qlcnic_rx_buffer *
1588 qlcnic_83xx_process_rcv(struct qlcnic_adapter *adapter,
1589 			struct qlcnic_host_sds_ring *sds_ring,
1590 			u8 ring, u64 sts_data[])
1591 {
1592 	struct net_device *netdev = adapter->netdev;
1593 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1594 	struct qlcnic_rx_buffer *buffer;
1595 	struct sk_buff *skb;
1596 	struct qlcnic_host_rds_ring *rds_ring;
1597 	int index, length, cksum, is_lb_pkt;
1598 	u16 vid = 0xffff, t_vid;
1599 
1600 	if (unlikely(ring >= adapter->max_rds_rings))
1601 		return NULL;
1602 
1603 	rds_ring = &recv_ctx->rds_rings[ring];
1604 
1605 	index = qlcnic_83xx_hndl(sts_data[0]);
1606 	if (unlikely(index >= rds_ring->num_desc))
1607 		return NULL;
1608 
1609 	buffer = &rds_ring->rx_buf_arr[index];
1610 	length = qlcnic_83xx_pktln(sts_data[0]);
1611 	cksum  = qlcnic_83xx_csum_status(sts_data[1]);
1612 	skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1613 	if (!skb)
1614 		return buffer;
1615 
1616 	if (adapter->drv_mac_learn &&
1617 	    (adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
1618 		t_vid = 0;
1619 		is_lb_pkt = qlcnic_83xx_is_lb_pkt(sts_data[1], 0);
1620 		qlcnic_add_lb_filter(adapter, skb, is_lb_pkt, t_vid);
1621 	}
1622 
1623 	if (length > rds_ring->skb_size)
1624 		skb_put(skb, rds_ring->skb_size);
1625 	else
1626 		skb_put(skb, length);
1627 
1628 	if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1629 		adapter->stats.rxdropped++;
1630 		dev_kfree_skb(skb);
1631 		return buffer;
1632 	}
1633 
1634 	skb->protocol = eth_type_trans(skb, netdev);
1635 
1636 	if (vid != 0xffff)
1637 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1638 
1639 	napi_gro_receive(&sds_ring->napi, skb);
1640 
1641 	adapter->stats.rx_pkts++;
1642 	adapter->stats.rxbytes += length;
1643 
1644 	return buffer;
1645 }
1646 
1647 static struct qlcnic_rx_buffer *
1648 qlcnic_83xx_process_lro(struct qlcnic_adapter *adapter,
1649 			u8 ring, u64 sts_data[])
1650 {
1651 	struct net_device *netdev = adapter->netdev;
1652 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1653 	struct qlcnic_rx_buffer *buffer;
1654 	struct sk_buff *skb;
1655 	struct qlcnic_host_rds_ring *rds_ring;
1656 	struct iphdr *iph;
1657 	struct ipv6hdr *ipv6h;
1658 	struct tcphdr *th;
1659 	bool push;
1660 	int l2_hdr_offset, l4_hdr_offset;
1661 	int index, is_lb_pkt;
1662 	u16 lro_length, length, data_offset, gso_size;
1663 	u16 vid = 0xffff, t_vid;
1664 
1665 	if (unlikely(ring > adapter->max_rds_rings))
1666 		return NULL;
1667 
1668 	rds_ring = &recv_ctx->rds_rings[ring];
1669 
1670 	index = qlcnic_83xx_hndl(sts_data[0]);
1671 	if (unlikely(index > rds_ring->num_desc))
1672 		return NULL;
1673 
1674 	buffer = &rds_ring->rx_buf_arr[index];
1675 
1676 	lro_length = qlcnic_83xx_lro_pktln(sts_data[0]);
1677 	l2_hdr_offset = qlcnic_83xx_l2_hdr_off(sts_data[1]);
1678 	l4_hdr_offset = qlcnic_83xx_l4_hdr_off(sts_data[1]);
1679 	push = qlcnic_83xx_is_psh_bit(sts_data[1]);
1680 
1681 	skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1682 	if (!skb)
1683 		return buffer;
1684 
1685 	if (adapter->drv_mac_learn &&
1686 	    (adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
1687 		t_vid = 0;
1688 		is_lb_pkt = qlcnic_83xx_is_lb_pkt(sts_data[1], 1);
1689 		qlcnic_add_lb_filter(adapter, skb, is_lb_pkt, t_vid);
1690 	}
1691 	if (qlcnic_83xx_is_tstamp(sts_data[1]))
1692 		data_offset = l4_hdr_offset + QLCNIC_TCP_TS_HDR_SIZE;
1693 	else
1694 		data_offset = l4_hdr_offset + QLCNIC_TCP_HDR_SIZE;
1695 
1696 	skb_put(skb, lro_length + data_offset);
1697 	skb_pull(skb, l2_hdr_offset);
1698 
1699 	if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1700 		adapter->stats.rxdropped++;
1701 		dev_kfree_skb(skb);
1702 		return buffer;
1703 	}
1704 
1705 	skb->protocol = eth_type_trans(skb, netdev);
1706 	if (ntohs(skb->protocol) == ETH_P_IPV6) {
1707 		ipv6h = (struct ipv6hdr *)skb->data;
1708 		th = (struct tcphdr *)(skb->data + sizeof(struct ipv6hdr));
1709 
1710 		length = (th->doff << 2) + lro_length;
1711 		ipv6h->payload_len = htons(length);
1712 	} else {
1713 		iph = (struct iphdr *)skb->data;
1714 		th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1715 		length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1716 		csum_replace2(&iph->check, iph->tot_len, htons(length));
1717 		iph->tot_len = htons(length);
1718 	}
1719 
1720 	th->psh = push;
1721 	length = skb->len;
1722 
1723 	if (adapter->flags & QLCNIC_FW_LRO_MSS_CAP) {
1724 		gso_size = qlcnic_83xx_get_lro_sts_mss(sts_data[0]);
1725 		skb_shinfo(skb)->gso_size = gso_size;
1726 		if (skb->protocol == htons(ETH_P_IPV6))
1727 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1728 		else
1729 			skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1730 	}
1731 
1732 	if (vid != 0xffff)
1733 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1734 
1735 	netif_receive_skb(skb);
1736 
1737 	adapter->stats.lro_pkts++;
1738 	adapter->stats.lrobytes += length;
1739 	return buffer;
1740 }
1741 
1742 static int qlcnic_83xx_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring,
1743 					int max)
1744 {
1745 	struct qlcnic_host_rds_ring *rds_ring;
1746 	struct qlcnic_adapter *adapter = sds_ring->adapter;
1747 	struct list_head *cur;
1748 	struct status_desc *desc;
1749 	struct qlcnic_rx_buffer *rxbuf = NULL;
1750 	u8 ring;
1751 	u64 sts_data[2];
1752 	int count = 0, opcode;
1753 	u32 consumer = sds_ring->consumer;
1754 
1755 	while (count < max) {
1756 		desc = &sds_ring->desc_head[consumer];
1757 		sts_data[1] = le64_to_cpu(desc->status_desc_data[1]);
1758 		opcode = qlcnic_83xx_opcode(sts_data[1]);
1759 		if (!opcode)
1760 			break;
1761 		sts_data[0] = le64_to_cpu(desc->status_desc_data[0]);
1762 		ring = QLCNIC_FETCH_RING_ID(sts_data[0]);
1763 
1764 		switch (opcode) {
1765 		case QLC_83XX_REG_DESC:
1766 			rxbuf = qlcnic_83xx_process_rcv(adapter, sds_ring,
1767 							ring, sts_data);
1768 			break;
1769 		case QLC_83XX_LRO_DESC:
1770 			rxbuf = qlcnic_83xx_process_lro(adapter, ring,
1771 							sts_data);
1772 			break;
1773 		default:
1774 			dev_info(&adapter->pdev->dev,
1775 				 "Unknown opcode: 0x%x\n", opcode);
1776 			goto skip;
1777 		}
1778 
1779 		if (likely(rxbuf))
1780 			list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1781 		else
1782 			adapter->stats.null_rxbuf++;
1783 skip:
1784 		desc = &sds_ring->desc_head[consumer];
1785 		/* Reset the descriptor */
1786 		desc->status_desc_data[1] = 0;
1787 		consumer = get_next_index(consumer, sds_ring->num_desc);
1788 		count++;
1789 	}
1790 	for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1791 		rds_ring = &adapter->recv_ctx->rds_rings[ring];
1792 		if (!list_empty(&sds_ring->free_list[ring])) {
1793 			list_for_each(cur, &sds_ring->free_list[ring]) {
1794 				rxbuf = list_entry(cur, struct qlcnic_rx_buffer,
1795 						   list);
1796 				qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1797 			}
1798 			spin_lock(&rds_ring->lock);
1799 			list_splice_tail_init(&sds_ring->free_list[ring],
1800 					      &rds_ring->free_list);
1801 			spin_unlock(&rds_ring->lock);
1802 		}
1803 		qlcnic_post_rx_buffers_nodb(adapter, rds_ring, ring);
1804 	}
1805 	if (count) {
1806 		sds_ring->consumer = consumer;
1807 		writel(consumer, sds_ring->crb_sts_consumer);
1808 	}
1809 	return count;
1810 }
1811 
1812 static int qlcnic_83xx_msix_sriov_vf_poll(struct napi_struct *napi, int budget)
1813 {
1814 	int tx_complete;
1815 	int work_done;
1816 	struct qlcnic_host_sds_ring *sds_ring;
1817 	struct qlcnic_adapter *adapter;
1818 	struct qlcnic_host_tx_ring *tx_ring;
1819 
1820 	sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1821 	adapter = sds_ring->adapter;
1822 	/* tx ring count = 1 */
1823 	tx_ring = adapter->tx_ring;
1824 
1825 	tx_complete = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
1826 	work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
1827 	if ((work_done < budget) && tx_complete) {
1828 		napi_complete(&sds_ring->napi);
1829 		qlcnic_83xx_enable_intr(adapter, sds_ring);
1830 	}
1831 
1832 	return work_done;
1833 }
1834 
1835 static int qlcnic_83xx_poll(struct napi_struct *napi, int budget)
1836 {
1837 	int tx_complete;
1838 	int work_done;
1839 	struct qlcnic_host_sds_ring *sds_ring;
1840 	struct qlcnic_adapter *adapter;
1841 	struct qlcnic_host_tx_ring *tx_ring;
1842 
1843 	sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1844 	adapter = sds_ring->adapter;
1845 	/* tx ring count = 1 */
1846 	tx_ring = adapter->tx_ring;
1847 
1848 	tx_complete = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
1849 	work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
1850 	if ((work_done < budget) && tx_complete) {
1851 		napi_complete(&sds_ring->napi);
1852 		qlcnic_83xx_enable_intr(adapter, sds_ring);
1853 	}
1854 
1855 	return work_done;
1856 }
1857 
1858 static int qlcnic_83xx_msix_tx_poll(struct napi_struct *napi, int budget)
1859 {
1860 	int work_done;
1861 	struct qlcnic_host_tx_ring *tx_ring;
1862 	struct qlcnic_adapter *adapter;
1863 
1864 	budget = QLCNIC_TX_POLL_BUDGET;
1865 	tx_ring = container_of(napi, struct qlcnic_host_tx_ring, napi);
1866 	adapter = tx_ring->adapter;
1867 	work_done = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
1868 	if (work_done) {
1869 		napi_complete(&tx_ring->napi);
1870 		if (test_bit(__QLCNIC_DEV_UP , &adapter->state))
1871 			qlcnic_83xx_enable_tx_intr(adapter, tx_ring);
1872 	}
1873 
1874 	return work_done;
1875 }
1876 
1877 static int qlcnic_83xx_rx_poll(struct napi_struct *napi, int budget)
1878 {
1879 	int work_done;
1880 	struct qlcnic_host_sds_ring *sds_ring;
1881 	struct qlcnic_adapter *adapter;
1882 
1883 	sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1884 	adapter = sds_ring->adapter;
1885 	work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
1886 	if (work_done < budget) {
1887 		napi_complete(&sds_ring->napi);
1888 		if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1889 			qlcnic_83xx_enable_intr(adapter, sds_ring);
1890 	}
1891 
1892 	return work_done;
1893 }
1894 
1895 void qlcnic_83xx_napi_enable(struct qlcnic_adapter *adapter)
1896 {
1897 	int ring;
1898 	struct qlcnic_host_sds_ring *sds_ring;
1899 	struct qlcnic_host_tx_ring *tx_ring;
1900 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1901 
1902 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1903 		return;
1904 
1905 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1906 		sds_ring = &recv_ctx->sds_rings[ring];
1907 		napi_enable(&sds_ring->napi);
1908 		if (adapter->flags & QLCNIC_MSIX_ENABLED)
1909 			qlcnic_83xx_enable_intr(adapter, sds_ring);
1910 	}
1911 
1912 	if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
1913 	    !(adapter->flags & QLCNIC_TX_INTR_SHARED)) {
1914 		for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1915 			tx_ring = &adapter->tx_ring[ring];
1916 			napi_enable(&tx_ring->napi);
1917 			qlcnic_83xx_enable_tx_intr(adapter, tx_ring);
1918 		}
1919 	}
1920 }
1921 
1922 void qlcnic_83xx_napi_disable(struct qlcnic_adapter *adapter)
1923 {
1924 	int ring;
1925 	struct qlcnic_host_sds_ring *sds_ring;
1926 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1927 	struct qlcnic_host_tx_ring *tx_ring;
1928 
1929 	if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1930 		return;
1931 
1932 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1933 		sds_ring = &recv_ctx->sds_rings[ring];
1934 		if (adapter->flags & QLCNIC_MSIX_ENABLED)
1935 			qlcnic_83xx_disable_intr(adapter, sds_ring);
1936 		napi_synchronize(&sds_ring->napi);
1937 		napi_disable(&sds_ring->napi);
1938 	}
1939 
1940 	if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
1941 	    !(adapter->flags & QLCNIC_TX_INTR_SHARED)) {
1942 		for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1943 			tx_ring = &adapter->tx_ring[ring];
1944 			qlcnic_83xx_disable_tx_intr(adapter, tx_ring);
1945 			napi_synchronize(&tx_ring->napi);
1946 			napi_disable(&tx_ring->napi);
1947 		}
1948 	}
1949 }
1950 
1951 int qlcnic_83xx_napi_add(struct qlcnic_adapter *adapter,
1952 			 struct net_device *netdev)
1953 {
1954 	int ring;
1955 	struct qlcnic_host_sds_ring *sds_ring;
1956 	struct qlcnic_host_tx_ring *tx_ring;
1957 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1958 
1959 	if (qlcnic_alloc_sds_rings(recv_ctx, adapter->drv_sds_rings))
1960 		return -ENOMEM;
1961 
1962 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1963 		sds_ring = &recv_ctx->sds_rings[ring];
1964 		if (adapter->flags & QLCNIC_MSIX_ENABLED) {
1965 			if (!(adapter->flags & QLCNIC_TX_INTR_SHARED))
1966 				netif_napi_add(netdev, &sds_ring->napi,
1967 					       qlcnic_83xx_rx_poll,
1968 					       NAPI_POLL_WEIGHT);
1969 			else
1970 				netif_napi_add(netdev, &sds_ring->napi,
1971 					       qlcnic_83xx_msix_sriov_vf_poll,
1972 					       NAPI_POLL_WEIGHT);
1973 
1974 		} else {
1975 			netif_napi_add(netdev, &sds_ring->napi,
1976 				       qlcnic_83xx_poll,
1977 				       NAPI_POLL_WEIGHT);
1978 		}
1979 	}
1980 
1981 	if (qlcnic_alloc_tx_rings(adapter, netdev)) {
1982 		qlcnic_free_sds_rings(recv_ctx);
1983 		return -ENOMEM;
1984 	}
1985 
1986 	if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
1987 	    !(adapter->flags & QLCNIC_TX_INTR_SHARED)) {
1988 		for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1989 			tx_ring = &adapter->tx_ring[ring];
1990 			netif_napi_add(netdev, &tx_ring->napi,
1991 				       qlcnic_83xx_msix_tx_poll,
1992 				       NAPI_POLL_WEIGHT);
1993 		}
1994 	}
1995 
1996 	return 0;
1997 }
1998 
1999 void qlcnic_83xx_napi_del(struct qlcnic_adapter *adapter)
2000 {
2001 	int ring;
2002 	struct qlcnic_host_sds_ring *sds_ring;
2003 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2004 	struct qlcnic_host_tx_ring *tx_ring;
2005 
2006 	for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2007 		sds_ring = &recv_ctx->sds_rings[ring];
2008 		netif_napi_del(&sds_ring->napi);
2009 	}
2010 
2011 	qlcnic_free_sds_rings(adapter->recv_ctx);
2012 
2013 	if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
2014 	    !(adapter->flags & QLCNIC_TX_INTR_SHARED)) {
2015 		for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2016 			tx_ring = &adapter->tx_ring[ring];
2017 			netif_napi_del(&tx_ring->napi);
2018 		}
2019 	}
2020 
2021 	qlcnic_free_tx_rings(adapter);
2022 }
2023 
2024 void qlcnic_83xx_process_rcv_diag(struct qlcnic_adapter *adapter,
2025 				  int ring, u64 sts_data[])
2026 {
2027 	struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2028 	struct sk_buff *skb;
2029 	struct qlcnic_host_rds_ring *rds_ring;
2030 	int index, length;
2031 
2032 	if (unlikely(ring >= adapter->max_rds_rings))
2033 		return;
2034 
2035 	rds_ring = &recv_ctx->rds_rings[ring];
2036 	index = qlcnic_83xx_hndl(sts_data[0]);
2037 	if (unlikely(index >= rds_ring->num_desc))
2038 		return;
2039 
2040 	length = qlcnic_83xx_pktln(sts_data[0]);
2041 
2042 	skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
2043 	if (!skb)
2044 		return;
2045 
2046 	if (length > rds_ring->skb_size)
2047 		skb_put(skb, rds_ring->skb_size);
2048 	else
2049 		skb_put(skb, length);
2050 
2051 	if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
2052 		adapter->ahw->diag_cnt++;
2053 	else
2054 		dump_skb(skb, adapter);
2055 
2056 	dev_kfree_skb_any(skb);
2057 	return;
2058 }
2059 
2060 void qlcnic_83xx_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
2061 {
2062 	struct qlcnic_adapter *adapter = sds_ring->adapter;
2063 	struct status_desc *desc;
2064 	u64 sts_data[2];
2065 	int ring, opcode;
2066 	u32 consumer = sds_ring->consumer;
2067 
2068 	desc = &sds_ring->desc_head[consumer];
2069 	sts_data[0] = le64_to_cpu(desc->status_desc_data[0]);
2070 	sts_data[1] = le64_to_cpu(desc->status_desc_data[1]);
2071 	opcode = qlcnic_83xx_opcode(sts_data[1]);
2072 	if (!opcode)
2073 		return;
2074 
2075 	ring = QLCNIC_FETCH_RING_ID(qlcnic_83xx_hndl(sts_data[0]));
2076 	qlcnic_83xx_process_rcv_diag(adapter, ring, sts_data);
2077 	desc = &sds_ring->desc_head[consumer];
2078 	desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
2079 	consumer = get_next_index(consumer, sds_ring->num_desc);
2080 	sds_ring->consumer = consumer;
2081 	writel(consumer, sds_ring->crb_sts_consumer);
2082 }
2083