netcp_core.c 60 KB

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