Linux 4.4.145
[linux/fpc-iii.git] / drivers / net / fjes / fjes_main.c
bloba539e831b4b1f6573d40a01da7815f7f040798e9
1 /*
2 * FUJITSU Extended Socket Network Device driver
3 * Copyright (c) 2015 FUJITSU LIMITED
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see <http://www.gnu.org/licenses/>.
17 * The full GNU General Public License is included in this distribution in
18 * the file called "COPYING".
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/nls.h>
25 #include <linux/platform_device.h>
26 #include <linux/netdevice.h>
27 #include <linux/interrupt.h>
29 #include "fjes.h"
31 #define MAJ 1
32 #define MIN 0
33 #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN)
34 #define DRV_NAME "fjes"
35 char fjes_driver_name[] = DRV_NAME;
36 char fjes_driver_version[] = DRV_VERSION;
37 static const char fjes_driver_string[] =
38 "FUJITSU Extended Socket Network Device Driver";
39 static const char fjes_copyright[] =
40 "Copyright (c) 2015 FUJITSU LIMITED";
42 MODULE_AUTHOR("Taku Izumi <izumi.taku@jp.fujitsu.com>");
43 MODULE_DESCRIPTION("FUJITSU Extended Socket Network Device Driver");
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION(DRV_VERSION);
47 static int fjes_request_irq(struct fjes_adapter *);
48 static void fjes_free_irq(struct fjes_adapter *);
50 static int fjes_open(struct net_device *);
51 static int fjes_close(struct net_device *);
52 static int fjes_setup_resources(struct fjes_adapter *);
53 static void fjes_free_resources(struct fjes_adapter *);
54 static netdev_tx_t fjes_xmit_frame(struct sk_buff *, struct net_device *);
55 static void fjes_raise_intr_rxdata_task(struct work_struct *);
56 static void fjes_tx_stall_task(struct work_struct *);
57 static void fjes_force_close_task(struct work_struct *);
58 static irqreturn_t fjes_intr(int, void*);
59 static struct rtnl_link_stats64 *
60 fjes_get_stats64(struct net_device *, struct rtnl_link_stats64 *);
61 static int fjes_change_mtu(struct net_device *, int);
62 static int fjes_vlan_rx_add_vid(struct net_device *, __be16 proto, u16);
63 static int fjes_vlan_rx_kill_vid(struct net_device *, __be16 proto, u16);
64 static void fjes_tx_retry(struct net_device *);
66 static int fjes_acpi_add(struct acpi_device *);
67 static int fjes_acpi_remove(struct acpi_device *);
68 static acpi_status fjes_get_acpi_resource(struct acpi_resource *, void*);
70 static int fjes_probe(struct platform_device *);
71 static int fjes_remove(struct platform_device *);
73 static int fjes_sw_init(struct fjes_adapter *);
74 static void fjes_netdev_setup(struct net_device *);
75 static void fjes_irq_watch_task(struct work_struct *);
76 static void fjes_watch_unshare_task(struct work_struct *);
77 static void fjes_rx_irq(struct fjes_adapter *, int);
78 static int fjes_poll(struct napi_struct *, int);
80 static const struct acpi_device_id fjes_acpi_ids[] = {
81 {"PNP0C02", 0},
82 {"", 0},
84 MODULE_DEVICE_TABLE(acpi, fjes_acpi_ids);
86 static struct acpi_driver fjes_acpi_driver = {
87 .name = DRV_NAME,
88 .class = DRV_NAME,
89 .owner = THIS_MODULE,
90 .ids = fjes_acpi_ids,
91 .ops = {
92 .add = fjes_acpi_add,
93 .remove = fjes_acpi_remove,
97 static struct platform_driver fjes_driver = {
98 .driver = {
99 .name = DRV_NAME,
100 .owner = THIS_MODULE,
102 .probe = fjes_probe,
103 .remove = fjes_remove,
106 static struct resource fjes_resource[] = {
108 .flags = IORESOURCE_MEM,
109 .start = 0,
110 .end = 0,
113 .flags = IORESOURCE_IRQ,
114 .start = 0,
115 .end = 0,
119 static int fjes_acpi_add(struct acpi_device *device)
121 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
122 char str_buf[sizeof(FJES_ACPI_SYMBOL) + 1];
123 struct platform_device *plat_dev;
124 union acpi_object *str;
125 acpi_status status;
126 int result;
128 status = acpi_evaluate_object(device->handle, "_STR", NULL, &buffer);
129 if (ACPI_FAILURE(status))
130 return -ENODEV;
132 str = buffer.pointer;
133 result = utf16s_to_utf8s((wchar_t *)str->string.pointer,
134 str->string.length, UTF16_LITTLE_ENDIAN,
135 str_buf, sizeof(str_buf) - 1);
136 str_buf[result] = 0;
138 if (strncmp(FJES_ACPI_SYMBOL, str_buf, strlen(FJES_ACPI_SYMBOL)) != 0) {
139 kfree(buffer.pointer);
140 return -ENODEV;
142 kfree(buffer.pointer);
144 status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
145 fjes_get_acpi_resource, fjes_resource);
146 if (ACPI_FAILURE(status))
147 return -ENODEV;
149 /* create platform_device */
150 plat_dev = platform_device_register_simple(DRV_NAME, 0, fjes_resource,
151 ARRAY_SIZE(fjes_resource));
152 device->driver_data = plat_dev;
154 return 0;
157 static int fjes_acpi_remove(struct acpi_device *device)
159 struct platform_device *plat_dev;
161 plat_dev = (struct platform_device *)acpi_driver_data(device);
162 platform_device_unregister(plat_dev);
164 return 0;
167 static acpi_status
168 fjes_get_acpi_resource(struct acpi_resource *acpi_res, void *data)
170 struct acpi_resource_address32 *addr;
171 struct acpi_resource_irq *irq;
172 struct resource *res = data;
174 switch (acpi_res->type) {
175 case ACPI_RESOURCE_TYPE_ADDRESS32:
176 addr = &acpi_res->data.address32;
177 res[0].start = addr->address.minimum;
178 res[0].end = addr->address.minimum +
179 addr->address.address_length - 1;
180 break;
182 case ACPI_RESOURCE_TYPE_IRQ:
183 irq = &acpi_res->data.irq;
184 if (irq->interrupt_count != 1)
185 return AE_ERROR;
186 res[1].start = irq->interrupts[0];
187 res[1].end = irq->interrupts[0];
188 break;
190 default:
191 break;
194 return AE_OK;
197 static int fjes_request_irq(struct fjes_adapter *adapter)
199 struct net_device *netdev = adapter->netdev;
200 int result = -1;
202 adapter->interrupt_watch_enable = true;
203 if (!delayed_work_pending(&adapter->interrupt_watch_task)) {
204 queue_delayed_work(adapter->control_wq,
205 &adapter->interrupt_watch_task,
206 FJES_IRQ_WATCH_DELAY);
209 if (!adapter->irq_registered) {
210 result = request_irq(adapter->hw.hw_res.irq, fjes_intr,
211 IRQF_SHARED, netdev->name, adapter);
212 if (result)
213 adapter->irq_registered = false;
214 else
215 adapter->irq_registered = true;
218 return result;
221 static void fjes_free_irq(struct fjes_adapter *adapter)
223 struct fjes_hw *hw = &adapter->hw;
225 adapter->interrupt_watch_enable = false;
226 cancel_delayed_work_sync(&adapter->interrupt_watch_task);
228 fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, true);
230 if (adapter->irq_registered) {
231 free_irq(adapter->hw.hw_res.irq, adapter);
232 adapter->irq_registered = false;
236 static const struct net_device_ops fjes_netdev_ops = {
237 .ndo_open = fjes_open,
238 .ndo_stop = fjes_close,
239 .ndo_start_xmit = fjes_xmit_frame,
240 .ndo_get_stats64 = fjes_get_stats64,
241 .ndo_change_mtu = fjes_change_mtu,
242 .ndo_tx_timeout = fjes_tx_retry,
243 .ndo_vlan_rx_add_vid = fjes_vlan_rx_add_vid,
244 .ndo_vlan_rx_kill_vid = fjes_vlan_rx_kill_vid,
247 /* fjes_open - Called when a network interface is made active */
248 static int fjes_open(struct net_device *netdev)
250 struct fjes_adapter *adapter = netdev_priv(netdev);
251 struct fjes_hw *hw = &adapter->hw;
252 int result;
254 if (adapter->open_guard)
255 return -ENXIO;
257 result = fjes_setup_resources(adapter);
258 if (result)
259 goto err_setup_res;
261 hw->txrx_stop_req_bit = 0;
262 hw->epstop_req_bit = 0;
264 napi_enable(&adapter->napi);
266 fjes_hw_capture_interrupt_status(hw);
268 result = fjes_request_irq(adapter);
269 if (result)
270 goto err_req_irq;
272 fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, false);
274 netif_tx_start_all_queues(netdev);
275 netif_carrier_on(netdev);
277 return 0;
279 err_req_irq:
280 fjes_free_irq(adapter);
281 napi_disable(&adapter->napi);
283 err_setup_res:
284 fjes_free_resources(adapter);
285 return result;
288 /* fjes_close - Disables a network interface */
289 static int fjes_close(struct net_device *netdev)
291 struct fjes_adapter *adapter = netdev_priv(netdev);
292 struct fjes_hw *hw = &adapter->hw;
293 int epidx;
295 netif_tx_stop_all_queues(netdev);
296 netif_carrier_off(netdev);
298 fjes_hw_raise_epstop(hw);
300 napi_disable(&adapter->napi);
302 for (epidx = 0; epidx < hw->max_epid; epidx++) {
303 if (epidx == hw->my_epid)
304 continue;
306 adapter->hw.ep_shm_info[epidx].tx.info->v1i.rx_status &=
307 ~FJES_RX_POLL_WORK;
310 fjes_free_irq(adapter);
312 cancel_delayed_work_sync(&adapter->interrupt_watch_task);
313 cancel_work_sync(&adapter->unshare_watch_task);
314 adapter->unshare_watch_bitmask = 0;
315 cancel_work_sync(&adapter->raise_intr_rxdata_task);
316 cancel_work_sync(&adapter->tx_stall_task);
318 cancel_work_sync(&hw->update_zone_task);
319 cancel_work_sync(&hw->epstop_task);
321 fjes_hw_wait_epstop(hw);
323 fjes_free_resources(adapter);
325 return 0;
328 static int fjes_setup_resources(struct fjes_adapter *adapter)
330 struct net_device *netdev = adapter->netdev;
331 struct ep_share_mem_info *buf_pair;
332 struct fjes_hw *hw = &adapter->hw;
333 int result;
334 int epidx;
336 mutex_lock(&hw->hw_info.lock);
337 result = fjes_hw_request_info(hw);
338 switch (result) {
339 case 0:
340 for (epidx = 0; epidx < hw->max_epid; epidx++) {
341 hw->ep_shm_info[epidx].es_status =
342 hw->hw_info.res_buf->info.info[epidx].es_status;
343 hw->ep_shm_info[epidx].zone =
344 hw->hw_info.res_buf->info.info[epidx].zone;
346 break;
347 default:
348 case -ENOMSG:
349 case -EBUSY:
350 adapter->force_reset = true;
352 mutex_unlock(&hw->hw_info.lock);
353 return result;
355 mutex_unlock(&hw->hw_info.lock);
357 for (epidx = 0; epidx < (hw->max_epid); epidx++) {
358 if ((epidx != hw->my_epid) &&
359 (hw->ep_shm_info[epidx].es_status ==
360 FJES_ZONING_STATUS_ENABLE)) {
361 fjes_hw_raise_interrupt(hw, epidx,
362 REG_ICTL_MASK_INFO_UPDATE);
366 msleep(FJES_OPEN_ZONE_UPDATE_WAIT * hw->max_epid);
368 for (epidx = 0; epidx < (hw->max_epid); epidx++) {
369 if (epidx == hw->my_epid)
370 continue;
372 buf_pair = &hw->ep_shm_info[epidx];
374 fjes_hw_setup_epbuf(&buf_pair->tx, netdev->dev_addr,
375 netdev->mtu);
377 if (fjes_hw_epid_is_same_zone(hw, epidx)) {
378 mutex_lock(&hw->hw_info.lock);
379 result =
380 fjes_hw_register_buff_addr(hw, epidx, buf_pair);
381 mutex_unlock(&hw->hw_info.lock);
383 switch (result) {
384 case 0:
385 break;
386 case -ENOMSG:
387 case -EBUSY:
388 default:
389 adapter->force_reset = true;
390 return result;
395 return 0;
398 static void fjes_free_resources(struct fjes_adapter *adapter)
400 struct net_device *netdev = adapter->netdev;
401 struct fjes_device_command_param param;
402 struct ep_share_mem_info *buf_pair;
403 struct fjes_hw *hw = &adapter->hw;
404 bool reset_flag = false;
405 int result;
406 int epidx;
408 for (epidx = 0; epidx < hw->max_epid; epidx++) {
409 if (epidx == hw->my_epid)
410 continue;
412 mutex_lock(&hw->hw_info.lock);
413 result = fjes_hw_unregister_buff_addr(hw, epidx);
414 mutex_unlock(&hw->hw_info.lock);
416 if (result)
417 reset_flag = true;
419 buf_pair = &hw->ep_shm_info[epidx];
421 fjes_hw_setup_epbuf(&buf_pair->tx,
422 netdev->dev_addr, netdev->mtu);
424 clear_bit(epidx, &hw->txrx_stop_req_bit);
427 if (reset_flag || adapter->force_reset) {
428 result = fjes_hw_reset(hw);
430 adapter->force_reset = false;
432 if (result)
433 adapter->open_guard = true;
435 hw->hw_info.buffer_share_bit = 0;
437 memset((void *)&param, 0, sizeof(param));
439 param.req_len = hw->hw_info.req_buf_size;
440 param.req_start = __pa(hw->hw_info.req_buf);
441 param.res_len = hw->hw_info.res_buf_size;
442 param.res_start = __pa(hw->hw_info.res_buf);
443 param.share_start = __pa(hw->hw_info.share->ep_status);
445 fjes_hw_init_command_registers(hw, &param);
449 static void fjes_tx_stall_task(struct work_struct *work)
451 struct fjes_adapter *adapter = container_of(work,
452 struct fjes_adapter, tx_stall_task);
453 struct net_device *netdev = adapter->netdev;
454 struct fjes_hw *hw = &adapter->hw;
455 int all_queue_available, sendable;
456 enum ep_partner_status pstatus;
457 int max_epid, my_epid, epid;
458 union ep_buffer_info *info;
459 int i;
461 if (((long)jiffies -
462 (long)(netdev->trans_start)) > FJES_TX_TX_STALL_TIMEOUT) {
463 netif_wake_queue(netdev);
464 return;
467 my_epid = hw->my_epid;
468 max_epid = hw->max_epid;
470 for (i = 0; i < 5; i++) {
471 all_queue_available = 1;
473 for (epid = 0; epid < max_epid; epid++) {
474 if (my_epid == epid)
475 continue;
477 pstatus = fjes_hw_get_partner_ep_status(hw, epid);
478 sendable = (pstatus == EP_PARTNER_SHARED);
479 if (!sendable)
480 continue;
482 info = adapter->hw.ep_shm_info[epid].tx.info;
484 if (EP_RING_FULL(info->v1i.head, info->v1i.tail,
485 info->v1i.count_max)) {
486 all_queue_available = 0;
487 break;
491 if (all_queue_available) {
492 netif_wake_queue(netdev);
493 return;
497 usleep_range(50, 100);
499 queue_work(adapter->txrx_wq, &adapter->tx_stall_task);
502 static void fjes_force_close_task(struct work_struct *work)
504 struct fjes_adapter *adapter = container_of(work,
505 struct fjes_adapter, force_close_task);
506 struct net_device *netdev = adapter->netdev;
508 rtnl_lock();
509 dev_close(netdev);
510 rtnl_unlock();
513 static void fjes_raise_intr_rxdata_task(struct work_struct *work)
515 struct fjes_adapter *adapter = container_of(work,
516 struct fjes_adapter, raise_intr_rxdata_task);
517 struct fjes_hw *hw = &adapter->hw;
518 enum ep_partner_status pstatus;
519 int max_epid, my_epid, epid;
521 my_epid = hw->my_epid;
522 max_epid = hw->max_epid;
524 for (epid = 0; epid < max_epid; epid++)
525 hw->ep_shm_info[epid].tx_status_work = 0;
527 for (epid = 0; epid < max_epid; epid++) {
528 if (epid == my_epid)
529 continue;
531 pstatus = fjes_hw_get_partner_ep_status(hw, epid);
532 if (pstatus == EP_PARTNER_SHARED) {
533 hw->ep_shm_info[epid].tx_status_work =
534 hw->ep_shm_info[epid].tx.info->v1i.tx_status;
536 if (hw->ep_shm_info[epid].tx_status_work ==
537 FJES_TX_DELAY_SEND_PENDING) {
538 hw->ep_shm_info[epid].tx.info->v1i.tx_status =
539 FJES_TX_DELAY_SEND_NONE;
544 for (epid = 0; epid < max_epid; epid++) {
545 if (epid == my_epid)
546 continue;
548 pstatus = fjes_hw_get_partner_ep_status(hw, epid);
549 if ((hw->ep_shm_info[epid].tx_status_work ==
550 FJES_TX_DELAY_SEND_PENDING) &&
551 (pstatus == EP_PARTNER_SHARED) &&
552 !(hw->ep_shm_info[epid].rx.info->v1i.rx_status)) {
553 fjes_hw_raise_interrupt(hw, epid,
554 REG_ICTL_MASK_RX_DATA);
558 usleep_range(500, 1000);
561 static int fjes_tx_send(struct fjes_adapter *adapter, int dest,
562 void *data, size_t len)
564 int retval;
566 retval = fjes_hw_epbuf_tx_pkt_send(&adapter->hw.ep_shm_info[dest].tx,
567 data, len);
568 if (retval)
569 return retval;
571 adapter->hw.ep_shm_info[dest].tx.info->v1i.tx_status =
572 FJES_TX_DELAY_SEND_PENDING;
573 if (!work_pending(&adapter->raise_intr_rxdata_task))
574 queue_work(adapter->txrx_wq,
575 &adapter->raise_intr_rxdata_task);
577 retval = 0;
578 return retval;
581 static netdev_tx_t
582 fjes_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
584 struct fjes_adapter *adapter = netdev_priv(netdev);
585 struct fjes_hw *hw = &adapter->hw;
587 int max_epid, my_epid, dest_epid;
588 enum ep_partner_status pstatus;
589 struct netdev_queue *cur_queue;
590 char shortpkt[VLAN_ETH_HLEN];
591 bool is_multi, vlan;
592 struct ethhdr *eth;
593 u16 queue_no = 0;
594 u16 vlan_id = 0;
595 netdev_tx_t ret;
596 char *data;
597 int len;
599 ret = NETDEV_TX_OK;
600 is_multi = false;
601 cur_queue = netdev_get_tx_queue(netdev, queue_no);
603 eth = (struct ethhdr *)skb->data;
604 my_epid = hw->my_epid;
606 vlan = (vlan_get_tag(skb, &vlan_id) == 0) ? true : false;
608 data = skb->data;
609 len = skb->len;
611 if (is_multicast_ether_addr(eth->h_dest)) {
612 dest_epid = 0;
613 max_epid = hw->max_epid;
614 is_multi = true;
615 } else if (is_local_ether_addr(eth->h_dest)) {
616 dest_epid = eth->h_dest[ETH_ALEN - 1];
617 max_epid = dest_epid + 1;
619 if ((eth->h_dest[0] == 0x02) &&
620 (0x00 == (eth->h_dest[1] | eth->h_dest[2] |
621 eth->h_dest[3] | eth->h_dest[4])) &&
622 (dest_epid < hw->max_epid)) {
624 } else {
625 dest_epid = 0;
626 max_epid = 0;
627 ret = NETDEV_TX_OK;
629 adapter->stats64.tx_packets += 1;
630 hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
631 adapter->stats64.tx_bytes += len;
632 hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
634 } else {
635 dest_epid = 0;
636 max_epid = 0;
637 ret = NETDEV_TX_OK;
639 adapter->stats64.tx_packets += 1;
640 hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
641 adapter->stats64.tx_bytes += len;
642 hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
645 for (; dest_epid < max_epid; dest_epid++) {
646 if (my_epid == dest_epid)
647 continue;
649 pstatus = fjes_hw_get_partner_ep_status(hw, dest_epid);
650 if (pstatus != EP_PARTNER_SHARED) {
651 ret = NETDEV_TX_OK;
652 } else if (!fjes_hw_check_epbuf_version(
653 &adapter->hw.ep_shm_info[dest_epid].rx, 0)) {
654 /* version is NOT 0 */
655 adapter->stats64.tx_carrier_errors += 1;
656 hw->ep_shm_info[my_epid].net_stats
657 .tx_carrier_errors += 1;
659 ret = NETDEV_TX_OK;
660 } else if (!fjes_hw_check_mtu(
661 &adapter->hw.ep_shm_info[dest_epid].rx,
662 netdev->mtu)) {
663 adapter->stats64.tx_dropped += 1;
664 hw->ep_shm_info[my_epid].net_stats.tx_dropped += 1;
665 adapter->stats64.tx_errors += 1;
666 hw->ep_shm_info[my_epid].net_stats.tx_errors += 1;
668 ret = NETDEV_TX_OK;
669 } else if (vlan &&
670 !fjes_hw_check_vlan_id(
671 &adapter->hw.ep_shm_info[dest_epid].rx,
672 vlan_id)) {
673 ret = NETDEV_TX_OK;
674 } else {
675 if (len < VLAN_ETH_HLEN) {
676 memset(shortpkt, 0, VLAN_ETH_HLEN);
677 memcpy(shortpkt, skb->data, skb->len);
678 len = VLAN_ETH_HLEN;
679 data = shortpkt;
682 if (adapter->tx_retry_count == 0) {
683 adapter->tx_start_jiffies = jiffies;
684 adapter->tx_retry_count = 1;
685 } else {
686 adapter->tx_retry_count++;
689 if (fjes_tx_send(adapter, dest_epid, data, len)) {
690 if (is_multi) {
691 ret = NETDEV_TX_OK;
692 } else if (
693 ((long)jiffies -
694 (long)adapter->tx_start_jiffies) >=
695 FJES_TX_RETRY_TIMEOUT) {
696 adapter->stats64.tx_fifo_errors += 1;
697 hw->ep_shm_info[my_epid].net_stats
698 .tx_fifo_errors += 1;
699 adapter->stats64.tx_errors += 1;
700 hw->ep_shm_info[my_epid].net_stats
701 .tx_errors += 1;
703 ret = NETDEV_TX_OK;
704 } else {
705 netdev->trans_start = jiffies;
706 netif_tx_stop_queue(cur_queue);
708 if (!work_pending(&adapter->tx_stall_task))
709 queue_work(adapter->txrx_wq,
710 &adapter->tx_stall_task);
712 ret = NETDEV_TX_BUSY;
714 } else {
715 if (!is_multi) {
716 adapter->stats64.tx_packets += 1;
717 hw->ep_shm_info[my_epid].net_stats
718 .tx_packets += 1;
719 adapter->stats64.tx_bytes += len;
720 hw->ep_shm_info[my_epid].net_stats
721 .tx_bytes += len;
724 adapter->tx_retry_count = 0;
725 ret = NETDEV_TX_OK;
730 if (ret == NETDEV_TX_OK) {
731 dev_kfree_skb(skb);
732 if (is_multi) {
733 adapter->stats64.tx_packets += 1;
734 hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
735 adapter->stats64.tx_bytes += 1;
736 hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
740 return ret;
743 static void fjes_tx_retry(struct net_device *netdev)
745 struct netdev_queue *queue = netdev_get_tx_queue(netdev, 0);
747 netif_tx_wake_queue(queue);
750 static struct rtnl_link_stats64 *
751 fjes_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
753 struct fjes_adapter *adapter = netdev_priv(netdev);
755 memcpy(stats, &adapter->stats64, sizeof(struct rtnl_link_stats64));
757 return stats;
760 static int fjes_change_mtu(struct net_device *netdev, int new_mtu)
762 bool running = netif_running(netdev);
763 int ret = 0;
764 int idx;
766 for (idx = 0; fjes_support_mtu[idx] != 0; idx++) {
767 if (new_mtu <= fjes_support_mtu[idx]) {
768 new_mtu = fjes_support_mtu[idx];
769 if (new_mtu == netdev->mtu)
770 return 0;
772 if (running)
773 fjes_close(netdev);
775 netdev->mtu = new_mtu;
777 if (running)
778 ret = fjes_open(netdev);
780 return ret;
784 return -EINVAL;
787 static int fjes_vlan_rx_add_vid(struct net_device *netdev,
788 __be16 proto, u16 vid)
790 struct fjes_adapter *adapter = netdev_priv(netdev);
791 bool ret = true;
792 int epid;
794 for (epid = 0; epid < adapter->hw.max_epid; epid++) {
795 if (epid == adapter->hw.my_epid)
796 continue;
798 if (!fjes_hw_check_vlan_id(
799 &adapter->hw.ep_shm_info[epid].tx, vid))
800 ret = fjes_hw_set_vlan_id(
801 &adapter->hw.ep_shm_info[epid].tx, vid);
804 return ret ? 0 : -ENOSPC;
807 static int fjes_vlan_rx_kill_vid(struct net_device *netdev,
808 __be16 proto, u16 vid)
810 struct fjes_adapter *adapter = netdev_priv(netdev);
811 int epid;
813 for (epid = 0; epid < adapter->hw.max_epid; epid++) {
814 if (epid == adapter->hw.my_epid)
815 continue;
817 fjes_hw_del_vlan_id(&adapter->hw.ep_shm_info[epid].tx, vid);
820 return 0;
823 static void fjes_txrx_stop_req_irq(struct fjes_adapter *adapter,
824 int src_epid)
826 struct fjes_hw *hw = &adapter->hw;
827 enum ep_partner_status status;
829 status = fjes_hw_get_partner_ep_status(hw, src_epid);
830 switch (status) {
831 case EP_PARTNER_UNSHARE:
832 case EP_PARTNER_COMPLETE:
833 default:
834 break;
835 case EP_PARTNER_WAITING:
836 if (src_epid < hw->my_epid) {
837 hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
838 FJES_RX_STOP_REQ_DONE;
840 clear_bit(src_epid, &hw->txrx_stop_req_bit);
841 set_bit(src_epid, &adapter->unshare_watch_bitmask);
843 if (!work_pending(&adapter->unshare_watch_task))
844 queue_work(adapter->control_wq,
845 &adapter->unshare_watch_task);
847 break;
848 case EP_PARTNER_SHARED:
849 if (hw->ep_shm_info[src_epid].rx.info->v1i.rx_status &
850 FJES_RX_STOP_REQ_REQUEST) {
851 set_bit(src_epid, &hw->epstop_req_bit);
852 if (!work_pending(&hw->epstop_task))
853 queue_work(adapter->control_wq,
854 &hw->epstop_task);
856 break;
860 static void fjes_stop_req_irq(struct fjes_adapter *adapter, int src_epid)
862 struct fjes_hw *hw = &adapter->hw;
863 enum ep_partner_status status;
865 set_bit(src_epid, &hw->hw_info.buffer_unshare_reserve_bit);
867 status = fjes_hw_get_partner_ep_status(hw, src_epid);
868 switch (status) {
869 case EP_PARTNER_WAITING:
870 hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
871 FJES_RX_STOP_REQ_DONE;
872 clear_bit(src_epid, &hw->txrx_stop_req_bit);
873 /* fall through */
874 case EP_PARTNER_UNSHARE:
875 case EP_PARTNER_COMPLETE:
876 default:
877 set_bit(src_epid, &adapter->unshare_watch_bitmask);
878 if (!work_pending(&adapter->unshare_watch_task))
879 queue_work(adapter->control_wq,
880 &adapter->unshare_watch_task);
881 break;
882 case EP_PARTNER_SHARED:
883 set_bit(src_epid, &hw->epstop_req_bit);
885 if (!work_pending(&hw->epstop_task))
886 queue_work(adapter->control_wq, &hw->epstop_task);
887 break;
891 static void fjes_update_zone_irq(struct fjes_adapter *adapter,
892 int src_epid)
894 struct fjes_hw *hw = &adapter->hw;
896 if (!work_pending(&hw->update_zone_task))
897 queue_work(adapter->control_wq, &hw->update_zone_task);
900 static irqreturn_t fjes_intr(int irq, void *data)
902 struct fjes_adapter *adapter = data;
903 struct fjes_hw *hw = &adapter->hw;
904 irqreturn_t ret;
905 u32 icr;
907 icr = fjes_hw_capture_interrupt_status(hw);
909 if (icr & REG_IS_MASK_IS_ASSERT) {
910 if (icr & REG_ICTL_MASK_RX_DATA)
911 fjes_rx_irq(adapter, icr & REG_IS_MASK_EPID);
913 if (icr & REG_ICTL_MASK_DEV_STOP_REQ)
914 fjes_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
916 if (icr & REG_ICTL_MASK_TXRX_STOP_REQ)
917 fjes_txrx_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
919 if (icr & REG_ICTL_MASK_TXRX_STOP_DONE)
920 fjes_hw_set_irqmask(hw,
921 REG_ICTL_MASK_TXRX_STOP_DONE, true);
923 if (icr & REG_ICTL_MASK_INFO_UPDATE)
924 fjes_update_zone_irq(adapter, icr & REG_IS_MASK_EPID);
926 ret = IRQ_HANDLED;
927 } else {
928 ret = IRQ_NONE;
931 return ret;
934 static int fjes_rxframe_search_exist(struct fjes_adapter *adapter,
935 int start_epid)
937 struct fjes_hw *hw = &adapter->hw;
938 enum ep_partner_status pstatus;
939 int max_epid, cur_epid;
940 int i;
942 max_epid = hw->max_epid;
943 start_epid = (start_epid + 1 + max_epid) % max_epid;
945 for (i = 0; i < max_epid; i++) {
946 cur_epid = (start_epid + i) % max_epid;
947 if (cur_epid == hw->my_epid)
948 continue;
950 pstatus = fjes_hw_get_partner_ep_status(hw, cur_epid);
951 if (pstatus == EP_PARTNER_SHARED) {
952 if (!fjes_hw_epbuf_rx_is_empty(
953 &hw->ep_shm_info[cur_epid].rx))
954 return cur_epid;
957 return -1;
960 static void *fjes_rxframe_get(struct fjes_adapter *adapter, size_t *psize,
961 int *cur_epid)
963 void *frame;
965 *cur_epid = fjes_rxframe_search_exist(adapter, *cur_epid);
966 if (*cur_epid < 0)
967 return NULL;
969 frame =
970 fjes_hw_epbuf_rx_curpkt_get_addr(
971 &adapter->hw.ep_shm_info[*cur_epid].rx, psize);
973 return frame;
976 static void fjes_rxframe_release(struct fjes_adapter *adapter, int cur_epid)
978 fjes_hw_epbuf_rx_curpkt_drop(&adapter->hw.ep_shm_info[cur_epid].rx);
981 static void fjes_rx_irq(struct fjes_adapter *adapter, int src_epid)
983 struct fjes_hw *hw = &adapter->hw;
985 fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, true);
987 adapter->unset_rx_last = true;
988 napi_schedule(&adapter->napi);
991 static int fjes_poll(struct napi_struct *napi, int budget)
993 struct fjes_adapter *adapter =
994 container_of(napi, struct fjes_adapter, napi);
995 struct net_device *netdev = napi->dev;
996 struct fjes_hw *hw = &adapter->hw;
997 struct sk_buff *skb;
998 int work_done = 0;
999 int cur_epid = 0;
1000 int epidx;
1001 size_t frame_len;
1002 void *frame;
1004 for (epidx = 0; epidx < hw->max_epid; epidx++) {
1005 if (epidx == hw->my_epid)
1006 continue;
1008 adapter->hw.ep_shm_info[epidx].tx.info->v1i.rx_status |=
1009 FJES_RX_POLL_WORK;
1012 while (work_done < budget) {
1013 prefetch(&adapter->hw);
1014 frame = fjes_rxframe_get(adapter, &frame_len, &cur_epid);
1016 if (frame) {
1017 skb = napi_alloc_skb(napi, frame_len);
1018 if (!skb) {
1019 adapter->stats64.rx_dropped += 1;
1020 hw->ep_shm_info[cur_epid].net_stats
1021 .rx_dropped += 1;
1022 adapter->stats64.rx_errors += 1;
1023 hw->ep_shm_info[cur_epid].net_stats
1024 .rx_errors += 1;
1025 } else {
1026 memcpy(skb_put(skb, frame_len),
1027 frame, frame_len);
1028 skb->protocol = eth_type_trans(skb, netdev);
1029 skb->ip_summed = CHECKSUM_UNNECESSARY;
1031 netif_receive_skb(skb);
1033 work_done++;
1035 adapter->stats64.rx_packets += 1;
1036 hw->ep_shm_info[cur_epid].net_stats
1037 .rx_packets += 1;
1038 adapter->stats64.rx_bytes += frame_len;
1039 hw->ep_shm_info[cur_epid].net_stats
1040 .rx_bytes += frame_len;
1042 if (is_multicast_ether_addr(
1043 ((struct ethhdr *)frame)->h_dest)) {
1044 adapter->stats64.multicast += 1;
1045 hw->ep_shm_info[cur_epid].net_stats
1046 .multicast += 1;
1050 fjes_rxframe_release(adapter, cur_epid);
1051 adapter->unset_rx_last = true;
1052 } else {
1053 break;
1057 if (work_done < budget) {
1058 napi_complete(napi);
1060 if (adapter->unset_rx_last) {
1061 adapter->rx_last_jiffies = jiffies;
1062 adapter->unset_rx_last = false;
1065 if (((long)jiffies - (long)adapter->rx_last_jiffies) < 3) {
1066 napi_reschedule(napi);
1067 } else {
1068 for (epidx = 0; epidx < hw->max_epid; epidx++) {
1069 if (epidx == hw->my_epid)
1070 continue;
1071 adapter->hw.ep_shm_info[epidx]
1072 .tx.info->v1i.rx_status &=
1073 ~FJES_RX_POLL_WORK;
1076 fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, false);
1080 return work_done;
1083 /* fjes_probe - Device Initialization Routine */
1084 static int fjes_probe(struct platform_device *plat_dev)
1086 struct fjes_adapter *adapter;
1087 struct net_device *netdev;
1088 struct resource *res;
1089 struct fjes_hw *hw;
1090 int err;
1092 err = -ENOMEM;
1093 netdev = alloc_netdev_mq(sizeof(struct fjes_adapter), "es%d",
1094 NET_NAME_UNKNOWN, fjes_netdev_setup,
1095 FJES_MAX_QUEUES);
1097 if (!netdev)
1098 goto err_out;
1100 SET_NETDEV_DEV(netdev, &plat_dev->dev);
1102 dev_set_drvdata(&plat_dev->dev, netdev);
1103 adapter = netdev_priv(netdev);
1104 adapter->netdev = netdev;
1105 adapter->plat_dev = plat_dev;
1106 hw = &adapter->hw;
1107 hw->back = adapter;
1109 /* setup the private structure */
1110 err = fjes_sw_init(adapter);
1111 if (err)
1112 goto err_free_netdev;
1114 INIT_WORK(&adapter->force_close_task, fjes_force_close_task);
1115 adapter->force_reset = false;
1116 adapter->open_guard = false;
1118 adapter->txrx_wq = create_workqueue(DRV_NAME "/txrx");
1119 adapter->control_wq = create_workqueue(DRV_NAME "/control");
1121 INIT_WORK(&adapter->tx_stall_task, fjes_tx_stall_task);
1122 INIT_WORK(&adapter->raise_intr_rxdata_task,
1123 fjes_raise_intr_rxdata_task);
1124 INIT_WORK(&adapter->unshare_watch_task, fjes_watch_unshare_task);
1125 adapter->unshare_watch_bitmask = 0;
1127 INIT_DELAYED_WORK(&adapter->interrupt_watch_task, fjes_irq_watch_task);
1128 adapter->interrupt_watch_enable = false;
1130 res = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
1131 hw->hw_res.start = res->start;
1132 hw->hw_res.size = res->end - res->start + 1;
1133 hw->hw_res.irq = platform_get_irq(plat_dev, 0);
1134 err = fjes_hw_init(&adapter->hw);
1135 if (err)
1136 goto err_free_netdev;
1138 /* setup MAC address (02:00:00:00:00:[epid])*/
1139 netdev->dev_addr[0] = 2;
1140 netdev->dev_addr[1] = 0;
1141 netdev->dev_addr[2] = 0;
1142 netdev->dev_addr[3] = 0;
1143 netdev->dev_addr[4] = 0;
1144 netdev->dev_addr[5] = hw->my_epid; /* EPID */
1146 err = register_netdev(netdev);
1147 if (err)
1148 goto err_hw_exit;
1150 netif_carrier_off(netdev);
1152 return 0;
1154 err_hw_exit:
1155 fjes_hw_exit(&adapter->hw);
1156 err_free_netdev:
1157 free_netdev(netdev);
1158 err_out:
1159 return err;
1162 /* fjes_remove - Device Removal Routine */
1163 static int fjes_remove(struct platform_device *plat_dev)
1165 struct net_device *netdev = dev_get_drvdata(&plat_dev->dev);
1166 struct fjes_adapter *adapter = netdev_priv(netdev);
1167 struct fjes_hw *hw = &adapter->hw;
1169 cancel_delayed_work_sync(&adapter->interrupt_watch_task);
1170 cancel_work_sync(&adapter->unshare_watch_task);
1171 cancel_work_sync(&adapter->raise_intr_rxdata_task);
1172 cancel_work_sync(&adapter->tx_stall_task);
1173 if (adapter->control_wq)
1174 destroy_workqueue(adapter->control_wq);
1175 if (adapter->txrx_wq)
1176 destroy_workqueue(adapter->txrx_wq);
1178 unregister_netdev(netdev);
1180 fjes_hw_exit(hw);
1182 netif_napi_del(&adapter->napi);
1184 free_netdev(netdev);
1186 return 0;
1189 static int fjes_sw_init(struct fjes_adapter *adapter)
1191 struct net_device *netdev = adapter->netdev;
1193 netif_napi_add(netdev, &adapter->napi, fjes_poll, 64);
1195 return 0;
1198 /* fjes_netdev_setup - netdevice initialization routine */
1199 static void fjes_netdev_setup(struct net_device *netdev)
1201 ether_setup(netdev);
1203 netdev->watchdog_timeo = FJES_TX_RETRY_INTERVAL;
1204 netdev->netdev_ops = &fjes_netdev_ops;
1205 fjes_set_ethtool_ops(netdev);
1206 netdev->mtu = fjes_support_mtu[0];
1207 netdev->flags |= IFF_BROADCAST;
1208 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1211 static void fjes_irq_watch_task(struct work_struct *work)
1213 struct fjes_adapter *adapter = container_of(to_delayed_work(work),
1214 struct fjes_adapter, interrupt_watch_task);
1216 local_irq_disable();
1217 fjes_intr(adapter->hw.hw_res.irq, adapter);
1218 local_irq_enable();
1220 if (fjes_rxframe_search_exist(adapter, 0) >= 0)
1221 napi_schedule(&adapter->napi);
1223 if (adapter->interrupt_watch_enable) {
1224 if (!delayed_work_pending(&adapter->interrupt_watch_task))
1225 queue_delayed_work(adapter->control_wq,
1226 &adapter->interrupt_watch_task,
1227 FJES_IRQ_WATCH_DELAY);
1231 static void fjes_watch_unshare_task(struct work_struct *work)
1233 struct fjes_adapter *adapter =
1234 container_of(work, struct fjes_adapter, unshare_watch_task);
1236 struct net_device *netdev = adapter->netdev;
1237 struct fjes_hw *hw = &adapter->hw;
1239 int unshare_watch, unshare_reserve;
1240 int max_epid, my_epid, epidx;
1241 int stop_req, stop_req_done;
1242 ulong unshare_watch_bitmask;
1243 int wait_time = 0;
1244 int is_shared;
1245 int ret;
1247 my_epid = hw->my_epid;
1248 max_epid = hw->max_epid;
1250 unshare_watch_bitmask = adapter->unshare_watch_bitmask;
1251 adapter->unshare_watch_bitmask = 0;
1253 while ((unshare_watch_bitmask || hw->txrx_stop_req_bit) &&
1254 (wait_time < 3000)) {
1255 for (epidx = 0; epidx < hw->max_epid; epidx++) {
1256 if (epidx == hw->my_epid)
1257 continue;
1259 is_shared = fjes_hw_epid_is_shared(hw->hw_info.share,
1260 epidx);
1262 stop_req = test_bit(epidx, &hw->txrx_stop_req_bit);
1264 stop_req_done = hw->ep_shm_info[epidx].rx.info->v1i.rx_status &
1265 FJES_RX_STOP_REQ_DONE;
1267 unshare_watch = test_bit(epidx, &unshare_watch_bitmask);
1269 unshare_reserve = test_bit(epidx,
1270 &hw->hw_info.buffer_unshare_reserve_bit);
1272 if ((!stop_req ||
1273 (is_shared && (!is_shared || !stop_req_done))) &&
1274 (is_shared || !unshare_watch || !unshare_reserve))
1275 continue;
1277 mutex_lock(&hw->hw_info.lock);
1278 ret = fjes_hw_unregister_buff_addr(hw, epidx);
1279 switch (ret) {
1280 case 0:
1281 break;
1282 case -ENOMSG:
1283 case -EBUSY:
1284 default:
1285 if (!work_pending(
1286 &adapter->force_close_task)) {
1287 adapter->force_reset = true;
1288 schedule_work(
1289 &adapter->force_close_task);
1291 break;
1293 mutex_unlock(&hw->hw_info.lock);
1295 fjes_hw_setup_epbuf(&hw->ep_shm_info[epidx].tx,
1296 netdev->dev_addr, netdev->mtu);
1298 clear_bit(epidx, &hw->txrx_stop_req_bit);
1299 clear_bit(epidx, &unshare_watch_bitmask);
1300 clear_bit(epidx,
1301 &hw->hw_info.buffer_unshare_reserve_bit);
1304 msleep(100);
1305 wait_time += 100;
1308 if (hw->hw_info.buffer_unshare_reserve_bit) {
1309 for (epidx = 0; epidx < hw->max_epid; epidx++) {
1310 if (epidx == hw->my_epid)
1311 continue;
1313 if (test_bit(epidx,
1314 &hw->hw_info.buffer_unshare_reserve_bit)) {
1315 mutex_lock(&hw->hw_info.lock);
1317 ret = fjes_hw_unregister_buff_addr(hw, epidx);
1318 switch (ret) {
1319 case 0:
1320 break;
1321 case -ENOMSG:
1322 case -EBUSY:
1323 default:
1324 if (!work_pending(
1325 &adapter->force_close_task)) {
1326 adapter->force_reset = true;
1327 schedule_work(
1328 &adapter->force_close_task);
1330 break;
1332 mutex_unlock(&hw->hw_info.lock);
1334 fjes_hw_setup_epbuf(
1335 &hw->ep_shm_info[epidx].tx,
1336 netdev->dev_addr, netdev->mtu);
1338 clear_bit(epidx, &hw->txrx_stop_req_bit);
1339 clear_bit(epidx, &unshare_watch_bitmask);
1340 clear_bit(epidx, &hw->hw_info.buffer_unshare_reserve_bit);
1343 if (test_bit(epidx, &unshare_watch_bitmask)) {
1344 hw->ep_shm_info[epidx].tx.info->v1i.rx_status &=
1345 ~FJES_RX_STOP_REQ_DONE;
1351 /* fjes_init_module - Driver Registration Routine */
1352 static int __init fjes_init_module(void)
1354 int result;
1356 pr_info("%s - version %s - %s\n",
1357 fjes_driver_string, fjes_driver_version, fjes_copyright);
1359 result = platform_driver_register(&fjes_driver);
1360 if (result < 0)
1361 return result;
1363 result = acpi_bus_register_driver(&fjes_acpi_driver);
1364 if (result < 0)
1365 goto fail_acpi_driver;
1367 return 0;
1369 fail_acpi_driver:
1370 platform_driver_unregister(&fjes_driver);
1371 return result;
1374 module_init(fjes_init_module);
1376 /* fjes_exit_module - Driver Exit Cleanup Routine */
1377 static void __exit fjes_exit_module(void)
1379 acpi_bus_unregister_driver(&fjes_acpi_driver);
1380 platform_driver_unregister(&fjes_driver);
1383 module_exit(fjes_exit_module);