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

firewire: nosy: endianess fixes and annotations

1.) The DMA programs (struct pcl) are PCI-endian = little endian data
(except for the 3rd quadlet in a PCL which the controller does not
touch). Annotate them as such.

Fix all accesses of the PCL to work with big endian CPUs also. Not
actually tested, I only have a little endian PC to test with. This
includes replacement of a bitfield struct pcl_status by open-coded
shift and mask operations.

2.) The two __attribute__ ((packed)) at struct pcl are not really
required since it consists of u32/__le32 only, i.e. there will be no
padding with or without the attribute.

3.) The received IEEE 1394 data are byteswapped by the controller from
IEEE 1394 endian = big endian to PCI endian = little endian because the
PCL_BIGENDIAN control bit is set. Therefore annotate the DMA buffer as
a __le32 array.

Fix the one access of the DMA buffer (the check of the transaction code
of link packets) to work with big endian CPUs. Also fix the two
accesses of the client bounce buffer (the reading of packet length).

4.) Add a comment to the userspace ABI header that all of the data gets
out as little endian data, except for the timestamp which is CPU endian.
(We could make it little endian too, but why? Vice versa, an ioctl
could be added to dump packet data in big endian byte order...)

Signed-off-by: Stefan Richter <stefanr@s5r6.in-berlin.de>

+29 -51
+3 -3
drivers/firewire/nosy-user.h
··· 17 17 /* 18 18 * Format of packets returned from the kernel driver: 19 19 * 20 - * quadlet with timestamp (microseconds) 21 - * quadlet padded packet data... 22 - * quadlet with ack 20 + * quadlet with timestamp (microseconds, CPU endian) 21 + * quadlet-padded packet data... (little endian) 22 + * quadlet with ack (little endian) 23 23 */ 24 24 25 25 #endif /* __nosy_user_h */
+26 -48
drivers/firewire/nosy.c
··· 51 51 52 52 static char driver_name[] = KBUILD_MODNAME; 53 53 54 - struct pcl_status { 55 - unsigned int transfer_count : 13; 56 - unsigned int reserved0 : 1; 57 - unsigned int ack_type : 1; 58 - unsigned int ack : 4; 59 - unsigned int rcv_speed : 2; 60 - unsigned int rcv_dma_channel : 6; 61 - unsigned int packet_complete : 1; 62 - unsigned int packet_error : 1; 63 - unsigned int master_error : 1; 64 - unsigned int iso_mode : 1; 65 - unsigned int self_id : 1; 66 - }; 67 - 68 54 /* this is the physical layout of a PCL, its size is 128 bytes */ 69 55 struct pcl { 70 - u32 next; 71 - u32 async_error_next; 72 - u32 user_data; 73 - struct pcl_status pcl_status; 74 - u32 remaining_transfer_count; 75 - u32 next_data_buffer; 76 - struct { 77 - u32 control; 78 - u32 pointer; 79 - } buffer[13] __attribute__ ((packed)); 80 - } __attribute__ ((packed)); 56 + __le32 next; 57 + __le32 async_error_next; 58 + u32 user_data; 59 + __le32 pcl_status; 60 + __le32 remaining_transfer_count; 61 + __le32 next_data_buffer; 62 + struct { 63 + __le32 control; 64 + __le32 pointer; 65 + } buffer[13]; 66 + }; 81 67 82 68 struct packet { 83 69 unsigned int length; ··· 84 98 __iomem char *registers; 85 99 86 100 struct pcl *rcv_start_pcl, *rcv_pcl; 87 - u32 *rcv_buffer; 101 + __le32 *rcv_buffer; 88 102 89 103 dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus; 90 104 ··· 412 426 413 427 #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */ 414 428 415 - struct link_packet { 416 - unsigned int priority : 4; 417 - unsigned int tcode : 4; 418 - unsigned int rt : 2; 419 - unsigned int tlabel : 6; 420 - unsigned int destination : 16; 421 - }; 422 - 423 429 static void 424 430 packet_irq_handler(struct pcilynx *lynx) 425 431 { 426 432 struct client *client; 427 - u32 tcode_mask; 433 + u32 tcode_mask, tcode; 428 434 size_t length; 429 - struct link_packet *packet; 430 435 struct timeval tv; 431 436 432 437 /* FIXME: Also report rcv_speed. */ 433 438 434 - length = lynx->rcv_pcl->pcl_status.transfer_count; 435 - packet = (struct link_packet *) &lynx->rcv_buffer[1]; 439 + length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff; 440 + tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf; 436 441 437 442 do_gettimeofday(&tv); 438 - lynx->rcv_buffer[0] = tv.tv_usec; 443 + lynx->rcv_buffer[0] = (__force __le32)tv.tv_usec; 439 444 440 445 if (length == PHY_PACKET_SIZE) 441 446 tcode_mask = 1 << TCODE_PHY_PACKET; 442 447 else 443 - tcode_mask = 1 << packet->tcode; 448 + tcode_mask = 1 << tcode; 444 449 445 450 spin_lock(&lynx->client_list_lock); 446 451 ··· 579 602 ret = -ENOMEM; 580 603 goto fail_deallocate; 581 604 } 582 - lynx->rcv_start_pcl->next = lynx->rcv_pcl_bus; 583 - lynx->rcv_pcl->next = PCL_NEXT_INVALID; 584 - lynx->rcv_pcl->async_error_next = PCL_NEXT_INVALID; 605 + lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus); 606 + lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID); 607 + lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID); 585 608 586 609 lynx->rcv_pcl->buffer[0].control = 587 - PCL_CMD_RCV | PCL_BIGENDIAN | 2044; 588 - lynx->rcv_pcl->buffer[0].pointer = lynx->rcv_buffer_bus + 4; 610 + cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044); 611 + lynx->rcv_pcl->buffer[0].pointer = 612 + cpu_to_le32(lynx->rcv_buffer_bus + 4); 589 613 p = lynx->rcv_buffer_bus + 2048; 590 614 end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE; 591 615 for (i = 1; p < end; i++, p += 2048) { 592 616 lynx->rcv_pcl->buffer[i].control = 593 - PCL_CMD_RCV | PCL_BIGENDIAN | 2048; 594 - lynx->rcv_pcl->buffer[i].pointer = p; 617 + cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048); 618 + lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p); 595 619 } 596 - lynx->rcv_pcl->buffer[i - 1].control |= PCL_LAST_BUFF; 620 + lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF); 597 621 598 622 reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET); 599 623 /* Fix buggy cards with autoboot pin not tied low: */