Commit 213ad5ad authored by Ben Segal's avatar Ben Segal Committed by Oded Gabbay
Browse files

habanalabs: fix endianness handling for packets from user



Packets that arrive from the user and need to be parsed by the driver are
assumed to be in LE format.

This patch fix all the places where the code handles these packets and use
the correct endianness macros to handle them, as the driver handles the
packets in CPU format (LE or BE depending on the arch).

Signed-off-by: default avatarBen Segal <bpsegal20@gmail.com>
Reviewed-by: default avatarOded Gabbay <oded.gabbay@gmail.com>
Signed-off-by: default avatarOded Gabbay <oded.gabbay@gmail.com>
parent c8113756
Loading
Loading
Loading
Loading
+19 −13
Original line number Diff line number Diff line
@@ -3428,12 +3428,13 @@ static int goya_validate_cb(struct hl_device *hdev,
	while (cb_parsed_length < parser->user_cb_size) {
		enum packet_id pkt_id;
		u16 pkt_size;
		void *user_pkt;
		struct goya_packet *user_pkt;

		user_pkt = (void *) (uintptr_t)
		user_pkt = (struct goya_packet *) (uintptr_t)
			(parser->user_cb->kernel_address + cb_parsed_length);

		pkt_id = (enum packet_id) (((*(u64 *) user_pkt) &
		pkt_id = (enum packet_id) (
				(le64_to_cpu(user_pkt->header) &
				PACKET_HEADER_PACKET_ID_MASK) >>
					PACKET_HEADER_PACKET_ID_SHIFT);

@@ -3453,7 +3454,8 @@ static int goya_validate_cb(struct hl_device *hdev,
			 * need to validate here as well because patch_cb() is
			 * not called in MMU path while this function is called
			 */
			rc = goya_validate_wreg32(hdev, parser, user_pkt);
			rc = goya_validate_wreg32(hdev,
				parser, (struct packet_wreg32 *) user_pkt);
			break;

		case PACKET_WREG_BULK:
@@ -3481,10 +3483,10 @@ static int goya_validate_cb(struct hl_device *hdev,
		case PACKET_LIN_DMA:
			if (is_mmu)
				rc = goya_validate_dma_pkt_mmu(hdev, parser,
						user_pkt);
					(struct packet_lin_dma *) user_pkt);
			else
				rc = goya_validate_dma_pkt_no_mmu(hdev, parser,
						user_pkt);
					(struct packet_lin_dma *) user_pkt);
			break;

		case PACKET_MSG_LONG:
@@ -3657,15 +3659,16 @@ static int goya_patch_cb(struct hl_device *hdev,
		enum packet_id pkt_id;
		u16 pkt_size;
		u32 new_pkt_size = 0;
		void *user_pkt, *kernel_pkt;
		struct goya_packet *user_pkt, *kernel_pkt;

		user_pkt = (void *) (uintptr_t)
		user_pkt = (struct goya_packet *) (uintptr_t)
			(parser->user_cb->kernel_address + cb_parsed_length);
		kernel_pkt = (void *) (uintptr_t)
		kernel_pkt = (struct goya_packet *) (uintptr_t)
			(parser->patched_cb->kernel_address +
					cb_patched_cur_length);

		pkt_id = (enum packet_id) (((*(u64 *) user_pkt) &
		pkt_id = (enum packet_id) (
				(le64_to_cpu(user_pkt->header) &
				PACKET_HEADER_PACKET_ID_MASK) >>
					PACKET_HEADER_PACKET_ID_SHIFT);

@@ -3680,15 +3683,18 @@ static int goya_patch_cb(struct hl_device *hdev,

		switch (pkt_id) {
		case PACKET_LIN_DMA:
			rc = goya_patch_dma_packet(hdev, parser, user_pkt,
						kernel_pkt, &new_pkt_size);
			rc = goya_patch_dma_packet(hdev, parser,
					(struct packet_lin_dma *) user_pkt,
					(struct packet_lin_dma *) kernel_pkt,
					&new_pkt_size);
			cb_patched_cur_length += new_pkt_size;
			break;

		case PACKET_WREG_32:
			memcpy(kernel_pkt, user_pkt, pkt_size);
			cb_patched_cur_length += pkt_size;
			rc = goya_validate_wreg32(hdev, parser, kernel_pkt);
			rc = goya_validate_wreg32(hdev, parser,
					(struct packet_wreg32 *) kernel_pkt);
			break;

		case PACKET_WREG_BULK:
+13 −0
Original line number Diff line number Diff line
@@ -52,6 +52,19 @@ enum goya_dma_direction {
#define GOYA_PKT_CTL_MB_SHIFT		31
#define GOYA_PKT_CTL_MB_MASK		0x80000000

/* All packets have, at least, an 8-byte header, which contains
 * the packet type. The kernel driver uses the packet header for packet
 * validation and to perform any necessary required preparation before
 * sending them off to the hardware.
 */
struct goya_packet {
	__le64 header;
	/* The rest of the packet data follows. Use the corresponding
	 * packet_XXX struct to deference the data, based on packet type
	 */
	u8 contents[0];
};

struct packet_nop {
	__le32 reserved;
	__le32 ctl;