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

Configure Feed

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

Merge tag 'firewire-updates-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394/linux1394

Pull firewire updates from Takashi Sakamoto:
"In the FireWire subsystem, tasklets have been used as the bottom half
of 1394 OHCi hardIRQ. In recent kernel updates, BH workqueues have
become available, and some developers have proposed replacing the
tasklet with a BH workqueue.

As a first step towards dropping tasklet use, the 1394 OHCI
isochronous context can use regular workqueues. In this context, the
batch of packets is processed in the specific queue, thus the timing
jitter caused by task scheduling is not so critical.

Additionally, DMA transmission can be scheduled per-packet basis,
therefore the context can be sleep between the operation of
transmissions. Furthermore, in-kernel protocol implementation involves
some CPU-bound tasks, which can sometimes consumes CPU time so long.
These characteristics suggest that normal workqueues are suitable,
through BH workqueues are not.

The replacement with a workqueue allows unit drivers to process the
content of packets in non-atomic context. It brings some reliefs to
some drivers in sound subsystem that spin-lock is not mandatory
anymore during isochronous packet processing.

Summary:

- Replace tasklet with workqueue for isochronous context

- Replace IDR with XArray

- Utilize guard macro where possible

- Print deprecation warning when enabling debug parameter of
firewire-ohci module

- Switch to nonatomic PCM operation"

* tag 'firewire-updates-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394/linux1394: (55 commits)
firewire: core: rename cause flag of tracepoints event
firewire: core: update documentation of kernel APIs for flushing completions
firewire: core: add helper function to retire descriptors
Revert "firewire: core: move workqueue handler from 1394 OHCI driver to core function"
Revert "firewire: core: use mutex to coordinate concurrent calls to flush completions"
firewire: core: use mutex to coordinate concurrent calls to flush completions
firewire: core: move workqueue handler from 1394 OHCI driver to core function
firewire: core: fulfill documentation of fw_iso_context_flush_completions()
firewire: core: expose kernel API to schedule work item to process isochronous context
firewire: core: use WARN_ON_ONCE() to avoid superfluous dumps
ALSA: firewire: use nonatomic PCM operation
firewire: core: non-atomic memory allocation for isochronous event to user client
firewire: ohci: operate IT/IR events in sleepable work process instead of tasklet softIRQ
firewire: core: add local API to queue work item to workqueue specific to isochronous contexts
firewire: core: allocate workqueue to handle isochronous contexts in card
firewire: ohci: obsolete direct usage of printk_ratelimit()
firewire: ohci: deprecate debug parameter
firewire: core: update fw_device outside of device_find_child()
firewire: ohci: fix error path to detect initiated reset in TI TSB41BA3D phy
firewire: core/ohci: minor refactoring for computation of configuration ROM size
...

+1065 -774
+2
Documentation/driver-api/firewire.rst
··· 43 43 Firewire Isochronous I/O interfaces 44 44 =================================== 45 45 46 + .. kernel-doc:: include/linux/firewire.h 47 + :functions: fw_iso_context_schedule_flush_completions 46 48 .. kernel-doc:: drivers/firewire/core-iso.c 47 49 :export: 48 50
+54 -37
drivers/firewire/core-card.c
··· 168 168 int fw_core_add_descriptor(struct fw_descriptor *desc) 169 169 { 170 170 size_t i; 171 - int ret; 172 171 173 172 /* 174 173 * Check descriptor is valid; the length of all blocks in the ··· 181 182 if (i != desc->length) 182 183 return -EINVAL; 183 184 184 - mutex_lock(&card_mutex); 185 + guard(mutex)(&card_mutex); 185 186 186 - if (config_rom_length + required_space(desc) > 256) { 187 - ret = -EBUSY; 188 - } else { 189 - list_add_tail(&desc->link, &descriptor_list); 190 - config_rom_length += required_space(desc); 187 + if (config_rom_length + required_space(desc) > 256) 188 + return -EBUSY; 189 + 190 + list_add_tail(&desc->link, &descriptor_list); 191 + config_rom_length += required_space(desc); 192 + descriptor_count++; 193 + if (desc->immediate > 0) 191 194 descriptor_count++; 192 - if (desc->immediate > 0) 193 - descriptor_count++; 194 - update_config_roms(); 195 - ret = 0; 196 - } 195 + update_config_roms(); 197 196 198 - mutex_unlock(&card_mutex); 199 - 200 - return ret; 197 + return 0; 201 198 } 202 199 EXPORT_SYMBOL(fw_core_add_descriptor); 203 200 204 201 void fw_core_remove_descriptor(struct fw_descriptor *desc) 205 202 { 206 - mutex_lock(&card_mutex); 203 + guard(mutex)(&card_mutex); 207 204 208 205 list_del(&desc->link); 209 206 config_rom_length -= required_space(desc); ··· 207 212 if (desc->immediate > 0) 208 213 descriptor_count--; 209 214 update_config_roms(); 210 - 211 - mutex_unlock(&card_mutex); 212 215 } 213 216 EXPORT_SYMBOL(fw_core_remove_descriptor); 214 217 ··· 374 381 375 382 bm_id = be32_to_cpu(transaction_data[0]); 376 383 377 - spin_lock_irq(&card->lock); 378 - if (rcode == RCODE_COMPLETE && generation == card->generation) 379 - card->bm_node_id = 380 - bm_id == 0x3f ? local_id : 0xffc0 | bm_id; 381 - spin_unlock_irq(&card->lock); 384 + scoped_guard(spinlock_irq, &card->lock) { 385 + if (rcode == RCODE_COMPLETE && generation == card->generation) 386 + card->bm_node_id = 387 + bm_id == 0x3f ? local_id : 0xffc0 | bm_id; 388 + } 382 389 383 390 if (rcode == RCODE_COMPLETE && bm_id != 0x3f) { 384 391 /* Somebody else is BM. Only act as IRM. */ ··· 571 578 } 572 579 EXPORT_SYMBOL(fw_card_initialize); 573 580 574 - int fw_card_add(struct fw_card *card, 575 - u32 max_receive, u32 link_speed, u64 guid) 581 + int fw_card_add(struct fw_card *card, u32 max_receive, u32 link_speed, u64 guid, 582 + unsigned int supported_isoc_contexts) 576 583 { 584 + struct workqueue_struct *isoc_wq; 577 585 int ret; 586 + 587 + // This workqueue should be: 588 + // * != WQ_BH Sleepable. 589 + // * == WQ_UNBOUND Any core can process data for isoc context. The 590 + // implementation of unit protocol could consumes the core 591 + // longer somehow. 592 + // * != WQ_MEM_RECLAIM Not used for any backend of block device. 593 + // * == WQ_FREEZABLE Isochronous communication is at regular interval in real 594 + // time, thus should be drained if possible at freeze phase. 595 + // * == WQ_HIGHPRI High priority to process semi-realtime timestamped data. 596 + // * == WQ_SYSFS Parameters are available via sysfs. 597 + // * max_active == n_it + n_ir A hardIRQ could notify events for multiple isochronous 598 + // contexts if they are scheduled to the same cycle. 599 + isoc_wq = alloc_workqueue("firewire-isoc-card%u", 600 + WQ_UNBOUND | WQ_FREEZABLE | WQ_HIGHPRI | WQ_SYSFS, 601 + supported_isoc_contexts, card->index); 602 + if (!isoc_wq) 603 + return -ENOMEM; 578 604 579 605 card->max_receive = max_receive; 580 606 card->link_speed = link_speed; 581 607 card->guid = guid; 582 608 583 - mutex_lock(&card_mutex); 609 + guard(mutex)(&card_mutex); 584 610 585 611 generate_config_rom(card, tmp_config_rom); 586 612 ret = card->driver->enable(card, tmp_config_rom, config_rom_length); 587 - if (ret == 0) 588 - list_add_tail(&card->link, &card_list); 613 + if (ret < 0) { 614 + destroy_workqueue(isoc_wq); 615 + return ret; 616 + } 589 617 590 - mutex_unlock(&card_mutex); 618 + card->isoc_wq = isoc_wq; 619 + list_add_tail(&card->link, &card_list); 591 620 592 - return ret; 621 + return 0; 593 622 } 594 623 EXPORT_SYMBOL(fw_card_add); 595 624 ··· 729 714 void fw_core_remove_card(struct fw_card *card) 730 715 { 731 716 struct fw_card_driver dummy_driver = dummy_driver_template; 732 - unsigned long flags; 717 + 718 + might_sleep(); 733 719 734 720 card->driver->update_phy_reg(card, 4, 735 721 PHY_LINK_ACTIVE | PHY_CONTENDER, 0); 736 722 fw_schedule_bus_reset(card, false, true); 737 723 738 - mutex_lock(&card_mutex); 739 - list_del_init(&card->link); 740 - mutex_unlock(&card_mutex); 724 + scoped_guard(mutex, &card_mutex) 725 + list_del_init(&card->link); 741 726 742 727 /* Switch off most of the card driver interface. */ 743 728 dummy_driver.free_iso_context = card->driver->free_iso_context; 744 729 dummy_driver.stop_iso = card->driver->stop_iso; 745 730 card->driver = &dummy_driver; 731 + drain_workqueue(card->isoc_wq); 746 732 747 - spin_lock_irqsave(&card->lock, flags); 748 - fw_destroy_nodes(card); 749 - spin_unlock_irqrestore(&card->lock, flags); 733 + scoped_guard(spinlock_irqsave, &card->lock) 734 + fw_destroy_nodes(card); 750 735 751 736 /* Wait for all users, especially device workqueue jobs, to finish. */ 752 737 fw_card_put(card); 753 738 wait_for_completion(&card->done); 739 + 740 + destroy_workqueue(card->isoc_wq); 754 741 755 742 WARN_ON(!list_empty(&card->transaction_list)); 756 743 }
+192 -208
drivers/firewire/core-cdev.c
··· 14 14 #include <linux/errno.h> 15 15 #include <linux/firewire.h> 16 16 #include <linux/firewire-cdev.h> 17 - #include <linux/idr.h> 18 17 #include <linux/irqflags.h> 19 18 #include <linux/jiffies.h> 20 19 #include <linux/kernel.h> ··· 36 37 #include "core.h" 37 38 #include <trace/events/firewire.h> 38 39 40 + #include "packet-header-definitions.h" 41 + 39 42 /* 40 43 * ABI version history is documented in linux/firewire-cdev.h. 41 44 */ ··· 53 52 54 53 spinlock_t lock; 55 54 bool in_shutdown; 56 - struct idr resource_idr; 55 + struct xarray resource_xa; 57 56 struct list_head event_list; 58 57 wait_queue_head_t wait; 59 58 wait_queue_head_t tx_flush_wait; ··· 138 137 struct iso_resource_event *e_alloc, *e_dealloc; 139 138 }; 140 139 140 + static struct address_handler_resource *to_address_handler_resource(struct client_resource *resource) 141 + { 142 + return container_of(resource, struct address_handler_resource, resource); 143 + } 144 + 145 + static struct inbound_transaction_resource *to_inbound_transaction_resource(struct client_resource *resource) 146 + { 147 + return container_of(resource, struct inbound_transaction_resource, resource); 148 + } 149 + 150 + static struct descriptor_resource *to_descriptor_resource(struct client_resource *resource) 151 + { 152 + return container_of(resource, struct descriptor_resource, resource); 153 + } 154 + 155 + static struct iso_resource *to_iso_resource(struct client_resource *resource) 156 + { 157 + return container_of(resource, struct iso_resource, resource); 158 + } 159 + 141 160 static void release_iso_resource(struct client *, struct client_resource *); 161 + 162 + static int is_iso_resource(const struct client_resource *resource) 163 + { 164 + return resource->release == release_iso_resource; 165 + } 166 + 167 + static void release_transaction(struct client *client, 168 + struct client_resource *resource); 169 + 170 + static int is_outbound_transaction_resource(const struct client_resource *resource) 171 + { 172 + return resource->release == release_transaction; 173 + } 142 174 143 175 static void schedule_iso_resource(struct iso_resource *r, unsigned long delay) 144 176 { 145 177 client_get(r->client); 146 178 if (!queue_delayed_work(fw_workqueue, &r->work, delay)) 147 179 client_put(r->client); 148 - } 149 - 150 - static void schedule_if_iso_resource(struct client_resource *resource) 151 - { 152 - if (resource->release == release_iso_resource) 153 - schedule_iso_resource(container_of(resource, 154 - struct iso_resource, resource), 0); 155 180 } 156 181 157 182 /* ··· 296 269 297 270 client->device = device; 298 271 spin_lock_init(&client->lock); 299 - idr_init(&client->resource_idr); 272 + xa_init_flags(&client->resource_xa, XA_FLAGS_ALLOC1 | XA_FLAGS_LOCK_BH); 300 273 INIT_LIST_HEAD(&client->event_list); 301 274 init_waitqueue_head(&client->wait); 302 275 init_waitqueue_head(&client->tx_flush_wait); ··· 312 285 static void queue_event(struct client *client, struct event *event, 313 286 void *data0, size_t size0, void *data1, size_t size1) 314 287 { 315 - unsigned long flags; 316 - 317 288 event->v[0].data = data0; 318 289 event->v[0].size = size0; 319 290 event->v[1].data = data1; 320 291 event->v[1].size = size1; 321 292 322 - spin_lock_irqsave(&client->lock, flags); 323 - if (client->in_shutdown) 324 - kfree(event); 325 - else 326 - list_add_tail(&event->link, &client->event_list); 327 - spin_unlock_irqrestore(&client->lock, flags); 293 + scoped_guard(spinlock_irqsave, &client->lock) { 294 + if (client->in_shutdown) 295 + kfree(event); 296 + else 297 + list_add_tail(&event->link, &client->event_list); 298 + } 328 299 329 300 wake_up_interruptible(&client->wait); 330 301 } ··· 344 319 fw_device_is_shutdown(client->device)) 345 320 return -ENODEV; 346 321 347 - spin_lock_irq(&client->lock); 348 - event = list_first_entry(&client->event_list, struct event, link); 349 - list_del(&event->link); 350 - spin_unlock_irq(&client->lock); 322 + scoped_guard(spinlock_irq, &client->lock) { 323 + event = list_first_entry(&client->event_list, struct event, link); 324 + list_del(&event->link); 325 + } 351 326 352 327 total = 0; 353 328 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) { ··· 379 354 { 380 355 struct fw_card *card = client->device->card; 381 356 382 - spin_lock_irq(&card->lock); 357 + guard(spinlock_irq)(&card->lock); 383 358 384 359 event->closure = client->bus_reset_closure; 385 360 event->type = FW_CDEV_EVENT_BUS_RESET; ··· 389 364 event->bm_node_id = card->bm_node_id; 390 365 event->irm_node_id = card->irm_node->node_id; 391 366 event->root_node_id = card->root_node->node_id; 392 - 393 - spin_unlock_irq(&card->lock); 394 367 } 395 368 396 369 static void for_each_client(struct fw_device *device, ··· 396 373 { 397 374 struct client *c; 398 375 399 - mutex_lock(&device->client_list_mutex); 376 + guard(mutex)(&device->client_list_mutex); 377 + 400 378 list_for_each_entry(c, &device->client_list, link) 401 379 callback(c); 402 - mutex_unlock(&device->client_list_mutex); 403 - } 404 - 405 - static int schedule_reallocations(int id, void *p, void *data) 406 - { 407 - schedule_if_iso_resource(p); 408 - 409 - return 0; 410 380 } 411 381 412 382 static void queue_bus_reset_event(struct client *client) 413 383 { 414 384 struct bus_reset_event *e; 385 + struct client_resource *resource; 386 + unsigned long index; 415 387 416 388 e = kzalloc(sizeof(*e), GFP_KERNEL); 417 389 if (e == NULL) ··· 417 399 queue_event(client, &e->event, 418 400 &e->reset, sizeof(e->reset), NULL, 0); 419 401 420 - spin_lock_irq(&client->lock); 421 - idr_for_each(&client->resource_idr, schedule_reallocations, client); 422 - spin_unlock_irq(&client->lock); 402 + guard(spinlock_irq)(&client->lock); 403 + 404 + xa_for_each(&client->resource_xa, index, resource) { 405 + if (is_iso_resource(resource)) 406 + schedule_iso_resource(to_iso_resource(resource), 0); 407 + } 423 408 } 424 409 425 410 void fw_device_cdev_update(struct fw_device *device) ··· 473 452 a->version = FW_CDEV_KERNEL_VERSION; 474 453 a->card = client->device->card->index; 475 454 476 - down_read(&fw_device_rwsem); 455 + scoped_guard(rwsem_read, &fw_device_rwsem) { 456 + if (a->rom != 0) { 457 + size_t want = a->rom_length; 458 + size_t have = client->device->config_rom_length * 4; 477 459 478 - if (a->rom != 0) { 479 - size_t want = a->rom_length; 480 - size_t have = client->device->config_rom_length * 4; 481 - 482 - ret = copy_to_user(u64_to_uptr(a->rom), 483 - client->device->config_rom, min(want, have)); 460 + ret = copy_to_user(u64_to_uptr(a->rom), client->device->config_rom, 461 + min(want, have)); 462 + if (ret != 0) 463 + return -EFAULT; 464 + } 465 + a->rom_length = client->device->config_rom_length * 4; 484 466 } 485 - a->rom_length = client->device->config_rom_length * 4; 486 467 487 - up_read(&fw_device_rwsem); 488 - 489 - if (ret != 0) 490 - return -EFAULT; 491 - 492 - mutex_lock(&client->device->client_list_mutex); 468 + guard(mutex)(&client->device->client_list_mutex); 493 469 494 470 client->bus_reset_closure = a->bus_reset_closure; 495 471 if (a->bus_reset != 0) { ··· 497 479 if (ret == 0 && list_empty(&client->link)) 498 480 list_add_tail(&client->link, &client->device->client_list); 499 481 500 - mutex_unlock(&client->device->client_list_mutex); 501 - 502 482 return ret ? -EFAULT : 0; 503 483 } 504 484 505 - static int add_client_resource(struct client *client, 506 - struct client_resource *resource, gfp_t gfp_mask) 485 + static int add_client_resource(struct client *client, struct client_resource *resource, 486 + gfp_t gfp_mask) 507 487 { 508 - bool preload = gfpflags_allow_blocking(gfp_mask); 509 - unsigned long flags; 510 488 int ret; 511 489 512 - if (preload) 513 - idr_preload(gfp_mask); 514 - spin_lock_irqsave(&client->lock, flags); 490 + scoped_guard(spinlock_irqsave, &client->lock) { 491 + u32 index; 515 492 516 - if (client->in_shutdown) 517 - ret = -ECANCELED; 518 - else 519 - ret = idr_alloc(&client->resource_idr, resource, 0, 0, 520 - GFP_NOWAIT); 521 - if (ret >= 0) { 522 - resource->handle = ret; 523 - client_get(client); 524 - schedule_if_iso_resource(resource); 493 + if (client->in_shutdown) { 494 + ret = -ECANCELED; 495 + } else { 496 + if (gfpflags_allow_blocking(gfp_mask)) { 497 + ret = xa_alloc(&client->resource_xa, &index, resource, xa_limit_32b, 498 + GFP_NOWAIT); 499 + } else { 500 + ret = xa_alloc_bh(&client->resource_xa, &index, resource, 501 + xa_limit_32b, GFP_NOWAIT); 502 + } 503 + } 504 + if (ret >= 0) { 505 + resource->handle = index; 506 + client_get(client); 507 + if (is_iso_resource(resource)) 508 + schedule_iso_resource(to_iso_resource(resource), 0); 509 + } 525 510 } 526 - 527 - spin_unlock_irqrestore(&client->lock, flags); 528 - if (preload) 529 - idr_preload_end(); 530 511 531 512 return ret < 0 ? ret : 0; 532 513 } ··· 534 517 client_resource_release_fn_t release, 535 518 struct client_resource **return_resource) 536 519 { 520 + unsigned long index = handle; 537 521 struct client_resource *resource; 538 522 539 - spin_lock_irq(&client->lock); 540 - if (client->in_shutdown) 541 - resource = NULL; 542 - else 543 - resource = idr_find(&client->resource_idr, handle); 544 - if (resource && resource->release == release) 545 - idr_remove(&client->resource_idr, handle); 546 - spin_unlock_irq(&client->lock); 523 + scoped_guard(spinlock_irq, &client->lock) { 524 + if (client->in_shutdown) 525 + return -EINVAL; 547 526 548 - if (!(resource && resource->release == release)) 549 - return -EINVAL; 527 + resource = xa_load(&client->resource_xa, index); 528 + if (!resource || resource->release != release) 529 + return -EINVAL; 530 + 531 + xa_erase(&client->resource_xa, handle); 532 + } 550 533 551 534 if (return_resource) 552 535 *return_resource = resource; ··· 568 551 { 569 552 struct outbound_transaction_event *e = data; 570 553 struct client *client = e->client; 571 - unsigned long flags; 554 + unsigned long index = e->r.resource.handle; 572 555 573 - spin_lock_irqsave(&client->lock, flags); 574 - idr_remove(&client->resource_idr, e->r.resource.handle); 575 - if (client->in_shutdown) 576 - wake_up(&client->tx_flush_wait); 577 - spin_unlock_irqrestore(&client->lock, flags); 556 + scoped_guard(spinlock_irqsave, &client->lock) { 557 + xa_erase(&client->resource_xa, index); 558 + if (client->in_shutdown) 559 + wake_up(&client->tx_flush_wait); 560 + } 578 561 579 562 switch (e->rsp.without_tstamp.type) { 580 563 case FW_CDEV_EVENT_RESPONSE: ··· 616 599 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length, NULL, 0); 617 600 618 601 break; 602 + } 619 603 default: 620 604 WARN_ON(1); 621 605 break; 622 606 } 623 - } 624 607 625 - /* Drop the idr's reference */ 608 + // Drop the xarray's reference. 626 609 client_put(client); 627 610 } 628 611 ··· 710 693 static void release_request(struct client *client, 711 694 struct client_resource *resource) 712 695 { 713 - struct inbound_transaction_resource *r = container_of(resource, 714 - struct inbound_transaction_resource, resource); 696 + struct inbound_transaction_resource *r = to_inbound_transaction_resource(resource); 715 697 716 698 if (r->is_fcp) 717 699 fw_request_put(r->request); ··· 820 804 static void release_address_handler(struct client *client, 821 805 struct client_resource *resource) 822 806 { 823 - struct address_handler_resource *r = 824 - container_of(resource, struct address_handler_resource, resource); 807 + struct address_handler_resource *r = to_address_handler_resource(resource); 825 808 826 809 fw_core_remove_address_handler(&r->handler); 827 810 kfree(r); ··· 884 869 release_request, &resource) < 0) 885 870 return -EINVAL; 886 871 887 - r = container_of(resource, struct inbound_transaction_resource, 888 - resource); 872 + r = to_inbound_transaction_resource(resource); 889 873 if (r->is_fcp) { 890 874 fw_request_put(r->request); 891 875 goto out; ··· 918 904 static void release_descriptor(struct client *client, 919 905 struct client_resource *resource) 920 906 { 921 - struct descriptor_resource *r = 922 - container_of(resource, struct descriptor_resource, resource); 907 + struct descriptor_resource *r = to_descriptor_resource(resource); 923 908 924 909 fw_core_remove_descriptor(&r->descriptor); 925 910 kfree(r); ··· 982 969 struct client *client = data; 983 970 struct iso_interrupt_event *e; 984 971 985 - e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC); 972 + e = kmalloc(sizeof(*e) + header_length, GFP_KERNEL); 986 973 if (e == NULL) 987 974 return; 988 975 ··· 1001 988 struct client *client = data; 1002 989 struct iso_interrupt_mc_event *e; 1003 990 1004 - e = kmalloc(sizeof(*e), GFP_ATOMIC); 991 + e = kmalloc(sizeof(*e), GFP_KERNEL); 1005 992 if (e == NULL) 1006 993 return; 1007 994 ··· 1083 1070 if (client->version < FW_CDEV_VERSION_AUTO_FLUSH_ISO_OVERFLOW) 1084 1071 context->drop_overflow_headers = true; 1085 1072 1086 - /* We only support one context at this time. */ 1087 - spin_lock_irq(&client->lock); 1073 + // We only support one context at this time. 1074 + guard(spinlock_irq)(&client->lock); 1075 + 1088 1076 if (client->iso_context != NULL) { 1089 - spin_unlock_irq(&client->lock); 1090 1077 fw_iso_context_destroy(context); 1091 1078 1092 1079 return -EBUSY; ··· 1096 1083 client->device->card, 1097 1084 iso_dma_direction(context)); 1098 1085 if (ret < 0) { 1099 - spin_unlock_irq(&client->lock); 1100 1086 fw_iso_context_destroy(context); 1101 1087 1102 1088 return ret; ··· 1104 1092 } 1105 1093 client->iso_closure = a->closure; 1106 1094 client->iso_context = context; 1107 - spin_unlock_irq(&client->lock); 1108 1095 1109 1096 a->handle = 0; 1110 1097 ··· 1277 1266 struct fw_card *card = client->device->card; 1278 1267 struct timespec64 ts = {0, 0}; 1279 1268 u32 cycle_time = 0; 1280 - int ret = 0; 1269 + int ret; 1281 1270 1282 - local_irq_disable(); 1271 + guard(irq)(); 1283 1272 1284 1273 ret = fw_card_read_cycle_time(card, &cycle_time); 1285 1274 if (ret < 0) 1286 - goto end; 1275 + return ret; 1287 1276 1288 1277 switch (a->clk_id) { 1289 1278 case CLOCK_REALTIME: ktime_get_real_ts64(&ts); break; 1290 1279 case CLOCK_MONOTONIC: ktime_get_ts64(&ts); break; 1291 1280 case CLOCK_MONOTONIC_RAW: ktime_get_raw_ts64(&ts); break; 1292 1281 default: 1293 - ret = -EINVAL; 1282 + return -EINVAL; 1294 1283 } 1295 - end: 1296 - local_irq_enable(); 1297 1284 1298 1285 a->tv_sec = ts.tv_sec; 1299 1286 a->tv_nsec = ts.tv_nsec; 1300 1287 a->cycle_timer = cycle_time; 1301 1288 1302 - return ret; 1289 + return 0; 1303 1290 } 1304 1291 1305 1292 static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg) ··· 1320 1311 struct iso_resource *r = 1321 1312 container_of(work, struct iso_resource, work.work); 1322 1313 struct client *client = r->client; 1314 + unsigned long index = r->resource.handle; 1323 1315 int generation, channel, bandwidth, todo; 1324 1316 bool skip, free, success; 1325 1317 1326 - spin_lock_irq(&client->lock); 1327 - generation = client->device->generation; 1328 - todo = r->todo; 1329 - /* Allow 1000ms grace period for other reallocations. */ 1330 - if (todo == ISO_RES_ALLOC && 1331 - time_before64(get_jiffies_64(), 1332 - client->device->card->reset_jiffies + HZ)) { 1333 - schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3)); 1334 - skip = true; 1335 - } else { 1336 - /* We could be called twice within the same generation. */ 1337 - skip = todo == ISO_RES_REALLOC && 1338 - r->generation == generation; 1318 + scoped_guard(spinlock_irq, &client->lock) { 1319 + generation = client->device->generation; 1320 + todo = r->todo; 1321 + // Allow 1000ms grace period for other reallocations. 1322 + if (todo == ISO_RES_ALLOC && 1323 + time_before64(get_jiffies_64(), client->device->card->reset_jiffies + HZ)) { 1324 + schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3)); 1325 + skip = true; 1326 + } else { 1327 + // We could be called twice within the same generation. 1328 + skip = todo == ISO_RES_REALLOC && 1329 + r->generation == generation; 1330 + } 1331 + free = todo == ISO_RES_DEALLOC || 1332 + todo == ISO_RES_ALLOC_ONCE || 1333 + todo == ISO_RES_DEALLOC_ONCE; 1334 + r->generation = generation; 1339 1335 } 1340 - free = todo == ISO_RES_DEALLOC || 1341 - todo == ISO_RES_ALLOC_ONCE || 1342 - todo == ISO_RES_DEALLOC_ONCE; 1343 - r->generation = generation; 1344 - spin_unlock_irq(&client->lock); 1345 1336 1346 1337 if (skip) 1347 1338 goto out; ··· 1355 1346 todo == ISO_RES_ALLOC_ONCE); 1356 1347 /* 1357 1348 * Is this generation outdated already? As long as this resource sticks 1358 - * in the idr, it will be scheduled again for a newer generation or at 1349 + * in the xarray, it will be scheduled again for a newer generation or at 1359 1350 * shutdown. 1360 1351 */ 1361 1352 if (channel == -EAGAIN && ··· 1364 1355 1365 1356 success = channel >= 0 || bandwidth > 0; 1366 1357 1367 - spin_lock_irq(&client->lock); 1368 - /* 1369 - * Transit from allocation to reallocation, except if the client 1370 - * requested deallocation in the meantime. 1371 - */ 1372 - if (r->todo == ISO_RES_ALLOC) 1373 - r->todo = ISO_RES_REALLOC; 1374 - /* 1375 - * Allocation or reallocation failure? Pull this resource out of the 1376 - * idr and prepare for deletion, unless the client is shutting down. 1377 - */ 1378 - if (r->todo == ISO_RES_REALLOC && !success && 1379 - !client->in_shutdown && 1380 - idr_remove(&client->resource_idr, r->resource.handle)) { 1381 - client_put(client); 1382 - free = true; 1358 + scoped_guard(spinlock_irq, &client->lock) { 1359 + // Transit from allocation to reallocation, except if the client 1360 + // requested deallocation in the meantime. 1361 + if (r->todo == ISO_RES_ALLOC) 1362 + r->todo = ISO_RES_REALLOC; 1363 + // Allocation or reallocation failure? Pull this resource out of the 1364 + // xarray and prepare for deletion, unless the client is shutting down. 1365 + if (r->todo == ISO_RES_REALLOC && !success && 1366 + !client->in_shutdown && 1367 + xa_erase(&client->resource_xa, index)) { 1368 + client_put(client); 1369 + free = true; 1370 + } 1383 1371 } 1384 - spin_unlock_irq(&client->lock); 1385 1372 1386 1373 if (todo == ISO_RES_ALLOC && channel >= 0) 1387 1374 r->channels = 1ULL << channel; ··· 1412 1407 static void release_iso_resource(struct client *client, 1413 1408 struct client_resource *resource) 1414 1409 { 1415 - struct iso_resource *r = 1416 - container_of(resource, struct iso_resource, resource); 1410 + struct iso_resource *r = to_iso_resource(resource); 1417 1411 1418 - spin_lock_irq(&client->lock); 1412 + guard(spinlock_irq)(&client->lock); 1413 + 1419 1414 r->todo = ISO_RES_DEALLOC; 1420 1415 schedule_iso_resource(r, 0); 1421 - spin_unlock_irq(&client->lock); 1422 1416 } 1423 1417 1424 1418 static int init_iso_resource(struct client *client, ··· 1639 1635 e->client = client; 1640 1636 e->p.speed = SCODE_100; 1641 1637 e->p.generation = a->generation; 1642 - e->p.header[0] = TCODE_LINK_INTERNAL << 4; 1638 + async_header_set_tcode(e->p.header, TCODE_LINK_INTERNAL); 1643 1639 e->p.header[1] = a->data[0]; 1644 1640 e->p.header[2] = a->data[1]; 1645 1641 e->p.header_length = 12; ··· 1680 1676 if (!client->device->is_local) 1681 1677 return -ENOSYS; 1682 1678 1683 - spin_lock_irq(&card->lock); 1679 + guard(spinlock_irq)(&card->lock); 1684 1680 1685 1681 list_move_tail(&client->phy_receiver_link, &card->phy_receiver_list); 1686 1682 client->phy_receiver_closure = a->closure; 1687 - 1688 - spin_unlock_irq(&card->lock); 1689 1683 1690 1684 return 0; 1691 1685 } ··· 1691 1689 void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p) 1692 1690 { 1693 1691 struct client *client; 1694 - struct inbound_phy_packet_event *e; 1695 - unsigned long flags; 1696 1692 1697 - spin_lock_irqsave(&card->lock, flags); 1693 + guard(spinlock_irqsave)(&card->lock); 1698 1694 1699 1695 list_for_each_entry(client, &card->phy_receiver_list, phy_receiver_link) { 1700 - e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC); 1696 + struct inbound_phy_packet_event *e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC); 1701 1697 if (e == NULL) 1702 1698 break; 1703 1699 ··· 1723 1723 queue_event(client, &e->event, &e->phy_packet, sizeof(*pp) + 8, NULL, 0); 1724 1724 } 1725 1725 } 1726 - 1727 - spin_unlock_irqrestore(&card->lock, flags); 1728 1726 } 1729 1727 1730 1728 static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = { ··· 1819 1821 if (ret < 0) 1820 1822 return ret; 1821 1823 1822 - spin_lock_irq(&client->lock); 1823 - if (client->iso_context) { 1824 - ret = fw_iso_buffer_map_dma(&client->buffer, 1825 - client->device->card, 1826 - iso_dma_direction(client->iso_context)); 1827 - client->buffer_is_mapped = (ret == 0); 1824 + scoped_guard(spinlock_irq, &client->lock) { 1825 + if (client->iso_context) { 1826 + ret = fw_iso_buffer_map_dma(&client->buffer, client->device->card, 1827 + iso_dma_direction(client->iso_context)); 1828 + if (ret < 0) 1829 + goto fail; 1830 + client->buffer_is_mapped = true; 1831 + } 1828 1832 } 1829 - spin_unlock_irq(&client->lock); 1830 - if (ret < 0) 1831 - goto fail; 1832 1833 1833 1834 ret = vm_map_pages_zero(vma, client->buffer.pages, 1834 1835 client->buffer.page_count); ··· 1840 1843 return ret; 1841 1844 } 1842 1845 1843 - static int is_outbound_transaction_resource(int id, void *p, void *data) 1846 + static bool has_outbound_transactions(struct client *client) 1844 1847 { 1845 - struct client_resource *resource = p; 1848 + struct client_resource *resource; 1849 + unsigned long index; 1846 1850 1847 - return resource->release == release_transaction; 1848 - } 1851 + guard(spinlock_irq)(&client->lock); 1849 1852 1850 - static int has_outbound_transactions(struct client *client) 1851 - { 1852 - int ret; 1853 + xa_for_each(&client->resource_xa, index, resource) { 1854 + if (is_outbound_transaction_resource(resource)) 1855 + return true; 1856 + } 1853 1857 1854 - spin_lock_irq(&client->lock); 1855 - ret = idr_for_each(&client->resource_idr, 1856 - is_outbound_transaction_resource, NULL); 1857 - spin_unlock_irq(&client->lock); 1858 - 1859 - return ret; 1860 - } 1861 - 1862 - static int shutdown_resource(int id, void *p, void *data) 1863 - { 1864 - struct client_resource *resource = p; 1865 - struct client *client = data; 1866 - 1867 - resource->release(client, resource); 1868 - client_put(client); 1869 - 1870 - return 0; 1858 + return false; 1871 1859 } 1872 1860 1873 1861 static int fw_device_op_release(struct inode *inode, struct file *file) 1874 1862 { 1875 1863 struct client *client = file->private_data; 1876 1864 struct event *event, *next_event; 1865 + struct client_resource *resource; 1866 + unsigned long index; 1877 1867 1878 - spin_lock_irq(&client->device->card->lock); 1879 - list_del(&client->phy_receiver_link); 1880 - spin_unlock_irq(&client->device->card->lock); 1868 + scoped_guard(spinlock_irq, &client->device->card->lock) 1869 + list_del(&client->phy_receiver_link); 1881 1870 1882 - mutex_lock(&client->device->client_list_mutex); 1883 - list_del(&client->link); 1884 - mutex_unlock(&client->device->client_list_mutex); 1871 + scoped_guard(mutex, &client->device->client_list_mutex) 1872 + list_del(&client->link); 1885 1873 1886 1874 if (client->iso_context) 1887 1875 fw_iso_context_destroy(client->iso_context); ··· 1874 1892 if (client->buffer.pages) 1875 1893 fw_iso_buffer_destroy(&client->buffer, client->device->card); 1876 1894 1877 - /* Freeze client->resource_idr and client->event_list */ 1878 - spin_lock_irq(&client->lock); 1879 - client->in_shutdown = true; 1880 - spin_unlock_irq(&client->lock); 1895 + // Freeze client->resource_xa and client->event_list. 1896 + scoped_guard(spinlock_irq, &client->lock) 1897 + client->in_shutdown = true; 1881 1898 1882 1899 wait_event(client->tx_flush_wait, !has_outbound_transactions(client)); 1883 1900 1884 - idr_for_each(&client->resource_idr, shutdown_resource, client); 1885 - idr_destroy(&client->resource_idr); 1901 + xa_for_each(&client->resource_xa, index, resource) { 1902 + resource->release(client, resource); 1903 + client_put(client); 1904 + } 1905 + xa_destroy(&client->resource_xa); 1886 1906 1887 1907 list_for_each_entry_safe(event, next_event, &client->event_list, link) 1888 1908 kfree(event);
+92 -110
drivers/firewire/core-device.c
··· 12 12 #include <linux/errno.h> 13 13 #include <linux/firewire.h> 14 14 #include <linux/firewire-constants.h> 15 - #include <linux/idr.h> 16 15 #include <linux/jiffies.h> 17 16 #include <linux/kobject.h> 18 17 #include <linux/list.h> ··· 287 288 const u32 *directories[] = {NULL, NULL}; 288 289 int i, value = -1; 289 290 290 - down_read(&fw_device_rwsem); 291 + guard(rwsem_read)(&fw_device_rwsem); 291 292 292 293 if (is_fw_unit(dev)) { 293 294 directories[0] = fw_unit(dev)->directory; ··· 316 317 } 317 318 } 318 319 319 - up_read(&fw_device_rwsem); 320 - 321 320 if (value < 0) 322 321 return -ENOENT; 323 322 ··· 336 339 char dummy_buf[2]; 337 340 int i, ret = -ENOENT; 338 341 339 - down_read(&fw_device_rwsem); 342 + guard(rwsem_read)(&fw_device_rwsem); 340 343 341 344 if (is_fw_unit(dev)) { 342 345 directories[0] = fw_unit(dev)->directory; ··· 379 382 } 380 383 } 381 384 382 - if (ret >= 0) { 383 - /* Strip trailing whitespace and add newline. */ 384 - while (ret > 0 && isspace(buf[ret - 1])) 385 - ret--; 386 - strcpy(buf + ret, "\n"); 387 - ret++; 388 - } 385 + if (ret < 0) 386 + return ret; 389 387 390 - up_read(&fw_device_rwsem); 388 + // Strip trailing whitespace and add newline. 389 + while (ret > 0 && isspace(buf[ret - 1])) 390 + ret--; 391 + strcpy(buf + ret, "\n"); 392 + ret++; 391 393 392 394 return ret; 393 395 } ··· 462 466 struct fw_device *device = fw_device(dev); 463 467 size_t length; 464 468 465 - down_read(&fw_device_rwsem); 469 + guard(rwsem_read)(&fw_device_rwsem); 470 + 466 471 length = device->config_rom_length * 4; 467 472 memcpy(buf, device->config_rom, length); 468 - up_read(&fw_device_rwsem); 469 473 470 474 return length; 471 475 } ··· 474 478 struct device_attribute *attr, char *buf) 475 479 { 476 480 struct fw_device *device = fw_device(dev); 477 - int ret; 478 481 479 - down_read(&fw_device_rwsem); 480 - ret = sysfs_emit(buf, "0x%08x%08x\n", device->config_rom[3], device->config_rom[4]); 481 - up_read(&fw_device_rwsem); 482 + guard(rwsem_read)(&fw_device_rwsem); 482 483 483 - return ret; 484 + return sysfs_emit(buf, "0x%08x%08x\n", device->config_rom[3], device->config_rom[4]); 484 485 } 485 486 486 487 static ssize_t is_local_show(struct device *dev, ··· 517 524 struct fw_csr_iterator ci; 518 525 int key, value, i = 0; 519 526 520 - down_read(&fw_device_rwsem); 527 + guard(rwsem_read)(&fw_device_rwsem); 528 + 521 529 fw_csr_iterator_init(&ci, &device->config_rom[ROOT_DIR_OFFSET]); 522 530 while (fw_csr_iterator_next(&ci, &key, &value)) { 523 531 if (key != (CSR_UNIT | CSR_DIRECTORY)) ··· 527 533 if (i >= PAGE_SIZE - (8 + 1 + 8 + 1)) 528 534 break; 529 535 } 530 - up_read(&fw_device_rwsem); 531 536 532 537 if (i) 533 538 buf[i - 1] = '\n'; ··· 564 571 return rcode; 565 572 } 566 573 567 - #define MAX_CONFIG_ROM_SIZE 256 574 + // By quadlet unit. 575 + #define MAX_CONFIG_ROM_SIZE ((CSR_CONFIG_ROM_END - CSR_CONFIG_ROM) / sizeof(u32)) 568 576 569 577 /* 570 578 * Read the bus info block, perform a speed probe, and read all of the rest of ··· 723 729 goto out; 724 730 } 725 731 726 - down_write(&fw_device_rwsem); 727 - device->config_rom = new_rom; 728 - device->config_rom_length = length; 729 - up_write(&fw_device_rwsem); 732 + scoped_guard(rwsem_write, &fw_device_rwsem) { 733 + device->config_rom = new_rom; 734 + device->config_rom_length = length; 735 + } 730 736 731 737 kfree(old_rom); 732 738 ret = RCODE_COMPLETE; ··· 807 813 808 814 /* 809 815 * fw_device_rwsem acts as dual purpose mutex: 810 - * - serializes accesses to fw_device_idr, 811 816 * - serializes accesses to fw_device.config_rom/.config_rom_length and 812 817 * fw_unit.directory, unless those accesses happen at safe occasions 813 818 */ 814 819 DECLARE_RWSEM(fw_device_rwsem); 815 820 816 - DEFINE_IDR(fw_device_idr); 821 + DEFINE_XARRAY_ALLOC(fw_device_xa); 817 822 int fw_cdev_major; 818 823 819 824 struct fw_device *fw_device_get_by_devt(dev_t devt) 820 825 { 821 826 struct fw_device *device; 822 827 823 - down_read(&fw_device_rwsem); 824 - device = idr_find(&fw_device_idr, MINOR(devt)); 828 + device = xa_load(&fw_device_xa, MINOR(devt)); 825 829 if (device) 826 830 fw_device_get(device); 827 - up_read(&fw_device_rwsem); 828 831 829 832 return device; 830 833 } ··· 855 864 { 856 865 struct fw_device *device = 857 866 container_of(work, struct fw_device, work.work); 858 - int minor = MINOR(device->device.devt); 859 867 860 868 if (time_before64(get_jiffies_64(), 861 869 device->card->reset_jiffies + SHUTDOWN_DELAY) ··· 872 882 device_for_each_child(&device->device, NULL, shutdown_unit); 873 883 device_unregister(&device->device); 874 884 875 - down_write(&fw_device_rwsem); 876 - idr_remove(&fw_device_idr, minor); 877 - up_write(&fw_device_rwsem); 885 + xa_erase(&fw_device_xa, MINOR(device->device.devt)); 878 886 879 887 fw_device_put(device); 880 888 } ··· 881 893 { 882 894 struct fw_device *device = fw_device(dev); 883 895 struct fw_card *card = device->card; 884 - unsigned long flags; 885 896 886 897 /* 887 898 * Take the card lock so we don't set this to NULL while a 888 899 * FW_NODE_UPDATED callback is being handled or while the 889 900 * bus manager work looks at this node. 890 901 */ 891 - spin_lock_irqsave(&card->lock, flags); 892 - device->node->data = NULL; 893 - spin_unlock_irqrestore(&card->lock, flags); 902 + scoped_guard(spinlock_irqsave, &card->lock) 903 + device->node->data = NULL; 894 904 895 905 fw_node_put(device->node); 896 906 kfree(device->config_rom); ··· 926 940 927 941 fw_device_cdev_update(device); 928 942 device_for_each_child(&device->device, NULL, update_unit); 929 - } 930 - 931 - /* 932 - * If a device was pending for deletion because its node went away but its 933 - * bus info block and root directory header matches that of a newly discovered 934 - * device, revive the existing fw_device. 935 - * The newly allocated fw_device becomes obsolete instead. 936 - */ 937 - static int lookup_existing_device(struct device *dev, void *data) 938 - { 939 - struct fw_device *old = fw_device(dev); 940 - struct fw_device *new = data; 941 - struct fw_card *card = new->card; 942 - int match = 0; 943 - 944 - if (!is_fw_device(dev)) 945 - return 0; 946 - 947 - down_read(&fw_device_rwsem); /* serialize config_rom access */ 948 - spin_lock_irq(&card->lock); /* serialize node access */ 949 - 950 - if (memcmp(old->config_rom, new->config_rom, 6 * 4) == 0 && 951 - atomic_cmpxchg(&old->state, 952 - FW_DEVICE_GONE, 953 - FW_DEVICE_RUNNING) == FW_DEVICE_GONE) { 954 - struct fw_node *current_node = new->node; 955 - struct fw_node *obsolete_node = old->node; 956 - 957 - new->node = obsolete_node; 958 - new->node->data = new; 959 - old->node = current_node; 960 - old->node->data = old; 961 - 962 - old->max_speed = new->max_speed; 963 - old->node_id = current_node->node_id; 964 - smp_wmb(); /* update node_id before generation */ 965 - old->generation = card->generation; 966 - old->config_rom_retries = 0; 967 - fw_notice(card, "rediscovered device %s\n", dev_name(dev)); 968 - 969 - old->workfn = fw_device_update; 970 - fw_schedule_device_work(old, 0); 971 - 972 - if (current_node == card->root_node) 973 - fw_schedule_bm_work(card, 0); 974 - 975 - match = 1; 976 - } 977 - 978 - spin_unlock_irq(&card->lock); 979 - up_read(&fw_device_rwsem); 980 - 981 - return match; 982 943 } 983 944 984 945 enum { BC_UNKNOWN = 0, BC_UNIMPLEMENTED, BC_IMPLEMENTED, }; ··· 988 1055 return 0; 989 1056 } 990 1057 1058 + static int compare_configuration_rom(struct device *dev, void *data) 1059 + { 1060 + const struct fw_device *old = fw_device(dev); 1061 + const u32 *config_rom = data; 1062 + 1063 + if (!is_fw_device(dev)) 1064 + return 0; 1065 + 1066 + // Compare the bus information block and root_length/root_crc. 1067 + return !memcmp(old->config_rom, config_rom, 6 * 4); 1068 + } 1069 + 991 1070 static void fw_device_init(struct work_struct *work) 992 1071 { 993 1072 struct fw_device *device = 994 1073 container_of(work, struct fw_device, work.work); 995 1074 struct fw_card *card = device->card; 996 - struct device *revived_dev; 997 - int minor, ret; 1075 + struct device *found; 1076 + u32 minor; 1077 + int ret; 998 1078 999 1079 /* 1000 1080 * All failure paths here set node->data to NULL, so that we ··· 1033 1087 return; 1034 1088 } 1035 1089 1036 - revived_dev = device_find_child(card->device, 1037 - device, lookup_existing_device); 1038 - if (revived_dev) { 1039 - put_device(revived_dev); 1040 - fw_device_release(&device->device); 1090 + // If a device was pending for deletion because its node went away but its bus info block 1091 + // and root directory header matches that of a newly discovered device, revive the 1092 + // existing fw_device. The newly allocated fw_device becomes obsolete instead. 1093 + // 1094 + // serialize config_rom access. 1095 + scoped_guard(rwsem_read, &fw_device_rwsem) { 1096 + found = device_find_child(card->device, (void *)device->config_rom, 1097 + compare_configuration_rom); 1098 + } 1099 + if (found) { 1100 + struct fw_device *reused = fw_device(found); 1041 1101 1042 - return; 1102 + if (atomic_cmpxchg(&reused->state, 1103 + FW_DEVICE_GONE, 1104 + FW_DEVICE_RUNNING) == FW_DEVICE_GONE) { 1105 + // serialize node access 1106 + scoped_guard(spinlock_irq, &card->lock) { 1107 + struct fw_node *current_node = device->node; 1108 + struct fw_node *obsolete_node = reused->node; 1109 + 1110 + device->node = obsolete_node; 1111 + device->node->data = device; 1112 + reused->node = current_node; 1113 + reused->node->data = reused; 1114 + 1115 + reused->max_speed = device->max_speed; 1116 + reused->node_id = current_node->node_id; 1117 + smp_wmb(); /* update node_id before generation */ 1118 + reused->generation = card->generation; 1119 + reused->config_rom_retries = 0; 1120 + fw_notice(card, "rediscovered device %s\n", 1121 + dev_name(found)); 1122 + 1123 + reused->workfn = fw_device_update; 1124 + fw_schedule_device_work(reused, 0); 1125 + 1126 + if (current_node == card->root_node) 1127 + fw_schedule_bm_work(card, 0); 1128 + } 1129 + 1130 + put_device(found); 1131 + fw_device_release(&device->device); 1132 + 1133 + return; 1134 + } 1135 + 1136 + put_device(found); 1043 1137 } 1044 1138 1045 1139 device_initialize(&device->device); 1046 1140 1047 1141 fw_device_get(device); 1048 - down_write(&fw_device_rwsem); 1049 - minor = idr_alloc(&fw_device_idr, device, 0, 1 << MINORBITS, 1050 - GFP_KERNEL); 1051 - up_write(&fw_device_rwsem); 1052 1142 1053 - if (minor < 0) 1143 + // The index of allocated entry is used for minor identifier of device node. 1144 + ret = xa_alloc(&fw_device_xa, &minor, device, XA_LIMIT(0, MINORMASK), GFP_KERNEL); 1145 + if (ret < 0) 1054 1146 goto error; 1055 1147 1056 1148 device->device.bus = &fw_bus_type; ··· 1149 1165 return; 1150 1166 1151 1167 error_with_cdev: 1152 - down_write(&fw_device_rwsem); 1153 - idr_remove(&fw_device_idr, minor); 1154 - up_write(&fw_device_rwsem); 1168 + xa_erase(&fw_device_xa, minor); 1155 1169 error: 1156 - fw_device_put(device); /* fw_device_idr's reference */ 1170 + fw_device_put(device); // fw_device_xa's reference. 1157 1171 1158 1172 put_device(&device->device); /* our reference */ 1159 1173 }
+44 -5
drivers/firewire/core-iso.c
··· 209 209 } 210 210 EXPORT_SYMBOL(fw_iso_context_queue_flush); 211 211 212 + /** 213 + * fw_iso_context_flush_completions() - process isochronous context in current process context. 214 + * @ctx: the isochronous context 215 + * 216 + * Process the isochronous context in the current process context. The registered callback function 217 + * is called when a queued packet buffer with the interrupt flag is completed, either after 218 + * transmission in the IT context or after being filled in the IR context. Additionally, the 219 + * callback function is also called for the packet buffer completed at last. Furthermore, the 220 + * callback function is called as well when the header buffer in the context becomes full. If it is 221 + * required to process the context asynchronously, fw_iso_context_schedule_flush_completions() is 222 + * available instead. 223 + * 224 + * Context: Process context. May sleep due to disable_work_sync(). 225 + */ 212 226 int fw_iso_context_flush_completions(struct fw_iso_context *ctx) 213 227 { 228 + int err; 229 + 214 230 trace_isoc_outbound_flush_completions(ctx); 215 231 trace_isoc_inbound_single_flush_completions(ctx); 216 232 trace_isoc_inbound_multiple_flush_completions(ctx); 217 233 218 - return ctx->card->driver->flush_iso_completions(ctx); 234 + might_sleep(); 235 + 236 + // Avoid dead lock due to programming mistake. 237 + if (WARN_ON_ONCE(current_work() == &ctx->work)) 238 + return 0; 239 + 240 + disable_work_sync(&ctx->work); 241 + 242 + err = ctx->card->driver->flush_iso_completions(ctx); 243 + 244 + enable_work(&ctx->work); 245 + 246 + return err; 219 247 } 220 248 EXPORT_SYMBOL(fw_iso_context_flush_completions); 221 249 222 250 int fw_iso_context_stop(struct fw_iso_context *ctx) 223 251 { 252 + int err; 253 + 224 254 trace_isoc_outbound_stop(ctx); 225 255 trace_isoc_inbound_single_stop(ctx); 226 256 trace_isoc_inbound_multiple_stop(ctx); 227 257 228 - return ctx->card->driver->stop_iso(ctx); 258 + might_sleep(); 259 + 260 + // Avoid dead lock due to programming mistake. 261 + if (WARN_ON_ONCE(current_work() == &ctx->work)) 262 + return 0; 263 + 264 + err = ctx->card->driver->stop_iso(ctx); 265 + 266 + cancel_work_sync(&ctx->work); 267 + 268 + return err; 229 269 } 230 270 EXPORT_SYMBOL(fw_iso_context_stop); 231 271 ··· 415 375 u32 channels_lo = channels_mask >> 32; /* channels 63...32 */ 416 376 int irm_id, ret, c = -EINVAL; 417 377 418 - spin_lock_irq(&card->lock); 419 - irm_id = card->irm_node->node_id; 420 - spin_unlock_irq(&card->lock); 378 + scoped_guard(spinlock_irq, &card->lock) 379 + irm_id = card->irm_node->node_id; 421 380 422 381 if (channels_hi) 423 382 c = manage_channel(card, irm_id, generation, channels_hi,
+2 -5
drivers/firewire/core-topology.c
··· 39 39 node->initiated_reset = phy_packet_self_id_zero_get_initiated_reset(sid); 40 40 node->port_count = port_count; 41 41 42 - refcount_set(&node->ref_count, 1); 42 + kref_init(&node->kref); 43 43 INIT_LIST_HEAD(&node->link); 44 44 45 45 return node; ··· 455 455 int self_id_count, u32 *self_ids, bool bm_abdicate) 456 456 { 457 457 struct fw_node *local_node; 458 - unsigned long flags; 459 458 460 459 trace_bus_reset_handle(card->index, generation, node_id, bm_abdicate, self_ids, self_id_count); 461 460 462 - spin_lock_irqsave(&card->lock, flags); 461 + guard(spinlock_irqsave)(&card->lock); 463 462 464 463 /* 465 464 * If the selfID buffer is not the immediate successor of the ··· 499 500 } else { 500 501 update_tree(card, local_node); 501 502 } 502 - 503 - spin_unlock_irqrestore(&card->lock, flags); 504 503 } 505 504 EXPORT_SYMBOL(fw_core_handle_bus_reset);
+61 -90
drivers/firewire/core-transaction.c
··· 13 13 #include <linux/firewire-constants.h> 14 14 #include <linux/fs.h> 15 15 #include <linux/init.h> 16 - #include <linux/idr.h> 17 16 #include <linux/jiffies.h> 18 17 #include <linux/kernel.h> 19 18 #include <linux/list.h> ··· 48 49 u32 response_tstamp) 49 50 { 50 51 struct fw_transaction *t = NULL, *iter; 51 - unsigned long flags; 52 52 53 - spin_lock_irqsave(&card->lock, flags); 54 - list_for_each_entry(iter, &card->transaction_list, link) { 55 - if (iter == transaction) { 56 - if (!try_cancel_split_timeout(iter)) { 57 - spin_unlock_irqrestore(&card->lock, flags); 58 - goto timed_out; 53 + scoped_guard(spinlock_irqsave, &card->lock) { 54 + list_for_each_entry(iter, &card->transaction_list, link) { 55 + if (iter == transaction) { 56 + if (try_cancel_split_timeout(iter)) { 57 + list_del_init(&iter->link); 58 + card->tlabel_mask &= ~(1ULL << iter->tlabel); 59 + t = iter; 60 + } 61 + break; 59 62 } 60 - list_del_init(&iter->link); 61 - card->tlabel_mask &= ~(1ULL << iter->tlabel); 62 - t = iter; 63 - break; 64 63 } 65 64 } 66 - spin_unlock_irqrestore(&card->lock, flags); 67 65 68 - if (t) { 69 - if (!t->with_tstamp) { 70 - t->callback.without_tstamp(card, rcode, NULL, 0, t->callback_data); 71 - } else { 72 - t->callback.with_tstamp(card, rcode, t->packet.timestamp, response_tstamp, 73 - NULL, 0, t->callback_data); 74 - } 75 - return 0; 66 + if (!t) 67 + return -ENOENT; 68 + 69 + if (!t->with_tstamp) { 70 + t->callback.without_tstamp(card, rcode, NULL, 0, t->callback_data); 71 + } else { 72 + t->callback.with_tstamp(card, rcode, t->packet.timestamp, response_tstamp, NULL, 0, 73 + t->callback_data); 76 74 } 77 75 78 - timed_out: 79 - return -ENOENT; 76 + return 0; 80 77 } 81 78 82 79 /* ··· 116 121 { 117 122 struct fw_transaction *t = from_timer(t, timer, split_timeout_timer); 118 123 struct fw_card *card = t->card; 119 - unsigned long flags; 120 124 121 - spin_lock_irqsave(&card->lock, flags); 122 - if (list_empty(&t->link)) { 123 - spin_unlock_irqrestore(&card->lock, flags); 124 - return; 125 + scoped_guard(spinlock_irqsave, &card->lock) { 126 + if (list_empty(&t->link)) 127 + return; 128 + list_del(&t->link); 129 + card->tlabel_mask &= ~(1ULL << t->tlabel); 125 130 } 126 - list_del(&t->link); 127 - card->tlabel_mask &= ~(1ULL << t->tlabel); 128 - spin_unlock_irqrestore(&card->lock, flags); 129 131 130 132 if (!t->with_tstamp) { 131 133 t->callback.without_tstamp(card, RCODE_CANCELLED, NULL, 0, t->callback_data); ··· 135 143 static void start_split_transaction_timeout(struct fw_transaction *t, 136 144 struct fw_card *card) 137 145 { 138 - unsigned long flags; 146 + guard(spinlock_irqsave)(&card->lock); 139 147 140 - spin_lock_irqsave(&card->lock, flags); 141 - 142 - if (list_empty(&t->link) || WARN_ON(t->is_split_transaction)) { 143 - spin_unlock_irqrestore(&card->lock, flags); 148 + if (list_empty(&t->link) || WARN_ON(t->is_split_transaction)) 144 149 return; 145 - } 146 150 147 151 t->is_split_transaction = true; 148 152 mod_timer(&t->split_timeout_timer, 149 153 jiffies + card->split_timeout_jiffies); 150 - 151 - spin_unlock_irqrestore(&card->lock, flags); 152 154 } 153 155 154 156 static u32 compute_split_timeout_timestamp(struct fw_card *card, u32 request_timestamp); ··· 450 464 451 465 static struct fw_packet phy_config_packet = { 452 466 .header_length = 12, 453 - .header[0] = TCODE_LINK_INTERNAL << 4, 454 467 .payload_length = 0, 455 468 .speed = SCODE_100, 456 469 .callback = transmit_phy_packet_callback, ··· 480 495 phy_packet_phy_config_set_gap_count(&data, gap_count); 481 496 phy_packet_phy_config_set_gap_count_optimization(&data, true); 482 497 483 - mutex_lock(&phy_config_mutex); 498 + guard(mutex)(&phy_config_mutex); 484 499 500 + async_header_set_tcode(phy_config_packet.header, TCODE_LINK_INTERNAL); 485 501 phy_config_packet.header[1] = data; 486 502 phy_config_packet.header[2] = ~data; 487 503 phy_config_packet.generation = generation; ··· 494 508 495 509 card->driver->send_request(card, &phy_config_packet); 496 510 wait_for_completion_timeout(&phy_config_done, timeout); 497 - 498 - mutex_unlock(&phy_config_mutex); 499 511 } 500 512 501 513 static struct fw_address_handler *lookup_overlapping_address_handler( ··· 582 598 handler->length == 0) 583 599 return -EINVAL; 584 600 585 - spin_lock(&address_handler_list_lock); 601 + guard(spinlock)(&address_handler_list_lock); 586 602 587 603 handler->offset = region->start; 588 604 while (handler->offset + handler->length <= region->end) { ··· 601 617 } 602 618 } 603 619 604 - spin_unlock(&address_handler_list_lock); 605 - 606 620 return ret; 607 621 } 608 622 EXPORT_SYMBOL(fw_core_add_address_handler); ··· 616 634 */ 617 635 void fw_core_remove_address_handler(struct fw_address_handler *handler) 618 636 { 619 - spin_lock(&address_handler_list_lock); 620 - list_del_rcu(&handler->link); 621 - spin_unlock(&address_handler_list_lock); 637 + scoped_guard(spinlock, &address_handler_list_lock) 638 + list_del_rcu(&handler->link); 639 + 622 640 synchronize_rcu(); 623 641 } 624 642 EXPORT_SYMBOL(fw_core_remove_address_handler); ··· 909 927 if (tcode == TCODE_LOCK_REQUEST) 910 928 tcode = 0x10 + async_header_get_extended_tcode(p->header); 911 929 912 - rcu_read_lock(); 913 - handler = lookup_enclosing_address_handler(&address_handler_list, 914 - offset, request->length); 915 - if (handler) 916 - handler->address_callback(card, request, 917 - tcode, destination, source, 918 - p->generation, offset, 919 - request->data, request->length, 920 - handler->callback_data); 921 - rcu_read_unlock(); 930 + scoped_guard(rcu) { 931 + handler = lookup_enclosing_address_handler(&address_handler_list, offset, 932 + request->length); 933 + if (handler) 934 + handler->address_callback(card, request, tcode, destination, source, 935 + p->generation, offset, request->data, 936 + request->length, handler->callback_data); 937 + } 922 938 923 939 if (!handler) 924 940 fw_send_response(card, request, RCODE_ADDRESS_ERROR); ··· 949 969 return; 950 970 } 951 971 952 - rcu_read_lock(); 953 - list_for_each_entry_rcu(handler, &address_handler_list, link) { 954 - if (is_enclosing_handler(handler, offset, request->length)) 955 - handler->address_callback(card, request, tcode, 956 - destination, source, 957 - p->generation, offset, 958 - request->data, 959 - request->length, 960 - handler->callback_data); 972 + scoped_guard(rcu) { 973 + list_for_each_entry_rcu(handler, &address_handler_list, link) { 974 + if (is_enclosing_handler(handler, offset, request->length)) 975 + handler->address_callback(card, request, tcode, destination, source, 976 + p->generation, offset, request->data, 977 + request->length, handler->callback_data); 978 + } 961 979 } 962 - rcu_read_unlock(); 963 980 964 981 fw_send_response(card, request, RCODE_COMPLETE); 965 982 } ··· 1001 1024 void fw_core_handle_response(struct fw_card *card, struct fw_packet *p) 1002 1025 { 1003 1026 struct fw_transaction *t = NULL, *iter; 1004 - unsigned long flags; 1005 1027 u32 *data; 1006 1028 size_t data_length; 1007 1029 int tcode, tlabel, source, rcode; ··· 1039 1063 break; 1040 1064 } 1041 1065 1042 - spin_lock_irqsave(&card->lock, flags); 1043 - list_for_each_entry(iter, &card->transaction_list, link) { 1044 - if (iter->node_id == source && iter->tlabel == tlabel) { 1045 - if (!try_cancel_split_timeout(iter)) { 1046 - spin_unlock_irqrestore(&card->lock, flags); 1047 - goto timed_out; 1066 + scoped_guard(spinlock_irqsave, &card->lock) { 1067 + list_for_each_entry(iter, &card->transaction_list, link) { 1068 + if (iter->node_id == source && iter->tlabel == tlabel) { 1069 + if (try_cancel_split_timeout(iter)) { 1070 + list_del_init(&iter->link); 1071 + card->tlabel_mask &= ~(1ULL << iter->tlabel); 1072 + t = iter; 1073 + } 1074 + break; 1048 1075 } 1049 - list_del_init(&iter->link); 1050 - card->tlabel_mask &= ~(1ULL << iter->tlabel); 1051 - t = iter; 1052 - break; 1053 1076 } 1054 1077 } 1055 - spin_unlock_irqrestore(&card->lock, flags); 1056 1078 1057 1079 trace_async_response_inbound((uintptr_t)t, card->index, p->generation, p->speed, p->ack, 1058 1080 p->timestamp, p->header, data, data_length / 4); 1059 1081 1060 1082 if (!t) { 1061 - timed_out: 1062 1083 fw_notice(card, "unsolicited response (source %x, tlabel %x)\n", 1063 1084 source, tlabel); 1064 1085 return; ··· 1159 1186 int reg = offset & ~CSR_REGISTER_BASE; 1160 1187 __be32 *data = payload; 1161 1188 int rcode = RCODE_COMPLETE; 1162 - unsigned long flags; 1163 1189 1164 1190 switch (reg) { 1165 1191 case CSR_PRIORITY_BUDGET: ··· 1200 1228 if (tcode == TCODE_READ_QUADLET_REQUEST) { 1201 1229 *data = cpu_to_be32(card->split_timeout_hi); 1202 1230 } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) { 1203 - spin_lock_irqsave(&card->lock, flags); 1231 + guard(spinlock_irqsave)(&card->lock); 1232 + 1204 1233 card->split_timeout_hi = be32_to_cpu(*data) & 7; 1205 1234 update_split_timeout(card); 1206 - spin_unlock_irqrestore(&card->lock, flags); 1207 1235 } else { 1208 1236 rcode = RCODE_TYPE_ERROR; 1209 1237 } ··· 1213 1241 if (tcode == TCODE_READ_QUADLET_REQUEST) { 1214 1242 *data = cpu_to_be32(card->split_timeout_lo); 1215 1243 } else if (tcode == TCODE_WRITE_QUADLET_REQUEST) { 1216 - spin_lock_irqsave(&card->lock, flags); 1217 - card->split_timeout_lo = 1218 - be32_to_cpu(*data) & 0xfff80000; 1244 + guard(spinlock_irqsave)(&card->lock); 1245 + 1246 + card->split_timeout_lo = be32_to_cpu(*data) & 0xfff80000; 1219 1247 update_split_timeout(card); 1220 - spin_unlock_irqrestore(&card->lock, flags); 1221 1248 } else { 1222 1249 rcode = RCODE_TYPE_ERROR; 1223 1250 } ··· 1358 1387 unregister_chrdev(fw_cdev_major, "firewire"); 1359 1388 bus_unregister(&fw_bus_type); 1360 1389 destroy_workqueue(fw_workqueue); 1361 - idr_destroy(&fw_device_idr); 1390 + xa_destroy(&fw_device_xa); 1362 1391 } 1363 1392 1364 1393 module_init(fw_core_init);
+20 -8
drivers/firewire/core.h
··· 7 7 #include <linux/dma-mapping.h> 8 8 #include <linux/fs.h> 9 9 #include <linux/list.h> 10 - #include <linux/idr.h> 10 + #include <linux/xarray.h> 11 11 #include <linux/mm_types.h> 12 12 #include <linux/rwsem.h> 13 13 #include <linux/slab.h> ··· 115 115 116 116 void fw_card_initialize(struct fw_card *card, 117 117 const struct fw_card_driver *driver, struct device *device); 118 - int fw_card_add(struct fw_card *card, 119 - u32 max_receive, u32 link_speed, u64 guid); 118 + int fw_card_add(struct fw_card *card, u32 max_receive, u32 link_speed, u64 guid, 119 + unsigned int supported_isoc_contexts); 120 120 void fw_core_remove_card(struct fw_card *card); 121 121 int fw_compute_block_crc(__be32 *block); 122 122 void fw_schedule_bm_work(struct fw_card *card, unsigned long delay); ··· 133 133 /* -device */ 134 134 135 135 extern struct rw_semaphore fw_device_rwsem; 136 - extern struct idr fw_device_idr; 136 + extern struct xarray fw_device_xa; 137 137 extern int fw_cdev_major; 138 138 139 139 static inline struct fw_device *fw_device_get(struct fw_device *device) ··· 159 159 int fw_iso_buffer_map_dma(struct fw_iso_buffer *buffer, struct fw_card *card, 160 160 enum dma_data_direction direction); 161 161 162 + static inline void fw_iso_context_init_work(struct fw_iso_context *ctx, work_func_t func) 163 + { 164 + INIT_WORK(&ctx->work, func); 165 + } 166 + 162 167 163 168 /* -topology */ 164 169 ··· 188 183 * local node to this node. */ 189 184 u8 max_depth:4; /* Maximum depth to any leaf node */ 190 185 u8 max_hops:4; /* Max hops in this sub tree */ 191 - refcount_t ref_count; 186 + 187 + struct kref kref; 192 188 193 189 /* For serializing node topology into a list. */ 194 190 struct list_head link; ··· 202 196 203 197 static inline struct fw_node *fw_node_get(struct fw_node *node) 204 198 { 205 - refcount_inc(&node->ref_count); 199 + kref_get(&node->kref); 206 200 207 201 return node; 208 202 } 209 203 204 + static void release_node(struct kref *kref) 205 + { 206 + struct fw_node *node = container_of(kref, struct fw_node, kref); 207 + 208 + kfree(node); 209 + } 210 + 210 211 static inline void fw_node_put(struct fw_node *node) 211 212 { 212 - if (refcount_dec_and_test(&node->ref_count)) 213 - kfree(node); 213 + kref_put(&node->kref, release_node); 214 214 } 215 215 216 216 void fw_core_handle_bus_reset(struct fw_card *card, int node_id,
+66
drivers/firewire/ohci-serdes-test.c
··· 40 40 KUNIT_EXPECT_EQ(test, 0xf38b, timestamp); 41 41 } 42 42 43 + static void test_at_data_serdes(struct kunit *test) 44 + { 45 + static const __le32 expected[] = { 46 + cpu_to_le32(0x00020e80), 47 + cpu_to_le32(0xffc2ffff), 48 + cpu_to_le32(0xe0000000), 49 + }; 50 + __le32 quadlets[] = {0, 0, 0}; 51 + bool has_src_bus_id = ohci1394_at_data_get_src_bus_id(expected); 52 + unsigned int speed = ohci1394_at_data_get_speed(expected); 53 + unsigned int tlabel = ohci1394_at_data_get_tlabel(expected); 54 + unsigned int retry = ohci1394_at_data_get_retry(expected); 55 + unsigned int tcode = ohci1394_at_data_get_tcode(expected); 56 + unsigned int destination_id = ohci1394_at_data_get_destination_id(expected); 57 + u64 destination_offset = ohci1394_at_data_get_destination_offset(expected); 58 + 59 + KUNIT_EXPECT_FALSE(test, has_src_bus_id); 60 + KUNIT_EXPECT_EQ(test, 0x02, speed); 61 + KUNIT_EXPECT_EQ(test, 0x03, tlabel); 62 + KUNIT_EXPECT_EQ(test, 0x02, retry); 63 + KUNIT_EXPECT_EQ(test, 0x08, tcode); 64 + 65 + ohci1394_at_data_set_src_bus_id(quadlets, has_src_bus_id); 66 + ohci1394_at_data_set_speed(quadlets, speed); 67 + ohci1394_at_data_set_tlabel(quadlets, tlabel); 68 + ohci1394_at_data_set_retry(quadlets, retry); 69 + ohci1394_at_data_set_tcode(quadlets, tcode); 70 + ohci1394_at_data_set_destination_id(quadlets, destination_id); 71 + ohci1394_at_data_set_destination_offset(quadlets, destination_offset); 72 + 73 + KUNIT_EXPECT_MEMEQ(test, quadlets, expected, sizeof(expected)); 74 + } 75 + 76 + static void test_it_data_serdes(struct kunit *test) 77 + { 78 + static const __le32 expected[] = { 79 + cpu_to_le32(0x000349a7), 80 + cpu_to_le32(0x02300000), 81 + }; 82 + __le32 quadlets[] = {0, 0}; 83 + unsigned int scode = ohci1394_it_data_get_speed(expected); 84 + unsigned int tag = ohci1394_it_data_get_tag(expected); 85 + unsigned int channel = ohci1394_it_data_get_channel(expected); 86 + unsigned int tcode = ohci1394_it_data_get_tcode(expected); 87 + unsigned int sync = ohci1394_it_data_get_sync(expected); 88 + unsigned int data_length = ohci1394_it_data_get_data_length(expected); 89 + 90 + KUNIT_EXPECT_EQ(test, 0x03, scode); 91 + KUNIT_EXPECT_EQ(test, 0x01, tag); 92 + KUNIT_EXPECT_EQ(test, 0x09, channel); 93 + KUNIT_EXPECT_EQ(test, 0x0a, tcode); 94 + KUNIT_EXPECT_EQ(test, 0x7, sync); 95 + KUNIT_EXPECT_EQ(test, 0x0230, data_length); 96 + 97 + ohci1394_it_data_set_speed(quadlets, scode); 98 + ohci1394_it_data_set_tag(quadlets, tag); 99 + ohci1394_it_data_set_channel(quadlets, channel); 100 + ohci1394_it_data_set_tcode(quadlets, tcode); 101 + ohci1394_it_data_set_sync(quadlets, sync); 102 + ohci1394_it_data_set_data_length(quadlets, data_length); 103 + 104 + KUNIT_EXPECT_MEMEQ(test, quadlets, expected, sizeof(expected)); 105 + } 106 + 43 107 static struct kunit_case ohci_serdes_test_cases[] = { 44 108 KUNIT_CASE(test_self_id_count_register_deserialization), 45 109 KUNIT_CASE(test_self_id_receive_buffer_deserialization), 110 + KUNIT_CASE(test_at_data_serdes), 111 + KUNIT_CASE(test_it_data_serdes), 46 112 {} 47 113 }; 48 114
+273 -301
drivers/firewire/ohci.c
··· 50 50 #define CREATE_TRACE_POINTS 51 51 #include <trace/events/firewire_ohci.h> 52 52 53 - #define ohci_info(ohci, f, args...) dev_info(ohci->card.device, f, ##args) 54 53 #define ohci_notice(ohci, f, args...) dev_notice(ohci->card.device, f, ##args) 55 54 #define ohci_err(ohci, f, args...) dev_err(ohci->card.device, f, ##args) 56 55 ··· 76 77 __le32 branch_address; 77 78 __le16 res_count; 78 79 __le16 transfer_status; 79 - } __attribute__((aligned(16))); 80 + } __aligned(16); 80 81 81 82 #define CONTROL_SET(regs) (regs) 82 83 #define CONTROL_CLEAR(regs) ((regs) + 4) ··· 161 162 struct tasklet_struct tasklet; 162 163 }; 163 164 164 - #define IT_HEADER_SY(v) ((v) << 0) 165 - #define IT_HEADER_TCODE(v) ((v) << 4) 166 - #define IT_HEADER_CHANNEL(v) ((v) << 8) 167 - #define IT_HEADER_TAG(v) ((v) << 14) 168 - #define IT_HEADER_SPEED(v) ((v) << 16) 169 - #define IT_HEADER_DATA_LENGTH(v) ((v) << 16) 170 - 171 165 struct iso_context { 172 166 struct fw_iso_context base; 173 167 struct context context; ··· 174 182 u8 tags; 175 183 }; 176 184 177 - #define CONFIG_ROM_SIZE 1024 185 + #define CONFIG_ROM_SIZE (CSR_CONFIG_ROM_END - CSR_CONFIG_ROM) 178 186 179 187 struct fw_ohci { 180 188 struct fw_card card; ··· 256 264 #define OHCI1394_REGISTER_SIZE 0x800 257 265 #define OHCI1394_PCI_HCI_Control 0x40 258 266 #define SELF_ID_BUF_SIZE 0x800 259 - #define OHCI_TCODE_PHY_PACKET 0x0e 260 267 #define OHCI_VERSION_1_1 0x010010 261 268 262 269 static char ohci_driver_name[] = KBUILD_MODNAME; ··· 396 405 397 406 static int param_debug; 398 407 module_param_named(debug, param_debug, int, 0644); 399 - MODULE_PARM_DESC(debug, "Verbose logging (default = 0" 408 + MODULE_PARM_DESC(debug, "Verbose logging, deprecated in v6.11 kernel or later. (default = 0" 400 409 ", AT/AR events = " __stringify(OHCI_PARAM_DEBUG_AT_AR) 401 410 ", self-IDs = " __stringify(OHCI_PARAM_DEBUG_SELFIDS) 402 411 ", IRQs = " __stringify(OHCI_PARAM_DEBUG_IRQS) ··· 523 532 [0x1e] = "ack_type_error", [0x1f] = "-reserved-", 524 533 [0x20] = "pending/cancelled", 525 534 }; 526 - static const char *tcodes[] = { 527 - [0x0] = "QW req", [0x1] = "BW req", 528 - [0x2] = "W resp", [0x3] = "-reserved-", 529 - [0x4] = "QR req", [0x5] = "BR req", 530 - [0x6] = "QR resp", [0x7] = "BR resp", 531 - [0x8] = "cycle start", [0x9] = "Lk req", 532 - [0xa] = "async stream packet", [0xb] = "Lk resp", 533 - [0xc] = "-reserved-", [0xd] = "-reserved-", 534 - [0xe] = "link internal", [0xf] = "-reserved-", 535 - }; 536 535 537 536 static void log_ar_at_event(struct fw_ohci *ohci, 538 537 char dir, int speed, u32 *header, int evt) 539 538 { 539 + static const char *const tcodes[] = { 540 + [TCODE_WRITE_QUADLET_REQUEST] = "QW req", 541 + [TCODE_WRITE_BLOCK_REQUEST] = "BW req", 542 + [TCODE_WRITE_RESPONSE] = "W resp", 543 + [0x3] = "-reserved-", 544 + [TCODE_READ_QUADLET_REQUEST] = "QR req", 545 + [TCODE_READ_BLOCK_REQUEST] = "BR req", 546 + [TCODE_READ_QUADLET_RESPONSE] = "QR resp", 547 + [TCODE_READ_BLOCK_RESPONSE] = "BR resp", 548 + [TCODE_CYCLE_START] = "cycle start", 549 + [TCODE_LOCK_REQUEST] = "Lk req", 550 + [TCODE_STREAM_DATA] = "async stream packet", 551 + [TCODE_LOCK_RESPONSE] = "Lk resp", 552 + [0xc] = "-reserved-", 553 + [0xd] = "-reserved-", 554 + [TCODE_LINK_INTERNAL] = "link internal", 555 + [0xf] = "-reserved-", 556 + }; 540 557 int tcode = async_header_get_tcode(header); 541 558 char specific[12]; 542 559 ··· 585 586 ohci_notice(ohci, "A%c %s, %s\n", 586 587 dir, evts[evt], tcodes[tcode]); 587 588 break; 588 - case 0xe: 589 + case TCODE_LINK_INTERNAL: 589 590 ohci_notice(ohci, "A%c %s, PHY %08x %08x\n", 590 591 dir, evts[evt], header[1], header[2]); 591 592 break; ··· 712 713 static int ohci_read_phy_reg(struct fw_card *card, int addr) 713 714 { 714 715 struct fw_ohci *ohci = fw_ohci(card); 715 - int ret; 716 716 717 - mutex_lock(&ohci->phy_reg_mutex); 718 - ret = read_phy_reg(ohci, addr); 719 - mutex_unlock(&ohci->phy_reg_mutex); 717 + guard(mutex)(&ohci->phy_reg_mutex); 720 718 721 - return ret; 719 + return read_phy_reg(ohci, addr); 722 720 } 723 721 724 722 static int ohci_update_phy_reg(struct fw_card *card, int addr, 725 723 int clear_bits, int set_bits) 726 724 { 727 725 struct fw_ohci *ohci = fw_ohci(card); 728 - int ret; 729 726 730 - mutex_lock(&ohci->phy_reg_mutex); 731 - ret = update_phy_reg(ohci, addr, clear_bits, set_bits); 732 - mutex_unlock(&ohci->phy_reg_mutex); 727 + guard(mutex)(&ohci->phy_reg_mutex); 733 728 734 - return ret; 729 + return update_phy_reg(ohci, addr, clear_bits, set_bits); 735 730 } 736 731 737 732 static inline dma_addr_t ar_buffer_bus(struct ar_context *ctx, unsigned int i) ··· 932 939 933 940 case TCODE_WRITE_RESPONSE: 934 941 case TCODE_READ_QUADLET_REQUEST: 935 - case OHCI_TCODE_PHY_PACKET: 942 + case TCODE_LINK_INTERNAL: 936 943 p.header_length = 12; 937 944 p.payload_length = 0; 938 945 break; ··· 960 967 * Several controllers, notably from NEC and VIA, forget to 961 968 * write ack_complete status at PHY packet reception. 962 969 */ 963 - if (evt == OHCI1394_evt_no_status && tcode == OHCI1394_phy_tcode) 970 + if (evt == OHCI1394_evt_no_status && tcode == TCODE_LINK_INTERNAL) 964 971 p.ack = ACK_COMPLETE; 965 972 966 973 /* ··· 1141 1148 return d + z - 1; 1142 1149 } 1143 1150 1144 - static void context_tasklet(unsigned long data) 1151 + static void context_retire_descriptors(struct context *ctx) 1145 1152 { 1146 - struct context *ctx = (struct context *) data; 1147 1153 struct descriptor *d, *last; 1148 1154 u32 address; 1149 1155 int z; ··· 1171 1179 break; 1172 1180 1173 1181 if (old_desc != desc) { 1174 - /* If we've advanced to the next buffer, move the 1175 - * previous buffer to the free list. */ 1176 - unsigned long flags; 1182 + // If we've advanced to the next buffer, move the previous buffer to the 1183 + // free list. 1177 1184 old_desc->used = 0; 1178 - spin_lock_irqsave(&ctx->ohci->lock, flags); 1185 + guard(spinlock_irqsave)(&ctx->ohci->lock); 1179 1186 list_move_tail(&old_desc->list, &ctx->buffer_list); 1180 - spin_unlock_irqrestore(&ctx->ohci->lock, flags); 1181 1187 } 1182 1188 ctx->last = last; 1183 1189 } 1190 + } 1191 + 1192 + static void context_tasklet(unsigned long data) 1193 + { 1194 + struct context *ctx = (struct context *) data; 1195 + 1196 + context_retire_descriptors(ctx); 1197 + } 1198 + 1199 + static void ohci_isoc_context_work(struct work_struct *work) 1200 + { 1201 + struct fw_iso_context *base = container_of(work, struct fw_iso_context, work); 1202 + struct iso_context *isoc_ctx = container_of(base, struct iso_context, base); 1203 + 1204 + context_retire_descriptors(&isoc_ctx->context); 1184 1205 } 1185 1206 1186 1207 /* ··· 1407 1402 d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE); 1408 1403 d[0].res_count = cpu_to_le16(packet->timestamp); 1409 1404 1410 - /* 1411 - * The DMA format for asynchronous link packets is different 1412 - * from the IEEE1394 layout, so shift the fields around 1413 - * accordingly. 1414 - */ 1415 - 1416 1405 tcode = async_header_get_tcode(packet->header); 1417 1406 header = (__le32 *) &d[1]; 1418 1407 switch (tcode) { ··· 1419 1420 case TCODE_READ_BLOCK_RESPONSE: 1420 1421 case TCODE_LOCK_REQUEST: 1421 1422 case TCODE_LOCK_RESPONSE: 1422 - header[0] = cpu_to_le32((packet->header[0] & 0xffff) | 1423 - (packet->speed << 16)); 1424 - header[1] = cpu_to_le32((packet->header[1] & 0xffff) | 1425 - (packet->header[0] & 0xffff0000)); 1426 - header[2] = cpu_to_le32(packet->header[2]); 1423 + ohci1394_at_data_set_src_bus_id(header, false); 1424 + ohci1394_at_data_set_speed(header, packet->speed); 1425 + ohci1394_at_data_set_tlabel(header, async_header_get_tlabel(packet->header)); 1426 + ohci1394_at_data_set_retry(header, async_header_get_retry(packet->header)); 1427 + ohci1394_at_data_set_tcode(header, tcode); 1428 + 1429 + ohci1394_at_data_set_destination_id(header, 1430 + async_header_get_destination(packet->header)); 1431 + 1432 + if (ctx == &ctx->ohci->at_response_ctx) { 1433 + ohci1394_at_data_set_rcode(header, async_header_get_rcode(packet->header)); 1434 + } else { 1435 + ohci1394_at_data_set_destination_offset(header, 1436 + async_header_get_offset(packet->header)); 1437 + } 1427 1438 1428 1439 if (tcode_is_block_packet(tcode)) 1429 1440 header[3] = cpu_to_le32(packet->header[3]); ··· 1442 1433 1443 1434 d[0].req_count = cpu_to_le16(packet->header_length); 1444 1435 break; 1445 - 1446 1436 case TCODE_LINK_INTERNAL: 1447 - header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) | 1448 - (packet->speed << 16)); 1437 + ohci1394_at_data_set_speed(header, packet->speed); 1438 + ohci1394_at_data_set_tcode(header, TCODE_LINK_INTERNAL); 1439 + 1449 1440 header[1] = cpu_to_le32(packet->header[1]); 1450 1441 header[2] = cpu_to_le32(packet->header[2]); 1451 1442 d[0].req_count = cpu_to_le16(12); ··· 1455 1446 break; 1456 1447 1457 1448 case TCODE_STREAM_DATA: 1458 - header[0] = cpu_to_le32((packet->header[0] & 0xffff) | 1459 - (packet->speed << 16)); 1460 - header[1] = cpu_to_le32(packet->header[0] & 0xffff0000); 1449 + ohci1394_it_data_set_speed(header, packet->speed); 1450 + ohci1394_it_data_set_tag(header, isoc_header_get_tag(packet->header[0])); 1451 + ohci1394_it_data_set_channel(header, isoc_header_get_channel(packet->header[0])); 1452 + ohci1394_it_data_set_tcode(header, TCODE_STREAM_DATA); 1453 + ohci1394_it_data_set_sync(header, isoc_header_get_sy(packet->header[0])); 1454 + 1455 + ohci1394_it_data_set_data_length(header, isoc_header_get_data_length(packet->header[0])); 1456 + 1461 1457 d[0].req_count = cpu_to_le16(8); 1462 1458 break; 1463 1459 ··· 1887 1873 { 1888 1874 int reg; 1889 1875 1890 - mutex_lock(&ohci->phy_reg_mutex); 1891 - reg = write_phy_reg(ohci, 7, port_index); 1892 - if (reg >= 0) 1876 + scoped_guard(mutex, &ohci->phy_reg_mutex) { 1877 + reg = write_phy_reg(ohci, 7, port_index); 1878 + if (reg < 0) 1879 + return reg; 1880 + 1893 1881 reg = read_phy_reg(ohci, 8); 1894 - mutex_unlock(&ohci->phy_reg_mutex); 1895 - if (reg < 0) 1896 - return reg; 1882 + if (reg < 0) 1883 + return reg; 1884 + } 1897 1885 1898 1886 switch (reg & 0x0f) { 1899 1887 case 0x06: ··· 1933 1917 return i; 1934 1918 } 1935 1919 1936 - static bool initiated_reset(struct fw_ohci *ohci) 1920 + static int detect_initiated_reset(struct fw_ohci *ohci, bool *is_initiated_reset) 1937 1921 { 1938 1922 int reg; 1939 - int ret = false; 1940 1923 1941 - mutex_lock(&ohci->phy_reg_mutex); 1942 - reg = write_phy_reg(ohci, 7, 0xe0); /* Select page 7 */ 1943 - if (reg >= 0) { 1944 - reg = read_phy_reg(ohci, 8); 1945 - reg |= 0x40; 1946 - reg = write_phy_reg(ohci, 8, reg); /* set PMODE bit */ 1947 - if (reg >= 0) { 1948 - reg = read_phy_reg(ohci, 12); /* read register 12 */ 1949 - if (reg >= 0) { 1950 - if ((reg & 0x08) == 0x08) { 1951 - /* bit 3 indicates "initiated reset" */ 1952 - ret = true; 1953 - } 1954 - } 1955 - } 1956 - } 1957 - mutex_unlock(&ohci->phy_reg_mutex); 1958 - return ret; 1924 + guard(mutex)(&ohci->phy_reg_mutex); 1925 + 1926 + // Select page 7 1927 + reg = write_phy_reg(ohci, 7, 0xe0); 1928 + if (reg < 0) 1929 + return reg; 1930 + 1931 + reg = read_phy_reg(ohci, 8); 1932 + if (reg < 0) 1933 + return reg; 1934 + 1935 + // set PMODE bit 1936 + reg |= 0x40; 1937 + reg = write_phy_reg(ohci, 8, reg); 1938 + if (reg < 0) 1939 + return reg; 1940 + 1941 + // read register 12 1942 + reg = read_phy_reg(ohci, 12); 1943 + if (reg < 0) 1944 + return reg; 1945 + 1946 + // bit 3 indicates "initiated reset" 1947 + *is_initiated_reset = !!((reg & 0x08) == 0x08); 1948 + 1949 + return 0; 1959 1950 } 1960 1951 1961 1952 /* ··· 1972 1949 */ 1973 1950 static int find_and_insert_self_id(struct fw_ohci *ohci, int self_id_count) 1974 1951 { 1975 - int reg, i, pos; 1952 + int reg, i, pos, err; 1953 + bool is_initiated_reset; 1976 1954 u32 self_id = 0; 1977 1955 1978 1956 // link active 1, speed 3, bridge 0, contender 1, more packets 0. ··· 2002 1978 2003 1979 for (i = 0; i < 3; i++) { 2004 1980 enum phy_packet_self_id_port_status status; 2005 - int err; 2006 1981 2007 1982 err = get_status_for_port(ohci, i, &status); 2008 1983 if (err < 0) ··· 2010 1987 self_id_sequence_set_port_status(&self_id, 1, i, status); 2011 1988 } 2012 1989 2013 - phy_packet_self_id_zero_set_initiated_reset(&self_id, initiated_reset(ohci)); 1990 + err = detect_initiated_reset(ohci, &is_initiated_reset); 1991 + if (err < 0) 1992 + return err; 1993 + phy_packet_self_id_zero_set_initiated_reset(&self_id, is_initiated_reset); 2014 1994 2015 1995 pos = get_self_id_pos(ohci, self_id, self_id_count); 2016 1996 if (pos >= 0) { ··· 2138 2112 return; 2139 2113 } 2140 2114 2141 - /* FIXME: Document how the locking works. */ 2142 - spin_lock_irq(&ohci->lock); 2143 - 2144 - ohci->generation = -1; /* prevent AT packet queueing */ 2145 - context_stop(&ohci->at_request_ctx); 2146 - context_stop(&ohci->at_response_ctx); 2147 - 2148 - spin_unlock_irq(&ohci->lock); 2115 + // FIXME: Document how the locking works. 2116 + scoped_guard(spinlock_irq, &ohci->lock) { 2117 + ohci->generation = -1; // prevent AT packet queueing 2118 + context_stop(&ohci->at_request_ctx); 2119 + context_stop(&ohci->at_response_ctx); 2120 + } 2149 2121 2150 2122 /* 2151 2123 * Per OHCI 1.2 draft, clause 7.2.3.3, hardware may leave unsent ··· 2153 2129 at_context_flush(&ohci->at_request_ctx); 2154 2130 at_context_flush(&ohci->at_response_ctx); 2155 2131 2156 - spin_lock_irq(&ohci->lock); 2132 + scoped_guard(spinlock_irq, &ohci->lock) { 2133 + ohci->generation = generation; 2134 + reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset); 2135 + reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_busReset); 2157 2136 2158 - ohci->generation = generation; 2159 - reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset); 2160 - reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_busReset); 2137 + if (ohci->quirks & QUIRK_RESET_PACKET) 2138 + ohci->request_generation = generation; 2161 2139 2162 - if (ohci->quirks & QUIRK_RESET_PACKET) 2163 - ohci->request_generation = generation; 2140 + // This next bit is unrelated to the AT context stuff but we have to do it under the 2141 + // spinlock also. If a new config rom was set up before this reset, the old one is 2142 + // now no longer in use and we can free it. Update the config rom pointers to point 2143 + // to the current config rom and clear the next_config_rom pointer so a new update 2144 + // can take place. 2145 + if (ohci->next_config_rom != NULL) { 2146 + if (ohci->next_config_rom != ohci->config_rom) { 2147 + free_rom = ohci->config_rom; 2148 + free_rom_bus = ohci->config_rom_bus; 2149 + } 2150 + ohci->config_rom = ohci->next_config_rom; 2151 + ohci->config_rom_bus = ohci->next_config_rom_bus; 2152 + ohci->next_config_rom = NULL; 2164 2153 2165 - /* 2166 - * This next bit is unrelated to the AT context stuff but we 2167 - * have to do it under the spinlock also. If a new config rom 2168 - * was set up before this reset, the old one is now no longer 2169 - * in use and we can free it. Update the config rom pointers 2170 - * to point to the current config rom and clear the 2171 - * next_config_rom pointer so a new update can take place. 2172 - */ 2173 - 2174 - if (ohci->next_config_rom != NULL) { 2175 - if (ohci->next_config_rom != ohci->config_rom) { 2176 - free_rom = ohci->config_rom; 2177 - free_rom_bus = ohci->config_rom_bus; 2154 + // Restore config_rom image and manually update config_rom registers. 2155 + // Writing the header quadlet will indicate that the config rom is ready, 2156 + // so we do that last. 2157 + reg_write(ohci, OHCI1394_BusOptions, be32_to_cpu(ohci->config_rom[2])); 2158 + ohci->config_rom[0] = ohci->next_header; 2159 + reg_write(ohci, OHCI1394_ConfigROMhdr, be32_to_cpu(ohci->next_header)); 2178 2160 } 2179 - ohci->config_rom = ohci->next_config_rom; 2180 - ohci->config_rom_bus = ohci->next_config_rom_bus; 2181 - ohci->next_config_rom = NULL; 2182 2161 2183 - /* 2184 - * Restore config_rom image and manually update 2185 - * config_rom registers. Writing the header quadlet 2186 - * will indicate that the config rom is ready, so we 2187 - * do that last. 2188 - */ 2189 - reg_write(ohci, OHCI1394_BusOptions, 2190 - be32_to_cpu(ohci->config_rom[2])); 2191 - ohci->config_rom[0] = ohci->next_header; 2192 - reg_write(ohci, OHCI1394_ConfigROMhdr, 2193 - be32_to_cpu(ohci->next_header)); 2162 + if (param_remote_dma) { 2163 + reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0); 2164 + reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0); 2165 + } 2194 2166 } 2195 - 2196 - if (param_remote_dma) { 2197 - reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0); 2198 - reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0); 2199 - } 2200 - 2201 - spin_unlock_irq(&ohci->lock); 2202 2167 2203 2168 if (free_rom) 2204 2169 dmam_free_coherent(ohci->card.device, CONFIG_ROM_SIZE, free_rom, free_rom_bus); ··· 2210 2197 2211 2198 if (!event || !~event) 2212 2199 return IRQ_NONE; 2200 + 2201 + if (unlikely(param_debug > 0)) { 2202 + dev_notice_ratelimited(ohci->card.device, 2203 + "The debug parameter is superceded by tracepoints events, and deprecated."); 2204 + } 2213 2205 2214 2206 /* 2215 2207 * busReset and postedWriteErr events must not be cleared yet ··· 2256 2238 2257 2239 while (iso_event) { 2258 2240 i = ffs(iso_event) - 1; 2259 - tasklet_schedule( 2260 - &ohci->ir_context_list[i].context.tasklet); 2241 + fw_iso_context_schedule_flush_completions(&ohci->ir_context_list[i].base); 2261 2242 iso_event &= ~(1 << i); 2262 2243 } 2263 2244 } ··· 2267 2250 2268 2251 while (iso_event) { 2269 2252 i = ffs(iso_event) - 1; 2270 - tasklet_schedule( 2271 - &ohci->it_context_list[i].context.tasklet); 2253 + fw_iso_context_schedule_flush_completions(&ohci->it_context_list[i].base); 2272 2254 iso_event &= ~(1 << i); 2273 2255 } 2274 2256 } ··· 2280 2264 reg_read(ohci, OHCI1394_PostedWriteAddressLo); 2281 2265 reg_write(ohci, OHCI1394_IntEventClear, 2282 2266 OHCI1394_postedWriteErr); 2283 - if (printk_ratelimit()) 2284 - ohci_err(ohci, "PCI posted write error\n"); 2267 + dev_err_ratelimited(ohci->card.device, "PCI posted write error\n"); 2285 2268 } 2286 2269 2287 2270 if (unlikely(event & OHCI1394_cycleTooLong)) { 2288 - if (printk_ratelimit()) 2289 - ohci_notice(ohci, "isochronous cycle too long\n"); 2271 + dev_notice_ratelimited(ohci->card.device, "isochronous cycle too long\n"); 2290 2272 reg_write(ohci, OHCI1394_LinkControlSet, 2291 2273 OHCI1394_LinkControl_cycleMaster); 2292 2274 } ··· 2296 2282 * stop active cycleMatch iso contexts now and restart 2297 2283 * them at least two cycles later. (FIXME?) 2298 2284 */ 2299 - if (printk_ratelimit()) 2300 - ohci_notice(ohci, "isochronous cycle inconsistent\n"); 2285 + dev_notice_ratelimited(ohci->card.device, "isochronous cycle inconsistent\n"); 2301 2286 } 2302 2287 2303 2288 if (unlikely(event & OHCI1394_unrecoverableError)) 2304 2289 handle_dead_contexts(ohci); 2305 2290 2306 2291 if (event & OHCI1394_cycle64Seconds) { 2307 - spin_lock(&ohci->lock); 2292 + guard(spinlock)(&ohci->lock); 2308 2293 update_bus_time(ohci); 2309 - spin_unlock(&ohci->lock); 2310 2294 } else 2311 2295 flush_writes(ohci); 2312 2296 ··· 2629 2617 if (next_config_rom == NULL) 2630 2618 return -ENOMEM; 2631 2619 2632 - spin_lock_irq(&ohci->lock); 2620 + scoped_guard(spinlock_irq, &ohci->lock) { 2621 + // If there is not an already pending config_rom update, push our new allocation 2622 + // into the ohci->next_config_rom and then mark the local variable as null so that 2623 + // we won't deallocate the new buffer. 2624 + // 2625 + // OTOH, if there is a pending config_rom update, just use that buffer with the new 2626 + // config_rom data, and let this routine free the unused DMA allocation. 2627 + if (ohci->next_config_rom == NULL) { 2628 + ohci->next_config_rom = next_config_rom; 2629 + ohci->next_config_rom_bus = next_config_rom_bus; 2630 + next_config_rom = NULL; 2631 + } 2633 2632 2634 - /* 2635 - * If there is not an already pending config_rom update, 2636 - * push our new allocation into the ohci->next_config_rom 2637 - * and then mark the local variable as null so that we 2638 - * won't deallocate the new buffer. 2639 - * 2640 - * OTOH, if there is a pending config_rom update, just 2641 - * use that buffer with the new config_rom data, and 2642 - * let this routine free the unused DMA allocation. 2643 - */ 2633 + copy_config_rom(ohci->next_config_rom, config_rom, length); 2644 2634 2645 - if (ohci->next_config_rom == NULL) { 2646 - ohci->next_config_rom = next_config_rom; 2647 - ohci->next_config_rom_bus = next_config_rom_bus; 2648 - next_config_rom = NULL; 2635 + ohci->next_header = config_rom[0]; 2636 + ohci->next_config_rom[0] = 0; 2637 + 2638 + reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus); 2649 2639 } 2650 - 2651 - copy_config_rom(ohci->next_config_rom, config_rom, length); 2652 - 2653 - ohci->next_header = config_rom[0]; 2654 - ohci->next_config_rom[0] = 0; 2655 - 2656 - reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus); 2657 - 2658 - spin_unlock_irq(&ohci->lock); 2659 2640 2660 2641 /* If we didn't use the DMA allocation, delete it. */ 2661 2642 if (next_config_rom != NULL) { ··· 2718 2713 int node_id, int generation) 2719 2714 { 2720 2715 struct fw_ohci *ohci = fw_ohci(card); 2721 - unsigned long flags; 2722 2716 int n, ret = 0; 2723 2717 2724 2718 if (param_remote_dma) ··· 2728 2724 * interrupt bit. Clear physReqResourceAllBuses on bus reset. 2729 2725 */ 2730 2726 2731 - spin_lock_irqsave(&ohci->lock, flags); 2727 + guard(spinlock_irqsave)(&ohci->lock); 2732 2728 2733 - if (ohci->generation != generation) { 2734 - ret = -ESTALE; 2735 - goto out; 2736 - } 2729 + if (ohci->generation != generation) 2730 + return -ESTALE; 2737 2731 2738 2732 /* 2739 2733 * Note, if the node ID contains a non-local bus ID, physical DMA is ··· 2745 2743 reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32)); 2746 2744 2747 2745 flush_writes(ohci); 2748 - out: 2749 - spin_unlock_irqrestore(&ohci->lock, flags); 2750 2746 2751 2747 return ret; 2752 2748 } ··· 2752 2752 static u32 ohci_read_csr(struct fw_card *card, int csr_offset) 2753 2753 { 2754 2754 struct fw_ohci *ohci = fw_ohci(card); 2755 - unsigned long flags; 2756 2755 u32 value; 2757 2756 2758 2757 switch (csr_offset) { ··· 2775 2776 return get_cycle_time(ohci); 2776 2777 2777 2778 case CSR_BUS_TIME: 2778 - /* 2779 - * We might be called just after the cycle timer has wrapped 2780 - * around but just before the cycle64Seconds handler, so we 2781 - * better check here, too, if the bus time needs to be updated. 2782 - */ 2783 - spin_lock_irqsave(&ohci->lock, flags); 2784 - value = update_bus_time(ohci); 2785 - spin_unlock_irqrestore(&ohci->lock, flags); 2786 - return value; 2779 + { 2780 + // We might be called just after the cycle timer has wrapped around but just before 2781 + // the cycle64Seconds handler, so we better check here, too, if the bus time needs 2782 + // to be updated. 2787 2783 2784 + guard(spinlock_irqsave)(&ohci->lock); 2785 + return update_bus_time(ohci); 2786 + } 2788 2787 case CSR_BUSY_TIMEOUT: 2789 2788 value = reg_read(ohci, OHCI1394_ATRetries); 2790 2789 return (value >> 4) & 0x0ffff00f; ··· 2800 2803 static void ohci_write_csr(struct fw_card *card, int csr_offset, u32 value) 2801 2804 { 2802 2805 struct fw_ohci *ohci = fw_ohci(card); 2803 - unsigned long flags; 2804 2806 2805 2807 switch (csr_offset) { 2806 2808 case CSR_STATE_CLEAR: ··· 2835 2839 break; 2836 2840 2837 2841 case CSR_BUS_TIME: 2838 - spin_lock_irqsave(&ohci->lock, flags); 2839 - ohci->bus_time = (update_bus_time(ohci) & 0x40) | 2840 - (value & ~0x7f); 2841 - spin_unlock_irqrestore(&ohci->lock, flags); 2842 + { 2843 + guard(spinlock_irqsave)(&ohci->lock); 2844 + ohci->bus_time = (update_bus_time(ohci) & 0x40) | (value & ~0x7f); 2842 2845 break; 2843 - 2846 + } 2844 2847 case CSR_BUSY_TIMEOUT: 2845 2848 value = (value & 0xf) | ((value & 0xf) << 4) | 2846 2849 ((value & 0xf) << 8) | ((value & 0x0ffff000) << 4); ··· 2927 2932 copy_iso_headers(ctx, (u32 *) (last + 1)); 2928 2933 2929 2934 if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) 2930 - flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ); 2935 + flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT); 2931 2936 2932 2937 return 1; 2933 2938 } ··· 2963 2968 2964 2969 if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) { 2965 2970 trace_isoc_inbound_multiple_completions(&ctx->base, completed, 2966 - FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ); 2971 + FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT); 2967 2972 2968 2973 ctx->base.callback.mc(&ctx->base, 2969 2974 buffer_dma + completed, ··· 3059 3064 ctx->header_length += 4; 3060 3065 3061 3066 if (last->control & cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS)) 3062 - flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ); 3067 + flush_iso_completions(ctx, FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT); 3063 3068 3064 3069 return 1; 3065 3070 } ··· 3085 3090 u32 *mask, regs; 3086 3091 int index, ret = -EBUSY; 3087 3092 3088 - spin_lock_irq(&ohci->lock); 3093 + scoped_guard(spinlock_irq, &ohci->lock) { 3094 + switch (type) { 3095 + case FW_ISO_CONTEXT_TRANSMIT: 3096 + mask = &ohci->it_context_mask; 3097 + callback = handle_it_packet; 3098 + index = ffs(*mask) - 1; 3099 + if (index >= 0) { 3100 + *mask &= ~(1 << index); 3101 + regs = OHCI1394_IsoXmitContextBase(index); 3102 + ctx = &ohci->it_context_list[index]; 3103 + } 3104 + break; 3089 3105 3090 - switch (type) { 3091 - case FW_ISO_CONTEXT_TRANSMIT: 3092 - mask = &ohci->it_context_mask; 3093 - callback = handle_it_packet; 3094 - index = ffs(*mask) - 1; 3095 - if (index >= 0) { 3096 - *mask &= ~(1 << index); 3097 - regs = OHCI1394_IsoXmitContextBase(index); 3098 - ctx = &ohci->it_context_list[index]; 3106 + case FW_ISO_CONTEXT_RECEIVE: 3107 + channels = &ohci->ir_context_channels; 3108 + mask = &ohci->ir_context_mask; 3109 + callback = handle_ir_packet_per_buffer; 3110 + index = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1; 3111 + if (index >= 0) { 3112 + *channels &= ~(1ULL << channel); 3113 + *mask &= ~(1 << index); 3114 + regs = OHCI1394_IsoRcvContextBase(index); 3115 + ctx = &ohci->ir_context_list[index]; 3116 + } 3117 + break; 3118 + 3119 + case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3120 + mask = &ohci->ir_context_mask; 3121 + callback = handle_ir_buffer_fill; 3122 + index = !ohci->mc_allocated ? ffs(*mask) - 1 : -1; 3123 + if (index >= 0) { 3124 + ohci->mc_allocated = true; 3125 + *mask &= ~(1 << index); 3126 + regs = OHCI1394_IsoRcvContextBase(index); 3127 + ctx = &ohci->ir_context_list[index]; 3128 + } 3129 + break; 3130 + 3131 + default: 3132 + index = -1; 3133 + ret = -ENOSYS; 3099 3134 } 3100 - break; 3101 3135 3102 - case FW_ISO_CONTEXT_RECEIVE: 3103 - channels = &ohci->ir_context_channels; 3104 - mask = &ohci->ir_context_mask; 3105 - callback = handle_ir_packet_per_buffer; 3106 - index = *channels & 1ULL << channel ? ffs(*mask) - 1 : -1; 3107 - if (index >= 0) { 3108 - *channels &= ~(1ULL << channel); 3109 - *mask &= ~(1 << index); 3110 - regs = OHCI1394_IsoRcvContextBase(index); 3111 - ctx = &ohci->ir_context_list[index]; 3112 - } 3113 - break; 3114 - 3115 - case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3116 - mask = &ohci->ir_context_mask; 3117 - callback = handle_ir_buffer_fill; 3118 - index = !ohci->mc_allocated ? ffs(*mask) - 1 : -1; 3119 - if (index >= 0) { 3120 - ohci->mc_allocated = true; 3121 - *mask &= ~(1 << index); 3122 - regs = OHCI1394_IsoRcvContextBase(index); 3123 - ctx = &ohci->ir_context_list[index]; 3124 - } 3125 - break; 3126 - 3127 - default: 3128 - index = -1; 3129 - ret = -ENOSYS; 3136 + if (index < 0) 3137 + return ERR_PTR(ret); 3130 3138 } 3131 - 3132 - spin_unlock_irq(&ohci->lock); 3133 - 3134 - if (index < 0) 3135 - return ERR_PTR(ret); 3136 3139 3137 3140 memset(ctx, 0, sizeof(*ctx)); 3138 3141 ctx->header_length = 0; ··· 3142 3149 ret = context_init(&ctx->context, ohci, regs, callback); 3143 3150 if (ret < 0) 3144 3151 goto out_with_header; 3152 + fw_iso_context_init_work(&ctx->base, ohci_isoc_context_work); 3145 3153 3146 3154 if (type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL) { 3147 3155 set_multichannel_mask(ohci, 0); ··· 3154 3160 out_with_header: 3155 3161 free_page((unsigned long)ctx->header); 3156 3162 out: 3157 - spin_lock_irq(&ohci->lock); 3163 + scoped_guard(spinlock_irq, &ohci->lock) { 3164 + switch (type) { 3165 + case FW_ISO_CONTEXT_RECEIVE: 3166 + *channels |= 1ULL << channel; 3167 + break; 3158 3168 3159 - switch (type) { 3160 - case FW_ISO_CONTEXT_RECEIVE: 3161 - *channels |= 1ULL << channel; 3162 - break; 3163 - 3164 - case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3165 - ohci->mc_allocated = false; 3166 - break; 3169 + case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3170 + ohci->mc_allocated = false; 3171 + break; 3172 + } 3173 + *mask |= 1 << index; 3167 3174 } 3168 - *mask |= 1 << index; 3169 - 3170 - spin_unlock_irq(&ohci->lock); 3171 3175 3172 3176 return ERR_PTR(ret); 3173 3177 } ··· 3240 3248 } 3241 3249 flush_writes(ohci); 3242 3250 context_stop(&ctx->context); 3243 - tasklet_kill(&ctx->context.tasklet); 3244 3251 3245 3252 return 0; 3246 3253 } ··· 3248 3257 { 3249 3258 struct fw_ohci *ohci = fw_ohci(base->card); 3250 3259 struct iso_context *ctx = container_of(base, struct iso_context, base); 3251 - unsigned long flags; 3252 3260 int index; 3253 3261 3254 3262 ohci_stop_iso(base); 3255 3263 context_release(&ctx->context); 3256 3264 free_page((unsigned long)ctx->header); 3257 3265 3258 - spin_lock_irqsave(&ohci->lock, flags); 3266 + guard(spinlock_irqsave)(&ohci->lock); 3259 3267 3260 3268 switch (base->type) { 3261 3269 case FW_ISO_CONTEXT_TRANSMIT: ··· 3276 3286 ohci->mc_allocated = false; 3277 3287 break; 3278 3288 } 3279 - 3280 - spin_unlock_irqrestore(&ohci->lock, flags); 3281 3289 } 3282 3290 3283 3291 static int ohci_set_iso_channels(struct fw_iso_context *base, u64 *channels) 3284 3292 { 3285 3293 struct fw_ohci *ohci = fw_ohci(base->card); 3286 - unsigned long flags; 3287 - int ret; 3288 3294 3289 3295 switch (base->type) { 3290 3296 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3297 + { 3298 + guard(spinlock_irqsave)(&ohci->lock); 3291 3299 3292 - spin_lock_irqsave(&ohci->lock, flags); 3293 - 3294 - /* Don't allow multichannel to grab other contexts' channels. */ 3300 + // Don't allow multichannel to grab other contexts' channels. 3295 3301 if (~ohci->ir_context_channels & ~ohci->mc_channels & *channels) { 3296 3302 *channels = ohci->ir_context_channels; 3297 - ret = -EBUSY; 3303 + return -EBUSY; 3298 3304 } else { 3299 3305 set_multichannel_mask(ohci, *channels); 3300 - ret = 0; 3306 + return 0; 3301 3307 } 3302 - 3303 - spin_unlock_irqrestore(&ohci->lock, flags); 3304 - 3305 - break; 3306 - default: 3307 - ret = -EINVAL; 3308 3308 } 3309 - 3310 - return ret; 3309 + default: 3310 + return -EINVAL; 3311 + } 3311 3312 } 3312 3313 3313 3314 #ifdef CONFIG_PM ··· 3373 3392 d[0].branch_address = cpu_to_le32(d_bus | z); 3374 3393 3375 3394 header = (__le32 *) &d[1]; 3376 - header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) | 3377 - IT_HEADER_TAG(p->tag) | 3378 - IT_HEADER_TCODE(TCODE_STREAM_DATA) | 3379 - IT_HEADER_CHANNEL(ctx->base.channel) | 3380 - IT_HEADER_SPEED(ctx->base.speed)); 3381 - header[1] = 3382 - cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length + 3383 - p->payload_length)); 3395 + 3396 + ohci1394_it_data_set_speed(header, ctx->base.speed); 3397 + ohci1394_it_data_set_tag(header, p->tag); 3398 + ohci1394_it_data_set_channel(header, ctx->base.channel); 3399 + ohci1394_it_data_set_tcode(header, TCODE_STREAM_DATA); 3400 + ohci1394_it_data_set_sync(header, p->sy); 3401 + 3402 + ohci1394_it_data_set_data_length(header, p->header_length + p->payload_length); 3384 3403 } 3385 3404 3386 3405 if (p->header_length > 0) { ··· 3568 3587 unsigned long payload) 3569 3588 { 3570 3589 struct iso_context *ctx = container_of(base, struct iso_context, base); 3571 - unsigned long flags; 3572 - int ret = -ENOSYS; 3573 3590 3574 - spin_lock_irqsave(&ctx->context.ohci->lock, flags); 3591 + guard(spinlock_irqsave)(&ctx->context.ohci->lock); 3592 + 3575 3593 switch (base->type) { 3576 3594 case FW_ISO_CONTEXT_TRANSMIT: 3577 - ret = queue_iso_transmit(ctx, packet, buffer, payload); 3578 - break; 3595 + return queue_iso_transmit(ctx, packet, buffer, payload); 3579 3596 case FW_ISO_CONTEXT_RECEIVE: 3580 - ret = queue_iso_packet_per_buffer(ctx, packet, buffer, payload); 3581 - break; 3597 + return queue_iso_packet_per_buffer(ctx, packet, buffer, payload); 3582 3598 case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL: 3583 - ret = queue_iso_buffer_fill(ctx, packet, buffer, payload); 3584 - break; 3599 + return queue_iso_buffer_fill(ctx, packet, buffer, payload); 3600 + default: 3601 + return -ENOSYS; 3585 3602 } 3586 - spin_unlock_irqrestore(&ctx->context.ohci->lock, flags); 3587 - 3588 - return ret; 3589 3603 } 3590 3604 3591 3605 static void ohci_flush_queue_iso(struct fw_iso_context *base) ··· 3596 3620 struct iso_context *ctx = container_of(base, struct iso_context, base); 3597 3621 int ret = 0; 3598 3622 3599 - tasklet_disable_in_atomic(&ctx->context.tasklet); 3600 - 3601 3623 if (!test_and_set_bit_lock(0, &ctx->flushing_completions)) { 3602 - context_tasklet((unsigned long)&ctx->context); 3624 + ohci_isoc_context_work(&base->work); 3603 3625 3604 3626 switch (base->type) { 3605 3627 case FW_ISO_CONTEXT_TRANSMIT: ··· 3616 3642 clear_bit_unlock(0, &ctx->flushing_completions); 3617 3643 smp_mb__after_atomic(); 3618 3644 } 3619 - 3620 - tasklet_enable(&ctx->context.tasklet); 3621 3645 3622 3646 return ret; 3623 3647 } ··· 3835 3863 goto fail_msi; 3836 3864 } 3837 3865 3838 - err = fw_card_add(&ohci->card, max_receive, link_speed, guid); 3866 + err = fw_card_add(&ohci->card, max_receive, link_speed, guid, ohci->n_it + ohci->n_ir); 3839 3867 if (err) 3840 3868 goto fail_irq; 3841 3869
+199 -1
drivers/firewire/ohci.h
··· 153 153 #define OHCI1394_evt_unknown 0xe 154 154 #define OHCI1394_evt_flushed 0xf 155 155 156 - #define OHCI1394_phy_tcode 0xe 156 + 157 + // Asynchronous Transmit DMA. 158 + // 159 + // The content of first two quadlets of data for AT DMA is different from the header for IEEE 1394 160 + // asynchronous packet. 161 + 162 + #define OHCI1394_AT_DATA_Q0_srcBusID_MASK 0x00800000 163 + #define OHCI1394_AT_DATA_Q0_srcBusID_SHIFT 23 164 + #define OHCI1394_AT_DATA_Q0_spd_MASK 0x00070000 165 + #define OHCI1394_AT_DATA_Q0_spd_SHIFT 16 166 + #define OHCI1394_AT_DATA_Q0_tLabel_MASK 0x0000fc00 167 + #define OHCI1394_AT_DATA_Q0_tLabel_SHIFT 10 168 + #define OHCI1394_AT_DATA_Q0_rt_MASK 0x00000300 169 + #define OHCI1394_AT_DATA_Q0_rt_SHIFT 8 170 + #define OHCI1394_AT_DATA_Q0_tCode_MASK 0x000000f0 171 + #define OHCI1394_AT_DATA_Q0_tCode_SHIFT 4 172 + #define OHCI1394_AT_DATA_Q1_destinationId_MASK 0xffff0000 173 + #define OHCI1394_AT_DATA_Q1_destinationId_SHIFT 16 174 + #define OHCI1394_AT_DATA_Q1_destinationOffsetHigh_MASK 0x0000ffff 175 + #define OHCI1394_AT_DATA_Q1_destinationOffsetHigh_SHIFT 0 176 + #define OHCI1394_AT_DATA_Q1_rCode_MASK 0x0000f000 177 + #define OHCI1394_AT_DATA_Q1_rCode_SHIFT 12 178 + 179 + static inline bool ohci1394_at_data_get_src_bus_id(const __le32 *data) 180 + { 181 + return !!((data[0] & OHCI1394_AT_DATA_Q0_srcBusID_MASK) >> OHCI1394_AT_DATA_Q0_srcBusID_SHIFT); 182 + } 183 + 184 + static inline void ohci1394_at_data_set_src_bus_id(__le32 *data, bool src_bus_id) 185 + { 186 + data[0] &= cpu_to_le32(~OHCI1394_AT_DATA_Q0_srcBusID_MASK); 187 + data[0] |= cpu_to_le32((src_bus_id << OHCI1394_AT_DATA_Q0_srcBusID_SHIFT) & OHCI1394_AT_DATA_Q0_srcBusID_MASK); 188 + } 189 + 190 + static inline unsigned int ohci1394_at_data_get_speed(const __le32 *data) 191 + { 192 + return (le32_to_cpu(data[0]) & OHCI1394_AT_DATA_Q0_spd_MASK) >> OHCI1394_AT_DATA_Q0_spd_SHIFT; 193 + } 194 + 195 + static inline void ohci1394_at_data_set_speed(__le32 *data, unsigned int scode) 196 + { 197 + data[0] &= cpu_to_le32(~OHCI1394_AT_DATA_Q0_spd_MASK); 198 + data[0] |= cpu_to_le32((scode << OHCI1394_AT_DATA_Q0_spd_SHIFT) & OHCI1394_AT_DATA_Q0_spd_MASK); 199 + } 200 + 201 + static inline unsigned int ohci1394_at_data_get_tlabel(const __le32 *data) 202 + { 203 + return (le32_to_cpu(data[0]) & OHCI1394_AT_DATA_Q0_tLabel_MASK) >> OHCI1394_AT_DATA_Q0_tLabel_SHIFT; 204 + } 205 + 206 + static inline void ohci1394_at_data_set_tlabel(__le32 *data, unsigned int tlabel) 207 + { 208 + data[0] &= cpu_to_le32(~OHCI1394_AT_DATA_Q0_tLabel_MASK); 209 + data[0] |= cpu_to_le32((tlabel << OHCI1394_AT_DATA_Q0_tLabel_SHIFT) & OHCI1394_AT_DATA_Q0_tLabel_MASK); 210 + } 211 + 212 + static inline unsigned int ohci1394_at_data_get_retry(const __le32 *data) 213 + { 214 + return (le32_to_cpu(data[0]) & OHCI1394_AT_DATA_Q0_rt_MASK) >> OHCI1394_AT_DATA_Q0_rt_SHIFT; 215 + } 216 + 217 + static inline void ohci1394_at_data_set_retry(__le32 *data, unsigned int retry) 218 + { 219 + data[0] &= cpu_to_le32(~OHCI1394_AT_DATA_Q0_rt_MASK); 220 + data[0] |= cpu_to_le32((retry << OHCI1394_AT_DATA_Q0_rt_SHIFT) & OHCI1394_AT_DATA_Q0_rt_MASK); 221 + } 222 + 223 + static inline unsigned int ohci1394_at_data_get_tcode(const __le32 *data) 224 + { 225 + return (le32_to_cpu(data[0]) & OHCI1394_AT_DATA_Q0_tCode_MASK) >> OHCI1394_AT_DATA_Q0_tCode_SHIFT; 226 + } 227 + 228 + static inline void ohci1394_at_data_set_tcode(__le32 *data, unsigned int tcode) 229 + { 230 + data[0] &= cpu_to_le32(~OHCI1394_AT_DATA_Q0_tCode_MASK); 231 + data[0] |= cpu_to_le32((tcode << OHCI1394_AT_DATA_Q0_tCode_SHIFT) & OHCI1394_AT_DATA_Q0_tCode_MASK); 232 + } 233 + 234 + static inline unsigned int ohci1394_at_data_get_destination_id(const __le32 *data) 235 + { 236 + return (le32_to_cpu(data[1]) & OHCI1394_AT_DATA_Q1_destinationId_MASK) >> OHCI1394_AT_DATA_Q1_destinationId_SHIFT; 237 + } 238 + 239 + static inline void ohci1394_at_data_set_destination_id(__le32 *data, unsigned int destination_id) 240 + { 241 + data[1] &= cpu_to_le32(~OHCI1394_AT_DATA_Q1_destinationId_MASK); 242 + data[1] |= cpu_to_le32((destination_id << OHCI1394_AT_DATA_Q1_destinationId_SHIFT) & OHCI1394_AT_DATA_Q1_destinationId_MASK); 243 + } 244 + 245 + static inline u64 ohci1394_at_data_get_destination_offset(const __le32 *data) 246 + { 247 + u64 hi = (u64)((le32_to_cpu(data[1]) & OHCI1394_AT_DATA_Q1_destinationOffsetHigh_MASK) >> OHCI1394_AT_DATA_Q1_destinationOffsetHigh_SHIFT); 248 + u64 lo = (u64)le32_to_cpu(data[2]); 249 + return (hi << 32) | lo; 250 + } 251 + 252 + static inline void ohci1394_at_data_set_destination_offset(__le32 *data, u64 offset) 253 + { 254 + u32 hi = (u32)(offset >> 32); 255 + u32 lo = (u32)(offset & 0x00000000ffffffff); 256 + data[1] &= cpu_to_le32(~OHCI1394_AT_DATA_Q1_destinationOffsetHigh_MASK); 257 + data[1] |= cpu_to_le32((hi << OHCI1394_AT_DATA_Q1_destinationOffsetHigh_SHIFT) & OHCI1394_AT_DATA_Q1_destinationOffsetHigh_MASK); 258 + data[2] = cpu_to_le32(lo); 259 + } 260 + 261 + static inline unsigned int ohci1394_at_data_get_rcode(const __le32 *data) 262 + { 263 + return (le32_to_cpu(data[1]) & OHCI1394_AT_DATA_Q1_rCode_MASK) >> OHCI1394_AT_DATA_Q1_rCode_SHIFT; 264 + } 265 + 266 + static inline void ohci1394_at_data_set_rcode(__le32 *data, unsigned int rcode) 267 + { 268 + data[1] &= cpu_to_le32(~OHCI1394_AT_DATA_Q1_rCode_MASK); 269 + data[1] |= cpu_to_le32((rcode << OHCI1394_AT_DATA_Q1_rCode_SHIFT) & OHCI1394_AT_DATA_Q1_rCode_MASK); 270 + } 271 + 272 + // Isochronous Transmit DMA. 273 + // 274 + // The content of first two quadlets of data for IT DMA is different from the header for IEEE 1394 275 + // isochronous packet. 276 + 277 + #define OHCI1394_IT_DATA_Q0_spd_MASK 0x00070000 278 + #define OHCI1394_IT_DATA_Q0_spd_SHIFT 16 279 + #define OHCI1394_IT_DATA_Q0_tag_MASK 0x0000c000 280 + #define OHCI1394_IT_DATA_Q0_tag_SHIFT 14 281 + #define OHCI1394_IT_DATA_Q0_chanNum_MASK 0x00003f00 282 + #define OHCI1394_IT_DATA_Q0_chanNum_SHIFT 8 283 + #define OHCI1394_IT_DATA_Q0_tcode_MASK 0x000000f0 284 + #define OHCI1394_IT_DATA_Q0_tcode_SHIFT 4 285 + #define OHCI1394_IT_DATA_Q0_sy_MASK 0x0000000f 286 + #define OHCI1394_IT_DATA_Q0_sy_SHIFT 0 287 + #define OHCI1394_IT_DATA_Q1_dataLength_MASK 0xffff0000 288 + #define OHCI1394_IT_DATA_Q1_dataLength_SHIFT 16 289 + 290 + static inline unsigned int ohci1394_it_data_get_speed(const __le32 *data) 291 + { 292 + return (le32_to_cpu(data[0]) & OHCI1394_IT_DATA_Q0_spd_MASK) >> OHCI1394_IT_DATA_Q0_spd_SHIFT; 293 + } 294 + 295 + static inline void ohci1394_it_data_set_speed(__le32 *data, unsigned int scode) 296 + { 297 + data[0] &= cpu_to_le32(~OHCI1394_IT_DATA_Q0_spd_MASK); 298 + data[0] |= cpu_to_le32((scode << OHCI1394_IT_DATA_Q0_spd_SHIFT) & OHCI1394_IT_DATA_Q0_spd_MASK); 299 + } 300 + 301 + static inline unsigned int ohci1394_it_data_get_tag(const __le32 *data) 302 + { 303 + return (le32_to_cpu(data[0]) & OHCI1394_IT_DATA_Q0_tag_MASK) >> OHCI1394_IT_DATA_Q0_tag_SHIFT; 304 + } 305 + 306 + static inline void ohci1394_it_data_set_tag(__le32 *data, unsigned int tag) 307 + { 308 + data[0] &= cpu_to_le32(~OHCI1394_IT_DATA_Q0_tag_MASK); 309 + data[0] |= cpu_to_le32((tag << OHCI1394_IT_DATA_Q0_tag_SHIFT) & OHCI1394_IT_DATA_Q0_tag_MASK); 310 + } 311 + 312 + static inline unsigned int ohci1394_it_data_get_channel(const __le32 *data) 313 + { 314 + return (le32_to_cpu(data[0]) & OHCI1394_IT_DATA_Q0_chanNum_MASK) >> OHCI1394_IT_DATA_Q0_chanNum_SHIFT; 315 + } 316 + 317 + static inline void ohci1394_it_data_set_channel(__le32 *data, unsigned int channel) 318 + { 319 + data[0] &= cpu_to_le32(~OHCI1394_IT_DATA_Q0_chanNum_MASK); 320 + data[0] |= cpu_to_le32((channel << OHCI1394_IT_DATA_Q0_chanNum_SHIFT) & OHCI1394_IT_DATA_Q0_chanNum_MASK); 321 + } 322 + 323 + static inline unsigned int ohci1394_it_data_get_tcode(const __le32 *data) 324 + { 325 + return (le32_to_cpu(data[0]) & OHCI1394_IT_DATA_Q0_tcode_MASK) >> OHCI1394_IT_DATA_Q0_tcode_SHIFT; 326 + } 327 + 328 + static inline void ohci1394_it_data_set_tcode(__le32 *data, unsigned int tcode) 329 + { 330 + data[0] &= cpu_to_le32(~OHCI1394_IT_DATA_Q0_tcode_MASK); 331 + data[0] |= cpu_to_le32((tcode << OHCI1394_IT_DATA_Q0_tcode_SHIFT) & OHCI1394_IT_DATA_Q0_tcode_MASK); 332 + } 333 + 334 + static inline unsigned int ohci1394_it_data_get_sync(const __le32 *data) 335 + { 336 + return (le32_to_cpu(data[0]) & OHCI1394_IT_DATA_Q0_sy_MASK) >> OHCI1394_IT_DATA_Q0_sy_SHIFT; 337 + } 338 + 339 + static inline void ohci1394_it_data_set_sync(__le32 *data, unsigned int sync) 340 + { 341 + data[0] &= cpu_to_le32(~OHCI1394_IT_DATA_Q0_sy_MASK); 342 + data[0] |= cpu_to_le32((sync << OHCI1394_IT_DATA_Q0_sy_SHIFT) & OHCI1394_IT_DATA_Q0_sy_MASK); 343 + } 344 + 345 + static inline unsigned int ohci1394_it_data_get_data_length(const __le32 *data) 346 + { 347 + return (le32_to_cpu(data[1]) & OHCI1394_IT_DATA_Q1_dataLength_MASK) >> OHCI1394_IT_DATA_Q1_dataLength_SHIFT; 348 + } 349 + 350 + static inline void ohci1394_it_data_set_data_length(__le32 *data, unsigned int data_length) 351 + { 352 + data[1] &= cpu_to_le32(~OHCI1394_IT_DATA_Q1_dataLength_MASK); 353 + data[1] |= cpu_to_le32((data_length << OHCI1394_IT_DATA_Q1_dataLength_SHIFT) & OHCI1394_IT_DATA_Q1_dataLength_MASK); 354 + } 157 355 158 356 // Self-ID DMA. 159 357
+22
include/linux/firewire.h
··· 134 134 __be32 topology_map[(CSR_TOPOLOGY_MAP_END - CSR_TOPOLOGY_MAP) / 4]; 135 135 136 136 __be32 maint_utility_register; 137 + 138 + struct workqueue_struct *isoc_wq; 137 139 }; 138 140 139 141 static inline struct fw_card *fw_card_get(struct fw_card *card) ··· 511 509 512 510 struct fw_iso_context { 513 511 struct fw_card *card; 512 + struct work_struct work; 514 513 int type; 515 514 int channel; 516 515 int speed; ··· 531 528 unsigned long payload); 532 529 void fw_iso_context_queue_flush(struct fw_iso_context *ctx); 533 530 int fw_iso_context_flush_completions(struct fw_iso_context *ctx); 531 + 532 + /** 533 + * fw_iso_context_schedule_flush_completions() - schedule work item to process isochronous context. 534 + * @ctx: the isochronous context 535 + * 536 + * Schedule a work item on workqueue to process the isochronous context. The registered callback 537 + * function is called by the worker when a queued packet buffer with the interrupt flag is 538 + * completed, either after transmission in the IT context or after being filled in the IR context. 539 + * The callback function is also called when the header buffer in the context becomes full, If it 540 + * is required to process the context in the current context, fw_iso_context_flush_completions() is 541 + * available instead. 542 + * 543 + * Context: Any context. 544 + */ 545 + static inline void fw_iso_context_schedule_flush_completions(struct fw_iso_context *ctx) 546 + { 547 + queue_work(ctx->card->isoc_wq, &ctx->work); 548 + } 549 + 534 550 int fw_iso_context_start(struct fw_iso_context *ctx, 535 551 int cycle, int sync, int tags); 536 552 int fw_iso_context_stop(struct fw_iso_context *ctx);
+2 -2
include/trace/events/firewire.h
··· 830 830 #ifndef show_cause 831 831 enum fw_iso_context_completions_cause { 832 832 FW_ISO_CONTEXT_COMPLETIONS_CAUSE_FLUSH = 0, 833 - FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ, 833 + FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT, 834 834 FW_ISO_CONTEXT_COMPLETIONS_CAUSE_HEADER_OVERFLOW, 835 835 }; 836 836 #define show_cause(cause) \ 837 837 __print_symbolic(cause, \ 838 838 { FW_ISO_CONTEXT_COMPLETIONS_CAUSE_FLUSH, "FLUSH" }, \ 839 - { FW_ISO_CONTEXT_COMPLETIONS_CAUSE_IRQ, "IRQ" }, \ 839 + { FW_ISO_CONTEXT_COMPLETIONS_CAUSE_INTERRUPT, "INTERRUPT" }, \ 840 840 { FW_ISO_CONTEXT_COMPLETIONS_CAUSE_HEADER_OVERFLOW, "HEADER_OVERFLOW" } \ 841 841 ) 842 842 #endif
+27 -7
sound/firewire/amdtp-stream.c
··· 615 615 // The program in user process should periodically check the status of intermediate 616 616 // buffer associated to PCM substream to process PCM frames in the buffer, instead 617 617 // of receiving notification of period elapsed by poll wait. 618 + // 619 + // Use another work item for period elapsed event to prevent the following AB/BA 620 + // deadlock: 621 + // 622 + // thread 1 thread 2 623 + // ================================= ================================= 624 + // A.work item (process) pcm ioctl (process) 625 + // v v 626 + // process_rx_packets() B.PCM stream lock 627 + // process_tx_packets() v 628 + // v callbacks in snd_pcm_ops 629 + // update_pcm_pointers() v 630 + // snd_pcm_elapsed() fw_iso_context_flush_completions() 631 + // snd_pcm_stream_lock_irqsave() disable_work_sync() 632 + // v v 633 + // wait until release of B wait until A exits 618 634 if (!pcm->runtime->no_period_wakeup) 619 635 queue_work(system_highpri_wq, &s->period_work); 620 636 } ··· 1071 1055 1072 1056 static inline void cancel_stream(struct amdtp_stream *s) 1073 1057 { 1058 + struct work_struct *work = current_work(); 1059 + 1074 1060 s->packet_index = -1; 1075 - if (in_softirq()) 1061 + 1062 + // Detect work items for any isochronous context. The work item for pcm_period_work() 1063 + // should be avoided since the call of snd_pcm_period_elapsed() can reach via 1064 + // snd_pcm_ops.pointer() under acquiring PCM stream(group) lock and causes dead lock at 1065 + // snd_pcm_stop_xrun(). 1066 + if (work && work != &s->period_work) 1076 1067 amdtp_stream_pcm_abort(s); 1077 1068 WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN); 1078 1069 } ··· 1879 1856 struct amdtp_stream *irq_target = d->irq_target; 1880 1857 1881 1858 if (irq_target && amdtp_stream_running(irq_target)) { 1882 - // use wq to prevent AB/BA deadlock competition for 1883 - // substream lock: 1884 - // fw_iso_context_flush_completions() acquires 1885 - // lock by ohci_flush_iso_completions(), 1886 - // amdtp-stream process_rx_packets() attempts to 1887 - // acquire same lock by snd_pcm_elapsed() 1859 + // The work item to call snd_pcm_period_elapsed() can reach here by the call of 1860 + // snd_pcm_ops.pointer(), however less packets would be available then. Therefore 1861 + // the following call is just for user process contexts. 1888 1862 if (current_work() != &s->period_work) 1889 1863 fw_iso_context_flush_completions(irq_target->context); 1890 1864 }
+1
sound/firewire/bebob/bebob_pcm.c
··· 367 367 goto end; 368 368 369 369 pcm->private_data = bebob; 370 + pcm->nonatomic = true; 370 371 snprintf(pcm->name, sizeof(pcm->name), 371 372 "%s PCM", bebob->card->shortname); 372 373 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
+1
sound/firewire/dice/dice-pcm.c
··· 441 441 if (err < 0) 442 442 return err; 443 443 pcm->private_data = dice; 444 + pcm->nonatomic = true; 444 445 strcpy(pcm->name, dice->card->shortname); 445 446 446 447 if (capture > 0)
+1
sound/firewire/digi00x/digi00x-pcm.c
··· 350 350 return err; 351 351 352 352 pcm->private_data = dg00x; 353 + pcm->nonatomic = true; 353 354 snprintf(pcm->name, sizeof(pcm->name), 354 355 "%s PCM", dg00x->card->shortname); 355 356 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);
+1
sound/firewire/fireface/ff-pcm.c
··· 390 390 return err; 391 391 392 392 pcm->private_data = ff; 393 + pcm->nonatomic = true; 393 394 snprintf(pcm->name, sizeof(pcm->name), 394 395 "%s PCM", ff->card->shortname); 395 396 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_playback_ops);
+1
sound/firewire/fireworks/fireworks_pcm.c
··· 397 397 goto end; 398 398 399 399 pcm->private_data = efw; 400 + pcm->nonatomic = true; 400 401 snprintf(pcm->name, sizeof(pcm->name), "%s PCM", efw->card->shortname); 401 402 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops); 402 403 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
+1
sound/firewire/isight.c
··· 454 454 if (err < 0) 455 455 return err; 456 456 pcm->private_data = isight; 457 + pcm->nonatomic = true; 457 458 strcpy(pcm->name, "iSight"); 458 459 isight->pcm = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 459 460 isight->pcm->ops = &ops;
+1
sound/firewire/motu/motu-pcm.c
··· 360 360 if (err < 0) 361 361 return err; 362 362 pcm->private_data = motu; 363 + pcm->nonatomic = true; 363 364 strcpy(pcm->name, motu->card->shortname); 364 365 365 366 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_ops);
+1
sound/firewire/oxfw/oxfw-pcm.c
··· 440 440 return err; 441 441 442 442 pcm->private_data = oxfw; 443 + pcm->nonatomic = true; 443 444 strcpy(pcm->name, oxfw->card->shortname); 444 445 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops); 445 446 if (cap > 0)
+1
sound/firewire/tascam/tascam-pcm.c
··· 279 279 return err; 280 280 281 281 pcm->private_data = tscm; 282 + pcm->nonatomic = true; 282 283 snprintf(pcm->name, sizeof(pcm->name), 283 284 "%s PCM", tscm->card->shortname); 284 285 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_ops);