sbni.c 42 KB

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