aqc111.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Aquantia Corp. Aquantia AQtion USB to 5GbE Controller
  3. * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
  4. * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
  5. * Copyright (C) 2002-2003 TiVo Inc.
  6. * Copyright (C) 2017-2018 ASIX
  7. * Copyright (C) 2018 Aquantia Corp.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/mii.h>
  13. #include <linux/usb.h>
  14. #include <linux/crc32.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/usb/cdc.h>
  17. #include <linux/usb/usbnet.h>
  18. #include <linux/linkmode.h>
  19. #include "aqc111.h"
  20. #define DRIVER_NAME "aqc111"
  21. static int aqc111_read_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
  22. u16 index, u16 size, void *data)
  23. {
  24. int ret;
  25. ret = usbnet_read_cmd_nopm(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR |
  26. USB_RECIP_DEVICE, value, index, data, size);
  27. if (unlikely(ret < size)) {
  28. netdev_warn(dev->net,
  29. "Failed to read(0x%x) reg index 0x%04x: %d\n",
  30. cmd, index, ret);
  31. ret = ret < 0 ? ret : -ENODATA;
  32. }
  33. return ret;
  34. }
  35. static int aqc111_read_cmd(struct usbnet *dev, u8 cmd, u16 value,
  36. u16 index, u16 size, void *data)
  37. {
  38. int ret;
  39. ret = usbnet_read_cmd(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR |
  40. USB_RECIP_DEVICE, value, index, data, size);
  41. if (unlikely(ret < size)) {
  42. netdev_warn(dev->net,
  43. "Failed to read(0x%x) reg index 0x%04x: %d\n",
  44. cmd, index, ret);
  45. ret = ret < 0 ? ret : -ENODATA;
  46. }
  47. return ret;
  48. }
  49. static int aqc111_read16_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
  50. u16 index, u16 *data)
  51. {
  52. int ret = 0;
  53. ret = aqc111_read_cmd_nopm(dev, cmd, value, index, sizeof(*data), data);
  54. le16_to_cpus(data);
  55. return ret;
  56. }
  57. static int aqc111_read16_cmd(struct usbnet *dev, u8 cmd, u16 value,
  58. u16 index, u16 *data)
  59. {
  60. int ret = 0;
  61. ret = aqc111_read_cmd(dev, cmd, value, index, sizeof(*data), data);
  62. le16_to_cpus(data);
  63. return ret;
  64. }
  65. static int __aqc111_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
  66. u16 value, u16 index, u16 size, const void *data)
  67. {
  68. int err = -ENOMEM;
  69. void *buf = NULL;
  70. netdev_dbg(dev->net,
  71. "%s cmd=%#x reqtype=%#x value=%#x index=%#x size=%d\n",
  72. __func__, cmd, reqtype, value, index, size);
  73. if (data) {
  74. buf = kmemdup(data, size, GFP_KERNEL);
  75. if (!buf)
  76. goto out;
  77. }
  78. err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  79. cmd, reqtype, value, index, buf, size,
  80. (cmd == AQ_PHY_POWER) ? AQ_USB_PHY_SET_TIMEOUT :
  81. AQ_USB_SET_TIMEOUT);
  82. if (unlikely(err < 0))
  83. netdev_warn(dev->net,
  84. "Failed to write(0x%x) reg index 0x%04x: %d\n",
  85. cmd, index, err);
  86. kfree(buf);
  87. out:
  88. return err;
  89. }
  90. static int aqc111_write_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
  91. u16 index, u16 size, void *data)
  92. {
  93. int ret;
  94. ret = __aqc111_write_cmd(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
  95. USB_RECIP_DEVICE, value, index, size, data);
  96. return ret;
  97. }
  98. static int aqc111_write_cmd(struct usbnet *dev, u8 cmd, u16 value,
  99. u16 index, u16 size, const void *data)
  100. {
  101. int ret;
  102. if (usb_autopm_get_interface(dev->intf) < 0)
  103. return -ENODEV;
  104. ret = __aqc111_write_cmd(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
  105. USB_RECIP_DEVICE, value, index, size, data);
  106. usb_autopm_put_interface(dev->intf);
  107. return ret;
  108. }
  109. static int aqc111_write16_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
  110. u16 index, u16 *data)
  111. {
  112. u16 tmp = *data;
  113. cpu_to_le16s(&tmp);
  114. return aqc111_write_cmd_nopm(dev, cmd, value, index, sizeof(tmp), &tmp);
  115. }
  116. static int aqc111_write16_cmd(struct usbnet *dev, u8 cmd, u16 value,
  117. u16 index, u16 *data)
  118. {
  119. u16 tmp = *data;
  120. cpu_to_le16s(&tmp);
  121. return aqc111_write_cmd(dev, cmd, value, index, sizeof(tmp), &tmp);
  122. }
  123. static int aqc111_write32_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
  124. u16 index, u32 *data)
  125. {
  126. u32 tmp = *data;
  127. cpu_to_le32s(&tmp);
  128. return aqc111_write_cmd_nopm(dev, cmd, value, index, sizeof(tmp), &tmp);
  129. }
  130. static int aqc111_write32_cmd(struct usbnet *dev, u8 cmd, u16 value,
  131. u16 index, u32 *data)
  132. {
  133. u32 tmp = *data;
  134. cpu_to_le32s(&tmp);
  135. return aqc111_write_cmd(dev, cmd, value, index, sizeof(tmp), &tmp);
  136. }
  137. static int aqc111_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
  138. u16 index, u16 size, void *data)
  139. {
  140. return usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
  141. USB_RECIP_DEVICE, value, index, data,
  142. size);
  143. }
  144. static int aqc111_write16_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
  145. u16 index, u16 *data)
  146. {
  147. u16 tmp = *data;
  148. cpu_to_le16s(&tmp);
  149. return aqc111_write_cmd_async(dev, cmd, value, index,
  150. sizeof(tmp), &tmp);
  151. }
  152. static void aqc111_get_drvinfo(struct net_device *net,
  153. struct ethtool_drvinfo *info)
  154. {
  155. struct usbnet *dev = netdev_priv(net);
  156. struct aqc111_data *aqc111_data = dev->driver_priv;
  157. /* Inherit standard device info */
  158. usbnet_get_drvinfo(net, info);
  159. strscpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  160. snprintf(info->fw_version, sizeof(info->fw_version), "%u.%u.%u",
  161. aqc111_data->fw_ver.major,
  162. aqc111_data->fw_ver.minor,
  163. aqc111_data->fw_ver.rev);
  164. info->eedump_len = 0x00;
  165. info->regdump_len = 0x00;
  166. }
  167. static void aqc111_get_wol(struct net_device *net,
  168. struct ethtool_wolinfo *wolinfo)
  169. {
  170. struct usbnet *dev = netdev_priv(net);
  171. struct aqc111_data *aqc111_data = dev->driver_priv;
  172. wolinfo->supported = WAKE_MAGIC;
  173. wolinfo->wolopts = 0;
  174. if (aqc111_data->wol_flags & AQ_WOL_FLAG_MP)
  175. wolinfo->wolopts |= WAKE_MAGIC;
  176. }
  177. static int aqc111_set_wol(struct net_device *net,
  178. struct ethtool_wolinfo *wolinfo)
  179. {
  180. struct usbnet *dev = netdev_priv(net);
  181. struct aqc111_data *aqc111_data = dev->driver_priv;
  182. if (wolinfo->wolopts & ~WAKE_MAGIC)
  183. return -EINVAL;
  184. aqc111_data->wol_flags = 0;
  185. if (wolinfo->wolopts & WAKE_MAGIC)
  186. aqc111_data->wol_flags |= AQ_WOL_FLAG_MP;
  187. return 0;
  188. }
  189. static void aqc111_speed_to_link_mode(u32 speed,
  190. struct ethtool_link_ksettings *elk)
  191. {
  192. switch (speed) {
  193. case SPEED_5000:
  194. ethtool_link_ksettings_add_link_mode(elk, advertising,
  195. 5000baseT_Full);
  196. break;
  197. case SPEED_2500:
  198. ethtool_link_ksettings_add_link_mode(elk, advertising,
  199. 2500baseT_Full);
  200. break;
  201. case SPEED_1000:
  202. ethtool_link_ksettings_add_link_mode(elk, advertising,
  203. 1000baseT_Full);
  204. break;
  205. case SPEED_100:
  206. ethtool_link_ksettings_add_link_mode(elk, advertising,
  207. 100baseT_Full);
  208. break;
  209. }
  210. }
  211. static int aqc111_get_link_ksettings(struct net_device *net,
  212. struct ethtool_link_ksettings *elk)
  213. {
  214. struct usbnet *dev = netdev_priv(net);
  215. struct aqc111_data *aqc111_data = dev->driver_priv;
  216. enum usb_device_speed usb_speed = dev->udev->speed;
  217. u32 speed = SPEED_UNKNOWN;
  218. ethtool_link_ksettings_zero_link_mode(elk, supported);
  219. ethtool_link_ksettings_add_link_mode(elk, supported,
  220. 100baseT_Full);
  221. ethtool_link_ksettings_add_link_mode(elk, supported,
  222. 1000baseT_Full);
  223. if (usb_speed == USB_SPEED_SUPER) {
  224. ethtool_link_ksettings_add_link_mode(elk, supported,
  225. 2500baseT_Full);
  226. ethtool_link_ksettings_add_link_mode(elk, supported,
  227. 5000baseT_Full);
  228. }
  229. ethtool_link_ksettings_add_link_mode(elk, supported, TP);
  230. ethtool_link_ksettings_add_link_mode(elk, supported, Autoneg);
  231. elk->base.port = PORT_TP;
  232. elk->base.transceiver = XCVR_INTERNAL;
  233. elk->base.mdio_support = 0x00; /*Not supported*/
  234. if (aqc111_data->autoneg)
  235. linkmode_copy(elk->link_modes.advertising,
  236. elk->link_modes.supported);
  237. else
  238. aqc111_speed_to_link_mode(aqc111_data->advertised_speed, elk);
  239. elk->base.autoneg = aqc111_data->autoneg;
  240. switch (aqc111_data->link_speed) {
  241. case AQ_INT_SPEED_5G:
  242. speed = SPEED_5000;
  243. break;
  244. case AQ_INT_SPEED_2_5G:
  245. speed = SPEED_2500;
  246. break;
  247. case AQ_INT_SPEED_1G:
  248. speed = SPEED_1000;
  249. break;
  250. case AQ_INT_SPEED_100M:
  251. speed = SPEED_100;
  252. break;
  253. }
  254. elk->base.duplex = DUPLEX_FULL;
  255. elk->base.speed = speed;
  256. return 0;
  257. }
  258. static void aqc111_set_phy_speed(struct usbnet *dev, u8 autoneg, u16 speed)
  259. {
  260. struct aqc111_data *aqc111_data = dev->driver_priv;
  261. aqc111_data->phy_cfg &= ~AQ_ADV_MASK;
  262. aqc111_data->phy_cfg |= AQ_PAUSE;
  263. aqc111_data->phy_cfg |= AQ_ASYM_PAUSE;
  264. aqc111_data->phy_cfg |= AQ_DOWNSHIFT;
  265. aqc111_data->phy_cfg &= ~AQ_DSH_RETRIES_MASK;
  266. aqc111_data->phy_cfg |= (3 << AQ_DSH_RETRIES_SHIFT) &
  267. AQ_DSH_RETRIES_MASK;
  268. if (autoneg == AUTONEG_ENABLE) {
  269. switch (speed) {
  270. case SPEED_5000:
  271. aqc111_data->phy_cfg |= AQ_ADV_5G;
  272. fallthrough;
  273. case SPEED_2500:
  274. aqc111_data->phy_cfg |= AQ_ADV_2G5;
  275. fallthrough;
  276. case SPEED_1000:
  277. aqc111_data->phy_cfg |= AQ_ADV_1G;
  278. fallthrough;
  279. case SPEED_100:
  280. aqc111_data->phy_cfg |= AQ_ADV_100M;
  281. /* fall-through */
  282. }
  283. } else {
  284. switch (speed) {
  285. case SPEED_5000:
  286. aqc111_data->phy_cfg |= AQ_ADV_5G;
  287. break;
  288. case SPEED_2500:
  289. aqc111_data->phy_cfg |= AQ_ADV_2G5;
  290. break;
  291. case SPEED_1000:
  292. aqc111_data->phy_cfg |= AQ_ADV_1G;
  293. break;
  294. case SPEED_100:
  295. aqc111_data->phy_cfg |= AQ_ADV_100M;
  296. break;
  297. }
  298. }
  299. aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0, &aqc111_data->phy_cfg);
  300. }
  301. static int aqc111_set_link_ksettings(struct net_device *net,
  302. const struct ethtool_link_ksettings *elk)
  303. {
  304. struct usbnet *dev = netdev_priv(net);
  305. struct aqc111_data *aqc111_data = dev->driver_priv;
  306. enum usb_device_speed usb_speed = dev->udev->speed;
  307. u8 autoneg = elk->base.autoneg;
  308. u32 speed = elk->base.speed;
  309. if (autoneg == AUTONEG_ENABLE) {
  310. if (aqc111_data->autoneg != AUTONEG_ENABLE) {
  311. aqc111_data->autoneg = AUTONEG_ENABLE;
  312. aqc111_data->advertised_speed =
  313. (usb_speed == USB_SPEED_SUPER) ?
  314. SPEED_5000 : SPEED_1000;
  315. aqc111_set_phy_speed(dev, aqc111_data->autoneg,
  316. aqc111_data->advertised_speed);
  317. }
  318. } else {
  319. if (speed != SPEED_100 &&
  320. speed != SPEED_1000 &&
  321. speed != SPEED_2500 &&
  322. speed != SPEED_5000 &&
  323. speed != SPEED_UNKNOWN)
  324. return -EINVAL;
  325. if (elk->base.duplex != DUPLEX_FULL)
  326. return -EINVAL;
  327. if (usb_speed != USB_SPEED_SUPER && speed > SPEED_1000)
  328. return -EINVAL;
  329. aqc111_data->autoneg = AUTONEG_DISABLE;
  330. if (speed != SPEED_UNKNOWN)
  331. aqc111_data->advertised_speed = speed;
  332. aqc111_set_phy_speed(dev, aqc111_data->autoneg,
  333. aqc111_data->advertised_speed);
  334. }
  335. return 0;
  336. }
  337. static const struct ethtool_ops aqc111_ethtool_ops = {
  338. .get_drvinfo = aqc111_get_drvinfo,
  339. .get_wol = aqc111_get_wol,
  340. .set_wol = aqc111_set_wol,
  341. .get_msglevel = usbnet_get_msglevel,
  342. .set_msglevel = usbnet_set_msglevel,
  343. .get_link = ethtool_op_get_link,
  344. .get_link_ksettings = aqc111_get_link_ksettings,
  345. .set_link_ksettings = aqc111_set_link_ksettings
  346. };
  347. static int aqc111_change_mtu(struct net_device *net, int new_mtu)
  348. {
  349. struct usbnet *dev = netdev_priv(net);
  350. u16 reg16 = 0;
  351. u8 buf[5];
  352. WRITE_ONCE(net->mtu, new_mtu);
  353. dev->hard_mtu = net->mtu + net->hard_header_len;
  354. aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  355. 2, &reg16);
  356. if (net->mtu > 1500)
  357. reg16 |= SFR_MEDIUM_JUMBO_EN;
  358. else
  359. reg16 &= ~SFR_MEDIUM_JUMBO_EN;
  360. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  361. 2, &reg16);
  362. if (dev->net->mtu > 12500) {
  363. memcpy(buf, &AQC111_BULKIN_SIZE[2], 5);
  364. /* RX bulk configuration */
  365. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL,
  366. 5, 5, buf);
  367. }
  368. /* Set high low water level */
  369. if (dev->net->mtu <= 4500)
  370. reg16 = 0x0810;
  371. else if (dev->net->mtu <= 9500)
  372. reg16 = 0x1020;
  373. else if (dev->net->mtu <= 12500)
  374. reg16 = 0x1420;
  375. else
  376. reg16 = 0x1A20;
  377. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_PAUSE_WATERLVL_LOW,
  378. 2, &reg16);
  379. return 0;
  380. }
  381. static int aqc111_set_mac_addr(struct net_device *net, void *p)
  382. {
  383. struct usbnet *dev = netdev_priv(net);
  384. int ret = 0;
  385. ret = eth_mac_addr(net, p);
  386. if (ret < 0)
  387. return ret;
  388. /* Set the MAC address */
  389. return aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_NODE_ID, ETH_ALEN,
  390. ETH_ALEN, net->dev_addr);
  391. }
  392. static int aqc111_vlan_rx_kill_vid(struct net_device *net,
  393. __be16 proto, u16 vid)
  394. {
  395. struct usbnet *dev = netdev_priv(net);
  396. u8 vlan_ctrl = 0;
  397. u16 reg16 = 0;
  398. u8 reg8 = 0;
  399. aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
  400. vlan_ctrl = reg8;
  401. /* Address */
  402. reg8 = (vid / 16);
  403. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_ADDRESS, 1, 1, &reg8);
  404. /* Data */
  405. reg8 = vlan_ctrl | SFR_VLAN_CONTROL_RD;
  406. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
  407. aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
  408. reg16 &= ~(1 << (vid % 16));
  409. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
  410. reg8 = vlan_ctrl | SFR_VLAN_CONTROL_WE;
  411. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
  412. return 0;
  413. }
  414. static int aqc111_vlan_rx_add_vid(struct net_device *net, __be16 proto, u16 vid)
  415. {
  416. struct usbnet *dev = netdev_priv(net);
  417. u8 vlan_ctrl = 0;
  418. u16 reg16 = 0;
  419. u8 reg8 = 0;
  420. aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
  421. vlan_ctrl = reg8;
  422. /* Address */
  423. reg8 = (vid / 16);
  424. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_ADDRESS, 1, 1, &reg8);
  425. /* Data */
  426. reg8 = vlan_ctrl | SFR_VLAN_CONTROL_RD;
  427. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
  428. aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
  429. reg16 |= (1 << (vid % 16));
  430. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
  431. reg8 = vlan_ctrl | SFR_VLAN_CONTROL_WE;
  432. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
  433. return 0;
  434. }
  435. static void aqc111_set_rx_mode(struct net_device *net)
  436. {
  437. struct usbnet *dev = netdev_priv(net);
  438. struct aqc111_data *aqc111_data = dev->driver_priv;
  439. int mc_count = 0;
  440. mc_count = netdev_mc_count(net);
  441. aqc111_data->rxctl &= ~(SFR_RX_CTL_PRO | SFR_RX_CTL_AMALL |
  442. SFR_RX_CTL_AM);
  443. if (net->flags & IFF_PROMISC) {
  444. aqc111_data->rxctl |= SFR_RX_CTL_PRO;
  445. } else if ((net->flags & IFF_ALLMULTI) || mc_count > AQ_MAX_MCAST) {
  446. aqc111_data->rxctl |= SFR_RX_CTL_AMALL;
  447. } else if (!netdev_mc_empty(net)) {
  448. u8 m_filter[AQ_MCAST_FILTER_SIZE] = { 0 };
  449. struct netdev_hw_addr *ha = NULL;
  450. u32 crc_bits = 0;
  451. netdev_for_each_mc_addr(ha, net) {
  452. crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
  453. m_filter[crc_bits >> 3] |= BIT(crc_bits & 7);
  454. }
  455. aqc111_write_cmd_async(dev, AQ_ACCESS_MAC,
  456. SFR_MULTI_FILTER_ARRY,
  457. AQ_MCAST_FILTER_SIZE,
  458. AQ_MCAST_FILTER_SIZE, m_filter);
  459. aqc111_data->rxctl |= SFR_RX_CTL_AM;
  460. }
  461. aqc111_write16_cmd_async(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
  462. 2, &aqc111_data->rxctl);
  463. }
  464. static int aqc111_set_features(struct net_device *net,
  465. netdev_features_t features)
  466. {
  467. struct usbnet *dev = netdev_priv(net);
  468. struct aqc111_data *aqc111_data = dev->driver_priv;
  469. netdev_features_t changed = net->features ^ features;
  470. u16 reg16 = 0;
  471. u8 reg8 = 0;
  472. if (changed & NETIF_F_IP_CSUM) {
  473. aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, &reg8);
  474. reg8 ^= SFR_TXCOE_TCP | SFR_TXCOE_UDP;
  475. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL,
  476. 1, 1, &reg8);
  477. }
  478. if (changed & NETIF_F_IPV6_CSUM) {
  479. aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, &reg8);
  480. reg8 ^= SFR_TXCOE_TCPV6 | SFR_TXCOE_UDPV6;
  481. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL,
  482. 1, 1, &reg8);
  483. }
  484. if (changed & NETIF_F_RXCSUM) {
  485. aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL, 1, 1, &reg8);
  486. if (features & NETIF_F_RXCSUM) {
  487. aqc111_data->rx_checksum = 1;
  488. reg8 &= ~(SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
  489. SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6);
  490. } else {
  491. aqc111_data->rx_checksum = 0;
  492. reg8 |= SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
  493. SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6;
  494. }
  495. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL,
  496. 1, 1, &reg8);
  497. }
  498. if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
  499. if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
  500. u16 i = 0;
  501. for (i = 0; i < 256; i++) {
  502. /* Address */
  503. reg8 = i;
  504. aqc111_write_cmd(dev, AQ_ACCESS_MAC,
  505. SFR_VLAN_ID_ADDRESS,
  506. 1, 1, &reg8);
  507. /* Data */
  508. aqc111_write16_cmd(dev, AQ_ACCESS_MAC,
  509. SFR_VLAN_ID_DATA0,
  510. 2, &reg16);
  511. reg8 = SFR_VLAN_CONTROL_WE;
  512. aqc111_write_cmd(dev, AQ_ACCESS_MAC,
  513. SFR_VLAN_ID_CONTROL,
  514. 1, 1, &reg8);
  515. }
  516. aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
  517. 1, 1, &reg8);
  518. reg8 |= SFR_VLAN_CONTROL_VFE;
  519. aqc111_write_cmd(dev, AQ_ACCESS_MAC,
  520. SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
  521. } else {
  522. aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
  523. 1, 1, &reg8);
  524. reg8 &= ~SFR_VLAN_CONTROL_VFE;
  525. aqc111_write_cmd(dev, AQ_ACCESS_MAC,
  526. SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
  527. }
  528. }
  529. return 0;
  530. }
  531. static const struct net_device_ops aqc111_netdev_ops = {
  532. .ndo_open = usbnet_open,
  533. .ndo_stop = usbnet_stop,
  534. .ndo_start_xmit = usbnet_start_xmit,
  535. .ndo_tx_timeout = usbnet_tx_timeout,
  536. .ndo_get_stats64 = dev_get_tstats64,
  537. .ndo_change_mtu = aqc111_change_mtu,
  538. .ndo_set_mac_address = aqc111_set_mac_addr,
  539. .ndo_validate_addr = eth_validate_addr,
  540. .ndo_vlan_rx_add_vid = aqc111_vlan_rx_add_vid,
  541. .ndo_vlan_rx_kill_vid = aqc111_vlan_rx_kill_vid,
  542. .ndo_set_rx_mode = aqc111_set_rx_mode,
  543. .ndo_set_features = aqc111_set_features,
  544. };
  545. static int aqc111_read_perm_mac(struct usbnet *dev)
  546. {
  547. u8 buf[ETH_ALEN];
  548. int ret;
  549. ret = aqc111_read_cmd(dev, AQ_FLASH_PARAMETERS, 0, 0, ETH_ALEN, buf);
  550. if (ret < 0)
  551. goto out;
  552. ether_addr_copy(dev->net->perm_addr, buf);
  553. return 0;
  554. out:
  555. return ret;
  556. }
  557. static void aqc111_read_fw_version(struct usbnet *dev,
  558. struct aqc111_data *aqc111_data)
  559. {
  560. aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_MAJOR,
  561. 1, 1, &aqc111_data->fw_ver.major);
  562. aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_MINOR,
  563. 1, 1, &aqc111_data->fw_ver.minor);
  564. aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_REV,
  565. 1, 1, &aqc111_data->fw_ver.rev);
  566. if (aqc111_data->fw_ver.major & 0x80)
  567. aqc111_data->fw_ver.major &= ~0x80;
  568. }
  569. static int aqc111_bind(struct usbnet *dev, struct usb_interface *intf)
  570. {
  571. struct usb_device *udev = interface_to_usbdev(intf);
  572. enum usb_device_speed usb_speed = udev->speed;
  573. struct aqc111_data *aqc111_data;
  574. int ret;
  575. /* Check if vendor configuration */
  576. if (udev->actconfig->desc.bConfigurationValue != 1) {
  577. usb_driver_set_configuration(udev, 1);
  578. return -ENODEV;
  579. }
  580. usb_reset_configuration(dev->udev);
  581. ret = usbnet_get_endpoints(dev, intf);
  582. if (ret < 0) {
  583. netdev_dbg(dev->net, "usbnet_get_endpoints failed");
  584. return ret;
  585. }
  586. aqc111_data = kzalloc(sizeof(*aqc111_data), GFP_KERNEL);
  587. if (!aqc111_data)
  588. return -ENOMEM;
  589. /* store aqc111_data pointer in device data field */
  590. dev->driver_priv = aqc111_data;
  591. /* Init the MAC address */
  592. ret = aqc111_read_perm_mac(dev);
  593. if (ret)
  594. goto out;
  595. eth_hw_addr_set(dev->net, dev->net->perm_addr);
  596. /* Set Rx urb size */
  597. dev->rx_urb_size = URB_SIZE;
  598. /* Set TX needed headroom & tailroom */
  599. dev->net->needed_headroom += sizeof(u64);
  600. dev->net->needed_tailroom += sizeof(u64);
  601. dev->net->max_mtu = 16334;
  602. dev->net->netdev_ops = &aqc111_netdev_ops;
  603. dev->net->ethtool_ops = &aqc111_ethtool_ops;
  604. if (usb_device_no_sg_constraint(dev->udev))
  605. dev->can_dma_sg = 1;
  606. dev->net->hw_features |= AQ_SUPPORT_HW_FEATURE;
  607. dev->net->features |= AQ_SUPPORT_FEATURE;
  608. dev->net->vlan_features |= AQ_SUPPORT_VLAN_FEATURE;
  609. netif_set_tso_max_size(dev->net, 65535);
  610. aqc111_read_fw_version(dev, aqc111_data);
  611. aqc111_data->autoneg = AUTONEG_ENABLE;
  612. aqc111_data->advertised_speed = (usb_speed == USB_SPEED_SUPER) ?
  613. SPEED_5000 : SPEED_1000;
  614. return 0;
  615. out:
  616. kfree(aqc111_data);
  617. return ret;
  618. }
  619. static void aqc111_unbind(struct usbnet *dev, struct usb_interface *intf)
  620. {
  621. struct aqc111_data *aqc111_data = dev->driver_priv;
  622. u16 reg16;
  623. /* Force bz */
  624. reg16 = SFR_PHYPWR_RSTCTL_BZ;
  625. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
  626. 2, &reg16);
  627. reg16 = 0;
  628. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
  629. 2, &reg16);
  630. /* Power down ethernet PHY */
  631. aqc111_data->phy_cfg &= ~AQ_ADV_MASK;
  632. aqc111_data->phy_cfg |= AQ_LOW_POWER;
  633. aqc111_data->phy_cfg &= ~AQ_PHY_POWER_EN;
  634. aqc111_write32_cmd_nopm(dev, AQ_PHY_OPS, 0, 0,
  635. &aqc111_data->phy_cfg);
  636. kfree(aqc111_data);
  637. }
  638. static void aqc111_status(struct usbnet *dev, struct urb *urb)
  639. {
  640. struct aqc111_data *aqc111_data = dev->driver_priv;
  641. u64 *event_data = NULL;
  642. int link = 0;
  643. if (urb->actual_length < sizeof(*event_data))
  644. return;
  645. event_data = urb->transfer_buffer;
  646. le64_to_cpus(event_data);
  647. if (*event_data & AQ_LS_MASK)
  648. link = 1;
  649. else
  650. link = 0;
  651. aqc111_data->link_speed = (*event_data & AQ_SPEED_MASK) >>
  652. AQ_SPEED_SHIFT;
  653. aqc111_data->link = link;
  654. if (netif_carrier_ok(dev->net) != link)
  655. usbnet_defer_kevent(dev, EVENT_LINK_RESET);
  656. }
  657. static void aqc111_configure_rx(struct usbnet *dev,
  658. struct aqc111_data *aqc111_data)
  659. {
  660. enum usb_device_speed usb_speed = dev->udev->speed;
  661. u16 link_speed = 0, usb_host = 0;
  662. u8 buf[5] = { 0 };
  663. u8 queue_num = 0;
  664. u16 reg16 = 0;
  665. u8 reg8 = 0;
  666. buf[0] = 0x00;
  667. buf[1] = 0xF8;
  668. buf[2] = 0x07;
  669. switch (aqc111_data->link_speed) {
  670. case AQ_INT_SPEED_5G:
  671. link_speed = 5000;
  672. reg8 = 0x05;
  673. reg16 = 0x001F;
  674. break;
  675. case AQ_INT_SPEED_2_5G:
  676. link_speed = 2500;
  677. reg16 = 0x003F;
  678. break;
  679. case AQ_INT_SPEED_1G:
  680. link_speed = 1000;
  681. reg16 = 0x009F;
  682. break;
  683. case AQ_INT_SPEED_100M:
  684. link_speed = 100;
  685. queue_num = 1;
  686. reg16 = 0x063F;
  687. buf[1] = 0xFB;
  688. buf[2] = 0x4;
  689. break;
  690. }
  691. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_INTER_PACKET_GAP_0,
  692. 1, 1, &reg8);
  693. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TX_PAUSE_RESEND_T, 3, 3, buf);
  694. switch (usb_speed) {
  695. case USB_SPEED_SUPER:
  696. usb_host = 3;
  697. break;
  698. case USB_SPEED_HIGH:
  699. usb_host = 2;
  700. break;
  701. case USB_SPEED_FULL:
  702. case USB_SPEED_LOW:
  703. usb_host = 1;
  704. queue_num = 0;
  705. break;
  706. default:
  707. usb_host = 0;
  708. break;
  709. }
  710. if (dev->net->mtu > 12500 && dev->net->mtu <= 16334)
  711. queue_num = 2; /* For Jumbo packet 16KB */
  712. memcpy(buf, &AQC111_BULKIN_SIZE[queue_num], 5);
  713. /* RX bulk configuration */
  714. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL, 5, 5, buf);
  715. /* Set high low water level */
  716. if (dev->net->mtu <= 4500)
  717. reg16 = 0x0810;
  718. else if (dev->net->mtu <= 9500)
  719. reg16 = 0x1020;
  720. else if (dev->net->mtu <= 12500)
  721. reg16 = 0x1420;
  722. else if (dev->net->mtu <= 16334)
  723. reg16 = 0x1A20;
  724. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_PAUSE_WATERLVL_LOW,
  725. 2, &reg16);
  726. netdev_info(dev->net, "Link Speed %d, USB %d", link_speed, usb_host);
  727. }
  728. static void aqc111_configure_csum_offload(struct usbnet *dev)
  729. {
  730. u8 reg8 = 0;
  731. if (dev->net->features & NETIF_F_RXCSUM) {
  732. reg8 |= SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
  733. SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6;
  734. }
  735. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL, 1, 1, &reg8);
  736. reg8 = 0;
  737. if (dev->net->features & NETIF_F_IP_CSUM)
  738. reg8 |= SFR_TXCOE_IP | SFR_TXCOE_TCP | SFR_TXCOE_UDP;
  739. if (dev->net->features & NETIF_F_IPV6_CSUM)
  740. reg8 |= SFR_TXCOE_TCPV6 | SFR_TXCOE_UDPV6;
  741. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, &reg8);
  742. }
  743. static int aqc111_link_reset(struct usbnet *dev)
  744. {
  745. struct aqc111_data *aqc111_data = dev->driver_priv;
  746. u16 reg16 = 0;
  747. u8 reg8 = 0;
  748. if (aqc111_data->link == 1) { /* Link up */
  749. aqc111_configure_rx(dev, aqc111_data);
  750. /* Vlan Tag Filter */
  751. reg8 = SFR_VLAN_CONTROL_VSO;
  752. if (dev->net->features & NETIF_F_HW_VLAN_CTAG_FILTER)
  753. reg8 |= SFR_VLAN_CONTROL_VFE;
  754. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
  755. 1, 1, &reg8);
  756. reg8 = 0x0;
  757. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL,
  758. 1, 1, &reg8);
  759. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMTX_DMA_CONTROL,
  760. 1, 1, &reg8);
  761. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_ARC_CTRL, 1, 1, &reg8);
  762. reg16 = SFR_RX_CTL_IPE | SFR_RX_CTL_AB;
  763. aqc111_data->rxctl = reg16;
  764. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
  765. reg8 = SFR_RX_PATH_READY;
  766. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
  767. 1, 1, &reg8);
  768. reg8 = SFR_BULK_OUT_EFF_EN;
  769. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
  770. 1, 1, &reg8);
  771. reg16 = 0;
  772. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  773. 2, &reg16);
  774. reg16 = SFR_MEDIUM_XGMIIMODE | SFR_MEDIUM_FULL_DUPLEX;
  775. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  776. 2, &reg16);
  777. aqc111_configure_csum_offload(dev);
  778. aqc111_set_rx_mode(dev->net);
  779. aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  780. 2, &reg16);
  781. if (dev->net->mtu > 1500)
  782. reg16 |= SFR_MEDIUM_JUMBO_EN;
  783. reg16 |= SFR_MEDIUM_RECEIVE_EN | SFR_MEDIUM_RXFLOW_CTRLEN |
  784. SFR_MEDIUM_TXFLOW_CTRLEN;
  785. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  786. 2, &reg16);
  787. aqc111_data->rxctl |= SFR_RX_CTL_START;
  788. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
  789. 2, &aqc111_data->rxctl);
  790. netif_carrier_on(dev->net);
  791. } else {
  792. aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  793. 2, &reg16);
  794. reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
  795. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  796. 2, &reg16);
  797. aqc111_data->rxctl &= ~SFR_RX_CTL_START;
  798. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
  799. 2, &aqc111_data->rxctl);
  800. reg8 = SFR_BULK_OUT_FLUSH_EN | SFR_BULK_OUT_EFF_EN;
  801. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
  802. 1, 1, &reg8);
  803. reg8 = SFR_BULK_OUT_EFF_EN;
  804. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
  805. 1, 1, &reg8);
  806. netif_carrier_off(dev->net);
  807. }
  808. return 0;
  809. }
  810. static int aqc111_reset(struct usbnet *dev)
  811. {
  812. struct aqc111_data *aqc111_data = dev->driver_priv;
  813. u8 reg8 = 0;
  814. dev->rx_urb_size = URB_SIZE;
  815. if (usb_device_no_sg_constraint(dev->udev))
  816. dev->can_dma_sg = 1;
  817. dev->net->hw_features |= AQ_SUPPORT_HW_FEATURE;
  818. dev->net->features |= AQ_SUPPORT_FEATURE;
  819. dev->net->vlan_features |= AQ_SUPPORT_VLAN_FEATURE;
  820. /* Power up ethernet PHY */
  821. aqc111_data->phy_cfg = AQ_PHY_POWER_EN;
  822. aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
  823. &aqc111_data->phy_cfg);
  824. /* Set the MAC address */
  825. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_NODE_ID, ETH_ALEN,
  826. ETH_ALEN, dev->net->dev_addr);
  827. reg8 = 0xFF;
  828. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK, 1, 1, &reg8);
  829. reg8 = 0x0;
  830. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_SWP_CTRL, 1, 1, &reg8);
  831. aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_MONITOR_MODE, 1, 1, &reg8);
  832. reg8 &= ~(SFR_MONITOR_MODE_EPHYRW | SFR_MONITOR_MODE_RWLC |
  833. SFR_MONITOR_MODE_RWMP | SFR_MONITOR_MODE_RWWF |
  834. SFR_MONITOR_MODE_RW_FLAG);
  835. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_MONITOR_MODE, 1, 1, &reg8);
  836. netif_carrier_off(dev->net);
  837. /* Phy advertise */
  838. aqc111_set_phy_speed(dev, aqc111_data->autoneg,
  839. aqc111_data->advertised_speed);
  840. return 0;
  841. }
  842. static int aqc111_stop(struct usbnet *dev)
  843. {
  844. struct aqc111_data *aqc111_data = dev->driver_priv;
  845. u16 reg16 = 0;
  846. aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  847. 2, &reg16);
  848. reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
  849. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  850. 2, &reg16);
  851. reg16 = 0;
  852. aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
  853. /* Put PHY to low power*/
  854. aqc111_data->phy_cfg |= AQ_LOW_POWER;
  855. aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
  856. &aqc111_data->phy_cfg);
  857. netif_carrier_off(dev->net);
  858. return 0;
  859. }
  860. static void aqc111_rx_checksum(struct sk_buff *skb, u64 pkt_desc)
  861. {
  862. u32 pkt_type = 0;
  863. skb->ip_summed = CHECKSUM_NONE;
  864. /* checksum error bit is set */
  865. if (pkt_desc & AQ_RX_PD_L4_ERR || pkt_desc & AQ_RX_PD_L3_ERR)
  866. return;
  867. pkt_type = pkt_desc & AQ_RX_PD_L4_TYPE_MASK;
  868. /* It must be a TCP or UDP packet with a valid checksum */
  869. if (pkt_type == AQ_RX_PD_L4_TCP || pkt_type == AQ_RX_PD_L4_UDP)
  870. skb->ip_summed = CHECKSUM_UNNECESSARY;
  871. }
  872. static int aqc111_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  873. {
  874. struct aqc111_data *aqc111_data = dev->driver_priv;
  875. struct sk_buff *new_skb = NULL;
  876. u32 pkt_total_offset = 0;
  877. u64 *pkt_desc_ptr = NULL;
  878. u32 start_of_descs = 0;
  879. u32 desc_offset = 0; /*RX Header Offset*/
  880. u16 pkt_count = 0;
  881. u64 desc_hdr = 0;
  882. u16 vlan_tag = 0;
  883. u32 skb_len;
  884. if (!skb)
  885. goto err;
  886. skb_len = skb->len;
  887. if (skb_len < sizeof(desc_hdr))
  888. goto err;
  889. /* RX Descriptor Header */
  890. skb_trim(skb, skb_len - sizeof(desc_hdr));
  891. desc_hdr = le64_to_cpup((u64 *)skb_tail_pointer(skb));
  892. /* Check these packets */
  893. desc_offset = (desc_hdr & AQ_RX_DH_DESC_OFFSET_MASK) >>
  894. AQ_RX_DH_DESC_OFFSET_SHIFT;
  895. pkt_count = desc_hdr & AQ_RX_DH_PKT_CNT_MASK;
  896. start_of_descs = skb_len - ((pkt_count + 1) * sizeof(desc_hdr));
  897. /* self check descs position */
  898. if (start_of_descs != desc_offset)
  899. goto err;
  900. /* self check desc_offset from header and make sure that the
  901. * bounds of the metadata array are inside the SKB
  902. */
  903. if (pkt_count * 2 + desc_offset >= skb_len)
  904. goto err;
  905. /* Packets must not overlap the metadata array */
  906. skb_trim(skb, desc_offset);
  907. if (pkt_count == 0)
  908. goto err;
  909. /* Get the first RX packet descriptor */
  910. pkt_desc_ptr = (u64 *)(skb->data + desc_offset);
  911. while (pkt_count--) {
  912. u64 pkt_desc = le64_to_cpup(pkt_desc_ptr);
  913. u32 pkt_len_with_padd = 0;
  914. u32 pkt_len = 0;
  915. pkt_len = (u32)((pkt_desc & AQ_RX_PD_LEN_MASK) >>
  916. AQ_RX_PD_LEN_SHIFT);
  917. pkt_len_with_padd = ((pkt_len + 7) & 0x7FFF8);
  918. pkt_total_offset += pkt_len_with_padd;
  919. if (pkt_total_offset > desc_offset ||
  920. (pkt_count == 0 && pkt_total_offset != desc_offset)) {
  921. goto err;
  922. }
  923. if (pkt_desc & AQ_RX_PD_DROP ||
  924. !(pkt_desc & AQ_RX_PD_RX_OK) ||
  925. pkt_len > (dev->hard_mtu + AQ_RX_HW_PAD)) {
  926. skb_pull(skb, pkt_len_with_padd);
  927. /* Next RX Packet Descriptor */
  928. pkt_desc_ptr++;
  929. continue;
  930. }
  931. new_skb = netdev_alloc_skb_ip_align(dev->net, pkt_len);
  932. if (!new_skb)
  933. goto err;
  934. skb_put(new_skb, pkt_len);
  935. memcpy(new_skb->data, skb->data, pkt_len);
  936. skb_pull(new_skb, AQ_RX_HW_PAD);
  937. if (aqc111_data->rx_checksum)
  938. aqc111_rx_checksum(new_skb, pkt_desc);
  939. if (pkt_desc & AQ_RX_PD_VLAN) {
  940. vlan_tag = pkt_desc >> AQ_RX_PD_VLAN_SHIFT;
  941. __vlan_hwaccel_put_tag(new_skb, htons(ETH_P_8021Q),
  942. vlan_tag & VLAN_VID_MASK);
  943. }
  944. usbnet_skb_return(dev, new_skb);
  945. if (pkt_count == 0)
  946. break;
  947. skb_pull(skb, pkt_len_with_padd);
  948. /* Next RX Packet Header */
  949. pkt_desc_ptr++;
  950. new_skb = NULL;
  951. }
  952. return 1;
  953. err:
  954. return 0;
  955. }
  956. static struct sk_buff *aqc111_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  957. gfp_t flags)
  958. {
  959. int frame_size = dev->maxpacket;
  960. struct sk_buff *new_skb = NULL;
  961. u64 *tx_desc_ptr = NULL;
  962. int padding_size = 0;
  963. int headroom = 0;
  964. int tailroom = 0;
  965. u64 tx_desc = 0;
  966. u16 tci = 0;
  967. /*Length of actual data*/
  968. tx_desc |= skb->len & AQ_TX_DESC_LEN_MASK;
  969. /* TSO MSS */
  970. tx_desc |= ((u64)(skb_shinfo(skb)->gso_size & AQ_TX_DESC_MSS_MASK)) <<
  971. AQ_TX_DESC_MSS_SHIFT;
  972. headroom = (skb->len + sizeof(tx_desc)) % 8;
  973. if (headroom != 0)
  974. padding_size = 8 - headroom;
  975. if (((skb->len + sizeof(tx_desc) + padding_size) % frame_size) == 0) {
  976. padding_size += 8;
  977. tx_desc |= AQ_TX_DESC_DROP_PADD;
  978. }
  979. /* Vlan Tag */
  980. if (vlan_get_tag(skb, &tci) >= 0) {
  981. tx_desc |= AQ_TX_DESC_VLAN;
  982. tx_desc |= ((u64)tci & AQ_TX_DESC_VLAN_MASK) <<
  983. AQ_TX_DESC_VLAN_SHIFT;
  984. }
  985. if (!dev->can_dma_sg && (dev->net->features & NETIF_F_SG) &&
  986. skb_linearize(skb))
  987. return NULL;
  988. headroom = skb_headroom(skb);
  989. tailroom = skb_tailroom(skb);
  990. if (!(headroom >= sizeof(tx_desc) && tailroom >= padding_size)) {
  991. new_skb = skb_copy_expand(skb, sizeof(tx_desc),
  992. padding_size, flags);
  993. dev_kfree_skb_any(skb);
  994. skb = new_skb;
  995. if (!skb)
  996. return NULL;
  997. }
  998. if (padding_size != 0)
  999. skb_put_zero(skb, padding_size);
  1000. /* Copy TX header */
  1001. tx_desc_ptr = skb_push(skb, sizeof(tx_desc));
  1002. *tx_desc_ptr = cpu_to_le64(tx_desc);
  1003. usbnet_set_skb_tx_stats(skb, 1, 0);
  1004. return skb;
  1005. }
  1006. static const struct driver_info aqc111_info = {
  1007. .description = "Aquantia AQtion USB to 5GbE Controller",
  1008. .bind = aqc111_bind,
  1009. .unbind = aqc111_unbind,
  1010. .status = aqc111_status,
  1011. .link_reset = aqc111_link_reset,
  1012. .reset = aqc111_reset,
  1013. .stop = aqc111_stop,
  1014. .flags = FLAG_ETHER | FLAG_FRAMING_AX |
  1015. FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
  1016. .rx_fixup = aqc111_rx_fixup,
  1017. .tx_fixup = aqc111_tx_fixup,
  1018. };
  1019. #define ASIX111_DESC \
  1020. "ASIX USB 3.1 Gen1 to 5G Multi-Gigabit Ethernet Adapter"
  1021. static const struct driver_info asix111_info = {
  1022. .description = ASIX111_DESC,
  1023. .bind = aqc111_bind,
  1024. .unbind = aqc111_unbind,
  1025. .status = aqc111_status,
  1026. .link_reset = aqc111_link_reset,
  1027. .reset = aqc111_reset,
  1028. .stop = aqc111_stop,
  1029. .flags = FLAG_ETHER | FLAG_FRAMING_AX |
  1030. FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
  1031. .rx_fixup = aqc111_rx_fixup,
  1032. .tx_fixup = aqc111_tx_fixup,
  1033. };
  1034. #undef ASIX111_DESC
  1035. #define ASIX112_DESC \
  1036. "ASIX USB 3.1 Gen1 to 2.5G Multi-Gigabit Ethernet Adapter"
  1037. static const struct driver_info asix112_info = {
  1038. .description = ASIX112_DESC,
  1039. .bind = aqc111_bind,
  1040. .unbind = aqc111_unbind,
  1041. .status = aqc111_status,
  1042. .link_reset = aqc111_link_reset,
  1043. .reset = aqc111_reset,
  1044. .stop = aqc111_stop,
  1045. .flags = FLAG_ETHER | FLAG_FRAMING_AX |
  1046. FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
  1047. .rx_fixup = aqc111_rx_fixup,
  1048. .tx_fixup = aqc111_tx_fixup,
  1049. };
  1050. #undef ASIX112_DESC
  1051. static const struct driver_info trendnet_info = {
  1052. .description = "USB-C 3.1 to 5GBASE-T Ethernet Adapter",
  1053. .bind = aqc111_bind,
  1054. .unbind = aqc111_unbind,
  1055. .status = aqc111_status,
  1056. .link_reset = aqc111_link_reset,
  1057. .reset = aqc111_reset,
  1058. .stop = aqc111_stop,
  1059. .flags = FLAG_ETHER | FLAG_FRAMING_AX |
  1060. FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
  1061. .rx_fixup = aqc111_rx_fixup,
  1062. .tx_fixup = aqc111_tx_fixup,
  1063. };
  1064. static const struct driver_info qnap_info = {
  1065. .description = "QNAP QNA-UC5G1T USB to 5GbE Adapter",
  1066. .bind = aqc111_bind,
  1067. .unbind = aqc111_unbind,
  1068. .status = aqc111_status,
  1069. .link_reset = aqc111_link_reset,
  1070. .reset = aqc111_reset,
  1071. .stop = aqc111_stop,
  1072. .flags = FLAG_ETHER | FLAG_FRAMING_AX |
  1073. FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
  1074. .rx_fixup = aqc111_rx_fixup,
  1075. .tx_fixup = aqc111_tx_fixup,
  1076. };
  1077. static int aqc111_suspend(struct usb_interface *intf, pm_message_t message)
  1078. {
  1079. struct usbnet *dev = usb_get_intfdata(intf);
  1080. struct aqc111_data *aqc111_data = dev->driver_priv;
  1081. u16 temp_rx_ctrl = 0x00;
  1082. u16 reg16;
  1083. u8 reg8;
  1084. usbnet_suspend(intf, message);
  1085. aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
  1086. temp_rx_ctrl = reg16;
  1087. /* Stop RX operations*/
  1088. reg16 &= ~SFR_RX_CTL_START;
  1089. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
  1090. /* Force bz */
  1091. aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
  1092. 2, &reg16);
  1093. reg16 |= SFR_PHYPWR_RSTCTL_BZ;
  1094. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
  1095. 2, &reg16);
  1096. reg8 = SFR_BULK_OUT_EFF_EN;
  1097. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
  1098. 1, 1, &reg8);
  1099. temp_rx_ctrl &= ~(SFR_RX_CTL_START | SFR_RX_CTL_RF_WAK |
  1100. SFR_RX_CTL_AP | SFR_RX_CTL_AM);
  1101. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
  1102. 2, &temp_rx_ctrl);
  1103. reg8 = 0x00;
  1104. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
  1105. 1, 1, &reg8);
  1106. if (aqc111_data->wol_flags) {
  1107. struct aqc111_wol_cfg wol_cfg;
  1108. memset(&wol_cfg, 0, sizeof(struct aqc111_wol_cfg));
  1109. aqc111_data->phy_cfg |= AQ_WOL;
  1110. ether_addr_copy(wol_cfg.hw_addr, dev->net->dev_addr);
  1111. wol_cfg.flags = aqc111_data->wol_flags;
  1112. temp_rx_ctrl |= (SFR_RX_CTL_AB | SFR_RX_CTL_START);
  1113. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
  1114. 2, &temp_rx_ctrl);
  1115. reg8 = 0x00;
  1116. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK,
  1117. 1, 1, &reg8);
  1118. reg8 = SFR_BMRX_DMA_EN;
  1119. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL,
  1120. 1, 1, &reg8);
  1121. reg8 = SFR_RX_PATH_READY;
  1122. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
  1123. 1, 1, &reg8);
  1124. reg8 = 0x07;
  1125. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL,
  1126. 1, 1, &reg8);
  1127. reg8 = 0x00;
  1128. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC,
  1129. SFR_RX_BULKIN_QTIMR_LOW, 1, 1, &reg8);
  1130. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC,
  1131. SFR_RX_BULKIN_QTIMR_HIGH, 1, 1, &reg8);
  1132. reg8 = 0xFF;
  1133. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QSIZE,
  1134. 1, 1, &reg8);
  1135. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QIFG,
  1136. 1, 1, &reg8);
  1137. aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC,
  1138. SFR_MEDIUM_STATUS_MODE, 2, &reg16);
  1139. reg16 |= SFR_MEDIUM_RECEIVE_EN;
  1140. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC,
  1141. SFR_MEDIUM_STATUS_MODE, 2, &reg16);
  1142. aqc111_write_cmd(dev, AQ_WOL_CFG, 0, 0,
  1143. WOL_CFG_SIZE, &wol_cfg);
  1144. aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
  1145. &aqc111_data->phy_cfg);
  1146. } else {
  1147. aqc111_data->phy_cfg |= AQ_LOW_POWER;
  1148. aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
  1149. &aqc111_data->phy_cfg);
  1150. /* Disable RX path */
  1151. aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC,
  1152. SFR_MEDIUM_STATUS_MODE, 2, &reg16);
  1153. reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
  1154. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC,
  1155. SFR_MEDIUM_STATUS_MODE, 2, &reg16);
  1156. }
  1157. return 0;
  1158. }
  1159. static int aqc111_resume(struct usb_interface *intf)
  1160. {
  1161. struct usbnet *dev = usb_get_intfdata(intf);
  1162. struct aqc111_data *aqc111_data = dev->driver_priv;
  1163. u16 reg16;
  1164. u8 reg8;
  1165. netif_carrier_off(dev->net);
  1166. /* Power up ethernet PHY */
  1167. aqc111_data->phy_cfg |= AQ_PHY_POWER_EN;
  1168. aqc111_data->phy_cfg &= ~AQ_LOW_POWER;
  1169. aqc111_data->phy_cfg &= ~AQ_WOL;
  1170. reg8 = 0xFF;
  1171. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK,
  1172. 1, 1, &reg8);
  1173. /* Configure RX control register => start operation */
  1174. reg16 = aqc111_data->rxctl;
  1175. reg16 &= ~SFR_RX_CTL_START;
  1176. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
  1177. reg16 |= SFR_RX_CTL_START;
  1178. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
  1179. aqc111_set_phy_speed(dev, aqc111_data->autoneg,
  1180. aqc111_data->advertised_speed);
  1181. aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  1182. 2, &reg16);
  1183. reg16 |= SFR_MEDIUM_RECEIVE_EN;
  1184. aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
  1185. 2, &reg16);
  1186. reg8 = SFR_RX_PATH_READY;
  1187. aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
  1188. 1, 1, &reg8);
  1189. reg8 = 0x0;
  1190. aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL, 1, 1, &reg8);
  1191. return usbnet_resume(intf);
  1192. }
  1193. #define AQC111_USB_ETH_DEV(vid, pid, table) \
  1194. USB_DEVICE_INTERFACE_CLASS((vid), (pid), USB_CLASS_VENDOR_SPEC), \
  1195. .driver_info = (unsigned long)&(table) \
  1196. }, \
  1197. { \
  1198. USB_DEVICE_AND_INTERFACE_INFO((vid), (pid), \
  1199. USB_CLASS_COMM, \
  1200. USB_CDC_SUBCLASS_ETHERNET, \
  1201. USB_CDC_PROTO_NONE), \
  1202. .driver_info = (unsigned long)&(table),
  1203. static const struct usb_device_id products[] = {
  1204. {AQC111_USB_ETH_DEV(0x2eca, 0xc101, aqc111_info)},
  1205. {AQC111_USB_ETH_DEV(0x0b95, 0x2790, asix111_info)},
  1206. {AQC111_USB_ETH_DEV(0x0b95, 0x2791, asix112_info)},
  1207. {AQC111_USB_ETH_DEV(0x20f4, 0xe05a, trendnet_info)},
  1208. {AQC111_USB_ETH_DEV(0x1c04, 0x0015, qnap_info)},
  1209. { },/* END */
  1210. };
  1211. MODULE_DEVICE_TABLE(usb, products);
  1212. static struct usb_driver aq_driver = {
  1213. .name = "aqc111",
  1214. .id_table = products,
  1215. .probe = usbnet_probe,
  1216. .suspend = aqc111_suspend,
  1217. .resume = aqc111_resume,
  1218. .disconnect = usbnet_disconnect,
  1219. };
  1220. module_usb_driver(aq_driver);
  1221. MODULE_DESCRIPTION("Aquantia AQtion USB to 5/2.5GbE Controllers");
  1222. MODULE_LICENSE("GPL");