ioc3-eth.c 44 KB

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