blob: 37b082be9d24c5c8109cfc1e4fc8450b7a023028 [file] [log] [blame]
Peter Hurley7355ba32012-11-02 08:16:33 -04001/*
2 * FireWire Serial driver
3 *
4 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/sched.h>
22#include <linux/slab.h>
23#include <linux/device.h>
24#include <linux/mod_devicetable.h>
25#include <linux/rculist.h>
26#include <linux/workqueue.h>
27#include <linux/ratelimit.h>
28#include <linux/bug.h>
29#include <linux/uaccess.h>
30
31#include "fwserial.h"
32
33#define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo))
34
35#define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
36#define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
37
38/* configurable options */
39static int num_ttys = 4; /* # of std ttys to create per fw_card */
40 /* - doubles as loopback port index */
41static bool auto_connect = true; /* try to VIRT_CABLE to every peer */
42static bool create_loop_dev = true; /* create a loopback device for each card */
Peter Hurley7355ba32012-11-02 08:16:33 -040043
44module_param_named(ttys, num_ttys, int, S_IRUGO | S_IWUSR);
45module_param_named(auto, auto_connect, bool, S_IRUGO | S_IWUSR);
46module_param_named(loop, create_loop_dev, bool, S_IRUGO | S_IWUSR);
Peter Hurley7355ba32012-11-02 08:16:33 -040047
48/*
49 * Threshold below which the tty is woken for writing
50 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
51 * even if the writer is woken, n_tty_poll() won't set POLLOUT until
52 * our fifo is below this level
53 */
54#define WAKEUP_CHARS 256
55
56/**
57 * fwserial_list: list of every fw_serial created for each fw_card
58 * See discussion in fwserial_probe.
59 */
60static LIST_HEAD(fwserial_list);
61static DEFINE_MUTEX(fwserial_list_mutex);
62
63/**
64 * port_table: array of tty ports allocated to each fw_card
65 *
66 * tty ports are allocated during probe when an fw_serial is first
67 * created for a given fw_card. Ports are allocated in a contiguous block,
68 * each block consisting of 'num_ports' ports.
69 */
70static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
71static DEFINE_MUTEX(port_table_lock);
72static bool port_table_corrupt;
73#define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
74
Peter Hurleyfa1da242013-01-28 22:34:38 -050075#define loop_idx(port) (((port)->index) / num_ports)
76#define table_idx(loop) ((loop) * num_ports + num_ttys)
77
Peter Hurley7355ba32012-11-02 08:16:33 -040078/* total # of tty ports created per fw_card */
79static int num_ports;
80
81/* slab used as pool for struct fwtty_transactions */
82static struct kmem_cache *fwtty_txn_cache;
83
Peter Hurleya3d9ad42013-01-28 22:34:37 -050084struct tty_driver *fwtty_driver;
Peter Hurleyfa1da242013-01-28 22:34:38 -050085static struct tty_driver *fwloop_driver;
Peter Hurleya3d9ad42013-01-28 22:34:37 -050086
Peter Hurley4df5bb02013-01-28 22:34:40 -050087static struct dentry *fwserial_debugfs;
88
Peter Hurley7355ba32012-11-02 08:16:33 -040089struct fwtty_transaction;
90typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
91 void *data, size_t length,
92 struct fwtty_transaction *txn);
93
94struct fwtty_transaction {
95 struct fw_transaction fw_txn;
96 fwtty_transaction_cb callback;
97 struct fwtty_port *port;
98 union {
99 struct dma_pending dma_pended;
100 };
101};
102
103#define to_device(a, b) (a->b)
104#define fwtty_err(p, s, v...) dev_err(to_device(p, device), s, ##v)
105#define fwtty_info(p, s, v...) dev_info(to_device(p, device), s, ##v)
106#define fwtty_notice(p, s, v...) dev_notice(to_device(p, device), s, ##v)
107#define fwtty_dbg(p, s, v...) \
108 dev_dbg(to_device(p, device), "%s: " s, __func__, ##v)
109#define fwtty_err_ratelimited(p, s, v...) \
110 dev_err_ratelimited(to_device(p, device), s, ##v)
111
112#ifdef DEBUG
113static inline void debug_short_write(struct fwtty_port *port, int c, int n)
114{
115 int avail;
116
117 if (n < c) {
118 spin_lock_bh(&port->lock);
119 avail = dma_fifo_avail(&port->tx_fifo);
120 spin_unlock_bh(&port->lock);
121 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d",
122 avail, c, n);
123 }
124}
125#else
126#define debug_short_write(port, c, n)
127#endif
128
129static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
130 int generation, int id);
131
132#ifdef FWTTY_PROFILING
133
134static void profile_fifo_avail(struct fwtty_port *port, unsigned *stat)
135{
136 spin_lock_bh(&port->lock);
137 profile_size_distrib(stat, dma_fifo_avail(&port->tx_fifo));
138 spin_unlock_bh(&port->lock);
139}
140
141static void dump_profile(struct seq_file *m, struct stats *stats)
142{
143 /* for each stat, print sum of 0 to 2^k, then individually */
144 int k = 4;
145 unsigned sum;
146 int j;
147 char t[10];
148
149 snprintf(t, 10, "< %d", 1 << k);
150 seq_printf(m, "\n%14s %6s", " ", t);
151 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j)
152 seq_printf(m, "%6d", 1 << j);
153
154 ++k;
155 for (j = 0, sum = 0; j <= k; ++j)
156 sum += stats->reads[j];
157 seq_printf(m, "\n%14s: %6d", "reads", sum);
158 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
159 seq_printf(m, "%6d", stats->reads[j]);
160
161 for (j = 0, sum = 0; j <= k; ++j)
162 sum += stats->writes[j];
163 seq_printf(m, "\n%14s: %6d", "writes", sum);
164 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
165 seq_printf(m, "%6d", stats->writes[j]);
166
167 for (j = 0, sum = 0; j <= k; ++j)
168 sum += stats->txns[j];
169 seq_printf(m, "\n%14s: %6d", "txns", sum);
170 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
171 seq_printf(m, "%6d", stats->txns[j]);
172
173 for (j = 0, sum = 0; j <= k; ++j)
174 sum += stats->unthrottle[j];
175 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum);
176 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
177 seq_printf(m, "%6d", stats->unthrottle[j]);
178}
179
180#else
181#define profile_fifo_avail(port, stat)
182#define dump_profile(m, stats)
183#endif
184
Peter Hurley9883a732013-01-28 20:57:48 -0500185/*
186 * Returns the max receive packet size for the given node
187 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
188 * are required by specification to support max_rec of 8 (512 bytes) or more.
189 */
Peter Hurley7355ba32012-11-02 08:16:33 -0400190static inline int device_max_receive(struct fw_device *fw_device)
191{
Peter Hurley9883a732013-01-28 20:57:48 -0500192 /* see IEEE 1394-2008 table 8-8 */
193 return min(2 << fw_device->max_rec, 4096);
Peter Hurley7355ba32012-11-02 08:16:33 -0400194}
195
196static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
197{
198 switch (rcode) {
199 case RCODE_SEND_ERROR:
200 fwtty_err_ratelimited(port, "card busy");
201 break;
202 case RCODE_ADDRESS_ERROR:
203 fwtty_err_ratelimited(port, "bad unit addr or write length");
204 break;
205 case RCODE_DATA_ERROR:
206 fwtty_err_ratelimited(port, "failed rx");
207 break;
208 case RCODE_NO_ACK:
209 fwtty_err_ratelimited(port, "missing ack");
210 break;
211 case RCODE_BUSY:
212 fwtty_err_ratelimited(port, "remote busy");
213 break;
214 default:
215 fwtty_err_ratelimited(port, "failed tx: %d", rcode);
216 }
217}
218
219static void fwtty_txn_constructor(void *this)
220{
221 struct fwtty_transaction *txn = this;
222
223 init_timer(&txn->fw_txn.split_timeout_timer);
224}
225
226static void fwtty_common_callback(struct fw_card *card, int rcode,
227 void *payload, size_t len, void *cb_data)
228{
229 struct fwtty_transaction *txn = cb_data;
230 struct fwtty_port *port = txn->port;
231
232 if (port && rcode != RCODE_COMPLETE)
233 fwtty_log_tx_error(port, rcode);
234 if (txn->callback)
235 txn->callback(card, rcode, payload, len, txn);
236 kmem_cache_free(fwtty_txn_cache, txn);
237}
238
239static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode,
240 unsigned long long addr, void *payload,
241 size_t len, fwtty_transaction_cb callback,
242 struct fwtty_port *port)
243{
244 struct fwtty_transaction *txn;
245 int generation;
246
247 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
248 if (!txn)
249 return -ENOMEM;
250
251 txn->callback = callback;
252 txn->port = port;
253
254 generation = peer->generation;
255 smp_rmb();
256 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
257 peer->node_id, generation, peer->speed, addr, payload,
258 len, fwtty_common_callback, txn);
259 return 0;
260}
261
262static void fwtty_send_txn_async(struct fwtty_peer *peer,
263 struct fwtty_transaction *txn, int tcode,
264 unsigned long long addr, void *payload,
265 size_t len, fwtty_transaction_cb callback,
266 struct fwtty_port *port)
267{
268 int generation;
269
270 txn->callback = callback;
271 txn->port = port;
272
273 generation = peer->generation;
274 smp_rmb();
275 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
276 peer->node_id, generation, peer->speed, addr, payload,
277 len, fwtty_common_callback, txn);
278}
279
280
281static void __fwtty_restart_tx(struct fwtty_port *port)
282{
283 int len, avail;
284
285 len = dma_fifo_out_level(&port->tx_fifo);
286 if (len)
287 schedule_delayed_work(&port->drain, 0);
288 avail = dma_fifo_avail(&port->tx_fifo);
289
290 fwtty_dbg(port, "fifo len: %d avail: %d", len, avail);
291}
292
293static void fwtty_restart_tx(struct fwtty_port *port)
294{
295 spin_lock_bh(&port->lock);
296 __fwtty_restart_tx(port);
297 spin_unlock_bh(&port->lock);
298}
299
300/**
301 * fwtty_update_port_status - decodes & dispatches line status changes
302 *
303 * Note: in loopback, the port->lock is being held. Only use functions that
304 * don't attempt to reclaim the port->lock.
305 */
306static void fwtty_update_port_status(struct fwtty_port *port, unsigned status)
307{
308 unsigned delta;
309 struct tty_struct *tty;
310
311 /* simulated LSR/MSR status from remote */
312 status &= ~MCTRL_MASK;
313 delta = (port->mstatus ^ status) & ~MCTRL_MASK;
314 delta &= ~(status & TIOCM_RNG);
315 port->mstatus = status;
316
317 if (delta & TIOCM_RNG)
318 ++port->icount.rng;
319 if (delta & TIOCM_DSR)
320 ++port->icount.dsr;
321 if (delta & TIOCM_CAR)
322 ++port->icount.dcd;
323 if (delta & TIOCM_CTS)
324 ++port->icount.cts;
325
326 fwtty_dbg(port, "status: %x delta: %x", status, delta);
327
328 if (delta & TIOCM_CAR) {
329 tty = tty_port_tty_get(&port->port);
330 if (tty && !C_CLOCAL(tty)) {
331 if (status & TIOCM_CAR)
332 wake_up_interruptible(&port->port.open_wait);
333 else
334 schedule_work(&port->hangup);
335 }
336 tty_kref_put(tty);
337 }
338
339 if (delta & TIOCM_CTS) {
340 tty = tty_port_tty_get(&port->port);
341 if (tty && C_CRTSCTS(tty)) {
342 if (tty->hw_stopped) {
343 if (status & TIOCM_CTS) {
344 tty->hw_stopped = 0;
345 if (port->loopback)
346 __fwtty_restart_tx(port);
347 else
348 fwtty_restart_tx(port);
349 }
350 } else {
351 if (~status & TIOCM_CTS)
352 tty->hw_stopped = 1;
353 }
354 }
355 tty_kref_put(tty);
356
357 } else if (delta & OOB_TX_THROTTLE) {
358 tty = tty_port_tty_get(&port->port);
359 if (tty) {
360 if (tty->hw_stopped) {
361 if (~status & OOB_TX_THROTTLE) {
362 tty->hw_stopped = 0;
363 if (port->loopback)
364 __fwtty_restart_tx(port);
365 else
366 fwtty_restart_tx(port);
367 }
368 } else {
369 if (status & OOB_TX_THROTTLE)
370 tty->hw_stopped = 1;
371 }
372 }
373 tty_kref_put(tty);
374 }
375
376 if (delta & (UART_LSR_BI << 24)) {
377 if (status & (UART_LSR_BI << 24)) {
378 port->break_last = jiffies;
379 schedule_delayed_work(&port->emit_breaks, 0);
380 } else {
381 /* run emit_breaks one last time (if pending) */
382 mod_delayed_work(system_wq, &port->emit_breaks, 0);
383 }
384 }
385
386 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
387 wake_up_interruptible(&port->port.delta_msr_wait);
388}
389
390/**
391 * __fwtty_port_line_status - generate 'line status' for indicated port
392 *
393 * This function returns a remote 'MSR' state based on the local 'MCR' state,
394 * as if a null modem cable was attached. The actual status is a mangling
395 * of TIOCM_* bits suitable for sending to a peer's status_addr.
396 *
397 * Note: caller must be holding port lock
398 */
399static unsigned __fwtty_port_line_status(struct fwtty_port *port)
400{
401 unsigned status = 0;
402
403 /* TODO: add module param to tie RNG to DTR as well */
404
405 if (port->mctrl & TIOCM_DTR)
406 status |= TIOCM_DSR | TIOCM_CAR;
407 if (port->mctrl & TIOCM_RTS)
408 status |= TIOCM_CTS;
409 if (port->mctrl & OOB_RX_THROTTLE)
410 status |= OOB_TX_THROTTLE;
411 /* emulate BRK as add'l line status */
412 if (port->break_ctl)
413 status |= UART_LSR_BI << 24;
414
415 return status;
416}
417
418/**
419 * __fwtty_write_port_status - send the port line status to peer
420 *
421 * Note: caller must be holding the port lock.
422 */
423static int __fwtty_write_port_status(struct fwtty_port *port)
424{
425 struct fwtty_peer *peer;
426 int err = -ENOENT;
427 unsigned status = __fwtty_port_line_status(port);
428
429 rcu_read_lock();
430 peer = rcu_dereference(port->peer);
431 if (peer) {
432 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
433 peer->status_addr, &status,
434 sizeof(status), NULL, port);
435 }
436 rcu_read_unlock();
437
438 return err;
439}
440
441/**
442 * fwtty_write_port_status - same as above but locked by port lock
443 */
444static int fwtty_write_port_status(struct fwtty_port *port)
445{
446 int err;
447
448 spin_lock_bh(&port->lock);
449 err = __fwtty_write_port_status(port);
450 spin_unlock_bh(&port->lock);
451 return err;
452}
453
454static void __fwtty_throttle(struct fwtty_port *port, struct tty_struct *tty)
455{
456 unsigned old;
457
458 old = port->mctrl;
459 port->mctrl |= OOB_RX_THROTTLE;
460 if (C_CRTSCTS(tty))
461 port->mctrl &= ~TIOCM_RTS;
462 if (~old & OOB_RX_THROTTLE)
463 __fwtty_write_port_status(port);
464}
465
466/**
467 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
468 *
469 * When the remote has finished tx, and all in-flight rx has been received and
470 * and pushed to the flip buffer, the remote may close its device. This will
471 * drop DTR on the remote which will drop carrier here. Typically, the tty is
472 * hung up when carrier is dropped or lost.
473 *
474 * However, there is a race between the hang up and the line discipline
475 * delivering its data to the reader. A hangup will cause the ldisc to flush
476 * (ie., clear) the read buffer and flip buffer. Because of firewire's
477 * relatively high throughput, the ldisc frequently lags well behind the driver,
478 * resulting in lost data (which has already been received and written to
479 * the flip buffer) when the remote closes its end.
480 *
481 * Unfortunately, since the flip buffer offers no direct method for determining
482 * if it holds data, ensuring the ldisc has delivered all data is problematic.
483 */
484
485/* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
486static void fwtty_do_hangup(struct work_struct *work)
487{
488 struct fwtty_port *port = to_port(work, hangup);
489 struct tty_struct *tty;
490
491 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
492
493 tty = tty_port_tty_get(&port->port);
494 if (tty)
495 tty_vhangup(tty);
496 tty_kref_put(tty);
497}
498
499
500static void fwtty_emit_breaks(struct work_struct *work)
501{
502 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks);
503 struct tty_struct *tty;
504 static const char buf[16];
505 unsigned long now = jiffies;
506 unsigned long elapsed = now - port->break_last;
507 int n, t, c, brk = 0;
508
509 tty = tty_port_tty_get(&port->port);
510 if (!tty)
511 return;
512
513 /* generate breaks at the line rate (but at least 1) */
514 n = (elapsed * port->cps) / HZ + 1;
515 port->break_last = now;
516
517 fwtty_dbg(port, "sending %d brks", n);
518
519 while (n) {
520 t = min(n, 16);
521 c = tty_insert_flip_string_fixed_flag(tty, buf, TTY_BREAK, t);
522 n -= c;
523 brk += c;
524 if (c < t)
525 break;
526 }
527 tty_flip_buffer_push(tty);
528
529 tty_kref_put(tty);
530
531 if (port->mstatus & (UART_LSR_BI << 24))
532 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
533 port->icount.brk += brk;
534}
535
536static void fwtty_pushrx(struct work_struct *work)
537{
538 struct fwtty_port *port = to_port(work, push);
539 struct tty_struct *tty;
540 struct buffered_rx *buf, *next;
541 int n, c = 0;
542
543 tty = tty_port_tty_get(&port->port);
544 if (!tty)
545 return;
546
547 spin_lock_bh(&port->lock);
548 list_for_each_entry_safe(buf, next, &port->buf_list, list) {
549 n = tty_insert_flip_string_fixed_flag(tty, buf->data,
550 TTY_NORMAL, buf->n);
551 c += n;
552 port->buffered -= n;
553 if (n < buf->n) {
554 if (n > 0) {
555 memmove(buf->data, buf->data + n, buf->n - n);
556 buf->n -= n;
557 }
558 __fwtty_throttle(port, tty);
559 break;
560 } else {
561 list_del(&buf->list);
562 kfree(buf);
563 }
564 }
565 if (c > 0)
566 tty_flip_buffer_push(tty);
567
568 if (list_empty(&port->buf_list))
569 clear_bit(BUFFERING_RX, &port->flags);
570 spin_unlock_bh(&port->lock);
571
572 tty_kref_put(tty);
573}
574
575static int fwtty_buffer_rx(struct fwtty_port *port, unsigned char *d, size_t n)
576{
577 struct buffered_rx *buf;
578 size_t size = (n + sizeof(struct buffered_rx) + 0xFF) & ~0xFF;
579
580 if (port->buffered + n > HIGH_WATERMARK)
581 return 0;
582 buf = kmalloc(size, GFP_ATOMIC);
583 if (!buf)
584 return 0;
585 INIT_LIST_HEAD(&buf->list);
586 buf->n = n;
587 memcpy(buf->data, d, n);
588
589 spin_lock_bh(&port->lock);
590 list_add_tail(&buf->list, &port->buf_list);
591 port->buffered += n;
592 if (port->buffered > port->stats.watermark)
593 port->stats.watermark = port->buffered;
594 set_bit(BUFFERING_RX, &port->flags);
595 spin_unlock_bh(&port->lock);
596
597 return n;
598}
599
600static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
601{
602 struct tty_struct *tty;
603 int c, n = len;
604 unsigned lsr;
605 int err = 0;
606
607 tty = tty_port_tty_get(&port->port);
608 if (!tty)
609 return -ENOENT;
610
611 fwtty_dbg(port, "%d", n);
612 profile_size_distrib(port->stats.reads, n);
613
614 if (port->write_only) {
615 n = 0;
616 goto out;
617 }
618
619 /* disregard break status; breaks are generated by emit_breaks work */
620 lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
621
622 if (port->overrun)
623 lsr |= UART_LSR_OE;
624
625 if (lsr & UART_LSR_OE)
626 ++port->icount.overrun;
627
628 lsr &= port->status_mask;
629 if (lsr & ~port->ignore_mask & UART_LSR_OE) {
630 if (!tty_insert_flip_char(tty, 0, TTY_OVERRUN)) {
631 err = -EIO;
632 goto out;
633 }
634 }
635 port->overrun = false;
636
637 if (lsr & port->ignore_mask & ~UART_LSR_OE) {
638 /* TODO: don't drop SAK and Magic SysRq here */
639 n = 0;
640 goto out;
641 }
642
643 if (!test_bit(BUFFERING_RX, &port->flags)) {
644 c = tty_insert_flip_string_fixed_flag(tty, data, TTY_NORMAL, n);
645 if (c > 0)
646 tty_flip_buffer_push(tty);
647 n -= c;
648
649 if (n) {
650 /* start buffering and throttling */
651 n -= fwtty_buffer_rx(port, &data[c], n);
652
653 spin_lock_bh(&port->lock);
654 __fwtty_throttle(port, tty);
655 spin_unlock_bh(&port->lock);
656 }
657 } else
658 n -= fwtty_buffer_rx(port, data, n);
659
660 if (n) {
661 port->overrun = true;
662 err = -EIO;
663 }
664
665out:
666 tty_kref_put(tty);
667
668 port->icount.rx += len;
669 port->stats.lost += n;
670 return err;
671}
672
673/**
674 * fwtty_port_handler - bus address handler for port reads/writes
675 * @parameters: fw_address_callback_t as specified by firewire core interface
676 *
677 * This handler is responsible for handling inbound read/write dma from remotes.
678 */
679static void fwtty_port_handler(struct fw_card *card,
680 struct fw_request *request,
681 int tcode, int destination, int source,
682 int generation,
683 unsigned long long addr,
684 void *data, size_t len,
685 void *callback_data)
686{
687 struct fwtty_port *port = callback_data;
688 struct fwtty_peer *peer;
689 int err;
690 int rcode;
691
692 /* Only accept rx from the peer virtual-cabled to this port */
693 rcu_read_lock();
694 peer = __fwserial_peer_by_node_id(card, generation, source);
695 rcu_read_unlock();
696 if (!peer || peer != rcu_access_pointer(port->peer)) {
697 rcode = RCODE_ADDRESS_ERROR;
698 fwtty_err_ratelimited(port, "ignoring unauthenticated data");
699 goto respond;
700 }
701
702 switch (tcode) {
703 case TCODE_WRITE_QUADLET_REQUEST:
704 if (addr != port->rx_handler.offset || len != 4)
705 rcode = RCODE_ADDRESS_ERROR;
706 else {
707 fwtty_update_port_status(port, *(unsigned *)data);
708 rcode = RCODE_COMPLETE;
709 }
710 break;
711
712 case TCODE_WRITE_BLOCK_REQUEST:
713 if (addr != port->rx_handler.offset + 4 ||
714 len > port->rx_handler.length - 4) {
715 rcode = RCODE_ADDRESS_ERROR;
716 } else {
717 err = fwtty_rx(port, data, len);
718 switch (err) {
719 case 0:
720 rcode = RCODE_COMPLETE;
721 break;
722 case -EIO:
723 rcode = RCODE_DATA_ERROR;
724 break;
725 default:
726 rcode = RCODE_CONFLICT_ERROR;
727 break;
728 }
729 }
730 break;
731
732 default:
733 rcode = RCODE_TYPE_ERROR;
734 }
735
736respond:
737 fw_send_response(card, request, rcode);
738}
739
740/**
741 * fwtty_tx_complete - callback for tx dma
742 * @data: ignored, has no meaning for write txns
743 * @length: ignored, has no meaning for write txns
744 *
745 * The writer must be woken here if the fifo has been emptied because it
746 * may have slept if chars_in_buffer was != 0
747 */
748static void fwtty_tx_complete(struct fw_card *card, int rcode,
749 void *data, size_t length,
750 struct fwtty_transaction *txn)
751{
752 struct fwtty_port *port = txn->port;
753 struct tty_struct *tty;
754 int len;
755
756 fwtty_dbg(port, "rcode: %d", rcode);
757
758 switch (rcode) {
759 case RCODE_COMPLETE:
760 spin_lock_bh(&port->lock);
761 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
762 len = dma_fifo_level(&port->tx_fifo);
763 spin_unlock_bh(&port->lock);
764
765 port->icount.tx += txn->dma_pended.len;
766 break;
767
768 default:
769 /* TODO: implement retries */
770 spin_lock_bh(&port->lock);
771 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
772 len = dma_fifo_level(&port->tx_fifo);
773 spin_unlock_bh(&port->lock);
774
775 port->stats.dropped += txn->dma_pended.len;
776 }
777
778 if (len < WAKEUP_CHARS) {
779 tty = tty_port_tty_get(&port->port);
780 if (tty) {
781 tty_wakeup(tty);
782 tty_kref_put(tty);
783 }
784 }
785}
786
787static int fwtty_tx(struct fwtty_port *port, bool drain)
788{
789 struct fwtty_peer *peer;
790 struct fwtty_transaction *txn;
791 struct tty_struct *tty;
792 int n, len;
793
794 tty = tty_port_tty_get(&port->port);
795 if (!tty)
796 return -ENOENT;
797
798 rcu_read_lock();
799 peer = rcu_dereference(port->peer);
800 if (!peer) {
801 n = -EIO;
802 goto out;
803 }
804
805 if (test_and_set_bit(IN_TX, &port->flags)) {
806 n = -EALREADY;
807 goto out;
808 }
809
810 /* try to write as many dma transactions out as possible */
811 n = -EAGAIN;
812 while (!tty->stopped && !tty->hw_stopped &&
813 !test_bit(STOP_TX, &port->flags)) {
814 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
815 if (!txn) {
816 n = -ENOMEM;
817 break;
818 }
819
820 spin_lock_bh(&port->lock);
821 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
822 spin_unlock_bh(&port->lock);
823
824 fwtty_dbg(port, "out: %u rem: %d", txn->dma_pended.len, n);
825
826 if (n < 0) {
827 kmem_cache_free(fwtty_txn_cache, txn);
828 if (n == -EAGAIN)
829 ++port->stats.tx_stall;
830 else if (n == -ENODATA)
831 profile_size_distrib(port->stats.txns, 0);
832 else {
833 ++port->stats.fifo_errs;
834 fwtty_err_ratelimited(port, "fifo err: %d", n);
835 }
836 break;
837 }
838
839 profile_size_distrib(port->stats.txns, txn->dma_pended.len);
840
841 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
842 peer->fifo_addr, txn->dma_pended.data,
843 txn->dma_pended.len, fwtty_tx_complete,
844 port);
845 ++port->stats.sent;
846
847 /*
848 * Stop tx if the 'last view' of the fifo is empty or if
849 * this is the writer and there's not enough data to bother
850 */
851 if (n == 0 || (!drain && n < WRITER_MINIMUM))
852 break;
853 }
854
855 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
856 spin_lock_bh(&port->lock);
857 len = dma_fifo_out_level(&port->tx_fifo);
858 if (len) {
859 unsigned long delay = (n == -ENOMEM) ? HZ : 1;
860 schedule_delayed_work(&port->drain, delay);
861 }
862 len = dma_fifo_level(&port->tx_fifo);
863 spin_unlock_bh(&port->lock);
864
865 /* wakeup the writer */
866 if (drain && len < WAKEUP_CHARS)
867 tty_wakeup(tty);
868 }
869
870 clear_bit(IN_TX, &port->flags);
871 wake_up_interruptible(&port->wait_tx);
872
873out:
874 rcu_read_unlock();
875 tty_kref_put(tty);
876 return n;
877}
878
879static void fwtty_drain_tx(struct work_struct *work)
880{
881 struct fwtty_port *port = to_port(to_delayed_work(work), drain);
882
883 fwtty_tx(port, true);
884}
885
886static void fwtty_write_xchar(struct fwtty_port *port, char ch)
887{
888 struct fwtty_peer *peer;
889
890 ++port->stats.xchars;
891
892 fwtty_dbg(port, "%02x", ch);
893
894 rcu_read_lock();
895 peer = rcu_dereference(port->peer);
896 if (peer) {
897 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
898 peer->fifo_addr, &ch, sizeof(ch),
899 NULL, port);
900 }
901 rcu_read_unlock();
902}
903
904struct fwtty_port *fwtty_port_get(unsigned index)
905{
906 struct fwtty_port *port;
907
908 if (index >= MAX_TOTAL_PORTS)
909 return NULL;
910
911 mutex_lock(&port_table_lock);
912 port = port_table[index];
913 if (port)
914 kref_get(&port->serial->kref);
915 mutex_unlock(&port_table_lock);
916 return port;
917}
918EXPORT_SYMBOL(fwtty_port_get);
919
920static int fwtty_ports_add(struct fw_serial *serial)
921{
922 int err = -EBUSY;
923 int i, j;
924
925 if (port_table_corrupt)
926 return err;
927
928 mutex_lock(&port_table_lock);
929 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
930 if (!port_table[i]) {
931 for (j = 0; j < num_ports; ++i, ++j) {
932 serial->ports[j]->index = i;
933 port_table[i] = serial->ports[j];
934 }
935 err = 0;
936 break;
937 }
938 }
939 mutex_unlock(&port_table_lock);
940 return err;
941}
942
943static void fwserial_destroy(struct kref *kref)
944{
945 struct fw_serial *serial = to_serial(kref, kref);
946 struct fwtty_port **ports = serial->ports;
947 int j, i = ports[0]->index;
948
949 synchronize_rcu();
950
951 mutex_lock(&port_table_lock);
952 for (j = 0; j < num_ports; ++i, ++j) {
Peter Hurley49b27462012-11-27 21:37:12 -0500953 port_table_corrupt |= port_table[i] != ports[j];
954 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
955 i, port_table[i], j, ports[j]);
Peter Hurley7355ba32012-11-02 08:16:33 -0400956
957 port_table[i] = NULL;
958 }
959 mutex_unlock(&port_table_lock);
960
961 for (j = 0; j < num_ports; ++j) {
962 fw_core_remove_address_handler(&ports[j]->rx_handler);
Peter Hurleya3218462012-11-27 21:37:11 -0500963 tty_port_destroy(&ports[j]->port);
Peter Hurley7355ba32012-11-02 08:16:33 -0400964 kfree(ports[j]);
965 }
966 kfree(serial);
967}
968
969void fwtty_port_put(struct fwtty_port *port)
970{
971 kref_put(&port->serial->kref, fwserial_destroy);
972}
973EXPORT_SYMBOL(fwtty_port_put);
974
975static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
976{
977 struct fwtty_port *port = to_port(tty_port, port);
978
979 fwtty_dbg(port, "on/off: %d", on);
980
981 spin_lock_bh(&port->lock);
982 /* Don't change carrier state if this is a console */
983 if (!port->port.console) {
984 if (on)
985 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
986 else
987 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
988 }
989
990 __fwtty_write_port_status(port);
991 spin_unlock_bh(&port->lock);
992}
993
994/**
995 * fwtty_port_carrier_raised: required tty_port operation
996 *
997 * This port operation is polled after a tty has been opened and is waiting for
998 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
999 */
1000static int fwtty_port_carrier_raised(struct tty_port *tty_port)
1001{
1002 struct fwtty_port *port = to_port(tty_port, port);
1003 int rc;
1004
1005 rc = (port->mstatus & TIOCM_CAR);
1006
1007 fwtty_dbg(port, "%d", rc);
1008
1009 return rc;
1010}
1011
1012static unsigned set_termios(struct fwtty_port *port, struct tty_struct *tty)
1013{
1014 unsigned baud, frame;
1015
1016 baud = tty_termios_baud_rate(&tty->termios);
1017 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
1018
1019 /* compute bit count of 2 frames */
1020 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
1021
1022 switch (C_CSIZE(tty)) {
1023 case CS5:
1024 frame -= (C_CSTOPB(tty)) ? 1 : 0;
1025 break;
1026 case CS6:
1027 frame += 2;
1028 break;
1029 case CS7:
1030 frame += 4;
1031 break;
1032 case CS8:
1033 frame += 6;
1034 break;
1035 }
1036
1037 port->cps = (baud << 1) / frame;
1038
1039 port->status_mask = UART_LSR_OE;
1040 if (_I_FLAG(tty, BRKINT | PARMRK))
1041 port->status_mask |= UART_LSR_BI;
1042
1043 port->ignore_mask = 0;
1044 if (I_IGNBRK(tty)) {
1045 port->ignore_mask |= UART_LSR_BI;
1046 if (I_IGNPAR(tty))
1047 port->ignore_mask |= UART_LSR_OE;
1048 }
1049
1050 port->write_only = !C_CREAD(tty);
1051
1052 /* turn off echo and newline xlat if loopback */
1053 if (port->loopback) {
1054 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
1055 ECHONL | ECHOPRT | ECHOCTL);
1056 tty->termios.c_oflag &= ~ONLCR;
1057 }
1058
1059 return baud;
1060}
1061
1062static int fwtty_port_activate(struct tty_port *tty_port,
1063 struct tty_struct *tty)
1064{
1065 struct fwtty_port *port = to_port(tty_port, port);
1066 unsigned baud;
1067 int err;
1068
1069 set_bit(TTY_IO_ERROR, &tty->flags);
1070
1071 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
1072 cache_line_size(),
1073 port->max_payload,
1074 FWTTY_PORT_MAX_PEND_DMA,
1075 GFP_KERNEL);
1076 if (err)
1077 return err;
1078
1079 spin_lock_bh(&port->lock);
1080
1081 baud = set_termios(port, tty);
1082
1083 /* if console, don't change carrier state */
1084 if (!port->port.console) {
1085 port->mctrl = 0;
1086 if (baud != 0)
1087 port->mctrl = TIOCM_DTR | TIOCM_RTS;
1088 }
1089
1090 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
1091 tty->hw_stopped = 1;
1092
1093 __fwtty_write_port_status(port);
1094 spin_unlock_bh(&port->lock);
1095
1096 clear_bit(TTY_IO_ERROR, &tty->flags);
1097
1098 return 0;
1099}
1100
1101/**
1102 * fwtty_port_shutdown
1103 *
1104 * Note: the tty port core ensures this is not the console and
1105 * manages TTY_IO_ERROR properly
1106 */
1107static void fwtty_port_shutdown(struct tty_port *tty_port)
1108{
1109 struct fwtty_port *port = to_port(tty_port, port);
1110 struct buffered_rx *buf, *next;
1111
1112 /* TODO: cancel outstanding transactions */
1113
1114 cancel_delayed_work_sync(&port->emit_breaks);
1115 cancel_delayed_work_sync(&port->drain);
1116 cancel_work_sync(&port->push);
1117
1118 spin_lock_bh(&port->lock);
1119 list_for_each_entry_safe(buf, next, &port->buf_list, list) {
1120 list_del(&buf->list);
1121 kfree(buf);
1122 }
1123 port->buffered = 0;
1124 port->flags = 0;
1125 port->break_ctl = 0;
1126 port->overrun = 0;
1127 __fwtty_write_port_status(port);
1128 dma_fifo_free(&port->tx_fifo);
1129 spin_unlock_bh(&port->lock);
1130}
1131
1132static int fwtty_open(struct tty_struct *tty, struct file *fp)
1133{
1134 struct fwtty_port *port = tty->driver_data;
1135
1136 return tty_port_open(&port->port, tty, fp);
1137}
1138
1139static void fwtty_close(struct tty_struct *tty, struct file *fp)
1140{
1141 struct fwtty_port *port = tty->driver_data;
1142
1143 tty_port_close(&port->port, tty, fp);
1144}
1145
1146static void fwtty_hangup(struct tty_struct *tty)
1147{
1148 struct fwtty_port *port = tty->driver_data;
1149
1150 tty_port_hangup(&port->port);
1151}
1152
1153static void fwtty_cleanup(struct tty_struct *tty)
1154{
1155 struct fwtty_port *port = tty->driver_data;
1156
1157 tty->driver_data = NULL;
1158 fwtty_port_put(port);
1159}
1160
1161static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
1162{
1163 struct fwtty_port *port = fwtty_port_get(tty->index);
1164 int err;
1165
1166 err = tty_standard_install(driver, tty);
1167 if (!err)
1168 tty->driver_data = port;
1169 else
1170 fwtty_port_put(port);
1171 return err;
1172}
1173
Peter Hurleyfa1da242013-01-28 22:34:38 -05001174static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty)
1175{
1176 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index));
1177 int err;
1178
1179 err = tty_standard_install(driver, tty);
1180 if (!err)
1181 tty->driver_data = port;
1182 else
1183 fwtty_port_put(port);
1184 return err;
1185}
1186
Peter Hurley7355ba32012-11-02 08:16:33 -04001187static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1188{
1189 struct fwtty_port *port = tty->driver_data;
1190 int n, len;
1191
1192 fwtty_dbg(port, "%d", c);
1193 profile_size_distrib(port->stats.writes, c);
1194
1195 spin_lock_bh(&port->lock);
1196 n = dma_fifo_in(&port->tx_fifo, buf, c);
1197 len = dma_fifo_out_level(&port->tx_fifo);
1198 if (len < DRAIN_THRESHOLD)
1199 schedule_delayed_work(&port->drain, 1);
1200 spin_unlock_bh(&port->lock);
1201
1202 if (len >= DRAIN_THRESHOLD)
1203 fwtty_tx(port, false);
1204
1205 debug_short_write(port, c, n);
1206
1207 return (n < 0) ? 0 : n;
1208}
1209
1210static int fwtty_write_room(struct tty_struct *tty)
1211{
1212 struct fwtty_port *port = tty->driver_data;
1213 int n;
1214
1215 spin_lock_bh(&port->lock);
1216 n = dma_fifo_avail(&port->tx_fifo);
1217 spin_unlock_bh(&port->lock);
1218
1219 fwtty_dbg(port, "%d", n);
1220
1221 return n;
1222}
1223
1224static int fwtty_chars_in_buffer(struct tty_struct *tty)
1225{
1226 struct fwtty_port *port = tty->driver_data;
1227 int n;
1228
1229 spin_lock_bh(&port->lock);
1230 n = dma_fifo_level(&port->tx_fifo);
1231 spin_unlock_bh(&port->lock);
1232
1233 fwtty_dbg(port, "%d", n);
1234
1235 return n;
1236}
1237
1238static void fwtty_send_xchar(struct tty_struct *tty, char ch)
1239{
1240 struct fwtty_port *port = tty->driver_data;
1241
1242 fwtty_dbg(port, "%02x", ch);
1243
1244 fwtty_write_xchar(port, ch);
1245}
1246
1247static void fwtty_throttle(struct tty_struct *tty)
1248{
1249 struct fwtty_port *port = tty->driver_data;
1250
1251 /*
1252 * Ignore throttling (but not unthrottling).
1253 * It only makes sense to throttle when data will no longer be
1254 * accepted by the tty flip buffer. For example, it is
1255 * possible for received data to overflow the tty buffer long
1256 * before the line discipline ever has a chance to throttle the driver.
1257 * Additionally, the driver may have already completed the I/O
1258 * but the tty buffer is still emptying, so the line discipline is
1259 * throttling and unthrottling nothing.
1260 */
1261
1262 ++port->stats.throttled;
1263}
1264
1265static void fwtty_unthrottle(struct tty_struct *tty)
1266{
1267 struct fwtty_port *port = tty->driver_data;
1268
1269 fwtty_dbg(port, "CRTSCTS: %d", (C_CRTSCTS(tty) != 0));
1270
1271 profile_fifo_avail(port, port->stats.unthrottle);
1272
1273 schedule_work(&port->push);
1274
1275 spin_lock_bh(&port->lock);
1276 port->mctrl &= ~OOB_RX_THROTTLE;
1277 if (C_CRTSCTS(tty))
1278 port->mctrl |= TIOCM_RTS;
1279 __fwtty_write_port_status(port);
1280 spin_unlock_bh(&port->lock);
1281}
1282
1283static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
1284 struct async_icount *prev)
1285{
1286 struct async_icount now;
1287 int delta;
1288
1289 now = port->icount;
1290
1291 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
1292 (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
1293 (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
1294 (mask & TIOCM_CTS && prev->cts != now.cts));
1295
1296 *prev = now;
1297
1298 return delta;
1299}
1300
1301static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
1302{
1303 struct async_icount prev;
1304
1305 prev = port->icount;
1306
1307 return wait_event_interruptible(port->port.delta_msr_wait,
1308 check_msr_delta(port, mask, &prev));
1309}
1310
1311static int get_serial_info(struct fwtty_port *port,
1312 struct serial_struct __user *info)
1313{
1314 struct serial_struct tmp;
1315
1316 memset(&tmp, 0, sizeof(tmp));
1317
1318 tmp.type = PORT_UNKNOWN;
1319 tmp.line = port->port.tty->index;
1320 tmp.flags = port->port.flags;
1321 tmp.xmit_fifo_size = FWTTY_PORT_TXFIFO_LEN;
1322 tmp.baud_base = 400000000;
1323 tmp.close_delay = port->port.close_delay;
1324
1325 return (copy_to_user(info, &tmp, sizeof(*info))) ? -EFAULT : 0;
1326}
1327
1328static int set_serial_info(struct fwtty_port *port,
1329 struct serial_struct __user *info)
1330{
1331 struct serial_struct tmp;
1332
1333 if (copy_from_user(&tmp, info, sizeof(tmp)))
1334 return -EFAULT;
1335
1336 if (tmp.irq != 0 || tmp.port != 0 || tmp.custom_divisor != 0 ||
1337 tmp.baud_base != 400000000)
1338 return -EPERM;
1339
1340 if (!capable(CAP_SYS_ADMIN)) {
1341 if (((tmp.flags & ~ASYNC_USR_MASK) !=
1342 (port->port.flags & ~ASYNC_USR_MASK)))
1343 return -EPERM;
1344 } else
1345 port->port.close_delay = tmp.close_delay * HZ / 100;
1346
1347 return 0;
1348}
1349
1350static int fwtty_ioctl(struct tty_struct *tty, unsigned cmd,
1351 unsigned long arg)
1352{
1353 struct fwtty_port *port = tty->driver_data;
1354 int err;
1355
1356 switch (cmd) {
1357 case TIOCGSERIAL:
1358 mutex_lock(&port->port.mutex);
1359 err = get_serial_info(port, (void __user *)arg);
1360 mutex_unlock(&port->port.mutex);
1361 break;
1362
1363 case TIOCSSERIAL:
1364 mutex_lock(&port->port.mutex);
1365 err = set_serial_info(port, (void __user *)arg);
1366 mutex_unlock(&port->port.mutex);
1367 break;
1368
1369 case TIOCMIWAIT:
1370 err = wait_msr_change(port, arg);
1371 break;
1372
1373 default:
1374 err = -ENOIOCTLCMD;
1375 }
1376
1377 return err;
1378}
1379
1380static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
1381{
1382 struct fwtty_port *port = tty->driver_data;
1383 unsigned baud;
1384
1385 spin_lock_bh(&port->lock);
1386 baud = set_termios(port, tty);
1387
1388 if ((baud == 0) && (old->c_cflag & CBAUD))
1389 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
1390 else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
1391 if (C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
1392 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
1393 else
1394 port->mctrl |= TIOCM_DTR;
1395 }
1396 __fwtty_write_port_status(port);
1397 spin_unlock_bh(&port->lock);
1398
1399 if (old->c_cflag & CRTSCTS) {
1400 if (!C_CRTSCTS(tty)) {
1401 tty->hw_stopped = 0;
1402 fwtty_restart_tx(port);
1403 }
1404 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
1405 tty->hw_stopped = 1;
1406 }
1407}
1408
1409/**
1410 * fwtty_break_ctl - start/stop sending breaks
1411 *
1412 * Signals the remote to start or stop generating simulated breaks.
1413 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1414 * before signalling the break line status. This guarantees any pending rx will
1415 * be queued to the line discipline before break is simulated on the remote.
1416 * Conversely, turning off break_ctl requires signalling the line status change,
1417 * then enabling tx.
1418 */
1419static int fwtty_break_ctl(struct tty_struct *tty, int state)
1420{
1421 struct fwtty_port *port = tty->driver_data;
1422 long ret;
1423
1424 fwtty_dbg(port, "%d", state);
1425
1426 if (state == -1) {
1427 set_bit(STOP_TX, &port->flags);
1428 ret = wait_event_interruptible_timeout(port->wait_tx,
1429 !test_bit(IN_TX, &port->flags),
1430 10);
1431 if (ret == 0 || ret == -ERESTARTSYS) {
1432 clear_bit(STOP_TX, &port->flags);
1433 fwtty_restart_tx(port);
1434 return -EINTR;
1435 }
1436 }
1437
1438 spin_lock_bh(&port->lock);
1439 port->break_ctl = (state == -1);
1440 __fwtty_write_port_status(port);
1441 spin_unlock_bh(&port->lock);
1442
1443 if (state == 0) {
1444 spin_lock_bh(&port->lock);
1445 dma_fifo_reset(&port->tx_fifo);
1446 clear_bit(STOP_TX, &port->flags);
1447 spin_unlock_bh(&port->lock);
1448 }
1449 return 0;
1450}
1451
1452static int fwtty_tiocmget(struct tty_struct *tty)
1453{
1454 struct fwtty_port *port = tty->driver_data;
1455 unsigned tiocm;
1456
1457 spin_lock_bh(&port->lock);
1458 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
1459 spin_unlock_bh(&port->lock);
1460
1461 fwtty_dbg(port, "%x", tiocm);
1462
1463 return tiocm;
1464}
1465
1466static int fwtty_tiocmset(struct tty_struct *tty, unsigned set, unsigned clear)
1467{
1468 struct fwtty_port *port = tty->driver_data;
1469
1470 fwtty_dbg(port, "set: %x clear: %x", set, clear);
1471
1472 /* TODO: simulate loopback if TIOCM_LOOP set */
1473
1474 spin_lock_bh(&port->lock);
1475 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
1476 port->mctrl |= set & MCTRL_MASK & 0xffff;
1477 __fwtty_write_port_status(port);
1478 spin_unlock_bh(&port->lock);
1479 return 0;
1480}
1481
1482static int fwtty_get_icount(struct tty_struct *tty,
1483 struct serial_icounter_struct *icount)
1484{
1485 struct fwtty_port *port = tty->driver_data;
1486 struct stats stats;
1487
1488 memcpy(&stats, &port->stats, sizeof(stats));
1489 if (port->port.console)
1490 (*port->fwcon_ops->stats)(&stats, port->con_data);
1491
1492 icount->cts = port->icount.cts;
1493 icount->dsr = port->icount.dsr;
1494 icount->rng = port->icount.rng;
1495 icount->dcd = port->icount.dcd;
1496 icount->rx = port->icount.rx;
1497 icount->tx = port->icount.tx + stats.xchars;
1498 icount->frame = port->icount.frame;
1499 icount->overrun = port->icount.overrun;
1500 icount->parity = port->icount.parity;
1501 icount->brk = port->icount.brk;
1502 icount->buf_overrun = port->icount.overrun;
1503 return 0;
1504}
1505
1506static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
1507{
1508 struct stats stats;
1509
1510 memcpy(&stats, &port->stats, sizeof(stats));
1511 if (port->port.console)
1512 (*port->fwcon_ops->stats)(&stats, port->con_data);
1513
Peter Hurleye16d1de2013-01-28 22:34:39 -05001514 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset,
1515 port->icount.tx + stats.xchars, port->icount.rx);
Peter Hurley7355ba32012-11-02 08:16:33 -04001516 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
1517 port->icount.dsr, port->icount.rng, port->icount.dcd);
1518 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
1519 port->icount.overrun, port->icount.parity, port->icount.brk);
Peter Hurleye16d1de2013-01-28 22:34:39 -05001520}
1521
1522static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port)
1523{
1524 struct stats stats;
1525
1526 memcpy(&stats, &port->stats, sizeof(stats));
1527 if (port->port.console)
1528 (*port->fwcon_ops->stats)(&stats, port->con_data);
1529
Peter Hurley7355ba32012-11-02 08:16:33 -04001530 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
1531 stats.tx_stall, stats.fifo_errs, stats.lost);
1532 seq_printf(m, " pkts:%d thr:%d wtrmk:%d", stats.sent, stats.throttled,
1533 stats.watermark);
Peter Hurley7355ba32012-11-02 08:16:33 -04001534
1535 if (port->port.console) {
1536 seq_printf(m, "\n ");
1537 (*port->fwcon_ops->proc_show)(m, port->con_data);
1538 }
1539
1540 dump_profile(m, &port->stats);
1541}
1542
Peter Hurleye16d1de2013-01-28 22:34:39 -05001543static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer)
Peter Hurley7355ba32012-11-02 08:16:33 -04001544{
1545 int generation = peer->generation;
1546
1547 smp_rmb();
1548 seq_printf(m, " %s:", dev_name(&peer->unit->device));
1549 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
1550 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
1551 peer->max_payload, (unsigned long long) peer->guid);
Peter Hurleye16d1de2013-01-28 22:34:39 -05001552 seq_printf(m, " mgmt:%012llx", (unsigned long long) peer->mgmt_addr);
1553 seq_printf(m, " addr:%012llx", (unsigned long long) peer->status_addr);
Peter Hurley7355ba32012-11-02 08:16:33 -04001554 seq_putc(m, '\n');
1555}
1556
1557static int fwtty_proc_show(struct seq_file *m, void *v)
1558{
1559 struct fwtty_port *port;
Peter Hurley7355ba32012-11-02 08:16:33 -04001560 int i;
1561
1562 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
1563 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
1564 seq_printf(m, "%2d:", i);
1565 if (capable(CAP_SYS_ADMIN))
1566 fwtty_proc_show_port(m, port);
1567 fwtty_port_put(port);
1568 seq_printf(m, "\n");
1569 }
Peter Hurley7355ba32012-11-02 08:16:33 -04001570 return 0;
1571}
1572
Peter Hurley4df5bb02013-01-28 22:34:40 -05001573static int fwtty_debugfs_stats_show(struct seq_file *m, void *v)
1574{
1575 struct fw_serial *serial = m->private;
1576 struct fwtty_port *port;
1577 int i;
1578
1579 for (i = 0; i < num_ports; ++i) {
1580 port = fwtty_port_get(serial->ports[i]->index);
1581 if (port) {
1582 seq_printf(m, "%2d:", port->index);
1583 fwtty_proc_show_port(m, port);
1584 fwtty_debugfs_show_port(m, port);
1585 fwtty_port_put(port);
1586 seq_printf(m, "\n");
1587 }
1588 }
1589 return 0;
1590}
1591
1592static int fwtty_debugfs_peers_show(struct seq_file *m, void *v)
1593{
1594 struct fw_serial *serial = m->private;
1595 struct fwtty_peer *peer;
1596
1597 rcu_read_lock();
1598 seq_printf(m, "card: %s guid: %016llx\n",
1599 dev_name(serial->card->device),
1600 (unsigned long long) serial->card->guid);
1601 list_for_each_entry_rcu(peer, &serial->peer_list, list)
1602 fwtty_debugfs_show_peer(m, peer);
1603 rcu_read_unlock();
1604 return 0;
1605}
1606
Peter Hurley7355ba32012-11-02 08:16:33 -04001607static int fwtty_proc_open(struct inode *inode, struct file *fp)
1608{
1609 return single_open(fp, fwtty_proc_show, NULL);
1610}
1611
Peter Hurley4df5bb02013-01-28 22:34:40 -05001612static int fwtty_stats_open(struct inode *inode, struct file *fp)
1613{
1614 return single_open(fp, fwtty_debugfs_stats_show, inode->i_private);
1615}
1616
1617static int fwtty_peers_open(struct inode *inode, struct file *fp)
1618{
1619 return single_open(fp, fwtty_debugfs_peers_show, inode->i_private);
1620}
1621
1622static const struct file_operations fwtty_stats_fops = {
1623 .owner = THIS_MODULE,
1624 .open = fwtty_stats_open,
1625 .read = seq_read,
1626 .llseek = seq_lseek,
1627 .release = single_release,
1628};
1629
1630static const struct file_operations fwtty_peers_fops = {
1631 .owner = THIS_MODULE,
1632 .open = fwtty_peers_open,
1633 .read = seq_read,
1634 .llseek = seq_lseek,
1635 .release = single_release,
1636};
1637
Peter Hurley7355ba32012-11-02 08:16:33 -04001638static const struct file_operations fwtty_proc_fops = {
1639 .owner = THIS_MODULE,
1640 .open = fwtty_proc_open,
1641 .read = seq_read,
1642 .llseek = seq_lseek,
1643 .release = single_release,
1644};
1645
1646static const struct tty_port_operations fwtty_port_ops = {
1647 .dtr_rts = fwtty_port_dtr_rts,
1648 .carrier_raised = fwtty_port_carrier_raised,
1649 .shutdown = fwtty_port_shutdown,
1650 .activate = fwtty_port_activate,
1651};
1652
1653static const struct tty_operations fwtty_ops = {
1654 .open = fwtty_open,
1655 .close = fwtty_close,
1656 .hangup = fwtty_hangup,
1657 .cleanup = fwtty_cleanup,
1658 .install = fwtty_install,
1659 .write = fwtty_write,
1660 .write_room = fwtty_write_room,
1661 .chars_in_buffer = fwtty_chars_in_buffer,
1662 .send_xchar = fwtty_send_xchar,
1663 .throttle = fwtty_throttle,
1664 .unthrottle = fwtty_unthrottle,
1665 .ioctl = fwtty_ioctl,
1666 .set_termios = fwtty_set_termios,
1667 .break_ctl = fwtty_break_ctl,
1668 .tiocmget = fwtty_tiocmget,
1669 .tiocmset = fwtty_tiocmset,
1670 .get_icount = fwtty_get_icount,
1671 .proc_fops = &fwtty_proc_fops,
1672};
1673
Peter Hurleyfa1da242013-01-28 22:34:38 -05001674static const struct tty_operations fwloop_ops = {
1675 .open = fwtty_open,
1676 .close = fwtty_close,
1677 .hangup = fwtty_hangup,
1678 .cleanup = fwtty_cleanup,
1679 .install = fwloop_install,
1680 .write = fwtty_write,
1681 .write_room = fwtty_write_room,
1682 .chars_in_buffer = fwtty_chars_in_buffer,
1683 .send_xchar = fwtty_send_xchar,
1684 .throttle = fwtty_throttle,
1685 .unthrottle = fwtty_unthrottle,
1686 .ioctl = fwtty_ioctl,
1687 .set_termios = fwtty_set_termios,
1688 .break_ctl = fwtty_break_ctl,
1689 .tiocmget = fwtty_tiocmget,
1690 .tiocmset = fwtty_tiocmset,
1691 .get_icount = fwtty_get_icount,
1692};
1693
Peter Hurley7355ba32012-11-02 08:16:33 -04001694static inline int mgmt_pkt_expected_len(__be16 code)
1695{
1696 static const struct fwserial_mgmt_pkt pkt;
1697
1698 switch (be16_to_cpu(code)) {
1699 case FWSC_VIRT_CABLE_PLUG:
1700 return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
1701
1702 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */
1703 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
1704
1705
1706 case FWSC_VIRT_CABLE_UNPLUG:
1707 case FWSC_VIRT_CABLE_UNPLUG_RSP:
1708 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
1709 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
1710 return sizeof(pkt.hdr);
1711
1712 default:
1713 return -1;
1714 }
1715}
1716
1717static inline void fill_plug_params(struct virt_plug_params *params,
1718 struct fwtty_port *port)
1719{
1720 u64 status_addr = port->rx_handler.offset;
1721 u64 fifo_addr = port->rx_handler.offset + 4;
1722 size_t fifo_len = port->rx_handler.length - 4;
1723
1724 params->status_hi = cpu_to_be32(status_addr >> 32);
1725 params->status_lo = cpu_to_be32(status_addr);
1726 params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
1727 params->fifo_lo = cpu_to_be32(fifo_addr);
1728 params->fifo_len = cpu_to_be32(fifo_len);
1729}
1730
1731static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
1732 struct fwtty_port *port)
1733{
1734 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
1735 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1736 fill_plug_params(&pkt->plug_req, port);
1737}
1738
1739static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
1740 struct fwtty_port *port)
1741{
1742 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
1743 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1744 fill_plug_params(&pkt->plug_rsp, port);
1745}
1746
1747static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1748{
1749 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
1750 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1751}
1752
1753static inline void fill_unplug_req(struct fwserial_mgmt_pkt *pkt)
1754{
1755 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG);
1756 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1757}
1758
1759static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1760{
1761 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
1762 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1763}
1764
1765static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
1766{
1767 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
1768 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1769}
1770
1771static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
1772 struct virt_plug_params *params)
1773{
1774 struct fwtty_port *port = peer->port;
1775
1776 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
1777 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
1778 peer->fifo_len = be32_to_cpu(params->fifo_len);
1779 peer_set_state(peer, FWPS_ATTACHED);
1780
1781 /* reconfigure tx_fifo optimally for this peer */
1782 spin_lock_bh(&port->lock);
Peter Hurley3b1f3152013-01-28 20:57:47 -05001783 port->max_payload = min(peer->max_payload, peer->fifo_len);
Peter Hurley7355ba32012-11-02 08:16:33 -04001784 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1785 spin_unlock_bh(&peer->port->lock);
1786
1787 if (port->port.console && port->fwcon_ops->notify != NULL)
1788 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
1789
1790 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s",
1791 (unsigned long long)peer->guid, dev_name(port->device));
1792}
1793
1794static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
1795 struct fwserial_mgmt_pkt *pkt)
1796{
1797 int generation;
1798 int rcode, tries = 5;
1799
1800 do {
1801 generation = peer->generation;
1802 smp_rmb();
1803
1804 rcode = fw_run_transaction(peer->serial->card,
1805 TCODE_WRITE_BLOCK_REQUEST,
1806 peer->node_id,
1807 generation, peer->speed,
1808 peer->mgmt_addr,
1809 pkt, be16_to_cpu(pkt->hdr.len));
1810 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
1811 rcode == RCODE_GENERATION) {
1812 fwtty_dbg(&peer->unit, "mgmt write error: %d", rcode);
1813 continue;
1814 } else
1815 break;
1816 } while (--tries > 0);
1817 return rcode;
1818}
1819
1820/**
1821 * fwserial_claim_port - attempt to claim port @ index for peer
1822 *
1823 * Returns ptr to claimed port or error code (as ERR_PTR())
1824 * Can sleep - must be called from process context
1825 */
1826static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
1827 int index)
1828{
1829 struct fwtty_port *port;
1830
1831 if (index < 0 || index >= num_ports)
1832 return ERR_PTR(-EINVAL);
1833
1834 /* must guarantee that previous port releases have completed */
1835 synchronize_rcu();
1836
1837 port = peer->serial->ports[index];
1838 spin_lock_bh(&port->lock);
1839 if (!rcu_access_pointer(port->peer))
1840 rcu_assign_pointer(port->peer, peer);
1841 else
1842 port = ERR_PTR(-EBUSY);
1843 spin_unlock_bh(&port->lock);
1844
1845 return port;
1846}
1847
1848/**
1849 * fwserial_find_port - find avail port and claim for peer
1850 *
1851 * Returns ptr to claimed port or NULL if none avail
1852 * Can sleep - must be called from process context
1853 */
1854static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
1855{
1856 struct fwtty_port **ports = peer->serial->ports;
1857 int i;
1858
1859 /* must guarantee that previous port releases have completed */
1860 synchronize_rcu();
1861
1862 /* TODO: implement optional GUID-to-specific port # matching */
1863
1864 /* find an unattached port (but not the loopback port, if present) */
1865 for (i = 0; i < num_ttys; ++i) {
1866 spin_lock_bh(&ports[i]->lock);
1867 if (!ports[i]->peer) {
1868 /* claim port */
1869 rcu_assign_pointer(ports[i]->peer, peer);
1870 spin_unlock_bh(&ports[i]->lock);
1871 return ports[i];
1872 }
1873 spin_unlock_bh(&ports[i]->lock);
1874 }
1875 return NULL;
1876}
1877
Peter Hurleyde321a12013-01-28 22:34:35 -05001878static void fwserial_release_port(struct fwtty_port *port, bool reset)
Peter Hurley7355ba32012-11-02 08:16:33 -04001879{
1880 /* drop carrier (and all other line status) */
Peter Hurleyde321a12013-01-28 22:34:35 -05001881 if (reset)
1882 fwtty_update_port_status(port, 0);
Peter Hurley7355ba32012-11-02 08:16:33 -04001883
1884 spin_lock_bh(&port->lock);
1885
1886 /* reset dma fifo max transmission size back to S100 */
1887 port->max_payload = link_speed_to_max_payload(SCODE_100);
1888 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1889
1890 rcu_assign_pointer(port->peer, NULL);
1891 spin_unlock_bh(&port->lock);
1892
1893 if (port->port.console && port->fwcon_ops->notify != NULL)
1894 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
1895}
1896
1897static void fwserial_plug_timeout(unsigned long data)
1898{
1899 struct fwtty_peer *peer = (struct fwtty_peer *) data;
1900 struct fwtty_port *port;
1901
1902 spin_lock_bh(&peer->lock);
1903 if (peer->state != FWPS_PLUG_PENDING) {
1904 spin_unlock_bh(&peer->lock);
1905 return;
1906 }
1907
1908 port = peer_revert_state(peer);
1909 spin_unlock_bh(&peer->lock);
1910
1911 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05001912 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04001913}
1914
1915/**
1916 * fwserial_connect_peer - initiate virtual cable with peer
1917 *
1918 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1919 * otherwise error code. Must be called from process context.
1920 */
1921static int fwserial_connect_peer(struct fwtty_peer *peer)
1922{
1923 struct fwtty_port *port;
1924 struct fwserial_mgmt_pkt *pkt;
1925 int err, rcode;
1926
1927 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1928 if (!pkt)
1929 return -ENOMEM;
1930
1931 port = fwserial_find_port(peer);
1932 if (!port) {
1933 fwtty_err(&peer->unit, "avail ports in use");
1934 err = -EBUSY;
1935 goto free_pkt;
1936 }
1937
1938 spin_lock_bh(&peer->lock);
1939
1940 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1941 if (peer->state != FWPS_NOT_ATTACHED) {
1942 err = -EBUSY;
1943 goto release_port;
1944 }
1945
1946 peer->port = port;
1947 peer_set_state(peer, FWPS_PLUG_PENDING);
1948
1949 fill_plug_req(pkt, peer->port);
1950
1951 setup_timer(&peer->timer, fwserial_plug_timeout, (unsigned long)peer);
1952 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
1953 spin_unlock_bh(&peer->lock);
1954
1955 rcode = fwserial_send_mgmt_sync(peer, pkt);
1956
1957 spin_lock_bh(&peer->lock);
1958 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
1959 if (rcode == RCODE_CONFLICT_ERROR)
1960 err = -EAGAIN;
1961 else
1962 err = -EIO;
1963 goto cancel_timer;
1964 }
1965 spin_unlock_bh(&peer->lock);
1966
1967 kfree(pkt);
1968 return 0;
1969
1970cancel_timer:
1971 del_timer(&peer->timer);
1972 peer_revert_state(peer);
1973release_port:
1974 spin_unlock_bh(&peer->lock);
Peter Hurleyde321a12013-01-28 22:34:35 -05001975 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04001976free_pkt:
1977 kfree(pkt);
1978 return err;
1979}
1980
1981/**
1982 * fwserial_close_port -
1983 * HUP the tty (if the tty exists) and unregister the tty device.
1984 * Only used by the unit driver upon unit removal to disconnect and
1985 * cleanup all attached ports
1986 *
1987 * The port reference is put by fwtty_cleanup (if a reference was
1988 * ever taken).
1989 */
Peter Hurleyfa1da242013-01-28 22:34:38 -05001990static void fwserial_close_port(struct tty_driver *driver,
1991 struct fwtty_port *port)
Peter Hurley7355ba32012-11-02 08:16:33 -04001992{
1993 struct tty_struct *tty;
1994
1995 mutex_lock(&port->port.mutex);
1996 tty = tty_port_tty_get(&port->port);
1997 if (tty) {
1998 tty_vhangup(tty);
1999 tty_kref_put(tty);
2000 }
2001 mutex_unlock(&port->port.mutex);
2002
Peter Hurleyfa1da242013-01-28 22:34:38 -05002003 if (driver == fwloop_driver)
2004 tty_unregister_device(driver, loop_idx(port));
2005 else
2006 tty_unregister_device(driver, port->index);
Peter Hurley7355ba32012-11-02 08:16:33 -04002007}
2008
2009/**
2010 * fwserial_lookup - finds first fw_serial associated with card
2011 * @card: fw_card to match
2012 *
2013 * NB: caller must be holding fwserial_list_mutex
2014 */
2015static struct fw_serial *fwserial_lookup(struct fw_card *card)
2016{
2017 struct fw_serial *serial;
2018
2019 list_for_each_entry(serial, &fwserial_list, list) {
2020 if (card == serial->card)
2021 return serial;
2022 }
2023
2024 return NULL;
2025}
2026
2027/**
2028 * __fwserial_lookup_rcu - finds first fw_serial associated with card
2029 * @card: fw_card to match
2030 *
2031 * NB: caller must be inside rcu_read_lock() section
2032 */
2033static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
2034{
2035 struct fw_serial *serial;
2036
2037 list_for_each_entry_rcu(serial, &fwserial_list, list) {
2038 if (card == serial->card)
2039 return serial;
2040 }
2041
2042 return NULL;
2043}
2044
2045/**
2046 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
2047 *
2048 * If a matching peer could not be found for the specified generation/node id,
2049 * this could be because:
2050 * a) the generation has changed and one of the nodes hasn't updated yet
2051 * b) the remote node has created its remote unit device before this
2052 * local node has created its corresponding remote unit device
2053 * In either case, the remote node should retry
2054 *
2055 * Note: caller must be in rcu_read_lock() section
2056 */
2057static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
2058 int generation, int id)
2059{
2060 struct fw_serial *serial;
2061 struct fwtty_peer *peer;
2062
2063 serial = __fwserial_lookup_rcu(card);
2064 if (!serial) {
2065 /*
2066 * Something is very wrong - there should be a matching
2067 * fw_serial structure for every fw_card. Maybe the remote node
2068 * has created its remote unit device before this driver has
2069 * been probed for any unit devices...
2070 */
2071 fwtty_err(card, "unknown card (guid %016llx)",
2072 (unsigned long long) card->guid);
2073 return NULL;
2074 }
2075
2076 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2077 int g = peer->generation;
2078 smp_rmb();
2079 if (generation == g && id == peer->node_id)
2080 return peer;
2081 }
2082
2083 return NULL;
2084}
2085
2086#ifdef DEBUG
2087static void __dump_peer_list(struct fw_card *card)
2088{
2089 struct fw_serial *serial;
2090 struct fwtty_peer *peer;
2091
2092 serial = __fwserial_lookup_rcu(card);
2093 if (!serial)
2094 return;
2095
2096 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2097 int g = peer->generation;
2098 smp_rmb();
2099 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n", g,
2100 peer->node_id, (unsigned long long) peer->guid);
2101 }
2102}
2103#else
2104#define __dump_peer_list(s)
2105#endif
2106
2107static void fwserial_auto_connect(struct work_struct *work)
2108{
2109 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
2110 int err;
2111
2112 err = fwserial_connect_peer(peer);
2113 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
2114 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
2115}
2116
2117/**
2118 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
2119 * @serial: aggregate representing the specific fw_card to add the peer to
2120 * @unit: 'peer' to create and add to peer_list of serial
2121 *
2122 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
2123 * peers for a specific fw_card. Optionally, auto-attach this peer to an
2124 * available tty port. This function is called either directly or indirectly
2125 * as a result of a 'serial' unit device being created & probed.
2126 *
2127 * Note: this function is serialized with fwserial_remove_peer() by the
2128 * fwserial_list_mutex held in fwserial_probe().
2129 *
2130 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
2131 * via the dev_set_drvdata() for the device of the fw_unit.
2132 */
2133static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
2134{
2135 struct device *dev = &unit->device;
2136 struct fw_device *parent = fw_parent_device(unit);
2137 struct fwtty_peer *peer;
2138 struct fw_csr_iterator ci;
2139 int key, val;
2140 int generation;
2141
2142 peer = kzalloc(sizeof(*peer), GFP_KERNEL);
2143 if (!peer)
2144 return -ENOMEM;
2145
2146 peer_set_state(peer, FWPS_NOT_ATTACHED);
2147
2148 dev_set_drvdata(dev, peer);
2149 peer->unit = unit;
2150 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
2151 peer->speed = parent->max_speed;
2152 peer->max_payload = min(device_max_receive(parent),
2153 link_speed_to_max_payload(peer->speed));
2154
2155 generation = parent->generation;
2156 smp_rmb();
2157 peer->node_id = parent->node_id;
2158 smp_wmb();
2159 peer->generation = generation;
2160
2161 /* retrieve the mgmt bus addr from the unit directory */
2162 fw_csr_iterator_init(&ci, unit->directory);
2163 while (fw_csr_iterator_next(&ci, &key, &val)) {
2164 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
2165 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
2166 break;
2167 }
2168 }
2169 if (peer->mgmt_addr == 0ULL) {
2170 /*
2171 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2172 * this peer will not be able to attach to a remote
2173 */
2174 peer_set_state(peer, FWPS_NO_MGMT_ADDR);
2175 }
2176
2177 spin_lock_init(&peer->lock);
2178 peer->port = NULL;
2179
2180 init_timer(&peer->timer);
2181 INIT_WORK(&peer->work, NULL);
2182 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
2183
2184 /* associate peer with specific fw_card */
2185 peer->serial = serial;
2186 list_add_rcu(&peer->list, &serial->peer_list);
2187
2188 fwtty_info(&peer->unit, "peer added (guid:%016llx)",
2189 (unsigned long long)peer->guid);
2190
2191 /* identify the local unit & virt cable to loopback port */
2192 if (parent->is_local) {
2193 serial->self = peer;
2194 if (create_loop_dev) {
2195 struct fwtty_port *port;
2196 port = fwserial_claim_port(peer, num_ttys);
2197 if (!IS_ERR(port)) {
2198 struct virt_plug_params params;
2199
2200 spin_lock_bh(&peer->lock);
2201 peer->port = port;
2202 fill_plug_params(&params, port);
2203 fwserial_virt_plug_complete(peer, &params);
2204 spin_unlock_bh(&peer->lock);
2205
2206 fwtty_write_port_status(port);
2207 }
2208 }
2209
2210 } else if (auto_connect) {
2211 /* auto-attach to remote units only (if policy allows) */
2212 schedule_delayed_work(&peer->connect, 1);
2213 }
2214
2215 return 0;
2216}
2217
2218/**
2219 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2220 *
2221 * Remove a 'peer' from its list of peers. This function is only
2222 * called by fwserial_remove() on bus removal of the unit device.
2223 *
2224 * Note: this function is serialized with fwserial_add_peer() by the
2225 * fwserial_list_mutex held in fwserial_remove().
2226 */
2227static void fwserial_remove_peer(struct fwtty_peer *peer)
2228{
2229 struct fwtty_port *port;
2230
2231 spin_lock_bh(&peer->lock);
2232 peer_set_state(peer, FWPS_GONE);
2233 spin_unlock_bh(&peer->lock);
2234
2235 cancel_delayed_work_sync(&peer->connect);
2236 cancel_work_sync(&peer->work);
2237
2238 spin_lock_bh(&peer->lock);
2239 /* if this unit is the local unit, clear link */
2240 if (peer == peer->serial->self)
2241 peer->serial->self = NULL;
2242
2243 /* cancel the request timeout timer (if running) */
2244 del_timer(&peer->timer);
2245
2246 port = peer->port;
2247 peer->port = NULL;
2248
2249 list_del_rcu(&peer->list);
2250
2251 fwtty_info(&peer->unit, "peer removed (guid:%016llx)",
2252 (unsigned long long)peer->guid);
2253
2254 spin_unlock_bh(&peer->lock);
2255
2256 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002257 fwserial_release_port(port, true);
Peter Hurley7355ba32012-11-02 08:16:33 -04002258
2259 synchronize_rcu();
2260 kfree(peer);
2261}
2262
2263/**
Peter Hurley7355ba32012-11-02 08:16:33 -04002264 * fwserial_create - init everything to create TTYs for a specific fw_card
2265 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2266 *
2267 * This function inits the aggregate structure (an fw_serial instance)
2268 * used to manage the TTY ports registered by a specific fw_card. Also, the
2269 * unit device is added as the first 'peer'.
2270 *
2271 * This unit device may represent a local unit device (as specified by the
2272 * config ROM unit directory) or it may represent a remote unit device
2273 * (as specified by the reading of the remote node's config ROM).
2274 *
2275 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2276 * value to indicate which error.
2277 */
2278static int fwserial_create(struct fw_unit *unit)
2279{
2280 struct fw_device *parent = fw_parent_device(unit);
2281 struct fw_card *card = parent->card;
2282 struct fw_serial *serial;
2283 struct fwtty_port *port;
2284 struct device *tty_dev;
2285 int i, j;
2286 int err;
2287
2288 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2289 if (!serial)
2290 return -ENOMEM;
2291
2292 kref_init(&serial->kref);
2293 serial->card = card;
2294 INIT_LIST_HEAD(&serial->peer_list);
2295
2296 for (i = 0; i < num_ports; ++i) {
2297 port = kzalloc(sizeof(*port), GFP_KERNEL);
2298 if (!port) {
2299 err = -ENOMEM;
2300 goto free_ports;
2301 }
2302 tty_port_init(&port->port);
2303 port->index = FWTTY_INVALID_INDEX;
2304 port->port.ops = &fwtty_port_ops;
2305 port->serial = serial;
2306
2307 spin_lock_init(&port->lock);
2308 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
2309 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
2310 INIT_WORK(&port->hangup, fwtty_do_hangup);
2311 INIT_WORK(&port->push, fwtty_pushrx);
2312 INIT_LIST_HEAD(&port->buf_list);
2313 init_waitqueue_head(&port->wait_tx);
2314 port->max_payload = link_speed_to_max_payload(SCODE_100);
2315 dma_fifo_init(&port->tx_fifo);
2316
2317 rcu_assign_pointer(port->peer, NULL);
2318 serial->ports[i] = port;
2319
2320 /* get unique bus addr region for port's status & recv fifo */
2321 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
2322 port->rx_handler.address_callback = fwtty_port_handler;
2323 port->rx_handler.callback_data = port;
2324 /*
2325 * XXX: use custom memory region above cpu physical memory addrs
2326 * this will ease porting to 64-bit firewire adapters
2327 */
2328 err = fw_core_add_address_handler(&port->rx_handler,
2329 &fw_high_memory_region);
2330 if (err) {
2331 kfree(port);
2332 goto free_ports;
2333 }
2334 }
2335 /* preserve i for error cleanup */
2336
2337 err = fwtty_ports_add(serial);
2338 if (err) {
2339 fwtty_err(&unit, "no space in port table");
2340 goto free_ports;
2341 }
2342
2343 for (j = 0; j < num_ttys; ++j) {
2344 tty_dev = tty_port_register_device(&serial->ports[j]->port,
2345 fwtty_driver,
2346 serial->ports[j]->index,
2347 card->device);
2348 if (IS_ERR(tty_dev)) {
2349 err = PTR_ERR(tty_dev);
2350 fwtty_err(&unit, "register tty device error (%d)", err);
2351 goto unregister_ttys;
2352 }
2353
2354 serial->ports[j]->device = tty_dev;
2355 }
2356 /* preserve j for error cleanup */
2357
2358 if (create_loop_dev) {
2359 struct device *loop_dev;
2360
Peter Hurleyfa1da242013-01-28 22:34:38 -05002361 loop_dev = tty_port_register_device(&serial->ports[j]->port,
2362 fwloop_driver,
2363 loop_idx(serial->ports[j]),
2364 card->device);
Peter Hurley7355ba32012-11-02 08:16:33 -04002365 if (IS_ERR(loop_dev)) {
2366 err = PTR_ERR(loop_dev);
2367 fwtty_err(&unit, "create loop device failed (%d)", err);
2368 goto unregister_ttys;
2369 }
Peter Hurleyfa1da242013-01-28 22:34:38 -05002370 serial->ports[j]->device = loop_dev;
2371 serial->ports[j]->loopback = true;
Peter Hurley7355ba32012-11-02 08:16:33 -04002372 }
2373
Peter Hurley4df5bb02013-01-28 22:34:40 -05002374 if (!IS_ERR_OR_NULL(fwserial_debugfs)) {
2375 serial->debugfs = debugfs_create_dir(dev_name(&unit->device),
2376 fwserial_debugfs);
2377 if (!IS_ERR_OR_NULL(serial->debugfs)) {
2378 debugfs_create_file("peers", 0444, serial->debugfs,
2379 serial, &fwtty_peers_fops);
2380 debugfs_create_file("stats", 0444, serial->debugfs,
2381 serial, &fwtty_stats_fops);
2382 }
2383 }
2384
Peter Hurley7355ba32012-11-02 08:16:33 -04002385 list_add_rcu(&serial->list, &fwserial_list);
2386
2387 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)",
2388 dev_name(card->device), (unsigned long long) card->guid);
2389
2390 err = fwserial_add_peer(serial, unit);
2391 if (!err)
2392 return 0;
2393
2394 fwtty_err(&unit, "unable to add peer unit device (%d)", err);
2395
2396 /* fall-through to error processing */
Peter Hurley4df5bb02013-01-28 22:34:40 -05002397 debugfs_remove_recursive(serial->debugfs);
2398
Peter Hurley7355ba32012-11-02 08:16:33 -04002399 list_del_rcu(&serial->list);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002400 if (create_loop_dev)
2401 tty_unregister_device(fwloop_driver, loop_idx(serial->ports[j]));
Peter Hurley7355ba32012-11-02 08:16:33 -04002402unregister_ttys:
2403 for (--j; j >= 0; --j)
2404 tty_unregister_device(fwtty_driver, serial->ports[j]->index);
2405 kref_put(&serial->kref, fwserial_destroy);
2406 return err;
2407
2408free_ports:
Peter Hurleya3218462012-11-27 21:37:11 -05002409 for (--i; i >= 0; --i) {
2410 tty_port_destroy(&serial->ports[i]->port);
Peter Hurley7355ba32012-11-02 08:16:33 -04002411 kfree(serial->ports[i]);
Peter Hurleya3218462012-11-27 21:37:11 -05002412 }
Peter Hurley7355ba32012-11-02 08:16:33 -04002413 kfree(serial);
2414 return err;
2415}
2416
2417/**
2418 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2419 *
2420 * A 'serial' unit device is created and probed as a result of:
2421 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2422 * 'serial' unit specifier id
2423 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2424 *
2425 * The firewire core registers unit devices by enumerating unit directories
2426 * of a node's config ROM after reading the config ROM when a new node is
2427 * added to the bus topology after a bus reset.
2428 *
2429 * The practical implications of this are:
2430 * - this probe is called for both local and remote nodes that have a 'serial'
2431 * unit directory in their config ROM (that matches the specifiers in
2432 * fwserial_id_table).
2433 * - no specific order is enforced for local vs. remote unit devices
2434 *
2435 * This unit driver copes with the lack of specific order in the same way the
2436 * firewire net driver does -- each probe, for either a local or remote unit
2437 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2438 * first peer created for a given fw_card (tracked by the global fwserial_list)
2439 * creates the underlying TTYs (aggregated in a fw_serial instance).
2440 *
2441 * NB: an early attempt to differentiate local & remote unit devices by creating
2442 * peers only for remote units and fw_serial instances (with their
2443 * associated TTY devices) only for local units was discarded. Managing
2444 * the peer lifetimes on device removal proved too complicated.
2445 *
2446 * fwserial_probe/fwserial_remove are effectively serialized by the
2447 * fwserial_list_mutex. This is necessary because the addition of the first peer
2448 * for a given fw_card will trigger the creation of the fw_serial for that
2449 * fw_card, which must not simultaneously contend with the removal of the
2450 * last peer for a given fw_card triggering the destruction of the same
2451 * fw_serial for the same fw_card.
2452 */
2453static int fwserial_probe(struct device *dev)
2454{
2455 struct fw_unit *unit = fw_unit(dev);
2456 struct fw_serial *serial;
2457 int err;
2458
2459 mutex_lock(&fwserial_list_mutex);
2460 serial = fwserial_lookup(fw_parent_device(unit)->card);
2461 if (!serial)
2462 err = fwserial_create(unit);
2463 else
2464 err = fwserial_add_peer(serial, unit);
2465 mutex_unlock(&fwserial_list_mutex);
2466 return err;
2467}
2468
2469/**
2470 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2471 *
2472 * The corresponding 'peer' for this unit device is removed from the list of
2473 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2474 * specific fw_card). If this is the last peer being removed, then trigger
2475 * the destruction of the underlying TTYs.
2476 */
2477static int fwserial_remove(struct device *dev)
2478{
2479 struct fwtty_peer *peer = dev_get_drvdata(dev);
2480 struct fw_serial *serial = peer->serial;
2481 int i;
2482
2483 mutex_lock(&fwserial_list_mutex);
2484 fwserial_remove_peer(peer);
2485
2486 if (list_empty(&serial->peer_list)) {
2487 /* unlink from the fwserial_list here */
2488 list_del_rcu(&serial->list);
2489
Peter Hurley4df5bb02013-01-28 22:34:40 -05002490 debugfs_remove_recursive(serial->debugfs);
2491
Peter Hurleyfa1da242013-01-28 22:34:38 -05002492 for (i = 0; i < num_ttys; ++i)
2493 fwserial_close_port(fwtty_driver, serial->ports[i]);
2494 if (create_loop_dev)
2495 fwserial_close_port(fwloop_driver, serial->ports[i]);
Peter Hurley7355ba32012-11-02 08:16:33 -04002496 kref_put(&serial->kref, fwserial_destroy);
2497 }
2498 mutex_unlock(&fwserial_list_mutex);
2499
2500 return 0;
2501}
2502
2503/**
2504 * fwserial_update: bus update function for 'firewire' serial unit devices
2505 *
2506 * Updates the new node_id and bus generation for this peer. Note that locking
2507 * is unnecessary; but careful memory barrier usage is important to enforce the
2508 * load and store order of generation & node_id.
2509 *
2510 * The fw-core orders the write of node_id before generation in the parent
2511 * fw_device to ensure that a stale node_id cannot be used with a current
2512 * bus generation. So the generation value must be read before the node_id.
2513 *
2514 * In turn, this orders the write of node_id before generation in the peer to
2515 * also ensure a stale node_id cannot be used with a current bus generation.
2516 */
2517static void fwserial_update(struct fw_unit *unit)
2518{
2519 struct fw_device *parent = fw_parent_device(unit);
2520 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2521 int generation;
2522
2523 generation = parent->generation;
2524 smp_rmb();
2525 peer->node_id = parent->node_id;
2526 smp_wmb();
2527 peer->generation = generation;
2528}
2529
2530static const struct ieee1394_device_id fwserial_id_table[] = {
2531 {
2532 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
2533 IEEE1394_MATCH_VERSION,
2534 .specifier_id = LINUX_VENDOR_ID,
2535 .version = FWSERIAL_VERSION,
2536 },
2537 { }
2538};
2539
2540static struct fw_driver fwserial_driver = {
2541 .driver = {
2542 .owner = THIS_MODULE,
2543 .name = KBUILD_MODNAME,
2544 .bus = &fw_bus_type,
2545 .probe = fwserial_probe,
2546 .remove = fwserial_remove,
2547 },
2548 .update = fwserial_update,
2549 .id_table = fwserial_id_table,
2550};
2551
2552#define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2553#define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2554#define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2555 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2556/* XXX: config ROM definitons could be improved with semi-automated offset
2557 * and length calculation
2558 */
2559#define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2560
2561struct fwserial_unit_directory_data {
2562 u16 crc;
2563 u16 len;
2564 u32 unit_specifier;
2565 u32 unit_sw_version;
2566 u32 unit_addr_offset;
2567 u32 desc1_ofs;
2568 u16 desc1_crc;
2569 u16 desc1_len;
2570 u32 desc1_data[5];
2571} __packed;
2572
2573static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
2574 .len = 4,
2575 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
2576 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
2577 .desc1_ofs = FW_ROM_DESCRIPTOR(1),
2578 .desc1_len = 5,
2579 .desc1_data = {
2580 0x00000000, /* type = text */
2581 0x00000000, /* enc = ASCII, lang EN */
2582 0x4c696e75, /* 'Linux TTY' */
2583 0x78205454,
2584 0x59000000,
2585 },
2586};
2587
2588static struct fw_descriptor fwserial_unit_directory = {
2589 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
2590 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
2591 .data = (u32 *)&fwserial_unit_directory_data,
2592};
2593
2594/*
2595 * The management address is in the unit space region but above other known
2596 * address users (to keep wild writes from causing havoc)
2597 */
Peter Hurleya3d9ad42013-01-28 22:34:37 -05002598static const struct fw_address_region fwserial_mgmt_addr_region = {
Peter Hurley7355ba32012-11-02 08:16:33 -04002599 .start = CSR_REGISTER_BASE + 0x1e0000ULL,
2600 .end = 0x1000000000000ULL,
2601};
2602
2603static struct fw_address_handler fwserial_mgmt_addr_handler;
2604
2605/**
2606 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2607 * @work: ptr to peer->work
2608 *
2609 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2610 *
2611 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2612 * already sent to this peer. If so, the collision is resolved by comparing
2613 * guid values; the loser sends the plug response.
2614 *
2615 * Note: if an error prevents a response, don't do anything -- the
2616 * remote will timeout its request.
2617 */
2618static void fwserial_handle_plug_req(struct work_struct *work)
2619{
2620 struct fwtty_peer *peer = to_peer(work, work);
2621 struct virt_plug_params *plug_req = &peer->work_params.plug_req;
2622 struct fwtty_port *port;
2623 struct fwserial_mgmt_pkt *pkt;
2624 int rcode;
2625
2626 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2627 if (!pkt)
2628 return;
2629
2630 port = fwserial_find_port(peer);
2631
2632 spin_lock_bh(&peer->lock);
2633
2634 switch (peer->state) {
2635 case FWPS_NOT_ATTACHED:
2636 if (!port) {
2637 fwtty_err(&peer->unit, "no more ports avail");
2638 fill_plug_rsp_nack(pkt);
2639 } else {
2640 peer->port = port;
2641 fill_plug_rsp_ok(pkt, peer->port);
2642 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2643 /* don't release claimed port */
2644 port = NULL;
2645 }
2646 break;
2647
2648 case FWPS_PLUG_PENDING:
2649 if (peer->serial->card->guid > peer->guid)
2650 goto cleanup;
2651
2652 /* We lost - hijack the already-claimed port and send ok */
2653 del_timer(&peer->timer);
2654 fill_plug_rsp_ok(pkt, peer->port);
2655 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2656 break;
2657
2658 default:
2659 fill_plug_rsp_nack(pkt);
2660 }
2661
2662 spin_unlock_bh(&peer->lock);
2663 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002664 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04002665
2666 rcode = fwserial_send_mgmt_sync(peer, pkt);
2667
2668 spin_lock_bh(&peer->lock);
2669 if (peer->state == FWPS_PLUG_RESPONDING) {
2670 if (rcode == RCODE_COMPLETE) {
2671 struct fwtty_port *tmp = peer->port;
2672
2673 fwserial_virt_plug_complete(peer, plug_req);
2674 spin_unlock_bh(&peer->lock);
2675
2676 fwtty_write_port_status(tmp);
2677 spin_lock_bh(&peer->lock);
2678 } else {
2679 fwtty_err(&peer->unit, "PLUG_RSP error (%d)", rcode);
2680 port = peer_revert_state(peer);
2681 }
2682 }
2683cleanup:
2684 spin_unlock_bh(&peer->lock);
2685 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002686 fwserial_release_port(port, false);
Peter Hurley7355ba32012-11-02 08:16:33 -04002687 kfree(pkt);
2688 return;
2689}
2690
2691static void fwserial_handle_unplug_req(struct work_struct *work)
2692{
2693 struct fwtty_peer *peer = to_peer(work, work);
2694 struct fwtty_port *port = NULL;
2695 struct fwserial_mgmt_pkt *pkt;
2696 int rcode;
2697
2698 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2699 if (!pkt)
2700 return;
2701
2702 spin_lock_bh(&peer->lock);
2703
2704 switch (peer->state) {
2705 case FWPS_ATTACHED:
2706 fill_unplug_rsp_ok(pkt);
2707 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2708 break;
2709
2710 case FWPS_UNPLUG_PENDING:
2711 if (peer->serial->card->guid > peer->guid)
2712 goto cleanup;
2713
2714 /* We lost - send unplug rsp */
2715 del_timer(&peer->timer);
2716 fill_unplug_rsp_ok(pkt);
2717 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2718 break;
2719
2720 default:
2721 fill_unplug_rsp_nack(pkt);
2722 }
2723
2724 spin_unlock_bh(&peer->lock);
2725
2726 rcode = fwserial_send_mgmt_sync(peer, pkt);
2727
2728 spin_lock_bh(&peer->lock);
2729 if (peer->state == FWPS_UNPLUG_RESPONDING) {
Peter Hurleyc88d40b2013-01-28 22:34:36 -05002730 if (rcode != RCODE_COMPLETE)
Peter Hurley7355ba32012-11-02 08:16:33 -04002731 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)", rcode);
Peter Hurleyc88d40b2013-01-28 22:34:36 -05002732 port = peer_revert_state(peer);
Peter Hurley7355ba32012-11-02 08:16:33 -04002733 }
2734cleanup:
2735 spin_unlock_bh(&peer->lock);
2736 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002737 fwserial_release_port(port, true);
Peter Hurley7355ba32012-11-02 08:16:33 -04002738 kfree(pkt);
2739 return;
2740}
2741
2742static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
2743 struct fwserial_mgmt_pkt *pkt,
2744 unsigned long long addr,
2745 size_t len)
2746{
2747 struct fwtty_port *port = NULL;
Peter Hurleyde321a12013-01-28 22:34:35 -05002748 bool reset = false;
Peter Hurley7355ba32012-11-02 08:16:33 -04002749 int rcode;
2750
2751 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
2752 return RCODE_ADDRESS_ERROR;
2753
2754 if (len != be16_to_cpu(pkt->hdr.len) ||
2755 len != mgmt_pkt_expected_len(pkt->hdr.code))
2756 return RCODE_DATA_ERROR;
2757
2758 spin_lock_bh(&peer->lock);
2759 if (peer->state == FWPS_GONE) {
2760 /*
2761 * This should never happen - it would mean that the
2762 * remote unit that just wrote this transaction was
2763 * already removed from the bus -- and the removal was
2764 * processed before we rec'd this transaction
2765 */
2766 fwtty_err(&peer->unit, "peer already removed");
2767 spin_unlock_bh(&peer->lock);
2768 return RCODE_ADDRESS_ERROR;
2769 }
2770
2771 rcode = RCODE_COMPLETE;
2772
2773 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04hx", pkt->hdr.code);
2774
2775 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
2776 case FWSC_VIRT_CABLE_PLUG:
2777 if (work_pending(&peer->work)) {
2778 fwtty_err(&peer->unit, "plug req: busy");
2779 rcode = RCODE_CONFLICT_ERROR;
2780
2781 } else {
2782 peer->work_params.plug_req = pkt->plug_req;
2783 PREPARE_WORK(&peer->work, fwserial_handle_plug_req);
2784 queue_work(system_unbound_wq, &peer->work);
2785 }
2786 break;
2787
2788 case FWSC_VIRT_CABLE_PLUG_RSP:
2789 if (peer->state != FWPS_PLUG_PENDING) {
2790 rcode = RCODE_CONFLICT_ERROR;
2791
2792 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
2793 fwtty_notice(&peer->unit, "NACK plug rsp");
2794 port = peer_revert_state(peer);
2795
2796 } else {
2797 struct fwtty_port *tmp = peer->port;
2798
2799 fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
2800 spin_unlock_bh(&peer->lock);
2801
2802 fwtty_write_port_status(tmp);
2803 spin_lock_bh(&peer->lock);
2804 }
2805 break;
2806
2807 case FWSC_VIRT_CABLE_UNPLUG:
2808 if (work_pending(&peer->work)) {
2809 fwtty_err(&peer->unit, "unplug req: busy");
2810 rcode = RCODE_CONFLICT_ERROR;
2811 } else {
2812 PREPARE_WORK(&peer->work, fwserial_handle_unplug_req);
2813 queue_work(system_unbound_wq, &peer->work);
2814 }
2815 break;
2816
2817 case FWSC_VIRT_CABLE_UNPLUG_RSP:
2818 if (peer->state != FWPS_UNPLUG_PENDING)
2819 rcode = RCODE_CONFLICT_ERROR;
2820 else {
2821 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
2822 fwtty_notice(&peer->unit, "NACK unplug?");
2823 port = peer_revert_state(peer);
Peter Hurleyde321a12013-01-28 22:34:35 -05002824 reset = true;
Peter Hurley7355ba32012-11-02 08:16:33 -04002825 }
2826 break;
2827
2828 default:
2829 fwtty_err(&peer->unit, "unknown mgmt code %d",
2830 be16_to_cpu(pkt->hdr.code));
2831 rcode = RCODE_DATA_ERROR;
2832 }
2833 spin_unlock_bh(&peer->lock);
2834
2835 if (port)
Peter Hurleyde321a12013-01-28 22:34:35 -05002836 fwserial_release_port(port, reset);
Peter Hurley7355ba32012-11-02 08:16:33 -04002837
2838 return rcode;
2839}
2840
2841/**
2842 * fwserial_mgmt_handler: bus address handler for mgmt requests
2843 * @parameters: fw_address_callback_t as specified by firewire core interface
2844 *
2845 * This handler is responsible for handling virtual cable requests from remotes
2846 * for all cards.
2847 */
2848static void fwserial_mgmt_handler(struct fw_card *card,
2849 struct fw_request *request,
2850 int tcode, int destination, int source,
2851 int generation,
2852 unsigned long long addr,
2853 void *data, size_t len,
2854 void *callback_data)
2855{
2856 struct fwserial_mgmt_pkt *pkt = data;
2857 struct fwtty_peer *peer;
2858 int rcode;
2859
2860 rcu_read_lock();
2861 peer = __fwserial_peer_by_node_id(card, generation, source);
2862 if (!peer) {
2863 fwtty_dbg(card, "peer(%d:%x) not found", generation, source);
2864 __dump_peer_list(card);
2865 rcode = RCODE_CONFLICT_ERROR;
2866
2867 } else {
2868 switch (tcode) {
2869 case TCODE_WRITE_BLOCK_REQUEST:
2870 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
2871 break;
2872
2873 default:
2874 rcode = RCODE_TYPE_ERROR;
2875 }
2876 }
2877
2878 rcu_read_unlock();
2879 fw_send_response(card, request, rcode);
2880}
2881
2882static int __init fwserial_init(void)
2883{
2884 int err, num_loops = !!(create_loop_dev);
2885
Peter Hurley4df5bb02013-01-28 22:34:40 -05002886 /* XXX: placeholder for a "firewire" debugfs node */
2887 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
2888
Peter Hurley7355ba32012-11-02 08:16:33 -04002889 /* num_ttys/num_ports must not be set above the static alloc avail */
2890 if (num_ttys + num_loops > MAX_CARD_PORTS)
2891 num_ttys = MAX_CARD_PORTS - num_loops;
2892 num_ports = num_ttys + num_loops;
2893
2894 fwtty_driver = alloc_tty_driver(MAX_TOTAL_PORTS);
2895 if (!fwtty_driver) {
2896 err = -ENOMEM;
2897 return err;
2898 }
2899
2900 fwtty_driver->driver_name = KBUILD_MODNAME;
2901 fwtty_driver->name = tty_dev_name;
2902 fwtty_driver->major = 0;
2903 fwtty_driver->minor_start = 0;
2904 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2905 fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
2906 fwtty_driver->flags = TTY_DRIVER_REAL_RAW |
2907 TTY_DRIVER_DYNAMIC_DEV;
2908
2909 fwtty_driver->init_termios = tty_std_termios;
2910 fwtty_driver->init_termios.c_cflag |= CLOCAL;
2911 tty_set_operations(fwtty_driver, &fwtty_ops);
2912
2913 err = tty_register_driver(fwtty_driver);
2914 if (err) {
2915 driver_err("register tty driver failed (%d)", err);
2916 goto put_tty;
2917 }
2918
Peter Hurleyfa1da242013-01-28 22:34:38 -05002919 if (create_loop_dev) {
2920 fwloop_driver = alloc_tty_driver(MAX_TOTAL_PORTS / num_ports);
2921 if (!fwloop_driver) {
2922 err = -ENOMEM;
2923 goto unregister_driver;
2924 }
2925
2926 fwloop_driver->driver_name = KBUILD_MODNAME "_loop";
2927 fwloop_driver->name = loop_dev_name;
2928 fwloop_driver->major = 0;
2929 fwloop_driver->minor_start = 0;
2930 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL;
2931 fwloop_driver->subtype = SERIAL_TYPE_NORMAL;
2932 fwloop_driver->flags = TTY_DRIVER_REAL_RAW |
2933 TTY_DRIVER_DYNAMIC_DEV;
2934
2935 fwloop_driver->init_termios = tty_std_termios;
2936 fwloop_driver->init_termios.c_cflag |= CLOCAL;
2937 tty_set_operations(fwloop_driver, &fwloop_ops);
2938
2939 err = tty_register_driver(fwloop_driver);
2940 if (err) {
2941 driver_err("register loop driver failed (%d)", err);
2942 goto put_loop;
2943 }
2944 }
2945
Peter Hurley7355ba32012-11-02 08:16:33 -04002946 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
2947 sizeof(struct fwtty_transaction),
2948 0, 0, fwtty_txn_constructor);
2949 if (!fwtty_txn_cache) {
2950 err = -ENOMEM;
Peter Hurleyfa1da242013-01-28 22:34:38 -05002951 goto unregister_loop;
Peter Hurley7355ba32012-11-02 08:16:33 -04002952 }
2953
2954 /*
2955 * Ideally, this address handler would be registered per local node
2956 * (rather than the same handler for all local nodes). However,
2957 * since the firewire core requires the config rom descriptor *before*
2958 * the local unit device(s) are created, a single management handler
2959 * must suffice for all local serial units.
2960 */
2961 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
2962 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
2963
2964 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
2965 &fwserial_mgmt_addr_region);
2966 if (err) {
2967 driver_err("add management handler failed (%d)", err);
2968 goto destroy_cache;
2969 }
2970
2971 fwserial_unit_directory_data.unit_addr_offset =
2972 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
2973 err = fw_core_add_descriptor(&fwserial_unit_directory);
2974 if (err) {
2975 driver_err("add unit descriptor failed (%d)", err);
2976 goto remove_handler;
2977 }
2978
2979 err = driver_register(&fwserial_driver.driver);
2980 if (err) {
2981 driver_err("register fwserial driver failed (%d)", err);
2982 goto remove_descriptor;
2983 }
2984
2985 return 0;
2986
2987remove_descriptor:
2988 fw_core_remove_descriptor(&fwserial_unit_directory);
2989remove_handler:
2990 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2991destroy_cache:
2992 kmem_cache_destroy(fwtty_txn_cache);
Peter Hurleyfa1da242013-01-28 22:34:38 -05002993unregister_loop:
2994 if (create_loop_dev)
2995 tty_unregister_driver(fwloop_driver);
2996put_loop:
2997 if (create_loop_dev)
2998 put_tty_driver(fwloop_driver);
Peter Hurley7355ba32012-11-02 08:16:33 -04002999unregister_driver:
3000 tty_unregister_driver(fwtty_driver);
3001put_tty:
3002 put_tty_driver(fwtty_driver);
Peter Hurley4df5bb02013-01-28 22:34:40 -05003003 debugfs_remove_recursive(fwserial_debugfs);
Peter Hurley7355ba32012-11-02 08:16:33 -04003004 return err;
3005}
3006
3007static void __exit fwserial_exit(void)
3008{
3009 driver_unregister(&fwserial_driver.driver);
3010 fw_core_remove_descriptor(&fwserial_unit_directory);
3011 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
3012 kmem_cache_destroy(fwtty_txn_cache);
Peter Hurleyfa1da242013-01-28 22:34:38 -05003013 if (create_loop_dev) {
3014 tty_unregister_driver(fwloop_driver);
3015 put_tty_driver(fwloop_driver);
3016 }
Peter Hurley7355ba32012-11-02 08:16:33 -04003017 tty_unregister_driver(fwtty_driver);
3018 put_tty_driver(fwtty_driver);
Peter Hurley4df5bb02013-01-28 22:34:40 -05003019 debugfs_remove_recursive(fwserial_debugfs);
Peter Hurley7355ba32012-11-02 08:16:33 -04003020}
3021
3022module_init(fwserial_init);
3023module_exit(fwserial_exit);
3024
3025MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
3026MODULE_DESCRIPTION("FireWire Serial TTY Driver");
3027MODULE_LICENSE("GPL");
3028MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
3029MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
3030MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
3031MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");