ioc3-eth.c 43 KB

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