aqc111.c 38 KB

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