diff options
author | Sathya Perla <sathyap@serverengines.com> | 2009-03-12 02:32:03 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2009-03-12 02:32:22 -0400 |
commit | 6b7c5b947c671a96e39f9526a5fd70c178b8dfd1 (patch) | |
tree | 6fba014e23f72f569dc3e89f3fc51c9e67c41850 /drivers/net/benet/be_main.c | |
parent | 2c5849ea38fdad477d72dcf1c8c4842db4b33aae (diff) |
net: Add be2net driver.
Signed-off-by: Sathya Perla <sathyap@serverengines.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'drivers/net/benet/be_main.c')
-rw-r--r-- | drivers/net/benet/be_main.c | 1903 |
1 files changed, 1903 insertions, 0 deletions
diff --git a/drivers/net/benet/be_main.c b/drivers/net/benet/be_main.c new file mode 100644 index 000000000000..897a63de5bdb --- /dev/null +++ b/drivers/net/benet/be_main.c | |||
@@ -0,0 +1,1903 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2005 - 2009 ServerEngines | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or | ||
6 | * modify it under the terms of the GNU General Public License version 2 | ||
7 | * as published by the Free Software Foundation. The full GNU General | ||
8 | * Public License is included in this distribution in the file called COPYING. | ||
9 | * | ||
10 | * Contact Information: | ||
11 | * linux-drivers@serverengines.com | ||
12 | * | ||
13 | * ServerEngines | ||
14 | * 209 N. Fair Oaks Ave | ||
15 | * Sunnyvale, CA 94085 | ||
16 | */ | ||
17 | |||
18 | #include "be.h" | ||
19 | |||
20 | MODULE_VERSION(DRV_VER); | ||
21 | MODULE_DEVICE_TABLE(pci, be_dev_ids); | ||
22 | MODULE_DESCRIPTION(DRV_DESC " " DRV_VER); | ||
23 | MODULE_AUTHOR("ServerEngines Corporation"); | ||
24 | MODULE_LICENSE("GPL"); | ||
25 | |||
26 | static unsigned int rx_frag_size = 2048; | ||
27 | module_param(rx_frag_size, uint, S_IRUGO); | ||
28 | MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data."); | ||
29 | |||
30 | #define BE_VENDOR_ID 0x19a2 | ||
31 | #define BE2_DEVICE_ID_1 0x0211 | ||
32 | static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = { | ||
33 | { PCI_DEVICE(BE_VENDOR_ID, BE2_DEVICE_ID_1) }, | ||
34 | { 0 } | ||
35 | }; | ||
36 | MODULE_DEVICE_TABLE(pci, be_dev_ids); | ||
37 | |||
38 | static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q) | ||
39 | { | ||
40 | struct be_dma_mem *mem = &q->dma_mem; | ||
41 | if (mem->va) | ||
42 | pci_free_consistent(adapter->pdev, mem->size, | ||
43 | mem->va, mem->dma); | ||
44 | } | ||
45 | |||
46 | static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q, | ||
47 | u16 len, u16 entry_size) | ||
48 | { | ||
49 | struct be_dma_mem *mem = &q->dma_mem; | ||
50 | |||
51 | memset(q, 0, sizeof(*q)); | ||
52 | q->len = len; | ||
53 | q->entry_size = entry_size; | ||
54 | mem->size = len * entry_size; | ||
55 | mem->va = pci_alloc_consistent(adapter->pdev, mem->size, &mem->dma); | ||
56 | if (!mem->va) | ||
57 | return -1; | ||
58 | memset(mem->va, 0, mem->size); | ||
59 | return 0; | ||
60 | } | ||
61 | |||
62 | static inline void *queue_head_node(struct be_queue_info *q) | ||
63 | { | ||
64 | return q->dma_mem.va + q->head * q->entry_size; | ||
65 | } | ||
66 | |||
67 | static inline void *queue_tail_node(struct be_queue_info *q) | ||
68 | { | ||
69 | return q->dma_mem.va + q->tail * q->entry_size; | ||
70 | } | ||
71 | |||
72 | static inline void queue_head_inc(struct be_queue_info *q) | ||
73 | { | ||
74 | index_inc(&q->head, q->len); | ||
75 | } | ||
76 | |||
77 | static inline void queue_tail_inc(struct be_queue_info *q) | ||
78 | { | ||
79 | index_inc(&q->tail, q->len); | ||
80 | } | ||
81 | |||
82 | static void be_intr_set(struct be_ctrl_info *ctrl, bool enable) | ||
83 | { | ||
84 | u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET; | ||
85 | u32 reg = ioread32(addr); | ||
86 | u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK; | ||
87 | if (!enabled && enable) { | ||
88 | reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK; | ||
89 | } else if (enabled && !enable) { | ||
90 | reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK; | ||
91 | } else { | ||
92 | printk(KERN_WARNING DRV_NAME | ||
93 | ": bad value in membar_int_ctrl reg=0x%x\n", reg); | ||
94 | return; | ||
95 | } | ||
96 | iowrite32(reg, addr); | ||
97 | } | ||
98 | |||
99 | static void be_rxq_notify(struct be_ctrl_info *ctrl, u16 qid, u16 posted) | ||
100 | { | ||
101 | u32 val = 0; | ||
102 | val |= qid & DB_RQ_RING_ID_MASK; | ||
103 | val |= posted << DB_RQ_NUM_POSTED_SHIFT; | ||
104 | iowrite32(val, ctrl->db + DB_RQ_OFFSET); | ||
105 | } | ||
106 | |||
107 | static void be_txq_notify(struct be_ctrl_info *ctrl, u16 qid, u16 posted) | ||
108 | { | ||
109 | u32 val = 0; | ||
110 | val |= qid & DB_TXULP_RING_ID_MASK; | ||
111 | val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT; | ||
112 | iowrite32(val, ctrl->db + DB_TXULP1_OFFSET); | ||
113 | } | ||
114 | |||
115 | static void be_eq_notify(struct be_ctrl_info *ctrl, u16 qid, | ||
116 | bool arm, bool clear_int, u16 num_popped) | ||
117 | { | ||
118 | u32 val = 0; | ||
119 | val |= qid & DB_EQ_RING_ID_MASK; | ||
120 | if (arm) | ||
121 | val |= 1 << DB_EQ_REARM_SHIFT; | ||
122 | if (clear_int) | ||
123 | val |= 1 << DB_EQ_CLR_SHIFT; | ||
124 | val |= 1 << DB_EQ_EVNT_SHIFT; | ||
125 | val |= num_popped << DB_EQ_NUM_POPPED_SHIFT; | ||
126 | iowrite32(val, ctrl->db + DB_EQ_OFFSET); | ||
127 | } | ||
128 | |||
129 | static void be_cq_notify(struct be_ctrl_info *ctrl, u16 qid, | ||
130 | bool arm, u16 num_popped) | ||
131 | { | ||
132 | u32 val = 0; | ||
133 | val |= qid & DB_CQ_RING_ID_MASK; | ||
134 | if (arm) | ||
135 | val |= 1 << DB_CQ_REARM_SHIFT; | ||
136 | val |= num_popped << DB_CQ_NUM_POPPED_SHIFT; | ||
137 | iowrite32(val, ctrl->db + DB_CQ_OFFSET); | ||
138 | } | ||
139 | |||
140 | |||
141 | static int be_mac_addr_set(struct net_device *netdev, void *p) | ||
142 | { | ||
143 | struct be_adapter *adapter = netdev_priv(netdev); | ||
144 | struct sockaddr *addr = p; | ||
145 | int status = 0; | ||
146 | |||
147 | if (netif_running(netdev)) { | ||
148 | status = be_cmd_pmac_del(&adapter->ctrl, adapter->if_handle, | ||
149 | adapter->pmac_id); | ||
150 | if (status) | ||
151 | return status; | ||
152 | |||
153 | status = be_cmd_pmac_add(&adapter->ctrl, (u8 *)addr->sa_data, | ||
154 | adapter->if_handle, &adapter->pmac_id); | ||
155 | } | ||
156 | |||
157 | if (!status) | ||
158 | memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); | ||
159 | |||
160 | return status; | ||
161 | } | ||
162 | |||
163 | static void netdev_stats_update(struct be_adapter *adapter) | ||
164 | { | ||
165 | struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats.cmd.va); | ||
166 | struct be_rxf_stats *rxf_stats = &hw_stats->rxf; | ||
167 | struct be_port_rxf_stats *port_stats = | ||
168 | &rxf_stats->port[adapter->port_num]; | ||
169 | struct net_device_stats *dev_stats = &adapter->stats.net_stats; | ||
170 | |||
171 | dev_stats->rx_packets = port_stats->rx_total_frames; | ||
172 | dev_stats->tx_packets = port_stats->tx_unicastframes + | ||
173 | port_stats->tx_multicastframes + port_stats->tx_broadcastframes; | ||
174 | dev_stats->rx_bytes = (u64) port_stats->rx_bytes_msd << 32 | | ||
175 | (u64) port_stats->rx_bytes_lsd; | ||
176 | dev_stats->tx_bytes = (u64) port_stats->tx_bytes_msd << 32 | | ||
177 | (u64) port_stats->tx_bytes_lsd; | ||
178 | |||
179 | /* bad pkts received */ | ||
180 | dev_stats->rx_errors = port_stats->rx_crc_errors + | ||
181 | port_stats->rx_alignment_symbol_errors + | ||
182 | port_stats->rx_in_range_errors + | ||
183 | port_stats->rx_out_range_errors + port_stats->rx_frame_too_long; | ||
184 | |||
185 | /* packet transmit problems */ | ||
186 | dev_stats->tx_errors = 0; | ||
187 | |||
188 | /* no space in linux buffers */ | ||
189 | dev_stats->rx_dropped = 0; | ||
190 | |||
191 | /* no space available in linux */ | ||
192 | dev_stats->tx_dropped = 0; | ||
193 | |||
194 | dev_stats->multicast = port_stats->tx_multicastframes; | ||
195 | dev_stats->collisions = 0; | ||
196 | |||
197 | /* detailed rx errors */ | ||
198 | dev_stats->rx_length_errors = port_stats->rx_in_range_errors + | ||
199 | port_stats->rx_out_range_errors + port_stats->rx_frame_too_long; | ||
200 | /* receive ring buffer overflow */ | ||
201 | dev_stats->rx_over_errors = 0; | ||
202 | dev_stats->rx_crc_errors = port_stats->rx_crc_errors; | ||
203 | |||
204 | /* frame alignment errors */ | ||
205 | dev_stats->rx_frame_errors = port_stats->rx_alignment_symbol_errors; | ||
206 | /* receiver fifo overrun */ | ||
207 | /* drops_no_pbuf is no per i/f, it's per BE card */ | ||
208 | dev_stats->rx_fifo_errors = port_stats->rx_fifo_overflow + | ||
209 | port_stats->rx_input_fifo_overflow + | ||
210 | rxf_stats->rx_drops_no_pbuf; | ||
211 | /* receiver missed packetd */ | ||
212 | dev_stats->rx_missed_errors = 0; | ||
213 | /* detailed tx_errors */ | ||
214 | dev_stats->tx_aborted_errors = 0; | ||
215 | dev_stats->tx_carrier_errors = 0; | ||
216 | dev_stats->tx_fifo_errors = 0; | ||
217 | dev_stats->tx_heartbeat_errors = 0; | ||
218 | dev_stats->tx_window_errors = 0; | ||
219 | } | ||
220 | |||
221 | static void be_link_status_update(struct be_adapter *adapter) | ||
222 | { | ||
223 | struct be_link_info *prev = &adapter->link; | ||
224 | struct be_link_info now = { 0 }; | ||
225 | struct net_device *netdev = adapter->netdev; | ||
226 | |||
227 | be_cmd_link_status_query(&adapter->ctrl, &now); | ||
228 | |||
229 | /* If link came up or went down */ | ||
230 | if (now.speed != prev->speed && (now.speed == PHY_LINK_SPEED_ZERO || | ||
231 | prev->speed == PHY_LINK_SPEED_ZERO)) { | ||
232 | if (now.speed == PHY_LINK_SPEED_ZERO) { | ||
233 | netif_stop_queue(netdev); | ||
234 | netif_carrier_off(netdev); | ||
235 | printk(KERN_INFO "%s: Link down\n", netdev->name); | ||
236 | } else { | ||
237 | netif_start_queue(netdev); | ||
238 | netif_carrier_on(netdev); | ||
239 | printk(KERN_INFO "%s: Link up\n", netdev->name); | ||
240 | } | ||
241 | } | ||
242 | *prev = now; | ||
243 | } | ||
244 | |||
245 | /* Update the EQ delay n BE based on the RX frags consumed / sec */ | ||
246 | static void be_rx_eqd_update(struct be_adapter *adapter) | ||
247 | { | ||
248 | u32 eqd; | ||
249 | struct be_ctrl_info *ctrl = &adapter->ctrl; | ||
250 | struct be_eq_obj *rx_eq = &adapter->rx_eq; | ||
251 | struct be_drvr_stats *stats = &adapter->stats.drvr_stats; | ||
252 | |||
253 | /* Update once a second */ | ||
254 | if (((jiffies - stats->rx_fps_jiffies) < HZ) || rx_eq->enable_aic == 0) | ||
255 | return; | ||
256 | |||
257 | stats->be_rx_fps = (stats->be_rx_frags - stats->be_prev_rx_frags) / | ||
258 | ((jiffies - stats->rx_fps_jiffies) / HZ); | ||
259 | |||
260 | stats->rx_fps_jiffies = jiffies; | ||
261 | stats->be_prev_rx_frags = stats->be_rx_frags; | ||
262 | eqd = stats->be_rx_fps / 110000; | ||
263 | eqd = eqd << 3; | ||
264 | if (eqd > rx_eq->max_eqd) | ||
265 | eqd = rx_eq->max_eqd; | ||
266 | if (eqd < rx_eq->min_eqd) | ||
267 | eqd = rx_eq->min_eqd; | ||
268 | if (eqd < 10) | ||
269 | eqd = 0; | ||
270 | if (eqd != rx_eq->cur_eqd) | ||
271 | be_cmd_modify_eqd(ctrl, rx_eq->q.id, eqd); | ||
272 | |||
273 | rx_eq->cur_eqd = eqd; | ||
274 | } | ||
275 | |||
276 | static void be_worker(struct work_struct *work) | ||
277 | { | ||
278 | struct be_adapter *adapter = | ||
279 | container_of(work, struct be_adapter, work.work); | ||
280 | int status; | ||
281 | |||
282 | /* Check link */ | ||
283 | be_link_status_update(adapter); | ||
284 | |||
285 | /* Get Stats */ | ||
286 | status = be_cmd_get_stats(&adapter->ctrl, &adapter->stats.cmd); | ||
287 | if (!status) | ||
288 | netdev_stats_update(adapter); | ||
289 | |||
290 | /* Set EQ delay */ | ||
291 | be_rx_eqd_update(adapter); | ||
292 | |||
293 | schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000)); | ||
294 | } | ||
295 | |||
296 | static struct net_device_stats *be_get_stats(struct net_device *dev) | ||
297 | { | ||
298 | struct be_adapter *adapter = netdev_priv(dev); | ||
299 | |||
300 | return &adapter->stats.net_stats; | ||
301 | } | ||
302 | |||
303 | static void be_tx_stats_update(struct be_adapter *adapter, | ||
304 | u32 wrb_cnt, u32 copied, bool stopped) | ||
305 | { | ||
306 | struct be_drvr_stats *stats = &adapter->stats.drvr_stats; | ||
307 | stats->be_tx_reqs++; | ||
308 | stats->be_tx_wrbs += wrb_cnt; | ||
309 | stats->be_tx_bytes += copied; | ||
310 | if (stopped) | ||
311 | stats->be_tx_stops++; | ||
312 | |||
313 | /* Update tx rate once in two seconds */ | ||
314 | if ((jiffies - stats->be_tx_jiffies) > 2 * HZ) { | ||
315 | u32 r; | ||
316 | r = (stats->be_tx_bytes - stats->be_tx_bytes_prev) / | ||
317 | ((u32) (jiffies - stats->be_tx_jiffies) / HZ); | ||
318 | r = (r / 1000000); /* M bytes/s */ | ||
319 | stats->be_tx_rate = (r * 8); /* M bits/s */ | ||
320 | stats->be_tx_jiffies = jiffies; | ||
321 | stats->be_tx_bytes_prev = stats->be_tx_bytes; | ||
322 | } | ||
323 | } | ||
324 | |||
325 | /* Determine number of WRB entries needed to xmit data in an skb */ | ||
326 | static u32 wrb_cnt_for_skb(struct sk_buff *skb, bool *dummy) | ||
327 | { | ||
328 | int cnt = 0; | ||
329 | while (skb) { | ||
330 | if (skb->len > skb->data_len) | ||
331 | cnt++; | ||
332 | cnt += skb_shinfo(skb)->nr_frags; | ||
333 | skb = skb_shinfo(skb)->frag_list; | ||
334 | } | ||
335 | /* to account for hdr wrb */ | ||
336 | cnt++; | ||
337 | if (cnt & 1) { | ||
338 | /* add a dummy to make it an even num */ | ||
339 | cnt++; | ||
340 | *dummy = true; | ||
341 | } else | ||
342 | *dummy = false; | ||
343 | BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT); | ||
344 | return cnt; | ||
345 | } | ||
346 | |||
347 | static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len) | ||
348 | { | ||
349 | wrb->frag_pa_hi = upper_32_bits(addr); | ||
350 | wrb->frag_pa_lo = addr & 0xFFFFFFFF; | ||
351 | wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK; | ||
352 | } | ||
353 | |||
354 | static void wrb_fill_hdr(struct be_eth_hdr_wrb *hdr, struct sk_buff *skb, | ||
355 | bool vlan, u32 wrb_cnt, u32 len) | ||
356 | { | ||
357 | memset(hdr, 0, sizeof(*hdr)); | ||
358 | |||
359 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1); | ||
360 | |||
361 | if (skb_shinfo(skb)->gso_segs > 1 && skb_shinfo(skb)->gso_size) { | ||
362 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1); | ||
363 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss, | ||
364 | hdr, skb_shinfo(skb)->gso_size); | ||
365 | } else if (skb->ip_summed == CHECKSUM_PARTIAL) { | ||
366 | if (is_tcp_pkt(skb)) | ||
367 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1); | ||
368 | else if (is_udp_pkt(skb)) | ||
369 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1); | ||
370 | } | ||
371 | |||
372 | if (vlan && vlan_tx_tag_present(skb)) { | ||
373 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1); | ||
374 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag, | ||
375 | hdr, vlan_tx_tag_get(skb)); | ||
376 | } | ||
377 | |||
378 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1); | ||
379 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1); | ||
380 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt); | ||
381 | AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len); | ||
382 | } | ||
383 | |||
384 | |||
385 | static int make_tx_wrbs(struct be_adapter *adapter, | ||
386 | struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb) | ||
387 | { | ||
388 | u64 busaddr; | ||
389 | u32 i, copied = 0; | ||
390 | struct pci_dev *pdev = adapter->pdev; | ||
391 | struct sk_buff *first_skb = skb; | ||
392 | struct be_queue_info *txq = &adapter->tx_obj.q; | ||
393 | struct be_eth_wrb *wrb; | ||
394 | struct be_eth_hdr_wrb *hdr; | ||
395 | |||
396 | atomic_add(wrb_cnt, &txq->used); | ||
397 | hdr = queue_head_node(txq); | ||
398 | queue_head_inc(txq); | ||
399 | |||
400 | while (skb) { | ||
401 | if (skb->len > skb->data_len) { | ||
402 | int len = skb->len - skb->data_len; | ||
403 | busaddr = pci_map_single(pdev, skb->data, len, | ||
404 | PCI_DMA_TODEVICE); | ||
405 | wrb = queue_head_node(txq); | ||
406 | wrb_fill(wrb, busaddr, len); | ||
407 | be_dws_cpu_to_le(wrb, sizeof(*wrb)); | ||
408 | queue_head_inc(txq); | ||
409 | copied += len; | ||
410 | } | ||
411 | |||
412 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | ||
413 | struct skb_frag_struct *frag = | ||
414 | &skb_shinfo(skb)->frags[i]; | ||
415 | busaddr = pci_map_page(pdev, frag->page, | ||
416 | frag->page_offset, | ||
417 | frag->size, PCI_DMA_TODEVICE); | ||
418 | wrb = queue_head_node(txq); | ||
419 | wrb_fill(wrb, busaddr, frag->size); | ||
420 | be_dws_cpu_to_le(wrb, sizeof(*wrb)); | ||
421 | queue_head_inc(txq); | ||
422 | copied += frag->size; | ||
423 | } | ||
424 | skb = skb_shinfo(skb)->frag_list; | ||
425 | } | ||
426 | |||
427 | if (dummy_wrb) { | ||
428 | wrb = queue_head_node(txq); | ||
429 | wrb_fill(wrb, 0, 0); | ||
430 | be_dws_cpu_to_le(wrb, sizeof(*wrb)); | ||
431 | queue_head_inc(txq); | ||
432 | } | ||
433 | |||
434 | wrb_fill_hdr(hdr, first_skb, adapter->vlan_grp ? true : false, | ||
435 | wrb_cnt, copied); | ||
436 | be_dws_cpu_to_le(hdr, sizeof(*hdr)); | ||
437 | |||
438 | return copied; | ||
439 | } | ||
440 | |||
441 | static int be_xmit(struct sk_buff *skb, struct net_device *netdev) | ||
442 | { | ||
443 | struct be_adapter *adapter = netdev_priv(netdev); | ||
444 | struct be_tx_obj *tx_obj = &adapter->tx_obj; | ||
445 | struct be_queue_info *txq = &tx_obj->q; | ||
446 | u32 wrb_cnt = 0, copied = 0; | ||
447 | u32 start = txq->head; | ||
448 | bool dummy_wrb, stopped = false; | ||
449 | |||
450 | wrb_cnt = wrb_cnt_for_skb(skb, &dummy_wrb); | ||
451 | |||
452 | copied = make_tx_wrbs(adapter, skb, wrb_cnt, dummy_wrb); | ||
453 | |||
454 | /* record the sent skb in the sent_skb table */ | ||
455 | BUG_ON(tx_obj->sent_skb_list[start]); | ||
456 | tx_obj->sent_skb_list[start] = skb; | ||
457 | |||
458 | /* Ensure that txq has space for the next skb; Else stop the queue | ||
459 | * *BEFORE* ringing the tx doorbell, so that we serialze the | ||
460 | * tx compls of the current transmit which'll wake up the queue | ||
461 | */ | ||
462 | if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >= txq->len) { | ||
463 | netif_stop_queue(netdev); | ||
464 | stopped = true; | ||
465 | } | ||
466 | |||
467 | be_txq_notify(&adapter->ctrl, txq->id, wrb_cnt); | ||
468 | |||
469 | netdev->trans_start = jiffies; | ||
470 | |||
471 | be_tx_stats_update(adapter, wrb_cnt, copied, stopped); | ||
472 | return NETDEV_TX_OK; | ||
473 | } | ||
474 | |||
475 | static int be_change_mtu(struct net_device *netdev, int new_mtu) | ||
476 | { | ||
477 | struct be_adapter *adapter = netdev_priv(netdev); | ||
478 | if (new_mtu < BE_MIN_MTU || | ||
479 | new_mtu > BE_MAX_JUMBO_FRAME_SIZE) { | ||
480 | dev_info(&adapter->pdev->dev, | ||
481 | "MTU must be between %d and %d bytes\n", | ||
482 | BE_MIN_MTU, BE_MAX_JUMBO_FRAME_SIZE); | ||
483 | return -EINVAL; | ||
484 | } | ||
485 | dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n", | ||
486 | netdev->mtu, new_mtu); | ||
487 | netdev->mtu = new_mtu; | ||
488 | return 0; | ||
489 | } | ||
490 | |||
491 | /* | ||
492 | * if there are BE_NUM_VLANS_SUPPORTED or lesser number of VLANS configured, | ||
493 | * program them in BE. If more than BE_NUM_VLANS_SUPPORTED are configured, | ||
494 | * set the BE in promiscuous VLAN mode. | ||
495 | */ | ||
496 | static void be_vids_config(struct net_device *netdev) | ||
497 | { | ||
498 | struct be_adapter *adapter = netdev_priv(netdev); | ||
499 | u16 vtag[BE_NUM_VLANS_SUPPORTED]; | ||
500 | u16 ntags = 0, i; | ||
501 | |||
502 | if (adapter->num_vlans <= BE_NUM_VLANS_SUPPORTED) { | ||
503 | /* Construct VLAN Table to give to HW */ | ||
504 | for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) { | ||
505 | if (adapter->vlan_tag[i]) { | ||
506 | vtag[ntags] = cpu_to_le16(i); | ||
507 | ntags++; | ||
508 | } | ||
509 | } | ||
510 | be_cmd_vlan_config(&adapter->ctrl, adapter->if_handle, | ||
511 | vtag, ntags, 1, 0); | ||
512 | } else { | ||
513 | be_cmd_vlan_config(&adapter->ctrl, adapter->if_handle, | ||
514 | NULL, 0, 1, 1); | ||
515 | } | ||
516 | } | ||
517 | |||
518 | static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp) | ||
519 | { | ||
520 | struct be_adapter *adapter = netdev_priv(netdev); | ||
521 | struct be_eq_obj *rx_eq = &adapter->rx_eq; | ||
522 | struct be_eq_obj *tx_eq = &adapter->tx_eq; | ||
523 | struct be_ctrl_info *ctrl = &adapter->ctrl; | ||
524 | |||
525 | be_eq_notify(ctrl, rx_eq->q.id, false, false, 0); | ||
526 | be_eq_notify(ctrl, tx_eq->q.id, false, false, 0); | ||
527 | adapter->vlan_grp = grp; | ||
528 | be_eq_notify(ctrl, rx_eq->q.id, true, false, 0); | ||
529 | be_eq_notify(ctrl, tx_eq->q.id, true, false, 0); | ||
530 | } | ||
531 | |||
532 | static void be_vlan_add_vid(struct net_device *netdev, u16 vid) | ||
533 | { | ||
534 | struct be_adapter *adapter = netdev_priv(netdev); | ||
535 | |||
536 | adapter->num_vlans++; | ||
537 | adapter->vlan_tag[vid] = 1; | ||
538 | |||
539 | be_vids_config(netdev); | ||
540 | } | ||
541 | |||
542 | static void be_vlan_rem_vid(struct net_device *netdev, u16 vid) | ||
543 | { | ||
544 | struct be_adapter *adapter = netdev_priv(netdev); | ||
545 | |||
546 | adapter->num_vlans--; | ||
547 | adapter->vlan_tag[vid] = 0; | ||
548 | |||
549 | vlan_group_set_device(adapter->vlan_grp, vid, NULL); | ||
550 | be_vids_config(netdev); | ||
551 | } | ||
552 | |||
553 | static void be_set_multicast_filter(struct net_device *netdev) | ||
554 | { | ||
555 | struct be_adapter *adapter = netdev_priv(netdev); | ||
556 | struct dev_mc_list *mc_ptr; | ||
557 | u8 mac_addr[32][ETH_ALEN]; | ||
558 | int i = 0; | ||
559 | |||
560 | if (netdev->flags & IFF_ALLMULTI) { | ||
561 | /* set BE in Multicast promiscuous */ | ||
562 | be_cmd_mcast_mac_set(&adapter->ctrl, | ||
563 | adapter->if_handle, NULL, 0, true); | ||
564 | return; | ||
565 | } | ||
566 | |||
567 | for (mc_ptr = netdev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) { | ||
568 | memcpy(&mac_addr[i][0], mc_ptr->dmi_addr, ETH_ALEN); | ||
569 | if (++i >= 32) { | ||
570 | be_cmd_mcast_mac_set(&adapter->ctrl, | ||
571 | adapter->if_handle, &mac_addr[0][0], i, false); | ||
572 | i = 0; | ||
573 | } | ||
574 | |||
575 | } | ||
576 | |||
577 | if (i) { | ||
578 | /* reset the promiscuous mode also. */ | ||
579 | be_cmd_mcast_mac_set(&adapter->ctrl, | ||
580 | adapter->if_handle, &mac_addr[0][0], i, false); | ||
581 | } | ||
582 | } | ||
583 | |||
584 | static void be_set_multicast_list(struct net_device *netdev) | ||
585 | { | ||
586 | struct be_adapter *adapter = netdev_priv(netdev); | ||
587 | |||
588 | if (netdev->flags & IFF_PROMISC) { | ||
589 | be_cmd_promiscuous_config(&adapter->ctrl, adapter->port_num, 1); | ||
590 | } else { | ||
591 | be_cmd_promiscuous_config(&adapter->ctrl, adapter->port_num, 0); | ||
592 | be_set_multicast_filter(netdev); | ||
593 | } | ||
594 | } | ||
595 | |||
596 | static void be_rx_rate_update(struct be_adapter *adapter, u32 pktsize, | ||
597 | u16 numfrags) | ||
598 | { | ||
599 | struct be_drvr_stats *stats = &adapter->stats.drvr_stats; | ||
600 | u32 rate; | ||
601 | |||
602 | stats->be_rx_compl++; | ||
603 | stats->be_rx_frags += numfrags; | ||
604 | stats->be_rx_bytes += pktsize; | ||
605 | |||
606 | /* Update the rate once in two seconds */ | ||
607 | if ((jiffies - stats->be_rx_jiffies) < 2 * HZ) | ||
608 | return; | ||
609 | |||
610 | rate = (stats->be_rx_bytes - stats->be_rx_bytes_prev) / | ||
611 | ((u32) (jiffies - stats->be_rx_jiffies) / HZ); | ||
612 | rate = (rate / 1000000); /* MB/Sec */ | ||
613 | stats->be_rx_rate = (rate * 8); /* Mega Bits/Sec */ | ||
614 | stats->be_rx_jiffies = jiffies; | ||
615 | stats->be_rx_bytes_prev = stats->be_rx_bytes; | ||
616 | } | ||
617 | |||
618 | static struct be_rx_page_info * | ||
619 | get_rx_page_info(struct be_adapter *adapter, u16 frag_idx) | ||
620 | { | ||
621 | struct be_rx_page_info *rx_page_info; | ||
622 | struct be_queue_info *rxq = &adapter->rx_obj.q; | ||
623 | |||
624 | rx_page_info = &adapter->rx_obj.page_info_tbl[frag_idx]; | ||
625 | BUG_ON(!rx_page_info->page); | ||
626 | |||
627 | if (rx_page_info->last_page_user) | ||
628 | pci_unmap_page(adapter->pdev, pci_unmap_addr(rx_page_info, bus), | ||
629 | adapter->big_page_size, PCI_DMA_FROMDEVICE); | ||
630 | |||
631 | atomic_dec(&rxq->used); | ||
632 | return rx_page_info; | ||
633 | } | ||
634 | |||
635 | /* Throwaway the data in the Rx completion */ | ||
636 | static void be_rx_compl_discard(struct be_adapter *adapter, | ||
637 | struct be_eth_rx_compl *rxcp) | ||
638 | { | ||
639 | struct be_queue_info *rxq = &adapter->rx_obj.q; | ||
640 | struct be_rx_page_info *page_info; | ||
641 | u16 rxq_idx, i, num_rcvd; | ||
642 | |||
643 | rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp); | ||
644 | num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp); | ||
645 | |||
646 | for (i = 0; i < num_rcvd; i++) { | ||
647 | page_info = get_rx_page_info(adapter, rxq_idx); | ||
648 | put_page(page_info->page); | ||
649 | memset(page_info, 0, sizeof(*page_info)); | ||
650 | index_inc(&rxq_idx, rxq->len); | ||
651 | } | ||
652 | } | ||
653 | |||
654 | /* | ||
655 | * skb_fill_rx_data forms a complete skb for an ether frame | ||
656 | * indicated by rxcp. | ||
657 | */ | ||
658 | static void skb_fill_rx_data(struct be_adapter *adapter, | ||
659 | struct sk_buff *skb, struct be_eth_rx_compl *rxcp) | ||
660 | { | ||
661 | struct be_queue_info *rxq = &adapter->rx_obj.q; | ||
662 | struct be_rx_page_info *page_info; | ||
663 | u16 rxq_idx, i, num_rcvd; | ||
664 | u32 pktsize, hdr_len, curr_frag_len; | ||
665 | u8 *start; | ||
666 | |||
667 | rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp); | ||
668 | pktsize = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp); | ||
669 | num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp); | ||
670 | |||
671 | page_info = get_rx_page_info(adapter, rxq_idx); | ||
672 | |||
673 | start = page_address(page_info->page) + page_info->page_offset; | ||
674 | prefetch(start); | ||
675 | |||
676 | /* Copy data in the first descriptor of this completion */ | ||
677 | curr_frag_len = min(pktsize, rx_frag_size); | ||
678 | |||
679 | /* Copy the header portion into skb_data */ | ||
680 | hdr_len = min((u32)BE_HDR_LEN, curr_frag_len); | ||
681 | memcpy(skb->data, start, hdr_len); | ||
682 | skb->len = curr_frag_len; | ||
683 | if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */ | ||
684 | /* Complete packet has now been moved to data */ | ||
685 | put_page(page_info->page); | ||
686 | skb->data_len = 0; | ||
687 | skb->tail += curr_frag_len; | ||
688 | } else { | ||
689 | skb_shinfo(skb)->nr_frags = 1; | ||
690 | skb_shinfo(skb)->frags[0].page = page_info->page; | ||
691 | skb_shinfo(skb)->frags[0].page_offset = | ||
692 | page_info->page_offset + hdr_len; | ||
693 | skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len; | ||
694 | skb->data_len = curr_frag_len - hdr_len; | ||
695 | skb->tail += hdr_len; | ||
696 | } | ||
697 | memset(page_info, 0, sizeof(*page_info)); | ||
698 | |||
699 | if (pktsize <= rx_frag_size) { | ||
700 | BUG_ON(num_rcvd != 1); | ||
701 | return; | ||
702 | } | ||
703 | |||
704 | /* More frags present for this completion */ | ||
705 | pktsize -= curr_frag_len; /* account for above copied frag */ | ||
706 | for (i = 1; i < num_rcvd; i++) { | ||
707 | index_inc(&rxq_idx, rxq->len); | ||
708 | page_info = get_rx_page_info(adapter, rxq_idx); | ||
709 | |||
710 | curr_frag_len = min(pktsize, rx_frag_size); | ||
711 | |||
712 | skb_shinfo(skb)->frags[i].page = page_info->page; | ||
713 | skb_shinfo(skb)->frags[i].page_offset = page_info->page_offset; | ||
714 | skb_shinfo(skb)->frags[i].size = curr_frag_len; | ||
715 | skb->len += curr_frag_len; | ||
716 | skb->data_len += curr_frag_len; | ||
717 | skb_shinfo(skb)->nr_frags++; | ||
718 | pktsize -= curr_frag_len; | ||
719 | |||
720 | memset(page_info, 0, sizeof(*page_info)); | ||
721 | } | ||
722 | |||
723 | be_rx_rate_update(adapter, pktsize, num_rcvd); | ||
724 | return; | ||
725 | } | ||
726 | |||
727 | /* Process the RX completion indicated by rxcp when LRO is disabled */ | ||
728 | static void be_rx_compl_process(struct be_adapter *adapter, | ||
729 | struct be_eth_rx_compl *rxcp) | ||
730 | { | ||
731 | struct sk_buff *skb; | ||
732 | u32 vtp, vid; | ||
733 | int l4_cksm; | ||
734 | |||
735 | l4_cksm = AMAP_GET_BITS(struct amap_eth_rx_compl, l4_cksm, rxcp); | ||
736 | vtp = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp); | ||
737 | |||
738 | skb = netdev_alloc_skb(adapter->netdev, BE_HDR_LEN + NET_IP_ALIGN); | ||
739 | if (!skb) { | ||
740 | if (net_ratelimit()) | ||
741 | dev_warn(&adapter->pdev->dev, "skb alloc failed\n"); | ||
742 | be_rx_compl_discard(adapter, rxcp); | ||
743 | return; | ||
744 | } | ||
745 | |||
746 | skb_reserve(skb, NET_IP_ALIGN); | ||
747 | |||
748 | skb_fill_rx_data(adapter, skb, rxcp); | ||
749 | |||
750 | if (l4_cksm && adapter->rx_csum) | ||
751 | skb->ip_summed = CHECKSUM_UNNECESSARY; | ||
752 | else | ||
753 | skb->ip_summed = CHECKSUM_NONE; | ||
754 | |||
755 | skb->truesize = skb->len + sizeof(struct sk_buff); | ||
756 | skb->protocol = eth_type_trans(skb, adapter->netdev); | ||
757 | skb->dev = adapter->netdev; | ||
758 | |||
759 | if (vtp) { | ||
760 | if (!adapter->vlan_grp || adapter->num_vlans == 0) { | ||
761 | kfree_skb(skb); | ||
762 | return; | ||
763 | } | ||
764 | vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp); | ||
765 | vid = be16_to_cpu(vid); | ||
766 | vlan_hwaccel_receive_skb(skb, adapter->vlan_grp, vid); | ||
767 | } else { | ||
768 | netif_receive_skb(skb); | ||
769 | } | ||
770 | |||
771 | adapter->netdev->last_rx = jiffies; | ||
772 | |||
773 | return; | ||
774 | } | ||
775 | |||
776 | /* Process the RX completion indicated by rxcp when LRO is enabled */ | ||
777 | static void be_rx_compl_process_lro(struct be_adapter *adapter, | ||
778 | struct be_eth_rx_compl *rxcp) | ||
779 | { | ||
780 | struct be_rx_page_info *page_info; | ||
781 | struct skb_frag_struct rx_frags[BE_MAX_FRAGS_PER_FRAME]; | ||
782 | struct be_queue_info *rxq = &adapter->rx_obj.q; | ||
783 | u32 num_rcvd, pkt_size, remaining, vlanf, curr_frag_len; | ||
784 | u16 i, rxq_idx = 0, vid; | ||
785 | |||
786 | num_rcvd = AMAP_GET_BITS(struct amap_eth_rx_compl, numfrags, rxcp); | ||
787 | pkt_size = AMAP_GET_BITS(struct amap_eth_rx_compl, pktsize, rxcp); | ||
788 | vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl, vtp, rxcp); | ||
789 | rxq_idx = AMAP_GET_BITS(struct amap_eth_rx_compl, fragndx, rxcp); | ||
790 | |||
791 | remaining = pkt_size; | ||
792 | for (i = 0; i < num_rcvd; i++) { | ||
793 | page_info = get_rx_page_info(adapter, rxq_idx); | ||
794 | |||
795 | curr_frag_len = min(remaining, rx_frag_size); | ||
796 | |||
797 | rx_frags[i].page = page_info->page; | ||
798 | rx_frags[i].page_offset = page_info->page_offset; | ||
799 | rx_frags[i].size = curr_frag_len; | ||
800 | remaining -= curr_frag_len; | ||
801 | |||
802 | index_inc(&rxq_idx, rxq->len); | ||
803 | |||
804 | memset(page_info, 0, sizeof(*page_info)); | ||
805 | } | ||
806 | |||
807 | if (likely(!vlanf)) { | ||
808 | lro_receive_frags(&adapter->rx_obj.lro_mgr, rx_frags, pkt_size, | ||
809 | pkt_size, NULL, 0); | ||
810 | } else { | ||
811 | vid = AMAP_GET_BITS(struct amap_eth_rx_compl, vlan_tag, rxcp); | ||
812 | vid = be16_to_cpu(vid); | ||
813 | |||
814 | if (!adapter->vlan_grp || adapter->num_vlans == 0) | ||
815 | return; | ||
816 | |||
817 | lro_vlan_hwaccel_receive_frags(&adapter->rx_obj.lro_mgr, | ||
818 | rx_frags, pkt_size, pkt_size, adapter->vlan_grp, | ||
819 | vid, NULL, 0); | ||
820 | } | ||
821 | |||
822 | be_rx_rate_update(adapter, pkt_size, num_rcvd); | ||
823 | return; | ||
824 | } | ||
825 | |||
826 | static struct be_eth_rx_compl *be_rx_compl_get(struct be_adapter *adapter) | ||
827 | { | ||
828 | struct be_eth_rx_compl *rxcp = queue_tail_node(&adapter->rx_obj.cq); | ||
829 | |||
830 | if (rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] == 0) | ||
831 | return NULL; | ||
832 | |||
833 | be_dws_le_to_cpu(rxcp, sizeof(*rxcp)); | ||
834 | |||
835 | rxcp->dw[offsetof(struct amap_eth_rx_compl, valid) / 32] = 0; | ||
836 | |||
837 | queue_tail_inc(&adapter->rx_obj.cq); | ||
838 | return rxcp; | ||
839 | } | ||
840 | |||
841 | static inline struct page *be_alloc_pages(u32 size) | ||
842 | { | ||
843 | gfp_t alloc_flags = GFP_ATOMIC; | ||
844 | u32 order = get_order(size); | ||
845 | if (order > 0) | ||
846 | alloc_flags |= __GFP_COMP; | ||
847 | return alloc_pages(alloc_flags, order); | ||
848 | } | ||
849 | |||
850 | /* | ||
851 | * Allocate a page, split it to fragments of size rx_frag_size and post as | ||
852 | * receive buffers to BE | ||
853 | */ | ||
854 | static void be_post_rx_frags(struct be_adapter *adapter) | ||
855 | { | ||
856 | struct be_rx_page_info *page_info_tbl = adapter->rx_obj.page_info_tbl; | ||
857 | struct be_rx_page_info *page_info = NULL; | ||
858 | struct be_queue_info *rxq = &adapter->rx_obj.q; | ||
859 | struct page *pagep = NULL; | ||
860 | struct be_eth_rx_d *rxd; | ||
861 | u64 page_dmaaddr = 0, frag_dmaaddr; | ||
862 | u32 posted, page_offset = 0; | ||
863 | |||
864 | |||
865 | page_info = &page_info_tbl[rxq->head]; | ||
866 | for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) { | ||
867 | if (!pagep) { | ||
868 | pagep = be_alloc_pages(adapter->big_page_size); | ||
869 | if (unlikely(!pagep)) { | ||
870 | drvr_stats(adapter)->be_ethrx_post_fail++; | ||
871 | break; | ||
872 | } | ||
873 | page_dmaaddr = pci_map_page(adapter->pdev, pagep, 0, | ||
874 | adapter->big_page_size, | ||
875 | PCI_DMA_FROMDEVICE); | ||
876 | page_info->page_offset = 0; | ||
877 | } else { | ||
878 | get_page(pagep); | ||
879 | page_info->page_offset = page_offset + rx_frag_size; | ||
880 | } | ||
881 | page_offset = page_info->page_offset; | ||
882 | page_info->page = pagep; | ||
883 | pci_unmap_addr_set(page_info, bus, page_dmaaddr); | ||
884 | frag_dmaaddr = page_dmaaddr + page_info->page_offset; | ||
885 | |||
886 | rxd = queue_head_node(rxq); | ||
887 | rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF); | ||
888 | rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr)); | ||
889 | queue_head_inc(rxq); | ||
890 | |||
891 | /* Any space left in the current big page for another frag? */ | ||
892 | if ((page_offset + rx_frag_size + rx_frag_size) > | ||
893 | adapter->big_page_size) { | ||
894 | pagep = NULL; | ||
895 | page_info->last_page_user = true; | ||
896 | } | ||
897 | page_info = &page_info_tbl[rxq->head]; | ||
898 | } | ||
899 | if (pagep) | ||
900 | page_info->last_page_user = true; | ||
901 | |||
902 | if (posted) { | ||
903 | be_rxq_notify(&adapter->ctrl, rxq->id, posted); | ||
904 | atomic_add(posted, &rxq->used); | ||
905 | } | ||
906 | |||
907 | return; | ||
908 | } | ||
909 | |||
910 | static struct be_eth_tx_compl * | ||
911 | be_tx_compl_get(struct be_adapter *adapter) | ||
912 | { | ||
913 | struct be_queue_info *tx_cq = &adapter->tx_obj.cq; | ||
914 | struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq); | ||
915 | |||
916 | if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0) | ||
917 | return NULL; | ||
918 | |||
919 | be_dws_le_to_cpu(txcp, sizeof(*txcp)); | ||
920 | |||
921 | txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0; | ||
922 | |||
923 | queue_tail_inc(tx_cq); | ||
924 | return txcp; | ||
925 | } | ||
926 | |||
927 | static void be_tx_compl_process(struct be_adapter *adapter, u16 last_index) | ||
928 | { | ||
929 | struct be_queue_info *txq = &adapter->tx_obj.q; | ||
930 | struct be_eth_wrb *wrb; | ||
931 | struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list; | ||
932 | struct sk_buff *sent_skb; | ||
933 | u64 busaddr; | ||
934 | u16 cur_index, num_wrbs = 0; | ||
935 | |||
936 | cur_index = txq->tail; | ||
937 | sent_skb = sent_skbs[cur_index]; | ||
938 | BUG_ON(!sent_skb); | ||
939 | sent_skbs[cur_index] = NULL; | ||
940 | |||
941 | do { | ||
942 | cur_index = txq->tail; | ||
943 | wrb = queue_tail_node(txq); | ||
944 | be_dws_le_to_cpu(wrb, sizeof(*wrb)); | ||
945 | busaddr = ((u64)wrb->frag_pa_hi << 32) | (u64)wrb->frag_pa_lo; | ||
946 | if (busaddr != 0) { | ||
947 | pci_unmap_single(adapter->pdev, busaddr, | ||
948 | wrb->frag_len, PCI_DMA_TODEVICE); | ||
949 | } | ||
950 | num_wrbs++; | ||
951 | queue_tail_inc(txq); | ||
952 | } while (cur_index != last_index); | ||
953 | |||
954 | atomic_sub(num_wrbs, &txq->used); | ||
955 | |||
956 | kfree_skb(sent_skb); | ||
957 | } | ||
958 | |||
959 | static void be_rx_q_clean(struct be_adapter *adapter) | ||
960 | { | ||
961 | struct be_rx_page_info *page_info; | ||
962 | struct be_queue_info *rxq = &adapter->rx_obj.q; | ||
963 | struct be_queue_info *rx_cq = &adapter->rx_obj.cq; | ||
964 | struct be_eth_rx_compl *rxcp; | ||
965 | u16 tail; | ||
966 | |||
967 | /* First cleanup pending rx completions */ | ||
968 | while ((rxcp = be_rx_compl_get(adapter)) != NULL) { | ||
969 | be_rx_compl_discard(adapter, rxcp); | ||
970 | be_cq_notify(&adapter->ctrl, rx_cq->id, true, 1); | ||
971 | } | ||
972 | |||
973 | /* Then free posted rx buffer that were not used */ | ||
974 | tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len; | ||
975 | for (; tail != rxq->head; index_inc(&tail, rxq->len)) { | ||
976 | page_info = get_rx_page_info(adapter, tail); | ||
977 | put_page(page_info->page); | ||
978 | memset(page_info, 0, sizeof(*page_info)); | ||
979 | } | ||
980 | BUG_ON(atomic_read(&rxq->used)); | ||
981 | } | ||
982 | |||
983 | static void be_tx_q_clean(struct be_adapter *adapter) | ||
984 | { | ||
985 | struct sk_buff **sent_skbs = adapter->tx_obj.sent_skb_list; | ||
986 | struct sk_buff *sent_skb; | ||
987 | struct be_queue_info *txq = &adapter->tx_obj.q; | ||
988 | u16 last_index; | ||
989 | bool dummy_wrb; | ||
990 | |||
991 | while (atomic_read(&txq->used)) { | ||
992 | sent_skb = sent_skbs[txq->tail]; | ||
993 | last_index = txq->tail; | ||
994 | index_adv(&last_index, | ||
995 | wrb_cnt_for_skb(sent_skb, &dummy_wrb) - 1, txq->len); | ||
996 | be_tx_compl_process(adapter, last_index); | ||
997 | } | ||
998 | } | ||
999 | |||
1000 | static void be_tx_queues_destroy(struct be_adapter *adapter) | ||
1001 | { | ||
1002 | struct be_queue_info *q; | ||
1003 | |||
1004 | q = &adapter->tx_obj.q; | ||
1005 | if (q->created) | ||
1006 | be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_TXQ); | ||
1007 | be_queue_free(adapter, q); | ||
1008 | |||
1009 | q = &adapter->tx_obj.cq; | ||
1010 | if (q->created) | ||
1011 | be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_CQ); | ||
1012 | be_queue_free(adapter, q); | ||
1013 | |||
1014 | /* No more tx completions can be rcvd now; clean up if there are | ||
1015 | * any pending completions or pending tx requests */ | ||
1016 | be_tx_q_clean(adapter); | ||
1017 | |||
1018 | q = &adapter->tx_eq.q; | ||
1019 | if (q->created) | ||
1020 | be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_EQ); | ||
1021 | be_queue_free(adapter, q); | ||
1022 | } | ||
1023 | |||
1024 | static int be_tx_queues_create(struct be_adapter *adapter) | ||
1025 | { | ||
1026 | struct be_queue_info *eq, *q, *cq; | ||
1027 | |||
1028 | adapter->tx_eq.max_eqd = 0; | ||
1029 | adapter->tx_eq.min_eqd = 0; | ||
1030 | adapter->tx_eq.cur_eqd = 96; | ||
1031 | adapter->tx_eq.enable_aic = false; | ||
1032 | /* Alloc Tx Event queue */ | ||
1033 | eq = &adapter->tx_eq.q; | ||
1034 | if (be_queue_alloc(adapter, eq, EVNT_Q_LEN, sizeof(struct be_eq_entry))) | ||
1035 | return -1; | ||
1036 | |||
1037 | /* Ask BE to create Tx Event queue */ | ||
1038 | if (be_cmd_eq_create(&adapter->ctrl, eq, adapter->tx_eq.cur_eqd)) | ||
1039 | goto tx_eq_free; | ||
1040 | /* Alloc TX eth compl queue */ | ||
1041 | cq = &adapter->tx_obj.cq; | ||
1042 | if (be_queue_alloc(adapter, cq, TX_CQ_LEN, | ||
1043 | sizeof(struct be_eth_tx_compl))) | ||
1044 | goto tx_eq_destroy; | ||
1045 | |||
1046 | /* Ask BE to create Tx eth compl queue */ | ||
1047 | if (be_cmd_cq_create(&adapter->ctrl, cq, eq, false, false, 3)) | ||
1048 | goto tx_cq_free; | ||
1049 | |||
1050 | /* Alloc TX eth queue */ | ||
1051 | q = &adapter->tx_obj.q; | ||
1052 | if (be_queue_alloc(adapter, q, TX_Q_LEN, sizeof(struct be_eth_wrb))) | ||
1053 | goto tx_cq_destroy; | ||
1054 | |||
1055 | /* Ask BE to create Tx eth queue */ | ||
1056 | if (be_cmd_txq_create(&adapter->ctrl, q, cq)) | ||
1057 | goto tx_q_free; | ||
1058 | return 0; | ||
1059 | |||
1060 | tx_q_free: | ||
1061 | be_queue_free(adapter, q); | ||
1062 | tx_cq_destroy: | ||
1063 | be_cmd_q_destroy(&adapter->ctrl, cq, QTYPE_CQ); | ||
1064 | tx_cq_free: | ||
1065 | be_queue_free(adapter, cq); | ||
1066 | tx_eq_destroy: | ||
1067 | be_cmd_q_destroy(&adapter->ctrl, eq, QTYPE_EQ); | ||
1068 | tx_eq_free: | ||
1069 | be_queue_free(adapter, eq); | ||
1070 | return -1; | ||
1071 | } | ||
1072 | |||
1073 | static void be_rx_queues_destroy(struct be_adapter *adapter) | ||
1074 | { | ||
1075 | struct be_queue_info *q; | ||
1076 | |||
1077 | q = &adapter->rx_obj.q; | ||
1078 | if (q->created) { | ||
1079 | be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_RXQ); | ||
1080 | be_rx_q_clean(adapter); | ||
1081 | } | ||
1082 | be_queue_free(adapter, q); | ||
1083 | |||
1084 | q = &adapter->rx_obj.cq; | ||
1085 | if (q->created) | ||
1086 | be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_CQ); | ||
1087 | be_queue_free(adapter, q); | ||
1088 | |||
1089 | q = &adapter->rx_eq.q; | ||
1090 | if (q->created) | ||
1091 | be_cmd_q_destroy(&adapter->ctrl, q, QTYPE_EQ); | ||
1092 | be_queue_free(adapter, q); | ||
1093 | } | ||
1094 | |||
1095 | static int be_rx_queues_create(struct be_adapter *adapter) | ||
1096 | { | ||
1097 | struct be_queue_info *eq, *q, *cq; | ||
1098 | int rc; | ||
1099 | |||
1100 | adapter->max_rx_coal = BE_MAX_FRAGS_PER_FRAME; | ||
1101 | adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE; | ||
1102 | adapter->rx_eq.max_eqd = BE_MAX_EQD; | ||
1103 | adapter->rx_eq.min_eqd = 0; | ||
1104 | adapter->rx_eq.cur_eqd = 0; | ||
1105 | adapter->rx_eq.enable_aic = true; | ||
1106 | |||
1107 | /* Alloc Rx Event queue */ | ||
1108 | eq = &adapter->rx_eq.q; | ||
1109 | rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN, | ||
1110 | sizeof(struct be_eq_entry)); | ||
1111 | if (rc) | ||
1112 | return rc; | ||
1113 | |||
1114 | /* Ask BE to create Rx Event queue */ | ||
1115 | rc = be_cmd_eq_create(&adapter->ctrl, eq, adapter->rx_eq.cur_eqd); | ||
1116 | if (rc) | ||
1117 | goto rx_eq_free; | ||
1118 | |||
1119 | /* Alloc RX eth compl queue */ | ||
1120 | cq = &adapter->rx_obj.cq; | ||
1121 | rc = be_queue_alloc(adapter, cq, RX_CQ_LEN, | ||
1122 | sizeof(struct be_eth_rx_compl)); | ||
1123 | if (rc) | ||
1124 | goto rx_eq_destroy; | ||
1125 | |||
1126 | /* Ask BE to create Rx eth compl queue */ | ||
1127 | rc = be_cmd_cq_create(&adapter->ctrl, cq, eq, false, false, 3); | ||
1128 | if (rc) | ||
1129 | goto rx_cq_free; | ||
1130 | |||
1131 | /* Alloc RX eth queue */ | ||
1132 | q = &adapter->rx_obj.q; | ||
1133 | rc = be_queue_alloc(adapter, q, RX_Q_LEN, sizeof(struct be_eth_rx_d)); | ||
1134 | if (rc) | ||
1135 | goto rx_cq_destroy; | ||
1136 | |||
1137 | /* Ask BE to create Rx eth queue */ | ||
1138 | rc = be_cmd_rxq_create(&adapter->ctrl, q, cq->id, rx_frag_size, | ||
1139 | BE_MAX_JUMBO_FRAME_SIZE, adapter->if_handle, false); | ||
1140 | if (rc) | ||
1141 | goto rx_q_free; | ||
1142 | |||
1143 | return 0; | ||
1144 | rx_q_free: | ||
1145 | be_queue_free(adapter, q); | ||
1146 | rx_cq_destroy: | ||
1147 | be_cmd_q_destroy(&adapter->ctrl, cq, QTYPE_CQ); | ||
1148 | rx_cq_free: | ||
1149 | be_queue_free(adapter, cq); | ||
1150 | rx_eq_destroy: | ||
1151 | be_cmd_q_destroy(&adapter->ctrl, eq, QTYPE_EQ); | ||
1152 | rx_eq_free: | ||
1153 | be_queue_free(adapter, eq); | ||
1154 | return rc; | ||
1155 | } | ||
1156 | static bool event_get(struct be_eq_obj *eq_obj, u16 *rid) | ||
1157 | { | ||
1158 | struct be_eq_entry *entry = queue_tail_node(&eq_obj->q); | ||
1159 | u32 evt = entry->evt; | ||
1160 | |||
1161 | if (!evt) | ||
1162 | return false; | ||
1163 | |||
1164 | evt = le32_to_cpu(evt); | ||
1165 | *rid = (evt >> EQ_ENTRY_RES_ID_SHIFT) & EQ_ENTRY_RES_ID_MASK; | ||
1166 | entry->evt = 0; | ||
1167 | queue_tail_inc(&eq_obj->q); | ||
1168 | return true; | ||
1169 | } | ||
1170 | |||
1171 | static int event_handle(struct be_ctrl_info *ctrl, | ||
1172 | struct be_eq_obj *eq_obj) | ||
1173 | { | ||
1174 | u16 rid = 0, num = 0; | ||
1175 | |||
1176 | while (event_get(eq_obj, &rid)) | ||
1177 | num++; | ||
1178 | |||
1179 | /* We can see an interrupt and no event */ | ||
1180 | be_eq_notify(ctrl, eq_obj->q.id, true, true, num); | ||
1181 | if (num) | ||
1182 | napi_schedule(&eq_obj->napi); | ||
1183 | |||
1184 | return num; | ||
1185 | } | ||
1186 | |||
1187 | static irqreturn_t be_intx(int irq, void *dev) | ||
1188 | { | ||
1189 | struct be_adapter *adapter = dev; | ||
1190 | struct be_ctrl_info *ctrl = &adapter->ctrl; | ||
1191 | int rx, tx; | ||
1192 | |||
1193 | tx = event_handle(ctrl, &adapter->tx_eq); | ||
1194 | rx = event_handle(ctrl, &adapter->rx_eq); | ||
1195 | |||
1196 | if (rx || tx) | ||
1197 | return IRQ_HANDLED; | ||
1198 | else | ||
1199 | return IRQ_NONE; | ||
1200 | } | ||
1201 | |||
1202 | static irqreturn_t be_msix_rx(int irq, void *dev) | ||
1203 | { | ||
1204 | struct be_adapter *adapter = dev; | ||
1205 | |||
1206 | event_handle(&adapter->ctrl, &adapter->rx_eq); | ||
1207 | |||
1208 | return IRQ_HANDLED; | ||
1209 | } | ||
1210 | |||
1211 | static irqreturn_t be_msix_tx(int irq, void *dev) | ||
1212 | { | ||
1213 | struct be_adapter *adapter = dev; | ||
1214 | |||
1215 | event_handle(&adapter->ctrl, &adapter->tx_eq); | ||
1216 | |||
1217 | return IRQ_HANDLED; | ||
1218 | } | ||
1219 | |||
1220 | static inline bool do_lro(struct be_adapter *adapter, | ||
1221 | struct be_eth_rx_compl *rxcp) | ||
1222 | { | ||
1223 | int err = AMAP_GET_BITS(struct amap_eth_rx_compl, err, rxcp); | ||
1224 | int tcp_frame = AMAP_GET_BITS(struct amap_eth_rx_compl, tcpf, rxcp); | ||
1225 | |||
1226 | if (err) | ||
1227 | drvr_stats(adapter)->be_rxcp_err++; | ||
1228 | |||
1229 | return (!tcp_frame || err || (adapter->max_rx_coal <= 1)) ? | ||
1230 | false : true; | ||
1231 | } | ||
1232 | |||
1233 | int be_poll_rx(struct napi_struct *napi, int budget) | ||
1234 | { | ||
1235 | struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi); | ||
1236 | struct be_adapter *adapter = | ||
1237 | container_of(rx_eq, struct be_adapter, rx_eq); | ||
1238 | struct be_queue_info *rx_cq = &adapter->rx_obj.cq; | ||
1239 | struct be_eth_rx_compl *rxcp; | ||
1240 | u32 work_done; | ||
1241 | |||
1242 | for (work_done = 0; work_done < budget; work_done++) { | ||
1243 | rxcp = be_rx_compl_get(adapter); | ||
1244 | if (!rxcp) | ||
1245 | break; | ||
1246 | |||
1247 | if (do_lro(adapter, rxcp)) | ||
1248 | be_rx_compl_process_lro(adapter, rxcp); | ||
1249 | else | ||
1250 | be_rx_compl_process(adapter, rxcp); | ||
1251 | } | ||
1252 | |||
1253 | lro_flush_all(&adapter->rx_obj.lro_mgr); | ||
1254 | |||
1255 | /* Refill the queue */ | ||
1256 | if (atomic_read(&adapter->rx_obj.q.used) < RX_FRAGS_REFILL_WM) | ||
1257 | be_post_rx_frags(adapter); | ||
1258 | |||
1259 | /* All consumed */ | ||
1260 | if (work_done < budget) { | ||
1261 | napi_complete(napi); | ||
1262 | be_cq_notify(&adapter->ctrl, rx_cq->id, true, work_done); | ||
1263 | } else { | ||
1264 | /* More to be consumed; continue with interrupts disabled */ | ||
1265 | be_cq_notify(&adapter->ctrl, rx_cq->id, false, work_done); | ||
1266 | } | ||
1267 | return work_done; | ||
1268 | } | ||
1269 | |||
1270 | /* For TX we don't honour budget; consume everything */ | ||
1271 | int be_poll_tx(struct napi_struct *napi, int budget) | ||
1272 | { | ||
1273 | struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi); | ||
1274 | struct be_adapter *adapter = | ||
1275 | container_of(tx_eq, struct be_adapter, tx_eq); | ||
1276 | struct be_tx_obj *tx_obj = &adapter->tx_obj; | ||
1277 | struct be_queue_info *tx_cq = &tx_obj->cq; | ||
1278 | struct be_queue_info *txq = &tx_obj->q; | ||
1279 | struct be_eth_tx_compl *txcp; | ||
1280 | u32 num_cmpl = 0; | ||
1281 | u16 end_idx; | ||
1282 | |||
1283 | while ((txcp = be_tx_compl_get(adapter))) { | ||
1284 | end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl, | ||
1285 | wrb_index, txcp); | ||
1286 | be_tx_compl_process(adapter, end_idx); | ||
1287 | num_cmpl++; | ||
1288 | } | ||
1289 | |||
1290 | /* As Tx wrbs have been freed up, wake up netdev queue if | ||
1291 | * it was stopped due to lack of tx wrbs. | ||
1292 | */ | ||
1293 | if (netif_queue_stopped(adapter->netdev) && | ||
1294 | atomic_read(&txq->used) < txq->len / 2) { | ||
1295 | netif_wake_queue(adapter->netdev); | ||
1296 | } | ||
1297 | |||
1298 | napi_complete(napi); | ||
1299 | |||
1300 | be_cq_notify(&adapter->ctrl, tx_cq->id, true, num_cmpl); | ||
1301 | |||
1302 | drvr_stats(adapter)->be_tx_events++; | ||
1303 | drvr_stats(adapter)->be_tx_compl += num_cmpl; | ||
1304 | |||
1305 | return 1; | ||
1306 | } | ||
1307 | |||
1308 | static void be_msix_enable(struct be_adapter *adapter) | ||
1309 | { | ||
1310 | int i, status; | ||
1311 | |||
1312 | for (i = 0; i < BE_NUM_MSIX_VECTORS; i++) | ||
1313 | adapter->msix_entries[i].entry = i; | ||
1314 | |||
1315 | status = pci_enable_msix(adapter->pdev, adapter->msix_entries, | ||
1316 | BE_NUM_MSIX_VECTORS); | ||
1317 | if (status == 0) | ||
1318 | adapter->msix_enabled = true; | ||
1319 | return; | ||
1320 | } | ||
1321 | |||
1322 | static inline int be_msix_vec_get(struct be_adapter *adapter, u32 eq_id) | ||
1323 | { | ||
1324 | return adapter->msix_entries[eq_id - | ||
1325 | 8 * adapter->ctrl.pci_func].vector; | ||
1326 | } | ||
1327 | |||
1328 | static int be_msix_register(struct be_adapter *adapter) | ||
1329 | { | ||
1330 | struct net_device *netdev = adapter->netdev; | ||
1331 | struct be_eq_obj *tx_eq = &adapter->tx_eq; | ||
1332 | struct be_eq_obj *rx_eq = &adapter->rx_eq; | ||
1333 | int status, vec; | ||
1334 | |||
1335 | sprintf(tx_eq->desc, "%s-tx", netdev->name); | ||
1336 | vec = be_msix_vec_get(adapter, tx_eq->q.id); | ||
1337 | status = request_irq(vec, be_msix_tx, 0, tx_eq->desc, adapter); | ||
1338 | if (status) | ||
1339 | goto err; | ||
1340 | |||
1341 | sprintf(rx_eq->desc, "%s-rx", netdev->name); | ||
1342 | vec = be_msix_vec_get(adapter, rx_eq->q.id); | ||
1343 | status = request_irq(vec, be_msix_rx, 0, rx_eq->desc, adapter); | ||
1344 | if (status) { /* Free TX IRQ */ | ||
1345 | vec = be_msix_vec_get(adapter, tx_eq->q.id); | ||
1346 | free_irq(vec, adapter); | ||
1347 | goto err; | ||
1348 | } | ||
1349 | return 0; | ||
1350 | err: | ||
1351 | dev_warn(&adapter->pdev->dev, | ||
1352 | "MSIX Request IRQ failed - err %d\n", status); | ||
1353 | pci_disable_msix(adapter->pdev); | ||
1354 | adapter->msix_enabled = false; | ||
1355 | return status; | ||
1356 | } | ||
1357 | |||
1358 | static int be_irq_register(struct be_adapter *adapter) | ||
1359 | { | ||
1360 | struct net_device *netdev = adapter->netdev; | ||
1361 | int status; | ||
1362 | |||
1363 | if (adapter->msix_enabled) { | ||
1364 | status = be_msix_register(adapter); | ||
1365 | if (status == 0) | ||
1366 | goto done; | ||
1367 | } | ||
1368 | |||
1369 | /* INTx */ | ||
1370 | netdev->irq = adapter->pdev->irq; | ||
1371 | status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name, | ||
1372 | adapter); | ||
1373 | if (status) { | ||
1374 | dev_err(&adapter->pdev->dev, | ||
1375 | "INTx request IRQ failed - err %d\n", status); | ||
1376 | return status; | ||
1377 | } | ||
1378 | done: | ||
1379 | adapter->isr_registered = true; | ||
1380 | return 0; | ||
1381 | } | ||
1382 | |||
1383 | static void be_irq_unregister(struct be_adapter *adapter) | ||
1384 | { | ||
1385 | struct net_device *netdev = adapter->netdev; | ||
1386 | int vec; | ||
1387 | |||
1388 | if (!adapter->isr_registered) | ||
1389 | return; | ||
1390 | |||
1391 | /* INTx */ | ||
1392 | if (!adapter->msix_enabled) { | ||
1393 | free_irq(netdev->irq, adapter); | ||
1394 | goto done; | ||
1395 | } | ||
1396 | |||
1397 | /* MSIx */ | ||
1398 | vec = be_msix_vec_get(adapter, adapter->tx_eq.q.id); | ||
1399 | free_irq(vec, adapter); | ||
1400 | vec = be_msix_vec_get(adapter, adapter->rx_eq.q.id); | ||
1401 | free_irq(vec, adapter); | ||
1402 | done: | ||
1403 | adapter->isr_registered = false; | ||
1404 | return; | ||
1405 | } | ||
1406 | |||
1407 | static int be_open(struct net_device *netdev) | ||
1408 | { | ||
1409 | struct be_adapter *adapter = netdev_priv(netdev); | ||
1410 | struct be_ctrl_info *ctrl = &adapter->ctrl; | ||
1411 | struct be_eq_obj *rx_eq = &adapter->rx_eq; | ||
1412 | struct be_eq_obj *tx_eq = &adapter->tx_eq; | ||
1413 | u32 if_flags; | ||
1414 | int status; | ||
1415 | |||
1416 | if_flags = BE_IF_FLAGS_BROADCAST | BE_IF_FLAGS_PROMISCUOUS | | ||
1417 | BE_IF_FLAGS_MCAST_PROMISCUOUS | BE_IF_FLAGS_UNTAGGED | | ||
1418 | BE_IF_FLAGS_PASS_L3L4_ERRORS; | ||
1419 | status = be_cmd_if_create(ctrl, if_flags, netdev->dev_addr, | ||
1420 | false/* pmac_invalid */, &adapter->if_handle, | ||
1421 | &adapter->pmac_id); | ||
1422 | if (status != 0) | ||
1423 | goto do_none; | ||
1424 | |||
1425 | status = be_cmd_set_flow_control(ctrl, true, true); | ||
1426 | if (status != 0) | ||
1427 | goto if_destroy; | ||
1428 | |||
1429 | status = be_tx_queues_create(adapter); | ||
1430 | if (status != 0) | ||
1431 | goto if_destroy; | ||
1432 | |||
1433 | status = be_rx_queues_create(adapter); | ||
1434 | if (status != 0) | ||
1435 | goto tx_qs_destroy; | ||
1436 | |||
1437 | /* First time posting */ | ||
1438 | be_post_rx_frags(adapter); | ||
1439 | |||
1440 | napi_enable(&rx_eq->napi); | ||
1441 | napi_enable(&tx_eq->napi); | ||
1442 | |||
1443 | be_irq_register(adapter); | ||
1444 | |||
1445 | be_intr_set(ctrl, true); | ||
1446 | |||
1447 | /* The evt queues are created in the unarmed state; arm them */ | ||
1448 | be_eq_notify(ctrl, rx_eq->q.id, true, false, 0); | ||
1449 | be_eq_notify(ctrl, tx_eq->q.id, true, false, 0); | ||
1450 | |||
1451 | /* The compl queues are created in the unarmed state; arm them */ | ||
1452 | be_cq_notify(ctrl, adapter->rx_obj.cq.id, true, 0); | ||
1453 | be_cq_notify(ctrl, adapter->tx_obj.cq.id, true, 0); | ||
1454 | |||
1455 | be_link_status_update(adapter); | ||
1456 | |||
1457 | schedule_delayed_work(&adapter->work, msecs_to_jiffies(100)); | ||
1458 | return 0; | ||
1459 | |||
1460 | tx_qs_destroy: | ||
1461 | be_tx_queues_destroy(adapter); | ||
1462 | if_destroy: | ||
1463 | be_cmd_if_destroy(ctrl, adapter->if_handle); | ||
1464 | do_none: | ||
1465 | return status; | ||
1466 | } | ||
1467 | |||
1468 | static int be_close(struct net_device *netdev) | ||
1469 | { | ||
1470 | struct be_adapter *adapter = netdev_priv(netdev); | ||
1471 | struct be_ctrl_info *ctrl = &adapter->ctrl; | ||
1472 | struct be_eq_obj *rx_eq = &adapter->rx_eq; | ||
1473 | struct be_eq_obj *tx_eq = &adapter->tx_eq; | ||
1474 | int vec; | ||
1475 | |||
1476 | cancel_delayed_work(&adapter->work); | ||
1477 | |||
1478 | netif_stop_queue(netdev); | ||
1479 | netif_carrier_off(netdev); | ||
1480 | adapter->link.speed = PHY_LINK_SPEED_ZERO; | ||
1481 | |||
1482 | be_intr_set(ctrl, false); | ||
1483 | |||
1484 | if (adapter->msix_enabled) { | ||
1485 | vec = be_msix_vec_get(adapter, tx_eq->q.id); | ||
1486 | synchronize_irq(vec); | ||
1487 | vec = be_msix_vec_get(adapter, rx_eq->q.id); | ||
1488 | synchronize_irq(vec); | ||
1489 | } else { | ||
1490 | synchronize_irq(netdev->irq); | ||
1491 | } | ||
1492 | be_irq_unregister(adapter); | ||
1493 | |||
1494 | napi_disable(&rx_eq->napi); | ||
1495 | napi_disable(&tx_eq->napi); | ||
1496 | |||
1497 | be_rx_queues_destroy(adapter); | ||
1498 | be_tx_queues_destroy(adapter); | ||
1499 | |||
1500 | be_cmd_if_destroy(ctrl, adapter->if_handle); | ||
1501 | return 0; | ||
1502 | } | ||
1503 | |||
1504 | static int be_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr, | ||
1505 | void **ip_hdr, void **tcpudp_hdr, | ||
1506 | u64 *hdr_flags, void *priv) | ||
1507 | { | ||
1508 | struct ethhdr *eh; | ||
1509 | struct vlan_ethhdr *veh; | ||
1510 | struct iphdr *iph; | ||
1511 | u8 *va = page_address(frag->page) + frag->page_offset; | ||
1512 | unsigned long ll_hlen; | ||
1513 | |||
1514 | prefetch(va); | ||
1515 | eh = (struct ethhdr *)va; | ||
1516 | *mac_hdr = eh; | ||
1517 | ll_hlen = ETH_HLEN; | ||
1518 | if (eh->h_proto != htons(ETH_P_IP)) { | ||
1519 | if (eh->h_proto == htons(ETH_P_8021Q)) { | ||
1520 | veh = (struct vlan_ethhdr *)va; | ||
1521 | if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP)) | ||
1522 | return -1; | ||
1523 | |||
1524 | ll_hlen += VLAN_HLEN; | ||
1525 | } else { | ||
1526 | return -1; | ||
1527 | } | ||
1528 | } | ||
1529 | *hdr_flags = LRO_IPV4; | ||
1530 | iph = (struct iphdr *)(va + ll_hlen); | ||
1531 | *ip_hdr = iph; | ||
1532 | if (iph->protocol != IPPROTO_TCP) | ||
1533 | return -1; | ||
1534 | *hdr_flags |= LRO_TCP; | ||
1535 | *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2); | ||
1536 | |||
1537 | return 0; | ||
1538 | } | ||
1539 | |||
1540 | static void be_lro_init(struct be_adapter *adapter, struct net_device *netdev) | ||
1541 | { | ||
1542 | struct net_lro_mgr *lro_mgr; | ||
1543 | |||
1544 | lro_mgr = &adapter->rx_obj.lro_mgr; | ||
1545 | lro_mgr->dev = netdev; | ||
1546 | lro_mgr->features = LRO_F_NAPI; | ||
1547 | lro_mgr->ip_summed = CHECKSUM_UNNECESSARY; | ||
1548 | lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY; | ||
1549 | lro_mgr->max_desc = BE_MAX_LRO_DESCRIPTORS; | ||
1550 | lro_mgr->lro_arr = adapter->rx_obj.lro_desc; | ||
1551 | lro_mgr->get_frag_header = be_get_frag_header; | ||
1552 | lro_mgr->max_aggr = BE_MAX_FRAGS_PER_FRAME; | ||
1553 | } | ||
1554 | |||
1555 | static struct net_device_ops be_netdev_ops = { | ||
1556 | .ndo_open = be_open, | ||
1557 | .ndo_stop = be_close, | ||
1558 | .ndo_start_xmit = be_xmit, | ||
1559 | .ndo_get_stats = be_get_stats, | ||
1560 | .ndo_set_rx_mode = be_set_multicast_list, | ||
1561 | .ndo_set_mac_address = be_mac_addr_set, | ||
1562 | .ndo_change_mtu = be_change_mtu, | ||
1563 | .ndo_validate_addr = eth_validate_addr, | ||
1564 | .ndo_vlan_rx_register = be_vlan_register, | ||
1565 | .ndo_vlan_rx_add_vid = be_vlan_add_vid, | ||
1566 | .ndo_vlan_rx_kill_vid = be_vlan_rem_vid, | ||
1567 | }; | ||
1568 | |||
1569 | static void be_netdev_init(struct net_device *netdev) | ||
1570 | { | ||
1571 | struct be_adapter *adapter = netdev_priv(netdev); | ||
1572 | |||
1573 | netdev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_RX | NETIF_F_TSO | | ||
1574 | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER | NETIF_F_IP_CSUM | | ||
1575 | NETIF_F_IPV6_CSUM | NETIF_F_TSO6; | ||
1576 | |||
1577 | netdev->flags |= IFF_MULTICAST; | ||
1578 | |||
1579 | BE_SET_NETDEV_OPS(netdev, &be_netdev_ops); | ||
1580 | |||
1581 | SET_ETHTOOL_OPS(netdev, &be_ethtool_ops); | ||
1582 | |||
1583 | be_lro_init(adapter, netdev); | ||
1584 | |||
1585 | netif_napi_add(netdev, &adapter->rx_eq.napi, be_poll_rx, | ||
1586 | BE_NAPI_WEIGHT); | ||
1587 | netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx, | ||
1588 | BE_NAPI_WEIGHT); | ||
1589 | |||
1590 | netif_carrier_off(netdev); | ||
1591 | netif_stop_queue(netdev); | ||
1592 | } | ||
1593 | |||
1594 | static void be_unmap_pci_bars(struct be_adapter *adapter) | ||
1595 | { | ||
1596 | struct be_ctrl_info *ctrl = &adapter->ctrl; | ||
1597 | if (ctrl->csr) | ||
1598 | iounmap(ctrl->csr); | ||
1599 | if (ctrl->db) | ||
1600 | iounmap(ctrl->db); | ||
1601 | if (ctrl->pcicfg) | ||
1602 | iounmap(ctrl->pcicfg); | ||
1603 | } | ||
1604 | |||
1605 | static int be_map_pci_bars(struct be_adapter *adapter) | ||
1606 | { | ||
1607 | u8 __iomem *addr; | ||
1608 | |||
1609 | addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2), | ||
1610 | pci_resource_len(adapter->pdev, 2)); | ||
1611 | if (addr == NULL) | ||
1612 | return -ENOMEM; | ||
1613 | adapter->ctrl.csr = addr; | ||
1614 | |||
1615 | addr = ioremap_nocache(pci_resource_start(adapter->pdev, 4), | ||
1616 | 128 * 1024); | ||
1617 | if (addr == NULL) | ||
1618 | goto pci_map_err; | ||
1619 | adapter->ctrl.db = addr; | ||
1620 | |||
1621 | addr = ioremap_nocache(pci_resource_start(adapter->pdev, 1), | ||
1622 | pci_resource_len(adapter->pdev, 1)); | ||
1623 | if (addr == NULL) | ||
1624 | goto pci_map_err; | ||
1625 | adapter->ctrl.pcicfg = addr; | ||
1626 | |||
1627 | return 0; | ||
1628 | pci_map_err: | ||
1629 | be_unmap_pci_bars(adapter); | ||
1630 | return -ENOMEM; | ||
1631 | } | ||
1632 | |||
1633 | |||
1634 | static void be_ctrl_cleanup(struct be_adapter *adapter) | ||
1635 | { | ||
1636 | struct be_dma_mem *mem = &adapter->ctrl.mbox_mem_alloced; | ||
1637 | |||
1638 | be_unmap_pci_bars(adapter); | ||
1639 | |||
1640 | if (mem->va) | ||
1641 | pci_free_consistent(adapter->pdev, mem->size, | ||
1642 | mem->va, mem->dma); | ||
1643 | } | ||
1644 | |||
1645 | /* Initialize the mbox required to send cmds to BE */ | ||
1646 | static int be_ctrl_init(struct be_adapter *adapter) | ||
1647 | { | ||
1648 | struct be_ctrl_info *ctrl = &adapter->ctrl; | ||
1649 | struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced; | ||
1650 | struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem; | ||
1651 | int status; | ||
1652 | u32 val; | ||
1653 | |||
1654 | status = be_map_pci_bars(adapter); | ||
1655 | if (status) | ||
1656 | return status; | ||
1657 | |||
1658 | mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16; | ||
1659 | mbox_mem_alloc->va = pci_alloc_consistent(adapter->pdev, | ||
1660 | mbox_mem_alloc->size, &mbox_mem_alloc->dma); | ||
1661 | if (!mbox_mem_alloc->va) { | ||
1662 | be_unmap_pci_bars(adapter); | ||
1663 | return -1; | ||
1664 | } | ||
1665 | mbox_mem_align->size = sizeof(struct be_mcc_mailbox); | ||
1666 | mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16); | ||
1667 | mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16); | ||
1668 | memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox)); | ||
1669 | spin_lock_init(&ctrl->cmd_lock); | ||
1670 | |||
1671 | val = ioread32(ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET); | ||
1672 | ctrl->pci_func = (val >> MEMBAR_CTRL_INT_CTRL_PFUNC_SHIFT) & | ||
1673 | MEMBAR_CTRL_INT_CTRL_PFUNC_MASK; | ||
1674 | return 0; | ||
1675 | } | ||
1676 | |||
1677 | static void be_stats_cleanup(struct be_adapter *adapter) | ||
1678 | { | ||
1679 | struct be_stats_obj *stats = &adapter->stats; | ||
1680 | struct be_dma_mem *cmd = &stats->cmd; | ||
1681 | |||
1682 | if (cmd->va) | ||
1683 | pci_free_consistent(adapter->pdev, cmd->size, | ||
1684 | cmd->va, cmd->dma); | ||
1685 | } | ||
1686 | |||
1687 | static int be_stats_init(struct be_adapter *adapter) | ||
1688 | { | ||
1689 | struct be_stats_obj *stats = &adapter->stats; | ||
1690 | struct be_dma_mem *cmd = &stats->cmd; | ||
1691 | |||
1692 | cmd->size = sizeof(struct be_cmd_req_get_stats); | ||
1693 | cmd->va = pci_alloc_consistent(adapter->pdev, cmd->size, &cmd->dma); | ||
1694 | if (cmd->va == NULL) | ||
1695 | return -1; | ||
1696 | return 0; | ||
1697 | } | ||
1698 | |||
1699 | static void __devexit be_remove(struct pci_dev *pdev) | ||
1700 | { | ||
1701 | struct be_adapter *adapter = pci_get_drvdata(pdev); | ||
1702 | if (!adapter) | ||
1703 | return; | ||
1704 | |||
1705 | unregister_netdev(adapter->netdev); | ||
1706 | |||
1707 | be_stats_cleanup(adapter); | ||
1708 | |||
1709 | be_ctrl_cleanup(adapter); | ||
1710 | |||
1711 | if (adapter->msix_enabled) { | ||
1712 | pci_disable_msix(adapter->pdev); | ||
1713 | adapter->msix_enabled = false; | ||
1714 | } | ||
1715 | |||
1716 | pci_set_drvdata(pdev, NULL); | ||
1717 | pci_release_regions(pdev); | ||
1718 | pci_disable_device(pdev); | ||
1719 | |||
1720 | free_netdev(adapter->netdev); | ||
1721 | } | ||
1722 | |||
1723 | static int be_hw_up(struct be_adapter *adapter) | ||
1724 | { | ||
1725 | struct be_ctrl_info *ctrl = &adapter->ctrl; | ||
1726 | int status; | ||
1727 | |||
1728 | status = be_cmd_POST(ctrl); | ||
1729 | if (status) | ||
1730 | return status; | ||
1731 | |||
1732 | status = be_cmd_get_fw_ver(ctrl, adapter->fw_ver); | ||
1733 | if (status) | ||
1734 | return status; | ||
1735 | |||
1736 | status = be_cmd_query_fw_cfg(ctrl, &adapter->port_num); | ||
1737 | return status; | ||
1738 | } | ||
1739 | |||
1740 | static int __devinit be_probe(struct pci_dev *pdev, | ||
1741 | const struct pci_device_id *pdev_id) | ||
1742 | { | ||
1743 | int status = 0; | ||
1744 | struct be_adapter *adapter; | ||
1745 | struct net_device *netdev; | ||
1746 | struct be_ctrl_info *ctrl; | ||
1747 | u8 mac[ETH_ALEN]; | ||
1748 | |||
1749 | status = pci_enable_device(pdev); | ||
1750 | if (status) | ||
1751 | goto do_none; | ||
1752 | |||
1753 | status = pci_request_regions(pdev, DRV_NAME); | ||
1754 | if (status) | ||
1755 | goto disable_dev; | ||
1756 | pci_set_master(pdev); | ||
1757 | |||
1758 | netdev = alloc_etherdev(sizeof(struct be_adapter)); | ||
1759 | if (netdev == NULL) { | ||
1760 | status = -ENOMEM; | ||
1761 | goto rel_reg; | ||
1762 | } | ||
1763 | adapter = netdev_priv(netdev); | ||
1764 | adapter->pdev = pdev; | ||
1765 | pci_set_drvdata(pdev, adapter); | ||
1766 | adapter->netdev = netdev; | ||
1767 | |||
1768 | be_msix_enable(adapter); | ||
1769 | |||
1770 | status = pci_set_dma_mask(pdev, DMA_64BIT_MASK); | ||
1771 | if (!status) { | ||
1772 | netdev->features |= NETIF_F_HIGHDMA; | ||
1773 | } else { | ||
1774 | status = pci_set_dma_mask(pdev, DMA_32BIT_MASK); | ||
1775 | if (status) { | ||
1776 | dev_err(&pdev->dev, "Could not set PCI DMA Mask\n"); | ||
1777 | goto free_netdev; | ||
1778 | } | ||
1779 | } | ||
1780 | |||
1781 | ctrl = &adapter->ctrl; | ||
1782 | status = be_ctrl_init(adapter); | ||
1783 | if (status) | ||
1784 | goto free_netdev; | ||
1785 | |||
1786 | status = be_stats_init(adapter); | ||
1787 | if (status) | ||
1788 | goto ctrl_clean; | ||
1789 | |||
1790 | status = be_hw_up(adapter); | ||
1791 | if (status) | ||
1792 | goto stats_clean; | ||
1793 | |||
1794 | status = be_cmd_mac_addr_query(ctrl, mac, MAC_ADDRESS_TYPE_NETWORK, | ||
1795 | true /* permanent */, 0); | ||
1796 | if (status) | ||
1797 | goto stats_clean; | ||
1798 | memcpy(netdev->dev_addr, mac, ETH_ALEN); | ||
1799 | |||
1800 | INIT_DELAYED_WORK(&adapter->work, be_worker); | ||
1801 | be_netdev_init(netdev); | ||
1802 | SET_NETDEV_DEV(netdev, &adapter->pdev->dev); | ||
1803 | |||
1804 | status = register_netdev(netdev); | ||
1805 | if (status != 0) | ||
1806 | goto stats_clean; | ||
1807 | |||
1808 | dev_info(&pdev->dev, BE_NAME " port %d\n", adapter->port_num); | ||
1809 | return 0; | ||
1810 | |||
1811 | stats_clean: | ||
1812 | be_stats_cleanup(adapter); | ||
1813 | ctrl_clean: | ||
1814 | be_ctrl_cleanup(adapter); | ||
1815 | free_netdev: | ||
1816 | free_netdev(adapter->netdev); | ||
1817 | rel_reg: | ||
1818 | pci_release_regions(pdev); | ||
1819 | disable_dev: | ||
1820 | pci_disable_device(pdev); | ||
1821 | do_none: | ||
1822 | dev_warn(&pdev->dev, BE_NAME " initialization failed\n"); | ||
1823 | return status; | ||
1824 | } | ||
1825 | |||
1826 | static int be_suspend(struct pci_dev *pdev, pm_message_t state) | ||
1827 | { | ||
1828 | struct be_adapter *adapter = pci_get_drvdata(pdev); | ||
1829 | struct net_device *netdev = adapter->netdev; | ||
1830 | |||
1831 | netif_device_detach(netdev); | ||
1832 | if (netif_running(netdev)) { | ||
1833 | rtnl_lock(); | ||
1834 | be_close(netdev); | ||
1835 | rtnl_unlock(); | ||
1836 | } | ||
1837 | |||
1838 | pci_save_state(pdev); | ||
1839 | pci_disable_device(pdev); | ||
1840 | pci_set_power_state(pdev, pci_choose_state(pdev, state)); | ||
1841 | return 0; | ||
1842 | } | ||
1843 | |||
1844 | static int be_resume(struct pci_dev *pdev) | ||
1845 | { | ||
1846 | int status = 0; | ||
1847 | struct be_adapter *adapter = pci_get_drvdata(pdev); | ||
1848 | struct net_device *netdev = adapter->netdev; | ||
1849 | |||
1850 | netif_device_detach(netdev); | ||
1851 | |||
1852 | status = pci_enable_device(pdev); | ||
1853 | if (status) | ||
1854 | return status; | ||
1855 | |||
1856 | pci_set_power_state(pdev, 0); | ||
1857 | pci_restore_state(pdev); | ||
1858 | |||
1859 | be_vids_config(netdev); | ||
1860 | |||
1861 | if (netif_running(netdev)) { | ||
1862 | rtnl_lock(); | ||
1863 | be_open(netdev); | ||
1864 | rtnl_unlock(); | ||
1865 | } | ||
1866 | netif_device_attach(netdev); | ||
1867 | return 0; | ||
1868 | } | ||
1869 | |||
1870 | static struct pci_driver be_driver = { | ||
1871 | .name = DRV_NAME, | ||
1872 | .id_table = be_dev_ids, | ||
1873 | .probe = be_probe, | ||
1874 | .remove = be_remove, | ||
1875 | .suspend = be_suspend, | ||
1876 | .resume = be_resume | ||
1877 | }; | ||
1878 | |||
1879 | static int __init be_init_module(void) | ||
1880 | { | ||
1881 | if (rx_frag_size != 8192 && rx_frag_size != 4096 | ||
1882 | && rx_frag_size != 2048) { | ||
1883 | printk(KERN_WARNING DRV_NAME | ||
1884 | " : Module param rx_frag_size must be 2048/4096/8192." | ||
1885 | " Using 2048\n"); | ||
1886 | rx_frag_size = 2048; | ||
1887 | } | ||
1888 | /* Ensure rx_frag_size is aligned to chache line */ | ||
1889 | if (SKB_DATA_ALIGN(rx_frag_size) != rx_frag_size) { | ||
1890 | printk(KERN_WARNING DRV_NAME | ||
1891 | " : Bad module param rx_frag_size. Using 2048\n"); | ||
1892 | rx_frag_size = 2048; | ||
1893 | } | ||
1894 | |||
1895 | return pci_register_driver(&be_driver); | ||
1896 | } | ||
1897 | module_init(be_init_module); | ||
1898 | |||
1899 | static void __exit be_exit_module(void) | ||
1900 | { | ||
1901 | pci_unregister_driver(&be_driver); | ||
1902 | } | ||
1903 | module_exit(be_exit_module); | ||