sbni.c 42 KB

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