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 27KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. int rc;
  446. /* Reset device */
  447. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_HW_CFG,
  448. SMSC75XX_HW_CFG_LRST ) ) != 0 )
  449. return rc;
  450. /* Wait for reset to complete */
  451. udelay ( SMSC75XX_RESET_DELAY_US );
  452. /* Check that reset has completed */
  453. if ( ( rc = smsc75xx_readl ( smsc75xx, SMSC75XX_HW_CFG,
  454. &hw_cfg ) ) != 0 )
  455. return rc;
  456. if ( hw_cfg & SMSC75XX_HW_CFG_LRST ) {
  457. DBGC ( smsc75xx, "SMSC75XX %p failed to reset\n", smsc75xx );
  458. return -ETIMEDOUT;
  459. }
  460. return 0;
  461. }
  462. /******************************************************************************
  463. *
  464. * Endpoint operations
  465. *
  466. ******************************************************************************
  467. */
  468. /**
  469. * Complete interrupt transfer
  470. *
  471. * @v ep USB endpoint
  472. * @v iobuf I/O buffer
  473. * @v rc Completion status code
  474. */
  475. static void smsc75xx_intr_complete ( struct usb_endpoint *ep,
  476. struct io_buffer *iobuf, int rc ) {
  477. struct smsc75xx_device *smsc75xx =
  478. container_of ( ep, struct smsc75xx_device, usbnet.intr );
  479. struct net_device *netdev = smsc75xx->netdev;
  480. struct smsc75xx_interrupt *intr;
  481. /* Profile completions */
  482. profile_start ( &smsc75xx_intr_profiler );
  483. /* Ignore packets cancelled when the endpoint closes */
  484. if ( ! ep->open )
  485. goto done;
  486. /* Record USB errors against the network device */
  487. if ( rc != 0 ) {
  488. DBGC ( smsc75xx, "SMSC75XX %p interrupt failed: %s\n",
  489. smsc75xx, strerror ( rc ) );
  490. DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
  491. netdev_rx_err ( netdev, NULL, rc );
  492. goto done;
  493. }
  494. /* Extract interrupt data */
  495. if ( iob_len ( iobuf ) != sizeof ( *intr ) ) {
  496. DBGC ( smsc75xx, "SMSC75XX %p malformed interrupt\n",
  497. smsc75xx );
  498. DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
  499. netdev_rx_err ( netdev, NULL, rc );
  500. goto done;
  501. }
  502. intr = iobuf->data;
  503. /* Record interrupt status */
  504. smsc75xx->int_sts = le32_to_cpu ( intr->int_sts );
  505. profile_stop ( &smsc75xx_intr_profiler );
  506. done:
  507. /* Free I/O buffer */
  508. free_iob ( iobuf );
  509. }
  510. /** Interrupt endpoint operations */
  511. static struct usb_endpoint_driver_operations smsc75xx_intr_operations = {
  512. .complete = smsc75xx_intr_complete,
  513. };
  514. /**
  515. * Complete bulk IN transfer
  516. *
  517. * @v ep USB endpoint
  518. * @v iobuf I/O buffer
  519. * @v rc Completion status code
  520. */
  521. static void smsc75xx_in_complete ( struct usb_endpoint *ep,
  522. struct io_buffer *iobuf, int rc ) {
  523. struct smsc75xx_device *smsc75xx =
  524. container_of ( ep, struct smsc75xx_device, usbnet.in );
  525. struct net_device *netdev = smsc75xx->netdev;
  526. struct smsc75xx_rx_header *header;
  527. /* Profile completions */
  528. profile_start ( &smsc75xx_in_profiler );
  529. /* Ignore packets cancelled when the endpoint closes */
  530. if ( ! ep->open ) {
  531. free_iob ( iobuf );
  532. return;
  533. }
  534. /* Record USB errors against the network device */
  535. if ( rc != 0 ) {
  536. DBGC ( smsc75xx, "SMSC75XX %p bulk IN failed: %s\n",
  537. smsc75xx, strerror ( rc ) );
  538. goto err;
  539. }
  540. /* Sanity check */
  541. if ( iob_len ( iobuf ) < ( sizeof ( *header ) ) ) {
  542. DBGC ( smsc75xx, "SMSC75XX %p underlength bulk IN\n",
  543. smsc75xx );
  544. DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
  545. rc = -EINVAL;
  546. goto err;
  547. }
  548. /* Strip header */
  549. header = iobuf->data;
  550. iob_pull ( iobuf, sizeof ( *header ) );
  551. /* Check for errors */
  552. if ( header->command & cpu_to_le32 ( SMSC75XX_RX_RED ) ) {
  553. DBGC ( smsc75xx, "SMSC75XX %p receive error (%08x):\n",
  554. smsc75xx, le32_to_cpu ( header->command ) );
  555. DBGC_HDA ( smsc75xx, 0, iobuf->data, iob_len ( iobuf ) );
  556. rc = -EIO;
  557. goto err;
  558. }
  559. /* Hand off to network stack */
  560. netdev_rx ( netdev, iob_disown ( iobuf ) );
  561. profile_stop ( &smsc75xx_in_profiler );
  562. return;
  563. err:
  564. /* Hand off to network stack */
  565. netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
  566. }
  567. /** Bulk IN endpoint operations */
  568. static struct usb_endpoint_driver_operations smsc75xx_in_operations = {
  569. .complete = smsc75xx_in_complete,
  570. };
  571. /**
  572. * Transmit packet
  573. *
  574. * @v smsc75xx SMSC75xx device
  575. * @v iobuf I/O buffer
  576. * @ret rc Return status code
  577. */
  578. static int smsc75xx_out_transmit ( struct smsc75xx_device *smsc75xx,
  579. struct io_buffer *iobuf ) {
  580. struct smsc75xx_tx_header *header;
  581. size_t len = iob_len ( iobuf );
  582. int rc;
  583. /* Profile transmissions */
  584. profile_start ( &smsc75xx_out_profiler );
  585. /* Prepend header */
  586. if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *header ) ) ) != 0 )
  587. return rc;
  588. header = iob_push ( iobuf, sizeof ( *header ) );
  589. header->command = cpu_to_le32 ( SMSC75XX_TX_FCS | len );
  590. header->tag = 0;
  591. header->mss = 0;
  592. /* Enqueue I/O buffer */
  593. if ( ( rc = usb_stream ( &smsc75xx->usbnet.out, iobuf, 0 ) ) != 0 )
  594. return rc;
  595. profile_stop ( &smsc75xx_out_profiler );
  596. return 0;
  597. }
  598. /**
  599. * Complete bulk OUT transfer
  600. *
  601. * @v ep USB endpoint
  602. * @v iobuf I/O buffer
  603. * @v rc Completion status code
  604. */
  605. static void smsc75xx_out_complete ( struct usb_endpoint *ep,
  606. struct io_buffer *iobuf, int rc ) {
  607. struct smsc75xx_device *smsc75xx =
  608. container_of ( ep, struct smsc75xx_device, usbnet.out );
  609. struct net_device *netdev = smsc75xx->netdev;
  610. /* Report TX completion */
  611. netdev_tx_complete_err ( netdev, iobuf, rc );
  612. }
  613. /** Bulk OUT endpoint operations */
  614. static struct usb_endpoint_driver_operations smsc75xx_out_operations = {
  615. .complete = smsc75xx_out_complete,
  616. };
  617. /******************************************************************************
  618. *
  619. * Network device interface
  620. *
  621. ******************************************************************************
  622. */
  623. /**
  624. * Open network device
  625. *
  626. * @v netdev Network device
  627. * @ret rc Return status code
  628. */
  629. static int smsc75xx_open ( struct net_device *netdev ) {
  630. struct smsc75xx_device *smsc75xx = netdev->priv;
  631. union smsc75xx_mac mac;
  632. int rc;
  633. /* Clear stored interrupt status */
  634. smsc75xx->int_sts = 0;
  635. /* Copy MAC address */
  636. memset ( &mac, 0, sizeof ( mac ) );
  637. memcpy ( mac.raw, netdev->ll_addr, ETH_ALEN );
  638. /* Configure bulk IN empty response */
  639. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_HW_CFG,
  640. SMSC75XX_HW_CFG_BIR ) ) != 0 )
  641. goto err_hw_cfg;
  642. /* Open USB network device */
  643. if ( ( rc = usbnet_open ( &smsc75xx->usbnet ) ) != 0 ) {
  644. DBGC ( smsc75xx, "SMSC75XX %p could not open: %s\n",
  645. smsc75xx, strerror ( rc ) );
  646. goto err_open;
  647. }
  648. /* Configure interrupt endpoint */
  649. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_INT_EP_CTL,
  650. ( SMSC75XX_INT_EP_CTL_RDFO_EN |
  651. SMSC75XX_INT_EP_CTL_PHY_EN ) ) ) != 0 )
  652. goto err_int_ep_ctl;
  653. /* Configure bulk IN delay */
  654. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_BULK_IN_DLY,
  655. SMSC75XX_BULK_IN_DLY_SET ( 0 ) ) ) != 0 )
  656. goto err_bulk_in_dly;
  657. /* Configure receive filters */
  658. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_RFE_CTL,
  659. ( SMSC75XX_RFE_CTL_AB |
  660. SMSC75XX_RFE_CTL_AM |
  661. SMSC75XX_RFE_CTL_AU ) ) ) != 0 )
  662. goto err_rfe_ctl;
  663. /* Configure receive FIFO */
  664. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_FCT_RX_CTL,
  665. ( SMSC75XX_FCT_RX_CTL_EN |
  666. SMSC75XX_FCT_RX_CTL_BAD ) ) ) != 0 )
  667. goto err_fct_rx_ctl;
  668. /* Configure transmit FIFO */
  669. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_FCT_TX_CTL,
  670. SMSC75XX_FCT_TX_CTL_EN ) ) != 0 )
  671. goto err_fct_tx_ctl;
  672. /* Configure receive datapath */
  673. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MAC_RX,
  674. ( SMSC75XX_MAC_RX_MAX_SIZE_DEFAULT |
  675. SMSC75XX_MAC_RX_FCS |
  676. SMSC75XX_MAC_RX_EN ) ) ) != 0 )
  677. goto err_mac_rx;
  678. /* Configure transmit datapath */
  679. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_MAC_TX,
  680. SMSC75XX_MAC_TX_EN ) ) != 0 )
  681. goto err_mac_tx;
  682. /* Write MAC address high register */
  683. if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_RX_ADDRH,
  684. mac.addr.h ) ) != 0 )
  685. goto err_rx_addrh;
  686. /* Write MAC address low register */
  687. if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_RX_ADDRL,
  688. mac.addr.l ) ) != 0 )
  689. goto err_rx_addrl;
  690. /* Write MAC address perfect filter high register */
  691. mac.addr.h |= cpu_to_le32 ( SMSC75XX_ADDR_FILTH_VALID );
  692. if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_ADDR_FILTH ( 0 ),
  693. mac.addr.h ) ) != 0 )
  694. goto err_addr_filth;
  695. /* Write MAC address perfect filter low register */
  696. if ( ( rc = smsc75xx_raw_writel ( smsc75xx, SMSC75XX_ADDR_FILTL ( 0 ),
  697. mac.addr.l ) ) != 0 )
  698. goto err_addr_filtl;
  699. /* Enable PHY interrupts */
  700. if ( ( rc = mii_write ( &smsc75xx->mii, SMSC75XX_MII_PHY_INTR_MASK,
  701. ( SMSC75XX_PHY_INTR_ANEG_DONE |
  702. SMSC75XX_PHY_INTR_LINK_DOWN ) ) ) != 0 ) {
  703. DBGC ( smsc75xx, "SMSC75XX %p could not set PHY interrupt "
  704. "mask: %s\n", smsc75xx, strerror ( rc ) );
  705. goto err_phy_intr_mask;
  706. }
  707. /* Update link status */
  708. smsc75xx_check_link ( smsc75xx );
  709. return 0;
  710. err_phy_intr_mask:
  711. err_addr_filtl:
  712. err_addr_filth:
  713. err_rx_addrl:
  714. err_rx_addrh:
  715. err_mac_tx:
  716. err_mac_rx:
  717. err_fct_tx_ctl:
  718. err_fct_rx_ctl:
  719. err_rfe_ctl:
  720. err_bulk_in_dly:
  721. err_int_ep_ctl:
  722. usbnet_close ( &smsc75xx->usbnet );
  723. err_open:
  724. err_hw_cfg:
  725. smsc75xx_reset ( smsc75xx );
  726. return rc;
  727. }
  728. /**
  729. * Close network device
  730. *
  731. * @v netdev Network device
  732. */
  733. static void smsc75xx_close ( struct net_device *netdev ) {
  734. struct smsc75xx_device *smsc75xx = netdev->priv;
  735. /* Close USB network device */
  736. usbnet_close ( &smsc75xx->usbnet );
  737. /* Dump statistics (for debugging) */
  738. smsc75xx_dump_statistics ( smsc75xx );
  739. /* Reset device */
  740. smsc75xx_reset ( smsc75xx );
  741. }
  742. /**
  743. * Transmit packet
  744. *
  745. * @v netdev Network device
  746. * @v iobuf I/O buffer
  747. * @ret rc Return status code
  748. */
  749. static int smsc75xx_transmit ( struct net_device *netdev,
  750. struct io_buffer *iobuf ) {
  751. struct smsc75xx_device *smsc75xx = netdev->priv;
  752. int rc;
  753. /* Transmit packet */
  754. if ( ( rc = smsc75xx_out_transmit ( smsc75xx, iobuf ) ) != 0 )
  755. return rc;
  756. return 0;
  757. }
  758. /**
  759. * Poll for completed and received packets
  760. *
  761. * @v netdev Network device
  762. */
  763. static void smsc75xx_poll ( struct net_device *netdev ) {
  764. struct smsc75xx_device *smsc75xx = netdev->priv;
  765. uint32_t int_sts;
  766. int rc;
  767. /* Poll USB bus */
  768. usb_poll ( smsc75xx->bus );
  769. /* Refill endpoints */
  770. if ( ( rc = usbnet_refill ( &smsc75xx->usbnet ) ) != 0 )
  771. netdev_rx_err ( netdev, NULL, rc );
  772. /* Do nothing more unless there are interrupts to handle */
  773. int_sts = smsc75xx->int_sts;
  774. if ( ! int_sts )
  775. return;
  776. /* Check link status if applicable */
  777. if ( int_sts & SMSC75XX_INT_STS_PHY_INT ) {
  778. smsc75xx_check_link ( smsc75xx );
  779. int_sts &= ~SMSC75XX_INT_STS_PHY_INT;
  780. }
  781. /* Record RX FIFO overflow if applicable */
  782. if ( int_sts & SMSC75XX_INT_STS_RDFO_INT ) {
  783. DBGC2 ( smsc75xx, "SMSC75XX %p RX FIFO overflowed\n",
  784. smsc75xx );
  785. netdev_rx_err ( netdev, NULL, -ENOBUFS );
  786. int_sts &= ~SMSC75XX_INT_STS_RDFO_INT;
  787. }
  788. /* Check for unexpected interrupts */
  789. if ( int_sts ) {
  790. DBGC ( smsc75xx, "SMSC75XX %p unexpected interrupt %#08x\n",
  791. smsc75xx, int_sts );
  792. netdev_rx_err ( netdev, NULL, -ENOTTY );
  793. }
  794. /* Clear interrupts */
  795. if ( ( rc = smsc75xx_writel ( smsc75xx, SMSC75XX_INT_STS,
  796. smsc75xx->int_sts ) ) != 0 )
  797. netdev_rx_err ( netdev, NULL, rc );
  798. smsc75xx->int_sts = 0;
  799. }
  800. /** SMSC75xx network device operations */
  801. static struct net_device_operations smsc75xx_operations = {
  802. .open = smsc75xx_open,
  803. .close = smsc75xx_close,
  804. .transmit = smsc75xx_transmit,
  805. .poll = smsc75xx_poll,
  806. };
  807. /******************************************************************************
  808. *
  809. * USB interface
  810. *
  811. ******************************************************************************
  812. */
  813. /**
  814. * Probe device
  815. *
  816. * @v func USB function
  817. * @v config Configuration descriptor
  818. * @ret rc Return status code
  819. */
  820. static int smsc75xx_probe ( struct usb_function *func,
  821. struct usb_configuration_descriptor *config ) {
  822. struct usb_device *usb = func->usb;
  823. struct net_device *netdev;
  824. struct smsc75xx_device *smsc75xx;
  825. int rc;
  826. /* Allocate and initialise structure */
  827. netdev = alloc_etherdev ( sizeof ( *smsc75xx ) );
  828. if ( ! netdev ) {
  829. rc = -ENOMEM;
  830. goto err_alloc;
  831. }
  832. netdev_init ( netdev, &smsc75xx_operations );
  833. netdev->dev = &func->dev;
  834. smsc75xx = netdev->priv;
  835. memset ( smsc75xx, 0, sizeof ( *smsc75xx ) );
  836. smsc75xx->usb = usb;
  837. smsc75xx->bus = usb->port->hub->bus;
  838. smsc75xx->netdev = netdev;
  839. usbnet_init ( &smsc75xx->usbnet, func, &smsc75xx_intr_operations,
  840. &smsc75xx_in_operations, &smsc75xx_out_operations );
  841. usb_refill_init ( &smsc75xx->usbnet.intr, 0, 0,
  842. SMSC75XX_INTR_MAX_FILL );
  843. usb_refill_init ( &smsc75xx->usbnet.in, 0, SMSC75XX_IN_MTU,
  844. SMSC75XX_IN_MAX_FILL );
  845. mii_init ( &smsc75xx->mii, &smsc75xx_mii_operations );
  846. DBGC ( smsc75xx, "SMSC75XX %p on %s\n", smsc75xx, func->name );
  847. /* Describe USB network device */
  848. if ( ( rc = usbnet_describe ( &smsc75xx->usbnet, config ) ) != 0 ) {
  849. DBGC ( smsc75xx, "SMSC75XX %p could not describe: %s\n",
  850. smsc75xx, strerror ( rc ) );
  851. goto err_describe;
  852. }
  853. /* Reset device */
  854. if ( ( rc = smsc75xx_reset ( smsc75xx ) ) != 0 )
  855. goto err_reset;
  856. /* Read MAC address */
  857. if ( ( rc = smsc75xx_eeprom_read ( smsc75xx, SMSC75XX_EEPROM_MAC,
  858. netdev->hw_addr, ETH_ALEN ) ) != 0 )
  859. goto err_eeprom_read;
  860. /* Register network device */
  861. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  862. goto err_register;
  863. usb_func_set_drvdata ( func, netdev );
  864. return 0;
  865. unregister_netdev ( netdev );
  866. err_register:
  867. err_eeprom_read:
  868. err_reset:
  869. err_describe:
  870. netdev_nullify ( netdev );
  871. netdev_put ( netdev );
  872. err_alloc:
  873. return rc;
  874. }
  875. /**
  876. * Remove device
  877. *
  878. * @v func USB function
  879. */
  880. static void smsc75xx_remove ( struct usb_function *func ) {
  881. struct net_device *netdev = usb_func_get_drvdata ( func );
  882. unregister_netdev ( netdev );
  883. netdev_nullify ( netdev );
  884. netdev_put ( netdev );
  885. }
  886. /** SMSC75xx device IDs */
  887. static struct usb_device_id smsc75xx_ids[] = {
  888. {
  889. .name = "smsc7500",
  890. .vendor = 0x0424,
  891. .product = 0x7500,
  892. },
  893. {
  894. .name = "smsc7505",
  895. .vendor = 0x0424,
  896. .product = 0x7505,
  897. },
  898. };
  899. /** SMSC LAN75xx driver */
  900. struct usb_driver smsc75xx_driver __usb_driver = {
  901. .ids = smsc75xx_ids,
  902. .id_count = ( sizeof ( smsc75xx_ids ) / sizeof ( smsc75xx_ids[0] ) ),
  903. .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
  904. .score = USB_SCORE_NORMAL,
  905. .probe = smsc75xx_probe,
  906. .remove = smsc75xx_remove,
  907. };