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