atari_pamsnet.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /* atari_pamsnet.c PAMsNet device driver for linux68k.
  2. *
  3. * Version: @(#)PAMsNet.c 0.2ß 03/31/96
  4. *
  5. * Author: Torsten Lang <Torsten.Lang@ap.physik.uni-giessen.de>
  6. * <Torsten.Lang@jung.de>
  7. *
  8. * This driver is based on my driver PAMSDMA.c for MiNT-Net and
  9. * on the driver bionet.c written by
  10. * Hartmut Laue <laue@ifk-mp.uni-kiel.de>
  11. * and Torsten Narjes <narjes@ifk-mp.uni-kiel.de>
  12. *
  13. * Little adaptions for integration into pl7 by Roman Hodek
  14. *
  15. What is it ?
  16. ------------
  17. This driver controls the PAMsNet LAN-Adapter which connects
  18. an ATARI ST/TT via the ACSI-port to an Ethernet-based network.
  19. This version can be compiled as a loadable module (See the
  20. compile command at the bottom of this file).
  21. At load time, you can optionally set the debugging level and the
  22. fastest response time on the command line of 'insmod'.
  23. 'pamsnet_debug'
  24. controls the amount of diagnostic messages:
  25. 0 : no messages
  26. >0 : see code for meaning of printed messages
  27. 'pamsnet_min_poll_time' (always >=1)
  28. gives the time (in jiffies) between polls. Low values
  29. increase the system load (beware!)
  30. When loaded, a net device with the name 'eth?' becomes available,
  31. which can be controlled with the usual 'ifconfig' command.
  32. It is possible to compile this driver into the kernel like other
  33. (net) drivers. For this purpose, some source files (e.g. config-files
  34. makefiles, Space.c) must be changed accordingly. (You may refer to
  35. other drivers how to do it.) In this case, the device will be detected
  36. at boot time and (probably) appear as 'eth0'.
  37. Theory of Operation
  38. -------------------
  39. Because the ATARI DMA port is usually shared between several
  40. devices (eg. harddisk, floppy) we cannot block the ACSI bus
  41. while waiting for interrupts. Therefore we use a polling mechanism
  42. to fetch packets from the adapter. For the same reason, we send
  43. packets without checking that the previous packet has been sent to
  44. the LAN. We rely on the higher levels of the networking code to detect
  45. missing packets and resend them.
  46. Before we access the ATARI DMA controller, we check if another
  47. process is using the DMA. If not, we lock the DMA, perform one or
  48. more packet transfers and unlock the DMA before returning.
  49. We do not use 'stdma_lock' unconditionally because it is unclear
  50. if the networking code can be set to sleep, which will happen if
  51. another (possibly slow) device is using the DMA controller.
  52. The polling is done via timer interrupts which periodically
  53. 'simulate' an interrupt from the Ethernet adapter. The time (in jiffies)
  54. between polls varies depending on an estimate of the net activity.
  55. The allowed range is given by the variable 'bionet_min_poll_time'
  56. for the lower (fastest) limit and the constant 'MAX_POLL_TIME'
  57. for the higher (slowest) limit.
  58. Whenever a packet arrives, we switch to fastest response by setting
  59. the polling time to its lowest limit. If the following poll fails,
  60. because no packets have arrived, we increase the time for the next
  61. poll. When the net activity is low, the polling time effectively
  62. stays at its maximum value, resulting in the lowest load for the
  63. machine.
  64. */
  65. #define MAX_POLL_TIME 10
  66. static char *version =
  67. "pamsnet.c:v0.2beta 30-mar-96 (c) Torsten Lang.\n";
  68. #include <linux/module.h>
  69. #include <linux/kernel.h>
  70. #include <linux/jiffies.h>
  71. #include <linux/types.h>
  72. #include <linux/fcntl.h>
  73. #include <linux/interrupt.h>
  74. #include <linux/ioport.h>
  75. #include <linux/in.h>
  76. #include <linux/slab.h>
  77. #include <linux/string.h>
  78. #include <linux/bitops.h>
  79. #include <asm/system.h>
  80. #include <asm/pgtable.h>
  81. #include <asm/io.h>
  82. #include <asm/dma.h>
  83. #include <linux/errno.h>
  84. #include <asm/atarihw.h>
  85. #include <asm/atariints.h>
  86. #include <asm/atari_stdma.h>
  87. #include <asm/atari_acsi.h>
  88. #include <linux/delay.h>
  89. #include <linux/timer.h>
  90. #include <linux/init.h>
  91. #include <linux/netdevice.h>
  92. #include <linux/etherdevice.h>
  93. #include <linux/skbuff.h>
  94. #undef READ
  95. #undef WRITE
  96. /* use 0 for production, 1 for verification, >2 for debug
  97. */
  98. #ifndef NET_DEBUG
  99. #define NET_DEBUG 0
  100. #endif
  101. /*
  102. * Global variable 'pamsnet_debug'. Can be set at load time by 'insmod'
  103. */
  104. unsigned int pamsnet_debug = NET_DEBUG;
  105. module_param(pamsnet_debug, int, 0);
  106. MODULE_PARM_DESC(pamsnet_debug, "pamsnet debug enable (0-1)");
  107. MODULE_LICENSE("GPL");
  108. static unsigned int pamsnet_min_poll_time = 2;
  109. /* Information that need to be kept for each board.
  110. */
  111. struct net_local {
  112. struct net_device_stats stats;
  113. long open_time; /* for debugging */
  114. int poll_time; /* polling time varies with net load */
  115. };
  116. static struct nic_pkt_s { /* packet format */
  117. unsigned char buffer[2048];
  118. } *nic_packet = 0;
  119. unsigned char *phys_nic_packet;
  120. typedef unsigned char HADDR[6]; /* 6-byte hardware address of lance */
  121. /* Index to functions, as function prototypes.
  122. */
  123. static void start (int target);
  124. static int stop (int target);
  125. static int testpkt (int target);
  126. static int sendpkt (int target, unsigned char *buffer, int length);
  127. static int receivepkt (int target, unsigned char *buffer);
  128. static int inquiry (int target, unsigned char *buffer);
  129. static HADDR *read_hw_addr(int target, unsigned char *buffer);
  130. static void setup_dma (void *address, unsigned rw_flag, int num_blocks);
  131. static int send_first (int target, unsigned char byte);
  132. static int send_1_5 (int lun, unsigned char *command, int dma);
  133. static int get_status (void);
  134. static int calc_received (void *start_address);
  135. static int pamsnet_open(struct net_device *dev);
  136. static int pamsnet_send_packet(struct sk_buff *skb, struct net_device *dev);
  137. static void pamsnet_poll_rx(struct net_device *);
  138. static int pamsnet_close(struct net_device *dev);
  139. static struct net_device_stats *net_get_stats(struct net_device *dev);
  140. static void pamsnet_tick(unsigned long);
  141. static irqreturn_t pamsnet_intr(int irq, void *data);
  142. static DEFINE_TIMER(pamsnet_timer, pamsnet_tick, 0, 0);
  143. #define STRAM_ADDR(a) (((a) & 0xff000000) == 0)
  144. typedef struct
  145. {
  146. unsigned char reserved1[0x38];
  147. HADDR hwaddr;
  148. unsigned char reserved2[0x1c2];
  149. } DMAHWADDR;
  150. /*
  151. * Definitions of commands understood by the PAMs DMA adaptor.
  152. *
  153. * In general the DMA adaptor uses LUN 0, 5, 6 and 7 on one ID changeable
  154. * by the PAM's Net software.
  155. *
  156. * LUN 0 works as a harddisk. You can boot the PAM's Net driver there.
  157. * LUN 5 works as a harddisk and lets you access the RAM and some I/O HW
  158. * area. In sector 0, bytes 0x38-0x3d you find the ethernet HW address
  159. * of the adaptor.
  160. * LUN 6 works as a harddisk and lets you access the firmware ROM.
  161. * LUN 7 lets you send and receive packets.
  162. *
  163. * Some commands like the INQUIRY command work identical on all used LUNs.
  164. *
  165. * UNKNOWN1 seems to read some data.
  166. * Command length is 6 bytes.
  167. * UNKNOWN2 seems to read some data (command byte 1 must be !=0). The
  168. * following bytes seem to be something like an allocation length.
  169. * Command length is 6 bytes.
  170. * READPKT reads a packet received by the DMA adaptor.
  171. * Command length is 6 bytes.
  172. * WRITEPKT sends a packet transferred by the following DMA phase. The length
  173. * of the packet is transferred in command bytes 3 and 4.
  174. * The adaptor automatically replaces the src hw address in an ethernet
  175. * packet by its own hw address.
  176. * Command length is 6 bytes.
  177. * INQUIRY has the same function as the INQUIRY command supported by harddisks
  178. * and other SCSI devices. It lets you detect which device you found
  179. * at a given address.
  180. * Command length is 6 bytes.
  181. * START initializes the DMA adaptor. After this command it is able to send
  182. * and receive packets. There is no status byte returned!
  183. * Command length is 1 byte.
  184. * NUMPKTS gives back the number of received packets waiting in the queue in
  185. * the status byte.
  186. * Command length is 1 byte.
  187. * UNKNOWN3
  188. * UNKNOWN4 Function of these three commands is unknown.
  189. * UNKNOWN5 The command length of these three commands is 1 byte.
  190. * DESELECT immediately deselects the DMA adaptor. May important with interrupt
  191. * driven operation.
  192. * Command length is 1 byte.
  193. * STOP resets the DMA adaptor. After this command packets can no longer
  194. * be received or transferred.
  195. * Command length is 6 byte.
  196. */
  197. enum {UNKNOWN1=3, READPKT=8, UNKNOWN2, WRITEPKT=10, INQUIRY=18, START,
  198. NUMPKTS=22, UNKNOWN3, UNKNOWN4, UNKNOWN5, DESELECT, STOP};
  199. #define READSECTOR READPKT
  200. #define WRITESECTOR WRITEPKT
  201. u_char *inquire8="MV PAM's NET/GK";
  202. #define DMALOW dma_wd.dma_lo
  203. #define DMAMID dma_wd.dma_md
  204. #define DMAHIGH dma_wd.dma_hi
  205. #define DACCESS dma_wd.fdc_acces_seccount
  206. #define MFP_GPIP mfp.par_dt_reg
  207. /* Some useful functions */
  208. #define INT (!(MFP_GPIP & 0x20))
  209. #define DELAY ({MFP_GPIP; MFP_GPIP; MFP_GPIP;})
  210. #define WRITEMODE(value) \
  211. ({ u_short dummy = value; \
  212. __asm__ volatile("movew %0, 0xFFFF8606" : : "d"(dummy)); \
  213. DELAY; \
  214. })
  215. #define WRITEBOTH(value1, value2) \
  216. ({ u_long dummy = (u_long)(value1)<<16 | (u_short)(value2); \
  217. __asm__ volatile("movel %0, 0xFFFF8604" : : "d"(dummy)); \
  218. DELAY; \
  219. })
  220. /* Definitions for DMODE */
  221. #define READ 0x000
  222. #define WRITE 0x100
  223. #define DMA_FDC 0x080
  224. #define DMA_ACSI 0x000
  225. #define DMA_DISABLE 0x040
  226. #define SEC_COUNT 0x010
  227. #define DMA_WINDOW 0x000
  228. #define REG_ACSI 0x008
  229. #define REG_FDC 0x000
  230. #define A1 0x002
  231. /* Timeout constants */
  232. #define TIMEOUTCMD HZ/2 /* ca. 500ms */
  233. #define TIMEOUTDMA HZ /* ca. 1s */
  234. #define COMMAND_DELAY 500 /* ca. 0.5ms */
  235. unsigned rw;
  236. int lance_target = -1;
  237. int if_up = 0;
  238. /* The following routines access the ethernet board connected to the
  239. * ACSI port via the st_dma chip.
  240. */
  241. /* The following lowlevel routines work on physical addresses only and assume
  242. * that eventually needed buffers are
  243. * - completely located in ST RAM
  244. * - are contigous in the physical address space
  245. */
  246. /* Setup the DMA counter */
  247. static void
  248. setup_dma (address, rw_flag, num_blocks)
  249. void *address;
  250. unsigned rw_flag;
  251. int num_blocks;
  252. {
  253. WRITEMODE((unsigned) rw_flag | DMA_FDC | SEC_COUNT | REG_ACSI |
  254. A1);
  255. WRITEMODE((unsigned)(rw_flag ^ WRITE) | DMA_FDC | SEC_COUNT | REG_ACSI |
  256. A1);
  257. WRITEMODE((unsigned) rw_flag | DMA_FDC | SEC_COUNT | REG_ACSI |
  258. A1);
  259. DMALOW = (unsigned char)((unsigned long)address & 0xFF);
  260. DMAMID = (unsigned char)(((unsigned long)address >> 8) & 0xFF);
  261. DMAHIGH = (unsigned char)(((unsigned long)address >> 16) & 0xFF);
  262. WRITEBOTH((unsigned)num_blocks & 0xFF,
  263. rw_flag | DMA_FDC | DMA_WINDOW | REG_ACSI | A1);
  264. rw = rw_flag;
  265. }
  266. /* Send the first byte of an command block */
  267. static int
  268. send_first (target, byte)
  269. int target;
  270. unsigned char byte;
  271. {
  272. rw = READ;
  273. acsi_delay_end(COMMAND_DELAY);
  274. /*
  275. * wake up ACSI
  276. */
  277. WRITEMODE(DMA_FDC | DMA_WINDOW | REG_ACSI);
  278. /*
  279. * write command byte
  280. */
  281. WRITEBOTH((target << 5) | (byte & 0x1F), DMA_FDC |
  282. DMA_WINDOW | REG_ACSI | A1);
  283. return (!acsi_wait_for_IRQ(TIMEOUTCMD));
  284. }
  285. /* Send the rest of an command block */
  286. static int
  287. send_1_5 (lun, command, dma)
  288. int lun;
  289. unsigned char *command;
  290. int dma;
  291. {
  292. int i, j;
  293. for (i=0; i<5; i++) {
  294. WRITEBOTH((!i ? (((lun & 0x7) << 5) | (command[i] & 0x1F))
  295. : command[i]),
  296. rw | REG_ACSI | DMA_WINDOW |
  297. ((i < 4) ? DMA_FDC
  298. : (dma ? DMA_ACSI
  299. : DMA_FDC)) | A1);
  300. if (i < 4 && (j = !acsi_wait_for_IRQ(TIMEOUTCMD)))
  301. return (j);
  302. }
  303. return (0);
  304. }
  305. /* Read a status byte */
  306. static int
  307. get_status (void)
  308. {
  309. WRITEMODE(DMA_FDC | DMA_WINDOW | REG_ACSI | A1);
  310. acsi_delay_start();
  311. return ((int)(DACCESS & 0xFF));
  312. }
  313. /* Calculate the number of received bytes */
  314. static int
  315. calc_received (start_address)
  316. void *start_address;
  317. {
  318. return (int)(
  319. (((unsigned long)DMAHIGH << 16) | ((unsigned)DMAMID << 8) | DMALOW)
  320. - (unsigned long)start_address);
  321. }
  322. /* The following midlevel routines still work on physical addresses ... */
  323. /* start() starts the PAM's DMA adaptor */
  324. static void
  325. start (target)
  326. int target;
  327. {
  328. send_first(target, START);
  329. }
  330. /* stop() stops the PAM's DMA adaptor and returns a value of zero in case of success */
  331. static int
  332. stop (target)
  333. int target;
  334. {
  335. int ret = -1;
  336. unsigned char cmd_buffer[5];
  337. if (send_first(target, STOP))
  338. goto bad;
  339. cmd_buffer[0] = cmd_buffer[1] = cmd_buffer[2] =
  340. cmd_buffer[3] = cmd_buffer[4] = 0;
  341. if (send_1_5(7, cmd_buffer, 0) ||
  342. !acsi_wait_for_IRQ(TIMEOUTDMA) ||
  343. get_status())
  344. goto bad;
  345. ret = 0;
  346. bad:
  347. return (ret);
  348. }
  349. /* testpkt() returns the number of received packets waiting in the queue */
  350. static int
  351. testpkt(target)
  352. int target;
  353. {
  354. int ret = -1;
  355. if (send_first(target, NUMPKTS))
  356. goto bad;
  357. ret = get_status();
  358. bad:
  359. return (ret);
  360. }
  361. /* inquiry() returns 0 when PAM's DMA found, -1 when timeout, -2 otherwise */
  362. /* Please note: The buffer is for internal use only but must be defined! */
  363. static int
  364. inquiry (target, buffer)
  365. int target;
  366. unsigned char *buffer;
  367. {
  368. int ret = -1;
  369. unsigned char *vbuffer = phys_to_virt((unsigned long)buffer);
  370. unsigned char cmd_buffer[5];
  371. if (send_first(target, INQUIRY))
  372. goto bad;
  373. setup_dma(buffer, READ, 1);
  374. vbuffer[8] = vbuffer[27] = 0; /* Avoid confusion with previous read data */
  375. cmd_buffer[0] = cmd_buffer[1] = cmd_buffer[2] = cmd_buffer[4] = 0;
  376. cmd_buffer[3] = 48;
  377. if (send_1_5(5, cmd_buffer, 1) ||
  378. !acsi_wait_for_IRQ(TIMEOUTDMA) ||
  379. get_status() ||
  380. (calc_received(buffer) < 32))
  381. goto bad;
  382. dma_cache_maintenance((unsigned long)(buffer+8), 20, 0);
  383. if (memcmp(inquire8, vbuffer+8, 20))
  384. goto bad;
  385. ret = 0;
  386. bad:
  387. if (!!NET_DEBUG) {
  388. vbuffer[8+20]=0;
  389. printk("inquiry of target %d: %s\n", target, vbuffer+8);
  390. }
  391. return (ret);
  392. }
  393. /*
  394. * read_hw_addr() reads the sector containing the hwaddr and returns
  395. * a pointer to it (virtual address!) or 0 in case of an error
  396. */
  397. static HADDR
  398. *read_hw_addr(target, buffer)
  399. int target;
  400. unsigned char *buffer;
  401. {
  402. HADDR *ret = 0;
  403. unsigned char cmd_buffer[5];
  404. if (send_first(target, READSECTOR))
  405. goto bad;
  406. setup_dma(buffer, READ, 1);
  407. cmd_buffer[0] = cmd_buffer[1] = cmd_buffer[2] = cmd_buffer[4] = 0;
  408. cmd_buffer[3] = 1;
  409. if (send_1_5(5, cmd_buffer, 1) ||
  410. !acsi_wait_for_IRQ(TIMEOUTDMA) ||
  411. get_status())
  412. goto bad;
  413. ret = phys_to_virt((unsigned long)&(((DMAHWADDR *)buffer)->hwaddr));
  414. dma_cache_maintenance((unsigned long)buffer, 512, 0);
  415. bad:
  416. return (ret);
  417. }
  418. static irqreturn_t
  419. pamsnet_intr(irq, data, fp)
  420. int irq;
  421. void *data;
  422. {
  423. return IRQ_HANDLED;
  424. }
  425. /* receivepkt() loads a packet to a given buffer and returns its length */
  426. static int
  427. receivepkt (target, buffer)
  428. int target;
  429. unsigned char *buffer;
  430. {
  431. int ret = -1;
  432. unsigned char cmd_buffer[5];
  433. if (send_first(target, READPKT))
  434. goto bad;
  435. setup_dma(buffer, READ, 3);
  436. cmd_buffer[0] = cmd_buffer[1] = cmd_buffer[2] = cmd_buffer[4] = 0;
  437. cmd_buffer[3] = 3;
  438. if (send_1_5(7, cmd_buffer, 1) ||
  439. !acsi_wait_for_IRQ(TIMEOUTDMA) ||
  440. get_status())
  441. goto bad;
  442. ret = calc_received(buffer);
  443. bad:
  444. return (ret);
  445. }
  446. /* sendpkt() sends a packet and returns a value of zero when the packet was sent
  447. successfully */
  448. static int
  449. sendpkt (target, buffer, length)
  450. int target;
  451. unsigned char *buffer;
  452. int length;
  453. {
  454. int ret = -1;
  455. unsigned char cmd_buffer[5];
  456. if (send_first(target, WRITEPKT))
  457. goto bad;
  458. setup_dma(buffer, WRITE, 3);
  459. cmd_buffer[0] = cmd_buffer[1] = cmd_buffer[4] = 0;
  460. cmd_buffer[2] = length >> 8;
  461. cmd_buffer[3] = length & 0xFF;
  462. if (send_1_5(7, cmd_buffer, 1) ||
  463. !acsi_wait_for_IRQ(TIMEOUTDMA) ||
  464. get_status())
  465. goto bad;
  466. ret = 0;
  467. bad:
  468. return (ret);
  469. }
  470. /* The following higher level routines work on virtual addresses and convert them to
  471. * physical addresses when passed to the lowlevel routines. It's up to the higher level
  472. * routines to copy data from Alternate RAM to ST RAM if neccesary!
  473. */
  474. /* Check for a network adaptor of this type, and return '0' if one exists.
  475. */
  476. struct net_device * __init pamsnet_probe (int unit)
  477. {
  478. struct net_device *dev;
  479. int i;
  480. HADDR *hwaddr;
  481. int err;
  482. unsigned char station_addr[6];
  483. static unsigned version_printed;
  484. /* avoid "Probing for..." printed 4 times - the driver is supporting only one adapter now! */
  485. static int no_more_found;
  486. if (no_more_found)
  487. return ERR_PTR(-ENODEV);
  488. no_more_found = 1;
  489. dev = alloc_etherdev(sizeof(struct net_local));
  490. if (!dev)
  491. return ERR_PTR(-ENOMEM);
  492. if (unit >= 0) {
  493. sprintf(dev->name, "eth%d", unit);
  494. netdev_boot_setup_check(dev);
  495. }
  496. SET_MODULE_OWNER(dev);
  497. printk("Probing for PAM's Net/GK Adapter...\n");
  498. /* Allocate the DMA buffer here since we need it for probing! */
  499. nic_packet = (struct nic_pkt_s *)acsi_buffer;
  500. phys_nic_packet = (unsigned char *)phys_acsi_buffer;
  501. if (pamsnet_debug > 0) {
  502. printk("nic_packet at 0x%p, phys at 0x%p\n",
  503. nic_packet, phys_nic_packet );
  504. }
  505. stdma_lock(pamsnet_intr, NULL);
  506. DISABLE_IRQ();
  507. for (i=0; i<8; i++) {
  508. /* Do two inquiries to cover cases with strange equipment on previous ID */
  509. /* blocking the ACSI bus (like the SLMC804 laser printer controller... */
  510. inquiry(i, phys_nic_packet);
  511. if (!inquiry(i, phys_nic_packet)) {
  512. lance_target = i;
  513. break;
  514. }
  515. }
  516. if (!!NET_DEBUG)
  517. printk("ID: %d\n",i);
  518. if (lance_target >= 0) {
  519. if (!(hwaddr = read_hw_addr(lance_target, phys_nic_packet)))
  520. lance_target = -1;
  521. else
  522. memcpy (station_addr, hwaddr, ETH_ALEN);
  523. }
  524. ENABLE_IRQ();
  525. stdma_release();
  526. if (lance_target < 0) {
  527. printk("No PAM's Net/GK found.\n");
  528. free_netdev(dev);
  529. return ERR_PTR(-ENODEV);
  530. }
  531. if (pamsnet_debug > 0 && version_printed++ == 0)
  532. printk(version);
  533. printk("%s: %s found on target %01d, eth-addr: %02x:%02x:%02x:%02x:%02x:%02x.\n",
  534. dev->name, "PAM's Net/GK", lance_target,
  535. station_addr[0], station_addr[1], station_addr[2],
  536. station_addr[3], station_addr[4], station_addr[5]);
  537. /* Initialize the device structure. */
  538. dev->open = pamsnet_open;
  539. dev->stop = pamsnet_close;
  540. dev->hard_start_xmit = pamsnet_send_packet;
  541. dev->get_stats = net_get_stats;
  542. /* Fill in the fields of the device structure with ethernet-generic
  543. * values. This should be in a common file instead of per-driver.
  544. */
  545. for (i = 0; i < ETH_ALEN; i++) {
  546. #if 0
  547. dev->broadcast[i] = 0xff;
  548. #endif
  549. dev->dev_addr[i] = station_addr[i];
  550. }
  551. err = register_netdev(dev);
  552. if (!err)
  553. return dev;
  554. free_netdev(dev);
  555. return ERR_PTR(err);
  556. }
  557. /* Open/initialize the board. This is called (in the current kernel)
  558. sometime after booting when the 'ifconfig' program is run.
  559. This routine should set everything up anew at each open, even
  560. registers that "should" only need to be set once at boot, so that
  561. there is non-reboot way to recover if something goes wrong.
  562. */
  563. static int
  564. pamsnet_open(struct net_device *dev) {
  565. struct net_local *lp = netdev_priv(dev);
  566. if (pamsnet_debug > 0)
  567. printk("pamsnet_open\n");
  568. stdma_lock(pamsnet_intr, NULL);
  569. DISABLE_IRQ();
  570. /* Reset the hardware here.
  571. */
  572. if (!if_up)
  573. start(lance_target);
  574. if_up = 1;
  575. lp->open_time = 0; /*jiffies*/
  576. lp->poll_time = MAX_POLL_TIME;
  577. dev->tbusy = 0;
  578. dev->interrupt = 0;
  579. dev->start = 1;
  580. ENABLE_IRQ();
  581. stdma_release();
  582. pamsnet_timer.data = (long)dev;
  583. pamsnet_timer.expires = jiffies + lp->poll_time;
  584. add_timer(&pamsnet_timer);
  585. return 0;
  586. }
  587. static int
  588. pamsnet_send_packet(struct sk_buff *skb, struct net_device *dev) {
  589. struct net_local *lp = netdev_priv(dev);
  590. unsigned long flags;
  591. /* Block a timer-based transmit from overlapping. This could better be
  592. * done with atomic_swap(1, dev->tbusy), but set_bit() works as well.
  593. */
  594. local_irq_save(flags);
  595. if (stdma_islocked()) {
  596. local_irq_restore(flags);
  597. lp->stats.tx_errors++;
  598. }
  599. else {
  600. int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
  601. unsigned long buf = virt_to_phys(skb->data);
  602. int stat;
  603. stdma_lock(pamsnet_intr, NULL);
  604. DISABLE_IRQ();
  605. local_irq_restore(flags);
  606. if( !STRAM_ADDR(buf+length-1) ) {
  607. memcpy(nic_packet->buffer, skb->data, length);
  608. buf = (unsigned long)phys_nic_packet;
  609. }
  610. dma_cache_maintenance(buf, length, 1);
  611. stat = sendpkt(lance_target, (unsigned char *)buf, length);
  612. ENABLE_IRQ();
  613. stdma_release();
  614. dev->trans_start = jiffies;
  615. dev->tbusy = 0;
  616. lp->stats.tx_packets++;
  617. lp->stats.tx_bytes+=length;
  618. }
  619. dev_kfree_skb(skb);
  620. return 0;
  621. }
  622. /* We have a good packet(s), get it/them out of the buffers.
  623. */
  624. static void
  625. pamsnet_poll_rx(struct net_device *dev) {
  626. struct net_local *lp = netdev_priv(dev);
  627. int boguscount;
  628. int pkt_len;
  629. struct sk_buff *skb;
  630. unsigned long flags;
  631. local_irq_save(flags);
  632. /* ++roman: Take care at locking the ST-DMA... This must be done with ints
  633. * off, since otherwise an int could slip in between the question and the
  634. * locking itself, and then we'd go to sleep... And locking itself is
  635. * necessary to keep the floppy_change timer from working with ST-DMA
  636. * registers. */
  637. if (stdma_islocked()) {
  638. local_irq_restore(flags);
  639. return;
  640. }
  641. stdma_lock(pamsnet_intr, NULL);
  642. DISABLE_IRQ();
  643. local_irq_restore(flags);
  644. boguscount = testpkt(lance_target);
  645. if( lp->poll_time < MAX_POLL_TIME ) lp->poll_time++;
  646. while(boguscount--) {
  647. pkt_len = receivepkt(lance_target, phys_nic_packet);
  648. if( pkt_len < 60 ) break;
  649. /* Good packet... */
  650. dma_cache_maintenance((unsigned long)phys_nic_packet, pkt_len, 0);
  651. lp->poll_time = pamsnet_min_poll_time; /* fast poll */
  652. if( pkt_len >= 60 && pkt_len <= 2048 ) {
  653. if (pkt_len > 1514)
  654. pkt_len = 1514;
  655. /* Malloc up new buffer.
  656. */
  657. skb = alloc_skb(pkt_len, GFP_ATOMIC);
  658. if (skb == NULL) {
  659. printk("%s: Memory squeeze, dropping packet.\n",
  660. dev->name);
  661. lp->stats.rx_dropped++;
  662. break;
  663. }
  664. skb->len = pkt_len;
  665. skb->dev = dev;
  666. /* 'skb->data' points to the start of sk_buff data area.
  667. */
  668. memcpy(skb->data, nic_packet->buffer, pkt_len);
  669. netif_rx(skb);
  670. dev->last_rx = jiffies;
  671. lp->stats.rx_packets++;
  672. lp->stats.rx_bytes+=pkt_len;
  673. }
  674. }
  675. /* If any worth-while packets have been received, dev_rint()
  676. has done a mark_bh(INET_BH) for us and will work on them
  677. when we get to the bottom-half routine.
  678. */
  679. ENABLE_IRQ();
  680. stdma_release();
  681. return;
  682. }
  683. /* pamsnet_tick: called by pamsnet_timer. Reads packets from the adapter,
  684. * passes them to the higher layers and restarts the timer.
  685. */
  686. static void
  687. pamsnet_tick(unsigned long data) {
  688. struct net_device *dev = (struct net_device *)data;
  689. struct net_local *lp = netdev_priv(dev);
  690. if( pamsnet_debug > 0 && (lp->open_time++ & 7) == 8 )
  691. printk("pamsnet_tick: %ld\n", lp->open_time);
  692. pamsnet_poll_rx(dev);
  693. pamsnet_timer.expires = jiffies + lp->poll_time;
  694. add_timer(&pamsnet_timer);
  695. }
  696. /* The inverse routine to pamsnet_open().
  697. */
  698. static int
  699. pamsnet_close(struct net_device *dev) {
  700. struct net_local *lp = netdev_priv(dev);
  701. if (pamsnet_debug > 0)
  702. printk("pamsnet_close, open_time=%ld\n", lp->open_time);
  703. del_timer(&pamsnet_timer);
  704. stdma_lock(pamsnet_intr, NULL);
  705. DISABLE_IRQ();
  706. if (if_up)
  707. stop(lance_target);
  708. if_up = 0;
  709. lp->open_time = 0;
  710. dev->tbusy = 1;
  711. dev->start = 0;
  712. ENABLE_IRQ();
  713. stdma_release();
  714. return 0;
  715. }
  716. /* Get the current statistics.
  717. This may be called with the card open or closed.
  718. */
  719. static struct net_device_stats *net_get_stats(struct net_device *dev)
  720. {
  721. struct net_local *lp = netdev_priv(dev);
  722. return &lp->stats;
  723. }
  724. #ifdef MODULE
  725. static struct net_device *pam_dev;
  726. int init_module(void)
  727. {
  728. pam_dev = pamsnet_probe(-1);
  729. if (IS_ERR(pam_dev))
  730. return PTR_ERR(pam_dev);
  731. return 0;
  732. }
  733. void cleanup_module(void)
  734. {
  735. unregister_netdev(pam_dev);
  736. free_netdev(pam_dev);
  737. }
  738. #endif /* MODULE */
  739. /* Local variables:
  740. * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/include
  741. -b m68k-linuxaout -Wall -Wstrict-prototypes -O2
  742. -fomit-frame-pointer -pipe -DMODULE -I../../net/inet -c atari_pamsnet.c"
  743. * version-control: t
  744. * kept-new-versions: 5
  745. * tab-width: 8
  746. * End:
  747. */