You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smsc75xx.c 28KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <byteswap.h>
  28. #include <ipxe/ethernet.h>
  29. #include <ipxe/usb.h>
  30. #include <ipxe/usbnet.h>
  31. #include <ipxe/profile.h>
  32. #include "smsc75xx.h"
  33. /** @file
  34. *
  35. * SMSC LAN75xx USB Ethernet driver
  36. *
  37. */
  38. /** Interrupt completion profiler */
  39. static struct profiler smsc75xx_intr_profiler __profiler =
  40. { .name = "smsc75xx.intr" };
  41. /** Bulk IN completion profiler */
  42. static struct profiler smsc75xx_in_profiler __profiler =
  43. { .name = "smsc75xx.in" };
  44. /** Bulk OUT profiler */
  45. static struct profiler smsc75xx_out_profiler __profiler =
  46. { .name = "smsc75xx.out" };
  47. /******************************************************************************
  48. *
  49. * Register access
  50. *
  51. ******************************************************************************
  52. */
  53. /**
  54. * Write register (without byte-swapping)
  55. *
  56. * @v smsc75xx SMSC75xx device
  57. * @v address Register address
  58. * @v value Register value
  59. * @ret rc Return status code
  60. */
  61. static int smsc75xx_raw_writel ( struct smsc75xx_device *smsc75xx,
  62. unsigned int address, uint32_t value ) {
  63. int rc;
  64. /* Write register */
  65. if ( ( rc = usb_control ( smsc75xx->usb, SMSC75XX_REGISTER_WRITE, 0,
  66. address, &value, sizeof ( value ) ) ) != 0 ) {
  67. DBGC ( smsc75xx, "SMSC75XX %p could not write %03x: %s\n",
  68. smsc75xx, address, strerror ( rc ) );
  69. return rc;
  70. }
  71. return 0;
  72. }
  73. /**
  74. * Write register
  75. *
  76. * @v smsc75xx SMSC75xx device
  77. * @v address Register address
  78. * @v value Register value
  79. * @ret rc Return status code
  80. */
  81. static inline __attribute__ (( always_inline )) int
  82. smsc75xx_writel ( struct smsc75xx_device *smsc75xx, unsigned int address,
  83. uint32_t value ) {
  84. int rc;
  85. /* Write register */
  86. if ( ( rc = smsc75xx_raw_writel ( smsc75xx, address,
  87. cpu_to_le32 ( value ) ) ) != 0 )
  88. return rc;
  89. return 0;
  90. }
  91. /**
  92. * Read register (without byte-swapping)
  93. *
  94. * @v smsc75xx SMSC75xx device
  95. * @v address Register address
  96. * @ret value Register value
  97. * @ret rc Return status code
  98. */
  99. static int smsc75xx_raw_readl ( struct smsc75xx_device *smsc75xx,
  100. unsigned int address, uint32_t *value ) {
  101. int rc;
  102. /* Read register */
  103. if ( ( rc = usb_control ( smsc75xx->usb, SMSC75XX_REGISTER_READ, 0,
  104. address, value, sizeof ( *value ) ) ) != 0 ) {
  105. DBGC ( smsc75xx, "SMSC75XX %p could not read %03x: %s\n",
  106. smsc75xx, address, strerror ( rc ) );
  107. return rc;
  108. }
  109. return 0;
  110. }
  111. /**
  112. * Read register
  113. *
  114. * @v smsc75xx SMSC75xx device
  115. * @v address Register address
  116. * @ret value Register value
  117. * @ret rc Return status code
  118. */
  119. static inline __attribute__ (( always_inline )) int
  120. smsc75xx_readl ( struct smsc75xx_device *smsc75xx, unsigned int address,
  121. uint32_t *value ) {
  122. int rc;
  123. /* Read register */
  124. if ( ( rc = smsc75xx_raw_readl ( smsc75xx, address, value ) ) != 0 )
  125. return rc;
  126. le32_to_cpus ( value );
  127. return 0;
  128. }
  129. /******************************************************************************
  130. *
  131. * EEPROM access
  132. *
  133. ******************************************************************************
  134. */
  135. /**
  136. * Wait for EEPROM to become idle
  137. *
  138. * @v smsc75xx SMSC75xx device
  139. * @ret rc Return status code
  140. */
  141. static int smsc75xx_eeprom_wait ( struct smsc75xx_device *smsc75xx ) {
  142. uint32_t e2p_cmd;
  143. unsigned int i;
  144. int rc;
  145. /* Wait for EPC_BSY to become clear */
  146. for ( i = 0 ; i < SMSC75XX_EEPROM_MAX_WAIT_MS ; i++ ) {
  147. /* Read E2P_CMD and check EPC_BSY */
  148. if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_E2P_CMD,
  149. &e2p_cmd ) ) != 0 )
  150. return rc;
  151. if ( ! ( e2p_cmd & SMSC75XX_E2P_CMD_EPC_BSY ) )
  152. return 0;
  153. /* Delay */
  154. mdelay ( 1 );
  155. }
  156. DBGC ( smsc75xx, "SMSC75XX %p timed out waiting for EEPROM\n",
  157. smsc75xx );
  158. return -ETIMEDOUT;
  159. }
  160. /**
  161. * Read byte from EEPROM
  162. *
  163. * @v smsc75xx SMSC75xx device
  164. * @v address EEPROM address
  165. * @ret byte Byte read, or negative error
  166. */
  167. static int smsc75xx_eeprom_read_byte ( struct smsc75xx_device *smsc75xx,
  168. unsigned int address ) {
  169. uint32_t e2p_cmd;
  170. uint32_t e2p_data;
  171. int rc;
  172. /* Wait for EEPROM to become idle */
  173. if ( ( rc = smsc75xx_eeprom_wait ( smsc75xx ) ) != 0 )
  174. return rc;
  175. /* Initiate read command */
  176. e2p_cmd = ( SMSC75XX_E2P_CMD_EPC_BSY | SMSC75XX_E2P_CMD_EPC_CMD_READ |
  177. SMSC75XX_E2P_CMD_EPC_ADDR ( address ) );
  178. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_E2P_CMD,
  179. e2p_cmd ) ) != 0 )
  180. return rc;
  181. /* Wait for command to complete */
  182. if ( ( rc = smsc75xx_eeprom_wait ( smsc75xx ) ) != 0 )
  183. return rc;
  184. /* Read EEPROM data */
  185. if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_E2P_DATA,
  186. &e2p_data ) ) != 0 )
  187. return rc;
  188. return SMSC75XX_E2P_DATA_GET ( e2p_data );
  189. }
  190. /**
  191. * Read data from EEPROM
  192. *
  193. * @v smsc75xx SMSC75xx device
  194. * @v address EEPROM address
  195. * @v data Data buffer
  196. * @v len Length of data
  197. * @ret rc Return status code
  198. */
  199. static int smsc75xx_eeprom_read ( struct smsc75xx_device *smsc75xx,
  200. unsigned int address, void *data,
  201. size_t len ) {
  202. uint8_t *bytes;
  203. int byte;
  204. /* Read bytes */
  205. for ( bytes = data ; len-- ; address++, bytes++ ) {
  206. byte = smsc75xx_eeprom_read_byte ( smsc75xx, address );
  207. if ( byte < 0 )
  208. return byte;
  209. *bytes = byte;
  210. }
  211. return 0;
  212. }
  213. /******************************************************************************
  214. *
  215. * MII access
  216. *
  217. ******************************************************************************
  218. */
  219. /**
  220. * Wait for MII to become idle
  221. *
  222. * @v smsc75xx SMSC75xx device
  223. * @ret rc Return status code
  224. */
  225. static int smsc75xx_mii_wait ( struct smsc75xx_device *smsc75xx ) {
  226. uint32_t mii_access;
  227. unsigned int i;
  228. int rc;
  229. /* Wait for MIIBZY to become clear */
  230. for ( i = 0 ; i < SMSC75XX_MII_MAX_WAIT_MS ; i++ ) {
  231. /* Read MII_ACCESS and check MIIBZY */
  232. if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_MII_ACCESS,
  233. &mii_access ) ) != 0 )
  234. return rc;
  235. if ( ! ( mii_access & SMSC75XX_MII_ACCESS_MIIBZY ) )
  236. return 0;
  237. /* Delay */
  238. mdelay ( 1 );
  239. }
  240. DBGC ( smsc75xx, "SMSC75XX %p timed out waiting for MII\n",
  241. smsc75xx );
  242. return -ETIMEDOUT;
  243. }
  244. /**
  245. * Read from MII register
  246. *
  247. * @v mii MII interface
  248. * @v reg Register address
  249. * @ret value Data read, or negative error
  250. */
  251. static int smsc75xx_mii_read ( struct mii_interface *mii, unsigned int reg ) {
  252. struct smsc75xx_device *smsc75xx =
  253. container_of ( mii, struct smsc75xx_device, mii );
  254. uint32_t mii_access;
  255. uint32_t mii_data;
  256. int rc;
  257. /* Wait for MII to become idle */
  258. if ( ( rc = smsc75xx_mii_wait ( smsc75xx ) ) != 0 )
  259. return rc;
  260. /* Initiate read command */
  261. mii_access = ( SMSC75XX_MII_ACCESS_PHY_ADDRESS |
  262. SMSC75XX_MII_ACCESS_MIIRINDA ( reg ) |
  263. SMSC75XX_MII_ACCESS_MIIBZY );
  264. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MII_ACCESS,
  265. mii_access ) ) != 0 )
  266. return rc;
  267. /* Wait for command to complete */
  268. if ( ( rc = smsc75xx_mii_wait ( smsc75xx ) ) != 0 )
  269. return rc;
  270. /* Read MII data */
  271. if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_MII_DATA,
  272. &mii_data ) ) != 0 )
  273. return rc;
  274. return SMSC75XX_MII_DATA_GET ( mii_data );
  275. }
  276. /**
  277. * Write to MII register
  278. *
  279. * @v mii MII interface
  280. * @v reg Register address
  281. * @v data Data to write
  282. * @ret rc Return status code
  283. */
  284. static int smsc75xx_mii_write ( struct mii_interface *mii, unsigned int reg,
  285. unsigned int data ) {
  286. struct smsc75xx_device *smsc75xx =
  287. container_of ( mii, struct smsc75xx_device, mii );
  288. uint32_t mii_access;
  289. uint32_t mii_data;
  290. int rc;
  291. /* Wait for MII to become idle */
  292. if ( ( rc = smsc75xx_mii_wait ( smsc75xx ) ) != 0 )
  293. return rc;
  294. /* Write MII data */
  295. mii_data = SMSC75XX_MII_DATA_SET ( data );
  296. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MII_DATA,
  297. mii_data ) ) != 0 )
  298. return rc;
  299. /* Initiate write command */
  300. mii_access = ( SMSC75XX_MII_ACCESS_PHY_ADDRESS |
  301. SMSC75XX_MII_ACCESS_MIIRINDA ( reg ) |
  302. SMSC75XX_MII_ACCESS_MIIWNR |
  303. SMSC75XX_MII_ACCESS_MIIBZY );
  304. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MII_ACCESS,
  305. mii_access ) ) != 0 )
  306. return rc;
  307. /* Wait for command to complete */
  308. if ( ( rc = smsc75xx_mii_wait ( smsc75xx ) ) != 0 )
  309. return rc;
  310. return 0;
  311. }
  312. /** MII operations */
  313. static struct mii_operations smsc75xx_mii_operations = {
  314. .read = smsc75xx_mii_read,
  315. .write = smsc75xx_mii_write,
  316. };
  317. /**
  318. * Check link status
  319. *
  320. * @v smsc75xx SMSC75xx device
  321. * @ret rc Return status code
  322. */
  323. static int smsc75xx_check_link ( struct smsc75xx_device *smsc75xx ) {
  324. struct net_device *netdev = smsc75xx->netdev;
  325. int intr;
  326. int rc;
  327. /* Read PHY interrupt source */
  328. intr = mii_read ( &smsc75xx->mii, SMSC75XX_MII_PHY_INTR_SOURCE );
  329. if ( intr < 0 ) {
  330. rc = intr;
  331. DBGC ( smsc75xx, "SMSC75XX %p could not get PHY interrupt "
  332. "source: %s\n", smsc75xx, strerror ( rc ) );
  333. return rc;
  334. }
  335. /* Acknowledge PHY interrupt */
  336. if ( ( rc = mii_write ( &smsc75xx->mii, SMSC75XX_MII_PHY_INTR_SOURCE,
  337. intr ) ) != 0 ) {
  338. DBGC ( smsc75xx, "SMSC75XX %p could not acknowledge PHY "
  339. "interrupt: %s\n", smsc75xx, strerror ( rc ) );
  340. return rc;
  341. }
  342. /* Check link status */
  343. if ( ( rc = mii_check_link ( &smsc75xx->mii, netdev ) ) != 0 ) {
  344. DBGC ( smsc75xx, "SMSC75XX %p could not check link: %s\n",
  345. smsc75xx, strerror ( rc ) );
  346. return rc;
  347. }
  348. DBGC ( smsc75xx, "SMSC75XX %p link %s (intr %#04x)\n",
  349. smsc75xx, ( netdev_link_ok ( netdev ) ? "up" : "down" ), intr );
  350. return 0;
  351. }
  352. /******************************************************************************
  353. *
  354. * Statistics (for debugging)
  355. *
  356. ******************************************************************************
  357. */
  358. /**
  359. * Get statistics
  360. *
  361. * @v smsc75xx SMSC75xx device
  362. * @v stats Statistics to fill in
  363. * @ret rc Return status code
  364. */
  365. static int smsc75xx_get_statistics ( struct smsc75xx_device *smsc75xx,
  366. struct smsc75xx_statistics *stats ) {
  367. int rc;
  368. /* Get statistics */
  369. if ( ( rc = usb_control ( smsc75xx->usb, SMSC75XX_GET_STATISTICS, 0, 0,
  370. stats, sizeof ( *stats ) ) ) != 0 ) {
  371. DBGC ( smsc75xx, "SMSC75XX %p could not get statistics: %s\n",
  372. smsc75xx, strerror ( rc ) );
  373. return rc;
  374. }
  375. return 0;
  376. }
  377. /**
  378. * Dump statistics (for debugging)
  379. *
  380. * @v smsc75xx SMSC75xx device
  381. * @ret rc Return status code
  382. */
  383. static int smsc75xx_dump_statistics ( struct smsc75xx_device *smsc75xx ) {
  384. struct smsc75xx_statistics stats;
  385. int rc;
  386. /* Do nothing unless debugging is enabled */
  387. if ( ! DBG_LOG )
  388. return 0;
  389. /* Get statistics */
  390. if ( ( rc = smsc75xx_get_statistics ( smsc75xx, &stats ) ) != 0 )
  391. return rc;
  392. /* Dump statistics */
  393. DBGC ( smsc75xx, "SMSC75XX %p RXE fcs %d aln %d frg %d jab %d und %d "
  394. "ovr %d drp %d\n", smsc75xx, le32_to_cpu ( stats.rx.err.fcs ),
  395. le32_to_cpu ( stats.rx.err.alignment ),
  396. le32_to_cpu ( stats.rx.err.fragment ),
  397. le32_to_cpu ( stats.rx.err.jabber ),
  398. le32_to_cpu ( stats.rx.err.undersize ),
  399. le32_to_cpu ( stats.rx.err.oversize ),
  400. le32_to_cpu ( stats.rx.err.dropped ) );
  401. DBGC ( smsc75xx, "SMSC75XX %p RXB ucast %d bcast %d mcast %d\n",
  402. smsc75xx, le32_to_cpu ( stats.rx.byte.unicast ),
  403. le32_to_cpu ( stats.rx.byte.broadcast ),
  404. le32_to_cpu ( stats.rx.byte.multicast ) );
  405. DBGC ( smsc75xx, "SMSC75XX %p RXF ucast %d bcast %d mcast %d pause "
  406. "%d\n", smsc75xx, le32_to_cpu ( stats.rx.frame.unicast ),
  407. le32_to_cpu ( stats.rx.frame.broadcast ),
  408. le32_to_cpu ( stats.rx.frame.multicast ),
  409. le32_to_cpu ( stats.rx.frame.pause ) );
  410. DBGC ( smsc75xx, "SMSC75XX %p TXE fcs %d def %d car %d cnt %d sgl %d "
  411. "mul %d exc %d lat %d\n", smsc75xx,
  412. le32_to_cpu ( stats.tx.err.fcs ),
  413. le32_to_cpu ( stats.tx.err.deferral ),
  414. le32_to_cpu ( stats.tx.err.carrier ),
  415. le32_to_cpu ( stats.tx.err.count ),
  416. le32_to_cpu ( stats.tx.err.single ),
  417. le32_to_cpu ( stats.tx.err.multiple ),
  418. le32_to_cpu ( stats.tx.err.excessive ),
  419. le32_to_cpu ( stats.tx.err.late ) );
  420. DBGC ( smsc75xx, "SMSC75XX %p TXB ucast %d bcast %d mcast %d\n",
  421. smsc75xx, le32_to_cpu ( stats.tx.byte.unicast ),
  422. le32_to_cpu ( stats.tx.byte.broadcast ),
  423. le32_to_cpu ( stats.tx.byte.multicast ) );
  424. DBGC ( smsc75xx, "SMSC75XX %p TXF ucast %d bcast %d mcast %d pause "
  425. "%d\n", smsc75xx, le32_to_cpu ( stats.tx.frame.unicast ),
  426. le32_to_cpu ( stats.tx.frame.broadcast ),
  427. le32_to_cpu ( stats.tx.frame.multicast ),
  428. le32_to_cpu ( stats.tx.frame.pause ) );
  429. return 0;
  430. }
  431. /******************************************************************************
  432. *
  433. * Device reset
  434. *
  435. ******************************************************************************
  436. */
  437. /**
  438. * Reset device
  439. *
  440. * @v smsc75xx SMSC75xx device
  441. * @ret rc Return status code
  442. */
  443. static int smsc75xx_reset ( struct smsc75xx_device *smsc75xx ) {
  444. uint32_t hw_cfg;
  445. unsigned int i;
  446. int rc;
  447. /* Reset device */
  448. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_HW_CFG,
  449. SMSC75XX_HW_CFG_LRST ) ) != 0 )
  450. return rc;
  451. /* Wait for reset to complete */
  452. for ( i = 0 ; i < SMSC75XX_RESET_MAX_WAIT_MS ; i++ ) {
  453. /* Check if reset has completed */
  454. if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_HW_CFG,
  455. &hw_cfg ) ) != 0 )
  456. return rc;
  457. if ( ! ( hw_cfg & SMSC75XX_HW_CFG_LRST ) )
  458. return 0;
  459. /* Delay */
  460. mdelay ( 1 );
  461. }
  462. DBGC ( smsc75xx, "SMSC75XX %p timed out waiting for reset\n",
  463. smsc75xx );
  464. return -ETIMEDOUT;
  465. }
  466. /******************************************************************************
  467. *
  468. * Endpoint operations
  469. *
  470. ******************************************************************************
  471. */
  472. /**
  473. * Complete interrupt transfer
  474. *
  475. * @v ep USB endpoint
  476. * @v iobuf I/O buffer
  477. * @v rc Completion status code
  478. */
  479. static void smsc75xx_intr_complete ( struct usb_endpoint *ep,
  480. struct io_buffer *iobuf, int rc ) {
  481. struct smsc75xx_device *smsc75xx =
  482. container_of ( ep, struct smsc75xx_device, usbnet.intr );
  483. struct net_device *netdev = smsc75xx->netdev;
  484. struct smsc75xx_interrupt *intr;
  485. /* Profile completions */
  486. profile_start ( &smsc75xx_intr_profiler );
  487. /* Ignore packets cancelled when the endpoint closes */
  488. if ( ! ep->open )
  489. goto done;
  490. /* Record USB errors against the network device */
  491. if ( rc != 0 ) {
  492. DBGC ( smsc75xx, "SMSC75XX %p interrupt failed: %s\n",
  493. smsc75xx, strerror ( rc ) );
  494. DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
  495. netdev_rx_err ( netdev, NULL, rc );
  496. goto done;
  497. }
  498. /* Extract interrupt data */
  499. if ( iob_len ( iobuf ) != sizeof ( *intr ) ) {
  500. DBGC ( smsc75xx, "SMSC75XX %p malformed interrupt\n",
  501. smsc75xx );
  502. DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
  503. netdev_rx_err ( netdev, NULL, rc );
  504. goto done;
  505. }
  506. intr = iobuf->data;
  507. /* Record interrupt status */
  508. smsc75xx->int_sts = le32_to_cpu ( intr->int_sts );
  509. profile_stop ( &smsc75xx_intr_profiler );
  510. done:
  511. /* Free I/O buffer */
  512. free_iob ( iobuf );
  513. }
  514. /** Interrupt endpoint operations */
  515. static struct usb_endpoint_driver_operations smsc75xx_intr_operations = {
  516. .complete = smsc75xx_intr_complete,
  517. };
  518. /**
  519. * Complete bulk IN transfer
  520. *
  521. * @v ep USB endpoint
  522. * @v iobuf I/O buffer
  523. * @v rc Completion status code
  524. */
  525. static void smsc75xx_in_complete ( struct usb_endpoint *ep,
  526. struct io_buffer *iobuf, int rc ) {
  527. struct smsc75xx_device *smsc75xx =
  528. container_of ( ep, struct smsc75xx_device, usbnet.in );
  529. struct net_device *netdev = smsc75xx->netdev;
  530. struct smsc75xx_rx_header *header;
  531. /* Profile completions */
  532. profile_start ( &smsc75xx_in_profiler );
  533. /* Ignore packets cancelled when the endpoint closes */
  534. if ( ! ep->open ) {
  535. free_iob ( iobuf );
  536. return;
  537. }
  538. /* Record USB errors against the network device */
  539. if ( rc != 0 ) {
  540. DBGC ( smsc75xx, "SMSC75XX %p bulk IN failed: %s\n",
  541. smsc75xx, strerror ( rc ) );
  542. goto err;
  543. }
  544. /* Sanity check */
  545. if ( iob_len ( iobuf ) < ( sizeof ( *header ) ) ) {
  546. DBGC ( smsc75xx, "SMSC75XX %p underlength bulk IN\n",
  547. smsc75xx );
  548. DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
  549. rc = -EINVAL;
  550. goto err;
  551. }
  552. /* Strip header */
  553. header = iobuf->data;
  554. iob_pull ( iobuf, sizeof ( *header ) );
  555. /* Check for errors */
  556. if ( header->command & cpu_to_le32 ( SMSC75XX_RX_RED ) ) {
  557. DBGC ( smsc75xx, "SMSC75XX %p receive error (%08x):\n",
  558. smsc75xx, le32_to_cpu ( header->command ) );
  559. DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
  560. rc = -EIO;
  561. goto err;
  562. }
  563. /* Hand off to network stack */
  564. netdev_rx ( netdev, iob_disown ( iobuf ) );
  565. profile_stop ( &smsc75xx_in_profiler );
  566. return;
  567. err:
  568. /* Hand off to network stack */
  569. netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
  570. }
  571. /** Bulk IN endpoint operations */
  572. static struct usb_endpoint_driver_operations smsc75xx_in_operations = {
  573. .complete = smsc75xx_in_complete,
  574. };
  575. /**
  576. * Transmit packet
  577. *
  578. * @v smsc75xx SMSC75xx device
  579. * @v iobuf I/O buffer
  580. * @ret rc Return status code
  581. */
  582. static int smsc75xx_out_transmit ( struct smsc75xx_device *smsc75xx,
  583. struct io_buffer *iobuf ) {
  584. struct smsc75xx_tx_header *header;
  585. size_t len = iob_len ( iobuf );
  586. int rc;
  587. /* Profile transmissions */
  588. profile_start ( &smsc75xx_out_profiler );
  589. /* Prepend header */
  590. if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *header ) ) ) != 0 )
  591. return rc;
  592. header = iob_push ( iobuf, sizeof ( *header ) );
  593. header->command = cpu_to_le32 ( SMSC75XX_TX_FCS | len );
  594. header->tag = 0;
  595. header->mss = 0;
  596. /* Enqueue I/O buffer */
  597. if ( ( rc = usb_stream ( &smsc75xx->usbnet.out, iobuf, 0 ) ) != 0 )
  598. return rc;
  599. profile_stop ( &smsc75xx_out_profiler );
  600. return 0;
  601. }
  602. /**
  603. * Complete bulk OUT transfer
  604. *
  605. * @v ep USB endpoint
  606. * @v iobuf I/O buffer
  607. * @v rc Completion status code
  608. */
  609. static void smsc75xx_out_complete ( struct usb_endpoint *ep,
  610. struct io_buffer *iobuf, int rc ) {
  611. struct smsc75xx_device *smsc75xx =
  612. container_of ( ep, struct smsc75xx_device, usbnet.out );
  613. struct net_device *netdev = smsc75xx->netdev;
  614. /* Report TX completion */
  615. netdev_tx_complete_err ( netdev, iobuf, rc );
  616. }
  617. /** Bulk OUT endpoint operations */
  618. static struct usb_endpoint_driver_operations smsc75xx_out_operations = {
  619. .complete = smsc75xx_out_complete,
  620. };
  621. /******************************************************************************
  622. *
  623. * Network device interface
  624. *
  625. ******************************************************************************
  626. */
  627. /**
  628. * Open network device
  629. *
  630. * @v netdev Network device
  631. * @ret rc Return status code
  632. */
  633. static int smsc75xx_open ( struct net_device *netdev ) {
  634. struct smsc75xx_device *smsc75xx = netdev->priv;
  635. union smsc75xx_mac mac;
  636. int rc;
  637. /* Clear stored interrupt status */
  638. smsc75xx->int_sts = 0;
  639. /* Copy MAC address */
  640. memset ( &mac, 0, sizeof ( mac ) );
  641. memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
  642. /* Configure bulk IN empty response */
  643. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_HW_CFG,
  644. SMSC75XX_HW_CFG_BIR ) ) != 0 )
  645. goto err_hw_cfg;
  646. /* Open USB network device */
  647. if ( ( rc = usbnet_open ( &smsc75xx->usbnet ) ) != 0 ) {
  648. DBGC ( smsc75xx, "SMSC75XX %p could not open: %s\n",
  649. smsc75xx, strerror ( rc ) );
  650. goto err_open;
  651. }
  652. /* Configure interrupt endpoint */
  653. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_INT_EP_CTL,
  654. ( SMSC75XX_INT_EP_CTL_RDFO_EN |
  655. SMSC75XX_INT_EP_CTL_PHY_EN ) ) ) != 0 )
  656. goto err_int_ep_ctl;
  657. /* Configure bulk IN delay */
  658. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_BULK_IN_DLY,
  659. SMSC75XX_BULK_IN_DLY_SET ( 0 ) ) ) != 0 )
  660. goto err_bulk_in_dly;
  661. /* Configure receive filters */
  662. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_RFE_CTL,
  663. ( SMSC75XX_RFE_CTL_AB |
  664. SMSC75XX_RFE_CTL_AM |
  665. SMSC75XX_RFE_CTL_AU ) ) ) != 0 )
  666. goto err_rfe_ctl;
  667. /* Configure receive FIFO */
  668. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_FCT_RX_CTL,
  669. ( SMSC75XX_FCT_RX_CTL_EN |
  670. SMSC75XX_FCT_RX_CTL_BAD ) ) ) != 0 )
  671. goto err_fct_rx_ctl;
  672. /* Configure transmit FIFO */
  673. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_FCT_TX_CTL,
  674. SMSC75XX_FCT_TX_CTL_EN ) ) != 0 )
  675. goto err_fct_tx_ctl;
  676. /* Configure receive datapath */
  677. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MAC_RX,
  678. ( SMSC75XX_MAC_RX_MAX_SIZE_DEFAULT |
  679. SMSC75XX_MAC_RX_FCS |
  680. SMSC75XX_MAC_RX_EN ) ) ) != 0 )
  681. goto err_mac_rx;
  682. /* Configure transmit datapath */
  683. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MAC_TX,
  684. SMSC75XX_MAC_TX_EN ) ) != 0 )
  685. goto err_mac_tx;
  686. /* Write MAC address high register */
  687. if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_RX_ADDRH,
  688. mac.addr.h ) ) != 0 )
  689. goto err_rx_addrh;
  690. /* Write MAC address low register */
  691. if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_RX_ADDRL,
  692. mac.addr.l ) ) != 0 )
  693. goto err_rx_addrl;
  694. /* Write MAC address perfect filter high register */
  695. mac.addr.h |= cpu_to_le32 ( SMSC75XX_ADDR_FILTH_VALID );
  696. if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_ADDR_FILTH ( 0 ),
  697. mac.addr.h ) ) != 0 )
  698. goto err_addr_filth;
  699. /* Write MAC address perfect filter low register */
  700. if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_ADDR_FILTL ( 0 ),
  701. mac.addr.l ) ) != 0 )
  702. goto err_addr_filtl;
  703. /* Enable PHY interrupts */
  704. if ( ( rc = mii_write ( &smsc75xx->mii, SMSC75XX_MII_PHY_INTR_MASK,
  705. ( SMSC75XX_PHY_INTR_ANEG_DONE |
  706. SMSC75XX_PHY_INTR_LINK_DOWN ) ) ) != 0 ) {
  707. DBGC ( smsc75xx, "SMSC75XX %p could not set PHY interrupt "
  708. "mask: %s\n", smsc75xx, strerror ( rc ) );
  709. goto err_phy_intr_mask;
  710. }
  711. /* Update link status */
  712. smsc75xx_check_link ( smsc75xx );
  713. return 0;
  714. err_phy_intr_mask:
  715. err_addr_filtl:
  716. err_addr_filth:
  717. err_rx_addrl:
  718. err_rx_addrh:
  719. err_mac_tx:
  720. err_mac_rx:
  721. err_fct_tx_ctl:
  722. err_fct_rx_ctl:
  723. err_rfe_ctl:
  724. err_bulk_in_dly:
  725. err_int_ep_ctl:
  726. usbnet_close ( &smsc75xx->usbnet );
  727. err_open:
  728. err_hw_cfg:
  729. smsc75xx_reset ( smsc75xx );
  730. return rc;
  731. }
  732. /**
  733. * Close network device
  734. *
  735. * @v netdev Network device
  736. */
  737. static void smsc75xx_close ( struct net_device *netdev ) {
  738. struct smsc75xx_device *smsc75xx = netdev->priv;
  739. /* Close USB network device */
  740. usbnet_close ( &smsc75xx->usbnet );
  741. /* Dump statistics (for debugging) */
  742. smsc75xx_dump_statistics ( smsc75xx );
  743. /* Reset device */
  744. smsc75xx_reset ( smsc75xx );
  745. }
  746. /**
  747. * Transmit packet
  748. *
  749. * @v netdev Network device
  750. * @v iobuf I/O buffer
  751. * @ret rc Return status code
  752. */
  753. static int smsc75xx_transmit ( struct net_device *netdev,
  754. struct io_buffer *iobuf ) {
  755. struct smsc75xx_device *smsc75xx = netdev->priv;
  756. int rc;
  757. /* Transmit packet */
  758. if ( ( rc = smsc75xx_out_transmit ( smsc75xx, iobuf ) ) != 0 )
  759. return rc;
  760. return 0;
  761. }
  762. /**
  763. * Poll for completed and received packets
  764. *
  765. * @v netdev Network device
  766. */
  767. static void smsc75xx_poll ( struct net_device *netdev ) {
  768. struct smsc75xx_device *smsc75xx = netdev->priv;
  769. uint32_t int_sts;
  770. int rc;
  771. /* Poll USB bus */
  772. usb_poll ( smsc75xx->bus );
  773. /* Refill endpoints */
  774. if ( ( rc = usbnet_refill ( &smsc75xx->usbnet ) ) != 0 )
  775. netdev_rx_err ( netdev, NULL, rc );
  776. /* Do nothing more unless there are interrupts to handle */
  777. int_sts = smsc75xx->int_sts;
  778. if ( ! int_sts )
  779. return;
  780. /* Check link status if applicable */
  781. if ( int_sts & SMSC75XX_INT_STS_PHY_INT ) {
  782. smsc75xx_check_link ( smsc75xx );
  783. int_sts &= ~SMSC75XX_INT_STS_PHY_INT;
  784. }
  785. /* Record RX FIFO overflow if applicable */
  786. if ( int_sts & SMSC75XX_INT_STS_RDFO_INT ) {
  787. DBGC2 ( smsc75xx, "SMSC75XX %p RX FIFO overflowed\n",
  788. smsc75xx );
  789. netdev_rx_err ( netdev, NULL, -ENOBUFS );
  790. int_sts &= ~SMSC75XX_INT_STS_RDFO_INT;
  791. }
  792. /* Check for unexpected interrupts */
  793. if ( int_sts ) {
  794. DBGC ( smsc75xx, "SMSC75XX %p unexpected interrupt %#08x\n",
  795. smsc75xx, int_sts );
  796. netdev_rx_err ( netdev, NULL, -ENOTTY );
  797. }
  798. /* Clear interrupts */
  799. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_INT_STS,
  800. smsc75xx->int_sts ) ) != 0 )
  801. netdev_rx_err ( netdev, NULL, rc );
  802. smsc75xx->int_sts = 0;
  803. }
  804. /** SMSC75xx network device operations */
  805. static struct net_device_operations smsc75xx_operations = {
  806. .open = smsc75xx_open,
  807. .close = smsc75xx_close,
  808. .transmit = smsc75xx_transmit,
  809. .poll = smsc75xx_poll,
  810. };
  811. /******************************************************************************
  812. *
  813. * USB interface
  814. *
  815. ******************************************************************************
  816. */
  817. /**
  818. * Probe device
  819. *
  820. * @v func USB function
  821. * @v config Configuration descriptor
  822. * @ret rc Return status code
  823. */
  824. static int smsc75xx_probe ( struct usb_function *func,
  825. struct usb_configuration_descriptor *config ) {
  826. struct usb_device *usb = func->usb;
  827. struct net_device *netdev;
  828. struct smsc75xx_device *smsc75xx;
  829. int rc;
  830. /* Allocate and initialise structure */
  831. netdev = alloc_etherdev ( sizeof ( *smsc75xx ) );
  832. if ( ! netdev ) {
  833. rc = -ENOMEM;
  834. goto err_alloc;
  835. }
  836. netdev_init ( netdev, &smsc75xx_operations );
  837. netdev->dev = &func->dev;
  838. smsc75xx = netdev->priv;
  839. memset ( smsc75xx, 0, sizeof ( *smsc75xx ) );
  840. smsc75xx->usb = usb;
  841. smsc75xx->bus = usb->port->hub->bus;
  842. smsc75xx->netdev = netdev;
  843. usbnet_init ( &smsc75xx->usbnet, func, &smsc75xx_intr_operations,
  844. &smsc75xx_in_operations, &smsc75xx_out_operations );
  845. usb_refill_init ( &smsc75xx->usbnet.intr, 0, 0,
  846. SMSC75XX_INTR_MAX_FILL );
  847. usb_refill_init ( &smsc75xx->usbnet.in, 0, SMSC75XX_IN_MTU,
  848. SMSC75XX_IN_MAX_FILL );
  849. mii_init ( &smsc75xx->mii, &smsc75xx_mii_operations );
  850. DBGC ( smsc75xx, "SMSC75XX %p on %s\n", smsc75xx, func->name );
  851. /* Describe USB network device */
  852. if ( ( rc = usbnet_describe ( &smsc75xx->usbnet, config ) ) != 0 ) {
  853. DBGC ( smsc75xx, "SMSC75XX %p could not describe: %s\n",
  854. smsc75xx, strerror ( rc ) );
  855. goto err_describe;
  856. }
  857. /* Reset device */
  858. if ( ( rc = smsc75xx_reset ( smsc75xx ) ) != 0 )
  859. goto err_reset;
  860. /* Read MAC address */
  861. if ( ( rc = smsc75xx_eeprom_read ( smsc75xx, SMSC75XX_EEPROM_MAC,
  862. netdev->hw_addr, ETH_ALEN ) ) != 0 )
  863. goto err_eeprom_read;
  864. /* Register network device */
  865. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  866. goto err_register;
  867. usb_func_set_drvdata ( func, netdev );
  868. return 0;
  869. unregister_netdev ( netdev );
  870. err_register:
  871. err_eeprom_read:
  872. err_reset:
  873. err_describe:
  874. netdev_nullify ( netdev );
  875. netdev_put ( netdev );
  876. err_alloc:
  877. return rc;
  878. }
  879. /**
  880. * Remove device
  881. *
  882. * @v func USB function
  883. */
  884. static void smsc75xx_remove ( struct usb_function *func ) {
  885. struct net_device *netdev = usb_func_get_drvdata ( func );
  886. unregister_netdev ( netdev );
  887. netdev_nullify ( netdev );
  888. netdev_put ( netdev );
  889. }
  890. /** SMSC75xx device IDs */
  891. static struct usb_device_id smsc75xx_ids[] = {
  892. {
  893. .name = "smsc7500",
  894. .vendor = 0x0424,
  895. .product = 0x7500,
  896. },
  897. {
  898. .name = "smsc7505",
  899. .vendor = 0x0424,
  900. .product = 0x7505,
  901. },
  902. };
  903. /** SMSC LAN75xx driver */
  904. struct usb_driver smsc75xx_driver __usb_driver = {
  905. .ids = smsc75xx_ids,
  906. .id_count = ( sizeof ( smsc75xx_ids ) / sizeof ( smsc75xx_ids[0] ) ),
  907. .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
  908. .score = USB_SCORE_NORMAL,
  909. .probe = smsc75xx_probe,
  910. .remove = smsc75xx_remove,
  911. };