Commit | Line | Data |
---|---|---|
84640e27 KM |
1 | /* |
2 | * Keystone NetCP Core driver | |
3 | * | |
4 | * Copyright (C) 2014 Texas Instruments Incorporated | |
5 | * Authors: Sandeep Nair <sandeep_n@ti.com> | |
6 | * Sandeep Paulraj <s-paulraj@ti.com> | |
7 | * Cyril Chemparathy <cyril@ti.com> | |
8 | * Santosh Shilimkar <santosh.shilimkar@ti.com> | |
9 | * Murali Karicheri <m-karicheri2@ti.com> | |
10 | * Wingman Kwok <w-kwok2@ti.com> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License as | |
14 | * published by the Free Software Foundation version 2. | |
15 | * | |
16 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any | |
17 | * kind, whether express or implied; without even the implied warranty | |
18 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | */ | |
21 | ||
22 | #include <linux/io.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/of_net.h> | |
25 | #include <linux/of_address.h> | |
26 | #include <linux/if_vlan.h> | |
27 | #include <linux/pm_runtime.h> | |
28 | #include <linux/platform_device.h> | |
29 | #include <linux/soc/ti/knav_qmss.h> | |
30 | #include <linux/soc/ti/knav_dma.h> | |
31 | ||
32 | #include "netcp.h" | |
33 | ||
34 | #define NETCP_SOP_OFFSET (NET_IP_ALIGN + NET_SKB_PAD) | |
35 | #define NETCP_NAPI_WEIGHT 64 | |
36 | #define NETCP_TX_TIMEOUT (5 * HZ) | |
866b8b18 | 37 | #define NETCP_PACKET_SIZE (ETH_FRAME_LEN + ETH_FCS_LEN) |
84640e27 KM |
38 | #define NETCP_MIN_PACKET_SIZE ETH_ZLEN |
39 | #define NETCP_MAX_MCAST_ADDR 16 | |
40 | ||
41 | #define NETCP_EFUSE_REG_INDEX 0 | |
42 | ||
43 | #define NETCP_MOD_PROBE_SKIPPED 1 | |
44 | #define NETCP_MOD_PROBE_FAILED 2 | |
45 | ||
46 | #define NETCP_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \ | |
47 | NETIF_MSG_DRV | NETIF_MSG_LINK | \ | |
48 | NETIF_MSG_IFUP | NETIF_MSG_INTR | \ | |
49 | NETIF_MSG_PROBE | NETIF_MSG_TIMER | \ | |
50 | NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \ | |
51 | NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \ | |
52 | NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \ | |
53 | NETIF_MSG_RX_STATUS) | |
54 | ||
71382bc0 WK |
55 | #define NETCP_EFUSE_ADDR_SWAP 2 |
56 | ||
84640e27 KM |
57 | #define knav_queue_get_id(q) knav_queue_device_control(q, \ |
58 | KNAV_QUEUE_GET_ID, (unsigned long)NULL) | |
59 | ||
60 | #define knav_queue_enable_notify(q) knav_queue_device_control(q, \ | |
61 | KNAV_QUEUE_ENABLE_NOTIFY, \ | |
62 | (unsigned long)NULL) | |
63 | ||
64 | #define knav_queue_disable_notify(q) knav_queue_device_control(q, \ | |
65 | KNAV_QUEUE_DISABLE_NOTIFY, \ | |
66 | (unsigned long)NULL) | |
67 | ||
68 | #define knav_queue_get_count(q) knav_queue_device_control(q, \ | |
69 | KNAV_QUEUE_GET_COUNT, (unsigned long)NULL) | |
70 | ||
71 | #define for_each_netcp_module(module) \ | |
72 | list_for_each_entry(module, &netcp_modules, module_list) | |
73 | ||
74 | #define for_each_netcp_device_module(netcp_device, inst_modpriv) \ | |
75 | list_for_each_entry(inst_modpriv, \ | |
76 | &((netcp_device)->modpriv_head), inst_list) | |
77 | ||
78 | #define for_each_module(netcp, intf_modpriv) \ | |
79 | list_for_each_entry(intf_modpriv, &netcp->module_head, intf_list) | |
80 | ||
81 | /* Module management structures */ | |
82 | struct netcp_device { | |
83 | struct list_head device_list; | |
84 | struct list_head interface_head; | |
85 | struct list_head modpriv_head; | |
86 | struct device *device; | |
87 | }; | |
88 | ||
89 | struct netcp_inst_modpriv { | |
90 | struct netcp_device *netcp_device; | |
91 | struct netcp_module *netcp_module; | |
92 | struct list_head inst_list; | |
93 | void *module_priv; | |
94 | }; | |
95 | ||
96 | struct netcp_intf_modpriv { | |
97 | struct netcp_intf *netcp_priv; | |
98 | struct netcp_module *netcp_module; | |
99 | struct list_head intf_list; | |
100 | void *module_priv; | |
101 | }; | |
102 | ||
103 | static LIST_HEAD(netcp_devices); | |
104 | static LIST_HEAD(netcp_modules); | |
105 | static DEFINE_MUTEX(netcp_modules_lock); | |
106 | ||
107 | static int netcp_debug_level = -1; | |
108 | module_param(netcp_debug_level, int, 0); | |
109 | MODULE_PARM_DESC(netcp_debug_level, "Netcp debug level (NETIF_MSG bits) (0=none,...,16=all)"); | |
110 | ||
111 | /* Helper functions - Get/Set */ | |
112 | static void get_pkt_info(u32 *buff, u32 *buff_len, u32 *ndesc, | |
113 | struct knav_dma_desc *desc) | |
114 | { | |
115 | *buff_len = desc->buff_len; | |
116 | *buff = desc->buff; | |
117 | *ndesc = desc->next_desc; | |
118 | } | |
119 | ||
120 | static void get_pad_info(u32 *pad0, u32 *pad1, struct knav_dma_desc *desc) | |
121 | { | |
122 | *pad0 = desc->pad[0]; | |
123 | *pad1 = desc->pad[1]; | |
124 | } | |
125 | ||
126 | static void get_org_pkt_info(u32 *buff, u32 *buff_len, | |
127 | struct knav_dma_desc *desc) | |
128 | { | |
129 | *buff = desc->orig_buff; | |
130 | *buff_len = desc->orig_len; | |
131 | } | |
132 | ||
133 | static void get_words(u32 *words, int num_words, u32 *desc) | |
134 | { | |
135 | int i; | |
136 | ||
137 | for (i = 0; i < num_words; i++) | |
138 | words[i] = desc[i]; | |
139 | } | |
140 | ||
141 | static void set_pkt_info(u32 buff, u32 buff_len, u32 ndesc, | |
142 | struct knav_dma_desc *desc) | |
143 | { | |
144 | desc->buff_len = buff_len; | |
145 | desc->buff = buff; | |
146 | desc->next_desc = ndesc; | |
147 | } | |
148 | ||
149 | static void set_desc_info(u32 desc_info, u32 pkt_info, | |
150 | struct knav_dma_desc *desc) | |
151 | { | |
152 | desc->desc_info = desc_info; | |
153 | desc->packet_info = pkt_info; | |
154 | } | |
155 | ||
156 | static void set_pad_info(u32 pad0, u32 pad1, struct knav_dma_desc *desc) | |
157 | { | |
158 | desc->pad[0] = pad0; | |
159 | desc->pad[1] = pad1; | |
160 | } | |
161 | ||
162 | static void set_org_pkt_info(u32 buff, u32 buff_len, | |
163 | struct knav_dma_desc *desc) | |
164 | { | |
165 | desc->orig_buff = buff; | |
166 | desc->orig_len = buff_len; | |
167 | } | |
168 | ||
169 | static void set_words(u32 *words, int num_words, u32 *desc) | |
170 | { | |
171 | int i; | |
172 | ||
173 | for (i = 0; i < num_words; i++) | |
174 | desc[i] = words[i]; | |
175 | } | |
176 | ||
177 | /* Read the e-fuse value as 32 bit values to be endian independent */ | |
71382bc0 | 178 | static int emac_arch_get_mac_addr(char *x, void __iomem *efuse_mac, u32 swap) |
84640e27 KM |
179 | { |
180 | unsigned int addr0, addr1; | |
181 | ||
182 | addr1 = readl(efuse_mac + 4); | |
183 | addr0 = readl(efuse_mac); | |
184 | ||
71382bc0 WK |
185 | switch (swap) { |
186 | case NETCP_EFUSE_ADDR_SWAP: | |
187 | addr0 = addr1; | |
188 | addr1 = readl(efuse_mac); | |
189 | break; | |
190 | default: | |
191 | break; | |
192 | } | |
193 | ||
84640e27 KM |
194 | x[0] = (addr1 & 0x0000ff00) >> 8; |
195 | x[1] = addr1 & 0x000000ff; | |
196 | x[2] = (addr0 & 0xff000000) >> 24; | |
197 | x[3] = (addr0 & 0x00ff0000) >> 16; | |
198 | x[4] = (addr0 & 0x0000ff00) >> 8; | |
199 | x[5] = addr0 & 0x000000ff; | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
204 | static const char *netcp_node_name(struct device_node *node) | |
205 | { | |
206 | const char *name; | |
207 | ||
208 | if (of_property_read_string(node, "label", &name) < 0) | |
209 | name = node->name; | |
210 | if (!name) | |
211 | name = "unknown"; | |
212 | return name; | |
213 | } | |
214 | ||
215 | /* Module management routines */ | |
216 | static int netcp_register_interface(struct netcp_intf *netcp) | |
217 | { | |
218 | int ret; | |
219 | ||
220 | ret = register_netdev(netcp->ndev); | |
221 | if (!ret) | |
222 | netcp->netdev_registered = true; | |
223 | return ret; | |
224 | } | |
225 | ||
226 | static int netcp_module_probe(struct netcp_device *netcp_device, | |
227 | struct netcp_module *module) | |
228 | { | |
229 | struct device *dev = netcp_device->device; | |
230 | struct device_node *devices, *interface, *node = dev->of_node; | |
231 | struct device_node *child; | |
232 | struct netcp_inst_modpriv *inst_modpriv; | |
233 | struct netcp_intf *netcp_intf; | |
234 | struct netcp_module *tmp; | |
235 | bool primary_module_registered = false; | |
236 | int ret; | |
237 | ||
238 | /* Find this module in the sub-tree for this device */ | |
239 | devices = of_get_child_by_name(node, "netcp-devices"); | |
240 | if (!devices) { | |
241 | dev_err(dev, "could not find netcp-devices node\n"); | |
242 | return NETCP_MOD_PROBE_SKIPPED; | |
243 | } | |
244 | ||
245 | for_each_available_child_of_node(devices, child) { | |
246 | const char *name = netcp_node_name(child); | |
247 | ||
248 | if (!strcasecmp(module->name, name)) | |
249 | break; | |
250 | } | |
251 | ||
252 | of_node_put(devices); | |
253 | /* If module not used for this device, skip it */ | |
254 | if (!child) { | |
255 | dev_warn(dev, "module(%s) not used for device\n", module->name); | |
256 | return NETCP_MOD_PROBE_SKIPPED; | |
257 | } | |
258 | ||
259 | inst_modpriv = devm_kzalloc(dev, sizeof(*inst_modpriv), GFP_KERNEL); | |
260 | if (!inst_modpriv) { | |
261 | of_node_put(child); | |
262 | return -ENOMEM; | |
263 | } | |
264 | ||
265 | inst_modpriv->netcp_device = netcp_device; | |
266 | inst_modpriv->netcp_module = module; | |
267 | list_add_tail(&inst_modpriv->inst_list, &netcp_device->modpriv_head); | |
268 | ||
269 | ret = module->probe(netcp_device, dev, child, | |
270 | &inst_modpriv->module_priv); | |
271 | of_node_put(child); | |
272 | if (ret) { | |
273 | dev_err(dev, "Probe of module(%s) failed with %d\n", | |
274 | module->name, ret); | |
275 | list_del(&inst_modpriv->inst_list); | |
276 | devm_kfree(dev, inst_modpriv); | |
277 | return NETCP_MOD_PROBE_FAILED; | |
278 | } | |
279 | ||
280 | /* Attach modules only if the primary module is probed */ | |
281 | for_each_netcp_module(tmp) { | |
282 | if (tmp->primary) | |
283 | primary_module_registered = true; | |
284 | } | |
285 | ||
286 | if (!primary_module_registered) | |
287 | return 0; | |
288 | ||
289 | /* Attach module to interfaces */ | |
290 | list_for_each_entry(netcp_intf, &netcp_device->interface_head, | |
291 | interface_list) { | |
292 | struct netcp_intf_modpriv *intf_modpriv; | |
293 | ||
84640e27 KM |
294 | intf_modpriv = devm_kzalloc(dev, sizeof(*intf_modpriv), |
295 | GFP_KERNEL); | |
296 | if (!intf_modpriv) | |
297 | return -ENOMEM; | |
298 | ||
299 | interface = of_parse_phandle(netcp_intf->node_interface, | |
300 | module->name, 0); | |
301 | ||
915c5857 KM |
302 | if (!interface) { |
303 | devm_kfree(dev, intf_modpriv); | |
304 | continue; | |
305 | } | |
306 | ||
84640e27 KM |
307 | intf_modpriv->netcp_priv = netcp_intf; |
308 | intf_modpriv->netcp_module = module; | |
309 | list_add_tail(&intf_modpriv->intf_list, | |
310 | &netcp_intf->module_head); | |
311 | ||
312 | ret = module->attach(inst_modpriv->module_priv, | |
313 | netcp_intf->ndev, interface, | |
314 | &intf_modpriv->module_priv); | |
315 | of_node_put(interface); | |
316 | if (ret) { | |
317 | dev_dbg(dev, "Attach of module %s declined with %d\n", | |
318 | module->name, ret); | |
319 | list_del(&intf_modpriv->intf_list); | |
320 | devm_kfree(dev, intf_modpriv); | |
321 | continue; | |
322 | } | |
323 | } | |
736532a0 KM |
324 | |
325 | /* Now register the interface with netdev */ | |
326 | list_for_each_entry(netcp_intf, | |
327 | &netcp_device->interface_head, | |
328 | interface_list) { | |
329 | /* If interface not registered then register now */ | |
330 | if (!netcp_intf->netdev_registered) { | |
331 | ret = netcp_register_interface(netcp_intf); | |
332 | if (ret) | |
333 | return -ENODEV; | |
334 | } | |
335 | } | |
84640e27 KM |
336 | return 0; |
337 | } | |
338 | ||
339 | int netcp_register_module(struct netcp_module *module) | |
340 | { | |
341 | struct netcp_device *netcp_device; | |
342 | struct netcp_module *tmp; | |
343 | int ret; | |
344 | ||
345 | if (!module->name) { | |
346 | WARN(1, "error registering netcp module: no name\n"); | |
347 | return -EINVAL; | |
348 | } | |
349 | ||
350 | if (!module->probe) { | |
351 | WARN(1, "error registering netcp module: no probe\n"); | |
352 | return -EINVAL; | |
353 | } | |
354 | ||
355 | mutex_lock(&netcp_modules_lock); | |
356 | ||
357 | for_each_netcp_module(tmp) { | |
358 | if (!strcasecmp(tmp->name, module->name)) { | |
359 | mutex_unlock(&netcp_modules_lock); | |
360 | return -EEXIST; | |
361 | } | |
362 | } | |
363 | list_add_tail(&module->module_list, &netcp_modules); | |
364 | ||
365 | list_for_each_entry(netcp_device, &netcp_devices, device_list) { | |
366 | ret = netcp_module_probe(netcp_device, module); | |
367 | if (ret < 0) | |
368 | goto fail; | |
369 | } | |
370 | ||
371 | mutex_unlock(&netcp_modules_lock); | |
372 | return 0; | |
373 | ||
374 | fail: | |
375 | mutex_unlock(&netcp_modules_lock); | |
376 | netcp_unregister_module(module); | |
377 | return ret; | |
378 | } | |
58c11b5f | 379 | EXPORT_SYMBOL_GPL(netcp_register_module); |
84640e27 KM |
380 | |
381 | static void netcp_release_module(struct netcp_device *netcp_device, | |
382 | struct netcp_module *module) | |
383 | { | |
384 | struct netcp_inst_modpriv *inst_modpriv, *inst_tmp; | |
385 | struct netcp_intf *netcp_intf, *netcp_tmp; | |
386 | struct device *dev = netcp_device->device; | |
387 | ||
388 | /* Release the module from each interface */ | |
389 | list_for_each_entry_safe(netcp_intf, netcp_tmp, | |
390 | &netcp_device->interface_head, | |
391 | interface_list) { | |
392 | struct netcp_intf_modpriv *intf_modpriv, *intf_tmp; | |
393 | ||
394 | list_for_each_entry_safe(intf_modpriv, intf_tmp, | |
395 | &netcp_intf->module_head, | |
396 | intf_list) { | |
397 | if (intf_modpriv->netcp_module == module) { | |
398 | module->release(intf_modpriv->module_priv); | |
399 | list_del(&intf_modpriv->intf_list); | |
400 | devm_kfree(dev, intf_modpriv); | |
401 | break; | |
402 | } | |
403 | } | |
404 | } | |
405 | ||
406 | /* Remove the module from each instance */ | |
407 | list_for_each_entry_safe(inst_modpriv, inst_tmp, | |
408 | &netcp_device->modpriv_head, inst_list) { | |
409 | if (inst_modpriv->netcp_module == module) { | |
410 | module->remove(netcp_device, | |
411 | inst_modpriv->module_priv); | |
412 | list_del(&inst_modpriv->inst_list); | |
413 | devm_kfree(dev, inst_modpriv); | |
414 | break; | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
419 | void netcp_unregister_module(struct netcp_module *module) | |
420 | { | |
421 | struct netcp_device *netcp_device; | |
422 | struct netcp_module *module_tmp; | |
423 | ||
424 | mutex_lock(&netcp_modules_lock); | |
425 | ||
426 | list_for_each_entry(netcp_device, &netcp_devices, device_list) { | |
427 | netcp_release_module(netcp_device, module); | |
428 | } | |
429 | ||
430 | /* Remove the module from the module list */ | |
431 | for_each_netcp_module(module_tmp) { | |
432 | if (module == module_tmp) { | |
433 | list_del(&module->module_list); | |
434 | break; | |
435 | } | |
436 | } | |
437 | ||
438 | mutex_unlock(&netcp_modules_lock); | |
439 | } | |
58c11b5f | 440 | EXPORT_SYMBOL_GPL(netcp_unregister_module); |
84640e27 KM |
441 | |
442 | void *netcp_module_get_intf_data(struct netcp_module *module, | |
443 | struct netcp_intf *intf) | |
444 | { | |
445 | struct netcp_intf_modpriv *intf_modpriv; | |
446 | ||
447 | list_for_each_entry(intf_modpriv, &intf->module_head, intf_list) | |
448 | if (intf_modpriv->netcp_module == module) | |
449 | return intf_modpriv->module_priv; | |
450 | return NULL; | |
451 | } | |
58c11b5f | 452 | EXPORT_SYMBOL_GPL(netcp_module_get_intf_data); |
84640e27 KM |
453 | |
454 | /* Module TX and RX Hook management */ | |
455 | struct netcp_hook_list { | |
456 | struct list_head list; | |
457 | netcp_hook_rtn *hook_rtn; | |
458 | void *hook_data; | |
459 | int order; | |
460 | }; | |
461 | ||
462 | int netcp_register_txhook(struct netcp_intf *netcp_priv, int order, | |
463 | netcp_hook_rtn *hook_rtn, void *hook_data) | |
464 | { | |
465 | struct netcp_hook_list *entry; | |
466 | struct netcp_hook_list *next; | |
467 | unsigned long flags; | |
468 | ||
469 | entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL); | |
470 | if (!entry) | |
471 | return -ENOMEM; | |
472 | ||
473 | entry->hook_rtn = hook_rtn; | |
474 | entry->hook_data = hook_data; | |
475 | entry->order = order; | |
476 | ||
477 | spin_lock_irqsave(&netcp_priv->lock, flags); | |
478 | list_for_each_entry(next, &netcp_priv->txhook_list_head, list) { | |
479 | if (next->order > order) | |
480 | break; | |
481 | } | |
482 | __list_add(&entry->list, next->list.prev, &next->list); | |
483 | spin_unlock_irqrestore(&netcp_priv->lock, flags); | |
484 | ||
485 | return 0; | |
486 | } | |
58c11b5f | 487 | EXPORT_SYMBOL_GPL(netcp_register_txhook); |
84640e27 KM |
488 | |
489 | int netcp_unregister_txhook(struct netcp_intf *netcp_priv, int order, | |
490 | netcp_hook_rtn *hook_rtn, void *hook_data) | |
491 | { | |
492 | struct netcp_hook_list *next, *n; | |
493 | unsigned long flags; | |
494 | ||
495 | spin_lock_irqsave(&netcp_priv->lock, flags); | |
496 | list_for_each_entry_safe(next, n, &netcp_priv->txhook_list_head, list) { | |
497 | if ((next->order == order) && | |
498 | (next->hook_rtn == hook_rtn) && | |
499 | (next->hook_data == hook_data)) { | |
500 | list_del(&next->list); | |
501 | spin_unlock_irqrestore(&netcp_priv->lock, flags); | |
502 | devm_kfree(netcp_priv->dev, next); | |
503 | return 0; | |
504 | } | |
505 | } | |
506 | spin_unlock_irqrestore(&netcp_priv->lock, flags); | |
507 | return -ENOENT; | |
508 | } | |
58c11b5f | 509 | EXPORT_SYMBOL_GPL(netcp_unregister_txhook); |
84640e27 KM |
510 | |
511 | int netcp_register_rxhook(struct netcp_intf *netcp_priv, int order, | |
512 | netcp_hook_rtn *hook_rtn, void *hook_data) | |
513 | { | |
514 | struct netcp_hook_list *entry; | |
515 | struct netcp_hook_list *next; | |
516 | unsigned long flags; | |
517 | ||
518 | entry = devm_kzalloc(netcp_priv->dev, sizeof(*entry), GFP_KERNEL); | |
519 | if (!entry) | |
520 | return -ENOMEM; | |
521 | ||
522 | entry->hook_rtn = hook_rtn; | |
523 | entry->hook_data = hook_data; | |
524 | entry->order = order; | |
525 | ||
526 | spin_lock_irqsave(&netcp_priv->lock, flags); | |
527 | list_for_each_entry(next, &netcp_priv->rxhook_list_head, list) { | |
528 | if (next->order > order) | |
529 | break; | |
530 | } | |
531 | __list_add(&entry->list, next->list.prev, &next->list); | |
532 | spin_unlock_irqrestore(&netcp_priv->lock, flags); | |
533 | ||
534 | return 0; | |
535 | } | |
536 | ||
537 | int netcp_unregister_rxhook(struct netcp_intf *netcp_priv, int order, | |
538 | netcp_hook_rtn *hook_rtn, void *hook_data) | |
539 | { | |
540 | struct netcp_hook_list *next, *n; | |
541 | unsigned long flags; | |
542 | ||
543 | spin_lock_irqsave(&netcp_priv->lock, flags); | |
544 | list_for_each_entry_safe(next, n, &netcp_priv->rxhook_list_head, list) { | |
545 | if ((next->order == order) && | |
546 | (next->hook_rtn == hook_rtn) && | |
547 | (next->hook_data == hook_data)) { | |
548 | list_del(&next->list); | |
549 | spin_unlock_irqrestore(&netcp_priv->lock, flags); | |
550 | devm_kfree(netcp_priv->dev, next); | |
551 | return 0; | |
552 | } | |
553 | } | |
554 | spin_unlock_irqrestore(&netcp_priv->lock, flags); | |
555 | ||
556 | return -ENOENT; | |
557 | } | |
558 | ||
559 | static void netcp_frag_free(bool is_frag, void *ptr) | |
560 | { | |
561 | if (is_frag) | |
7d525c4e | 562 | skb_free_frag(ptr); |
84640e27 KM |
563 | else |
564 | kfree(ptr); | |
565 | } | |
566 | ||
567 | static void netcp_free_rx_desc_chain(struct netcp_intf *netcp, | |
568 | struct knav_dma_desc *desc) | |
569 | { | |
570 | struct knav_dma_desc *ndesc; | |
571 | dma_addr_t dma_desc, dma_buf; | |
572 | unsigned int buf_len, dma_sz = sizeof(*ndesc); | |
573 | void *buf_ptr; | |
574 | u32 tmp; | |
575 | ||
576 | get_words(&dma_desc, 1, &desc->next_desc); | |
577 | ||
578 | while (dma_desc) { | |
579 | ndesc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz); | |
580 | if (unlikely(!ndesc)) { | |
581 | dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n"); | |
582 | break; | |
583 | } | |
584 | get_pkt_info(&dma_buf, &tmp, &dma_desc, ndesc); | |
585 | get_pad_info((u32 *)&buf_ptr, &tmp, ndesc); | |
586 | dma_unmap_page(netcp->dev, dma_buf, PAGE_SIZE, DMA_FROM_DEVICE); | |
587 | __free_page(buf_ptr); | |
588 | knav_pool_desc_put(netcp->rx_pool, desc); | |
589 | } | |
590 | ||
591 | get_pad_info((u32 *)&buf_ptr, &buf_len, desc); | |
592 | if (buf_ptr) | |
593 | netcp_frag_free(buf_len <= PAGE_SIZE, buf_ptr); | |
594 | knav_pool_desc_put(netcp->rx_pool, desc); | |
595 | } | |
596 | ||
597 | static void netcp_empty_rx_queue(struct netcp_intf *netcp) | |
598 | { | |
599 | struct knav_dma_desc *desc; | |
600 | unsigned int dma_sz; | |
601 | dma_addr_t dma; | |
602 | ||
603 | for (; ;) { | |
604 | dma = knav_queue_pop(netcp->rx_queue, &dma_sz); | |
605 | if (!dma) | |
606 | break; | |
607 | ||
608 | desc = knav_pool_desc_unmap(netcp->rx_pool, dma, dma_sz); | |
609 | if (unlikely(!desc)) { | |
610 | dev_err(netcp->ndev_dev, "%s: failed to unmap Rx desc\n", | |
611 | __func__); | |
612 | netcp->ndev->stats.rx_errors++; | |
613 | continue; | |
614 | } | |
615 | netcp_free_rx_desc_chain(netcp, desc); | |
616 | netcp->ndev->stats.rx_dropped++; | |
617 | } | |
618 | } | |
619 | ||
620 | static int netcp_process_one_rx_packet(struct netcp_intf *netcp) | |
621 | { | |
622 | unsigned int dma_sz, buf_len, org_buf_len; | |
623 | struct knav_dma_desc *desc, *ndesc; | |
624 | unsigned int pkt_sz = 0, accum_sz; | |
625 | struct netcp_hook_list *rx_hook; | |
626 | dma_addr_t dma_desc, dma_buff; | |
627 | struct netcp_packet p_info; | |
628 | struct sk_buff *skb; | |
629 | void *org_buf_ptr; | |
630 | u32 tmp; | |
631 | ||
632 | dma_desc = knav_queue_pop(netcp->rx_queue, &dma_sz); | |
633 | if (!dma_desc) | |
634 | return -1; | |
635 | ||
636 | desc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz); | |
637 | if (unlikely(!desc)) { | |
638 | dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n"); | |
639 | return 0; | |
640 | } | |
641 | ||
642 | get_pkt_info(&dma_buff, &buf_len, &dma_desc, desc); | |
643 | get_pad_info((u32 *)&org_buf_ptr, &org_buf_len, desc); | |
644 | ||
645 | if (unlikely(!org_buf_ptr)) { | |
646 | dev_err(netcp->ndev_dev, "NULL bufptr in desc\n"); | |
647 | goto free_desc; | |
648 | } | |
649 | ||
650 | pkt_sz &= KNAV_DMA_DESC_PKT_LEN_MASK; | |
651 | accum_sz = buf_len; | |
652 | dma_unmap_single(netcp->dev, dma_buff, buf_len, DMA_FROM_DEVICE); | |
653 | ||
654 | /* Build a new sk_buff for the primary buffer */ | |
655 | skb = build_skb(org_buf_ptr, org_buf_len); | |
656 | if (unlikely(!skb)) { | |
657 | dev_err(netcp->ndev_dev, "build_skb() failed\n"); | |
658 | goto free_desc; | |
659 | } | |
660 | ||
661 | /* update data, tail and len */ | |
662 | skb_reserve(skb, NETCP_SOP_OFFSET); | |
663 | __skb_put(skb, buf_len); | |
664 | ||
665 | /* Fill in the page fragment list */ | |
666 | while (dma_desc) { | |
667 | struct page *page; | |
668 | ||
669 | ndesc = knav_pool_desc_unmap(netcp->rx_pool, dma_desc, dma_sz); | |
670 | if (unlikely(!ndesc)) { | |
671 | dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n"); | |
672 | goto free_desc; | |
673 | } | |
674 | ||
675 | get_pkt_info(&dma_buff, &buf_len, &dma_desc, ndesc); | |
676 | get_pad_info((u32 *)&page, &tmp, ndesc); | |
677 | ||
678 | if (likely(dma_buff && buf_len && page)) { | |
679 | dma_unmap_page(netcp->dev, dma_buff, PAGE_SIZE, | |
680 | DMA_FROM_DEVICE); | |
681 | } else { | |
682 | dev_err(netcp->ndev_dev, "Bad Rx desc dma_buff(%p), len(%d), page(%p)\n", | |
683 | (void *)dma_buff, buf_len, page); | |
684 | goto free_desc; | |
685 | } | |
686 | ||
687 | skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, | |
688 | offset_in_page(dma_buff), buf_len, PAGE_SIZE); | |
689 | accum_sz += buf_len; | |
690 | ||
691 | /* Free the descriptor */ | |
692 | knav_pool_desc_put(netcp->rx_pool, ndesc); | |
693 | } | |
694 | ||
695 | /* Free the primary descriptor */ | |
696 | knav_pool_desc_put(netcp->rx_pool, desc); | |
697 | ||
698 | /* check for packet len and warn */ | |
699 | if (unlikely(pkt_sz != accum_sz)) | |
700 | dev_dbg(netcp->ndev_dev, "mismatch in packet size(%d) & sum of fragments(%d)\n", | |
701 | pkt_sz, accum_sz); | |
702 | ||
703 | /* Remove ethernet FCS from the packet */ | |
704 | __pskb_trim(skb, skb->len - ETH_FCS_LEN); | |
705 | ||
706 | /* Call each of the RX hooks */ | |
707 | p_info.skb = skb; | |
708 | p_info.rxtstamp_complete = false; | |
709 | list_for_each_entry(rx_hook, &netcp->rxhook_list_head, list) { | |
710 | int ret; | |
711 | ||
712 | ret = rx_hook->hook_rtn(rx_hook->order, rx_hook->hook_data, | |
713 | &p_info); | |
714 | if (unlikely(ret)) { | |
715 | dev_err(netcp->ndev_dev, "RX hook %d failed: %d\n", | |
716 | rx_hook->order, ret); | |
717 | netcp->ndev->stats.rx_errors++; | |
718 | dev_kfree_skb(skb); | |
719 | return 0; | |
720 | } | |
721 | } | |
722 | ||
84640e27 KM |
723 | netcp->ndev->stats.rx_packets++; |
724 | netcp->ndev->stats.rx_bytes += skb->len; | |
725 | ||
726 | /* push skb up the stack */ | |
727 | skb->protocol = eth_type_trans(skb, netcp->ndev); | |
728 | netif_receive_skb(skb); | |
729 | return 0; | |
730 | ||
731 | free_desc: | |
732 | netcp_free_rx_desc_chain(netcp, desc); | |
733 | netcp->ndev->stats.rx_errors++; | |
734 | return 0; | |
735 | } | |
736 | ||
737 | static int netcp_process_rx_packets(struct netcp_intf *netcp, | |
738 | unsigned int budget) | |
739 | { | |
740 | int i; | |
741 | ||
742 | for (i = 0; (i < budget) && !netcp_process_one_rx_packet(netcp); i++) | |
743 | ; | |
744 | return i; | |
745 | } | |
746 | ||
747 | /* Release descriptors and attached buffers from Rx FDQ */ | |
748 | static void netcp_free_rx_buf(struct netcp_intf *netcp, int fdq) | |
749 | { | |
750 | struct knav_dma_desc *desc; | |
751 | unsigned int buf_len, dma_sz; | |
752 | dma_addr_t dma; | |
753 | void *buf_ptr; | |
754 | u32 tmp; | |
755 | ||
756 | /* Allocate descriptor */ | |
757 | while ((dma = knav_queue_pop(netcp->rx_fdq[fdq], &dma_sz))) { | |
758 | desc = knav_pool_desc_unmap(netcp->rx_pool, dma, dma_sz); | |
759 | if (unlikely(!desc)) { | |
760 | dev_err(netcp->ndev_dev, "failed to unmap Rx desc\n"); | |
761 | continue; | |
762 | } | |
763 | ||
764 | get_org_pkt_info(&dma, &buf_len, desc); | |
765 | get_pad_info((u32 *)&buf_ptr, &tmp, desc); | |
766 | ||
767 | if (unlikely(!dma)) { | |
768 | dev_err(netcp->ndev_dev, "NULL orig_buff in desc\n"); | |
769 | knav_pool_desc_put(netcp->rx_pool, desc); | |
770 | continue; | |
771 | } | |
772 | ||
773 | if (unlikely(!buf_ptr)) { | |
774 | dev_err(netcp->ndev_dev, "NULL bufptr in desc\n"); | |
775 | knav_pool_desc_put(netcp->rx_pool, desc); | |
776 | continue; | |
777 | } | |
778 | ||
779 | if (fdq == 0) { | |
780 | dma_unmap_single(netcp->dev, dma, buf_len, | |
781 | DMA_FROM_DEVICE); | |
782 | netcp_frag_free((buf_len <= PAGE_SIZE), buf_ptr); | |
783 | } else { | |
784 | dma_unmap_page(netcp->dev, dma, buf_len, | |
785 | DMA_FROM_DEVICE); | |
786 | __free_page(buf_ptr); | |
787 | } | |
788 | ||
789 | knav_pool_desc_put(netcp->rx_pool, desc); | |
790 | } | |
791 | } | |
792 | ||
793 | static void netcp_rxpool_free(struct netcp_intf *netcp) | |
794 | { | |
795 | int i; | |
796 | ||
797 | for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && | |
798 | !IS_ERR_OR_NULL(netcp->rx_fdq[i]); i++) | |
799 | netcp_free_rx_buf(netcp, i); | |
800 | ||
801 | if (knav_pool_count(netcp->rx_pool) != netcp->rx_pool_size) | |
802 | dev_err(netcp->ndev_dev, "Lost Rx (%d) descriptors\n", | |
803 | netcp->rx_pool_size - knav_pool_count(netcp->rx_pool)); | |
804 | ||
805 | knav_pool_destroy(netcp->rx_pool); | |
806 | netcp->rx_pool = NULL; | |
807 | } | |
808 | ||
e558b1fb | 809 | static int netcp_allocate_rx_buf(struct netcp_intf *netcp, int fdq) |
84640e27 KM |
810 | { |
811 | struct knav_dma_desc *hwdesc; | |
812 | unsigned int buf_len, dma_sz; | |
813 | u32 desc_info, pkt_info; | |
814 | struct page *page; | |
815 | dma_addr_t dma; | |
816 | void *bufptr; | |
817 | u32 pad[2]; | |
818 | ||
819 | /* Allocate descriptor */ | |
820 | hwdesc = knav_pool_desc_get(netcp->rx_pool); | |
821 | if (IS_ERR_OR_NULL(hwdesc)) { | |
822 | dev_dbg(netcp->ndev_dev, "out of rx pool desc\n"); | |
e558b1fb | 823 | return -ENOMEM; |
84640e27 KM |
824 | } |
825 | ||
826 | if (likely(fdq == 0)) { | |
827 | unsigned int primary_buf_len; | |
828 | /* Allocate a primary receive queue entry */ | |
866b8b18 | 829 | buf_len = NETCP_PACKET_SIZE + NETCP_SOP_OFFSET; |
84640e27 KM |
830 | primary_buf_len = SKB_DATA_ALIGN(buf_len) + |
831 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); | |
832 | ||
866b8b18 WK |
833 | bufptr = netdev_alloc_frag(primary_buf_len); |
834 | pad[1] = primary_buf_len; | |
84640e27 KM |
835 | |
836 | if (unlikely(!bufptr)) { | |
866b8b18 WK |
837 | dev_warn_ratelimited(netcp->ndev_dev, |
838 | "Primary RX buffer alloc failed\n"); | |
84640e27 KM |
839 | goto fail; |
840 | } | |
841 | dma = dma_map_single(netcp->dev, bufptr, buf_len, | |
842 | DMA_TO_DEVICE); | |
866b8b18 WK |
843 | if (unlikely(dma_mapping_error(netcp->dev, dma))) |
844 | goto fail; | |
845 | ||
84640e27 KM |
846 | pad[0] = (u32)bufptr; |
847 | ||
848 | } else { | |
849 | /* Allocate a secondary receive queue entry */ | |
866b8b18 | 850 | page = alloc_page(GFP_ATOMIC | GFP_DMA | __GFP_COLD); |
84640e27 KM |
851 | if (unlikely(!page)) { |
852 | dev_warn_ratelimited(netcp->ndev_dev, "Secondary page alloc failed\n"); | |
853 | goto fail; | |
854 | } | |
855 | buf_len = PAGE_SIZE; | |
856 | dma = dma_map_page(netcp->dev, page, 0, buf_len, DMA_TO_DEVICE); | |
857 | pad[0] = (u32)page; | |
858 | pad[1] = 0; | |
859 | } | |
860 | ||
861 | desc_info = KNAV_DMA_DESC_PS_INFO_IN_DESC; | |
862 | desc_info |= buf_len & KNAV_DMA_DESC_PKT_LEN_MASK; | |
863 | pkt_info = KNAV_DMA_DESC_HAS_EPIB; | |
864 | pkt_info |= KNAV_DMA_NUM_PS_WORDS << KNAV_DMA_DESC_PSLEN_SHIFT; | |
865 | pkt_info |= (netcp->rx_queue_id & KNAV_DMA_DESC_RETQ_MASK) << | |
866 | KNAV_DMA_DESC_RETQ_SHIFT; | |
867 | set_org_pkt_info(dma, buf_len, hwdesc); | |
868 | set_pad_info(pad[0], pad[1], hwdesc); | |
869 | set_desc_info(desc_info, pkt_info, hwdesc); | |
870 | ||
871 | /* Push to FDQs */ | |
872 | knav_pool_desc_map(netcp->rx_pool, hwdesc, sizeof(*hwdesc), &dma, | |
873 | &dma_sz); | |
874 | knav_queue_push(netcp->rx_fdq[fdq], dma, sizeof(*hwdesc), 0); | |
e558b1fb | 875 | return 0; |
84640e27 KM |
876 | |
877 | fail: | |
878 | knav_pool_desc_put(netcp->rx_pool, hwdesc); | |
e558b1fb | 879 | return -ENOMEM; |
84640e27 KM |
880 | } |
881 | ||
882 | /* Refill Rx FDQ with descriptors & attached buffers */ | |
883 | static void netcp_rxpool_refill(struct netcp_intf *netcp) | |
884 | { | |
885 | u32 fdq_deficit[KNAV_DMA_FDQ_PER_CHAN] = {0}; | |
e558b1fb | 886 | int i, ret = 0; |
84640e27 KM |
887 | |
888 | /* Calculate the FDQ deficit and refill */ | |
889 | for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_fdq[i]; i++) { | |
890 | fdq_deficit[i] = netcp->rx_queue_depths[i] - | |
891 | knav_queue_get_count(netcp->rx_fdq[i]); | |
892 | ||
e558b1fb KM |
893 | while (fdq_deficit[i]-- && !ret) |
894 | ret = netcp_allocate_rx_buf(netcp, i); | |
84640e27 KM |
895 | } /* end for fdqs */ |
896 | } | |
897 | ||
898 | /* NAPI poll */ | |
899 | static int netcp_rx_poll(struct napi_struct *napi, int budget) | |
900 | { | |
901 | struct netcp_intf *netcp = container_of(napi, struct netcp_intf, | |
902 | rx_napi); | |
903 | unsigned int packets; | |
904 | ||
905 | packets = netcp_process_rx_packets(netcp, budget); | |
906 | ||
907 | if (packets < budget) { | |
908 | napi_complete(&netcp->rx_napi); | |
909 | knav_queue_enable_notify(netcp->rx_queue); | |
910 | } | |
911 | ||
912 | netcp_rxpool_refill(netcp); | |
913 | return packets; | |
914 | } | |
915 | ||
916 | static void netcp_rx_notify(void *arg) | |
917 | { | |
918 | struct netcp_intf *netcp = arg; | |
919 | ||
920 | knav_queue_disable_notify(netcp->rx_queue); | |
921 | napi_schedule(&netcp->rx_napi); | |
922 | } | |
923 | ||
924 | static void netcp_free_tx_desc_chain(struct netcp_intf *netcp, | |
925 | struct knav_dma_desc *desc, | |
926 | unsigned int desc_sz) | |
927 | { | |
928 | struct knav_dma_desc *ndesc = desc; | |
929 | dma_addr_t dma_desc, dma_buf; | |
930 | unsigned int buf_len; | |
931 | ||
932 | while (ndesc) { | |
933 | get_pkt_info(&dma_buf, &buf_len, &dma_desc, ndesc); | |
934 | ||
935 | if (dma_buf && buf_len) | |
936 | dma_unmap_single(netcp->dev, dma_buf, buf_len, | |
937 | DMA_TO_DEVICE); | |
938 | else | |
939 | dev_warn(netcp->ndev_dev, "bad Tx desc buf(%p), len(%d)\n", | |
940 | (void *)dma_buf, buf_len); | |
941 | ||
942 | knav_pool_desc_put(netcp->tx_pool, ndesc); | |
943 | ndesc = NULL; | |
944 | if (dma_desc) { | |
945 | ndesc = knav_pool_desc_unmap(netcp->tx_pool, dma_desc, | |
946 | desc_sz); | |
947 | if (!ndesc) | |
948 | dev_err(netcp->ndev_dev, "failed to unmap Tx desc\n"); | |
949 | } | |
950 | } | |
951 | } | |
952 | ||
953 | static int netcp_process_tx_compl_packets(struct netcp_intf *netcp, | |
954 | unsigned int budget) | |
955 | { | |
956 | struct knav_dma_desc *desc; | |
957 | struct sk_buff *skb; | |
958 | unsigned int dma_sz; | |
959 | dma_addr_t dma; | |
960 | int pkts = 0; | |
961 | u32 tmp; | |
962 | ||
963 | while (budget--) { | |
964 | dma = knav_queue_pop(netcp->tx_compl_q, &dma_sz); | |
965 | if (!dma) | |
966 | break; | |
967 | desc = knav_pool_desc_unmap(netcp->tx_pool, dma, dma_sz); | |
968 | if (unlikely(!desc)) { | |
969 | dev_err(netcp->ndev_dev, "failed to unmap Tx desc\n"); | |
970 | netcp->ndev->stats.tx_errors++; | |
971 | continue; | |
972 | } | |
973 | ||
974 | get_pad_info((u32 *)&skb, &tmp, desc); | |
975 | netcp_free_tx_desc_chain(netcp, desc, dma_sz); | |
976 | if (!skb) { | |
977 | dev_err(netcp->ndev_dev, "No skb in Tx desc\n"); | |
978 | netcp->ndev->stats.tx_errors++; | |
979 | continue; | |
980 | } | |
981 | ||
982 | if (netif_subqueue_stopped(netcp->ndev, skb) && | |
983 | netif_running(netcp->ndev) && | |
984 | (knav_pool_count(netcp->tx_pool) > | |
985 | netcp->tx_resume_threshold)) { | |
986 | u16 subqueue = skb_get_queue_mapping(skb); | |
987 | ||
988 | netif_wake_subqueue(netcp->ndev, subqueue); | |
989 | } | |
990 | ||
991 | netcp->ndev->stats.tx_packets++; | |
992 | netcp->ndev->stats.tx_bytes += skb->len; | |
993 | dev_kfree_skb(skb); | |
994 | pkts++; | |
995 | } | |
996 | return pkts; | |
997 | } | |
998 | ||
999 | static int netcp_tx_poll(struct napi_struct *napi, int budget) | |
1000 | { | |
1001 | int packets; | |
1002 | struct netcp_intf *netcp = container_of(napi, struct netcp_intf, | |
1003 | tx_napi); | |
1004 | ||
1005 | packets = netcp_process_tx_compl_packets(netcp, budget); | |
1006 | if (packets < budget) { | |
1007 | napi_complete(&netcp->tx_napi); | |
1008 | knav_queue_enable_notify(netcp->tx_compl_q); | |
1009 | } | |
1010 | ||
1011 | return packets; | |
1012 | } | |
1013 | ||
1014 | static void netcp_tx_notify(void *arg) | |
1015 | { | |
1016 | struct netcp_intf *netcp = arg; | |
1017 | ||
1018 | knav_queue_disable_notify(netcp->tx_compl_q); | |
1019 | napi_schedule(&netcp->tx_napi); | |
1020 | } | |
1021 | ||
1022 | static struct knav_dma_desc* | |
1023 | netcp_tx_map_skb(struct sk_buff *skb, struct netcp_intf *netcp) | |
1024 | { | |
1025 | struct knav_dma_desc *desc, *ndesc, *pdesc; | |
1026 | unsigned int pkt_len = skb_headlen(skb); | |
1027 | struct device *dev = netcp->dev; | |
1028 | dma_addr_t dma_addr; | |
1029 | unsigned int dma_sz; | |
1030 | int i; | |
1031 | ||
1032 | /* Map the linear buffer */ | |
1033 | dma_addr = dma_map_single(dev, skb->data, pkt_len, DMA_TO_DEVICE); | |
866b8b18 | 1034 | if (unlikely(dma_mapping_error(dev, dma_addr))) { |
84640e27 KM |
1035 | dev_err(netcp->ndev_dev, "Failed to map skb buffer\n"); |
1036 | return NULL; | |
1037 | } | |
1038 | ||
1039 | desc = knav_pool_desc_get(netcp->tx_pool); | |
1040 | if (unlikely(IS_ERR_OR_NULL(desc))) { | |
1041 | dev_err(netcp->ndev_dev, "out of TX desc\n"); | |
1042 | dma_unmap_single(dev, dma_addr, pkt_len, DMA_TO_DEVICE); | |
1043 | return NULL; | |
1044 | } | |
1045 | ||
1046 | set_pkt_info(dma_addr, pkt_len, 0, desc); | |
1047 | if (skb_is_nonlinear(skb)) { | |
1048 | prefetchw(skb_shinfo(skb)); | |
1049 | } else { | |
1050 | desc->next_desc = 0; | |
1051 | goto upd_pkt_len; | |
1052 | } | |
1053 | ||
1054 | pdesc = desc; | |
1055 | ||
1056 | /* Handle the case where skb is fragmented in pages */ | |
1057 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
1058 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
1059 | struct page *page = skb_frag_page(frag); | |
1060 | u32 page_offset = frag->page_offset; | |
1061 | u32 buf_len = skb_frag_size(frag); | |
1062 | dma_addr_t desc_dma; | |
1063 | u32 pkt_info; | |
1064 | ||
1065 | dma_addr = dma_map_page(dev, page, page_offset, buf_len, | |
1066 | DMA_TO_DEVICE); | |
1067 | if (unlikely(!dma_addr)) { | |
1068 | dev_err(netcp->ndev_dev, "Failed to map skb page\n"); | |
1069 | goto free_descs; | |
1070 | } | |
1071 | ||
1072 | ndesc = knav_pool_desc_get(netcp->tx_pool); | |
1073 | if (unlikely(IS_ERR_OR_NULL(ndesc))) { | |
1074 | dev_err(netcp->ndev_dev, "out of TX desc for frags\n"); | |
1075 | dma_unmap_page(dev, dma_addr, buf_len, DMA_TO_DEVICE); | |
1076 | goto free_descs; | |
1077 | } | |
1078 | ||
1079 | desc_dma = knav_pool_desc_virt_to_dma(netcp->tx_pool, | |
1080 | (void *)ndesc); | |
1081 | pkt_info = | |
1082 | (netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) << | |
1083 | KNAV_DMA_DESC_RETQ_SHIFT; | |
1084 | set_pkt_info(dma_addr, buf_len, 0, ndesc); | |
1085 | set_words(&desc_dma, 1, &pdesc->next_desc); | |
1086 | pkt_len += buf_len; | |
1087 | if (pdesc != desc) | |
1088 | knav_pool_desc_map(netcp->tx_pool, pdesc, | |
1089 | sizeof(*pdesc), &desc_dma, &dma_sz); | |
1090 | pdesc = ndesc; | |
1091 | } | |
1092 | if (pdesc != desc) | |
1093 | knav_pool_desc_map(netcp->tx_pool, pdesc, sizeof(*pdesc), | |
1094 | &dma_addr, &dma_sz); | |
1095 | ||
1096 | /* frag list based linkage is not supported for now. */ | |
1097 | if (skb_shinfo(skb)->frag_list) { | |
1098 | dev_err_ratelimited(netcp->ndev_dev, "NETIF_F_FRAGLIST not supported\n"); | |
1099 | goto free_descs; | |
1100 | } | |
1101 | ||
1102 | upd_pkt_len: | |
1103 | WARN_ON(pkt_len != skb->len); | |
1104 | ||
1105 | pkt_len &= KNAV_DMA_DESC_PKT_LEN_MASK; | |
1106 | set_words(&pkt_len, 1, &desc->desc_info); | |
1107 | return desc; | |
1108 | ||
1109 | free_descs: | |
1110 | netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc)); | |
1111 | return NULL; | |
1112 | } | |
1113 | ||
1114 | static int netcp_tx_submit_skb(struct netcp_intf *netcp, | |
1115 | struct sk_buff *skb, | |
1116 | struct knav_dma_desc *desc) | |
1117 | { | |
1118 | struct netcp_tx_pipe *tx_pipe = NULL; | |
1119 | struct netcp_hook_list *tx_hook; | |
1120 | struct netcp_packet p_info; | |
84640e27 KM |
1121 | unsigned int dma_sz; |
1122 | dma_addr_t dma; | |
e170f409 | 1123 | u32 tmp = 0; |
84640e27 KM |
1124 | int ret = 0; |
1125 | ||
1126 | p_info.netcp = netcp; | |
1127 | p_info.skb = skb; | |
1128 | p_info.tx_pipe = NULL; | |
1129 | p_info.psdata_len = 0; | |
1130 | p_info.ts_context = NULL; | |
1131 | p_info.txtstamp_complete = NULL; | |
1132 | p_info.epib = desc->epib; | |
1133 | p_info.psdata = desc->psdata; | |
1134 | memset(p_info.epib, 0, KNAV_DMA_NUM_EPIB_WORDS * sizeof(u32)); | |
1135 | ||
1136 | /* Find out where to inject the packet for transmission */ | |
1137 | list_for_each_entry(tx_hook, &netcp->txhook_list_head, list) { | |
1138 | ret = tx_hook->hook_rtn(tx_hook->order, tx_hook->hook_data, | |
1139 | &p_info); | |
1140 | if (unlikely(ret != 0)) { | |
1141 | dev_err(netcp->ndev_dev, "TX hook %d rejected the packet with reason(%d)\n", | |
1142 | tx_hook->order, ret); | |
1143 | ret = (ret < 0) ? ret : NETDEV_TX_OK; | |
1144 | goto out; | |
1145 | } | |
1146 | } | |
1147 | ||
1148 | /* Make sure some TX hook claimed the packet */ | |
1149 | tx_pipe = p_info.tx_pipe; | |
1150 | if (!tx_pipe) { | |
1151 | dev_err(netcp->ndev_dev, "No TX hook claimed the packet!\n"); | |
1152 | ret = -ENXIO; | |
1153 | goto out; | |
1154 | } | |
1155 | ||
1156 | /* update descriptor */ | |
1157 | if (p_info.psdata_len) { | |
1158 | u32 *psdata = p_info.psdata; | |
1159 | ||
1160 | memmove(p_info.psdata, p_info.psdata + p_info.psdata_len, | |
1161 | p_info.psdata_len); | |
1162 | set_words(psdata, p_info.psdata_len, psdata); | |
e170f409 | 1163 | tmp |= (p_info.psdata_len & KNAV_DMA_DESC_PSLEN_MASK) << |
84640e27 KM |
1164 | KNAV_DMA_DESC_PSLEN_SHIFT; |
1165 | } | |
1166 | ||
e170f409 | 1167 | tmp |= KNAV_DMA_DESC_HAS_EPIB | |
84640e27 | 1168 | ((netcp->tx_compl_qid & KNAV_DMA_DESC_RETQ_MASK) << |
e170f409 | 1169 | KNAV_DMA_DESC_RETQ_SHIFT); |
84640e27 | 1170 | |
e170f409 KM |
1171 | if (!(tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO)) { |
1172 | tmp |= ((tx_pipe->switch_to_port & KNAV_DMA_DESC_PSFLAG_MASK) << | |
1173 | KNAV_DMA_DESC_PSFLAG_SHIFT); | |
1174 | } | |
1175 | ||
1176 | set_words(&tmp, 1, &desc->packet_info); | |
84640e27 KM |
1177 | set_words((u32 *)&skb, 1, &desc->pad[0]); |
1178 | ||
e170f409 KM |
1179 | if (tx_pipe->flags & SWITCH_TO_PORT_IN_TAGINFO) { |
1180 | tmp = tx_pipe->switch_to_port; | |
1181 | set_words((u32 *)&tmp, 1, &desc->tag_info); | |
1182 | } | |
1183 | ||
84640e27 KM |
1184 | /* submit packet descriptor */ |
1185 | ret = knav_pool_desc_map(netcp->tx_pool, desc, sizeof(*desc), &dma, | |
1186 | &dma_sz); | |
1187 | if (unlikely(ret)) { | |
1188 | dev_err(netcp->ndev_dev, "%s() failed to map desc\n", __func__); | |
1189 | ret = -ENOMEM; | |
1190 | goto out; | |
1191 | } | |
1192 | skb_tx_timestamp(skb); | |
1193 | knav_queue_push(tx_pipe->dma_queue, dma, dma_sz, 0); | |
1194 | ||
1195 | out: | |
1196 | return ret; | |
1197 | } | |
1198 | ||
1199 | /* Submit the packet */ | |
1200 | static int netcp_ndo_start_xmit(struct sk_buff *skb, struct net_device *ndev) | |
1201 | { | |
1202 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1203 | int subqueue = skb_get_queue_mapping(skb); | |
1204 | struct knav_dma_desc *desc; | |
1205 | int desc_count, ret = 0; | |
1206 | ||
1207 | if (unlikely(skb->len <= 0)) { | |
1208 | dev_kfree_skb(skb); | |
1209 | return NETDEV_TX_OK; | |
1210 | } | |
1211 | ||
1212 | if (unlikely(skb->len < NETCP_MIN_PACKET_SIZE)) { | |
1213 | ret = skb_padto(skb, NETCP_MIN_PACKET_SIZE); | |
1214 | if (ret < 0) { | |
1215 | /* If we get here, the skb has already been dropped */ | |
1216 | dev_warn(netcp->ndev_dev, "padding failed (%d), packet dropped\n", | |
1217 | ret); | |
1218 | ndev->stats.tx_dropped++; | |
1219 | return ret; | |
1220 | } | |
1221 | skb->len = NETCP_MIN_PACKET_SIZE; | |
1222 | } | |
1223 | ||
1224 | desc = netcp_tx_map_skb(skb, netcp); | |
1225 | if (unlikely(!desc)) { | |
1226 | netif_stop_subqueue(ndev, subqueue); | |
1227 | ret = -ENOBUFS; | |
1228 | goto drop; | |
1229 | } | |
1230 | ||
1231 | ret = netcp_tx_submit_skb(netcp, skb, desc); | |
1232 | if (ret) | |
1233 | goto drop; | |
1234 | ||
1235 | ndev->trans_start = jiffies; | |
1236 | ||
1237 | /* Check Tx pool count & stop subqueue if needed */ | |
1238 | desc_count = knav_pool_count(netcp->tx_pool); | |
1239 | if (desc_count < netcp->tx_pause_threshold) { | |
1240 | dev_dbg(netcp->ndev_dev, "pausing tx, count(%d)\n", desc_count); | |
1241 | netif_stop_subqueue(ndev, subqueue); | |
1242 | } | |
1243 | return NETDEV_TX_OK; | |
1244 | ||
1245 | drop: | |
1246 | ndev->stats.tx_dropped++; | |
1247 | if (desc) | |
1248 | netcp_free_tx_desc_chain(netcp, desc, sizeof(*desc)); | |
1249 | dev_kfree_skb(skb); | |
1250 | return ret; | |
1251 | } | |
1252 | ||
1253 | int netcp_txpipe_close(struct netcp_tx_pipe *tx_pipe) | |
1254 | { | |
1255 | if (tx_pipe->dma_channel) { | |
1256 | knav_dma_close_channel(tx_pipe->dma_channel); | |
1257 | tx_pipe->dma_channel = NULL; | |
1258 | } | |
1259 | return 0; | |
1260 | } | |
58c11b5f | 1261 | EXPORT_SYMBOL_GPL(netcp_txpipe_close); |
84640e27 KM |
1262 | |
1263 | int netcp_txpipe_open(struct netcp_tx_pipe *tx_pipe) | |
1264 | { | |
1265 | struct device *dev = tx_pipe->netcp_device->device; | |
1266 | struct knav_dma_cfg config; | |
1267 | int ret = 0; | |
1268 | u8 name[16]; | |
1269 | ||
1270 | memset(&config, 0, sizeof(config)); | |
1271 | config.direction = DMA_MEM_TO_DEV; | |
1272 | config.u.tx.filt_einfo = false; | |
1273 | config.u.tx.filt_pswords = false; | |
1274 | config.u.tx.priority = DMA_PRIO_MED_L; | |
1275 | ||
1276 | tx_pipe->dma_channel = knav_dma_open_channel(dev, | |
1277 | tx_pipe->dma_chan_name, &config); | |
1278 | if (IS_ERR_OR_NULL(tx_pipe->dma_channel)) { | |
1279 | dev_err(dev, "failed opening tx chan(%s)\n", | |
1280 | tx_pipe->dma_chan_name); | |
1281 | goto err; | |
1282 | } | |
1283 | ||
1284 | snprintf(name, sizeof(name), "tx-pipe-%s", dev_name(dev)); | |
1285 | tx_pipe->dma_queue = knav_queue_open(name, tx_pipe->dma_queue_id, | |
1286 | KNAV_QUEUE_SHARED); | |
1287 | if (IS_ERR(tx_pipe->dma_queue)) { | |
1288 | dev_err(dev, "Could not open DMA queue for channel \"%s\": %d\n", | |
1289 | name, ret); | |
1290 | ret = PTR_ERR(tx_pipe->dma_queue); | |
1291 | goto err; | |
1292 | } | |
1293 | ||
1294 | dev_dbg(dev, "opened tx pipe %s\n", name); | |
1295 | return 0; | |
1296 | ||
1297 | err: | |
1298 | if (!IS_ERR_OR_NULL(tx_pipe->dma_channel)) | |
1299 | knav_dma_close_channel(tx_pipe->dma_channel); | |
1300 | tx_pipe->dma_channel = NULL; | |
1301 | return ret; | |
1302 | } | |
58c11b5f | 1303 | EXPORT_SYMBOL_GPL(netcp_txpipe_open); |
84640e27 KM |
1304 | |
1305 | int netcp_txpipe_init(struct netcp_tx_pipe *tx_pipe, | |
1306 | struct netcp_device *netcp_device, | |
1307 | const char *dma_chan_name, unsigned int dma_queue_id) | |
1308 | { | |
1309 | memset(tx_pipe, 0, sizeof(*tx_pipe)); | |
1310 | tx_pipe->netcp_device = netcp_device; | |
1311 | tx_pipe->dma_chan_name = dma_chan_name; | |
1312 | tx_pipe->dma_queue_id = dma_queue_id; | |
1313 | return 0; | |
1314 | } | |
58c11b5f | 1315 | EXPORT_SYMBOL_GPL(netcp_txpipe_init); |
84640e27 KM |
1316 | |
1317 | static struct netcp_addr *netcp_addr_find(struct netcp_intf *netcp, | |
1318 | const u8 *addr, | |
1319 | enum netcp_addr_type type) | |
1320 | { | |
1321 | struct netcp_addr *naddr; | |
1322 | ||
1323 | list_for_each_entry(naddr, &netcp->addr_list, node) { | |
1324 | if (naddr->type != type) | |
1325 | continue; | |
1326 | if (addr && memcmp(addr, naddr->addr, ETH_ALEN)) | |
1327 | continue; | |
1328 | return naddr; | |
1329 | } | |
1330 | ||
1331 | return NULL; | |
1332 | } | |
1333 | ||
1334 | static struct netcp_addr *netcp_addr_add(struct netcp_intf *netcp, | |
1335 | const u8 *addr, | |
1336 | enum netcp_addr_type type) | |
1337 | { | |
1338 | struct netcp_addr *naddr; | |
1339 | ||
1340 | naddr = devm_kmalloc(netcp->dev, sizeof(*naddr), GFP_ATOMIC); | |
1341 | if (!naddr) | |
1342 | return NULL; | |
1343 | ||
1344 | naddr->type = type; | |
1345 | naddr->flags = 0; | |
1346 | naddr->netcp = netcp; | |
1347 | if (addr) | |
1348 | ether_addr_copy(naddr->addr, addr); | |
1349 | else | |
c7bf7169 | 1350 | eth_zero_addr(naddr->addr); |
84640e27 KM |
1351 | list_add_tail(&naddr->node, &netcp->addr_list); |
1352 | ||
1353 | return naddr; | |
1354 | } | |
1355 | ||
1356 | static void netcp_addr_del(struct netcp_intf *netcp, struct netcp_addr *naddr) | |
1357 | { | |
1358 | list_del(&naddr->node); | |
1359 | devm_kfree(netcp->dev, naddr); | |
1360 | } | |
1361 | ||
1362 | static void netcp_addr_clear_mark(struct netcp_intf *netcp) | |
1363 | { | |
1364 | struct netcp_addr *naddr; | |
1365 | ||
1366 | list_for_each_entry(naddr, &netcp->addr_list, node) | |
1367 | naddr->flags = 0; | |
1368 | } | |
1369 | ||
1370 | static void netcp_addr_add_mark(struct netcp_intf *netcp, const u8 *addr, | |
1371 | enum netcp_addr_type type) | |
1372 | { | |
1373 | struct netcp_addr *naddr; | |
1374 | ||
1375 | naddr = netcp_addr_find(netcp, addr, type); | |
1376 | if (naddr) { | |
1377 | naddr->flags |= ADDR_VALID; | |
1378 | return; | |
1379 | } | |
1380 | ||
1381 | naddr = netcp_addr_add(netcp, addr, type); | |
1382 | if (!WARN_ON(!naddr)) | |
1383 | naddr->flags |= ADDR_NEW; | |
1384 | } | |
1385 | ||
1386 | static void netcp_addr_sweep_del(struct netcp_intf *netcp) | |
1387 | { | |
1388 | struct netcp_addr *naddr, *tmp; | |
1389 | struct netcp_intf_modpriv *priv; | |
1390 | struct netcp_module *module; | |
1391 | int error; | |
1392 | ||
1393 | list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) { | |
1394 | if (naddr->flags & (ADDR_VALID | ADDR_NEW)) | |
1395 | continue; | |
1396 | dev_dbg(netcp->ndev_dev, "deleting address %pM, type %x\n", | |
1397 | naddr->addr, naddr->type); | |
1398 | mutex_lock(&netcp_modules_lock); | |
1399 | for_each_module(netcp, priv) { | |
1400 | module = priv->netcp_module; | |
1401 | if (!module->del_addr) | |
1402 | continue; | |
1403 | error = module->del_addr(priv->module_priv, | |
1404 | naddr); | |
1405 | WARN_ON(error); | |
1406 | } | |
1407 | mutex_unlock(&netcp_modules_lock); | |
1408 | netcp_addr_del(netcp, naddr); | |
1409 | } | |
1410 | } | |
1411 | ||
1412 | static void netcp_addr_sweep_add(struct netcp_intf *netcp) | |
1413 | { | |
1414 | struct netcp_addr *naddr, *tmp; | |
1415 | struct netcp_intf_modpriv *priv; | |
1416 | struct netcp_module *module; | |
1417 | int error; | |
1418 | ||
1419 | list_for_each_entry_safe(naddr, tmp, &netcp->addr_list, node) { | |
1420 | if (!(naddr->flags & ADDR_NEW)) | |
1421 | continue; | |
1422 | dev_dbg(netcp->ndev_dev, "adding address %pM, type %x\n", | |
1423 | naddr->addr, naddr->type); | |
1424 | mutex_lock(&netcp_modules_lock); | |
1425 | for_each_module(netcp, priv) { | |
1426 | module = priv->netcp_module; | |
1427 | if (!module->add_addr) | |
1428 | continue; | |
1429 | error = module->add_addr(priv->module_priv, naddr); | |
1430 | WARN_ON(error); | |
1431 | } | |
1432 | mutex_unlock(&netcp_modules_lock); | |
1433 | } | |
1434 | } | |
1435 | ||
1436 | static void netcp_set_rx_mode(struct net_device *ndev) | |
1437 | { | |
1438 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1439 | struct netdev_hw_addr *ndev_addr; | |
1440 | bool promisc; | |
1441 | ||
1442 | promisc = (ndev->flags & IFF_PROMISC || | |
1443 | ndev->flags & IFF_ALLMULTI || | |
1444 | netdev_mc_count(ndev) > NETCP_MAX_MCAST_ADDR); | |
1445 | ||
1446 | /* first clear all marks */ | |
1447 | netcp_addr_clear_mark(netcp); | |
1448 | ||
1449 | /* next add new entries, mark existing ones */ | |
1450 | netcp_addr_add_mark(netcp, ndev->broadcast, ADDR_BCAST); | |
1451 | for_each_dev_addr(ndev, ndev_addr) | |
1452 | netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_DEV); | |
1453 | netdev_for_each_uc_addr(ndev_addr, ndev) | |
1454 | netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_UCAST); | |
1455 | netdev_for_each_mc_addr(ndev_addr, ndev) | |
1456 | netcp_addr_add_mark(netcp, ndev_addr->addr, ADDR_MCAST); | |
1457 | ||
1458 | if (promisc) | |
1459 | netcp_addr_add_mark(netcp, NULL, ADDR_ANY); | |
1460 | ||
1461 | /* finally sweep and callout into modules */ | |
1462 | netcp_addr_sweep_del(netcp); | |
1463 | netcp_addr_sweep_add(netcp); | |
1464 | } | |
1465 | ||
1466 | static void netcp_free_navigator_resources(struct netcp_intf *netcp) | |
1467 | { | |
1468 | int i; | |
1469 | ||
1470 | if (netcp->rx_channel) { | |
1471 | knav_dma_close_channel(netcp->rx_channel); | |
1472 | netcp->rx_channel = NULL; | |
1473 | } | |
1474 | ||
1475 | if (!IS_ERR_OR_NULL(netcp->rx_pool)) | |
1476 | netcp_rxpool_free(netcp); | |
1477 | ||
1478 | if (!IS_ERR_OR_NULL(netcp->rx_queue)) { | |
1479 | knav_queue_close(netcp->rx_queue); | |
1480 | netcp->rx_queue = NULL; | |
1481 | } | |
1482 | ||
1483 | for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && | |
1484 | !IS_ERR_OR_NULL(netcp->rx_fdq[i]) ; ++i) { | |
1485 | knav_queue_close(netcp->rx_fdq[i]); | |
1486 | netcp->rx_fdq[i] = NULL; | |
1487 | } | |
1488 | ||
1489 | if (!IS_ERR_OR_NULL(netcp->tx_compl_q)) { | |
1490 | knav_queue_close(netcp->tx_compl_q); | |
1491 | netcp->tx_compl_q = NULL; | |
1492 | } | |
1493 | ||
1494 | if (!IS_ERR_OR_NULL(netcp->tx_pool)) { | |
1495 | knav_pool_destroy(netcp->tx_pool); | |
1496 | netcp->tx_pool = NULL; | |
1497 | } | |
1498 | } | |
1499 | ||
1500 | static int netcp_setup_navigator_resources(struct net_device *ndev) | |
1501 | { | |
1502 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1503 | struct knav_queue_notify_config notify_cfg; | |
1504 | struct knav_dma_cfg config; | |
1505 | u32 last_fdq = 0; | |
1506 | u8 name[16]; | |
1507 | int ret; | |
1508 | int i; | |
1509 | ||
1510 | /* Create Rx/Tx descriptor pools */ | |
1511 | snprintf(name, sizeof(name), "rx-pool-%s", ndev->name); | |
1512 | netcp->rx_pool = knav_pool_create(name, netcp->rx_pool_size, | |
1513 | netcp->rx_pool_region_id); | |
1514 | if (IS_ERR_OR_NULL(netcp->rx_pool)) { | |
1515 | dev_err(netcp->ndev_dev, "Couldn't create rx pool\n"); | |
1516 | ret = PTR_ERR(netcp->rx_pool); | |
1517 | goto fail; | |
1518 | } | |
1519 | ||
1520 | snprintf(name, sizeof(name), "tx-pool-%s", ndev->name); | |
1521 | netcp->tx_pool = knav_pool_create(name, netcp->tx_pool_size, | |
1522 | netcp->tx_pool_region_id); | |
1523 | if (IS_ERR_OR_NULL(netcp->tx_pool)) { | |
1524 | dev_err(netcp->ndev_dev, "Couldn't create tx pool\n"); | |
1525 | ret = PTR_ERR(netcp->tx_pool); | |
1526 | goto fail; | |
1527 | } | |
1528 | ||
1529 | /* open Tx completion queue */ | |
1530 | snprintf(name, sizeof(name), "tx-compl-%s", ndev->name); | |
1531 | netcp->tx_compl_q = knav_queue_open(name, netcp->tx_compl_qid, 0); | |
1532 | if (IS_ERR_OR_NULL(netcp->tx_compl_q)) { | |
1533 | ret = PTR_ERR(netcp->tx_compl_q); | |
1534 | goto fail; | |
1535 | } | |
1536 | netcp->tx_compl_qid = knav_queue_get_id(netcp->tx_compl_q); | |
1537 | ||
1538 | /* Set notification for Tx completion */ | |
1539 | notify_cfg.fn = netcp_tx_notify; | |
1540 | notify_cfg.fn_arg = netcp; | |
1541 | ret = knav_queue_device_control(netcp->tx_compl_q, | |
1542 | KNAV_QUEUE_SET_NOTIFIER, | |
1543 | (unsigned long)¬ify_cfg); | |
1544 | if (ret) | |
1545 | goto fail; | |
1546 | ||
1547 | knav_queue_disable_notify(netcp->tx_compl_q); | |
1548 | ||
1549 | /* open Rx completion queue */ | |
1550 | snprintf(name, sizeof(name), "rx-compl-%s", ndev->name); | |
1551 | netcp->rx_queue = knav_queue_open(name, netcp->rx_queue_id, 0); | |
1552 | if (IS_ERR_OR_NULL(netcp->rx_queue)) { | |
1553 | ret = PTR_ERR(netcp->rx_queue); | |
1554 | goto fail; | |
1555 | } | |
1556 | netcp->rx_queue_id = knav_queue_get_id(netcp->rx_queue); | |
1557 | ||
1558 | /* Set notification for Rx completion */ | |
1559 | notify_cfg.fn = netcp_rx_notify; | |
1560 | notify_cfg.fn_arg = netcp; | |
1561 | ret = knav_queue_device_control(netcp->rx_queue, | |
1562 | KNAV_QUEUE_SET_NOTIFIER, | |
1563 | (unsigned long)¬ify_cfg); | |
1564 | if (ret) | |
1565 | goto fail; | |
1566 | ||
1567 | knav_queue_disable_notify(netcp->rx_queue); | |
1568 | ||
1569 | /* open Rx FDQs */ | |
866b8b18 WK |
1570 | for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN && netcp->rx_queue_depths[i]; |
1571 | ++i) { | |
84640e27 KM |
1572 | snprintf(name, sizeof(name), "rx-fdq-%s-%d", ndev->name, i); |
1573 | netcp->rx_fdq[i] = knav_queue_open(name, KNAV_QUEUE_GP, 0); | |
1574 | if (IS_ERR_OR_NULL(netcp->rx_fdq[i])) { | |
1575 | ret = PTR_ERR(netcp->rx_fdq[i]); | |
1576 | goto fail; | |
1577 | } | |
1578 | } | |
1579 | ||
1580 | memset(&config, 0, sizeof(config)); | |
1581 | config.direction = DMA_DEV_TO_MEM; | |
1582 | config.u.rx.einfo_present = true; | |
1583 | config.u.rx.psinfo_present = true; | |
1584 | config.u.rx.err_mode = DMA_DROP; | |
1585 | config.u.rx.desc_type = DMA_DESC_HOST; | |
1586 | config.u.rx.psinfo_at_sop = false; | |
1587 | config.u.rx.sop_offset = NETCP_SOP_OFFSET; | |
1588 | config.u.rx.dst_q = netcp->rx_queue_id; | |
1589 | config.u.rx.thresh = DMA_THRESH_NONE; | |
1590 | ||
1591 | for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN; ++i) { | |
1592 | if (netcp->rx_fdq[i]) | |
1593 | last_fdq = knav_queue_get_id(netcp->rx_fdq[i]); | |
1594 | config.u.rx.fdq[i] = last_fdq; | |
1595 | } | |
1596 | ||
1597 | netcp->rx_channel = knav_dma_open_channel(netcp->netcp_device->device, | |
1598 | netcp->dma_chan_name, &config); | |
1599 | if (IS_ERR_OR_NULL(netcp->rx_channel)) { | |
1600 | dev_err(netcp->ndev_dev, "failed opening rx chan(%s\n", | |
1601 | netcp->dma_chan_name); | |
1602 | goto fail; | |
1603 | } | |
1604 | ||
1605 | dev_dbg(netcp->ndev_dev, "opened RX channel: %p\n", netcp->rx_channel); | |
1606 | return 0; | |
1607 | ||
1608 | fail: | |
1609 | netcp_free_navigator_resources(netcp); | |
1610 | return ret; | |
1611 | } | |
1612 | ||
1613 | /* Open the device */ | |
1614 | static int netcp_ndo_open(struct net_device *ndev) | |
1615 | { | |
1616 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1617 | struct netcp_intf_modpriv *intf_modpriv; | |
1618 | struct netcp_module *module; | |
1619 | int ret; | |
1620 | ||
1621 | netif_carrier_off(ndev); | |
1622 | ret = netcp_setup_navigator_resources(ndev); | |
1623 | if (ret) { | |
1624 | dev_err(netcp->ndev_dev, "Failed to setup navigator resources\n"); | |
1625 | goto fail; | |
1626 | } | |
1627 | ||
1628 | mutex_lock(&netcp_modules_lock); | |
1629 | for_each_module(netcp, intf_modpriv) { | |
1630 | module = intf_modpriv->netcp_module; | |
1631 | if (module->open) { | |
1632 | ret = module->open(intf_modpriv->module_priv, ndev); | |
1633 | if (ret != 0) { | |
1634 | dev_err(netcp->ndev_dev, "module open failed\n"); | |
1635 | goto fail_open; | |
1636 | } | |
1637 | } | |
1638 | } | |
1639 | mutex_unlock(&netcp_modules_lock); | |
1640 | ||
84640e27 KM |
1641 | napi_enable(&netcp->rx_napi); |
1642 | napi_enable(&netcp->tx_napi); | |
1643 | knav_queue_enable_notify(netcp->tx_compl_q); | |
1644 | knav_queue_enable_notify(netcp->rx_queue); | |
194ac06e | 1645 | netcp_rxpool_refill(netcp); |
84640e27 KM |
1646 | netif_tx_wake_all_queues(ndev); |
1647 | dev_dbg(netcp->ndev_dev, "netcp device %s opened\n", ndev->name); | |
1648 | return 0; | |
1649 | ||
1650 | fail_open: | |
1651 | for_each_module(netcp, intf_modpriv) { | |
1652 | module = intf_modpriv->netcp_module; | |
1653 | if (module->close) | |
1654 | module->close(intf_modpriv->module_priv, ndev); | |
1655 | } | |
1656 | mutex_unlock(&netcp_modules_lock); | |
1657 | ||
1658 | fail: | |
1659 | netcp_free_navigator_resources(netcp); | |
1660 | return ret; | |
1661 | } | |
1662 | ||
1663 | /* Close the device */ | |
1664 | static int netcp_ndo_stop(struct net_device *ndev) | |
1665 | { | |
1666 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1667 | struct netcp_intf_modpriv *intf_modpriv; | |
1668 | struct netcp_module *module; | |
1669 | int err = 0; | |
1670 | ||
1671 | netif_tx_stop_all_queues(ndev); | |
1672 | netif_carrier_off(ndev); | |
1673 | netcp_addr_clear_mark(netcp); | |
1674 | netcp_addr_sweep_del(netcp); | |
1675 | knav_queue_disable_notify(netcp->rx_queue); | |
1676 | knav_queue_disable_notify(netcp->tx_compl_q); | |
1677 | napi_disable(&netcp->rx_napi); | |
1678 | napi_disable(&netcp->tx_napi); | |
1679 | ||
1680 | mutex_lock(&netcp_modules_lock); | |
1681 | for_each_module(netcp, intf_modpriv) { | |
1682 | module = intf_modpriv->netcp_module; | |
1683 | if (module->close) { | |
1684 | err = module->close(intf_modpriv->module_priv, ndev); | |
1685 | if (err != 0) | |
1686 | dev_err(netcp->ndev_dev, "Close failed\n"); | |
1687 | } | |
1688 | } | |
1689 | mutex_unlock(&netcp_modules_lock); | |
1690 | ||
1691 | /* Recycle Rx descriptors from completion queue */ | |
1692 | netcp_empty_rx_queue(netcp); | |
1693 | ||
1694 | /* Recycle Tx descriptors from completion queue */ | |
1695 | netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size); | |
1696 | ||
1697 | if (knav_pool_count(netcp->tx_pool) != netcp->tx_pool_size) | |
1698 | dev_err(netcp->ndev_dev, "Lost (%d) Tx descs\n", | |
1699 | netcp->tx_pool_size - knav_pool_count(netcp->tx_pool)); | |
1700 | ||
1701 | netcp_free_navigator_resources(netcp); | |
1702 | dev_dbg(netcp->ndev_dev, "netcp device %s stopped\n", ndev->name); | |
1703 | return 0; | |
1704 | } | |
1705 | ||
1706 | static int netcp_ndo_ioctl(struct net_device *ndev, | |
1707 | struct ifreq *req, int cmd) | |
1708 | { | |
1709 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1710 | struct netcp_intf_modpriv *intf_modpriv; | |
1711 | struct netcp_module *module; | |
1712 | int ret = -1, err = -EOPNOTSUPP; | |
1713 | ||
1714 | if (!netif_running(ndev)) | |
1715 | return -EINVAL; | |
1716 | ||
1717 | mutex_lock(&netcp_modules_lock); | |
1718 | for_each_module(netcp, intf_modpriv) { | |
1719 | module = intf_modpriv->netcp_module; | |
1720 | if (!module->ioctl) | |
1721 | continue; | |
1722 | ||
1723 | err = module->ioctl(intf_modpriv->module_priv, req, cmd); | |
1724 | if ((err < 0) && (err != -EOPNOTSUPP)) { | |
1725 | ret = err; | |
1726 | goto out; | |
1727 | } | |
1728 | if (err == 0) | |
1729 | ret = err; | |
1730 | } | |
1731 | ||
1732 | out: | |
1733 | mutex_unlock(&netcp_modules_lock); | |
1734 | return (ret == 0) ? 0 : err; | |
1735 | } | |
1736 | ||
1737 | static int netcp_ndo_change_mtu(struct net_device *ndev, int new_mtu) | |
1738 | { | |
1739 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1740 | ||
1741 | /* MTU < 68 is an error for IPv4 traffic */ | |
1742 | if ((new_mtu < 68) || | |
1743 | (new_mtu > (NETCP_MAX_FRAME_SIZE - ETH_HLEN - ETH_FCS_LEN))) { | |
1744 | dev_err(netcp->ndev_dev, "Invalid mtu size = %d\n", new_mtu); | |
1745 | return -EINVAL; | |
1746 | } | |
1747 | ||
1748 | ndev->mtu = new_mtu; | |
1749 | return 0; | |
1750 | } | |
1751 | ||
1752 | static void netcp_ndo_tx_timeout(struct net_device *ndev) | |
1753 | { | |
1754 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1755 | unsigned int descs = knav_pool_count(netcp->tx_pool); | |
1756 | ||
1757 | dev_err(netcp->ndev_dev, "transmit timed out tx descs(%d)\n", descs); | |
1758 | netcp_process_tx_compl_packets(netcp, netcp->tx_pool_size); | |
1759 | ndev->trans_start = jiffies; | |
1760 | netif_tx_wake_all_queues(ndev); | |
1761 | } | |
1762 | ||
1763 | static int netcp_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid) | |
1764 | { | |
1765 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1766 | struct netcp_intf_modpriv *intf_modpriv; | |
1767 | struct netcp_module *module; | |
1768 | int err = 0; | |
1769 | ||
1770 | dev_dbg(netcp->ndev_dev, "adding rx vlan id: %d\n", vid); | |
1771 | ||
1772 | mutex_lock(&netcp_modules_lock); | |
1773 | for_each_module(netcp, intf_modpriv) { | |
1774 | module = intf_modpriv->netcp_module; | |
1775 | if ((module->add_vid) && (vid != 0)) { | |
1776 | err = module->add_vid(intf_modpriv->module_priv, vid); | |
1777 | if (err != 0) { | |
1778 | dev_err(netcp->ndev_dev, "Could not add vlan id = %d\n", | |
1779 | vid); | |
1780 | break; | |
1781 | } | |
1782 | } | |
1783 | } | |
1784 | mutex_unlock(&netcp_modules_lock); | |
1785 | return err; | |
1786 | } | |
1787 | ||
1788 | static int netcp_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid) | |
1789 | { | |
1790 | struct netcp_intf *netcp = netdev_priv(ndev); | |
1791 | struct netcp_intf_modpriv *intf_modpriv; | |
1792 | struct netcp_module *module; | |
1793 | int err = 0; | |
1794 | ||
1795 | dev_dbg(netcp->ndev_dev, "removing rx vlan id: %d\n", vid); | |
1796 | ||
1797 | mutex_lock(&netcp_modules_lock); | |
1798 | for_each_module(netcp, intf_modpriv) { | |
1799 | module = intf_modpriv->netcp_module; | |
1800 | if (module->del_vid) { | |
1801 | err = module->del_vid(intf_modpriv->module_priv, vid); | |
1802 | if (err != 0) { | |
1803 | dev_err(netcp->ndev_dev, "Could not delete vlan id = %d\n", | |
1804 | vid); | |
1805 | break; | |
1806 | } | |
1807 | } | |
1808 | } | |
1809 | mutex_unlock(&netcp_modules_lock); | |
1810 | return err; | |
1811 | } | |
1812 | ||
1813 | static u16 netcp_select_queue(struct net_device *dev, struct sk_buff *skb, | |
1814 | void *accel_priv, | |
1815 | select_queue_fallback_t fallback) | |
1816 | { | |
1817 | return 0; | |
1818 | } | |
1819 | ||
1820 | static int netcp_setup_tc(struct net_device *dev, u8 num_tc) | |
1821 | { | |
1822 | int i; | |
1823 | ||
1824 | /* setup tc must be called under rtnl lock */ | |
1825 | ASSERT_RTNL(); | |
1826 | ||
1827 | /* Sanity-check the number of traffic classes requested */ | |
1828 | if ((dev->real_num_tx_queues <= 1) || | |
1829 | (dev->real_num_tx_queues < num_tc)) | |
1830 | return -EINVAL; | |
1831 | ||
1832 | /* Configure traffic class to queue mappings */ | |
1833 | if (num_tc) { | |
1834 | netdev_set_num_tc(dev, num_tc); | |
1835 | for (i = 0; i < num_tc; i++) | |
1836 | netdev_set_tc_queue(dev, i, 1, i); | |
1837 | } else { | |
1838 | netdev_reset_tc(dev); | |
1839 | } | |
1840 | ||
1841 | return 0; | |
1842 | } | |
1843 | ||
1844 | static const struct net_device_ops netcp_netdev_ops = { | |
1845 | .ndo_open = netcp_ndo_open, | |
1846 | .ndo_stop = netcp_ndo_stop, | |
1847 | .ndo_start_xmit = netcp_ndo_start_xmit, | |
1848 | .ndo_set_rx_mode = netcp_set_rx_mode, | |
1849 | .ndo_do_ioctl = netcp_ndo_ioctl, | |
1850 | .ndo_change_mtu = netcp_ndo_change_mtu, | |
1851 | .ndo_set_mac_address = eth_mac_addr, | |
1852 | .ndo_validate_addr = eth_validate_addr, | |
1853 | .ndo_vlan_rx_add_vid = netcp_rx_add_vid, | |
1854 | .ndo_vlan_rx_kill_vid = netcp_rx_kill_vid, | |
1855 | .ndo_tx_timeout = netcp_ndo_tx_timeout, | |
1856 | .ndo_select_queue = netcp_select_queue, | |
1857 | .ndo_setup_tc = netcp_setup_tc, | |
1858 | }; | |
1859 | ||
1860 | static int netcp_create_interface(struct netcp_device *netcp_device, | |
1861 | struct device_node *node_interface) | |
1862 | { | |
1863 | struct device *dev = netcp_device->device; | |
1864 | struct device_node *node = dev->of_node; | |
1865 | struct netcp_intf *netcp; | |
1866 | struct net_device *ndev; | |
1867 | resource_size_t size; | |
1868 | struct resource res; | |
1869 | void __iomem *efuse = NULL; | |
1870 | u32 efuse_mac = 0; | |
1871 | const void *mac_addr; | |
1872 | u8 efuse_mac_addr[6]; | |
1873 | u32 temp[2]; | |
1874 | int ret = 0; | |
1875 | ||
1876 | ndev = alloc_etherdev_mqs(sizeof(*netcp), 1, 1); | |
1877 | if (!ndev) { | |
1878 | dev_err(dev, "Error allocating netdev\n"); | |
1879 | return -ENOMEM; | |
1880 | } | |
1881 | ||
1882 | ndev->features |= NETIF_F_SG; | |
1883 | ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; | |
1884 | ndev->hw_features = ndev->features; | |
1885 | ndev->vlan_features |= NETIF_F_SG; | |
1886 | ||
1887 | netcp = netdev_priv(ndev); | |
1888 | spin_lock_init(&netcp->lock); | |
1889 | INIT_LIST_HEAD(&netcp->module_head); | |
1890 | INIT_LIST_HEAD(&netcp->txhook_list_head); | |
1891 | INIT_LIST_HEAD(&netcp->rxhook_list_head); | |
1892 | INIT_LIST_HEAD(&netcp->addr_list); | |
1893 | netcp->netcp_device = netcp_device; | |
1894 | netcp->dev = netcp_device->device; | |
1895 | netcp->ndev = ndev; | |
1896 | netcp->ndev_dev = &ndev->dev; | |
1897 | netcp->msg_enable = netif_msg_init(netcp_debug_level, NETCP_DEBUG); | |
1898 | netcp->tx_pause_threshold = MAX_SKB_FRAGS; | |
1899 | netcp->tx_resume_threshold = netcp->tx_pause_threshold; | |
1900 | netcp->node_interface = node_interface; | |
1901 | ||
1902 | ret = of_property_read_u32(node_interface, "efuse-mac", &efuse_mac); | |
1903 | if (efuse_mac) { | |
1904 | if (of_address_to_resource(node, NETCP_EFUSE_REG_INDEX, &res)) { | |
1905 | dev_err(dev, "could not find efuse-mac reg resource\n"); | |
1906 | ret = -ENODEV; | |
1907 | goto quit; | |
1908 | } | |
1909 | size = resource_size(&res); | |
1910 | ||
1911 | if (!devm_request_mem_region(dev, res.start, size, | |
1912 | dev_name(dev))) { | |
1913 | dev_err(dev, "could not reserve resource\n"); | |
1914 | ret = -ENOMEM; | |
1915 | goto quit; | |
1916 | } | |
1917 | ||
1918 | efuse = devm_ioremap_nocache(dev, res.start, size); | |
1919 | if (!efuse) { | |
1920 | dev_err(dev, "could not map resource\n"); | |
1921 | devm_release_mem_region(dev, res.start, size); | |
1922 | ret = -ENOMEM; | |
1923 | goto quit; | |
1924 | } | |
1925 | ||
71382bc0 | 1926 | emac_arch_get_mac_addr(efuse_mac_addr, efuse, efuse_mac); |
84640e27 KM |
1927 | if (is_valid_ether_addr(efuse_mac_addr)) |
1928 | ether_addr_copy(ndev->dev_addr, efuse_mac_addr); | |
1929 | else | |
1930 | random_ether_addr(ndev->dev_addr); | |
1931 | ||
1932 | devm_iounmap(dev, efuse); | |
1933 | devm_release_mem_region(dev, res.start, size); | |
1934 | } else { | |
1935 | mac_addr = of_get_mac_address(node_interface); | |
1936 | if (mac_addr) | |
1937 | ether_addr_copy(ndev->dev_addr, mac_addr); | |
1938 | else | |
1939 | random_ether_addr(ndev->dev_addr); | |
1940 | } | |
1941 | ||
1942 | ret = of_property_read_string(node_interface, "rx-channel", | |
1943 | &netcp->dma_chan_name); | |
1944 | if (ret < 0) { | |
1945 | dev_err(dev, "missing \"rx-channel\" parameter\n"); | |
1946 | ret = -ENODEV; | |
1947 | goto quit; | |
1948 | } | |
1949 | ||
1950 | ret = of_property_read_u32(node_interface, "rx-queue", | |
1951 | &netcp->rx_queue_id); | |
1952 | if (ret < 0) { | |
1953 | dev_warn(dev, "missing \"rx-queue\" parameter\n"); | |
1954 | netcp->rx_queue_id = KNAV_QUEUE_QPEND; | |
1955 | } | |
1956 | ||
1957 | ret = of_property_read_u32_array(node_interface, "rx-queue-depth", | |
1958 | netcp->rx_queue_depths, | |
1959 | KNAV_DMA_FDQ_PER_CHAN); | |
1960 | if (ret < 0) { | |
1961 | dev_err(dev, "missing \"rx-queue-depth\" parameter\n"); | |
1962 | netcp->rx_queue_depths[0] = 128; | |
1963 | } | |
1964 | ||
84640e27 KM |
1965 | ret = of_property_read_u32_array(node_interface, "rx-pool", temp, 2); |
1966 | if (ret < 0) { | |
1967 | dev_err(dev, "missing \"rx-pool\" parameter\n"); | |
1968 | ret = -ENODEV; | |
1969 | goto quit; | |
1970 | } | |
1971 | netcp->rx_pool_size = temp[0]; | |
1972 | netcp->rx_pool_region_id = temp[1]; | |
1973 | ||
1974 | ret = of_property_read_u32_array(node_interface, "tx-pool", temp, 2); | |
1975 | if (ret < 0) { | |
1976 | dev_err(dev, "missing \"tx-pool\" parameter\n"); | |
1977 | ret = -ENODEV; | |
1978 | goto quit; | |
1979 | } | |
1980 | netcp->tx_pool_size = temp[0]; | |
1981 | netcp->tx_pool_region_id = temp[1]; | |
1982 | ||
1983 | if (netcp->tx_pool_size < MAX_SKB_FRAGS) { | |
1984 | dev_err(dev, "tx-pool size too small, must be atleast(%ld)\n", | |
1985 | MAX_SKB_FRAGS); | |
1986 | ret = -ENODEV; | |
1987 | goto quit; | |
1988 | } | |
1989 | ||
1990 | ret = of_property_read_u32(node_interface, "tx-completion-queue", | |
1991 | &netcp->tx_compl_qid); | |
1992 | if (ret < 0) { | |
1993 | dev_warn(dev, "missing \"tx-completion-queue\" parameter\n"); | |
1994 | netcp->tx_compl_qid = KNAV_QUEUE_QPEND; | |
1995 | } | |
1996 | ||
1997 | /* NAPI register */ | |
1998 | netif_napi_add(ndev, &netcp->rx_napi, netcp_rx_poll, NETCP_NAPI_WEIGHT); | |
1999 | netif_napi_add(ndev, &netcp->tx_napi, netcp_tx_poll, NETCP_NAPI_WEIGHT); | |
2000 | ||
2001 | /* Register the network device */ | |
2002 | ndev->dev_id = 0; | |
2003 | ndev->watchdog_timeo = NETCP_TX_TIMEOUT; | |
2004 | ndev->netdev_ops = &netcp_netdev_ops; | |
2005 | SET_NETDEV_DEV(ndev, dev); | |
2006 | ||
2007 | list_add_tail(&netcp->interface_list, &netcp_device->interface_head); | |
2008 | return 0; | |
2009 | ||
2010 | quit: | |
2011 | free_netdev(ndev); | |
2012 | return ret; | |
2013 | } | |
2014 | ||
2015 | static void netcp_delete_interface(struct netcp_device *netcp_device, | |
2016 | struct net_device *ndev) | |
2017 | { | |
2018 | struct netcp_intf_modpriv *intf_modpriv, *tmp; | |
2019 | struct netcp_intf *netcp = netdev_priv(ndev); | |
2020 | struct netcp_module *module; | |
2021 | ||
2022 | dev_dbg(netcp_device->device, "Removing interface \"%s\"\n", | |
2023 | ndev->name); | |
2024 | ||
2025 | /* Notify each of the modules that the interface is going away */ | |
2026 | list_for_each_entry_safe(intf_modpriv, tmp, &netcp->module_head, | |
2027 | intf_list) { | |
2028 | module = intf_modpriv->netcp_module; | |
2029 | dev_dbg(netcp_device->device, "Releasing module \"%s\"\n", | |
2030 | module->name); | |
2031 | if (module->release) | |
2032 | module->release(intf_modpriv->module_priv); | |
2033 | list_del(&intf_modpriv->intf_list); | |
2034 | kfree(intf_modpriv); | |
2035 | } | |
2036 | WARN(!list_empty(&netcp->module_head), "%s interface module list is not empty!\n", | |
2037 | ndev->name); | |
2038 | ||
2039 | list_del(&netcp->interface_list); | |
2040 | ||
2041 | of_node_put(netcp->node_interface); | |
2042 | unregister_netdev(ndev); | |
2043 | netif_napi_del(&netcp->rx_napi); | |
2044 | free_netdev(ndev); | |
2045 | } | |
2046 | ||
2047 | static int netcp_probe(struct platform_device *pdev) | |
2048 | { | |
2049 | struct device_node *node = pdev->dev.of_node; | |
2050 | struct netcp_intf *netcp_intf, *netcp_tmp; | |
2051 | struct device_node *child, *interfaces; | |
2052 | struct netcp_device *netcp_device; | |
2053 | struct device *dev = &pdev->dev; | |
84640e27 KM |
2054 | int ret; |
2055 | ||
2056 | if (!node) { | |
2057 | dev_err(dev, "could not find device info\n"); | |
2058 | return -ENODEV; | |
2059 | } | |
2060 | ||
2061 | /* Allocate a new NETCP device instance */ | |
2062 | netcp_device = devm_kzalloc(dev, sizeof(*netcp_device), GFP_KERNEL); | |
2063 | if (!netcp_device) | |
2064 | return -ENOMEM; | |
2065 | ||
2066 | pm_runtime_enable(&pdev->dev); | |
2067 | ret = pm_runtime_get_sync(&pdev->dev); | |
2068 | if (ret < 0) { | |
2069 | dev_err(dev, "Failed to enable NETCP power-domain\n"); | |
2070 | pm_runtime_disable(&pdev->dev); | |
2071 | return ret; | |
2072 | } | |
2073 | ||
2074 | /* Initialize the NETCP device instance */ | |
2075 | INIT_LIST_HEAD(&netcp_device->interface_head); | |
2076 | INIT_LIST_HEAD(&netcp_device->modpriv_head); | |
2077 | netcp_device->device = dev; | |
2078 | platform_set_drvdata(pdev, netcp_device); | |
2079 | ||
2080 | /* create interfaces */ | |
2081 | interfaces = of_get_child_by_name(node, "netcp-interfaces"); | |
2082 | if (!interfaces) { | |
2083 | dev_err(dev, "could not find netcp-interfaces node\n"); | |
2084 | ret = -ENODEV; | |
2085 | goto probe_quit; | |
2086 | } | |
2087 | ||
2088 | for_each_available_child_of_node(interfaces, child) { | |
2089 | ret = netcp_create_interface(netcp_device, child); | |
2090 | if (ret) { | |
2091 | dev_err(dev, "could not create interface(%s)\n", | |
2092 | child->name); | |
2093 | goto probe_quit_interface; | |
2094 | } | |
2095 | } | |
2096 | ||
2097 | /* Add the device instance to the list */ | |
2098 | list_add_tail(&netcp_device->device_list, &netcp_devices); | |
2099 | ||
84640e27 KM |
2100 | return 0; |
2101 | ||
2102 | probe_quit_interface: | |
2103 | list_for_each_entry_safe(netcp_intf, netcp_tmp, | |
2104 | &netcp_device->interface_head, | |
2105 | interface_list) { | |
2106 | netcp_delete_interface(netcp_device, netcp_intf->ndev); | |
2107 | } | |
2108 | ||
2109 | probe_quit: | |
2110 | pm_runtime_put_sync(&pdev->dev); | |
2111 | pm_runtime_disable(&pdev->dev); | |
2112 | platform_set_drvdata(pdev, NULL); | |
2113 | return ret; | |
2114 | } | |
2115 | ||
2116 | static int netcp_remove(struct platform_device *pdev) | |
2117 | { | |
2118 | struct netcp_device *netcp_device = platform_get_drvdata(pdev); | |
01a03099 | 2119 | struct netcp_intf *netcp_intf, *netcp_tmp; |
84640e27 KM |
2120 | struct netcp_inst_modpriv *inst_modpriv, *tmp; |
2121 | struct netcp_module *module; | |
2122 | ||
2123 | list_for_each_entry_safe(inst_modpriv, tmp, &netcp_device->modpriv_head, | |
2124 | inst_list) { | |
2125 | module = inst_modpriv->netcp_module; | |
2126 | dev_dbg(&pdev->dev, "Removing module \"%s\"\n", module->name); | |
2127 | module->remove(netcp_device, inst_modpriv->module_priv); | |
2128 | list_del(&inst_modpriv->inst_list); | |
2129 | kfree(inst_modpriv); | |
2130 | } | |
84640e27 | 2131 | |
01a03099 KM |
2132 | /* now that all modules are removed, clean up the interfaces */ |
2133 | list_for_each_entry_safe(netcp_intf, netcp_tmp, | |
2134 | &netcp_device->interface_head, | |
2135 | interface_list) { | |
2136 | netcp_delete_interface(netcp_device, netcp_intf->ndev); | |
2137 | } | |
2138 | ||
2139 | WARN(!list_empty(&netcp_device->interface_head), | |
2140 | "%s interface list not empty!\n", pdev->name); | |
84640e27 | 2141 | |
84640e27 KM |
2142 | pm_runtime_put_sync(&pdev->dev); |
2143 | pm_runtime_disable(&pdev->dev); | |
2144 | platform_set_drvdata(pdev, NULL); | |
2145 | return 0; | |
2146 | } | |
2147 | ||
1156c965 | 2148 | static const struct of_device_id of_match[] = { |
84640e27 KM |
2149 | { .compatible = "ti,netcp-1.0", }, |
2150 | {}, | |
2151 | }; | |
2152 | MODULE_DEVICE_TABLE(of, of_match); | |
2153 | ||
2154 | static struct platform_driver netcp_driver = { | |
2155 | .driver = { | |
2156 | .name = "netcp-1.0", | |
84640e27 KM |
2157 | .of_match_table = of_match, |
2158 | }, | |
2159 | .probe = netcp_probe, | |
2160 | .remove = netcp_remove, | |
2161 | }; | |
2162 | module_platform_driver(netcp_driver); | |
2163 | ||
2164 | MODULE_LICENSE("GPL v2"); | |
2165 | MODULE_DESCRIPTION("TI NETCP driver for Keystone SOCs"); | |
2166 | MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com"); |