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