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