1/*
2** -----------------------------------------------------------------------------
3**
4**  Perle Specialix driver for Linux
5**  Ported from existing RIO Driver for SCO sources.
6 *
7 *  (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 *      This program is free software; you can redistribute it and/or modify
10 *      it under the terms of the GNU General Public License as published by
11 *      the Free Software Foundation; either version 2 of the License, or
12 *      (at your option) any later version.
13 *
14 *      This program is distributed in the hope that it will be useful,
15 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *      GNU General Public License for more details.
18 *
19 *      You should have received a copy of the GNU General Public License
20 *      along with this program; if not, write to the Free Software
21 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23**	Module		: rioparam.c
24**	SID		: 1.3
25**	Last Modified	: 11/6/98 10:33:45
26**	Retrieved	: 11/6/98 10:33:50
27**
28**  ident @(#)rioparam.c	1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifdef SCCS_LABELS
34static char *_rioparam_c_sccs_ = "@(#)rioparam.c	1.3";
35#endif
36
37#include <linux/module.h>
38#include <linux/slab.h>
39#include <linux/errno.h>
40#include <linux/tty.h>
41#include <asm/io.h>
42#include <asm/system.h>
43#include <asm/string.h>
44#include <asm/semaphore.h>
45#include <asm/uaccess.h>
46
47#include <linux/termios.h>
48#include <linux/serial.h>
49
50#include <linux/generic_serial.h>
51
52
53#include "linux_compat.h"
54#include "rio_linux.h"
55#include "pkt.h"
56#include "daemon.h"
57#include "rio.h"
58#include "riospace.h"
59#include "cmdpkt.h"
60#include "map.h"
61#include "rup.h"
62#include "port.h"
63#include "riodrvr.h"
64#include "rioinfo.h"
65#include "func.h"
66#include "errors.h"
67#include "pci.h"
68
69#include "parmmap.h"
70#include "unixrup.h"
71#include "board.h"
72#include "host.h"
73#include "phb.h"
74#include "link.h"
75#include "cmdblk.h"
76#include "route.h"
77#include "cirrus.h"
78#include "rioioctl.h"
79#include "param.h"
80
81
82
83/*
84** The Scam, based on email from jeremyr@bugs.specialix.co.uk....
85**
86** To send a command on a particular port, you put a packet with the
87** command bit set onto the port. The command bit is in the len field,
88** and gets ORed in with the actual byte count.
89**
90** When you send a packet with the command bit set the first
91** data byte (data[0]) is interpreted as the command to execute.
92** It also governs what data structure overlay should accompany the packet.
93** Commands are defined in cirrus/cirrus.h
94**
95** If you want the command to pre-emt data already on the queue for the
96** port, set the pre-emptive bit in conjunction with the command bit.
97** It is not defined what will happen if you set the preemptive bit
98** on a packet that is NOT a command.
99**
100** Pre-emptive commands should be queued at the head of the queue using
101** add_start(), whereas normal commands and data are enqueued using
102** add_end().
103**
104** Most commands do not use the remaining bytes in the data array. The
105** exceptions are OPEN MOPEN and CONFIG. (NB. As with the SI CONFIG and
106** OPEN are currently analogous). With these three commands the following
107** 11 data bytes are all used to pass config information such as baud rate etc.
108** The fields are also defined in cirrus.h. Some contain straightforward
109** information such as the transmit XON character. Two contain the transmit and
110** receive baud rates respectively. For most baud rates there is a direct
111** mapping between the rates defined in <sys/termio.h> and the byte in the
112** packet. There are additional (non UNIX-standard) rates defined in
113** /u/dos/rio/cirrus/h/brates.h.
114**
115** The rest of the data fields contain approximations to the Cirrus registers
116** that are used to program number of bits etc. Each registers bit fields is
117** defined in cirrus.h.
118**
119** NB. Only use those bits that are defined as being driver specific
120** or common to the RTA and the driver.
121**
122** All commands going from RTA->Host will be dealt with by the Host code - you
123** will never see them. As with the SI there will be three fields to look out
124** for in each phb (not yet defined - needs defining a.s.a.p).
125**
126** modem_status	- current state of handshake pins.
127**
128** port_status	 - current port status - equivalent to hi_stat for SI, indicates
129** if port is IDLE_OPEN, IDLE_CLOSED etc.
130**
131** break_status	- bit X set if break has been received.
132**
133** Happy hacking.
134**
135*/
136
137/*
138** RIOParam is used to open or configure a port. You pass it a PortP,
139** which will have a tty struct attached to it. You also pass a command,
140** either OPEN or CONFIG. The port's setup is taken from the t_ fields
141** of the tty struct inside the PortP, and the port is either opened
142** or re-configured. You must also tell RIOParam if the device is a modem
143** device or not (i.e. top bit of minor number set or clear - take special
144** care when deciding on this!).
145** RIOParam neither flushes nor waits for drain, and is NOT preemptive.
146**
147** RIOParam assumes it will be called at splrio(), and also assumes
148** that CookMode is set correctly in the port structure.
149**
150** NB. for MPX
151**	tty lock must NOT have been previously acquired.
152*/
153int RIOParam(struct Port *PortP, int cmd, int Modem, int SleepFlag)
154{
155	struct tty_struct *TtyP;
156	int retval;
157	struct phb_param __iomem *phb_param_ptr;
158	struct PKT __iomem *PacketP;
159	int res;
160	u8 Cor1 = 0, Cor2 = 0, Cor4 = 0, Cor5 = 0;
161	u8 TxXon = 0, TxXoff = 0, RxXon = 0, RxXoff = 0;
162	u8 LNext = 0, TxBaud = 0, RxBaud = 0;
163	int retries = 0xff;
164	unsigned long flags;
165
166	func_enter();
167
168	TtyP = PortP->gs.tty;
169
170	rio_dprintk(RIO_DEBUG_PARAM, "RIOParam: Port:%d cmd:%d Modem:%d SleepFlag:%d Mapped: %d, tty=%p\n", PortP->PortNum, cmd, Modem, SleepFlag, PortP->Mapped, TtyP);
171
172	if (!TtyP) {
173		rio_dprintk(RIO_DEBUG_PARAM, "Can't call rioparam with null tty.\n");
174
175		func_exit();
176
177		return RIO_FAIL;
178	}
179	rio_spin_lock_irqsave(&PortP->portSem, flags);
180
181	if (cmd == OPEN) {
182		/*
183		 ** If the port is set to store or lock the parameters, and it is
184		 ** paramed with OPEN, we want to restore the saved port termio, but
185		 ** only if StoredTermio has been saved, i.e. NOT 1st open after reboot.
186		 */
187	}
188
189	/*
190	 ** wait for space
191	 */
192	while (!(res = can_add_transmit(&PacketP, PortP)) || (PortP->InUse != NOT_INUSE)) {
193		if (retries-- <= 0) {
194			break;
195		}
196		if (PortP->InUse != NOT_INUSE) {
197			rio_dprintk(RIO_DEBUG_PARAM, "Port IN_USE for pre-emptive command\n");
198		}
199
200		if (!res) {
201			rio_dprintk(RIO_DEBUG_PARAM, "Port has no space on transmit queue\n");
202		}
203
204		if (SleepFlag != OK_TO_SLEEP) {
205			rio_spin_unlock_irqrestore(&PortP->portSem, flags);
206			func_exit();
207
208			return RIO_FAIL;
209		}
210
211		rio_dprintk(RIO_DEBUG_PARAM, "wait for can_add_transmit\n");
212		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
213		retval = RIODelay(PortP, HUNDRED_MS);
214		rio_spin_lock_irqsave(&PortP->portSem, flags);
215		if (retval == RIO_FAIL) {
216			rio_dprintk(RIO_DEBUG_PARAM, "wait for can_add_transmit broken by signal\n");
217			rio_spin_unlock_irqrestore(&PortP->portSem, flags);
218			func_exit();
219			return -EINTR;
220		}
221		if (PortP->State & RIO_DELETED) {
222			rio_spin_unlock_irqrestore(&PortP->portSem, flags);
223			func_exit();
224			return 0;
225		}
226	}
227
228	if (!res) {
229		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
230		func_exit();
231
232		return RIO_FAIL;
233	}
234
235	rio_dprintk(RIO_DEBUG_PARAM, "can_add_transmit() returns %x\n", res);
236	rio_dprintk(RIO_DEBUG_PARAM, "Packet is %p\n", PacketP);
237
238	phb_param_ptr = (struct phb_param __iomem *) PacketP->data;
239
240
241	switch (TtyP->termios->c_cflag & CSIZE) {
242	case CS5:
243		{
244			rio_dprintk(RIO_DEBUG_PARAM, "5 bit data\n");
245			Cor1 |= COR1_5BITS;
246			break;
247		}
248	case CS6:
249		{
250			rio_dprintk(RIO_DEBUG_PARAM, "6 bit data\n");
251			Cor1 |= COR1_6BITS;
252			break;
253		}
254	case CS7:
255		{
256			rio_dprintk(RIO_DEBUG_PARAM, "7 bit data\n");
257			Cor1 |= COR1_7BITS;
258			break;
259		}
260	case CS8:
261		{
262			rio_dprintk(RIO_DEBUG_PARAM, "8 bit data\n");
263			Cor1 |= COR1_8BITS;
264			break;
265		}
266	}
267
268	if (TtyP->termios->c_cflag & CSTOPB) {
269		rio_dprintk(RIO_DEBUG_PARAM, "2 stop bits\n");
270		Cor1 |= COR1_2STOP;
271	} else {
272		rio_dprintk(RIO_DEBUG_PARAM, "1 stop bit\n");
273		Cor1 |= COR1_1STOP;
274	}
275
276	if (TtyP->termios->c_cflag & PARENB) {
277		rio_dprintk(RIO_DEBUG_PARAM, "Enable parity\n");
278		Cor1 |= COR1_NORMAL;
279	} else {
280		rio_dprintk(RIO_DEBUG_PARAM, "Disable parity\n");
281		Cor1 |= COR1_NOP;
282	}
283	if (TtyP->termios->c_cflag & PARODD) {
284		rio_dprintk(RIO_DEBUG_PARAM, "Odd parity\n");
285		Cor1 |= COR1_ODD;
286	} else {
287		rio_dprintk(RIO_DEBUG_PARAM, "Even parity\n");
288		Cor1 |= COR1_EVEN;
289	}
290
291	/*
292	 ** COR 2
293	 */
294	if (TtyP->termios->c_iflag & IXON) {
295		rio_dprintk(RIO_DEBUG_PARAM, "Enable start/stop output control\n");
296		Cor2 |= COR2_IXON;
297	} else {
298		if (PortP->Config & RIO_IXON) {
299			rio_dprintk(RIO_DEBUG_PARAM, "Force enable start/stop output control\n");
300			Cor2 |= COR2_IXON;
301		} else
302			rio_dprintk(RIO_DEBUG_PARAM, "IXON has been disabled.\n");
303	}
304
305	if (TtyP->termios->c_iflag & IXANY) {
306		if (PortP->Config & RIO_IXANY) {
307			rio_dprintk(RIO_DEBUG_PARAM, "Enable any key to restart output\n");
308			Cor2 |= COR2_IXANY;
309		} else
310			rio_dprintk(RIO_DEBUG_PARAM, "IXANY has been disabled due to sanity reasons.\n");
311	}
312
313	if (TtyP->termios->c_iflag & IXOFF) {
314		rio_dprintk(RIO_DEBUG_PARAM, "Enable start/stop input control 2\n");
315		Cor2 |= COR2_IXOFF;
316	}
317
318	if (TtyP->termios->c_cflag & HUPCL) {
319		rio_dprintk(RIO_DEBUG_PARAM, "Hangup on last close\n");
320		Cor2 |= COR2_HUPCL;
321	}
322
323	if (C_CRTSCTS(TtyP)) {
324		rio_dprintk(RIO_DEBUG_PARAM, "Rx hardware flow control enabled\n");
325		Cor2 |= COR2_CTSFLOW;
326		Cor2 |= COR2_RTSFLOW;
327	} else {
328		rio_dprintk(RIO_DEBUG_PARAM, "Rx hardware flow control disabled\n");
329		Cor2 &= ~COR2_CTSFLOW;
330		Cor2 &= ~COR2_RTSFLOW;
331	}
332
333
334	if (TtyP->termios->c_cflag & CLOCAL) {
335		rio_dprintk(RIO_DEBUG_PARAM, "Local line\n");
336	} else {
337		rio_dprintk(RIO_DEBUG_PARAM, "Possible Modem line\n");
338	}
339
340	/*
341	 ** COR 4 (there is no COR 3)
342	 */
343	if (TtyP->termios->c_iflag & IGNBRK) {
344		rio_dprintk(RIO_DEBUG_PARAM, "Ignore break condition\n");
345		Cor4 |= COR4_IGNBRK;
346	}
347	if (!(TtyP->termios->c_iflag & BRKINT)) {
348		rio_dprintk(RIO_DEBUG_PARAM, "Break generates NULL condition\n");
349		Cor4 |= COR4_NBRKINT;
350	} else {
351		rio_dprintk(RIO_DEBUG_PARAM, "Interrupt on	break condition\n");
352	}
353
354	if (TtyP->termios->c_iflag & INLCR) {
355		rio_dprintk(RIO_DEBUG_PARAM, "Map newline to carriage return on input\n");
356		Cor4 |= COR4_INLCR;
357	}
358
359	if (TtyP->termios->c_iflag & IGNCR) {
360		rio_dprintk(RIO_DEBUG_PARAM, "Ignore carriage return on input\n");
361		Cor4 |= COR4_IGNCR;
362	}
363
364	if (TtyP->termios->c_iflag & ICRNL) {
365		rio_dprintk(RIO_DEBUG_PARAM, "Map carriage return to newline on input\n");
366		Cor4 |= COR4_ICRNL;
367	}
368	if (TtyP->termios->c_iflag & IGNPAR) {
369		rio_dprintk(RIO_DEBUG_PARAM, "Ignore characters with parity errors\n");
370		Cor4 |= COR4_IGNPAR;
371	}
372	if (TtyP->termios->c_iflag & PARMRK) {
373		rio_dprintk(RIO_DEBUG_PARAM, "Mark parity errors\n");
374		Cor4 |= COR4_PARMRK;
375	}
376
377	/*
378	 ** Set the RAISEMOD flag to ensure that the modem lines are raised
379	 ** on reception of a config packet.
380	 ** The download code handles the zero baud condition.
381	 */
382	Cor4 |= COR4_RAISEMOD;
383
384	/*
385	 ** COR 5
386	 */
387
388	Cor5 = COR5_CMOE;
389
390	/*
391	 ** Set to monitor tbusy/tstop (or not).
392	 */
393
394	if (PortP->MonitorTstate)
395		Cor5 |= COR5_TSTATE_ON;
396	else
397		Cor5 |= COR5_TSTATE_OFF;
398
399	/*
400	 ** Could set LNE here if you wanted LNext processing. SVR4 will use it.
401	 */
402	if (TtyP->termios->c_iflag & ISTRIP) {
403		rio_dprintk(RIO_DEBUG_PARAM, "Strip input characters\n");
404		if (!(PortP->State & RIO_TRIAD_MODE)) {
405			Cor5 |= COR5_ISTRIP;
406		}
407	}
408
409	if (TtyP->termios->c_oflag & ONLCR) {
410		rio_dprintk(RIO_DEBUG_PARAM, "Map newline to carriage-return, newline on output\n");
411		if (PortP->CookMode == COOK_MEDIUM)
412			Cor5 |= COR5_ONLCR;
413	}
414	if (TtyP->termios->c_oflag & OCRNL) {
415		rio_dprintk(RIO_DEBUG_PARAM, "Map carriage return to newline on output\n");
416		if (PortP->CookMode == COOK_MEDIUM)
417			Cor5 |= COR5_OCRNL;
418	}
419	if ((TtyP->termios->c_oflag & TABDLY) == TAB3) {
420		rio_dprintk(RIO_DEBUG_PARAM, "Tab delay 3 set\n");
421		if (PortP->CookMode == COOK_MEDIUM)
422			Cor5 |= COR5_TAB3;
423	}
424
425	/*
426	 ** Flow control bytes.
427	 */
428	TxXon = TtyP->termios->c_cc[VSTART];
429	TxXoff = TtyP->termios->c_cc[VSTOP];
430	RxXon = TtyP->termios->c_cc[VSTART];
431	RxXoff = TtyP->termios->c_cc[VSTOP];
432	/*
433	 ** LNEXT byte
434	 */
435	LNext = 0;
436
437	/*
438	 ** Baud rate bytes
439	 */
440	rio_dprintk(RIO_DEBUG_PARAM, "Mapping of rx/tx baud %x (%x)\n", TtyP->termios->c_cflag, CBAUD);
441
442	switch (TtyP->termios->c_cflag & CBAUD) {
443#define e(b) case B ## b : RxBaud = TxBaud = RIO_B ## b ;break
444		e(50);
445		e(75);
446		e(110);
447		e(134);
448		e(150);
449		e(200);
450		e(300);
451		e(600);
452		e(1200);
453		e(1800);
454		e(2400);
455		e(4800);
456		e(9600);
457		e(19200);
458		e(38400);
459		e(57600);
460		e(115200);	/* e(230400);e(460800); e(921600);  */
461	}
462
463	rio_dprintk(RIO_DEBUG_PARAM, "tx baud 0x%x, rx baud 0x%x\n", TxBaud, RxBaud);
464
465
466	/*
467	 ** Leftovers
468	 */
469	if (TtyP->termios->c_cflag & CREAD)
470		rio_dprintk(RIO_DEBUG_PARAM, "Enable receiver\n");
471#ifdef RCV1EN
472	if (TtyP->termios->c_cflag & RCV1EN)
473		rio_dprintk(RIO_DEBUG_PARAM, "RCV1EN (?)\n");
474#endif
475#ifdef XMT1EN
476	if (TtyP->termios->c_cflag & XMT1EN)
477		rio_dprintk(RIO_DEBUG_PARAM, "XMT1EN (?)\n");
478#endif
479	if (TtyP->termios->c_lflag & ISIG)
480		rio_dprintk(RIO_DEBUG_PARAM, "Input character signal generating enabled\n");
481	if (TtyP->termios->c_lflag & ICANON)
482		rio_dprintk(RIO_DEBUG_PARAM, "Canonical input: erase and kill enabled\n");
483	if (TtyP->termios->c_lflag & XCASE)
484		rio_dprintk(RIO_DEBUG_PARAM, "Canonical upper/lower presentation\n");
485	if (TtyP->termios->c_lflag & ECHO)
486		rio_dprintk(RIO_DEBUG_PARAM, "Enable input echo\n");
487	if (TtyP->termios->c_lflag & ECHOE)
488		rio_dprintk(RIO_DEBUG_PARAM, "Enable echo erase\n");
489	if (TtyP->termios->c_lflag & ECHOK)
490		rio_dprintk(RIO_DEBUG_PARAM, "Enable echo kill\n");
491	if (TtyP->termios->c_lflag & ECHONL)
492		rio_dprintk(RIO_DEBUG_PARAM, "Enable echo newline\n");
493	if (TtyP->termios->c_lflag & NOFLSH)
494		rio_dprintk(RIO_DEBUG_PARAM, "Disable flush after interrupt or quit\n");
495#ifdef TOSTOP
496	if (TtyP->termios->c_lflag & TOSTOP)
497		rio_dprintk(RIO_DEBUG_PARAM, "Send SIGTTOU for background output\n");
498#endif
499#ifdef XCLUDE
500	if (TtyP->termios->c_lflag & XCLUDE)
501		rio_dprintk(RIO_DEBUG_PARAM, "Exclusive use of this line\n");
502#endif
503	if (TtyP->termios->c_iflag & IUCLC)
504		rio_dprintk(RIO_DEBUG_PARAM, "Map uppercase to lowercase on input\n");
505	if (TtyP->termios->c_oflag & OPOST)
506		rio_dprintk(RIO_DEBUG_PARAM, "Enable output post-processing\n");
507	if (TtyP->termios->c_oflag & OLCUC)
508		rio_dprintk(RIO_DEBUG_PARAM, "Map lowercase to uppercase on output\n");
509	if (TtyP->termios->c_oflag & ONOCR)
510		rio_dprintk(RIO_DEBUG_PARAM, "No carriage return output at column 0\n");
511	if (TtyP->termios->c_oflag & ONLRET)
512		rio_dprintk(RIO_DEBUG_PARAM, "Newline performs carriage return function\n");
513	if (TtyP->termios->c_oflag & OFILL)
514		rio_dprintk(RIO_DEBUG_PARAM, "Use fill characters for delay\n");
515	if (TtyP->termios->c_oflag & OFDEL)
516		rio_dprintk(RIO_DEBUG_PARAM, "Fill character is DEL\n");
517	if (TtyP->termios->c_oflag & NLDLY)
518		rio_dprintk(RIO_DEBUG_PARAM, "Newline delay set\n");
519	if (TtyP->termios->c_oflag & CRDLY)
520		rio_dprintk(RIO_DEBUG_PARAM, "Carriage return delay set\n");
521	if (TtyP->termios->c_oflag & TABDLY)
522		rio_dprintk(RIO_DEBUG_PARAM, "Tab delay set\n");
523	/*
524	 ** These things are kind of useful in a later life!
525	 */
526	PortP->Cor2Copy = Cor2;
527
528	if (PortP->State & RIO_DELETED) {
529		rio_spin_unlock_irqrestore(&PortP->portSem, flags);
530		func_exit();
531
532		return RIO_FAIL;
533	}
534
535	/*
536	 ** Actually write the info into the packet to be sent
537	 */
538	writeb(cmd, &phb_param_ptr->Cmd);
539	writeb(Cor1, &phb_param_ptr->Cor1);
540	writeb(Cor2, &phb_param_ptr->Cor2);
541	writeb(Cor4, &phb_param_ptr->Cor4);
542	writeb(Cor5, &phb_param_ptr->Cor5);
543	writeb(TxXon, &phb_param_ptr->TxXon);
544	writeb(RxXon, &phb_param_ptr->RxXon);
545	writeb(TxXoff, &phb_param_ptr->TxXoff);
546	writeb(RxXoff, &phb_param_ptr->RxXoff);
547	writeb(LNext, &phb_param_ptr->LNext);
548	writeb(TxBaud, &phb_param_ptr->TxBaud);
549	writeb(RxBaud, &phb_param_ptr->RxBaud);
550
551	/*
552	 ** Set the length/command field
553	 */
554	writeb(12 | PKT_CMD_BIT, &PacketP->len);
555
556	/*
557	 ** The packet is formed - now, whack it off
558	 ** to its final destination:
559	 */
560	add_transmit(PortP);
561	/*
562	 ** Count characters transmitted for port statistics reporting
563	 */
564	if (PortP->statsGather)
565		PortP->txchars += 12;
566
567	rio_spin_unlock_irqrestore(&PortP->portSem, flags);
568
569	rio_dprintk(RIO_DEBUG_PARAM, "add_transmit returned.\n");
570	/*
571	 ** job done.
572	 */
573	func_exit();
574
575	return 0;
576}
577
578
579/*
580** We can add another packet to a transmit queue if the packet pointer pointed
581** to by the TxAdd pointer has PKT_IN_USE clear in its address.
582*/
583int can_add_transmit(struct PKT __iomem **PktP, struct Port *PortP)
584{
585	struct PKT __iomem *tp;
586
587	*PktP = tp = (struct PKT __iomem *) RIO_PTR(PortP->Caddr, readw(PortP->TxAdd));
588
589	return !((unsigned long) tp & PKT_IN_USE);
590}
591
592/*
593** To add a packet to the queue, you set the PKT_IN_USE bit in the address,
594** and then move the TxAdd pointer along one position to point to the next
595** packet pointer. You must wrap the pointer from the end back to the start.
596*/
597void add_transmit(struct Port *PortP)
598{
599	if (readw(PortP->TxAdd) & PKT_IN_USE) {
600		rio_dprintk(RIO_DEBUG_PARAM, "add_transmit: Packet has been stolen!");
601	}
602	writew(readw(PortP->TxAdd) | PKT_IN_USE, PortP->TxAdd);
603	PortP->TxAdd = (PortP->TxAdd == PortP->TxEnd) ? PortP->TxStart : PortP->TxAdd + 1;
604	writew(RIO_OFF(PortP->Caddr, PortP->TxAdd), &PortP->PhbP->tx_add);
605}
606
607/****************************************
608 * Put a packet onto the end of the
609 * free list
610 ****************************************/
611void put_free_end(struct Host *HostP, struct PKT __iomem *PktP)
612{
613	struct rio_free_list __iomem *tmp_pointer;
614	unsigned short old_end, new_end;
615	unsigned long flags;
616
617	rio_spin_lock_irqsave(&HostP->HostLock, flags);
618
619	 /*************************************************
620	* Put a packet back onto the back of the free list
621	*
622	************************************************/
623
624	rio_dprintk(RIO_DEBUG_PFE, "put_free_end(PktP=%p)\n", PktP);
625
626	if ((old_end = readw(&HostP->ParmMapP->free_list_end)) != TPNULL) {
627		new_end = RIO_OFF(HostP->Caddr, PktP);
628		tmp_pointer = (struct rio_free_list __iomem *) RIO_PTR(HostP->Caddr, old_end);
629		writew(new_end, &tmp_pointer->next);
630		writew(old_end, &((struct rio_free_list __iomem *) PktP)->prev);
631		writew(TPNULL, &((struct rio_free_list __iomem *) PktP)->next);
632		writew(new_end, &HostP->ParmMapP->free_list_end);
633	} else {		/* First packet on the free list this should never happen! */
634		rio_dprintk(RIO_DEBUG_PFE, "put_free_end(): This should never happen\n");
635		writew(RIO_OFF(HostP->Caddr, PktP), &HostP->ParmMapP->free_list_end);
636		tmp_pointer = (struct rio_free_list __iomem *) PktP;
637		writew(TPNULL, &tmp_pointer->prev);
638		writew(TPNULL, &tmp_pointer->next);
639	}
640	rio_dprintk(RIO_DEBUG_CMD, "Before unlock: %p\n", &HostP->HostLock);
641	rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
642}
643
644/*
645** can_remove_receive(PktP,P) returns non-zero if PKT_IN_USE is set
646** for the next packet on the queue. It will also set PktP to point to the
647** relevant packet, [having cleared the PKT_IN_USE bit]. If PKT_IN_USE is clear,
648** then can_remove_receive() returns 0.
649*/
650int can_remove_receive(struct PKT __iomem **PktP, struct Port *PortP)
651{
652	if (readw(PortP->RxRemove) & PKT_IN_USE) {
653		*PktP = (struct PKT __iomem *) RIO_PTR(PortP->Caddr, readw(PortP->RxRemove) & ~PKT_IN_USE);
654		return 1;
655	}
656	return 0;
657}
658
659/*
660** To remove a packet from the receive queue you clear its PKT_IN_USE bit,
661** and then bump the pointers. Once the pointers get to the end, they must
662** be wrapped back to the start.
663*/
664void remove_receive(struct Port *PortP)
665{
666	writew(readw(PortP->RxRemove) & ~PKT_IN_USE, PortP->RxRemove);
667	PortP->RxRemove = (PortP->RxRemove == PortP->RxEnd) ? PortP->RxStart : PortP->RxRemove + 1;
668	writew(RIO_OFF(PortP->Caddr, PortP->RxRemove), &PortP->PhbP->rx_remove);
669}
670