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