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