xref: /linux/drivers/net/fjes/fjes_main.c (revision d87c25e8f4051f813762da6a182c57f246b17441)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  FUJITSU Extended Socket Network Device driver
4  *  Copyright (c) 2015 FUJITSU LIMITED
5  */
6 
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/nls.h>
10 #include <linux/platform_device.h>
11 #include <linux/netdevice.h>
12 #include <linux/interrupt.h>
13 
14 #include "fjes.h"
15 #include "fjes_trace.h"
16 
17 #define MAJ 1
18 #define MIN 2
19 #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN)
20 #define DRV_NAME	"fjes"
21 char fjes_driver_name[] = DRV_NAME;
22 char fjes_driver_version[] = DRV_VERSION;
23 static const char fjes_driver_string[] =
24 		"FUJITSU Extended Socket Network Device Driver";
25 static const char fjes_copyright[] =
26 		"Copyright (c) 2015 FUJITSU LIMITED";
27 
28 MODULE_AUTHOR("Taku Izumi <izumi.taku@jp.fujitsu.com>");
29 MODULE_DESCRIPTION("FUJITSU Extended Socket Network Device Driver");
30 MODULE_LICENSE("GPL");
31 MODULE_VERSION(DRV_VERSION);
32 
33 #define ACPI_MOTHERBOARD_RESOURCE_HID "PNP0C02"
34 
35 static int fjes_request_irq(struct fjes_adapter *);
36 static void fjes_free_irq(struct fjes_adapter *);
37 
38 static int fjes_open(struct net_device *);
39 static int fjes_close(struct net_device *);
40 static int fjes_setup_resources(struct fjes_adapter *);
41 static void fjes_free_resources(struct fjes_adapter *);
42 static netdev_tx_t fjes_xmit_frame(struct sk_buff *, struct net_device *);
43 static void fjes_raise_intr_rxdata_task(struct work_struct *);
44 static void fjes_tx_stall_task(struct work_struct *);
45 static void fjes_force_close_task(struct work_struct *);
46 static irqreturn_t fjes_intr(int, void*);
47 static void fjes_get_stats64(struct net_device *, struct rtnl_link_stats64 *);
48 static int fjes_change_mtu(struct net_device *, int);
49 static int fjes_vlan_rx_add_vid(struct net_device *, __be16 proto, u16);
50 static int fjes_vlan_rx_kill_vid(struct net_device *, __be16 proto, u16);
51 static void fjes_tx_retry(struct net_device *, unsigned int txqueue);
52 
53 static int fjes_acpi_add(struct acpi_device *);
54 static int fjes_acpi_remove(struct acpi_device *);
55 static acpi_status fjes_get_acpi_resource(struct acpi_resource *, void*);
56 
57 static int fjes_probe(struct platform_device *);
58 static int fjes_remove(struct platform_device *);
59 
60 static int fjes_sw_init(struct fjes_adapter *);
61 static void fjes_netdev_setup(struct net_device *);
62 static void fjes_irq_watch_task(struct work_struct *);
63 static void fjes_watch_unshare_task(struct work_struct *);
64 static void fjes_rx_irq(struct fjes_adapter *, int);
65 static int fjes_poll(struct napi_struct *, int);
66 
67 static const struct acpi_device_id fjes_acpi_ids[] = {
68 	{ACPI_MOTHERBOARD_RESOURCE_HID, 0},
69 	{"", 0},
70 };
71 MODULE_DEVICE_TABLE(acpi, fjes_acpi_ids);
72 
73 static struct acpi_driver fjes_acpi_driver = {
74 	.name = DRV_NAME,
75 	.class = DRV_NAME,
76 	.owner = THIS_MODULE,
77 	.ids = fjes_acpi_ids,
78 	.ops = {
79 		.add = fjes_acpi_add,
80 		.remove = fjes_acpi_remove,
81 	},
82 };
83 
84 static struct platform_driver fjes_driver = {
85 	.driver = {
86 		.name = DRV_NAME,
87 	},
88 	.probe = fjes_probe,
89 	.remove = fjes_remove,
90 };
91 
92 static struct resource fjes_resource[] = {
93 	DEFINE_RES_MEM(0, 1),
94 	DEFINE_RES_IRQ(0)
95 };
96 
97 static bool is_extended_socket_device(struct acpi_device *device)
98 {
99 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
100 	char str_buf[sizeof(FJES_ACPI_SYMBOL) + 1];
101 	union acpi_object *str;
102 	acpi_status status;
103 	int result;
104 
105 	status = acpi_evaluate_object(device->handle, "_STR", NULL, &buffer);
106 	if (ACPI_FAILURE(status))
107 		return false;
108 
109 	str = buffer.pointer;
110 	result = utf16s_to_utf8s((wchar_t *)str->string.pointer,
111 				 str->string.length, UTF16_LITTLE_ENDIAN,
112 				 str_buf, sizeof(str_buf) - 1);
113 	str_buf[result] = 0;
114 
115 	if (strncmp(FJES_ACPI_SYMBOL, str_buf, strlen(FJES_ACPI_SYMBOL)) != 0) {
116 		kfree(buffer.pointer);
117 		return false;
118 	}
119 	kfree(buffer.pointer);
120 
121 	return true;
122 }
123 
124 static int acpi_check_extended_socket_status(struct acpi_device *device)
125 {
126 	unsigned long long sta;
127 	acpi_status status;
128 
129 	status = acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
130 	if (ACPI_FAILURE(status))
131 		return -ENODEV;
132 
133 	if (!((sta & ACPI_STA_DEVICE_PRESENT) &&
134 	      (sta & ACPI_STA_DEVICE_ENABLED) &&
135 	      (sta & ACPI_STA_DEVICE_UI) &&
136 	      (sta & ACPI_STA_DEVICE_FUNCTIONING)))
137 		return -ENODEV;
138 
139 	return 0;
140 }
141 
142 static int fjes_acpi_add(struct acpi_device *device)
143 {
144 	struct platform_device *plat_dev;
145 	acpi_status status;
146 
147 	if (!is_extended_socket_device(device))
148 		return -ENODEV;
149 
150 	if (acpi_check_extended_socket_status(device))
151 		return -ENODEV;
152 
153 	status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
154 				     fjes_get_acpi_resource, fjes_resource);
155 	if (ACPI_FAILURE(status))
156 		return -ENODEV;
157 
158 	/* create platform_device */
159 	plat_dev = platform_device_register_simple(DRV_NAME, 0, fjes_resource,
160 						   ARRAY_SIZE(fjes_resource));
161 	if (IS_ERR(plat_dev))
162 		return PTR_ERR(plat_dev);
163 
164 	device->driver_data = plat_dev;
165 
166 	return 0;
167 }
168 
169 static int fjes_acpi_remove(struct acpi_device *device)
170 {
171 	struct platform_device *plat_dev;
172 
173 	plat_dev = (struct platform_device *)acpi_driver_data(device);
174 	platform_device_unregister(plat_dev);
175 
176 	return 0;
177 }
178 
179 static acpi_status
180 fjes_get_acpi_resource(struct acpi_resource *acpi_res, void *data)
181 {
182 	struct acpi_resource_address32 *addr;
183 	struct acpi_resource_irq *irq;
184 	struct resource *res = data;
185 
186 	switch (acpi_res->type) {
187 	case ACPI_RESOURCE_TYPE_ADDRESS32:
188 		addr = &acpi_res->data.address32;
189 		res[0].start = addr->address.minimum;
190 		res[0].end = addr->address.minimum +
191 			addr->address.address_length - 1;
192 		break;
193 
194 	case ACPI_RESOURCE_TYPE_IRQ:
195 		irq = &acpi_res->data.irq;
196 		if (irq->interrupt_count != 1)
197 			return AE_ERROR;
198 		res[1].start = irq->interrupts[0];
199 		res[1].end = irq->interrupts[0];
200 		break;
201 
202 	default:
203 		break;
204 	}
205 
206 	return AE_OK;
207 }
208 
209 static int fjes_request_irq(struct fjes_adapter *adapter)
210 {
211 	struct net_device *netdev = adapter->netdev;
212 	int result = -1;
213 
214 	adapter->interrupt_watch_enable = true;
215 	if (!delayed_work_pending(&adapter->interrupt_watch_task)) {
216 		queue_delayed_work(adapter->control_wq,
217 				   &adapter->interrupt_watch_task,
218 				   FJES_IRQ_WATCH_DELAY);
219 	}
220 
221 	if (!adapter->irq_registered) {
222 		result = request_irq(adapter->hw.hw_res.irq, fjes_intr,
223 				     IRQF_SHARED, netdev->name, adapter);
224 		if (result)
225 			adapter->irq_registered = false;
226 		else
227 			adapter->irq_registered = true;
228 	}
229 
230 	return result;
231 }
232 
233 static void fjes_free_irq(struct fjes_adapter *adapter)
234 {
235 	struct fjes_hw *hw = &adapter->hw;
236 
237 	adapter->interrupt_watch_enable = false;
238 	cancel_delayed_work_sync(&adapter->interrupt_watch_task);
239 
240 	fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, true);
241 
242 	if (adapter->irq_registered) {
243 		free_irq(adapter->hw.hw_res.irq, adapter);
244 		adapter->irq_registered = false;
245 	}
246 }
247 
248 static const struct net_device_ops fjes_netdev_ops = {
249 	.ndo_open		= fjes_open,
250 	.ndo_stop		= fjes_close,
251 	.ndo_start_xmit		= fjes_xmit_frame,
252 	.ndo_get_stats64	= fjes_get_stats64,
253 	.ndo_change_mtu		= fjes_change_mtu,
254 	.ndo_tx_timeout		= fjes_tx_retry,
255 	.ndo_vlan_rx_add_vid	= fjes_vlan_rx_add_vid,
256 	.ndo_vlan_rx_kill_vid = fjes_vlan_rx_kill_vid,
257 };
258 
259 /* fjes_open - Called when a network interface is made active */
260 static int fjes_open(struct net_device *netdev)
261 {
262 	struct fjes_adapter *adapter = netdev_priv(netdev);
263 	struct fjes_hw *hw = &adapter->hw;
264 	int result;
265 
266 	if (adapter->open_guard)
267 		return -ENXIO;
268 
269 	result = fjes_setup_resources(adapter);
270 	if (result)
271 		goto err_setup_res;
272 
273 	hw->txrx_stop_req_bit = 0;
274 	hw->epstop_req_bit = 0;
275 
276 	napi_enable(&adapter->napi);
277 
278 	fjes_hw_capture_interrupt_status(hw);
279 
280 	result = fjes_request_irq(adapter);
281 	if (result)
282 		goto err_req_irq;
283 
284 	fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, false);
285 
286 	netif_tx_start_all_queues(netdev);
287 	netif_carrier_on(netdev);
288 
289 	return 0;
290 
291 err_req_irq:
292 	fjes_free_irq(adapter);
293 	napi_disable(&adapter->napi);
294 
295 err_setup_res:
296 	fjes_free_resources(adapter);
297 	return result;
298 }
299 
300 /* fjes_close - Disables a network interface */
301 static int fjes_close(struct net_device *netdev)
302 {
303 	struct fjes_adapter *adapter = netdev_priv(netdev);
304 	struct fjes_hw *hw = &adapter->hw;
305 	unsigned long flags;
306 	int epidx;
307 
308 	netif_tx_stop_all_queues(netdev);
309 	netif_carrier_off(netdev);
310 
311 	fjes_hw_raise_epstop(hw);
312 
313 	napi_disable(&adapter->napi);
314 
315 	spin_lock_irqsave(&hw->rx_status_lock, flags);
316 	for (epidx = 0; epidx < hw->max_epid; epidx++) {
317 		if (epidx == hw->my_epid)
318 			continue;
319 
320 		if (fjes_hw_get_partner_ep_status(hw, epidx) ==
321 		    EP_PARTNER_SHARED)
322 			adapter->hw.ep_shm_info[epidx]
323 				   .tx.info->v1i.rx_status &=
324 				~FJES_RX_POLL_WORK;
325 	}
326 	spin_unlock_irqrestore(&hw->rx_status_lock, flags);
327 
328 	fjes_free_irq(adapter);
329 
330 	cancel_delayed_work_sync(&adapter->interrupt_watch_task);
331 	cancel_work_sync(&adapter->unshare_watch_task);
332 	adapter->unshare_watch_bitmask = 0;
333 	cancel_work_sync(&adapter->raise_intr_rxdata_task);
334 	cancel_work_sync(&adapter->tx_stall_task);
335 
336 	cancel_work_sync(&hw->update_zone_task);
337 	cancel_work_sync(&hw->epstop_task);
338 
339 	fjes_hw_wait_epstop(hw);
340 
341 	fjes_free_resources(adapter);
342 
343 	return 0;
344 }
345 
346 static int fjes_setup_resources(struct fjes_adapter *adapter)
347 {
348 	struct net_device *netdev = adapter->netdev;
349 	struct ep_share_mem_info *buf_pair;
350 	struct fjes_hw *hw = &adapter->hw;
351 	unsigned long flags;
352 	int result;
353 	int epidx;
354 
355 	mutex_lock(&hw->hw_info.lock);
356 	result = fjes_hw_request_info(hw);
357 	switch (result) {
358 	case 0:
359 		for (epidx = 0; epidx < hw->max_epid; epidx++) {
360 			hw->ep_shm_info[epidx].es_status =
361 			    hw->hw_info.res_buf->info.info[epidx].es_status;
362 			hw->ep_shm_info[epidx].zone =
363 			    hw->hw_info.res_buf->info.info[epidx].zone;
364 		}
365 		break;
366 	default:
367 	case -ENOMSG:
368 	case -EBUSY:
369 		adapter->force_reset = true;
370 
371 		mutex_unlock(&hw->hw_info.lock);
372 		return result;
373 	}
374 	mutex_unlock(&hw->hw_info.lock);
375 
376 	for (epidx = 0; epidx < (hw->max_epid); epidx++) {
377 		if ((epidx != hw->my_epid) &&
378 		    (hw->ep_shm_info[epidx].es_status ==
379 		     FJES_ZONING_STATUS_ENABLE)) {
380 			fjes_hw_raise_interrupt(hw, epidx,
381 						REG_ICTL_MASK_INFO_UPDATE);
382 			hw->ep_shm_info[epidx].ep_stats
383 				.send_intr_zoneupdate += 1;
384 		}
385 	}
386 
387 	msleep(FJES_OPEN_ZONE_UPDATE_WAIT * hw->max_epid);
388 
389 	for (epidx = 0; epidx < (hw->max_epid); epidx++) {
390 		if (epidx == hw->my_epid)
391 			continue;
392 
393 		buf_pair = &hw->ep_shm_info[epidx];
394 
395 		spin_lock_irqsave(&hw->rx_status_lock, flags);
396 		fjes_hw_setup_epbuf(&buf_pair->tx, netdev->dev_addr,
397 				    netdev->mtu);
398 		spin_unlock_irqrestore(&hw->rx_status_lock, flags);
399 
400 		if (fjes_hw_epid_is_same_zone(hw, epidx)) {
401 			mutex_lock(&hw->hw_info.lock);
402 			result =
403 			fjes_hw_register_buff_addr(hw, epidx, buf_pair);
404 			mutex_unlock(&hw->hw_info.lock);
405 
406 			switch (result) {
407 			case 0:
408 				break;
409 			case -ENOMSG:
410 			case -EBUSY:
411 			default:
412 				adapter->force_reset = true;
413 				return result;
414 			}
415 
416 			hw->ep_shm_info[epidx].ep_stats
417 				.com_regist_buf_exec += 1;
418 		}
419 	}
420 
421 	return 0;
422 }
423 
424 static void fjes_free_resources(struct fjes_adapter *adapter)
425 {
426 	struct net_device *netdev = adapter->netdev;
427 	struct fjes_device_command_param param;
428 	struct ep_share_mem_info *buf_pair;
429 	struct fjes_hw *hw = &adapter->hw;
430 	bool reset_flag = false;
431 	unsigned long flags;
432 	int result;
433 	int epidx;
434 
435 	for (epidx = 0; epidx < hw->max_epid; epidx++) {
436 		if (epidx == hw->my_epid)
437 			continue;
438 
439 		mutex_lock(&hw->hw_info.lock);
440 		result = fjes_hw_unregister_buff_addr(hw, epidx);
441 		mutex_unlock(&hw->hw_info.lock);
442 
443 		hw->ep_shm_info[epidx].ep_stats.com_unregist_buf_exec += 1;
444 
445 		if (result)
446 			reset_flag = true;
447 
448 		buf_pair = &hw->ep_shm_info[epidx];
449 
450 		spin_lock_irqsave(&hw->rx_status_lock, flags);
451 		fjes_hw_setup_epbuf(&buf_pair->tx,
452 				    netdev->dev_addr, netdev->mtu);
453 		spin_unlock_irqrestore(&hw->rx_status_lock, flags);
454 
455 		clear_bit(epidx, &hw->txrx_stop_req_bit);
456 	}
457 
458 	if (reset_flag || adapter->force_reset) {
459 		result = fjes_hw_reset(hw);
460 
461 		adapter->force_reset = false;
462 
463 		if (result)
464 			adapter->open_guard = true;
465 
466 		hw->hw_info.buffer_share_bit = 0;
467 
468 		memset((void *)&param, 0, sizeof(param));
469 
470 		param.req_len = hw->hw_info.req_buf_size;
471 		param.req_start = __pa(hw->hw_info.req_buf);
472 		param.res_len = hw->hw_info.res_buf_size;
473 		param.res_start = __pa(hw->hw_info.res_buf);
474 		param.share_start = __pa(hw->hw_info.share->ep_status);
475 
476 		fjes_hw_init_command_registers(hw, &param);
477 	}
478 }
479 
480 static void fjes_tx_stall_task(struct work_struct *work)
481 {
482 	struct fjes_adapter *adapter = container_of(work,
483 			struct fjes_adapter, tx_stall_task);
484 	struct net_device *netdev = adapter->netdev;
485 	struct fjes_hw *hw = &adapter->hw;
486 	int all_queue_available, sendable;
487 	enum ep_partner_status pstatus;
488 	int max_epid, my_epid, epid;
489 	union ep_buffer_info *info;
490 	int i;
491 
492 	if (((long)jiffies -
493 		dev_trans_start(netdev)) > FJES_TX_TX_STALL_TIMEOUT) {
494 		netif_wake_queue(netdev);
495 		return;
496 	}
497 
498 	my_epid = hw->my_epid;
499 	max_epid = hw->max_epid;
500 
501 	for (i = 0; i < 5; i++) {
502 		all_queue_available = 1;
503 
504 		for (epid = 0; epid < max_epid; epid++) {
505 			if (my_epid == epid)
506 				continue;
507 
508 			pstatus = fjes_hw_get_partner_ep_status(hw, epid);
509 			sendable = (pstatus == EP_PARTNER_SHARED);
510 			if (!sendable)
511 				continue;
512 
513 			info = adapter->hw.ep_shm_info[epid].tx.info;
514 
515 			if (!(info->v1i.rx_status & FJES_RX_MTU_CHANGING_DONE))
516 				return;
517 
518 			if (EP_RING_FULL(info->v1i.head, info->v1i.tail,
519 					 info->v1i.count_max)) {
520 				all_queue_available = 0;
521 				break;
522 			}
523 		}
524 
525 		if (all_queue_available) {
526 			netif_wake_queue(netdev);
527 			return;
528 		}
529 	}
530 
531 	usleep_range(50, 100);
532 
533 	queue_work(adapter->txrx_wq, &adapter->tx_stall_task);
534 }
535 
536 static void fjes_force_close_task(struct work_struct *work)
537 {
538 	struct fjes_adapter *adapter = container_of(work,
539 			struct fjes_adapter, force_close_task);
540 	struct net_device *netdev = adapter->netdev;
541 
542 	rtnl_lock();
543 	dev_close(netdev);
544 	rtnl_unlock();
545 }
546 
547 static void fjes_raise_intr_rxdata_task(struct work_struct *work)
548 {
549 	struct fjes_adapter *adapter = container_of(work,
550 			struct fjes_adapter, raise_intr_rxdata_task);
551 	struct fjes_hw *hw = &adapter->hw;
552 	enum ep_partner_status pstatus;
553 	int max_epid, my_epid, epid;
554 
555 	my_epid = hw->my_epid;
556 	max_epid = hw->max_epid;
557 
558 	for (epid = 0; epid < max_epid; epid++)
559 		hw->ep_shm_info[epid].tx_status_work = 0;
560 
561 	for (epid = 0; epid < max_epid; epid++) {
562 		if (epid == my_epid)
563 			continue;
564 
565 		pstatus = fjes_hw_get_partner_ep_status(hw, epid);
566 		if (pstatus == EP_PARTNER_SHARED) {
567 			hw->ep_shm_info[epid].tx_status_work =
568 				hw->ep_shm_info[epid].tx.info->v1i.tx_status;
569 
570 			if (hw->ep_shm_info[epid].tx_status_work ==
571 				FJES_TX_DELAY_SEND_PENDING) {
572 				hw->ep_shm_info[epid].tx.info->v1i.tx_status =
573 					FJES_TX_DELAY_SEND_NONE;
574 			}
575 		}
576 	}
577 
578 	for (epid = 0; epid < max_epid; epid++) {
579 		if (epid == my_epid)
580 			continue;
581 
582 		pstatus = fjes_hw_get_partner_ep_status(hw, epid);
583 		if ((hw->ep_shm_info[epid].tx_status_work ==
584 		     FJES_TX_DELAY_SEND_PENDING) &&
585 		    (pstatus == EP_PARTNER_SHARED) &&
586 		    !(hw->ep_shm_info[epid].rx.info->v1i.rx_status &
587 		      FJES_RX_POLL_WORK)) {
588 			fjes_hw_raise_interrupt(hw, epid,
589 						REG_ICTL_MASK_RX_DATA);
590 			hw->ep_shm_info[epid].ep_stats.send_intr_rx += 1;
591 		}
592 	}
593 
594 	usleep_range(500, 1000);
595 }
596 
597 static int fjes_tx_send(struct fjes_adapter *adapter, int dest,
598 			void *data, size_t len)
599 {
600 	int retval;
601 
602 	retval = fjes_hw_epbuf_tx_pkt_send(&adapter->hw.ep_shm_info[dest].tx,
603 					   data, len);
604 	if (retval)
605 		return retval;
606 
607 	adapter->hw.ep_shm_info[dest].tx.info->v1i.tx_status =
608 		FJES_TX_DELAY_SEND_PENDING;
609 	if (!work_pending(&adapter->raise_intr_rxdata_task))
610 		queue_work(adapter->txrx_wq,
611 			   &adapter->raise_intr_rxdata_task);
612 
613 	retval = 0;
614 	return retval;
615 }
616 
617 static netdev_tx_t
618 fjes_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
619 {
620 	struct fjes_adapter *adapter = netdev_priv(netdev);
621 	struct fjes_hw *hw = &adapter->hw;
622 
623 	int max_epid, my_epid, dest_epid;
624 	enum ep_partner_status pstatus;
625 	struct netdev_queue *cur_queue;
626 	char shortpkt[VLAN_ETH_HLEN];
627 	bool is_multi, vlan;
628 	struct ethhdr *eth;
629 	u16 queue_no = 0;
630 	u16 vlan_id = 0;
631 	netdev_tx_t ret;
632 	char *data;
633 	int len;
634 
635 	ret = NETDEV_TX_OK;
636 	is_multi = false;
637 	cur_queue = netdev_get_tx_queue(netdev, queue_no);
638 
639 	eth = (struct ethhdr *)skb->data;
640 	my_epid = hw->my_epid;
641 
642 	vlan = (vlan_get_tag(skb, &vlan_id) == 0) ? true : false;
643 
644 	data = skb->data;
645 	len = skb->len;
646 
647 	if (is_multicast_ether_addr(eth->h_dest)) {
648 		dest_epid = 0;
649 		max_epid = hw->max_epid;
650 		is_multi = true;
651 	} else if (is_local_ether_addr(eth->h_dest)) {
652 		dest_epid = eth->h_dest[ETH_ALEN - 1];
653 		max_epid = dest_epid + 1;
654 
655 		if ((eth->h_dest[0] == 0x02) &&
656 		    (0x00 == (eth->h_dest[1] | eth->h_dest[2] |
657 			      eth->h_dest[3] | eth->h_dest[4])) &&
658 		    (dest_epid < hw->max_epid)) {
659 			;
660 		} else {
661 			dest_epid = 0;
662 			max_epid = 0;
663 			ret = NETDEV_TX_OK;
664 
665 			adapter->stats64.tx_packets += 1;
666 			hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
667 			adapter->stats64.tx_bytes += len;
668 			hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
669 		}
670 	} else {
671 		dest_epid = 0;
672 		max_epid = 0;
673 		ret = NETDEV_TX_OK;
674 
675 		adapter->stats64.tx_packets += 1;
676 		hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
677 		adapter->stats64.tx_bytes += len;
678 		hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
679 	}
680 
681 	for (; dest_epid < max_epid; dest_epid++) {
682 		if (my_epid == dest_epid)
683 			continue;
684 
685 		pstatus = fjes_hw_get_partner_ep_status(hw, dest_epid);
686 		if (pstatus != EP_PARTNER_SHARED) {
687 			if (!is_multi)
688 				hw->ep_shm_info[dest_epid].ep_stats
689 					.tx_dropped_not_shared += 1;
690 			ret = NETDEV_TX_OK;
691 		} else if (!fjes_hw_check_epbuf_version(
692 				&adapter->hw.ep_shm_info[dest_epid].rx, 0)) {
693 			/* version is NOT 0 */
694 			adapter->stats64.tx_carrier_errors += 1;
695 			hw->ep_shm_info[dest_epid].net_stats
696 						.tx_carrier_errors += 1;
697 			hw->ep_shm_info[dest_epid].ep_stats
698 					.tx_dropped_ver_mismatch += 1;
699 
700 			ret = NETDEV_TX_OK;
701 		} else if (!fjes_hw_check_mtu(
702 				&adapter->hw.ep_shm_info[dest_epid].rx,
703 				netdev->mtu)) {
704 			adapter->stats64.tx_dropped += 1;
705 			hw->ep_shm_info[dest_epid].net_stats.tx_dropped += 1;
706 			adapter->stats64.tx_errors += 1;
707 			hw->ep_shm_info[dest_epid].net_stats.tx_errors += 1;
708 			hw->ep_shm_info[dest_epid].ep_stats
709 					.tx_dropped_buf_size_mismatch += 1;
710 
711 			ret = NETDEV_TX_OK;
712 		} else if (vlan &&
713 			   !fjes_hw_check_vlan_id(
714 				&adapter->hw.ep_shm_info[dest_epid].rx,
715 				vlan_id)) {
716 			hw->ep_shm_info[dest_epid].ep_stats
717 				.tx_dropped_vlanid_mismatch += 1;
718 			ret = NETDEV_TX_OK;
719 		} else {
720 			if (len < VLAN_ETH_HLEN) {
721 				memset(shortpkt, 0, VLAN_ETH_HLEN);
722 				memcpy(shortpkt, skb->data, skb->len);
723 				len = VLAN_ETH_HLEN;
724 				data = shortpkt;
725 			}
726 
727 			if (adapter->tx_retry_count == 0) {
728 				adapter->tx_start_jiffies = jiffies;
729 				adapter->tx_retry_count = 1;
730 			} else {
731 				adapter->tx_retry_count++;
732 			}
733 
734 			if (fjes_tx_send(adapter, dest_epid, data, len)) {
735 				if (is_multi) {
736 					ret = NETDEV_TX_OK;
737 				} else if (
738 					   ((long)jiffies -
739 					    (long)adapter->tx_start_jiffies) >=
740 					    FJES_TX_RETRY_TIMEOUT) {
741 					adapter->stats64.tx_fifo_errors += 1;
742 					hw->ep_shm_info[dest_epid].net_stats
743 								.tx_fifo_errors += 1;
744 					adapter->stats64.tx_errors += 1;
745 					hw->ep_shm_info[dest_epid].net_stats
746 								.tx_errors += 1;
747 
748 					ret = NETDEV_TX_OK;
749 				} else {
750 					netif_trans_update(netdev);
751 					hw->ep_shm_info[dest_epid].ep_stats
752 						.tx_buffer_full += 1;
753 					netif_tx_stop_queue(cur_queue);
754 
755 					if (!work_pending(&adapter->tx_stall_task))
756 						queue_work(adapter->txrx_wq,
757 							   &adapter->tx_stall_task);
758 
759 					ret = NETDEV_TX_BUSY;
760 				}
761 			} else {
762 				if (!is_multi) {
763 					adapter->stats64.tx_packets += 1;
764 					hw->ep_shm_info[dest_epid].net_stats
765 								.tx_packets += 1;
766 					adapter->stats64.tx_bytes += len;
767 					hw->ep_shm_info[dest_epid].net_stats
768 								.tx_bytes += len;
769 				}
770 
771 				adapter->tx_retry_count = 0;
772 				ret = NETDEV_TX_OK;
773 			}
774 		}
775 	}
776 
777 	if (ret == NETDEV_TX_OK) {
778 		dev_kfree_skb(skb);
779 		if (is_multi) {
780 			adapter->stats64.tx_packets += 1;
781 			hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
782 			adapter->stats64.tx_bytes += 1;
783 			hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
784 		}
785 	}
786 
787 	return ret;
788 }
789 
790 static void fjes_tx_retry(struct net_device *netdev, unsigned int txqueue)
791 {
792 	struct netdev_queue *queue = netdev_get_tx_queue(netdev, 0);
793 
794 	netif_tx_wake_queue(queue);
795 }
796 
797 static void
798 fjes_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
799 {
800 	struct fjes_adapter *adapter = netdev_priv(netdev);
801 
802 	memcpy(stats, &adapter->stats64, sizeof(struct rtnl_link_stats64));
803 }
804 
805 static int fjes_change_mtu(struct net_device *netdev, int new_mtu)
806 {
807 	struct fjes_adapter *adapter = netdev_priv(netdev);
808 	bool running = netif_running(netdev);
809 	struct fjes_hw *hw = &adapter->hw;
810 	unsigned long flags;
811 	int ret = -EINVAL;
812 	int idx, epidx;
813 
814 	for (idx = 0; fjes_support_mtu[idx] != 0; idx++) {
815 		if (new_mtu <= fjes_support_mtu[idx]) {
816 			new_mtu = fjes_support_mtu[idx];
817 			if (new_mtu == netdev->mtu)
818 				return 0;
819 
820 			ret = 0;
821 			break;
822 		}
823 	}
824 
825 	if (ret)
826 		return ret;
827 
828 	if (running) {
829 		spin_lock_irqsave(&hw->rx_status_lock, flags);
830 		for (epidx = 0; epidx < hw->max_epid; epidx++) {
831 			if (epidx == hw->my_epid)
832 				continue;
833 			hw->ep_shm_info[epidx].tx.info->v1i.rx_status &=
834 				~FJES_RX_MTU_CHANGING_DONE;
835 		}
836 		spin_unlock_irqrestore(&hw->rx_status_lock, flags);
837 
838 		netif_tx_stop_all_queues(netdev);
839 		netif_carrier_off(netdev);
840 		cancel_work_sync(&adapter->tx_stall_task);
841 		napi_disable(&adapter->napi);
842 
843 		msleep(1000);
844 
845 		netif_tx_stop_all_queues(netdev);
846 	}
847 
848 	netdev->mtu = new_mtu;
849 
850 	if (running) {
851 		for (epidx = 0; epidx < hw->max_epid; epidx++) {
852 			if (epidx == hw->my_epid)
853 				continue;
854 
855 			spin_lock_irqsave(&hw->rx_status_lock, flags);
856 			fjes_hw_setup_epbuf(&hw->ep_shm_info[epidx].tx,
857 					    netdev->dev_addr,
858 					    netdev->mtu);
859 
860 			hw->ep_shm_info[epidx].tx.info->v1i.rx_status |=
861 				FJES_RX_MTU_CHANGING_DONE;
862 			spin_unlock_irqrestore(&hw->rx_status_lock, flags);
863 		}
864 
865 		netif_tx_wake_all_queues(netdev);
866 		netif_carrier_on(netdev);
867 		napi_enable(&adapter->napi);
868 		napi_schedule(&adapter->napi);
869 	}
870 
871 	return ret;
872 }
873 
874 static int fjes_vlan_rx_add_vid(struct net_device *netdev,
875 				__be16 proto, u16 vid)
876 {
877 	struct fjes_adapter *adapter = netdev_priv(netdev);
878 	bool ret = true;
879 	int epid;
880 
881 	for (epid = 0; epid < adapter->hw.max_epid; epid++) {
882 		if (epid == adapter->hw.my_epid)
883 			continue;
884 
885 		if (!fjes_hw_check_vlan_id(
886 			&adapter->hw.ep_shm_info[epid].tx, vid))
887 			ret = fjes_hw_set_vlan_id(
888 				&adapter->hw.ep_shm_info[epid].tx, vid);
889 	}
890 
891 	return ret ? 0 : -ENOSPC;
892 }
893 
894 static int fjes_vlan_rx_kill_vid(struct net_device *netdev,
895 				 __be16 proto, u16 vid)
896 {
897 	struct fjes_adapter *adapter = netdev_priv(netdev);
898 	int epid;
899 
900 	for (epid = 0; epid < adapter->hw.max_epid; epid++) {
901 		if (epid == adapter->hw.my_epid)
902 			continue;
903 
904 		fjes_hw_del_vlan_id(&adapter->hw.ep_shm_info[epid].tx, vid);
905 	}
906 
907 	return 0;
908 }
909 
910 static void fjes_txrx_stop_req_irq(struct fjes_adapter *adapter,
911 				   int src_epid)
912 {
913 	struct fjes_hw *hw = &adapter->hw;
914 	enum ep_partner_status status;
915 	unsigned long flags;
916 
917 	status = fjes_hw_get_partner_ep_status(hw, src_epid);
918 	trace_fjes_txrx_stop_req_irq_pre(hw, src_epid, status);
919 	switch (status) {
920 	case EP_PARTNER_UNSHARE:
921 	case EP_PARTNER_COMPLETE:
922 	default:
923 		break;
924 	case EP_PARTNER_WAITING:
925 		if (src_epid < hw->my_epid) {
926 			spin_lock_irqsave(&hw->rx_status_lock, flags);
927 			hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
928 				FJES_RX_STOP_REQ_DONE;
929 			spin_unlock_irqrestore(&hw->rx_status_lock, flags);
930 
931 			clear_bit(src_epid, &hw->txrx_stop_req_bit);
932 			set_bit(src_epid, &adapter->unshare_watch_bitmask);
933 
934 			if (!work_pending(&adapter->unshare_watch_task))
935 				queue_work(adapter->control_wq,
936 					   &adapter->unshare_watch_task);
937 		}
938 		break;
939 	case EP_PARTNER_SHARED:
940 		if (hw->ep_shm_info[src_epid].rx.info->v1i.rx_status &
941 		    FJES_RX_STOP_REQ_REQUEST) {
942 			set_bit(src_epid, &hw->epstop_req_bit);
943 			if (!work_pending(&hw->epstop_task))
944 				queue_work(adapter->control_wq,
945 					   &hw->epstop_task);
946 		}
947 		break;
948 	}
949 	trace_fjes_txrx_stop_req_irq_post(hw, src_epid);
950 }
951 
952 static void fjes_stop_req_irq(struct fjes_adapter *adapter, int src_epid)
953 {
954 	struct fjes_hw *hw = &adapter->hw;
955 	enum ep_partner_status status;
956 	unsigned long flags;
957 
958 	set_bit(src_epid, &hw->hw_info.buffer_unshare_reserve_bit);
959 
960 	status = fjes_hw_get_partner_ep_status(hw, src_epid);
961 	trace_fjes_stop_req_irq_pre(hw, src_epid, status);
962 	switch (status) {
963 	case EP_PARTNER_WAITING:
964 		spin_lock_irqsave(&hw->rx_status_lock, flags);
965 		hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
966 				FJES_RX_STOP_REQ_DONE;
967 		spin_unlock_irqrestore(&hw->rx_status_lock, flags);
968 		clear_bit(src_epid, &hw->txrx_stop_req_bit);
969 		fallthrough;
970 	case EP_PARTNER_UNSHARE:
971 	case EP_PARTNER_COMPLETE:
972 	default:
973 		set_bit(src_epid, &adapter->unshare_watch_bitmask);
974 		if (!work_pending(&adapter->unshare_watch_task))
975 			queue_work(adapter->control_wq,
976 				   &adapter->unshare_watch_task);
977 		break;
978 	case EP_PARTNER_SHARED:
979 		set_bit(src_epid, &hw->epstop_req_bit);
980 
981 		if (!work_pending(&hw->epstop_task))
982 			queue_work(adapter->control_wq, &hw->epstop_task);
983 		break;
984 	}
985 	trace_fjes_stop_req_irq_post(hw, src_epid);
986 }
987 
988 static void fjes_update_zone_irq(struct fjes_adapter *adapter,
989 				 int src_epid)
990 {
991 	struct fjes_hw *hw = &adapter->hw;
992 
993 	if (!work_pending(&hw->update_zone_task))
994 		queue_work(adapter->control_wq, &hw->update_zone_task);
995 }
996 
997 static irqreturn_t fjes_intr(int irq, void *data)
998 {
999 	struct fjes_adapter *adapter = data;
1000 	struct fjes_hw *hw = &adapter->hw;
1001 	irqreturn_t ret;
1002 	u32 icr;
1003 
1004 	icr = fjes_hw_capture_interrupt_status(hw);
1005 
1006 	if (icr & REG_IS_MASK_IS_ASSERT) {
1007 		if (icr & REG_ICTL_MASK_RX_DATA) {
1008 			fjes_rx_irq(adapter, icr & REG_IS_MASK_EPID);
1009 			hw->ep_shm_info[icr & REG_IS_MASK_EPID].ep_stats
1010 				.recv_intr_rx += 1;
1011 		}
1012 
1013 		if (icr & REG_ICTL_MASK_DEV_STOP_REQ) {
1014 			fjes_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
1015 			hw->ep_shm_info[icr & REG_IS_MASK_EPID].ep_stats
1016 				.recv_intr_stop += 1;
1017 		}
1018 
1019 		if (icr & REG_ICTL_MASK_TXRX_STOP_REQ) {
1020 			fjes_txrx_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
1021 			hw->ep_shm_info[icr & REG_IS_MASK_EPID].ep_stats
1022 				.recv_intr_unshare += 1;
1023 		}
1024 
1025 		if (icr & REG_ICTL_MASK_TXRX_STOP_DONE)
1026 			fjes_hw_set_irqmask(hw,
1027 					    REG_ICTL_MASK_TXRX_STOP_DONE, true);
1028 
1029 		if (icr & REG_ICTL_MASK_INFO_UPDATE) {
1030 			fjes_update_zone_irq(adapter, icr & REG_IS_MASK_EPID);
1031 			hw->ep_shm_info[icr & REG_IS_MASK_EPID].ep_stats
1032 				.recv_intr_zoneupdate += 1;
1033 		}
1034 
1035 		ret = IRQ_HANDLED;
1036 	} else {
1037 		ret = IRQ_NONE;
1038 	}
1039 
1040 	return ret;
1041 }
1042 
1043 static int fjes_rxframe_search_exist(struct fjes_adapter *adapter,
1044 				     int start_epid)
1045 {
1046 	struct fjes_hw *hw = &adapter->hw;
1047 	enum ep_partner_status pstatus;
1048 	int max_epid, cur_epid;
1049 	int i;
1050 
1051 	max_epid = hw->max_epid;
1052 	start_epid = (start_epid + 1 + max_epid) % max_epid;
1053 
1054 	for (i = 0; i < max_epid; i++) {
1055 		cur_epid = (start_epid + i) % max_epid;
1056 		if (cur_epid == hw->my_epid)
1057 			continue;
1058 
1059 		pstatus = fjes_hw_get_partner_ep_status(hw, cur_epid);
1060 		if (pstatus == EP_PARTNER_SHARED) {
1061 			if (!fjes_hw_epbuf_rx_is_empty(
1062 				&hw->ep_shm_info[cur_epid].rx))
1063 				return cur_epid;
1064 		}
1065 	}
1066 	return -1;
1067 }
1068 
1069 static void *fjes_rxframe_get(struct fjes_adapter *adapter, size_t *psize,
1070 			      int *cur_epid)
1071 {
1072 	void *frame;
1073 
1074 	*cur_epid = fjes_rxframe_search_exist(adapter, *cur_epid);
1075 	if (*cur_epid < 0)
1076 		return NULL;
1077 
1078 	frame =
1079 	fjes_hw_epbuf_rx_curpkt_get_addr(
1080 		&adapter->hw.ep_shm_info[*cur_epid].rx, psize);
1081 
1082 	return frame;
1083 }
1084 
1085 static void fjes_rxframe_release(struct fjes_adapter *adapter, int cur_epid)
1086 {
1087 	fjes_hw_epbuf_rx_curpkt_drop(&adapter->hw.ep_shm_info[cur_epid].rx);
1088 }
1089 
1090 static void fjes_rx_irq(struct fjes_adapter *adapter, int src_epid)
1091 {
1092 	struct fjes_hw *hw = &adapter->hw;
1093 
1094 	fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, true);
1095 
1096 	adapter->unset_rx_last = true;
1097 	napi_schedule(&adapter->napi);
1098 }
1099 
1100 static int fjes_poll(struct napi_struct *napi, int budget)
1101 {
1102 	struct fjes_adapter *adapter =
1103 			container_of(napi, struct fjes_adapter, napi);
1104 	struct net_device *netdev = napi->dev;
1105 	struct fjes_hw *hw = &adapter->hw;
1106 	struct sk_buff *skb;
1107 	int work_done = 0;
1108 	int cur_epid = 0;
1109 	int epidx;
1110 	size_t frame_len;
1111 	void *frame;
1112 
1113 	spin_lock(&hw->rx_status_lock);
1114 	for (epidx = 0; epidx < hw->max_epid; epidx++) {
1115 		if (epidx == hw->my_epid)
1116 			continue;
1117 
1118 		if (fjes_hw_get_partner_ep_status(hw, epidx) ==
1119 		    EP_PARTNER_SHARED)
1120 			adapter->hw.ep_shm_info[epidx]
1121 				   .tx.info->v1i.rx_status |= FJES_RX_POLL_WORK;
1122 	}
1123 	spin_unlock(&hw->rx_status_lock);
1124 
1125 	while (work_done < budget) {
1126 		prefetch(&adapter->hw);
1127 		frame = fjes_rxframe_get(adapter, &frame_len, &cur_epid);
1128 
1129 		if (frame) {
1130 			skb = napi_alloc_skb(napi, frame_len);
1131 			if (!skb) {
1132 				adapter->stats64.rx_dropped += 1;
1133 				hw->ep_shm_info[cur_epid].net_stats
1134 							 .rx_dropped += 1;
1135 				adapter->stats64.rx_errors += 1;
1136 				hw->ep_shm_info[cur_epid].net_stats
1137 							 .rx_errors += 1;
1138 			} else {
1139 				skb_put_data(skb, frame, frame_len);
1140 				skb->protocol = eth_type_trans(skb, netdev);
1141 				skb->ip_summed = CHECKSUM_UNNECESSARY;
1142 
1143 				netif_receive_skb(skb);
1144 
1145 				work_done++;
1146 
1147 				adapter->stats64.rx_packets += 1;
1148 				hw->ep_shm_info[cur_epid].net_stats
1149 							 .rx_packets += 1;
1150 				adapter->stats64.rx_bytes += frame_len;
1151 				hw->ep_shm_info[cur_epid].net_stats
1152 							 .rx_bytes += frame_len;
1153 
1154 				if (is_multicast_ether_addr(
1155 					((struct ethhdr *)frame)->h_dest)) {
1156 					adapter->stats64.multicast += 1;
1157 					hw->ep_shm_info[cur_epid].net_stats
1158 								 .multicast += 1;
1159 				}
1160 			}
1161 
1162 			fjes_rxframe_release(adapter, cur_epid);
1163 			adapter->unset_rx_last = true;
1164 		} else {
1165 			break;
1166 		}
1167 	}
1168 
1169 	if (work_done < budget) {
1170 		napi_complete_done(napi, work_done);
1171 
1172 		if (adapter->unset_rx_last) {
1173 			adapter->rx_last_jiffies = jiffies;
1174 			adapter->unset_rx_last = false;
1175 		}
1176 
1177 		if (((long)jiffies - (long)adapter->rx_last_jiffies) < 3) {
1178 			napi_reschedule(napi);
1179 		} else {
1180 			spin_lock(&hw->rx_status_lock);
1181 			for (epidx = 0; epidx < hw->max_epid; epidx++) {
1182 				if (epidx == hw->my_epid)
1183 					continue;
1184 				if (fjes_hw_get_partner_ep_status(hw, epidx) ==
1185 				    EP_PARTNER_SHARED)
1186 					adapter->hw.ep_shm_info[epidx].tx
1187 						   .info->v1i.rx_status &=
1188 						~FJES_RX_POLL_WORK;
1189 			}
1190 			spin_unlock(&hw->rx_status_lock);
1191 
1192 			fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, false);
1193 		}
1194 	}
1195 
1196 	return work_done;
1197 }
1198 
1199 /* fjes_probe - Device Initialization Routine */
1200 static int fjes_probe(struct platform_device *plat_dev)
1201 {
1202 	struct fjes_adapter *adapter;
1203 	struct net_device *netdev;
1204 	struct resource *res;
1205 	struct fjes_hw *hw;
1206 	u8 addr[ETH_ALEN];
1207 	int err;
1208 
1209 	err = -ENOMEM;
1210 	netdev = alloc_netdev_mq(sizeof(struct fjes_adapter), "es%d",
1211 				 NET_NAME_UNKNOWN, fjes_netdev_setup,
1212 				 FJES_MAX_QUEUES);
1213 
1214 	if (!netdev)
1215 		goto err_out;
1216 
1217 	SET_NETDEV_DEV(netdev, &plat_dev->dev);
1218 
1219 	dev_set_drvdata(&plat_dev->dev, netdev);
1220 	adapter = netdev_priv(netdev);
1221 	adapter->netdev = netdev;
1222 	adapter->plat_dev = plat_dev;
1223 	hw = &adapter->hw;
1224 	hw->back = adapter;
1225 
1226 	/* setup the private structure */
1227 	err = fjes_sw_init(adapter);
1228 	if (err)
1229 		goto err_free_netdev;
1230 
1231 	INIT_WORK(&adapter->force_close_task, fjes_force_close_task);
1232 	adapter->force_reset = false;
1233 	adapter->open_guard = false;
1234 
1235 	adapter->txrx_wq = alloc_workqueue(DRV_NAME "/txrx", WQ_MEM_RECLAIM, 0);
1236 	if (unlikely(!adapter->txrx_wq)) {
1237 		err = -ENOMEM;
1238 		goto err_free_netdev;
1239 	}
1240 
1241 	adapter->control_wq = alloc_workqueue(DRV_NAME "/control",
1242 					      WQ_MEM_RECLAIM, 0);
1243 	if (unlikely(!adapter->control_wq)) {
1244 		err = -ENOMEM;
1245 		goto err_free_txrx_wq;
1246 	}
1247 
1248 	INIT_WORK(&adapter->tx_stall_task, fjes_tx_stall_task);
1249 	INIT_WORK(&adapter->raise_intr_rxdata_task,
1250 		  fjes_raise_intr_rxdata_task);
1251 	INIT_WORK(&adapter->unshare_watch_task, fjes_watch_unshare_task);
1252 	adapter->unshare_watch_bitmask = 0;
1253 
1254 	INIT_DELAYED_WORK(&adapter->interrupt_watch_task, fjes_irq_watch_task);
1255 	adapter->interrupt_watch_enable = false;
1256 
1257 	res = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
1258 	if (!res) {
1259 		err = -EINVAL;
1260 		goto err_free_control_wq;
1261 	}
1262 	hw->hw_res.start = res->start;
1263 	hw->hw_res.size = resource_size(res);
1264 	hw->hw_res.irq = platform_get_irq(plat_dev, 0);
1265 	if (hw->hw_res.irq < 0) {
1266 		err = hw->hw_res.irq;
1267 		goto err_free_control_wq;
1268 	}
1269 
1270 	err = fjes_hw_init(&adapter->hw);
1271 	if (err)
1272 		goto err_free_control_wq;
1273 
1274 	/* setup MAC address (02:00:00:00:00:[epid])*/
1275 	addr[0] = 2;
1276 	addr[1] = 0;
1277 	addr[2] = 0;
1278 	addr[3] = 0;
1279 	addr[4] = 0;
1280 	addr[5] = hw->my_epid; /* EPID */
1281 	eth_hw_addr_set(netdev, addr);
1282 
1283 	err = register_netdev(netdev);
1284 	if (err)
1285 		goto err_hw_exit;
1286 
1287 	netif_carrier_off(netdev);
1288 
1289 	fjes_dbg_adapter_init(adapter);
1290 
1291 	return 0;
1292 
1293 err_hw_exit:
1294 	fjes_hw_exit(&adapter->hw);
1295 err_free_control_wq:
1296 	destroy_workqueue(adapter->control_wq);
1297 err_free_txrx_wq:
1298 	destroy_workqueue(adapter->txrx_wq);
1299 err_free_netdev:
1300 	free_netdev(netdev);
1301 err_out:
1302 	return err;
1303 }
1304 
1305 /* fjes_remove - Device Removal Routine */
1306 static int fjes_remove(struct platform_device *plat_dev)
1307 {
1308 	struct net_device *netdev = dev_get_drvdata(&plat_dev->dev);
1309 	struct fjes_adapter *adapter = netdev_priv(netdev);
1310 	struct fjes_hw *hw = &adapter->hw;
1311 
1312 	fjes_dbg_adapter_exit(adapter);
1313 
1314 	cancel_delayed_work_sync(&adapter->interrupt_watch_task);
1315 	cancel_work_sync(&adapter->unshare_watch_task);
1316 	cancel_work_sync(&adapter->raise_intr_rxdata_task);
1317 	cancel_work_sync(&adapter->tx_stall_task);
1318 	if (adapter->control_wq)
1319 		destroy_workqueue(adapter->control_wq);
1320 	if (adapter->txrx_wq)
1321 		destroy_workqueue(adapter->txrx_wq);
1322 
1323 	unregister_netdev(netdev);
1324 
1325 	fjes_hw_exit(hw);
1326 
1327 	netif_napi_del(&adapter->napi);
1328 
1329 	free_netdev(netdev);
1330 
1331 	return 0;
1332 }
1333 
1334 static int fjes_sw_init(struct fjes_adapter *adapter)
1335 {
1336 	struct net_device *netdev = adapter->netdev;
1337 
1338 	netif_napi_add(netdev, &adapter->napi, fjes_poll, 64);
1339 
1340 	return 0;
1341 }
1342 
1343 /* fjes_netdev_setup - netdevice initialization routine */
1344 static void fjes_netdev_setup(struct net_device *netdev)
1345 {
1346 	ether_setup(netdev);
1347 
1348 	netdev->watchdog_timeo = FJES_TX_RETRY_INTERVAL;
1349 	netdev->netdev_ops = &fjes_netdev_ops;
1350 	fjes_set_ethtool_ops(netdev);
1351 	netdev->mtu = fjes_support_mtu[3];
1352 	netdev->min_mtu = fjes_support_mtu[0];
1353 	netdev->max_mtu = fjes_support_mtu[3];
1354 	netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1355 }
1356 
1357 static void fjes_irq_watch_task(struct work_struct *work)
1358 {
1359 	struct fjes_adapter *adapter = container_of(to_delayed_work(work),
1360 			struct fjes_adapter, interrupt_watch_task);
1361 
1362 	local_irq_disable();
1363 	fjes_intr(adapter->hw.hw_res.irq, adapter);
1364 	local_irq_enable();
1365 
1366 	if (fjes_rxframe_search_exist(adapter, 0) >= 0)
1367 		napi_schedule(&adapter->napi);
1368 
1369 	if (adapter->interrupt_watch_enable) {
1370 		if (!delayed_work_pending(&adapter->interrupt_watch_task))
1371 			queue_delayed_work(adapter->control_wq,
1372 					   &adapter->interrupt_watch_task,
1373 					   FJES_IRQ_WATCH_DELAY);
1374 	}
1375 }
1376 
1377 static void fjes_watch_unshare_task(struct work_struct *work)
1378 {
1379 	struct fjes_adapter *adapter =
1380 	container_of(work, struct fjes_adapter, unshare_watch_task);
1381 
1382 	struct net_device *netdev = adapter->netdev;
1383 	struct fjes_hw *hw = &adapter->hw;
1384 
1385 	int unshare_watch, unshare_reserve;
1386 	int max_epid, my_epid, epidx;
1387 	int stop_req, stop_req_done;
1388 	ulong unshare_watch_bitmask;
1389 	unsigned long flags;
1390 	int wait_time = 0;
1391 	int is_shared;
1392 	int ret;
1393 
1394 	my_epid = hw->my_epid;
1395 	max_epid = hw->max_epid;
1396 
1397 	unshare_watch_bitmask = adapter->unshare_watch_bitmask;
1398 	adapter->unshare_watch_bitmask = 0;
1399 
1400 	while ((unshare_watch_bitmask || hw->txrx_stop_req_bit) &&
1401 	       (wait_time < 3000)) {
1402 		for (epidx = 0; epidx < max_epid; epidx++) {
1403 			if (epidx == my_epid)
1404 				continue;
1405 
1406 			is_shared = fjes_hw_epid_is_shared(hw->hw_info.share,
1407 							   epidx);
1408 
1409 			stop_req = test_bit(epidx, &hw->txrx_stop_req_bit);
1410 
1411 			stop_req_done = hw->ep_shm_info[epidx].rx.info->v1i.rx_status &
1412 					FJES_RX_STOP_REQ_DONE;
1413 
1414 			unshare_watch = test_bit(epidx, &unshare_watch_bitmask);
1415 
1416 			unshare_reserve = test_bit(epidx,
1417 						   &hw->hw_info.buffer_unshare_reserve_bit);
1418 
1419 			if ((!stop_req ||
1420 			     (is_shared && (!is_shared || !stop_req_done))) &&
1421 			    (is_shared || !unshare_watch || !unshare_reserve))
1422 				continue;
1423 
1424 			mutex_lock(&hw->hw_info.lock);
1425 			ret = fjes_hw_unregister_buff_addr(hw, epidx);
1426 			switch (ret) {
1427 			case 0:
1428 				break;
1429 			case -ENOMSG:
1430 			case -EBUSY:
1431 			default:
1432 				if (!work_pending(
1433 					&adapter->force_close_task)) {
1434 					adapter->force_reset = true;
1435 					schedule_work(
1436 						&adapter->force_close_task);
1437 				}
1438 				break;
1439 			}
1440 			mutex_unlock(&hw->hw_info.lock);
1441 			hw->ep_shm_info[epidx].ep_stats
1442 					.com_unregist_buf_exec += 1;
1443 
1444 			spin_lock_irqsave(&hw->rx_status_lock, flags);
1445 			fjes_hw_setup_epbuf(&hw->ep_shm_info[epidx].tx,
1446 					    netdev->dev_addr, netdev->mtu);
1447 			spin_unlock_irqrestore(&hw->rx_status_lock, flags);
1448 
1449 			clear_bit(epidx, &hw->txrx_stop_req_bit);
1450 			clear_bit(epidx, &unshare_watch_bitmask);
1451 			clear_bit(epidx,
1452 				  &hw->hw_info.buffer_unshare_reserve_bit);
1453 		}
1454 
1455 		msleep(100);
1456 		wait_time += 100;
1457 	}
1458 
1459 	if (hw->hw_info.buffer_unshare_reserve_bit) {
1460 		for (epidx = 0; epidx < max_epid; epidx++) {
1461 			if (epidx == my_epid)
1462 				continue;
1463 
1464 			if (test_bit(epidx,
1465 				     &hw->hw_info.buffer_unshare_reserve_bit)) {
1466 				mutex_lock(&hw->hw_info.lock);
1467 
1468 				ret = fjes_hw_unregister_buff_addr(hw, epidx);
1469 				switch (ret) {
1470 				case 0:
1471 					break;
1472 				case -ENOMSG:
1473 				case -EBUSY:
1474 				default:
1475 					if (!work_pending(
1476 						&adapter->force_close_task)) {
1477 						adapter->force_reset = true;
1478 						schedule_work(
1479 							&adapter->force_close_task);
1480 					}
1481 					break;
1482 				}
1483 				mutex_unlock(&hw->hw_info.lock);
1484 
1485 				hw->ep_shm_info[epidx].ep_stats
1486 					.com_unregist_buf_exec += 1;
1487 
1488 				spin_lock_irqsave(&hw->rx_status_lock, flags);
1489 				fjes_hw_setup_epbuf(
1490 					&hw->ep_shm_info[epidx].tx,
1491 					netdev->dev_addr, netdev->mtu);
1492 				spin_unlock_irqrestore(&hw->rx_status_lock,
1493 						       flags);
1494 
1495 				clear_bit(epidx, &hw->txrx_stop_req_bit);
1496 				clear_bit(epidx, &unshare_watch_bitmask);
1497 				clear_bit(epidx, &hw->hw_info.buffer_unshare_reserve_bit);
1498 			}
1499 
1500 			if (test_bit(epidx, &unshare_watch_bitmask)) {
1501 				spin_lock_irqsave(&hw->rx_status_lock, flags);
1502 				hw->ep_shm_info[epidx].tx.info->v1i.rx_status &=
1503 						~FJES_RX_STOP_REQ_DONE;
1504 				spin_unlock_irqrestore(&hw->rx_status_lock,
1505 						       flags);
1506 			}
1507 		}
1508 	}
1509 }
1510 
1511 static acpi_status
1512 acpi_find_extended_socket_device(acpi_handle obj_handle, u32 level,
1513 				 void *context, void **return_value)
1514 {
1515 	struct acpi_device *device;
1516 	bool *found = context;
1517 	int result;
1518 
1519 	result = acpi_bus_get_device(obj_handle, &device);
1520 	if (result)
1521 		return AE_OK;
1522 
1523 	if (strcmp(acpi_device_hid(device), ACPI_MOTHERBOARD_RESOURCE_HID))
1524 		return AE_OK;
1525 
1526 	if (!is_extended_socket_device(device))
1527 		return AE_OK;
1528 
1529 	if (acpi_check_extended_socket_status(device))
1530 		return AE_OK;
1531 
1532 	*found = true;
1533 	return AE_CTRL_TERMINATE;
1534 }
1535 
1536 /* fjes_init_module - Driver Registration Routine */
1537 static int __init fjes_init_module(void)
1538 {
1539 	bool found = false;
1540 	int result;
1541 
1542 	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
1543 			    acpi_find_extended_socket_device, NULL, &found,
1544 			    NULL);
1545 
1546 	if (!found)
1547 		return -ENODEV;
1548 
1549 	pr_info("%s - version %s - %s\n",
1550 		fjes_driver_string, fjes_driver_version, fjes_copyright);
1551 
1552 	fjes_dbg_init();
1553 
1554 	result = platform_driver_register(&fjes_driver);
1555 	if (result < 0) {
1556 		fjes_dbg_exit();
1557 		return result;
1558 	}
1559 
1560 	result = acpi_bus_register_driver(&fjes_acpi_driver);
1561 	if (result < 0)
1562 		goto fail_acpi_driver;
1563 
1564 	return 0;
1565 
1566 fail_acpi_driver:
1567 	platform_driver_unregister(&fjes_driver);
1568 	fjes_dbg_exit();
1569 	return result;
1570 }
1571 
1572 module_init(fjes_init_module);
1573 
1574 /* fjes_exit_module - Driver Exit Cleanup Routine */
1575 static void __exit fjes_exit_module(void)
1576 {
1577 	acpi_bus_unregister_driver(&fjes_acpi_driver);
1578 	platform_driver_unregister(&fjes_driver);
1579 	fjes_dbg_exit();
1580 }
1581 
1582 module_exit(fjes_exit_module);
1583