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