Skip to contentSkip to conversation

nvme: Add support for Apple 2018+ models

PATCH In review Benjamin Herrenschmidt 3 participants 7 messages Jul 15, 2019 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme: Add support for Apple 2018+ models

Based on reverse engineering and original patch by

Paul Pawlowski <paul at mrarm.io>

This adds support for Apple weird implementation of NVME in their 2018 or later machines. It accounts for the twice-as-big SQ entries for the IO queues, and the fact that only interrupt vector 0 appears to function properly.

Signed-off-by Benjamin Herrenschmidt <benh at kernel.crashing.org>

---

I reworked Paul's patch to be less invasive in nvme_submit_cmd() hot path, effectively only adding a shift with a value hopefully coming from the same cache line as existing stuff.

It could probably be made even less by making sq_extra_shift be instead "sq_shift" and contain the complete shift between entries, ie, 6 or 7, and then replacing the memcpy to

	&nvmeq->sq_cmds[nvmeq->sq_tail]

With something like

	((char *)nvmeq->sq_cmds) + ((size_t)nvmeq->sq_tail) << nvmeq->sq_shift

But I doubt the difference will be measurable anywhere and it makes the code grosser imho.

Note: I'm not subscribed to linux-nvme, please CC me on replies.

 drivers/nvme/host/nvme.h |  5 ++++
 drivers/nvme/host/pci.c  | 59 ++++++++++++++++++++++++++++------------
 2 files changed, 47 insertions(+), 17 deletions(-)
drivers/nvme/host/nvme.h +5 -0
index 55553d293a98..9ae53cbfb320 100644
@@ -92,6 +92,11 @@ enum nvme_quirks {
 	 * Broken Write Zeroes.
 	 */
 	NVME_QUIRK_DISABLE_WRITE_ZEROES		= (1 << 9),
+
+	/*
+	 * Apple 2018 and latter variant has a few issues
+	 */
+	NVME_QUIRK_APPLE_2018			= (1 << 10),
 };
 
 /*
drivers/nvme/host/pci.c +42 -17

149 lines not on this page · raw message

#1 lore raw

drivers/nvme/host/nvme.h enum nvme_quirks { · line 99

Quoted 4 lines
drivers/nvme/host/nvme.h +4 -0
+	/*
+	 * Apple 2018 and latter variant has a few issues
+	 */
+	NVME_QUIRK_APPLE_2018			= (1 << 10),

We try to have quirks for the actual issue, so this should be one quirk for the irq vectors issues, and another for the sq entry size. Note that NVMe actually has the concept of an I/O queue entry size (IOSQES in the Cc register based on values reported in the SQES field in Identify Controller. Do these controllers report anything interesting there?

At the very least I'd make all the terminology based on that and then just treat the Apple controllers as a buggy implementation of that model.

Btw, are there open source darwin NVMe driver that could explain this mess a little better?

drivers/nvme/host/pci.c static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq) · line 512

Quoted 10 lines
drivers/nvme/host/pci.c +4 -1
@@ -504,8 +505,11 @@ static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq)
 static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
 			    bool write_sq)
 {
+	u16 sq_actual_pos;
+
 	spin_lock(&nvmeq->sq_lock);
-	memcpy(&nvmeq->sq_cmds[nvmeq->sq_tail], cmd, sizeof(*cmd));
+	sq_actual_pos = nvmeq->sq_tail << nvmeq->sq_extra_shift;
+	memcpy(&nvmeq->sq_cmds[sq_actual_pos], cmd, sizeof(*cmd));

This is a little too magic. I think we'd better off making sq_cmds a void array and use manual indexing, at least that makes it very obvious what is going on.

drivers/nvme/host/pci.c static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved) · line 1375

Quoted 2 lines
drivers/nvme/host/pci.c +1 -1
-				nvmeq->sq_cmds, SQ_SIZE(nvmeq->q_depth));
+				nvmeq->sq_cmds, SQ_SIZE(nvmeq));

Btw, chaning SQ_SIZE to take the queue seems like something that should be split into a prep patch, making the main change a lot smaller.

drivers/nvme/host/pci.c static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled) · line 1569

Quoted 5 lines
drivers/nvme/host/pci.c +4 -1
-	if (!polled)
+	if (!polled) {
+
+		/*
+		 * On Apple 2018 or later implementations, only vector 0 is accepted

Please fix the > 80 char line.

#2 lore raw
Quoted 10 lines · Christoph Hellwig

drivers/nvme/host/nvme.h enum nvme_quirks { · line 99

Quoted 4 lines
drivers/nvme/host/nvme.h +4 -0
+	/*
+	 * Apple 2018 and latter variant has a few issues
+	 */
+	NVME_QUIRK_APPLE_2018			= (1 << 10),

We try to have quirks for the actual issue, so this should be one quirk for the irq vectors issues, and another for the sq entry size. Note that NVMe actually has the concept of an I/O queue entry size (IOSQES in the Cc register based on values reported in the SQES field in Identify Controller. Do these controllers report anything interesting there?

Ah good to know, I'll dig.

Quoted 2 lines

At the very least I'd make all the terminology based on that and then just treat the Apple controllers as a buggy implementation of that model.

Yup, sounds good. I'll poke around tomorrow.

Quoted 2 lines

Btw, are there open source darwin NVMe driver that could explain this mess a little better?

You wish...

Quoted 6 lines
Quoted 6 lines
drivers/nvme/host/pci.c +2 -0
@@ -504,8 +505,11 @@ static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq)
 static void nvme_submit_cmd(struct nvme_queue *nvmeq, struct nvme_command *cmd,
 			    bool write_sq)
 {
+	u16 sq_actual_pos;
+
Quoted 8 lines

drivers/nvme/host/pci.c static inline void nvme_write_sq_db(struct nvme_queue *nvmeq, bool write_sq) · line 512

Quoted 4 lines
drivers/nvme/host/pci.c +2 -1
 	spin_lock(&nvmeq->sq_lock);
-	memcpy(&nvmeq->sq_cmds[nvmeq->sq_tail], cmd, sizeof(*cmd));
+	sq_actual_pos = nvmeq->sq_tail << nvmeq->sq_extra_shift;
+	memcpy(&nvmeq->sq_cmds[sq_actual_pos], cmd, sizeof(*cmd));

This is a little too magic. I think we'd better off making sq_cmds a void array and use manual indexing, at least that makes it very obvious what is going on.

Ok. That's plan B as I described in the message. There's an advantage to do that, it merges the indexing shift and the quirk shift into one.

I'll look into it & respin

Quoted 5 lines

drivers/nvme/host/pci.c static enum blk_eh_timer_return nvme_timeout(struct request *req, bool reserved) · line 1375

Quoted 2 lines
drivers/nvme/host/pci.c +1 -1
-				nvmeq->sq_cmds, SQ_SIZE(nvmeq->q_depth));
+				nvmeq->sq_cmds, SQ_SIZE(nvmeq));

Btw, chaning SQ_SIZE to take the queue seems like something that should be split into a prep patch, making the main change a lot smaller.

Sure. Will do.

Quoted 7 lines

drivers/nvme/host/pci.c static int nvme_create_queue(struct nvme_queue *nvmeq, int qid, bool polled) · line 1569

Quoted 5 lines
drivers/nvme/host/pci.c +4 -1
-	if (!polled)
+	if (!polled) {
+
+		/*
+		 * On Apple 2018 or later implementations, only vector 0 is accepted

Please fix the > 80 char line.

Ok.

Thanks for the review.

Cheers,
Ben.

#3 lore raw
Quoted 12 lines · Benjamin Herrenschmidt
Quoted 10 lines · Christoph Hellwig

drivers/nvme/host/nvme.h enum nvme_quirks { · line 99

Quoted 4 lines
drivers/nvme/host/nvme.h +4 -0
+	/*
+	 * Apple 2018 and latter variant has a few issues
+	 */
+	NVME_QUIRK_APPLE_2018			= (1 << 10),

We try to have quirks for the actual issue, so this should be one quirk for the irq vectors issues, and another for the sq entry size. Note that NVMe actually has the concept of an I/O queue entry size (IOSQES in the Cc register based on values reported in the SQES field in Identify Controller. Do these controllers report anything interesting there?

Quoted 1 line

Ah good to know, I'll dig.

Interesting... so SQES is 0x76, indicating that it supports the larger entry size but not that it mandates it.

However, we configure CC:IOSQES with 6 and the HW fails unless we have the 128 bytes entry size.

So the HW is bogus, but we can probably sort that by doing a better job at fixing up SQES in the identify on the Apple HW, and then actually using it for the SQ.

I checked and CC is 0x00460001 so it takes our write of "6" fine. I think they just ignore the value.

How do you want to proceed here ? Should I go all the way at attempting to honor sqes "mandatory" size field (and quirk *that*) or just I go the simpler way and stick to shift 6 unless Apple ?

If I go the complicated path, should I do the same with cq size (knowing that no known HW has a non-4 mandatory size there and we don't know of a HW bug... yet).

Cheers,
Ben.

#4 lore raw
Quoted 32 lines · Benjamin Herrenschmidt
Quoted 12 lines · Benjamin Herrenschmidt
Quoted 10 lines · Christoph Hellwig

drivers/nvme/host/nvme.h enum nvme_quirks { · line 99

Quoted 4 lines
drivers/nvme/host/nvme.h +4 -0
+	/*
+	 * Apple 2018 and latter variant has a few issues
+	 */
+	NVME_QUIRK_APPLE_2018			= (1 << 10),

We try to have quirks for the actual issue, so this should be one quirk for the irq vectors issues, and another for the sq entry size. Note that NVMe actually has the concept of an I/O queue entry size (IOSQES in the Cc register based on values reported in the SQES field in Identify Controller. Do these controllers report anything interesting there?

Quoted 1 line

Ah good to know, I'll dig.

Interesting... so SQES is 0x76, indicating that it supports the larger entry size but not that it mandates it.

However, we configure CC:IOSQES with 6 and the HW fails unless we have the 128 bytes entry size.

So the HW is bogus, but we can probably sort that by doing a better job at fixing up SQES in the identify on the Apple HW, and then actually using it for the SQ.

I checked and CC is 0x00460001 so it takes our write of "6" fine. I think they just ignore the value.

How do you want to proceed here ? Should I go all the way at attempting to honor sqes "mandatory" size field (and quirk *that*) or just I go the simpler way and stick to shift 6 unless Apple ?

Quoted 7 lines

If I go the complicated path, should I do the same with cq size (knowing that no known HW has a non-4 mandatory size there and we don't know of a HW bug... yet).

Cheers,
Ben.

To be honest, the spec explicitly states that minimum submission queue entry size is 64 and minimum completion entry size should be is 16 bytes for NVM command set:

"Bits 3:0 define the required (i.e., minimum) Submission Queue Entry size when using the NVM Command Set. This is the minimum entry size that may be used. The value is in bytes and is reported as a power of two (2^n). The required value shall be 6, corresponding to 64."

"Bits 3:0 define the required (i.e., minimum) Completion Queue entry size when using the NVM Command Set. This is the minimum entry size that may be used. The value is in bytes and is reported as a power of two (2^n). The required value shall be 4, corresponding to 16."

Pages 136/137, NVME 1.3d.

In theory the spec allows for non NVM IO command set, and for which the sq/cq entry sizes can be of any size, as indicated in SQES/CQES and set in CC.IOCQES/CC.IOSQES, but than most of the spec won't apply to it.

Also FYI, values in CC (IOCQES/IOSQES) are for I/O queues, which kind of implies that admin queue, should always use the 64/16 bytes entries, although I haven't found any explicit mention of that.

Best regards,
Maxim Levitsky

#5 lore raw
Quoted 8 lines · Maxim Levitsky

To be honest, the spec explicitly states that minimum submission queue entry size is 64 and minimum completion entry size should be is 16 bytes for NVM command set:

"Bits 3:0 define the required (i.e., minimum) Submission Queue Entry size when using the NVM Command Set. This is the minimum entry size that may be used. The value is in bytes and is reported as a power of two (2^n). The required value shall be 6, corresponding to 64."

Yes, I saw that :-) Apple seems to ignore this and CC:IOSQES and effectively hard wire a size of 7 (128 bytes) for the IO queue.

Quoted 5 lines

"Bits 3:0 define the required (i.e., minimum) Completion Queue entry size when using the NVM Command Set. This is the minimum entry size that may be used. The value is in bytes and is reported as a power of two (2^n). The required value shall be 4, corresponding to 16."

Quoted 8 lines

Pages 136/137, NVME 1.3d.

In theory the spec allows for non NVM IO command set, and for which the sq/cq entry sizes can be of any size, as indicated in SQES/CQES and set in CC.IOCQES/CC.IOSQES, but than most of the spec won't apply to it.

Also FYI, values in CC (IOCQES/IOSQES) are for I/O queues, which kind of implies that admin queue, should always use the 64/16 bytes entries, although I haven't found any explicit mention of that.

Right, and it does on the Apple HW as well.

Cheers,
Ben.

#6 lore raw
In reply to Maxim Levitsky
Quoted 3 lines · Maxim Levitsky

To be honest, the spec explicitly states that minimum submission queue entry size is 64 and minimum completion entry size should be is 16 bytes for NVM command set:

That doesn't keep Apple from implementing whatever they want and soldering that down on their MacBook mainboards unfortunately :(