sbni.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. /* sbni.c: Granch SBNI12 leased line adapters driver for linux
  2. *
  3. * Written 2001 by Denis I.Timofeev (timofeev@granch.ru)
  4. *
  5. * Previous versions were written by Yaroslav Polyakov,
  6. * Alexey Zverev and Max Khon.
  7. *
  8. * Driver supports SBNI12-02,-04,-05,-10,-11 cards, single and
  9. * double-channel, PCI and ISA modifications.
  10. * More info and useful utilities to work with SBNI12 cards you can find
  11. * at http://www.granch.com (English) or http://www.granch.ru (Russian)
  12. *
  13. * This software may be used and distributed according to the terms
  14. * of the GNU General Public License.
  15. *
  16. *
  17. * 5.0.1 Jun 22 2001
  18. * - Fixed bug in probe
  19. * 5.0.0 Jun 06 2001
  20. * - Driver was completely redesigned by Denis I.Timofeev,
  21. * - now PCI/Dual, ISA/Dual (with single interrupt line) models are
  22. * - supported
  23. * 3.3.0 Thu Feb 24 21:30:28 NOVT 2000
  24. * - PCI cards support
  25. * 3.2.0 Mon Dec 13 22:26:53 NOVT 1999
  26. * - Completely rebuilt all the packet storage system
  27. * - to work in Ethernet-like style.
  28. * 3.1.1 just fixed some bugs (5 aug 1999)
  29. * 3.1.0 added balancing feature (26 apr 1999)
  30. * 3.0.1 just fixed some bugs (14 apr 1999).
  31. * 3.0.0 Initial Revision, Yaroslav Polyakov (24 Feb 1999)
  32. * - added pre-calculation for CRC, fixed bug with "len-2" frames,
  33. * - removed outbound fragmentation (MTU=1000), written CRC-calculation
  34. * - on asm, added work with hard_headers and now we have our own cache
  35. * - for them, optionally supported word-interchange on some chipsets,
  36. *
  37. * Known problem: this driver wasn't tested on multiprocessor machine.
  38. */
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/ptrace.h>
  42. #include <linux/fcntl.h>
  43. #include <linux/ioport.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/slab.h>
  46. #include <linux/string.h>
  47. #include <linux/errno.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/etherdevice.h>
  50. #include <linux/pci.h>
  51. #include <linux/skbuff.h>
  52. #include <linux/timer.h>
  53. #include <linux/init.h>
  54. #include <linux/delay.h>
  55. #include <net/arp.h>
  56. #include <asm/io.h>
  57. #include <asm/types.h>
  58. #include <asm/byteorder.h>
  59. #include <asm/irq.h>
  60. #include <asm/uaccess.h>
  61. #include "sbni.h"
  62. /* device private data */
  63. struct net_local {
  64. struct net_device_stats stats;
  65. struct timer_list watchdog;
  66. spinlock_t lock;
  67. struct sk_buff *rx_buf_p; /* receive buffer ptr */
  68. struct sk_buff *tx_buf_p; /* transmit buffer ptr */
  69. unsigned int framelen; /* current frame length */
  70. unsigned int maxframe; /* maximum valid frame length */
  71. unsigned int state;
  72. unsigned int inppos, outpos; /* positions in rx/tx buffers */
  73. /* transmitting frame number - from frames qty to 1 */
  74. unsigned int tx_frameno;
  75. /* expected number of next receiving frame */
  76. unsigned int wait_frameno;
  77. /* count of failed attempts to frame send - 32 attempts do before
  78. error - while receiver tunes on opposite side of wire */
  79. unsigned int trans_errors;
  80. /* idle time; send pong when limit exceeded */
  81. unsigned int timer_ticks;
  82. /* fields used for receive level autoselection */
  83. int delta_rxl;
  84. unsigned int cur_rxl_index, timeout_rxl;
  85. unsigned long cur_rxl_rcvd, prev_rxl_rcvd;
  86. struct sbni_csr1 csr1; /* current value of CSR1 */
  87. struct sbni_in_stats in_stats; /* internal statistics */
  88. struct net_device *second; /* for ISA/dual cards */
  89. #ifdef CONFIG_SBNI_MULTILINE
  90. struct net_device *master;
  91. struct net_device *link;
  92. #endif
  93. };
  94. static int sbni_card_probe( unsigned long );
  95. static int sbni_pci_probe( struct net_device * );
  96. static struct net_device *sbni_probe1(struct net_device *, unsigned long, int);
  97. static int sbni_open( struct net_device * );
  98. static int sbni_close( struct net_device * );
  99. static int sbni_start_xmit( struct sk_buff *, struct net_device * );
  100. static int sbni_ioctl( struct net_device *, struct ifreq *, int );
  101. static struct net_device_stats *sbni_get_stats( struct net_device * );
  102. static void set_multicast_list( struct net_device * );
  103. static irqreturn_t sbni_interrupt( int, void *, struct pt_regs * );
  104. static void handle_channel( struct net_device * );
  105. static int recv_frame( struct net_device * );
  106. static void send_frame( struct net_device * );
  107. static int upload_data( struct net_device *,
  108. unsigned, unsigned, unsigned, u32 );
  109. static void download_data( struct net_device *, u32 * );
  110. static void sbni_watchdog( unsigned long );
  111. static void interpret_ack( struct net_device *, unsigned );
  112. static int append_frame_to_pkt( struct net_device *, unsigned, u32 );
  113. static void indicate_pkt( struct net_device * );
  114. static void card_start( struct net_device * );
  115. static void prepare_to_send( struct sk_buff *, struct net_device * );
  116. static void drop_xmit_queue( struct net_device * );
  117. static void send_frame_header( struct net_device *, u32 * );
  118. static int skip_tail( unsigned int, unsigned int, u32 );
  119. static int check_fhdr( u32, u32 *, u32 *, u32 *, u32 *, u32 * );
  120. static void change_level( struct net_device * );
  121. static void timeout_change_level( struct net_device * );
  122. static u32 calc_crc32( u32, u8 *, u32 );
  123. static struct sk_buff * get_rx_buf( struct net_device * );
  124. static int sbni_init( struct net_device * );
  125. #ifdef CONFIG_SBNI_MULTILINE
  126. static int enslave( struct net_device *, struct net_device * );
  127. static int emancipate( struct net_device * );
  128. #endif
  129. #ifdef __i386__
  130. #define ASM_CRC 1
  131. #endif
  132. static const char version[] =
  133. "Granch SBNI12 driver ver 5.0.1 Jun 22 2001 Denis I.Timofeev.\n";
  134. static int skip_pci_probe __initdata = 0;
  135. static int scandone __initdata = 0;
  136. static int num __initdata = 0;
  137. static unsigned char rxl_tab[];
  138. static u32 crc32tab[];
  139. /* A list of all installed devices, for removing the driver module. */
  140. static struct net_device *sbni_cards[ SBNI_MAX_NUM_CARDS ];
  141. /* Lists of device's parameters */
  142. static u32 io[ SBNI_MAX_NUM_CARDS ] __initdata =
  143. { [0 ... SBNI_MAX_NUM_CARDS-1] = -1 };
  144. static u32 irq[ SBNI_MAX_NUM_CARDS ] __initdata;
  145. static u32 baud[ SBNI_MAX_NUM_CARDS ] __initdata;
  146. static u32 rxl[ SBNI_MAX_NUM_CARDS ] __initdata =
  147. { [0 ... SBNI_MAX_NUM_CARDS-1] = -1 };
  148. static u32 mac[ SBNI_MAX_NUM_CARDS ] __initdata;
  149. #ifndef MODULE
  150. typedef u32 iarr[];
  151. static iarr __initdata *dest[5] = { &io, &irq, &baud, &rxl, &mac };
  152. #endif
  153. /* A zero-terminated list of I/O addresses to be probed on ISA bus */
  154. static unsigned int netcard_portlist[ ] __initdata = {
  155. 0x210, 0x214, 0x220, 0x224, 0x230, 0x234, 0x240, 0x244, 0x250, 0x254,
  156. 0x260, 0x264, 0x270, 0x274, 0x280, 0x284, 0x290, 0x294, 0x2a0, 0x2a4,
  157. 0x2b0, 0x2b4, 0x2c0, 0x2c4, 0x2d0, 0x2d4, 0x2e0, 0x2e4, 0x2f0, 0x2f4,
  158. 0 };
  159. /*
  160. * Look for SBNI card which addr stored in dev->base_addr, if nonzero.
  161. * Otherwise, look through PCI bus. If none PCI-card was found, scan ISA.
  162. */
  163. static inline int __init
  164. sbni_isa_probe( struct net_device *dev )
  165. {
  166. if( dev->base_addr > 0x1ff
  167. && request_region( dev->base_addr, SBNI_IO_EXTENT, dev->name )
  168. && sbni_probe1( dev, dev->base_addr, dev->irq ) )
  169. return 0;
  170. else {
  171. printk( KERN_ERR "sbni: base address 0x%lx is busy, or adapter "
  172. "is malfunctional!\n", dev->base_addr );
  173. return -ENODEV;
  174. }
  175. }
  176. static void __init sbni_devsetup(struct net_device *dev)
  177. {
  178. ether_setup( dev );
  179. dev->open = &sbni_open;
  180. dev->stop = &sbni_close;
  181. dev->hard_start_xmit = &sbni_start_xmit;
  182. dev->get_stats = &sbni_get_stats;
  183. dev->set_multicast_list = &set_multicast_list;
  184. dev->do_ioctl = &sbni_ioctl;
  185. SET_MODULE_OWNER( dev );
  186. }
  187. int __init sbni_probe(int unit)
  188. {
  189. struct net_device *dev;
  190. static unsigned version_printed __initdata = 0;
  191. int err;
  192. dev = alloc_netdev(sizeof(struct net_local), "sbni", sbni_devsetup);
  193. if (!dev)
  194. return -ENOMEM;
  195. sprintf(dev->name, "sbni%d", unit);
  196. netdev_boot_setup_check(dev);
  197. err = sbni_init(dev);
  198. if (err) {
  199. free_netdev(dev);
  200. return err;
  201. }
  202. err = register_netdev(dev);
  203. if (err) {
  204. release_region( dev->base_addr, SBNI_IO_EXTENT );
  205. free_netdev(dev);
  206. return err;
  207. }
  208. if( version_printed++ == 0 )
  209. printk( KERN_INFO "%s", version );
  210. return 0;
  211. }
  212. static int __init sbni_init(struct net_device *dev)
  213. {
  214. int i;
  215. if( dev->base_addr )
  216. return sbni_isa_probe( dev );
  217. /* otherwise we have to perform search our adapter */
  218. if( io[ num ] != -1 )
  219. dev->base_addr = io[ num ],
  220. dev->irq = irq[ num ];
  221. else if( scandone || io[ 0 ] != -1 )
  222. return -ENODEV;
  223. /* if io[ num ] contains non-zero address, then that is on ISA bus */
  224. if( dev->base_addr )
  225. return sbni_isa_probe( dev );
  226. /* ...otherwise - scan PCI first */
  227. if( !skip_pci_probe && !sbni_pci_probe( dev ) )
  228. return 0;
  229. if( io[ num ] == -1 ) {
  230. /* Auto-scan will be stopped when first ISA card were found */
  231. scandone = 1;
  232. if( num > 0 )
  233. return -ENODEV;
  234. }
  235. for( i = 0; netcard_portlist[ i ]; ++i ) {
  236. int ioaddr = netcard_portlist[ i ];
  237. if( request_region( ioaddr, SBNI_IO_EXTENT, dev->name )
  238. && sbni_probe1( dev, ioaddr, 0 ))
  239. return 0;
  240. }
  241. return -ENODEV;
  242. }
  243. int __init
  244. sbni_pci_probe( struct net_device *dev )
  245. {
  246. struct pci_dev *pdev = NULL;
  247. while( (pdev = pci_get_class( PCI_CLASS_NETWORK_OTHER << 8, pdev ))
  248. != NULL ) {
  249. int pci_irq_line;
  250. unsigned long pci_ioaddr;
  251. u16 subsys;
  252. if( pdev->vendor != SBNI_PCI_VENDOR
  253. && pdev->device != SBNI_PCI_DEVICE )
  254. continue;
  255. pci_ioaddr = pci_resource_start( pdev, 0 );
  256. pci_irq_line = pdev->irq;
  257. /* Avoid already found cards from previous calls */
  258. if( !request_region( pci_ioaddr, SBNI_IO_EXTENT, dev->name ) ) {
  259. pci_read_config_word( pdev, PCI_SUBSYSTEM_ID, &subsys );
  260. if (subsys != 2)
  261. continue;
  262. /* Dual adapter is present */
  263. if (!request_region(pci_ioaddr += 4, SBNI_IO_EXTENT,
  264. dev->name ) )
  265. continue;
  266. }
  267. if( pci_irq_line <= 0 || pci_irq_line >= NR_IRQS )
  268. printk( KERN_WARNING " WARNING: The PCI BIOS assigned "
  269. "this PCI card to IRQ %d, which is unlikely "
  270. "to work!.\n"
  271. KERN_WARNING " You should use the PCI BIOS "
  272. "setup to assign a valid IRQ line.\n",
  273. pci_irq_line );
  274. /* avoiding re-enable dual adapters */
  275. if( (pci_ioaddr & 7) == 0 && pci_enable_device( pdev ) ) {
  276. release_region( pci_ioaddr, SBNI_IO_EXTENT );
  277. pci_dev_put( pdev );
  278. return -EIO;
  279. }
  280. if( sbni_probe1( dev, pci_ioaddr, pci_irq_line ) ) {
  281. SET_NETDEV_DEV(dev, &pdev->dev);
  282. /* not the best thing to do, but this is all messed up
  283. for hotplug systems anyway... */
  284. pci_dev_put( pdev );
  285. return 0;
  286. }
  287. }
  288. return -ENODEV;
  289. }
  290. static struct net_device * __init
  291. sbni_probe1( struct net_device *dev, unsigned long ioaddr, int irq )
  292. {
  293. struct net_local *nl;
  294. if( sbni_card_probe( ioaddr ) ) {
  295. release_region( ioaddr, SBNI_IO_EXTENT );
  296. return NULL;
  297. }
  298. outb( 0, ioaddr + CSR0 );
  299. if( irq < 2 ) {
  300. unsigned long irq_mask;
  301. irq_mask = probe_irq_on();
  302. outb( EN_INT | TR_REQ, ioaddr + CSR0 );
  303. outb( PR_RES, ioaddr + CSR1 );
  304. mdelay(50);
  305. irq = probe_irq_off(irq_mask);
  306. outb( 0, ioaddr + CSR0 );
  307. if( !irq ) {
  308. printk( KERN_ERR "%s: can't detect device irq!\n",
  309. dev->name );
  310. release_region( ioaddr, SBNI_IO_EXTENT );
  311. return NULL;
  312. }
  313. } else if( irq == 2 )
  314. irq = 9;
  315. dev->irq = irq;
  316. dev->base_addr = ioaddr;
  317. /* Allocate dev->priv and fill in sbni-specific dev fields. */
  318. nl = dev->priv;
  319. if( !nl ) {
  320. printk( KERN_ERR "%s: unable to get memory!\n", dev->name );
  321. release_region( ioaddr, SBNI_IO_EXTENT );
  322. return NULL;
  323. }
  324. dev->priv = nl;
  325. memset( nl, 0, sizeof(struct net_local) );
  326. spin_lock_init( &nl->lock );
  327. /* store MAC address (generate if that isn't known) */
  328. *(u16 *)dev->dev_addr = htons( 0x00ff );
  329. *(u32 *)(dev->dev_addr + 2) = htonl( 0x01000000 |
  330. ( (mac[num] ? mac[num] : (u32)((long)dev->priv)) & 0x00ffffff) );
  331. /* store link settings (speed, receive level ) */
  332. nl->maxframe = DEFAULT_FRAME_LEN;
  333. nl->csr1.rate = baud[ num ];
  334. if( (nl->cur_rxl_index = rxl[ num ]) == -1 )
  335. /* autotune rxl */
  336. nl->cur_rxl_index = DEF_RXL,
  337. nl->delta_rxl = DEF_RXL_DELTA;
  338. else
  339. nl->delta_rxl = 0;
  340. nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index ];
  341. if( inb( ioaddr + CSR0 ) & 0x01 )
  342. nl->state |= FL_SLOW_MODE;
  343. printk( KERN_NOTICE "%s: ioaddr %#lx, irq %d, "
  344. "MAC: 00:ff:01:%02x:%02x:%02x\n",
  345. dev->name, dev->base_addr, dev->irq,
  346. ((u8 *) dev->dev_addr) [3],
  347. ((u8 *) dev->dev_addr) [4],
  348. ((u8 *) dev->dev_addr) [5] );
  349. printk( KERN_NOTICE "%s: speed %d, receive level ", dev->name,
  350. ( (nl->state & FL_SLOW_MODE) ? 500000 : 2000000)
  351. / (1 << nl->csr1.rate) );
  352. if( nl->delta_rxl == 0 )
  353. printk( "0x%x (fixed)\n", nl->cur_rxl_index );
  354. else
  355. printk( "(auto)\n");
  356. #ifdef CONFIG_SBNI_MULTILINE
  357. nl->master = dev;
  358. nl->link = NULL;
  359. #endif
  360. sbni_cards[ num++ ] = dev;
  361. return dev;
  362. }
  363. /* -------------------------------------------------------------------------- */
  364. #ifdef CONFIG_SBNI_MULTILINE
  365. static int
  366. sbni_start_xmit( struct sk_buff *skb, struct net_device *dev )
  367. {
  368. struct net_device *p;
  369. netif_stop_queue( dev );
  370. /* Looking for idle device in the list */
  371. for( p = dev; p; ) {
  372. struct net_local *nl = (struct net_local *) p->priv;
  373. spin_lock( &nl->lock );
  374. if( nl->tx_buf_p || (nl->state & FL_LINE_DOWN) ) {
  375. p = nl->link;
  376. spin_unlock( &nl->lock );
  377. } else {
  378. /* Idle dev is found */
  379. prepare_to_send( skb, p );
  380. spin_unlock( &nl->lock );
  381. netif_start_queue( dev );
  382. return 0;
  383. }
  384. }
  385. return 1;
  386. }
  387. #else /* CONFIG_SBNI_MULTILINE */
  388. static int
  389. sbni_start_xmit( struct sk_buff *skb, struct net_device *dev )
  390. {
  391. struct net_local *nl = (struct net_local *) dev->priv;
  392. netif_stop_queue( dev );
  393. spin_lock( &nl->lock );
  394. prepare_to_send( skb, dev );
  395. spin_unlock( &nl->lock );
  396. return 0;
  397. }
  398. #endif /* CONFIG_SBNI_MULTILINE */
  399. /* -------------------------------------------------------------------------- */
  400. /* interrupt handler */
  401. /*
  402. * SBNI12D-10, -11/ISA boards within "common interrupt" mode could not
  403. * be looked as two independent single-channel devices. Every channel seems
  404. * as Ethernet interface but interrupt handler must be common. Really, first
  405. * channel ("master") driver only registers the handler. In its struct net_local
  406. * it has got pointer to "slave" channel's struct net_local and handles that's
  407. * interrupts too.
  408. * dev of successfully attached ISA SBNI boards is linked to list.
  409. * While next board driver is initialized, it scans this list. If one
  410. * has found dev with same irq and ioaddr different by 4 then it assumes
  411. * this board to be "master".
  412. */
  413. static irqreturn_t
  414. sbni_interrupt( int irq, void *dev_id, struct pt_regs *regs )
  415. {
  416. struct net_device *dev = (struct net_device *) dev_id;
  417. struct net_local *nl = (struct net_local *) dev->priv;
  418. int repeat;
  419. spin_lock( &nl->lock );
  420. if( nl->second )
  421. spin_lock( &((struct net_local *) nl->second->priv)->lock );
  422. do {
  423. repeat = 0;
  424. if( inb( dev->base_addr + CSR0 ) & (RC_RDY | TR_RDY) )
  425. handle_channel( dev ),
  426. repeat = 1;
  427. if( nl->second && /* second channel present */
  428. (inb( nl->second->base_addr+CSR0 ) & (RC_RDY | TR_RDY)) )
  429. handle_channel( nl->second ),
  430. repeat = 1;
  431. } while( repeat );
  432. if( nl->second )
  433. spin_unlock( &((struct net_local *)nl->second->priv)->lock );
  434. spin_unlock( &nl->lock );
  435. return IRQ_HANDLED;
  436. }
  437. static void
  438. handle_channel( struct net_device *dev )
  439. {
  440. struct net_local *nl = (struct net_local *) dev->priv;
  441. unsigned long ioaddr = dev->base_addr;
  442. int req_ans;
  443. unsigned char csr0;
  444. #ifdef CONFIG_SBNI_MULTILINE
  445. /* Lock the master device because we going to change its local data */
  446. if( nl->state & FL_SLAVE )
  447. spin_lock( &((struct net_local *) nl->master->priv)->lock );
  448. #endif
  449. outb( (inb( ioaddr + CSR0 ) & ~EN_INT) | TR_REQ, ioaddr + CSR0 );
  450. nl->timer_ticks = CHANGE_LEVEL_START_TICKS;
  451. for(;;) {
  452. csr0 = inb( ioaddr + CSR0 );
  453. if( ( csr0 & (RC_RDY | TR_RDY) ) == 0 )
  454. break;
  455. req_ans = !(nl->state & FL_PREV_OK);
  456. if( csr0 & RC_RDY )
  457. req_ans = recv_frame( dev );
  458. /*
  459. * TR_RDY always equals 1 here because we have owned the marker,
  460. * and we set TR_REQ when disabled interrupts
  461. */
  462. csr0 = inb( ioaddr + CSR0 );
  463. if( !(csr0 & TR_RDY) || (csr0 & RC_RDY) )
  464. printk( KERN_ERR "%s: internal error!\n", dev->name );
  465. /* if state & FL_NEED_RESEND != 0 then tx_frameno != 0 */
  466. if( req_ans || nl->tx_frameno != 0 )
  467. send_frame( dev );
  468. else
  469. /* send marker without any data */
  470. outb( inb( ioaddr + CSR0 ) & ~TR_REQ, ioaddr + CSR0 );
  471. }
  472. outb( inb( ioaddr + CSR0 ) | EN_INT, ioaddr + CSR0 );
  473. #ifdef CONFIG_SBNI_MULTILINE
  474. if( nl->state & FL_SLAVE )
  475. spin_unlock( &((struct net_local *) nl->master->priv)->lock );
  476. #endif
  477. }
  478. /*
  479. * Routine returns 1 if it need to acknoweledge received frame.
  480. * Empty frame received without errors won't be acknoweledged.
  481. */
  482. static int
  483. recv_frame( struct net_device *dev )
  484. {
  485. struct net_local *nl = (struct net_local *) dev->priv;
  486. unsigned long ioaddr = dev->base_addr;
  487. u32 crc = CRC32_INITIAL;
  488. unsigned framelen, frameno, ack;
  489. unsigned is_first, frame_ok;
  490. if( check_fhdr( ioaddr, &framelen, &frameno, &ack, &is_first, &crc ) ) {
  491. frame_ok = framelen > 4
  492. ? upload_data( dev, framelen, frameno, is_first, crc )
  493. : skip_tail( ioaddr, framelen, crc );
  494. if( frame_ok )
  495. interpret_ack( dev, ack );
  496. } else
  497. frame_ok = 0;
  498. outb( inb( ioaddr + CSR0 ) ^ CT_ZER, ioaddr + CSR0 );
  499. if( frame_ok ) {
  500. nl->state |= FL_PREV_OK;
  501. if( framelen > 4 )
  502. nl->in_stats.all_rx_number++;
  503. } else
  504. nl->state &= ~FL_PREV_OK,
  505. change_level( dev ),
  506. nl->in_stats.all_rx_number++,
  507. nl->in_stats.bad_rx_number++;
  508. return !frame_ok || framelen > 4;
  509. }
  510. static void
  511. send_frame( struct net_device *dev )
  512. {
  513. struct net_local *nl = (struct net_local *) dev->priv;
  514. u32 crc = CRC32_INITIAL;
  515. if( nl->state & FL_NEED_RESEND ) {
  516. /* if frame was sended but not ACK'ed - resend it */
  517. if( nl->trans_errors ) {
  518. --nl->trans_errors;
  519. if( nl->framelen != 0 )
  520. nl->in_stats.resend_tx_number++;
  521. } else {
  522. /* cannot xmit with many attempts */
  523. #ifdef CONFIG_SBNI_MULTILINE
  524. if( (nl->state & FL_SLAVE) || nl->link )
  525. #endif
  526. nl->state |= FL_LINE_DOWN;
  527. drop_xmit_queue( dev );
  528. goto do_send;
  529. }
  530. } else
  531. nl->trans_errors = TR_ERROR_COUNT;
  532. send_frame_header( dev, &crc );
  533. nl->state |= FL_NEED_RESEND;
  534. /*
  535. * FL_NEED_RESEND will be cleared after ACK, but if empty
  536. * frame sended then in prepare_to_send next frame
  537. */
  538. if( nl->framelen ) {
  539. download_data( dev, &crc );
  540. nl->in_stats.all_tx_number++;
  541. nl->state |= FL_WAIT_ACK;
  542. }
  543. outsb( dev->base_addr + DAT, (u8 *)&crc, sizeof crc );
  544. do_send:
  545. outb( inb( dev->base_addr + CSR0 ) & ~TR_REQ, dev->base_addr + CSR0 );
  546. if( nl->tx_frameno )
  547. /* next frame exists - we request card to send it */
  548. outb( inb( dev->base_addr + CSR0 ) | TR_REQ,
  549. dev->base_addr + CSR0 );
  550. }
  551. /*
  552. * Write the frame data into adapter's buffer memory, and calculate CRC.
  553. * Do padding if necessary.
  554. */
  555. static void
  556. download_data( struct net_device *dev, u32 *crc_p )
  557. {
  558. struct net_local *nl = (struct net_local *) dev->priv;
  559. struct sk_buff *skb = nl->tx_buf_p;
  560. unsigned len = min_t(unsigned int, skb->len - nl->outpos, nl->framelen);
  561. outsb( dev->base_addr + DAT, skb->data + nl->outpos, len );
  562. *crc_p = calc_crc32( *crc_p, skb->data + nl->outpos, len );
  563. /* if packet too short we should write some more bytes to pad */
  564. for( len = nl->framelen - len; len--; )
  565. outb( 0, dev->base_addr + DAT ),
  566. *crc_p = CRC32( 0, *crc_p );
  567. }
  568. static int
  569. upload_data( struct net_device *dev, unsigned framelen, unsigned frameno,
  570. unsigned is_first, u32 crc )
  571. {
  572. struct net_local *nl = (struct net_local *) dev->priv;
  573. int frame_ok;
  574. if( is_first )
  575. nl->wait_frameno = frameno,
  576. nl->inppos = 0;
  577. if( nl->wait_frameno == frameno ) {
  578. if( nl->inppos + framelen <= ETHER_MAX_LEN )
  579. frame_ok = append_frame_to_pkt( dev, framelen, crc );
  580. /*
  581. * if CRC is right but framelen incorrect then transmitter
  582. * error was occurred... drop entire packet
  583. */
  584. else if( (frame_ok = skip_tail( dev->base_addr, framelen, crc ))
  585. != 0 )
  586. nl->wait_frameno = 0,
  587. nl->inppos = 0,
  588. #ifdef CONFIG_SBNI_MULTILINE
  589. ((struct net_local *) nl->master->priv)
  590. ->stats.rx_errors++,
  591. ((struct net_local *) nl->master->priv)
  592. ->stats.rx_missed_errors++;
  593. #else
  594. nl->stats.rx_errors++,
  595. nl->stats.rx_missed_errors++;
  596. #endif
  597. /* now skip all frames until is_first != 0 */
  598. } else
  599. frame_ok = skip_tail( dev->base_addr, framelen, crc );
  600. if( is_first && !frame_ok )
  601. /*
  602. * Frame has been broken, but we had already stored
  603. * is_first... Drop entire packet.
  604. */
  605. nl->wait_frameno = 0,
  606. #ifdef CONFIG_SBNI_MULTILINE
  607. ((struct net_local *) nl->master->priv)->stats.rx_errors++,
  608. ((struct net_local *) nl->master->priv)->stats.rx_crc_errors++;
  609. #else
  610. nl->stats.rx_errors++,
  611. nl->stats.rx_crc_errors++;
  612. #endif
  613. return frame_ok;
  614. }
  615. static __inline void
  616. send_complete( struct net_local *nl )
  617. {
  618. #ifdef CONFIG_SBNI_MULTILINE
  619. ((struct net_local *) nl->master->priv)->stats.tx_packets++;
  620. ((struct net_local *) nl->master->priv)->stats.tx_bytes
  621. += nl->tx_buf_p->len;
  622. #else
  623. nl->stats.tx_packets++;
  624. nl->stats.tx_bytes += nl->tx_buf_p->len;
  625. #endif
  626. dev_kfree_skb_irq( nl->tx_buf_p );
  627. nl->tx_buf_p = NULL;
  628. nl->outpos = 0;
  629. nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
  630. nl->framelen = 0;
  631. }
  632. static void
  633. interpret_ack( struct net_device *dev, unsigned ack )
  634. {
  635. struct net_local *nl = (struct net_local *) dev->priv;
  636. if( ack == FRAME_SENT_OK ) {
  637. nl->state &= ~FL_NEED_RESEND;
  638. if( nl->state & FL_WAIT_ACK ) {
  639. nl->outpos += nl->framelen;
  640. if( --nl->tx_frameno )
  641. nl->framelen = min_t(unsigned int,
  642. nl->maxframe,
  643. nl->tx_buf_p->len - nl->outpos);
  644. else
  645. send_complete( nl ),
  646. #ifdef CONFIG_SBNI_MULTILINE
  647. netif_wake_queue( nl->master );
  648. #else
  649. netif_wake_queue( dev );
  650. #endif
  651. }
  652. }
  653. nl->state &= ~FL_WAIT_ACK;
  654. }
  655. /*
  656. * Glue received frame with previous fragments of packet.
  657. * Indicate packet when last frame would be accepted.
  658. */
  659. static int
  660. append_frame_to_pkt( struct net_device *dev, unsigned framelen, u32 crc )
  661. {
  662. struct net_local *nl = (struct net_local *) dev->priv;
  663. u8 *p;
  664. if( nl->inppos + framelen > ETHER_MAX_LEN )
  665. return 0;
  666. if( !nl->rx_buf_p && !(nl->rx_buf_p = get_rx_buf( dev )) )
  667. return 0;
  668. p = nl->rx_buf_p->data + nl->inppos;
  669. insb( dev->base_addr + DAT, p, framelen );
  670. if( calc_crc32( crc, p, framelen ) != CRC32_REMAINDER )
  671. return 0;
  672. nl->inppos += framelen - 4;
  673. if( --nl->wait_frameno == 0 ) /* last frame received */
  674. indicate_pkt( dev );
  675. return 1;
  676. }
  677. /*
  678. * Prepare to start output on adapter.
  679. * Transmitter will be actually activated when marker is accepted.
  680. */
  681. static void
  682. prepare_to_send( struct sk_buff *skb, struct net_device *dev )
  683. {
  684. struct net_local *nl = (struct net_local *) dev->priv;
  685. unsigned int len;
  686. /* nl->tx_buf_p == NULL here! */
  687. if( nl->tx_buf_p )
  688. printk( KERN_ERR "%s: memory leak!\n", dev->name );
  689. nl->outpos = 0;
  690. nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
  691. len = skb->len;
  692. if( len < SBNI_MIN_LEN )
  693. len = SBNI_MIN_LEN;
  694. nl->tx_buf_p = skb;
  695. nl->tx_frameno = (len + nl->maxframe - 1) / nl->maxframe;
  696. nl->framelen = len < nl->maxframe ? len : nl->maxframe;
  697. outb( inb( dev->base_addr + CSR0 ) | TR_REQ, dev->base_addr + CSR0 );
  698. #ifdef CONFIG_SBNI_MULTILINE
  699. nl->master->trans_start = jiffies;
  700. #else
  701. dev->trans_start = jiffies;
  702. #endif
  703. }
  704. static void
  705. drop_xmit_queue( struct net_device *dev )
  706. {
  707. struct net_local *nl = (struct net_local *) dev->priv;
  708. if( nl->tx_buf_p )
  709. dev_kfree_skb_any( nl->tx_buf_p ),
  710. nl->tx_buf_p = NULL,
  711. #ifdef CONFIG_SBNI_MULTILINE
  712. ((struct net_local *) nl->master->priv)
  713. ->stats.tx_errors++,
  714. ((struct net_local *) nl->master->priv)
  715. ->stats.tx_carrier_errors++;
  716. #else
  717. nl->stats.tx_errors++,
  718. nl->stats.tx_carrier_errors++;
  719. #endif
  720. nl->tx_frameno = 0;
  721. nl->framelen = 0;
  722. nl->outpos = 0;
  723. nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
  724. #ifdef CONFIG_SBNI_MULTILINE
  725. netif_start_queue( nl->master );
  726. nl->master->trans_start = jiffies;
  727. #else
  728. netif_start_queue( dev );
  729. dev->trans_start = jiffies;
  730. #endif
  731. }
  732. static void
  733. send_frame_header( struct net_device *dev, u32 *crc_p )
  734. {
  735. struct net_local *nl = (struct net_local *) dev->priv;
  736. u32 crc = *crc_p;
  737. u32 len_field = nl->framelen + 6; /* CRC + frameno + reserved */
  738. u8 value;
  739. if( nl->state & FL_NEED_RESEND )
  740. len_field |= FRAME_RETRY; /* non-first attempt... */
  741. if( nl->outpos == 0 )
  742. len_field |= FRAME_FIRST;
  743. len_field |= (nl->state & FL_PREV_OK) ? FRAME_SENT_OK : FRAME_SENT_BAD;
  744. outb( SBNI_SIG, dev->base_addr + DAT );
  745. value = (u8) len_field;
  746. outb( value, dev->base_addr + DAT );
  747. crc = CRC32( value, crc );
  748. value = (u8) (len_field >> 8);
  749. outb( value, dev->base_addr + DAT );
  750. crc = CRC32( value, crc );
  751. outb( nl->tx_frameno, dev->base_addr + DAT );
  752. crc = CRC32( nl->tx_frameno, crc );
  753. outb( 0, dev->base_addr + DAT );
  754. crc = CRC32( 0, crc );
  755. *crc_p = crc;
  756. }
  757. /*
  758. * if frame tail not needed (incorrect number or received twice),
  759. * it won't store, but CRC will be calculated
  760. */
  761. static int
  762. skip_tail( unsigned int ioaddr, unsigned int tail_len, u32 crc )
  763. {
  764. while( tail_len-- )
  765. crc = CRC32( inb( ioaddr + DAT ), crc );
  766. return crc == CRC32_REMAINDER;
  767. }
  768. /*
  769. * Preliminary checks if frame header is correct, calculates its CRC
  770. * and split it to simple fields
  771. */
  772. static int
  773. check_fhdr( u32 ioaddr, u32 *framelen, u32 *frameno, u32 *ack,
  774. u32 *is_first, u32 *crc_p )
  775. {
  776. u32 crc = *crc_p;
  777. u8 value;
  778. if( inb( ioaddr + DAT ) != SBNI_SIG )
  779. return 0;
  780. value = inb( ioaddr + DAT );
  781. *framelen = (u32)value;
  782. crc = CRC32( value, crc );
  783. value = inb( ioaddr + DAT );
  784. *framelen |= ((u32)value) << 8;
  785. crc = CRC32( value, crc );
  786. *ack = *framelen & FRAME_ACK_MASK;
  787. *is_first = (*framelen & FRAME_FIRST) != 0;
  788. if( (*framelen &= FRAME_LEN_MASK) < 6
  789. || *framelen > SBNI_MAX_FRAME - 3 )
  790. return 0;
  791. value = inb( ioaddr + DAT );
  792. *frameno = (u32)value;
  793. crc = CRC32( value, crc );
  794. crc = CRC32( inb( ioaddr + DAT ), crc ); /* reserved byte */
  795. *framelen -= 2;
  796. *crc_p = crc;
  797. return 1;
  798. }
  799. static struct sk_buff *
  800. get_rx_buf( struct net_device *dev )
  801. {
  802. /* +2 is to compensate for the alignment fixup below */
  803. struct sk_buff *skb = dev_alloc_skb( ETHER_MAX_LEN + 2 );
  804. if( !skb )
  805. return NULL;
  806. #ifdef CONFIG_SBNI_MULTILINE
  807. skb->dev = ((struct net_local *) dev->priv)->master;
  808. #else
  809. skb->dev = dev;
  810. #endif
  811. skb_reserve( skb, 2 ); /* Align IP on longword boundaries */
  812. return skb;
  813. }
  814. static void
  815. indicate_pkt( struct net_device *dev )
  816. {
  817. struct net_local *nl = (struct net_local *) dev->priv;
  818. struct sk_buff *skb = nl->rx_buf_p;
  819. skb_put( skb, nl->inppos );
  820. #ifdef CONFIG_SBNI_MULTILINE
  821. skb->protocol = eth_type_trans( skb, nl->master );
  822. netif_rx( skb );
  823. dev->last_rx = jiffies;
  824. ++((struct net_local *) nl->master->priv)->stats.rx_packets;
  825. ((struct net_local *) nl->master->priv)->stats.rx_bytes += nl->inppos;
  826. #else
  827. skb->protocol = eth_type_trans( skb, dev );
  828. netif_rx( skb );
  829. dev->last_rx = jiffies;
  830. ++nl->stats.rx_packets;
  831. nl->stats.rx_bytes += nl->inppos;
  832. #endif
  833. nl->rx_buf_p = NULL; /* protocol driver will clear this sk_buff */
  834. }
  835. /* -------------------------------------------------------------------------- */
  836. /*
  837. * Routine checks periodically wire activity and regenerates marker if
  838. * connect was inactive for a long time.
  839. */
  840. static void
  841. sbni_watchdog( unsigned long arg )
  842. {
  843. struct net_device *dev = (struct net_device *) arg;
  844. struct net_local *nl = (struct net_local *) dev->priv;
  845. struct timer_list *w = &nl->watchdog;
  846. unsigned long flags;
  847. unsigned char csr0;
  848. spin_lock_irqsave( &nl->lock, flags );
  849. csr0 = inb( dev->base_addr + CSR0 );
  850. if( csr0 & RC_CHK ) {
  851. if( nl->timer_ticks ) {
  852. if( csr0 & (RC_RDY | BU_EMP) )
  853. /* receiving not active */
  854. nl->timer_ticks--;
  855. } else {
  856. nl->in_stats.timeout_number++;
  857. if( nl->delta_rxl )
  858. timeout_change_level( dev );
  859. outb( *(u_char *)&nl->csr1 | PR_RES,
  860. dev->base_addr + CSR1 );
  861. csr0 = inb( dev->base_addr + CSR0 );
  862. }
  863. } else
  864. nl->state &= ~FL_LINE_DOWN;
  865. outb( csr0 | RC_CHK, dev->base_addr + CSR0 );
  866. init_timer( w );
  867. w->expires = jiffies + SBNI_TIMEOUT;
  868. w->data = arg;
  869. w->function = sbni_watchdog;
  870. add_timer( w );
  871. spin_unlock_irqrestore( &nl->lock, flags );
  872. }
  873. static unsigned char rxl_tab[] = {
  874. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08,
  875. 0x0a, 0x0c, 0x0f, 0x16, 0x18, 0x1a, 0x1c, 0x1f
  876. };
  877. #define SIZE_OF_TIMEOUT_RXL_TAB 4
  878. static unsigned char timeout_rxl_tab[] = {
  879. 0x03, 0x05, 0x08, 0x0b
  880. };
  881. /* -------------------------------------------------------------------------- */
  882. static void
  883. card_start( struct net_device *dev )
  884. {
  885. struct net_local *nl = (struct net_local *) dev->priv;
  886. nl->timer_ticks = CHANGE_LEVEL_START_TICKS;
  887. nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
  888. nl->state |= FL_PREV_OK;
  889. nl->inppos = nl->outpos = 0;
  890. nl->wait_frameno = 0;
  891. nl->tx_frameno = 0;
  892. nl->framelen = 0;
  893. outb( *(u_char *)&nl->csr1 | PR_RES, dev->base_addr + CSR1 );
  894. outb( EN_INT, dev->base_addr + CSR0 );
  895. }
  896. /* -------------------------------------------------------------------------- */
  897. /* Receive level auto-selection */
  898. static void
  899. change_level( struct net_device *dev )
  900. {
  901. struct net_local *nl = (struct net_local *) dev->priv;
  902. if( nl->delta_rxl == 0 ) /* do not auto-negotiate RxL */
  903. return;
  904. if( nl->cur_rxl_index == 0 )
  905. nl->delta_rxl = 1;
  906. else if( nl->cur_rxl_index == 15 )
  907. nl->delta_rxl = -1;
  908. else if( nl->cur_rxl_rcvd < nl->prev_rxl_rcvd )
  909. nl->delta_rxl = -nl->delta_rxl;
  910. nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index += nl->delta_rxl ];
  911. inb( dev->base_addr + CSR0 ); /* needs for PCI cards */
  912. outb( *(u8 *)&nl->csr1, dev->base_addr + CSR1 );
  913. nl->prev_rxl_rcvd = nl->cur_rxl_rcvd;
  914. nl->cur_rxl_rcvd = 0;
  915. }
  916. static void
  917. timeout_change_level( struct net_device *dev )
  918. {
  919. struct net_local *nl = (struct net_local *) dev->priv;
  920. nl->cur_rxl_index = timeout_rxl_tab[ nl->timeout_rxl ];
  921. if( ++nl->timeout_rxl >= 4 )
  922. nl->timeout_rxl = 0;
  923. nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index ];
  924. inb( dev->base_addr + CSR0 );
  925. outb( *(unsigned char *)&nl->csr1, dev->base_addr + CSR1 );
  926. nl->prev_rxl_rcvd = nl->cur_rxl_rcvd;
  927. nl->cur_rxl_rcvd = 0;
  928. }
  929. /* -------------------------------------------------------------------------- */
  930. /*
  931. * Open/initialize the board.
  932. */
  933. static int
  934. sbni_open( struct net_device *dev )
  935. {
  936. struct net_local *nl = (struct net_local *) dev->priv;
  937. struct timer_list *w = &nl->watchdog;
  938. /*
  939. * For double ISA adapters within "common irq" mode, we have to
  940. * determine whether primary or secondary channel is initialized,
  941. * and set the irq handler only in first case.
  942. */
  943. if( dev->base_addr < 0x400 ) { /* ISA only */
  944. struct net_device **p = sbni_cards;
  945. for( ; *p && p < sbni_cards + SBNI_MAX_NUM_CARDS; ++p )
  946. if( (*p)->irq == dev->irq
  947. && ((*p)->base_addr == dev->base_addr + 4
  948. || (*p)->base_addr == dev->base_addr - 4)
  949. && (*p)->flags & IFF_UP ) {
  950. ((struct net_local *) ((*p)->priv))
  951. ->second = dev;
  952. printk( KERN_NOTICE "%s: using shared irq "
  953. "with %s\n", dev->name, (*p)->name );
  954. nl->state |= FL_SECONDARY;
  955. goto handler_attached;
  956. }
  957. }
  958. if( request_irq(dev->irq, sbni_interrupt, IRQF_SHARED, dev->name, dev) ) {
  959. printk( KERN_ERR "%s: unable to get IRQ %d.\n",
  960. dev->name, dev->irq );
  961. return -EAGAIN;
  962. }
  963. handler_attached:
  964. spin_lock( &nl->lock );
  965. memset( &nl->stats, 0, sizeof(struct net_device_stats) );
  966. memset( &nl->in_stats, 0, sizeof(struct sbni_in_stats) );
  967. card_start( dev );
  968. netif_start_queue( dev );
  969. /* set timer watchdog */
  970. init_timer( w );
  971. w->expires = jiffies + SBNI_TIMEOUT;
  972. w->data = (unsigned long) dev;
  973. w->function = sbni_watchdog;
  974. add_timer( w );
  975. spin_unlock( &nl->lock );
  976. return 0;
  977. }
  978. static int
  979. sbni_close( struct net_device *dev )
  980. {
  981. struct net_local *nl = (struct net_local *) dev->priv;
  982. if( nl->second && nl->second->flags & IFF_UP ) {
  983. printk( KERN_NOTICE "Secondary channel (%s) is active!\n",
  984. nl->second->name );
  985. return -EBUSY;
  986. }
  987. #ifdef CONFIG_SBNI_MULTILINE
  988. if( nl->state & FL_SLAVE )
  989. emancipate( dev );
  990. else
  991. while( nl->link ) /* it's master device! */
  992. emancipate( nl->link );
  993. #endif
  994. spin_lock( &nl->lock );
  995. nl->second = NULL;
  996. drop_xmit_queue( dev );
  997. netif_stop_queue( dev );
  998. del_timer( &nl->watchdog );
  999. outb( 0, dev->base_addr + CSR0 );
  1000. if( !(nl->state & FL_SECONDARY) )
  1001. free_irq( dev->irq, dev );
  1002. nl->state &= FL_SECONDARY;
  1003. spin_unlock( &nl->lock );
  1004. return 0;
  1005. }
  1006. /*
  1007. Valid combinations in CSR0 (for probing):
  1008. VALID_DECODER 0000,0011,1011,1010
  1009. ; 0 ; -
  1010. TR_REQ ; 1 ; +
  1011. TR_RDY ; 2 ; -
  1012. TR_RDY TR_REQ ; 3 ; +
  1013. BU_EMP ; 4 ; +
  1014. BU_EMP TR_REQ ; 5 ; +
  1015. BU_EMP TR_RDY ; 6 ; -
  1016. BU_EMP TR_RDY TR_REQ ; 7 ; +
  1017. RC_RDY ; 8 ; +
  1018. RC_RDY TR_REQ ; 9 ; +
  1019. RC_RDY TR_RDY ; 10 ; -
  1020. RC_RDY TR_RDY TR_REQ ; 11 ; -
  1021. RC_RDY BU_EMP ; 12 ; -
  1022. RC_RDY BU_EMP TR_REQ ; 13 ; -
  1023. RC_RDY BU_EMP TR_RDY ; 14 ; -
  1024. RC_RDY BU_EMP TR_RDY TR_REQ ; 15 ; -
  1025. */
  1026. #define VALID_DECODER (2 + 8 + 0x10 + 0x20 + 0x80 + 0x100 + 0x200)
  1027. static int
  1028. sbni_card_probe( unsigned long ioaddr )
  1029. {
  1030. unsigned char csr0;
  1031. csr0 = inb( ioaddr + CSR0 );
  1032. if( csr0 != 0xff && csr0 != 0x00 ) {
  1033. csr0 &= ~EN_INT;
  1034. if( csr0 & BU_EMP )
  1035. csr0 |= EN_INT;
  1036. if( VALID_DECODER & (1 << (csr0 >> 4)) )
  1037. return 0;
  1038. }
  1039. return -ENODEV;
  1040. }
  1041. /* -------------------------------------------------------------------------- */
  1042. static int
  1043. sbni_ioctl( struct net_device *dev, struct ifreq *ifr, int cmd )
  1044. {
  1045. struct net_local *nl = (struct net_local *) dev->priv;
  1046. struct sbni_flags flags;
  1047. int error = 0;
  1048. #ifdef CONFIG_SBNI_MULTILINE
  1049. struct net_device *slave_dev;
  1050. char slave_name[ 8 ];
  1051. #endif
  1052. switch( cmd ) {
  1053. case SIOCDEVGETINSTATS :
  1054. if (copy_to_user( ifr->ifr_data, &nl->in_stats,
  1055. sizeof(struct sbni_in_stats) ))
  1056. error = -EFAULT;
  1057. break;
  1058. case SIOCDEVRESINSTATS :
  1059. if( current->euid != 0 ) /* root only */
  1060. return -EPERM;
  1061. memset( &nl->in_stats, 0, sizeof(struct sbni_in_stats) );
  1062. break;
  1063. case SIOCDEVGHWSTATE :
  1064. flags.mac_addr = *(u32 *)(dev->dev_addr + 3);
  1065. flags.rate = nl->csr1.rate;
  1066. flags.slow_mode = (nl->state & FL_SLOW_MODE) != 0;
  1067. flags.rxl = nl->cur_rxl_index;
  1068. flags.fixed_rxl = nl->delta_rxl == 0;
  1069. if (copy_to_user( ifr->ifr_data, &flags, sizeof flags ))
  1070. error = -EFAULT;
  1071. break;
  1072. case SIOCDEVSHWSTATE :
  1073. if( current->euid != 0 ) /* root only */
  1074. return -EPERM;
  1075. spin_lock( &nl->lock );
  1076. flags = *(struct sbni_flags*) &ifr->ifr_ifru;
  1077. if( flags.fixed_rxl )
  1078. nl->delta_rxl = 0,
  1079. nl->cur_rxl_index = flags.rxl;
  1080. else
  1081. nl->delta_rxl = DEF_RXL_DELTA,
  1082. nl->cur_rxl_index = DEF_RXL;
  1083. nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index ];
  1084. nl->csr1.rate = flags.rate;
  1085. outb( *(u8 *)&nl->csr1 | PR_RES, dev->base_addr + CSR1 );
  1086. spin_unlock( &nl->lock );
  1087. break;
  1088. #ifdef CONFIG_SBNI_MULTILINE
  1089. case SIOCDEVENSLAVE :
  1090. if( current->euid != 0 ) /* root only */
  1091. return -EPERM;
  1092. if (copy_from_user( slave_name, ifr->ifr_data, sizeof slave_name ))
  1093. return -EFAULT;
  1094. slave_dev = dev_get_by_name( slave_name );
  1095. if( !slave_dev || !(slave_dev->flags & IFF_UP) ) {
  1096. printk( KERN_ERR "%s: trying to enslave non-active "
  1097. "device %s\n", dev->name, slave_name );
  1098. return -EPERM;
  1099. }
  1100. return enslave( dev, slave_dev );
  1101. case SIOCDEVEMANSIPATE :
  1102. if( current->euid != 0 ) /* root only */
  1103. return -EPERM;
  1104. return emancipate( dev );
  1105. #endif /* CONFIG_SBNI_MULTILINE */
  1106. default :
  1107. return -EOPNOTSUPP;
  1108. }
  1109. return error;
  1110. }
  1111. #ifdef CONFIG_SBNI_MULTILINE
  1112. static int
  1113. enslave( struct net_device *dev, struct net_device *slave_dev )
  1114. {
  1115. struct net_local *nl = (struct net_local *) dev->priv;
  1116. struct net_local *snl = (struct net_local *) slave_dev->priv;
  1117. if( nl->state & FL_SLAVE ) /* This isn't master or free device */
  1118. return -EBUSY;
  1119. if( snl->state & FL_SLAVE ) /* That was already enslaved */
  1120. return -EBUSY;
  1121. spin_lock( &nl->lock );
  1122. spin_lock( &snl->lock );
  1123. /* append to list */
  1124. snl->link = nl->link;
  1125. nl->link = slave_dev;
  1126. snl->master = dev;
  1127. snl->state |= FL_SLAVE;
  1128. /* Summary statistics of MultiLine operation will be stored
  1129. in master's counters */
  1130. memset( &snl->stats, 0, sizeof(struct net_device_stats) );
  1131. netif_stop_queue( slave_dev );
  1132. netif_wake_queue( dev ); /* Now we are able to transmit */
  1133. spin_unlock( &snl->lock );
  1134. spin_unlock( &nl->lock );
  1135. printk( KERN_NOTICE "%s: slave device (%s) attached.\n",
  1136. dev->name, slave_dev->name );
  1137. return 0;
  1138. }
  1139. static int
  1140. emancipate( struct net_device *dev )
  1141. {
  1142. struct net_local *snl = (struct net_local *) dev->priv;
  1143. struct net_device *p = snl->master;
  1144. struct net_local *nl = (struct net_local *) p->priv;
  1145. if( !(snl->state & FL_SLAVE) )
  1146. return -EINVAL;
  1147. spin_lock( &nl->lock );
  1148. spin_lock( &snl->lock );
  1149. drop_xmit_queue( dev );
  1150. /* exclude from list */
  1151. for(;;) { /* must be in list */
  1152. struct net_local *t = (struct net_local *) p->priv;
  1153. if( t->link == dev ) {
  1154. t->link = snl->link;
  1155. break;
  1156. }
  1157. p = t->link;
  1158. }
  1159. snl->link = NULL;
  1160. snl->master = dev;
  1161. snl->state &= ~FL_SLAVE;
  1162. netif_start_queue( dev );
  1163. spin_unlock( &snl->lock );
  1164. spin_unlock( &nl->lock );
  1165. dev_put( dev );
  1166. return 0;
  1167. }
  1168. #endif
  1169. static struct net_device_stats *
  1170. sbni_get_stats( struct net_device *dev )
  1171. {
  1172. return &((struct net_local *) dev->priv)->stats;
  1173. }
  1174. static void
  1175. set_multicast_list( struct net_device *dev )
  1176. {
  1177. return; /* sbni always operate in promiscuos mode */
  1178. }
  1179. #ifdef MODULE
  1180. module_param_array(io, int, NULL, 0);
  1181. module_param_array(irq, int, NULL, 0);
  1182. module_param_array(baud, int, NULL, 0);
  1183. module_param_array(rxl, int, NULL, 0);
  1184. module_param_array(mac, int, NULL, 0);
  1185. module_param(skip_pci_probe, bool, 0);
  1186. MODULE_LICENSE("GPL");
  1187. int __init init_module( void )
  1188. {
  1189. struct net_device *dev;
  1190. int err;
  1191. while( num < SBNI_MAX_NUM_CARDS ) {
  1192. dev = alloc_netdev(sizeof(struct net_local),
  1193. "sbni%d", sbni_devsetup);
  1194. if( !dev)
  1195. break;
  1196. sprintf( dev->name, "sbni%d", num );
  1197. err = sbni_init(dev);
  1198. if (err) {
  1199. free_netdev(dev);
  1200. break;
  1201. }
  1202. if( register_netdev( dev ) ) {
  1203. release_region( dev->base_addr, SBNI_IO_EXTENT );
  1204. free_netdev( dev );
  1205. break;
  1206. }
  1207. }
  1208. return *sbni_cards ? 0 : -ENODEV;
  1209. }
  1210. void
  1211. cleanup_module( void )
  1212. {
  1213. struct net_device *dev;
  1214. int num;
  1215. for( num = 0; num < SBNI_MAX_NUM_CARDS; ++num )
  1216. if( (dev = sbni_cards[ num ]) != NULL ) {
  1217. unregister_netdev( dev );
  1218. release_region( dev->base_addr, SBNI_IO_EXTENT );
  1219. free_netdev( dev );
  1220. }
  1221. }
  1222. #else /* MODULE */
  1223. static int __init
  1224. sbni_setup( char *p )
  1225. {
  1226. int n, parm;
  1227. if( *p++ != '(' )
  1228. goto bad_param;
  1229. for( n = 0, parm = 0; *p && n < 8; ) {
  1230. (*dest[ parm ])[ n ] = simple_strtol( p, &p, 0 );
  1231. if( !*p || *p == ')' )
  1232. return 1;
  1233. if( *p == ';' )
  1234. ++p, ++n, parm = 0;
  1235. else if( *p++ != ',' )
  1236. break;
  1237. else
  1238. if( ++parm >= 5 )
  1239. break;
  1240. }
  1241. bad_param:
  1242. printk( KERN_ERR "Error in sbni kernel parameter!\n" );
  1243. return 0;
  1244. }
  1245. __setup( "sbni=", sbni_setup );
  1246. #endif /* MODULE */
  1247. /* -------------------------------------------------------------------------- */
  1248. #ifdef ASM_CRC
  1249. static u32
  1250. calc_crc32( u32 crc, u8 *p, u32 len )
  1251. {
  1252. register u32 _crc;
  1253. _crc = crc;
  1254. __asm__ __volatile__ (
  1255. "xorl %%ebx, %%ebx\n"
  1256. "movl %2, %%esi\n"
  1257. "movl %3, %%ecx\n"
  1258. "movl $crc32tab, %%edi\n"
  1259. "shrl $2, %%ecx\n"
  1260. "jz 1f\n"
  1261. ".align 4\n"
  1262. "0:\n"
  1263. "movb %%al, %%bl\n"
  1264. "movl (%%esi), %%edx\n"
  1265. "shrl $8, %%eax\n"
  1266. "xorb %%dl, %%bl\n"
  1267. "shrl $8, %%edx\n"
  1268. "xorl (%%edi,%%ebx,4), %%eax\n"
  1269. "movb %%al, %%bl\n"
  1270. "shrl $8, %%eax\n"
  1271. "xorb %%dl, %%bl\n"
  1272. "shrl $8, %%edx\n"
  1273. "xorl (%%edi,%%ebx,4), %%eax\n"
  1274. "movb %%al, %%bl\n"
  1275. "shrl $8, %%eax\n"
  1276. "xorb %%dl, %%bl\n"
  1277. "movb %%dh, %%dl\n"
  1278. "xorl (%%edi,%%ebx,4), %%eax\n"
  1279. "movb %%al, %%bl\n"
  1280. "shrl $8, %%eax\n"
  1281. "xorb %%dl, %%bl\n"
  1282. "addl $4, %%esi\n"
  1283. "xorl (%%edi,%%ebx,4), %%eax\n"
  1284. "decl %%ecx\n"
  1285. "jnz 0b\n"
  1286. "1:\n"
  1287. "movl %3, %%ecx\n"
  1288. "andl $3, %%ecx\n"
  1289. "jz 2f\n"
  1290. "movb %%al, %%bl\n"
  1291. "shrl $8, %%eax\n"
  1292. "xorb (%%esi), %%bl\n"
  1293. "xorl (%%edi,%%ebx,4), %%eax\n"
  1294. "decl %%ecx\n"
  1295. "jz 2f\n"
  1296. "movb %%al, %%bl\n"
  1297. "shrl $8, %%eax\n"
  1298. "xorb 1(%%esi), %%bl\n"
  1299. "xorl (%%edi,%%ebx,4), %%eax\n"
  1300. "decl %%ecx\n"
  1301. "jz 2f\n"
  1302. "movb %%al, %%bl\n"
  1303. "shrl $8, %%eax\n"
  1304. "xorb 2(%%esi), %%bl\n"
  1305. "xorl (%%edi,%%ebx,4), %%eax\n"
  1306. "2:\n"
  1307. : "=a" (_crc)
  1308. : "0" (_crc), "g" (p), "g" (len)
  1309. : "bx", "cx", "dx", "si", "di"
  1310. );
  1311. return _crc;
  1312. }
  1313. #else /* ASM_CRC */
  1314. static u32
  1315. calc_crc32( u32 crc, u8 *p, u32 len )
  1316. {
  1317. while( len-- )
  1318. crc = CRC32( *p++, crc );
  1319. return crc;
  1320. }
  1321. #endif /* ASM_CRC */
  1322. static u32 crc32tab[] __attribute__ ((aligned(8))) = {
  1323. 0xD202EF8D, 0xA505DF1B, 0x3C0C8EA1, 0x4B0BBE37,
  1324. 0xD56F2B94, 0xA2681B02, 0x3B614AB8, 0x4C667A2E,
  1325. 0xDCD967BF, 0xABDE5729, 0x32D70693, 0x45D03605,
  1326. 0xDBB4A3A6, 0xACB39330, 0x35BAC28A, 0x42BDF21C,
  1327. 0xCFB5FFE9, 0xB8B2CF7F, 0x21BB9EC5, 0x56BCAE53,
  1328. 0xC8D83BF0, 0xBFDF0B66, 0x26D65ADC, 0x51D16A4A,
  1329. 0xC16E77DB, 0xB669474D, 0x2F6016F7, 0x58672661,
  1330. 0xC603B3C2, 0xB1048354, 0x280DD2EE, 0x5F0AE278,
  1331. 0xE96CCF45, 0x9E6BFFD3, 0x0762AE69, 0x70659EFF,
  1332. 0xEE010B5C, 0x99063BCA, 0x000F6A70, 0x77085AE6,
  1333. 0xE7B74777, 0x90B077E1, 0x09B9265B, 0x7EBE16CD,
  1334. 0xE0DA836E, 0x97DDB3F8, 0x0ED4E242, 0x79D3D2D4,
  1335. 0xF4DBDF21, 0x83DCEFB7, 0x1AD5BE0D, 0x6DD28E9B,
  1336. 0xF3B61B38, 0x84B12BAE, 0x1DB87A14, 0x6ABF4A82,
  1337. 0xFA005713, 0x8D076785, 0x140E363F, 0x630906A9,
  1338. 0xFD6D930A, 0x8A6AA39C, 0x1363F226, 0x6464C2B0,
  1339. 0xA4DEAE1D, 0xD3D99E8B, 0x4AD0CF31, 0x3DD7FFA7,
  1340. 0xA3B36A04, 0xD4B45A92, 0x4DBD0B28, 0x3ABA3BBE,
  1341. 0xAA05262F, 0xDD0216B9, 0x440B4703, 0x330C7795,
  1342. 0xAD68E236, 0xDA6FD2A0, 0x4366831A, 0x3461B38C,
  1343. 0xB969BE79, 0xCE6E8EEF, 0x5767DF55, 0x2060EFC3,
  1344. 0xBE047A60, 0xC9034AF6, 0x500A1B4C, 0x270D2BDA,
  1345. 0xB7B2364B, 0xC0B506DD, 0x59BC5767, 0x2EBB67F1,
  1346. 0xB0DFF252, 0xC7D8C2C4, 0x5ED1937E, 0x29D6A3E8,
  1347. 0x9FB08ED5, 0xE8B7BE43, 0x71BEEFF9, 0x06B9DF6F,
  1348. 0x98DD4ACC, 0xEFDA7A5A, 0x76D32BE0, 0x01D41B76,
  1349. 0x916B06E7, 0xE66C3671, 0x7F6567CB, 0x0862575D,
  1350. 0x9606C2FE, 0xE101F268, 0x7808A3D2, 0x0F0F9344,
  1351. 0x82079EB1, 0xF500AE27, 0x6C09FF9D, 0x1B0ECF0B,
  1352. 0x856A5AA8, 0xF26D6A3E, 0x6B643B84, 0x1C630B12,
  1353. 0x8CDC1683, 0xFBDB2615, 0x62D277AF, 0x15D54739,
  1354. 0x8BB1D29A, 0xFCB6E20C, 0x65BFB3B6, 0x12B88320,
  1355. 0x3FBA6CAD, 0x48BD5C3B, 0xD1B40D81, 0xA6B33D17,
  1356. 0x38D7A8B4, 0x4FD09822, 0xD6D9C998, 0xA1DEF90E,
  1357. 0x3161E49F, 0x4666D409, 0xDF6F85B3, 0xA868B525,
  1358. 0x360C2086, 0x410B1010, 0xD80241AA, 0xAF05713C,
  1359. 0x220D7CC9, 0x550A4C5F, 0xCC031DE5, 0xBB042D73,
  1360. 0x2560B8D0, 0x52678846, 0xCB6ED9FC, 0xBC69E96A,
  1361. 0x2CD6F4FB, 0x5BD1C46D, 0xC2D895D7, 0xB5DFA541,
  1362. 0x2BBB30E2, 0x5CBC0074, 0xC5B551CE, 0xB2B26158,
  1363. 0x04D44C65, 0x73D37CF3, 0xEADA2D49, 0x9DDD1DDF,
  1364. 0x03B9887C, 0x74BEB8EA, 0xEDB7E950, 0x9AB0D9C6,
  1365. 0x0A0FC457, 0x7D08F4C1, 0xE401A57B, 0x930695ED,
  1366. 0x0D62004E, 0x7A6530D8, 0xE36C6162, 0x946B51F4,
  1367. 0x19635C01, 0x6E646C97, 0xF76D3D2D, 0x806A0DBB,
  1368. 0x1E0E9818, 0x6909A88E, 0xF000F934, 0x8707C9A2,
  1369. 0x17B8D433, 0x60BFE4A5, 0xF9B6B51F, 0x8EB18589,
  1370. 0x10D5102A, 0x67D220BC, 0xFEDB7106, 0x89DC4190,
  1371. 0x49662D3D, 0x3E611DAB, 0xA7684C11, 0xD06F7C87,
  1372. 0x4E0BE924, 0x390CD9B2, 0xA0058808, 0xD702B89E,
  1373. 0x47BDA50F, 0x30BA9599, 0xA9B3C423, 0xDEB4F4B5,
  1374. 0x40D06116, 0x37D75180, 0xAEDE003A, 0xD9D930AC,
  1375. 0x54D13D59, 0x23D60DCF, 0xBADF5C75, 0xCDD86CE3,
  1376. 0x53BCF940, 0x24BBC9D6, 0xBDB2986C, 0xCAB5A8FA,
  1377. 0x5A0AB56B, 0x2D0D85FD, 0xB404D447, 0xC303E4D1,
  1378. 0x5D677172, 0x2A6041E4, 0xB369105E, 0xC46E20C8,
  1379. 0x72080DF5, 0x050F3D63, 0x9C066CD9, 0xEB015C4F,
  1380. 0x7565C9EC, 0x0262F97A, 0x9B6BA8C0, 0xEC6C9856,
  1381. 0x7CD385C7, 0x0BD4B551, 0x92DDE4EB, 0xE5DAD47D,
  1382. 0x7BBE41DE, 0x0CB97148, 0x95B020F2, 0xE2B71064,
  1383. 0x6FBF1D91, 0x18B82D07, 0x81B17CBD, 0xF6B64C2B,
  1384. 0x68D2D988, 0x1FD5E91E, 0x86DCB8A4, 0xF1DB8832,
  1385. 0x616495A3, 0x1663A535, 0x8F6AF48F, 0xF86DC419,
  1386. 0x660951BA, 0x110E612C, 0x88073096, 0xFF000000
  1387. };