1181905Sed/*- 2181905Sed * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 3181905Sed * All rights reserved. 4181905Sed * 5181905Sed * Portions of this software were developed under sponsorship from Snow 6181905Sed * B.V., the Netherlands. 7181905Sed * 8181905Sed * Redistribution and use in source and binary forms, with or without 9181905Sed * modification, are permitted provided that the following conditions 10181905Sed * are met: 11181905Sed * 1. Redistributions of source code must retain the above copyright 12181905Sed * notice, this list of conditions and the following disclaimer. 13181905Sed * 2. Redistributions in binary form must reproduce the above copyright 14181905Sed * notice, this list of conditions and the following disclaimer in the 15181905Sed * documentation and/or other materials provided with the distribution. 16181905Sed * 17181905Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18181905Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19181905Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20181905Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21181905Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22181905Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23181905Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24181905Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25181905Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26181905Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27181905Sed * SUCH DAMAGE. 28181905Sed */ 29181905Sed 30181905Sed#include <sys/cdefs.h> 31181905Sed__FBSDID("$FreeBSD$"); 32181905Sed 33181905Sed#include <sys/param.h> 34181905Sed#include <sys/kernel.h> 35181905Sed#include <sys/lock.h> 36181905Sed#include <sys/queue.h> 37181905Sed#include <sys/systm.h> 38181905Sed#include <sys/tty.h> 39181905Sed#include <sys/uio.h> 40181905Sed 41181905Sed#include <vm/uma.h> 42181905Sed 43181905Sed/* 44181905Sed * TTY output queue buffering. 45181905Sed * 46181905Sed * The previous design of the TTY layer offered the so-called clists. 47181905Sed * These clists were used for both the input queues and the output 48181905Sed * queue. We don't use certain features on the output side, like quoting 49181905Sed * bits for parity marking and such. This mechanism is similar to the 50181905Sed * old clists, but only contains the features we need to buffer the 51181905Sed * output. 52181905Sed */ 53181905Sed 54181905Sedstruct ttyoutq_block { 55188096Sed struct ttyoutq_block *tob_next; 56188096Sed char tob_data[TTYOUTQ_DATASIZE]; 57181905Sed}; 58181905Sed 59181905Sedstatic uma_zone_t ttyoutq_zone; 60181905Sed 61188096Sed#define TTYOUTQ_INSERT_TAIL(to, tob) do { \ 62188096Sed if (to->to_end == 0) { \ 63188096Sed tob->tob_next = to->to_firstblock; \ 64188096Sed to->to_firstblock = tob; \ 65188096Sed } else { \ 66188096Sed tob->tob_next = to->to_lastblock->tob_next; \ 67188096Sed to->to_lastblock->tob_next = tob; \ 68188096Sed } \ 69188096Sed to->to_nblocks++; \ 70188096Sed} while (0) 71188096Sed 72188096Sed#define TTYOUTQ_REMOVE_HEAD(to) do { \ 73188096Sed to->to_firstblock = to->to_firstblock->tob_next; \ 74188096Sed to->to_nblocks--; \ 75188096Sed} while (0) 76188096Sed 77188096Sed#define TTYOUTQ_RECYCLE(to, tob) do { \ 78188096Sed if (to->to_quota <= to->to_nblocks) \ 79188096Sed uma_zfree(ttyoutq_zone, tob); \ 80188096Sed else \ 81188096Sed TTYOUTQ_INSERT_TAIL(to, tob); \ 82188096Sed} while(0) 83188096Sed 84181905Sedvoid 85181905Sedttyoutq_flush(struct ttyoutq *to) 86181905Sed{ 87181905Sed 88181905Sed to->to_begin = 0; 89181905Sed to->to_end = 0; 90181905Sed} 91181905Sed 92181905Sedvoid 93181905Sedttyoutq_setsize(struct ttyoutq *to, struct tty *tp, size_t size) 94181905Sed{ 95181905Sed struct ttyoutq_block *tob; 96181905Sed 97182471Sed to->to_quota = howmany(size, TTYOUTQ_DATASIZE); 98181905Sed 99182471Sed while (to->to_quota > to->to_nblocks) { 100181905Sed /* 101181905Sed * List is getting bigger. 102181905Sed * Add new blocks to the tail of the list. 103181905Sed * 104181905Sed * We must unlock the TTY temporarily, because we need 105181905Sed * to allocate memory. This won't be a problem, because 106181905Sed * in the worst case, another thread ends up here, which 107181905Sed * may cause us to allocate too many blocks, but this 108181905Sed * will be caught by the loop below. 109181905Sed */ 110181905Sed tty_unlock(tp); 111181905Sed tob = uma_zalloc(ttyoutq_zone, M_WAITOK); 112181905Sed tty_lock(tp); 113181905Sed 114188096Sed TTYOUTQ_INSERT_TAIL(to, tob); 115181905Sed } 116182471Sed} 117181905Sed 118182471Sedvoid 119182471Sedttyoutq_free(struct ttyoutq *to) 120182471Sed{ 121182471Sed struct ttyoutq_block *tob; 122223575Sed 123182471Sed ttyoutq_flush(to); 124182471Sed to->to_quota = 0; 125181905Sed 126188096Sed while ((tob = to->to_firstblock) != NULL) { 127188096Sed TTYOUTQ_REMOVE_HEAD(to); 128181905Sed uma_zfree(ttyoutq_zone, tob); 129181905Sed } 130182471Sed 131182471Sed MPASS(to->to_nblocks == 0); 132181905Sed} 133181905Sed 134181905Sedsize_t 135181905Sedttyoutq_read(struct ttyoutq *to, void *buf, size_t len) 136181905Sed{ 137181905Sed char *cbuf = buf; 138181905Sed 139181905Sed while (len > 0) { 140181905Sed struct ttyoutq_block *tob; 141181905Sed size_t cbegin, cend, clen; 142181905Sed 143181905Sed /* See if there still is data. */ 144181905Sed if (to->to_begin == to->to_end) 145181905Sed break; 146188096Sed tob = to->to_firstblock; 147181905Sed if (tob == NULL) 148181905Sed break; 149181905Sed 150181905Sed /* 151181905Sed * The end address should be the lowest of these three: 152181905Sed * - The write pointer 153181905Sed * - The blocksize - we can't read beyond the block 154181905Sed * - The end address if we could perform the full read 155181905Sed */ 156181905Sed cbegin = to->to_begin; 157181905Sed cend = MIN(MIN(to->to_end, to->to_begin + len), 158181905Sed TTYOUTQ_DATASIZE); 159181905Sed clen = cend - cbegin; 160181905Sed 161188096Sed /* Copy the data out of the buffers. */ 162188096Sed memcpy(cbuf, tob->tob_data + cbegin, clen); 163188096Sed cbuf += clen; 164188096Sed len -= clen; 165188096Sed 166188096Sed if (cend == to->to_end) { 167188096Sed /* Read the complete queue. */ 168188096Sed to->to_begin = 0; 169188096Sed to->to_end = 0; 170188096Sed } else if (cend == TTYOUTQ_DATASIZE) { 171181905Sed /* Read the block until the end. */ 172188096Sed TTYOUTQ_REMOVE_HEAD(to); 173181905Sed to->to_begin = 0; 174188096Sed to->to_end -= TTYOUTQ_DATASIZE; 175188096Sed TTYOUTQ_RECYCLE(to, tob); 176181905Sed } else { 177181905Sed /* Read the block partially. */ 178181905Sed to->to_begin += clen; 179181905Sed } 180181905Sed } 181181905Sed 182181905Sed return (cbuf - (char *)buf); 183181905Sed} 184181905Sed 185181905Sed/* 186181905Sed * An optimized version of ttyoutq_read() which can be used in pseudo 187181905Sed * TTY drivers to directly copy data from the outq to userspace, instead 188181905Sed * of buffering it. 189181905Sed * 190181905Sed * We can only copy data directly if we need to read the entire block 191181905Sed * back to the user, because we temporarily remove the block from the 192181905Sed * queue. Otherwise we need to copy it to a temporary buffer first, to 193181905Sed * make sure data remains in the correct order. 194181905Sed */ 195181905Sedint 196181905Sedttyoutq_read_uio(struct ttyoutq *to, struct tty *tp, struct uio *uio) 197181905Sed{ 198181905Sed 199181905Sed while (uio->uio_resid > 0) { 200181905Sed int error; 201181905Sed struct ttyoutq_block *tob; 202181905Sed size_t cbegin, cend, clen; 203181905Sed 204181905Sed /* See if there still is data. */ 205181905Sed if (to->to_begin == to->to_end) 206181905Sed return (0); 207188096Sed tob = to->to_firstblock; 208181905Sed if (tob == NULL) 209181905Sed return (0); 210181905Sed 211181905Sed /* 212181905Sed * The end address should be the lowest of these three: 213181905Sed * - The write pointer 214181905Sed * - The blocksize - we can't read beyond the block 215181905Sed * - The end address if we could perform the full read 216181905Sed */ 217181905Sed cbegin = to->to_begin; 218181905Sed cend = MIN(MIN(to->to_end, to->to_begin + uio->uio_resid), 219181905Sed TTYOUTQ_DATASIZE); 220181905Sed clen = cend - cbegin; 221181905Sed 222181905Sed /* 223181905Sed * We can prevent buffering in some cases: 224181905Sed * - We need to read the block until the end. 225181905Sed * - We don't need to read the block until the end, but 226181905Sed * there is no data beyond it, which allows us to move 227181905Sed * the write pointer to a new block. 228181905Sed */ 229181905Sed if (cend == TTYOUTQ_DATASIZE || cend == to->to_end) { 230181905Sed /* 231181905Sed * Fast path: zero copy. Remove the first block, 232181905Sed * so we can unlock the TTY temporarily. 233181905Sed */ 234188096Sed TTYOUTQ_REMOVE_HEAD(to); 235181905Sed to->to_begin = 0; 236188096Sed if (to->to_end <= TTYOUTQ_DATASIZE) 237181905Sed to->to_end = 0; 238188096Sed else 239181905Sed to->to_end -= TTYOUTQ_DATASIZE; 240181905Sed 241181905Sed /* Temporary unlock and copy the data to userspace. */ 242181905Sed tty_unlock(tp); 243181905Sed error = uiomove(tob->tob_data + cbegin, clen, uio); 244181905Sed tty_lock(tp); 245181905Sed 246181905Sed /* Block can now be readded to the list. */ 247188096Sed TTYOUTQ_RECYCLE(to, tob); 248181905Sed } else { 249181905Sed char ob[TTYOUTQ_DATASIZE - 1]; 250181905Sed 251181905Sed /* 252181905Sed * Slow path: store data in a temporary buffer. 253181905Sed */ 254181905Sed memcpy(ob, tob->tob_data + cbegin, clen); 255181905Sed to->to_begin += clen; 256181905Sed MPASS(to->to_begin < TTYOUTQ_DATASIZE); 257181905Sed 258181905Sed /* Temporary unlock and copy the data to userspace. */ 259181905Sed tty_unlock(tp); 260181905Sed error = uiomove(ob, clen, uio); 261181905Sed tty_lock(tp); 262182471Sed } 263181905Sed 264182471Sed if (error != 0) 265182471Sed return (error); 266181905Sed } 267181905Sed 268181905Sed return (0); 269181905Sed} 270181905Sed 271181905Sedsize_t 272181905Sedttyoutq_write(struct ttyoutq *to, const void *buf, size_t nbytes) 273181905Sed{ 274181905Sed const char *cbuf = buf; 275181905Sed struct ttyoutq_block *tob; 276181905Sed unsigned int boff; 277181905Sed size_t l; 278181905Sed 279181905Sed while (nbytes > 0) { 280181905Sed boff = to->to_end % TTYOUTQ_DATASIZE; 281181905Sed 282181905Sed if (to->to_end == 0) { 283181905Sed /* First time we're being used or drained. */ 284181905Sed MPASS(to->to_begin == 0); 285188096Sed tob = to->to_firstblock; 286181905Sed if (tob == NULL) { 287181905Sed /* Queue has no blocks. */ 288181905Sed break; 289181905Sed } 290188096Sed to->to_lastblock = tob; 291181905Sed } else if (boff == 0) { 292181905Sed /* We reached the end of this block on last write. */ 293188096Sed tob = to->to_lastblock->tob_next; 294181905Sed if (tob == NULL) { 295181905Sed /* We've reached the watermark. */ 296181905Sed break; 297181905Sed } 298181905Sed to->to_lastblock = tob; 299188096Sed } else { 300188096Sed tob = to->to_lastblock; 301181905Sed } 302181905Sed 303181905Sed /* Don't copy more than was requested. */ 304181905Sed l = MIN(nbytes, TTYOUTQ_DATASIZE - boff); 305181905Sed MPASS(l > 0); 306181905Sed memcpy(tob->tob_data + boff, cbuf, l); 307181905Sed 308181905Sed cbuf += l; 309181905Sed nbytes -= l; 310181905Sed to->to_end += l; 311181905Sed } 312181905Sed 313181905Sed return (cbuf - (const char *)buf); 314181905Sed} 315181905Sed 316181905Sedint 317181905Sedttyoutq_write_nofrag(struct ttyoutq *to, const void *buf, size_t nbytes) 318181905Sed{ 319181905Sed size_t ret; 320181905Sed 321181905Sed if (ttyoutq_bytesleft(to) < nbytes) 322181905Sed return (-1); 323181905Sed 324181905Sed /* We should always be able to write it back. */ 325181905Sed ret = ttyoutq_write(to, buf, nbytes); 326181905Sed MPASS(ret == nbytes); 327181905Sed 328181905Sed return (0); 329181905Sed} 330181905Sed 331181905Sedstatic void 332181905Sedttyoutq_startup(void *dummy) 333181905Sed{ 334181905Sed 335181905Sed ttyoutq_zone = uma_zcreate("ttyoutq", sizeof(struct ttyoutq_block), 336181905Sed NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 337181905Sed} 338181905Sed 339181905SedSYSINIT(ttyoutq, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyoutq_startup, NULL); 340