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