xref: /openbmc/linux/drivers/net/fjes/fjes_main.c (revision 3c3bd4a91ec12ad7c140bb3fd04b199e411760cb)
1658d439bSTaku Izumi /*
2658d439bSTaku Izumi  *  FUJITSU Extended Socket Network Device driver
3658d439bSTaku Izumi  *  Copyright (c) 2015 FUJITSU LIMITED
4658d439bSTaku Izumi  *
5658d439bSTaku Izumi  * This program is free software; you can redistribute it and/or modify it
6658d439bSTaku Izumi  * under the terms and conditions of the GNU General Public License,
7658d439bSTaku Izumi  * version 2, as published by the Free Software Foundation.
8658d439bSTaku Izumi  *
9658d439bSTaku Izumi  * This program is distributed in the hope it will be useful, but WITHOUT
10658d439bSTaku Izumi  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11658d439bSTaku Izumi  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12658d439bSTaku Izumi  * more details.
13658d439bSTaku Izumi  *
14658d439bSTaku Izumi  * You should have received a copy of the GNU General Public License along with
15658d439bSTaku Izumi  * this program; if not, see <http://www.gnu.org/licenses/>.
16658d439bSTaku Izumi  *
17658d439bSTaku Izumi  * The full GNU General Public License is included in this distribution in
18658d439bSTaku Izumi  * the file called "COPYING".
19658d439bSTaku Izumi  *
20658d439bSTaku Izumi  */
21658d439bSTaku Izumi 
22658d439bSTaku Izumi #include <linux/module.h>
23658d439bSTaku Izumi #include <linux/types.h>
24658d439bSTaku Izumi #include <linux/nls.h>
25658d439bSTaku Izumi #include <linux/platform_device.h>
262fcbca68STaku Izumi #include <linux/netdevice.h>
27e5d486dcSTaku Izumi #include <linux/interrupt.h>
28658d439bSTaku Izumi 
29658d439bSTaku Izumi #include "fjes.h"
30658d439bSTaku Izumi 
31658d439bSTaku Izumi #define MAJ 1
32658d439bSTaku Izumi #define MIN 0
33658d439bSTaku Izumi #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN)
34658d439bSTaku Izumi #define DRV_NAME	"fjes"
35658d439bSTaku Izumi char fjes_driver_name[] = DRV_NAME;
36658d439bSTaku Izumi char fjes_driver_version[] = DRV_VERSION;
37658d439bSTaku Izumi static const char fjes_driver_string[] =
38658d439bSTaku Izumi 		"FUJITSU Extended Socket Network Device Driver";
39658d439bSTaku Izumi static const char fjes_copyright[] =
40658d439bSTaku Izumi 		"Copyright (c) 2015 FUJITSU LIMITED";
41658d439bSTaku Izumi 
42658d439bSTaku Izumi MODULE_AUTHOR("Taku Izumi <izumi.taku@jp.fujitsu.com>");
43658d439bSTaku Izumi MODULE_DESCRIPTION("FUJITSU Extended Socket Network Device Driver");
44658d439bSTaku Izumi MODULE_LICENSE("GPL");
45658d439bSTaku Izumi MODULE_VERSION(DRV_VERSION);
46658d439bSTaku Izumi 
47e5d486dcSTaku Izumi static int fjes_request_irq(struct fjes_adapter *);
48e5d486dcSTaku Izumi static void fjes_free_irq(struct fjes_adapter *);
49e5d486dcSTaku Izumi 
50e5d486dcSTaku Izumi static int fjes_open(struct net_device *);
51e5d486dcSTaku Izumi static int fjes_close(struct net_device *);
52e5d486dcSTaku Izumi static int fjes_setup_resources(struct fjes_adapter *);
53e5d486dcSTaku Izumi static void fjes_free_resources(struct fjes_adapter *);
549acf51cbSTaku Izumi static netdev_tx_t fjes_xmit_frame(struct sk_buff *, struct net_device *);
55b772b9dcSTaku Izumi static void fjes_raise_intr_rxdata_task(struct work_struct *);
56ac63b947STaku Izumi static void fjes_tx_stall_task(struct work_struct *);
57ff5b4210STaku Izumi static void fjes_force_close_task(struct work_struct *);
58e5d486dcSTaku Izumi static irqreturn_t fjes_intr(int, void*);
59879bc9a3STaku Izumi static struct rtnl_link_stats64 *
60879bc9a3STaku Izumi fjes_get_stats64(struct net_device *, struct rtnl_link_stats64 *);
61b9e23a67STaku Izumi static int fjes_change_mtu(struct net_device *, int);
623e3feddaSTaku Izumi static int fjes_vlan_rx_add_vid(struct net_device *, __be16 proto, u16);
633e3feddaSTaku Izumi static int fjes_vlan_rx_kill_vid(struct net_device *, __be16 proto, u16);
644393e767STaku Izumi static void fjes_tx_retry(struct net_device *);
65e5d486dcSTaku Izumi 
66658d439bSTaku Izumi static int fjes_acpi_add(struct acpi_device *);
67658d439bSTaku Izumi static int fjes_acpi_remove(struct acpi_device *);
68658d439bSTaku Izumi static acpi_status fjes_get_acpi_resource(struct acpi_resource *, void*);
69658d439bSTaku Izumi 
70658d439bSTaku Izumi static int fjes_probe(struct platform_device *);
71658d439bSTaku Izumi static int fjes_remove(struct platform_device *);
72658d439bSTaku Izumi 
732fcbca68STaku Izumi static int fjes_sw_init(struct fjes_adapter *);
742fcbca68STaku Izumi static void fjes_netdev_setup(struct net_device *);
758edb62a8STaku Izumi static void fjes_irq_watch_task(struct work_struct *);
768fc4cadbSTaku Izumi static void fjes_watch_unshare_task(struct work_struct *);
7726585930STaku Izumi static void fjes_rx_irq(struct fjes_adapter *, int);
7826585930STaku Izumi static int fjes_poll(struct napi_struct *, int);
7926585930STaku Izumi 
80658d439bSTaku Izumi static const struct acpi_device_id fjes_acpi_ids[] = {
81658d439bSTaku Izumi 	{"PNP0C02", 0},
82658d439bSTaku Izumi 	{"", 0},
83658d439bSTaku Izumi };
84658d439bSTaku Izumi MODULE_DEVICE_TABLE(acpi, fjes_acpi_ids);
85658d439bSTaku Izumi 
86658d439bSTaku Izumi static struct acpi_driver fjes_acpi_driver = {
87658d439bSTaku Izumi 	.name = DRV_NAME,
88658d439bSTaku Izumi 	.class = DRV_NAME,
89658d439bSTaku Izumi 	.owner = THIS_MODULE,
90658d439bSTaku Izumi 	.ids = fjes_acpi_ids,
91658d439bSTaku Izumi 	.ops = {
92658d439bSTaku Izumi 		.add = fjes_acpi_add,
93658d439bSTaku Izumi 		.remove = fjes_acpi_remove,
94658d439bSTaku Izumi 	},
95658d439bSTaku Izumi };
96658d439bSTaku Izumi 
97658d439bSTaku Izumi static struct platform_driver fjes_driver = {
98658d439bSTaku Izumi 	.driver = {
99658d439bSTaku Izumi 		.name = DRV_NAME,
100658d439bSTaku Izumi 		.owner = THIS_MODULE,
101658d439bSTaku Izumi 	},
102658d439bSTaku Izumi 	.probe = fjes_probe,
103658d439bSTaku Izumi 	.remove = fjes_remove,
104658d439bSTaku Izumi };
105658d439bSTaku Izumi 
106658d439bSTaku Izumi static struct resource fjes_resource[] = {
107658d439bSTaku Izumi 	{
108658d439bSTaku Izumi 		.flags = IORESOURCE_MEM,
109658d439bSTaku Izumi 		.start = 0,
110658d439bSTaku Izumi 		.end = 0,
111658d439bSTaku Izumi 	},
112658d439bSTaku Izumi 	{
113658d439bSTaku Izumi 		.flags = IORESOURCE_IRQ,
114658d439bSTaku Izumi 		.start = 0,
115658d439bSTaku Izumi 		.end = 0,
116658d439bSTaku Izumi 	},
117658d439bSTaku Izumi };
118658d439bSTaku Izumi 
119658d439bSTaku Izumi static int fjes_acpi_add(struct acpi_device *device)
120658d439bSTaku Izumi {
121658d439bSTaku Izumi 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
122658d439bSTaku Izumi 	char str_buf[sizeof(FJES_ACPI_SYMBOL) + 1];
123658d439bSTaku Izumi 	struct platform_device *plat_dev;
124658d439bSTaku Izumi 	union acpi_object *str;
125658d439bSTaku Izumi 	acpi_status status;
126658d439bSTaku Izumi 	int result;
127658d439bSTaku Izumi 
128658d439bSTaku Izumi 	status = acpi_evaluate_object(device->handle, "_STR", NULL, &buffer);
129658d439bSTaku Izumi 	if (ACPI_FAILURE(status))
130658d439bSTaku Izumi 		return -ENODEV;
131658d439bSTaku Izumi 
132658d439bSTaku Izumi 	str = buffer.pointer;
133658d439bSTaku Izumi 	result = utf16s_to_utf8s((wchar_t *)str->string.pointer,
134658d439bSTaku Izumi 				 str->string.length, UTF16_LITTLE_ENDIAN,
135658d439bSTaku Izumi 				 str_buf, sizeof(str_buf) - 1);
136658d439bSTaku Izumi 	str_buf[result] = 0;
137658d439bSTaku Izumi 
138658d439bSTaku Izumi 	if (strncmp(FJES_ACPI_SYMBOL, str_buf, strlen(FJES_ACPI_SYMBOL)) != 0) {
139658d439bSTaku Izumi 		kfree(buffer.pointer);
140658d439bSTaku Izumi 		return -ENODEV;
141658d439bSTaku Izumi 	}
142658d439bSTaku Izumi 	kfree(buffer.pointer);
143658d439bSTaku Izumi 
144658d439bSTaku Izumi 	status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
145658d439bSTaku Izumi 				     fjes_get_acpi_resource, fjes_resource);
146658d439bSTaku Izumi 	if (ACPI_FAILURE(status))
147658d439bSTaku Izumi 		return -ENODEV;
148658d439bSTaku Izumi 
149658d439bSTaku Izumi 	/* create platform_device */
150658d439bSTaku Izumi 	plat_dev = platform_device_register_simple(DRV_NAME, 0, fjes_resource,
151658d439bSTaku Izumi 						   ARRAY_SIZE(fjes_resource));
152658d439bSTaku Izumi 	device->driver_data = plat_dev;
153658d439bSTaku Izumi 
154658d439bSTaku Izumi 	return 0;
155658d439bSTaku Izumi }
156658d439bSTaku Izumi 
157658d439bSTaku Izumi static int fjes_acpi_remove(struct acpi_device *device)
158658d439bSTaku Izumi {
159658d439bSTaku Izumi 	struct platform_device *plat_dev;
160658d439bSTaku Izumi 
161658d439bSTaku Izumi 	plat_dev = (struct platform_device *)acpi_driver_data(device);
162658d439bSTaku Izumi 	platform_device_unregister(plat_dev);
163658d439bSTaku Izumi 
164658d439bSTaku Izumi 	return 0;
165658d439bSTaku Izumi }
166658d439bSTaku Izumi 
167658d439bSTaku Izumi static acpi_status
168658d439bSTaku Izumi fjes_get_acpi_resource(struct acpi_resource *acpi_res, void *data)
169658d439bSTaku Izumi {
170658d439bSTaku Izumi 	struct acpi_resource_address32 *addr;
171658d439bSTaku Izumi 	struct acpi_resource_irq *irq;
172658d439bSTaku Izumi 	struct resource *res = data;
173658d439bSTaku Izumi 
174658d439bSTaku Izumi 	switch (acpi_res->type) {
175658d439bSTaku Izumi 	case ACPI_RESOURCE_TYPE_ADDRESS32:
176658d439bSTaku Izumi 		addr = &acpi_res->data.address32;
177658d439bSTaku Izumi 		res[0].start = addr->address.minimum;
178658d439bSTaku Izumi 		res[0].end = addr->address.minimum +
179658d439bSTaku Izumi 			addr->address.address_length - 1;
180658d439bSTaku Izumi 		break;
181658d439bSTaku Izumi 
182658d439bSTaku Izumi 	case ACPI_RESOURCE_TYPE_IRQ:
183658d439bSTaku Izumi 		irq = &acpi_res->data.irq;
184658d439bSTaku Izumi 		if (irq->interrupt_count != 1)
185658d439bSTaku Izumi 			return AE_ERROR;
186658d439bSTaku Izumi 		res[1].start = irq->interrupts[0];
187658d439bSTaku Izumi 		res[1].end = irq->interrupts[0];
188658d439bSTaku Izumi 		break;
189658d439bSTaku Izumi 
190658d439bSTaku Izumi 	default:
191658d439bSTaku Izumi 		break;
192658d439bSTaku Izumi 	}
193658d439bSTaku Izumi 
194658d439bSTaku Izumi 	return AE_OK;
195658d439bSTaku Izumi }
196658d439bSTaku Izumi 
197e5d486dcSTaku Izumi static int fjes_request_irq(struct fjes_adapter *adapter)
198e5d486dcSTaku Izumi {
199e5d486dcSTaku Izumi 	struct net_device *netdev = adapter->netdev;
200e5d486dcSTaku Izumi 	int result = -1;
201e5d486dcSTaku Izumi 
2028edb62a8STaku Izumi 	adapter->interrupt_watch_enable = true;
2038edb62a8STaku Izumi 	if (!delayed_work_pending(&adapter->interrupt_watch_task)) {
2048edb62a8STaku Izumi 		queue_delayed_work(adapter->control_wq,
2058edb62a8STaku Izumi 				   &adapter->interrupt_watch_task,
2068edb62a8STaku Izumi 				   FJES_IRQ_WATCH_DELAY);
2078edb62a8STaku Izumi 	}
2088edb62a8STaku Izumi 
209e5d486dcSTaku Izumi 	if (!adapter->irq_registered) {
210e5d486dcSTaku Izumi 		result = request_irq(adapter->hw.hw_res.irq, fjes_intr,
211e5d486dcSTaku Izumi 				     IRQF_SHARED, netdev->name, adapter);
212e5d486dcSTaku Izumi 		if (result)
213e5d486dcSTaku Izumi 			adapter->irq_registered = false;
214e5d486dcSTaku Izumi 		else
215e5d486dcSTaku Izumi 			adapter->irq_registered = true;
216e5d486dcSTaku Izumi 	}
217e5d486dcSTaku Izumi 
218e5d486dcSTaku Izumi 	return result;
219e5d486dcSTaku Izumi }
220e5d486dcSTaku Izumi 
221e5d486dcSTaku Izumi static void fjes_free_irq(struct fjes_adapter *adapter)
222e5d486dcSTaku Izumi {
223e5d486dcSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
224e5d486dcSTaku Izumi 
2258edb62a8STaku Izumi 	adapter->interrupt_watch_enable = false;
2268edb62a8STaku Izumi 	cancel_delayed_work_sync(&adapter->interrupt_watch_task);
2278edb62a8STaku Izumi 
228e5d486dcSTaku Izumi 	fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, true);
229e5d486dcSTaku Izumi 
230e5d486dcSTaku Izumi 	if (adapter->irq_registered) {
231e5d486dcSTaku Izumi 		free_irq(adapter->hw.hw_res.irq, adapter);
232e5d486dcSTaku Izumi 		adapter->irq_registered = false;
233e5d486dcSTaku Izumi 	}
234e5d486dcSTaku Izumi }
235e5d486dcSTaku Izumi 
2362fcbca68STaku Izumi static const struct net_device_ops fjes_netdev_ops = {
237e5d486dcSTaku Izumi 	.ndo_open		= fjes_open,
238e5d486dcSTaku Izumi 	.ndo_stop		= fjes_close,
2399acf51cbSTaku Izumi 	.ndo_start_xmit		= fjes_xmit_frame,
240879bc9a3STaku Izumi 	.ndo_get_stats64	= fjes_get_stats64,
241b9e23a67STaku Izumi 	.ndo_change_mtu		= fjes_change_mtu,
2424393e767STaku Izumi 	.ndo_tx_timeout		= fjes_tx_retry,
2433e3feddaSTaku Izumi 	.ndo_vlan_rx_add_vid	= fjes_vlan_rx_add_vid,
2443e3feddaSTaku Izumi 	.ndo_vlan_rx_kill_vid = fjes_vlan_rx_kill_vid,
2452fcbca68STaku Izumi };
2462fcbca68STaku Izumi 
247e5d486dcSTaku Izumi /* fjes_open - Called when a network interface is made active */
248e5d486dcSTaku Izumi static int fjes_open(struct net_device *netdev)
249e5d486dcSTaku Izumi {
250e5d486dcSTaku Izumi 	struct fjes_adapter *adapter = netdev_priv(netdev);
251e5d486dcSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
252e5d486dcSTaku Izumi 	int result;
253e5d486dcSTaku Izumi 
254e5d486dcSTaku Izumi 	if (adapter->open_guard)
255e5d486dcSTaku Izumi 		return -ENXIO;
256e5d486dcSTaku Izumi 
257e5d486dcSTaku Izumi 	result = fjes_setup_resources(adapter);
258e5d486dcSTaku Izumi 	if (result)
259e5d486dcSTaku Izumi 		goto err_setup_res;
260e5d486dcSTaku Izumi 
261e5d486dcSTaku Izumi 	hw->txrx_stop_req_bit = 0;
262e5d486dcSTaku Izumi 	hw->epstop_req_bit = 0;
263e5d486dcSTaku Izumi 
26426585930STaku Izumi 	napi_enable(&adapter->napi);
26526585930STaku Izumi 
266e5d486dcSTaku Izumi 	fjes_hw_capture_interrupt_status(hw);
267e5d486dcSTaku Izumi 
268e5d486dcSTaku Izumi 	result = fjes_request_irq(adapter);
269e5d486dcSTaku Izumi 	if (result)
270e5d486dcSTaku Izumi 		goto err_req_irq;
271e5d486dcSTaku Izumi 
272e5d486dcSTaku Izumi 	fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, false);
273e5d486dcSTaku Izumi 
274e5d486dcSTaku Izumi 	netif_tx_start_all_queues(netdev);
275e5d486dcSTaku Izumi 	netif_carrier_on(netdev);
276e5d486dcSTaku Izumi 
277e5d486dcSTaku Izumi 	return 0;
278e5d486dcSTaku Izumi 
279e5d486dcSTaku Izumi err_req_irq:
280e5d486dcSTaku Izumi 	fjes_free_irq(adapter);
28126585930STaku Izumi 	napi_disable(&adapter->napi);
282e5d486dcSTaku Izumi 
283e5d486dcSTaku Izumi err_setup_res:
284e5d486dcSTaku Izumi 	fjes_free_resources(adapter);
285e5d486dcSTaku Izumi 	return result;
286e5d486dcSTaku Izumi }
287e5d486dcSTaku Izumi 
288e5d486dcSTaku Izumi /* fjes_close - Disables a network interface */
289e5d486dcSTaku Izumi static int fjes_close(struct net_device *netdev)
290e5d486dcSTaku Izumi {
291e5d486dcSTaku Izumi 	struct fjes_adapter *adapter = netdev_priv(netdev);
292e5d486dcSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
293e5d486dcSTaku Izumi 	int epidx;
294e5d486dcSTaku Izumi 
295e5d486dcSTaku Izumi 	netif_tx_stop_all_queues(netdev);
296e5d486dcSTaku Izumi 	netif_carrier_off(netdev);
297e5d486dcSTaku Izumi 
298e5d486dcSTaku Izumi 	fjes_hw_raise_epstop(hw);
299e5d486dcSTaku Izumi 
30026585930STaku Izumi 	napi_disable(&adapter->napi);
30126585930STaku Izumi 
302e5d486dcSTaku Izumi 	for (epidx = 0; epidx < hw->max_epid; epidx++) {
303e5d486dcSTaku Izumi 		if (epidx == hw->my_epid)
304e5d486dcSTaku Izumi 			continue;
305e5d486dcSTaku Izumi 
306e5d486dcSTaku Izumi 		adapter->hw.ep_shm_info[epidx].tx.info->v1i.rx_status &=
307e5d486dcSTaku Izumi 			~FJES_RX_POLL_WORK;
308e5d486dcSTaku Izumi 	}
309e5d486dcSTaku Izumi 
310e5d486dcSTaku Izumi 	fjes_free_irq(adapter);
311e5d486dcSTaku Izumi 
3128edb62a8STaku Izumi 	cancel_delayed_work_sync(&adapter->interrupt_watch_task);
3138fc4cadbSTaku Izumi 	cancel_work_sync(&adapter->unshare_watch_task);
3148fc4cadbSTaku Izumi 	adapter->unshare_watch_bitmask = 0;
315b772b9dcSTaku Izumi 	cancel_work_sync(&adapter->raise_intr_rxdata_task);
316ac63b947STaku Izumi 	cancel_work_sync(&adapter->tx_stall_task);
317b772b9dcSTaku Izumi 
318785f28e0STaku Izumi 	cancel_work_sync(&hw->update_zone_task);
319b5a9152dSTaku Izumi 	cancel_work_sync(&hw->epstop_task);
320785f28e0STaku Izumi 
321e5d486dcSTaku Izumi 	fjes_hw_wait_epstop(hw);
322e5d486dcSTaku Izumi 
323e5d486dcSTaku Izumi 	fjes_free_resources(adapter);
324e5d486dcSTaku Izumi 
325e5d486dcSTaku Izumi 	return 0;
326e5d486dcSTaku Izumi }
327e5d486dcSTaku Izumi 
328e5d486dcSTaku Izumi static int fjes_setup_resources(struct fjes_adapter *adapter)
329e5d486dcSTaku Izumi {
330e5d486dcSTaku Izumi 	struct net_device *netdev = adapter->netdev;
331e5d486dcSTaku Izumi 	struct ep_share_mem_info *buf_pair;
332e5d486dcSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
333e5d486dcSTaku Izumi 	int result;
334e5d486dcSTaku Izumi 	int epidx;
335e5d486dcSTaku Izumi 
336e5d486dcSTaku Izumi 	mutex_lock(&hw->hw_info.lock);
337e5d486dcSTaku Izumi 	result = fjes_hw_request_info(hw);
338e5d486dcSTaku Izumi 	switch (result) {
339e5d486dcSTaku Izumi 	case 0:
340e5d486dcSTaku Izumi 		for (epidx = 0; epidx < hw->max_epid; epidx++) {
341e5d486dcSTaku Izumi 			hw->ep_shm_info[epidx].es_status =
342e5d486dcSTaku Izumi 			    hw->hw_info.res_buf->info.info[epidx].es_status;
343e5d486dcSTaku Izumi 			hw->ep_shm_info[epidx].zone =
344e5d486dcSTaku Izumi 			    hw->hw_info.res_buf->info.info[epidx].zone;
345e5d486dcSTaku Izumi 		}
346e5d486dcSTaku Izumi 		break;
347e5d486dcSTaku Izumi 	default:
348e5d486dcSTaku Izumi 	case -ENOMSG:
349e5d486dcSTaku Izumi 	case -EBUSY:
350e5d486dcSTaku Izumi 		adapter->force_reset = true;
351e5d486dcSTaku Izumi 
352e5d486dcSTaku Izumi 		mutex_unlock(&hw->hw_info.lock);
353e5d486dcSTaku Izumi 		return result;
354e5d486dcSTaku Izumi 	}
355e5d486dcSTaku Izumi 	mutex_unlock(&hw->hw_info.lock);
356e5d486dcSTaku Izumi 
357e5d486dcSTaku Izumi 	for (epidx = 0; epidx < (hw->max_epid); epidx++) {
358e5d486dcSTaku Izumi 		if ((epidx != hw->my_epid) &&
359e5d486dcSTaku Izumi 		    (hw->ep_shm_info[epidx].es_status ==
360e5d486dcSTaku Izumi 		     FJES_ZONING_STATUS_ENABLE)) {
361e5d486dcSTaku Izumi 			fjes_hw_raise_interrupt(hw, epidx,
362e5d486dcSTaku Izumi 						REG_ICTL_MASK_INFO_UPDATE);
363e5d486dcSTaku Izumi 		}
364e5d486dcSTaku Izumi 	}
365e5d486dcSTaku Izumi 
366e5d486dcSTaku Izumi 	msleep(FJES_OPEN_ZONE_UPDATE_WAIT * hw->max_epid);
367e5d486dcSTaku Izumi 
368e5d486dcSTaku Izumi 	for (epidx = 0; epidx < (hw->max_epid); epidx++) {
369e5d486dcSTaku Izumi 		if (epidx == hw->my_epid)
370e5d486dcSTaku Izumi 			continue;
371e5d486dcSTaku Izumi 
372e5d486dcSTaku Izumi 		buf_pair = &hw->ep_shm_info[epidx];
373e5d486dcSTaku Izumi 
374e5d486dcSTaku Izumi 		fjes_hw_setup_epbuf(&buf_pair->tx, netdev->dev_addr,
375e5d486dcSTaku Izumi 				    netdev->mtu);
376e5d486dcSTaku Izumi 
377e5d486dcSTaku Izumi 		if (fjes_hw_epid_is_same_zone(hw, epidx)) {
378e5d486dcSTaku Izumi 			mutex_lock(&hw->hw_info.lock);
379e5d486dcSTaku Izumi 			result =
380e5d486dcSTaku Izumi 			fjes_hw_register_buff_addr(hw, epidx, buf_pair);
381e5d486dcSTaku Izumi 			mutex_unlock(&hw->hw_info.lock);
382e5d486dcSTaku Izumi 
383e5d486dcSTaku Izumi 			switch (result) {
384e5d486dcSTaku Izumi 			case 0:
385e5d486dcSTaku Izumi 				break;
386e5d486dcSTaku Izumi 			case -ENOMSG:
387e5d486dcSTaku Izumi 			case -EBUSY:
388e5d486dcSTaku Izumi 			default:
389e5d486dcSTaku Izumi 				adapter->force_reset = true;
390e5d486dcSTaku Izumi 				return result;
391e5d486dcSTaku Izumi 			}
392e5d486dcSTaku Izumi 		}
393e5d486dcSTaku Izumi 	}
394e5d486dcSTaku Izumi 
395e5d486dcSTaku Izumi 	return 0;
396e5d486dcSTaku Izumi }
397e5d486dcSTaku Izumi 
398e5d486dcSTaku Izumi static void fjes_free_resources(struct fjes_adapter *adapter)
399e5d486dcSTaku Izumi {
400e5d486dcSTaku Izumi 	struct net_device *netdev = adapter->netdev;
401e5d486dcSTaku Izumi 	struct fjes_device_command_param param;
402e5d486dcSTaku Izumi 	struct ep_share_mem_info *buf_pair;
403e5d486dcSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
404e5d486dcSTaku Izumi 	bool reset_flag = false;
405e5d486dcSTaku Izumi 	int result;
406e5d486dcSTaku Izumi 	int epidx;
407e5d486dcSTaku Izumi 
408e5d486dcSTaku Izumi 	for (epidx = 0; epidx < hw->max_epid; epidx++) {
409e5d486dcSTaku Izumi 		if (epidx == hw->my_epid)
410e5d486dcSTaku Izumi 			continue;
411e5d486dcSTaku Izumi 
412e5d486dcSTaku Izumi 		mutex_lock(&hw->hw_info.lock);
413e5d486dcSTaku Izumi 		result = fjes_hw_unregister_buff_addr(hw, epidx);
414e5d486dcSTaku Izumi 		mutex_unlock(&hw->hw_info.lock);
415e5d486dcSTaku Izumi 
416e5d486dcSTaku Izumi 		if (result)
417e5d486dcSTaku Izumi 			reset_flag = true;
418e5d486dcSTaku Izumi 
419e5d486dcSTaku Izumi 		buf_pair = &hw->ep_shm_info[epidx];
420e5d486dcSTaku Izumi 
421e5d486dcSTaku Izumi 		fjes_hw_setup_epbuf(&buf_pair->tx,
422e5d486dcSTaku Izumi 				    netdev->dev_addr, netdev->mtu);
423e5d486dcSTaku Izumi 
424e5d486dcSTaku Izumi 		clear_bit(epidx, &hw->txrx_stop_req_bit);
425e5d486dcSTaku Izumi 	}
426e5d486dcSTaku Izumi 
427e5d486dcSTaku Izumi 	if (reset_flag || adapter->force_reset) {
428e5d486dcSTaku Izumi 		result = fjes_hw_reset(hw);
429e5d486dcSTaku Izumi 
430e5d486dcSTaku Izumi 		adapter->force_reset = false;
431e5d486dcSTaku Izumi 
432e5d486dcSTaku Izumi 		if (result)
433e5d486dcSTaku Izumi 			adapter->open_guard = true;
434e5d486dcSTaku Izumi 
435e5d486dcSTaku Izumi 		hw->hw_info.buffer_share_bit = 0;
436e5d486dcSTaku Izumi 
437e5d486dcSTaku Izumi 		memset((void *)&param, 0, sizeof(param));
438e5d486dcSTaku Izumi 
439e5d486dcSTaku Izumi 		param.req_len = hw->hw_info.req_buf_size;
440e5d486dcSTaku Izumi 		param.req_start = __pa(hw->hw_info.req_buf);
441e5d486dcSTaku Izumi 		param.res_len = hw->hw_info.res_buf_size;
442e5d486dcSTaku Izumi 		param.res_start = __pa(hw->hw_info.res_buf);
443e5d486dcSTaku Izumi 		param.share_start = __pa(hw->hw_info.share->ep_status);
444e5d486dcSTaku Izumi 
445e5d486dcSTaku Izumi 		fjes_hw_init_command_registers(hw, &param);
446e5d486dcSTaku Izumi 	}
447e5d486dcSTaku Izumi }
448e5d486dcSTaku Izumi 
449ac63b947STaku Izumi static void fjes_tx_stall_task(struct work_struct *work)
450ac63b947STaku Izumi {
451ac63b947STaku Izumi 	struct fjes_adapter *adapter = container_of(work,
452ac63b947STaku Izumi 			struct fjes_adapter, tx_stall_task);
453ac63b947STaku Izumi 	struct net_device *netdev = adapter->netdev;
454ac63b947STaku Izumi 	struct fjes_hw *hw = &adapter->hw;
455ac63b947STaku Izumi 	int all_queue_available, sendable;
456ac63b947STaku Izumi 	enum ep_partner_status pstatus;
457ac63b947STaku Izumi 	int max_epid, my_epid, epid;
458ac63b947STaku Izumi 	union ep_buffer_info *info;
459ac63b947STaku Izumi 	int i;
460ac63b947STaku Izumi 
461ac63b947STaku Izumi 	if (((long)jiffies -
462ac63b947STaku Izumi 		(long)(netdev->trans_start)) > FJES_TX_TX_STALL_TIMEOUT) {
463ac63b947STaku Izumi 		netif_wake_queue(netdev);
464ac63b947STaku Izumi 		return;
465ac63b947STaku Izumi 	}
466ac63b947STaku Izumi 
467ac63b947STaku Izumi 	my_epid = hw->my_epid;
468ac63b947STaku Izumi 	max_epid = hw->max_epid;
469ac63b947STaku Izumi 
470ac63b947STaku Izumi 	for (i = 0; i < 5; i++) {
471ac63b947STaku Izumi 		all_queue_available = 1;
472ac63b947STaku Izumi 
473ac63b947STaku Izumi 		for (epid = 0; epid < max_epid; epid++) {
474ac63b947STaku Izumi 			if (my_epid == epid)
475ac63b947STaku Izumi 				continue;
476ac63b947STaku Izumi 
477ac63b947STaku Izumi 			pstatus = fjes_hw_get_partner_ep_status(hw, epid);
478ac63b947STaku Izumi 			sendable = (pstatus == EP_PARTNER_SHARED);
479ac63b947STaku Izumi 			if (!sendable)
480ac63b947STaku Izumi 				continue;
481ac63b947STaku Izumi 
482ac63b947STaku Izumi 			info = adapter->hw.ep_shm_info[epid].tx.info;
483ac63b947STaku Izumi 
484ac63b947STaku Izumi 			if (EP_RING_FULL(info->v1i.head, info->v1i.tail,
485ac63b947STaku Izumi 					 info->v1i.count_max)) {
486ac63b947STaku Izumi 				all_queue_available = 0;
487ac63b947STaku Izumi 				break;
488ac63b947STaku Izumi 			}
489ac63b947STaku Izumi 		}
490ac63b947STaku Izumi 
491ac63b947STaku Izumi 		if (all_queue_available) {
492ac63b947STaku Izumi 			netif_wake_queue(netdev);
493ac63b947STaku Izumi 			return;
494ac63b947STaku Izumi 		}
495ac63b947STaku Izumi 	}
496ac63b947STaku Izumi 
497ac63b947STaku Izumi 	usleep_range(50, 100);
498ac63b947STaku Izumi 
499ac63b947STaku Izumi 	queue_work(adapter->txrx_wq, &adapter->tx_stall_task);
500ac63b947STaku Izumi }
501ac63b947STaku Izumi 
502ff5b4210STaku Izumi static void fjes_force_close_task(struct work_struct *work)
503ff5b4210STaku Izumi {
504ff5b4210STaku Izumi 	struct fjes_adapter *adapter = container_of(work,
505ff5b4210STaku Izumi 			struct fjes_adapter, force_close_task);
506ff5b4210STaku Izumi 	struct net_device *netdev = adapter->netdev;
507ff5b4210STaku Izumi 
508ff5b4210STaku Izumi 	rtnl_lock();
509ff5b4210STaku Izumi 	dev_close(netdev);
510ff5b4210STaku Izumi 	rtnl_unlock();
511ff5b4210STaku Izumi }
512ff5b4210STaku Izumi 
513b772b9dcSTaku Izumi static void fjes_raise_intr_rxdata_task(struct work_struct *work)
514b772b9dcSTaku Izumi {
515b772b9dcSTaku Izumi 	struct fjes_adapter *adapter = container_of(work,
516b772b9dcSTaku Izumi 			struct fjes_adapter, raise_intr_rxdata_task);
517b772b9dcSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
518b772b9dcSTaku Izumi 	enum ep_partner_status pstatus;
519b772b9dcSTaku Izumi 	int max_epid, my_epid, epid;
520b772b9dcSTaku Izumi 
521b772b9dcSTaku Izumi 	my_epid = hw->my_epid;
522b772b9dcSTaku Izumi 	max_epid = hw->max_epid;
523b772b9dcSTaku Izumi 
524b772b9dcSTaku Izumi 	for (epid = 0; epid < max_epid; epid++)
525b772b9dcSTaku Izumi 		hw->ep_shm_info[epid].tx_status_work = 0;
526b772b9dcSTaku Izumi 
527b772b9dcSTaku Izumi 	for (epid = 0; epid < max_epid; epid++) {
528b772b9dcSTaku Izumi 		if (epid == my_epid)
529b772b9dcSTaku Izumi 			continue;
530b772b9dcSTaku Izumi 
531b772b9dcSTaku Izumi 		pstatus = fjes_hw_get_partner_ep_status(hw, epid);
532b772b9dcSTaku Izumi 		if (pstatus == EP_PARTNER_SHARED) {
533b772b9dcSTaku Izumi 			hw->ep_shm_info[epid].tx_status_work =
534b772b9dcSTaku Izumi 				hw->ep_shm_info[epid].tx.info->v1i.tx_status;
535b772b9dcSTaku Izumi 
536b772b9dcSTaku Izumi 			if (hw->ep_shm_info[epid].tx_status_work ==
537b772b9dcSTaku Izumi 				FJES_TX_DELAY_SEND_PENDING) {
538b772b9dcSTaku Izumi 				hw->ep_shm_info[epid].tx.info->v1i.tx_status =
539b772b9dcSTaku Izumi 					FJES_TX_DELAY_SEND_NONE;
540b772b9dcSTaku Izumi 			}
541b772b9dcSTaku Izumi 		}
542b772b9dcSTaku Izumi 	}
543b772b9dcSTaku Izumi 
544b772b9dcSTaku Izumi 	for (epid = 0; epid < max_epid; epid++) {
545b772b9dcSTaku Izumi 		if (epid == my_epid)
546b772b9dcSTaku Izumi 			continue;
547b772b9dcSTaku Izumi 
548b772b9dcSTaku Izumi 		pstatus = fjes_hw_get_partner_ep_status(hw, epid);
549b772b9dcSTaku Izumi 		if ((hw->ep_shm_info[epid].tx_status_work ==
550b772b9dcSTaku Izumi 		     FJES_TX_DELAY_SEND_PENDING) &&
551b772b9dcSTaku Izumi 		    (pstatus == EP_PARTNER_SHARED) &&
552b772b9dcSTaku Izumi 		    !(hw->ep_shm_info[epid].rx.info->v1i.rx_status)) {
553b772b9dcSTaku Izumi 			fjes_hw_raise_interrupt(hw, epid,
554b772b9dcSTaku Izumi 						REG_ICTL_MASK_RX_DATA);
555b772b9dcSTaku Izumi 		}
556b772b9dcSTaku Izumi 	}
557b772b9dcSTaku Izumi 
558b772b9dcSTaku Izumi 	usleep_range(500, 1000);
559b772b9dcSTaku Izumi }
560b772b9dcSTaku Izumi 
5619acf51cbSTaku Izumi static int fjes_tx_send(struct fjes_adapter *adapter, int dest,
5629acf51cbSTaku Izumi 			void *data, size_t len)
5639acf51cbSTaku Izumi {
5649acf51cbSTaku Izumi 	int retval;
5659acf51cbSTaku Izumi 
5669acf51cbSTaku Izumi 	retval = fjes_hw_epbuf_tx_pkt_send(&adapter->hw.ep_shm_info[dest].tx,
5679acf51cbSTaku Izumi 					   data, len);
5689acf51cbSTaku Izumi 	if (retval)
5699acf51cbSTaku Izumi 		return retval;
5709acf51cbSTaku Izumi 
5719acf51cbSTaku Izumi 	adapter->hw.ep_shm_info[dest].tx.info->v1i.tx_status =
5729acf51cbSTaku Izumi 		FJES_TX_DELAY_SEND_PENDING;
573b772b9dcSTaku Izumi 	if (!work_pending(&adapter->raise_intr_rxdata_task))
574b772b9dcSTaku Izumi 		queue_work(adapter->txrx_wq,
575b772b9dcSTaku Izumi 			   &adapter->raise_intr_rxdata_task);
5769acf51cbSTaku Izumi 
5779acf51cbSTaku Izumi 	retval = 0;
5789acf51cbSTaku Izumi 	return retval;
5799acf51cbSTaku Izumi }
5809acf51cbSTaku Izumi 
5819acf51cbSTaku Izumi static netdev_tx_t
5829acf51cbSTaku Izumi fjes_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
5839acf51cbSTaku Izumi {
5849acf51cbSTaku Izumi 	struct fjes_adapter *adapter = netdev_priv(netdev);
5859acf51cbSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
5869acf51cbSTaku Izumi 
5879acf51cbSTaku Izumi 	int max_epid, my_epid, dest_epid;
5889acf51cbSTaku Izumi 	enum ep_partner_status pstatus;
5899acf51cbSTaku Izumi 	struct netdev_queue *cur_queue;
5909acf51cbSTaku Izumi 	char shortpkt[VLAN_ETH_HLEN];
5919acf51cbSTaku Izumi 	bool is_multi, vlan;
5929acf51cbSTaku Izumi 	struct ethhdr *eth;
5939acf51cbSTaku Izumi 	u16 queue_no = 0;
5949acf51cbSTaku Izumi 	u16 vlan_id = 0;
5959acf51cbSTaku Izumi 	netdev_tx_t ret;
5969acf51cbSTaku Izumi 	char *data;
5979acf51cbSTaku Izumi 	int len;
5989acf51cbSTaku Izumi 
5999acf51cbSTaku Izumi 	ret = NETDEV_TX_OK;
6009acf51cbSTaku Izumi 	is_multi = false;
6019acf51cbSTaku Izumi 	cur_queue = netdev_get_tx_queue(netdev, queue_no);
6029acf51cbSTaku Izumi 
6039acf51cbSTaku Izumi 	eth = (struct ethhdr *)skb->data;
6049acf51cbSTaku Izumi 	my_epid = hw->my_epid;
6059acf51cbSTaku Izumi 
6069acf51cbSTaku Izumi 	vlan = (vlan_get_tag(skb, &vlan_id) == 0) ? true : false;
6079acf51cbSTaku Izumi 
6089acf51cbSTaku Izumi 	data = skb->data;
6099acf51cbSTaku Izumi 	len = skb->len;
6109acf51cbSTaku Izumi 
6119acf51cbSTaku Izumi 	if (is_multicast_ether_addr(eth->h_dest)) {
6129acf51cbSTaku Izumi 		dest_epid = 0;
6139acf51cbSTaku Izumi 		max_epid = hw->max_epid;
6149acf51cbSTaku Izumi 		is_multi = true;
6159acf51cbSTaku Izumi 	} else if (is_local_ether_addr(eth->h_dest)) {
6169acf51cbSTaku Izumi 		dest_epid = eth->h_dest[ETH_ALEN - 1];
6179acf51cbSTaku Izumi 		max_epid = dest_epid + 1;
6189acf51cbSTaku Izumi 
6199acf51cbSTaku Izumi 		if ((eth->h_dest[0] == 0x02) &&
6209acf51cbSTaku Izumi 		    (0x00 == (eth->h_dest[1] | eth->h_dest[2] |
6219acf51cbSTaku Izumi 			      eth->h_dest[3] | eth->h_dest[4])) &&
6229acf51cbSTaku Izumi 		    (dest_epid < hw->max_epid)) {
6239acf51cbSTaku Izumi 			;
6249acf51cbSTaku Izumi 		} else {
6259acf51cbSTaku Izumi 			dest_epid = 0;
6269acf51cbSTaku Izumi 			max_epid = 0;
6279acf51cbSTaku Izumi 			ret = NETDEV_TX_OK;
6289acf51cbSTaku Izumi 
6299acf51cbSTaku Izumi 			adapter->stats64.tx_packets += 1;
6309acf51cbSTaku Izumi 			hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
6319acf51cbSTaku Izumi 			adapter->stats64.tx_bytes += len;
6329acf51cbSTaku Izumi 			hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
6339acf51cbSTaku Izumi 		}
6349acf51cbSTaku Izumi 	} else {
6359acf51cbSTaku Izumi 		dest_epid = 0;
6369acf51cbSTaku Izumi 		max_epid = 0;
6379acf51cbSTaku Izumi 		ret = NETDEV_TX_OK;
6389acf51cbSTaku Izumi 
6399acf51cbSTaku Izumi 		adapter->stats64.tx_packets += 1;
6409acf51cbSTaku Izumi 		hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
6419acf51cbSTaku Izumi 		adapter->stats64.tx_bytes += len;
6429acf51cbSTaku Izumi 		hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
6439acf51cbSTaku Izumi 	}
6449acf51cbSTaku Izumi 
6459acf51cbSTaku Izumi 	for (; dest_epid < max_epid; dest_epid++) {
6469acf51cbSTaku Izumi 		if (my_epid == dest_epid)
6479acf51cbSTaku Izumi 			continue;
6489acf51cbSTaku Izumi 
6499acf51cbSTaku Izumi 		pstatus = fjes_hw_get_partner_ep_status(hw, dest_epid);
6509acf51cbSTaku Izumi 		if (pstatus != EP_PARTNER_SHARED) {
6519acf51cbSTaku Izumi 			ret = NETDEV_TX_OK;
6529acf51cbSTaku Izumi 		} else if (!fjes_hw_check_epbuf_version(
6539acf51cbSTaku Izumi 				&adapter->hw.ep_shm_info[dest_epid].rx, 0)) {
6549acf51cbSTaku Izumi 			/* version is NOT 0 */
6559acf51cbSTaku Izumi 			adapter->stats64.tx_carrier_errors += 1;
656*3c3bd4a9STaku Izumi 			hw->ep_shm_info[dest_epid].net_stats
6579acf51cbSTaku Izumi 						.tx_carrier_errors += 1;
6589acf51cbSTaku Izumi 
6599acf51cbSTaku Izumi 			ret = NETDEV_TX_OK;
6609acf51cbSTaku Izumi 		} else if (!fjes_hw_check_mtu(
6619acf51cbSTaku Izumi 				&adapter->hw.ep_shm_info[dest_epid].rx,
6629acf51cbSTaku Izumi 				netdev->mtu)) {
6639acf51cbSTaku Izumi 			adapter->stats64.tx_dropped += 1;
664*3c3bd4a9STaku Izumi 			hw->ep_shm_info[dest_epid].net_stats.tx_dropped += 1;
6659acf51cbSTaku Izumi 			adapter->stats64.tx_errors += 1;
666*3c3bd4a9STaku Izumi 			hw->ep_shm_info[dest_epid].net_stats.tx_errors += 1;
6679acf51cbSTaku Izumi 
6689acf51cbSTaku Izumi 			ret = NETDEV_TX_OK;
6699acf51cbSTaku Izumi 		} else if (vlan &&
6709acf51cbSTaku Izumi 			   !fjes_hw_check_vlan_id(
6719acf51cbSTaku Izumi 				&adapter->hw.ep_shm_info[dest_epid].rx,
6729acf51cbSTaku Izumi 				vlan_id)) {
6739acf51cbSTaku Izumi 			ret = NETDEV_TX_OK;
6749acf51cbSTaku Izumi 		} else {
6759acf51cbSTaku Izumi 			if (len < VLAN_ETH_HLEN) {
6769acf51cbSTaku Izumi 				memset(shortpkt, 0, VLAN_ETH_HLEN);
6779acf51cbSTaku Izumi 				memcpy(shortpkt, skb->data, skb->len);
6789acf51cbSTaku Izumi 				len = VLAN_ETH_HLEN;
6799acf51cbSTaku Izumi 				data = shortpkt;
6809acf51cbSTaku Izumi 			}
6819acf51cbSTaku Izumi 
6829acf51cbSTaku Izumi 			if (adapter->tx_retry_count == 0) {
6839acf51cbSTaku Izumi 				adapter->tx_start_jiffies = jiffies;
6849acf51cbSTaku Izumi 				adapter->tx_retry_count = 1;
6859acf51cbSTaku Izumi 			} else {
6869acf51cbSTaku Izumi 				adapter->tx_retry_count++;
6879acf51cbSTaku Izumi 			}
6889acf51cbSTaku Izumi 
6899acf51cbSTaku Izumi 			if (fjes_tx_send(adapter, dest_epid, data, len)) {
6909acf51cbSTaku Izumi 				if (is_multi) {
6919acf51cbSTaku Izumi 					ret = NETDEV_TX_OK;
6929acf51cbSTaku Izumi 				} else if (
6939acf51cbSTaku Izumi 					   ((long)jiffies -
6949acf51cbSTaku Izumi 					    (long)adapter->tx_start_jiffies) >=
6959acf51cbSTaku Izumi 					    FJES_TX_RETRY_TIMEOUT) {
6969acf51cbSTaku Izumi 					adapter->stats64.tx_fifo_errors += 1;
697*3c3bd4a9STaku Izumi 					hw->ep_shm_info[dest_epid].net_stats
6989acf51cbSTaku Izumi 								.tx_fifo_errors += 1;
6999acf51cbSTaku Izumi 					adapter->stats64.tx_errors += 1;
700*3c3bd4a9STaku Izumi 					hw->ep_shm_info[dest_epid].net_stats
7019acf51cbSTaku Izumi 								.tx_errors += 1;
7029acf51cbSTaku Izumi 
7039acf51cbSTaku Izumi 					ret = NETDEV_TX_OK;
7049acf51cbSTaku Izumi 				} else {
7059acf51cbSTaku Izumi 					netdev->trans_start = jiffies;
7069acf51cbSTaku Izumi 					netif_tx_stop_queue(cur_queue);
7079acf51cbSTaku Izumi 
708ac63b947STaku Izumi 					if (!work_pending(&adapter->tx_stall_task))
709ac63b947STaku Izumi 						queue_work(adapter->txrx_wq,
710ac63b947STaku Izumi 							   &adapter->tx_stall_task);
711ac63b947STaku Izumi 
7129acf51cbSTaku Izumi 					ret = NETDEV_TX_BUSY;
7139acf51cbSTaku Izumi 				}
7149acf51cbSTaku Izumi 			} else {
7159acf51cbSTaku Izumi 				if (!is_multi) {
7169acf51cbSTaku Izumi 					adapter->stats64.tx_packets += 1;
717*3c3bd4a9STaku Izumi 					hw->ep_shm_info[dest_epid].net_stats
7189acf51cbSTaku Izumi 								.tx_packets += 1;
7199acf51cbSTaku Izumi 					adapter->stats64.tx_bytes += len;
720*3c3bd4a9STaku Izumi 					hw->ep_shm_info[dest_epid].net_stats
7219acf51cbSTaku Izumi 								.tx_bytes += len;
7229acf51cbSTaku Izumi 				}
7239acf51cbSTaku Izumi 
7249acf51cbSTaku Izumi 				adapter->tx_retry_count = 0;
7259acf51cbSTaku Izumi 				ret = NETDEV_TX_OK;
7269acf51cbSTaku Izumi 			}
7279acf51cbSTaku Izumi 		}
7289acf51cbSTaku Izumi 	}
7299acf51cbSTaku Izumi 
7309acf51cbSTaku Izumi 	if (ret == NETDEV_TX_OK) {
7319acf51cbSTaku Izumi 		dev_kfree_skb(skb);
7329acf51cbSTaku Izumi 		if (is_multi) {
7339acf51cbSTaku Izumi 			adapter->stats64.tx_packets += 1;
7349acf51cbSTaku Izumi 			hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
7359acf51cbSTaku Izumi 			adapter->stats64.tx_bytes += 1;
7369acf51cbSTaku Izumi 			hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
7379acf51cbSTaku Izumi 		}
7389acf51cbSTaku Izumi 	}
7399acf51cbSTaku Izumi 
7409acf51cbSTaku Izumi 	return ret;
7419acf51cbSTaku Izumi }
7429acf51cbSTaku Izumi 
7434393e767STaku Izumi static void fjes_tx_retry(struct net_device *netdev)
7444393e767STaku Izumi {
7454393e767STaku Izumi 	struct netdev_queue *queue = netdev_get_tx_queue(netdev, 0);
7464393e767STaku Izumi 
7474393e767STaku Izumi 	netif_tx_wake_queue(queue);
7484393e767STaku Izumi }
7494393e767STaku Izumi 
750879bc9a3STaku Izumi static struct rtnl_link_stats64 *
751879bc9a3STaku Izumi fjes_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
752879bc9a3STaku Izumi {
753879bc9a3STaku Izumi 	struct fjes_adapter *adapter = netdev_priv(netdev);
754879bc9a3STaku Izumi 
755879bc9a3STaku Izumi 	memcpy(stats, &adapter->stats64, sizeof(struct rtnl_link_stats64));
756879bc9a3STaku Izumi 
757879bc9a3STaku Izumi 	return stats;
758879bc9a3STaku Izumi }
759879bc9a3STaku Izumi 
760b9e23a67STaku Izumi static int fjes_change_mtu(struct net_device *netdev, int new_mtu)
761b9e23a67STaku Izumi {
762b9e23a67STaku Izumi 	bool running = netif_running(netdev);
763b9e23a67STaku Izumi 	int ret = 0;
764b9e23a67STaku Izumi 	int idx;
765b9e23a67STaku Izumi 
766b9e23a67STaku Izumi 	for (idx = 0; fjes_support_mtu[idx] != 0; idx++) {
767b9e23a67STaku Izumi 		if (new_mtu <= fjes_support_mtu[idx]) {
768b9e23a67STaku Izumi 			new_mtu = fjes_support_mtu[idx];
769b9e23a67STaku Izumi 			if (new_mtu == netdev->mtu)
770b9e23a67STaku Izumi 				return 0;
771b9e23a67STaku Izumi 
772b9e23a67STaku Izumi 			if (running)
773b9e23a67STaku Izumi 				fjes_close(netdev);
774b9e23a67STaku Izumi 
775b9e23a67STaku Izumi 			netdev->mtu = new_mtu;
776b9e23a67STaku Izumi 
777b9e23a67STaku Izumi 			if (running)
778b9e23a67STaku Izumi 				ret = fjes_open(netdev);
779b9e23a67STaku Izumi 
780b9e23a67STaku Izumi 			return ret;
781b9e23a67STaku Izumi 		}
782b9e23a67STaku Izumi 	}
783b9e23a67STaku Izumi 
784b9e23a67STaku Izumi 	return -EINVAL;
785b9e23a67STaku Izumi }
786b9e23a67STaku Izumi 
7873e3feddaSTaku Izumi static int fjes_vlan_rx_add_vid(struct net_device *netdev,
7883e3feddaSTaku Izumi 				__be16 proto, u16 vid)
7893e3feddaSTaku Izumi {
7903e3feddaSTaku Izumi 	struct fjes_adapter *adapter = netdev_priv(netdev);
7913e3feddaSTaku Izumi 	bool ret = true;
7923e3feddaSTaku Izumi 	int epid;
7933e3feddaSTaku Izumi 
7943e3feddaSTaku Izumi 	for (epid = 0; epid < adapter->hw.max_epid; epid++) {
7953e3feddaSTaku Izumi 		if (epid == adapter->hw.my_epid)
7963e3feddaSTaku Izumi 			continue;
7973e3feddaSTaku Izumi 
7983e3feddaSTaku Izumi 		if (!fjes_hw_check_vlan_id(
7993e3feddaSTaku Izumi 			&adapter->hw.ep_shm_info[epid].tx, vid))
8003e3feddaSTaku Izumi 			ret = fjes_hw_set_vlan_id(
8013e3feddaSTaku Izumi 				&adapter->hw.ep_shm_info[epid].tx, vid);
8023e3feddaSTaku Izumi 	}
8033e3feddaSTaku Izumi 
8043e3feddaSTaku Izumi 	return ret ? 0 : -ENOSPC;
8053e3feddaSTaku Izumi }
8063e3feddaSTaku Izumi 
8073e3feddaSTaku Izumi static int fjes_vlan_rx_kill_vid(struct net_device *netdev,
8083e3feddaSTaku Izumi 				 __be16 proto, u16 vid)
8093e3feddaSTaku Izumi {
8103e3feddaSTaku Izumi 	struct fjes_adapter *adapter = netdev_priv(netdev);
8113e3feddaSTaku Izumi 	int epid;
8123e3feddaSTaku Izumi 
8133e3feddaSTaku Izumi 	for (epid = 0; epid < adapter->hw.max_epid; epid++) {
8143e3feddaSTaku Izumi 		if (epid == adapter->hw.my_epid)
8153e3feddaSTaku Izumi 			continue;
8163e3feddaSTaku Izumi 
8173e3feddaSTaku Izumi 		fjes_hw_del_vlan_id(&adapter->hw.ep_shm_info[epid].tx, vid);
8183e3feddaSTaku Izumi 	}
8193e3feddaSTaku Izumi 
8203e3feddaSTaku Izumi 	return 0;
8213e3feddaSTaku Izumi }
8223e3feddaSTaku Izumi 
823cb79eaaeSTaku Izumi static void fjes_txrx_stop_req_irq(struct fjes_adapter *adapter,
824cb79eaaeSTaku Izumi 				   int src_epid)
825cb79eaaeSTaku Izumi {
826cb79eaaeSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
827cb79eaaeSTaku Izumi 	enum ep_partner_status status;
828cb79eaaeSTaku Izumi 
829cb79eaaeSTaku Izumi 	status = fjes_hw_get_partner_ep_status(hw, src_epid);
830cb79eaaeSTaku Izumi 	switch (status) {
831cb79eaaeSTaku Izumi 	case EP_PARTNER_UNSHARE:
832cb79eaaeSTaku Izumi 	case EP_PARTNER_COMPLETE:
833cb79eaaeSTaku Izumi 	default:
834cb79eaaeSTaku Izumi 		break;
835cb79eaaeSTaku Izumi 	case EP_PARTNER_WAITING:
836cb79eaaeSTaku Izumi 		if (src_epid < hw->my_epid) {
837cb79eaaeSTaku Izumi 			hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
838cb79eaaeSTaku Izumi 				FJES_RX_STOP_REQ_DONE;
839cb79eaaeSTaku Izumi 
840cb79eaaeSTaku Izumi 			clear_bit(src_epid, &hw->txrx_stop_req_bit);
841cb79eaaeSTaku Izumi 			set_bit(src_epid, &adapter->unshare_watch_bitmask);
842cb79eaaeSTaku Izumi 
843cb79eaaeSTaku Izumi 			if (!work_pending(&adapter->unshare_watch_task))
844cb79eaaeSTaku Izumi 				queue_work(adapter->control_wq,
845cb79eaaeSTaku Izumi 					   &adapter->unshare_watch_task);
846cb79eaaeSTaku Izumi 		}
847cb79eaaeSTaku Izumi 		break;
848cb79eaaeSTaku Izumi 	case EP_PARTNER_SHARED:
849cb79eaaeSTaku Izumi 		if (hw->ep_shm_info[src_epid].rx.info->v1i.rx_status &
850cb79eaaeSTaku Izumi 		    FJES_RX_STOP_REQ_REQUEST) {
851cb79eaaeSTaku Izumi 			set_bit(src_epid, &hw->epstop_req_bit);
852cb79eaaeSTaku Izumi 			if (!work_pending(&hw->epstop_task))
853cb79eaaeSTaku Izumi 				queue_work(adapter->control_wq,
854cb79eaaeSTaku Izumi 					   &hw->epstop_task);
855cb79eaaeSTaku Izumi 		}
856cb79eaaeSTaku Izumi 		break;
857cb79eaaeSTaku Izumi 	}
858cb79eaaeSTaku Izumi }
859cb79eaaeSTaku Izumi 
860cb79eaaeSTaku Izumi static void fjes_stop_req_irq(struct fjes_adapter *adapter, int src_epid)
861cb79eaaeSTaku Izumi {
862cb79eaaeSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
863cb79eaaeSTaku Izumi 	enum ep_partner_status status;
864cb79eaaeSTaku Izumi 
865cb79eaaeSTaku Izumi 	set_bit(src_epid, &hw->hw_info.buffer_unshare_reserve_bit);
866cb79eaaeSTaku Izumi 
867cb79eaaeSTaku Izumi 	status = fjes_hw_get_partner_ep_status(hw, src_epid);
868cb79eaaeSTaku Izumi 	switch (status) {
869cb79eaaeSTaku Izumi 	case EP_PARTNER_WAITING:
870cb79eaaeSTaku Izumi 		hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
871cb79eaaeSTaku Izumi 				FJES_RX_STOP_REQ_DONE;
872cb79eaaeSTaku Izumi 		clear_bit(src_epid, &hw->txrx_stop_req_bit);
873cb79eaaeSTaku Izumi 		/* fall through */
874cb79eaaeSTaku Izumi 	case EP_PARTNER_UNSHARE:
875cb79eaaeSTaku Izumi 	case EP_PARTNER_COMPLETE:
876cb79eaaeSTaku Izumi 	default:
877cb79eaaeSTaku Izumi 		set_bit(src_epid, &adapter->unshare_watch_bitmask);
878cb79eaaeSTaku Izumi 		if (!work_pending(&adapter->unshare_watch_task))
879cb79eaaeSTaku Izumi 			queue_work(adapter->control_wq,
880cb79eaaeSTaku Izumi 				   &adapter->unshare_watch_task);
881cb79eaaeSTaku Izumi 		break;
882cb79eaaeSTaku Izumi 	case EP_PARTNER_SHARED:
883cb79eaaeSTaku Izumi 		set_bit(src_epid, &hw->epstop_req_bit);
884cb79eaaeSTaku Izumi 
885cb79eaaeSTaku Izumi 		if (!work_pending(&hw->epstop_task))
886cb79eaaeSTaku Izumi 			queue_work(adapter->control_wq, &hw->epstop_task);
887cb79eaaeSTaku Izumi 		break;
888cb79eaaeSTaku Izumi 	}
889cb79eaaeSTaku Izumi }
890cb79eaaeSTaku Izumi 
891785f28e0STaku Izumi static void fjes_update_zone_irq(struct fjes_adapter *adapter,
892785f28e0STaku Izumi 				 int src_epid)
893785f28e0STaku Izumi {
894785f28e0STaku Izumi 	struct fjes_hw *hw = &adapter->hw;
895785f28e0STaku Izumi 
896785f28e0STaku Izumi 	if (!work_pending(&hw->update_zone_task))
897785f28e0STaku Izumi 		queue_work(adapter->control_wq, &hw->update_zone_task);
898785f28e0STaku Izumi }
899785f28e0STaku Izumi 
900e5d486dcSTaku Izumi static irqreturn_t fjes_intr(int irq, void *data)
901e5d486dcSTaku Izumi {
902e5d486dcSTaku Izumi 	struct fjes_adapter *adapter = data;
903e5d486dcSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
904e5d486dcSTaku Izumi 	irqreturn_t ret;
905e5d486dcSTaku Izumi 	u32 icr;
906e5d486dcSTaku Izumi 
907e5d486dcSTaku Izumi 	icr = fjes_hw_capture_interrupt_status(hw);
908e5d486dcSTaku Izumi 
90926585930STaku Izumi 	if (icr & REG_IS_MASK_IS_ASSERT) {
91026585930STaku Izumi 		if (icr & REG_ICTL_MASK_RX_DATA)
91126585930STaku Izumi 			fjes_rx_irq(adapter, icr & REG_IS_MASK_EPID);
91226585930STaku Izumi 
913cb79eaaeSTaku Izumi 		if (icr & REG_ICTL_MASK_DEV_STOP_REQ)
914cb79eaaeSTaku Izumi 			fjes_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
915cb79eaaeSTaku Izumi 
916cb79eaaeSTaku Izumi 		if (icr & REG_ICTL_MASK_TXRX_STOP_REQ)
917cb79eaaeSTaku Izumi 			fjes_txrx_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
918cb79eaaeSTaku Izumi 
919cb79eaaeSTaku Izumi 		if (icr & REG_ICTL_MASK_TXRX_STOP_DONE)
920cb79eaaeSTaku Izumi 			fjes_hw_set_irqmask(hw,
921cb79eaaeSTaku Izumi 					    REG_ICTL_MASK_TXRX_STOP_DONE, true);
922cb79eaaeSTaku Izumi 
923785f28e0STaku Izumi 		if (icr & REG_ICTL_MASK_INFO_UPDATE)
924785f28e0STaku Izumi 			fjes_update_zone_irq(adapter, icr & REG_IS_MASK_EPID);
925785f28e0STaku Izumi 
926e5d486dcSTaku Izumi 		ret = IRQ_HANDLED;
92726585930STaku Izumi 	} else {
928e5d486dcSTaku Izumi 		ret = IRQ_NONE;
92926585930STaku Izumi 	}
930e5d486dcSTaku Izumi 
931e5d486dcSTaku Izumi 	return ret;
932e5d486dcSTaku Izumi }
933e5d486dcSTaku Izumi 
93426585930STaku Izumi static int fjes_rxframe_search_exist(struct fjes_adapter *adapter,
93526585930STaku Izumi 				     int start_epid)
93626585930STaku Izumi {
93726585930STaku Izumi 	struct fjes_hw *hw = &adapter->hw;
93826585930STaku Izumi 	enum ep_partner_status pstatus;
93926585930STaku Izumi 	int max_epid, cur_epid;
94026585930STaku Izumi 	int i;
94126585930STaku Izumi 
94226585930STaku Izumi 	max_epid = hw->max_epid;
94326585930STaku Izumi 	start_epid = (start_epid + 1 + max_epid) % max_epid;
94426585930STaku Izumi 
94526585930STaku Izumi 	for (i = 0; i < max_epid; i++) {
94626585930STaku Izumi 		cur_epid = (start_epid + i) % max_epid;
94726585930STaku Izumi 		if (cur_epid == hw->my_epid)
94826585930STaku Izumi 			continue;
94926585930STaku Izumi 
95026585930STaku Izumi 		pstatus = fjes_hw_get_partner_ep_status(hw, cur_epid);
95126585930STaku Izumi 		if (pstatus == EP_PARTNER_SHARED) {
95226585930STaku Izumi 			if (!fjes_hw_epbuf_rx_is_empty(
95326585930STaku Izumi 				&hw->ep_shm_info[cur_epid].rx))
95426585930STaku Izumi 				return cur_epid;
95526585930STaku Izumi 		}
95626585930STaku Izumi 	}
95726585930STaku Izumi 	return -1;
95826585930STaku Izumi }
95926585930STaku Izumi 
96026585930STaku Izumi static void *fjes_rxframe_get(struct fjes_adapter *adapter, size_t *psize,
96126585930STaku Izumi 			      int *cur_epid)
96226585930STaku Izumi {
96326585930STaku Izumi 	void *frame;
96426585930STaku Izumi 
96526585930STaku Izumi 	*cur_epid = fjes_rxframe_search_exist(adapter, *cur_epid);
96626585930STaku Izumi 	if (*cur_epid < 0)
96726585930STaku Izumi 		return NULL;
96826585930STaku Izumi 
96926585930STaku Izumi 	frame =
97026585930STaku Izumi 	fjes_hw_epbuf_rx_curpkt_get_addr(
97126585930STaku Izumi 		&adapter->hw.ep_shm_info[*cur_epid].rx, psize);
97226585930STaku Izumi 
97326585930STaku Izumi 	return frame;
97426585930STaku Izumi }
97526585930STaku Izumi 
97626585930STaku Izumi static void fjes_rxframe_release(struct fjes_adapter *adapter, int cur_epid)
97726585930STaku Izumi {
97826585930STaku Izumi 	fjes_hw_epbuf_rx_curpkt_drop(&adapter->hw.ep_shm_info[cur_epid].rx);
97926585930STaku Izumi }
98026585930STaku Izumi 
98126585930STaku Izumi static void fjes_rx_irq(struct fjes_adapter *adapter, int src_epid)
98226585930STaku Izumi {
98326585930STaku Izumi 	struct fjes_hw *hw = &adapter->hw;
98426585930STaku Izumi 
98526585930STaku Izumi 	fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, true);
98626585930STaku Izumi 
98726585930STaku Izumi 	adapter->unset_rx_last = true;
98826585930STaku Izumi 	napi_schedule(&adapter->napi);
98926585930STaku Izumi }
99026585930STaku Izumi 
99126585930STaku Izumi static int fjes_poll(struct napi_struct *napi, int budget)
99226585930STaku Izumi {
99326585930STaku Izumi 	struct fjes_adapter *adapter =
99426585930STaku Izumi 			container_of(napi, struct fjes_adapter, napi);
99526585930STaku Izumi 	struct net_device *netdev = napi->dev;
99626585930STaku Izumi 	struct fjes_hw *hw = &adapter->hw;
99726585930STaku Izumi 	struct sk_buff *skb;
99826585930STaku Izumi 	int work_done = 0;
99926585930STaku Izumi 	int cur_epid = 0;
100026585930STaku Izumi 	int epidx;
100126585930STaku Izumi 	size_t frame_len;
100226585930STaku Izumi 	void *frame;
100326585930STaku Izumi 
100426585930STaku Izumi 	for (epidx = 0; epidx < hw->max_epid; epidx++) {
100526585930STaku Izumi 		if (epidx == hw->my_epid)
100626585930STaku Izumi 			continue;
100726585930STaku Izumi 
100826585930STaku Izumi 		adapter->hw.ep_shm_info[epidx].tx.info->v1i.rx_status |=
100926585930STaku Izumi 			FJES_RX_POLL_WORK;
101026585930STaku Izumi 	}
101126585930STaku Izumi 
101226585930STaku Izumi 	while (work_done < budget) {
101326585930STaku Izumi 		prefetch(&adapter->hw);
101426585930STaku Izumi 		frame = fjes_rxframe_get(adapter, &frame_len, &cur_epid);
101526585930STaku Izumi 
101626585930STaku Izumi 		if (frame) {
101726585930STaku Izumi 			skb = napi_alloc_skb(napi, frame_len);
101826585930STaku Izumi 			if (!skb) {
101926585930STaku Izumi 				adapter->stats64.rx_dropped += 1;
102026585930STaku Izumi 				hw->ep_shm_info[cur_epid].net_stats
102126585930STaku Izumi 							 .rx_dropped += 1;
102226585930STaku Izumi 				adapter->stats64.rx_errors += 1;
102326585930STaku Izumi 				hw->ep_shm_info[cur_epid].net_stats
102426585930STaku Izumi 							 .rx_errors += 1;
102526585930STaku Izumi 			} else {
102626585930STaku Izumi 				memcpy(skb_put(skb, frame_len),
102726585930STaku Izumi 				       frame, frame_len);
102826585930STaku Izumi 				skb->protocol = eth_type_trans(skb, netdev);
102926585930STaku Izumi 				skb->ip_summed = CHECKSUM_UNNECESSARY;
103026585930STaku Izumi 
103126585930STaku Izumi 				netif_receive_skb(skb);
103226585930STaku Izumi 
103326585930STaku Izumi 				work_done++;
103426585930STaku Izumi 
103526585930STaku Izumi 				adapter->stats64.rx_packets += 1;
103626585930STaku Izumi 				hw->ep_shm_info[cur_epid].net_stats
103726585930STaku Izumi 							 .rx_packets += 1;
103826585930STaku Izumi 				adapter->stats64.rx_bytes += frame_len;
103926585930STaku Izumi 				hw->ep_shm_info[cur_epid].net_stats
104026585930STaku Izumi 							 .rx_bytes += frame_len;
104126585930STaku Izumi 
104226585930STaku Izumi 				if (is_multicast_ether_addr(
104326585930STaku Izumi 					((struct ethhdr *)frame)->h_dest)) {
104426585930STaku Izumi 					adapter->stats64.multicast += 1;
104526585930STaku Izumi 					hw->ep_shm_info[cur_epid].net_stats
104626585930STaku Izumi 								 .multicast += 1;
104726585930STaku Izumi 				}
104826585930STaku Izumi 			}
104926585930STaku Izumi 
105026585930STaku Izumi 			fjes_rxframe_release(adapter, cur_epid);
105126585930STaku Izumi 			adapter->unset_rx_last = true;
105226585930STaku Izumi 		} else {
105326585930STaku Izumi 			break;
105426585930STaku Izumi 		}
105526585930STaku Izumi 	}
105626585930STaku Izumi 
105726585930STaku Izumi 	if (work_done < budget) {
105826585930STaku Izumi 		napi_complete(napi);
105926585930STaku Izumi 
106026585930STaku Izumi 		if (adapter->unset_rx_last) {
106126585930STaku Izumi 			adapter->rx_last_jiffies = jiffies;
106226585930STaku Izumi 			adapter->unset_rx_last = false;
106326585930STaku Izumi 		}
106426585930STaku Izumi 
106526585930STaku Izumi 		if (((long)jiffies - (long)adapter->rx_last_jiffies) < 3) {
106626585930STaku Izumi 			napi_reschedule(napi);
106726585930STaku Izumi 		} else {
106826585930STaku Izumi 			for (epidx = 0; epidx < hw->max_epid; epidx++) {
106926585930STaku Izumi 				if (epidx == hw->my_epid)
107026585930STaku Izumi 					continue;
107126585930STaku Izumi 				adapter->hw.ep_shm_info[epidx]
107226585930STaku Izumi 					   .tx.info->v1i.rx_status &=
107326585930STaku Izumi 						~FJES_RX_POLL_WORK;
107426585930STaku Izumi 			}
107526585930STaku Izumi 
107626585930STaku Izumi 			fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, false);
107726585930STaku Izumi 		}
107826585930STaku Izumi 	}
107926585930STaku Izumi 
108026585930STaku Izumi 	return work_done;
108126585930STaku Izumi }
108226585930STaku Izumi 
1083658d439bSTaku Izumi /* fjes_probe - Device Initialization Routine */
1084658d439bSTaku Izumi static int fjes_probe(struct platform_device *plat_dev)
1085658d439bSTaku Izumi {
10862fcbca68STaku Izumi 	struct fjes_adapter *adapter;
10872fcbca68STaku Izumi 	struct net_device *netdev;
10882fcbca68STaku Izumi 	struct resource *res;
10892fcbca68STaku Izumi 	struct fjes_hw *hw;
10902fcbca68STaku Izumi 	int err;
10912fcbca68STaku Izumi 
10922fcbca68STaku Izumi 	err = -ENOMEM;
10932fcbca68STaku Izumi 	netdev = alloc_netdev_mq(sizeof(struct fjes_adapter), "es%d",
10942fcbca68STaku Izumi 				 NET_NAME_UNKNOWN, fjes_netdev_setup,
10952fcbca68STaku Izumi 				 FJES_MAX_QUEUES);
10962fcbca68STaku Izumi 
10972fcbca68STaku Izumi 	if (!netdev)
10982fcbca68STaku Izumi 		goto err_out;
10992fcbca68STaku Izumi 
11002fcbca68STaku Izumi 	SET_NETDEV_DEV(netdev, &plat_dev->dev);
11012fcbca68STaku Izumi 
11022fcbca68STaku Izumi 	dev_set_drvdata(&plat_dev->dev, netdev);
11032fcbca68STaku Izumi 	adapter = netdev_priv(netdev);
11042fcbca68STaku Izumi 	adapter->netdev = netdev;
11052fcbca68STaku Izumi 	adapter->plat_dev = plat_dev;
11062fcbca68STaku Izumi 	hw = &adapter->hw;
11072fcbca68STaku Izumi 	hw->back = adapter;
11082fcbca68STaku Izumi 
11092fcbca68STaku Izumi 	/* setup the private structure */
11102fcbca68STaku Izumi 	err = fjes_sw_init(adapter);
11112fcbca68STaku Izumi 	if (err)
11122fcbca68STaku Izumi 		goto err_free_netdev;
11132fcbca68STaku Izumi 
1114ff5b4210STaku Izumi 	INIT_WORK(&adapter->force_close_task, fjes_force_close_task);
11152fcbca68STaku Izumi 	adapter->force_reset = false;
11162fcbca68STaku Izumi 	adapter->open_guard = false;
11172fcbca68STaku Izumi 
1118b772b9dcSTaku Izumi 	adapter->txrx_wq = create_workqueue(DRV_NAME "/txrx");
11198edb62a8STaku Izumi 	adapter->control_wq = create_workqueue(DRV_NAME "/control");
1120b772b9dcSTaku Izumi 
1121ac63b947STaku Izumi 	INIT_WORK(&adapter->tx_stall_task, fjes_tx_stall_task);
1122b772b9dcSTaku Izumi 	INIT_WORK(&adapter->raise_intr_rxdata_task,
1123b772b9dcSTaku Izumi 		  fjes_raise_intr_rxdata_task);
11248fc4cadbSTaku Izumi 	INIT_WORK(&adapter->unshare_watch_task, fjes_watch_unshare_task);
11258fc4cadbSTaku Izumi 	adapter->unshare_watch_bitmask = 0;
1126b772b9dcSTaku Izumi 
11278edb62a8STaku Izumi 	INIT_DELAYED_WORK(&adapter->interrupt_watch_task, fjes_irq_watch_task);
11288edb62a8STaku Izumi 	adapter->interrupt_watch_enable = false;
11298edb62a8STaku Izumi 
11302fcbca68STaku Izumi 	res = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
11312fcbca68STaku Izumi 	hw->hw_res.start = res->start;
1132e0897ae3SVaishali Thakkar 	hw->hw_res.size = resource_size(res);
11332fcbca68STaku Izumi 	hw->hw_res.irq = platform_get_irq(plat_dev, 0);
11342fcbca68STaku Izumi 	err = fjes_hw_init(&adapter->hw);
11352fcbca68STaku Izumi 	if (err)
11362fcbca68STaku Izumi 		goto err_free_netdev;
11372fcbca68STaku Izumi 
11382fcbca68STaku Izumi 	/* setup MAC address (02:00:00:00:00:[epid])*/
11392fcbca68STaku Izumi 	netdev->dev_addr[0] = 2;
11402fcbca68STaku Izumi 	netdev->dev_addr[1] = 0;
11412fcbca68STaku Izumi 	netdev->dev_addr[2] = 0;
11422fcbca68STaku Izumi 	netdev->dev_addr[3] = 0;
11432fcbca68STaku Izumi 	netdev->dev_addr[4] = 0;
11442fcbca68STaku Izumi 	netdev->dev_addr[5] = hw->my_epid; /* EPID */
11452fcbca68STaku Izumi 
11462fcbca68STaku Izumi 	err = register_netdev(netdev);
11472fcbca68STaku Izumi 	if (err)
11482fcbca68STaku Izumi 		goto err_hw_exit;
11492fcbca68STaku Izumi 
11502fcbca68STaku Izumi 	netif_carrier_off(netdev);
11512fcbca68STaku Izumi 
1152658d439bSTaku Izumi 	return 0;
11532fcbca68STaku Izumi 
11542fcbca68STaku Izumi err_hw_exit:
11552fcbca68STaku Izumi 	fjes_hw_exit(&adapter->hw);
11562fcbca68STaku Izumi err_free_netdev:
11572fcbca68STaku Izumi 	free_netdev(netdev);
11582fcbca68STaku Izumi err_out:
11592fcbca68STaku Izumi 	return err;
1160658d439bSTaku Izumi }
1161658d439bSTaku Izumi 
1162658d439bSTaku Izumi /* fjes_remove - Device Removal Routine */
1163658d439bSTaku Izumi static int fjes_remove(struct platform_device *plat_dev)
1164658d439bSTaku Izumi {
11652fcbca68STaku Izumi 	struct net_device *netdev = dev_get_drvdata(&plat_dev->dev);
11662fcbca68STaku Izumi 	struct fjes_adapter *adapter = netdev_priv(netdev);
11672fcbca68STaku Izumi 	struct fjes_hw *hw = &adapter->hw;
11682fcbca68STaku Izumi 
11698edb62a8STaku Izumi 	cancel_delayed_work_sync(&adapter->interrupt_watch_task);
11708fc4cadbSTaku Izumi 	cancel_work_sync(&adapter->unshare_watch_task);
1171b772b9dcSTaku Izumi 	cancel_work_sync(&adapter->raise_intr_rxdata_task);
1172ac63b947STaku Izumi 	cancel_work_sync(&adapter->tx_stall_task);
11738edb62a8STaku Izumi 	if (adapter->control_wq)
11748edb62a8STaku Izumi 		destroy_workqueue(adapter->control_wq);
1175b772b9dcSTaku Izumi 	if (adapter->txrx_wq)
1176b772b9dcSTaku Izumi 		destroy_workqueue(adapter->txrx_wq);
1177b772b9dcSTaku Izumi 
11782fcbca68STaku Izumi 	unregister_netdev(netdev);
11792fcbca68STaku Izumi 
11802fcbca68STaku Izumi 	fjes_hw_exit(hw);
11812fcbca68STaku Izumi 
118226585930STaku Izumi 	netif_napi_del(&adapter->napi);
118326585930STaku Izumi 
11842fcbca68STaku Izumi 	free_netdev(netdev);
11852fcbca68STaku Izumi 
1186658d439bSTaku Izumi 	return 0;
1187658d439bSTaku Izumi }
1188658d439bSTaku Izumi 
11892fcbca68STaku Izumi static int fjes_sw_init(struct fjes_adapter *adapter)
11902fcbca68STaku Izumi {
119126585930STaku Izumi 	struct net_device *netdev = adapter->netdev;
119226585930STaku Izumi 
119326585930STaku Izumi 	netif_napi_add(netdev, &adapter->napi, fjes_poll, 64);
119426585930STaku Izumi 
11952fcbca68STaku Izumi 	return 0;
11962fcbca68STaku Izumi }
11972fcbca68STaku Izumi 
11982fcbca68STaku Izumi /* fjes_netdev_setup - netdevice initialization routine */
11992fcbca68STaku Izumi static void fjes_netdev_setup(struct net_device *netdev)
12002fcbca68STaku Izumi {
12012fcbca68STaku Izumi 	ether_setup(netdev);
12022fcbca68STaku Izumi 
12032fcbca68STaku Izumi 	netdev->watchdog_timeo = FJES_TX_RETRY_INTERVAL;
12042fcbca68STaku Izumi 	netdev->netdev_ops = &fjes_netdev_ops;
1205786eec27STaku Izumi 	fjes_set_ethtool_ops(netdev);
12062fcbca68STaku Izumi 	netdev->mtu = fjes_support_mtu[0];
12072fcbca68STaku Izumi 	netdev->flags |= IFF_BROADCAST;
12082fcbca68STaku Izumi 	netdev->features |= NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_CTAG_FILTER;
12092fcbca68STaku Izumi }
12102fcbca68STaku Izumi 
12118edb62a8STaku Izumi static void fjes_irq_watch_task(struct work_struct *work)
12128edb62a8STaku Izumi {
12138edb62a8STaku Izumi 	struct fjes_adapter *adapter = container_of(to_delayed_work(work),
12148edb62a8STaku Izumi 			struct fjes_adapter, interrupt_watch_task);
12158edb62a8STaku Izumi 
12168edb62a8STaku Izumi 	local_irq_disable();
12178edb62a8STaku Izumi 	fjes_intr(adapter->hw.hw_res.irq, adapter);
12188edb62a8STaku Izumi 	local_irq_enable();
12198edb62a8STaku Izumi 
12208edb62a8STaku Izumi 	if (fjes_rxframe_search_exist(adapter, 0) >= 0)
12218edb62a8STaku Izumi 		napi_schedule(&adapter->napi);
12228edb62a8STaku Izumi 
12238edb62a8STaku Izumi 	if (adapter->interrupt_watch_enable) {
12248edb62a8STaku Izumi 		if (!delayed_work_pending(&adapter->interrupt_watch_task))
12258edb62a8STaku Izumi 			queue_delayed_work(adapter->control_wq,
12268edb62a8STaku Izumi 					   &adapter->interrupt_watch_task,
12278edb62a8STaku Izumi 					   FJES_IRQ_WATCH_DELAY);
12288edb62a8STaku Izumi 	}
12298edb62a8STaku Izumi }
12308edb62a8STaku Izumi 
12318fc4cadbSTaku Izumi static void fjes_watch_unshare_task(struct work_struct *work)
12328fc4cadbSTaku Izumi {
12338fc4cadbSTaku Izumi 	struct fjes_adapter *adapter =
12348fc4cadbSTaku Izumi 	container_of(work, struct fjes_adapter, unshare_watch_task);
12358fc4cadbSTaku Izumi 
12368fc4cadbSTaku Izumi 	struct net_device *netdev = adapter->netdev;
12378fc4cadbSTaku Izumi 	struct fjes_hw *hw = &adapter->hw;
12388fc4cadbSTaku Izumi 
12398fc4cadbSTaku Izumi 	int unshare_watch, unshare_reserve;
12408fc4cadbSTaku Izumi 	int max_epid, my_epid, epidx;
12418fc4cadbSTaku Izumi 	int stop_req, stop_req_done;
12428fc4cadbSTaku Izumi 	ulong unshare_watch_bitmask;
12438fc4cadbSTaku Izumi 	int wait_time = 0;
12448fc4cadbSTaku Izumi 	int is_shared;
12458fc4cadbSTaku Izumi 	int ret;
12468fc4cadbSTaku Izumi 
12478fc4cadbSTaku Izumi 	my_epid = hw->my_epid;
12488fc4cadbSTaku Izumi 	max_epid = hw->max_epid;
12498fc4cadbSTaku Izumi 
12508fc4cadbSTaku Izumi 	unshare_watch_bitmask = adapter->unshare_watch_bitmask;
12518fc4cadbSTaku Izumi 	adapter->unshare_watch_bitmask = 0;
12528fc4cadbSTaku Izumi 
12538fc4cadbSTaku Izumi 	while ((unshare_watch_bitmask || hw->txrx_stop_req_bit) &&
12548fc4cadbSTaku Izumi 	       (wait_time < 3000)) {
12558fc4cadbSTaku Izumi 		for (epidx = 0; epidx < hw->max_epid; epidx++) {
12568fc4cadbSTaku Izumi 			if (epidx == hw->my_epid)
12578fc4cadbSTaku Izumi 				continue;
12588fc4cadbSTaku Izumi 
12598fc4cadbSTaku Izumi 			is_shared = fjes_hw_epid_is_shared(hw->hw_info.share,
12608fc4cadbSTaku Izumi 							   epidx);
12618fc4cadbSTaku Izumi 
12628fc4cadbSTaku Izumi 			stop_req = test_bit(epidx, &hw->txrx_stop_req_bit);
12638fc4cadbSTaku Izumi 
12648fc4cadbSTaku Izumi 			stop_req_done = hw->ep_shm_info[epidx].rx.info->v1i.rx_status &
12658fc4cadbSTaku Izumi 					FJES_RX_STOP_REQ_DONE;
12668fc4cadbSTaku Izumi 
12678fc4cadbSTaku Izumi 			unshare_watch = test_bit(epidx, &unshare_watch_bitmask);
12688fc4cadbSTaku Izumi 
12698fc4cadbSTaku Izumi 			unshare_reserve = test_bit(epidx,
12708fc4cadbSTaku Izumi 						   &hw->hw_info.buffer_unshare_reserve_bit);
12718fc4cadbSTaku Izumi 
12728fc4cadbSTaku Izumi 			if ((!stop_req ||
12738fc4cadbSTaku Izumi 			     (is_shared && (!is_shared || !stop_req_done))) &&
12748fc4cadbSTaku Izumi 			    (is_shared || !unshare_watch || !unshare_reserve))
12758fc4cadbSTaku Izumi 				continue;
12768fc4cadbSTaku Izumi 
12778fc4cadbSTaku Izumi 			mutex_lock(&hw->hw_info.lock);
12788fc4cadbSTaku Izumi 			ret = fjes_hw_unregister_buff_addr(hw, epidx);
12798fc4cadbSTaku Izumi 			switch (ret) {
12808fc4cadbSTaku Izumi 			case 0:
12818fc4cadbSTaku Izumi 				break;
12828fc4cadbSTaku Izumi 			case -ENOMSG:
12838fc4cadbSTaku Izumi 			case -EBUSY:
12848fc4cadbSTaku Izumi 			default:
12858fc4cadbSTaku Izumi 				if (!work_pending(
12868fc4cadbSTaku Izumi 					&adapter->force_close_task)) {
12878fc4cadbSTaku Izumi 					adapter->force_reset = true;
12888fc4cadbSTaku Izumi 					schedule_work(
12898fc4cadbSTaku Izumi 						&adapter->force_close_task);
12908fc4cadbSTaku Izumi 				}
12918fc4cadbSTaku Izumi 				break;
12928fc4cadbSTaku Izumi 			}
12938fc4cadbSTaku Izumi 			mutex_unlock(&hw->hw_info.lock);
12948fc4cadbSTaku Izumi 
12958fc4cadbSTaku Izumi 			fjes_hw_setup_epbuf(&hw->ep_shm_info[epidx].tx,
12968fc4cadbSTaku Izumi 					    netdev->dev_addr, netdev->mtu);
12978fc4cadbSTaku Izumi 
12988fc4cadbSTaku Izumi 			clear_bit(epidx, &hw->txrx_stop_req_bit);
12998fc4cadbSTaku Izumi 			clear_bit(epidx, &unshare_watch_bitmask);
13008fc4cadbSTaku Izumi 			clear_bit(epidx,
13018fc4cadbSTaku Izumi 				  &hw->hw_info.buffer_unshare_reserve_bit);
13028fc4cadbSTaku Izumi 		}
13038fc4cadbSTaku Izumi 
13048fc4cadbSTaku Izumi 		msleep(100);
13058fc4cadbSTaku Izumi 		wait_time += 100;
13068fc4cadbSTaku Izumi 	}
13078fc4cadbSTaku Izumi 
13088fc4cadbSTaku Izumi 	if (hw->hw_info.buffer_unshare_reserve_bit) {
13098fc4cadbSTaku Izumi 		for (epidx = 0; epidx < hw->max_epid; epidx++) {
13108fc4cadbSTaku Izumi 			if (epidx == hw->my_epid)
13118fc4cadbSTaku Izumi 				continue;
13128fc4cadbSTaku Izumi 
13138fc4cadbSTaku Izumi 			if (test_bit(epidx,
13148fc4cadbSTaku Izumi 				     &hw->hw_info.buffer_unshare_reserve_bit)) {
13158fc4cadbSTaku Izumi 				mutex_lock(&hw->hw_info.lock);
13168fc4cadbSTaku Izumi 
13178fc4cadbSTaku Izumi 				ret = fjes_hw_unregister_buff_addr(hw, epidx);
13188fc4cadbSTaku Izumi 				switch (ret) {
13198fc4cadbSTaku Izumi 				case 0:
13208fc4cadbSTaku Izumi 					break;
13218fc4cadbSTaku Izumi 				case -ENOMSG:
13228fc4cadbSTaku Izumi 				case -EBUSY:
13238fc4cadbSTaku Izumi 				default:
13248fc4cadbSTaku Izumi 					if (!work_pending(
13258fc4cadbSTaku Izumi 						&adapter->force_close_task)) {
13268fc4cadbSTaku Izumi 						adapter->force_reset = true;
13278fc4cadbSTaku Izumi 						schedule_work(
13288fc4cadbSTaku Izumi 							&adapter->force_close_task);
13298fc4cadbSTaku Izumi 					}
13308fc4cadbSTaku Izumi 					break;
13318fc4cadbSTaku Izumi 				}
13328fc4cadbSTaku Izumi 				mutex_unlock(&hw->hw_info.lock);
13338fc4cadbSTaku Izumi 
13348fc4cadbSTaku Izumi 				fjes_hw_setup_epbuf(
13358fc4cadbSTaku Izumi 					&hw->ep_shm_info[epidx].tx,
13368fc4cadbSTaku Izumi 					netdev->dev_addr, netdev->mtu);
13378fc4cadbSTaku Izumi 
13388fc4cadbSTaku Izumi 				clear_bit(epidx, &hw->txrx_stop_req_bit);
13398fc4cadbSTaku Izumi 				clear_bit(epidx, &unshare_watch_bitmask);
13408fc4cadbSTaku Izumi 				clear_bit(epidx, &hw->hw_info.buffer_unshare_reserve_bit);
13418fc4cadbSTaku Izumi 			}
13428fc4cadbSTaku Izumi 
13438fc4cadbSTaku Izumi 			if (test_bit(epidx, &unshare_watch_bitmask)) {
13448fc4cadbSTaku Izumi 				hw->ep_shm_info[epidx].tx.info->v1i.rx_status &=
13458fc4cadbSTaku Izumi 						~FJES_RX_STOP_REQ_DONE;
13468fc4cadbSTaku Izumi 			}
13478fc4cadbSTaku Izumi 		}
13488fc4cadbSTaku Izumi 	}
13498fc4cadbSTaku Izumi }
13508fc4cadbSTaku Izumi 
1351658d439bSTaku Izumi /* fjes_init_module - Driver Registration Routine */
1352658d439bSTaku Izumi static int __init fjes_init_module(void)
1353658d439bSTaku Izumi {
1354658d439bSTaku Izumi 	int result;
1355658d439bSTaku Izumi 
1356658d439bSTaku Izumi 	pr_info("%s - version %s - %s\n",
1357658d439bSTaku Izumi 		fjes_driver_string, fjes_driver_version, fjes_copyright);
1358658d439bSTaku Izumi 
1359658d439bSTaku Izumi 	result = platform_driver_register(&fjes_driver);
1360658d439bSTaku Izumi 	if (result < 0)
1361658d439bSTaku Izumi 		return result;
1362658d439bSTaku Izumi 
1363658d439bSTaku Izumi 	result = acpi_bus_register_driver(&fjes_acpi_driver);
1364658d439bSTaku Izumi 	if (result < 0)
1365658d439bSTaku Izumi 		goto fail_acpi_driver;
1366658d439bSTaku Izumi 
1367658d439bSTaku Izumi 	return 0;
1368658d439bSTaku Izumi 
1369658d439bSTaku Izumi fail_acpi_driver:
1370658d439bSTaku Izumi 	platform_driver_unregister(&fjes_driver);
1371658d439bSTaku Izumi 	return result;
1372658d439bSTaku Izumi }
1373658d439bSTaku Izumi 
1374658d439bSTaku Izumi module_init(fjes_init_module);
1375658d439bSTaku Izumi 
1376658d439bSTaku Izumi /* fjes_exit_module - Driver Exit Cleanup Routine */
1377658d439bSTaku Izumi static void __exit fjes_exit_module(void)
1378658d439bSTaku Izumi {
1379658d439bSTaku Izumi 	acpi_bus_unregister_driver(&fjes_acpi_driver);
1380658d439bSTaku Izumi 	platform_driver_unregister(&fjes_driver);
1381658d439bSTaku Izumi }
1382658d439bSTaku Izumi 
1383658d439bSTaku Izumi module_exit(fjes_exit_module);
1384