Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at 92d5a606721f759ebebf448b3bd2b7a781d50bd0 1911 lines 50 kB view raw
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Char device for device raw access 4 * 5 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net> 6 */ 7 8#include <linux/bug.h> 9#include <linux/compat.h> 10#include <linux/delay.h> 11#include <linux/device.h> 12#include <linux/dma-mapping.h> 13#include <linux/err.h> 14#include <linux/errno.h> 15#include <linux/firewire.h> 16#include <linux/firewire-cdev.h> 17#include <linux/irqflags.h> 18#include <linux/jiffies.h> 19#include <linux/kernel.h> 20#include <linux/kref.h> 21#include <linux/mm.h> 22#include <linux/module.h> 23#include <linux/mutex.h> 24#include <linux/poll.h> 25#include <linux/sched.h> /* required for linux/wait.h */ 26#include <linux/slab.h> 27#include <linux/spinlock.h> 28#include <linux/string.h> 29#include <linux/time.h> 30#include <linux/uaccess.h> 31#include <linux/vmalloc.h> 32#include <linux/wait.h> 33#include <linux/workqueue.h> 34 35 36#include "core.h" 37#include <trace/events/firewire.h> 38 39#include "packet-header-definitions.h" 40 41/* 42 * ABI version history is documented in linux/firewire-cdev.h. 43 */ 44#define FW_CDEV_KERNEL_VERSION 6 45#define FW_CDEV_VERSION_EVENT_REQUEST2 4 46#define FW_CDEV_VERSION_ALLOCATE_REGION_END 4 47#define FW_CDEV_VERSION_AUTO_FLUSH_ISO_OVERFLOW 5 48#define FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP 6 49 50static DEFINE_SPINLOCK(phy_receiver_list_lock); 51static LIST_HEAD(phy_receiver_list); 52 53struct client { 54 u32 version; 55 struct fw_device *device; 56 57 spinlock_t lock; 58 bool in_shutdown; 59 struct xarray resource_xa; 60 struct list_head event_list; 61 wait_queue_head_t wait; 62 wait_queue_head_t tx_flush_wait; 63 u64 bus_reset_closure; 64 65 struct fw_iso_context *iso_context; 66 struct mutex iso_context_mutex; 67 u64 iso_closure; 68 struct fw_iso_buffer buffer; 69 unsigned long vm_start; 70 71 struct list_head phy_receiver_link; 72 u64 phy_receiver_closure; 73 74 struct list_head link; 75 struct kref kref; 76}; 77 78static inline void client_get(struct client *client) 79{ 80 kref_get(&client->kref); 81} 82 83static void client_release(struct kref *kref) 84{ 85 struct client *client = container_of(kref, struct client, kref); 86 87 fw_device_put(client->device); 88 kfree(client); 89} 90 91static void client_put(struct client *client) 92{ 93 kref_put(&client->kref, client_release); 94} 95 96struct client_resource; 97typedef void (*client_resource_release_fn_t)(struct client *, 98 struct client_resource *); 99struct client_resource { 100 client_resource_release_fn_t release; 101 int handle; 102}; 103 104struct address_handler_resource { 105 struct client_resource resource; 106 struct fw_address_handler handler; 107 __u64 closure; 108 struct client *client; 109}; 110 111struct outbound_transaction_resource { 112 struct client_resource resource; 113 struct fw_transaction transaction; 114}; 115 116struct inbound_transaction_resource { 117 struct client_resource resource; 118 struct fw_card *card; 119 struct fw_request *request; 120 bool is_fcp; 121 void *data; 122 size_t length; 123}; 124 125struct descriptor_resource { 126 struct client_resource resource; 127 struct fw_descriptor descriptor; 128 u32 data[]; 129}; 130 131struct iso_resource { 132 struct client_resource resource; 133 struct client *client; 134 /* Schedule work and access todo only with client->lock held. */ 135 struct delayed_work work; 136 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC, 137 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo; 138 int generation; 139 u64 channels; 140 s32 bandwidth; 141 struct iso_resource_event *e_alloc, *e_dealloc; 142}; 143 144static struct address_handler_resource *to_address_handler_resource(struct client_resource *resource) 145{ 146 return container_of(resource, struct address_handler_resource, resource); 147} 148 149static struct inbound_transaction_resource *to_inbound_transaction_resource(struct client_resource *resource) 150{ 151 return container_of(resource, struct inbound_transaction_resource, resource); 152} 153 154static struct descriptor_resource *to_descriptor_resource(struct client_resource *resource) 155{ 156 return container_of(resource, struct descriptor_resource, resource); 157} 158 159static struct iso_resource *to_iso_resource(struct client_resource *resource) 160{ 161 return container_of(resource, struct iso_resource, resource); 162} 163 164static void release_iso_resource(struct client *, struct client_resource *); 165 166static int is_iso_resource(const struct client_resource *resource) 167{ 168 return resource->release == release_iso_resource; 169} 170 171static void release_transaction(struct client *client, 172 struct client_resource *resource); 173 174static int is_outbound_transaction_resource(const struct client_resource *resource) 175{ 176 return resource->release == release_transaction; 177} 178 179static void schedule_iso_resource(struct iso_resource *r, unsigned long delay) 180{ 181 client_get(r->client); 182 if (!queue_delayed_work(fw_workqueue, &r->work, delay)) 183 client_put(r->client); 184} 185 186/* 187 * dequeue_event() just kfree()'s the event, so the event has to be 188 * the first field in a struct XYZ_event. 189 */ 190struct event { 191 struct { void *data; size_t size; } v[2]; 192 struct list_head link; 193}; 194 195struct bus_reset_event { 196 struct event event; 197 struct fw_cdev_event_bus_reset reset; 198}; 199 200struct outbound_transaction_event { 201 struct event event; 202 struct client *client; 203 struct outbound_transaction_resource r; 204 union { 205 struct fw_cdev_event_response without_tstamp; 206 struct fw_cdev_event_response2 with_tstamp; 207 } rsp; 208}; 209 210struct inbound_transaction_event { 211 struct event event; 212 union { 213 struct fw_cdev_event_request request; 214 struct fw_cdev_event_request2 request2; 215 struct fw_cdev_event_request3 with_tstamp; 216 } req; 217}; 218 219struct iso_interrupt_event { 220 struct event event; 221 struct fw_cdev_event_iso_interrupt interrupt; 222}; 223 224struct iso_interrupt_mc_event { 225 struct event event; 226 struct fw_cdev_event_iso_interrupt_mc interrupt; 227}; 228 229struct iso_resource_event { 230 struct event event; 231 struct fw_cdev_event_iso_resource iso_resource; 232}; 233 234struct outbound_phy_packet_event { 235 struct event event; 236 struct client *client; 237 struct fw_packet p; 238 union { 239 struct fw_cdev_event_phy_packet without_tstamp; 240 struct fw_cdev_event_phy_packet2 with_tstamp; 241 } phy_packet; 242}; 243 244struct inbound_phy_packet_event { 245 struct event event; 246 union { 247 struct fw_cdev_event_phy_packet without_tstamp; 248 struct fw_cdev_event_phy_packet2 with_tstamp; 249 } phy_packet; 250}; 251 252#ifdef CONFIG_COMPAT 253static void __user *u64_to_uptr(u64 value) 254{ 255 if (in_compat_syscall()) 256 return compat_ptr(value); 257 else 258 return (void __user *)(unsigned long)value; 259} 260 261static u64 uptr_to_u64(void __user *ptr) 262{ 263 if (in_compat_syscall()) 264 return ptr_to_compat(ptr); 265 else 266 return (u64)(unsigned long)ptr; 267} 268#else 269static inline void __user *u64_to_uptr(u64 value) 270{ 271 return (void __user *)(unsigned long)value; 272} 273 274static inline u64 uptr_to_u64(void __user *ptr) 275{ 276 return (u64)(unsigned long)ptr; 277} 278#endif /* CONFIG_COMPAT */ 279 280static int fw_device_op_open(struct inode *inode, struct file *file) 281{ 282 struct fw_device *device; 283 struct client *client; 284 285 device = fw_device_get_by_devt(inode->i_rdev); 286 if (device == NULL) 287 return -ENODEV; 288 289 if (fw_device_is_shutdown(device)) { 290 fw_device_put(device); 291 return -ENODEV; 292 } 293 294 client = kzalloc_obj(*client); 295 if (client == NULL) { 296 fw_device_put(device); 297 return -ENOMEM; 298 } 299 300 client->device = device; 301 spin_lock_init(&client->lock); 302 xa_init_flags(&client->resource_xa, XA_FLAGS_ALLOC1 | XA_FLAGS_LOCK_BH); 303 INIT_LIST_HEAD(&client->event_list); 304 init_waitqueue_head(&client->wait); 305 init_waitqueue_head(&client->tx_flush_wait); 306 INIT_LIST_HEAD(&client->phy_receiver_link); 307 INIT_LIST_HEAD(&client->link); 308 kref_init(&client->kref); 309 mutex_init(&client->iso_context_mutex); 310 311 file->private_data = client; 312 313 return nonseekable_open(inode, file); 314} 315 316static void queue_event(struct client *client, struct event *event, 317 void *data0, size_t size0, void *data1, size_t size1) 318{ 319 event->v[0].data = data0; 320 event->v[0].size = size0; 321 event->v[1].data = data1; 322 event->v[1].size = size1; 323 324 scoped_guard(spinlock_irqsave, &client->lock) { 325 if (client->in_shutdown) 326 kfree(event); 327 else 328 list_add_tail(&event->link, &client->event_list); 329 } 330 331 wake_up_interruptible(&client->wait); 332} 333 334static int dequeue_event(struct client *client, 335 char __user *buffer, size_t count) 336{ 337 struct event *event; 338 size_t size, total; 339 int i, ret; 340 341 ret = wait_event_interruptible(client->wait, 342 !list_empty(&client->event_list) || 343 fw_device_is_shutdown(client->device)); 344 if (ret < 0) 345 return ret; 346 347 if (list_empty(&client->event_list) && 348 fw_device_is_shutdown(client->device)) 349 return -ENODEV; 350 351 scoped_guard(spinlock_irq, &client->lock) { 352 event = list_first_entry(&client->event_list, struct event, link); 353 list_del(&event->link); 354 } 355 356 total = 0; 357 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) { 358 size = min(event->v[i].size, count - total); 359 if (copy_to_user(buffer + total, event->v[i].data, size)) { 360 ret = -EFAULT; 361 goto out; 362 } 363 total += size; 364 } 365 ret = total; 366 367 out: 368 kfree(event); 369 370 return ret; 371} 372 373static ssize_t fw_device_op_read(struct file *file, char __user *buffer, 374 size_t count, loff_t *offset) 375{ 376 struct client *client = file->private_data; 377 378 return dequeue_event(client, buffer, count); 379} 380 381static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event, 382 struct client *client) 383{ 384 struct fw_card *card = client->device->card; 385 386 guard(spinlock_irq)(&card->lock); 387 388 event->closure = client->bus_reset_closure; 389 event->type = FW_CDEV_EVENT_BUS_RESET; 390 event->generation = client->device->generation; 391 event->node_id = client->device->node_id; 392 event->local_node_id = card->local_node->node_id; 393 event->bm_node_id = card->bm_node_id; 394 event->irm_node_id = card->irm_node->node_id; 395 event->root_node_id = card->root_node->node_id; 396} 397 398static void for_each_client(struct fw_device *device, 399 void (*callback)(struct client *client)) 400{ 401 struct client *c; 402 403 guard(mutex)(&device->client_list_mutex); 404 405 list_for_each_entry(c, &device->client_list, link) 406 callback(c); 407} 408 409static void queue_bus_reset_event(struct client *client) 410{ 411 struct bus_reset_event *e; 412 struct client_resource *resource; 413 unsigned long index; 414 415 e = kzalloc_obj(*e); 416 if (e == NULL) 417 return; 418 419 fill_bus_reset_event(&e->reset, client); 420 421 queue_event(client, &e->event, 422 &e->reset, sizeof(e->reset), NULL, 0); 423 424 guard(spinlock_irq)(&client->lock); 425 426 xa_for_each(&client->resource_xa, index, resource) { 427 if (is_iso_resource(resource)) 428 schedule_iso_resource(to_iso_resource(resource), 0); 429 } 430} 431 432void fw_device_cdev_update(struct fw_device *device) 433{ 434 for_each_client(device, queue_bus_reset_event); 435} 436 437static void wake_up_client(struct client *client) 438{ 439 wake_up_interruptible(&client->wait); 440} 441 442void fw_device_cdev_remove(struct fw_device *device) 443{ 444 for_each_client(device, wake_up_client); 445} 446 447union ioctl_arg { 448 struct fw_cdev_get_info get_info; 449 struct fw_cdev_send_request send_request; 450 struct fw_cdev_allocate allocate; 451 struct fw_cdev_deallocate deallocate; 452 struct fw_cdev_send_response send_response; 453 struct fw_cdev_initiate_bus_reset initiate_bus_reset; 454 struct fw_cdev_add_descriptor add_descriptor; 455 struct fw_cdev_remove_descriptor remove_descriptor; 456 struct fw_cdev_create_iso_context create_iso_context; 457 struct fw_cdev_queue_iso queue_iso; 458 struct fw_cdev_start_iso start_iso; 459 struct fw_cdev_stop_iso stop_iso; 460 struct fw_cdev_get_cycle_timer get_cycle_timer; 461 struct fw_cdev_allocate_iso_resource allocate_iso_resource; 462 struct fw_cdev_send_stream_packet send_stream_packet; 463 struct fw_cdev_get_cycle_timer2 get_cycle_timer2; 464 struct fw_cdev_send_phy_packet send_phy_packet; 465 struct fw_cdev_receive_phy_packets receive_phy_packets; 466 struct fw_cdev_set_iso_channels set_iso_channels; 467 struct fw_cdev_flush_iso flush_iso; 468}; 469 470static int ioctl_get_info(struct client *client, union ioctl_arg *arg) 471{ 472 struct fw_cdev_get_info *a = &arg->get_info; 473 struct fw_cdev_event_bus_reset bus_reset; 474 unsigned long ret = 0; 475 476 client->version = a->version; 477 a->version = FW_CDEV_KERNEL_VERSION; 478 a->card = client->device->card->index; 479 480 scoped_guard(rwsem_read, &fw_device_rwsem) { 481 if (a->rom != 0) { 482 size_t want = a->rom_length; 483 size_t have = client->device->config_rom_length * 4; 484 485 ret = copy_to_user(u64_to_uptr(a->rom), client->device->config_rom, 486 min(want, have)); 487 if (ret != 0) 488 return -EFAULT; 489 } 490 a->rom_length = client->device->config_rom_length * 4; 491 } 492 493 guard(mutex)(&client->device->client_list_mutex); 494 495 client->bus_reset_closure = a->bus_reset_closure; 496 if (a->bus_reset != 0) { 497 fill_bus_reset_event(&bus_reset, client); 498 /* unaligned size of bus_reset is 36 bytes */ 499 ret = copy_to_user(u64_to_uptr(a->bus_reset), &bus_reset, 36); 500 } 501 if (ret == 0 && list_empty(&client->link)) 502 list_add_tail(&client->link, &client->device->client_list); 503 504 return ret ? -EFAULT : 0; 505} 506 507static int add_client_resource(struct client *client, struct client_resource *resource, 508 gfp_t gfp_mask) 509{ 510 int ret; 511 512 scoped_guard(spinlock_irqsave, &client->lock) { 513 u32 index; 514 515 if (client->in_shutdown) { 516 ret = -ECANCELED; 517 } else { 518 if (gfpflags_allow_blocking(gfp_mask)) { 519 ret = xa_alloc(&client->resource_xa, &index, resource, xa_limit_32b, 520 GFP_NOWAIT); 521 } else { 522 ret = xa_alloc_bh(&client->resource_xa, &index, resource, 523 xa_limit_32b, GFP_NOWAIT); 524 } 525 } 526 if (ret >= 0) { 527 resource->handle = index; 528 client_get(client); 529 if (is_iso_resource(resource)) 530 schedule_iso_resource(to_iso_resource(resource), 0); 531 } 532 } 533 534 return ret < 0 ? ret : 0; 535} 536 537static int release_client_resource(struct client *client, u32 handle, 538 client_resource_release_fn_t release, 539 struct client_resource **return_resource) 540{ 541 unsigned long index = handle; 542 struct client_resource *resource; 543 544 scoped_guard(spinlock_irq, &client->lock) { 545 if (client->in_shutdown) 546 return -EINVAL; 547 548 resource = xa_load(&client->resource_xa, index); 549 if (!resource || resource->release != release) 550 return -EINVAL; 551 552 xa_erase(&client->resource_xa, handle); 553 } 554 555 if (return_resource) 556 *return_resource = resource; 557 else 558 resource->release(client, resource); 559 560 client_put(client); 561 562 return 0; 563} 564 565static void release_transaction(struct client *client, 566 struct client_resource *resource) 567{ 568} 569 570static void complete_transaction(struct fw_card *card, int rcode, u32 request_tstamp, 571 u32 response_tstamp, void *payload, size_t length, void *data) 572{ 573 struct outbound_transaction_event *e = data; 574 struct client *client = e->client; 575 unsigned long index = e->r.resource.handle; 576 577 scoped_guard(spinlock_irqsave, &client->lock) { 578 xa_erase(&client->resource_xa, index); 579 if (client->in_shutdown) 580 wake_up(&client->tx_flush_wait); 581 } 582 583 switch (e->rsp.without_tstamp.type) { 584 case FW_CDEV_EVENT_RESPONSE: 585 { 586 struct fw_cdev_event_response *rsp = &e->rsp.without_tstamp; 587 588 if (length < rsp->length) 589 rsp->length = length; 590 if (rcode == RCODE_COMPLETE) 591 memcpy(rsp->data, payload, rsp->length); 592 593 rsp->rcode = rcode; 594 595 // In the case that sizeof(*rsp) doesn't align with the position of the 596 // data, and the read is short, preserve an extra copy of the data 597 // to stay compatible with a pre-2.6.27 bug. Since the bug is harmless 598 // for short reads and some apps depended on it, this is both safe 599 // and prudent for compatibility. 600 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data)) 601 queue_event(client, &e->event, rsp, sizeof(*rsp), rsp->data, rsp->length); 602 else 603 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length, NULL, 0); 604 605 break; 606 } 607 case FW_CDEV_EVENT_RESPONSE2: 608 { 609 struct fw_cdev_event_response2 *rsp = &e->rsp.with_tstamp; 610 611 if (length < rsp->length) 612 rsp->length = length; 613 if (rcode == RCODE_COMPLETE) 614 memcpy(rsp->data, payload, rsp->length); 615 616 rsp->rcode = rcode; 617 rsp->request_tstamp = request_tstamp; 618 rsp->response_tstamp = response_tstamp; 619 620 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length, NULL, 0); 621 622 break; 623 } 624 default: 625 WARN_ON(1); 626 break; 627 } 628 629 // Drop the xarray's reference. 630 client_put(client); 631} 632 633static int init_request(struct client *client, 634 struct fw_cdev_send_request *request, 635 int destination_id, int speed) 636{ 637 struct outbound_transaction_event *e; 638 void *payload; 639 int ret; 640 641 if (request->tcode != TCODE_STREAM_DATA && 642 (request->length > 4096 || request->length > 512 << speed)) 643 return -EIO; 644 645 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST && 646 request->length < 4) 647 return -EINVAL; 648 649 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL); 650 if (e == NULL) 651 return -ENOMEM; 652 e->client = client; 653 654 if (client->version < FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP) { 655 struct fw_cdev_event_response *rsp = &e->rsp.without_tstamp; 656 657 rsp->type = FW_CDEV_EVENT_RESPONSE; 658 rsp->length = request->length; 659 rsp->closure = request->closure; 660 payload = rsp->data; 661 } else { 662 struct fw_cdev_event_response2 *rsp = &e->rsp.with_tstamp; 663 664 rsp->type = FW_CDEV_EVENT_RESPONSE2; 665 rsp->length = request->length; 666 rsp->closure = request->closure; 667 payload = rsp->data; 668 } 669 670 if (request->data && copy_from_user(payload, u64_to_uptr(request->data), request->length)) { 671 ret = -EFAULT; 672 goto failed; 673 } 674 675 e->r.resource.release = release_transaction; 676 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL); 677 if (ret < 0) 678 goto failed; 679 680 fw_send_request_with_tstamp(client->device->card, &e->r.transaction, request->tcode, 681 destination_id, request->generation, speed, request->offset, 682 payload, request->length, complete_transaction, e); 683 return 0; 684 685 failed: 686 kfree(e); 687 688 return ret; 689} 690 691static int ioctl_send_request(struct client *client, union ioctl_arg *arg) 692{ 693 switch (arg->send_request.tcode) { 694 case TCODE_WRITE_QUADLET_REQUEST: 695 case TCODE_WRITE_BLOCK_REQUEST: 696 case TCODE_READ_QUADLET_REQUEST: 697 case TCODE_READ_BLOCK_REQUEST: 698 case TCODE_LOCK_MASK_SWAP: 699 case TCODE_LOCK_COMPARE_SWAP: 700 case TCODE_LOCK_FETCH_ADD: 701 case TCODE_LOCK_LITTLE_ADD: 702 case TCODE_LOCK_BOUNDED_ADD: 703 case TCODE_LOCK_WRAP_ADD: 704 case TCODE_LOCK_VENDOR_DEPENDENT: 705 break; 706 default: 707 return -EINVAL; 708 } 709 710 return init_request(client, &arg->send_request, client->device->node_id, 711 client->device->max_speed); 712} 713 714static void release_request(struct client *client, 715 struct client_resource *resource) 716{ 717 struct inbound_transaction_resource *r = to_inbound_transaction_resource(resource); 718 719 if (r->is_fcp) 720 fw_request_put(r->request); 721 else 722 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR); 723 724 fw_card_put(r->card); 725 kfree(r); 726} 727 728static void handle_request(struct fw_card *card, struct fw_request *request, 729 int tcode, int destination, int source, 730 int generation, unsigned long long offset, 731 void *payload, size_t length, void *callback_data) 732{ 733 struct address_handler_resource *handler = callback_data; 734 bool is_fcp = is_in_fcp_region(offset, length); 735 struct inbound_transaction_resource *r; 736 struct inbound_transaction_event *e; 737 size_t event_size0; 738 int ret; 739 740 /* card may be different from handler->client->device->card */ 741 fw_card_get(card); 742 743 // Extend the lifetime of data for request so that its payload is safely accessible in 744 // the process context for the client. 745 if (is_fcp) 746 fw_request_get(request); 747 748 r = kmalloc_obj(*r, GFP_ATOMIC); 749 e = kmalloc_obj(*e, GFP_ATOMIC); 750 if (r == NULL || e == NULL) 751 goto failed; 752 753 r->card = card; 754 r->request = request; 755 r->is_fcp = is_fcp; 756 r->data = payload; 757 r->length = length; 758 759 r->resource.release = release_request; 760 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC); 761 if (ret < 0) 762 goto failed; 763 764 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) { 765 struct fw_cdev_event_request *req = &e->req.request; 766 767 if (tcode & 0x10) 768 tcode = TCODE_LOCK_REQUEST; 769 770 req->type = FW_CDEV_EVENT_REQUEST; 771 req->tcode = tcode; 772 req->offset = offset; 773 req->length = length; 774 req->handle = r->resource.handle; 775 req->closure = handler->closure; 776 event_size0 = sizeof(*req); 777 } else if (handler->client->version < FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP) { 778 struct fw_cdev_event_request2 *req = &e->req.request2; 779 780 req->type = FW_CDEV_EVENT_REQUEST2; 781 req->tcode = tcode; 782 req->offset = offset; 783 req->source_node_id = source; 784 req->destination_node_id = destination; 785 req->card = card->index; 786 req->generation = generation; 787 req->length = length; 788 req->handle = r->resource.handle; 789 req->closure = handler->closure; 790 event_size0 = sizeof(*req); 791 } else { 792 struct fw_cdev_event_request3 *req = &e->req.with_tstamp; 793 794 req->type = FW_CDEV_EVENT_REQUEST3; 795 req->tcode = tcode; 796 req->offset = offset; 797 req->source_node_id = source; 798 req->destination_node_id = destination; 799 req->card = card->index; 800 req->generation = generation; 801 req->length = length; 802 req->handle = r->resource.handle; 803 req->closure = handler->closure; 804 req->tstamp = fw_request_get_timestamp(request); 805 event_size0 = sizeof(*req); 806 } 807 808 queue_event(handler->client, &e->event, 809 &e->req, event_size0, r->data, length); 810 return; 811 812 failed: 813 kfree(r); 814 kfree(e); 815 816 if (!is_fcp) 817 fw_send_response(card, request, RCODE_CONFLICT_ERROR); 818 else 819 fw_request_put(request); 820 821 fw_card_put(card); 822} 823 824static void release_address_handler(struct client *client, 825 struct client_resource *resource) 826{ 827 struct address_handler_resource *r = to_address_handler_resource(resource); 828 829 fw_core_remove_address_handler(&r->handler); 830 kfree(r); 831} 832 833static int ioctl_allocate(struct client *client, union ioctl_arg *arg) 834{ 835 struct fw_cdev_allocate *a = &arg->allocate; 836 struct address_handler_resource *r; 837 struct fw_address_region region; 838 int ret; 839 840 r = kmalloc_obj(*r); 841 if (r == NULL) 842 return -ENOMEM; 843 844 region.start = a->offset; 845 if (client->version < FW_CDEV_VERSION_ALLOCATE_REGION_END) 846 region.end = a->offset + a->length; 847 else 848 region.end = a->region_end; 849 850 r->handler.length = a->length; 851 r->handler.address_callback = handle_request; 852 r->handler.callback_data = r; 853 r->closure = a->closure; 854 r->client = client; 855 856 ret = fw_core_add_address_handler(&r->handler, &region); 857 if (ret < 0) { 858 kfree(r); 859 return ret; 860 } 861 a->offset = r->handler.offset; 862 863 r->resource.release = release_address_handler; 864 ret = add_client_resource(client, &r->resource, GFP_KERNEL); 865 if (ret < 0) { 866 release_address_handler(client, &r->resource); 867 return ret; 868 } 869 a->handle = r->resource.handle; 870 871 return 0; 872} 873 874static int ioctl_deallocate(struct client *client, union ioctl_arg *arg) 875{ 876 return release_client_resource(client, arg->deallocate.handle, 877 release_address_handler, NULL); 878} 879 880static int ioctl_send_response(struct client *client, union ioctl_arg *arg) 881{ 882 struct fw_cdev_send_response *a = &arg->send_response; 883 struct client_resource *resource; 884 struct inbound_transaction_resource *r; 885 int ret = 0; 886 887 if (release_client_resource(client, a->handle, 888 release_request, &resource) < 0) 889 return -EINVAL; 890 891 r = to_inbound_transaction_resource(resource); 892 if (r->is_fcp) { 893 fw_request_put(r->request); 894 goto out; 895 } 896 897 if (a->length != fw_get_response_length(r->request)) { 898 ret = -EINVAL; 899 fw_request_put(r->request); 900 goto out; 901 } 902 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) { 903 ret = -EFAULT; 904 fw_request_put(r->request); 905 goto out; 906 } 907 fw_send_response(r->card, r->request, a->rcode); 908 out: 909 fw_card_put(r->card); 910 kfree(r); 911 912 return ret; 913} 914 915static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg) 916{ 917 fw_schedule_bus_reset(client->device->card, true, 918 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET); 919 return 0; 920} 921 922static void release_descriptor(struct client *client, 923 struct client_resource *resource) 924{ 925 struct descriptor_resource *r = to_descriptor_resource(resource); 926 927 fw_core_remove_descriptor(&r->descriptor); 928 kfree(r); 929} 930 931static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg) 932{ 933 struct fw_cdev_add_descriptor *a = &arg->add_descriptor; 934 struct descriptor_resource *r; 935 int ret; 936 937 /* Access policy: Allow this ioctl only on local nodes' device files. */ 938 if (!client->device->is_local) 939 return -ENOSYS; 940 941 if (a->length > 256) 942 return -EINVAL; 943 944 r = kmalloc_flex(*r, data, a->length); 945 if (r == NULL) 946 return -ENOMEM; 947 948 if (copy_from_user(r->data, u64_to_uptr(a->data), 949 flex_array_size(r, data, a->length))) { 950 ret = -EFAULT; 951 goto failed; 952 } 953 954 r->descriptor.length = a->length; 955 r->descriptor.immediate = a->immediate; 956 r->descriptor.key = a->key; 957 r->descriptor.data = r->data; 958 959 ret = fw_core_add_descriptor(&r->descriptor); 960 if (ret < 0) 961 goto failed; 962 963 r->resource.release = release_descriptor; 964 ret = add_client_resource(client, &r->resource, GFP_KERNEL); 965 if (ret < 0) { 966 fw_core_remove_descriptor(&r->descriptor); 967 goto failed; 968 } 969 a->handle = r->resource.handle; 970 971 return 0; 972 failed: 973 kfree(r); 974 975 return ret; 976} 977 978static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg) 979{ 980 return release_client_resource(client, arg->remove_descriptor.handle, 981 release_descriptor, NULL); 982} 983 984static void iso_callback(struct fw_iso_context *context, u32 cycle, 985 size_t header_length, void *header, void *data) 986{ 987 struct client *client = data; 988 struct iso_interrupt_event *e; 989 990 e = kmalloc(sizeof(*e) + header_length, GFP_KERNEL); 991 if (e == NULL) 992 return; 993 994 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT; 995 e->interrupt.closure = client->iso_closure; 996 e->interrupt.cycle = cycle; 997 e->interrupt.header_length = header_length; 998 memcpy(e->interrupt.header, header, header_length); 999 queue_event(client, &e->event, &e->interrupt, 1000 sizeof(e->interrupt) + header_length, NULL, 0); 1001} 1002 1003static void iso_mc_callback(struct fw_iso_context *context, 1004 dma_addr_t completed, void *data) 1005{ 1006 struct client *client = data; 1007 struct iso_interrupt_mc_event *e; 1008 1009 e = kmalloc_obj(*e); 1010 if (e == NULL) 1011 return; 1012 1013 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL; 1014 e->interrupt.closure = client->iso_closure; 1015 e->interrupt.completed = fw_iso_buffer_lookup(&client->buffer, 1016 completed); 1017 queue_event(client, &e->event, &e->interrupt, 1018 sizeof(e->interrupt), NULL, 0); 1019} 1020 1021static enum dma_data_direction iso_dma_direction(struct fw_iso_context *context) 1022{ 1023 if (context->type == FW_ISO_CONTEXT_TRANSMIT) 1024 return DMA_TO_DEVICE; 1025 else 1026 return DMA_FROM_DEVICE; 1027} 1028 1029static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg) 1030{ 1031 struct fw_cdev_create_iso_context *a = &arg->create_iso_context; 1032 struct fw_iso_context *context; 1033 int ret; 1034 1035 BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT || 1036 FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE || 1037 FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL != 1038 FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL); 1039 1040 switch (a->type) { 1041 case FW_ISO_CONTEXT_TRANSMIT: 1042 if (a->speed > SCODE_3200 || a->channel > 63) 1043 return -EINVAL; 1044 break; 1045 1046 case FW_ISO_CONTEXT_RECEIVE: 1047 if (a->header_size < 4 || (a->header_size & 3) || 1048 a->channel > 63) 1049 return -EINVAL; 1050 break; 1051 1052 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 1053 break; 1054 1055 default: 1056 return -EINVAL; 1057 } 1058 1059 if (a->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL) 1060 context = fw_iso_mc_context_create(client->device->card, iso_mc_callback, client); 1061 else 1062 context = fw_iso_context_create(client->device->card, a->type, a->channel, a->speed, 1063 a->header_size, iso_callback, client); 1064 if (IS_ERR(context)) 1065 return PTR_ERR(context); 1066 if (client->version < FW_CDEV_VERSION_AUTO_FLUSH_ISO_OVERFLOW) 1067 context->flags |= FW_ISO_CONTEXT_FLAG_DROP_OVERFLOW_HEADERS; 1068 1069 // We only support one context at this time. 1070 scoped_guard(mutex, &client->iso_context_mutex) { 1071 if (client->iso_context != NULL) { 1072 fw_iso_context_destroy(context); 1073 1074 return -EBUSY; 1075 } 1076 // The DMA mapping operation is available if the buffer is already allocated by 1077 // mmap(2) system call. If not, it is delegated to the system call. 1078 if (client->buffer.pages && !client->buffer.dma_addrs) { 1079 ret = fw_iso_buffer_map_dma(&client->buffer, client->device->card, 1080 iso_dma_direction(context)); 1081 if (ret < 0) { 1082 fw_iso_context_destroy(context); 1083 1084 return ret; 1085 } 1086 } 1087 client->iso_closure = a->closure; 1088 client->iso_context = context; 1089 } 1090 1091 a->handle = 0; 1092 1093 return 0; 1094} 1095 1096static int ioctl_set_iso_channels(struct client *client, union ioctl_arg *arg) 1097{ 1098 struct fw_cdev_set_iso_channels *a = &arg->set_iso_channels; 1099 struct fw_iso_context *ctx = client->iso_context; 1100 1101 if (ctx == NULL || a->handle != 0) 1102 return -EINVAL; 1103 1104 return fw_iso_context_set_channels(ctx, &a->channels); 1105} 1106 1107/* Macros for decoding the iso packet control header. */ 1108#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff) 1109#define GET_INTERRUPT(v) (((v) >> 16) & 0x01) 1110#define GET_SKIP(v) (((v) >> 17) & 0x01) 1111#define GET_TAG(v) (((v) >> 18) & 0x03) 1112#define GET_SY(v) (((v) >> 20) & 0x0f) 1113#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff) 1114 1115static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg) 1116{ 1117 struct fw_cdev_queue_iso *a = &arg->queue_iso; 1118 struct fw_cdev_iso_packet __user *p, *end, *next; 1119 struct fw_iso_context *ctx = client->iso_context; 1120 unsigned long payload, buffer_end, transmit_header_bytes = 0; 1121 u32 control; 1122 int count; 1123 DEFINE_RAW_FLEX(struct fw_iso_packet, u, header, 64); 1124 1125 if (ctx == NULL || a->handle != 0) 1126 return -EINVAL; 1127 1128 /* 1129 * If the user passes a non-NULL data pointer, has mmap()'ed 1130 * the iso buffer, and the pointer points inside the buffer, 1131 * we setup the payload pointers accordingly. Otherwise we 1132 * set them both to 0, which will still let packets with 1133 * payload_length == 0 through. In other words, if no packets 1134 * use the indirect payload, the iso buffer need not be mapped 1135 * and the a->data pointer is ignored. 1136 */ 1137 payload = (unsigned long)a->data - client->vm_start; 1138 buffer_end = client->buffer.page_count << PAGE_SHIFT; 1139 if (a->data == 0 || client->buffer.pages == NULL || 1140 payload >= buffer_end) { 1141 payload = 0; 1142 buffer_end = 0; 1143 } 1144 1145 if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3) 1146 return -EINVAL; 1147 1148 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets); 1149 1150 end = (void __user *)p + a->size; 1151 count = 0; 1152 while (p < end) { 1153 if (get_user(control, &p->control)) 1154 return -EFAULT; 1155 u->payload_length = GET_PAYLOAD_LENGTH(control); 1156 u->interrupt = GET_INTERRUPT(control); 1157 u->skip = GET_SKIP(control); 1158 u->tag = GET_TAG(control); 1159 u->sy = GET_SY(control); 1160 u->header_length = GET_HEADER_LENGTH(control); 1161 1162 switch (ctx->type) { 1163 case FW_ISO_CONTEXT_TRANSMIT: 1164 if (u->header_length & 3) 1165 return -EINVAL; 1166 transmit_header_bytes = u->header_length; 1167 break; 1168 1169 case FW_ISO_CONTEXT_RECEIVE: 1170 if (u->header_length == 0 || 1171 u->header_length % ctx->header_size != 0) 1172 return -EINVAL; 1173 break; 1174 1175 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 1176 if (u->payload_length == 0 || 1177 u->payload_length & 3) 1178 return -EINVAL; 1179 break; 1180 } 1181 1182 next = (struct fw_cdev_iso_packet __user *) 1183 &p->header[transmit_header_bytes / 4]; 1184 if (next > end) 1185 return -EINVAL; 1186 if (copy_from_user 1187 (u->header, p->header, transmit_header_bytes)) 1188 return -EFAULT; 1189 if (u->skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT && 1190 u->header_length + u->payload_length > 0) 1191 return -EINVAL; 1192 if (payload + u->payload_length > buffer_end) 1193 return -EINVAL; 1194 1195 if (fw_iso_context_queue(ctx, u, &client->buffer, payload)) 1196 break; 1197 1198 p = next; 1199 payload += u->payload_length; 1200 count++; 1201 } 1202 fw_iso_context_queue_flush(ctx); 1203 1204 a->size -= uptr_to_u64(p) - a->packets; 1205 a->packets = uptr_to_u64(p); 1206 a->data = client->vm_start + payload; 1207 1208 return count; 1209} 1210 1211static int ioctl_start_iso(struct client *client, union ioctl_arg *arg) 1212{ 1213 struct fw_cdev_start_iso *a = &arg->start_iso; 1214 1215 BUILD_BUG_ON( 1216 FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 || 1217 FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 || 1218 FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 || 1219 FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 || 1220 FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS); 1221 1222 if (client->iso_context == NULL || a->handle != 0) 1223 return -EINVAL; 1224 1225 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE && 1226 (a->tags == 0 || a->tags > 15 || a->sync > 15)) 1227 return -EINVAL; 1228 1229 return fw_iso_context_start(client->iso_context, 1230 a->cycle, a->sync, a->tags); 1231} 1232 1233static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg) 1234{ 1235 struct fw_cdev_stop_iso *a = &arg->stop_iso; 1236 1237 if (client->iso_context == NULL || a->handle != 0) 1238 return -EINVAL; 1239 1240 return fw_iso_context_stop(client->iso_context); 1241} 1242 1243static int ioctl_flush_iso(struct client *client, union ioctl_arg *arg) 1244{ 1245 struct fw_cdev_flush_iso *a = &arg->flush_iso; 1246 1247 if (client->iso_context == NULL || a->handle != 0) 1248 return -EINVAL; 1249 1250 return fw_iso_context_flush_completions(client->iso_context); 1251} 1252 1253static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg) 1254{ 1255 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2; 1256 struct fw_card *card = client->device->card; 1257 struct timespec64 ts = {0, 0}; 1258 u32 cycle_time = 0; 1259 int ret; 1260 1261 guard(irq)(); 1262 1263 ret = fw_card_read_cycle_time(card, &cycle_time); 1264 if (ret < 0) 1265 return ret; 1266 1267 switch (a->clk_id) { 1268 case CLOCK_REALTIME: ktime_get_real_ts64(&ts); break; 1269 case CLOCK_MONOTONIC: ktime_get_ts64(&ts); break; 1270 case CLOCK_MONOTONIC_RAW: ktime_get_raw_ts64(&ts); break; 1271 default: 1272 return -EINVAL; 1273 } 1274 1275 a->tv_sec = ts.tv_sec; 1276 a->tv_nsec = ts.tv_nsec; 1277 a->cycle_timer = cycle_time; 1278 1279 return 0; 1280} 1281 1282static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg) 1283{ 1284 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer; 1285 struct fw_cdev_get_cycle_timer2 ct2; 1286 1287 ct2.clk_id = CLOCK_REALTIME; 1288 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2); 1289 1290 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC; 1291 a->cycle_timer = ct2.cycle_timer; 1292 1293 return 0; 1294} 1295 1296static void iso_resource_work(struct work_struct *work) 1297{ 1298 struct iso_resource_event *e; 1299 struct iso_resource *r = from_work(r, work, work.work); 1300 struct client *client = r->client; 1301 unsigned long index = r->resource.handle; 1302 int generation, channel, bandwidth, todo; 1303 bool skip, free, success; 1304 1305 scoped_guard(spinlock_irq, &client->lock) { 1306 generation = client->device->generation; 1307 todo = r->todo; 1308 // Allow 1000ms grace period for other reallocations. 1309 if (todo == ISO_RES_ALLOC && 1310 time_is_after_jiffies64(client->device->card->reset_jiffies + secs_to_jiffies(1))) { 1311 schedule_iso_resource(r, msecs_to_jiffies(333)); 1312 skip = true; 1313 } else { 1314 // We could be called twice within the same generation. 1315 skip = todo == ISO_RES_REALLOC && 1316 r->generation == generation; 1317 } 1318 free = todo == ISO_RES_DEALLOC || 1319 todo == ISO_RES_ALLOC_ONCE || 1320 todo == ISO_RES_DEALLOC_ONCE; 1321 r->generation = generation; 1322 } 1323 1324 if (skip) 1325 goto out; 1326 1327 bandwidth = r->bandwidth; 1328 1329 fw_iso_resource_manage(client->device->card, generation, 1330 r->channels, &channel, &bandwidth, 1331 todo == ISO_RES_ALLOC || 1332 todo == ISO_RES_REALLOC || 1333 todo == ISO_RES_ALLOC_ONCE); 1334 /* 1335 * Is this generation outdated already? As long as this resource sticks 1336 * in the xarray, it will be scheduled again for a newer generation or at 1337 * shutdown. 1338 */ 1339 if (channel == -EAGAIN && 1340 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC)) 1341 goto out; 1342 1343 success = channel >= 0 || bandwidth > 0; 1344 1345 scoped_guard(spinlock_irq, &client->lock) { 1346 // Transit from allocation to reallocation, except if the client 1347 // requested deallocation in the meantime. 1348 if (r->todo == ISO_RES_ALLOC) 1349 r->todo = ISO_RES_REALLOC; 1350 // Allocation or reallocation failure? Pull this resource out of the 1351 // xarray and prepare for deletion, unless the client is shutting down. 1352 if (r->todo == ISO_RES_REALLOC && !success && 1353 !client->in_shutdown && 1354 xa_erase(&client->resource_xa, index)) { 1355 client_put(client); 1356 free = true; 1357 } 1358 } 1359 1360 if (todo == ISO_RES_ALLOC && channel >= 0) 1361 r->channels = 1ULL << channel; 1362 1363 if (todo == ISO_RES_REALLOC && success) 1364 goto out; 1365 1366 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) { 1367 e = r->e_alloc; 1368 r->e_alloc = NULL; 1369 } else { 1370 e = r->e_dealloc; 1371 r->e_dealloc = NULL; 1372 } 1373 e->iso_resource.handle = r->resource.handle; 1374 e->iso_resource.channel = channel; 1375 e->iso_resource.bandwidth = bandwidth; 1376 1377 queue_event(client, &e->event, 1378 &e->iso_resource, sizeof(e->iso_resource), NULL, 0); 1379 1380 if (free) { 1381 cancel_delayed_work(&r->work); 1382 kfree(r->e_alloc); 1383 kfree(r->e_dealloc); 1384 kfree(r); 1385 } 1386 out: 1387 client_put(client); 1388} 1389 1390static void release_iso_resource(struct client *client, 1391 struct client_resource *resource) 1392{ 1393 struct iso_resource *r = to_iso_resource(resource); 1394 1395 guard(spinlock_irq)(&client->lock); 1396 1397 r->todo = ISO_RES_DEALLOC; 1398 schedule_iso_resource(r, 0); 1399} 1400 1401static int init_iso_resource(struct client *client, 1402 struct fw_cdev_allocate_iso_resource *request, int todo) 1403{ 1404 struct iso_resource_event *e1, *e2; 1405 struct iso_resource *r; 1406 int ret; 1407 1408 if ((request->channels == 0 && request->bandwidth == 0) || 1409 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL) 1410 return -EINVAL; 1411 1412 r = kmalloc_obj(*r); 1413 e1 = kmalloc_obj(*e1); 1414 e2 = kmalloc_obj(*e2); 1415 if (r == NULL || e1 == NULL || e2 == NULL) { 1416 ret = -ENOMEM; 1417 goto fail; 1418 } 1419 1420 INIT_DELAYED_WORK(&r->work, iso_resource_work); 1421 r->client = client; 1422 r->todo = todo; 1423 r->generation = -1; 1424 r->channels = request->channels; 1425 r->bandwidth = request->bandwidth; 1426 r->e_alloc = e1; 1427 r->e_dealloc = e2; 1428 1429 e1->iso_resource.closure = request->closure; 1430 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED; 1431 e2->iso_resource.closure = request->closure; 1432 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED; 1433 1434 if (todo == ISO_RES_ALLOC) { 1435 r->resource.release = release_iso_resource; 1436 ret = add_client_resource(client, &r->resource, GFP_KERNEL); 1437 if (ret < 0) 1438 goto fail; 1439 } else { 1440 r->resource.release = NULL; 1441 r->resource.handle = -1; 1442 schedule_iso_resource(r, 0); 1443 } 1444 request->handle = r->resource.handle; 1445 1446 return 0; 1447 fail: 1448 kfree(r); 1449 kfree(e1); 1450 kfree(e2); 1451 1452 return ret; 1453} 1454 1455static int ioctl_allocate_iso_resource(struct client *client, 1456 union ioctl_arg *arg) 1457{ 1458 return init_iso_resource(client, 1459 &arg->allocate_iso_resource, ISO_RES_ALLOC); 1460} 1461 1462static int ioctl_deallocate_iso_resource(struct client *client, 1463 union ioctl_arg *arg) 1464{ 1465 return release_client_resource(client, 1466 arg->deallocate.handle, release_iso_resource, NULL); 1467} 1468 1469static int ioctl_allocate_iso_resource_once(struct client *client, 1470 union ioctl_arg *arg) 1471{ 1472 return init_iso_resource(client, 1473 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE); 1474} 1475 1476static int ioctl_deallocate_iso_resource_once(struct client *client, 1477 union ioctl_arg *arg) 1478{ 1479 return init_iso_resource(client, 1480 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE); 1481} 1482 1483/* 1484 * Returns a speed code: Maximum speed to or from this device, 1485 * limited by the device's link speed, the local node's link speed, 1486 * and all PHY port speeds between the two links. 1487 */ 1488static int ioctl_get_speed(struct client *client, union ioctl_arg *arg) 1489{ 1490 return client->device->max_speed; 1491} 1492 1493static int ioctl_send_broadcast_request(struct client *client, 1494 union ioctl_arg *arg) 1495{ 1496 struct fw_cdev_send_request *a = &arg->send_request; 1497 1498 switch (a->tcode) { 1499 case TCODE_WRITE_QUADLET_REQUEST: 1500 case TCODE_WRITE_BLOCK_REQUEST: 1501 break; 1502 default: 1503 return -EINVAL; 1504 } 1505 1506 /* Security policy: Only allow accesses to Units Space. */ 1507 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END) 1508 return -EACCES; 1509 1510 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100); 1511} 1512 1513static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg) 1514{ 1515 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet; 1516 struct fw_cdev_send_request request; 1517 int dest; 1518 1519 if (a->speed > client->device->card->link_speed || 1520 a->length > 1024 << a->speed) 1521 return -EIO; 1522 1523 if (a->tag > 3 || a->channel > 63 || a->sy > 15) 1524 return -EINVAL; 1525 1526 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy); 1527 request.tcode = TCODE_STREAM_DATA; 1528 request.length = a->length; 1529 request.closure = a->closure; 1530 request.data = a->data; 1531 request.generation = a->generation; 1532 1533 return init_request(client, &request, dest, a->speed); 1534} 1535 1536static void outbound_phy_packet_callback(struct fw_packet *packet, 1537 struct fw_card *card, int status) 1538{ 1539 struct outbound_phy_packet_event *e = 1540 container_of(packet, struct outbound_phy_packet_event, p); 1541 struct client *e_client = e->client; 1542 u32 rcode; 1543 1544 trace_async_phy_outbound_complete((uintptr_t)packet, card->index, status, packet->generation, 1545 packet->timestamp); 1546 1547 switch (status) { 1548 // expected: 1549 case ACK_COMPLETE: 1550 rcode = RCODE_COMPLETE; 1551 break; 1552 // should never happen with PHY packets: 1553 case ACK_PENDING: 1554 rcode = RCODE_COMPLETE; 1555 break; 1556 case ACK_BUSY_X: 1557 case ACK_BUSY_A: 1558 case ACK_BUSY_B: 1559 rcode = RCODE_BUSY; 1560 break; 1561 case ACK_DATA_ERROR: 1562 rcode = RCODE_DATA_ERROR; 1563 break; 1564 case ACK_TYPE_ERROR: 1565 rcode = RCODE_TYPE_ERROR; 1566 break; 1567 // stale generation; cancelled; on certain controllers: no ack 1568 default: 1569 rcode = status; 1570 break; 1571 } 1572 1573 switch (e->phy_packet.without_tstamp.type) { 1574 case FW_CDEV_EVENT_PHY_PACKET_SENT: 1575 { 1576 struct fw_cdev_event_phy_packet *pp = &e->phy_packet.without_tstamp; 1577 1578 pp->rcode = rcode; 1579 pp->data[0] = packet->timestamp; 1580 queue_event(e->client, &e->event, &e->phy_packet, sizeof(*pp) + pp->length, 1581 NULL, 0); 1582 break; 1583 } 1584 case FW_CDEV_EVENT_PHY_PACKET_SENT2: 1585 { 1586 struct fw_cdev_event_phy_packet2 *pp = &e->phy_packet.with_tstamp; 1587 1588 pp->rcode = rcode; 1589 pp->tstamp = packet->timestamp; 1590 queue_event(e->client, &e->event, &e->phy_packet, sizeof(*pp) + pp->length, 1591 NULL, 0); 1592 break; 1593 } 1594 default: 1595 WARN_ON(1); 1596 break; 1597 } 1598 1599 client_put(e_client); 1600} 1601 1602static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg) 1603{ 1604 struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet; 1605 struct fw_card *card = client->device->card; 1606 struct outbound_phy_packet_event *e; 1607 1608 /* Access policy: Allow this ioctl only on local nodes' device files. */ 1609 if (!client->device->is_local) 1610 return -ENOSYS; 1611 1612 e = kzalloc(sizeof(*e) + sizeof(a->data), GFP_KERNEL); 1613 if (e == NULL) 1614 return -ENOMEM; 1615 1616 client_get(client); 1617 e->client = client; 1618 e->p.speed = SCODE_100; 1619 e->p.generation = a->generation; 1620 async_header_set_tcode(e->p.header, TCODE_LINK_INTERNAL); 1621 e->p.header[1] = a->data[0]; 1622 e->p.header[2] = a->data[1]; 1623 e->p.header_length = 12; 1624 e->p.callback = outbound_phy_packet_callback; 1625 1626 if (client->version < FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP) { 1627 struct fw_cdev_event_phy_packet *pp = &e->phy_packet.without_tstamp; 1628 1629 pp->closure = a->closure; 1630 pp->type = FW_CDEV_EVENT_PHY_PACKET_SENT; 1631 if (is_ping_packet(a->data)) 1632 pp->length = 4; 1633 } else { 1634 struct fw_cdev_event_phy_packet2 *pp = &e->phy_packet.with_tstamp; 1635 1636 pp->closure = a->closure; 1637 pp->type = FW_CDEV_EVENT_PHY_PACKET_SENT2; 1638 // Keep the data field so that application can match the response event to the 1639 // request. 1640 pp->length = sizeof(a->data); 1641 memcpy(pp->data, a->data, sizeof(a->data)); 1642 } 1643 1644 trace_async_phy_outbound_initiate((uintptr_t)&e->p, card->index, e->p.generation, 1645 e->p.header[1], e->p.header[2]); 1646 1647 card->driver->send_request(card, &e->p); 1648 1649 return 0; 1650} 1651 1652static int ioctl_receive_phy_packets(struct client *client, union ioctl_arg *arg) 1653{ 1654 struct fw_cdev_receive_phy_packets *a = &arg->receive_phy_packets; 1655 1656 /* Access policy: Allow this ioctl only on local nodes' device files. */ 1657 if (!client->device->is_local) 1658 return -ENOSYS; 1659 1660 // NOTE: This can be without irq when we can guarantee that __fw_send_request() for local 1661 // destination never runs in any type of IRQ context. 1662 scoped_guard(spinlock_irq, &phy_receiver_list_lock) 1663 list_move_tail(&client->phy_receiver_link, &phy_receiver_list); 1664 1665 client->phy_receiver_closure = a->closure; 1666 1667 return 0; 1668} 1669 1670void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p) 1671{ 1672 struct client *client; 1673 1674 // NOTE: This can be without irqsave when we can guarantee that __fw_send_request() for local 1675 // destination never runs in any type of IRQ context. 1676 guard(spinlock_irqsave)(&phy_receiver_list_lock); 1677 1678 list_for_each_entry(client, &phy_receiver_list, phy_receiver_link) { 1679 struct inbound_phy_packet_event *e; 1680 1681 if (client->device->card != card) 1682 continue; 1683 1684 e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC); 1685 if (e == NULL) 1686 break; 1687 1688 if (client->version < FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP) { 1689 struct fw_cdev_event_phy_packet *pp = &e->phy_packet.without_tstamp; 1690 1691 pp->closure = client->phy_receiver_closure; 1692 pp->type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED; 1693 pp->rcode = RCODE_COMPLETE; 1694 pp->length = 8; 1695 pp->data[0] = p->header[1]; 1696 pp->data[1] = p->header[2]; 1697 queue_event(client, &e->event, &e->phy_packet, sizeof(*pp) + 8, NULL, 0); 1698 } else { 1699 struct fw_cdev_event_phy_packet2 *pp = &e->phy_packet.with_tstamp; 1700 1701 pp = &e->phy_packet.with_tstamp; 1702 pp->closure = client->phy_receiver_closure; 1703 pp->type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED2; 1704 pp->rcode = RCODE_COMPLETE; 1705 pp->length = 8; 1706 pp->tstamp = p->timestamp; 1707 pp->data[0] = p->header[1]; 1708 pp->data[1] = p->header[2]; 1709 queue_event(client, &e->event, &e->phy_packet, sizeof(*pp) + 8, NULL, 0); 1710 } 1711 } 1712} 1713 1714static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = { 1715 [0x00] = ioctl_get_info, 1716 [0x01] = ioctl_send_request, 1717 [0x02] = ioctl_allocate, 1718 [0x03] = ioctl_deallocate, 1719 [0x04] = ioctl_send_response, 1720 [0x05] = ioctl_initiate_bus_reset, 1721 [0x06] = ioctl_add_descriptor, 1722 [0x07] = ioctl_remove_descriptor, 1723 [0x08] = ioctl_create_iso_context, 1724 [0x09] = ioctl_queue_iso, 1725 [0x0a] = ioctl_start_iso, 1726 [0x0b] = ioctl_stop_iso, 1727 [0x0c] = ioctl_get_cycle_timer, 1728 [0x0d] = ioctl_allocate_iso_resource, 1729 [0x0e] = ioctl_deallocate_iso_resource, 1730 [0x0f] = ioctl_allocate_iso_resource_once, 1731 [0x10] = ioctl_deallocate_iso_resource_once, 1732 [0x11] = ioctl_get_speed, 1733 [0x12] = ioctl_send_broadcast_request, 1734 [0x13] = ioctl_send_stream_packet, 1735 [0x14] = ioctl_get_cycle_timer2, 1736 [0x15] = ioctl_send_phy_packet, 1737 [0x16] = ioctl_receive_phy_packets, 1738 [0x17] = ioctl_set_iso_channels, 1739 [0x18] = ioctl_flush_iso, 1740}; 1741 1742static int dispatch_ioctl(struct client *client, 1743 unsigned int cmd, void __user *arg) 1744{ 1745 union ioctl_arg buffer; 1746 int ret; 1747 1748 if (fw_device_is_shutdown(client->device)) 1749 return -ENODEV; 1750 1751 if (_IOC_TYPE(cmd) != '#' || 1752 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) || 1753 _IOC_SIZE(cmd) > sizeof(buffer)) 1754 return -ENOTTY; 1755 1756 memset(&buffer, 0, sizeof(buffer)); 1757 1758 if (_IOC_DIR(cmd) & _IOC_WRITE) 1759 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd))) 1760 return -EFAULT; 1761 1762 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer); 1763 if (ret < 0) 1764 return ret; 1765 1766 if (_IOC_DIR(cmd) & _IOC_READ) 1767 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd))) 1768 return -EFAULT; 1769 1770 return ret; 1771} 1772 1773static long fw_device_op_ioctl(struct file *file, 1774 unsigned int cmd, unsigned long arg) 1775{ 1776 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg); 1777} 1778 1779static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma) 1780{ 1781 struct client *client = file->private_data; 1782 unsigned long size; 1783 int page_count, ret; 1784 1785 if (fw_device_is_shutdown(client->device)) 1786 return -ENODEV; 1787 1788 /* FIXME: We could support multiple buffers, but we don't. */ 1789 if (client->buffer.pages != NULL) 1790 return -EBUSY; 1791 1792 if (!(vma->vm_flags & VM_SHARED)) 1793 return -EINVAL; 1794 1795 if (vma->vm_start & ~PAGE_MASK) 1796 return -EINVAL; 1797 1798 client->vm_start = vma->vm_start; 1799 size = vma->vm_end - vma->vm_start; 1800 page_count = size >> PAGE_SHIFT; 1801 if (size & ~PAGE_MASK) 1802 return -EINVAL; 1803 1804 ret = fw_iso_buffer_alloc(&client->buffer, page_count); 1805 if (ret < 0) 1806 return ret; 1807 1808 scoped_guard(mutex, &client->iso_context_mutex) { 1809 // The direction of DMA can be determined if the isochronous context is already 1810 // allocated. If not, the DMA mapping operation is postponed after the allocation. 1811 if (client->iso_context) { 1812 ret = fw_iso_buffer_map_dma(&client->buffer, client->device->card, 1813 iso_dma_direction(client->iso_context)); 1814 if (ret < 0) 1815 goto fail; 1816 } 1817 } 1818 1819 ret = vm_map_pages_zero(vma, client->buffer.pages, 1820 client->buffer.page_count); 1821 if (ret < 0) 1822 goto fail; 1823 1824 return 0; 1825 fail: 1826 fw_iso_buffer_destroy(&client->buffer, client->device->card); 1827 return ret; 1828} 1829 1830static bool has_outbound_transactions(struct client *client) 1831{ 1832 struct client_resource *resource; 1833 unsigned long index; 1834 1835 guard(spinlock_irq)(&client->lock); 1836 1837 xa_for_each(&client->resource_xa, index, resource) { 1838 if (is_outbound_transaction_resource(resource)) 1839 return true; 1840 } 1841 1842 return false; 1843} 1844 1845static int fw_device_op_release(struct inode *inode, struct file *file) 1846{ 1847 struct client *client = file->private_data; 1848 struct event *event, *next_event; 1849 struct client_resource *resource; 1850 unsigned long index; 1851 1852 // NOTE: This can be without irq when we can guarantee that __fw_send_request() for local 1853 // destination never runs in any type of IRQ context. 1854 scoped_guard(spinlock_irq, &phy_receiver_list_lock) 1855 list_del(&client->phy_receiver_link); 1856 1857 scoped_guard(mutex, &client->device->client_list_mutex) 1858 list_del(&client->link); 1859 1860 if (client->iso_context) 1861 fw_iso_context_destroy(client->iso_context); 1862 mutex_destroy(&client->iso_context_mutex); 1863 1864 if (client->buffer.pages) 1865 fw_iso_buffer_destroy(&client->buffer, client->device->card); 1866 1867 // Freeze client->resource_xa and client->event_list. 1868 scoped_guard(spinlock_irq, &client->lock) 1869 client->in_shutdown = true; 1870 1871 wait_event(client->tx_flush_wait, !has_outbound_transactions(client)); 1872 1873 xa_for_each(&client->resource_xa, index, resource) { 1874 resource->release(client, resource); 1875 client_put(client); 1876 } 1877 xa_destroy(&client->resource_xa); 1878 1879 list_for_each_entry_safe(event, next_event, &client->event_list, link) 1880 kfree(event); 1881 1882 client_put(client); 1883 1884 return 0; 1885} 1886 1887static __poll_t fw_device_op_poll(struct file *file, poll_table * pt) 1888{ 1889 struct client *client = file->private_data; 1890 __poll_t mask = 0; 1891 1892 poll_wait(file, &client->wait, pt); 1893 1894 if (fw_device_is_shutdown(client->device)) 1895 mask |= EPOLLHUP | EPOLLERR; 1896 if (!list_empty(&client->event_list)) 1897 mask |= EPOLLIN | EPOLLRDNORM; 1898 1899 return mask; 1900} 1901 1902const struct file_operations fw_device_ops = { 1903 .owner = THIS_MODULE, 1904 .open = fw_device_op_open, 1905 .read = fw_device_op_read, 1906 .unlocked_ioctl = fw_device_op_ioctl, 1907 .mmap = fw_device_op_mmap, 1908 .release = fw_device_op_release, 1909 .poll = fw_device_op_poll, 1910 .compat_ioctl = compat_ptr_ioctl, 1911};