2 * Copyright (C) 2005 - 2011 Emulex
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.
10 * Contact Information:
11 * linux-drivers@emulex.com
15 * Costa Mesa, CA 92626
18 #include <linux/prefetch.h>
21 #include <asm/div64.h>
23 MODULE_VERSION(DRV_VER);
24 MODULE_DEVICE_TABLE(pci, be_dev_ids);
25 MODULE_DESCRIPTION(DRV_DESC " " DRV_VER);
26 MODULE_AUTHOR("ServerEngines Corporation");
27 MODULE_LICENSE("GPL");
29 static ushort rx_frag_size = 2048;
30 static unsigned int num_vfs;
31 module_param(rx_frag_size, ushort, S_IRUGO);
32 module_param(num_vfs, uint, S_IRUGO);
33 MODULE_PARM_DESC(rx_frag_size, "Size of a fragment that holds rcvd data.");
34 MODULE_PARM_DESC(num_vfs, "Number of PCI VFs to initialize");
36 static DEFINE_PCI_DEVICE_TABLE(be_dev_ids) = {
37 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
38 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
39 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
40 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
41 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID3)},
42 { PCI_DEVICE(EMULEX_VENDOR_ID, OC_DEVICE_ID4)},
45 MODULE_DEVICE_TABLE(pci, be_dev_ids);
46 /* UE Status Low CSR */
47 static char *ue_status_low_desc[] = {
81 /* UE Status High CSR */
82 static char *ue_status_hi_desc[] = {
117 static void be_queue_free(struct be_adapter *adapter, struct be_queue_info *q)
119 struct be_dma_mem *mem = &q->dma_mem;
121 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
125 static int be_queue_alloc(struct be_adapter *adapter, struct be_queue_info *q,
126 u16 len, u16 entry_size)
128 struct be_dma_mem *mem = &q->dma_mem;
130 memset(q, 0, sizeof(*q));
132 q->entry_size = entry_size;
133 mem->size = len * entry_size;
134 mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size, &mem->dma,
138 memset(mem->va, 0, mem->size);
142 static void be_intr_set(struct be_adapter *adapter, bool enable)
144 u8 __iomem *addr = adapter->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
145 u32 reg = ioread32(addr);
146 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
148 if (adapter->eeh_err)
151 if (!enabled && enable)
152 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
153 else if (enabled && !enable)
154 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
158 iowrite32(reg, addr);
161 static void be_rxq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
164 val |= qid & DB_RQ_RING_ID_MASK;
165 val |= posted << DB_RQ_NUM_POSTED_SHIFT;
168 iowrite32(val, adapter->db + DB_RQ_OFFSET);
171 static void be_txq_notify(struct be_adapter *adapter, u16 qid, u16 posted)
174 val |= qid & DB_TXULP_RING_ID_MASK;
175 val |= (posted & DB_TXULP_NUM_POSTED_MASK) << DB_TXULP_NUM_POSTED_SHIFT;
178 iowrite32(val, adapter->db + DB_TXULP1_OFFSET);
181 static void be_eq_notify(struct be_adapter *adapter, u16 qid,
182 bool arm, bool clear_int, u16 num_popped)
185 val |= qid & DB_EQ_RING_ID_MASK;
186 val |= ((qid & DB_EQ_RING_ID_EXT_MASK) <<
187 DB_EQ_RING_ID_EXT_MASK_SHIFT);
189 if (adapter->eeh_err)
193 val |= 1 << DB_EQ_REARM_SHIFT;
195 val |= 1 << DB_EQ_CLR_SHIFT;
196 val |= 1 << DB_EQ_EVNT_SHIFT;
197 val |= num_popped << DB_EQ_NUM_POPPED_SHIFT;
198 iowrite32(val, adapter->db + DB_EQ_OFFSET);
201 void be_cq_notify(struct be_adapter *adapter, u16 qid, bool arm, u16 num_popped)
204 val |= qid & DB_CQ_RING_ID_MASK;
205 val |= ((qid & DB_CQ_RING_ID_EXT_MASK) <<
206 DB_CQ_RING_ID_EXT_MASK_SHIFT);
208 if (adapter->eeh_err)
212 val |= 1 << DB_CQ_REARM_SHIFT;
213 val |= num_popped << DB_CQ_NUM_POPPED_SHIFT;
214 iowrite32(val, adapter->db + DB_CQ_OFFSET);
217 static int be_mac_addr_set(struct net_device *netdev, void *p)
219 struct be_adapter *adapter = netdev_priv(netdev);
220 struct sockaddr *addr = p;
223 if (!is_valid_ether_addr(addr->sa_data))
224 return -EADDRNOTAVAIL;
226 /* MAC addr configuration will be done in hardware for VFs
227 * by their corresponding PFs. Just copy to netdev addr here
229 if (!be_physfn(adapter))
232 status = be_cmd_pmac_del(adapter, adapter->if_handle,
233 adapter->pmac_id, 0);
237 status = be_cmd_pmac_add(adapter, (u8 *)addr->sa_data,
238 adapter->if_handle, &adapter->pmac_id, 0);
241 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
246 static void populate_be2_stats(struct be_adapter *adapter)
249 struct be_drv_stats *drvs = &adapter->drv_stats;
250 struct be_pmem_stats *pmem_sts = be_pmem_stats_from_cmd(adapter);
251 struct be_port_rxf_stats_v0 *port_stats =
252 be_port_rxf_stats_from_cmd(adapter);
253 struct be_rxf_stats_v0 *rxf_stats =
254 be_rxf_stats_from_cmd(adapter);
256 drvs->rx_pause_frames = port_stats->rx_pause_frames;
257 drvs->rx_crc_errors = port_stats->rx_crc_errors;
258 drvs->rx_control_frames = port_stats->rx_control_frames;
259 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
260 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
261 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
262 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
263 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
264 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
265 drvs->rxpp_fifo_overflow_drop = port_stats->rx_fifo_overflow;
266 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
267 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
268 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
269 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
270 drvs->rx_input_fifo_overflow_drop =
271 port_stats->rx_input_fifo_overflow;
272 drvs->rx_dropped_header_too_small =
273 port_stats->rx_dropped_header_too_small;
274 drvs->rx_address_match_errors =
275 port_stats->rx_address_match_errors;
276 drvs->rx_alignment_symbol_errors =
277 port_stats->rx_alignment_symbol_errors;
279 drvs->tx_pauseframes = port_stats->tx_pauseframes;
280 drvs->tx_controlframes = port_stats->tx_controlframes;
282 if (adapter->port_num)
283 drvs->jabber_events =
284 rxf_stats->port1_jabber_events;
286 drvs->jabber_events =
287 rxf_stats->port0_jabber_events;
288 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
289 drvs->rx_drops_no_txpb = rxf_stats->rx_drops_no_txpb;
290 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
291 drvs->rx_drops_invalid_ring = rxf_stats->rx_drops_invalid_ring;
292 drvs->forwarded_packets = rxf_stats->forwarded_packets;
293 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
294 drvs->rx_drops_no_tpre_descr =
295 rxf_stats->rx_drops_no_tpre_descr;
296 drvs->rx_drops_too_many_frags =
297 rxf_stats->rx_drops_too_many_frags;
298 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
301 static void populate_be3_stats(struct be_adapter *adapter)
303 struct be_drv_stats *drvs = &adapter->drv_stats;
304 struct be_pmem_stats *pmem_sts = be_pmem_stats_from_cmd(adapter);
306 struct be_rxf_stats_v1 *rxf_stats =
307 be_rxf_stats_from_cmd(adapter);
308 struct be_port_rxf_stats_v1 *port_stats =
309 be_port_rxf_stats_from_cmd(adapter);
311 drvs->rx_priority_pause_frames = 0;
312 drvs->pmem_fifo_overflow_drop = 0;
313 drvs->rx_pause_frames = port_stats->rx_pause_frames;
314 drvs->rx_crc_errors = port_stats->rx_crc_errors;
315 drvs->rx_control_frames = port_stats->rx_control_frames;
316 drvs->rx_in_range_errors = port_stats->rx_in_range_errors;
317 drvs->rx_frame_too_long = port_stats->rx_frame_too_long;
318 drvs->rx_dropped_runt = port_stats->rx_dropped_runt;
319 drvs->rx_ip_checksum_errs = port_stats->rx_ip_checksum_errs;
320 drvs->rx_tcp_checksum_errs = port_stats->rx_tcp_checksum_errs;
321 drvs->rx_udp_checksum_errs = port_stats->rx_udp_checksum_errs;
322 drvs->rx_dropped_tcp_length = port_stats->rx_dropped_tcp_length;
323 drvs->rx_dropped_too_small = port_stats->rx_dropped_too_small;
324 drvs->rx_dropped_too_short = port_stats->rx_dropped_too_short;
325 drvs->rx_out_range_errors = port_stats->rx_out_range_errors;
326 drvs->rx_dropped_header_too_small =
327 port_stats->rx_dropped_header_too_small;
328 drvs->rx_input_fifo_overflow_drop =
329 port_stats->rx_input_fifo_overflow_drop;
330 drvs->rx_address_match_errors =
331 port_stats->rx_address_match_errors;
332 drvs->rx_alignment_symbol_errors =
333 port_stats->rx_alignment_symbol_errors;
334 drvs->rxpp_fifo_overflow_drop =
335 port_stats->rxpp_fifo_overflow_drop;
336 drvs->tx_pauseframes = port_stats->tx_pauseframes;
337 drvs->tx_controlframes = port_stats->tx_controlframes;
338 drvs->jabber_events = port_stats->jabber_events;
339 drvs->rx_drops_no_pbuf = rxf_stats->rx_drops_no_pbuf;
340 drvs->rx_drops_no_txpb = rxf_stats->rx_drops_no_txpb;
341 drvs->rx_drops_no_erx_descr = rxf_stats->rx_drops_no_erx_descr;
342 drvs->rx_drops_invalid_ring = rxf_stats->rx_drops_invalid_ring;
343 drvs->forwarded_packets = rxf_stats->forwarded_packets;
344 drvs->rx_drops_mtu = rxf_stats->rx_drops_mtu;
345 drvs->rx_drops_no_tpre_descr =
346 rxf_stats->rx_drops_no_tpre_descr;
347 drvs->rx_drops_too_many_frags =
348 rxf_stats->rx_drops_too_many_frags;
349 adapter->drv_stats.eth_red_drops = pmem_sts->eth_red_drops;
352 static void populate_lancer_stats(struct be_adapter *adapter)
355 struct be_drv_stats *drvs = &adapter->drv_stats;
356 struct lancer_cmd_pport_stats *pport_stats = pport_stats_from_cmd
358 drvs->rx_priority_pause_frames = 0;
359 drvs->pmem_fifo_overflow_drop = 0;
360 drvs->rx_pause_frames =
361 make_64bit_val(pport_stats->rx_pause_frames_hi,
362 pport_stats->rx_pause_frames_lo);
363 drvs->rx_crc_errors = make_64bit_val(pport_stats->rx_crc_errors_hi,
364 pport_stats->rx_crc_errors_lo);
365 drvs->rx_control_frames =
366 make_64bit_val(pport_stats->rx_control_frames_hi,
367 pport_stats->rx_control_frames_lo);
368 drvs->rx_in_range_errors = pport_stats->rx_in_range_errors;
369 drvs->rx_frame_too_long =
370 make_64bit_val(pport_stats->rx_internal_mac_errors_hi,
371 pport_stats->rx_frames_too_long_lo);
372 drvs->rx_dropped_runt = pport_stats->rx_dropped_runt;
373 drvs->rx_ip_checksum_errs = pport_stats->rx_ip_checksum_errors;
374 drvs->rx_tcp_checksum_errs = pport_stats->rx_tcp_checksum_errors;
375 drvs->rx_udp_checksum_errs = pport_stats->rx_udp_checksum_errors;
376 drvs->rx_dropped_tcp_length =
377 pport_stats->rx_dropped_invalid_tcp_length;
378 drvs->rx_dropped_too_small = pport_stats->rx_dropped_too_small;
379 drvs->rx_dropped_too_short = pport_stats->rx_dropped_too_short;
380 drvs->rx_out_range_errors = pport_stats->rx_out_of_range_errors;
381 drvs->rx_dropped_header_too_small =
382 pport_stats->rx_dropped_header_too_small;
383 drvs->rx_input_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
384 drvs->rx_address_match_errors = pport_stats->rx_address_match_errors;
385 drvs->rx_alignment_symbol_errors =
386 make_64bit_val(pport_stats->rx_symbol_errors_hi,
387 pport_stats->rx_symbol_errors_lo);
388 drvs->rxpp_fifo_overflow_drop = pport_stats->rx_fifo_overflow;
389 drvs->tx_pauseframes = make_64bit_val(pport_stats->tx_pause_frames_hi,
390 pport_stats->tx_pause_frames_lo);
391 drvs->tx_controlframes =
392 make_64bit_val(pport_stats->tx_control_frames_hi,
393 pport_stats->tx_control_frames_lo);
394 drvs->jabber_events = pport_stats->rx_jabbers;
395 drvs->rx_drops_no_pbuf = 0;
396 drvs->rx_drops_no_txpb = 0;
397 drvs->rx_drops_no_erx_descr = 0;
398 drvs->rx_drops_invalid_ring = pport_stats->rx_drops_invalid_queue;
399 drvs->forwarded_packets = make_64bit_val(pport_stats->num_forwards_hi,
400 pport_stats->num_forwards_lo);
401 drvs->rx_drops_mtu = make_64bit_val(pport_stats->rx_drops_mtu_hi,
402 pport_stats->rx_drops_mtu_lo);
403 drvs->rx_drops_no_tpre_descr = 0;
404 drvs->rx_drops_too_many_frags =
405 make_64bit_val(pport_stats->rx_drops_too_many_frags_hi,
406 pport_stats->rx_drops_too_many_frags_lo);
409 void be_parse_stats(struct be_adapter *adapter)
411 if (adapter->generation == BE_GEN3) {
412 if (lancer_chip(adapter))
413 populate_lancer_stats(adapter);
415 populate_be3_stats(adapter);
417 populate_be2_stats(adapter);
421 void netdev_stats_update(struct be_adapter *adapter)
423 struct be_drv_stats *drvs = &adapter->drv_stats;
424 struct net_device_stats *dev_stats = &adapter->netdev->stats;
425 struct be_rx_obj *rxo;
426 struct be_tx_obj *txo;
427 unsigned long pkts = 0, bytes = 0, mcast = 0, drops = 0;
430 for_all_rx_queues(adapter, rxo, i) {
431 pkts += rx_stats(rxo)->rx_pkts;
432 bytes += rx_stats(rxo)->rx_bytes;
433 mcast += rx_stats(rxo)->rx_mcast_pkts;
434 /* no space in linux buffers: best possible approximation */
435 if (adapter->generation == BE_GEN3) {
436 if (!(lancer_chip(adapter))) {
437 struct be_erx_stats_v1 *erx =
438 be_erx_stats_from_cmd(adapter);
439 drops += erx->rx_drops_no_fragments[rxo->q.id];
442 struct be_erx_stats_v0 *erx =
443 be_erx_stats_from_cmd(adapter);
444 drops += erx->rx_drops_no_fragments[rxo->q.id];
447 dev_stats->rx_packets = pkts;
448 dev_stats->rx_bytes = bytes;
449 dev_stats->multicast = mcast;
450 dev_stats->rx_dropped = drops;
453 for_all_tx_queues(adapter, txo, i) {
454 pkts += tx_stats(txo)->be_tx_pkts;
455 bytes += tx_stats(txo)->be_tx_bytes;
457 dev_stats->tx_packets = pkts;
458 dev_stats->tx_bytes = bytes;
460 /* bad pkts received */
461 dev_stats->rx_errors = drvs->rx_crc_errors +
462 drvs->rx_alignment_symbol_errors +
463 drvs->rx_in_range_errors +
464 drvs->rx_out_range_errors +
465 drvs->rx_frame_too_long +
466 drvs->rx_dropped_too_small +
467 drvs->rx_dropped_too_short +
468 drvs->rx_dropped_header_too_small +
469 drvs->rx_dropped_tcp_length +
470 drvs->rx_dropped_runt +
471 drvs->rx_tcp_checksum_errs +
472 drvs->rx_ip_checksum_errs +
473 drvs->rx_udp_checksum_errs;
475 /* detailed rx errors */
476 dev_stats->rx_length_errors = drvs->rx_in_range_errors +
477 drvs->rx_out_range_errors +
478 drvs->rx_frame_too_long;
480 dev_stats->rx_crc_errors = drvs->rx_crc_errors;
482 /* frame alignment errors */
483 dev_stats->rx_frame_errors = drvs->rx_alignment_symbol_errors;
485 /* receiver fifo overrun */
486 /* drops_no_pbuf is no per i/f, it's per BE card */
487 dev_stats->rx_fifo_errors = drvs->rxpp_fifo_overflow_drop +
488 drvs->rx_input_fifo_overflow_drop +
489 drvs->rx_drops_no_pbuf;
492 void be_link_status_update(struct be_adapter *adapter, bool link_up)
494 struct net_device *netdev = adapter->netdev;
496 /* If link came up or went down */
497 if (adapter->link_up != link_up) {
498 adapter->link_speed = -1;
500 netif_carrier_on(netdev);
501 printk(KERN_INFO "%s: Link up\n", netdev->name);
503 netif_carrier_off(netdev);
504 printk(KERN_INFO "%s: Link down\n", netdev->name);
506 adapter->link_up = link_up;
510 /* Update the EQ delay n BE based on the RX frags consumed / sec */
511 static void be_rx_eqd_update(struct be_adapter *adapter, struct be_rx_obj *rxo)
513 struct be_eq_obj *rx_eq = &rxo->rx_eq;
514 struct be_rx_stats *stats = &rxo->stats;
518 if (!rx_eq->enable_aic)
522 if (time_before(now, stats->rx_fps_jiffies)) {
523 stats->rx_fps_jiffies = now;
527 /* Update once a second */
528 if ((now - stats->rx_fps_jiffies) < HZ)
531 stats->rx_fps = (stats->rx_frags - stats->prev_rx_frags) /
532 ((now - stats->rx_fps_jiffies) / HZ);
534 stats->rx_fps_jiffies = now;
535 stats->prev_rx_frags = stats->rx_frags;
536 eqd = stats->rx_fps / 110000;
538 if (eqd > rx_eq->max_eqd)
539 eqd = rx_eq->max_eqd;
540 if (eqd < rx_eq->min_eqd)
541 eqd = rx_eq->min_eqd;
544 if (eqd != rx_eq->cur_eqd)
545 be_cmd_modify_eqd(adapter, rx_eq->q.id, eqd);
547 rx_eq->cur_eqd = eqd;
550 static u32 be_calc_rate(u64 bytes, unsigned long ticks)
554 do_div(rate, ticks / HZ);
555 rate <<= 3; /* bytes/sec -> bits/sec */
556 do_div(rate, 1000000ul); /* MB/Sec */
561 static void be_tx_rate_update(struct be_tx_obj *txo)
563 struct be_tx_stats *stats = tx_stats(txo);
566 /* Wrapped around? */
567 if (time_before(now, stats->be_tx_jiffies)) {
568 stats->be_tx_jiffies = now;
572 /* Update tx rate once in two seconds */
573 if ((now - stats->be_tx_jiffies) > 2 * HZ) {
574 stats->be_tx_rate = be_calc_rate(stats->be_tx_bytes
575 - stats->be_tx_bytes_prev,
576 now - stats->be_tx_jiffies);
577 stats->be_tx_jiffies = now;
578 stats->be_tx_bytes_prev = stats->be_tx_bytes;
582 static void be_tx_stats_update(struct be_tx_obj *txo,
583 u32 wrb_cnt, u32 copied, u32 gso_segs, bool stopped)
585 struct be_tx_stats *stats = tx_stats(txo);
588 stats->be_tx_wrbs += wrb_cnt;
589 stats->be_tx_bytes += copied;
590 stats->be_tx_pkts += (gso_segs ? gso_segs : 1);
592 stats->be_tx_stops++;
595 /* Determine number of WRB entries needed to xmit data in an skb */
596 static u32 wrb_cnt_for_skb(struct be_adapter *adapter, struct sk_buff *skb,
599 int cnt = (skb->len > skb->data_len);
601 cnt += skb_shinfo(skb)->nr_frags;
603 /* to account for hdr wrb */
605 if (lancer_chip(adapter) || !(cnt & 1)) {
608 /* add a dummy to make it an even num */
612 BUG_ON(cnt > BE_MAX_TX_FRAG_COUNT);
616 static inline void wrb_fill(struct be_eth_wrb *wrb, u64 addr, int len)
618 wrb->frag_pa_hi = upper_32_bits(addr);
619 wrb->frag_pa_lo = addr & 0xFFFFFFFF;
620 wrb->frag_len = len & ETH_WRB_FRAG_LEN_MASK;
623 static void wrb_fill_hdr(struct be_adapter *adapter, struct be_eth_hdr_wrb *hdr,
624 struct sk_buff *skb, u32 wrb_cnt, u32 len)
629 memset(hdr, 0, sizeof(*hdr));
631 AMAP_SET_BITS(struct amap_eth_hdr_wrb, crc, hdr, 1);
633 if (skb_is_gso(skb)) {
634 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso, hdr, 1);
635 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso_mss,
636 hdr, skb_shinfo(skb)->gso_size);
637 if (skb_is_gso_v6(skb) && !lancer_chip(adapter))
638 AMAP_SET_BITS(struct amap_eth_hdr_wrb, lso6, hdr, 1);
639 if (lancer_chip(adapter) && adapter->sli_family ==
640 LANCER_A0_SLI_FAMILY) {
641 AMAP_SET_BITS(struct amap_eth_hdr_wrb, ipcs, hdr, 1);
643 AMAP_SET_BITS(struct amap_eth_hdr_wrb,
645 else if (is_udp_pkt(skb))
646 AMAP_SET_BITS(struct amap_eth_hdr_wrb,
649 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
651 AMAP_SET_BITS(struct amap_eth_hdr_wrb, tcpcs, hdr, 1);
652 else if (is_udp_pkt(skb))
653 AMAP_SET_BITS(struct amap_eth_hdr_wrb, udpcs, hdr, 1);
656 if (adapter->vlan_grp && vlan_tx_tag_present(skb)) {
657 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan, hdr, 1);
658 vlan_tag = vlan_tx_tag_get(skb);
659 vlan_prio = (vlan_tag & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
660 /* If vlan priority provided by OS is NOT in available bmap */
661 if (!(adapter->vlan_prio_bmap & (1 << vlan_prio)))
662 vlan_tag = (vlan_tag & ~VLAN_PRIO_MASK) |
663 adapter->recommended_prio;
664 AMAP_SET_BITS(struct amap_eth_hdr_wrb, vlan_tag, hdr, vlan_tag);
667 AMAP_SET_BITS(struct amap_eth_hdr_wrb, event, hdr, 1);
668 AMAP_SET_BITS(struct amap_eth_hdr_wrb, complete, hdr, 1);
669 AMAP_SET_BITS(struct amap_eth_hdr_wrb, num_wrb, hdr, wrb_cnt);
670 AMAP_SET_BITS(struct amap_eth_hdr_wrb, len, hdr, len);
673 static void unmap_tx_frag(struct device *dev, struct be_eth_wrb *wrb,
678 be_dws_le_to_cpu(wrb, sizeof(*wrb));
680 dma = (u64)wrb->frag_pa_hi << 32 | (u64)wrb->frag_pa_lo;
683 dma_unmap_single(dev, dma, wrb->frag_len,
686 dma_unmap_page(dev, dma, wrb->frag_len, DMA_TO_DEVICE);
690 static int make_tx_wrbs(struct be_adapter *adapter, struct be_queue_info *txq,
691 struct sk_buff *skb, u32 wrb_cnt, bool dummy_wrb)
695 struct device *dev = &adapter->pdev->dev;
696 struct sk_buff *first_skb = skb;
697 struct be_eth_wrb *wrb;
698 struct be_eth_hdr_wrb *hdr;
699 bool map_single = false;
702 hdr = queue_head_node(txq);
704 map_head = txq->head;
706 if (skb->len > skb->data_len) {
707 int len = skb_headlen(skb);
708 busaddr = dma_map_single(dev, skb->data, len, DMA_TO_DEVICE);
709 if (dma_mapping_error(dev, busaddr))
712 wrb = queue_head_node(txq);
713 wrb_fill(wrb, busaddr, len);
714 be_dws_cpu_to_le(wrb, sizeof(*wrb));
719 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
720 struct skb_frag_struct *frag =
721 &skb_shinfo(skb)->frags[i];
722 busaddr = dma_map_page(dev, frag->page, frag->page_offset,
723 frag->size, DMA_TO_DEVICE);
724 if (dma_mapping_error(dev, busaddr))
726 wrb = queue_head_node(txq);
727 wrb_fill(wrb, busaddr, frag->size);
728 be_dws_cpu_to_le(wrb, sizeof(*wrb));
730 copied += frag->size;
734 wrb = queue_head_node(txq);
736 be_dws_cpu_to_le(wrb, sizeof(*wrb));
740 wrb_fill_hdr(adapter, hdr, first_skb, wrb_cnt, copied);
741 be_dws_cpu_to_le(hdr, sizeof(*hdr));
745 txq->head = map_head;
747 wrb = queue_head_node(txq);
748 unmap_tx_frag(dev, wrb, map_single);
750 copied -= wrb->frag_len;
756 static netdev_tx_t be_xmit(struct sk_buff *skb,
757 struct net_device *netdev)
759 struct be_adapter *adapter = netdev_priv(netdev);
760 struct be_tx_obj *txo = &adapter->tx_obj[skb_get_queue_mapping(skb)];
761 struct be_queue_info *txq = &txo->q;
762 u32 wrb_cnt = 0, copied = 0;
763 u32 start = txq->head;
764 bool dummy_wrb, stopped = false;
766 wrb_cnt = wrb_cnt_for_skb(adapter, skb, &dummy_wrb);
768 copied = make_tx_wrbs(adapter, txq, skb, wrb_cnt, dummy_wrb);
770 /* record the sent skb in the sent_skb table */
771 BUG_ON(txo->sent_skb_list[start]);
772 txo->sent_skb_list[start] = skb;
774 /* Ensure txq has space for the next skb; Else stop the queue
775 * *BEFORE* ringing the tx doorbell, so that we serialze the
776 * tx compls of the current transmit which'll wake up the queue
778 atomic_add(wrb_cnt, &txq->used);
779 if ((BE_MAX_TX_FRAG_COUNT + atomic_read(&txq->used)) >=
781 netif_stop_subqueue(netdev, skb_get_queue_mapping(skb));
785 be_txq_notify(adapter, txq->id, wrb_cnt);
787 be_tx_stats_update(txo, wrb_cnt, copied,
788 skb_shinfo(skb)->gso_segs, stopped);
791 dev_kfree_skb_any(skb);
796 static int be_change_mtu(struct net_device *netdev, int new_mtu)
798 struct be_adapter *adapter = netdev_priv(netdev);
799 if (new_mtu < BE_MIN_MTU ||
800 new_mtu > (BE_MAX_JUMBO_FRAME_SIZE -
801 (ETH_HLEN + ETH_FCS_LEN))) {
802 dev_info(&adapter->pdev->dev,
803 "MTU must be between %d and %d bytes\n",
805 (BE_MAX_JUMBO_FRAME_SIZE - (ETH_HLEN + ETH_FCS_LEN)));
808 dev_info(&adapter->pdev->dev, "MTU changed from %d to %d bytes\n",
809 netdev->mtu, new_mtu);
810 netdev->mtu = new_mtu;
815 * A max of 64 (BE_NUM_VLANS_SUPPORTED) vlans can be configured in BE.
816 * If the user configures more, place BE in vlan promiscuous mode.
818 static int be_vid_config(struct be_adapter *adapter, bool vf, u32 vf_num)
820 u16 vtag[BE_NUM_VLANS_SUPPORTED];
826 if_handle = adapter->vf_cfg[vf_num].vf_if_handle;
827 vtag[0] = cpu_to_le16(adapter->vf_cfg[vf_num].vf_vlan_tag);
828 status = be_cmd_vlan_config(adapter, if_handle, vtag, 1, 1, 0);
831 if (adapter->vlans_added <= adapter->max_vlans) {
832 /* Construct VLAN Table to give to HW */
833 for (i = 0; i < VLAN_N_VID; i++) {
834 if (adapter->vlan_tag[i]) {
835 vtag[ntags] = cpu_to_le16(i);
839 status = be_cmd_vlan_config(adapter, adapter->if_handle,
842 status = be_cmd_vlan_config(adapter, adapter->if_handle,
849 static void be_vlan_register(struct net_device *netdev, struct vlan_group *grp)
851 struct be_adapter *adapter = netdev_priv(netdev);
853 adapter->vlan_grp = grp;
856 static void be_vlan_add_vid(struct net_device *netdev, u16 vid)
858 struct be_adapter *adapter = netdev_priv(netdev);
860 adapter->vlans_added++;
861 if (!be_physfn(adapter))
864 adapter->vlan_tag[vid] = 1;
865 if (adapter->vlans_added <= (adapter->max_vlans + 1))
866 be_vid_config(adapter, false, 0);
869 static void be_vlan_rem_vid(struct net_device *netdev, u16 vid)
871 struct be_adapter *adapter = netdev_priv(netdev);
873 adapter->vlans_added--;
874 vlan_group_set_device(adapter->vlan_grp, vid, NULL);
876 if (!be_physfn(adapter))
879 adapter->vlan_tag[vid] = 0;
880 if (adapter->vlans_added <= adapter->max_vlans)
881 be_vid_config(adapter, false, 0);
884 static void be_set_multicast_list(struct net_device *netdev)
886 struct be_adapter *adapter = netdev_priv(netdev);
888 if (netdev->flags & IFF_PROMISC) {
889 be_cmd_promiscuous_config(adapter, true);
890 adapter->promiscuous = true;
894 /* BE was previously in promiscuous mode; disable it */
895 if (adapter->promiscuous) {
896 adapter->promiscuous = false;
897 be_cmd_promiscuous_config(adapter, false);
900 /* Enable multicast promisc if num configured exceeds what we support */
901 if (netdev->flags & IFF_ALLMULTI ||
902 netdev_mc_count(netdev) > BE_MAX_MC) {
903 be_cmd_multicast_set(adapter, adapter->if_handle, NULL,
904 &adapter->mc_cmd_mem);
908 be_cmd_multicast_set(adapter, adapter->if_handle, netdev,
909 &adapter->mc_cmd_mem);
914 static int be_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
916 struct be_adapter *adapter = netdev_priv(netdev);
919 if (!adapter->sriov_enabled)
922 if (!is_valid_ether_addr(mac) || (vf >= num_vfs))
925 if (adapter->vf_cfg[vf].vf_pmac_id != BE_INVALID_PMAC_ID)
926 status = be_cmd_pmac_del(adapter,
927 adapter->vf_cfg[vf].vf_if_handle,
928 adapter->vf_cfg[vf].vf_pmac_id, vf + 1);
930 status = be_cmd_pmac_add(adapter, mac,
931 adapter->vf_cfg[vf].vf_if_handle,
932 &adapter->vf_cfg[vf].vf_pmac_id, vf + 1);
935 dev_err(&adapter->pdev->dev, "MAC %pM set on VF %d Failed\n",
938 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
943 static int be_get_vf_config(struct net_device *netdev, int vf,
944 struct ifla_vf_info *vi)
946 struct be_adapter *adapter = netdev_priv(netdev);
948 if (!adapter->sriov_enabled)
955 vi->tx_rate = adapter->vf_cfg[vf].vf_tx_rate;
956 vi->vlan = adapter->vf_cfg[vf].vf_vlan_tag;
958 memcpy(&vi->mac, adapter->vf_cfg[vf].vf_mac_addr, ETH_ALEN);
963 static int be_set_vf_vlan(struct net_device *netdev,
964 int vf, u16 vlan, u8 qos)
966 struct be_adapter *adapter = netdev_priv(netdev);
969 if (!adapter->sriov_enabled)
972 if ((vf >= num_vfs) || (vlan > 4095))
976 adapter->vf_cfg[vf].vf_vlan_tag = vlan;
977 adapter->vlans_added++;
979 adapter->vf_cfg[vf].vf_vlan_tag = 0;
980 adapter->vlans_added--;
983 status = be_vid_config(adapter, true, vf);
986 dev_info(&adapter->pdev->dev,
987 "VLAN %d config on VF %d failed\n", vlan, vf);
991 static int be_set_vf_tx_rate(struct net_device *netdev,
994 struct be_adapter *adapter = netdev_priv(netdev);
997 if (!adapter->sriov_enabled)
1000 if ((vf >= num_vfs) || (rate < 0))
1006 adapter->vf_cfg[vf].vf_tx_rate = rate;
1007 status = be_cmd_set_qos(adapter, rate / 10, vf + 1);
1010 dev_info(&adapter->pdev->dev,
1011 "tx rate %d on VF %d failed\n", rate, vf);
1015 static void be_rx_rate_update(struct be_rx_obj *rxo)
1017 struct be_rx_stats *stats = &rxo->stats;
1018 ulong now = jiffies;
1020 /* Wrapped around */
1021 if (time_before(now, stats->rx_jiffies)) {
1022 stats->rx_jiffies = now;
1026 /* Update the rate once in two seconds */
1027 if ((now - stats->rx_jiffies) < 2 * HZ)
1030 stats->rx_rate = be_calc_rate(stats->rx_bytes - stats->rx_bytes_prev,
1031 now - stats->rx_jiffies);
1032 stats->rx_jiffies = now;
1033 stats->rx_bytes_prev = stats->rx_bytes;
1036 static void be_rx_stats_update(struct be_rx_obj *rxo,
1037 struct be_rx_compl_info *rxcp)
1039 struct be_rx_stats *stats = &rxo->stats;
1042 stats->rx_frags += rxcp->num_rcvd;
1043 stats->rx_bytes += rxcp->pkt_size;
1045 if (rxcp->pkt_type == BE_MULTICAST_PACKET)
1046 stats->rx_mcast_pkts++;
1051 static inline bool csum_passed(struct be_rx_compl_info *rxcp)
1053 /* L4 checksum is not reliable for non TCP/UDP packets.
1054 * Also ignore ipcksm for ipv6 pkts */
1055 return (rxcp->tcpf || rxcp->udpf) && rxcp->l4_csum &&
1056 (rxcp->ip_csum || rxcp->ipv6);
1059 static struct be_rx_page_info *
1060 get_rx_page_info(struct be_adapter *adapter,
1061 struct be_rx_obj *rxo,
1064 struct be_rx_page_info *rx_page_info;
1065 struct be_queue_info *rxq = &rxo->q;
1067 rx_page_info = &rxo->page_info_tbl[frag_idx];
1068 BUG_ON(!rx_page_info->page);
1070 if (rx_page_info->last_page_user) {
1071 dma_unmap_page(&adapter->pdev->dev,
1072 dma_unmap_addr(rx_page_info, bus),
1073 adapter->big_page_size, DMA_FROM_DEVICE);
1074 rx_page_info->last_page_user = false;
1077 atomic_dec(&rxq->used);
1078 return rx_page_info;
1081 /* Throwaway the data in the Rx completion */
1082 static void be_rx_compl_discard(struct be_adapter *adapter,
1083 struct be_rx_obj *rxo,
1084 struct be_rx_compl_info *rxcp)
1086 struct be_queue_info *rxq = &rxo->q;
1087 struct be_rx_page_info *page_info;
1088 u16 i, num_rcvd = rxcp->num_rcvd;
1090 for (i = 0; i < num_rcvd; i++) {
1091 page_info = get_rx_page_info(adapter, rxo, rxcp->rxq_idx);
1092 put_page(page_info->page);
1093 memset(page_info, 0, sizeof(*page_info));
1094 index_inc(&rxcp->rxq_idx, rxq->len);
1099 * skb_fill_rx_data forms a complete skb for an ether frame
1100 * indicated by rxcp.
1102 static void skb_fill_rx_data(struct be_adapter *adapter, struct be_rx_obj *rxo,
1103 struct sk_buff *skb, struct be_rx_compl_info *rxcp)
1105 struct be_queue_info *rxq = &rxo->q;
1106 struct be_rx_page_info *page_info;
1108 u16 hdr_len, curr_frag_len, remaining;
1111 page_info = get_rx_page_info(adapter, rxo, rxcp->rxq_idx);
1112 start = page_address(page_info->page) + page_info->page_offset;
1115 /* Copy data in the first descriptor of this completion */
1116 curr_frag_len = min(rxcp->pkt_size, rx_frag_size);
1118 /* Copy the header portion into skb_data */
1119 hdr_len = min(BE_HDR_LEN, curr_frag_len);
1120 memcpy(skb->data, start, hdr_len);
1121 skb->len = curr_frag_len;
1122 if (curr_frag_len <= BE_HDR_LEN) { /* tiny packet */
1123 /* Complete packet has now been moved to data */
1124 put_page(page_info->page);
1126 skb->tail += curr_frag_len;
1128 skb_shinfo(skb)->nr_frags = 1;
1129 skb_shinfo(skb)->frags[0].page = page_info->page;
1130 skb_shinfo(skb)->frags[0].page_offset =
1131 page_info->page_offset + hdr_len;
1132 skb_shinfo(skb)->frags[0].size = curr_frag_len - hdr_len;
1133 skb->data_len = curr_frag_len - hdr_len;
1134 skb->tail += hdr_len;
1136 page_info->page = NULL;
1138 if (rxcp->pkt_size <= rx_frag_size) {
1139 BUG_ON(rxcp->num_rcvd != 1);
1143 /* More frags present for this completion */
1144 index_inc(&rxcp->rxq_idx, rxq->len);
1145 remaining = rxcp->pkt_size - curr_frag_len;
1146 for (i = 1, j = 0; i < rxcp->num_rcvd; i++) {
1147 page_info = get_rx_page_info(adapter, rxo, rxcp->rxq_idx);
1148 curr_frag_len = min(remaining, rx_frag_size);
1150 /* Coalesce all frags from the same physical page in one slot */
1151 if (page_info->page_offset == 0) {
1154 skb_shinfo(skb)->frags[j].page = page_info->page;
1155 skb_shinfo(skb)->frags[j].page_offset =
1156 page_info->page_offset;
1157 skb_shinfo(skb)->frags[j].size = 0;
1158 skb_shinfo(skb)->nr_frags++;
1160 put_page(page_info->page);
1163 skb_shinfo(skb)->frags[j].size += curr_frag_len;
1164 skb->len += curr_frag_len;
1165 skb->data_len += curr_frag_len;
1167 remaining -= curr_frag_len;
1168 index_inc(&rxcp->rxq_idx, rxq->len);
1169 page_info->page = NULL;
1171 BUG_ON(j > MAX_SKB_FRAGS);
1174 /* Process the RX completion indicated by rxcp when GRO is disabled */
1175 static void be_rx_compl_process(struct be_adapter *adapter,
1176 struct be_rx_obj *rxo,
1177 struct be_rx_compl_info *rxcp)
1179 struct net_device *netdev = adapter->netdev;
1180 struct sk_buff *skb;
1182 skb = netdev_alloc_skb_ip_align(netdev, BE_HDR_LEN);
1183 if (unlikely(!skb)) {
1184 if (net_ratelimit())
1185 dev_warn(&adapter->pdev->dev, "skb alloc failed\n");
1186 be_rx_compl_discard(adapter, rxo, rxcp);
1190 skb_fill_rx_data(adapter, rxo, skb, rxcp);
1192 if (likely((netdev->features & NETIF_F_RXCSUM) && csum_passed(rxcp)))
1193 skb->ip_summed = CHECKSUM_UNNECESSARY;
1195 skb_checksum_none_assert(skb);
1197 skb->truesize = skb->len + sizeof(struct sk_buff);
1198 skb->protocol = eth_type_trans(skb, netdev);
1199 if (adapter->netdev->features & NETIF_F_RXHASH)
1200 skb->rxhash = rxcp->rss_hash;
1203 if (unlikely(rxcp->vlanf)) {
1204 if (!adapter->vlan_grp || adapter->vlans_added == 0) {
1208 vlan_hwaccel_receive_skb(skb, adapter->vlan_grp,
1211 netif_receive_skb(skb);
1215 /* Process the RX completion indicated by rxcp when GRO is enabled */
1216 static void be_rx_compl_process_gro(struct be_adapter *adapter,
1217 struct be_rx_obj *rxo,
1218 struct be_rx_compl_info *rxcp)
1220 struct be_rx_page_info *page_info;
1221 struct sk_buff *skb = NULL;
1222 struct be_queue_info *rxq = &rxo->q;
1223 struct be_eq_obj *eq_obj = &rxo->rx_eq;
1224 u16 remaining, curr_frag_len;
1227 skb = napi_get_frags(&eq_obj->napi);
1229 be_rx_compl_discard(adapter, rxo, rxcp);
1233 remaining = rxcp->pkt_size;
1234 for (i = 0, j = -1; i < rxcp->num_rcvd; i++) {
1235 page_info = get_rx_page_info(adapter, rxo, rxcp->rxq_idx);
1237 curr_frag_len = min(remaining, rx_frag_size);
1239 /* Coalesce all frags from the same physical page in one slot */
1240 if (i == 0 || page_info->page_offset == 0) {
1241 /* First frag or Fresh page */
1243 skb_shinfo(skb)->frags[j].page = page_info->page;
1244 skb_shinfo(skb)->frags[j].page_offset =
1245 page_info->page_offset;
1246 skb_shinfo(skb)->frags[j].size = 0;
1248 put_page(page_info->page);
1250 skb_shinfo(skb)->frags[j].size += curr_frag_len;
1252 remaining -= curr_frag_len;
1253 index_inc(&rxcp->rxq_idx, rxq->len);
1254 memset(page_info, 0, sizeof(*page_info));
1256 BUG_ON(j > MAX_SKB_FRAGS);
1258 skb_shinfo(skb)->nr_frags = j + 1;
1259 skb->len = rxcp->pkt_size;
1260 skb->data_len = rxcp->pkt_size;
1261 skb->truesize += rxcp->pkt_size;
1262 skb->ip_summed = CHECKSUM_UNNECESSARY;
1263 if (adapter->netdev->features & NETIF_F_RXHASH)
1264 skb->rxhash = rxcp->rss_hash;
1266 if (likely(!rxcp->vlanf))
1267 napi_gro_frags(&eq_obj->napi);
1269 vlan_gro_frags(&eq_obj->napi, adapter->vlan_grp,
1273 static void be_parse_rx_compl_v1(struct be_adapter *adapter,
1274 struct be_eth_rx_compl *compl,
1275 struct be_rx_compl_info *rxcp)
1278 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, pktsize, compl);
1279 rxcp->vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vtp, compl);
1280 rxcp->err = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, err, compl);
1281 rxcp->tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, tcpf, compl);
1282 rxcp->udpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, udpf, compl);
1284 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, ipcksm, compl);
1286 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, l4_cksm, compl);
1288 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, ip_version, compl);
1290 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, fragndx, compl);
1292 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, numfrags, compl);
1294 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, cast_enc, compl);
1296 AMAP_GET_BITS(struct amap_eth_rx_compl_v1, rsshash, rxcp);
1298 rxcp->vtm = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vtm,
1300 rxcp->vlan_tag = AMAP_GET_BITS(struct amap_eth_rx_compl_v1, vlan_tag,
1305 static void be_parse_rx_compl_v0(struct be_adapter *adapter,
1306 struct be_eth_rx_compl *compl,
1307 struct be_rx_compl_info *rxcp)
1310 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, pktsize, compl);
1311 rxcp->vlanf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vtp, compl);
1312 rxcp->err = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, err, compl);
1313 rxcp->tcpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, tcpf, compl);
1314 rxcp->udpf = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, udpf, compl);
1316 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, ipcksm, compl);
1318 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, l4_cksm, compl);
1320 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, ip_version, compl);
1322 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, fragndx, compl);
1324 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, numfrags, compl);
1326 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, cast_enc, compl);
1328 AMAP_GET_BITS(struct amap_eth_rx_compl_v0, rsshash, rxcp);
1330 rxcp->vtm = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vtm,
1332 rxcp->vlan_tag = AMAP_GET_BITS(struct amap_eth_rx_compl_v0, vlan_tag,
1337 static struct be_rx_compl_info *be_rx_compl_get(struct be_rx_obj *rxo)
1339 struct be_eth_rx_compl *compl = queue_tail_node(&rxo->cq);
1340 struct be_rx_compl_info *rxcp = &rxo->rxcp;
1341 struct be_adapter *adapter = rxo->adapter;
1343 /* For checking the valid bit it is Ok to use either definition as the
1344 * valid bit is at the same position in both v0 and v1 Rx compl */
1345 if (compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] == 0)
1349 be_dws_le_to_cpu(compl, sizeof(*compl));
1351 if (adapter->be3_native)
1352 be_parse_rx_compl_v1(adapter, compl, rxcp);
1354 be_parse_rx_compl_v0(adapter, compl, rxcp);
1357 /* vlanf could be wrongly set in some cards.
1358 * ignore if vtm is not set */
1359 if ((adapter->function_mode & 0x400) && !rxcp->vtm)
1362 if (!lancer_chip(adapter))
1363 rxcp->vlan_tag = swab16(rxcp->vlan_tag);
1365 if (((adapter->pvid & VLAN_VID_MASK) ==
1366 (rxcp->vlan_tag & VLAN_VID_MASK)) &&
1367 !adapter->vlan_tag[rxcp->vlan_tag])
1371 /* As the compl has been parsed, reset it; we wont touch it again */
1372 compl->dw[offsetof(struct amap_eth_rx_compl_v1, valid) / 32] = 0;
1374 queue_tail_inc(&rxo->cq);
1378 static inline struct page *be_alloc_pages(u32 size, gfp_t gfp)
1380 u32 order = get_order(size);
1384 return alloc_pages(gfp, order);
1388 * Allocate a page, split it to fragments of size rx_frag_size and post as
1389 * receive buffers to BE
1391 static void be_post_rx_frags(struct be_rx_obj *rxo, gfp_t gfp)
1393 struct be_adapter *adapter = rxo->adapter;
1394 struct be_rx_page_info *page_info_tbl = rxo->page_info_tbl;
1395 struct be_rx_page_info *page_info = NULL, *prev_page_info = NULL;
1396 struct be_queue_info *rxq = &rxo->q;
1397 struct page *pagep = NULL;
1398 struct be_eth_rx_d *rxd;
1399 u64 page_dmaaddr = 0, frag_dmaaddr;
1400 u32 posted, page_offset = 0;
1402 page_info = &rxo->page_info_tbl[rxq->head];
1403 for (posted = 0; posted < MAX_RX_POST && !page_info->page; posted++) {
1405 pagep = be_alloc_pages(adapter->big_page_size, gfp);
1406 if (unlikely(!pagep)) {
1407 rxo->stats.rx_post_fail++;
1410 page_dmaaddr = dma_map_page(&adapter->pdev->dev, pagep,
1411 0, adapter->big_page_size,
1413 page_info->page_offset = 0;
1416 page_info->page_offset = page_offset + rx_frag_size;
1418 page_offset = page_info->page_offset;
1419 page_info->page = pagep;
1420 dma_unmap_addr_set(page_info, bus, page_dmaaddr);
1421 frag_dmaaddr = page_dmaaddr + page_info->page_offset;
1423 rxd = queue_head_node(rxq);
1424 rxd->fragpa_lo = cpu_to_le32(frag_dmaaddr & 0xFFFFFFFF);
1425 rxd->fragpa_hi = cpu_to_le32(upper_32_bits(frag_dmaaddr));
1427 /* Any space left in the current big page for another frag? */
1428 if ((page_offset + rx_frag_size + rx_frag_size) >
1429 adapter->big_page_size) {
1431 page_info->last_page_user = true;
1434 prev_page_info = page_info;
1435 queue_head_inc(rxq);
1436 page_info = &page_info_tbl[rxq->head];
1439 prev_page_info->last_page_user = true;
1442 atomic_add(posted, &rxq->used);
1443 be_rxq_notify(adapter, rxq->id, posted);
1444 } else if (atomic_read(&rxq->used) == 0) {
1445 /* Let be_worker replenish when memory is available */
1446 rxo->rx_post_starved = true;
1450 static struct be_eth_tx_compl *be_tx_compl_get(struct be_queue_info *tx_cq)
1452 struct be_eth_tx_compl *txcp = queue_tail_node(tx_cq);
1454 if (txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] == 0)
1458 be_dws_le_to_cpu(txcp, sizeof(*txcp));
1460 txcp->dw[offsetof(struct amap_eth_tx_compl, valid) / 32] = 0;
1462 queue_tail_inc(tx_cq);
1466 static u16 be_tx_compl_process(struct be_adapter *adapter,
1467 struct be_tx_obj *txo, u16 last_index)
1469 struct be_queue_info *txq = &txo->q;
1470 struct be_eth_wrb *wrb;
1471 struct sk_buff **sent_skbs = txo->sent_skb_list;
1472 struct sk_buff *sent_skb;
1473 u16 cur_index, num_wrbs = 1; /* account for hdr wrb */
1474 bool unmap_skb_hdr = true;
1476 sent_skb = sent_skbs[txq->tail];
1478 sent_skbs[txq->tail] = NULL;
1480 /* skip header wrb */
1481 queue_tail_inc(txq);
1484 cur_index = txq->tail;
1485 wrb = queue_tail_node(txq);
1486 unmap_tx_frag(&adapter->pdev->dev, wrb,
1487 (unmap_skb_hdr && skb_headlen(sent_skb)));
1488 unmap_skb_hdr = false;
1491 queue_tail_inc(txq);
1492 } while (cur_index != last_index);
1494 kfree_skb(sent_skb);
1498 static inline struct be_eq_entry *event_get(struct be_eq_obj *eq_obj)
1500 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1506 eqe->evt = le32_to_cpu(eqe->evt);
1507 queue_tail_inc(&eq_obj->q);
1511 static int event_handle(struct be_adapter *adapter,
1512 struct be_eq_obj *eq_obj,
1515 struct be_eq_entry *eqe;
1518 while ((eqe = event_get(eq_obj)) != NULL) {
1523 /* Deal with any spurious interrupts that come
1529 be_eq_notify(adapter, eq_obj->q.id, rearm, true, num);
1531 napi_schedule(&eq_obj->napi);
1536 /* Just read and notify events without processing them.
1537 * Used at the time of destroying event queues */
1538 static void be_eq_clean(struct be_adapter *adapter,
1539 struct be_eq_obj *eq_obj)
1541 struct be_eq_entry *eqe;
1544 while ((eqe = event_get(eq_obj)) != NULL) {
1550 be_eq_notify(adapter, eq_obj->q.id, false, true, num);
1553 static void be_rx_q_clean(struct be_adapter *adapter, struct be_rx_obj *rxo)
1555 struct be_rx_page_info *page_info;
1556 struct be_queue_info *rxq = &rxo->q;
1557 struct be_queue_info *rx_cq = &rxo->cq;
1558 struct be_rx_compl_info *rxcp;
1561 /* First cleanup pending rx completions */
1562 while ((rxcp = be_rx_compl_get(rxo)) != NULL) {
1563 be_rx_compl_discard(adapter, rxo, rxcp);
1564 be_cq_notify(adapter, rx_cq->id, false, 1);
1567 /* Then free posted rx buffer that were not used */
1568 tail = (rxq->head + rxq->len - atomic_read(&rxq->used)) % rxq->len;
1569 for (; atomic_read(&rxq->used) > 0; index_inc(&tail, rxq->len)) {
1570 page_info = get_rx_page_info(adapter, rxo, tail);
1571 put_page(page_info->page);
1572 memset(page_info, 0, sizeof(*page_info));
1574 BUG_ON(atomic_read(&rxq->used));
1575 rxq->tail = rxq->head = 0;
1578 static void be_tx_compl_clean(struct be_adapter *adapter,
1579 struct be_tx_obj *txo)
1581 struct be_queue_info *tx_cq = &txo->cq;
1582 struct be_queue_info *txq = &txo->q;
1583 struct be_eth_tx_compl *txcp;
1584 u16 end_idx, cmpl = 0, timeo = 0, num_wrbs = 0;
1585 struct sk_buff **sent_skbs = txo->sent_skb_list;
1586 struct sk_buff *sent_skb;
1589 /* Wait for a max of 200ms for all the tx-completions to arrive. */
1591 while ((txcp = be_tx_compl_get(tx_cq))) {
1592 end_idx = AMAP_GET_BITS(struct amap_eth_tx_compl,
1594 num_wrbs += be_tx_compl_process(adapter, txo, end_idx);
1598 be_cq_notify(adapter, tx_cq->id, false, cmpl);
1599 atomic_sub(num_wrbs, &txq->used);
1604 if (atomic_read(&txq->used) == 0 || ++timeo > 200)
1610 if (atomic_read(&txq->used))
1611 dev_err(&adapter->pdev->dev, "%d pending tx-completions\n",
1612 atomic_read(&txq->used));
1614 /* free posted tx for which compls will never arrive */
1615 while (atomic_read(&txq->used)) {
1616 sent_skb = sent_skbs[txq->tail];
1617 end_idx = txq->tail;
1619 wrb_cnt_for_skb(adapter, sent_skb, &dummy_wrb) - 1,
1621 num_wrbs = be_tx_compl_process(adapter, txo, end_idx);
1622 atomic_sub(num_wrbs, &txq->used);
1626 static void be_mcc_queues_destroy(struct be_adapter *adapter)
1628 struct be_queue_info *q;
1630 q = &adapter->mcc_obj.q;
1632 be_cmd_q_destroy(adapter, q, QTYPE_MCCQ);
1633 be_queue_free(adapter, q);
1635 q = &adapter->mcc_obj.cq;
1637 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1638 be_queue_free(adapter, q);
1641 /* Must be called only after TX qs are created as MCC shares TX EQ */
1642 static int be_mcc_queues_create(struct be_adapter *adapter)
1644 struct be_queue_info *q, *cq;
1646 /* Alloc MCC compl queue */
1647 cq = &adapter->mcc_obj.cq;
1648 if (be_queue_alloc(adapter, cq, MCC_CQ_LEN,
1649 sizeof(struct be_mcc_compl)))
1652 /* Ask BE to create MCC compl queue; share TX's eq */
1653 if (be_cmd_cq_create(adapter, cq, &adapter->tx_eq.q, false, true, 0))
1656 /* Alloc MCC queue */
1657 q = &adapter->mcc_obj.q;
1658 if (be_queue_alloc(adapter, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
1659 goto mcc_cq_destroy;
1661 /* Ask BE to create MCC queue */
1662 if (be_cmd_mccq_create(adapter, q, cq))
1668 be_queue_free(adapter, q);
1670 be_cmd_q_destroy(adapter, cq, QTYPE_CQ);
1672 be_queue_free(adapter, cq);
1677 static void be_tx_queues_destroy(struct be_adapter *adapter)
1679 struct be_queue_info *q;
1680 struct be_tx_obj *txo;
1683 for_all_tx_queues(adapter, txo, i) {
1686 be_cmd_q_destroy(adapter, q, QTYPE_TXQ);
1687 be_queue_free(adapter, q);
1691 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1692 be_queue_free(adapter, q);
1695 /* Clear any residual events */
1696 be_eq_clean(adapter, &adapter->tx_eq);
1698 q = &adapter->tx_eq.q;
1700 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
1701 be_queue_free(adapter, q);
1704 /* One TX event queue is shared by all TX compl qs */
1705 static int be_tx_queues_create(struct be_adapter *adapter)
1707 struct be_queue_info *eq, *q, *cq;
1708 struct be_tx_obj *txo;
1711 adapter->tx_eq.max_eqd = 0;
1712 adapter->tx_eq.min_eqd = 0;
1713 adapter->tx_eq.cur_eqd = 96;
1714 adapter->tx_eq.enable_aic = false;
1716 eq = &adapter->tx_eq.q;
1717 if (be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1718 sizeof(struct be_eq_entry)))
1721 if (be_cmd_eq_create(adapter, eq, adapter->tx_eq.cur_eqd))
1723 adapter->tx_eq.eq_idx = adapter->eq_next_idx++;
1725 for_all_tx_queues(adapter, txo, i) {
1727 if (be_queue_alloc(adapter, cq, TX_CQ_LEN,
1728 sizeof(struct be_eth_tx_compl)))
1731 if (be_cmd_cq_create(adapter, cq, eq, false, false, 3))
1735 if (be_queue_alloc(adapter, q, TX_Q_LEN,
1736 sizeof(struct be_eth_wrb)))
1739 if (be_cmd_txq_create(adapter, q, cq))
1745 be_tx_queues_destroy(adapter);
1749 static void be_rx_queues_destroy(struct be_adapter *adapter)
1751 struct be_queue_info *q;
1752 struct be_rx_obj *rxo;
1755 for_all_rx_queues(adapter, rxo, i) {
1756 be_queue_free(adapter, &rxo->q);
1760 be_cmd_q_destroy(adapter, q, QTYPE_CQ);
1761 be_queue_free(adapter, q);
1765 be_cmd_q_destroy(adapter, q, QTYPE_EQ);
1766 be_queue_free(adapter, q);
1770 static u32 be_num_rxqs_want(struct be_adapter *adapter)
1772 if ((adapter->function_caps & BE_FUNCTION_CAPS_RSS) &&
1773 !adapter->sriov_enabled && !(adapter->function_mode & 0x400)) {
1774 return 1 + MAX_RSS_QS; /* one default non-RSS queue */
1776 dev_warn(&adapter->pdev->dev,
1777 "No support for multiple RX queues\n");
1782 static int be_rx_queues_create(struct be_adapter *adapter)
1784 struct be_queue_info *eq, *q, *cq;
1785 struct be_rx_obj *rxo;
1788 adapter->num_rx_qs = min(be_num_rxqs_want(adapter),
1789 msix_enabled(adapter) ?
1790 adapter->num_msix_vec - 1 : 1);
1791 if (adapter->num_rx_qs != MAX_RX_QS)
1792 dev_warn(&adapter->pdev->dev,
1793 "Can create only %d RX queues", adapter->num_rx_qs);
1795 adapter->big_page_size = (1 << get_order(rx_frag_size)) * PAGE_SIZE;
1796 for_all_rx_queues(adapter, rxo, i) {
1797 rxo->adapter = adapter;
1798 rxo->rx_eq.max_eqd = BE_MAX_EQD;
1799 rxo->rx_eq.enable_aic = true;
1803 rc = be_queue_alloc(adapter, eq, EVNT_Q_LEN,
1804 sizeof(struct be_eq_entry));
1808 rc = be_cmd_eq_create(adapter, eq, rxo->rx_eq.cur_eqd);
1812 rxo->rx_eq.eq_idx = adapter->eq_next_idx++;
1816 rc = be_queue_alloc(adapter, cq, RX_CQ_LEN,
1817 sizeof(struct be_eth_rx_compl));
1821 rc = be_cmd_cq_create(adapter, cq, eq, false, false, 3);
1825 /* Rx Q - will be created in be_open() */
1827 rc = be_queue_alloc(adapter, q, RX_Q_LEN,
1828 sizeof(struct be_eth_rx_d));
1836 be_rx_queues_destroy(adapter);
1840 static bool event_peek(struct be_eq_obj *eq_obj)
1842 struct be_eq_entry *eqe = queue_tail_node(&eq_obj->q);
1849 static irqreturn_t be_intx(int irq, void *dev)
1851 struct be_adapter *adapter = dev;
1852 struct be_rx_obj *rxo;
1853 int isr, i, tx = 0 , rx = 0;
1855 if (lancer_chip(adapter)) {
1856 if (event_peek(&adapter->tx_eq))
1857 tx = event_handle(adapter, &adapter->tx_eq, false);
1858 for_all_rx_queues(adapter, rxo, i) {
1859 if (event_peek(&rxo->rx_eq))
1860 rx |= event_handle(adapter, &rxo->rx_eq, true);
1867 isr = ioread32(adapter->csr + CEV_ISR0_OFFSET +
1868 (adapter->tx_eq.q.id / 8) * CEV_ISR_SIZE);
1872 if ((1 << adapter->tx_eq.eq_idx & isr))
1873 event_handle(adapter, &adapter->tx_eq, false);
1875 for_all_rx_queues(adapter, rxo, i) {
1876 if ((1 << rxo->rx_eq.eq_idx & isr))
1877 event_handle(adapter, &rxo->rx_eq, true);
1884 static irqreturn_t be_msix_rx(int irq, void *dev)
1886 struct be_rx_obj *rxo = dev;
1887 struct be_adapter *adapter = rxo->adapter;
1889 event_handle(adapter, &rxo->rx_eq, true);
1894 static irqreturn_t be_msix_tx_mcc(int irq, void *dev)
1896 struct be_adapter *adapter = dev;
1898 event_handle(adapter, &adapter->tx_eq, false);
1903 static inline bool do_gro(struct be_rx_compl_info *rxcp)
1905 return (rxcp->tcpf && !rxcp->err) ? true : false;
1908 static int be_poll_rx(struct napi_struct *napi, int budget)
1910 struct be_eq_obj *rx_eq = container_of(napi, struct be_eq_obj, napi);
1911 struct be_rx_obj *rxo = container_of(rx_eq, struct be_rx_obj, rx_eq);
1912 struct be_adapter *adapter = rxo->adapter;
1913 struct be_queue_info *rx_cq = &rxo->cq;
1914 struct be_rx_compl_info *rxcp;
1917 rxo->stats.rx_polls++;
1918 for (work_done = 0; work_done < budget; work_done++) {
1919 rxcp = be_rx_compl_get(rxo);
1923 /* Ignore flush completions */
1924 if (rxcp->num_rcvd && rxcp->pkt_size) {
1926 be_rx_compl_process_gro(adapter, rxo, rxcp);
1928 be_rx_compl_process(adapter, rxo, rxcp);
1929 } else if (rxcp->pkt_size == 0) {
1930 be_rx_compl_discard(adapter, rxo, rxcp);
1933 be_rx_stats_update(rxo, rxcp);
1936 /* Refill the queue */
1937 if (atomic_read(&rxo->q.used) < RX_FRAGS_REFILL_WM)
1938 be_post_rx_frags(rxo, GFP_ATOMIC);
1941 if (work_done < budget) {
1942 napi_complete(napi);
1943 be_cq_notify(adapter, rx_cq->id, true, work_done);
1945 /* More to be consumed; continue with interrupts disabled */
1946 be_cq_notify(adapter, rx_cq->id, false, work_done);
1951 /* As TX and MCC share the same EQ check for both TX and MCC completions.
1952 * For TX/MCC we don't honour budget; consume everything
1954 static int be_poll_tx_mcc(struct napi_struct *napi, int budget)
1956 struct be_eq_obj *tx_eq = container_of(napi, struct be_eq_obj, napi);
1957 struct be_adapter *adapter =
1958 container_of(tx_eq, struct be_adapter, tx_eq);
1959 struct be_tx_obj *txo;
1960 struct be_eth_tx_compl *txcp;
1961 int tx_compl, mcc_compl, status = 0;
1965 for_all_tx_queues(adapter, txo, i) {
1968 while ((txcp = be_tx_compl_get(&txo->cq))) {
1969 num_wrbs += be_tx_compl_process(adapter, txo,
1970 AMAP_GET_BITS(struct amap_eth_tx_compl,
1975 be_cq_notify(adapter, txo->cq.id, true, tx_compl);
1977 atomic_sub(num_wrbs, &txo->q.used);
1979 /* As Tx wrbs have been freed up, wake up netdev queue
1980 * if it was stopped due to lack of tx wrbs. */
1981 if (__netif_subqueue_stopped(adapter->netdev, i) &&
1982 atomic_read(&txo->q.used) < txo->q.len / 2) {
1983 netif_wake_subqueue(adapter->netdev, i);
1986 adapter->drv_stats.be_tx_events++;
1987 txo->stats.be_tx_compl += tx_compl;
1991 mcc_compl = be_process_mcc(adapter, &status);
1994 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
1995 be_cq_notify(adapter, mcc_obj->cq.id, true, mcc_compl);
1998 napi_complete(napi);
2000 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
2004 void be_detect_dump_ue(struct be_adapter *adapter)
2006 u32 ue_status_lo, ue_status_hi, ue_status_lo_mask, ue_status_hi_mask;
2009 pci_read_config_dword(adapter->pdev,
2010 PCICFG_UE_STATUS_LOW, &ue_status_lo);
2011 pci_read_config_dword(adapter->pdev,
2012 PCICFG_UE_STATUS_HIGH, &ue_status_hi);
2013 pci_read_config_dword(adapter->pdev,
2014 PCICFG_UE_STATUS_LOW_MASK, &ue_status_lo_mask);
2015 pci_read_config_dword(adapter->pdev,
2016 PCICFG_UE_STATUS_HI_MASK, &ue_status_hi_mask);
2018 ue_status_lo = (ue_status_lo & (~ue_status_lo_mask));
2019 ue_status_hi = (ue_status_hi & (~ue_status_hi_mask));
2021 if (ue_status_lo || ue_status_hi) {
2022 adapter->ue_detected = true;
2023 adapter->eeh_err = true;
2024 dev_err(&adapter->pdev->dev, "UE Detected!!\n");
2028 for (i = 0; ue_status_lo; ue_status_lo >>= 1, i++) {
2029 if (ue_status_lo & 1)
2030 dev_err(&adapter->pdev->dev,
2031 "UE: %s bit set\n", ue_status_low_desc[i]);
2035 for (i = 0; ue_status_hi; ue_status_hi >>= 1, i++) {
2036 if (ue_status_hi & 1)
2037 dev_err(&adapter->pdev->dev,
2038 "UE: %s bit set\n", ue_status_hi_desc[i]);
2044 static void be_worker(struct work_struct *work)
2046 struct be_adapter *adapter =
2047 container_of(work, struct be_adapter, work.work);
2048 struct be_rx_obj *rxo;
2049 struct be_tx_obj *txo;
2052 if (!adapter->ue_detected && !lancer_chip(adapter))
2053 be_detect_dump_ue(adapter);
2055 /* when interrupts are not yet enabled, just reap any pending
2056 * mcc completions */
2057 if (!netif_running(adapter->netdev)) {
2058 int mcc_compl, status = 0;
2060 mcc_compl = be_process_mcc(adapter, &status);
2063 struct be_mcc_obj *mcc_obj = &adapter->mcc_obj;
2064 be_cq_notify(adapter, mcc_obj->cq.id, false, mcc_compl);
2070 if (!adapter->stats_cmd_sent) {
2071 if (lancer_chip(adapter))
2072 lancer_cmd_get_pport_stats(adapter,
2073 &adapter->stats_cmd);
2075 be_cmd_get_stats(adapter, &adapter->stats_cmd);
2078 for_all_tx_queues(adapter, txo, i)
2079 be_tx_rate_update(txo);
2081 for_all_rx_queues(adapter, rxo, i) {
2082 be_rx_rate_update(rxo);
2083 be_rx_eqd_update(adapter, rxo);
2085 if (rxo->rx_post_starved) {
2086 rxo->rx_post_starved = false;
2087 be_post_rx_frags(rxo, GFP_KERNEL);
2092 adapter->work_counter++;
2093 schedule_delayed_work(&adapter->work, msecs_to_jiffies(1000));
2096 static void be_msix_disable(struct be_adapter *adapter)
2098 if (msix_enabled(adapter)) {
2099 pci_disable_msix(adapter->pdev);
2100 adapter->num_msix_vec = 0;
2104 static void be_msix_enable(struct be_adapter *adapter)
2106 #define BE_MIN_MSIX_VECTORS (1 + 1) /* Rx + Tx */
2107 int i, status, num_vec;
2109 num_vec = be_num_rxqs_want(adapter) + 1;
2111 for (i = 0; i < num_vec; i++)
2112 adapter->msix_entries[i].entry = i;
2114 status = pci_enable_msix(adapter->pdev, adapter->msix_entries, num_vec);
2117 } else if (status >= BE_MIN_MSIX_VECTORS) {
2119 if (pci_enable_msix(adapter->pdev, adapter->msix_entries,
2125 adapter->num_msix_vec = num_vec;
2129 static void be_sriov_enable(struct be_adapter *adapter)
2131 be_check_sriov_fn_type(adapter);
2132 #ifdef CONFIG_PCI_IOV
2133 if (be_physfn(adapter) && num_vfs) {
2137 pos = pci_find_ext_capability(adapter->pdev,
2138 PCI_EXT_CAP_ID_SRIOV);
2139 pci_read_config_word(adapter->pdev,
2140 pos + PCI_SRIOV_TOTAL_VF, &nvfs);
2142 if (num_vfs > nvfs) {
2143 dev_info(&adapter->pdev->dev,
2144 "Device supports %d VFs and not %d\n",
2149 status = pci_enable_sriov(adapter->pdev, num_vfs);
2150 adapter->sriov_enabled = status ? false : true;
2155 static void be_sriov_disable(struct be_adapter *adapter)
2157 #ifdef CONFIG_PCI_IOV
2158 if (adapter->sriov_enabled) {
2159 pci_disable_sriov(adapter->pdev);
2160 adapter->sriov_enabled = false;
2165 static inline int be_msix_vec_get(struct be_adapter *adapter,
2166 struct be_eq_obj *eq_obj)
2168 return adapter->msix_entries[eq_obj->eq_idx].vector;
2171 static int be_request_irq(struct be_adapter *adapter,
2172 struct be_eq_obj *eq_obj,
2173 void *handler, char *desc, void *context)
2175 struct net_device *netdev = adapter->netdev;
2178 sprintf(eq_obj->desc, "%s-%s", netdev->name, desc);
2179 vec = be_msix_vec_get(adapter, eq_obj);
2180 return request_irq(vec, handler, 0, eq_obj->desc, context);
2183 static void be_free_irq(struct be_adapter *adapter, struct be_eq_obj *eq_obj,
2186 int vec = be_msix_vec_get(adapter, eq_obj);
2187 free_irq(vec, context);
2190 static int be_msix_register(struct be_adapter *adapter)
2192 struct be_rx_obj *rxo;
2196 status = be_request_irq(adapter, &adapter->tx_eq, be_msix_tx_mcc, "tx",
2201 for_all_rx_queues(adapter, rxo, i) {
2202 sprintf(qname, "rxq%d", i);
2203 status = be_request_irq(adapter, &rxo->rx_eq, be_msix_rx,
2212 be_free_irq(adapter, &adapter->tx_eq, adapter);
2214 for (i--, rxo = &adapter->rx_obj[i]; i >= 0; i--, rxo--)
2215 be_free_irq(adapter, &rxo->rx_eq, rxo);
2218 dev_warn(&adapter->pdev->dev,
2219 "MSIX Request IRQ failed - err %d\n", status);
2220 be_msix_disable(adapter);
2224 static int be_irq_register(struct be_adapter *adapter)
2226 struct net_device *netdev = adapter->netdev;
2229 if (msix_enabled(adapter)) {
2230 status = be_msix_register(adapter);
2233 /* INTx is not supported for VF */
2234 if (!be_physfn(adapter))
2239 netdev->irq = adapter->pdev->irq;
2240 status = request_irq(netdev->irq, be_intx, IRQF_SHARED, netdev->name,
2243 dev_err(&adapter->pdev->dev,
2244 "INTx request IRQ failed - err %d\n", status);
2248 adapter->isr_registered = true;
2252 static void be_irq_unregister(struct be_adapter *adapter)
2254 struct net_device *netdev = adapter->netdev;
2255 struct be_rx_obj *rxo;
2258 if (!adapter->isr_registered)
2262 if (!msix_enabled(adapter)) {
2263 free_irq(netdev->irq, adapter);
2268 be_free_irq(adapter, &adapter->tx_eq, adapter);
2270 for_all_rx_queues(adapter, rxo, i)
2271 be_free_irq(adapter, &rxo->rx_eq, rxo);
2274 adapter->isr_registered = false;
2277 static void be_rx_queues_clear(struct be_adapter *adapter)
2279 struct be_queue_info *q;
2280 struct be_rx_obj *rxo;
2283 for_all_rx_queues(adapter, rxo, i) {
2286 be_cmd_rxq_destroy(adapter, q);
2287 /* After the rxq is invalidated, wait for a grace time
2288 * of 1ms for all dma to end and the flush compl to
2292 be_rx_q_clean(adapter, rxo);
2295 /* Clear any residual events */
2298 be_eq_clean(adapter, &rxo->rx_eq);
2302 static int be_close(struct net_device *netdev)
2304 struct be_adapter *adapter = netdev_priv(netdev);
2305 struct be_rx_obj *rxo;
2306 struct be_tx_obj *txo;
2307 struct be_eq_obj *tx_eq = &adapter->tx_eq;
2310 be_async_mcc_disable(adapter);
2312 netif_carrier_off(netdev);
2313 adapter->link_up = false;
2315 if (!lancer_chip(adapter))
2316 be_intr_set(adapter, false);
2318 for_all_rx_queues(adapter, rxo, i)
2319 napi_disable(&rxo->rx_eq.napi);
2321 napi_disable(&tx_eq->napi);
2323 if (lancer_chip(adapter)) {
2324 be_cq_notify(adapter, adapter->mcc_obj.cq.id, false, 0);
2325 for_all_rx_queues(adapter, rxo, i)
2326 be_cq_notify(adapter, rxo->cq.id, false, 0);
2327 for_all_tx_queues(adapter, txo, i)
2328 be_cq_notify(adapter, txo->cq.id, false, 0);
2331 if (msix_enabled(adapter)) {
2332 vec = be_msix_vec_get(adapter, tx_eq);
2333 synchronize_irq(vec);
2335 for_all_rx_queues(adapter, rxo, i) {
2336 vec = be_msix_vec_get(adapter, &rxo->rx_eq);
2337 synchronize_irq(vec);
2340 synchronize_irq(netdev->irq);
2342 be_irq_unregister(adapter);
2344 /* Wait for all pending tx completions to arrive so that
2345 * all tx skbs are freed.
2347 for_all_tx_queues(adapter, txo, i)
2348 be_tx_compl_clean(adapter, txo);
2350 be_rx_queues_clear(adapter);
2354 static int be_rx_queues_setup(struct be_adapter *adapter)
2356 struct be_rx_obj *rxo;
2358 u8 rsstable[MAX_RSS_QS];
2360 for_all_rx_queues(adapter, rxo, i) {
2361 rc = be_cmd_rxq_create(adapter, &rxo->q, rxo->cq.id,
2362 rx_frag_size, BE_MAX_JUMBO_FRAME_SIZE,
2364 (i > 0) ? 1 : 0/* rss enable */, &rxo->rss_id);
2369 if (be_multi_rxq(adapter)) {
2370 for_all_rss_queues(adapter, rxo, i)
2371 rsstable[i] = rxo->rss_id;
2373 rc = be_cmd_rss_config(adapter, rsstable,
2374 adapter->num_rx_qs - 1);
2379 /* First time posting */
2380 for_all_rx_queues(adapter, rxo, i) {
2381 be_post_rx_frags(rxo, GFP_KERNEL);
2382 napi_enable(&rxo->rx_eq.napi);
2387 static int be_open(struct net_device *netdev)
2389 struct be_adapter *adapter = netdev_priv(netdev);
2390 struct be_eq_obj *tx_eq = &adapter->tx_eq;
2391 struct be_rx_obj *rxo;
2397 status = be_rx_queues_setup(adapter);
2401 napi_enable(&tx_eq->napi);
2403 be_irq_register(adapter);
2405 if (!lancer_chip(adapter))
2406 be_intr_set(adapter, true);
2408 /* The evt queues are created in unarmed state; arm them */
2409 for_all_rx_queues(adapter, rxo, i) {
2410 be_eq_notify(adapter, rxo->rx_eq.q.id, true, false, 0);
2411 be_cq_notify(adapter, rxo->cq.id, true, 0);
2413 be_eq_notify(adapter, tx_eq->q.id, true, false, 0);
2415 /* Now that interrupts are on we can process async mcc */
2416 be_async_mcc_enable(adapter);
2418 status = be_cmd_link_status_query(adapter, &link_up, &mac_speed,
2422 be_link_status_update(adapter, link_up);
2424 if (be_physfn(adapter)) {
2425 status = be_vid_config(adapter, false, 0);
2429 status = be_cmd_set_flow_control(adapter,
2430 adapter->tx_fc, adapter->rx_fc);
2437 be_close(adapter->netdev);
2441 static int be_setup_wol(struct be_adapter *adapter, bool enable)
2443 struct be_dma_mem cmd;
2447 memset(mac, 0, ETH_ALEN);
2449 cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
2450 cmd.va = dma_alloc_coherent(&adapter->pdev->dev, cmd.size, &cmd.dma,
2454 memset(cmd.va, 0, cmd.size);
2457 status = pci_write_config_dword(adapter->pdev,
2458 PCICFG_PM_CONTROL_OFFSET, PCICFG_PM_CONTROL_MASK);
2460 dev_err(&adapter->pdev->dev,
2461 "Could not enable Wake-on-lan\n");
2462 dma_free_coherent(&adapter->pdev->dev, cmd.size, cmd.va,
2466 status = be_cmd_enable_magic_wol(adapter,
2467 adapter->netdev->dev_addr, &cmd);
2468 pci_enable_wake(adapter->pdev, PCI_D3hot, 1);
2469 pci_enable_wake(adapter->pdev, PCI_D3cold, 1);
2471 status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
2472 pci_enable_wake(adapter->pdev, PCI_D3hot, 0);
2473 pci_enable_wake(adapter->pdev, PCI_D3cold, 0);
2476 dma_free_coherent(&adapter->pdev->dev, cmd.size, cmd.va, cmd.dma);
2481 * Generate a seed MAC address from the PF MAC Address using jhash.
2482 * MAC Address for VFs are assigned incrementally starting from the seed.
2483 * These addresses are programmed in the ASIC by the PF and the VF driver
2484 * queries for the MAC address during its probe.
2486 static inline int be_vf_eth_addr_config(struct be_adapter *adapter)
2492 be_vf_eth_addr_generate(adapter, mac);
2494 for (vf = 0; vf < num_vfs; vf++) {
2495 status = be_cmd_pmac_add(adapter, mac,
2496 adapter->vf_cfg[vf].vf_if_handle,
2497 &adapter->vf_cfg[vf].vf_pmac_id,
2500 dev_err(&adapter->pdev->dev,
2501 "Mac address add failed for VF %d\n", vf);
2503 memcpy(adapter->vf_cfg[vf].vf_mac_addr, mac, ETH_ALEN);
2510 static inline void be_vf_eth_addr_rem(struct be_adapter *adapter)
2514 for (vf = 0; vf < num_vfs; vf++) {
2515 if (adapter->vf_cfg[vf].vf_pmac_id != BE_INVALID_PMAC_ID)
2516 be_cmd_pmac_del(adapter,
2517 adapter->vf_cfg[vf].vf_if_handle,
2518 adapter->vf_cfg[vf].vf_pmac_id, vf + 1);
2522 static int be_setup(struct be_adapter *adapter)
2524 struct net_device *netdev = adapter->netdev;
2525 u32 cap_flags, en_flags, vf = 0;
2529 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED |
2530 BE_IF_FLAGS_BROADCAST |
2531 BE_IF_FLAGS_MULTICAST;
2533 if (be_physfn(adapter)) {
2534 cap_flags |= BE_IF_FLAGS_MCAST_PROMISCUOUS |
2535 BE_IF_FLAGS_PROMISCUOUS |
2536 BE_IF_FLAGS_PASS_L3L4_ERRORS;
2537 en_flags |= BE_IF_FLAGS_PASS_L3L4_ERRORS;
2539 if (adapter->function_caps & BE_FUNCTION_CAPS_RSS) {
2540 cap_flags |= BE_IF_FLAGS_RSS;
2541 en_flags |= BE_IF_FLAGS_RSS;
2545 status = be_cmd_if_create(adapter, cap_flags, en_flags,
2546 netdev->dev_addr, false/* pmac_invalid */,
2547 &adapter->if_handle, &adapter->pmac_id, 0);
2551 if (be_physfn(adapter)) {
2552 if (adapter->sriov_enabled) {
2553 while (vf < num_vfs) {
2554 cap_flags = en_flags = BE_IF_FLAGS_UNTAGGED |
2555 BE_IF_FLAGS_BROADCAST;
2556 status = be_cmd_if_create(adapter, cap_flags,
2557 en_flags, mac, true,
2558 &adapter->vf_cfg[vf].vf_if_handle,
2561 dev_err(&adapter->pdev->dev,
2562 "Interface Create failed for VF %d\n",
2566 adapter->vf_cfg[vf].vf_pmac_id =
2572 status = be_cmd_mac_addr_query(adapter, mac,
2573 MAC_ADDRESS_TYPE_NETWORK, false, adapter->if_handle);
2575 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
2576 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
2580 status = be_tx_queues_create(adapter);
2584 status = be_rx_queues_create(adapter);
2588 /* Allow all priorities by default. A GRP5 evt may modify this */
2589 adapter->vlan_prio_bmap = 0xff;
2591 status = be_mcc_queues_create(adapter);
2595 adapter->link_speed = -1;
2600 be_rx_queues_destroy(adapter);
2602 be_tx_queues_destroy(adapter);
2604 if (be_physfn(adapter) && adapter->sriov_enabled)
2605 for (vf = 0; vf < num_vfs; vf++)
2606 if (adapter->vf_cfg[vf].vf_if_handle)
2607 be_cmd_if_destroy(adapter,
2608 adapter->vf_cfg[vf].vf_if_handle,
2610 be_cmd_if_destroy(adapter, adapter->if_handle, 0);
2615 static int be_clear(struct be_adapter *adapter)
2619 if (be_physfn(adapter) && adapter->sriov_enabled)
2620 be_vf_eth_addr_rem(adapter);
2622 be_mcc_queues_destroy(adapter);
2623 be_rx_queues_destroy(adapter);
2624 be_tx_queues_destroy(adapter);
2625 adapter->eq_next_idx = 0;
2627 if (be_physfn(adapter) && adapter->sriov_enabled)
2628 for (vf = 0; vf < num_vfs; vf++)
2629 if (adapter->vf_cfg[vf].vf_if_handle)
2630 be_cmd_if_destroy(adapter,
2631 adapter->vf_cfg[vf].vf_if_handle,
2634 be_cmd_if_destroy(adapter, adapter->if_handle, 0);
2636 /* tell fw we're done with firing cmds */
2637 be_cmd_fw_clean(adapter);
2642 #define FW_FILE_HDR_SIGN "ServerEngines Corp. "
2643 static bool be_flash_redboot(struct be_adapter *adapter,
2644 const u8 *p, u32 img_start, int image_size,
2651 crc_offset = hdr_size + img_start + image_size - 4;
2655 status = be_cmd_get_flash_crc(adapter, flashed_crc,
2658 dev_err(&adapter->pdev->dev,
2659 "could not get crc from flash, not flashing redboot\n");
2663 /*update redboot only if crc does not match*/
2664 if (!memcmp(flashed_crc, p, 4))
2670 static int be_flash_data(struct be_adapter *adapter,
2671 const struct firmware *fw,
2672 struct be_dma_mem *flash_cmd, int num_of_images)
2675 int status = 0, i, filehdr_size = 0;
2676 u32 total_bytes = 0, flash_op;
2678 const u8 *p = fw->data;
2679 struct be_cmd_write_flashrom *req = flash_cmd->va;
2680 const struct flash_comp *pflashcomp;
2683 static const struct flash_comp gen3_flash_types[9] = {
2684 { FLASH_iSCSI_PRIMARY_IMAGE_START_g3, IMG_TYPE_ISCSI_ACTIVE,
2685 FLASH_IMAGE_MAX_SIZE_g3},
2686 { FLASH_REDBOOT_START_g3, IMG_TYPE_REDBOOT,
2687 FLASH_REDBOOT_IMAGE_MAX_SIZE_g3},
2688 { FLASH_iSCSI_BIOS_START_g3, IMG_TYPE_BIOS,
2689 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2690 { FLASH_PXE_BIOS_START_g3, IMG_TYPE_PXE_BIOS,
2691 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2692 { FLASH_FCoE_BIOS_START_g3, IMG_TYPE_FCOE_BIOS,
2693 FLASH_BIOS_IMAGE_MAX_SIZE_g3},
2694 { FLASH_iSCSI_BACKUP_IMAGE_START_g3, IMG_TYPE_ISCSI_BACKUP,
2695 FLASH_IMAGE_MAX_SIZE_g3},
2696 { FLASH_FCoE_PRIMARY_IMAGE_START_g3, IMG_TYPE_FCOE_FW_ACTIVE,
2697 FLASH_IMAGE_MAX_SIZE_g3},
2698 { FLASH_FCoE_BACKUP_IMAGE_START_g3, IMG_TYPE_FCOE_FW_BACKUP,
2699 FLASH_IMAGE_MAX_SIZE_g3},
2700 { FLASH_NCSI_START_g3, IMG_TYPE_NCSI_FW,
2701 FLASH_NCSI_IMAGE_MAX_SIZE_g3}
2703 static const struct flash_comp gen2_flash_types[8] = {
2704 { FLASH_iSCSI_PRIMARY_IMAGE_START_g2, IMG_TYPE_ISCSI_ACTIVE,
2705 FLASH_IMAGE_MAX_SIZE_g2},
2706 { FLASH_REDBOOT_START_g2, IMG_TYPE_REDBOOT,
2707 FLASH_REDBOOT_IMAGE_MAX_SIZE_g2},
2708 { FLASH_iSCSI_BIOS_START_g2, IMG_TYPE_BIOS,
2709 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2710 { FLASH_PXE_BIOS_START_g2, IMG_TYPE_PXE_BIOS,
2711 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2712 { FLASH_FCoE_BIOS_START_g2, IMG_TYPE_FCOE_BIOS,
2713 FLASH_BIOS_IMAGE_MAX_SIZE_g2},
2714 { FLASH_iSCSI_BACKUP_IMAGE_START_g2, IMG_TYPE_ISCSI_BACKUP,
2715 FLASH_IMAGE_MAX_SIZE_g2},
2716 { FLASH_FCoE_PRIMARY_IMAGE_START_g2, IMG_TYPE_FCOE_FW_ACTIVE,
2717 FLASH_IMAGE_MAX_SIZE_g2},
2718 { FLASH_FCoE_BACKUP_IMAGE_START_g2, IMG_TYPE_FCOE_FW_BACKUP,
2719 FLASH_IMAGE_MAX_SIZE_g2}
2722 if (adapter->generation == BE_GEN3) {
2723 pflashcomp = gen3_flash_types;
2724 filehdr_size = sizeof(struct flash_file_hdr_g3);
2725 num_comp = ARRAY_SIZE(gen3_flash_types);
2727 pflashcomp = gen2_flash_types;
2728 filehdr_size = sizeof(struct flash_file_hdr_g2);
2729 num_comp = ARRAY_SIZE(gen2_flash_types);
2731 for (i = 0; i < num_comp; i++) {
2732 if ((pflashcomp[i].optype == IMG_TYPE_NCSI_FW) &&
2733 memcmp(adapter->fw_ver, "3.102.148.0", 11) < 0)
2735 if ((pflashcomp[i].optype == IMG_TYPE_REDBOOT) &&
2736 (!be_flash_redboot(adapter, fw->data,
2737 pflashcomp[i].offset, pflashcomp[i].size, filehdr_size +
2738 (num_of_images * sizeof(struct image_hdr)))))
2741 p += filehdr_size + pflashcomp[i].offset
2742 + (num_of_images * sizeof(struct image_hdr));
2743 if (p + pflashcomp[i].size > fw->data + fw->size)
2745 total_bytes = pflashcomp[i].size;
2746 while (total_bytes) {
2747 if (total_bytes > 32*1024)
2748 num_bytes = 32*1024;
2750 num_bytes = total_bytes;
2751 total_bytes -= num_bytes;
2754 flash_op = FLASHROM_OPER_FLASH;
2756 flash_op = FLASHROM_OPER_SAVE;
2757 memcpy(req->params.data_buf, p, num_bytes);
2759 status = be_cmd_write_flashrom(adapter, flash_cmd,
2760 pflashcomp[i].optype, flash_op, num_bytes);
2762 dev_err(&adapter->pdev->dev,
2763 "cmd to write to flash rom failed.\n");
2771 static int get_ufigen_type(struct flash_file_hdr_g2 *fhdr)
2775 if (fhdr->build[0] == '3')
2777 else if (fhdr->build[0] == '2')
2783 static int lancer_fw_download(struct be_adapter *adapter,
2784 const struct firmware *fw)
2786 #define LANCER_FW_DOWNLOAD_CHUNK (32 * 1024)
2787 #define LANCER_FW_DOWNLOAD_LOCATION "/prg"
2788 struct be_dma_mem flash_cmd;
2789 const u8 *data_ptr = NULL;
2790 u8 *dest_image_ptr = NULL;
2791 size_t image_size = 0;
2793 u32 data_written = 0;
2798 if (!IS_ALIGNED(fw->size, sizeof(u32))) {
2799 dev_err(&adapter->pdev->dev,
2800 "FW Image not properly aligned. "
2801 "Length must be 4 byte aligned.\n");
2803 goto lancer_fw_exit;
2806 flash_cmd.size = sizeof(struct lancer_cmd_req_write_object)
2807 + LANCER_FW_DOWNLOAD_CHUNK;
2808 flash_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, flash_cmd.size,
2809 &flash_cmd.dma, GFP_KERNEL);
2810 if (!flash_cmd.va) {
2812 dev_err(&adapter->pdev->dev,
2813 "Memory allocation failure while flashing\n");
2814 goto lancer_fw_exit;
2817 dest_image_ptr = flash_cmd.va +
2818 sizeof(struct lancer_cmd_req_write_object);
2819 image_size = fw->size;
2820 data_ptr = fw->data;
2822 while (image_size) {
2823 chunk_size = min_t(u32, image_size, LANCER_FW_DOWNLOAD_CHUNK);
2825 /* Copy the image chunk content. */
2826 memcpy(dest_image_ptr, data_ptr, chunk_size);
2828 status = lancer_cmd_write_object(adapter, &flash_cmd,
2829 chunk_size, offset, LANCER_FW_DOWNLOAD_LOCATION,
2830 &data_written, &add_status);
2835 offset += data_written;
2836 data_ptr += data_written;
2837 image_size -= data_written;
2841 /* Commit the FW written */
2842 status = lancer_cmd_write_object(adapter, &flash_cmd,
2843 0, offset, LANCER_FW_DOWNLOAD_LOCATION,
2844 &data_written, &add_status);
2847 dma_free_coherent(&adapter->pdev->dev, flash_cmd.size, flash_cmd.va,
2850 dev_err(&adapter->pdev->dev,
2851 "Firmware load error. "
2852 "Status code: 0x%x Additional Status: 0x%x\n",
2853 status, add_status);
2854 goto lancer_fw_exit;
2857 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
2862 static int be_fw_download(struct be_adapter *adapter, const struct firmware* fw)
2864 struct flash_file_hdr_g2 *fhdr;
2865 struct flash_file_hdr_g3 *fhdr3;
2866 struct image_hdr *img_hdr_ptr = NULL;
2867 struct be_dma_mem flash_cmd;
2869 int status = 0, i = 0, num_imgs = 0;
2872 fhdr = (struct flash_file_hdr_g2 *) p;
2874 flash_cmd.size = sizeof(struct be_cmd_write_flashrom) + 32*1024;
2875 flash_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, flash_cmd.size,
2876 &flash_cmd.dma, GFP_KERNEL);
2877 if (!flash_cmd.va) {
2879 dev_err(&adapter->pdev->dev,
2880 "Memory allocation failure while flashing\n");
2884 if ((adapter->generation == BE_GEN3) &&
2885 (get_ufigen_type(fhdr) == BE_GEN3)) {
2886 fhdr3 = (struct flash_file_hdr_g3 *) fw->data;
2887 num_imgs = le32_to_cpu(fhdr3->num_imgs);
2888 for (i = 0; i < num_imgs; i++) {
2889 img_hdr_ptr = (struct image_hdr *) (fw->data +
2890 (sizeof(struct flash_file_hdr_g3) +
2891 i * sizeof(struct image_hdr)));
2892 if (le32_to_cpu(img_hdr_ptr->imageid) == 1)
2893 status = be_flash_data(adapter, fw, &flash_cmd,
2896 } else if ((adapter->generation == BE_GEN2) &&
2897 (get_ufigen_type(fhdr) == BE_GEN2)) {
2898 status = be_flash_data(adapter, fw, &flash_cmd, 0);
2900 dev_err(&adapter->pdev->dev,
2901 "UFI and Interface are not compatible for flashing\n");
2905 dma_free_coherent(&adapter->pdev->dev, flash_cmd.size, flash_cmd.va,
2908 dev_err(&adapter->pdev->dev, "Firmware load error\n");
2912 dev_info(&adapter->pdev->dev, "Firmware flashed successfully\n");
2918 int be_load_fw(struct be_adapter *adapter, u8 *fw_file)
2920 const struct firmware *fw;
2923 if (!netif_running(adapter->netdev)) {
2924 dev_err(&adapter->pdev->dev,
2925 "Firmware load not allowed (interface is down)\n");
2929 status = request_firmware(&fw, fw_file, &adapter->pdev->dev);
2933 dev_info(&adapter->pdev->dev, "Flashing firmware file %s\n", fw_file);
2935 if (lancer_chip(adapter))
2936 status = lancer_fw_download(adapter, fw);
2938 status = be_fw_download(adapter, fw);
2941 release_firmware(fw);
2945 static struct net_device_ops be_netdev_ops = {
2946 .ndo_open = be_open,
2947 .ndo_stop = be_close,
2948 .ndo_start_xmit = be_xmit,
2949 .ndo_set_rx_mode = be_set_multicast_list,
2950 .ndo_set_mac_address = be_mac_addr_set,
2951 .ndo_change_mtu = be_change_mtu,
2952 .ndo_validate_addr = eth_validate_addr,
2953 .ndo_vlan_rx_register = be_vlan_register,
2954 .ndo_vlan_rx_add_vid = be_vlan_add_vid,
2955 .ndo_vlan_rx_kill_vid = be_vlan_rem_vid,
2956 .ndo_set_vf_mac = be_set_vf_mac,
2957 .ndo_set_vf_vlan = be_set_vf_vlan,
2958 .ndo_set_vf_tx_rate = be_set_vf_tx_rate,
2959 .ndo_get_vf_config = be_get_vf_config
2962 static void be_netdev_init(struct net_device *netdev)
2964 struct be_adapter *adapter = netdev_priv(netdev);
2965 struct be_rx_obj *rxo;
2968 netdev->hw_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
2969 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM |
2971 if (be_multi_rxq(adapter))
2972 netdev->hw_features |= NETIF_F_RXHASH;
2974 netdev->features |= netdev->hw_features |
2975 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
2977 netdev->vlan_features |= NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
2978 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2980 netdev->flags |= IFF_MULTICAST;
2982 /* Default settings for Rx and Tx flow control */
2983 adapter->rx_fc = true;
2984 adapter->tx_fc = true;
2986 netif_set_gso_max_size(netdev, 65535);
2988 BE_SET_NETDEV_OPS(netdev, &be_netdev_ops);
2990 SET_ETHTOOL_OPS(netdev, &be_ethtool_ops);
2992 for_all_rx_queues(adapter, rxo, i)
2993 netif_napi_add(netdev, &rxo->rx_eq.napi, be_poll_rx,
2996 netif_napi_add(netdev, &adapter->tx_eq.napi, be_poll_tx_mcc,
3000 static void be_unmap_pci_bars(struct be_adapter *adapter)
3003 iounmap(adapter->csr);
3005 iounmap(adapter->db);
3006 if (adapter->pcicfg && be_physfn(adapter))
3007 iounmap(adapter->pcicfg);
3010 static int be_map_pci_bars(struct be_adapter *adapter)
3013 int pcicfg_reg, db_reg;
3015 if (lancer_chip(adapter)) {
3016 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 0),
3017 pci_resource_len(adapter->pdev, 0));
3024 if (be_physfn(adapter)) {
3025 addr = ioremap_nocache(pci_resource_start(adapter->pdev, 2),
3026 pci_resource_len(adapter->pdev, 2));
3029 adapter->csr = addr;
3032 if (adapter->generation == BE_GEN2) {
3037 if (be_physfn(adapter))
3042 addr = ioremap_nocache(pci_resource_start(adapter->pdev, db_reg),
3043 pci_resource_len(adapter->pdev, db_reg));
3048 if (be_physfn(adapter)) {
3049 addr = ioremap_nocache(
3050 pci_resource_start(adapter->pdev, pcicfg_reg),
3051 pci_resource_len(adapter->pdev, pcicfg_reg));
3054 adapter->pcicfg = addr;
3056 adapter->pcicfg = adapter->db + SRIOV_VF_PCICFG_OFFSET;
3060 be_unmap_pci_bars(adapter);
3065 static void be_ctrl_cleanup(struct be_adapter *adapter)
3067 struct be_dma_mem *mem = &adapter->mbox_mem_alloced;
3069 be_unmap_pci_bars(adapter);
3072 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
3075 mem = &adapter->mc_cmd_mem;
3077 dma_free_coherent(&adapter->pdev->dev, mem->size, mem->va,
3081 static int be_ctrl_init(struct be_adapter *adapter)
3083 struct be_dma_mem *mbox_mem_alloc = &adapter->mbox_mem_alloced;
3084 struct be_dma_mem *mbox_mem_align = &adapter->mbox_mem;
3085 struct be_dma_mem *mc_cmd_mem = &adapter->mc_cmd_mem;
3088 status = be_map_pci_bars(adapter);
3092 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
3093 mbox_mem_alloc->va = dma_alloc_coherent(&adapter->pdev->dev,
3094 mbox_mem_alloc->size,
3095 &mbox_mem_alloc->dma,
3097 if (!mbox_mem_alloc->va) {
3099 goto unmap_pci_bars;
3102 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
3103 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
3104 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
3105 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
3107 mc_cmd_mem->size = sizeof(struct be_cmd_req_mcast_mac_config);
3108 mc_cmd_mem->va = dma_alloc_coherent(&adapter->pdev->dev,
3109 mc_cmd_mem->size, &mc_cmd_mem->dma,
3111 if (mc_cmd_mem->va == NULL) {
3115 memset(mc_cmd_mem->va, 0, mc_cmd_mem->size);
3117 mutex_init(&adapter->mbox_lock);
3118 spin_lock_init(&adapter->mcc_lock);
3119 spin_lock_init(&adapter->mcc_cq_lock);
3121 init_completion(&adapter->flash_compl);
3122 pci_save_state(adapter->pdev);
3126 dma_free_coherent(&adapter->pdev->dev, mbox_mem_alloc->size,
3127 mbox_mem_alloc->va, mbox_mem_alloc->dma);
3130 be_unmap_pci_bars(adapter);
3136 static void be_stats_cleanup(struct be_adapter *adapter)
3138 struct be_dma_mem *cmd = &adapter->stats_cmd;
3141 dma_free_coherent(&adapter->pdev->dev, cmd->size,
3145 static int be_stats_init(struct be_adapter *adapter)
3147 struct be_dma_mem *cmd = &adapter->stats_cmd;
3149 if (adapter->generation == BE_GEN2) {
3150 cmd->size = sizeof(struct be_cmd_req_get_stats_v0);
3152 if (lancer_chip(adapter))
3153 cmd->size = sizeof(struct lancer_cmd_req_pport_stats);
3155 cmd->size = sizeof(struct be_cmd_req_get_stats_v1);
3157 cmd->va = dma_alloc_coherent(&adapter->pdev->dev, cmd->size, &cmd->dma,
3159 if (cmd->va == NULL)
3161 memset(cmd->va, 0, cmd->size);
3165 static void __devexit be_remove(struct pci_dev *pdev)
3167 struct be_adapter *adapter = pci_get_drvdata(pdev);
3172 cancel_delayed_work_sync(&adapter->work);
3174 unregister_netdev(adapter->netdev);
3178 be_stats_cleanup(adapter);
3180 be_ctrl_cleanup(adapter);
3182 kfree(adapter->vf_cfg);
3183 be_sriov_disable(adapter);
3185 be_msix_disable(adapter);
3187 pci_set_drvdata(pdev, NULL);
3188 pci_release_regions(pdev);
3189 pci_disable_device(pdev);
3191 free_netdev(adapter->netdev);
3194 static int be_get_config(struct be_adapter *adapter)
3199 status = be_cmd_get_fw_ver(adapter, adapter->fw_ver);
3203 status = be_cmd_query_fw_cfg(adapter, &adapter->port_num,
3204 &adapter->function_mode, &adapter->function_caps);
3208 memset(mac, 0, ETH_ALEN);
3210 /* A default permanent address is given to each VF for Lancer*/
3211 if (be_physfn(adapter) || lancer_chip(adapter)) {
3212 status = be_cmd_mac_addr_query(adapter, mac,
3213 MAC_ADDRESS_TYPE_NETWORK, true /*permanent */, 0);
3218 if (!is_valid_ether_addr(mac))
3219 return -EADDRNOTAVAIL;
3221 memcpy(adapter->netdev->dev_addr, mac, ETH_ALEN);
3222 memcpy(adapter->netdev->perm_addr, mac, ETH_ALEN);
3225 if (adapter->function_mode & 0x400)
3226 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED/4;
3228 adapter->max_vlans = BE_NUM_VLANS_SUPPORTED;
3230 status = be_cmd_get_cntl_attributes(adapter);
3234 be_cmd_check_native_mode(adapter);
3236 if ((num_vfs && adapter->sriov_enabled) ||
3237 (adapter->function_mode & 0x400) ||
3238 lancer_chip(adapter) || !be_physfn(adapter)) {
3239 adapter->num_tx_qs = 1;
3240 netif_set_real_num_tx_queues(adapter->netdev,
3241 adapter->num_tx_qs);
3243 adapter->num_tx_qs = MAX_TX_QS;
3249 static int be_dev_family_check(struct be_adapter *adapter)
3251 struct pci_dev *pdev = adapter->pdev;
3252 u32 sli_intf = 0, if_type;
3254 switch (pdev->device) {
3257 adapter->generation = BE_GEN2;
3261 adapter->generation = BE_GEN3;
3265 pci_read_config_dword(pdev, SLI_INTF_REG_OFFSET, &sli_intf);
3266 if_type = (sli_intf & SLI_INTF_IF_TYPE_MASK) >>
3267 SLI_INTF_IF_TYPE_SHIFT;
3269 if (((sli_intf & SLI_INTF_VALID_MASK) != SLI_INTF_VALID) ||
3271 dev_err(&pdev->dev, "SLI_INTF reg val is not valid\n");
3274 adapter->sli_family = ((sli_intf & SLI_INTF_FAMILY_MASK) >>
3275 SLI_INTF_FAMILY_SHIFT);
3276 adapter->generation = BE_GEN3;
3279 adapter->generation = 0;
3284 static int lancer_wait_ready(struct be_adapter *adapter)
3286 #define SLIPORT_READY_TIMEOUT 500
3290 for (i = 0; i < SLIPORT_READY_TIMEOUT; i++) {
3291 sliport_status = ioread32(adapter->db + SLIPORT_STATUS_OFFSET);
3292 if (sliport_status & SLIPORT_STATUS_RDY_MASK)
3298 if (i == SLIPORT_READY_TIMEOUT)
3304 static int lancer_test_and_set_rdy_state(struct be_adapter *adapter)
3307 u32 sliport_status, err, reset_needed;
3308 status = lancer_wait_ready(adapter);
3310 sliport_status = ioread32(adapter->db + SLIPORT_STATUS_OFFSET);
3311 err = sliport_status & SLIPORT_STATUS_ERR_MASK;
3312 reset_needed = sliport_status & SLIPORT_STATUS_RN_MASK;
3313 if (err && reset_needed) {
3314 iowrite32(SLI_PORT_CONTROL_IP_MASK,
3315 adapter->db + SLIPORT_CONTROL_OFFSET);
3317 /* check adapter has corrected the error */
3318 status = lancer_wait_ready(adapter);
3319 sliport_status = ioread32(adapter->db +
3320 SLIPORT_STATUS_OFFSET);
3321 sliport_status &= (SLIPORT_STATUS_ERR_MASK |
3322 SLIPORT_STATUS_RN_MASK);
3323 if (status || sliport_status)
3325 } else if (err || reset_needed) {
3332 static int __devinit be_probe(struct pci_dev *pdev,
3333 const struct pci_device_id *pdev_id)
3336 struct be_adapter *adapter;
3337 struct net_device *netdev;
3339 status = pci_enable_device(pdev);
3343 status = pci_request_regions(pdev, DRV_NAME);
3346 pci_set_master(pdev);
3348 netdev = alloc_etherdev_mq(sizeof(struct be_adapter), MAX_TX_QS);
3349 if (netdev == NULL) {
3353 adapter = netdev_priv(netdev);
3354 adapter->pdev = pdev;
3355 pci_set_drvdata(pdev, adapter);
3357 status = be_dev_family_check(adapter);
3361 adapter->netdev = netdev;
3362 SET_NETDEV_DEV(netdev, &pdev->dev);
3364 status = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
3366 netdev->features |= NETIF_F_HIGHDMA;
3368 status = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3370 dev_err(&pdev->dev, "Could not set PCI DMA Mask\n");
3375 be_sriov_enable(adapter);
3376 if (adapter->sriov_enabled) {
3377 adapter->vf_cfg = kcalloc(num_vfs,
3378 sizeof(struct be_vf_cfg), GFP_KERNEL);
3380 if (!adapter->vf_cfg)
3384 status = be_ctrl_init(adapter);
3388 if (lancer_chip(adapter)) {
3389 status = lancer_test_and_set_rdy_state(adapter);
3391 dev_err(&pdev->dev, "Adapter in non recoverable error\n");
3396 /* sync up with fw's ready state */
3397 if (be_physfn(adapter)) {
3398 status = be_cmd_POST(adapter);
3403 /* tell fw we're ready to fire cmds */
3404 status = be_cmd_fw_init(adapter);
3408 status = be_cmd_reset_function(adapter);
3412 status = be_stats_init(adapter);
3416 status = be_get_config(adapter);
3420 /* The INTR bit may be set in the card when probed by a kdump kernel
3423 if (!lancer_chip(adapter))
3424 be_intr_set(adapter, false);
3426 be_msix_enable(adapter);
3428 INIT_DELAYED_WORK(&adapter->work, be_worker);
3430 status = be_setup(adapter);
3434 be_netdev_init(netdev);
3435 status = register_netdev(netdev);
3438 netif_carrier_off(netdev);
3440 if (be_physfn(adapter) && adapter->sriov_enabled) {
3445 if (!lancer_chip(adapter)) {
3446 status = be_vf_eth_addr_config(adapter);
3451 for (vf = 0; vf < num_vfs; vf++) {
3452 status = be_cmd_link_status_query(adapter, &link_up,
3453 &mac_speed, &lnk_speed, vf + 1);
3455 adapter->vf_cfg[vf].vf_tx_rate = lnk_speed * 10;
3461 dev_info(&pdev->dev, "%s port %d\n", nic_name(pdev), adapter->port_num);
3463 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
3467 unregister_netdev(netdev);
3471 be_msix_disable(adapter);
3473 be_stats_cleanup(adapter);
3475 be_ctrl_cleanup(adapter);
3477 kfree(adapter->vf_cfg);
3479 be_sriov_disable(adapter);
3480 free_netdev(netdev);
3481 pci_set_drvdata(pdev, NULL);
3483 pci_release_regions(pdev);
3485 pci_disable_device(pdev);
3487 dev_err(&pdev->dev, "%s initialization failed\n", nic_name(pdev));
3491 static int be_suspend(struct pci_dev *pdev, pm_message_t state)
3493 struct be_adapter *adapter = pci_get_drvdata(pdev);
3494 struct net_device *netdev = adapter->netdev;
3496 cancel_delayed_work_sync(&adapter->work);
3498 be_setup_wol(adapter, true);
3500 netif_device_detach(netdev);
3501 if (netif_running(netdev)) {
3506 be_cmd_get_flow_control(adapter, &adapter->tx_fc, &adapter->rx_fc);
3509 be_msix_disable(adapter);
3510 pci_save_state(pdev);
3511 pci_disable_device(pdev);
3512 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3516 static int be_resume(struct pci_dev *pdev)
3519 struct be_adapter *adapter = pci_get_drvdata(pdev);
3520 struct net_device *netdev = adapter->netdev;
3522 netif_device_detach(netdev);
3524 status = pci_enable_device(pdev);
3528 pci_set_power_state(pdev, 0);
3529 pci_restore_state(pdev);
3531 be_msix_enable(adapter);
3532 /* tell fw we're ready to fire cmds */
3533 status = be_cmd_fw_init(adapter);
3538 if (netif_running(netdev)) {
3543 netif_device_attach(netdev);
3546 be_setup_wol(adapter, false);
3548 schedule_delayed_work(&adapter->work, msecs_to_jiffies(100));
3553 * An FLR will stop BE from DMAing any data.
3555 static void be_shutdown(struct pci_dev *pdev)
3557 struct be_adapter *adapter = pci_get_drvdata(pdev);
3562 cancel_delayed_work_sync(&adapter->work);
3564 netif_device_detach(adapter->netdev);
3567 be_setup_wol(adapter, true);
3569 be_cmd_reset_function(adapter);
3571 pci_disable_device(pdev);
3574 static pci_ers_result_t be_eeh_err_detected(struct pci_dev *pdev,
3575 pci_channel_state_t state)
3577 struct be_adapter *adapter = pci_get_drvdata(pdev);
3578 struct net_device *netdev = adapter->netdev;
3580 dev_err(&adapter->pdev->dev, "EEH error detected\n");
3582 adapter->eeh_err = true;
3584 netif_device_detach(netdev);
3586 if (netif_running(netdev)) {
3593 if (state == pci_channel_io_perm_failure)
3594 return PCI_ERS_RESULT_DISCONNECT;
3596 pci_disable_device(pdev);
3598 return PCI_ERS_RESULT_NEED_RESET;
3601 static pci_ers_result_t be_eeh_reset(struct pci_dev *pdev)
3603 struct be_adapter *adapter = pci_get_drvdata(pdev);
3606 dev_info(&adapter->pdev->dev, "EEH reset\n");
3607 adapter->eeh_err = false;
3609 status = pci_enable_device(pdev);
3611 return PCI_ERS_RESULT_DISCONNECT;
3613 pci_set_master(pdev);
3614 pci_set_power_state(pdev, 0);
3615 pci_restore_state(pdev);
3617 /* Check if card is ok and fw is ready */
3618 status = be_cmd_POST(adapter);
3620 return PCI_ERS_RESULT_DISCONNECT;
3622 return PCI_ERS_RESULT_RECOVERED;
3625 static void be_eeh_resume(struct pci_dev *pdev)
3628 struct be_adapter *adapter = pci_get_drvdata(pdev);
3629 struct net_device *netdev = adapter->netdev;
3631 dev_info(&adapter->pdev->dev, "EEH resume\n");
3633 pci_save_state(pdev);
3635 /* tell fw we're ready to fire cmds */
3636 status = be_cmd_fw_init(adapter);
3640 status = be_setup(adapter);
3644 if (netif_running(netdev)) {
3645 status = be_open(netdev);
3649 netif_device_attach(netdev);
3652 dev_err(&adapter->pdev->dev, "EEH resume failed\n");
3655 static struct pci_error_handlers be_eeh_handlers = {
3656 .error_detected = be_eeh_err_detected,
3657 .slot_reset = be_eeh_reset,
3658 .resume = be_eeh_resume,
3661 static struct pci_driver be_driver = {
3663 .id_table = be_dev_ids,
3665 .remove = be_remove,
3666 .suspend = be_suspend,
3667 .resume = be_resume,
3668 .shutdown = be_shutdown,
3669 .err_handler = &be_eeh_handlers
3672 static int __init be_init_module(void)
3674 if (rx_frag_size != 8192 && rx_frag_size != 4096 &&
3675 rx_frag_size != 2048) {
3676 printk(KERN_WARNING DRV_NAME
3677 " : Module param rx_frag_size must be 2048/4096/8192."
3679 rx_frag_size = 2048;
3682 return pci_register_driver(&be_driver);
3684 module_init(be_init_module);
3686 static void __exit be_exit_module(void)
3688 pci_unregister_driver(&be_driver);
3690 module_exit(be_exit_module);