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