sbni.c 43 KB

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