ioc3-eth.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Driver for SGI's IOC3 based Ethernet cards as found in the PCI card.
  7. *
  8. * Copyright (C) 1999, 2000, 2001, 2003 Ralf Baechle
  9. * Copyright (C) 1995, 1999, 2000, 2001 by Silicon Graphics, Inc.
  10. *
  11. * References:
  12. * o IOC3 ASIC specification 4.51, 1996-04-18
  13. * o IEEE 802.3 specification, 2000 edition
  14. * o DP38840A Specification, National Semiconductor, March 1997
  15. *
  16. * To do:
  17. *
  18. * o Handle allocation failures in ioc3_alloc_skb() more gracefully.
  19. * o Handle allocation failures in ioc3_init_rings().
  20. * o Use prefetching for large packets. What is a good lower limit for
  21. * prefetching?
  22. * o We're probably allocating a bit too much memory.
  23. * o Use hardware checksums.
  24. * o Convert to using a IOC3 meta driver.
  25. * o Which PHYs might possibly be attached to the IOC3 in real live,
  26. * which workarounds are required for them? Do we ever have Lucent's?
  27. * o For the 2.5 branch kill the mii-tool ioctls.
  28. */
  29. #define IOC3_NAME "ioc3-eth"
  30. #define IOC3_VERSION "2.6.3-3"
  31. #include <linux/config.h>
  32. #include <linux/init.h>
  33. #include <linux/delay.h>
  34. #include <linux/kernel.h>
  35. #include <linux/mm.h>
  36. #include <linux/errno.h>
  37. #include <linux/module.h>
  38. #include <linux/pci.h>
  39. #include <linux/crc32.h>
  40. #include <linux/mii.h>
  41. #include <linux/in.h>
  42. #include <linux/ip.h>
  43. #include <linux/tcp.h>
  44. #include <linux/udp.h>
  45. #ifdef CONFIG_SERIAL_8250
  46. #include <linux/serial.h>
  47. #include <asm/serial.h>
  48. #define IOC3_BAUD (22000000 / (3*16))
  49. #define IOC3_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
  50. #endif
  51. #include <linux/netdevice.h>
  52. #include <linux/etherdevice.h>
  53. #include <linux/ethtool.h>
  54. #include <linux/skbuff.h>
  55. #include <net/ip.h>
  56. #include <asm/byteorder.h>
  57. #include <asm/checksum.h>
  58. #include <asm/io.h>
  59. #include <asm/pgtable.h>
  60. #include <asm/uaccess.h>
  61. #include <asm/sn/types.h>
  62. #include <asm/sn/sn0/addrs.h>
  63. #include <asm/sn/sn0/hubni.h>
  64. #include <asm/sn/sn0/hubio.h>
  65. #include <asm/sn/klconfig.h>
  66. #include <asm/sn/ioc3.h>
  67. #include <asm/sn/sn0/ip27.h>
  68. #include <asm/pci/bridge.h>
  69. /*
  70. * 64 RX buffers. This is tunable in the range of 16 <= x < 512. The
  71. * value must be a power of two.
  72. */
  73. #define RX_BUFFS 64
  74. #define ETCSR_FD ((17<<ETCSR_IPGR2_SHIFT) | (11<<ETCSR_IPGR1_SHIFT) | 21)
  75. #define ETCSR_HD ((21<<ETCSR_IPGR2_SHIFT) | (21<<ETCSR_IPGR1_SHIFT) | 21)
  76. /* Private per NIC data of the driver. */
  77. struct ioc3_private {
  78. struct ioc3 *regs;
  79. unsigned long *rxr; /* pointer to receiver ring */
  80. struct ioc3_etxd *txr;
  81. struct sk_buff *rx_skbs[512];
  82. struct sk_buff *tx_skbs[128];
  83. struct net_device_stats stats;
  84. int rx_ci; /* RX consumer index */
  85. int rx_pi; /* RX producer index */
  86. int tx_ci; /* TX consumer index */
  87. int tx_pi; /* TX producer index */
  88. int txqlen;
  89. u32 emcr, ehar_h, ehar_l;
  90. spinlock_t ioc3_lock;
  91. struct mii_if_info mii;
  92. struct pci_dev *pdev;
  93. /* Members used by autonegotiation */
  94. struct timer_list ioc3_timer;
  95. };
  96. static inline struct net_device *priv_netdev(struct ioc3_private *dev)
  97. {
  98. return (void *)dev - ((sizeof(struct net_device) + 31) & ~31);
  99. }
  100. static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  101. static void ioc3_set_multicast_list(struct net_device *dev);
  102. static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev);
  103. static void ioc3_timeout(struct net_device *dev);
  104. static inline unsigned int ioc3_hash(const unsigned char *addr);
  105. static inline void ioc3_stop(struct ioc3_private *ip);
  106. static void ioc3_init(struct net_device *dev);
  107. static const char ioc3_str[] = "IOC3 Ethernet";
  108. static struct ethtool_ops ioc3_ethtool_ops;
  109. /* We use this to acquire receive skb's that we can DMA directly into. */
  110. #define IOC3_CACHELINE 128UL
  111. static inline unsigned long aligned_rx_skb_addr(unsigned long addr)
  112. {
  113. return (~addr + 1) & (IOC3_CACHELINE - 1UL);
  114. }
  115. static inline struct sk_buff * ioc3_alloc_skb(unsigned long length,
  116. unsigned int gfp_mask)
  117. {
  118. struct sk_buff *skb;
  119. skb = alloc_skb(length + IOC3_CACHELINE - 1, gfp_mask);
  120. if (likely(skb)) {
  121. int offset = aligned_rx_skb_addr((unsigned long) skb->data);
  122. if (offset)
  123. skb_reserve(skb, offset);
  124. }
  125. return skb;
  126. }
  127. static inline unsigned long ioc3_map(void *ptr, unsigned long vdev)
  128. {
  129. #ifdef CONFIG_SGI_IP27
  130. vdev <<= 58; /* Shift to PCI64_ATTR_VIRTUAL */
  131. return vdev | (0xaUL << PCI64_ATTR_TARG_SHFT) | PCI64_ATTR_PREF |
  132. ((unsigned long)ptr & TO_PHYS_MASK);
  133. #else
  134. return virt_to_bus(ptr);
  135. #endif
  136. }
  137. /* BEWARE: The IOC3 documentation documents the size of rx buffers as
  138. 1644 while it's actually 1664. This one was nasty to track down ... */
  139. #define RX_OFFSET 10
  140. #define RX_BUF_ALLOC_SIZE (1664 + RX_OFFSET + IOC3_CACHELINE)
  141. /* DMA barrier to separate cached and uncached accesses. */
  142. #define BARRIER() \
  143. __asm__("sync" ::: "memory")
  144. #define IOC3_SIZE 0x100000
  145. /*
  146. * IOC3 is a big endian device
  147. *
  148. * Unorthodox but makes the users of these macros more readable - the pointer
  149. * to the IOC3's memory mapped registers is expected as struct ioc3 * ioc3
  150. * in the environment.
  151. */
  152. #define ioc3_r_mcr() be32_to_cpu(ioc3->mcr)
  153. #define ioc3_w_mcr(v) do { ioc3->mcr = cpu_to_be32(v); } while (0)
  154. #define ioc3_w_gpcr_s(v) do { ioc3->gpcr_s = cpu_to_be32(v); } while (0)
  155. #define ioc3_r_emcr() be32_to_cpu(ioc3->emcr)
  156. #define ioc3_w_emcr(v) do { ioc3->emcr = cpu_to_be32(v); } while (0)
  157. #define ioc3_r_eisr() be32_to_cpu(ioc3->eisr)
  158. #define ioc3_w_eisr(v) do { ioc3->eisr = cpu_to_be32(v); } while (0)
  159. #define ioc3_r_eier() be32_to_cpu(ioc3->eier)
  160. #define ioc3_w_eier(v) do { ioc3->eier = cpu_to_be32(v); } while (0)
  161. #define ioc3_r_ercsr() be32_to_cpu(ioc3->ercsr)
  162. #define ioc3_w_ercsr(v) do { ioc3->ercsr = cpu_to_be32(v); } while (0)
  163. #define ioc3_r_erbr_h() be32_to_cpu(ioc3->erbr_h)
  164. #define ioc3_w_erbr_h(v) do { ioc3->erbr_h = cpu_to_be32(v); } while (0)
  165. #define ioc3_r_erbr_l() be32_to_cpu(ioc3->erbr_l)
  166. #define ioc3_w_erbr_l(v) do { ioc3->erbr_l = cpu_to_be32(v); } while (0)
  167. #define ioc3_r_erbar() be32_to_cpu(ioc3->erbar)
  168. #define ioc3_w_erbar(v) do { ioc3->erbar = cpu_to_be32(v); } while (0)
  169. #define ioc3_r_ercir() be32_to_cpu(ioc3->ercir)
  170. #define ioc3_w_ercir(v) do { ioc3->ercir = cpu_to_be32(v); } while (0)
  171. #define ioc3_r_erpir() be32_to_cpu(ioc3->erpir)
  172. #define ioc3_w_erpir(v) do { ioc3->erpir = cpu_to_be32(v); } while (0)
  173. #define ioc3_r_ertr() be32_to_cpu(ioc3->ertr)
  174. #define ioc3_w_ertr(v) do { ioc3->ertr = cpu_to_be32(v); } while (0)
  175. #define ioc3_r_etcsr() be32_to_cpu(ioc3->etcsr)
  176. #define ioc3_w_etcsr(v) do { ioc3->etcsr = cpu_to_be32(v); } while (0)
  177. #define ioc3_r_ersr() be32_to_cpu(ioc3->ersr)
  178. #define ioc3_w_ersr(v) do { ioc3->ersr = cpu_to_be32(v); } while (0)
  179. #define ioc3_r_etcdc() be32_to_cpu(ioc3->etcdc)
  180. #define ioc3_w_etcdc(v) do { ioc3->etcdc = cpu_to_be32(v); } while (0)
  181. #define ioc3_r_ebir() be32_to_cpu(ioc3->ebir)
  182. #define ioc3_w_ebir(v) do { ioc3->ebir = cpu_to_be32(v); } while (0)
  183. #define ioc3_r_etbr_h() be32_to_cpu(ioc3->etbr_h)
  184. #define ioc3_w_etbr_h(v) do { ioc3->etbr_h = cpu_to_be32(v); } while (0)
  185. #define ioc3_r_etbr_l() be32_to_cpu(ioc3->etbr_l)
  186. #define ioc3_w_etbr_l(v) do { ioc3->etbr_l = cpu_to_be32(v); } while (0)
  187. #define ioc3_r_etcir() be32_to_cpu(ioc3->etcir)
  188. #define ioc3_w_etcir(v) do { ioc3->etcir = cpu_to_be32(v); } while (0)
  189. #define ioc3_r_etpir() be32_to_cpu(ioc3->etpir)
  190. #define ioc3_w_etpir(v) do { ioc3->etpir = cpu_to_be32(v); } while (0)
  191. #define ioc3_r_emar_h() be32_to_cpu(ioc3->emar_h)
  192. #define ioc3_w_emar_h(v) do { ioc3->emar_h = cpu_to_be32(v); } while (0)
  193. #define ioc3_r_emar_l() be32_to_cpu(ioc3->emar_l)
  194. #define ioc3_w_emar_l(v) do { ioc3->emar_l = cpu_to_be32(v); } while (0)
  195. #define ioc3_r_ehar_h() be32_to_cpu(ioc3->ehar_h)
  196. #define ioc3_w_ehar_h(v) do { ioc3->ehar_h = cpu_to_be32(v); } while (0)
  197. #define ioc3_r_ehar_l() be32_to_cpu(ioc3->ehar_l)
  198. #define ioc3_w_ehar_l(v) do { ioc3->ehar_l = cpu_to_be32(v); } while (0)
  199. #define ioc3_r_micr() be32_to_cpu(ioc3->micr)
  200. #define ioc3_w_micr(v) do { ioc3->micr = cpu_to_be32(v); } while (0)
  201. #define ioc3_r_midr_r() be32_to_cpu(ioc3->midr_r)
  202. #define ioc3_w_midr_r(v) do { ioc3->midr_r = cpu_to_be32(v); } while (0)
  203. #define ioc3_r_midr_w() be32_to_cpu(ioc3->midr_w)
  204. #define ioc3_w_midr_w(v) do { ioc3->midr_w = cpu_to_be32(v); } while (0)
  205. static inline u32 mcr_pack(u32 pulse, u32 sample)
  206. {
  207. return (pulse << 10) | (sample << 2);
  208. }
  209. static int nic_wait(struct ioc3 *ioc3)
  210. {
  211. u32 mcr;
  212. do {
  213. mcr = ioc3_r_mcr();
  214. } while (!(mcr & 2));
  215. return mcr & 1;
  216. }
  217. static int nic_reset(struct ioc3 *ioc3)
  218. {
  219. int presence;
  220. ioc3_w_mcr(mcr_pack(500, 65));
  221. presence = nic_wait(ioc3);
  222. ioc3_w_mcr(mcr_pack(0, 500));
  223. nic_wait(ioc3);
  224. return presence;
  225. }
  226. static inline int nic_read_bit(struct ioc3 *ioc3)
  227. {
  228. int result;
  229. ioc3_w_mcr(mcr_pack(6, 13));
  230. result = nic_wait(ioc3);
  231. ioc3_w_mcr(mcr_pack(0, 100));
  232. nic_wait(ioc3);
  233. return result;
  234. }
  235. static inline void nic_write_bit(struct ioc3 *ioc3, int bit)
  236. {
  237. if (bit)
  238. ioc3_w_mcr(mcr_pack(6, 110));
  239. else
  240. ioc3_w_mcr(mcr_pack(80, 30));
  241. nic_wait(ioc3);
  242. }
  243. /*
  244. * Read a byte from an iButton device
  245. */
  246. static u32 nic_read_byte(struct ioc3 *ioc3)
  247. {
  248. u32 result = 0;
  249. int i;
  250. for (i = 0; i < 8; i++)
  251. result = (result >> 1) | (nic_read_bit(ioc3) << 7);
  252. return result;
  253. }
  254. /*
  255. * Write a byte to an iButton device
  256. */
  257. static void nic_write_byte(struct ioc3 *ioc3, int byte)
  258. {
  259. int i, bit;
  260. for (i = 8; i; i--) {
  261. bit = byte & 1;
  262. byte >>= 1;
  263. nic_write_bit(ioc3, bit);
  264. }
  265. }
  266. static u64 nic_find(struct ioc3 *ioc3, int *last)
  267. {
  268. int a, b, index, disc;
  269. u64 address = 0;
  270. nic_reset(ioc3);
  271. /* Search ROM. */
  272. nic_write_byte(ioc3, 0xf0);
  273. /* Algorithm from ``Book of iButton Standards''. */
  274. for (index = 0, disc = 0; index < 64; index++) {
  275. a = nic_read_bit(ioc3);
  276. b = nic_read_bit(ioc3);
  277. if (a && b) {
  278. printk("NIC search failed (not fatal).\n");
  279. *last = 0;
  280. return 0;
  281. }
  282. if (!a && !b) {
  283. if (index == *last) {
  284. address |= 1UL << index;
  285. } else if (index > *last) {
  286. address &= ~(1UL << index);
  287. disc = index;
  288. } else if ((address & (1UL << index)) == 0)
  289. disc = index;
  290. nic_write_bit(ioc3, address & (1UL << index));
  291. continue;
  292. } else {
  293. if (a)
  294. address |= 1UL << index;
  295. else
  296. address &= ~(1UL << index);
  297. nic_write_bit(ioc3, a);
  298. continue;
  299. }
  300. }
  301. *last = disc;
  302. return address;
  303. }
  304. static int nic_init(struct ioc3 *ioc3)
  305. {
  306. const char *type;
  307. u8 crc;
  308. u8 serial[6];
  309. int save = 0, i;
  310. type = "unknown";
  311. while (1) {
  312. u64 reg;
  313. reg = nic_find(ioc3, &save);
  314. switch (reg & 0xff) {
  315. case 0x91:
  316. type = "DS1981U";
  317. break;
  318. default:
  319. if (save == 0) {
  320. /* Let the caller try again. */
  321. return -1;
  322. }
  323. continue;
  324. }
  325. nic_reset(ioc3);
  326. /* Match ROM. */
  327. nic_write_byte(ioc3, 0x55);
  328. for (i = 0; i < 8; i++)
  329. nic_write_byte(ioc3, (reg >> (i << 3)) & 0xff);
  330. reg >>= 8; /* Shift out type. */
  331. for (i = 0; i < 6; i++) {
  332. serial[i] = reg & 0xff;
  333. reg >>= 8;
  334. }
  335. crc = reg & 0xff;
  336. break;
  337. }
  338. printk("Found %s NIC", type);
  339. if (type != "unknown") {
  340. printk (" registration number %02x:%02x:%02x:%02x:%02x:%02x,"
  341. " CRC %02x", serial[0], serial[1], serial[2],
  342. serial[3], serial[4], serial[5], crc);
  343. }
  344. printk(".\n");
  345. return 0;
  346. }
  347. /*
  348. * Read the NIC (Number-In-a-Can) device used to store the MAC address on
  349. * SN0 / SN00 nodeboards and PCI cards.
  350. */
  351. static void ioc3_get_eaddr_nic(struct ioc3_private *ip)
  352. {
  353. struct ioc3 *ioc3 = ip->regs;
  354. u8 nic[14];
  355. int tries = 2; /* There may be some problem with the battery? */
  356. int i;
  357. ioc3_w_gpcr_s(1 << 21);
  358. while (tries--) {
  359. if (!nic_init(ioc3))
  360. break;
  361. udelay(500);
  362. }
  363. if (tries < 0) {
  364. printk("Failed to read MAC address\n");
  365. return;
  366. }
  367. /* Read Memory. */
  368. nic_write_byte(ioc3, 0xf0);
  369. nic_write_byte(ioc3, 0x00);
  370. nic_write_byte(ioc3, 0x00);
  371. for (i = 13; i >= 0; i--)
  372. nic[i] = nic_read_byte(ioc3);
  373. for (i = 2; i < 8; i++)
  374. priv_netdev(ip)->dev_addr[i - 2] = nic[i];
  375. }
  376. /*
  377. * Ok, this is hosed by design. It's necessary to know what machine the
  378. * NIC is in in order to know how to read the NIC address. We also have
  379. * to know if it's a PCI card or a NIC in on the node board ...
  380. */
  381. static void ioc3_get_eaddr(struct ioc3_private *ip)
  382. {
  383. int i;
  384. ioc3_get_eaddr_nic(ip);
  385. printk("Ethernet address is ");
  386. for (i = 0; i < 6; i++) {
  387. printk("%02x", priv_netdev(ip)->dev_addr[i]);
  388. if (i < 5)
  389. printk(":");
  390. }
  391. printk(".\n");
  392. }
  393. static void __ioc3_set_mac_address(struct net_device *dev)
  394. {
  395. struct ioc3_private *ip = netdev_priv(dev);
  396. struct ioc3 *ioc3 = ip->regs;
  397. ioc3_w_emar_h((dev->dev_addr[5] << 8) | dev->dev_addr[4]);
  398. ioc3_w_emar_l((dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16) |
  399. (dev->dev_addr[1] << 8) | dev->dev_addr[0]);
  400. }
  401. static int ioc3_set_mac_address(struct net_device *dev, void *addr)
  402. {
  403. struct ioc3_private *ip = netdev_priv(dev);
  404. struct sockaddr *sa = addr;
  405. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  406. spin_lock_irq(&ip->ioc3_lock);
  407. __ioc3_set_mac_address(dev);
  408. spin_unlock_irq(&ip->ioc3_lock);
  409. return 0;
  410. }
  411. /*
  412. * Caller must hold the ioc3_lock ever for MII readers. This is also
  413. * used to protect the transmitter side but it's low contention.
  414. */
  415. static int ioc3_mdio_read(struct net_device *dev, int phy, int reg)
  416. {
  417. struct ioc3_private *ip = netdev_priv(dev);
  418. struct ioc3 *ioc3 = ip->regs;
  419. while (ioc3_r_micr() & MICR_BUSY);
  420. ioc3_w_micr((phy << MICR_PHYADDR_SHIFT) | reg | MICR_READTRIG);
  421. while (ioc3_r_micr() & MICR_BUSY);
  422. return ioc3_r_midr_r() & MIDR_DATA_MASK;
  423. }
  424. static void ioc3_mdio_write(struct net_device *dev, int phy, int reg, int data)
  425. {
  426. struct ioc3_private *ip = netdev_priv(dev);
  427. struct ioc3 *ioc3 = ip->regs;
  428. while (ioc3_r_micr() & MICR_BUSY);
  429. ioc3_w_midr_w(data);
  430. ioc3_w_micr((phy << MICR_PHYADDR_SHIFT) | reg);
  431. while (ioc3_r_micr() & MICR_BUSY);
  432. }
  433. static int ioc3_mii_init(struct ioc3_private *ip);
  434. static struct net_device_stats *ioc3_get_stats(struct net_device *dev)
  435. {
  436. struct ioc3_private *ip = netdev_priv(dev);
  437. struct ioc3 *ioc3 = ip->regs;
  438. ip->stats.collisions += (ioc3_r_etcdc() & ETCDC_COLLCNT_MASK);
  439. return &ip->stats;
  440. }
  441. #ifdef CONFIG_SGI_IOC3_ETH_HW_RX_CSUM
  442. static void ioc3_tcpudp_checksum(struct sk_buff *skb, uint32_t hwsum, int len)
  443. {
  444. struct ethhdr *eh = eth_hdr(skb);
  445. uint32_t csum, ehsum;
  446. unsigned int proto;
  447. struct iphdr *ih;
  448. uint16_t *ew;
  449. unsigned char *cp;
  450. /*
  451. * Did hardware handle the checksum at all? The cases we can handle
  452. * are:
  453. *
  454. * - TCP and UDP checksums of IPv4 only.
  455. * - IPv6 would be doable but we keep that for later ...
  456. * - Only unfragmented packets. Did somebody already tell you
  457. * fragmentation is evil?
  458. * - don't care about packet size. Worst case when processing a
  459. * malformed packet we'll try to access the packet at ip header +
  460. * 64 bytes which is still inside the skb. Even in the unlikely
  461. * case where the checksum is right the higher layers will still
  462. * drop the packet as appropriate.
  463. */
  464. if (eh->h_proto != ntohs(ETH_P_IP))
  465. return;
  466. ih = (struct iphdr *) ((char *)eh + ETH_HLEN);
  467. if (ih->frag_off & htons(IP_MF | IP_OFFSET))
  468. return;
  469. proto = ih->protocol;
  470. if (proto != IPPROTO_TCP && proto != IPPROTO_UDP)
  471. return;
  472. /* Same as tx - compute csum of pseudo header */
  473. csum = hwsum +
  474. (ih->tot_len - (ih->ihl << 2)) +
  475. htons((uint16_t)ih->protocol) +
  476. (ih->saddr >> 16) + (ih->saddr & 0xffff) +
  477. (ih->daddr >> 16) + (ih->daddr & 0xffff);
  478. /* Sum up ethernet dest addr, src addr and protocol */
  479. ew = (uint16_t *) eh;
  480. ehsum = ew[0] + ew[1] + ew[2] + ew[3] + ew[4] + ew[5] + ew[6];
  481. ehsum = (ehsum & 0xffff) + (ehsum >> 16);
  482. ehsum = (ehsum & 0xffff) + (ehsum >> 16);
  483. csum += 0xffff ^ ehsum;
  484. /* In the next step we also subtract the 1's complement
  485. checksum of the trailing ethernet CRC. */
  486. cp = (char *)eh + len; /* points at trailing CRC */
  487. if (len & 1) {
  488. csum += 0xffff ^ (uint16_t) ((cp[1] << 8) | cp[0]);
  489. csum += 0xffff ^ (uint16_t) ((cp[3] << 8) | cp[2]);
  490. } else {
  491. csum += 0xffff ^ (uint16_t) ((cp[0] << 8) | cp[1]);
  492. csum += 0xffff ^ (uint16_t) ((cp[2] << 8) | cp[3]);
  493. }
  494. csum = (csum & 0xffff) + (csum >> 16);
  495. csum = (csum & 0xffff) + (csum >> 16);
  496. if (csum == 0xffff)
  497. skb->ip_summed = CHECKSUM_UNNECESSARY;
  498. }
  499. #endif /* CONFIG_SGI_IOC3_ETH_HW_RX_CSUM */
  500. static inline void ioc3_rx(struct ioc3_private *ip)
  501. {
  502. struct sk_buff *skb, *new_skb;
  503. struct ioc3 *ioc3 = ip->regs;
  504. int rx_entry, n_entry, len;
  505. struct ioc3_erxbuf *rxb;
  506. unsigned long *rxr;
  507. u32 w0, err;
  508. rxr = (unsigned long *) ip->rxr; /* Ring base */
  509. rx_entry = ip->rx_ci; /* RX consume index */
  510. n_entry = ip->rx_pi;
  511. skb = ip->rx_skbs[rx_entry];
  512. rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
  513. w0 = be32_to_cpu(rxb->w0);
  514. while (w0 & ERXBUF_V) {
  515. err = be32_to_cpu(rxb->err); /* It's valid ... */
  516. if (err & ERXBUF_GOODPKT) {
  517. len = ((w0 >> ERXBUF_BYTECNT_SHIFT) & 0x7ff) - 4;
  518. skb_trim(skb, len);
  519. skb->protocol = eth_type_trans(skb, priv_netdev(ip));
  520. new_skb = ioc3_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
  521. if (!new_skb) {
  522. /* Ouch, drop packet and just recycle packet
  523. to keep the ring filled. */
  524. ip->stats.rx_dropped++;
  525. new_skb = skb;
  526. goto next;
  527. }
  528. #ifdef CONFIG_SGI_IOC3_ETH_HW_RX_CSUM
  529. ioc3_tcpudp_checksum(skb, w0 & ERXBUF_IPCKSUM_MASK,len);
  530. #endif
  531. netif_rx(skb);
  532. ip->rx_skbs[rx_entry] = NULL; /* Poison */
  533. new_skb->dev = priv_netdev(ip);
  534. /* Because we reserve afterwards. */
  535. skb_put(new_skb, (1664 + RX_OFFSET));
  536. rxb = (struct ioc3_erxbuf *) new_skb->data;
  537. skb_reserve(new_skb, RX_OFFSET);
  538. priv_netdev(ip)->last_rx = jiffies;
  539. ip->stats.rx_packets++; /* Statistics */
  540. ip->stats.rx_bytes += len;
  541. } else {
  542. /* The frame is invalid and the skb never
  543. reached the network layer so we can just
  544. recycle it. */
  545. new_skb = skb;
  546. ip->stats.rx_errors++;
  547. }
  548. if (err & ERXBUF_CRCERR) /* Statistics */
  549. ip->stats.rx_crc_errors++;
  550. if (err & ERXBUF_FRAMERR)
  551. ip->stats.rx_frame_errors++;
  552. next:
  553. ip->rx_skbs[n_entry] = new_skb;
  554. rxr[n_entry] = cpu_to_be64(ioc3_map(rxb, 1));
  555. rxb->w0 = 0; /* Clear valid flag */
  556. n_entry = (n_entry + 1) & 511; /* Update erpir */
  557. /* Now go on to the next ring entry. */
  558. rx_entry = (rx_entry + 1) & 511;
  559. skb = ip->rx_skbs[rx_entry];
  560. rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
  561. w0 = be32_to_cpu(rxb->w0);
  562. }
  563. ioc3_w_erpir((n_entry << 3) | ERPIR_ARM);
  564. ip->rx_pi = n_entry;
  565. ip->rx_ci = rx_entry;
  566. }
  567. static inline void ioc3_tx(struct ioc3_private *ip)
  568. {
  569. unsigned long packets, bytes;
  570. struct ioc3 *ioc3 = ip->regs;
  571. int tx_entry, o_entry;
  572. struct sk_buff *skb;
  573. u32 etcir;
  574. spin_lock(&ip->ioc3_lock);
  575. etcir = ioc3_r_etcir();
  576. tx_entry = (etcir >> 7) & 127;
  577. o_entry = ip->tx_ci;
  578. packets = 0;
  579. bytes = 0;
  580. while (o_entry != tx_entry) {
  581. packets++;
  582. skb = ip->tx_skbs[o_entry];
  583. bytes += skb->len;
  584. dev_kfree_skb_irq(skb);
  585. ip->tx_skbs[o_entry] = NULL;
  586. o_entry = (o_entry + 1) & 127; /* Next */
  587. etcir = ioc3_r_etcir(); /* More pkts sent? */
  588. tx_entry = (etcir >> 7) & 127;
  589. }
  590. ip->stats.tx_packets += packets;
  591. ip->stats.tx_bytes += bytes;
  592. ip->txqlen -= packets;
  593. if (ip->txqlen < 128)
  594. netif_wake_queue(priv_netdev(ip));
  595. ip->tx_ci = o_entry;
  596. spin_unlock(&ip->ioc3_lock);
  597. }
  598. /*
  599. * Deal with fatal IOC3 errors. This condition might be caused by a hard or
  600. * software problems, so we should try to recover
  601. * more gracefully if this ever happens. In theory we might be flooded
  602. * with such error interrupts if something really goes wrong, so we might
  603. * also consider to take the interface down.
  604. */
  605. static void ioc3_error(struct ioc3_private *ip, u32 eisr)
  606. {
  607. struct net_device *dev = priv_netdev(ip);
  608. unsigned char *iface = dev->name;
  609. spin_lock(&ip->ioc3_lock);
  610. if (eisr & EISR_RXOFLO)
  611. printk(KERN_ERR "%s: RX overflow.\n", iface);
  612. if (eisr & EISR_RXBUFOFLO)
  613. printk(KERN_ERR "%s: RX buffer overflow.\n", iface);
  614. if (eisr & EISR_RXMEMERR)
  615. printk(KERN_ERR "%s: RX PCI error.\n", iface);
  616. if (eisr & EISR_RXPARERR)
  617. printk(KERN_ERR "%s: RX SSRAM parity error.\n", iface);
  618. if (eisr & EISR_TXBUFUFLO)
  619. printk(KERN_ERR "%s: TX buffer underflow.\n", iface);
  620. if (eisr & EISR_TXMEMERR)
  621. printk(KERN_ERR "%s: TX PCI error.\n", iface);
  622. ioc3_stop(ip);
  623. ioc3_init(dev);
  624. ioc3_mii_init(ip);
  625. netif_wake_queue(dev);
  626. spin_unlock(&ip->ioc3_lock);
  627. }
  628. /* The interrupt handler does all of the Rx thread work and cleans up
  629. after the Tx thread. */
  630. static irqreturn_t ioc3_interrupt(int irq, void *_dev, struct pt_regs *regs)
  631. {
  632. struct net_device *dev = (struct net_device *)_dev;
  633. struct ioc3_private *ip = netdev_priv(dev);
  634. struct ioc3 *ioc3 = ip->regs;
  635. const u32 enabled = EISR_RXTIMERINT | EISR_RXOFLO | EISR_RXBUFOFLO |
  636. EISR_RXMEMERR | EISR_RXPARERR | EISR_TXBUFUFLO |
  637. EISR_TXEXPLICIT | EISR_TXMEMERR;
  638. u32 eisr;
  639. eisr = ioc3_r_eisr() & enabled;
  640. ioc3_w_eisr(eisr);
  641. (void) ioc3_r_eisr(); /* Flush */
  642. if (eisr & (EISR_RXOFLO | EISR_RXBUFOFLO | EISR_RXMEMERR |
  643. EISR_RXPARERR | EISR_TXBUFUFLO | EISR_TXMEMERR))
  644. ioc3_error(ip, eisr);
  645. if (eisr & EISR_RXTIMERINT)
  646. ioc3_rx(ip);
  647. if (eisr & EISR_TXEXPLICIT)
  648. ioc3_tx(ip);
  649. return IRQ_HANDLED;
  650. }
  651. static inline void ioc3_setup_duplex(struct ioc3_private *ip)
  652. {
  653. struct ioc3 *ioc3 = ip->regs;
  654. if (ip->mii.full_duplex) {
  655. ioc3_w_etcsr(ETCSR_FD);
  656. ip->emcr |= EMCR_DUPLEX;
  657. } else {
  658. ioc3_w_etcsr(ETCSR_HD);
  659. ip->emcr &= ~EMCR_DUPLEX;
  660. }
  661. ioc3_w_emcr(ip->emcr);
  662. }
  663. static void ioc3_timer(unsigned long data)
  664. {
  665. struct ioc3_private *ip = (struct ioc3_private *) data;
  666. /* Print the link status if it has changed */
  667. mii_check_media(&ip->mii, 1, 0);
  668. ioc3_setup_duplex(ip);
  669. ip->ioc3_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2s */
  670. add_timer(&ip->ioc3_timer);
  671. }
  672. /*
  673. * Try to find a PHY. There is no apparent relation between the MII addresses
  674. * in the SGI documentation and what we find in reality, so we simply probe
  675. * for the PHY. It seems IOC3 PHYs usually live on address 31. One of my
  676. * onboard IOC3s has the special oddity that probing doesn't seem to find it
  677. * yet the interface seems to work fine, so if probing fails we for now will
  678. * simply default to PHY 31 instead of bailing out.
  679. */
  680. static int ioc3_mii_init(struct ioc3_private *ip)
  681. {
  682. struct net_device *dev = priv_netdev(ip);
  683. int i, found = 0, res = 0;
  684. int ioc3_phy_workaround = 1;
  685. u16 word;
  686. for (i = 0; i < 32; i++) {
  687. word = ioc3_mdio_read(dev, i, MII_PHYSID1);
  688. if (word != 0xffff && word != 0x0000) {
  689. found = 1;
  690. break; /* Found a PHY */
  691. }
  692. }
  693. if (!found) {
  694. if (ioc3_phy_workaround)
  695. i = 31;
  696. else {
  697. ip->mii.phy_id = -1;
  698. res = -ENODEV;
  699. goto out;
  700. }
  701. }
  702. ip->mii.phy_id = i;
  703. ip->ioc3_timer.expires = jiffies + (12 * HZ)/10; /* 1.2 sec. */
  704. ip->ioc3_timer.data = (unsigned long) ip;
  705. ip->ioc3_timer.function = &ioc3_timer;
  706. add_timer(&ip->ioc3_timer);
  707. out:
  708. return res;
  709. }
  710. static inline void ioc3_clean_rx_ring(struct ioc3_private *ip)
  711. {
  712. struct sk_buff *skb;
  713. int i;
  714. for (i = ip->rx_ci; i & 15; i++) {
  715. ip->rx_skbs[ip->rx_pi] = ip->rx_skbs[ip->rx_ci];
  716. ip->rxr[ip->rx_pi++] = ip->rxr[ip->rx_ci++];
  717. }
  718. ip->rx_pi &= 511;
  719. ip->rx_ci &= 511;
  720. for (i = ip->rx_ci; i != ip->rx_pi; i = (i+1) & 511) {
  721. struct ioc3_erxbuf *rxb;
  722. skb = ip->rx_skbs[i];
  723. rxb = (struct ioc3_erxbuf *) (skb->data - RX_OFFSET);
  724. rxb->w0 = 0;
  725. }
  726. }
  727. static inline void ioc3_clean_tx_ring(struct ioc3_private *ip)
  728. {
  729. struct sk_buff *skb;
  730. int i;
  731. for (i=0; i < 128; i++) {
  732. skb = ip->tx_skbs[i];
  733. if (skb) {
  734. ip->tx_skbs[i] = NULL;
  735. dev_kfree_skb_any(skb);
  736. }
  737. ip->txr[i].cmd = 0;
  738. }
  739. ip->tx_pi = 0;
  740. ip->tx_ci = 0;
  741. }
  742. static void ioc3_free_rings(struct ioc3_private *ip)
  743. {
  744. struct sk_buff *skb;
  745. int rx_entry, n_entry;
  746. if (ip->txr) {
  747. ioc3_clean_tx_ring(ip);
  748. free_pages((unsigned long)ip->txr, 2);
  749. ip->txr = NULL;
  750. }
  751. if (ip->rxr) {
  752. n_entry = ip->rx_ci;
  753. rx_entry = ip->rx_pi;
  754. while (n_entry != rx_entry) {
  755. skb = ip->rx_skbs[n_entry];
  756. if (skb)
  757. dev_kfree_skb_any(skb);
  758. n_entry = (n_entry + 1) & 511;
  759. }
  760. free_page((unsigned long)ip->rxr);
  761. ip->rxr = NULL;
  762. }
  763. }
  764. static void ioc3_alloc_rings(struct net_device *dev)
  765. {
  766. struct ioc3_private *ip = netdev_priv(dev);
  767. struct ioc3_erxbuf *rxb;
  768. unsigned long *rxr;
  769. int i;
  770. if (ip->rxr == NULL) {
  771. /* Allocate and initialize rx ring. 4kb = 512 entries */
  772. ip->rxr = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
  773. rxr = (unsigned long *) ip->rxr;
  774. if (!rxr)
  775. printk("ioc3_alloc_rings(): get_zeroed_page() failed!\n");
  776. /* Now the rx buffers. The RX ring may be larger but
  777. we only allocate 16 buffers for now. Need to tune
  778. this for performance and memory later. */
  779. for (i = 0; i < RX_BUFFS; i++) {
  780. struct sk_buff *skb;
  781. skb = ioc3_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
  782. if (!skb) {
  783. show_free_areas();
  784. continue;
  785. }
  786. ip->rx_skbs[i] = skb;
  787. skb->dev = dev;
  788. /* Because we reserve afterwards. */
  789. skb_put(skb, (1664 + RX_OFFSET));
  790. rxb = (struct ioc3_erxbuf *) skb->data;
  791. rxr[i] = cpu_to_be64(ioc3_map(rxb, 1));
  792. skb_reserve(skb, RX_OFFSET);
  793. }
  794. ip->rx_ci = 0;
  795. ip->rx_pi = RX_BUFFS;
  796. }
  797. if (ip->txr == NULL) {
  798. /* Allocate and initialize tx rings. 16kb = 128 bufs. */
  799. ip->txr = (struct ioc3_etxd *)__get_free_pages(GFP_KERNEL, 2);
  800. if (!ip->txr)
  801. printk("ioc3_alloc_rings(): __get_free_pages() failed!\n");
  802. ip->tx_pi = 0;
  803. ip->tx_ci = 0;
  804. }
  805. }
  806. static void ioc3_init_rings(struct net_device *dev)
  807. {
  808. struct ioc3_private *ip = netdev_priv(dev);
  809. struct ioc3 *ioc3 = ip->regs;
  810. unsigned long ring;
  811. ioc3_free_rings(ip);
  812. ioc3_alloc_rings(dev);
  813. ioc3_clean_rx_ring(ip);
  814. ioc3_clean_tx_ring(ip);
  815. /* Now the rx ring base, consume & produce registers. */
  816. ring = ioc3_map(ip->rxr, 0);
  817. ioc3_w_erbr_h(ring >> 32);
  818. ioc3_w_erbr_l(ring & 0xffffffff);
  819. ioc3_w_ercir(ip->rx_ci << 3);
  820. ioc3_w_erpir((ip->rx_pi << 3) | ERPIR_ARM);
  821. ring = ioc3_map(ip->txr, 0);
  822. ip->txqlen = 0; /* nothing queued */
  823. /* Now the tx ring base, consume & produce registers. */
  824. ioc3_w_etbr_h(ring >> 32);
  825. ioc3_w_etbr_l(ring & 0xffffffff);
  826. ioc3_w_etpir(ip->tx_pi << 7);
  827. ioc3_w_etcir(ip->tx_ci << 7);
  828. (void) ioc3_r_etcir(); /* Flush */
  829. }
  830. static inline void ioc3_ssram_disc(struct ioc3_private *ip)
  831. {
  832. struct ioc3 *ioc3 = ip->regs;
  833. volatile u32 *ssram0 = &ioc3->ssram[0x0000];
  834. volatile u32 *ssram1 = &ioc3->ssram[0x4000];
  835. unsigned int pattern = 0x5555;
  836. /* Assume the larger size SSRAM and enable parity checking */
  837. ioc3_w_emcr(ioc3_r_emcr() | (EMCR_BUFSIZ | EMCR_RAMPAR));
  838. *ssram0 = pattern;
  839. *ssram1 = ~pattern & IOC3_SSRAM_DM;
  840. if ((*ssram0 & IOC3_SSRAM_DM) != pattern ||
  841. (*ssram1 & IOC3_SSRAM_DM) != (~pattern & IOC3_SSRAM_DM)) {
  842. /* set ssram size to 64 KB */
  843. ip->emcr = EMCR_RAMPAR;
  844. ioc3_w_emcr(ioc3_r_emcr() & ~EMCR_BUFSIZ);
  845. } else
  846. ip->emcr = EMCR_BUFSIZ | EMCR_RAMPAR;
  847. }
  848. static void ioc3_init(struct net_device *dev)
  849. {
  850. struct ioc3_private *ip = netdev_priv(dev);
  851. struct ioc3 *ioc3 = ip->regs;
  852. del_timer(&ip->ioc3_timer); /* Kill if running */
  853. ioc3_w_emcr(EMCR_RST); /* Reset */
  854. (void) ioc3_r_emcr(); /* Flush WB */
  855. udelay(4); /* Give it time ... */
  856. ioc3_w_emcr(0);
  857. (void) ioc3_r_emcr();
  858. /* Misc registers */
  859. #ifdef CONFIG_SGI_IP27
  860. ioc3_w_erbar(PCI64_ATTR_BAR >> 32); /* Barrier on last store */
  861. #else
  862. ioc3_w_erbar(0); /* Let PCI API get it right */
  863. #endif
  864. (void) ioc3_r_etcdc(); /* Clear on read */
  865. ioc3_w_ercsr(15); /* RX low watermark */
  866. ioc3_w_ertr(0); /* Interrupt immediately */
  867. __ioc3_set_mac_address(dev);
  868. ioc3_w_ehar_h(ip->ehar_h);
  869. ioc3_w_ehar_l(ip->ehar_l);
  870. ioc3_w_ersr(42); /* XXX should be random */
  871. ioc3_init_rings(dev);
  872. ip->emcr |= ((RX_OFFSET / 2) << EMCR_RXOFF_SHIFT) | EMCR_TXDMAEN |
  873. EMCR_TXEN | EMCR_RXDMAEN | EMCR_RXEN | EMCR_PADEN;
  874. ioc3_w_emcr(ip->emcr);
  875. ioc3_w_eier(EISR_RXTIMERINT | EISR_RXOFLO | EISR_RXBUFOFLO |
  876. EISR_RXMEMERR | EISR_RXPARERR | EISR_TXBUFUFLO |
  877. EISR_TXEXPLICIT | EISR_TXMEMERR);
  878. (void) ioc3_r_eier();
  879. }
  880. static inline void ioc3_stop(struct ioc3_private *ip)
  881. {
  882. struct ioc3 *ioc3 = ip->regs;
  883. ioc3_w_emcr(0); /* Shutup */
  884. ioc3_w_eier(0); /* Disable interrupts */
  885. (void) ioc3_r_eier(); /* Flush */
  886. }
  887. static int ioc3_open(struct net_device *dev)
  888. {
  889. struct ioc3_private *ip = netdev_priv(dev);
  890. if (request_irq(dev->irq, ioc3_interrupt, SA_SHIRQ, ioc3_str, dev)) {
  891. printk(KERN_ERR "%s: Can't get irq %d\n", dev->name, dev->irq);
  892. return -EAGAIN;
  893. }
  894. ip->ehar_h = 0;
  895. ip->ehar_l = 0;
  896. ioc3_init(dev);
  897. netif_start_queue(dev);
  898. return 0;
  899. }
  900. static int ioc3_close(struct net_device *dev)
  901. {
  902. struct ioc3_private *ip = netdev_priv(dev);
  903. del_timer(&ip->ioc3_timer);
  904. netif_stop_queue(dev);
  905. ioc3_stop(ip);
  906. free_irq(dev->irq, dev);
  907. ioc3_free_rings(ip);
  908. return 0;
  909. }
  910. /*
  911. * MENET cards have four IOC3 chips, which are attached to two sets of
  912. * PCI slot resources each: the primary connections are on slots
  913. * 0..3 and the secondaries are on 4..7
  914. *
  915. * All four ethernets are brought out to connectors; six serial ports
  916. * (a pair from each of the first three IOC3s) are brought out to
  917. * MiniDINs; all other subdevices are left swinging in the wind, leave
  918. * them disabled.
  919. */
  920. static inline int ioc3_is_menet(struct pci_dev *pdev)
  921. {
  922. struct pci_dev *dev;
  923. return pdev->bus->parent == NULL
  924. && (dev = pci_find_slot(pdev->bus->number, PCI_DEVFN(0, 0)))
  925. && dev->vendor == PCI_VENDOR_ID_SGI
  926. && dev->device == PCI_DEVICE_ID_SGI_IOC3
  927. && (dev = pci_find_slot(pdev->bus->number, PCI_DEVFN(1, 0)))
  928. && dev->vendor == PCI_VENDOR_ID_SGI
  929. && dev->device == PCI_DEVICE_ID_SGI_IOC3
  930. && (dev = pci_find_slot(pdev->bus->number, PCI_DEVFN(2, 0)))
  931. && dev->vendor == PCI_VENDOR_ID_SGI
  932. && dev->device == PCI_DEVICE_ID_SGI_IOC3;
  933. }
  934. #ifdef CONFIG_SERIAL_8250
  935. /*
  936. * Note about serial ports and consoles:
  937. * For console output, everyone uses the IOC3 UARTA (offset 0x178)
  938. * connected to the master node (look in ip27_setup_console() and
  939. * ip27prom_console_write()).
  940. *
  941. * For serial (/dev/ttyS0 etc), we can not have hardcoded serial port
  942. * addresses on a partitioned machine. Since we currently use the ioc3
  943. * serial ports, we use dynamic serial port discovery that the serial.c
  944. * driver uses for pci/pnp ports (there is an entry for the SGI ioc3
  945. * boards in pci_boards[]). Unfortunately, UARTA's pio address is greater
  946. * than UARTB's, although UARTA on o200s has traditionally been known as
  947. * port 0. So, we just use one serial port from each ioc3 (since the
  948. * serial driver adds addresses to get to higher ports).
  949. *
  950. * The first one to do a register_console becomes the preferred console
  951. * (if there is no kernel command line console= directive). /dev/console
  952. * (ie 5, 1) is then "aliased" into the device number returned by the
  953. * "device" routine referred to in this console structure
  954. * (ip27prom_console_dev).
  955. *
  956. * Also look in ip27-pci.c:pci_fixup_ioc3() for some comments on working
  957. * around ioc3 oddities in this respect.
  958. *
  959. * The IOC3 serials use a 22MHz clock rate with an additional divider by 3.
  960. * (IOC3_BAUD = (22000000 / (3*16)))
  961. */
  962. static void __devinit ioc3_serial_probe(struct pci_dev *pdev, struct ioc3 *ioc3)
  963. {
  964. struct serial_struct req;
  965. /*
  966. * We need to recognice and treat the fourth MENET serial as it
  967. * does not have an SuperIO chip attached to it, therefore attempting
  968. * to access it will result in bus errors. We call something an
  969. * MENET if PCI slot 0, 1, 2 and 3 of a master PCI bus all have an IOC3
  970. * in it. This is paranoid but we want to avoid blowing up on a
  971. * showhorn PCI box that happens to have 4 IOC3 cards in it so it's
  972. * not paranoid enough ...
  973. */
  974. if (ioc3_is_menet(pdev) && PCI_SLOT(pdev->devfn) == 3)
  975. return;
  976. /* Register to interrupt zero because we share the interrupt with
  977. the serial driver which we don't properly support yet. */
  978. memset(&req, 0, sizeof(req));
  979. req.irq = 0;
  980. req.flags = IOC3_COM_FLAGS;
  981. req.io_type = SERIAL_IO_MEM;
  982. req.iomem_reg_shift = 0;
  983. req.baud_base = IOC3_BAUD;
  984. req.iomem_base = (unsigned char *) &ioc3->sregs.uarta;
  985. register_serial(&req);
  986. req.iomem_base = (unsigned char *) &ioc3->sregs.uartb;
  987. register_serial(&req);
  988. }
  989. #endif
  990. static int ioc3_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  991. {
  992. unsigned int sw_physid1, sw_physid2;
  993. struct net_device *dev = NULL;
  994. struct ioc3_private *ip;
  995. struct ioc3 *ioc3;
  996. unsigned long ioc3_base, ioc3_size;
  997. u32 vendor, model, rev;
  998. int err, pci_using_dac;
  999. /* Configure DMA attributes. */
  1000. err = pci_set_dma_mask(pdev, 0xffffffffffffffffULL);
  1001. if (!err) {
  1002. pci_using_dac = 1;
  1003. err = pci_set_consistent_dma_mask(pdev, 0xffffffffffffffffULL);
  1004. if (err < 0) {
  1005. printk(KERN_ERR "%s: Unable to obtain 64 bit DMA "
  1006. "for consistent allocations\n", pci_name(pdev));
  1007. goto out;
  1008. }
  1009. } else {
  1010. err = pci_set_dma_mask(pdev, 0xffffffffULL);
  1011. if (err) {
  1012. printk(KERN_ERR "%s: No usable DMA configuration, "
  1013. "aborting.\n", pci_name(pdev));
  1014. goto out;
  1015. }
  1016. pci_using_dac = 0;
  1017. }
  1018. if (pci_enable_device(pdev))
  1019. return -ENODEV;
  1020. dev = alloc_etherdev(sizeof(struct ioc3_private));
  1021. if (!dev) {
  1022. err = -ENOMEM;
  1023. goto out_disable;
  1024. }
  1025. if (pci_using_dac)
  1026. dev->features |= NETIF_F_HIGHDMA;
  1027. err = pci_request_regions(pdev, "ioc3");
  1028. if (err)
  1029. goto out_free;
  1030. SET_MODULE_OWNER(dev);
  1031. SET_NETDEV_DEV(dev, &pdev->dev);
  1032. ip = netdev_priv(dev);
  1033. dev->irq = pdev->irq;
  1034. ioc3_base = pci_resource_start(pdev, 0);
  1035. ioc3_size = pci_resource_len(pdev, 0);
  1036. ioc3 = (struct ioc3 *) ioremap(ioc3_base, ioc3_size);
  1037. if (!ioc3) {
  1038. printk(KERN_CRIT "ioc3eth(%s): ioremap failed, goodbye.\n",
  1039. pci_name(pdev));
  1040. err = -ENOMEM;
  1041. goto out_res;
  1042. }
  1043. ip->regs = ioc3;
  1044. #ifdef CONFIG_SERIAL_8250
  1045. ioc3_serial_probe(pdev, ioc3);
  1046. #endif
  1047. spin_lock_init(&ip->ioc3_lock);
  1048. init_timer(&ip->ioc3_timer);
  1049. ioc3_stop(ip);
  1050. ioc3_init(dev);
  1051. ip->pdev = pdev;
  1052. ip->mii.phy_id_mask = 0x1f;
  1053. ip->mii.reg_num_mask = 0x1f;
  1054. ip->mii.dev = dev;
  1055. ip->mii.mdio_read = ioc3_mdio_read;
  1056. ip->mii.mdio_write = ioc3_mdio_write;
  1057. ioc3_mii_init(ip);
  1058. if (ip->mii.phy_id == -1) {
  1059. printk(KERN_CRIT "ioc3-eth(%s): Didn't find a PHY, goodbye.\n",
  1060. pci_name(pdev));
  1061. err = -ENODEV;
  1062. goto out_stop;
  1063. }
  1064. ioc3_ssram_disc(ip);
  1065. ioc3_get_eaddr(ip);
  1066. /* The IOC3-specific entries in the device structure. */
  1067. dev->open = ioc3_open;
  1068. dev->hard_start_xmit = ioc3_start_xmit;
  1069. dev->tx_timeout = ioc3_timeout;
  1070. dev->watchdog_timeo = 5 * HZ;
  1071. dev->stop = ioc3_close;
  1072. dev->get_stats = ioc3_get_stats;
  1073. dev->do_ioctl = ioc3_ioctl;
  1074. dev->set_multicast_list = ioc3_set_multicast_list;
  1075. dev->set_mac_address = ioc3_set_mac_address;
  1076. dev->ethtool_ops = &ioc3_ethtool_ops;
  1077. #ifdef CONFIG_SGI_IOC3_ETH_HW_TX_CSUM
  1078. dev->features = NETIF_F_IP_CSUM;
  1079. #endif
  1080. sw_physid1 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID1);
  1081. sw_physid2 = ioc3_mdio_read(dev, ip->mii.phy_id, MII_PHYSID2);
  1082. err = register_netdev(dev);
  1083. if (err)
  1084. goto out_stop;
  1085. mii_check_media(&ip->mii, 1, 1);
  1086. ioc3_setup_duplex(ip);
  1087. vendor = (sw_physid1 << 12) | (sw_physid2 >> 4);
  1088. model = (sw_physid2 >> 4) & 0x3f;
  1089. rev = sw_physid2 & 0xf;
  1090. printk(KERN_INFO "%s: Using PHY %d, vendor 0x%x, model %d, "
  1091. "rev %d.\n", dev->name, ip->mii.phy_id, vendor, model, rev);
  1092. printk(KERN_INFO "%s: IOC3 SSRAM has %d kbyte.\n", dev->name,
  1093. ip->emcr & EMCR_BUFSIZ ? 128 : 64);
  1094. return 0;
  1095. out_stop:
  1096. ioc3_stop(ip);
  1097. ioc3_free_rings(ip);
  1098. out_res:
  1099. pci_release_regions(pdev);
  1100. out_free:
  1101. free_netdev(dev);
  1102. out_disable:
  1103. /*
  1104. * We should call pci_disable_device(pdev); here if the IOC3 wasn't
  1105. * such a weird device ...
  1106. */
  1107. out:
  1108. return err;
  1109. }
  1110. static void __devexit ioc3_remove_one (struct pci_dev *pdev)
  1111. {
  1112. struct net_device *dev = pci_get_drvdata(pdev);
  1113. struct ioc3_private *ip = netdev_priv(dev);
  1114. struct ioc3 *ioc3 = ip->regs;
  1115. unregister_netdev(dev);
  1116. iounmap(ioc3);
  1117. pci_release_regions(pdev);
  1118. free_netdev(dev);
  1119. /*
  1120. * We should call pci_disable_device(pdev); here if the IOC3 wasn't
  1121. * such a weird device ...
  1122. */
  1123. }
  1124. static struct pci_device_id ioc3_pci_tbl[] = {
  1125. { PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3, PCI_ANY_ID, PCI_ANY_ID },
  1126. { 0 }
  1127. };
  1128. MODULE_DEVICE_TABLE(pci, ioc3_pci_tbl);
  1129. static struct pci_driver ioc3_driver = {
  1130. .name = "ioc3-eth",
  1131. .id_table = ioc3_pci_tbl,
  1132. .probe = ioc3_probe,
  1133. .remove = __devexit_p(ioc3_remove_one),
  1134. };
  1135. static int __init ioc3_init_module(void)
  1136. {
  1137. return pci_module_init(&ioc3_driver);
  1138. }
  1139. static void __exit ioc3_cleanup_module(void)
  1140. {
  1141. pci_unregister_driver(&ioc3_driver);
  1142. }
  1143. static int ioc3_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1144. {
  1145. unsigned long data;
  1146. struct ioc3_private *ip = netdev_priv(dev);
  1147. struct ioc3 *ioc3 = ip->regs;
  1148. unsigned int len;
  1149. struct ioc3_etxd *desc;
  1150. uint32_t w0 = 0;
  1151. int produce;
  1152. #ifdef CONFIG_SGI_IOC3_ETH_HW_TX_CSUM
  1153. /*
  1154. * IOC3 has a fairly simple minded checksumming hardware which simply
  1155. * adds up the 1's complement checksum for the entire packet and
  1156. * inserts it at an offset which can be specified in the descriptor
  1157. * into the transmit packet. This means we have to compensate for the
  1158. * MAC header which should not be summed and the TCP/UDP pseudo headers
  1159. * manually.
  1160. */
  1161. if (skb->ip_summed == CHECKSUM_HW) {
  1162. int proto = ntohs(skb->nh.iph->protocol);
  1163. unsigned int csoff;
  1164. struct iphdr *ih = skb->nh.iph;
  1165. uint32_t csum, ehsum;
  1166. uint16_t *eh;
  1167. /* The MAC header. skb->mac seem the logic approach
  1168. to find the MAC header - except it's a NULL pointer ... */
  1169. eh = (uint16_t *) skb->data;
  1170. /* Sum up dest addr, src addr and protocol */
  1171. ehsum = eh[0] + eh[1] + eh[2] + eh[3] + eh[4] + eh[5] + eh[6];
  1172. /* Fold ehsum. can't use csum_fold which negates also ... */
  1173. ehsum = (ehsum & 0xffff) + (ehsum >> 16);
  1174. ehsum = (ehsum & 0xffff) + (ehsum >> 16);
  1175. /* Skip IP header; it's sum is always zero and was
  1176. already filled in by ip_output.c */
  1177. csum = csum_tcpudp_nofold(ih->saddr, ih->daddr,
  1178. ih->tot_len - (ih->ihl << 2),
  1179. proto, 0xffff ^ ehsum);
  1180. csum = (csum & 0xffff) + (csum >> 16); /* Fold again */
  1181. csum = (csum & 0xffff) + (csum >> 16);
  1182. csoff = ETH_HLEN + (ih->ihl << 2);
  1183. if (proto == IPPROTO_UDP) {
  1184. csoff += offsetof(struct udphdr, check);
  1185. skb->h.uh->check = csum;
  1186. }
  1187. if (proto == IPPROTO_TCP) {
  1188. csoff += offsetof(struct tcphdr, check);
  1189. skb->h.th->check = csum;
  1190. }
  1191. w0 = ETXD_DOCHECKSUM | (csoff << ETXD_CHKOFF_SHIFT);
  1192. }
  1193. #endif /* CONFIG_SGI_IOC3_ETH_HW_TX_CSUM */
  1194. spin_lock_irq(&ip->ioc3_lock);
  1195. data = (unsigned long) skb->data;
  1196. len = skb->len;
  1197. produce = ip->tx_pi;
  1198. desc = &ip->txr[produce];
  1199. if (len <= 104) {
  1200. /* Short packet, let's copy it directly into the ring. */
  1201. memcpy(desc->data, skb->data, skb->len);
  1202. if (len < ETH_ZLEN) {
  1203. /* Very short packet, pad with zeros at the end. */
  1204. memset(desc->data + len, 0, ETH_ZLEN - len);
  1205. len = ETH_ZLEN;
  1206. }
  1207. desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_D0V | w0);
  1208. desc->bufcnt = cpu_to_be32(len);
  1209. } else if ((data ^ (data + len - 1)) & 0x4000) {
  1210. unsigned long b2 = (data | 0x3fffUL) + 1UL;
  1211. unsigned long s1 = b2 - data;
  1212. unsigned long s2 = data + len - b2;
  1213. desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE |
  1214. ETXD_B1V | ETXD_B2V | w0);
  1215. desc->bufcnt = cpu_to_be32((s1 << ETXD_B1CNT_SHIFT) |
  1216. (s2 << ETXD_B2CNT_SHIFT));
  1217. desc->p1 = cpu_to_be64(ioc3_map(skb->data, 1));
  1218. desc->p2 = cpu_to_be64(ioc3_map((void *) b2, 1));
  1219. } else {
  1220. /* Normal sized packet that doesn't cross a page boundary. */
  1221. desc->cmd = cpu_to_be32(len | ETXD_INTWHENDONE | ETXD_B1V | w0);
  1222. desc->bufcnt = cpu_to_be32(len << ETXD_B1CNT_SHIFT);
  1223. desc->p1 = cpu_to_be64(ioc3_map(skb->data, 1));
  1224. }
  1225. BARRIER();
  1226. dev->trans_start = jiffies;
  1227. ip->tx_skbs[produce] = skb; /* Remember skb */
  1228. produce = (produce + 1) & 127;
  1229. ip->tx_pi = produce;
  1230. ioc3_w_etpir(produce << 7); /* Fire ... */
  1231. ip->txqlen++;
  1232. if (ip->txqlen >= 127)
  1233. netif_stop_queue(dev);
  1234. spin_unlock_irq(&ip->ioc3_lock);
  1235. return 0;
  1236. }
  1237. static void ioc3_timeout(struct net_device *dev)
  1238. {
  1239. struct ioc3_private *ip = netdev_priv(dev);
  1240. printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
  1241. spin_lock_irq(&ip->ioc3_lock);
  1242. ioc3_stop(ip);
  1243. ioc3_init(dev);
  1244. ioc3_mii_init(ip);
  1245. spin_unlock_irq(&ip->ioc3_lock);
  1246. netif_wake_queue(dev);
  1247. }
  1248. /*
  1249. * Given a multicast ethernet address, this routine calculates the
  1250. * address's bit index in the logical address filter mask
  1251. */
  1252. static inline unsigned int ioc3_hash(const unsigned char *addr)
  1253. {
  1254. unsigned int temp = 0;
  1255. u32 crc;
  1256. int bits;
  1257. crc = ether_crc_le(ETH_ALEN, addr);
  1258. crc &= 0x3f; /* bit reverse lowest 6 bits for hash index */
  1259. for (bits = 6; --bits >= 0; ) {
  1260. temp <<= 1;
  1261. temp |= (crc & 0x1);
  1262. crc >>= 1;
  1263. }
  1264. return temp;
  1265. }
  1266. static void ioc3_get_drvinfo (struct net_device *dev,
  1267. struct ethtool_drvinfo *info)
  1268. {
  1269. struct ioc3_private *ip = netdev_priv(dev);
  1270. strcpy (info->driver, IOC3_NAME);
  1271. strcpy (info->version, IOC3_VERSION);
  1272. strcpy (info->bus_info, pci_name(ip->pdev));
  1273. }
  1274. static int ioc3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1275. {
  1276. struct ioc3_private *ip = netdev_priv(dev);
  1277. int rc;
  1278. spin_lock_irq(&ip->ioc3_lock);
  1279. rc = mii_ethtool_gset(&ip->mii, cmd);
  1280. spin_unlock_irq(&ip->ioc3_lock);
  1281. return rc;
  1282. }
  1283. static int ioc3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1284. {
  1285. struct ioc3_private *ip = netdev_priv(dev);
  1286. int rc;
  1287. spin_lock_irq(&ip->ioc3_lock);
  1288. rc = mii_ethtool_sset(&ip->mii, cmd);
  1289. spin_unlock_irq(&ip->ioc3_lock);
  1290. return rc;
  1291. }
  1292. static int ioc3_nway_reset(struct net_device *dev)
  1293. {
  1294. struct ioc3_private *ip = netdev_priv(dev);
  1295. int rc;
  1296. spin_lock_irq(&ip->ioc3_lock);
  1297. rc = mii_nway_restart(&ip->mii);
  1298. spin_unlock_irq(&ip->ioc3_lock);
  1299. return rc;
  1300. }
  1301. static u32 ioc3_get_link(struct net_device *dev)
  1302. {
  1303. struct ioc3_private *ip = netdev_priv(dev);
  1304. int rc;
  1305. spin_lock_irq(&ip->ioc3_lock);
  1306. rc = mii_link_ok(&ip->mii);
  1307. spin_unlock_irq(&ip->ioc3_lock);
  1308. return rc;
  1309. }
  1310. static struct ethtool_ops ioc3_ethtool_ops = {
  1311. .get_drvinfo = ioc3_get_drvinfo,
  1312. .get_settings = ioc3_get_settings,
  1313. .set_settings = ioc3_set_settings,
  1314. .nway_reset = ioc3_nway_reset,
  1315. .get_link = ioc3_get_link,
  1316. };
  1317. static int ioc3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1318. {
  1319. struct ioc3_private *ip = netdev_priv(dev);
  1320. int rc;
  1321. spin_lock_irq(&ip->ioc3_lock);
  1322. rc = generic_mii_ioctl(&ip->mii, if_mii(rq), cmd, NULL);
  1323. spin_unlock_irq(&ip->ioc3_lock);
  1324. return rc;
  1325. }
  1326. static void ioc3_set_multicast_list(struct net_device *dev)
  1327. {
  1328. struct dev_mc_list *dmi = dev->mc_list;
  1329. struct ioc3_private *ip = netdev_priv(dev);
  1330. struct ioc3 *ioc3 = ip->regs;
  1331. u64 ehar = 0;
  1332. int i;
  1333. netif_stop_queue(dev); /* Lock out others. */
  1334. if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
  1335. /* Unconditionally log net taps. */
  1336. printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
  1337. ip->emcr |= EMCR_PROMISC;
  1338. ioc3_w_emcr(ip->emcr);
  1339. (void) ioc3_r_emcr();
  1340. } else {
  1341. ip->emcr &= ~EMCR_PROMISC;
  1342. ioc3_w_emcr(ip->emcr); /* Clear promiscuous. */
  1343. (void) ioc3_r_emcr();
  1344. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  1345. /* Too many for hashing to make sense or we want all
  1346. multicast packets anyway, so skip computing all the
  1347. hashes and just accept all packets. */
  1348. ip->ehar_h = 0xffffffff;
  1349. ip->ehar_l = 0xffffffff;
  1350. } else {
  1351. for (i = 0; i < dev->mc_count; i++) {
  1352. char *addr = dmi->dmi_addr;
  1353. dmi = dmi->next;
  1354. if (!(*addr & 1))
  1355. continue;
  1356. ehar |= (1UL << ioc3_hash(addr));
  1357. }
  1358. ip->ehar_h = ehar >> 32;
  1359. ip->ehar_l = ehar & 0xffffffff;
  1360. }
  1361. ioc3_w_ehar_h(ip->ehar_h);
  1362. ioc3_w_ehar_l(ip->ehar_l);
  1363. }
  1364. netif_wake_queue(dev); /* Let us get going again. */
  1365. }
  1366. MODULE_AUTHOR("Ralf Baechle <ralf@linux-mips.org>");
  1367. MODULE_DESCRIPTION("SGI IOC3 Ethernet driver");
  1368. MODULE_LICENSE("GPL");
  1369. module_init(ioc3_init_module);
  1370. module_exit(ioc3_cleanup_module);