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