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