smsc75xx.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /***************************************************************************
  2. *
  3. * Copyright (C) 2007-2010 SMSC
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. *****************************************************************************/
  20. #include <linux/module.h>
  21. #include <linux/kmod.h>
  22. #include <linux/init.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/mii.h>
  27. #include <linux/usb.h>
  28. #include <linux/crc32.h>
  29. #include <linux/usb/usbnet.h>
  30. #include <linux/slab.h>
  31. #include "smsc75xx.h"
  32. #define SMSC_CHIPNAME "smsc75xx"
  33. #define SMSC_DRIVER_VERSION "1.0.0"
  34. #define HS_USB_PKT_SIZE (512)
  35. #define FS_USB_PKT_SIZE (64)
  36. #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
  37. #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
  38. #define DEFAULT_BULK_IN_DELAY (0x00002000)
  39. #define MAX_SINGLE_PACKET_SIZE (9000)
  40. #define LAN75XX_EEPROM_MAGIC (0x7500)
  41. #define EEPROM_MAC_OFFSET (0x01)
  42. #define DEFAULT_TX_CSUM_ENABLE (true)
  43. #define DEFAULT_RX_CSUM_ENABLE (true)
  44. #define DEFAULT_TSO_ENABLE (true)
  45. #define SMSC75XX_INTERNAL_PHY_ID (1)
  46. #define SMSC75XX_TX_OVERHEAD (8)
  47. #define MAX_RX_FIFO_SIZE (20 * 1024)
  48. #define MAX_TX_FIFO_SIZE (12 * 1024)
  49. #define USB_VENDOR_ID_SMSC (0x0424)
  50. #define USB_PRODUCT_ID_LAN7500 (0x7500)
  51. #define USB_PRODUCT_ID_LAN7505 (0x7505)
  52. #define check_warn(ret, fmt, args...) \
  53. ({ if (ret < 0) netdev_warn(dev->net, fmt, ##args); })
  54. #define check_warn_return(ret, fmt, args...) \
  55. ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); return ret; } })
  56. #define check_warn_goto_done(ret, fmt, args...) \
  57. ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); goto done; } })
  58. struct smsc75xx_priv {
  59. struct usbnet *dev;
  60. u32 rfe_ctl;
  61. u32 multicast_hash_table[DP_SEL_VHF_HASH_LEN];
  62. struct mutex dataport_mutex;
  63. spinlock_t rfe_ctl_lock;
  64. struct work_struct set_multicast;
  65. };
  66. struct usb_context {
  67. struct usb_ctrlrequest req;
  68. struct usbnet *dev;
  69. };
  70. static int turbo_mode = true;
  71. module_param(turbo_mode, bool, 0644);
  72. MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
  73. static int __must_check smsc75xx_read_reg(struct usbnet *dev, u32 index,
  74. u32 *data)
  75. {
  76. u32 *buf = kmalloc(4, GFP_KERNEL);
  77. int ret;
  78. BUG_ON(!dev);
  79. if (!buf)
  80. return -ENOMEM;
  81. ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  82. USB_VENDOR_REQUEST_READ_REGISTER,
  83. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  84. 00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
  85. if (unlikely(ret < 0))
  86. netdev_warn(dev->net,
  87. "Failed to read register index 0x%08x", index);
  88. le32_to_cpus(buf);
  89. *data = *buf;
  90. kfree(buf);
  91. return ret;
  92. }
  93. static int __must_check smsc75xx_write_reg(struct usbnet *dev, u32 index,
  94. u32 data)
  95. {
  96. u32 *buf = kmalloc(4, GFP_KERNEL);
  97. int ret;
  98. BUG_ON(!dev);
  99. if (!buf)
  100. return -ENOMEM;
  101. *buf = data;
  102. cpu_to_le32s(buf);
  103. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  104. USB_VENDOR_REQUEST_WRITE_REGISTER,
  105. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  106. 00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
  107. if (unlikely(ret < 0))
  108. netdev_warn(dev->net,
  109. "Failed to write register index 0x%08x", index);
  110. kfree(buf);
  111. return ret;
  112. }
  113. /* Loop until the read is completed with timeout
  114. * called with phy_mutex held */
  115. static int smsc75xx_phy_wait_not_busy(struct usbnet *dev)
  116. {
  117. unsigned long start_time = jiffies;
  118. u32 val;
  119. int ret;
  120. do {
  121. ret = smsc75xx_read_reg(dev, MII_ACCESS, &val);
  122. check_warn_return(ret, "Error reading MII_ACCESS");
  123. if (!(val & MII_ACCESS_BUSY))
  124. return 0;
  125. } while (!time_after(jiffies, start_time + HZ));
  126. return -EIO;
  127. }
  128. static int smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
  129. {
  130. struct usbnet *dev = netdev_priv(netdev);
  131. u32 val, addr;
  132. int ret;
  133. mutex_lock(&dev->phy_mutex);
  134. /* confirm MII not busy */
  135. ret = smsc75xx_phy_wait_not_busy(dev);
  136. check_warn_goto_done(ret, "MII is busy in smsc75xx_mdio_read");
  137. /* set the address, index & direction (read from PHY) */
  138. phy_id &= dev->mii.phy_id_mask;
  139. idx &= dev->mii.reg_num_mask;
  140. addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
  141. | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
  142. | MII_ACCESS_READ;
  143. ret = smsc75xx_write_reg(dev, MII_ACCESS, addr);
  144. check_warn_goto_done(ret, "Error writing MII_ACCESS");
  145. ret = smsc75xx_phy_wait_not_busy(dev);
  146. check_warn_goto_done(ret, "Timed out reading MII reg %02X", idx);
  147. ret = smsc75xx_read_reg(dev, MII_DATA, &val);
  148. check_warn_goto_done(ret, "Error reading MII_DATA");
  149. ret = (u16)(val & 0xFFFF);
  150. done:
  151. mutex_unlock(&dev->phy_mutex);
  152. return ret;
  153. }
  154. static void smsc75xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
  155. int regval)
  156. {
  157. struct usbnet *dev = netdev_priv(netdev);
  158. u32 val, addr;
  159. int ret;
  160. mutex_lock(&dev->phy_mutex);
  161. /* confirm MII not busy */
  162. ret = smsc75xx_phy_wait_not_busy(dev);
  163. check_warn_goto_done(ret, "MII is busy in smsc75xx_mdio_write");
  164. val = regval;
  165. ret = smsc75xx_write_reg(dev, MII_DATA, val);
  166. check_warn_goto_done(ret, "Error writing MII_DATA");
  167. /* set the address, index & direction (write to PHY) */
  168. phy_id &= dev->mii.phy_id_mask;
  169. idx &= dev->mii.reg_num_mask;
  170. addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
  171. | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
  172. | MII_ACCESS_WRITE;
  173. ret = smsc75xx_write_reg(dev, MII_ACCESS, addr);
  174. check_warn_goto_done(ret, "Error writing MII_ACCESS");
  175. ret = smsc75xx_phy_wait_not_busy(dev);
  176. check_warn_goto_done(ret, "Timed out writing MII reg %02X", idx);
  177. done:
  178. mutex_unlock(&dev->phy_mutex);
  179. }
  180. static int smsc75xx_wait_eeprom(struct usbnet *dev)
  181. {
  182. unsigned long start_time = jiffies;
  183. u32 val;
  184. int ret;
  185. do {
  186. ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
  187. check_warn_return(ret, "Error reading E2P_CMD");
  188. if (!(val & E2P_CMD_BUSY) || (val & E2P_CMD_TIMEOUT))
  189. break;
  190. udelay(40);
  191. } while (!time_after(jiffies, start_time + HZ));
  192. if (val & (E2P_CMD_TIMEOUT | E2P_CMD_BUSY)) {
  193. netdev_warn(dev->net, "EEPROM read operation timeout");
  194. return -EIO;
  195. }
  196. return 0;
  197. }
  198. static int smsc75xx_eeprom_confirm_not_busy(struct usbnet *dev)
  199. {
  200. unsigned long start_time = jiffies;
  201. u32 val;
  202. int ret;
  203. do {
  204. ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
  205. check_warn_return(ret, "Error reading E2P_CMD");
  206. if (!(val & E2P_CMD_BUSY))
  207. return 0;
  208. udelay(40);
  209. } while (!time_after(jiffies, start_time + HZ));
  210. netdev_warn(dev->net, "EEPROM is busy");
  211. return -EIO;
  212. }
  213. static int smsc75xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
  214. u8 *data)
  215. {
  216. u32 val;
  217. int i, ret;
  218. BUG_ON(!dev);
  219. BUG_ON(!data);
  220. ret = smsc75xx_eeprom_confirm_not_busy(dev);
  221. if (ret)
  222. return ret;
  223. for (i = 0; i < length; i++) {
  224. val = E2P_CMD_BUSY | E2P_CMD_READ | (offset & E2P_CMD_ADDR);
  225. ret = smsc75xx_write_reg(dev, E2P_CMD, val);
  226. check_warn_return(ret, "Error writing E2P_CMD");
  227. ret = smsc75xx_wait_eeprom(dev);
  228. if (ret < 0)
  229. return ret;
  230. ret = smsc75xx_read_reg(dev, E2P_DATA, &val);
  231. check_warn_return(ret, "Error reading E2P_DATA");
  232. data[i] = val & 0xFF;
  233. offset++;
  234. }
  235. return 0;
  236. }
  237. static int smsc75xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
  238. u8 *data)
  239. {
  240. u32 val;
  241. int i, ret;
  242. BUG_ON(!dev);
  243. BUG_ON(!data);
  244. ret = smsc75xx_eeprom_confirm_not_busy(dev);
  245. if (ret)
  246. return ret;
  247. /* Issue write/erase enable command */
  248. val = E2P_CMD_BUSY | E2P_CMD_EWEN;
  249. ret = smsc75xx_write_reg(dev, E2P_CMD, val);
  250. check_warn_return(ret, "Error writing E2P_CMD");
  251. ret = smsc75xx_wait_eeprom(dev);
  252. if (ret < 0)
  253. return ret;
  254. for (i = 0; i < length; i++) {
  255. /* Fill data register */
  256. val = data[i];
  257. ret = smsc75xx_write_reg(dev, E2P_DATA, val);
  258. check_warn_return(ret, "Error writing E2P_DATA");
  259. /* Send "write" command */
  260. val = E2P_CMD_BUSY | E2P_CMD_WRITE | (offset & E2P_CMD_ADDR);
  261. ret = smsc75xx_write_reg(dev, E2P_CMD, val);
  262. check_warn_return(ret, "Error writing E2P_CMD");
  263. ret = smsc75xx_wait_eeprom(dev);
  264. if (ret < 0)
  265. return ret;
  266. offset++;
  267. }
  268. return 0;
  269. }
  270. static int smsc75xx_dataport_wait_not_busy(struct usbnet *dev)
  271. {
  272. int i, ret;
  273. for (i = 0; i < 100; i++) {
  274. u32 dp_sel;
  275. ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
  276. check_warn_return(ret, "Error reading DP_SEL");
  277. if (dp_sel & DP_SEL_DPRDY)
  278. return 0;
  279. udelay(40);
  280. }
  281. netdev_warn(dev->net, "smsc75xx_dataport_wait_not_busy timed out");
  282. return -EIO;
  283. }
  284. static int smsc75xx_dataport_write(struct usbnet *dev, u32 ram_select, u32 addr,
  285. u32 length, u32 *buf)
  286. {
  287. struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
  288. u32 dp_sel;
  289. int i, ret;
  290. mutex_lock(&pdata->dataport_mutex);
  291. ret = smsc75xx_dataport_wait_not_busy(dev);
  292. check_warn_goto_done(ret, "smsc75xx_dataport_write busy on entry");
  293. ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
  294. check_warn_goto_done(ret, "Error reading DP_SEL");
  295. dp_sel &= ~DP_SEL_RSEL;
  296. dp_sel |= ram_select;
  297. ret = smsc75xx_write_reg(dev, DP_SEL, dp_sel);
  298. check_warn_goto_done(ret, "Error writing DP_SEL");
  299. for (i = 0; i < length; i++) {
  300. ret = smsc75xx_write_reg(dev, DP_ADDR, addr + i);
  301. check_warn_goto_done(ret, "Error writing DP_ADDR");
  302. ret = smsc75xx_write_reg(dev, DP_DATA, buf[i]);
  303. check_warn_goto_done(ret, "Error writing DP_DATA");
  304. ret = smsc75xx_write_reg(dev, DP_CMD, DP_CMD_WRITE);
  305. check_warn_goto_done(ret, "Error writing DP_CMD");
  306. ret = smsc75xx_dataport_wait_not_busy(dev);
  307. check_warn_goto_done(ret, "smsc75xx_dataport_write timeout");
  308. }
  309. done:
  310. mutex_unlock(&pdata->dataport_mutex);
  311. return ret;
  312. }
  313. /* returns hash bit number for given MAC address */
  314. static u32 smsc75xx_hash(char addr[ETH_ALEN])
  315. {
  316. return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
  317. }
  318. static void smsc75xx_deferred_multicast_write(struct work_struct *param)
  319. {
  320. struct smsc75xx_priv *pdata =
  321. container_of(param, struct smsc75xx_priv, set_multicast);
  322. struct usbnet *dev = pdata->dev;
  323. int ret;
  324. netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x",
  325. pdata->rfe_ctl);
  326. smsc75xx_dataport_write(dev, DP_SEL_VHF, DP_SEL_VHF_VLAN_LEN,
  327. DP_SEL_VHF_HASH_LEN, pdata->multicast_hash_table);
  328. ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
  329. check_warn(ret, "Error writing RFE_CRL");
  330. }
  331. static void smsc75xx_set_multicast(struct net_device *netdev)
  332. {
  333. struct usbnet *dev = netdev_priv(netdev);
  334. struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
  335. unsigned long flags;
  336. int i;
  337. spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
  338. pdata->rfe_ctl &=
  339. ~(RFE_CTL_AU | RFE_CTL_AM | RFE_CTL_DPF | RFE_CTL_MHF);
  340. pdata->rfe_ctl |= RFE_CTL_AB;
  341. for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
  342. pdata->multicast_hash_table[i] = 0;
  343. if (dev->net->flags & IFF_PROMISC) {
  344. netif_dbg(dev, drv, dev->net, "promiscuous mode enabled");
  345. pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_AU;
  346. } else if (dev->net->flags & IFF_ALLMULTI) {
  347. netif_dbg(dev, drv, dev->net, "receive all multicast enabled");
  348. pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_DPF;
  349. } else if (!netdev_mc_empty(dev->net)) {
  350. struct netdev_hw_addr *ha;
  351. netif_dbg(dev, drv, dev->net, "receive multicast hash filter");
  352. pdata->rfe_ctl |= RFE_CTL_MHF | RFE_CTL_DPF;
  353. netdev_for_each_mc_addr(ha, netdev) {
  354. u32 bitnum = smsc75xx_hash(ha->addr);
  355. pdata->multicast_hash_table[bitnum / 32] |=
  356. (1 << (bitnum % 32));
  357. }
  358. } else {
  359. netif_dbg(dev, drv, dev->net, "receive own packets only");
  360. pdata->rfe_ctl |= RFE_CTL_DPF;
  361. }
  362. spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
  363. /* defer register writes to a sleepable context */
  364. schedule_work(&pdata->set_multicast);
  365. }
  366. static int smsc75xx_update_flowcontrol(struct usbnet *dev, u8 duplex,
  367. u16 lcladv, u16 rmtadv)
  368. {
  369. u32 flow = 0, fct_flow = 0;
  370. int ret;
  371. if (duplex == DUPLEX_FULL) {
  372. u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  373. if (cap & FLOW_CTRL_TX) {
  374. flow = (FLOW_TX_FCEN | 0xFFFF);
  375. /* set fct_flow thresholds to 20% and 80% */
  376. fct_flow = (8 << 8) | 32;
  377. }
  378. if (cap & FLOW_CTRL_RX)
  379. flow |= FLOW_RX_FCEN;
  380. netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s",
  381. (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
  382. (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
  383. } else {
  384. netif_dbg(dev, link, dev->net, "half duplex");
  385. }
  386. ret = smsc75xx_write_reg(dev, FLOW, flow);
  387. check_warn_return(ret, "Error writing FLOW");
  388. ret = smsc75xx_write_reg(dev, FCT_FLOW, fct_flow);
  389. check_warn_return(ret, "Error writing FCT_FLOW");
  390. return 0;
  391. }
  392. static int smsc75xx_link_reset(struct usbnet *dev)
  393. {
  394. struct mii_if_info *mii = &dev->mii;
  395. struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
  396. u16 lcladv, rmtadv;
  397. int ret;
  398. /* clear interrupt status */
  399. ret = smsc75xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
  400. check_warn_return(ret, "Error reading PHY_INT_SRC");
  401. ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
  402. check_warn_return(ret, "Error writing INT_STS");
  403. mii_check_media(mii, 1, 1);
  404. mii_ethtool_gset(&dev->mii, &ecmd);
  405. lcladv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
  406. rmtadv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
  407. netif_dbg(dev, link, dev->net, "speed: %u duplex: %d lcladv: %04x"
  408. " rmtadv: %04x", ethtool_cmd_speed(&ecmd),
  409. ecmd.duplex, lcladv, rmtadv);
  410. return smsc75xx_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
  411. }
  412. static void smsc75xx_status(struct usbnet *dev, struct urb *urb)
  413. {
  414. u32 intdata;
  415. if (urb->actual_length != 4) {
  416. netdev_warn(dev->net,
  417. "unexpected urb length %d", urb->actual_length);
  418. return;
  419. }
  420. memcpy(&intdata, urb->transfer_buffer, 4);
  421. le32_to_cpus(&intdata);
  422. netif_dbg(dev, link, dev->net, "intdata: 0x%08X", intdata);
  423. if (intdata & INT_ENP_PHY_INT)
  424. usbnet_defer_kevent(dev, EVENT_LINK_RESET);
  425. else
  426. netdev_warn(dev->net,
  427. "unexpected interrupt, intdata=0x%08X", intdata);
  428. }
  429. static int smsc75xx_ethtool_get_eeprom_len(struct net_device *net)
  430. {
  431. return MAX_EEPROM_SIZE;
  432. }
  433. static int smsc75xx_ethtool_get_eeprom(struct net_device *netdev,
  434. struct ethtool_eeprom *ee, u8 *data)
  435. {
  436. struct usbnet *dev = netdev_priv(netdev);
  437. ee->magic = LAN75XX_EEPROM_MAGIC;
  438. return smsc75xx_read_eeprom(dev, ee->offset, ee->len, data);
  439. }
  440. static int smsc75xx_ethtool_set_eeprom(struct net_device *netdev,
  441. struct ethtool_eeprom *ee, u8 *data)
  442. {
  443. struct usbnet *dev = netdev_priv(netdev);
  444. if (ee->magic != LAN75XX_EEPROM_MAGIC) {
  445. netdev_warn(dev->net,
  446. "EEPROM: magic value mismatch: 0x%x", ee->magic);
  447. return -EINVAL;
  448. }
  449. return smsc75xx_write_eeprom(dev, ee->offset, ee->len, data);
  450. }
  451. static const struct ethtool_ops smsc75xx_ethtool_ops = {
  452. .get_link = usbnet_get_link,
  453. .nway_reset = usbnet_nway_reset,
  454. .get_drvinfo = usbnet_get_drvinfo,
  455. .get_msglevel = usbnet_get_msglevel,
  456. .set_msglevel = usbnet_set_msglevel,
  457. .get_settings = usbnet_get_settings,
  458. .set_settings = usbnet_set_settings,
  459. .get_eeprom_len = smsc75xx_ethtool_get_eeprom_len,
  460. .get_eeprom = smsc75xx_ethtool_get_eeprom,
  461. .set_eeprom = smsc75xx_ethtool_set_eeprom,
  462. };
  463. static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  464. {
  465. struct usbnet *dev = netdev_priv(netdev);
  466. if (!netif_running(netdev))
  467. return -EINVAL;
  468. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  469. }
  470. static void smsc75xx_init_mac_address(struct usbnet *dev)
  471. {
  472. /* try reading mac address from EEPROM */
  473. if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
  474. dev->net->dev_addr) == 0) {
  475. if (is_valid_ether_addr(dev->net->dev_addr)) {
  476. /* eeprom values are valid so use them */
  477. netif_dbg(dev, ifup, dev->net,
  478. "MAC address read from EEPROM");
  479. return;
  480. }
  481. }
  482. /* no eeprom, or eeprom values are invalid. generate random MAC */
  483. random_ether_addr(dev->net->dev_addr);
  484. netif_dbg(dev, ifup, dev->net, "MAC address set to random_ether_addr");
  485. }
  486. static int smsc75xx_set_mac_address(struct usbnet *dev)
  487. {
  488. u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
  489. dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
  490. u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
  491. int ret = smsc75xx_write_reg(dev, RX_ADDRH, addr_hi);
  492. check_warn_return(ret, "Failed to write RX_ADDRH: %d", ret);
  493. ret = smsc75xx_write_reg(dev, RX_ADDRL, addr_lo);
  494. check_warn_return(ret, "Failed to write RX_ADDRL: %d", ret);
  495. addr_hi |= ADDR_FILTX_FB_VALID;
  496. ret = smsc75xx_write_reg(dev, ADDR_FILTX, addr_hi);
  497. check_warn_return(ret, "Failed to write ADDR_FILTX: %d", ret);
  498. ret = smsc75xx_write_reg(dev, ADDR_FILTX + 4, addr_lo);
  499. check_warn_return(ret, "Failed to write ADDR_FILTX+4: %d", ret);
  500. return 0;
  501. }
  502. static int smsc75xx_phy_initialize(struct usbnet *dev)
  503. {
  504. int bmcr, timeout = 0;
  505. /* Initialize MII structure */
  506. dev->mii.dev = dev->net;
  507. dev->mii.mdio_read = smsc75xx_mdio_read;
  508. dev->mii.mdio_write = smsc75xx_mdio_write;
  509. dev->mii.phy_id_mask = 0x1f;
  510. dev->mii.reg_num_mask = 0x1f;
  511. dev->mii.phy_id = SMSC75XX_INTERNAL_PHY_ID;
  512. /* reset phy and wait for reset to complete */
  513. smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  514. do {
  515. msleep(10);
  516. bmcr = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
  517. check_warn_return(bmcr, "Error reading MII_BMCR");
  518. timeout++;
  519. } while ((bmcr & MII_BMCR) && (timeout < 100));
  520. if (timeout >= 100) {
  521. netdev_warn(dev->net, "timeout on PHY Reset");
  522. return -EIO;
  523. }
  524. smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  525. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
  526. ADVERTISE_PAUSE_ASYM);
  527. /* read to clear */
  528. smsc75xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
  529. check_warn_return(bmcr, "Error reading PHY_INT_SRC");
  530. smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
  531. PHY_INT_MASK_DEFAULT);
  532. mii_nway_restart(&dev->mii);
  533. netif_dbg(dev, ifup, dev->net, "phy initialised successfully");
  534. return 0;
  535. }
  536. static int smsc75xx_set_rx_max_frame_length(struct usbnet *dev, int size)
  537. {
  538. int ret = 0;
  539. u32 buf;
  540. bool rxenabled;
  541. ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
  542. check_warn_return(ret, "Failed to read MAC_RX: %d", ret);
  543. rxenabled = ((buf & MAC_RX_RXEN) != 0);
  544. if (rxenabled) {
  545. buf &= ~MAC_RX_RXEN;
  546. ret = smsc75xx_write_reg(dev, MAC_RX, buf);
  547. check_warn_return(ret, "Failed to write MAC_RX: %d", ret);
  548. }
  549. /* add 4 to size for FCS */
  550. buf &= ~MAC_RX_MAX_SIZE;
  551. buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT) & MAC_RX_MAX_SIZE);
  552. ret = smsc75xx_write_reg(dev, MAC_RX, buf);
  553. check_warn_return(ret, "Failed to write MAC_RX: %d", ret);
  554. if (rxenabled) {
  555. buf |= MAC_RX_RXEN;
  556. ret = smsc75xx_write_reg(dev, MAC_RX, buf);
  557. check_warn_return(ret, "Failed to write MAC_RX: %d", ret);
  558. }
  559. return 0;
  560. }
  561. static int smsc75xx_change_mtu(struct net_device *netdev, int new_mtu)
  562. {
  563. struct usbnet *dev = netdev_priv(netdev);
  564. int ret = smsc75xx_set_rx_max_frame_length(dev, new_mtu);
  565. check_warn_return(ret, "Failed to set mac rx frame length");
  566. return usbnet_change_mtu(netdev, new_mtu);
  567. }
  568. /* Enable or disable Rx checksum offload engine */
  569. static int smsc75xx_set_features(struct net_device *netdev, u32 features)
  570. {
  571. struct usbnet *dev = netdev_priv(netdev);
  572. struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
  573. unsigned long flags;
  574. int ret;
  575. spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
  576. if (features & NETIF_F_RXCSUM)
  577. pdata->rfe_ctl |= RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM;
  578. else
  579. pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM);
  580. spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
  581. /* it's racing here! */
  582. ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
  583. check_warn_return(ret, "Error writing RFE_CTL");
  584. return 0;
  585. }
  586. static int smsc75xx_reset(struct usbnet *dev)
  587. {
  588. struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
  589. u32 buf;
  590. int ret = 0, timeout;
  591. netif_dbg(dev, ifup, dev->net, "entering smsc75xx_reset");
  592. ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
  593. check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
  594. buf |= HW_CFG_LRST;
  595. ret = smsc75xx_write_reg(dev, HW_CFG, buf);
  596. check_warn_return(ret, "Failed to write HW_CFG: %d", ret);
  597. timeout = 0;
  598. do {
  599. msleep(10);
  600. ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
  601. check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
  602. timeout++;
  603. } while ((buf & HW_CFG_LRST) && (timeout < 100));
  604. if (timeout >= 100) {
  605. netdev_warn(dev->net, "timeout on completion of Lite Reset");
  606. return -EIO;
  607. }
  608. netif_dbg(dev, ifup, dev->net, "Lite reset complete, resetting PHY");
  609. ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
  610. check_warn_return(ret, "Failed to read PMT_CTL: %d", ret);
  611. buf |= PMT_CTL_PHY_RST;
  612. ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
  613. check_warn_return(ret, "Failed to write PMT_CTL: %d", ret);
  614. timeout = 0;
  615. do {
  616. msleep(10);
  617. ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
  618. check_warn_return(ret, "Failed to read PMT_CTL: %d", ret);
  619. timeout++;
  620. } while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
  621. if (timeout >= 100) {
  622. netdev_warn(dev->net, "timeout waiting for PHY Reset");
  623. return -EIO;
  624. }
  625. netif_dbg(dev, ifup, dev->net, "PHY reset complete");
  626. smsc75xx_init_mac_address(dev);
  627. ret = smsc75xx_set_mac_address(dev);
  628. check_warn_return(ret, "Failed to set mac address");
  629. netif_dbg(dev, ifup, dev->net, "MAC Address: %pM", dev->net->dev_addr);
  630. ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
  631. check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
  632. netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x", buf);
  633. buf |= HW_CFG_BIR;
  634. ret = smsc75xx_write_reg(dev, HW_CFG, buf);
  635. check_warn_return(ret, "Failed to write HW_CFG: %d", ret);
  636. ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
  637. check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
  638. netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG after "
  639. "writing HW_CFG_BIR: 0x%08x", buf);
  640. if (!turbo_mode) {
  641. buf = 0;
  642. dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
  643. } else if (dev->udev->speed == USB_SPEED_HIGH) {
  644. buf = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
  645. dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
  646. } else {
  647. buf = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
  648. dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
  649. }
  650. netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld",
  651. (ulong)dev->rx_urb_size);
  652. ret = smsc75xx_write_reg(dev, BURST_CAP, buf);
  653. check_warn_return(ret, "Failed to write BURST_CAP: %d", ret);
  654. ret = smsc75xx_read_reg(dev, BURST_CAP, &buf);
  655. check_warn_return(ret, "Failed to read BURST_CAP: %d", ret);
  656. netif_dbg(dev, ifup, dev->net,
  657. "Read Value from BURST_CAP after writing: 0x%08x", buf);
  658. ret = smsc75xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
  659. check_warn_return(ret, "Failed to write BULK_IN_DLY: %d", ret);
  660. ret = smsc75xx_read_reg(dev, BULK_IN_DLY, &buf);
  661. check_warn_return(ret, "Failed to read BULK_IN_DLY: %d", ret);
  662. netif_dbg(dev, ifup, dev->net,
  663. "Read Value from BULK_IN_DLY after writing: 0x%08x", buf);
  664. if (turbo_mode) {
  665. ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
  666. check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
  667. netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x", buf);
  668. buf |= (HW_CFG_MEF | HW_CFG_BCE);
  669. ret = smsc75xx_write_reg(dev, HW_CFG, buf);
  670. check_warn_return(ret, "Failed to write HW_CFG: %d", ret);
  671. ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
  672. check_warn_return(ret, "Failed to read HW_CFG: %d", ret);
  673. netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x", buf);
  674. }
  675. /* set FIFO sizes */
  676. buf = (MAX_RX_FIFO_SIZE - 512) / 512;
  677. ret = smsc75xx_write_reg(dev, FCT_RX_FIFO_END, buf);
  678. check_warn_return(ret, "Failed to write FCT_RX_FIFO_END: %d", ret);
  679. netif_dbg(dev, ifup, dev->net, "FCT_RX_FIFO_END set to 0x%08x", buf);
  680. buf = (MAX_TX_FIFO_SIZE - 512) / 512;
  681. ret = smsc75xx_write_reg(dev, FCT_TX_FIFO_END, buf);
  682. check_warn_return(ret, "Failed to write FCT_TX_FIFO_END: %d", ret);
  683. netif_dbg(dev, ifup, dev->net, "FCT_TX_FIFO_END set to 0x%08x", buf);
  684. ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
  685. check_warn_return(ret, "Failed to write INT_STS: %d", ret);
  686. ret = smsc75xx_read_reg(dev, ID_REV, &buf);
  687. check_warn_return(ret, "Failed to read ID_REV: %d", ret);
  688. netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x", buf);
  689. /* Configure GPIO pins as LED outputs */
  690. ret = smsc75xx_read_reg(dev, LED_GPIO_CFG, &buf);
  691. check_warn_return(ret, "Failed to read LED_GPIO_CFG: %d", ret);
  692. buf &= ~(LED_GPIO_CFG_LED2_FUN_SEL | LED_GPIO_CFG_LED10_FUN_SEL);
  693. buf |= LED_GPIO_CFG_LEDGPIO_EN | LED_GPIO_CFG_LED2_FUN_SEL;
  694. ret = smsc75xx_write_reg(dev, LED_GPIO_CFG, buf);
  695. check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d", ret);
  696. ret = smsc75xx_write_reg(dev, FLOW, 0);
  697. check_warn_return(ret, "Failed to write FLOW: %d", ret);
  698. ret = smsc75xx_write_reg(dev, FCT_FLOW, 0);
  699. check_warn_return(ret, "Failed to write FCT_FLOW: %d", ret);
  700. /* Don't need rfe_ctl_lock during initialisation */
  701. ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
  702. check_warn_return(ret, "Failed to read RFE_CTL: %d", ret);
  703. pdata->rfe_ctl |= RFE_CTL_AB | RFE_CTL_DPF;
  704. ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
  705. check_warn_return(ret, "Failed to write RFE_CTL: %d", ret);
  706. ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
  707. check_warn_return(ret, "Failed to read RFE_CTL: %d", ret);
  708. netif_dbg(dev, ifup, dev->net, "RFE_CTL set to 0x%08x", pdata->rfe_ctl);
  709. /* Enable or disable checksum offload engines */
  710. smsc75xx_set_features(dev->net, dev->net->features);
  711. smsc75xx_set_multicast(dev->net);
  712. ret = smsc75xx_phy_initialize(dev);
  713. check_warn_return(ret, "Failed to initialize PHY: %d", ret);
  714. ret = smsc75xx_read_reg(dev, INT_EP_CTL, &buf);
  715. check_warn_return(ret, "Failed to read INT_EP_CTL: %d", ret);
  716. /* enable PHY interrupts */
  717. buf |= INT_ENP_PHY_INT;
  718. ret = smsc75xx_write_reg(dev, INT_EP_CTL, buf);
  719. check_warn_return(ret, "Failed to write INT_EP_CTL: %d", ret);
  720. ret = smsc75xx_read_reg(dev, MAC_TX, &buf);
  721. check_warn_return(ret, "Failed to read MAC_TX: %d", ret);
  722. buf |= MAC_TX_TXEN;
  723. ret = smsc75xx_write_reg(dev, MAC_TX, buf);
  724. check_warn_return(ret, "Failed to write MAC_TX: %d", ret);
  725. netif_dbg(dev, ifup, dev->net, "MAC_TX set to 0x%08x", buf);
  726. ret = smsc75xx_read_reg(dev, FCT_TX_CTL, &buf);
  727. check_warn_return(ret, "Failed to read FCT_TX_CTL: %d", ret);
  728. buf |= FCT_TX_CTL_EN;
  729. ret = smsc75xx_write_reg(dev, FCT_TX_CTL, buf);
  730. check_warn_return(ret, "Failed to write FCT_TX_CTL: %d", ret);
  731. netif_dbg(dev, ifup, dev->net, "FCT_TX_CTL set to 0x%08x", buf);
  732. ret = smsc75xx_set_rx_max_frame_length(dev, 1514);
  733. check_warn_return(ret, "Failed to set max rx frame length");
  734. ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
  735. check_warn_return(ret, "Failed to read MAC_RX: %d", ret);
  736. buf |= MAC_RX_RXEN;
  737. ret = smsc75xx_write_reg(dev, MAC_RX, buf);
  738. check_warn_return(ret, "Failed to write MAC_RX: %d", ret);
  739. netif_dbg(dev, ifup, dev->net, "MAC_RX set to 0x%08x", buf);
  740. ret = smsc75xx_read_reg(dev, FCT_RX_CTL, &buf);
  741. check_warn_return(ret, "Failed to read FCT_RX_CTL: %d", ret);
  742. buf |= FCT_RX_CTL_EN;
  743. ret = smsc75xx_write_reg(dev, FCT_RX_CTL, buf);
  744. check_warn_return(ret, "Failed to write FCT_RX_CTL: %d", ret);
  745. netif_dbg(dev, ifup, dev->net, "FCT_RX_CTL set to 0x%08x", buf);
  746. netif_dbg(dev, ifup, dev->net, "smsc75xx_reset, return 0");
  747. return 0;
  748. }
  749. static const struct net_device_ops smsc75xx_netdev_ops = {
  750. .ndo_open = usbnet_open,
  751. .ndo_stop = usbnet_stop,
  752. .ndo_start_xmit = usbnet_start_xmit,
  753. .ndo_tx_timeout = usbnet_tx_timeout,
  754. .ndo_change_mtu = smsc75xx_change_mtu,
  755. .ndo_set_mac_address = eth_mac_addr,
  756. .ndo_validate_addr = eth_validate_addr,
  757. .ndo_do_ioctl = smsc75xx_ioctl,
  758. .ndo_set_multicast_list = smsc75xx_set_multicast,
  759. .ndo_set_features = smsc75xx_set_features,
  760. };
  761. static int smsc75xx_bind(struct usbnet *dev, struct usb_interface *intf)
  762. {
  763. struct smsc75xx_priv *pdata = NULL;
  764. int ret;
  765. printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
  766. ret = usbnet_get_endpoints(dev, intf);
  767. check_warn_return(ret, "usbnet_get_endpoints failed: %d", ret);
  768. dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc75xx_priv),
  769. GFP_KERNEL);
  770. pdata = (struct smsc75xx_priv *)(dev->data[0]);
  771. if (!pdata) {
  772. netdev_warn(dev->net, "Unable to allocate smsc75xx_priv");
  773. return -ENOMEM;
  774. }
  775. pdata->dev = dev;
  776. spin_lock_init(&pdata->rfe_ctl_lock);
  777. mutex_init(&pdata->dataport_mutex);
  778. INIT_WORK(&pdata->set_multicast, smsc75xx_deferred_multicast_write);
  779. if (DEFAULT_TX_CSUM_ENABLE) {
  780. dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  781. if (DEFAULT_TSO_ENABLE)
  782. dev->net->features |= NETIF_F_SG |
  783. NETIF_F_TSO | NETIF_F_TSO6;
  784. }
  785. if (DEFAULT_RX_CSUM_ENABLE)
  786. dev->net->features |= NETIF_F_RXCSUM;
  787. dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  788. NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_RXCSUM;
  789. /* Init all registers */
  790. ret = smsc75xx_reset(dev);
  791. dev->net->netdev_ops = &smsc75xx_netdev_ops;
  792. dev->net->ethtool_ops = &smsc75xx_ethtool_ops;
  793. dev->net->flags |= IFF_MULTICAST;
  794. dev->net->hard_header_len += SMSC75XX_TX_OVERHEAD;
  795. dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
  796. return 0;
  797. }
  798. static void smsc75xx_unbind(struct usbnet *dev, struct usb_interface *intf)
  799. {
  800. struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
  801. if (pdata) {
  802. netif_dbg(dev, ifdown, dev->net, "free pdata");
  803. kfree(pdata);
  804. pdata = NULL;
  805. dev->data[0] = 0;
  806. }
  807. }
  808. static void smsc75xx_rx_csum_offload(struct usbnet *dev, struct sk_buff *skb,
  809. u32 rx_cmd_a, u32 rx_cmd_b)
  810. {
  811. if (!(dev->net->features & NETIF_F_RXCSUM) ||
  812. unlikely(rx_cmd_a & RX_CMD_A_LCSM)) {
  813. skb->ip_summed = CHECKSUM_NONE;
  814. } else {
  815. skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT));
  816. skb->ip_summed = CHECKSUM_COMPLETE;
  817. }
  818. }
  819. static int smsc75xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  820. {
  821. while (skb->len > 0) {
  822. u32 rx_cmd_a, rx_cmd_b, align_count, size;
  823. struct sk_buff *ax_skb;
  824. unsigned char *packet;
  825. memcpy(&rx_cmd_a, skb->data, sizeof(rx_cmd_a));
  826. le32_to_cpus(&rx_cmd_a);
  827. skb_pull(skb, 4);
  828. memcpy(&rx_cmd_b, skb->data, sizeof(rx_cmd_b));
  829. le32_to_cpus(&rx_cmd_b);
  830. skb_pull(skb, 4 + NET_IP_ALIGN);
  831. packet = skb->data;
  832. /* get the packet length */
  833. size = (rx_cmd_a & RX_CMD_A_LEN) - NET_IP_ALIGN;
  834. align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
  835. if (unlikely(rx_cmd_a & RX_CMD_A_RED)) {
  836. netif_dbg(dev, rx_err, dev->net,
  837. "Error rx_cmd_a=0x%08x", rx_cmd_a);
  838. dev->net->stats.rx_errors++;
  839. dev->net->stats.rx_dropped++;
  840. if (rx_cmd_a & RX_CMD_A_FCS)
  841. dev->net->stats.rx_crc_errors++;
  842. else if (rx_cmd_a & (RX_CMD_A_LONG | RX_CMD_A_RUNT))
  843. dev->net->stats.rx_frame_errors++;
  844. } else {
  845. /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
  846. if (unlikely(size > (ETH_FRAME_LEN + 12))) {
  847. netif_dbg(dev, rx_err, dev->net,
  848. "size err rx_cmd_a=0x%08x", rx_cmd_a);
  849. return 0;
  850. }
  851. /* last frame in this batch */
  852. if (skb->len == size) {
  853. smsc75xx_rx_csum_offload(dev, skb, rx_cmd_a,
  854. rx_cmd_b);
  855. skb_trim(skb, skb->len - 4); /* remove fcs */
  856. skb->truesize = size + sizeof(struct sk_buff);
  857. return 1;
  858. }
  859. ax_skb = skb_clone(skb, GFP_ATOMIC);
  860. if (unlikely(!ax_skb)) {
  861. netdev_warn(dev->net, "Error allocating skb");
  862. return 0;
  863. }
  864. ax_skb->len = size;
  865. ax_skb->data = packet;
  866. skb_set_tail_pointer(ax_skb, size);
  867. smsc75xx_rx_csum_offload(dev, ax_skb, rx_cmd_a,
  868. rx_cmd_b);
  869. skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
  870. ax_skb->truesize = size + sizeof(struct sk_buff);
  871. usbnet_skb_return(dev, ax_skb);
  872. }
  873. skb_pull(skb, size);
  874. /* padding bytes before the next frame starts */
  875. if (skb->len)
  876. skb_pull(skb, align_count);
  877. }
  878. if (unlikely(skb->len < 0)) {
  879. netdev_warn(dev->net, "invalid rx length<0 %d", skb->len);
  880. return 0;
  881. }
  882. return 1;
  883. }
  884. static struct sk_buff *smsc75xx_tx_fixup(struct usbnet *dev,
  885. struct sk_buff *skb, gfp_t flags)
  886. {
  887. u32 tx_cmd_a, tx_cmd_b;
  888. skb_linearize(skb);
  889. if (skb_headroom(skb) < SMSC75XX_TX_OVERHEAD) {
  890. struct sk_buff *skb2 =
  891. skb_copy_expand(skb, SMSC75XX_TX_OVERHEAD, 0, flags);
  892. dev_kfree_skb_any(skb);
  893. skb = skb2;
  894. if (!skb)
  895. return NULL;
  896. }
  897. tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN) | TX_CMD_A_FCS;
  898. if (skb->ip_summed == CHECKSUM_PARTIAL)
  899. tx_cmd_a |= TX_CMD_A_IPE | TX_CMD_A_TPE;
  900. if (skb_is_gso(skb)) {
  901. u16 mss = max(skb_shinfo(skb)->gso_size, TX_MSS_MIN);
  902. tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT) & TX_CMD_B_MSS;
  903. tx_cmd_a |= TX_CMD_A_LSO;
  904. } else {
  905. tx_cmd_b = 0;
  906. }
  907. skb_push(skb, 4);
  908. cpu_to_le32s(&tx_cmd_b);
  909. memcpy(skb->data, &tx_cmd_b, 4);
  910. skb_push(skb, 4);
  911. cpu_to_le32s(&tx_cmd_a);
  912. memcpy(skb->data, &tx_cmd_a, 4);
  913. return skb;
  914. }
  915. static const struct driver_info smsc75xx_info = {
  916. .description = "smsc75xx USB 2.0 Gigabit Ethernet",
  917. .bind = smsc75xx_bind,
  918. .unbind = smsc75xx_unbind,
  919. .link_reset = smsc75xx_link_reset,
  920. .reset = smsc75xx_reset,
  921. .rx_fixup = smsc75xx_rx_fixup,
  922. .tx_fixup = smsc75xx_tx_fixup,
  923. .status = smsc75xx_status,
  924. .flags = FLAG_ETHER | FLAG_SEND_ZLP,
  925. };
  926. static const struct usb_device_id products[] = {
  927. {
  928. /* SMSC7500 USB Gigabit Ethernet Device */
  929. USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7500),
  930. .driver_info = (unsigned long) &smsc75xx_info,
  931. },
  932. {
  933. /* SMSC7500 USB Gigabit Ethernet Device */
  934. USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7505),
  935. .driver_info = (unsigned long) &smsc75xx_info,
  936. },
  937. { }, /* END */
  938. };
  939. MODULE_DEVICE_TABLE(usb, products);
  940. static struct usb_driver smsc75xx_driver = {
  941. .name = SMSC_CHIPNAME,
  942. .id_table = products,
  943. .probe = usbnet_probe,
  944. .suspend = usbnet_suspend,
  945. .resume = usbnet_resume,
  946. .disconnect = usbnet_disconnect,
  947. };
  948. static int __init smsc75xx_init(void)
  949. {
  950. return usb_register(&smsc75xx_driver);
  951. }
  952. module_init(smsc75xx_init);
  953. static void __exit smsc75xx_exit(void)
  954. {
  955. usb_deregister(&smsc75xx_driver);
  956. }
  957. module_exit(smsc75xx_exit);
  958. MODULE_AUTHOR("Nancy Lin");
  959. MODULE_AUTHOR("Steve Glendinning <steve.glendinning@smsc.com>");
  960. MODULE_DESCRIPTION("SMSC75XX USB 2.0 Gigabit Ethernet Devices");
  961. MODULE_LICENSE("GPL");