blob: e46b1ebbbff4d88312510e42fc5e279c54ed34a4 [file] [log] [blame]
Taku Izumi658d4392015-08-21 17:29:17 +09001/*
2 * FUJITSU Extended Socket Network Device driver
3 * Copyright (c) 2015 FUJITSU LIMITED
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * The full GNU General Public License is included in this distribution in
18 * the file called "COPYING".
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/types.h>
24#include <linux/nls.h>
25#include <linux/platform_device.h>
Taku Izumi2fcbca62015-08-21 17:29:20 +090026#include <linux/netdevice.h>
Taku Izumie5d486d2015-08-21 17:29:23 +090027#include <linux/interrupt.h>
Taku Izumi658d4392015-08-21 17:29:17 +090028
29#include "fjes.h"
30
31#define MAJ 1
Taku Izumi8f180fa2016-04-15 11:25:52 +090032#define MIN 1
Taku Izumi658d4392015-08-21 17:29:17 +090033#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN)
34#define DRV_NAME "fjes"
35char fjes_driver_name[] = DRV_NAME;
36char fjes_driver_version[] = DRV_VERSION;
37static const char fjes_driver_string[] =
38 "FUJITSU Extended Socket Network Device Driver";
39static const char fjes_copyright[] =
40 "Copyright (c) 2015 FUJITSU LIMITED";
41
42MODULE_AUTHOR("Taku Izumi <izumi.taku@jp.fujitsu.com>");
43MODULE_DESCRIPTION("FUJITSU Extended Socket Network Device Driver");
44MODULE_LICENSE("GPL");
45MODULE_VERSION(DRV_VERSION);
46
Taku Izumie5d486d2015-08-21 17:29:23 +090047static int fjes_request_irq(struct fjes_adapter *);
48static void fjes_free_irq(struct fjes_adapter *);
49
50static int fjes_open(struct net_device *);
51static int fjes_close(struct net_device *);
52static int fjes_setup_resources(struct fjes_adapter *);
53static void fjes_free_resources(struct fjes_adapter *);
Taku Izumi9acf51c2015-08-21 17:29:24 +090054static netdev_tx_t fjes_xmit_frame(struct sk_buff *, struct net_device *);
Taku Izumib772b9d2015-08-21 17:29:25 +090055static void fjes_raise_intr_rxdata_task(struct work_struct *);
Taku Izumiac63b942015-08-21 17:29:26 +090056static void fjes_tx_stall_task(struct work_struct *);
Taku Izumiff5b4212015-08-21 17:29:33 +090057static void fjes_force_close_task(struct work_struct *);
Taku Izumie5d486d2015-08-21 17:29:23 +090058static irqreturn_t fjes_intr(int, void*);
Taku Izumi879bc9a2015-08-21 17:29:28 +090059static struct rtnl_link_stats64 *
60fjes_get_stats64(struct net_device *, struct rtnl_link_stats64 *);
Taku Izumib9e23a62015-08-21 17:29:29 +090061static int fjes_change_mtu(struct net_device *, int);
Taku Izumi3e3fedd2015-08-21 17:29:31 +090062static int fjes_vlan_rx_add_vid(struct net_device *, __be16 proto, u16);
63static int fjes_vlan_rx_kill_vid(struct net_device *, __be16 proto, u16);
Taku Izumi4393e762015-08-21 17:29:30 +090064static void fjes_tx_retry(struct net_device *);
Taku Izumie5d486d2015-08-21 17:29:23 +090065
Taku Izumi658d4392015-08-21 17:29:17 +090066static int fjes_acpi_add(struct acpi_device *);
67static int fjes_acpi_remove(struct acpi_device *);
68static acpi_status fjes_get_acpi_resource(struct acpi_resource *, void*);
69
70static int fjes_probe(struct platform_device *);
71static int fjes_remove(struct platform_device *);
72
Taku Izumi2fcbca62015-08-21 17:29:20 +090073static int fjes_sw_init(struct fjes_adapter *);
74static void fjes_netdev_setup(struct net_device *);
Taku Izumi8edb62a2015-08-21 17:29:32 +090075static void fjes_irq_watch_task(struct work_struct *);
Taku Izumi8fc4cad2015-08-21 17:29:34 +090076static void fjes_watch_unshare_task(struct work_struct *);
Taku Izumi26585932015-08-21 17:29:27 +090077static void fjes_rx_irq(struct fjes_adapter *, int);
78static int fjes_poll(struct napi_struct *, int);
79
Taku Izumi658d4392015-08-21 17:29:17 +090080static const struct acpi_device_id fjes_acpi_ids[] = {
81 {"PNP0C02", 0},
82 {"", 0},
83};
84MODULE_DEVICE_TABLE(acpi, fjes_acpi_ids);
85
86static struct acpi_driver fjes_acpi_driver = {
87 .name = DRV_NAME,
88 .class = DRV_NAME,
89 .owner = THIS_MODULE,
90 .ids = fjes_acpi_ids,
91 .ops = {
92 .add = fjes_acpi_add,
93 .remove = fjes_acpi_remove,
94 },
95};
96
97static struct platform_driver fjes_driver = {
98 .driver = {
99 .name = DRV_NAME,
Taku Izumi658d4392015-08-21 17:29:17 +0900100 },
101 .probe = fjes_probe,
102 .remove = fjes_remove,
103};
104
105static struct resource fjes_resource[] = {
106 {
107 .flags = IORESOURCE_MEM,
108 .start = 0,
109 .end = 0,
110 },
111 {
112 .flags = IORESOURCE_IRQ,
113 .start = 0,
114 .end = 0,
115 },
116};
117
118static int fjes_acpi_add(struct acpi_device *device)
119{
120 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
121 char str_buf[sizeof(FJES_ACPI_SYMBOL) + 1];
122 struct platform_device *plat_dev;
123 union acpi_object *str;
124 acpi_status status;
125 int result;
126
127 status = acpi_evaluate_object(device->handle, "_STR", NULL, &buffer);
128 if (ACPI_FAILURE(status))
129 return -ENODEV;
130
131 str = buffer.pointer;
132 result = utf16s_to_utf8s((wchar_t *)str->string.pointer,
133 str->string.length, UTF16_LITTLE_ENDIAN,
134 str_buf, sizeof(str_buf) - 1);
135 str_buf[result] = 0;
136
137 if (strncmp(FJES_ACPI_SYMBOL, str_buf, strlen(FJES_ACPI_SYMBOL)) != 0) {
138 kfree(buffer.pointer);
139 return -ENODEV;
140 }
141 kfree(buffer.pointer);
142
143 status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
144 fjes_get_acpi_resource, fjes_resource);
145 if (ACPI_FAILURE(status))
146 return -ENODEV;
147
148 /* create platform_device */
149 plat_dev = platform_device_register_simple(DRV_NAME, 0, fjes_resource,
150 ARRAY_SIZE(fjes_resource));
151 device->driver_data = plat_dev;
152
153 return 0;
154}
155
156static int fjes_acpi_remove(struct acpi_device *device)
157{
158 struct platform_device *plat_dev;
159
160 plat_dev = (struct platform_device *)acpi_driver_data(device);
161 platform_device_unregister(plat_dev);
162
163 return 0;
164}
165
166static acpi_status
167fjes_get_acpi_resource(struct acpi_resource *acpi_res, void *data)
168{
169 struct acpi_resource_address32 *addr;
170 struct acpi_resource_irq *irq;
171 struct resource *res = data;
172
173 switch (acpi_res->type) {
174 case ACPI_RESOURCE_TYPE_ADDRESS32:
175 addr = &acpi_res->data.address32;
176 res[0].start = addr->address.minimum;
177 res[0].end = addr->address.minimum +
178 addr->address.address_length - 1;
179 break;
180
181 case ACPI_RESOURCE_TYPE_IRQ:
182 irq = &acpi_res->data.irq;
183 if (irq->interrupt_count != 1)
184 return AE_ERROR;
185 res[1].start = irq->interrupts[0];
186 res[1].end = irq->interrupts[0];
187 break;
188
189 default:
190 break;
191 }
192
193 return AE_OK;
194}
195
Taku Izumie5d486d2015-08-21 17:29:23 +0900196static int fjes_request_irq(struct fjes_adapter *adapter)
197{
198 struct net_device *netdev = adapter->netdev;
199 int result = -1;
200
Taku Izumi8edb62a2015-08-21 17:29:32 +0900201 adapter->interrupt_watch_enable = true;
202 if (!delayed_work_pending(&adapter->interrupt_watch_task)) {
203 queue_delayed_work(adapter->control_wq,
204 &adapter->interrupt_watch_task,
205 FJES_IRQ_WATCH_DELAY);
206 }
207
Taku Izumie5d486d2015-08-21 17:29:23 +0900208 if (!adapter->irq_registered) {
209 result = request_irq(adapter->hw.hw_res.irq, fjes_intr,
210 IRQF_SHARED, netdev->name, adapter);
211 if (result)
212 adapter->irq_registered = false;
213 else
214 adapter->irq_registered = true;
215 }
216
217 return result;
218}
219
220static void fjes_free_irq(struct fjes_adapter *adapter)
221{
222 struct fjes_hw *hw = &adapter->hw;
223
Taku Izumi8edb62a2015-08-21 17:29:32 +0900224 adapter->interrupt_watch_enable = false;
225 cancel_delayed_work_sync(&adapter->interrupt_watch_task);
226
Taku Izumie5d486d2015-08-21 17:29:23 +0900227 fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, true);
228
229 if (adapter->irq_registered) {
230 free_irq(adapter->hw.hw_res.irq, adapter);
231 adapter->irq_registered = false;
232 }
233}
234
Taku Izumi2fcbca62015-08-21 17:29:20 +0900235static const struct net_device_ops fjes_netdev_ops = {
Taku Izumie5d486d2015-08-21 17:29:23 +0900236 .ndo_open = fjes_open,
237 .ndo_stop = fjes_close,
Taku Izumi9acf51c2015-08-21 17:29:24 +0900238 .ndo_start_xmit = fjes_xmit_frame,
Taku Izumi879bc9a2015-08-21 17:29:28 +0900239 .ndo_get_stats64 = fjes_get_stats64,
Taku Izumib9e23a62015-08-21 17:29:29 +0900240 .ndo_change_mtu = fjes_change_mtu,
Taku Izumi4393e762015-08-21 17:29:30 +0900241 .ndo_tx_timeout = fjes_tx_retry,
Taku Izumi3e3fedd2015-08-21 17:29:31 +0900242 .ndo_vlan_rx_add_vid = fjes_vlan_rx_add_vid,
243 .ndo_vlan_rx_kill_vid = fjes_vlan_rx_kill_vid,
Taku Izumi2fcbca62015-08-21 17:29:20 +0900244};
245
Taku Izumie5d486d2015-08-21 17:29:23 +0900246/* fjes_open - Called when a network interface is made active */
247static int fjes_open(struct net_device *netdev)
248{
249 struct fjes_adapter *adapter = netdev_priv(netdev);
250 struct fjes_hw *hw = &adapter->hw;
251 int result;
252
253 if (adapter->open_guard)
254 return -ENXIO;
255
256 result = fjes_setup_resources(adapter);
257 if (result)
258 goto err_setup_res;
259
260 hw->txrx_stop_req_bit = 0;
261 hw->epstop_req_bit = 0;
262
Taku Izumi26585932015-08-21 17:29:27 +0900263 napi_enable(&adapter->napi);
264
Taku Izumie5d486d2015-08-21 17:29:23 +0900265 fjes_hw_capture_interrupt_status(hw);
266
267 result = fjes_request_irq(adapter);
268 if (result)
269 goto err_req_irq;
270
271 fjes_hw_set_irqmask(hw, REG_ICTL_MASK_ALL, false);
272
273 netif_tx_start_all_queues(netdev);
274 netif_carrier_on(netdev);
275
276 return 0;
277
278err_req_irq:
279 fjes_free_irq(adapter);
Taku Izumi26585932015-08-21 17:29:27 +0900280 napi_disable(&adapter->napi);
Taku Izumie5d486d2015-08-21 17:29:23 +0900281
282err_setup_res:
283 fjes_free_resources(adapter);
284 return result;
285}
286
287/* fjes_close - Disables a network interface */
288static int fjes_close(struct net_device *netdev)
289{
290 struct fjes_adapter *adapter = netdev_priv(netdev);
291 struct fjes_hw *hw = &adapter->hw;
Taku Izumibd5a2562016-04-15 11:25:46 +0900292 unsigned long flags;
Taku Izumie5d486d2015-08-21 17:29:23 +0900293 int epidx;
294
295 netif_tx_stop_all_queues(netdev);
296 netif_carrier_off(netdev);
297
298 fjes_hw_raise_epstop(hw);
299
Taku Izumi26585932015-08-21 17:29:27 +0900300 napi_disable(&adapter->napi);
301
Taku Izumibd5a2562016-04-15 11:25:46 +0900302 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumie5d486d2015-08-21 17:29:23 +0900303 for (epidx = 0; epidx < hw->max_epid; epidx++) {
304 if (epidx == hw->my_epid)
305 continue;
306
Taku Izumibd5a2562016-04-15 11:25:46 +0900307 if (fjes_hw_get_partner_ep_status(hw, epidx) ==
308 EP_PARTNER_SHARED)
309 adapter->hw.ep_shm_info[epidx]
310 .tx.info->v1i.rx_status &=
311 ~FJES_RX_POLL_WORK;
Taku Izumie5d486d2015-08-21 17:29:23 +0900312 }
Taku Izumibd5a2562016-04-15 11:25:46 +0900313 spin_unlock_irqrestore(&hw->rx_status_lock, flags);
Taku Izumie5d486d2015-08-21 17:29:23 +0900314
315 fjes_free_irq(adapter);
316
Taku Izumi8edb62a2015-08-21 17:29:32 +0900317 cancel_delayed_work_sync(&adapter->interrupt_watch_task);
Taku Izumi8fc4cad2015-08-21 17:29:34 +0900318 cancel_work_sync(&adapter->unshare_watch_task);
319 adapter->unshare_watch_bitmask = 0;
Taku Izumib772b9d2015-08-21 17:29:25 +0900320 cancel_work_sync(&adapter->raise_intr_rxdata_task);
Taku Izumiac63b942015-08-21 17:29:26 +0900321 cancel_work_sync(&adapter->tx_stall_task);
Taku Izumib772b9d2015-08-21 17:29:25 +0900322
Taku Izumi785f28e2015-08-21 17:29:35 +0900323 cancel_work_sync(&hw->update_zone_task);
Taku Izumib5a91522015-08-21 17:29:36 +0900324 cancel_work_sync(&hw->epstop_task);
Taku Izumi785f28e2015-08-21 17:29:35 +0900325
Taku Izumie5d486d2015-08-21 17:29:23 +0900326 fjes_hw_wait_epstop(hw);
327
328 fjes_free_resources(adapter);
329
330 return 0;
331}
332
333static int fjes_setup_resources(struct fjes_adapter *adapter)
334{
335 struct net_device *netdev = adapter->netdev;
336 struct ep_share_mem_info *buf_pair;
337 struct fjes_hw *hw = &adapter->hw;
Taku Izumibd5a2562016-04-15 11:25:46 +0900338 unsigned long flags;
Taku Izumie5d486d2015-08-21 17:29:23 +0900339 int result;
340 int epidx;
341
342 mutex_lock(&hw->hw_info.lock);
343 result = fjes_hw_request_info(hw);
344 switch (result) {
345 case 0:
346 for (epidx = 0; epidx < hw->max_epid; epidx++) {
347 hw->ep_shm_info[epidx].es_status =
348 hw->hw_info.res_buf->info.info[epidx].es_status;
349 hw->ep_shm_info[epidx].zone =
350 hw->hw_info.res_buf->info.info[epidx].zone;
351 }
352 break;
353 default:
354 case -ENOMSG:
355 case -EBUSY:
356 adapter->force_reset = true;
357
358 mutex_unlock(&hw->hw_info.lock);
359 return result;
360 }
361 mutex_unlock(&hw->hw_info.lock);
362
363 for (epidx = 0; epidx < (hw->max_epid); epidx++) {
364 if ((epidx != hw->my_epid) &&
365 (hw->ep_shm_info[epidx].es_status ==
366 FJES_ZONING_STATUS_ENABLE)) {
367 fjes_hw_raise_interrupt(hw, epidx,
368 REG_ICTL_MASK_INFO_UPDATE);
369 }
370 }
371
372 msleep(FJES_OPEN_ZONE_UPDATE_WAIT * hw->max_epid);
373
374 for (epidx = 0; epidx < (hw->max_epid); epidx++) {
375 if (epidx == hw->my_epid)
376 continue;
377
378 buf_pair = &hw->ep_shm_info[epidx];
379
Taku Izumibd5a2562016-04-15 11:25:46 +0900380 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumie5d486d2015-08-21 17:29:23 +0900381 fjes_hw_setup_epbuf(&buf_pair->tx, netdev->dev_addr,
382 netdev->mtu);
Taku Izumibd5a2562016-04-15 11:25:46 +0900383 spin_unlock_irqrestore(&hw->rx_status_lock, flags);
Taku Izumie5d486d2015-08-21 17:29:23 +0900384
385 if (fjes_hw_epid_is_same_zone(hw, epidx)) {
386 mutex_lock(&hw->hw_info.lock);
387 result =
388 fjes_hw_register_buff_addr(hw, epidx, buf_pair);
389 mutex_unlock(&hw->hw_info.lock);
390
391 switch (result) {
392 case 0:
393 break;
394 case -ENOMSG:
395 case -EBUSY:
396 default:
397 adapter->force_reset = true;
398 return result;
399 }
400 }
401 }
402
403 return 0;
404}
405
406static void fjes_free_resources(struct fjes_adapter *adapter)
407{
408 struct net_device *netdev = adapter->netdev;
409 struct fjes_device_command_param param;
410 struct ep_share_mem_info *buf_pair;
411 struct fjes_hw *hw = &adapter->hw;
412 bool reset_flag = false;
Taku Izumibd5a2562016-04-15 11:25:46 +0900413 unsigned long flags;
Taku Izumie5d486d2015-08-21 17:29:23 +0900414 int result;
415 int epidx;
416
417 for (epidx = 0; epidx < hw->max_epid; epidx++) {
418 if (epidx == hw->my_epid)
419 continue;
420
421 mutex_lock(&hw->hw_info.lock);
422 result = fjes_hw_unregister_buff_addr(hw, epidx);
423 mutex_unlock(&hw->hw_info.lock);
424
425 if (result)
426 reset_flag = true;
427
428 buf_pair = &hw->ep_shm_info[epidx];
429
Taku Izumibd5a2562016-04-15 11:25:46 +0900430 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumie5d486d2015-08-21 17:29:23 +0900431 fjes_hw_setup_epbuf(&buf_pair->tx,
432 netdev->dev_addr, netdev->mtu);
Taku Izumibd5a2562016-04-15 11:25:46 +0900433 spin_unlock_irqrestore(&hw->rx_status_lock, flags);
Taku Izumie5d486d2015-08-21 17:29:23 +0900434
435 clear_bit(epidx, &hw->txrx_stop_req_bit);
436 }
437
438 if (reset_flag || adapter->force_reset) {
439 result = fjes_hw_reset(hw);
440
441 adapter->force_reset = false;
442
443 if (result)
444 adapter->open_guard = true;
445
446 hw->hw_info.buffer_share_bit = 0;
447
448 memset((void *)&param, 0, sizeof(param));
449
450 param.req_len = hw->hw_info.req_buf_size;
451 param.req_start = __pa(hw->hw_info.req_buf);
452 param.res_len = hw->hw_info.res_buf_size;
453 param.res_start = __pa(hw->hw_info.res_buf);
454 param.share_start = __pa(hw->hw_info.share->ep_status);
455
456 fjes_hw_init_command_registers(hw, &param);
457 }
458}
459
Taku Izumiac63b942015-08-21 17:29:26 +0900460static void fjes_tx_stall_task(struct work_struct *work)
461{
462 struct fjes_adapter *adapter = container_of(work,
463 struct fjes_adapter, tx_stall_task);
464 struct net_device *netdev = adapter->netdev;
465 struct fjes_hw *hw = &adapter->hw;
466 int all_queue_available, sendable;
467 enum ep_partner_status pstatus;
468 int max_epid, my_epid, epid;
469 union ep_buffer_info *info;
470 int i;
471
472 if (((long)jiffies -
Florian Westphal4d0e9652016-05-03 16:30:59 +0200473 dev_trans_start(netdev)) > FJES_TX_TX_STALL_TIMEOUT) {
Taku Izumiac63b942015-08-21 17:29:26 +0900474 netif_wake_queue(netdev);
475 return;
476 }
477
478 my_epid = hw->my_epid;
479 max_epid = hw->max_epid;
480
481 for (i = 0; i < 5; i++) {
482 all_queue_available = 1;
483
484 for (epid = 0; epid < max_epid; epid++) {
485 if (my_epid == epid)
486 continue;
487
488 pstatus = fjes_hw_get_partner_ep_status(hw, epid);
489 sendable = (pstatus == EP_PARTNER_SHARED);
490 if (!sendable)
491 continue;
492
493 info = adapter->hw.ep_shm_info[epid].tx.info;
494
Taku Izumi16bbec32016-04-15 11:25:40 +0900495 if (!(info->v1i.rx_status & FJES_RX_MTU_CHANGING_DONE))
496 return;
497
Taku Izumiac63b942015-08-21 17:29:26 +0900498 if (EP_RING_FULL(info->v1i.head, info->v1i.tail,
499 info->v1i.count_max)) {
500 all_queue_available = 0;
501 break;
502 }
503 }
504
505 if (all_queue_available) {
506 netif_wake_queue(netdev);
507 return;
508 }
509 }
510
511 usleep_range(50, 100);
512
513 queue_work(adapter->txrx_wq, &adapter->tx_stall_task);
514}
515
Taku Izumiff5b4212015-08-21 17:29:33 +0900516static void fjes_force_close_task(struct work_struct *work)
517{
518 struct fjes_adapter *adapter = container_of(work,
519 struct fjes_adapter, force_close_task);
520 struct net_device *netdev = adapter->netdev;
521
522 rtnl_lock();
523 dev_close(netdev);
524 rtnl_unlock();
525}
526
Taku Izumib772b9d2015-08-21 17:29:25 +0900527static void fjes_raise_intr_rxdata_task(struct work_struct *work)
528{
529 struct fjes_adapter *adapter = container_of(work,
530 struct fjes_adapter, raise_intr_rxdata_task);
531 struct fjes_hw *hw = &adapter->hw;
532 enum ep_partner_status pstatus;
533 int max_epid, my_epid, epid;
534
535 my_epid = hw->my_epid;
536 max_epid = hw->max_epid;
537
538 for (epid = 0; epid < max_epid; epid++)
539 hw->ep_shm_info[epid].tx_status_work = 0;
540
541 for (epid = 0; epid < max_epid; epid++) {
542 if (epid == my_epid)
543 continue;
544
545 pstatus = fjes_hw_get_partner_ep_status(hw, epid);
546 if (pstatus == EP_PARTNER_SHARED) {
547 hw->ep_shm_info[epid].tx_status_work =
548 hw->ep_shm_info[epid].tx.info->v1i.tx_status;
549
550 if (hw->ep_shm_info[epid].tx_status_work ==
551 FJES_TX_DELAY_SEND_PENDING) {
552 hw->ep_shm_info[epid].tx.info->v1i.tx_status =
553 FJES_TX_DELAY_SEND_NONE;
554 }
555 }
556 }
557
558 for (epid = 0; epid < max_epid; epid++) {
559 if (epid == my_epid)
560 continue;
561
562 pstatus = fjes_hw_get_partner_ep_status(hw, epid);
563 if ((hw->ep_shm_info[epid].tx_status_work ==
564 FJES_TX_DELAY_SEND_PENDING) &&
565 (pstatus == EP_PARTNER_SHARED) &&
Taku Izumi19a0a7f2016-04-15 11:25:34 +0900566 !(hw->ep_shm_info[epid].rx.info->v1i.rx_status &
567 FJES_RX_POLL_WORK)) {
Taku Izumib772b9d2015-08-21 17:29:25 +0900568 fjes_hw_raise_interrupt(hw, epid,
569 REG_ICTL_MASK_RX_DATA);
570 }
571 }
572
573 usleep_range(500, 1000);
574}
575
Taku Izumi9acf51c2015-08-21 17:29:24 +0900576static int fjes_tx_send(struct fjes_adapter *adapter, int dest,
577 void *data, size_t len)
578{
579 int retval;
580
581 retval = fjes_hw_epbuf_tx_pkt_send(&adapter->hw.ep_shm_info[dest].tx,
582 data, len);
583 if (retval)
584 return retval;
585
586 adapter->hw.ep_shm_info[dest].tx.info->v1i.tx_status =
587 FJES_TX_DELAY_SEND_PENDING;
Taku Izumib772b9d2015-08-21 17:29:25 +0900588 if (!work_pending(&adapter->raise_intr_rxdata_task))
589 queue_work(adapter->txrx_wq,
590 &adapter->raise_intr_rxdata_task);
Taku Izumi9acf51c2015-08-21 17:29:24 +0900591
592 retval = 0;
593 return retval;
594}
595
596static netdev_tx_t
597fjes_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
598{
599 struct fjes_adapter *adapter = netdev_priv(netdev);
600 struct fjes_hw *hw = &adapter->hw;
601
602 int max_epid, my_epid, dest_epid;
603 enum ep_partner_status pstatus;
604 struct netdev_queue *cur_queue;
605 char shortpkt[VLAN_ETH_HLEN];
606 bool is_multi, vlan;
607 struct ethhdr *eth;
608 u16 queue_no = 0;
609 u16 vlan_id = 0;
610 netdev_tx_t ret;
611 char *data;
612 int len;
613
614 ret = NETDEV_TX_OK;
615 is_multi = false;
616 cur_queue = netdev_get_tx_queue(netdev, queue_no);
617
618 eth = (struct ethhdr *)skb->data;
619 my_epid = hw->my_epid;
620
621 vlan = (vlan_get_tag(skb, &vlan_id) == 0) ? true : false;
622
623 data = skb->data;
624 len = skb->len;
625
626 if (is_multicast_ether_addr(eth->h_dest)) {
627 dest_epid = 0;
628 max_epid = hw->max_epid;
629 is_multi = true;
630 } else if (is_local_ether_addr(eth->h_dest)) {
631 dest_epid = eth->h_dest[ETH_ALEN - 1];
632 max_epid = dest_epid + 1;
633
634 if ((eth->h_dest[0] == 0x02) &&
635 (0x00 == (eth->h_dest[1] | eth->h_dest[2] |
636 eth->h_dest[3] | eth->h_dest[4])) &&
637 (dest_epid < hw->max_epid)) {
638 ;
639 } else {
640 dest_epid = 0;
641 max_epid = 0;
642 ret = NETDEV_TX_OK;
643
644 adapter->stats64.tx_packets += 1;
645 hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
646 adapter->stats64.tx_bytes += len;
647 hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
648 }
649 } else {
650 dest_epid = 0;
651 max_epid = 0;
652 ret = NETDEV_TX_OK;
653
654 adapter->stats64.tx_packets += 1;
655 hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
656 adapter->stats64.tx_bytes += len;
657 hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
658 }
659
660 for (; dest_epid < max_epid; dest_epid++) {
661 if (my_epid == dest_epid)
662 continue;
663
664 pstatus = fjes_hw_get_partner_ep_status(hw, dest_epid);
665 if (pstatus != EP_PARTNER_SHARED) {
666 ret = NETDEV_TX_OK;
667 } else if (!fjes_hw_check_epbuf_version(
668 &adapter->hw.ep_shm_info[dest_epid].rx, 0)) {
669 /* version is NOT 0 */
670 adapter->stats64.tx_carrier_errors += 1;
Taku Izumi3c3bd4a2016-04-15 11:25:27 +0900671 hw->ep_shm_info[dest_epid].net_stats
Taku Izumi9acf51c2015-08-21 17:29:24 +0900672 .tx_carrier_errors += 1;
673
674 ret = NETDEV_TX_OK;
675 } else if (!fjes_hw_check_mtu(
676 &adapter->hw.ep_shm_info[dest_epid].rx,
677 netdev->mtu)) {
678 adapter->stats64.tx_dropped += 1;
Taku Izumi3c3bd4a2016-04-15 11:25:27 +0900679 hw->ep_shm_info[dest_epid].net_stats.tx_dropped += 1;
Taku Izumi9acf51c2015-08-21 17:29:24 +0900680 adapter->stats64.tx_errors += 1;
Taku Izumi3c3bd4a2016-04-15 11:25:27 +0900681 hw->ep_shm_info[dest_epid].net_stats.tx_errors += 1;
Taku Izumi9acf51c2015-08-21 17:29:24 +0900682
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 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;
Taku Izumi3c3bd4a2016-04-15 11:25:27 +0900712 hw->ep_shm_info[dest_epid].net_stats
Taku Izumi9acf51c2015-08-21 17:29:24 +0900713 .tx_fifo_errors += 1;
714 adapter->stats64.tx_errors += 1;
Taku Izumi3c3bd4a2016-04-15 11:25:27 +0900715 hw->ep_shm_info[dest_epid].net_stats
Taku Izumi9acf51c2015-08-21 17:29:24 +0900716 .tx_errors += 1;
717
718 ret = NETDEV_TX_OK;
719 } else {
Florian Westphal860e9532016-05-03 16:33:13 +0200720 netif_trans_update(netdev);
Taku Izumi9acf51c2015-08-21 17:29:24 +0900721 netif_tx_stop_queue(cur_queue);
722
Taku Izumiac63b942015-08-21 17:29:26 +0900723 if (!work_pending(&adapter->tx_stall_task))
724 queue_work(adapter->txrx_wq,
725 &adapter->tx_stall_task);
726
Taku Izumi9acf51c2015-08-21 17:29:24 +0900727 ret = NETDEV_TX_BUSY;
728 }
729 } else {
730 if (!is_multi) {
731 adapter->stats64.tx_packets += 1;
Taku Izumi3c3bd4a2016-04-15 11:25:27 +0900732 hw->ep_shm_info[dest_epid].net_stats
Taku Izumi9acf51c2015-08-21 17:29:24 +0900733 .tx_packets += 1;
734 adapter->stats64.tx_bytes += len;
Taku Izumi3c3bd4a2016-04-15 11:25:27 +0900735 hw->ep_shm_info[dest_epid].net_stats
Taku Izumi9acf51c2015-08-21 17:29:24 +0900736 .tx_bytes += len;
737 }
738
739 adapter->tx_retry_count = 0;
740 ret = NETDEV_TX_OK;
741 }
742 }
743 }
744
745 if (ret == NETDEV_TX_OK) {
746 dev_kfree_skb(skb);
747 if (is_multi) {
748 adapter->stats64.tx_packets += 1;
749 hw->ep_shm_info[my_epid].net_stats.tx_packets += 1;
750 adapter->stats64.tx_bytes += 1;
751 hw->ep_shm_info[my_epid].net_stats.tx_bytes += len;
752 }
753 }
754
755 return ret;
756}
757
Taku Izumi4393e762015-08-21 17:29:30 +0900758static void fjes_tx_retry(struct net_device *netdev)
759{
760 struct netdev_queue *queue = netdev_get_tx_queue(netdev, 0);
761
762 netif_tx_wake_queue(queue);
763}
764
Taku Izumi879bc9a2015-08-21 17:29:28 +0900765static struct rtnl_link_stats64 *
766fjes_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
767{
768 struct fjes_adapter *adapter = netdev_priv(netdev);
769
770 memcpy(stats, &adapter->stats64, sizeof(struct rtnl_link_stats64));
771
772 return stats;
773}
774
Taku Izumib9e23a62015-08-21 17:29:29 +0900775static int fjes_change_mtu(struct net_device *netdev, int new_mtu)
776{
Taku Izumi16bbec32016-04-15 11:25:40 +0900777 struct fjes_adapter *adapter = netdev_priv(netdev);
Taku Izumib9e23a62015-08-21 17:29:29 +0900778 bool running = netif_running(netdev);
Taku Izumi16bbec32016-04-15 11:25:40 +0900779 struct fjes_hw *hw = &adapter->hw;
Taku Izumibd5a2562016-04-15 11:25:46 +0900780 unsigned long flags;
Taku Izumi16bbec32016-04-15 11:25:40 +0900781 int ret = -EINVAL;
782 int idx, epidx;
Taku Izumib9e23a62015-08-21 17:29:29 +0900783
784 for (idx = 0; fjes_support_mtu[idx] != 0; idx++) {
785 if (new_mtu <= fjes_support_mtu[idx]) {
786 new_mtu = fjes_support_mtu[idx];
787 if (new_mtu == netdev->mtu)
788 return 0;
789
Taku Izumi16bbec32016-04-15 11:25:40 +0900790 ret = 0;
791 break;
Taku Izumib9e23a62015-08-21 17:29:29 +0900792 }
793 }
794
Taku Izumi16bbec32016-04-15 11:25:40 +0900795 if (ret)
796 return ret;
797
798 if (running) {
Taku Izumibd5a2562016-04-15 11:25:46 +0900799 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumi16bbec32016-04-15 11:25:40 +0900800 for (epidx = 0; epidx < hw->max_epid; epidx++) {
801 if (epidx == hw->my_epid)
802 continue;
803 hw->ep_shm_info[epidx].tx.info->v1i.rx_status &=
804 ~FJES_RX_MTU_CHANGING_DONE;
805 }
Taku Izumibd5a2562016-04-15 11:25:46 +0900806 spin_unlock_irqrestore(&hw->rx_status_lock, flags);
807
Taku Izumi16bbec32016-04-15 11:25:40 +0900808 netif_tx_stop_all_queues(netdev);
809 netif_carrier_off(netdev);
810 cancel_work_sync(&adapter->tx_stall_task);
811 napi_disable(&adapter->napi);
812
813 msleep(1000);
814
815 netif_tx_stop_all_queues(netdev);
816 }
817
818 netdev->mtu = new_mtu;
819
820 if (running) {
821 for (epidx = 0; epidx < hw->max_epid; epidx++) {
822 if (epidx == hw->my_epid)
823 continue;
824
Taku Izumibd5a2562016-04-15 11:25:46 +0900825 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumi16bbec32016-04-15 11:25:40 +0900826 fjes_hw_setup_epbuf(&hw->ep_shm_info[epidx].tx,
827 netdev->dev_addr,
828 netdev->mtu);
Taku Izumi16bbec32016-04-15 11:25:40 +0900829
830 hw->ep_shm_info[epidx].tx.info->v1i.rx_status |=
831 FJES_RX_MTU_CHANGING_DONE;
Taku Izumibd5a2562016-04-15 11:25:46 +0900832 spin_unlock_irqrestore(&hw->rx_status_lock, flags);
Taku Izumi16bbec32016-04-15 11:25:40 +0900833 }
834
835 netif_tx_wake_all_queues(netdev);
836 netif_carrier_on(netdev);
837 napi_enable(&adapter->napi);
Taku Izumibd5a2562016-04-15 11:25:46 +0900838 napi_schedule(&adapter->napi);
Taku Izumi16bbec32016-04-15 11:25:40 +0900839 }
840
841 return ret;
Taku Izumib9e23a62015-08-21 17:29:29 +0900842}
843
Taku Izumi3e3fedd2015-08-21 17:29:31 +0900844static 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
864static 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
Taku Izumicb79eaa2015-08-21 17:29:37 +0900880static void fjes_txrx_stop_req_irq(struct fjes_adapter *adapter,
881 int src_epid)
882{
883 struct fjes_hw *hw = &adapter->hw;
884 enum ep_partner_status status;
Taku Izumibd5a2562016-04-15 11:25:46 +0900885 unsigned long flags;
Taku Izumicb79eaa2015-08-21 17:29:37 +0900886
887 status = fjes_hw_get_partner_ep_status(hw, src_epid);
888 switch (status) {
889 case EP_PARTNER_UNSHARE:
890 case EP_PARTNER_COMPLETE:
891 default:
892 break;
893 case EP_PARTNER_WAITING:
894 if (src_epid < hw->my_epid) {
Taku Izumibd5a2562016-04-15 11:25:46 +0900895 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumicb79eaa2015-08-21 17:29:37 +0900896 hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
897 FJES_RX_STOP_REQ_DONE;
Taku Izumibd5a2562016-04-15 11:25:46 +0900898 spin_unlock_irqrestore(&hw->rx_status_lock, flags);
Taku Izumicb79eaa2015-08-21 17:29:37 +0900899
900 clear_bit(src_epid, &hw->txrx_stop_req_bit);
901 set_bit(src_epid, &adapter->unshare_watch_bitmask);
902
903 if (!work_pending(&adapter->unshare_watch_task))
904 queue_work(adapter->control_wq,
905 &adapter->unshare_watch_task);
906 }
907 break;
908 case EP_PARTNER_SHARED:
909 if (hw->ep_shm_info[src_epid].rx.info->v1i.rx_status &
910 FJES_RX_STOP_REQ_REQUEST) {
911 set_bit(src_epid, &hw->epstop_req_bit);
912 if (!work_pending(&hw->epstop_task))
913 queue_work(adapter->control_wq,
914 &hw->epstop_task);
915 }
916 break;
917 }
918}
919
920static void fjes_stop_req_irq(struct fjes_adapter *adapter, int src_epid)
921{
922 struct fjes_hw *hw = &adapter->hw;
923 enum ep_partner_status status;
Taku Izumibd5a2562016-04-15 11:25:46 +0900924 unsigned long flags;
Taku Izumicb79eaa2015-08-21 17:29:37 +0900925
926 set_bit(src_epid, &hw->hw_info.buffer_unshare_reserve_bit);
927
928 status = fjes_hw_get_partner_ep_status(hw, src_epid);
929 switch (status) {
930 case EP_PARTNER_WAITING:
Taku Izumibd5a2562016-04-15 11:25:46 +0900931 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumicb79eaa2015-08-21 17:29:37 +0900932 hw->ep_shm_info[src_epid].tx.info->v1i.rx_status |=
933 FJES_RX_STOP_REQ_DONE;
Taku Izumibd5a2562016-04-15 11:25:46 +0900934 spin_unlock_irqrestore(&hw->rx_status_lock, flags);
Taku Izumicb79eaa2015-08-21 17:29:37 +0900935 clear_bit(src_epid, &hw->txrx_stop_req_bit);
936 /* fall through */
937 case EP_PARTNER_UNSHARE:
938 case EP_PARTNER_COMPLETE:
939 default:
940 set_bit(src_epid, &adapter->unshare_watch_bitmask);
941 if (!work_pending(&adapter->unshare_watch_task))
942 queue_work(adapter->control_wq,
943 &adapter->unshare_watch_task);
944 break;
945 case EP_PARTNER_SHARED:
946 set_bit(src_epid, &hw->epstop_req_bit);
947
948 if (!work_pending(&hw->epstop_task))
949 queue_work(adapter->control_wq, &hw->epstop_task);
950 break;
951 }
952}
953
Taku Izumi785f28e2015-08-21 17:29:35 +0900954static void fjes_update_zone_irq(struct fjes_adapter *adapter,
955 int src_epid)
956{
957 struct fjes_hw *hw = &adapter->hw;
958
959 if (!work_pending(&hw->update_zone_task))
960 queue_work(adapter->control_wq, &hw->update_zone_task);
961}
962
Taku Izumie5d486d2015-08-21 17:29:23 +0900963static irqreturn_t fjes_intr(int irq, void *data)
964{
965 struct fjes_adapter *adapter = data;
966 struct fjes_hw *hw = &adapter->hw;
967 irqreturn_t ret;
968 u32 icr;
969
970 icr = fjes_hw_capture_interrupt_status(hw);
971
Taku Izumi26585932015-08-21 17:29:27 +0900972 if (icr & REG_IS_MASK_IS_ASSERT) {
973 if (icr & REG_ICTL_MASK_RX_DATA)
974 fjes_rx_irq(adapter, icr & REG_IS_MASK_EPID);
975
Taku Izumicb79eaa2015-08-21 17:29:37 +0900976 if (icr & REG_ICTL_MASK_DEV_STOP_REQ)
977 fjes_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
978
979 if (icr & REG_ICTL_MASK_TXRX_STOP_REQ)
980 fjes_txrx_stop_req_irq(adapter, icr & REG_IS_MASK_EPID);
981
982 if (icr & REG_ICTL_MASK_TXRX_STOP_DONE)
983 fjes_hw_set_irqmask(hw,
984 REG_ICTL_MASK_TXRX_STOP_DONE, true);
985
Taku Izumi785f28e2015-08-21 17:29:35 +0900986 if (icr & REG_ICTL_MASK_INFO_UPDATE)
987 fjes_update_zone_irq(adapter, icr & REG_IS_MASK_EPID);
988
Taku Izumie5d486d2015-08-21 17:29:23 +0900989 ret = IRQ_HANDLED;
Taku Izumi26585932015-08-21 17:29:27 +0900990 } else {
Taku Izumie5d486d2015-08-21 17:29:23 +0900991 ret = IRQ_NONE;
Taku Izumi26585932015-08-21 17:29:27 +0900992 }
Taku Izumie5d486d2015-08-21 17:29:23 +0900993
994 return ret;
995}
996
Taku Izumi26585932015-08-21 17:29:27 +0900997static int fjes_rxframe_search_exist(struct fjes_adapter *adapter,
998 int start_epid)
999{
1000 struct fjes_hw *hw = &adapter->hw;
1001 enum ep_partner_status pstatus;
1002 int max_epid, cur_epid;
1003 int i;
1004
1005 max_epid = hw->max_epid;
1006 start_epid = (start_epid + 1 + max_epid) % max_epid;
1007
1008 for (i = 0; i < max_epid; i++) {
1009 cur_epid = (start_epid + i) % max_epid;
1010 if (cur_epid == hw->my_epid)
1011 continue;
1012
1013 pstatus = fjes_hw_get_partner_ep_status(hw, cur_epid);
1014 if (pstatus == EP_PARTNER_SHARED) {
1015 if (!fjes_hw_epbuf_rx_is_empty(
1016 &hw->ep_shm_info[cur_epid].rx))
1017 return cur_epid;
1018 }
1019 }
1020 return -1;
1021}
1022
1023static void *fjes_rxframe_get(struct fjes_adapter *adapter, size_t *psize,
1024 int *cur_epid)
1025{
1026 void *frame;
1027
1028 *cur_epid = fjes_rxframe_search_exist(adapter, *cur_epid);
1029 if (*cur_epid < 0)
1030 return NULL;
1031
1032 frame =
1033 fjes_hw_epbuf_rx_curpkt_get_addr(
1034 &adapter->hw.ep_shm_info[*cur_epid].rx, psize);
1035
1036 return frame;
1037}
1038
1039static void fjes_rxframe_release(struct fjes_adapter *adapter, int cur_epid)
1040{
1041 fjes_hw_epbuf_rx_curpkt_drop(&adapter->hw.ep_shm_info[cur_epid].rx);
1042}
1043
1044static void fjes_rx_irq(struct fjes_adapter *adapter, int src_epid)
1045{
1046 struct fjes_hw *hw = &adapter->hw;
1047
1048 fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, true);
1049
1050 adapter->unset_rx_last = true;
1051 napi_schedule(&adapter->napi);
1052}
1053
1054static int fjes_poll(struct napi_struct *napi, int budget)
1055{
1056 struct fjes_adapter *adapter =
1057 container_of(napi, struct fjes_adapter, napi);
1058 struct net_device *netdev = napi->dev;
1059 struct fjes_hw *hw = &adapter->hw;
1060 struct sk_buff *skb;
1061 int work_done = 0;
1062 int cur_epid = 0;
1063 int epidx;
1064 size_t frame_len;
1065 void *frame;
1066
Taku Izumibd5a2562016-04-15 11:25:46 +09001067 spin_lock(&hw->rx_status_lock);
Taku Izumi26585932015-08-21 17:29:27 +09001068 for (epidx = 0; epidx < hw->max_epid; epidx++) {
1069 if (epidx == hw->my_epid)
1070 continue;
1071
Taku Izumibd5a2562016-04-15 11:25:46 +09001072 if (fjes_hw_get_partner_ep_status(hw, epidx) ==
1073 EP_PARTNER_SHARED)
1074 adapter->hw.ep_shm_info[epidx]
1075 .tx.info->v1i.rx_status |= FJES_RX_POLL_WORK;
Taku Izumi26585932015-08-21 17:29:27 +09001076 }
Taku Izumibd5a2562016-04-15 11:25:46 +09001077 spin_unlock(&hw->rx_status_lock);
Taku Izumi26585932015-08-21 17:29:27 +09001078
1079 while (work_done < budget) {
1080 prefetch(&adapter->hw);
1081 frame = fjes_rxframe_get(adapter, &frame_len, &cur_epid);
1082
1083 if (frame) {
1084 skb = napi_alloc_skb(napi, frame_len);
1085 if (!skb) {
1086 adapter->stats64.rx_dropped += 1;
1087 hw->ep_shm_info[cur_epid].net_stats
1088 .rx_dropped += 1;
1089 adapter->stats64.rx_errors += 1;
1090 hw->ep_shm_info[cur_epid].net_stats
1091 .rx_errors += 1;
1092 } else {
1093 memcpy(skb_put(skb, frame_len),
1094 frame, frame_len);
1095 skb->protocol = eth_type_trans(skb, netdev);
1096 skb->ip_summed = CHECKSUM_UNNECESSARY;
1097
1098 netif_receive_skb(skb);
1099
1100 work_done++;
1101
1102 adapter->stats64.rx_packets += 1;
1103 hw->ep_shm_info[cur_epid].net_stats
1104 .rx_packets += 1;
1105 adapter->stats64.rx_bytes += frame_len;
1106 hw->ep_shm_info[cur_epid].net_stats
1107 .rx_bytes += frame_len;
1108
1109 if (is_multicast_ether_addr(
1110 ((struct ethhdr *)frame)->h_dest)) {
1111 adapter->stats64.multicast += 1;
1112 hw->ep_shm_info[cur_epid].net_stats
1113 .multicast += 1;
1114 }
1115 }
1116
1117 fjes_rxframe_release(adapter, cur_epid);
1118 adapter->unset_rx_last = true;
1119 } else {
1120 break;
1121 }
1122 }
1123
1124 if (work_done < budget) {
1125 napi_complete(napi);
1126
1127 if (adapter->unset_rx_last) {
1128 adapter->rx_last_jiffies = jiffies;
1129 adapter->unset_rx_last = false;
1130 }
1131
1132 if (((long)jiffies - (long)adapter->rx_last_jiffies) < 3) {
1133 napi_reschedule(napi);
1134 } else {
Taku Izumibd5a2562016-04-15 11:25:46 +09001135 spin_lock(&hw->rx_status_lock);
Taku Izumi26585932015-08-21 17:29:27 +09001136 for (epidx = 0; epidx < hw->max_epid; epidx++) {
1137 if (epidx == hw->my_epid)
1138 continue;
Taku Izumibd5a2562016-04-15 11:25:46 +09001139 if (fjes_hw_get_partner_ep_status(hw, epidx) ==
1140 EP_PARTNER_SHARED)
1141 adapter->hw.ep_shm_info[epidx].tx
1142 .info->v1i.rx_status &=
Taku Izumi26585932015-08-21 17:29:27 +09001143 ~FJES_RX_POLL_WORK;
1144 }
Taku Izumibd5a2562016-04-15 11:25:46 +09001145 spin_unlock(&hw->rx_status_lock);
Taku Izumi26585932015-08-21 17:29:27 +09001146
1147 fjes_hw_set_irqmask(hw, REG_ICTL_MASK_RX_DATA, false);
1148 }
1149 }
1150
1151 return work_done;
1152}
1153
Taku Izumi658d4392015-08-21 17:29:17 +09001154/* fjes_probe - Device Initialization Routine */
1155static int fjes_probe(struct platform_device *plat_dev)
1156{
Taku Izumi2fcbca62015-08-21 17:29:20 +09001157 struct fjes_adapter *adapter;
1158 struct net_device *netdev;
1159 struct resource *res;
1160 struct fjes_hw *hw;
1161 int err;
1162
1163 err = -ENOMEM;
1164 netdev = alloc_netdev_mq(sizeof(struct fjes_adapter), "es%d",
1165 NET_NAME_UNKNOWN, fjes_netdev_setup,
1166 FJES_MAX_QUEUES);
1167
1168 if (!netdev)
1169 goto err_out;
1170
1171 SET_NETDEV_DEV(netdev, &plat_dev->dev);
1172
1173 dev_set_drvdata(&plat_dev->dev, netdev);
1174 adapter = netdev_priv(netdev);
1175 adapter->netdev = netdev;
1176 adapter->plat_dev = plat_dev;
1177 hw = &adapter->hw;
1178 hw->back = adapter;
1179
1180 /* setup the private structure */
1181 err = fjes_sw_init(adapter);
1182 if (err)
1183 goto err_free_netdev;
1184
Taku Izumiff5b4212015-08-21 17:29:33 +09001185 INIT_WORK(&adapter->force_close_task, fjes_force_close_task);
Taku Izumi2fcbca62015-08-21 17:29:20 +09001186 adapter->force_reset = false;
1187 adapter->open_guard = false;
1188
Bhaktipriya Shridharf2edc4e2016-06-02 15:00:57 +05301189 adapter->txrx_wq = alloc_workqueue(DRV_NAME "/txrx", WQ_MEM_RECLAIM, 0);
1190 adapter->control_wq = alloc_workqueue(DRV_NAME "/control",
1191 WQ_MEM_RECLAIM, 0);
Taku Izumib772b9d2015-08-21 17:29:25 +09001192
Taku Izumiac63b942015-08-21 17:29:26 +09001193 INIT_WORK(&adapter->tx_stall_task, fjes_tx_stall_task);
Taku Izumib772b9d2015-08-21 17:29:25 +09001194 INIT_WORK(&adapter->raise_intr_rxdata_task,
1195 fjes_raise_intr_rxdata_task);
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001196 INIT_WORK(&adapter->unshare_watch_task, fjes_watch_unshare_task);
1197 adapter->unshare_watch_bitmask = 0;
Taku Izumib772b9d2015-08-21 17:29:25 +09001198
Taku Izumi8edb62a2015-08-21 17:29:32 +09001199 INIT_DELAYED_WORK(&adapter->interrupt_watch_task, fjes_irq_watch_task);
1200 adapter->interrupt_watch_enable = false;
1201
Taku Izumi2fcbca62015-08-21 17:29:20 +09001202 res = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
1203 hw->hw_res.start = res->start;
Vaishali Thakkare0897ae2016-04-11 15:58:17 +05301204 hw->hw_res.size = resource_size(res);
Taku Izumi2fcbca62015-08-21 17:29:20 +09001205 hw->hw_res.irq = platform_get_irq(plat_dev, 0);
1206 err = fjes_hw_init(&adapter->hw);
1207 if (err)
1208 goto err_free_netdev;
1209
1210 /* setup MAC address (02:00:00:00:00:[epid])*/
1211 netdev->dev_addr[0] = 2;
1212 netdev->dev_addr[1] = 0;
1213 netdev->dev_addr[2] = 0;
1214 netdev->dev_addr[3] = 0;
1215 netdev->dev_addr[4] = 0;
1216 netdev->dev_addr[5] = hw->my_epid; /* EPID */
1217
1218 err = register_netdev(netdev);
1219 if (err)
1220 goto err_hw_exit;
1221
1222 netif_carrier_off(netdev);
1223
Taku Izumi658d4392015-08-21 17:29:17 +09001224 return 0;
Taku Izumi2fcbca62015-08-21 17:29:20 +09001225
1226err_hw_exit:
1227 fjes_hw_exit(&adapter->hw);
1228err_free_netdev:
1229 free_netdev(netdev);
1230err_out:
1231 return err;
Taku Izumi658d4392015-08-21 17:29:17 +09001232}
1233
1234/* fjes_remove - Device Removal Routine */
1235static int fjes_remove(struct platform_device *plat_dev)
1236{
Taku Izumi2fcbca62015-08-21 17:29:20 +09001237 struct net_device *netdev = dev_get_drvdata(&plat_dev->dev);
1238 struct fjes_adapter *adapter = netdev_priv(netdev);
1239 struct fjes_hw *hw = &adapter->hw;
1240
Taku Izumi8edb62a2015-08-21 17:29:32 +09001241 cancel_delayed_work_sync(&adapter->interrupt_watch_task);
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001242 cancel_work_sync(&adapter->unshare_watch_task);
Taku Izumib772b9d2015-08-21 17:29:25 +09001243 cancel_work_sync(&adapter->raise_intr_rxdata_task);
Taku Izumiac63b942015-08-21 17:29:26 +09001244 cancel_work_sync(&adapter->tx_stall_task);
Taku Izumi8edb62a2015-08-21 17:29:32 +09001245 if (adapter->control_wq)
1246 destroy_workqueue(adapter->control_wq);
Taku Izumib772b9d2015-08-21 17:29:25 +09001247 if (adapter->txrx_wq)
1248 destroy_workqueue(adapter->txrx_wq);
1249
Taku Izumi2fcbca62015-08-21 17:29:20 +09001250 unregister_netdev(netdev);
1251
1252 fjes_hw_exit(hw);
1253
Taku Izumi26585932015-08-21 17:29:27 +09001254 netif_napi_del(&adapter->napi);
1255
Taku Izumi2fcbca62015-08-21 17:29:20 +09001256 free_netdev(netdev);
1257
Taku Izumi658d4392015-08-21 17:29:17 +09001258 return 0;
1259}
1260
Taku Izumi2fcbca62015-08-21 17:29:20 +09001261static int fjes_sw_init(struct fjes_adapter *adapter)
1262{
Taku Izumi26585932015-08-21 17:29:27 +09001263 struct net_device *netdev = adapter->netdev;
1264
1265 netif_napi_add(netdev, &adapter->napi, fjes_poll, 64);
1266
Taku Izumi2fcbca62015-08-21 17:29:20 +09001267 return 0;
1268}
1269
1270/* fjes_netdev_setup - netdevice initialization routine */
1271static void fjes_netdev_setup(struct net_device *netdev)
1272{
1273 ether_setup(netdev);
1274
1275 netdev->watchdog_timeo = FJES_TX_RETRY_INTERVAL;
1276 netdev->netdev_ops = &fjes_netdev_ops;
Taku Izumi786eec22015-08-21 17:29:38 +09001277 fjes_set_ethtool_ops(netdev);
Taku Izumi16bbec32016-04-15 11:25:40 +09001278 netdev->mtu = fjes_support_mtu[3];
Taku Izumi2fcbca62015-08-21 17:29:20 +09001279 netdev->flags |= IFF_BROADCAST;
1280 netdev->features |= NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_CTAG_FILTER;
1281}
1282
Taku Izumi8edb62a2015-08-21 17:29:32 +09001283static void fjes_irq_watch_task(struct work_struct *work)
1284{
1285 struct fjes_adapter *adapter = container_of(to_delayed_work(work),
1286 struct fjes_adapter, interrupt_watch_task);
1287
1288 local_irq_disable();
1289 fjes_intr(adapter->hw.hw_res.irq, adapter);
1290 local_irq_enable();
1291
1292 if (fjes_rxframe_search_exist(adapter, 0) >= 0)
1293 napi_schedule(&adapter->napi);
1294
1295 if (adapter->interrupt_watch_enable) {
1296 if (!delayed_work_pending(&adapter->interrupt_watch_task))
1297 queue_delayed_work(adapter->control_wq,
1298 &adapter->interrupt_watch_task,
1299 FJES_IRQ_WATCH_DELAY);
1300 }
1301}
1302
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001303static void fjes_watch_unshare_task(struct work_struct *work)
1304{
1305 struct fjes_adapter *adapter =
1306 container_of(work, struct fjes_adapter, unshare_watch_task);
1307
1308 struct net_device *netdev = adapter->netdev;
1309 struct fjes_hw *hw = &adapter->hw;
1310
1311 int unshare_watch, unshare_reserve;
1312 int max_epid, my_epid, epidx;
1313 int stop_req, stop_req_done;
1314 ulong unshare_watch_bitmask;
Taku Izumibd5a2562016-04-15 11:25:46 +09001315 unsigned long flags;
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001316 int wait_time = 0;
1317 int is_shared;
1318 int ret;
1319
1320 my_epid = hw->my_epid;
1321 max_epid = hw->max_epid;
1322
1323 unshare_watch_bitmask = adapter->unshare_watch_bitmask;
1324 adapter->unshare_watch_bitmask = 0;
1325
1326 while ((unshare_watch_bitmask || hw->txrx_stop_req_bit) &&
1327 (wait_time < 3000)) {
1328 for (epidx = 0; epidx < hw->max_epid; epidx++) {
1329 if (epidx == hw->my_epid)
1330 continue;
1331
1332 is_shared = fjes_hw_epid_is_shared(hw->hw_info.share,
1333 epidx);
1334
1335 stop_req = test_bit(epidx, &hw->txrx_stop_req_bit);
1336
1337 stop_req_done = hw->ep_shm_info[epidx].rx.info->v1i.rx_status &
1338 FJES_RX_STOP_REQ_DONE;
1339
1340 unshare_watch = test_bit(epidx, &unshare_watch_bitmask);
1341
1342 unshare_reserve = test_bit(epidx,
1343 &hw->hw_info.buffer_unshare_reserve_bit);
1344
1345 if ((!stop_req ||
1346 (is_shared && (!is_shared || !stop_req_done))) &&
1347 (is_shared || !unshare_watch || !unshare_reserve))
1348 continue;
1349
1350 mutex_lock(&hw->hw_info.lock);
1351 ret = fjes_hw_unregister_buff_addr(hw, epidx);
1352 switch (ret) {
1353 case 0:
1354 break;
1355 case -ENOMSG:
1356 case -EBUSY:
1357 default:
1358 if (!work_pending(
1359 &adapter->force_close_task)) {
1360 adapter->force_reset = true;
1361 schedule_work(
1362 &adapter->force_close_task);
1363 }
1364 break;
1365 }
1366 mutex_unlock(&hw->hw_info.lock);
1367
Taku Izumibd5a2562016-04-15 11:25:46 +09001368 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001369 fjes_hw_setup_epbuf(&hw->ep_shm_info[epidx].tx,
1370 netdev->dev_addr, netdev->mtu);
Taku Izumibd5a2562016-04-15 11:25:46 +09001371 spin_unlock_irqrestore(&hw->rx_status_lock, flags);
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001372
1373 clear_bit(epidx, &hw->txrx_stop_req_bit);
1374 clear_bit(epidx, &unshare_watch_bitmask);
1375 clear_bit(epidx,
1376 &hw->hw_info.buffer_unshare_reserve_bit);
1377 }
1378
1379 msleep(100);
1380 wait_time += 100;
1381 }
1382
1383 if (hw->hw_info.buffer_unshare_reserve_bit) {
1384 for (epidx = 0; epidx < hw->max_epid; epidx++) {
1385 if (epidx == hw->my_epid)
1386 continue;
1387
1388 if (test_bit(epidx,
1389 &hw->hw_info.buffer_unshare_reserve_bit)) {
1390 mutex_lock(&hw->hw_info.lock);
1391
1392 ret = fjes_hw_unregister_buff_addr(hw, epidx);
1393 switch (ret) {
1394 case 0:
1395 break;
1396 case -ENOMSG:
1397 case -EBUSY:
1398 default:
1399 if (!work_pending(
1400 &adapter->force_close_task)) {
1401 adapter->force_reset = true;
1402 schedule_work(
1403 &adapter->force_close_task);
1404 }
1405 break;
1406 }
1407 mutex_unlock(&hw->hw_info.lock);
1408
Taku Izumibd5a2562016-04-15 11:25:46 +09001409 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001410 fjes_hw_setup_epbuf(
1411 &hw->ep_shm_info[epidx].tx,
1412 netdev->dev_addr, netdev->mtu);
Taku Izumibd5a2562016-04-15 11:25:46 +09001413 spin_unlock_irqrestore(&hw->rx_status_lock,
1414 flags);
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001415
1416 clear_bit(epidx, &hw->txrx_stop_req_bit);
1417 clear_bit(epidx, &unshare_watch_bitmask);
1418 clear_bit(epidx, &hw->hw_info.buffer_unshare_reserve_bit);
1419 }
1420
1421 if (test_bit(epidx, &unshare_watch_bitmask)) {
Taku Izumibd5a2562016-04-15 11:25:46 +09001422 spin_lock_irqsave(&hw->rx_status_lock, flags);
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001423 hw->ep_shm_info[epidx].tx.info->v1i.rx_status &=
1424 ~FJES_RX_STOP_REQ_DONE;
Taku Izumibd5a2562016-04-15 11:25:46 +09001425 spin_unlock_irqrestore(&hw->rx_status_lock,
1426 flags);
Taku Izumi8fc4cad2015-08-21 17:29:34 +09001427 }
1428 }
1429 }
1430}
1431
Taku Izumi658d4392015-08-21 17:29:17 +09001432/* fjes_init_module - Driver Registration Routine */
1433static int __init fjes_init_module(void)
1434{
1435 int result;
1436
1437 pr_info("%s - version %s - %s\n",
1438 fjes_driver_string, fjes_driver_version, fjes_copyright);
1439
1440 result = platform_driver_register(&fjes_driver);
1441 if (result < 0)
1442 return result;
1443
1444 result = acpi_bus_register_driver(&fjes_acpi_driver);
1445 if (result < 0)
1446 goto fail_acpi_driver;
1447
1448 return 0;
1449
1450fail_acpi_driver:
1451 platform_driver_unregister(&fjes_driver);
1452 return result;
1453}
1454
1455module_init(fjes_init_module);
1456
1457/* fjes_exit_module - Driver Exit Cleanup Routine */
1458static void __exit fjes_exit_module(void)
1459{
1460 acpi_bus_unregister_driver(&fjes_acpi_driver);
1461 platform_driver_unregister(&fjes_driver);
1462}
1463
1464module_exit(fjes_exit_module);