serial: sh-sci: Fix NULL pointer dereference if HIGHMEM is enabled
[firefly-linux-kernel-4.4.55.git] / drivers / tty / serial / sh-sci.c
index 35e24b726fe605d13f91d1110a78991d183b86fe..70e16f402e3108f48d4ab7129c2832eab4ea2efe 100644 (file)
@@ -110,6 +110,7 @@ struct sci_port {
        dma_addr_t                      tx_dma_addr;
        unsigned int                    tx_dma_len;
        struct scatterlist              sg_rx[2];
+       void                            *rx_buf[2];
        size_t                          buf_len_rx;
        struct sh_dmae_slave            param_tx;
        struct sh_dmae_slave            param_rx;
@@ -1301,37 +1302,35 @@ static void sci_dma_tx_complete(void *arg)
 }
 
 /* Locking: called with port lock held */
-static int sci_dma_rx_push(struct sci_port *s, size_t count)
+static int sci_dma_rx_push(struct sci_port *s, void *buf, size_t count)
 {
        struct uart_port *port = &s->port;
        struct tty_port *tport = &port->state->port;
-       int i, active, room;
+       int copied;
 
-       room = tty_buffer_request_room(tport, count);
-
-       if (s->active_rx == s->cookie_rx[0]) {
-               active = 0;
-       } else if (s->active_rx == s->cookie_rx[1]) {
-               active = 1;
-       } else {
-               dev_err(port->dev, "%s: Rx cookie %d not found!\n", __func__,
-                       s->active_rx);
-               return 0;
+       copied = tty_insert_flip_string(tport, buf, count);
+       if (copied < count) {
+               dev_warn(port->dev, "Rx overrun: dropping %zu bytes\n",
+                        count - copied);
+               port->icount.buf_overrun++;
        }
 
-       if (room < count)
-               dev_warn(port->dev, "Rx overrun: dropping %zu bytes\n",
-                        count - room);
-       if (!room)
-               return room;
+       port->icount.rx += copied;
+
+       return copied;
+}
 
-       for (i = 0; i < room; i++)
-               tty_insert_flip_char(tport, ((u8 *)sg_virt(&s->sg_rx[active]))[i],
-                                    TTY_NORMAL);
+static int sci_dma_rx_find_active(struct sci_port *s)
+{
+       unsigned int i;
 
-       port->icount.rx += room;
+       for (i = 0; i < ARRAY_SIZE(s->cookie_rx); i++)
+               if (s->active_rx == s->cookie_rx[i])
+                       return i;
 
-       return room;
+       dev_err(s->port.dev, "%s: Rx cookie %d not found!\n", __func__,
+               s->active_rx);
+       return -1;
 }
 
 static void sci_dma_rx_complete(void *arg)
@@ -1339,14 +1338,16 @@ static void sci_dma_rx_complete(void *arg)
        struct sci_port *s = arg;
        struct uart_port *port = &s->port;
        unsigned long flags;
-       int count;
+       int active, count = 0;
 
        dev_dbg(port->dev, "%s(%d) active cookie %d\n", __func__, port->line,
                s->active_rx);
 
        spin_lock_irqsave(&port->lock, flags);
 
-       count = sci_dma_rx_push(s, s->buf_len_rx);
+       active = sci_dma_rx_find_active(s);
+       if (active >= 0)
+               count = sci_dma_rx_push(s, s->rx_buf[active], s->buf_len_rx);
 
        mod_timer(&s->rx_timer, jiffies + s->rx_timeout);
 
@@ -1369,8 +1370,8 @@ static void sci_rx_dma_release(struct sci_port *s, bool enable_pio)
        s->cookie_rx[0] = s->cookie_rx[1] = -EINVAL;
        spin_unlock_irqrestore(&port->lock, flags);
        dmaengine_terminate_all(chan);
-       dma_free_coherent(chan->device->dev, s->buf_len_rx * 2,
-                         sg_virt(&s->sg_rx[0]), sg_dma_address(&s->sg_rx[0]));
+       dma_free_coherent(chan->device->dev, s->buf_len_rx * 2, s->rx_buf[0],
+                         sg_dma_address(&s->sg_rx[0]));
        dma_release_channel(chan);
        if (enable_pio)
                sci_start_rx(port);
@@ -1445,13 +1446,8 @@ static void work_fn_rx(struct work_struct *work)
        int new;
 
        spin_lock_irqsave(&port->lock, flags);
-       if (s->active_rx == s->cookie_rx[0]) {
-               new = 0;
-       } else if (s->active_rx == s->cookie_rx[1]) {
-               new = 1;
-       } else {
-               dev_err(port->dev, "%s: Rx cookie %d not found!\n", __func__,
-                       s->active_rx);
+       new = sci_dma_rx_find_active(s);
+       if (new < 0) {
                spin_unlock_irqrestore(&port->lock, flags);
                return;
        }
@@ -1468,7 +1464,7 @@ static void work_fn_rx(struct work_struct *work)
                dev_dbg(port->dev, "Read %u bytes with cookie %d\n", read,
                        s->active_rx);
 
-               count = sci_dma_rx_push(s, read);
+               count = sci_dma_rx_push(s, s->rx_buf[new], read);
 
                if (count)
                        tty_flip_buffer_push(&port->state->port);
@@ -1738,18 +1734,16 @@ static void sci_request_dma(struct uart_port *port)
        chan = dma_request_channel(mask, filter, param);
        dev_dbg(port->dev, "%s: RX: got channel %p\n", __func__, chan);
        if (chan) {
-               dma_addr_t dma[2];
-               void *buf[2];
-               int i;
+               unsigned int i;
+               dma_addr_t dma;
+               void *buf;
 
                s->chan_rx = chan;
 
                s->buf_len_rx = 2 * max_t(size_t, 16, port->fifosize);
-               buf[0] = dma_alloc_coherent(chan->device->dev,
-                                           s->buf_len_rx * 2, &dma[0],
-                                           GFP_KERNEL);
-
-               if (!buf[0]) {
+               buf = dma_alloc_coherent(chan->device->dev, s->buf_len_rx * 2,
+                                        &dma, GFP_KERNEL);
+               if (!buf) {
                        dev_warn(port->dev,
                                 "Failed to allocate Rx dma buffer, using PIO\n");
                        dma_release_channel(chan);
@@ -1758,16 +1752,16 @@ static void sci_request_dma(struct uart_port *port)
                        return;
                }
 
-               buf[1] = buf[0] + s->buf_len_rx;
-               dma[1] = dma[0] + s->buf_len_rx;
-
                for (i = 0; i < 2; i++) {
                        struct scatterlist *sg = &s->sg_rx[i];
 
                        sg_init_table(sg, 1);
-                       sg_set_page(sg, virt_to_page(buf[i]), s->buf_len_rx,
-                                   (uintptr_t)buf[i] & ~PAGE_MASK);
-                       sg_dma_address(sg) = dma[i];
+                       s->rx_buf[i] = buf;
+                       sg_dma_address(sg) = dma;
+                       sg->length = s->buf_len_rx;
+
+                       buf += s->buf_len_rx;
+                       dma += s->buf_len_rx;
                }
 
                INIT_WORK(&s->work_rx, work_fn_rx);