netcp_core.c 58 KB

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