sbni.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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/config.h>
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/ptrace.h>
  43. #include <linux/fcntl.h>
  44. #include <linux/ioport.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/slab.h>
  47. #include <linux/string.h>
  48. #include <linux/errno.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/pci.h>
  52. #include <linux/skbuff.h>
  53. #include <linux/timer.h>
  54. #include <linux/init.h>
  55. #include <linux/delay.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 *, struct pt_regs * );
  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. /*
  161. * Look for SBNI card which addr stored in dev->base_addr, if nonzero.
  162. * Otherwise, look through PCI bus. If none PCI-card was found, scan ISA.
  163. */
  164. static inline int __init
  165. sbni_isa_probe( struct net_device *dev )
  166. {
  167. if( dev->base_addr > 0x1ff
  168. && request_region( dev->base_addr, SBNI_IO_EXTENT, dev->name )
  169. && sbni_probe1( dev, dev->base_addr, dev->irq ) )
  170. return 0;
  171. else {
  172. printk( KERN_ERR "sbni: base address 0x%lx is busy, or adapter "
  173. "is malfunctional!\n", dev->base_addr );
  174. return -ENODEV;
  175. }
  176. }
  177. static void __init sbni_devsetup(struct net_device *dev)
  178. {
  179. ether_setup( dev );
  180. dev->open = &sbni_open;
  181. dev->stop = &sbni_close;
  182. dev->hard_start_xmit = &sbni_start_xmit;
  183. dev->get_stats = &sbni_get_stats;
  184. dev->set_multicast_list = &set_multicast_list;
  185. dev->do_ioctl = &sbni_ioctl;
  186. SET_MODULE_OWNER( dev );
  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. 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. /* Allocate dev->priv and fill in sbni-specific dev fields. */
  319. nl = dev->priv;
  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. dev->priv = nl;
  326. memset( nl, 0, sizeof(struct net_local) );
  327. spin_lock_init( &nl->lock );
  328. /* store MAC address (generate if that isn't known) */
  329. *(u16 *)dev->dev_addr = htons( 0x00ff );
  330. *(u32 *)(dev->dev_addr + 2) = htonl( 0x01000000 |
  331. ( (mac[num] ? mac[num] : (u32)((long)dev->priv)) & 0x00ffffff) );
  332. /* store link settings (speed, receive level ) */
  333. nl->maxframe = DEFAULT_FRAME_LEN;
  334. nl->csr1.rate = baud[ num ];
  335. if( (nl->cur_rxl_index = rxl[ num ]) == -1 )
  336. /* autotune rxl */
  337. nl->cur_rxl_index = DEF_RXL,
  338. nl->delta_rxl = DEF_RXL_DELTA;
  339. else
  340. nl->delta_rxl = 0;
  341. nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index ];
  342. if( inb( ioaddr + CSR0 ) & 0x01 )
  343. nl->state |= FL_SLOW_MODE;
  344. printk( KERN_NOTICE "%s: ioaddr %#lx, irq %d, "
  345. "MAC: 00:ff:01:%02x:%02x:%02x\n",
  346. dev->name, dev->base_addr, dev->irq,
  347. ((u8 *) dev->dev_addr) [3],
  348. ((u8 *) dev->dev_addr) [4],
  349. ((u8 *) dev->dev_addr) [5] );
  350. printk( KERN_NOTICE "%s: speed %d, receive level ", dev->name,
  351. ( (nl->state & FL_SLOW_MODE) ? 500000 : 2000000)
  352. / (1 << nl->csr1.rate) );
  353. if( nl->delta_rxl == 0 )
  354. printk( "0x%x (fixed)\n", nl->cur_rxl_index );
  355. else
  356. printk( "(auto)\n");
  357. #ifdef CONFIG_SBNI_MULTILINE
  358. nl->master = dev;
  359. nl->link = NULL;
  360. #endif
  361. sbni_cards[ num++ ] = dev;
  362. return dev;
  363. }
  364. /* -------------------------------------------------------------------------- */
  365. #ifdef CONFIG_SBNI_MULTILINE
  366. static int
  367. sbni_start_xmit( struct sk_buff *skb, struct net_device *dev )
  368. {
  369. struct net_device *p;
  370. netif_stop_queue( dev );
  371. /* Looking for idle device in the list */
  372. for( p = dev; p; ) {
  373. struct net_local *nl = (struct net_local *) p->priv;
  374. spin_lock( &nl->lock );
  375. if( nl->tx_buf_p || (nl->state & FL_LINE_DOWN) ) {
  376. p = nl->link;
  377. spin_unlock( &nl->lock );
  378. } else {
  379. /* Idle dev is found */
  380. prepare_to_send( skb, p );
  381. spin_unlock( &nl->lock );
  382. netif_start_queue( dev );
  383. return 0;
  384. }
  385. }
  386. return 1;
  387. }
  388. #else /* CONFIG_SBNI_MULTILINE */
  389. static int
  390. sbni_start_xmit( struct sk_buff *skb, struct net_device *dev )
  391. {
  392. struct net_local *nl = (struct net_local *) dev->priv;
  393. netif_stop_queue( dev );
  394. spin_lock( &nl->lock );
  395. prepare_to_send( skb, dev );
  396. spin_unlock( &nl->lock );
  397. return 0;
  398. }
  399. #endif /* CONFIG_SBNI_MULTILINE */
  400. /* -------------------------------------------------------------------------- */
  401. /* interrupt handler */
  402. /*
  403. * SBNI12D-10, -11/ISA boards within "common interrupt" mode could not
  404. * be looked as two independent single-channel devices. Every channel seems
  405. * as Ethernet interface but interrupt handler must be common. Really, first
  406. * channel ("master") driver only registers the handler. In its struct net_local
  407. * it has got pointer to "slave" channel's struct net_local and handles that's
  408. * interrupts too.
  409. * dev of successfully attached ISA SBNI boards is linked to list.
  410. * While next board driver is initialized, it scans this list. If one
  411. * has found dev with same irq and ioaddr different by 4 then it assumes
  412. * this board to be "master".
  413. */
  414. static irqreturn_t
  415. sbni_interrupt( int irq, void *dev_id, struct pt_regs *regs )
  416. {
  417. struct net_device *dev = (struct net_device *) dev_id;
  418. struct net_local *nl = (struct net_local *) dev->priv;
  419. int repeat;
  420. spin_lock( &nl->lock );
  421. if( nl->second )
  422. spin_lock( &((struct net_local *) nl->second->priv)->lock );
  423. do {
  424. repeat = 0;
  425. if( inb( dev->base_addr + CSR0 ) & (RC_RDY | TR_RDY) )
  426. handle_channel( dev ),
  427. repeat = 1;
  428. if( nl->second && /* second channel present */
  429. (inb( nl->second->base_addr+CSR0 ) & (RC_RDY | TR_RDY)) )
  430. handle_channel( nl->second ),
  431. repeat = 1;
  432. } while( repeat );
  433. if( nl->second )
  434. spin_unlock( &((struct net_local *)nl->second->priv)->lock );
  435. spin_unlock( &nl->lock );
  436. return IRQ_HANDLED;
  437. }
  438. static void
  439. handle_channel( struct net_device *dev )
  440. {
  441. struct net_local *nl = (struct net_local *) dev->priv;
  442. unsigned long ioaddr = dev->base_addr;
  443. int req_ans;
  444. unsigned char csr0;
  445. #ifdef CONFIG_SBNI_MULTILINE
  446. /* Lock the master device because we going to change its local data */
  447. if( nl->state & FL_SLAVE )
  448. spin_lock( &((struct net_local *) nl->master->priv)->lock );
  449. #endif
  450. outb( (inb( ioaddr + CSR0 ) & ~EN_INT) | TR_REQ, ioaddr + CSR0 );
  451. nl->timer_ticks = CHANGE_LEVEL_START_TICKS;
  452. for(;;) {
  453. csr0 = inb( ioaddr + CSR0 );
  454. if( ( csr0 & (RC_RDY | TR_RDY) ) == 0 )
  455. break;
  456. req_ans = !(nl->state & FL_PREV_OK);
  457. if( csr0 & RC_RDY )
  458. req_ans = recv_frame( dev );
  459. /*
  460. * TR_RDY always equals 1 here because we have owned the marker,
  461. * and we set TR_REQ when disabled interrupts
  462. */
  463. csr0 = inb( ioaddr + CSR0 );
  464. if( !(csr0 & TR_RDY) || (csr0 & RC_RDY) )
  465. printk( KERN_ERR "%s: internal error!\n", dev->name );
  466. /* if state & FL_NEED_RESEND != 0 then tx_frameno != 0 */
  467. if( req_ans || nl->tx_frameno != 0 )
  468. send_frame( dev );
  469. else
  470. /* send marker without any data */
  471. outb( inb( ioaddr + CSR0 ) & ~TR_REQ, ioaddr + CSR0 );
  472. }
  473. outb( inb( ioaddr + CSR0 ) | EN_INT, ioaddr + CSR0 );
  474. #ifdef CONFIG_SBNI_MULTILINE
  475. if( nl->state & FL_SLAVE )
  476. spin_unlock( &((struct net_local *) nl->master->priv)->lock );
  477. #endif
  478. }
  479. /*
  480. * Routine returns 1 if it need to acknoweledge received frame.
  481. * Empty frame received without errors won't be acknoweledged.
  482. */
  483. static int
  484. recv_frame( struct net_device *dev )
  485. {
  486. struct net_local *nl = (struct net_local *) dev->priv;
  487. unsigned long ioaddr = dev->base_addr;
  488. u32 crc = CRC32_INITIAL;
  489. unsigned framelen, frameno, ack;
  490. unsigned is_first, frame_ok;
  491. if( check_fhdr( ioaddr, &framelen, &frameno, &ack, &is_first, &crc ) ) {
  492. frame_ok = framelen > 4
  493. ? upload_data( dev, framelen, frameno, is_first, crc )
  494. : skip_tail( ioaddr, framelen, crc );
  495. if( frame_ok )
  496. interpret_ack( dev, ack );
  497. } else
  498. frame_ok = 0;
  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 = (struct net_local *) dev->priv;
  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 = (struct net_local *) dev->priv;
  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 = (struct net_local *) dev->priv;
  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 *) nl->master->priv)
  591. ->stats.rx_errors++,
  592. ((struct net_local *) nl->master->priv)
  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 *) nl->master->priv)->stats.rx_errors++,
  609. ((struct net_local *) nl->master->priv)->stats.rx_crc_errors++;
  610. #else
  611. nl->stats.rx_errors++,
  612. nl->stats.rx_crc_errors++;
  613. #endif
  614. return frame_ok;
  615. }
  616. static __inline void
  617. send_complete( struct net_local *nl )
  618. {
  619. #ifdef CONFIG_SBNI_MULTILINE
  620. ((struct net_local *) nl->master->priv)->stats.tx_packets++;
  621. ((struct net_local *) nl->master->priv)->stats.tx_bytes
  622. += nl->tx_buf_p->len;
  623. #else
  624. nl->stats.tx_packets++;
  625. nl->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 = (struct net_local *) dev->priv;
  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( nl ),
  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 = (struct net_local *) dev->priv;
  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 = (struct net_local *) dev->priv;
  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 = (len + nl->maxframe - 1) / 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 = (struct net_local *) dev->priv;
  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. ((struct net_local *) nl->master->priv)
  714. ->stats.tx_errors++,
  715. ((struct net_local *) nl->master->priv)
  716. ->stats.tx_carrier_errors++;
  717. #else
  718. nl->stats.tx_errors++,
  719. nl->stats.tx_carrier_errors++;
  720. #endif
  721. nl->tx_frameno = 0;
  722. nl->framelen = 0;
  723. nl->outpos = 0;
  724. nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
  725. #ifdef CONFIG_SBNI_MULTILINE
  726. netif_start_queue( nl->master );
  727. nl->master->trans_start = jiffies;
  728. #else
  729. netif_start_queue( dev );
  730. dev->trans_start = jiffies;
  731. #endif
  732. }
  733. static void
  734. send_frame_header( struct net_device *dev, u32 *crc_p )
  735. {
  736. struct net_local *nl = (struct net_local *) dev->priv;
  737. u32 crc = *crc_p;
  738. u32 len_field = nl->framelen + 6; /* CRC + frameno + reserved */
  739. u8 value;
  740. if( nl->state & FL_NEED_RESEND )
  741. len_field |= FRAME_RETRY; /* non-first attempt... */
  742. if( nl->outpos == 0 )
  743. len_field |= FRAME_FIRST;
  744. len_field |= (nl->state & FL_PREV_OK) ? FRAME_SENT_OK : FRAME_SENT_BAD;
  745. outb( SBNI_SIG, dev->base_addr + DAT );
  746. value = (u8) len_field;
  747. outb( value, dev->base_addr + DAT );
  748. crc = CRC32( value, crc );
  749. value = (u8) (len_field >> 8);
  750. outb( value, dev->base_addr + DAT );
  751. crc = CRC32( value, crc );
  752. outb( nl->tx_frameno, dev->base_addr + DAT );
  753. crc = CRC32( nl->tx_frameno, crc );
  754. outb( 0, dev->base_addr + DAT );
  755. crc = CRC32( 0, crc );
  756. *crc_p = crc;
  757. }
  758. /*
  759. * if frame tail not needed (incorrect number or received twice),
  760. * it won't store, but CRC will be calculated
  761. */
  762. static int
  763. skip_tail( unsigned int ioaddr, unsigned int tail_len, u32 crc )
  764. {
  765. while( tail_len-- )
  766. crc = CRC32( inb( ioaddr + DAT ), crc );
  767. return crc == CRC32_REMAINDER;
  768. }
  769. /*
  770. * Preliminary checks if frame header is correct, calculates its CRC
  771. * and split it to simple fields
  772. */
  773. static int
  774. check_fhdr( u32 ioaddr, u32 *framelen, u32 *frameno, u32 *ack,
  775. u32 *is_first, u32 *crc_p )
  776. {
  777. u32 crc = *crc_p;
  778. u8 value;
  779. if( inb( ioaddr + DAT ) != SBNI_SIG )
  780. return 0;
  781. value = inb( ioaddr + DAT );
  782. *framelen = (u32)value;
  783. crc = CRC32( value, crc );
  784. value = inb( ioaddr + DAT );
  785. *framelen |= ((u32)value) << 8;
  786. crc = CRC32( value, crc );
  787. *ack = *framelen & FRAME_ACK_MASK;
  788. *is_first = (*framelen & FRAME_FIRST) != 0;
  789. if( (*framelen &= FRAME_LEN_MASK) < 6
  790. || *framelen > SBNI_MAX_FRAME - 3 )
  791. return 0;
  792. value = inb( ioaddr + DAT );
  793. *frameno = (u32)value;
  794. crc = CRC32( value, crc );
  795. crc = CRC32( inb( ioaddr + DAT ), crc ); /* reserved byte */
  796. *framelen -= 2;
  797. *crc_p = crc;
  798. return 1;
  799. }
  800. static struct sk_buff *
  801. get_rx_buf( struct net_device *dev )
  802. {
  803. /* +2 is to compensate for the alignment fixup below */
  804. struct sk_buff *skb = dev_alloc_skb( ETHER_MAX_LEN + 2 );
  805. if( !skb )
  806. return NULL;
  807. #ifdef CONFIG_SBNI_MULTILINE
  808. skb->dev = ((struct net_local *) dev->priv)->master;
  809. #else
  810. skb->dev = dev;
  811. #endif
  812. skb_reserve( skb, 2 ); /* Align IP on longword boundaries */
  813. return skb;
  814. }
  815. static void
  816. indicate_pkt( struct net_device *dev )
  817. {
  818. struct net_local *nl = (struct net_local *) dev->priv;
  819. struct sk_buff *skb = nl->rx_buf_p;
  820. skb_put( skb, nl->inppos );
  821. #ifdef CONFIG_SBNI_MULTILINE
  822. skb->protocol = eth_type_trans( skb, nl->master );
  823. netif_rx( skb );
  824. dev->last_rx = jiffies;
  825. ++((struct net_local *) nl->master->priv)->stats.rx_packets;
  826. ((struct net_local *) nl->master->priv)->stats.rx_bytes += nl->inppos;
  827. #else
  828. skb->protocol = eth_type_trans( skb, dev );
  829. netif_rx( skb );
  830. dev->last_rx = jiffies;
  831. ++nl->stats.rx_packets;
  832. nl->stats.rx_bytes += nl->inppos;
  833. #endif
  834. nl->rx_buf_p = NULL; /* protocol driver will clear this sk_buff */
  835. }
  836. /* -------------------------------------------------------------------------- */
  837. /*
  838. * Routine checks periodically wire activity and regenerates marker if
  839. * connect was inactive for a long time.
  840. */
  841. static void
  842. sbni_watchdog( unsigned long arg )
  843. {
  844. struct net_device *dev = (struct net_device *) arg;
  845. struct net_local *nl = (struct net_local *) dev->priv;
  846. struct timer_list *w = &nl->watchdog;
  847. unsigned long flags;
  848. unsigned char csr0;
  849. spin_lock_irqsave( &nl->lock, flags );
  850. csr0 = inb( dev->base_addr + CSR0 );
  851. if( csr0 & RC_CHK ) {
  852. if( nl->timer_ticks ) {
  853. if( csr0 & (RC_RDY | BU_EMP) )
  854. /* receiving not active */
  855. nl->timer_ticks--;
  856. } else {
  857. nl->in_stats.timeout_number++;
  858. if( nl->delta_rxl )
  859. timeout_change_level( dev );
  860. outb( *(u_char *)&nl->csr1 | PR_RES,
  861. dev->base_addr + CSR1 );
  862. csr0 = inb( dev->base_addr + CSR0 );
  863. }
  864. } else
  865. nl->state &= ~FL_LINE_DOWN;
  866. outb( csr0 | RC_CHK, dev->base_addr + CSR0 );
  867. init_timer( w );
  868. w->expires = jiffies + SBNI_TIMEOUT;
  869. w->data = arg;
  870. w->function = sbni_watchdog;
  871. add_timer( w );
  872. spin_unlock_irqrestore( &nl->lock, flags );
  873. }
  874. static unsigned char rxl_tab[] = {
  875. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08,
  876. 0x0a, 0x0c, 0x0f, 0x16, 0x18, 0x1a, 0x1c, 0x1f
  877. };
  878. #define SIZE_OF_TIMEOUT_RXL_TAB 4
  879. static unsigned char timeout_rxl_tab[] = {
  880. 0x03, 0x05, 0x08, 0x0b
  881. };
  882. /* -------------------------------------------------------------------------- */
  883. static void
  884. card_start( struct net_device *dev )
  885. {
  886. struct net_local *nl = (struct net_local *) dev->priv;
  887. nl->timer_ticks = CHANGE_LEVEL_START_TICKS;
  888. nl->state &= ~(FL_WAIT_ACK | FL_NEED_RESEND);
  889. nl->state |= FL_PREV_OK;
  890. nl->inppos = nl->outpos = 0;
  891. nl->wait_frameno = 0;
  892. nl->tx_frameno = 0;
  893. nl->framelen = 0;
  894. outb( *(u_char *)&nl->csr1 | PR_RES, dev->base_addr + CSR1 );
  895. outb( EN_INT, dev->base_addr + CSR0 );
  896. }
  897. /* -------------------------------------------------------------------------- */
  898. /* Receive level auto-selection */
  899. static void
  900. change_level( struct net_device *dev )
  901. {
  902. struct net_local *nl = (struct net_local *) dev->priv;
  903. if( nl->delta_rxl == 0 ) /* do not auto-negotiate RxL */
  904. return;
  905. if( nl->cur_rxl_index == 0 )
  906. nl->delta_rxl = 1;
  907. else if( nl->cur_rxl_index == 15 )
  908. nl->delta_rxl = -1;
  909. else if( nl->cur_rxl_rcvd < nl->prev_rxl_rcvd )
  910. nl->delta_rxl = -nl->delta_rxl;
  911. nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index += nl->delta_rxl ];
  912. inb( dev->base_addr + CSR0 ); /* needs for PCI cards */
  913. outb( *(u8 *)&nl->csr1, dev->base_addr + CSR1 );
  914. nl->prev_rxl_rcvd = nl->cur_rxl_rcvd;
  915. nl->cur_rxl_rcvd = 0;
  916. }
  917. static void
  918. timeout_change_level( struct net_device *dev )
  919. {
  920. struct net_local *nl = (struct net_local *) dev->priv;
  921. nl->cur_rxl_index = timeout_rxl_tab[ nl->timeout_rxl ];
  922. if( ++nl->timeout_rxl >= 4 )
  923. nl->timeout_rxl = 0;
  924. nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index ];
  925. inb( dev->base_addr + CSR0 );
  926. outb( *(unsigned char *)&nl->csr1, dev->base_addr + CSR1 );
  927. nl->prev_rxl_rcvd = nl->cur_rxl_rcvd;
  928. nl->cur_rxl_rcvd = 0;
  929. }
  930. /* -------------------------------------------------------------------------- */
  931. /*
  932. * Open/initialize the board.
  933. */
  934. static int
  935. sbni_open( struct net_device *dev )
  936. {
  937. struct net_local *nl = (struct net_local *) dev->priv;
  938. struct timer_list *w = &nl->watchdog;
  939. /*
  940. * For double ISA adapters within "common irq" mode, we have to
  941. * determine whether primary or secondary channel is initialized,
  942. * and set the irq handler only in first case.
  943. */
  944. if( dev->base_addr < 0x400 ) { /* ISA only */
  945. struct net_device **p = sbni_cards;
  946. for( ; *p && p < sbni_cards + SBNI_MAX_NUM_CARDS; ++p )
  947. if( (*p)->irq == dev->irq
  948. && ((*p)->base_addr == dev->base_addr + 4
  949. || (*p)->base_addr == dev->base_addr - 4)
  950. && (*p)->flags & IFF_UP ) {
  951. ((struct net_local *) ((*p)->priv))
  952. ->second = dev;
  953. printk( KERN_NOTICE "%s: using shared irq "
  954. "with %s\n", dev->name, (*p)->name );
  955. nl->state |= FL_SECONDARY;
  956. goto handler_attached;
  957. }
  958. }
  959. if( request_irq(dev->irq, sbni_interrupt, SA_SHIRQ, dev->name, dev) ) {
  960. printk( KERN_ERR "%s: unable to get IRQ %d.\n",
  961. dev->name, dev->irq );
  962. return -EAGAIN;
  963. }
  964. handler_attached:
  965. spin_lock( &nl->lock );
  966. memset( &nl->stats, 0, sizeof(struct net_device_stats) );
  967. memset( &nl->in_stats, 0, sizeof(struct sbni_in_stats) );
  968. card_start( dev );
  969. netif_start_queue( dev );
  970. /* set timer watchdog */
  971. init_timer( w );
  972. w->expires = jiffies + SBNI_TIMEOUT;
  973. w->data = (unsigned long) dev;
  974. w->function = sbni_watchdog;
  975. add_timer( w );
  976. spin_unlock( &nl->lock );
  977. return 0;
  978. }
  979. static int
  980. sbni_close( struct net_device *dev )
  981. {
  982. struct net_local *nl = (struct net_local *) dev->priv;
  983. if( nl->second && nl->second->flags & IFF_UP ) {
  984. printk( KERN_NOTICE "Secondary channel (%s) is active!\n",
  985. nl->second->name );
  986. return -EBUSY;
  987. }
  988. #ifdef CONFIG_SBNI_MULTILINE
  989. if( nl->state & FL_SLAVE )
  990. emancipate( dev );
  991. else
  992. while( nl->link ) /* it's master device! */
  993. emancipate( nl->link );
  994. #endif
  995. spin_lock( &nl->lock );
  996. nl->second = NULL;
  997. drop_xmit_queue( dev );
  998. netif_stop_queue( dev );
  999. del_timer( &nl->watchdog );
  1000. outb( 0, dev->base_addr + CSR0 );
  1001. if( !(nl->state & FL_SECONDARY) )
  1002. free_irq( dev->irq, dev );
  1003. nl->state &= FL_SECONDARY;
  1004. spin_unlock( &nl->lock );
  1005. return 0;
  1006. }
  1007. /*
  1008. Valid combinations in CSR0 (for probing):
  1009. VALID_DECODER 0000,0011,1011,1010
  1010. ; 0 ; -
  1011. TR_REQ ; 1 ; +
  1012. TR_RDY ; 2 ; -
  1013. TR_RDY TR_REQ ; 3 ; +
  1014. BU_EMP ; 4 ; +
  1015. BU_EMP TR_REQ ; 5 ; +
  1016. BU_EMP TR_RDY ; 6 ; -
  1017. BU_EMP TR_RDY TR_REQ ; 7 ; +
  1018. RC_RDY ; 8 ; +
  1019. RC_RDY TR_REQ ; 9 ; +
  1020. RC_RDY TR_RDY ; 10 ; -
  1021. RC_RDY TR_RDY TR_REQ ; 11 ; -
  1022. RC_RDY BU_EMP ; 12 ; -
  1023. RC_RDY BU_EMP TR_REQ ; 13 ; -
  1024. RC_RDY BU_EMP TR_RDY ; 14 ; -
  1025. RC_RDY BU_EMP TR_RDY TR_REQ ; 15 ; -
  1026. */
  1027. #define VALID_DECODER (2 + 8 + 0x10 + 0x20 + 0x80 + 0x100 + 0x200)
  1028. static int
  1029. sbni_card_probe( unsigned long ioaddr )
  1030. {
  1031. unsigned char csr0;
  1032. csr0 = inb( ioaddr + CSR0 );
  1033. if( csr0 != 0xff && csr0 != 0x00 ) {
  1034. csr0 &= ~EN_INT;
  1035. if( csr0 & BU_EMP )
  1036. csr0 |= EN_INT;
  1037. if( VALID_DECODER & (1 << (csr0 >> 4)) )
  1038. return 0;
  1039. }
  1040. return -ENODEV;
  1041. }
  1042. /* -------------------------------------------------------------------------- */
  1043. static int
  1044. sbni_ioctl( struct net_device *dev, struct ifreq *ifr, int cmd )
  1045. {
  1046. struct net_local *nl = (struct net_local *) dev->priv;
  1047. struct sbni_flags flags;
  1048. int error = 0;
  1049. #ifdef CONFIG_SBNI_MULTILINE
  1050. struct net_device *slave_dev;
  1051. char slave_name[ 8 ];
  1052. #endif
  1053. switch( cmd ) {
  1054. case SIOCDEVGETINSTATS :
  1055. if (copy_to_user( ifr->ifr_data, &nl->in_stats,
  1056. sizeof(struct sbni_in_stats) ))
  1057. error = -EFAULT;
  1058. break;
  1059. case SIOCDEVRESINSTATS :
  1060. if( current->euid != 0 ) /* root only */
  1061. return -EPERM;
  1062. memset( &nl->in_stats, 0, sizeof(struct sbni_in_stats) );
  1063. break;
  1064. case SIOCDEVGHWSTATE :
  1065. flags.mac_addr = *(u32 *)(dev->dev_addr + 3);
  1066. flags.rate = nl->csr1.rate;
  1067. flags.slow_mode = (nl->state & FL_SLOW_MODE) != 0;
  1068. flags.rxl = nl->cur_rxl_index;
  1069. flags.fixed_rxl = nl->delta_rxl == 0;
  1070. if (copy_to_user( ifr->ifr_data, &flags, sizeof flags ))
  1071. error = -EFAULT;
  1072. break;
  1073. case SIOCDEVSHWSTATE :
  1074. if( current->euid != 0 ) /* root only */
  1075. return -EPERM;
  1076. spin_lock( &nl->lock );
  1077. flags = *(struct sbni_flags*) &ifr->ifr_ifru;
  1078. if( flags.fixed_rxl )
  1079. nl->delta_rxl = 0,
  1080. nl->cur_rxl_index = flags.rxl;
  1081. else
  1082. nl->delta_rxl = DEF_RXL_DELTA,
  1083. nl->cur_rxl_index = DEF_RXL;
  1084. nl->csr1.rxl = rxl_tab[ nl->cur_rxl_index ];
  1085. nl->csr1.rate = flags.rate;
  1086. outb( *(u8 *)&nl->csr1 | PR_RES, dev->base_addr + CSR1 );
  1087. spin_unlock( &nl->lock );
  1088. break;
  1089. #ifdef CONFIG_SBNI_MULTILINE
  1090. case SIOCDEVENSLAVE :
  1091. if( current->euid != 0 ) /* root only */
  1092. return -EPERM;
  1093. if (copy_from_user( slave_name, ifr->ifr_data, sizeof slave_name ))
  1094. return -EFAULT;
  1095. slave_dev = dev_get_by_name( slave_name );
  1096. if( !slave_dev || !(slave_dev->flags & IFF_UP) ) {
  1097. printk( KERN_ERR "%s: trying to enslave non-active "
  1098. "device %s\n", dev->name, slave_name );
  1099. return -EPERM;
  1100. }
  1101. return enslave( dev, slave_dev );
  1102. case SIOCDEVEMANSIPATE :
  1103. if( current->euid != 0 ) /* root only */
  1104. return -EPERM;
  1105. return emancipate( dev );
  1106. #endif /* CONFIG_SBNI_MULTILINE */
  1107. default :
  1108. return -EOPNOTSUPP;
  1109. }
  1110. return error;
  1111. }
  1112. #ifdef CONFIG_SBNI_MULTILINE
  1113. static int
  1114. enslave( struct net_device *dev, struct net_device *slave_dev )
  1115. {
  1116. struct net_local *nl = (struct net_local *) dev->priv;
  1117. struct net_local *snl = (struct net_local *) slave_dev->priv;
  1118. if( nl->state & FL_SLAVE ) /* This isn't master or free device */
  1119. return -EBUSY;
  1120. if( snl->state & FL_SLAVE ) /* That was already enslaved */
  1121. return -EBUSY;
  1122. spin_lock( &nl->lock );
  1123. spin_lock( &snl->lock );
  1124. /* append to list */
  1125. snl->link = nl->link;
  1126. nl->link = slave_dev;
  1127. snl->master = dev;
  1128. snl->state |= FL_SLAVE;
  1129. /* Summary statistics of MultiLine operation will be stored
  1130. in master's counters */
  1131. memset( &snl->stats, 0, sizeof(struct net_device_stats) );
  1132. netif_stop_queue( slave_dev );
  1133. netif_wake_queue( dev ); /* Now we are able to transmit */
  1134. spin_unlock( &snl->lock );
  1135. spin_unlock( &nl->lock );
  1136. printk( KERN_NOTICE "%s: slave device (%s) attached.\n",
  1137. dev->name, slave_dev->name );
  1138. return 0;
  1139. }
  1140. static int
  1141. emancipate( struct net_device *dev )
  1142. {
  1143. struct net_local *snl = (struct net_local *) dev->priv;
  1144. struct net_device *p = snl->master;
  1145. struct net_local *nl = (struct net_local *) p->priv;
  1146. if( !(snl->state & FL_SLAVE) )
  1147. return -EINVAL;
  1148. spin_lock( &nl->lock );
  1149. spin_lock( &snl->lock );
  1150. drop_xmit_queue( dev );
  1151. /* exclude from list */
  1152. for(;;) { /* must be in list */
  1153. struct net_local *t = (struct net_local *) p->priv;
  1154. if( t->link == dev ) {
  1155. t->link = snl->link;
  1156. break;
  1157. }
  1158. p = t->link;
  1159. }
  1160. snl->link = NULL;
  1161. snl->master = dev;
  1162. snl->state &= ~FL_SLAVE;
  1163. netif_start_queue( dev );
  1164. spin_unlock( &snl->lock );
  1165. spin_unlock( &nl->lock );
  1166. dev_put( dev );
  1167. return 0;
  1168. }
  1169. #endif
  1170. static struct net_device_stats *
  1171. sbni_get_stats( struct net_device *dev )
  1172. {
  1173. return &((struct net_local *) dev->priv)->stats;
  1174. }
  1175. static void
  1176. set_multicast_list( struct net_device *dev )
  1177. {
  1178. return; /* sbni always operate in promiscuos mode */
  1179. }
  1180. #ifdef MODULE
  1181. module_param_array(io, int, NULL, 0);
  1182. module_param_array(irq, int, NULL, 0);
  1183. module_param_array(baud, int, NULL, 0);
  1184. module_param_array(rxl, int, NULL, 0);
  1185. module_param_array(mac, int, NULL, 0);
  1186. module_param(skip_pci_probe, bool, 0);
  1187. MODULE_LICENSE("GPL");
  1188. int __init init_module( void )
  1189. {
  1190. struct net_device *dev;
  1191. int err;
  1192. while( num < SBNI_MAX_NUM_CARDS ) {
  1193. dev = alloc_netdev(sizeof(struct net_local),
  1194. "sbni%d", sbni_devsetup);
  1195. if( !dev)
  1196. break;
  1197. sprintf( dev->name, "sbni%d", num );
  1198. err = sbni_init(dev);
  1199. if (err) {
  1200. free_netdev(dev);
  1201. break;
  1202. }
  1203. if( register_netdev( dev ) ) {
  1204. release_region( dev->base_addr, SBNI_IO_EXTENT );
  1205. free_netdev( dev );
  1206. break;
  1207. }
  1208. }
  1209. return *sbni_cards ? 0 : -ENODEV;
  1210. }
  1211. void
  1212. cleanup_module( void )
  1213. {
  1214. struct net_device *dev;
  1215. int num;
  1216. for( num = 0; num < SBNI_MAX_NUM_CARDS; ++num )
  1217. if( (dev = sbni_cards[ num ]) != NULL ) {
  1218. unregister_netdev( dev );
  1219. release_region( dev->base_addr, SBNI_IO_EXTENT );
  1220. free_netdev( dev );
  1221. }
  1222. }
  1223. #else /* MODULE */
  1224. static int __init
  1225. sbni_setup( char *p )
  1226. {
  1227. int n, parm;
  1228. if( *p++ != '(' )
  1229. goto bad_param;
  1230. for( n = 0, parm = 0; *p && n < 8; ) {
  1231. (*dest[ parm ])[ n ] = simple_strtol( p, &p, 0 );
  1232. if( !*p || *p == ')' )
  1233. return 1;
  1234. if( *p == ';' )
  1235. ++p, ++n, parm = 0;
  1236. else if( *p++ != ',' )
  1237. break;
  1238. else
  1239. if( ++parm >= 5 )
  1240. break;
  1241. }
  1242. bad_param:
  1243. printk( KERN_ERR "Error in sbni kernel parameter!\n" );
  1244. return 0;
  1245. }
  1246. __setup( "sbni=", sbni_setup );
  1247. #endif /* MODULE */
  1248. /* -------------------------------------------------------------------------- */
  1249. #ifdef ASM_CRC
  1250. static u32
  1251. calc_crc32( u32 crc, u8 *p, u32 len )
  1252. {
  1253. register u32 _crc;
  1254. _crc = crc;
  1255. __asm__ __volatile__ (
  1256. "xorl %%ebx, %%ebx\n"
  1257. "movl %2, %%esi\n"
  1258. "movl %3, %%ecx\n"
  1259. "movl $crc32tab, %%edi\n"
  1260. "shrl $2, %%ecx\n"
  1261. "jz 1f\n"
  1262. ".align 4\n"
  1263. "0:\n"
  1264. "movb %%al, %%bl\n"
  1265. "movl (%%esi), %%edx\n"
  1266. "shrl $8, %%eax\n"
  1267. "xorb %%dl, %%bl\n"
  1268. "shrl $8, %%edx\n"
  1269. "xorl (%%edi,%%ebx,4), %%eax\n"
  1270. "movb %%al, %%bl\n"
  1271. "shrl $8, %%eax\n"
  1272. "xorb %%dl, %%bl\n"
  1273. "shrl $8, %%edx\n"
  1274. "xorl (%%edi,%%ebx,4), %%eax\n"
  1275. "movb %%al, %%bl\n"
  1276. "shrl $8, %%eax\n"
  1277. "xorb %%dl, %%bl\n"
  1278. "movb %%dh, %%dl\n"
  1279. "xorl (%%edi,%%ebx,4), %%eax\n"
  1280. "movb %%al, %%bl\n"
  1281. "shrl $8, %%eax\n"
  1282. "xorb %%dl, %%bl\n"
  1283. "addl $4, %%esi\n"
  1284. "xorl (%%edi,%%ebx,4), %%eax\n"
  1285. "decl %%ecx\n"
  1286. "jnz 0b\n"
  1287. "1:\n"
  1288. "movl %3, %%ecx\n"
  1289. "andl $3, %%ecx\n"
  1290. "jz 2f\n"
  1291. "movb %%al, %%bl\n"
  1292. "shrl $8, %%eax\n"
  1293. "xorb (%%esi), %%bl\n"
  1294. "xorl (%%edi,%%ebx,4), %%eax\n"
  1295. "decl %%ecx\n"
  1296. "jz 2f\n"
  1297. "movb %%al, %%bl\n"
  1298. "shrl $8, %%eax\n"
  1299. "xorb 1(%%esi), %%bl\n"
  1300. "xorl (%%edi,%%ebx,4), %%eax\n"
  1301. "decl %%ecx\n"
  1302. "jz 2f\n"
  1303. "movb %%al, %%bl\n"
  1304. "shrl $8, %%eax\n"
  1305. "xorb 2(%%esi), %%bl\n"
  1306. "xorl (%%edi,%%ebx,4), %%eax\n"
  1307. "2:\n"
  1308. : "=a" (_crc)
  1309. : "0" (_crc), "g" (p), "g" (len)
  1310. : "bx", "cx", "dx", "si", "di"
  1311. );
  1312. return _crc;
  1313. }
  1314. #else /* ASM_CRC */
  1315. static u32
  1316. calc_crc32( u32 crc, u8 *p, u32 len )
  1317. {
  1318. while( len-- )
  1319. crc = CRC32( *p++, crc );
  1320. return crc;
  1321. }
  1322. #endif /* ASM_CRC */
  1323. static u32 crc32tab[] __attribute__ ((aligned(8))) = {
  1324. 0xD202EF8D, 0xA505DF1B, 0x3C0C8EA1, 0x4B0BBE37,
  1325. 0xD56F2B94, 0xA2681B02, 0x3B614AB8, 0x4C667A2E,
  1326. 0xDCD967BF, 0xABDE5729, 0x32D70693, 0x45D03605,
  1327. 0xDBB4A3A6, 0xACB39330, 0x35BAC28A, 0x42BDF21C,
  1328. 0xCFB5FFE9, 0xB8B2CF7F, 0x21BB9EC5, 0x56BCAE53,
  1329. 0xC8D83BF0, 0xBFDF0B66, 0x26D65ADC, 0x51D16A4A,
  1330. 0xC16E77DB, 0xB669474D, 0x2F6016F7, 0x58672661,
  1331. 0xC603B3C2, 0xB1048354, 0x280DD2EE, 0x5F0AE278,
  1332. 0xE96CCF45, 0x9E6BFFD3, 0x0762AE69, 0x70659EFF,
  1333. 0xEE010B5C, 0x99063BCA, 0x000F6A70, 0x77085AE6,
  1334. 0xE7B74777, 0x90B077E1, 0x09B9265B, 0x7EBE16CD,
  1335. 0xE0DA836E, 0x97DDB3F8, 0x0ED4E242, 0x79D3D2D4,
  1336. 0xF4DBDF21, 0x83DCEFB7, 0x1AD5BE0D, 0x6DD28E9B,
  1337. 0xF3B61B38, 0x84B12BAE, 0x1DB87A14, 0x6ABF4A82,
  1338. 0xFA005713, 0x8D076785, 0x140E363F, 0x630906A9,
  1339. 0xFD6D930A, 0x8A6AA39C, 0x1363F226, 0x6464C2B0,
  1340. 0xA4DEAE1D, 0xD3D99E8B, 0x4AD0CF31, 0x3DD7FFA7,
  1341. 0xA3B36A04, 0xD4B45A92, 0x4DBD0B28, 0x3ABA3BBE,
  1342. 0xAA05262F, 0xDD0216B9, 0x440B4703, 0x330C7795,
  1343. 0xAD68E236, 0xDA6FD2A0, 0x4366831A, 0x3461B38C,
  1344. 0xB969BE79, 0xCE6E8EEF, 0x5767DF55, 0x2060EFC3,
  1345. 0xBE047A60, 0xC9034AF6, 0x500A1B4C, 0x270D2BDA,
  1346. 0xB7B2364B, 0xC0B506DD, 0x59BC5767, 0x2EBB67F1,
  1347. 0xB0DFF252, 0xC7D8C2C4, 0x5ED1937E, 0x29D6A3E8,
  1348. 0x9FB08ED5, 0xE8B7BE43, 0x71BEEFF9, 0x06B9DF6F,
  1349. 0x98DD4ACC, 0xEFDA7A5A, 0x76D32BE0, 0x01D41B76,
  1350. 0x916B06E7, 0xE66C3671, 0x7F6567CB, 0x0862575D,
  1351. 0x9606C2FE, 0xE101F268, 0x7808A3D2, 0x0F0F9344,
  1352. 0x82079EB1, 0xF500AE27, 0x6C09FF9D, 0x1B0ECF0B,
  1353. 0x856A5AA8, 0xF26D6A3E, 0x6B643B84, 0x1C630B12,
  1354. 0x8CDC1683, 0xFBDB2615, 0x62D277AF, 0x15D54739,
  1355. 0x8BB1D29A, 0xFCB6E20C, 0x65BFB3B6, 0x12B88320,
  1356. 0x3FBA6CAD, 0x48BD5C3B, 0xD1B40D81, 0xA6B33D17,
  1357. 0x38D7A8B4, 0x4FD09822, 0xD6D9C998, 0xA1DEF90E,
  1358. 0x3161E49F, 0x4666D409, 0xDF6F85B3, 0xA868B525,
  1359. 0x360C2086, 0x410B1010, 0xD80241AA, 0xAF05713C,
  1360. 0x220D7CC9, 0x550A4C5F, 0xCC031DE5, 0xBB042D73,
  1361. 0x2560B8D0, 0x52678846, 0xCB6ED9FC, 0xBC69E96A,
  1362. 0x2CD6F4FB, 0x5BD1C46D, 0xC2D895D7, 0xB5DFA541,
  1363. 0x2BBB30E2, 0x5CBC0074, 0xC5B551CE, 0xB2B26158,
  1364. 0x04D44C65, 0x73D37CF3, 0xEADA2D49, 0x9DDD1DDF,
  1365. 0x03B9887C, 0x74BEB8EA, 0xEDB7E950, 0x9AB0D9C6,
  1366. 0x0A0FC457, 0x7D08F4C1, 0xE401A57B, 0x930695ED,
  1367. 0x0D62004E, 0x7A6530D8, 0xE36C6162, 0x946B51F4,
  1368. 0x19635C01, 0x6E646C97, 0xF76D3D2D, 0x806A0DBB,
  1369. 0x1E0E9818, 0x6909A88E, 0xF000F934, 0x8707C9A2,
  1370. 0x17B8D433, 0x60BFE4A5, 0xF9B6B51F, 0x8EB18589,
  1371. 0x10D5102A, 0x67D220BC, 0xFEDB7106, 0x89DC4190,
  1372. 0x49662D3D, 0x3E611DAB, 0xA7684C11, 0xD06F7C87,
  1373. 0x4E0BE924, 0x390CD9B2, 0xA0058808, 0xD702B89E,
  1374. 0x47BDA50F, 0x30BA9599, 0xA9B3C423, 0xDEB4F4B5,
  1375. 0x40D06116, 0x37D75180, 0xAEDE003A, 0xD9D930AC,
  1376. 0x54D13D59, 0x23D60DCF, 0xBADF5C75, 0xCDD86CE3,
  1377. 0x53BCF940, 0x24BBC9D6, 0xBDB2986C, 0xCAB5A8FA,
  1378. 0x5A0AB56B, 0x2D0D85FD, 0xB404D447, 0xC303E4D1,
  1379. 0x5D677172, 0x2A6041E4, 0xB369105E, 0xC46E20C8,
  1380. 0x72080DF5, 0x050F3D63, 0x9C066CD9, 0xEB015C4F,
  1381. 0x7565C9EC, 0x0262F97A, 0x9B6BA8C0, 0xEC6C9856,
  1382. 0x7CD385C7, 0x0BD4B551, 0x92DDE4EB, 0xE5DAD47D,
  1383. 0x7BBE41DE, 0x0CB97148, 0x95B020F2, 0xE2B71064,
  1384. 0x6FBF1D91, 0x18B82D07, 0x81B17CBD, 0xF6B64C2B,
  1385. 0x68D2D988, 0x1FD5E91E, 0x86DCB8A4, 0xF1DB8832,
  1386. 0x616495A3, 0x1663A535, 0x8F6AF48F, 0xF86DC419,
  1387. 0x660951BA, 0x110E612C, 0x88073096, 0xFF000000
  1388. };