aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/moxa.c
diff options
context:
space:
mode:
authorAlan Cox <alan@lxorguk.ukuu.org.uk>2006-01-09 23:54:13 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-01-10 11:01:59 -0500
commit33f0f88f1c51ae5c2d593d26960c760ea154c2e2 (patch)
treef53a38cf49406863f079d74d0e8f91b276f7c1a9 /drivers/char/moxa.c
parent6ed80991a2dce4afc113be35089c564d62fa1f11 (diff)
[PATCH] TTY layer buffering revamp
The API and code have been through various bits of initial review by serial driver people but they definitely need to live somewhere for a while so the unconverted drivers can get knocked into shape, existing drivers that have been updated can be better tuned and bugs whacked out. This replaces the tty flip buffers with kmalloc objects in rings. In the normal situation for an IRQ driven serial port at typical speeds the behaviour is pretty much the same, two buffers end up allocated and the kernel cycles between them as before. When there are delays or at high speed we now behave far better as the buffer pool can grow a bit rather than lose characters. This also means that we can operate at higher speeds reliably. For drivers that receive characters in blocks (DMA based, USB and especially virtualisation) the layer allows a lot of driver specific code that works around the tty layer with private secondary queues to be removed. The IBM folks need this sort of layer, the smart serial port people do, the virtualisers do (because a virtualised tty typically operates at infinite speed rather than emulating 9600 baud). Finally many drivers had invalid and unsafe attempts to avoid buffer overflows by directly invoking tty methods extracted out of the innards of work queue structs. These are no longer needed and all go away. That fixes various random hangs with serial ports on overflow. The other change in here is to optimise the receive_room path that is used by some callers. It turns out that only one ldisc uses receive room except asa constant and it updates it far far less than the value is read. We thus make it a variable not a function call. I expect the code to contain bugs due to the size alone but I'll be watching and squashing them and feeding out new patches as it goes. Because the buffers now dynamically expand you should only run out of buffering when the kernel runs out of memory for real. That means a lot of the horrible hacks high performance drivers used to do just aren't needed any more. Description: tty_insert_flip_char is an old API and continues to work as before, as does tty_flip_buffer_push() [this is why many drivers dont need modification]. It does now also return the number of chars inserted There are also tty_buffer_request_room(tty, len) which asks for a buffer block of the length requested and returns the space found. This improves efficiency with hardware that knows how much to transfer. and tty_insert_flip_string_flags(tty, str, flags, len) to insert a string of characters and flags For a smart interface the usual code is len = tty_request_buffer_room(tty, amount_hardware_says); tty_insert_flip_string(tty, buffer_from_card, len); More description! At the moment tty buffers are attached directly to the tty. This is causing a lot of the problems related to tty layer locking, also problems at high speed and also with bursty data (such as occurs in virtualised environments) I'm working on ripping out the flip buffers and replacing them with a pool of dynamically allocated buffers. This allows both for old style "byte I/O" devices and also helps virtualisation and smart devices where large blocks of data suddenely materialise and need storing. So far so good. Lots of drivers reference tty->flip.*. Several of them also call directly and unsafely into function pointers it provides. This will all break. Most drivers can use tty_insert_flip_char which can be kept as an API but others need more. At the moment I've added the following interfaces, if people think more will be needed now is a good time to say int tty_buffer_request_room(tty, size) Try and ensure at least size bytes are available, returns actual room (may be zero). At the moment it just uses the flipbuf space but that will change. Repeated calls without characters being added are not cumulative. (ie if you call it with 1, 1, 1, and then 4 you'll have four characters of space. The other functions will also try and grow buffers in future but this will be a more efficient way when you know block sizes. int tty_insert_flip_char(tty, ch, flag) As before insert a character if there is room. Now returns 1 for success, 0 for failure. int tty_insert_flip_string(tty, str, len) Insert a block of non error characters. Returns the number inserted. int tty_prepare_flip_string(tty, strptr, len) Adjust the buffer to allow len characters to be added. Returns a buffer pointer in strptr and the length available. This allows for hardware that needs to use functions like insl or mencpy_fromio. Signed-off-by: Alan Cox <alan@redhat.com> Cc: Paul Fulghum <paulkf@microgate.com> Signed-off-by: Hirokazu Takata <takata@linux-m32r.org> Signed-off-by: Serge Hallyn <serue@us.ibm.com> Signed-off-by: Jeff Dike <jdike@addtoit.com> Signed-off-by: John Hawkes <hawkes@sgi.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com> Signed-off-by: Adrian Bunk <bunk@stusta.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'drivers/char/moxa.c')
-rw-r--r--drivers/char/moxa.c73
1 files changed, 30 insertions, 43 deletions
diff --git a/drivers/char/moxa.c b/drivers/char/moxa.c
index 46a3a8ccd65f..5e3ef5522194 100644
--- a/drivers/char/moxa.c
+++ b/drivers/char/moxa.c
@@ -269,7 +269,7 @@ static int MoxaPortDCDChange(int);
269static int MoxaPortDCDON(int); 269static int MoxaPortDCDON(int);
270static void MoxaPortFlushData(int, int); 270static void MoxaPortFlushData(int, int);
271static int MoxaPortWriteData(int, unsigned char *, int); 271static int MoxaPortWriteData(int, unsigned char *, int);
272static int MoxaPortReadData(int, unsigned char *, int); 272static int MoxaPortReadData(int, struct tty_struct *tty);
273static int MoxaPortTxQueue(int); 273static int MoxaPortTxQueue(int);
274static int MoxaPortRxQueue(int); 274static int MoxaPortRxQueue(int);
275static int MoxaPortTxFree(int); 275static int MoxaPortTxFree(int);
@@ -301,6 +301,8 @@ static struct tty_operations moxa_ops = {
301 .tiocmset = moxa_tiocmset, 301 .tiocmset = moxa_tiocmset,
302}; 302};
303 303
304static spinlock_t moxa_lock = SPIN_LOCK_UNLOCKED;
305
304#ifdef CONFIG_PCI 306#ifdef CONFIG_PCI
305static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board) 307static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
306{ 308{
@@ -645,10 +647,10 @@ static int moxa_write(struct tty_struct *tty,
645 if (ch == NULL) 647 if (ch == NULL)
646 return (0); 648 return (0);
647 port = ch->port; 649 port = ch->port;
648 save_flags(flags); 650
649 cli(); 651 spin_lock_irqsave(&moxa_lock, flags);
650 len = MoxaPortWriteData(port, (unsigned char *) buf, count); 652 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
651 restore_flags(flags); 653 spin_unlock_irqrestore(&moxa_lock, flags);
652 654
653 /********************************************* 655 /*********************************************
654 if ( !(ch->statusflags & LOWWAIT) && 656 if ( !(ch->statusflags & LOWWAIT) &&
@@ -723,11 +725,10 @@ static void moxa_put_char(struct tty_struct *tty, unsigned char c)
723 if (ch == NULL) 725 if (ch == NULL)
724 return; 726 return;
725 port = ch->port; 727 port = ch->port;
726 save_flags(flags); 728 spin_lock_irqsave(&moxa_lock, flags);
727 cli();
728 moxaXmitBuff[0] = c; 729 moxaXmitBuff[0] = c;
729 MoxaPortWriteData(port, moxaXmitBuff, 1); 730 MoxaPortWriteData(port, moxaXmitBuff, 1);
730 restore_flags(flags); 731 spin_unlock_irqrestore(&moxa_lock, flags);
731 /************************************************ 732 /************************************************
732 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) ) 733 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
733 *************************************************/ 734 *************************************************/
@@ -1030,12 +1031,12 @@ static int block_till_ready(struct tty_struct *tty, struct file *filp,
1030 printk("block_til_ready before block: ttys%d, count = %d\n", 1031 printk("block_til_ready before block: ttys%d, count = %d\n",
1031 ch->line, ch->count); 1032 ch->line, ch->count);
1032#endif 1033#endif
1033 save_flags(flags); 1034 spin_lock_irqsave(&moxa_lock, flags);
1034 cli();
1035 if (!tty_hung_up_p(filp)) 1035 if (!tty_hung_up_p(filp))
1036 ch->count--; 1036 ch->count--;
1037 restore_flags(flags);
1038 ch->blocked_open++; 1037 ch->blocked_open++;
1038 spin_unlock_irqrestore(&moxa_lock, flags);
1039
1039 while (1) { 1040 while (1) {
1040 set_current_state(TASK_INTERRUPTIBLE); 1041 set_current_state(TASK_INTERRUPTIBLE);
1041 if (tty_hung_up_p(filp) || 1042 if (tty_hung_up_p(filp) ||
@@ -1062,17 +1063,21 @@ static int block_till_ready(struct tty_struct *tty, struct file *filp,
1062 } 1063 }
1063 set_current_state(TASK_RUNNING); 1064 set_current_state(TASK_RUNNING);
1064 remove_wait_queue(&ch->open_wait, &wait); 1065 remove_wait_queue(&ch->open_wait, &wait);
1066
1067 spin_lock_irqsave(&moxa_lock, flags);
1065 if (!tty_hung_up_p(filp)) 1068 if (!tty_hung_up_p(filp))
1066 ch->count++; 1069 ch->count++;
1067 ch->blocked_open--; 1070 ch->blocked_open--;
1071 spin_unlock_irqrestore(&moxa_lock, flags);
1068#ifdef SERIAL_DEBUG_OPEN 1072#ifdef SERIAL_DEBUG_OPEN
1069 printk("block_til_ready after blocking: ttys%d, count = %d\n", 1073 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1070 ch->line, ch->count); 1074 ch->line, ch->count);
1071#endif 1075#endif
1072 if (retval) 1076 if (retval)
1073 return (retval); 1077 return (retval);
1078 /* FIXME: review to see if we need to use set_bit on these */
1074 ch->asyncflags |= ASYNC_NORMAL_ACTIVE; 1079 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1075 return (0); 1080 return 0;
1076} 1081}
1077 1082
1078static void setup_empty_event(struct tty_struct *tty) 1083static void setup_empty_event(struct tty_struct *tty)
@@ -1080,15 +1085,14 @@ static void setup_empty_event(struct tty_struct *tty)
1080 struct moxa_str *ch = tty->driver_data; 1085 struct moxa_str *ch = tty->driver_data;
1081 unsigned long flags; 1086 unsigned long flags;
1082 1087
1083 save_flags(flags); 1088 spin_lock_irqsave(&moxa_lock, flags);
1084 cli();
1085 ch->statusflags |= EMPTYWAIT; 1089 ch->statusflags |= EMPTYWAIT;
1086 moxaEmptyTimer_on[ch->port] = 0; 1090 moxaEmptyTimer_on[ch->port] = 0;
1087 del_timer(&moxaEmptyTimer[ch->port]); 1091 del_timer(&moxaEmptyTimer[ch->port]);
1088 moxaEmptyTimer[ch->port].expires = jiffies + HZ; 1092 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1089 moxaEmptyTimer_on[ch->port] = 1; 1093 moxaEmptyTimer_on[ch->port] = 1;
1090 add_timer(&moxaEmptyTimer[ch->port]); 1094 add_timer(&moxaEmptyTimer[ch->port]);
1091 restore_flags(flags); 1095 spin_unlock_irqrestore(&moxa_lock, flags);
1092} 1096}
1093 1097
1094static void check_xmit_empty(unsigned long data) 1098static void check_xmit_empty(unsigned long data)
@@ -1135,8 +1139,6 @@ static void receive_data(struct moxa_str *ch)
1135{ 1139{
1136 struct tty_struct *tp; 1140 struct tty_struct *tp;
1137 struct termios *ts; 1141 struct termios *ts;
1138 int i, count, rc, space;
1139 unsigned char *charptr, *flagptr;
1140 unsigned long flags; 1142 unsigned long flags;
1141 1143
1142 ts = NULL; 1144 ts = NULL;
@@ -1150,24 +1152,10 @@ static void receive_data(struct moxa_str *ch)
1150 MoxaPortFlushData(ch->port, 0); 1152 MoxaPortFlushData(ch->port, 0);
1151 return; 1153 return;
1152 } 1154 }
1153 space = TTY_FLIPBUF_SIZE - tp->flip.count; 1155 spin_lock_irqsave(&moxa_lock, flags);
1154 if (space <= 0) 1156 MoxaPortReadData(ch->port, tp);
1155 return; 1157 spin_unlock_irqrestore(&moxa_lock, flags);
1156 charptr = tp->flip.char_buf_ptr; 1158 tty_schedule_flip(tp);
1157 flagptr = tp->flip.flag_buf_ptr;
1158 rc = tp->flip.count;
1159 save_flags(flags);
1160 cli();
1161 count = MoxaPortReadData(ch->port, charptr, space);
1162 restore_flags(flags);
1163 for (i = 0; i < count; i++)
1164 *flagptr++ = 0;
1165 charptr += count;
1166 rc += count;
1167 tp->flip.count = rc;
1168 tp->flip.char_buf_ptr = charptr;
1169 tp->flip.flag_buf_ptr = flagptr;
1170 tty_schedule_flip(ch->tty);
1171} 1159}
1172 1160
1173#define Magic_code 0x404 1161#define Magic_code 0x404
@@ -1774,7 +1762,7 @@ int MoxaPortsOfCard(int cardno)
1774 * 14. MoxaPortDCDON(int port); * 1762 * 14. MoxaPortDCDON(int port); *
1775 * 15. MoxaPortFlushData(int port, int mode); * 1763 * 15. MoxaPortFlushData(int port, int mode); *
1776 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); * 1764 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1777 * 17. MoxaPortReadData(int port, unsigned char * buffer, int length); * 1765 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1778 * 18. MoxaPortTxBufSize(int port); * 1766 * 18. MoxaPortTxBufSize(int port); *
1779 * 19. MoxaPortRxBufSize(int port); * 1767 * 19. MoxaPortRxBufSize(int port); *
1780 * 20. MoxaPortTxQueue(int port); * 1768 * 20. MoxaPortTxQueue(int port); *
@@ -2003,10 +1991,9 @@ int MoxaPortsOfCard(int cardno)
2003 * 1991 *
2004 * Function 21: Read data. 1992 * Function 21: Read data.
2005 * Syntax: 1993 * Syntax:
2006 * int MoxaPortReadData(int port, unsigned char * buffer, int length); 1994 * int MoxaPortReadData(int port, struct tty_struct *tty);
2007 * int port : port number (0 - 127) 1995 * int port : port number (0 - 127)
2008 * unsigned char * buffer : pointer to read data buffer. 1996 * struct tty_struct *tty : tty for data
2009 * int length : read data buffer length
2010 * 1997 *
2011 * return: 0 - length : real read data length 1998 * return: 0 - length : real read data length
2012 * 1999 *
@@ -2504,7 +2491,7 @@ int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2504 return (total); 2491 return (total);
2505} 2492}
2506 2493
2507int MoxaPortReadData(int port, unsigned char * buffer, int space) 2494int MoxaPortReadData(int port, struct tty_struct *tty)
2508{ 2495{
2509 register ushort head, pageofs; 2496 register ushort head, pageofs;
2510 int i, count, cnt, len, total, remain; 2497 int i, count, cnt, len, total, remain;
@@ -2522,9 +2509,9 @@ int MoxaPortReadData(int port, unsigned char * buffer, int space)
2522 count = (tail >= head) ? (tail - head) 2509 count = (tail >= head) ? (tail - head)
2523 : (tail - head + rx_mask + 1); 2510 : (tail - head + rx_mask + 1);
2524 if (count == 0) 2511 if (count == 0)
2525 return (0); 2512 return 0;
2526 2513
2527 total = (space > count) ? count : space; 2514 total = count;
2528 remain = count - total; 2515 remain = count - total;
2529 moxaLog.rxcnt[port] += total; 2516 moxaLog.rxcnt[port] += total;
2530 count = total; 2517 count = total;
@@ -2539,7 +2526,7 @@ int MoxaPortReadData(int port, unsigned char * buffer, int space)
2539 len = (count > len) ? len : count; 2526 len = (count > len) ? len : count;
2540 ofs = baseAddr + DynPage_addr + bufhead + head; 2527 ofs = baseAddr + DynPage_addr + bufhead + head;
2541 for (i = 0; i < len; i++) 2528 for (i = 0; i < len; i++)
2542 *buffer++ = readb(ofs + i); 2529 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2543 head = (head + len) & rx_mask; 2530 head = (head + len) & rx_mask;
2544 count -= len; 2531 count -= len;
2545 } 2532 }
@@ -2556,7 +2543,7 @@ int MoxaPortReadData(int port, unsigned char * buffer, int space)
2556 writew(pageno, baseAddr + Control_reg); 2543 writew(pageno, baseAddr + Control_reg);
2557 ofs = baseAddr + DynPage_addr + pageofs; 2544 ofs = baseAddr + DynPage_addr + pageofs;
2558 for (i = 0; i < cnt; i++) 2545 for (i = 0; i < cnt; i++)
2559 *buffer++ = readb(ofs + i); 2546 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2560 if (count == 0) { 2547 if (count == 0) {
2561 writew((head + len) & rx_mask, ofsAddr + RXrptr); 2548 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2562 break; 2549 break;