smsc9420.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. /***************************************************************************
  2. *
  3. * Copyright (C) 2007,2008 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. */
  21. #include <linux/kernel.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/phy.h>
  24. #include <linux/pci.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/crc32.h>
  28. #include <linux/slab.h>
  29. #include <asm/unaligned.h>
  30. #include "smsc9420.h"
  31. #define DRV_NAME "smsc9420"
  32. #define PFX DRV_NAME ": "
  33. #define DRV_MDIONAME "smsc9420-mdio"
  34. #define DRV_DESCRIPTION "SMSC LAN9420 driver"
  35. #define DRV_VERSION "1.01"
  36. MODULE_LICENSE("GPL");
  37. MODULE_VERSION(DRV_VERSION);
  38. struct smsc9420_dma_desc {
  39. u32 status;
  40. u32 length;
  41. u32 buffer1;
  42. u32 buffer2;
  43. };
  44. struct smsc9420_ring_info {
  45. struct sk_buff *skb;
  46. dma_addr_t mapping;
  47. };
  48. struct smsc9420_pdata {
  49. void __iomem *base_addr;
  50. struct pci_dev *pdev;
  51. struct net_device *dev;
  52. struct smsc9420_dma_desc *rx_ring;
  53. struct smsc9420_dma_desc *tx_ring;
  54. struct smsc9420_ring_info *tx_buffers;
  55. struct smsc9420_ring_info *rx_buffers;
  56. dma_addr_t rx_dma_addr;
  57. dma_addr_t tx_dma_addr;
  58. int tx_ring_head, tx_ring_tail;
  59. int rx_ring_head, rx_ring_tail;
  60. spinlock_t int_lock;
  61. spinlock_t phy_lock;
  62. struct napi_struct napi;
  63. bool software_irq_signal;
  64. bool rx_csum;
  65. u32 msg_enable;
  66. struct phy_device *phy_dev;
  67. struct mii_bus *mii_bus;
  68. int phy_irq[PHY_MAX_ADDR];
  69. int last_duplex;
  70. int last_carrier;
  71. };
  72. static DEFINE_PCI_DEVICE_TABLE(smsc9420_id_table) = {
  73. { PCI_VENDOR_ID_9420, PCI_DEVICE_ID_9420, PCI_ANY_ID, PCI_ANY_ID, },
  74. { 0, }
  75. };
  76. MODULE_DEVICE_TABLE(pci, smsc9420_id_table);
  77. #define SMSC_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  78. static uint smsc_debug;
  79. static uint debug = -1;
  80. module_param(debug, uint, 0);
  81. MODULE_PARM_DESC(debug, "debug level");
  82. #define smsc_dbg(TYPE, f, a...) \
  83. do { if ((pd)->msg_enable & NETIF_MSG_##TYPE) \
  84. printk(KERN_DEBUG PFX f "\n", ## a); \
  85. } while (0)
  86. #define smsc_info(TYPE, f, a...) \
  87. do { if ((pd)->msg_enable & NETIF_MSG_##TYPE) \
  88. printk(KERN_INFO PFX f "\n", ## a); \
  89. } while (0)
  90. #define smsc_warn(TYPE, f, a...) \
  91. do { if ((pd)->msg_enable & NETIF_MSG_##TYPE) \
  92. printk(KERN_WARNING PFX f "\n", ## a); \
  93. } while (0)
  94. static inline u32 smsc9420_reg_read(struct smsc9420_pdata *pd, u32 offset)
  95. {
  96. return ioread32(pd->base_addr + offset);
  97. }
  98. static inline void
  99. smsc9420_reg_write(struct smsc9420_pdata *pd, u32 offset, u32 value)
  100. {
  101. iowrite32(value, pd->base_addr + offset);
  102. }
  103. static inline void smsc9420_pci_flush_write(struct smsc9420_pdata *pd)
  104. {
  105. /* to ensure PCI write completion, we must perform a PCI read */
  106. smsc9420_reg_read(pd, ID_REV);
  107. }
  108. static int smsc9420_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
  109. {
  110. struct smsc9420_pdata *pd = (struct smsc9420_pdata *)bus->priv;
  111. unsigned long flags;
  112. u32 addr;
  113. int i, reg = -EIO;
  114. spin_lock_irqsave(&pd->phy_lock, flags);
  115. /* confirm MII not busy */
  116. if ((smsc9420_reg_read(pd, MII_ACCESS) & MII_ACCESS_MII_BUSY_)) {
  117. smsc_warn(DRV, "MII is busy???");
  118. goto out;
  119. }
  120. /* set the address, index & direction (read from PHY) */
  121. addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
  122. MII_ACCESS_MII_READ_;
  123. smsc9420_reg_write(pd, MII_ACCESS, addr);
  124. /* wait for read to complete with 50us timeout */
  125. for (i = 0; i < 5; i++) {
  126. if (!(smsc9420_reg_read(pd, MII_ACCESS) &
  127. MII_ACCESS_MII_BUSY_)) {
  128. reg = (u16)smsc9420_reg_read(pd, MII_DATA);
  129. goto out;
  130. }
  131. udelay(10);
  132. }
  133. smsc_warn(DRV, "MII busy timeout!");
  134. out:
  135. spin_unlock_irqrestore(&pd->phy_lock, flags);
  136. return reg;
  137. }
  138. static int smsc9420_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
  139. u16 val)
  140. {
  141. struct smsc9420_pdata *pd = (struct smsc9420_pdata *)bus->priv;
  142. unsigned long flags;
  143. u32 addr;
  144. int i, reg = -EIO;
  145. spin_lock_irqsave(&pd->phy_lock, flags);
  146. /* confirm MII not busy */
  147. if ((smsc9420_reg_read(pd, MII_ACCESS) & MII_ACCESS_MII_BUSY_)) {
  148. smsc_warn(DRV, "MII is busy???");
  149. goto out;
  150. }
  151. /* put the data to write in the MAC */
  152. smsc9420_reg_write(pd, MII_DATA, (u32)val);
  153. /* set the address, index & direction (write to PHY) */
  154. addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
  155. MII_ACCESS_MII_WRITE_;
  156. smsc9420_reg_write(pd, MII_ACCESS, addr);
  157. /* wait for write to complete with 50us timeout */
  158. for (i = 0; i < 5; i++) {
  159. if (!(smsc9420_reg_read(pd, MII_ACCESS) &
  160. MII_ACCESS_MII_BUSY_)) {
  161. reg = 0;
  162. goto out;
  163. }
  164. udelay(10);
  165. }
  166. smsc_warn(DRV, "MII busy timeout!");
  167. out:
  168. spin_unlock_irqrestore(&pd->phy_lock, flags);
  169. return reg;
  170. }
  171. /* Returns hash bit number for given MAC address
  172. * Example:
  173. * 01 00 5E 00 00 01 -> returns bit number 31 */
  174. static u32 smsc9420_hash(u8 addr[ETH_ALEN])
  175. {
  176. return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
  177. }
  178. static int smsc9420_eeprom_reload(struct smsc9420_pdata *pd)
  179. {
  180. int timeout = 100000;
  181. BUG_ON(!pd);
  182. if (smsc9420_reg_read(pd, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
  183. smsc_dbg(DRV, "smsc9420_eeprom_reload: Eeprom busy");
  184. return -EIO;
  185. }
  186. smsc9420_reg_write(pd, E2P_CMD,
  187. (E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_RELOAD_));
  188. do {
  189. udelay(10);
  190. if (!(smsc9420_reg_read(pd, E2P_CMD) & E2P_CMD_EPC_BUSY_))
  191. return 0;
  192. } while (timeout--);
  193. smsc_warn(DRV, "smsc9420_eeprom_reload: Eeprom timed out");
  194. return -EIO;
  195. }
  196. /* Standard ioctls for mii-tool */
  197. static int smsc9420_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  198. {
  199. struct smsc9420_pdata *pd = netdev_priv(dev);
  200. if (!netif_running(dev) || !pd->phy_dev)
  201. return -EINVAL;
  202. return phy_mii_ioctl(pd->phy_dev, ifr, cmd);
  203. }
  204. static int smsc9420_ethtool_get_settings(struct net_device *dev,
  205. struct ethtool_cmd *cmd)
  206. {
  207. struct smsc9420_pdata *pd = netdev_priv(dev);
  208. if (!pd->phy_dev)
  209. return -ENODEV;
  210. cmd->maxtxpkt = 1;
  211. cmd->maxrxpkt = 1;
  212. return phy_ethtool_gset(pd->phy_dev, cmd);
  213. }
  214. static int smsc9420_ethtool_set_settings(struct net_device *dev,
  215. struct ethtool_cmd *cmd)
  216. {
  217. struct smsc9420_pdata *pd = netdev_priv(dev);
  218. if (!pd->phy_dev)
  219. return -ENODEV;
  220. return phy_ethtool_sset(pd->phy_dev, cmd);
  221. }
  222. static void smsc9420_ethtool_get_drvinfo(struct net_device *netdev,
  223. struct ethtool_drvinfo *drvinfo)
  224. {
  225. struct smsc9420_pdata *pd = netdev_priv(netdev);
  226. strcpy(drvinfo->driver, DRV_NAME);
  227. strcpy(drvinfo->bus_info, pci_name(pd->pdev));
  228. strcpy(drvinfo->version, DRV_VERSION);
  229. }
  230. static u32 smsc9420_ethtool_get_msglevel(struct net_device *netdev)
  231. {
  232. struct smsc9420_pdata *pd = netdev_priv(netdev);
  233. return pd->msg_enable;
  234. }
  235. static void smsc9420_ethtool_set_msglevel(struct net_device *netdev, u32 data)
  236. {
  237. struct smsc9420_pdata *pd = netdev_priv(netdev);
  238. pd->msg_enable = data;
  239. }
  240. static int smsc9420_ethtool_nway_reset(struct net_device *netdev)
  241. {
  242. struct smsc9420_pdata *pd = netdev_priv(netdev);
  243. if (!pd->phy_dev)
  244. return -ENODEV;
  245. return phy_start_aneg(pd->phy_dev);
  246. }
  247. static int smsc9420_ethtool_getregslen(struct net_device *dev)
  248. {
  249. /* all smsc9420 registers plus all phy registers */
  250. return 0x100 + (32 * sizeof(u32));
  251. }
  252. static void
  253. smsc9420_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
  254. void *buf)
  255. {
  256. struct smsc9420_pdata *pd = netdev_priv(dev);
  257. struct phy_device *phy_dev = pd->phy_dev;
  258. unsigned int i, j = 0;
  259. u32 *data = buf;
  260. regs->version = smsc9420_reg_read(pd, ID_REV);
  261. for (i = 0; i < 0x100; i += (sizeof(u32)))
  262. data[j++] = smsc9420_reg_read(pd, i);
  263. // cannot read phy registers if the net device is down
  264. if (!phy_dev)
  265. return;
  266. for (i = 0; i <= 31; i++)
  267. data[j++] = smsc9420_mii_read(phy_dev->bus, phy_dev->addr, i);
  268. }
  269. static void smsc9420_eeprom_enable_access(struct smsc9420_pdata *pd)
  270. {
  271. unsigned int temp = smsc9420_reg_read(pd, GPIO_CFG);
  272. temp &= ~GPIO_CFG_EEPR_EN_;
  273. smsc9420_reg_write(pd, GPIO_CFG, temp);
  274. msleep(1);
  275. }
  276. static int smsc9420_eeprom_send_cmd(struct smsc9420_pdata *pd, u32 op)
  277. {
  278. int timeout = 100;
  279. u32 e2cmd;
  280. smsc_dbg(HW, "op 0x%08x", op);
  281. if (smsc9420_reg_read(pd, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
  282. smsc_warn(HW, "Busy at start");
  283. return -EBUSY;
  284. }
  285. e2cmd = op | E2P_CMD_EPC_BUSY_;
  286. smsc9420_reg_write(pd, E2P_CMD, e2cmd);
  287. do {
  288. msleep(1);
  289. e2cmd = smsc9420_reg_read(pd, E2P_CMD);
  290. } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
  291. if (!timeout) {
  292. smsc_info(HW, "TIMED OUT");
  293. return -EAGAIN;
  294. }
  295. if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
  296. smsc_info(HW, "Error occurred during eeprom operation");
  297. return -EINVAL;
  298. }
  299. return 0;
  300. }
  301. static int smsc9420_eeprom_read_location(struct smsc9420_pdata *pd,
  302. u8 address, u8 *data)
  303. {
  304. u32 op = E2P_CMD_EPC_CMD_READ_ | address;
  305. int ret;
  306. smsc_dbg(HW, "address 0x%x", address);
  307. ret = smsc9420_eeprom_send_cmd(pd, op);
  308. if (!ret)
  309. data[address] = smsc9420_reg_read(pd, E2P_DATA);
  310. return ret;
  311. }
  312. static int smsc9420_eeprom_write_location(struct smsc9420_pdata *pd,
  313. u8 address, u8 data)
  314. {
  315. u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
  316. int ret;
  317. smsc_dbg(HW, "address 0x%x, data 0x%x", address, data);
  318. ret = smsc9420_eeprom_send_cmd(pd, op);
  319. if (!ret) {
  320. op = E2P_CMD_EPC_CMD_WRITE_ | address;
  321. smsc9420_reg_write(pd, E2P_DATA, (u32)data);
  322. ret = smsc9420_eeprom_send_cmd(pd, op);
  323. }
  324. return ret;
  325. }
  326. static int smsc9420_ethtool_get_eeprom_len(struct net_device *dev)
  327. {
  328. return SMSC9420_EEPROM_SIZE;
  329. }
  330. static int smsc9420_ethtool_get_eeprom(struct net_device *dev,
  331. struct ethtool_eeprom *eeprom, u8 *data)
  332. {
  333. struct smsc9420_pdata *pd = netdev_priv(dev);
  334. u8 eeprom_data[SMSC9420_EEPROM_SIZE];
  335. int len, i;
  336. smsc9420_eeprom_enable_access(pd);
  337. len = min(eeprom->len, SMSC9420_EEPROM_SIZE);
  338. for (i = 0; i < len; i++) {
  339. int ret = smsc9420_eeprom_read_location(pd, i, eeprom_data);
  340. if (ret < 0) {
  341. eeprom->len = 0;
  342. return ret;
  343. }
  344. }
  345. memcpy(data, &eeprom_data[eeprom->offset], len);
  346. eeprom->magic = SMSC9420_EEPROM_MAGIC;
  347. eeprom->len = len;
  348. return 0;
  349. }
  350. static int smsc9420_ethtool_set_eeprom(struct net_device *dev,
  351. struct ethtool_eeprom *eeprom, u8 *data)
  352. {
  353. struct smsc9420_pdata *pd = netdev_priv(dev);
  354. int ret;
  355. if (eeprom->magic != SMSC9420_EEPROM_MAGIC)
  356. return -EINVAL;
  357. smsc9420_eeprom_enable_access(pd);
  358. smsc9420_eeprom_send_cmd(pd, E2P_CMD_EPC_CMD_EWEN_);
  359. ret = smsc9420_eeprom_write_location(pd, eeprom->offset, *data);
  360. smsc9420_eeprom_send_cmd(pd, E2P_CMD_EPC_CMD_EWDS_);
  361. /* Single byte write, according to man page */
  362. eeprom->len = 1;
  363. return ret;
  364. }
  365. static const struct ethtool_ops smsc9420_ethtool_ops = {
  366. .get_settings = smsc9420_ethtool_get_settings,
  367. .set_settings = smsc9420_ethtool_set_settings,
  368. .get_drvinfo = smsc9420_ethtool_get_drvinfo,
  369. .get_msglevel = smsc9420_ethtool_get_msglevel,
  370. .set_msglevel = smsc9420_ethtool_set_msglevel,
  371. .nway_reset = smsc9420_ethtool_nway_reset,
  372. .get_link = ethtool_op_get_link,
  373. .get_eeprom_len = smsc9420_ethtool_get_eeprom_len,
  374. .get_eeprom = smsc9420_ethtool_get_eeprom,
  375. .set_eeprom = smsc9420_ethtool_set_eeprom,
  376. .get_regs_len = smsc9420_ethtool_getregslen,
  377. .get_regs = smsc9420_ethtool_getregs,
  378. };
  379. /* Sets the device MAC address to dev_addr */
  380. static void smsc9420_set_mac_address(struct net_device *dev)
  381. {
  382. struct smsc9420_pdata *pd = netdev_priv(dev);
  383. u8 *dev_addr = dev->dev_addr;
  384. u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
  385. u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
  386. (dev_addr[1] << 8) | dev_addr[0];
  387. smsc9420_reg_write(pd, ADDRH, mac_high16);
  388. smsc9420_reg_write(pd, ADDRL, mac_low32);
  389. }
  390. static void smsc9420_check_mac_address(struct net_device *dev)
  391. {
  392. struct smsc9420_pdata *pd = netdev_priv(dev);
  393. /* Check if mac address has been specified when bringing interface up */
  394. if (is_valid_ether_addr(dev->dev_addr)) {
  395. smsc9420_set_mac_address(dev);
  396. smsc_dbg(PROBE, "MAC Address is specified by configuration");
  397. } else {
  398. /* Try reading mac address from device. if EEPROM is present
  399. * it will already have been set */
  400. u32 mac_high16 = smsc9420_reg_read(pd, ADDRH);
  401. u32 mac_low32 = smsc9420_reg_read(pd, ADDRL);
  402. dev->dev_addr[0] = (u8)(mac_low32);
  403. dev->dev_addr[1] = (u8)(mac_low32 >> 8);
  404. dev->dev_addr[2] = (u8)(mac_low32 >> 16);
  405. dev->dev_addr[3] = (u8)(mac_low32 >> 24);
  406. dev->dev_addr[4] = (u8)(mac_high16);
  407. dev->dev_addr[5] = (u8)(mac_high16 >> 8);
  408. if (is_valid_ether_addr(dev->dev_addr)) {
  409. /* eeprom values are valid so use them */
  410. smsc_dbg(PROBE, "Mac Address is read from EEPROM");
  411. } else {
  412. /* eeprom values are invalid, generate random MAC */
  413. random_ether_addr(dev->dev_addr);
  414. smsc9420_set_mac_address(dev);
  415. smsc_dbg(PROBE,
  416. "MAC Address is set to random_ether_addr");
  417. }
  418. }
  419. }
  420. static void smsc9420_stop_tx(struct smsc9420_pdata *pd)
  421. {
  422. u32 dmac_control, mac_cr, dma_intr_ena;
  423. int timeout = 1000;
  424. /* disable TX DMAC */
  425. dmac_control = smsc9420_reg_read(pd, DMAC_CONTROL);
  426. dmac_control &= (~DMAC_CONTROL_ST_);
  427. smsc9420_reg_write(pd, DMAC_CONTROL, dmac_control);
  428. /* Wait max 10ms for transmit process to stop */
  429. while (--timeout) {
  430. if (smsc9420_reg_read(pd, DMAC_STATUS) & DMAC_STS_TS_)
  431. break;
  432. udelay(10);
  433. }
  434. if (!timeout)
  435. smsc_warn(IFDOWN, "TX DMAC failed to stop");
  436. /* ACK Tx DMAC stop bit */
  437. smsc9420_reg_write(pd, DMAC_STATUS, DMAC_STS_TXPS_);
  438. /* mask TX DMAC interrupts */
  439. dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
  440. dma_intr_ena &= ~(DMAC_INTR_ENA_TX_);
  441. smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
  442. smsc9420_pci_flush_write(pd);
  443. /* stop MAC TX */
  444. mac_cr = smsc9420_reg_read(pd, MAC_CR) & (~MAC_CR_TXEN_);
  445. smsc9420_reg_write(pd, MAC_CR, mac_cr);
  446. smsc9420_pci_flush_write(pd);
  447. }
  448. static void smsc9420_free_tx_ring(struct smsc9420_pdata *pd)
  449. {
  450. int i;
  451. BUG_ON(!pd->tx_ring);
  452. if (!pd->tx_buffers)
  453. return;
  454. for (i = 0; i < TX_RING_SIZE; i++) {
  455. struct sk_buff *skb = pd->tx_buffers[i].skb;
  456. if (skb) {
  457. BUG_ON(!pd->tx_buffers[i].mapping);
  458. pci_unmap_single(pd->pdev, pd->tx_buffers[i].mapping,
  459. skb->len, PCI_DMA_TODEVICE);
  460. dev_kfree_skb_any(skb);
  461. }
  462. pd->tx_ring[i].status = 0;
  463. pd->tx_ring[i].length = 0;
  464. pd->tx_ring[i].buffer1 = 0;
  465. pd->tx_ring[i].buffer2 = 0;
  466. }
  467. wmb();
  468. kfree(pd->tx_buffers);
  469. pd->tx_buffers = NULL;
  470. pd->tx_ring_head = 0;
  471. pd->tx_ring_tail = 0;
  472. }
  473. static void smsc9420_free_rx_ring(struct smsc9420_pdata *pd)
  474. {
  475. int i;
  476. BUG_ON(!pd->rx_ring);
  477. if (!pd->rx_buffers)
  478. return;
  479. for (i = 0; i < RX_RING_SIZE; i++) {
  480. if (pd->rx_buffers[i].skb)
  481. dev_kfree_skb_any(pd->rx_buffers[i].skb);
  482. if (pd->rx_buffers[i].mapping)
  483. pci_unmap_single(pd->pdev, pd->rx_buffers[i].mapping,
  484. PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
  485. pd->rx_ring[i].status = 0;
  486. pd->rx_ring[i].length = 0;
  487. pd->rx_ring[i].buffer1 = 0;
  488. pd->rx_ring[i].buffer2 = 0;
  489. }
  490. wmb();
  491. kfree(pd->rx_buffers);
  492. pd->rx_buffers = NULL;
  493. pd->rx_ring_head = 0;
  494. pd->rx_ring_tail = 0;
  495. }
  496. static void smsc9420_stop_rx(struct smsc9420_pdata *pd)
  497. {
  498. int timeout = 1000;
  499. u32 mac_cr, dmac_control, dma_intr_ena;
  500. /* mask RX DMAC interrupts */
  501. dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
  502. dma_intr_ena &= (~DMAC_INTR_ENA_RX_);
  503. smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
  504. smsc9420_pci_flush_write(pd);
  505. /* stop RX MAC prior to stoping DMA */
  506. mac_cr = smsc9420_reg_read(pd, MAC_CR) & (~MAC_CR_RXEN_);
  507. smsc9420_reg_write(pd, MAC_CR, mac_cr);
  508. smsc9420_pci_flush_write(pd);
  509. /* stop RX DMAC */
  510. dmac_control = smsc9420_reg_read(pd, DMAC_CONTROL);
  511. dmac_control &= (~DMAC_CONTROL_SR_);
  512. smsc9420_reg_write(pd, DMAC_CONTROL, dmac_control);
  513. smsc9420_pci_flush_write(pd);
  514. /* wait up to 10ms for receive to stop */
  515. while (--timeout) {
  516. if (smsc9420_reg_read(pd, DMAC_STATUS) & DMAC_STS_RS_)
  517. break;
  518. udelay(10);
  519. }
  520. if (!timeout)
  521. smsc_warn(IFDOWN, "RX DMAC did not stop! timeout.");
  522. /* ACK the Rx DMAC stop bit */
  523. smsc9420_reg_write(pd, DMAC_STATUS, DMAC_STS_RXPS_);
  524. }
  525. static irqreturn_t smsc9420_isr(int irq, void *dev_id)
  526. {
  527. struct smsc9420_pdata *pd = dev_id;
  528. u32 int_cfg, int_sts, int_ctl;
  529. irqreturn_t ret = IRQ_NONE;
  530. ulong flags;
  531. BUG_ON(!pd);
  532. BUG_ON(!pd->base_addr);
  533. int_cfg = smsc9420_reg_read(pd, INT_CFG);
  534. /* check if it's our interrupt */
  535. if ((int_cfg & (INT_CFG_IRQ_EN_ | INT_CFG_IRQ_INT_)) !=
  536. (INT_CFG_IRQ_EN_ | INT_CFG_IRQ_INT_))
  537. return IRQ_NONE;
  538. int_sts = smsc9420_reg_read(pd, INT_STAT);
  539. if (likely(INT_STAT_DMAC_INT_ & int_sts)) {
  540. u32 status = smsc9420_reg_read(pd, DMAC_STATUS);
  541. u32 ints_to_clear = 0;
  542. if (status & DMAC_STS_TX_) {
  543. ints_to_clear |= (DMAC_STS_TX_ | DMAC_STS_NIS_);
  544. netif_wake_queue(pd->dev);
  545. }
  546. if (status & DMAC_STS_RX_) {
  547. /* mask RX DMAC interrupts */
  548. u32 dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
  549. dma_intr_ena &= (~DMAC_INTR_ENA_RX_);
  550. smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
  551. smsc9420_pci_flush_write(pd);
  552. ints_to_clear |= (DMAC_STS_RX_ | DMAC_STS_NIS_);
  553. napi_schedule(&pd->napi);
  554. }
  555. if (ints_to_clear)
  556. smsc9420_reg_write(pd, DMAC_STATUS, ints_to_clear);
  557. ret = IRQ_HANDLED;
  558. }
  559. if (unlikely(INT_STAT_SW_INT_ & int_sts)) {
  560. /* mask software interrupt */
  561. spin_lock_irqsave(&pd->int_lock, flags);
  562. int_ctl = smsc9420_reg_read(pd, INT_CTL);
  563. int_ctl &= (~INT_CTL_SW_INT_EN_);
  564. smsc9420_reg_write(pd, INT_CTL, int_ctl);
  565. spin_unlock_irqrestore(&pd->int_lock, flags);
  566. smsc9420_reg_write(pd, INT_STAT, INT_STAT_SW_INT_);
  567. pd->software_irq_signal = true;
  568. smp_wmb();
  569. ret = IRQ_HANDLED;
  570. }
  571. /* to ensure PCI write completion, we must perform a PCI read */
  572. smsc9420_pci_flush_write(pd);
  573. return ret;
  574. }
  575. #ifdef CONFIG_NET_POLL_CONTROLLER
  576. static void smsc9420_poll_controller(struct net_device *dev)
  577. {
  578. disable_irq(dev->irq);
  579. smsc9420_isr(0, dev);
  580. enable_irq(dev->irq);
  581. }
  582. #endif /* CONFIG_NET_POLL_CONTROLLER */
  583. static void smsc9420_dmac_soft_reset(struct smsc9420_pdata *pd)
  584. {
  585. smsc9420_reg_write(pd, BUS_MODE, BUS_MODE_SWR_);
  586. smsc9420_reg_read(pd, BUS_MODE);
  587. udelay(2);
  588. if (smsc9420_reg_read(pd, BUS_MODE) & BUS_MODE_SWR_)
  589. smsc_warn(DRV, "Software reset not cleared");
  590. }
  591. static int smsc9420_stop(struct net_device *dev)
  592. {
  593. struct smsc9420_pdata *pd = netdev_priv(dev);
  594. u32 int_cfg;
  595. ulong flags;
  596. BUG_ON(!pd);
  597. BUG_ON(!pd->phy_dev);
  598. /* disable master interrupt */
  599. spin_lock_irqsave(&pd->int_lock, flags);
  600. int_cfg = smsc9420_reg_read(pd, INT_CFG) & (~INT_CFG_IRQ_EN_);
  601. smsc9420_reg_write(pd, INT_CFG, int_cfg);
  602. spin_unlock_irqrestore(&pd->int_lock, flags);
  603. netif_tx_disable(dev);
  604. napi_disable(&pd->napi);
  605. smsc9420_stop_tx(pd);
  606. smsc9420_free_tx_ring(pd);
  607. smsc9420_stop_rx(pd);
  608. smsc9420_free_rx_ring(pd);
  609. free_irq(dev->irq, pd);
  610. smsc9420_dmac_soft_reset(pd);
  611. phy_stop(pd->phy_dev);
  612. phy_disconnect(pd->phy_dev);
  613. pd->phy_dev = NULL;
  614. mdiobus_unregister(pd->mii_bus);
  615. mdiobus_free(pd->mii_bus);
  616. return 0;
  617. }
  618. static void smsc9420_rx_count_stats(struct net_device *dev, u32 desc_status)
  619. {
  620. if (unlikely(desc_status & RDES0_ERROR_SUMMARY_)) {
  621. dev->stats.rx_errors++;
  622. if (desc_status & RDES0_DESCRIPTOR_ERROR_)
  623. dev->stats.rx_over_errors++;
  624. else if (desc_status & (RDES0_FRAME_TOO_LONG_ |
  625. RDES0_RUNT_FRAME_ | RDES0_COLLISION_SEEN_))
  626. dev->stats.rx_frame_errors++;
  627. else if (desc_status & RDES0_CRC_ERROR_)
  628. dev->stats.rx_crc_errors++;
  629. }
  630. if (unlikely(desc_status & RDES0_LENGTH_ERROR_))
  631. dev->stats.rx_length_errors++;
  632. if (unlikely(!((desc_status & RDES0_LAST_DESCRIPTOR_) &&
  633. (desc_status & RDES0_FIRST_DESCRIPTOR_))))
  634. dev->stats.rx_length_errors++;
  635. if (desc_status & RDES0_MULTICAST_FRAME_)
  636. dev->stats.multicast++;
  637. }
  638. static void smsc9420_rx_handoff(struct smsc9420_pdata *pd, const int index,
  639. const u32 status)
  640. {
  641. struct net_device *dev = pd->dev;
  642. struct sk_buff *skb;
  643. u16 packet_length = (status & RDES0_FRAME_LENGTH_MASK_)
  644. >> RDES0_FRAME_LENGTH_SHFT_;
  645. /* remove crc from packet lendth */
  646. packet_length -= 4;
  647. if (pd->rx_csum)
  648. packet_length -= 2;
  649. dev->stats.rx_packets++;
  650. dev->stats.rx_bytes += packet_length;
  651. pci_unmap_single(pd->pdev, pd->rx_buffers[index].mapping,
  652. PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
  653. pd->rx_buffers[index].mapping = 0;
  654. skb = pd->rx_buffers[index].skb;
  655. pd->rx_buffers[index].skb = NULL;
  656. if (pd->rx_csum) {
  657. u16 hw_csum = get_unaligned_le16(skb_tail_pointer(skb) +
  658. NET_IP_ALIGN + packet_length + 4);
  659. put_unaligned_le16(hw_csum, &skb->csum);
  660. skb->ip_summed = CHECKSUM_COMPLETE;
  661. }
  662. skb_reserve(skb, NET_IP_ALIGN);
  663. skb_put(skb, packet_length);
  664. skb->protocol = eth_type_trans(skb, dev);
  665. netif_receive_skb(skb);
  666. }
  667. static int smsc9420_alloc_rx_buffer(struct smsc9420_pdata *pd, int index)
  668. {
  669. struct sk_buff *skb = netdev_alloc_skb(pd->dev, PKT_BUF_SZ);
  670. dma_addr_t mapping;
  671. BUG_ON(pd->rx_buffers[index].skb);
  672. BUG_ON(pd->rx_buffers[index].mapping);
  673. if (unlikely(!skb)) {
  674. smsc_warn(RX_ERR, "Failed to allocate new skb!");
  675. return -ENOMEM;
  676. }
  677. skb->dev = pd->dev;
  678. mapping = pci_map_single(pd->pdev, skb_tail_pointer(skb),
  679. PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
  680. if (pci_dma_mapping_error(pd->pdev, mapping)) {
  681. dev_kfree_skb_any(skb);
  682. smsc_warn(RX_ERR, "pci_map_single failed!");
  683. return -ENOMEM;
  684. }
  685. pd->rx_buffers[index].skb = skb;
  686. pd->rx_buffers[index].mapping = mapping;
  687. pd->rx_ring[index].buffer1 = mapping + NET_IP_ALIGN;
  688. pd->rx_ring[index].status = RDES0_OWN_;
  689. wmb();
  690. return 0;
  691. }
  692. static void smsc9420_alloc_new_rx_buffers(struct smsc9420_pdata *pd)
  693. {
  694. while (pd->rx_ring_tail != pd->rx_ring_head) {
  695. if (smsc9420_alloc_rx_buffer(pd, pd->rx_ring_tail))
  696. break;
  697. pd->rx_ring_tail = (pd->rx_ring_tail + 1) % RX_RING_SIZE;
  698. }
  699. }
  700. static int smsc9420_rx_poll(struct napi_struct *napi, int budget)
  701. {
  702. struct smsc9420_pdata *pd =
  703. container_of(napi, struct smsc9420_pdata, napi);
  704. struct net_device *dev = pd->dev;
  705. u32 drop_frame_cnt, dma_intr_ena, status;
  706. int work_done;
  707. for (work_done = 0; work_done < budget; work_done++) {
  708. rmb();
  709. status = pd->rx_ring[pd->rx_ring_head].status;
  710. /* stop if DMAC owns this dma descriptor */
  711. if (status & RDES0_OWN_)
  712. break;
  713. smsc9420_rx_count_stats(dev, status);
  714. smsc9420_rx_handoff(pd, pd->rx_ring_head, status);
  715. pd->rx_ring_head = (pd->rx_ring_head + 1) % RX_RING_SIZE;
  716. smsc9420_alloc_new_rx_buffers(pd);
  717. }
  718. drop_frame_cnt = smsc9420_reg_read(pd, MISS_FRAME_CNTR);
  719. dev->stats.rx_dropped +=
  720. (drop_frame_cnt & 0xFFFF) + ((drop_frame_cnt >> 17) & 0x3FF);
  721. /* Kick RXDMA */
  722. smsc9420_reg_write(pd, RX_POLL_DEMAND, 1);
  723. smsc9420_pci_flush_write(pd);
  724. if (work_done < budget) {
  725. napi_complete(&pd->napi);
  726. /* re-enable RX DMA interrupts */
  727. dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
  728. dma_intr_ena |= (DMAC_INTR_ENA_RX_ | DMAC_INTR_ENA_NIS_);
  729. smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
  730. smsc9420_pci_flush_write(pd);
  731. }
  732. return work_done;
  733. }
  734. static void
  735. smsc9420_tx_update_stats(struct net_device *dev, u32 status, u32 length)
  736. {
  737. if (unlikely(status & TDES0_ERROR_SUMMARY_)) {
  738. dev->stats.tx_errors++;
  739. if (status & (TDES0_EXCESSIVE_DEFERRAL_ |
  740. TDES0_EXCESSIVE_COLLISIONS_))
  741. dev->stats.tx_aborted_errors++;
  742. if (status & (TDES0_LOSS_OF_CARRIER_ | TDES0_NO_CARRIER_))
  743. dev->stats.tx_carrier_errors++;
  744. } else {
  745. dev->stats.tx_packets++;
  746. dev->stats.tx_bytes += (length & 0x7FF);
  747. }
  748. if (unlikely(status & TDES0_EXCESSIVE_COLLISIONS_)) {
  749. dev->stats.collisions += 16;
  750. } else {
  751. dev->stats.collisions +=
  752. (status & TDES0_COLLISION_COUNT_MASK_) >>
  753. TDES0_COLLISION_COUNT_SHFT_;
  754. }
  755. if (unlikely(status & TDES0_HEARTBEAT_FAIL_))
  756. dev->stats.tx_heartbeat_errors++;
  757. }
  758. /* Check for completed dma transfers, update stats and free skbs */
  759. static void smsc9420_complete_tx(struct net_device *dev)
  760. {
  761. struct smsc9420_pdata *pd = netdev_priv(dev);
  762. while (pd->tx_ring_tail != pd->tx_ring_head) {
  763. int index = pd->tx_ring_tail;
  764. u32 status, length;
  765. rmb();
  766. status = pd->tx_ring[index].status;
  767. length = pd->tx_ring[index].length;
  768. /* Check if DMA still owns this descriptor */
  769. if (unlikely(TDES0_OWN_ & status))
  770. break;
  771. smsc9420_tx_update_stats(dev, status, length);
  772. BUG_ON(!pd->tx_buffers[index].skb);
  773. BUG_ON(!pd->tx_buffers[index].mapping);
  774. pci_unmap_single(pd->pdev, pd->tx_buffers[index].mapping,
  775. pd->tx_buffers[index].skb->len, PCI_DMA_TODEVICE);
  776. pd->tx_buffers[index].mapping = 0;
  777. dev_kfree_skb_any(pd->tx_buffers[index].skb);
  778. pd->tx_buffers[index].skb = NULL;
  779. pd->tx_ring[index].buffer1 = 0;
  780. wmb();
  781. pd->tx_ring_tail = (pd->tx_ring_tail + 1) % TX_RING_SIZE;
  782. }
  783. }
  784. static netdev_tx_t smsc9420_hard_start_xmit(struct sk_buff *skb,
  785. struct net_device *dev)
  786. {
  787. struct smsc9420_pdata *pd = netdev_priv(dev);
  788. dma_addr_t mapping;
  789. int index = pd->tx_ring_head;
  790. u32 tmp_desc1;
  791. bool about_to_take_last_desc =
  792. (((pd->tx_ring_head + 2) % TX_RING_SIZE) == pd->tx_ring_tail);
  793. smsc9420_complete_tx(dev);
  794. rmb();
  795. BUG_ON(pd->tx_ring[index].status & TDES0_OWN_);
  796. BUG_ON(pd->tx_buffers[index].skb);
  797. BUG_ON(pd->tx_buffers[index].mapping);
  798. mapping = pci_map_single(pd->pdev, skb->data,
  799. skb->len, PCI_DMA_TODEVICE);
  800. if (pci_dma_mapping_error(pd->pdev, mapping)) {
  801. smsc_warn(TX_ERR, "pci_map_single failed, dropping packet");
  802. return NETDEV_TX_BUSY;
  803. }
  804. pd->tx_buffers[index].skb = skb;
  805. pd->tx_buffers[index].mapping = mapping;
  806. tmp_desc1 = (TDES1_LS_ | ((u32)skb->len & 0x7FF));
  807. if (unlikely(about_to_take_last_desc)) {
  808. tmp_desc1 |= TDES1_IC_;
  809. netif_stop_queue(pd->dev);
  810. }
  811. /* check if we are at the last descriptor and need to set EOR */
  812. if (unlikely(index == (TX_RING_SIZE - 1)))
  813. tmp_desc1 |= TDES1_TER_;
  814. pd->tx_ring[index].buffer1 = mapping;
  815. pd->tx_ring[index].length = tmp_desc1;
  816. wmb();
  817. /* increment head */
  818. pd->tx_ring_head = (pd->tx_ring_head + 1) % TX_RING_SIZE;
  819. /* assign ownership to DMAC */
  820. pd->tx_ring[index].status = TDES0_OWN_;
  821. wmb();
  822. /* kick the DMA */
  823. smsc9420_reg_write(pd, TX_POLL_DEMAND, 1);
  824. smsc9420_pci_flush_write(pd);
  825. return NETDEV_TX_OK;
  826. }
  827. static struct net_device_stats *smsc9420_get_stats(struct net_device *dev)
  828. {
  829. struct smsc9420_pdata *pd = netdev_priv(dev);
  830. u32 counter = smsc9420_reg_read(pd, MISS_FRAME_CNTR);
  831. dev->stats.rx_dropped +=
  832. (counter & 0x0000FFFF) + ((counter >> 17) & 0x000003FF);
  833. return &dev->stats;
  834. }
  835. static void smsc9420_set_multicast_list(struct net_device *dev)
  836. {
  837. struct smsc9420_pdata *pd = netdev_priv(dev);
  838. u32 mac_cr = smsc9420_reg_read(pd, MAC_CR);
  839. if (dev->flags & IFF_PROMISC) {
  840. smsc_dbg(HW, "Promiscuous Mode Enabled");
  841. mac_cr |= MAC_CR_PRMS_;
  842. mac_cr &= (~MAC_CR_MCPAS_);
  843. mac_cr &= (~MAC_CR_HPFILT_);
  844. } else if (dev->flags & IFF_ALLMULTI) {
  845. smsc_dbg(HW, "Receive all Multicast Enabled");
  846. mac_cr &= (~MAC_CR_PRMS_);
  847. mac_cr |= MAC_CR_MCPAS_;
  848. mac_cr &= (~MAC_CR_HPFILT_);
  849. } else if (!netdev_mc_empty(dev)) {
  850. struct netdev_hw_addr *ha;
  851. u32 hash_lo = 0, hash_hi = 0;
  852. smsc_dbg(HW, "Multicast filter enabled");
  853. netdev_for_each_mc_addr(ha, dev) {
  854. u32 bit_num = smsc9420_hash(ha->addr);
  855. u32 mask = 1 << (bit_num & 0x1F);
  856. if (bit_num & 0x20)
  857. hash_hi |= mask;
  858. else
  859. hash_lo |= mask;
  860. }
  861. smsc9420_reg_write(pd, HASHH, hash_hi);
  862. smsc9420_reg_write(pd, HASHL, hash_lo);
  863. mac_cr &= (~MAC_CR_PRMS_);
  864. mac_cr &= (~MAC_CR_MCPAS_);
  865. mac_cr |= MAC_CR_HPFILT_;
  866. } else {
  867. smsc_dbg(HW, "Receive own packets only.");
  868. smsc9420_reg_write(pd, HASHH, 0);
  869. smsc9420_reg_write(pd, HASHL, 0);
  870. mac_cr &= (~MAC_CR_PRMS_);
  871. mac_cr &= (~MAC_CR_MCPAS_);
  872. mac_cr &= (~MAC_CR_HPFILT_);
  873. }
  874. smsc9420_reg_write(pd, MAC_CR, mac_cr);
  875. smsc9420_pci_flush_write(pd);
  876. }
  877. static void smsc9420_phy_update_flowcontrol(struct smsc9420_pdata *pd)
  878. {
  879. struct phy_device *phy_dev = pd->phy_dev;
  880. u32 flow;
  881. if (phy_dev->duplex == DUPLEX_FULL) {
  882. u16 lcladv = phy_read(phy_dev, MII_ADVERTISE);
  883. u16 rmtadv = phy_read(phy_dev, MII_LPA);
  884. u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
  885. if (cap & FLOW_CTRL_RX)
  886. flow = 0xFFFF0002;
  887. else
  888. flow = 0;
  889. smsc_info(LINK, "rx pause %s, tx pause %s",
  890. (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
  891. (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
  892. } else {
  893. smsc_info(LINK, "half duplex");
  894. flow = 0;
  895. }
  896. smsc9420_reg_write(pd, FLOW, flow);
  897. }
  898. /* Update link mode if anything has changed. Called periodically when the
  899. * PHY is in polling mode, even if nothing has changed. */
  900. static void smsc9420_phy_adjust_link(struct net_device *dev)
  901. {
  902. struct smsc9420_pdata *pd = netdev_priv(dev);
  903. struct phy_device *phy_dev = pd->phy_dev;
  904. int carrier;
  905. if (phy_dev->duplex != pd->last_duplex) {
  906. u32 mac_cr = smsc9420_reg_read(pd, MAC_CR);
  907. if (phy_dev->duplex) {
  908. smsc_dbg(LINK, "full duplex mode");
  909. mac_cr |= MAC_CR_FDPX_;
  910. } else {
  911. smsc_dbg(LINK, "half duplex mode");
  912. mac_cr &= ~MAC_CR_FDPX_;
  913. }
  914. smsc9420_reg_write(pd, MAC_CR, mac_cr);
  915. smsc9420_phy_update_flowcontrol(pd);
  916. pd->last_duplex = phy_dev->duplex;
  917. }
  918. carrier = netif_carrier_ok(dev);
  919. if (carrier != pd->last_carrier) {
  920. if (carrier)
  921. smsc_dbg(LINK, "carrier OK");
  922. else
  923. smsc_dbg(LINK, "no carrier");
  924. pd->last_carrier = carrier;
  925. }
  926. }
  927. static int smsc9420_mii_probe(struct net_device *dev)
  928. {
  929. struct smsc9420_pdata *pd = netdev_priv(dev);
  930. struct phy_device *phydev = NULL;
  931. BUG_ON(pd->phy_dev);
  932. /* Device only supports internal PHY at address 1 */
  933. if (!pd->mii_bus->phy_map[1]) {
  934. pr_err("%s: no PHY found at address 1\n", dev->name);
  935. return -ENODEV;
  936. }
  937. phydev = pd->mii_bus->phy_map[1];
  938. smsc_info(PROBE, "PHY addr %d, phy_id 0x%08X", phydev->addr,
  939. phydev->phy_id);
  940. phydev = phy_connect(dev, dev_name(&phydev->dev),
  941. smsc9420_phy_adjust_link, 0, PHY_INTERFACE_MODE_MII);
  942. if (IS_ERR(phydev)) {
  943. pr_err("%s: Could not attach to PHY\n", dev->name);
  944. return PTR_ERR(phydev);
  945. }
  946. pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  947. dev->name, phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  948. /* mask with MAC supported features */
  949. phydev->supported &= (PHY_BASIC_FEATURES | SUPPORTED_Pause |
  950. SUPPORTED_Asym_Pause);
  951. phydev->advertising = phydev->supported;
  952. pd->phy_dev = phydev;
  953. pd->last_duplex = -1;
  954. pd->last_carrier = -1;
  955. return 0;
  956. }
  957. static int smsc9420_mii_init(struct net_device *dev)
  958. {
  959. struct smsc9420_pdata *pd = netdev_priv(dev);
  960. int err = -ENXIO, i;
  961. pd->mii_bus = mdiobus_alloc();
  962. if (!pd->mii_bus) {
  963. err = -ENOMEM;
  964. goto err_out_1;
  965. }
  966. pd->mii_bus->name = DRV_MDIONAME;
  967. snprintf(pd->mii_bus->id, MII_BUS_ID_SIZE, "%x",
  968. (pd->pdev->bus->number << 8) | pd->pdev->devfn);
  969. pd->mii_bus->priv = pd;
  970. pd->mii_bus->read = smsc9420_mii_read;
  971. pd->mii_bus->write = smsc9420_mii_write;
  972. pd->mii_bus->irq = pd->phy_irq;
  973. for (i = 0; i < PHY_MAX_ADDR; ++i)
  974. pd->mii_bus->irq[i] = PHY_POLL;
  975. /* Mask all PHYs except ID 1 (internal) */
  976. pd->mii_bus->phy_mask = ~(1 << 1);
  977. if (mdiobus_register(pd->mii_bus)) {
  978. smsc_warn(PROBE, "Error registering mii bus");
  979. goto err_out_free_bus_2;
  980. }
  981. if (smsc9420_mii_probe(dev) < 0) {
  982. smsc_warn(PROBE, "Error probing mii bus");
  983. goto err_out_unregister_bus_3;
  984. }
  985. return 0;
  986. err_out_unregister_bus_3:
  987. mdiobus_unregister(pd->mii_bus);
  988. err_out_free_bus_2:
  989. mdiobus_free(pd->mii_bus);
  990. err_out_1:
  991. return err;
  992. }
  993. static int smsc9420_alloc_tx_ring(struct smsc9420_pdata *pd)
  994. {
  995. int i;
  996. BUG_ON(!pd->tx_ring);
  997. pd->tx_buffers = kmalloc((sizeof(struct smsc9420_ring_info) *
  998. TX_RING_SIZE), GFP_KERNEL);
  999. if (!pd->tx_buffers) {
  1000. smsc_warn(IFUP, "Failed to allocated tx_buffers");
  1001. return -ENOMEM;
  1002. }
  1003. /* Initialize the TX Ring */
  1004. for (i = 0; i < TX_RING_SIZE; i++) {
  1005. pd->tx_buffers[i].skb = NULL;
  1006. pd->tx_buffers[i].mapping = 0;
  1007. pd->tx_ring[i].status = 0;
  1008. pd->tx_ring[i].length = 0;
  1009. pd->tx_ring[i].buffer1 = 0;
  1010. pd->tx_ring[i].buffer2 = 0;
  1011. }
  1012. pd->tx_ring[TX_RING_SIZE - 1].length = TDES1_TER_;
  1013. wmb();
  1014. pd->tx_ring_head = 0;
  1015. pd->tx_ring_tail = 0;
  1016. smsc9420_reg_write(pd, TX_BASE_ADDR, pd->tx_dma_addr);
  1017. smsc9420_pci_flush_write(pd);
  1018. return 0;
  1019. }
  1020. static int smsc9420_alloc_rx_ring(struct smsc9420_pdata *pd)
  1021. {
  1022. int i;
  1023. BUG_ON(!pd->rx_ring);
  1024. pd->rx_buffers = kmalloc((sizeof(struct smsc9420_ring_info) *
  1025. RX_RING_SIZE), GFP_KERNEL);
  1026. if (pd->rx_buffers == NULL) {
  1027. smsc_warn(IFUP, "Failed to allocated rx_buffers");
  1028. goto out;
  1029. }
  1030. /* initialize the rx ring */
  1031. for (i = 0; i < RX_RING_SIZE; i++) {
  1032. pd->rx_ring[i].status = 0;
  1033. pd->rx_ring[i].length = PKT_BUF_SZ;
  1034. pd->rx_ring[i].buffer2 = 0;
  1035. pd->rx_buffers[i].skb = NULL;
  1036. pd->rx_buffers[i].mapping = 0;
  1037. }
  1038. pd->rx_ring[RX_RING_SIZE - 1].length = (PKT_BUF_SZ | RDES1_RER_);
  1039. /* now allocate the entire ring of skbs */
  1040. for (i = 0; i < RX_RING_SIZE; i++) {
  1041. if (smsc9420_alloc_rx_buffer(pd, i)) {
  1042. smsc_warn(IFUP, "failed to allocate rx skb %d", i);
  1043. goto out_free_rx_skbs;
  1044. }
  1045. }
  1046. pd->rx_ring_head = 0;
  1047. pd->rx_ring_tail = 0;
  1048. smsc9420_reg_write(pd, VLAN1, ETH_P_8021Q);
  1049. smsc_dbg(IFUP, "VLAN1 = 0x%08x", smsc9420_reg_read(pd, VLAN1));
  1050. if (pd->rx_csum) {
  1051. /* Enable RX COE */
  1052. u32 coe = smsc9420_reg_read(pd, COE_CR) | RX_COE_EN;
  1053. smsc9420_reg_write(pd, COE_CR, coe);
  1054. smsc_dbg(IFUP, "COE_CR = 0x%08x", coe);
  1055. }
  1056. smsc9420_reg_write(pd, RX_BASE_ADDR, pd->rx_dma_addr);
  1057. smsc9420_pci_flush_write(pd);
  1058. return 0;
  1059. out_free_rx_skbs:
  1060. smsc9420_free_rx_ring(pd);
  1061. out:
  1062. return -ENOMEM;
  1063. }
  1064. static int smsc9420_open(struct net_device *dev)
  1065. {
  1066. struct smsc9420_pdata *pd;
  1067. u32 bus_mode, mac_cr, dmac_control, int_cfg, dma_intr_ena, int_ctl;
  1068. unsigned long flags;
  1069. int result = 0, timeout;
  1070. BUG_ON(!dev);
  1071. pd = netdev_priv(dev);
  1072. BUG_ON(!pd);
  1073. if (!is_valid_ether_addr(dev->dev_addr)) {
  1074. smsc_warn(IFUP, "dev_addr is not a valid MAC address");
  1075. result = -EADDRNOTAVAIL;
  1076. goto out_0;
  1077. }
  1078. netif_carrier_off(dev);
  1079. /* disable, mask and acknowledge all interrupts */
  1080. spin_lock_irqsave(&pd->int_lock, flags);
  1081. int_cfg = smsc9420_reg_read(pd, INT_CFG) & (~INT_CFG_IRQ_EN_);
  1082. smsc9420_reg_write(pd, INT_CFG, int_cfg);
  1083. smsc9420_reg_write(pd, INT_CTL, 0);
  1084. spin_unlock_irqrestore(&pd->int_lock, flags);
  1085. smsc9420_reg_write(pd, DMAC_INTR_ENA, 0);
  1086. smsc9420_reg_write(pd, INT_STAT, 0xFFFFFFFF);
  1087. smsc9420_pci_flush_write(pd);
  1088. if (request_irq(dev->irq, smsc9420_isr, IRQF_SHARED | IRQF_DISABLED,
  1089. DRV_NAME, pd)) {
  1090. smsc_warn(IFUP, "Unable to use IRQ = %d", dev->irq);
  1091. result = -ENODEV;
  1092. goto out_0;
  1093. }
  1094. smsc9420_dmac_soft_reset(pd);
  1095. /* make sure MAC_CR is sane */
  1096. smsc9420_reg_write(pd, MAC_CR, 0);
  1097. smsc9420_set_mac_address(dev);
  1098. /* Configure GPIO pins to drive LEDs */
  1099. smsc9420_reg_write(pd, GPIO_CFG,
  1100. (GPIO_CFG_LED_3_ | GPIO_CFG_LED_2_ | GPIO_CFG_LED_1_));
  1101. bus_mode = BUS_MODE_DMA_BURST_LENGTH_16;
  1102. #ifdef __BIG_ENDIAN
  1103. bus_mode |= BUS_MODE_DBO_;
  1104. #endif
  1105. smsc9420_reg_write(pd, BUS_MODE, bus_mode);
  1106. smsc9420_pci_flush_write(pd);
  1107. /* set bus master bridge arbitration priority for Rx and TX DMA */
  1108. smsc9420_reg_write(pd, BUS_CFG, BUS_CFG_RXTXWEIGHT_4_1);
  1109. smsc9420_reg_write(pd, DMAC_CONTROL,
  1110. (DMAC_CONTROL_SF_ | DMAC_CONTROL_OSF_));
  1111. smsc9420_pci_flush_write(pd);
  1112. /* test the IRQ connection to the ISR */
  1113. smsc_dbg(IFUP, "Testing ISR using IRQ %d", dev->irq);
  1114. pd->software_irq_signal = false;
  1115. spin_lock_irqsave(&pd->int_lock, flags);
  1116. /* configure interrupt deassertion timer and enable interrupts */
  1117. int_cfg = smsc9420_reg_read(pd, INT_CFG) | INT_CFG_IRQ_EN_;
  1118. int_cfg &= ~(INT_CFG_INT_DEAS_MASK);
  1119. int_cfg |= (INT_DEAS_TIME & INT_CFG_INT_DEAS_MASK);
  1120. smsc9420_reg_write(pd, INT_CFG, int_cfg);
  1121. /* unmask software interrupt */
  1122. int_ctl = smsc9420_reg_read(pd, INT_CTL) | INT_CTL_SW_INT_EN_;
  1123. smsc9420_reg_write(pd, INT_CTL, int_ctl);
  1124. spin_unlock_irqrestore(&pd->int_lock, flags);
  1125. smsc9420_pci_flush_write(pd);
  1126. timeout = 1000;
  1127. while (timeout--) {
  1128. if (pd->software_irq_signal)
  1129. break;
  1130. msleep(1);
  1131. }
  1132. /* disable interrupts */
  1133. spin_lock_irqsave(&pd->int_lock, flags);
  1134. int_cfg = smsc9420_reg_read(pd, INT_CFG) & (~INT_CFG_IRQ_EN_);
  1135. smsc9420_reg_write(pd, INT_CFG, int_cfg);
  1136. spin_unlock_irqrestore(&pd->int_lock, flags);
  1137. if (!pd->software_irq_signal) {
  1138. smsc_warn(IFUP, "ISR failed signaling test");
  1139. result = -ENODEV;
  1140. goto out_free_irq_1;
  1141. }
  1142. smsc_dbg(IFUP, "ISR passed test using IRQ %d", dev->irq);
  1143. result = smsc9420_alloc_tx_ring(pd);
  1144. if (result) {
  1145. smsc_warn(IFUP, "Failed to Initialize tx dma ring");
  1146. result = -ENOMEM;
  1147. goto out_free_irq_1;
  1148. }
  1149. result = smsc9420_alloc_rx_ring(pd);
  1150. if (result) {
  1151. smsc_warn(IFUP, "Failed to Initialize rx dma ring");
  1152. result = -ENOMEM;
  1153. goto out_free_tx_ring_2;
  1154. }
  1155. result = smsc9420_mii_init(dev);
  1156. if (result) {
  1157. smsc_warn(IFUP, "Failed to initialize Phy");
  1158. result = -ENODEV;
  1159. goto out_free_rx_ring_3;
  1160. }
  1161. /* Bring the PHY up */
  1162. phy_start(pd->phy_dev);
  1163. napi_enable(&pd->napi);
  1164. /* start tx and rx */
  1165. mac_cr = smsc9420_reg_read(pd, MAC_CR) | MAC_CR_TXEN_ | MAC_CR_RXEN_;
  1166. smsc9420_reg_write(pd, MAC_CR, mac_cr);
  1167. dmac_control = smsc9420_reg_read(pd, DMAC_CONTROL);
  1168. dmac_control |= DMAC_CONTROL_ST_ | DMAC_CONTROL_SR_;
  1169. smsc9420_reg_write(pd, DMAC_CONTROL, dmac_control);
  1170. smsc9420_pci_flush_write(pd);
  1171. dma_intr_ena = smsc9420_reg_read(pd, DMAC_INTR_ENA);
  1172. dma_intr_ena |=
  1173. (DMAC_INTR_ENA_TX_ | DMAC_INTR_ENA_RX_ | DMAC_INTR_ENA_NIS_);
  1174. smsc9420_reg_write(pd, DMAC_INTR_ENA, dma_intr_ena);
  1175. smsc9420_pci_flush_write(pd);
  1176. netif_wake_queue(dev);
  1177. smsc9420_reg_write(pd, RX_POLL_DEMAND, 1);
  1178. /* enable interrupts */
  1179. spin_lock_irqsave(&pd->int_lock, flags);
  1180. int_cfg = smsc9420_reg_read(pd, INT_CFG) | INT_CFG_IRQ_EN_;
  1181. smsc9420_reg_write(pd, INT_CFG, int_cfg);
  1182. spin_unlock_irqrestore(&pd->int_lock, flags);
  1183. return 0;
  1184. out_free_rx_ring_3:
  1185. smsc9420_free_rx_ring(pd);
  1186. out_free_tx_ring_2:
  1187. smsc9420_free_tx_ring(pd);
  1188. out_free_irq_1:
  1189. free_irq(dev->irq, pd);
  1190. out_0:
  1191. return result;
  1192. }
  1193. #ifdef CONFIG_PM
  1194. static int smsc9420_suspend(struct pci_dev *pdev, pm_message_t state)
  1195. {
  1196. struct net_device *dev = pci_get_drvdata(pdev);
  1197. struct smsc9420_pdata *pd = netdev_priv(dev);
  1198. u32 int_cfg;
  1199. ulong flags;
  1200. /* disable interrupts */
  1201. spin_lock_irqsave(&pd->int_lock, flags);
  1202. int_cfg = smsc9420_reg_read(pd, INT_CFG) & (~INT_CFG_IRQ_EN_);
  1203. smsc9420_reg_write(pd, INT_CFG, int_cfg);
  1204. spin_unlock_irqrestore(&pd->int_lock, flags);
  1205. if (netif_running(dev)) {
  1206. netif_tx_disable(dev);
  1207. smsc9420_stop_tx(pd);
  1208. smsc9420_free_tx_ring(pd);
  1209. napi_disable(&pd->napi);
  1210. smsc9420_stop_rx(pd);
  1211. smsc9420_free_rx_ring(pd);
  1212. free_irq(dev->irq, pd);
  1213. netif_device_detach(dev);
  1214. }
  1215. pci_save_state(pdev);
  1216. pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
  1217. pci_disable_device(pdev);
  1218. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1219. return 0;
  1220. }
  1221. static int smsc9420_resume(struct pci_dev *pdev)
  1222. {
  1223. struct net_device *dev = pci_get_drvdata(pdev);
  1224. struct smsc9420_pdata *pd = netdev_priv(dev);
  1225. int err;
  1226. pci_set_power_state(pdev, PCI_D0);
  1227. pci_restore_state(pdev);
  1228. err = pci_enable_device(pdev);
  1229. if (err)
  1230. return err;
  1231. pci_set_master(pdev);
  1232. err = pci_enable_wake(pdev, 0, 0);
  1233. if (err)
  1234. smsc_warn(IFUP, "pci_enable_wake failed: %d", err);
  1235. if (netif_running(dev)) {
  1236. err = smsc9420_open(dev);
  1237. netif_device_attach(dev);
  1238. }
  1239. return err;
  1240. }
  1241. #endif /* CONFIG_PM */
  1242. static const struct net_device_ops smsc9420_netdev_ops = {
  1243. .ndo_open = smsc9420_open,
  1244. .ndo_stop = smsc9420_stop,
  1245. .ndo_start_xmit = smsc9420_hard_start_xmit,
  1246. .ndo_get_stats = smsc9420_get_stats,
  1247. .ndo_set_multicast_list = smsc9420_set_multicast_list,
  1248. .ndo_do_ioctl = smsc9420_do_ioctl,
  1249. .ndo_validate_addr = eth_validate_addr,
  1250. .ndo_set_mac_address = eth_mac_addr,
  1251. #ifdef CONFIG_NET_POLL_CONTROLLER
  1252. .ndo_poll_controller = smsc9420_poll_controller,
  1253. #endif /* CONFIG_NET_POLL_CONTROLLER */
  1254. };
  1255. static int __devinit
  1256. smsc9420_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1257. {
  1258. struct net_device *dev;
  1259. struct smsc9420_pdata *pd;
  1260. void __iomem *virt_addr;
  1261. int result = 0;
  1262. u32 id_rev;
  1263. printk(KERN_INFO DRV_DESCRIPTION " version " DRV_VERSION "\n");
  1264. /* First do the PCI initialisation */
  1265. result = pci_enable_device(pdev);
  1266. if (unlikely(result)) {
  1267. printk(KERN_ERR "Cannot enable smsc9420\n");
  1268. goto out_0;
  1269. }
  1270. pci_set_master(pdev);
  1271. dev = alloc_etherdev(sizeof(*pd));
  1272. if (!dev) {
  1273. printk(KERN_ERR "ether device alloc failed\n");
  1274. goto out_disable_pci_device_1;
  1275. }
  1276. SET_NETDEV_DEV(dev, &pdev->dev);
  1277. if (!(pci_resource_flags(pdev, SMSC_BAR) & IORESOURCE_MEM)) {
  1278. printk(KERN_ERR "Cannot find PCI device base address\n");
  1279. goto out_free_netdev_2;
  1280. }
  1281. if ((pci_request_regions(pdev, DRV_NAME))) {
  1282. printk(KERN_ERR "Cannot obtain PCI resources, aborting.\n");
  1283. goto out_free_netdev_2;
  1284. }
  1285. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1286. printk(KERN_ERR "No usable DMA configuration, aborting.\n");
  1287. goto out_free_regions_3;
  1288. }
  1289. virt_addr = ioremap(pci_resource_start(pdev, SMSC_BAR),
  1290. pci_resource_len(pdev, SMSC_BAR));
  1291. if (!virt_addr) {
  1292. printk(KERN_ERR "Cannot map device registers, aborting.\n");
  1293. goto out_free_regions_3;
  1294. }
  1295. /* registers are double mapped with 0 offset for LE and 0x200 for BE */
  1296. virt_addr += LAN9420_CPSR_ENDIAN_OFFSET;
  1297. dev->base_addr = (ulong)virt_addr;
  1298. pd = netdev_priv(dev);
  1299. /* pci descriptors are created in the PCI consistent area */
  1300. pd->rx_ring = pci_alloc_consistent(pdev,
  1301. sizeof(struct smsc9420_dma_desc) * RX_RING_SIZE +
  1302. sizeof(struct smsc9420_dma_desc) * TX_RING_SIZE,
  1303. &pd->rx_dma_addr);
  1304. if (!pd->rx_ring)
  1305. goto out_free_io_4;
  1306. /* descriptors are aligned due to the nature of pci_alloc_consistent */
  1307. pd->tx_ring = (struct smsc9420_dma_desc *)
  1308. (pd->rx_ring + RX_RING_SIZE);
  1309. pd->tx_dma_addr = pd->rx_dma_addr +
  1310. sizeof(struct smsc9420_dma_desc) * RX_RING_SIZE;
  1311. pd->pdev = pdev;
  1312. pd->dev = dev;
  1313. pd->base_addr = virt_addr;
  1314. pd->msg_enable = smsc_debug;
  1315. pd->rx_csum = true;
  1316. smsc_dbg(PROBE, "lan_base=0x%08lx", (ulong)virt_addr);
  1317. id_rev = smsc9420_reg_read(pd, ID_REV);
  1318. switch (id_rev & 0xFFFF0000) {
  1319. case 0x94200000:
  1320. smsc_info(PROBE, "LAN9420 identified, ID_REV=0x%08X", id_rev);
  1321. break;
  1322. default:
  1323. smsc_warn(PROBE, "LAN9420 NOT identified");
  1324. smsc_warn(PROBE, "ID_REV=0x%08X", id_rev);
  1325. goto out_free_dmadesc_5;
  1326. }
  1327. smsc9420_dmac_soft_reset(pd);
  1328. smsc9420_eeprom_reload(pd);
  1329. smsc9420_check_mac_address(dev);
  1330. dev->netdev_ops = &smsc9420_netdev_ops;
  1331. dev->ethtool_ops = &smsc9420_ethtool_ops;
  1332. dev->irq = pdev->irq;
  1333. netif_napi_add(dev, &pd->napi, smsc9420_rx_poll, NAPI_WEIGHT);
  1334. result = register_netdev(dev);
  1335. if (result) {
  1336. smsc_warn(PROBE, "error %i registering device", result);
  1337. goto out_free_dmadesc_5;
  1338. }
  1339. pci_set_drvdata(pdev, dev);
  1340. spin_lock_init(&pd->int_lock);
  1341. spin_lock_init(&pd->phy_lock);
  1342. dev_info(&dev->dev, "MAC Address: %pM\n", dev->dev_addr);
  1343. return 0;
  1344. out_free_dmadesc_5:
  1345. pci_free_consistent(pdev, sizeof(struct smsc9420_dma_desc) *
  1346. (RX_RING_SIZE + TX_RING_SIZE), pd->rx_ring, pd->rx_dma_addr);
  1347. out_free_io_4:
  1348. iounmap(virt_addr - LAN9420_CPSR_ENDIAN_OFFSET);
  1349. out_free_regions_3:
  1350. pci_release_regions(pdev);
  1351. out_free_netdev_2:
  1352. free_netdev(dev);
  1353. out_disable_pci_device_1:
  1354. pci_disable_device(pdev);
  1355. out_0:
  1356. return -ENODEV;
  1357. }
  1358. static void __devexit smsc9420_remove(struct pci_dev *pdev)
  1359. {
  1360. struct net_device *dev;
  1361. struct smsc9420_pdata *pd;
  1362. dev = pci_get_drvdata(pdev);
  1363. if (!dev)
  1364. return;
  1365. pci_set_drvdata(pdev, NULL);
  1366. pd = netdev_priv(dev);
  1367. unregister_netdev(dev);
  1368. /* tx_buffers and rx_buffers are freed in stop */
  1369. BUG_ON(pd->tx_buffers);
  1370. BUG_ON(pd->rx_buffers);
  1371. BUG_ON(!pd->tx_ring);
  1372. BUG_ON(!pd->rx_ring);
  1373. pci_free_consistent(pdev, sizeof(struct smsc9420_dma_desc) *
  1374. (RX_RING_SIZE + TX_RING_SIZE), pd->rx_ring, pd->rx_dma_addr);
  1375. iounmap(pd->base_addr - LAN9420_CPSR_ENDIAN_OFFSET);
  1376. pci_release_regions(pdev);
  1377. free_netdev(dev);
  1378. pci_disable_device(pdev);
  1379. }
  1380. static struct pci_driver smsc9420_driver = {
  1381. .name = DRV_NAME,
  1382. .id_table = smsc9420_id_table,
  1383. .probe = smsc9420_probe,
  1384. .remove = __devexit_p(smsc9420_remove),
  1385. #ifdef CONFIG_PM
  1386. .suspend = smsc9420_suspend,
  1387. .resume = smsc9420_resume,
  1388. #endif /* CONFIG_PM */
  1389. };
  1390. static int __init smsc9420_init_module(void)
  1391. {
  1392. smsc_debug = netif_msg_init(debug, SMSC_MSG_DEFAULT);
  1393. return pci_register_driver(&smsc9420_driver);
  1394. }
  1395. static void __exit smsc9420_exit_module(void)
  1396. {
  1397. pci_unregister_driver(&smsc9420_driver);
  1398. }
  1399. module_init(smsc9420_init_module);
  1400. module_exit(smsc9420_exit_module);