ioc3-eth.c 44 KB

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