blob: bb4311e9aea95b03fae7ec14fe4f2d5339c79f82 [file] [log] [blame]
Jitendra Kalsaria577ae392013-02-04 12:33:07 +00001/*
2 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2013 QLogic Corporation
4 *
5 * See LICENSE.qlcnic for copyright and licensing details.
6 */
7
Sony Chackoc70001a2012-11-28 04:34:26 +00008#include <linux/netdevice.h>
9#include <linux/if_vlan.h>
10#include <net/ip.h>
11#include <linux/ipv6.h>
12
13#include "qlcnic.h"
14
Sony Chackoc70001a2012-11-28 04:34:26 +000015#define TX_ETHER_PKT 0x01
16#define TX_TCP_PKT 0x02
17#define TX_UDP_PKT 0x03
18#define TX_IP_PKT 0x04
19#define TX_TCP_LSO 0x05
20#define TX_TCP_LSO6 0x06
21#define TX_TCPV6_PKT 0x0b
22#define TX_UDPV6_PKT 0x0c
23#define FLAGS_VLAN_TAGGED 0x10
24#define FLAGS_VLAN_OOB 0x40
25
26#define qlcnic_set_tx_vlan_tci(cmd_desc, v) \
27 (cmd_desc)->vlan_TCI = cpu_to_le16(v);
28#define qlcnic_set_cmd_desc_port(cmd_desc, var) \
29 ((cmd_desc)->port_ctxid |= ((var) & 0x0F))
30#define qlcnic_set_cmd_desc_ctxid(cmd_desc, var) \
31 ((cmd_desc)->port_ctxid |= ((var) << 4 & 0xF0))
32
33#define qlcnic_set_tx_port(_desc, _port) \
34 ((_desc)->port_ctxid = ((_port) & 0xf) | (((_port) << 4) & 0xf0))
35
36#define qlcnic_set_tx_flags_opcode(_desc, _flags, _opcode) \
37 ((_desc)->flags_opcode |= \
38 cpu_to_le16(((_flags) & 0x7f) | (((_opcode) & 0x3f) << 7)))
39
40#define qlcnic_set_tx_frags_len(_desc, _frags, _len) \
41 ((_desc)->nfrags__length = \
42 cpu_to_le32(((_frags) & 0xff) | (((_len) & 0xffffff) << 8)))
43
44/* owner bits of status_desc */
45#define STATUS_OWNER_HOST (0x1ULL << 56)
46#define STATUS_OWNER_PHANTOM (0x2ULL << 56)
47
48/* Status descriptor:
49 0-3 port, 4-7 status, 8-11 type, 12-27 total_length
50 28-43 reference_handle, 44-47 protocol, 48-52 pkt_offset
51 53-55 desc_cnt, 56-57 owner, 58-63 opcode
52 */
53#define qlcnic_get_sts_port(sts_data) \
54 ((sts_data) & 0x0F)
55#define qlcnic_get_sts_status(sts_data) \
56 (((sts_data) >> 4) & 0x0F)
57#define qlcnic_get_sts_type(sts_data) \
58 (((sts_data) >> 8) & 0x0F)
59#define qlcnic_get_sts_totallength(sts_data) \
60 (((sts_data) >> 12) & 0xFFFF)
61#define qlcnic_get_sts_refhandle(sts_data) \
62 (((sts_data) >> 28) & 0xFFFF)
63#define qlcnic_get_sts_prot(sts_data) \
64 (((sts_data) >> 44) & 0x0F)
65#define qlcnic_get_sts_pkt_offset(sts_data) \
66 (((sts_data) >> 48) & 0x1F)
67#define qlcnic_get_sts_desc_cnt(sts_data) \
68 (((sts_data) >> 53) & 0x7)
69#define qlcnic_get_sts_opcode(sts_data) \
70 (((sts_data) >> 58) & 0x03F)
71
72#define qlcnic_get_lro_sts_refhandle(sts_data) \
Sony Chacko5796bd02012-12-04 03:33:57 +000073 ((sts_data) & 0x07FFF)
Sony Chackoc70001a2012-11-28 04:34:26 +000074#define qlcnic_get_lro_sts_length(sts_data) \
75 (((sts_data) >> 16) & 0x0FFFF)
76#define qlcnic_get_lro_sts_l2_hdr_offset(sts_data) \
77 (((sts_data) >> 32) & 0x0FF)
78#define qlcnic_get_lro_sts_l4_hdr_offset(sts_data) \
79 (((sts_data) >> 40) & 0x0FF)
80#define qlcnic_get_lro_sts_timestamp(sts_data) \
81 (((sts_data) >> 48) & 0x1)
82#define qlcnic_get_lro_sts_type(sts_data) \
83 (((sts_data) >> 49) & 0x7)
84#define qlcnic_get_lro_sts_push_flag(sts_data) \
85 (((sts_data) >> 52) & 0x1)
86#define qlcnic_get_lro_sts_seq_number(sts_data) \
87 ((sts_data) & 0x0FFFFFFFF)
88#define qlcnic_get_lro_sts_mss(sts_data1) \
89 ((sts_data1 >> 32) & 0x0FFFF)
90
91/* opcode field in status_desc */
92#define QLCNIC_SYN_OFFLOAD 0x03
93#define QLCNIC_RXPKT_DESC 0x04
94#define QLCNIC_OLD_RXPKT_DESC 0x3f
95#define QLCNIC_RESPONSE_DESC 0x05
96#define QLCNIC_LRO_DESC 0x12
97
Sony Chacko4be41e92013-01-01 03:20:20 +000098#define QLCNIC_TX_POLL_BUDGET 128
99#define QLCNIC_TCP_HDR_SIZE 20
100#define QLCNIC_TCP_TS_OPTION_SIZE 12
101#define QLCNIC_FETCH_RING_ID(handle) ((handle) >> 63)
102#define QLCNIC_DESC_OWNER_FW cpu_to_le64(STATUS_OWNER_PHANTOM)
103
104#define QLCNIC_TCP_TS_HDR_SIZE (QLCNIC_TCP_HDR_SIZE + QLCNIC_TCP_TS_OPTION_SIZE)
105
Sony Chackoc70001a2012-11-28 04:34:26 +0000106/* for status field in status_desc */
107#define STATUS_CKSUM_LOOP 0
108#define STATUS_CKSUM_OK 2
Sony Chackod17dd0d2012-11-28 04:34:27 +0000109
Sony Chacko4be41e92013-01-01 03:20:20 +0000110#define qlcnic_83xx_pktln(sts) ((sts >> 32) & 0x3FFF)
111#define qlcnic_83xx_hndl(sts) ((sts >> 48) & 0x7FFF)
112#define qlcnic_83xx_csum_status(sts) ((sts >> 39) & 7)
113#define qlcnic_83xx_opcode(sts) ((sts >> 42) & 0xF)
114#define qlcnic_83xx_vlan_tag(sts) (((sts) >> 48) & 0xFFFF)
115#define qlcnic_83xx_lro_pktln(sts) (((sts) >> 32) & 0x3FFF)
116#define qlcnic_83xx_l2_hdr_off(sts) (((sts) >> 16) & 0xFF)
117#define qlcnic_83xx_l4_hdr_off(sts) (((sts) >> 24) & 0xFF)
118#define qlcnic_83xx_pkt_cnt(sts) (((sts) >> 16) & 0x7)
119#define qlcnic_83xx_is_tstamp(sts) (((sts) >> 40) & 1)
120#define qlcnic_83xx_is_psh_bit(sts) (((sts) >> 41) & 1)
121#define qlcnic_83xx_is_ip_align(sts) (((sts) >> 46) & 1)
122#define qlcnic_83xx_has_vlan_tag(sts) (((sts) >> 47) & 1)
123
124struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *,
125 struct qlcnic_host_rds_ring *, u16, u16);
126
127inline void qlcnic_83xx_enable_tx_intr(struct qlcnic_adapter *adapter,
128 struct qlcnic_host_tx_ring *tx_ring)
129{
130 writel(0, tx_ring->crb_intr_mask);
131}
132
133inline void qlcnic_83xx_disable_tx_intr(struct qlcnic_adapter *adapter,
134 struct qlcnic_host_tx_ring *tx_ring)
135{
136 writel(1, tx_ring->crb_intr_mask);
137}
138
139static inline u8 qlcnic_mac_hash(u64 mac)
140{
141 return (u8)((mac & 0xff) ^ ((mac >> 40) & 0xff));
142}
143
144static inline u32 qlcnic_get_ref_handle(struct qlcnic_adapter *adapter,
145 u16 handle, u8 ring_id)
146{
147 if (adapter->pdev->device == PCI_DEVICE_ID_QLOGIC_QLE834X)
148 return handle | (ring_id << 15);
149 else
150 return handle;
151}
152
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000153void qlcnic_82xx_change_filter(struct qlcnic_adapter *adapter, u64 *uaddr,
154 __le16 vlan_id)
Sony Chackoc70001a2012-11-28 04:34:26 +0000155{
156 struct cmd_desc_type0 *hwdesc;
157 struct qlcnic_nic_req *req;
158 struct qlcnic_mac_req *mac_req;
159 struct qlcnic_vlan_req *vlan_req;
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000160 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
Sony Chackoc70001a2012-11-28 04:34:26 +0000161 u32 producer;
162 u64 word;
163
164 producer = tx_ring->producer;
165 hwdesc = &tx_ring->desc_head[tx_ring->producer];
166
167 req = (struct qlcnic_nic_req *)hwdesc;
168 memset(req, 0, sizeof(struct qlcnic_nic_req));
169 req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
170
171 word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
172 req->req_hdr = cpu_to_le64(word);
173
174 mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
175 mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
176 memcpy(mac_req->mac_addr, &uaddr, ETH_ALEN);
177
178 vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
179 vlan_req->vlan_id = vlan_id;
180
181 tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
182 smp_mb();
183}
184
Sony Chackod17dd0d2012-11-28 04:34:27 +0000185static void qlcnic_send_filter(struct qlcnic_adapter *adapter,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000186 struct cmd_desc_type0 *first_desc,
187 struct sk_buff *skb)
Sony Chackoc70001a2012-11-28 04:34:26 +0000188{
Sony Chackoc70001a2012-11-28 04:34:26 +0000189 struct qlcnic_filter *fil, *tmp_fil;
190 struct hlist_node *tmp_hnode, *n;
191 struct hlist_head *head;
Sony Chacko4be41e92013-01-01 03:20:20 +0000192 struct net_device *netdev = adapter->netdev;
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000193 struct ethhdr *phdr = (struct ethhdr *)(skb->data);
Sony Chackoc70001a2012-11-28 04:34:26 +0000194 u64 src_addr = 0;
195 __le16 vlan_id = 0;
196 u8 hindex;
197
Sony Chacko4be41e92013-01-01 03:20:20 +0000198 if (ether_addr_equal(phdr->h_source, adapter->mac_addr))
Sony Chackoc70001a2012-11-28 04:34:26 +0000199 return;
200
Sony Chacko4be41e92013-01-01 03:20:20 +0000201 if (adapter->fhash.fnum >= adapter->fhash.fmax) {
202 adapter->stats.mac_filter_limit_overrun++;
203 netdev_info(netdev, "Can not add more than %d mac addresses\n",
204 adapter->fhash.fmax);
Sony Chackoc70001a2012-11-28 04:34:26 +0000205 return;
Sony Chacko4be41e92013-01-01 03:20:20 +0000206 }
Sony Chackoc70001a2012-11-28 04:34:26 +0000207
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000208 /* Only NPAR capable devices support vlan based learning */
Sony Chackoc70001a2012-11-28 04:34:26 +0000209 if (adapter->flags & QLCNIC_ESWITCH_ENABLED)
210 vlan_id = first_desc->vlan_TCI;
211 memcpy(&src_addr, phdr->h_source, ETH_ALEN);
Sony Chacko4be41e92013-01-01 03:20:20 +0000212 hindex = qlcnic_mac_hash(src_addr) & (adapter->fhash.fbucket_size - 1);
Sony Chackoc70001a2012-11-28 04:34:26 +0000213 head = &(adapter->fhash.fhead[hindex]);
214
215 hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
216 if (!memcmp(tmp_fil->faddr, &src_addr, ETH_ALEN) &&
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000217 tmp_fil->vlan_id == vlan_id) {
Sony Chackod17dd0d2012-11-28 04:34:27 +0000218 if (jiffies > (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000219 qlcnic_change_filter(adapter, &src_addr,
220 vlan_id);
Sony Chackoc70001a2012-11-28 04:34:26 +0000221 tmp_fil->ftime = jiffies;
222 return;
223 }
224 }
225
226 fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
227 if (!fil)
228 return;
229
Sony Chacko7e2cf4f2013-01-01 03:20:17 +0000230 qlcnic_change_filter(adapter, &src_addr, vlan_id);
Sony Chackoc70001a2012-11-28 04:34:26 +0000231 fil->ftime = jiffies;
232 fil->vlan_id = vlan_id;
233 memcpy(fil->faddr, &src_addr, ETH_ALEN);
234 spin_lock(&adapter->mac_learn_lock);
235 hlist_add_head(&(fil->fnode), head);
236 adapter->fhash.fnum++;
237 spin_unlock(&adapter->mac_learn_lock);
238}
239
Sony Chackod17dd0d2012-11-28 04:34:27 +0000240static int qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
241 struct cmd_desc_type0 *first_desc, struct sk_buff *skb)
Sony Chackoc70001a2012-11-28 04:34:26 +0000242{
Sony Chackod17dd0d2012-11-28 04:34:27 +0000243 u8 l4proto, opcode = 0, hdr_len = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000244 u16 flags = 0, vlan_tci = 0;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000245 int copied, offset, copy_len, size;
Sony Chackoc70001a2012-11-28 04:34:26 +0000246 struct cmd_desc_type0 *hwdesc;
247 struct vlan_ethhdr *vh;
248 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
249 u16 protocol = ntohs(skb->protocol);
250 u32 producer = tx_ring->producer;
251
252 if (protocol == ETH_P_8021Q) {
253 vh = (struct vlan_ethhdr *)skb->data;
254 flags = FLAGS_VLAN_TAGGED;
255 vlan_tci = ntohs(vh->h_vlan_TCI);
256 protocol = ntohs(vh->h_vlan_encapsulated_proto);
257 } else if (vlan_tx_tag_present(skb)) {
258 flags = FLAGS_VLAN_OOB;
259 vlan_tci = vlan_tx_tag_get(skb);
260 }
261 if (unlikely(adapter->pvid)) {
262 if (vlan_tci && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
263 return -EIO;
264 if (vlan_tci && (adapter->flags & QLCNIC_TAGGING_ENABLED))
265 goto set_flags;
266
267 flags = FLAGS_VLAN_OOB;
268 vlan_tci = adapter->pvid;
269 }
270set_flags:
271 qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
272 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
273
274 if (*(skb->data) & BIT_0) {
275 flags |= BIT_0;
276 memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
277 }
278 opcode = TX_ETHER_PKT;
279 if ((adapter->netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
Sony Chackod17dd0d2012-11-28 04:34:27 +0000280 skb_shinfo(skb)->gso_size > 0) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000281 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Sony Chackoc70001a2012-11-28 04:34:26 +0000282 first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
283 first_desc->total_hdr_length = hdr_len;
Sony Chackoc70001a2012-11-28 04:34:26 +0000284 opcode = (protocol == ETH_P_IPV6) ? TX_TCP_LSO6 : TX_TCP_LSO;
285
286 /* For LSO, we need to copy the MAC/IP/TCP headers into
287 * the descriptor ring */
288 copied = 0;
289 offset = 2;
290
291 if (flags & FLAGS_VLAN_OOB) {
292 first_desc->total_hdr_length += VLAN_HLEN;
293 first_desc->tcp_hdr_offset = VLAN_HLEN;
294 first_desc->ip_hdr_offset = VLAN_HLEN;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000295
Sony Chackoc70001a2012-11-28 04:34:26 +0000296 /* Only in case of TSO on vlan device */
297 flags |= FLAGS_VLAN_TAGGED;
298
299 /* Create a TSO vlan header template for firmware */
Sony Chackoc70001a2012-11-28 04:34:26 +0000300 hwdesc = &tx_ring->desc_head[producer];
301 tx_ring->cmd_buf_arr[producer].skb = NULL;
302
303 copy_len = min((int)sizeof(struct cmd_desc_type0) -
Sony Chackod17dd0d2012-11-28 04:34:27 +0000304 offset, hdr_len + VLAN_HLEN);
Sony Chackoc70001a2012-11-28 04:34:26 +0000305
306 vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
307 skb_copy_from_linear_data(skb, vh, 12);
308 vh->h_vlan_proto = htons(ETH_P_8021Q);
309 vh->h_vlan_TCI = htons(vlan_tci);
310
311 skb_copy_from_linear_data_offset(skb, 12,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000312 (char *)vh + 16,
313 copy_len - 16);
Sony Chackoc70001a2012-11-28 04:34:26 +0000314 copied = copy_len - VLAN_HLEN;
315 offset = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000316 producer = get_next_index(producer, tx_ring->num_desc);
317 }
318
319 while (copied < hdr_len) {
Sony Chackod17dd0d2012-11-28 04:34:27 +0000320 size = (int)sizeof(struct cmd_desc_type0) - offset;
321 copy_len = min(size, (hdr_len - copied));
Sony Chackoc70001a2012-11-28 04:34:26 +0000322 hwdesc = &tx_ring->desc_head[producer];
323 tx_ring->cmd_buf_arr[producer].skb = NULL;
Sony Chackoc70001a2012-11-28 04:34:26 +0000324 skb_copy_from_linear_data_offset(skb, copied,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000325 (char *)hwdesc +
326 offset, copy_len);
Sony Chackoc70001a2012-11-28 04:34:26 +0000327 copied += copy_len;
328 offset = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000329 producer = get_next_index(producer, tx_ring->num_desc);
330 }
331
332 tx_ring->producer = producer;
333 smp_mb();
334 adapter->stats.lso_frames++;
335
336 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000337 if (protocol == ETH_P_IP) {
338 l4proto = ip_hdr(skb)->protocol;
339
340 if (l4proto == IPPROTO_TCP)
341 opcode = TX_TCP_PKT;
342 else if (l4proto == IPPROTO_UDP)
343 opcode = TX_UDP_PKT;
344 } else if (protocol == ETH_P_IPV6) {
345 l4proto = ipv6_hdr(skb)->nexthdr;
346
347 if (l4proto == IPPROTO_TCP)
348 opcode = TX_TCPV6_PKT;
349 else if (l4proto == IPPROTO_UDP)
350 opcode = TX_UDPV6_PKT;
351 }
352 }
353 first_desc->tcp_hdr_offset += skb_transport_offset(skb);
354 first_desc->ip_hdr_offset += skb_network_offset(skb);
355 qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
356
357 return 0;
358}
359
Sony Chackod17dd0d2012-11-28 04:34:27 +0000360static int qlcnic_map_tx_skb(struct pci_dev *pdev, struct sk_buff *skb,
361 struct qlcnic_cmd_buffer *pbuf)
Sony Chackoc70001a2012-11-28 04:34:26 +0000362{
363 struct qlcnic_skb_frag *nf;
364 struct skb_frag_struct *frag;
365 int i, nr_frags;
366 dma_addr_t map;
367
368 nr_frags = skb_shinfo(skb)->nr_frags;
369 nf = &pbuf->frag_array[0];
370
Sony Chackod17dd0d2012-11-28 04:34:27 +0000371 map = pci_map_single(pdev, skb->data, skb_headlen(skb),
372 PCI_DMA_TODEVICE);
Sony Chackoc70001a2012-11-28 04:34:26 +0000373 if (pci_dma_mapping_error(pdev, map))
374 goto out_err;
375
376 nf->dma = map;
377 nf->length = skb_headlen(skb);
378
379 for (i = 0; i < nr_frags; i++) {
380 frag = &skb_shinfo(skb)->frags[i];
381 nf = &pbuf->frag_array[i+1];
Sony Chackoc70001a2012-11-28 04:34:26 +0000382 map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
383 DMA_TO_DEVICE);
384 if (dma_mapping_error(&pdev->dev, map))
385 goto unwind;
386
387 nf->dma = map;
388 nf->length = skb_frag_size(frag);
389 }
390
391 return 0;
392
393unwind:
394 while (--i >= 0) {
395 nf = &pbuf->frag_array[i+1];
396 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
397 }
398
399 nf = &pbuf->frag_array[0];
400 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
401
402out_err:
403 return -ENOMEM;
404}
405
Sony Chackod17dd0d2012-11-28 04:34:27 +0000406static void qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
407 struct qlcnic_cmd_buffer *pbuf)
Sony Chackoc70001a2012-11-28 04:34:26 +0000408{
409 struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
Sony Chackod17dd0d2012-11-28 04:34:27 +0000410 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Sony Chackoc70001a2012-11-28 04:34:26 +0000411
412 for (i = 0; i < nr_frags; i++) {
413 nf = &pbuf->frag_array[i+1];
414 pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
415 }
416
417 nf = &pbuf->frag_array[0];
418 pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
419 pbuf->skb = NULL;
420}
421
Sony Chackod17dd0d2012-11-28 04:34:27 +0000422static inline void qlcnic_clear_cmddesc(u64 *desc)
Sony Chackoc70001a2012-11-28 04:34:26 +0000423{
424 desc[0] = 0ULL;
425 desc[2] = 0ULL;
426 desc[7] = 0ULL;
427}
428
Sony Chackod17dd0d2012-11-28 04:34:27 +0000429netdev_tx_t qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
Sony Chackoc70001a2012-11-28 04:34:26 +0000430{
431 struct qlcnic_adapter *adapter = netdev_priv(netdev);
432 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
433 struct qlcnic_cmd_buffer *pbuf;
434 struct qlcnic_skb_frag *buffrag;
435 struct cmd_desc_type0 *hwdesc, *first_desc;
436 struct pci_dev *pdev;
437 struct ethhdr *phdr;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000438 int i, k, frag_count, delta = 0;
439 u32 producer, num_txd;
Sony Chackoc70001a2012-11-28 04:34:26 +0000440
Sony Chackod17dd0d2012-11-28 04:34:27 +0000441 num_txd = tx_ring->num_desc;
Sony Chackoc70001a2012-11-28 04:34:26 +0000442
443 if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
444 netif_stop_queue(netdev);
445 return NETDEV_TX_BUSY;
446 }
447
448 if (adapter->flags & QLCNIC_MACSPOOF) {
449 phdr = (struct ethhdr *)skb->data;
450 if (!ether_addr_equal(phdr->h_source, adapter->mac_addr))
451 goto drop_packet;
452 }
453
454 frag_count = skb_shinfo(skb)->nr_frags + 1;
455 /* 14 frags supported for normal packet and
456 * 32 frags supported for TSO packet
457 */
458 if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000459 for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
460 delta += skb_frag_size(&skb_shinfo(skb)->frags[i]);
461
462 if (!__pskb_pull_tail(skb, delta))
463 goto drop_packet;
464
465 frag_count = 1 + skb_shinfo(skb)->nr_frags;
466 }
467
468 if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
469 netif_stop_queue(netdev);
Sony Chackod17dd0d2012-11-28 04:34:27 +0000470 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000471 netif_start_queue(netdev);
Sony Chackod17dd0d2012-11-28 04:34:27 +0000472 } else {
Sony Chackoc70001a2012-11-28 04:34:26 +0000473 adapter->stats.xmit_off++;
474 return NETDEV_TX_BUSY;
475 }
476 }
477
478 producer = tx_ring->producer;
479 pbuf = &tx_ring->cmd_buf_arr[producer];
Sony Chackoc70001a2012-11-28 04:34:26 +0000480 pdev = adapter->pdev;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000481 first_desc = &tx_ring->desc_head[producer];
482 hwdesc = &tx_ring->desc_head[producer];
Sony Chackoc70001a2012-11-28 04:34:26 +0000483 qlcnic_clear_cmddesc((u64 *)hwdesc);
484
485 if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
486 adapter->stats.tx_dma_map_error++;
487 goto drop_packet;
488 }
489
490 pbuf->skb = skb;
491 pbuf->frag_count = frag_count;
492
493 qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
494 qlcnic_set_tx_port(first_desc, adapter->portnum);
495
496 for (i = 0; i < frag_count; i++) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000497 k = i % 4;
498
499 if ((k == 0) && (i > 0)) {
500 /* move to next desc.*/
501 producer = get_next_index(producer, num_txd);
502 hwdesc = &tx_ring->desc_head[producer];
503 qlcnic_clear_cmddesc((u64 *)hwdesc);
504 tx_ring->cmd_buf_arr[producer].skb = NULL;
505 }
506
507 buffrag = &pbuf->frag_array[i];
Sony Chackoc70001a2012-11-28 04:34:26 +0000508 hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
509 switch (k) {
510 case 0:
511 hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
512 break;
513 case 1:
514 hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
515 break;
516 case 2:
517 hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
518 break;
519 case 3:
520 hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
521 break;
522 }
523 }
524
525 tx_ring->producer = get_next_index(producer, num_txd);
526 smp_mb();
527
528 if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb)))
529 goto unwind_buff;
530
Jitendra Kalsariafe1adc62013-01-25 10:20:40 +0000531 if (adapter->drv_mac_learn)
Sony Chacko4be41e92013-01-01 03:20:20 +0000532 qlcnic_send_filter(adapter, first_desc, skb);
Sony Chackoc70001a2012-11-28 04:34:26 +0000533
534 adapter->stats.txbytes += skb->len;
535 adapter->stats.xmitcalled++;
536
537 qlcnic_update_cmd_producer(tx_ring);
538
539 return NETDEV_TX_OK;
540
541unwind_buff:
542 qlcnic_unmap_buffers(pdev, skb, pbuf);
543drop_packet:
544 adapter->stats.txdropped++;
545 dev_kfree_skb_any(skb);
546 return NETDEV_TX_OK;
547}
548
Sony Chacko7f9664522013-01-01 03:20:19 +0000549void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
Sony Chackoc70001a2012-11-28 04:34:26 +0000550{
551 struct net_device *netdev = adapter->netdev;
552
553 if (adapter->ahw->linkup && !linkup) {
554 netdev_info(netdev, "NIC Link is down\n");
555 adapter->ahw->linkup = 0;
556 if (netif_running(netdev)) {
557 netif_carrier_off(netdev);
558 netif_stop_queue(netdev);
559 }
560 } else if (!adapter->ahw->linkup && linkup) {
561 netdev_info(netdev, "NIC Link is up\n");
562 adapter->ahw->linkup = 1;
563 if (netif_running(netdev)) {
564 netif_carrier_on(netdev);
565 netif_wake_queue(netdev);
566 }
567 }
568}
569
Sony Chackod17dd0d2012-11-28 04:34:27 +0000570static int qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
571 struct qlcnic_host_rds_ring *rds_ring,
572 struct qlcnic_rx_buffer *buffer)
Sony Chackoc70001a2012-11-28 04:34:26 +0000573{
574 struct sk_buff *skb;
575 dma_addr_t dma;
576 struct pci_dev *pdev = adapter->pdev;
577
578 skb = netdev_alloc_skb(adapter->netdev, rds_ring->skb_size);
579 if (!skb) {
580 adapter->stats.skb_alloc_failure++;
581 return -ENOMEM;
582 }
583
584 skb_reserve(skb, NET_IP_ALIGN);
Sony Chacko4be41e92013-01-01 03:20:20 +0000585 dma = pci_map_single(pdev, skb->data,
586 rds_ring->dma_size, PCI_DMA_FROMDEVICE);
Sony Chackoc70001a2012-11-28 04:34:26 +0000587
588 if (pci_dma_mapping_error(pdev, dma)) {
589 adapter->stats.rx_dma_map_error++;
590 dev_kfree_skb_any(skb);
591 return -ENOMEM;
592 }
593
594 buffer->skb = skb;
595 buffer->dma = dma;
596
597 return 0;
598}
599
600static void qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
Sony Chacko4be41e92013-01-01 03:20:20 +0000601 struct qlcnic_host_rds_ring *rds_ring,
602 u8 ring_id)
Sony Chackoc70001a2012-11-28 04:34:26 +0000603{
604 struct rcv_desc *pdesc;
605 struct qlcnic_rx_buffer *buffer;
606 int count = 0;
Sony Chacko4be41e92013-01-01 03:20:20 +0000607 uint32_t producer, handle;
Sony Chackoc70001a2012-11-28 04:34:26 +0000608 struct list_head *head;
609
610 if (!spin_trylock(&rds_ring->lock))
611 return;
612
613 producer = rds_ring->producer;
Sony Chackoc70001a2012-11-28 04:34:26 +0000614 head = &rds_ring->free_list;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000615 while (!list_empty(head)) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000616 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
617
618 if (!buffer->skb) {
619 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
620 break;
621 }
Sony Chackoc70001a2012-11-28 04:34:26 +0000622 count++;
623 list_del(&buffer->list);
624
625 /* make a rcv descriptor */
626 pdesc = &rds_ring->desc_head[producer];
Sony Chacko4be41e92013-01-01 03:20:20 +0000627 handle = qlcnic_get_ref_handle(adapter,
628 buffer->ref_handle, ring_id);
629 pdesc->reference_handle = cpu_to_le16(handle);
Sony Chackoc70001a2012-11-28 04:34:26 +0000630 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
631 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Sony Chackoc70001a2012-11-28 04:34:26 +0000632 producer = get_next_index(producer, rds_ring->num_desc);
633 }
Sony Chackoc70001a2012-11-28 04:34:26 +0000634 if (count) {
635 rds_ring->producer = producer;
636 writel((producer - 1) & (rds_ring->num_desc - 1),
Sony Chackod17dd0d2012-11-28 04:34:27 +0000637 rds_ring->crb_rcv_producer);
Sony Chackoc70001a2012-11-28 04:34:26 +0000638 }
639 spin_unlock(&rds_ring->lock);
640}
641
Sony Chacko4be41e92013-01-01 03:20:20 +0000642static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter,
643 struct qlcnic_host_tx_ring *tx_ring,
644 int budget)
Sony Chackoc70001a2012-11-28 04:34:26 +0000645{
646 u32 sw_consumer, hw_consumer;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000647 int i, done, count = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000648 struct qlcnic_cmd_buffer *buffer;
649 struct pci_dev *pdev = adapter->pdev;
650 struct net_device *netdev = adapter->netdev;
651 struct qlcnic_skb_frag *frag;
Sony Chackoc70001a2012-11-28 04:34:26 +0000652
653 if (!spin_trylock(&adapter->tx_clean_lock))
654 return 1;
655
656 sw_consumer = tx_ring->sw_consumer;
657 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
658
659 while (sw_consumer != hw_consumer) {
660 buffer = &tx_ring->cmd_buf_arr[sw_consumer];
661 if (buffer->skb) {
662 frag = &buffer->frag_array[0];
663 pci_unmap_single(pdev, frag->dma, frag->length,
664 PCI_DMA_TODEVICE);
665 frag->dma = 0ULL;
666 for (i = 1; i < buffer->frag_count; i++) {
667 frag++;
668 pci_unmap_page(pdev, frag->dma, frag->length,
669 PCI_DMA_TODEVICE);
670 frag->dma = 0ULL;
671 }
Sony Chackoc70001a2012-11-28 04:34:26 +0000672 adapter->stats.xmitfinished++;
673 dev_kfree_skb_any(buffer->skb);
674 buffer->skb = NULL;
675 }
676
677 sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
Sony Chacko4be41e92013-01-01 03:20:20 +0000678 if (++count >= budget)
Sony Chackoc70001a2012-11-28 04:34:26 +0000679 break;
680 }
681
682 if (count && netif_running(netdev)) {
683 tx_ring->sw_consumer = sw_consumer;
Sony Chackoc70001a2012-11-28 04:34:26 +0000684 smp_mb();
Sony Chackoc70001a2012-11-28 04:34:26 +0000685 if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
686 if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
687 netif_wake_queue(netdev);
688 adapter->stats.xmit_on++;
689 }
690 }
691 adapter->tx_timeo_cnt = 0;
692 }
693 /*
694 * If everything is freed up to consumer then check if the ring is full
695 * If the ring is full then check if more needs to be freed and
696 * schedule the call back again.
697 *
698 * This happens when there are 2 CPUs. One could be freeing and the
699 * other filling it. If the ring is full when we get out of here and
700 * the card has already interrupted the host then the host can miss the
701 * interrupt.
702 *
703 * There is still a possible race condition and the host could miss an
704 * interrupt. The card has to take care of this.
705 */
706 hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
707 done = (sw_consumer == hw_consumer);
708 spin_unlock(&adapter->tx_clean_lock);
709
710 return done;
711}
712
713static int qlcnic_poll(struct napi_struct *napi, int budget)
714{
Sony Chacko4be41e92013-01-01 03:20:20 +0000715 int tx_complete, work_done;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000716 struct qlcnic_host_sds_ring *sds_ring;
717 struct qlcnic_adapter *adapter;
Sony Chackoc70001a2012-11-28 04:34:26 +0000718
Sony Chackod17dd0d2012-11-28 04:34:27 +0000719 sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
720 adapter = sds_ring->adapter;
Sony Chacko4be41e92013-01-01 03:20:20 +0000721 tx_complete = qlcnic_process_cmd_ring(adapter, adapter->tx_ring,
722 budget);
Sony Chackoc70001a2012-11-28 04:34:26 +0000723 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
Sony Chackoc70001a2012-11-28 04:34:26 +0000724 if ((work_done < budget) && tx_complete) {
725 napi_complete(&sds_ring->napi);
726 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
727 qlcnic_enable_int(sds_ring);
728 }
729
730 return work_done;
731}
732
733static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
734{
Sony Chackod17dd0d2012-11-28 04:34:27 +0000735 struct qlcnic_host_sds_ring *sds_ring;
736 struct qlcnic_adapter *adapter;
Sony Chackoc70001a2012-11-28 04:34:26 +0000737 int work_done;
738
Sony Chackod17dd0d2012-11-28 04:34:27 +0000739 sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
740 adapter = sds_ring->adapter;
741
Sony Chackoc70001a2012-11-28 04:34:26 +0000742 work_done = qlcnic_process_rcv_ring(sds_ring, budget);
743
744 if (work_done < budget) {
745 napi_complete(&sds_ring->napi);
746 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
747 qlcnic_enable_int(sds_ring);
748 }
749
750 return work_done;
751}
752
Sony Chackod17dd0d2012-11-28 04:34:27 +0000753static void qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
754 struct qlcnic_fw_msg *msg)
Sony Chackoc70001a2012-11-28 04:34:26 +0000755{
756 u32 cable_OUI;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000757 u16 cable_len, link_speed;
758 u8 link_status, module, duplex, autoneg, lb_status = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +0000759 struct net_device *netdev = adapter->netdev;
760
Sony Chacko79788452012-12-04 03:33:53 +0000761 adapter->ahw->has_link_events = 1;
Sony Chackoc70001a2012-11-28 04:34:26 +0000762
763 cable_OUI = msg->body[1] & 0xffffffff;
764 cable_len = (msg->body[1] >> 32) & 0xffff;
765 link_speed = (msg->body[1] >> 48) & 0xffff;
766
767 link_status = msg->body[2] & 0xff;
768 duplex = (msg->body[2] >> 16) & 0xff;
769 autoneg = (msg->body[2] >> 24) & 0xff;
770 lb_status = (msg->body[2] >> 32) & 0x3;
771
772 module = (msg->body[2] >> 8) & 0xff;
773 if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
Sony Chackod17dd0d2012-11-28 04:34:27 +0000774 dev_info(&netdev->dev,
775 "unsupported cable: OUI 0x%x, length %d\n",
776 cable_OUI, cable_len);
Sony Chackoc70001a2012-11-28 04:34:26 +0000777 else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
778 dev_info(&netdev->dev, "unsupported cable length %d\n",
Sony Chackod17dd0d2012-11-28 04:34:27 +0000779 cable_len);
Sony Chackoc70001a2012-11-28 04:34:26 +0000780
781 if (!link_status && (lb_status == QLCNIC_ILB_MODE ||
782 lb_status == QLCNIC_ELB_MODE))
783 adapter->ahw->loopback_state |= QLCNIC_LINKEVENT;
784
785 qlcnic_advert_link_change(adapter, link_status);
786
787 if (duplex == LINKEVENT_FULL_DUPLEX)
Sony Chacko79788452012-12-04 03:33:53 +0000788 adapter->ahw->link_duplex = DUPLEX_FULL;
Sony Chackoc70001a2012-11-28 04:34:26 +0000789 else
Sony Chacko79788452012-12-04 03:33:53 +0000790 adapter->ahw->link_duplex = DUPLEX_HALF;
Sony Chackoc70001a2012-11-28 04:34:26 +0000791
Sony Chacko79788452012-12-04 03:33:53 +0000792 adapter->ahw->module_type = module;
793 adapter->ahw->link_autoneg = autoneg;
Sony Chackoc70001a2012-11-28 04:34:26 +0000794
795 if (link_status) {
Sony Chacko79788452012-12-04 03:33:53 +0000796 adapter->ahw->link_speed = link_speed;
Sony Chackoc70001a2012-11-28 04:34:26 +0000797 } else {
Sony Chacko79788452012-12-04 03:33:53 +0000798 adapter->ahw->link_speed = SPEED_UNKNOWN;
799 adapter->ahw->link_duplex = DUPLEX_UNKNOWN;
Sony Chackoc70001a2012-11-28 04:34:26 +0000800 }
801}
802
Sony Chackod17dd0d2012-11-28 04:34:27 +0000803static void qlcnic_handle_fw_message(int desc_cnt, int index,
804 struct qlcnic_host_sds_ring *sds_ring)
Sony Chackoc70001a2012-11-28 04:34:26 +0000805{
806 struct qlcnic_fw_msg msg;
807 struct status_desc *desc;
808 struct qlcnic_adapter *adapter;
809 struct device *dev;
810 int i = 0, opcode, ret;
811
812 while (desc_cnt > 0 && i < 8) {
813 desc = &sds_ring->desc_head[index];
814 msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
815 msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
816
817 index = get_next_index(index, sds_ring->num_desc);
818 desc_cnt--;
819 }
820
821 adapter = sds_ring->adapter;
822 dev = &adapter->pdev->dev;
823 opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
824
825 switch (opcode) {
826 case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
827 qlcnic_handle_linkevent(adapter, &msg);
828 break;
829 case QLCNIC_C2H_OPCODE_CONFIG_LOOPBACK:
830 ret = (u32)(msg.body[1]);
831 switch (ret) {
832 case 0:
833 adapter->ahw->loopback_state |= QLCNIC_LB_RESPONSE;
834 break;
835 case 1:
836 dev_info(dev, "loopback already in progress\n");
Sony Chacko79788452012-12-04 03:33:53 +0000837 adapter->ahw->diag_cnt = -QLCNIC_TEST_IN_PROGRESS;
Sony Chackoc70001a2012-11-28 04:34:26 +0000838 break;
839 case 2:
840 dev_info(dev, "loopback cable is not connected\n");
Sony Chacko79788452012-12-04 03:33:53 +0000841 adapter->ahw->diag_cnt = -QLCNIC_LB_CABLE_NOT_CONN;
Sony Chackoc70001a2012-11-28 04:34:26 +0000842 break;
843 default:
Sony Chackod17dd0d2012-11-28 04:34:27 +0000844 dev_info(dev,
845 "loopback configure request failed, err %x\n",
846 ret);
Sony Chacko79788452012-12-04 03:33:53 +0000847 adapter->ahw->diag_cnt = -QLCNIC_UNDEFINED_ERROR;
Sony Chackoc70001a2012-11-28 04:34:26 +0000848 break;
849 }
850 break;
851 default:
852 break;
853 }
854}
855
Sony Chacko4be41e92013-01-01 03:20:20 +0000856struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
857 struct qlcnic_host_rds_ring *ring,
858 u16 index, u16 cksum)
Sony Chackoc70001a2012-11-28 04:34:26 +0000859{
860 struct qlcnic_rx_buffer *buffer;
861 struct sk_buff *skb;
862
Sony Chacko4be41e92013-01-01 03:20:20 +0000863 buffer = &ring->rx_buf_arr[index];
Sony Chackoc70001a2012-11-28 04:34:26 +0000864 if (unlikely(buffer->skb == NULL)) {
865 WARN_ON(1);
866 return NULL;
867 }
868
Sony Chacko4be41e92013-01-01 03:20:20 +0000869 pci_unmap_single(adapter->pdev, buffer->dma, ring->dma_size,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000870 PCI_DMA_FROMDEVICE);
Sony Chackoc70001a2012-11-28 04:34:26 +0000871
872 skb = buffer->skb;
Sony Chackoc70001a2012-11-28 04:34:26 +0000873 if (likely((adapter->netdev->features & NETIF_F_RXCSUM) &&
Sony Chackod17dd0d2012-11-28 04:34:27 +0000874 (cksum == STATUS_CKSUM_OK || cksum == STATUS_CKSUM_LOOP))) {
Sony Chackoc70001a2012-11-28 04:34:26 +0000875 adapter->stats.csummed++;
876 skb->ip_summed = CHECKSUM_UNNECESSARY;
877 } else {
878 skb_checksum_none_assert(skb);
879 }
880
Sony Chacko4be41e92013-01-01 03:20:20 +0000881
Sony Chackoc70001a2012-11-28 04:34:26 +0000882 buffer->skb = NULL;
883
884 return skb;
885}
886
Sony Chackod17dd0d2012-11-28 04:34:27 +0000887static inline int qlcnic_check_rx_tagging(struct qlcnic_adapter *adapter,
888 struct sk_buff *skb, u16 *vlan_tag)
Sony Chackoc70001a2012-11-28 04:34:26 +0000889{
890 struct ethhdr *eth_hdr;
891
892 if (!__vlan_get_tag(skb, vlan_tag)) {
Sony Chackod17dd0d2012-11-28 04:34:27 +0000893 eth_hdr = (struct ethhdr *)skb->data;
Sony Chackoc70001a2012-11-28 04:34:26 +0000894 memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
895 skb_pull(skb, VLAN_HLEN);
896 }
897 if (!adapter->pvid)
898 return 0;
899
900 if (*vlan_tag == adapter->pvid) {
901 /* Outer vlan tag. Packet should follow non-vlan path */
902 *vlan_tag = 0xffff;
903 return 0;
904 }
905 if (adapter->flags & QLCNIC_TAGGING_ENABLED)
906 return 0;
907
908 return -EINVAL;
909}
910
911static struct qlcnic_rx_buffer *
912qlcnic_process_rcv(struct qlcnic_adapter *adapter,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000913 struct qlcnic_host_sds_ring *sds_ring, int ring,
914 u64 sts_data0)
Sony Chackoc70001a2012-11-28 04:34:26 +0000915{
916 struct net_device *netdev = adapter->netdev;
917 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
918 struct qlcnic_rx_buffer *buffer;
919 struct sk_buff *skb;
920 struct qlcnic_host_rds_ring *rds_ring;
921 int index, length, cksum, pkt_offset;
922 u16 vid = 0xffff;
923
924 if (unlikely(ring >= adapter->max_rds_rings))
925 return NULL;
926
927 rds_ring = &recv_ctx->rds_rings[ring];
928
929 index = qlcnic_get_sts_refhandle(sts_data0);
930 if (unlikely(index >= rds_ring->num_desc))
931 return NULL;
932
933 buffer = &rds_ring->rx_buf_arr[index];
Sony Chackoc70001a2012-11-28 04:34:26 +0000934 length = qlcnic_get_sts_totallength(sts_data0);
935 cksum = qlcnic_get_sts_status(sts_data0);
936 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
937
938 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
939 if (!skb)
940 return buffer;
941
942 if (length > rds_ring->skb_size)
943 skb_put(skb, rds_ring->skb_size);
944 else
945 skb_put(skb, length);
946
947 if (pkt_offset)
948 skb_pull(skb, pkt_offset);
949
950 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
951 adapter->stats.rxdropped++;
952 dev_kfree_skb(skb);
953 return buffer;
954 }
955
956 skb->protocol = eth_type_trans(skb, netdev);
957
958 if (vid != 0xffff)
959 __vlan_hwaccel_put_tag(skb, vid);
960
961 napi_gro_receive(&sds_ring->napi, skb);
962
963 adapter->stats.rx_pkts++;
964 adapter->stats.rxbytes += length;
965
966 return buffer;
967}
968
969#define QLC_TCP_HDR_SIZE 20
970#define QLC_TCP_TS_OPTION_SIZE 12
971#define QLC_TCP_TS_HDR_SIZE (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
972
973static struct qlcnic_rx_buffer *
974qlcnic_process_lro(struct qlcnic_adapter *adapter,
Sony Chackod17dd0d2012-11-28 04:34:27 +0000975 int ring, u64 sts_data0, u64 sts_data1)
Sony Chackoc70001a2012-11-28 04:34:26 +0000976{
977 struct net_device *netdev = adapter->netdev;
978 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
979 struct qlcnic_rx_buffer *buffer;
980 struct sk_buff *skb;
981 struct qlcnic_host_rds_ring *rds_ring;
982 struct iphdr *iph;
Shahed Shaikh776e7bd2013-01-25 10:20:35 +0000983 struct ipv6hdr *ipv6h;
Sony Chackoc70001a2012-11-28 04:34:26 +0000984 struct tcphdr *th;
985 bool push, timestamp;
Sony Chackod17dd0d2012-11-28 04:34:27 +0000986 int index, l2_hdr_offset, l4_hdr_offset;
987 u16 lro_length, length, data_offset, vid = 0xffff;
Sony Chackoc70001a2012-11-28 04:34:26 +0000988 u32 seq_number;
Sony Chackoc70001a2012-11-28 04:34:26 +0000989
990 if (unlikely(ring > adapter->max_rds_rings))
991 return NULL;
992
993 rds_ring = &recv_ctx->rds_rings[ring];
994
995 index = qlcnic_get_lro_sts_refhandle(sts_data0);
996 if (unlikely(index > rds_ring->num_desc))
997 return NULL;
998
999 buffer = &rds_ring->rx_buf_arr[index];
1000
1001 timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1002 lro_length = qlcnic_get_lro_sts_length(sts_data0);
1003 l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1004 l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1005 push = qlcnic_get_lro_sts_push_flag(sts_data0);
1006 seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1007
1008 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1009 if (!skb)
1010 return buffer;
1011
1012 if (timestamp)
1013 data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1014 else
1015 data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1016
1017 skb_put(skb, lro_length + data_offset);
Sony Chackoc70001a2012-11-28 04:34:26 +00001018 skb_pull(skb, l2_hdr_offset);
1019
1020 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1021 adapter->stats.rxdropped++;
1022 dev_kfree_skb(skb);
1023 return buffer;
1024 }
1025
1026 skb->protocol = eth_type_trans(skb, netdev);
Shahed Shaikh776e7bd2013-01-25 10:20:35 +00001027
Shahed Shaikh069048f2013-01-30 12:47:12 +00001028 if (ntohs(skb->protocol) == ETH_P_IPV6) {
Shahed Shaikh776e7bd2013-01-25 10:20:35 +00001029 ipv6h = (struct ipv6hdr *)skb->data;
1030 th = (struct tcphdr *)(skb->data + sizeof(struct ipv6hdr));
1031 length = (th->doff << 2) + lro_length;
1032 ipv6h->payload_len = htons(length);
1033 } else {
1034 iph = (struct iphdr *)skb->data;
1035 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1036 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1037 iph->tot_len = htons(length);
1038 iph->check = 0;
1039 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1040 }
1041
Sony Chackoc70001a2012-11-28 04:34:26 +00001042 th->psh = push;
1043 th->seq = htonl(seq_number);
Sony Chackoc70001a2012-11-28 04:34:26 +00001044 length = skb->len;
1045
1046 if (adapter->flags & QLCNIC_FW_LRO_MSS_CAP)
1047 skb_shinfo(skb)->gso_size = qlcnic_get_lro_sts_mss(sts_data1);
1048
1049 if (vid != 0xffff)
1050 __vlan_hwaccel_put_tag(skb, vid);
1051 netif_receive_skb(skb);
1052
1053 adapter->stats.lro_pkts++;
1054 adapter->stats.lrobytes += length;
1055
1056 return buffer;
1057}
1058
Sony Chackod17dd0d2012-11-28 04:34:27 +00001059int qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
Sony Chackoc70001a2012-11-28 04:34:26 +00001060{
Sony Chackod17dd0d2012-11-28 04:34:27 +00001061 struct qlcnic_host_rds_ring *rds_ring;
Sony Chackoc70001a2012-11-28 04:34:26 +00001062 struct qlcnic_adapter *adapter = sds_ring->adapter;
1063 struct list_head *cur;
1064 struct status_desc *desc;
1065 struct qlcnic_rx_buffer *rxbuf;
Sony Chacko4be41e92013-01-01 03:20:20 +00001066 int opcode, desc_cnt, count = 0;
Sony Chackoc70001a2012-11-28 04:34:26 +00001067 u64 sts_data0, sts_data1;
Sony Chacko4be41e92013-01-01 03:20:20 +00001068 u8 ring;
Sony Chackoc70001a2012-11-28 04:34:26 +00001069 u32 consumer = sds_ring->consumer;
1070
1071 while (count < max) {
1072 desc = &sds_ring->desc_head[consumer];
1073 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1074
1075 if (!(sts_data0 & STATUS_OWNER_HOST))
1076 break;
1077
1078 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1079 opcode = qlcnic_get_sts_opcode(sts_data0);
Sony Chackoc70001a2012-11-28 04:34:26 +00001080 switch (opcode) {
1081 case QLCNIC_RXPKT_DESC:
1082 case QLCNIC_OLD_RXPKT_DESC:
1083 case QLCNIC_SYN_OFFLOAD:
1084 ring = qlcnic_get_sts_type(sts_data0);
Sony Chackod17dd0d2012-11-28 04:34:27 +00001085 rxbuf = qlcnic_process_rcv(adapter, sds_ring, ring,
1086 sts_data0);
Sony Chackoc70001a2012-11-28 04:34:26 +00001087 break;
1088 case QLCNIC_LRO_DESC:
1089 ring = qlcnic_get_lro_sts_type(sts_data0);
1090 sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1091 rxbuf = qlcnic_process_lro(adapter, ring, sts_data0,
1092 sts_data1);
1093 break;
1094 case QLCNIC_RESPONSE_DESC:
1095 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1096 default:
1097 goto skip;
1098 }
Sony Chackoc70001a2012-11-28 04:34:26 +00001099 WARN_ON(desc_cnt > 1);
1100
1101 if (likely(rxbuf))
1102 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1103 else
1104 adapter->stats.null_rxbuf++;
Sony Chackoc70001a2012-11-28 04:34:26 +00001105skip:
1106 for (; desc_cnt > 0; desc_cnt--) {
1107 desc = &sds_ring->desc_head[consumer];
Sony Chacko4be41e92013-01-01 03:20:20 +00001108 desc->status_desc_data[0] = QLCNIC_DESC_OWNER_FW;
Sony Chackoc70001a2012-11-28 04:34:26 +00001109 consumer = get_next_index(consumer, sds_ring->num_desc);
1110 }
1111 count++;
1112 }
1113
1114 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
Sony Chackod17dd0d2012-11-28 04:34:27 +00001115 rds_ring = &adapter->recv_ctx->rds_rings[ring];
Sony Chackoc70001a2012-11-28 04:34:26 +00001116 if (!list_empty(&sds_ring->free_list[ring])) {
1117 list_for_each(cur, &sds_ring->free_list[ring]) {
Sony Chackod17dd0d2012-11-28 04:34:27 +00001118 rxbuf = list_entry(cur, struct qlcnic_rx_buffer,
1119 list);
Sony Chackoc70001a2012-11-28 04:34:26 +00001120 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1121 }
1122 spin_lock(&rds_ring->lock);
1123 list_splice_tail_init(&sds_ring->free_list[ring],
Sony Chackod17dd0d2012-11-28 04:34:27 +00001124 &rds_ring->free_list);
Sony Chackoc70001a2012-11-28 04:34:26 +00001125 spin_unlock(&rds_ring->lock);
1126 }
1127
Sony Chacko4be41e92013-01-01 03:20:20 +00001128 qlcnic_post_rx_buffers_nodb(adapter, rds_ring, ring);
Sony Chackoc70001a2012-11-28 04:34:26 +00001129 }
1130
1131 if (count) {
1132 sds_ring->consumer = consumer;
1133 writel(consumer, sds_ring->crb_sts_consumer);
1134 }
1135
1136 return count;
1137}
1138
Sony Chackod17dd0d2012-11-28 04:34:27 +00001139void qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter,
Sony Chacko4be41e92013-01-01 03:20:20 +00001140 struct qlcnic_host_rds_ring *rds_ring, u8 ring_id)
Sony Chackoc70001a2012-11-28 04:34:26 +00001141{
1142 struct rcv_desc *pdesc;
1143 struct qlcnic_rx_buffer *buffer;
1144 int count = 0;
Sony Chacko4be41e92013-01-01 03:20:20 +00001145 u32 producer, handle;
Sony Chackoc70001a2012-11-28 04:34:26 +00001146 struct list_head *head;
1147
1148 producer = rds_ring->producer;
Sony Chackoc70001a2012-11-28 04:34:26 +00001149 head = &rds_ring->free_list;
Sony Chackod17dd0d2012-11-28 04:34:27 +00001150
Sony Chackoc70001a2012-11-28 04:34:26 +00001151 while (!list_empty(head)) {
1152
1153 buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1154
1155 if (!buffer->skb) {
1156 if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1157 break;
1158 }
1159
1160 count++;
1161 list_del(&buffer->list);
1162
1163 /* make a rcv descriptor */
1164 pdesc = &rds_ring->desc_head[producer];
1165 pdesc->addr_buffer = cpu_to_le64(buffer->dma);
Sony Chacko4be41e92013-01-01 03:20:20 +00001166 handle = qlcnic_get_ref_handle(adapter, buffer->ref_handle,
1167 ring_id);
1168 pdesc->reference_handle = cpu_to_le16(handle);
Sony Chackoc70001a2012-11-28 04:34:26 +00001169 pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
Sony Chackoc70001a2012-11-28 04:34:26 +00001170 producer = get_next_index(producer, rds_ring->num_desc);
1171 }
1172
1173 if (count) {
1174 rds_ring->producer = producer;
1175 writel((producer-1) & (rds_ring->num_desc-1),
Sony Chackod17dd0d2012-11-28 04:34:27 +00001176 rds_ring->crb_rcv_producer);
Sony Chackoc70001a2012-11-28 04:34:26 +00001177 }
1178}
1179
1180static void dump_skb(struct sk_buff *skb, struct qlcnic_adapter *adapter)
1181{
1182 int i;
1183 unsigned char *data = skb->data;
1184
Sony Chackod17dd0d2012-11-28 04:34:27 +00001185 pr_info(KERN_INFO "\n");
Sony Chackoc70001a2012-11-28 04:34:26 +00001186 for (i = 0; i < skb->len; i++) {
1187 QLCDB(adapter, DRV, "%02x ", data[i]);
1188 if ((i & 0x0f) == 8)
Sony Chackod17dd0d2012-11-28 04:34:27 +00001189 pr_info(KERN_INFO "\n");
Sony Chackoc70001a2012-11-28 04:34:26 +00001190 }
1191}
1192
1193static void qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter, int ring,
1194 u64 sts_data0)
1195{
1196 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1197 struct sk_buff *skb;
1198 struct qlcnic_host_rds_ring *rds_ring;
1199 int index, length, cksum, pkt_offset;
1200
1201 if (unlikely(ring >= adapter->max_rds_rings))
1202 return;
1203
1204 rds_ring = &recv_ctx->rds_rings[ring];
1205
1206 index = qlcnic_get_sts_refhandle(sts_data0);
1207 length = qlcnic_get_sts_totallength(sts_data0);
1208 if (unlikely(index >= rds_ring->num_desc))
1209 return;
1210
1211 cksum = qlcnic_get_sts_status(sts_data0);
1212 pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1213
1214 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1215 if (!skb)
1216 return;
1217
1218 if (length > rds_ring->skb_size)
1219 skb_put(skb, rds_ring->skb_size);
1220 else
1221 skb_put(skb, length);
1222
1223 if (pkt_offset)
1224 skb_pull(skb, pkt_offset);
1225
1226 if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
Sony Chacko79788452012-12-04 03:33:53 +00001227 adapter->ahw->diag_cnt++;
Sony Chackoc70001a2012-11-28 04:34:26 +00001228 else
1229 dump_skb(skb, adapter);
1230
1231 dev_kfree_skb_any(skb);
1232 adapter->stats.rx_pkts++;
1233 adapter->stats.rxbytes += length;
1234
1235 return;
1236}
1237
Sony Chacko7e2cf4f2013-01-01 03:20:17 +00001238void qlcnic_82xx_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
Sony Chackoc70001a2012-11-28 04:34:26 +00001239{
1240 struct qlcnic_adapter *adapter = sds_ring->adapter;
1241 struct status_desc *desc;
1242 u64 sts_data0;
1243 int ring, opcode, desc_cnt;
1244
1245 u32 consumer = sds_ring->consumer;
1246
1247 desc = &sds_ring->desc_head[consumer];
1248 sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1249
1250 if (!(sts_data0 & STATUS_OWNER_HOST))
1251 return;
1252
1253 desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1254 opcode = qlcnic_get_sts_opcode(sts_data0);
1255 switch (opcode) {
1256 case QLCNIC_RESPONSE_DESC:
1257 qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1258 break;
1259 default:
1260 ring = qlcnic_get_sts_type(sts_data0);
1261 qlcnic_process_rcv_diag(adapter, ring, sts_data0);
1262 break;
1263 }
1264
1265 for (; desc_cnt > 0; desc_cnt--) {
1266 desc = &sds_ring->desc_head[consumer];
1267 desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1268 consumer = get_next_index(consumer, sds_ring->num_desc);
1269 }
1270
1271 sds_ring->consumer = consumer;
1272 writel(consumer, sds_ring->crb_sts_consumer);
1273}
1274
Sony Chacko7e2cf4f2013-01-01 03:20:17 +00001275int qlcnic_82xx_napi_add(struct qlcnic_adapter *adapter,
1276 struct net_device *netdev)
Sony Chackoc70001a2012-11-28 04:34:26 +00001277{
Sony Chackod17dd0d2012-11-28 04:34:27 +00001278 int ring, max_sds_rings;
Sony Chackoc70001a2012-11-28 04:34:26 +00001279 struct qlcnic_host_sds_ring *sds_ring;
1280 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1281
1282 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
1283 return -ENOMEM;
1284
Sony Chackod17dd0d2012-11-28 04:34:27 +00001285 max_sds_rings = adapter->max_sds_rings;
1286
Sony Chackoc70001a2012-11-28 04:34:26 +00001287 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1288 sds_ring = &recv_ctx->sds_rings[ring];
Sony Chacko4be41e92013-01-01 03:20:20 +00001289 if (ring == adapter->max_sds_rings - 1)
Sony Chackoc70001a2012-11-28 04:34:26 +00001290 netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
Sony Chackod17dd0d2012-11-28 04:34:27 +00001291 QLCNIC_NETDEV_WEIGHT / max_sds_rings);
Sony Chackoc70001a2012-11-28 04:34:26 +00001292 else
Sony Chackod17dd0d2012-11-28 04:34:27 +00001293 netif_napi_add(netdev, &sds_ring->napi, qlcnic_rx_poll,
1294 QLCNIC_NETDEV_WEIGHT*2);
Sony Chackoc70001a2012-11-28 04:34:26 +00001295 }
1296
Sony Chacko4be41e92013-01-01 03:20:20 +00001297 if (qlcnic_alloc_tx_rings(adapter, netdev)) {
1298 qlcnic_free_sds_rings(recv_ctx);
1299 return -ENOMEM;
1300 }
1301
Sony Chackoc70001a2012-11-28 04:34:26 +00001302 return 0;
1303}
1304
Sony Chacko4be41e92013-01-01 03:20:20 +00001305void qlcnic_82xx_napi_del(struct qlcnic_adapter *adapter)
Sony Chackoc70001a2012-11-28 04:34:26 +00001306{
1307 int ring;
1308 struct qlcnic_host_sds_ring *sds_ring;
1309 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1310
1311 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1312 sds_ring = &recv_ctx->sds_rings[ring];
1313 netif_napi_del(&sds_ring->napi);
1314 }
1315
1316 qlcnic_free_sds_rings(adapter->recv_ctx);
Sony Chacko4be41e92013-01-01 03:20:20 +00001317 qlcnic_free_tx_rings(adapter);
Sony Chackoc70001a2012-11-28 04:34:26 +00001318}
1319
Sony Chacko7e2cf4f2013-01-01 03:20:17 +00001320void qlcnic_82xx_napi_enable(struct qlcnic_adapter *adapter)
Sony Chackoc70001a2012-11-28 04:34:26 +00001321{
1322 int ring;
1323 struct qlcnic_host_sds_ring *sds_ring;
1324 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1325
1326 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1327 return;
1328
1329 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1330 sds_ring = &recv_ctx->sds_rings[ring];
1331 napi_enable(&sds_ring->napi);
1332 qlcnic_enable_int(sds_ring);
1333 }
1334}
1335
Sony Chacko7e2cf4f2013-01-01 03:20:17 +00001336void qlcnic_82xx_napi_disable(struct qlcnic_adapter *adapter)
Sony Chackoc70001a2012-11-28 04:34:26 +00001337{
1338 int ring;
1339 struct qlcnic_host_sds_ring *sds_ring;
1340 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1341
1342 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1343 return;
1344
1345 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1346 sds_ring = &recv_ctx->sds_rings[ring];
1347 qlcnic_disable_int(sds_ring);
1348 napi_synchronize(&sds_ring->napi);
1349 napi_disable(&sds_ring->napi);
1350 }
1351}
Sony Chacko4be41e92013-01-01 03:20:20 +00001352
1353static struct qlcnic_rx_buffer *
1354qlcnic_83xx_process_rcv(struct qlcnic_adapter *adapter,
1355 struct qlcnic_host_sds_ring *sds_ring,
1356 u8 ring, u64 sts_data[])
1357{
1358 struct net_device *netdev = adapter->netdev;
1359 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1360 struct qlcnic_rx_buffer *buffer;
1361 struct sk_buff *skb;
1362 struct qlcnic_host_rds_ring *rds_ring;
1363 int index, length, cksum;
1364 u16 vid = 0xffff;
1365
1366 if (unlikely(ring >= adapter->max_rds_rings))
1367 return NULL;
1368
1369 rds_ring = &recv_ctx->rds_rings[ring];
1370
1371 index = qlcnic_83xx_hndl(sts_data[0]);
1372 if (unlikely(index >= rds_ring->num_desc))
1373 return NULL;
1374
1375 buffer = &rds_ring->rx_buf_arr[index];
1376 length = qlcnic_83xx_pktln(sts_data[0]);
1377 cksum = qlcnic_83xx_csum_status(sts_data[1]);
1378 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1379 if (!skb)
1380 return buffer;
1381
1382 if (length > rds_ring->skb_size)
1383 skb_put(skb, rds_ring->skb_size);
1384 else
1385 skb_put(skb, length);
1386
1387 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1388 adapter->stats.rxdropped++;
1389 dev_kfree_skb(skb);
1390 return buffer;
1391 }
1392
1393 skb->protocol = eth_type_trans(skb, netdev);
1394
1395 if (vid != 0xffff)
1396 __vlan_hwaccel_put_tag(skb, vid);
1397
1398 napi_gro_receive(&sds_ring->napi, skb);
1399
1400 adapter->stats.rx_pkts++;
1401 adapter->stats.rxbytes += length;
1402
1403 return buffer;
1404}
1405
1406static struct qlcnic_rx_buffer *
1407qlcnic_83xx_process_lro(struct qlcnic_adapter *adapter,
1408 u8 ring, u64 sts_data[])
1409{
1410 struct net_device *netdev = adapter->netdev;
1411 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1412 struct qlcnic_rx_buffer *buffer;
1413 struct sk_buff *skb;
1414 struct qlcnic_host_rds_ring *rds_ring;
1415 struct iphdr *iph;
1416 struct ipv6hdr *ipv6h;
1417 struct tcphdr *th;
1418 bool push;
1419 int l2_hdr_offset, l4_hdr_offset;
1420 int index;
1421 u16 lro_length, length, data_offset;
1422 u16 vid = 0xffff;
1423
1424 if (unlikely(ring > adapter->max_rds_rings))
1425 return NULL;
1426
1427 rds_ring = &recv_ctx->rds_rings[ring];
1428
1429 index = qlcnic_83xx_hndl(sts_data[0]);
1430 if (unlikely(index > rds_ring->num_desc))
1431 return NULL;
1432
1433 buffer = &rds_ring->rx_buf_arr[index];
1434
1435 lro_length = qlcnic_83xx_lro_pktln(sts_data[0]);
1436 l2_hdr_offset = qlcnic_83xx_l2_hdr_off(sts_data[1]);
1437 l4_hdr_offset = qlcnic_83xx_l4_hdr_off(sts_data[1]);
1438 push = qlcnic_83xx_is_psh_bit(sts_data[1]);
1439
1440 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1441 if (!skb)
1442 return buffer;
1443 if (qlcnic_83xx_is_tstamp(sts_data[1]))
1444 data_offset = l4_hdr_offset + QLCNIC_TCP_TS_HDR_SIZE;
1445 else
1446 data_offset = l4_hdr_offset + QLCNIC_TCP_HDR_SIZE;
1447
1448 skb_put(skb, lro_length + data_offset);
1449 skb_pull(skb, l2_hdr_offset);
1450
1451 if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1452 adapter->stats.rxdropped++;
1453 dev_kfree_skb(skb);
1454 return buffer;
1455 }
1456
1457 skb->protocol = eth_type_trans(skb, netdev);
1458 if (ntohs(skb->protocol) == ETH_P_IPV6) {
1459 ipv6h = (struct ipv6hdr *)skb->data;
1460 th = (struct tcphdr *)(skb->data + sizeof(struct ipv6hdr));
1461
1462 length = (th->doff << 2) + lro_length;
1463 ipv6h->payload_len = htons(length);
1464 } else {
1465 iph = (struct iphdr *)skb->data;
1466 th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1467 length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1468 iph->tot_len = htons(length);
1469 iph->check = 0;
1470 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1471 }
1472
1473 th->psh = push;
1474 length = skb->len;
1475
1476 if (vid != 0xffff)
1477 __vlan_hwaccel_put_tag(skb, vid);
1478
1479 netif_receive_skb(skb);
1480
1481 adapter->stats.lro_pkts++;
1482 adapter->stats.lrobytes += length;
1483 return buffer;
1484}
1485
1486static int qlcnic_83xx_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring,
1487 int max)
1488{
1489 struct qlcnic_host_rds_ring *rds_ring;
1490 struct qlcnic_adapter *adapter = sds_ring->adapter;
1491 struct list_head *cur;
1492 struct status_desc *desc;
1493 struct qlcnic_rx_buffer *rxbuf = NULL;
1494 u8 ring;
1495 u64 sts_data[2];
1496 int count = 0, opcode;
1497 u32 consumer = sds_ring->consumer;
1498
1499 while (count < max) {
1500 desc = &sds_ring->desc_head[consumer];
1501 sts_data[1] = le64_to_cpu(desc->status_desc_data[1]);
1502 opcode = qlcnic_83xx_opcode(sts_data[1]);
1503 if (!opcode)
1504 break;
1505 sts_data[0] = le64_to_cpu(desc->status_desc_data[0]);
1506 ring = QLCNIC_FETCH_RING_ID(sts_data[0]);
1507
1508 switch (opcode) {
1509 case QLC_83XX_REG_DESC:
1510 rxbuf = qlcnic_83xx_process_rcv(adapter, sds_ring,
1511 ring, sts_data);
1512 break;
1513 case QLC_83XX_LRO_DESC:
1514 rxbuf = qlcnic_83xx_process_lro(adapter, ring,
1515 sts_data);
1516 break;
1517 default:
1518 dev_info(&adapter->pdev->dev,
1519 "Unkonwn opcode: 0x%x\n", opcode);
1520 goto skip;
1521 }
1522
1523 if (likely(rxbuf))
1524 list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1525 else
1526 adapter->stats.null_rxbuf++;
1527skip:
1528 desc = &sds_ring->desc_head[consumer];
1529 /* Reset the descriptor */
1530 desc->status_desc_data[1] = 0;
1531 consumer = get_next_index(consumer, sds_ring->num_desc);
1532 count++;
1533 }
1534 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1535 rds_ring = &adapter->recv_ctx->rds_rings[ring];
1536 if (!list_empty(&sds_ring->free_list[ring])) {
1537 list_for_each(cur, &sds_ring->free_list[ring]) {
1538 rxbuf = list_entry(cur, struct qlcnic_rx_buffer,
1539 list);
1540 qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1541 }
1542 spin_lock(&rds_ring->lock);
1543 list_splice_tail_init(&sds_ring->free_list[ring],
1544 &rds_ring->free_list);
1545 spin_unlock(&rds_ring->lock);
1546 }
1547 qlcnic_post_rx_buffers_nodb(adapter, rds_ring, ring);
1548 }
1549 if (count) {
1550 sds_ring->consumer = consumer;
1551 writel(consumer, sds_ring->crb_sts_consumer);
1552 }
1553 return count;
1554}
1555
1556static int qlcnic_83xx_poll(struct napi_struct *napi, int budget)
1557{
1558 int tx_complete;
1559 int work_done;
1560 struct qlcnic_host_sds_ring *sds_ring;
1561 struct qlcnic_adapter *adapter;
1562 struct qlcnic_host_tx_ring *tx_ring;
1563
1564 sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1565 adapter = sds_ring->adapter;
1566 /* tx ring count = 1 */
1567 tx_ring = adapter->tx_ring;
1568
1569 if (!(adapter->flags & QLCNIC_MSIX_ENABLED))
1570 qlcnic_83xx_process_aen(adapter);
1571
1572 tx_complete = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
1573 work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
1574 if ((work_done < budget) && tx_complete) {
1575 napi_complete(&sds_ring->napi);
1576 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1577 qlcnic_83xx_enable_intr(adapter, sds_ring);
1578 }
1579
1580 return work_done;
1581}
1582
1583static int qlcnic_83xx_msix_tx_poll(struct napi_struct *napi, int budget)
1584{
1585 int work_done;
1586 struct qlcnic_host_tx_ring *tx_ring;
1587 struct qlcnic_adapter *adapter;
1588
1589 budget = QLCNIC_TX_POLL_BUDGET;
1590 tx_ring = container_of(napi, struct qlcnic_host_tx_ring, napi);
1591 adapter = tx_ring->adapter;
1592 work_done = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
1593 if (work_done) {
1594 napi_complete(&tx_ring->napi);
1595 if (test_bit(__QLCNIC_DEV_UP , &adapter->state))
1596 qlcnic_83xx_enable_tx_intr(adapter, tx_ring);
1597 }
1598
1599 return work_done;
1600}
1601
1602static int qlcnic_83xx_rx_poll(struct napi_struct *napi, int budget)
1603{
1604 int work_done;
1605 struct qlcnic_host_sds_ring *sds_ring;
1606 struct qlcnic_adapter *adapter;
1607
1608 sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1609 adapter = sds_ring->adapter;
1610 work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
1611 if (work_done < budget) {
1612 napi_complete(&sds_ring->napi);
1613 if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1614 qlcnic_83xx_enable_intr(adapter, sds_ring);
1615 }
1616
1617 return work_done;
1618}
1619
1620void qlcnic_83xx_napi_enable(struct qlcnic_adapter *adapter)
1621{
1622 int ring;
1623 struct qlcnic_host_sds_ring *sds_ring;
1624 struct qlcnic_host_tx_ring *tx_ring;
1625 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1626
1627 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1628 return;
1629
1630 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1631 sds_ring = &recv_ctx->sds_rings[ring];
1632 napi_enable(&sds_ring->napi);
1633 qlcnic_83xx_enable_intr(adapter, sds_ring);
1634 }
1635
1636 if (adapter->flags & QLCNIC_MSIX_ENABLED) {
1637 for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
1638 tx_ring = &adapter->tx_ring[ring];
1639 napi_enable(&tx_ring->napi);
1640 qlcnic_83xx_enable_tx_intr(adapter, tx_ring);
1641 }
1642 }
1643}
1644
1645void qlcnic_83xx_napi_disable(struct qlcnic_adapter *adapter)
1646{
1647 int ring;
1648 struct qlcnic_host_sds_ring *sds_ring;
1649 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1650 struct qlcnic_host_tx_ring *tx_ring;
1651
1652 if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1653 return;
1654
1655 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1656 sds_ring = &recv_ctx->sds_rings[ring];
1657 writel(1, sds_ring->crb_intr_mask);
1658 napi_synchronize(&sds_ring->napi);
1659 napi_disable(&sds_ring->napi);
1660 }
1661
1662 if (adapter->flags & QLCNIC_MSIX_ENABLED) {
1663 for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
1664 tx_ring = &adapter->tx_ring[ring];
1665 qlcnic_83xx_disable_tx_intr(adapter, tx_ring);
1666 napi_synchronize(&tx_ring->napi);
1667 napi_disable(&tx_ring->napi);
1668 }
1669 }
1670}
1671
1672int qlcnic_83xx_napi_add(struct qlcnic_adapter *adapter,
1673 struct net_device *netdev)
1674{
1675 int ring, max_sds_rings;
1676 struct qlcnic_host_sds_ring *sds_ring;
1677 struct qlcnic_host_tx_ring *tx_ring;
1678 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1679
1680 if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
1681 return -ENOMEM;
1682
1683 max_sds_rings = adapter->max_sds_rings;
1684 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1685 sds_ring = &recv_ctx->sds_rings[ring];
1686 if (adapter->flags & QLCNIC_MSIX_ENABLED)
1687 netif_napi_add(netdev, &sds_ring->napi,
1688 qlcnic_83xx_rx_poll,
1689 QLCNIC_NETDEV_WEIGHT * 2);
1690 else
1691 netif_napi_add(netdev, &sds_ring->napi,
1692 qlcnic_83xx_poll,
1693 QLCNIC_NETDEV_WEIGHT / max_sds_rings);
1694 }
1695
1696 if (qlcnic_alloc_tx_rings(adapter, netdev)) {
1697 qlcnic_free_sds_rings(recv_ctx);
1698 return -ENOMEM;
1699 }
1700
1701 if (adapter->flags & QLCNIC_MSIX_ENABLED) {
1702 for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
1703 tx_ring = &adapter->tx_ring[ring];
1704 netif_napi_add(netdev, &tx_ring->napi,
1705 qlcnic_83xx_msix_tx_poll,
1706 QLCNIC_NETDEV_WEIGHT);
1707 }
1708 }
1709
1710 return 0;
1711}
1712
1713void qlcnic_83xx_napi_del(struct qlcnic_adapter *adapter)
1714{
1715 int ring;
1716 struct qlcnic_host_sds_ring *sds_ring;
1717 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1718 struct qlcnic_host_tx_ring *tx_ring;
1719
1720 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
1721 sds_ring = &recv_ctx->sds_rings[ring];
1722 netif_napi_del(&sds_ring->napi);
1723 }
1724
1725 qlcnic_free_sds_rings(adapter->recv_ctx);
1726
1727 if ((adapter->flags & QLCNIC_MSIX_ENABLED)) {
1728 for (ring = 0; ring < adapter->max_drv_tx_rings; ring++) {
1729 tx_ring = &adapter->tx_ring[ring];
1730 netif_napi_del(&tx_ring->napi);
1731 }
1732 }
1733
1734 qlcnic_free_tx_rings(adapter);
1735}
1736
1737void qlcnic_83xx_process_rcv_diag(struct qlcnic_adapter *adapter,
1738 int ring, u64 sts_data[])
1739{
1740 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1741 struct sk_buff *skb;
1742 struct qlcnic_host_rds_ring *rds_ring;
1743 int index, length;
1744
1745 if (unlikely(ring >= adapter->max_rds_rings))
1746 return;
1747
1748 rds_ring = &recv_ctx->rds_rings[ring];
1749 index = qlcnic_83xx_hndl(sts_data[0]);
1750 if (unlikely(index >= rds_ring->num_desc))
1751 return;
1752
1753 length = qlcnic_83xx_pktln(sts_data[0]);
1754
1755 skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1756 if (!skb)
1757 return;
1758
1759 if (length > rds_ring->skb_size)
1760 skb_put(skb, rds_ring->skb_size);
1761 else
1762 skb_put(skb, length);
1763
1764 if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
1765 adapter->ahw->diag_cnt++;
1766 else
1767 dump_skb(skb, adapter);
1768
1769 dev_kfree_skb_any(skb);
1770 return;
1771}
1772
1773void qlcnic_83xx_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
1774{
1775 struct qlcnic_adapter *adapter = sds_ring->adapter;
1776 struct status_desc *desc;
1777 u64 sts_data[2];
1778 int ring, opcode;
1779 u32 consumer = sds_ring->consumer;
1780
1781 desc = &sds_ring->desc_head[consumer];
1782 sts_data[0] = le64_to_cpu(desc->status_desc_data[0]);
1783 sts_data[1] = le64_to_cpu(desc->status_desc_data[1]);
1784 opcode = qlcnic_83xx_opcode(sts_data[1]);
1785 if (!opcode)
1786 return;
1787
1788 ring = QLCNIC_FETCH_RING_ID(qlcnic_83xx_hndl(sts_data[0]));
1789 qlcnic_83xx_process_rcv_diag(adapter, ring, sts_data);
1790 desc = &sds_ring->desc_head[consumer];
1791 desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1792 consumer = get_next_index(consumer, sds_ring->num_desc);
1793 sds_ring->consumer = consumer;
1794 writel(consumer, sds_ring->crb_sts_consumer);
1795}