1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Apple ANS NVM Express device driver
4 * Copyright The Asahi Linux Contributors
5 *
6 * Based on the pci.c NVM Express device driver
7 * Copyright (c) 2011-2014, Intel Corporation.
8 * and on the rdma.c NVMe over Fabrics RDMA host code.
9 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
10 */
11
12 #include <linux/async.h>
13 #include <linux/blkdev.h>
14 #include <linux/blk-mq.h>
15 #include <linux/device.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/dmapool.h>
18 #include <linux/interrupt.h>
19 #include <linux/io-64-nonatomic-lo-hi.h>
20 #include <linux/io.h>
21 #include <linux/iopoll.h>
22 #include <linux/jiffies.h>
23 #include <linux/mempool.h>
24 #include <linux/module.h>
25 #include <linux/of.h>
26 #include <linux/of_platform.h>
27 #include <linux/once.h>
28 #include <linux/platform_device.h>
29 #include <linux/pm_domain.h>
30 #include <linux/soc/apple/rtkit.h>
31 #include <linux/soc/apple/sart.h>
32 #include <linux/reset.h>
33 #include <linux/time64.h>
34
35 #include "nvme.h"
36
37 #define APPLE_ANS_BOOT_TIMEOUT USEC_PER_SEC
38
39 #define APPLE_ANS_COPROC_CPU_CONTROL 0x44
40 #define APPLE_ANS_COPROC_CPU_CONTROL_RUN BIT(4)
41
42 #define APPLE_ANS_ACQ_DB 0x1004
43 #define APPLE_ANS_IOCQ_DB 0x100c
44
45 #define APPLE_ANS_MAX_PEND_CMDS_CTRL 0x1210
46
47 #define APPLE_ANS_BOOT_STATUS 0x1300
48 #define APPLE_ANS_BOOT_STATUS_OK 0xde71ce55
49
50 #define APPLE_ANS_LINEAR_SQ_CTRL 0x24908
51 #define APPLE_ANS_LINEAR_SQ_EN BIT(0)
52
53 #define APPLE_ANS_LINEAR_ASQ_DB 0x2490c
54 #define APPLE_ANS_LINEAR_IOSQ_DB 0x24910
55
56 #define APPLE_NVMMU_NUM_TCBS 0x28100
57 #define APPLE_NVMMU_ASQ_TCB_BASE 0x28108
58 #define APPLE_NVMMU_IOSQ_TCB_BASE 0x28110
59 #define APPLE_NVMMU_TCB_INVAL 0x28118
60 #define APPLE_NVMMU_TCB_STAT 0x28120
61
62 /*
63 * This controller is a bit weird in the way command tags works: Both the
64 * admin and the IO queue share the same tag space. Additionally, tags
65 * cannot be higher than 0x40 which effectively limits the combined
66 * queue depth to 0x40. Instead of wasting half of that on the admin queue
67 * which gets much less traffic we instead reduce its size here.
68 * The controller also doesn't support async event such that no space must
69 * be reserved for NVME_NR_AEN_COMMANDS.
70 */
71 #define APPLE_NVME_AQ_DEPTH 2
72 #define APPLE_NVME_AQ_MQ_TAG_DEPTH (APPLE_NVME_AQ_DEPTH - 1)
73
74 #define APPLE_NVME_IOSQES 7
75
76 /*
77 * These can be higher, but we need to ensure that any command doesn't
78 * require an sg allocation that needs more than a page of data.
79 */
80 #define NVME_MAX_KB_SZ 4096
81 #define NVME_MAX_SEGS 127
82
83 /*
84 * This controller comes with an embedded IOMMU known as NVMMU.
85 * The NVMMU is pointed to an array of TCBs indexed by the command tag.
86 * Each command must be configured inside this structure before it's allowed
87 * to execute, including commands that don't require DMA transfers.
88 *
89 * An exception to this are Apple's vendor-specific commands (opcode 0xD8 on the
90 * admin queue): Those commands must still be added to the NVMMU but the DMA
91 * buffers cannot be represented as PRPs and must instead be allowed using SART.
92 *
93 * Programming the PRPs to the same values as those in the submission queue
94 * looks rather silly at first. This hardware is however designed for a kernel
95 * that runs the NVMMU code in a higher exception level than the NVMe driver.
96 * In that setting the NVMe driver first programs the submission queue entry
97 * and then executes a hypercall to the code that is allowed to program the
98 * NVMMU. The NVMMU driver then creates a shadow copy of the PRPs while
99 * verifying that they don't point to kernel text, data, pagetables, or similar
100 * protected areas before programming the TCB to point to this shadow copy.
101 * Since Linux doesn't do any of that we may as well just point both the queue
102 * and the TCB PRP pointer to the same memory.
103 */
104 struct apple_nvmmu_tcb {
105 u8 opcode;
106
107 #define APPLE_ANS_TCB_DMA_FROM_DEVICE BIT(0)
108 #define APPLE_ANS_TCB_DMA_TO_DEVICE BIT(1)
109 u8 dma_flags;
110
111 u8 command_id;
112 u8 _unk0;
113 __le16 length;
114 u8 _unk1[18];
115 __le64 prp1;
116 __le64 prp2;
117 u8 _unk2[16];
118 u8 aes_iv[8];
119 u8 _aes_unk[64];
120 };
121
122 /*
123 * The Apple NVMe controller only supports a single admin and a single IO queue
124 * which are both limited to 64 entries and share a single interrupt.
125 *
126 * The completion queue works as usual. The submission "queue" instead is
127 * an array indexed by the command tag on this hardware. Commands must also be
128 * present in the NVMMU's tcb array. They are triggered by writing their tag to
129 * a MMIO register.
130 */
131 struct apple_nvme_queue {
132 struct nvme_command *sqes;
133 struct nvme_completion *cqes;
134 struct apple_nvmmu_tcb *tcbs;
135
136 dma_addr_t sq_dma_addr;
137 dma_addr_t cq_dma_addr;
138 dma_addr_t tcb_dma_addr;
139
140 u32 __iomem *sq_db;
141 u32 __iomem *cq_db;
142
143 u16 sq_tail;
144 u16 cq_head;
145 u8 cq_phase;
146
147 bool is_adminq;
148 bool enabled;
149 };
150
apple_nvme_queue_enabled(struct apple_nvme_queue * q)151 static inline bool apple_nvme_queue_enabled(struct apple_nvme_queue *q)
152 {
153 /* Pair with apple_nvme_enable_queue(). */
154 return smp_load_acquire(&q->enabled);
155 }
156
apple_nvme_enable_queue(struct apple_nvme_queue * q)157 static inline void apple_nvme_enable_queue(struct apple_nvme_queue *q)
158 {
159 /* Publish queue initialization before setting q->enabled. */
160 smp_store_release(&q->enabled, true);
161 }
162
apple_nvme_disable_queue(struct apple_nvme_queue * q)163 static inline void apple_nvme_disable_queue(struct apple_nvme_queue *q)
164 {
165 WRITE_ONCE(q->enabled, false);
166 }
167
168 /*
169 * The apple_nvme_iod describes the data in an I/O.
170 *
171 * The sg pointer contains the list of PRP chunk allocations in addition
172 * to the actual struct scatterlist.
173 */
174 struct apple_nvme_iod {
175 struct nvme_request req;
176 struct nvme_command cmd;
177 struct apple_nvme_queue *q;
178 int npages; /* In the PRP list. 0 means small pool in use */
179 int nents; /* Used in scatterlist */
180 dma_addr_t first_dma;
181 unsigned int dma_len; /* length of single DMA segment mapping */
182 struct scatterlist *sg;
183 };
184
185 struct apple_nvme_hw {
186 bool has_lsq_nvmmu;
187 u32 max_queue_depth;
188 };
189
190 struct apple_nvme {
191 struct device *dev;
192
193 void __iomem *mmio_coproc;
194 void __iomem *mmio_nvme;
195 const struct apple_nvme_hw *hw;
196
197 struct device **pd_dev;
198 struct device_link **pd_link;
199 int pd_count;
200
201 struct apple_sart *sart;
202 struct apple_rtkit *rtk;
203 struct reset_control *reset;
204
205 struct dma_pool *prp_page_pool;
206 struct dma_pool *prp_small_pool;
207 mempool_t *iod_mempool;
208
209 struct nvme_ctrl ctrl;
210 struct work_struct remove_work;
211
212 struct apple_nvme_queue adminq;
213 struct apple_nvme_queue ioq;
214
215 struct blk_mq_tag_set admin_tagset;
216 struct blk_mq_tag_set tagset;
217
218 int irq;
219 spinlock_t lock;
220 };
221
222 static_assert(sizeof(struct nvme_command) == 64);
223 static_assert(sizeof(struct apple_nvmmu_tcb) == 128);
224
ctrl_to_apple_nvme(struct nvme_ctrl * ctrl)225 static inline struct apple_nvme *ctrl_to_apple_nvme(struct nvme_ctrl *ctrl)
226 {
227 return container_of(ctrl, struct apple_nvme, ctrl);
228 }
229
queue_to_apple_nvme(struct apple_nvme_queue * q)230 static inline struct apple_nvme *queue_to_apple_nvme(struct apple_nvme_queue *q)
231 {
232 if (q->is_adminq)
233 return container_of(q, struct apple_nvme, adminq);
234
235 return container_of(q, struct apple_nvme, ioq);
236 }
237
apple_nvme_queue_depth(struct apple_nvme_queue * q)238 static unsigned int apple_nvme_queue_depth(struct apple_nvme_queue *q)
239 {
240 struct apple_nvme *anv = queue_to_apple_nvme(q);
241
242 if (q->is_adminq)
243 return APPLE_NVME_AQ_DEPTH;
244
245 return anv->hw->max_queue_depth;
246 }
247
apple_nvme_rtkit_crashed(void * cookie,const void * crashlog,size_t crashlog_size)248 static void apple_nvme_rtkit_crashed(void *cookie, const void *crashlog, size_t crashlog_size)
249 {
250 struct apple_nvme *anv = cookie;
251
252 dev_warn(anv->dev, "RTKit crashed; unable to recover without a reboot");
253 nvme_reset_ctrl(&anv->ctrl);
254 }
255
apple_nvme_sart_dma_setup(void * cookie,struct apple_rtkit_shmem * bfr)256 static int apple_nvme_sart_dma_setup(void *cookie,
257 struct apple_rtkit_shmem *bfr)
258 {
259 struct apple_nvme *anv = cookie;
260 int ret;
261
262 if (bfr->iova)
263 return -EINVAL;
264 if (!bfr->size)
265 return -EINVAL;
266
267 bfr->buffer =
268 dma_alloc_coherent(anv->dev, bfr->size, &bfr->iova, GFP_KERNEL);
269 if (!bfr->buffer)
270 return -ENOMEM;
271
272 ret = apple_sart_add_allowed_region(anv->sart, bfr->iova, bfr->size);
273 if (ret) {
274 dma_free_coherent(anv->dev, bfr->size, bfr->buffer, bfr->iova);
275 bfr->buffer = NULL;
276 return -ENOMEM;
277 }
278
279 return 0;
280 }
281
apple_nvme_sart_dma_destroy(void * cookie,struct apple_rtkit_shmem * bfr)282 static void apple_nvme_sart_dma_destroy(void *cookie,
283 struct apple_rtkit_shmem *bfr)
284 {
285 struct apple_nvme *anv = cookie;
286
287 apple_sart_remove_allowed_region(anv->sart, bfr->iova, bfr->size);
288 dma_free_coherent(anv->dev, bfr->size, bfr->buffer, bfr->iova);
289 }
290
291 static const struct apple_rtkit_ops apple_nvme_rtkit_ops = {
292 .crashed = apple_nvme_rtkit_crashed,
293 .shmem_setup = apple_nvme_sart_dma_setup,
294 .shmem_destroy = apple_nvme_sart_dma_destroy,
295 };
296
apple_nvmmu_inval(struct apple_nvme_queue * q,unsigned int tag)297 static void apple_nvmmu_inval(struct apple_nvme_queue *q, unsigned int tag)
298 {
299 struct apple_nvme *anv = queue_to_apple_nvme(q);
300
301 writel(tag, anv->mmio_nvme + APPLE_NVMMU_TCB_INVAL);
302 if (readl(anv->mmio_nvme + APPLE_NVMMU_TCB_STAT))
303 dev_warn_ratelimited(anv->dev,
304 "NVMMU TCB invalidation failed\n");
305 }
306
apple_nvme_submit_cmd_t8015(struct apple_nvme_queue * q,struct nvme_command * cmd)307 static void apple_nvme_submit_cmd_t8015(struct apple_nvme_queue *q,
308 struct nvme_command *cmd)
309 {
310 struct apple_nvme *anv = queue_to_apple_nvme(q);
311
312 spin_lock_irq(&anv->lock);
313
314 if (q->is_adminq)
315 memcpy(&q->sqes[q->sq_tail], cmd, sizeof(*cmd));
316 else
317 memcpy((void *)q->sqes + (q->sq_tail << APPLE_NVME_IOSQES),
318 cmd, sizeof(*cmd));
319
320 if (++q->sq_tail == apple_nvme_queue_depth(q))
321 q->sq_tail = 0;
322
323 writel(q->sq_tail, q->sq_db);
324 spin_unlock_irq(&anv->lock);
325 }
326
327
apple_nvme_submit_cmd_t8103(struct apple_nvme_queue * q,struct nvme_command * cmd)328 static void apple_nvme_submit_cmd_t8103(struct apple_nvme_queue *q,
329 struct nvme_command *cmd)
330 {
331 struct apple_nvme *anv = queue_to_apple_nvme(q);
332 u32 tag = nvme_tag_from_cid(cmd->common.command_id);
333 struct apple_nvmmu_tcb *tcb = &q->tcbs[tag];
334
335 tcb->opcode = 0;
336 tcb->prp1 = cmd->common.dptr.prp1;
337 tcb->prp2 = cmd->common.dptr.prp2;
338 tcb->length = cmd->rw.length;
339 tcb->command_id = tag;
340
341 if (!cmd->common.dptr.prp1)
342 tcb->dma_flags = 0;
343 else if (nvme_is_write(cmd))
344 tcb->dma_flags = APPLE_ANS_TCB_DMA_TO_DEVICE;
345 else
346 tcb->dma_flags = APPLE_ANS_TCB_DMA_FROM_DEVICE;
347
348 memcpy(&q->sqes[tag], cmd, sizeof(*cmd));
349
350 /*
351 * This lock here doesn't make much sense at a first glance but
352 * removing it will result in occasional missed completion
353 * interrupts even though the commands still appear on the CQ.
354 * It's unclear why this happens but our best guess is that
355 * there is a bug in the firmware triggered when a new command
356 * is issued while we're inside the irq handler between the
357 * NVMMU invalidation (and making the tag available again)
358 * and the final CQ update.
359 */
360 spin_lock_irq(&anv->lock);
361 writel(tag, q->sq_db);
362 spin_unlock_irq(&anv->lock);
363 }
364
365 /*
366 * From pci.c:
367 * Will slightly overestimate the number of pages needed. This is OK
368 * as it only leads to a small amount of wasted memory for the lifetime of
369 * the I/O.
370 */
apple_nvme_iod_alloc_size(void)371 static inline size_t apple_nvme_iod_alloc_size(void)
372 {
373 const unsigned int nprps = DIV_ROUND_UP(
374 NVME_MAX_KB_SZ + NVME_CTRL_PAGE_SIZE, NVME_CTRL_PAGE_SIZE);
375 const int npages = DIV_ROUND_UP(8 * nprps, PAGE_SIZE - 8);
376 const size_t alloc_size = sizeof(__le64 *) * npages +
377 sizeof(struct scatterlist) * NVME_MAX_SEGS;
378
379 return alloc_size;
380 }
381
apple_nvme_iod_list(struct request * req)382 static void **apple_nvme_iod_list(struct request *req)
383 {
384 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
385
386 return (void **)(iod->sg + blk_rq_nr_phys_segments(req));
387 }
388
apple_nvme_free_prps(struct apple_nvme * anv,struct request * req)389 static void apple_nvme_free_prps(struct apple_nvme *anv, struct request *req)
390 {
391 const int last_prp = NVME_CTRL_PAGE_SIZE / sizeof(__le64) - 1;
392 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
393 dma_addr_t dma_addr = iod->first_dma;
394 int i;
395
396 for (i = 0; i < iod->npages; i++) {
397 __le64 *prp_list = apple_nvme_iod_list(req)[i];
398 dma_addr_t next_dma_addr = le64_to_cpu(prp_list[last_prp]);
399
400 dma_pool_free(anv->prp_page_pool, prp_list, dma_addr);
401 dma_addr = next_dma_addr;
402 }
403 }
404
apple_nvme_unmap_data(struct apple_nvme * anv,struct request * req)405 static void apple_nvme_unmap_data(struct apple_nvme *anv, struct request *req)
406 {
407 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
408
409 if (iod->dma_len) {
410 dma_unmap_page(anv->dev, iod->first_dma, iod->dma_len,
411 rq_dma_dir(req));
412 return;
413 }
414
415 WARN_ON_ONCE(!iod->nents);
416
417 dma_unmap_sg(anv->dev, iod->sg, iod->nents, rq_dma_dir(req));
418 if (iod->npages == 0)
419 dma_pool_free(anv->prp_small_pool, apple_nvme_iod_list(req)[0],
420 iod->first_dma);
421 else
422 apple_nvme_free_prps(anv, req);
423 mempool_free(iod->sg, anv->iod_mempool);
424 }
425
apple_nvme_print_sgl(struct scatterlist * sgl,int nents)426 static void apple_nvme_print_sgl(struct scatterlist *sgl, int nents)
427 {
428 int i;
429 struct scatterlist *sg;
430
431 for_each_sg(sgl, sg, nents, i) {
432 dma_addr_t phys = sg_phys(sg);
433
434 pr_warn("sg[%d] phys_addr:%pad offset:%d length:%d dma_address:%pad dma_length:%d\n",
435 i, &phys, sg->offset, sg->length, &sg_dma_address(sg),
436 sg_dma_len(sg));
437 }
438 }
439
apple_nvme_setup_prps(struct apple_nvme * anv,struct request * req,struct nvme_rw_command * cmnd)440 static blk_status_t apple_nvme_setup_prps(struct apple_nvme *anv,
441 struct request *req,
442 struct nvme_rw_command *cmnd)
443 {
444 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
445 struct dma_pool *pool;
446 int length = blk_rq_payload_bytes(req);
447 struct scatterlist *sg = iod->sg;
448 int dma_len = sg_dma_len(sg);
449 u64 dma_addr = sg_dma_address(sg);
450 int offset = dma_addr & (NVME_CTRL_PAGE_SIZE - 1);
451 __le64 *prp_list;
452 void **list = apple_nvme_iod_list(req);
453 dma_addr_t prp_dma;
454 int nprps, i;
455
456 length -= (NVME_CTRL_PAGE_SIZE - offset);
457 if (length <= 0) {
458 iod->first_dma = 0;
459 goto done;
460 }
461
462 dma_len -= (NVME_CTRL_PAGE_SIZE - offset);
463 if (dma_len) {
464 dma_addr += (NVME_CTRL_PAGE_SIZE - offset);
465 } else {
466 sg = sg_next(sg);
467 dma_addr = sg_dma_address(sg);
468 dma_len = sg_dma_len(sg);
469 }
470
471 if (length <= NVME_CTRL_PAGE_SIZE) {
472 iod->first_dma = dma_addr;
473 goto done;
474 }
475
476 nprps = DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE);
477 if (nprps <= (256 / 8)) {
478 pool = anv->prp_small_pool;
479 iod->npages = 0;
480 } else {
481 pool = anv->prp_page_pool;
482 iod->npages = 1;
483 }
484
485 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
486 if (!prp_list) {
487 iod->first_dma = dma_addr;
488 iod->npages = -1;
489 return BLK_STS_RESOURCE;
490 }
491 list[0] = prp_list;
492 iod->first_dma = prp_dma;
493 i = 0;
494 for (;;) {
495 if (i == NVME_CTRL_PAGE_SIZE >> 3) {
496 __le64 *old_prp_list = prp_list;
497
498 prp_list = dma_pool_alloc(pool, GFP_ATOMIC, &prp_dma);
499 if (!prp_list)
500 goto free_prps;
501 list[iod->npages++] = prp_list;
502 prp_list[0] = old_prp_list[i - 1];
503 old_prp_list[i - 1] = cpu_to_le64(prp_dma);
504 i = 1;
505 }
506 prp_list[i++] = cpu_to_le64(dma_addr);
507 dma_len -= NVME_CTRL_PAGE_SIZE;
508 dma_addr += NVME_CTRL_PAGE_SIZE;
509 length -= NVME_CTRL_PAGE_SIZE;
510 if (length <= 0)
511 break;
512 if (dma_len > 0)
513 continue;
514 if (unlikely(dma_len < 0))
515 goto bad_sgl;
516 sg = sg_next(sg);
517 dma_addr = sg_dma_address(sg);
518 dma_len = sg_dma_len(sg);
519 }
520 done:
521 cmnd->dptr.prp1 = cpu_to_le64(sg_dma_address(iod->sg));
522 cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma);
523 return BLK_STS_OK;
524 free_prps:
525 apple_nvme_free_prps(anv, req);
526 return BLK_STS_RESOURCE;
527 bad_sgl:
528 WARN(DO_ONCE(apple_nvme_print_sgl, iod->sg, iod->nents),
529 "Invalid SGL for payload:%d nents:%d\n", blk_rq_payload_bytes(req),
530 iod->nents);
531 return BLK_STS_IOERR;
532 }
533
apple_nvme_setup_prp_simple(struct apple_nvme * anv,struct request * req,struct nvme_rw_command * cmnd,struct bio_vec * bv)534 static blk_status_t apple_nvme_setup_prp_simple(struct apple_nvme *anv,
535 struct request *req,
536 struct nvme_rw_command *cmnd,
537 struct bio_vec *bv)
538 {
539 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
540 unsigned int offset = bv->bv_offset & (NVME_CTRL_PAGE_SIZE - 1);
541 unsigned int first_prp_len = NVME_CTRL_PAGE_SIZE - offset;
542
543 iod->first_dma = dma_map_bvec(anv->dev, bv, rq_dma_dir(req), 0);
544 if (dma_mapping_error(anv->dev, iod->first_dma))
545 return BLK_STS_RESOURCE;
546 iod->dma_len = bv->bv_len;
547
548 cmnd->dptr.prp1 = cpu_to_le64(iod->first_dma);
549 if (bv->bv_len > first_prp_len)
550 cmnd->dptr.prp2 = cpu_to_le64(iod->first_dma + first_prp_len);
551 return BLK_STS_OK;
552 }
553
apple_nvme_map_data(struct apple_nvme * anv,struct request * req,struct nvme_command * cmnd)554 static blk_status_t apple_nvme_map_data(struct apple_nvme *anv,
555 struct request *req,
556 struct nvme_command *cmnd)
557 {
558 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
559 blk_status_t ret = BLK_STS_RESOURCE;
560 int nr_mapped;
561
562 if (blk_rq_nr_phys_segments(req) == 1) {
563 struct bio_vec bv = req_bvec(req);
564
565 if (bv.bv_offset + bv.bv_len <= NVME_CTRL_PAGE_SIZE * 2)
566 return apple_nvme_setup_prp_simple(anv, req, &cmnd->rw,
567 &bv);
568 }
569
570 iod->dma_len = 0;
571 iod->sg = mempool_alloc(anv->iod_mempool, GFP_ATOMIC);
572 if (!iod->sg)
573 return BLK_STS_RESOURCE;
574 sg_init_table(iod->sg, blk_rq_nr_phys_segments(req));
575 iod->nents = blk_rq_map_sg(req, iod->sg);
576 if (!iod->nents)
577 goto out_free_sg;
578
579 nr_mapped = dma_map_sg_attrs(anv->dev, iod->sg, iod->nents,
580 rq_dma_dir(req), DMA_ATTR_NO_WARN);
581 if (!nr_mapped)
582 goto out_free_sg;
583
584 ret = apple_nvme_setup_prps(anv, req, &cmnd->rw);
585 if (ret != BLK_STS_OK)
586 goto out_unmap_sg;
587 return BLK_STS_OK;
588
589 out_unmap_sg:
590 dma_unmap_sg(anv->dev, iod->sg, iod->nents, rq_dma_dir(req));
591 out_free_sg:
592 mempool_free(iod->sg, anv->iod_mempool);
593 return ret;
594 }
595
apple_nvme_unmap_rq(struct request * req)596 static __always_inline void apple_nvme_unmap_rq(struct request *req)
597 {
598 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
599 struct apple_nvme *anv = queue_to_apple_nvme(iod->q);
600
601 if (blk_rq_nr_phys_segments(req))
602 apple_nvme_unmap_data(anv, req);
603 }
604
apple_nvme_complete_rq(struct request * req)605 static void apple_nvme_complete_rq(struct request *req)
606 {
607 apple_nvme_unmap_rq(req);
608 nvme_complete_rq(req);
609 }
610
apple_nvme_complete_batch(struct io_comp_batch * iob)611 static void apple_nvme_complete_batch(struct io_comp_batch *iob)
612 {
613 nvme_complete_batch(iob, apple_nvme_unmap_rq);
614 }
615
apple_nvme_cqe_pending(struct apple_nvme_queue * q)616 static inline bool apple_nvme_cqe_pending(struct apple_nvme_queue *q)
617 {
618 struct nvme_completion *hcqe = &q->cqes[q->cq_head];
619
620 return (le16_to_cpu(READ_ONCE(hcqe->status)) & 1) == q->cq_phase;
621 }
622
623 static inline struct blk_mq_tags *
apple_nvme_queue_tagset(struct apple_nvme * anv,struct apple_nvme_queue * q)624 apple_nvme_queue_tagset(struct apple_nvme *anv, struct apple_nvme_queue *q)
625 {
626 if (q->is_adminq)
627 return anv->admin_tagset.tags[0];
628 else
629 return anv->tagset.tags[0];
630 }
631
apple_nvme_handle_cqe(struct apple_nvme_queue * q,struct io_comp_batch * iob,u16 idx)632 static inline void apple_nvme_handle_cqe(struct apple_nvme_queue *q,
633 struct io_comp_batch *iob, u16 idx)
634 {
635 struct apple_nvme *anv = queue_to_apple_nvme(q);
636 struct nvme_completion *cqe = &q->cqes[idx];
637 __u16 command_id = READ_ONCE(cqe->command_id);
638 struct request *req;
639
640 if (anv->hw->has_lsq_nvmmu)
641 apple_nvmmu_inval(q, command_id);
642
643 req = nvme_find_rq(apple_nvme_queue_tagset(anv, q), command_id);
644 if (unlikely(!req)) {
645 dev_warn(anv->dev, "invalid id %d completed", command_id);
646 return;
647 }
648
649 if (!nvme_try_complete_req(req, cqe->status, cqe->result) &&
650 !blk_mq_add_to_batch(req, iob,
651 nvme_req(req)->status != NVME_SC_SUCCESS,
652 apple_nvme_complete_batch))
653 apple_nvme_complete_rq(req);
654 }
655
apple_nvme_update_cq_head(struct apple_nvme_queue * q)656 static inline void apple_nvme_update_cq_head(struct apple_nvme_queue *q)
657 {
658 u32 tmp = q->cq_head + 1;
659
660 if (tmp == apple_nvme_queue_depth(q)) {
661 q->cq_head = 0;
662 q->cq_phase ^= 1;
663 } else {
664 q->cq_head = tmp;
665 }
666 }
667
apple_nvme_poll_cq(struct apple_nvme_queue * q,struct io_comp_batch * iob)668 static bool apple_nvme_poll_cq(struct apple_nvme_queue *q,
669 struct io_comp_batch *iob)
670 {
671 bool found = false;
672
673 while (apple_nvme_cqe_pending(q)) {
674 found = true;
675
676 /*
677 * load-load control dependency between phase and the rest of
678 * the cqe requires a full read memory barrier
679 */
680 dma_rmb();
681 apple_nvme_handle_cqe(q, iob, q->cq_head);
682 apple_nvme_update_cq_head(q);
683 }
684
685 if (found)
686 writel(q->cq_head, q->cq_db);
687
688 return found;
689 }
690
apple_nvme_handle_cq(struct apple_nvme_queue * q,bool force)691 static bool apple_nvme_handle_cq(struct apple_nvme_queue *q, bool force)
692 {
693 bool found;
694 DEFINE_IO_COMP_BATCH(iob);
695
696 if (!apple_nvme_queue_enabled(q) && !force)
697 return false;
698
699 found = apple_nvme_poll_cq(q, &iob);
700
701 if (!rq_list_empty(&iob.req_list))
702 apple_nvme_complete_batch(&iob);
703
704 return found;
705 }
706
apple_nvme_irq(int irq,void * data)707 static irqreturn_t apple_nvme_irq(int irq, void *data)
708 {
709 struct apple_nvme *anv = data;
710 bool handled = false;
711 unsigned long flags;
712
713 spin_lock_irqsave(&anv->lock, flags);
714 if (apple_nvme_handle_cq(&anv->ioq, false))
715 handled = true;
716 if (apple_nvme_handle_cq(&anv->adminq, false))
717 handled = true;
718 spin_unlock_irqrestore(&anv->lock, flags);
719
720 if (handled)
721 return IRQ_HANDLED;
722 return IRQ_NONE;
723 }
724
apple_nvme_create_cq(struct apple_nvme * anv)725 static int apple_nvme_create_cq(struct apple_nvme *anv)
726 {
727 struct nvme_command c = {};
728
729 /*
730 * Note: we (ab)use the fact that the prp fields survive if no data
731 * is attached to the request.
732 */
733 c.create_cq.opcode = nvme_admin_create_cq;
734 c.create_cq.prp1 = cpu_to_le64(anv->ioq.cq_dma_addr);
735 c.create_cq.cqid = cpu_to_le16(1);
736 c.create_cq.qsize = cpu_to_le16(anv->hw->max_queue_depth - 1);
737 c.create_cq.cq_flags = cpu_to_le16(NVME_QUEUE_PHYS_CONTIG | NVME_CQ_IRQ_ENABLED);
738 c.create_cq.irq_vector = cpu_to_le16(0);
739
740 return nvme_submit_sync_cmd(anv->ctrl.admin_q, &c, NULL, 0);
741 }
742
apple_nvme_remove_cq(struct apple_nvme * anv)743 static int apple_nvme_remove_cq(struct apple_nvme *anv)
744 {
745 struct nvme_command c = {};
746
747 c.delete_queue.opcode = nvme_admin_delete_cq;
748 c.delete_queue.qid = cpu_to_le16(1);
749
750 return nvme_submit_sync_cmd(anv->ctrl.admin_q, &c, NULL, 0);
751 }
752
apple_nvme_create_sq(struct apple_nvme * anv)753 static int apple_nvme_create_sq(struct apple_nvme *anv)
754 {
755 struct nvme_command c = {};
756
757 /*
758 * Note: we (ab)use the fact that the prp fields survive if no data
759 * is attached to the request.
760 */
761 c.create_sq.opcode = nvme_admin_create_sq;
762 c.create_sq.prp1 = cpu_to_le64(anv->ioq.sq_dma_addr);
763 c.create_sq.sqid = cpu_to_le16(1);
764 c.create_sq.qsize = cpu_to_le16(anv->hw->max_queue_depth - 1);
765 c.create_sq.sq_flags = cpu_to_le16(NVME_QUEUE_PHYS_CONTIG);
766 c.create_sq.cqid = cpu_to_le16(1);
767
768 return nvme_submit_sync_cmd(anv->ctrl.admin_q, &c, NULL, 0);
769 }
770
apple_nvme_remove_sq(struct apple_nvme * anv)771 static int apple_nvme_remove_sq(struct apple_nvme *anv)
772 {
773 struct nvme_command c = {};
774
775 c.delete_queue.opcode = nvme_admin_delete_sq;
776 c.delete_queue.qid = cpu_to_le16(1);
777
778 return nvme_submit_sync_cmd(anv->ctrl.admin_q, &c, NULL, 0);
779 }
780
apple_nvme_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)781 static blk_status_t apple_nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
782 const struct blk_mq_queue_data *bd)
783 {
784 struct nvme_ns *ns = hctx->queue->queuedata;
785 struct apple_nvme_queue *q = hctx->driver_data;
786 struct apple_nvme *anv = queue_to_apple_nvme(q);
787 struct request *req = bd->rq;
788 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
789 struct nvme_command *cmnd = &iod->cmd;
790 blk_status_t ret;
791
792 iod->npages = -1;
793 iod->nents = 0;
794
795 /*
796 * We should not need to do this, but we're still using this to
797 * ensure we can drain requests on a dying queue.
798 */
799 if (unlikely(!apple_nvme_queue_enabled(q)))
800 return BLK_STS_IOERR;
801
802 if (!nvme_check_ready(&anv->ctrl, req, true))
803 return nvme_fail_nonready_command(&anv->ctrl, req);
804
805 ret = nvme_setup_cmd(ns, req);
806 if (ret)
807 return ret;
808
809 if (blk_rq_nr_phys_segments(req)) {
810 ret = apple_nvme_map_data(anv, req, cmnd);
811 if (ret)
812 goto out_free_cmd;
813 }
814
815 nvme_start_request(req);
816
817 if (anv->hw->has_lsq_nvmmu)
818 apple_nvme_submit_cmd_t8103(q, cmnd);
819 else
820 apple_nvme_submit_cmd_t8015(q, cmnd);
821
822 return BLK_STS_OK;
823
824 out_free_cmd:
825 nvme_cleanup_cmd(req);
826 return ret;
827 }
828
apple_nvme_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)829 static int apple_nvme_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
830 unsigned int hctx_idx)
831 {
832 hctx->driver_data = data;
833 return 0;
834 }
835
apple_nvme_init_request(struct blk_mq_tag_set * set,struct request * req,unsigned int hctx_idx,int numa_node)836 static int apple_nvme_init_request(struct blk_mq_tag_set *set,
837 struct request *req, unsigned int hctx_idx,
838 int numa_node)
839 {
840 struct apple_nvme_queue *q = set->driver_data;
841 struct apple_nvme *anv = queue_to_apple_nvme(q);
842 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
843 struct nvme_request *nreq = nvme_req(req);
844
845 iod->q = q;
846 nreq->ctrl = &anv->ctrl;
847 nreq->cmd = &iod->cmd;
848
849 return 0;
850 }
851
apple_nvme_disable(struct apple_nvme * anv,bool shutdown)852 static void apple_nvme_disable(struct apple_nvme *anv, bool shutdown)
853 {
854 enum nvme_ctrl_state state = nvme_ctrl_state(&anv->ctrl);
855 u32 csts = readl(anv->mmio_nvme + NVME_REG_CSTS);
856 bool dead = false, freeze = false;
857 unsigned long flags;
858
859 if (apple_rtkit_is_crashed(anv->rtk))
860 dead = true;
861 if (!(csts & NVME_CSTS_RDY))
862 dead = true;
863 if (csts & NVME_CSTS_CFS)
864 dead = true;
865
866 if (state == NVME_CTRL_LIVE ||
867 state == NVME_CTRL_RESETTING) {
868 freeze = true;
869 nvme_start_freeze(&anv->ctrl);
870 }
871
872 /*
873 * Give the controller a chance to complete all entered requests if
874 * doing a safe shutdown.
875 */
876 if (!dead && shutdown && freeze)
877 nvme_wait_freeze_timeout(&anv->ctrl);
878
879 nvme_quiesce_io_queues(&anv->ctrl);
880
881 if (!dead) {
882 if (apple_nvme_queue_enabled(&anv->ioq)) {
883 apple_nvme_remove_sq(anv);
884 apple_nvme_remove_cq(anv);
885 }
886
887 /*
888 * Always disable the NVMe controller after shutdown.
889 * We need to do this to bring it back up later anyway, and we
890 * can't do it while the firmware is not running (e.g. in the
891 * resume reset path before RTKit is initialized), so for Apple
892 * controllers it makes sense to unconditionally do it here.
893 * Additionally, this sequence of events is reliable, while
894 * others (like disabling after bringing back the firmware on
895 * resume) seem to run into trouble under some circumstances.
896 *
897 * Both U-Boot and m1n1 also use this convention (i.e. an ANS
898 * NVMe controller is handed off with firmware shut down, in an
899 * NVMe disabled state, after a clean shutdown).
900 */
901 if (shutdown)
902 nvme_disable_ctrl(&anv->ctrl, shutdown);
903 nvme_disable_ctrl(&anv->ctrl, false);
904 }
905
906 apple_nvme_disable_queue(&anv->ioq);
907 apple_nvme_disable_queue(&anv->adminq);
908 mb(); /* ensure that nvme_queue_rq() sees that enabled is cleared */
909 nvme_quiesce_admin_queue(&anv->ctrl);
910
911 /* last chance to complete any requests before nvme_cancel_request */
912 spin_lock_irqsave(&anv->lock, flags);
913 apple_nvme_handle_cq(&anv->ioq, true);
914 apple_nvme_handle_cq(&anv->adminq, true);
915 spin_unlock_irqrestore(&anv->lock, flags);
916
917 nvme_cancel_tagset(&anv->ctrl);
918 nvme_cancel_admin_tagset(&anv->ctrl);
919
920 /*
921 * The driver will not be starting up queues again if shutting down so
922 * must flush all entered requests to their failed completion to avoid
923 * deadlocking blk-mq hot-cpu notifier.
924 */
925 if (shutdown) {
926 nvme_unquiesce_io_queues(&anv->ctrl);
927 nvme_unquiesce_admin_queue(&anv->ctrl);
928 }
929 }
930
apple_nvme_timeout(struct request * req)931 static enum blk_eh_timer_return apple_nvme_timeout(struct request *req)
932 {
933 struct apple_nvme_iod *iod = blk_mq_rq_to_pdu(req);
934 struct apple_nvme_queue *q = iod->q;
935 struct apple_nvme *anv = queue_to_apple_nvme(q);
936 unsigned long flags;
937 u32 csts = readl(anv->mmio_nvme + NVME_REG_CSTS);
938
939 if (nvme_ctrl_state(&anv->ctrl) != NVME_CTRL_LIVE) {
940 /*
941 * From rdma.c:
942 * If we are resetting, connecting or deleting we should
943 * complete immediately because we may block controller
944 * teardown or setup sequence
945 * - ctrl disable/shutdown fabrics requests
946 * - connect requests
947 * - initialization admin requests
948 * - I/O requests that entered after unquiescing and
949 * the controller stopped responding
950 *
951 * All other requests should be cancelled by the error
952 * recovery work, so it's fine that we fail it here.
953 */
954 dev_warn(anv->dev,
955 "I/O %d(aq:%d) timeout while not in live state\n",
956 req->tag, q->is_adminq);
957 if (blk_mq_request_started(req) &&
958 !blk_mq_request_completed(req)) {
959 nvme_req(req)->status = NVME_SC_HOST_ABORTED_CMD;
960 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
961 blk_mq_complete_request(req);
962 }
963 return BLK_EH_DONE;
964 }
965
966 /* check if we just missed an interrupt if we're still alive */
967 if (!apple_rtkit_is_crashed(anv->rtk) && !(csts & NVME_CSTS_CFS)) {
968 spin_lock_irqsave(&anv->lock, flags);
969 apple_nvme_handle_cq(q, false);
970 spin_unlock_irqrestore(&anv->lock, flags);
971 if (blk_mq_request_completed(req)) {
972 dev_warn(anv->dev,
973 "I/O %d(aq:%d) timeout: completion polled\n",
974 req->tag, q->is_adminq);
975 return BLK_EH_DONE;
976 }
977 }
978
979 /*
980 * aborting commands isn't supported which leaves a full reset as our
981 * only option here
982 */
983 dev_warn(anv->dev, "I/O %d(aq:%d) timeout: resetting controller\n",
984 req->tag, q->is_adminq);
985 nvme_req(req)->flags |= NVME_REQ_CANCELLED;
986 apple_nvme_disable(anv, false);
987 nvme_reset_ctrl(&anv->ctrl);
988 return BLK_EH_DONE;
989 }
990
apple_nvme_poll(struct blk_mq_hw_ctx * hctx,struct io_comp_batch * iob)991 static int apple_nvme_poll(struct blk_mq_hw_ctx *hctx,
992 struct io_comp_batch *iob)
993 {
994 struct apple_nvme_queue *q = hctx->driver_data;
995 struct apple_nvme *anv = queue_to_apple_nvme(q);
996 bool found;
997 unsigned long flags;
998
999 spin_lock_irqsave(&anv->lock, flags);
1000 found = apple_nvme_poll_cq(q, iob);
1001 spin_unlock_irqrestore(&anv->lock, flags);
1002
1003 return found;
1004 }
1005
1006 static const struct blk_mq_ops apple_nvme_mq_admin_ops = {
1007 .queue_rq = apple_nvme_queue_rq,
1008 .complete = apple_nvme_complete_rq,
1009 .init_hctx = apple_nvme_init_hctx,
1010 .init_request = apple_nvme_init_request,
1011 .timeout = apple_nvme_timeout,
1012 };
1013
1014 static const struct blk_mq_ops apple_nvme_mq_ops = {
1015 .queue_rq = apple_nvme_queue_rq,
1016 .complete = apple_nvme_complete_rq,
1017 .init_hctx = apple_nvme_init_hctx,
1018 .init_request = apple_nvme_init_request,
1019 .timeout = apple_nvme_timeout,
1020 .poll = apple_nvme_poll,
1021 };
1022
apple_nvme_init_queue(struct apple_nvme_queue * q)1023 static void apple_nvme_init_queue(struct apple_nvme_queue *q)
1024 {
1025 unsigned int depth = apple_nvme_queue_depth(q);
1026 struct apple_nvme *anv = queue_to_apple_nvme(q);
1027
1028 q->sq_tail = 0;
1029 q->cq_head = 0;
1030 q->cq_phase = 1;
1031 if (anv->hw->has_lsq_nvmmu)
1032 memset(q->tcbs, 0, anv->hw->max_queue_depth
1033 * sizeof(struct apple_nvmmu_tcb));
1034 memset(q->cqes, 0, depth * sizeof(struct nvme_completion));
1035 apple_nvme_enable_queue(q);
1036 }
1037
apple_nvme_reset_work(struct work_struct * work)1038 static void apple_nvme_reset_work(struct work_struct *work)
1039 {
1040 unsigned int nr_io_queues = 1;
1041 int ret;
1042 u32 boot_status, aqa;
1043 struct apple_nvme *anv =
1044 container_of(work, struct apple_nvme, ctrl.reset_work);
1045 enum nvme_ctrl_state state = nvme_ctrl_state(&anv->ctrl);
1046
1047 if (state != NVME_CTRL_RESETTING) {
1048 dev_warn(anv->dev, "ctrl state %d is not RESETTING\n", state);
1049 ret = -ENODEV;
1050 goto out;
1051 }
1052
1053 /* there's unfortunately no known way to recover if RTKit crashed :( */
1054 if (apple_rtkit_is_crashed(anv->rtk)) {
1055 dev_err(anv->dev,
1056 "RTKit has crashed without any way to recover.");
1057 ret = -EIO;
1058 goto out;
1059 }
1060
1061 /* RTKit must be shut down cleanly for the (soft)-reset to work */
1062 if (apple_rtkit_is_running(anv->rtk)) {
1063 /* reset the controller if it is enabled */
1064 if (anv->ctrl.ctrl_config & NVME_CC_ENABLE)
1065 apple_nvme_disable(anv, false);
1066 dev_dbg(anv->dev, "Trying to shut down RTKit before reset.");
1067 ret = apple_rtkit_shutdown(anv->rtk);
1068 if (ret)
1069 goto out;
1070
1071 writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL);
1072 }
1073
1074 /*
1075 * Only do the soft-reset if the CPU is not running, which means either we
1076 * or the previous stage shut it down cleanly.
1077 */
1078 if (!(readl(anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL) &
1079 APPLE_ANS_COPROC_CPU_CONTROL_RUN)) {
1080
1081 ret = reset_control_assert(anv->reset);
1082 if (ret)
1083 goto out;
1084
1085 ret = apple_rtkit_reinit(anv->rtk);
1086 if (ret)
1087 goto out;
1088
1089 ret = reset_control_deassert(anv->reset);
1090 if (ret)
1091 goto out;
1092
1093 writel(APPLE_ANS_COPROC_CPU_CONTROL_RUN,
1094 anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL);
1095
1096 ret = apple_rtkit_boot(anv->rtk);
1097 } else {
1098 ret = apple_rtkit_wake(anv->rtk);
1099 }
1100
1101 if (ret) {
1102 dev_err(anv->dev, "ANS did not boot");
1103 goto out;
1104 }
1105
1106 ret = readl_poll_timeout(anv->mmio_nvme + APPLE_ANS_BOOT_STATUS,
1107 boot_status,
1108 boot_status == APPLE_ANS_BOOT_STATUS_OK,
1109 USEC_PER_MSEC, APPLE_ANS_BOOT_TIMEOUT);
1110 if (ret) {
1111 dev_err(anv->dev, "ANS did not initialize");
1112 goto out;
1113 }
1114
1115 dev_dbg(anv->dev, "ANS booted successfully.");
1116
1117 /*
1118 * Limit the max command size to prevent iod->sg allocations going
1119 * over a single page.
1120 */
1121 anv->ctrl.max_hw_sectors = min_t(u32, NVME_MAX_KB_SZ << 1,
1122 dma_max_mapping_size(anv->dev) >> 9);
1123 anv->ctrl.max_segments = NVME_MAX_SEGS;
1124
1125 dma_set_max_seg_size(anv->dev, 0xffffffff);
1126
1127 if (anv->hw->has_lsq_nvmmu) {
1128 /*
1129 * Enable NVMMU and linear submission queues which is required
1130 * since T6000.
1131 */
1132 writel(APPLE_ANS_LINEAR_SQ_EN,
1133 anv->mmio_nvme + APPLE_ANS_LINEAR_SQ_CTRL);
1134
1135 /* Allow as many pending command as possible for both queues */
1136 writel(anv->hw->max_queue_depth
1137 | (anv->hw->max_queue_depth << 16), anv->mmio_nvme
1138 + APPLE_ANS_MAX_PEND_CMDS_CTRL);
1139
1140 /* Setup the NVMMU for the maximum admin and IO queue depth */
1141 writel(anv->hw->max_queue_depth - 1,
1142 anv->mmio_nvme + APPLE_NVMMU_NUM_TCBS);
1143 }
1144
1145 /* Setup the admin queue */
1146 aqa = APPLE_NVME_AQ_DEPTH - 1;
1147 aqa |= aqa << 16;
1148 writel(aqa, anv->mmio_nvme + NVME_REG_AQA);
1149 writeq(anv->adminq.sq_dma_addr, anv->mmio_nvme + NVME_REG_ASQ);
1150 writeq(anv->adminq.cq_dma_addr, anv->mmio_nvme + NVME_REG_ACQ);
1151
1152 if (anv->hw->has_lsq_nvmmu) {
1153 /* Setup NVMMU for both queues */
1154 writeq(anv->adminq.tcb_dma_addr,
1155 anv->mmio_nvme + APPLE_NVMMU_ASQ_TCB_BASE);
1156 writeq(anv->ioq.tcb_dma_addr,
1157 anv->mmio_nvme + APPLE_NVMMU_IOSQ_TCB_BASE);
1158 }
1159
1160 anv->ctrl.sqsize =
1161 anv->hw->max_queue_depth - 1; /* 0's based queue depth */
1162 anv->ctrl.cap = readq(anv->mmio_nvme + NVME_REG_CAP);
1163
1164 dev_dbg(anv->dev, "Enabling controller now");
1165 ret = nvme_enable_ctrl(&anv->ctrl);
1166 if (ret)
1167 goto out;
1168
1169 dev_dbg(anv->dev, "Starting admin queue");
1170 apple_nvme_init_queue(&anv->adminq);
1171 nvme_unquiesce_admin_queue(&anv->ctrl);
1172
1173 if (!nvme_change_ctrl_state(&anv->ctrl, NVME_CTRL_CONNECTING)) {
1174 dev_warn(anv->ctrl.device,
1175 "failed to mark controller CONNECTING\n");
1176 ret = -ENODEV;
1177 goto out;
1178 }
1179
1180 ret = nvme_init_ctrl_finish(&anv->ctrl, false);
1181 if (ret)
1182 goto out;
1183
1184 dev_dbg(anv->dev, "Creating IOCQ");
1185 ret = apple_nvme_create_cq(anv);
1186 if (ret)
1187 goto out;
1188 dev_dbg(anv->dev, "Creating IOSQ");
1189 ret = apple_nvme_create_sq(anv);
1190 if (ret)
1191 goto out_remove_cq;
1192
1193 apple_nvme_init_queue(&anv->ioq);
1194 nr_io_queues = 1;
1195 ret = nvme_set_queue_count(&anv->ctrl, &nr_io_queues);
1196 if (ret)
1197 goto out_remove_sq;
1198 if (nr_io_queues != 1) {
1199 ret = -ENXIO;
1200 goto out_remove_sq;
1201 }
1202
1203 anv->ctrl.queue_count = nr_io_queues + 1;
1204
1205 nvme_unquiesce_io_queues(&anv->ctrl);
1206 nvme_wait_freeze(&anv->ctrl);
1207 blk_mq_update_nr_hw_queues(&anv->tagset, 1);
1208 nvme_unfreeze(&anv->ctrl);
1209
1210 if (!nvme_change_ctrl_state(&anv->ctrl, NVME_CTRL_LIVE)) {
1211 dev_warn(anv->ctrl.device,
1212 "failed to mark controller live state\n");
1213 ret = -ENODEV;
1214 goto out_remove_sq;
1215 }
1216
1217 nvme_start_ctrl(&anv->ctrl);
1218
1219 dev_dbg(anv->dev, "ANS boot and NVMe init completed.");
1220 return;
1221
1222 out_remove_sq:
1223 apple_nvme_remove_sq(anv);
1224 out_remove_cq:
1225 apple_nvme_remove_cq(anv);
1226 out:
1227 dev_warn(anv->ctrl.device, "Reset failure status: %d\n", ret);
1228 nvme_change_ctrl_state(&anv->ctrl, NVME_CTRL_DELETING);
1229 nvme_get_ctrl(&anv->ctrl);
1230 apple_nvme_disable(anv, false);
1231 nvme_mark_namespaces_dead(&anv->ctrl);
1232 if (!queue_work(nvme_wq, &anv->remove_work))
1233 nvme_put_ctrl(&anv->ctrl);
1234 }
1235
apple_nvme_remove_dead_ctrl_work(struct work_struct * work)1236 static void apple_nvme_remove_dead_ctrl_work(struct work_struct *work)
1237 {
1238 struct apple_nvme *anv =
1239 container_of(work, struct apple_nvme, remove_work);
1240
1241 nvme_put_ctrl(&anv->ctrl);
1242 device_release_driver(anv->dev);
1243 }
1244
apple_nvme_reg_read32(struct nvme_ctrl * ctrl,u32 off,u32 * val)1245 static int apple_nvme_reg_read32(struct nvme_ctrl *ctrl, u32 off, u32 *val)
1246 {
1247 *val = readl(ctrl_to_apple_nvme(ctrl)->mmio_nvme + off);
1248 return 0;
1249 }
1250
apple_nvme_reg_write32(struct nvme_ctrl * ctrl,u32 off,u32 val)1251 static int apple_nvme_reg_write32(struct nvme_ctrl *ctrl, u32 off, u32 val)
1252 {
1253 writel(val, ctrl_to_apple_nvme(ctrl)->mmio_nvme + off);
1254 return 0;
1255 }
1256
apple_nvme_reg_read64(struct nvme_ctrl * ctrl,u32 off,u64 * val)1257 static int apple_nvme_reg_read64(struct nvme_ctrl *ctrl, u32 off, u64 *val)
1258 {
1259 *val = readq(ctrl_to_apple_nvme(ctrl)->mmio_nvme + off);
1260 return 0;
1261 }
1262
apple_nvme_get_address(struct nvme_ctrl * ctrl,char * buf,int size)1263 static int apple_nvme_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
1264 {
1265 struct device *dev = ctrl_to_apple_nvme(ctrl)->dev;
1266
1267 return snprintf(buf, size, "%s\n", dev_name(dev));
1268 }
1269
apple_nvme_free_ctrl(struct nvme_ctrl * ctrl)1270 static void apple_nvme_free_ctrl(struct nvme_ctrl *ctrl)
1271 {
1272 put_device(ctrl->dev);
1273 }
1274
1275 static const struct nvme_ctrl_ops nvme_ctrl_ops = {
1276 .name = "apple-nvme",
1277 .module = THIS_MODULE,
1278 .flags = 0,
1279 .reg_read32 = apple_nvme_reg_read32,
1280 .reg_write32 = apple_nvme_reg_write32,
1281 .reg_read64 = apple_nvme_reg_read64,
1282 .free_ctrl = apple_nvme_free_ctrl,
1283 .get_address = apple_nvme_get_address,
1284 .get_virt_boundary = nvme_get_virt_boundary,
1285 };
1286
apple_nvme_async_probe(void * data,async_cookie_t cookie)1287 static void apple_nvme_async_probe(void *data, async_cookie_t cookie)
1288 {
1289 struct apple_nvme *anv = data;
1290
1291 flush_work(&anv->ctrl.reset_work);
1292 flush_work(&anv->ctrl.scan_work);
1293 nvme_put_ctrl(&anv->ctrl);
1294 }
1295
devm_apple_nvme_put_tag_set(void * data)1296 static void devm_apple_nvme_put_tag_set(void *data)
1297 {
1298 blk_mq_free_tag_set(data);
1299 }
1300
apple_nvme_alloc_tagsets(struct apple_nvme * anv)1301 static int apple_nvme_alloc_tagsets(struct apple_nvme *anv)
1302 {
1303 int ret;
1304
1305 anv->admin_tagset.ops = &apple_nvme_mq_admin_ops;
1306 anv->admin_tagset.nr_hw_queues = 1;
1307 anv->admin_tagset.queue_depth = APPLE_NVME_AQ_MQ_TAG_DEPTH;
1308 anv->admin_tagset.timeout = NVME_ADMIN_TIMEOUT;
1309 anv->admin_tagset.numa_node = NUMA_NO_NODE;
1310 anv->admin_tagset.cmd_size = sizeof(struct apple_nvme_iod);
1311 anv->admin_tagset.driver_data = &anv->adminq;
1312
1313 ret = blk_mq_alloc_tag_set(&anv->admin_tagset);
1314 if (ret)
1315 return ret;
1316 ret = devm_add_action_or_reset(anv->dev, devm_apple_nvme_put_tag_set,
1317 &anv->admin_tagset);
1318 if (ret)
1319 return ret;
1320
1321 anv->tagset.ops = &apple_nvme_mq_ops;
1322 anv->tagset.nr_hw_queues = 1;
1323 anv->tagset.nr_maps = 1;
1324 /*
1325 * Tags are used as an index to the NVMMU and must be unique across
1326 * both queues. The admin queue gets the first APPLE_NVME_AQ_DEPTH which
1327 * must be marked as reserved in the IO queue.
1328 */
1329 anv->tagset.reserved_tags = APPLE_NVME_AQ_DEPTH;
1330 anv->tagset.queue_depth = anv->hw->max_queue_depth - 1;
1331 anv->tagset.timeout = NVME_IO_TIMEOUT;
1332 anv->tagset.numa_node = NUMA_NO_NODE;
1333 anv->tagset.cmd_size = sizeof(struct apple_nvme_iod);
1334 anv->tagset.driver_data = &anv->ioq;
1335
1336 ret = blk_mq_alloc_tag_set(&anv->tagset);
1337 if (ret)
1338 return ret;
1339 ret = devm_add_action_or_reset(anv->dev, devm_apple_nvme_put_tag_set,
1340 &anv->tagset);
1341 if (ret)
1342 return ret;
1343
1344 anv->ctrl.admin_tagset = &anv->admin_tagset;
1345 anv->ctrl.tagset = &anv->tagset;
1346
1347 return 0;
1348 }
1349
apple_nvme_queue_alloc(struct apple_nvme * anv,struct apple_nvme_queue * q)1350 static int apple_nvme_queue_alloc(struct apple_nvme *anv,
1351 struct apple_nvme_queue *q)
1352 {
1353 unsigned int depth = apple_nvme_queue_depth(q);
1354 size_t iosq_size;
1355
1356 q->cqes = dmam_alloc_coherent(anv->dev,
1357 depth * sizeof(struct nvme_completion),
1358 &q->cq_dma_addr, GFP_KERNEL);
1359 if (!q->cqes)
1360 return -ENOMEM;
1361
1362 if (anv->hw->has_lsq_nvmmu)
1363 iosq_size = depth * sizeof(struct nvme_command);
1364 else
1365 iosq_size = depth << APPLE_NVME_IOSQES;
1366
1367 q->sqes = dmam_alloc_coherent(anv->dev, iosq_size,
1368 &q->sq_dma_addr, GFP_KERNEL);
1369 if (!q->sqes)
1370 return -ENOMEM;
1371
1372 if (anv->hw->has_lsq_nvmmu) {
1373 /*
1374 * We need the maximum queue depth here because the NVMMU only
1375 * has a single depth configuration shared between both queues.
1376 */
1377 q->tcbs = dmam_alloc_coherent(anv->dev,
1378 anv->hw->max_queue_depth *
1379 sizeof(struct apple_nvmmu_tcb),
1380 &q->tcb_dma_addr, GFP_KERNEL);
1381 if (!q->tcbs)
1382 return -ENOMEM;
1383 }
1384
1385 /*
1386 * initialize phase to make sure the allocated and empty memory
1387 * doesn't look like a full cq already.
1388 */
1389 q->cq_phase = 1;
1390 return 0;
1391 }
1392
apple_nvme_detach_genpd(struct apple_nvme * anv)1393 static void apple_nvme_detach_genpd(struct apple_nvme *anv)
1394 {
1395 int i;
1396
1397 if (anv->pd_count <= 1)
1398 return;
1399
1400 for (i = anv->pd_count - 1; i >= 0; i--) {
1401 if (anv->pd_link[i])
1402 device_link_del(anv->pd_link[i]);
1403 if (!IS_ERR_OR_NULL(anv->pd_dev[i]))
1404 dev_pm_domain_detach(anv->pd_dev[i], true);
1405 }
1406 }
1407
apple_nvme_attach_genpd(struct apple_nvme * anv)1408 static int apple_nvme_attach_genpd(struct apple_nvme *anv)
1409 {
1410 struct device *dev = anv->dev;
1411 int i;
1412
1413 anv->pd_count = of_count_phandle_with_args(
1414 dev->of_node, "power-domains", "#power-domain-cells");
1415 if (anv->pd_count <= 1)
1416 return 0;
1417
1418 anv->pd_dev = devm_kcalloc(dev, anv->pd_count, sizeof(*anv->pd_dev),
1419 GFP_KERNEL);
1420 if (!anv->pd_dev)
1421 return -ENOMEM;
1422
1423 anv->pd_link = devm_kcalloc(dev, anv->pd_count, sizeof(*anv->pd_link),
1424 GFP_KERNEL);
1425 if (!anv->pd_link)
1426 return -ENOMEM;
1427
1428 for (i = 0; i < anv->pd_count; i++) {
1429 anv->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i);
1430 if (IS_ERR(anv->pd_dev[i])) {
1431 apple_nvme_detach_genpd(anv);
1432 return PTR_ERR(anv->pd_dev[i]);
1433 }
1434
1435 anv->pd_link[i] = device_link_add(dev, anv->pd_dev[i],
1436 DL_FLAG_STATELESS |
1437 DL_FLAG_PM_RUNTIME |
1438 DL_FLAG_RPM_ACTIVE);
1439 if (!anv->pd_link[i]) {
1440 apple_nvme_detach_genpd(anv);
1441 return -EINVAL;
1442 }
1443 }
1444
1445 return 0;
1446 }
1447
devm_apple_nvme_mempool_destroy(void * data)1448 static void devm_apple_nvme_mempool_destroy(void *data)
1449 {
1450 mempool_destroy(data);
1451 }
1452
apple_nvme_alloc(struct platform_device * pdev)1453 static struct apple_nvme *apple_nvme_alloc(struct platform_device *pdev)
1454 {
1455 struct device *dev = &pdev->dev;
1456 struct apple_nvme *anv;
1457 int ret;
1458
1459 anv = devm_kzalloc(dev, sizeof(*anv), GFP_KERNEL);
1460 if (!anv)
1461 return ERR_PTR(-ENOMEM);
1462
1463 anv->dev = get_device(dev);
1464 anv->adminq.is_adminq = true;
1465 platform_set_drvdata(pdev, anv);
1466
1467 anv->hw = of_device_get_match_data(&pdev->dev);
1468 if (!anv->hw) {
1469 ret = -ENODEV;
1470 goto put_dev;
1471 }
1472
1473 ret = apple_nvme_attach_genpd(anv);
1474 if (ret < 0) {
1475 dev_err_probe(dev, ret, "Failed to attach power domains");
1476 goto put_dev;
1477 }
1478 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) {
1479 ret = -ENXIO;
1480 goto put_dev;
1481 }
1482
1483 anv->irq = platform_get_irq(pdev, 0);
1484 if (anv->irq < 0) {
1485 ret = anv->irq;
1486 goto put_dev;
1487 }
1488 if (!anv->irq) {
1489 ret = -ENXIO;
1490 goto put_dev;
1491 }
1492
1493 anv->mmio_coproc = devm_platform_ioremap_resource_byname(pdev, "ans");
1494 if (IS_ERR(anv->mmio_coproc)) {
1495 ret = PTR_ERR(anv->mmio_coproc);
1496 goto put_dev;
1497 }
1498 anv->mmio_nvme = devm_platform_ioremap_resource_byname(pdev, "nvme");
1499 if (IS_ERR(anv->mmio_nvme)) {
1500 ret = PTR_ERR(anv->mmio_nvme);
1501 goto put_dev;
1502 }
1503
1504 if (anv->hw->has_lsq_nvmmu) {
1505 anv->adminq.sq_db = anv->mmio_nvme + APPLE_ANS_LINEAR_ASQ_DB;
1506 anv->adminq.cq_db = anv->mmio_nvme + APPLE_ANS_ACQ_DB;
1507 anv->ioq.sq_db = anv->mmio_nvme + APPLE_ANS_LINEAR_IOSQ_DB;
1508 anv->ioq.cq_db = anv->mmio_nvme + APPLE_ANS_IOCQ_DB;
1509 } else {
1510 anv->adminq.sq_db = anv->mmio_nvme + NVME_REG_DBS;
1511 anv->adminq.cq_db = anv->mmio_nvme + APPLE_ANS_ACQ_DB;
1512 anv->ioq.sq_db = anv->mmio_nvme + NVME_REG_DBS + 8;
1513 anv->ioq.cq_db = anv->mmio_nvme + APPLE_ANS_IOCQ_DB;
1514 }
1515
1516 anv->sart = devm_apple_sart_get(dev);
1517 if (IS_ERR(anv->sart)) {
1518 ret = dev_err_probe(dev, PTR_ERR(anv->sart),
1519 "Failed to initialize SART");
1520 goto put_dev;
1521 }
1522
1523 anv->reset = devm_reset_control_array_get_exclusive(anv->dev);
1524 if (IS_ERR(anv->reset)) {
1525 ret = dev_err_probe(dev, PTR_ERR(anv->reset),
1526 "Failed to get reset control");
1527 goto put_dev;
1528 }
1529
1530 INIT_WORK(&anv->ctrl.reset_work, apple_nvme_reset_work);
1531 INIT_WORK(&anv->remove_work, apple_nvme_remove_dead_ctrl_work);
1532 spin_lock_init(&anv->lock);
1533
1534 ret = apple_nvme_queue_alloc(anv, &anv->adminq);
1535 if (ret)
1536 goto put_dev;
1537 ret = apple_nvme_queue_alloc(anv, &anv->ioq);
1538 if (ret)
1539 goto put_dev;
1540
1541 anv->prp_page_pool = dmam_pool_create("prp list page", anv->dev,
1542 NVME_CTRL_PAGE_SIZE,
1543 NVME_CTRL_PAGE_SIZE, 0);
1544 if (!anv->prp_page_pool) {
1545 ret = -ENOMEM;
1546 goto put_dev;
1547 }
1548
1549 anv->prp_small_pool =
1550 dmam_pool_create("prp list 256", anv->dev, 256, 256, 0);
1551 if (!anv->prp_small_pool) {
1552 ret = -ENOMEM;
1553 goto put_dev;
1554 }
1555
1556 WARN_ON_ONCE(apple_nvme_iod_alloc_size() > PAGE_SIZE);
1557 anv->iod_mempool =
1558 mempool_create_kmalloc_pool(1, apple_nvme_iod_alloc_size());
1559 if (!anv->iod_mempool) {
1560 ret = -ENOMEM;
1561 goto put_dev;
1562 }
1563 ret = devm_add_action_or_reset(anv->dev,
1564 devm_apple_nvme_mempool_destroy, anv->iod_mempool);
1565 if (ret)
1566 goto put_dev;
1567
1568 ret = apple_nvme_alloc_tagsets(anv);
1569 if (ret)
1570 goto put_dev;
1571
1572 ret = devm_request_irq(anv->dev, anv->irq, apple_nvme_irq, 0,
1573 "nvme-apple", anv);
1574 if (ret)
1575 goto put_dev;
1576
1577 anv->rtk =
1578 devm_apple_rtkit_init(dev, anv, NULL, 0, &apple_nvme_rtkit_ops);
1579 if (IS_ERR(anv->rtk)) {
1580 ret = dev_err_probe(dev, PTR_ERR(anv->rtk),
1581 "Failed to initialize RTKit");
1582 goto put_dev;
1583 }
1584
1585 ret = nvme_init_ctrl(&anv->ctrl, anv->dev, &nvme_ctrl_ops,
1586 NVME_QUIRK_SKIP_CID_GEN | NVME_QUIRK_IDENTIFY_CNS |
1587 NVME_QUIRK_ADMIN_PAGE_ALIGN);
1588 if (ret) {
1589 dev_err_probe(dev, ret, "Failed to initialize nvme_ctrl");
1590 goto put_dev;
1591 }
1592
1593 return anv;
1594 put_dev:
1595 apple_nvme_detach_genpd(anv);
1596 put_device(anv->dev);
1597 return ERR_PTR(ret);
1598 }
1599
apple_nvme_probe(struct platform_device * pdev)1600 static int apple_nvme_probe(struct platform_device *pdev)
1601 {
1602 struct apple_nvme *anv;
1603 int ret;
1604
1605 anv = apple_nvme_alloc(pdev);
1606 if (IS_ERR(anv))
1607 return PTR_ERR(anv);
1608
1609 ret = nvme_add_ctrl(&anv->ctrl);
1610 if (ret)
1611 goto out_put_ctrl;
1612
1613 anv->ctrl.admin_q = blk_mq_alloc_queue(&anv->admin_tagset, NULL, NULL);
1614 if (IS_ERR(anv->ctrl.admin_q)) {
1615 ret = -ENOMEM;
1616 anv->ctrl.admin_q = NULL;
1617 goto out_uninit_ctrl;
1618 }
1619
1620 nvme_reset_ctrl(&anv->ctrl);
1621 async_schedule(apple_nvme_async_probe, anv);
1622
1623 return 0;
1624
1625 out_uninit_ctrl:
1626 nvme_uninit_ctrl(&anv->ctrl);
1627 out_put_ctrl:
1628 nvme_put_ctrl(&anv->ctrl);
1629 apple_nvme_detach_genpd(anv);
1630 return ret;
1631 }
1632
apple_nvme_remove(struct platform_device * pdev)1633 static void apple_nvme_remove(struct platform_device *pdev)
1634 {
1635 struct apple_nvme *anv = platform_get_drvdata(pdev);
1636
1637 nvme_change_ctrl_state(&anv->ctrl, NVME_CTRL_DELETING);
1638 flush_work(&anv->ctrl.reset_work);
1639 nvme_stop_ctrl(&anv->ctrl);
1640 nvme_remove_namespaces(&anv->ctrl);
1641 apple_nvme_disable(anv, true);
1642 if (anv->ctrl.admin_q && !blk_queue_dying(anv->ctrl.admin_q)) {
1643 /*
1644 * If the controller was reset during removal, it's possible
1645 * user requests may be waiting on a stopped queue. Start the
1646 * queue to flush these to completion.
1647 */
1648 nvme_unquiesce_admin_queue(&anv->ctrl);
1649 blk_mq_destroy_queue(anv->ctrl.admin_q);
1650 }
1651 nvme_uninit_ctrl(&anv->ctrl);
1652
1653 if (apple_rtkit_is_running(anv->rtk)) {
1654 apple_rtkit_shutdown(anv->rtk);
1655
1656 writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL);
1657 }
1658
1659 apple_nvme_detach_genpd(anv);
1660 }
1661
apple_nvme_shutdown(struct platform_device * pdev)1662 static void apple_nvme_shutdown(struct platform_device *pdev)
1663 {
1664 struct apple_nvme *anv = platform_get_drvdata(pdev);
1665
1666 apple_nvme_disable(anv, true);
1667 if (apple_rtkit_is_running(anv->rtk)) {
1668 apple_rtkit_shutdown(anv->rtk);
1669
1670 writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL);
1671 }
1672 }
1673
apple_nvme_resume(struct device * dev)1674 static int apple_nvme_resume(struct device *dev)
1675 {
1676 struct apple_nvme *anv = dev_get_drvdata(dev);
1677
1678 return nvme_reset_ctrl(&anv->ctrl);
1679 }
1680
apple_nvme_suspend(struct device * dev)1681 static int apple_nvme_suspend(struct device *dev)
1682 {
1683 struct apple_nvme *anv = dev_get_drvdata(dev);
1684 int ret = 0;
1685
1686 apple_nvme_disable(anv, true);
1687
1688 if (apple_rtkit_is_running(anv->rtk)) {
1689 ret = apple_rtkit_shutdown(anv->rtk);
1690
1691 writel(0, anv->mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL);
1692 }
1693
1694 return ret;
1695 }
1696
1697 static DEFINE_SIMPLE_DEV_PM_OPS(apple_nvme_pm_ops, apple_nvme_suspend,
1698 apple_nvme_resume);
1699
1700 static const struct apple_nvme_hw apple_nvme_t8015_hw = {
1701 .has_lsq_nvmmu = false,
1702 .max_queue_depth = 16,
1703 };
1704
1705 static const struct apple_nvme_hw apple_nvme_t8103_hw = {
1706 .has_lsq_nvmmu = true,
1707 .max_queue_depth = 64,
1708 };
1709
1710 static const struct of_device_id apple_nvme_of_match[] = {
1711 { .compatible = "apple,t8015-nvme-ans2", .data = &apple_nvme_t8015_hw },
1712 { .compatible = "apple,t8103-nvme-ans2", .data = &apple_nvme_t8103_hw },
1713 { .compatible = "apple,nvme-ans2", .data = &apple_nvme_t8103_hw },
1714 {},
1715 };
1716 MODULE_DEVICE_TABLE(of, apple_nvme_of_match);
1717
1718 static struct platform_driver apple_nvme_driver = {
1719 .driver = {
1720 .name = "nvme-apple",
1721 .of_match_table = apple_nvme_of_match,
1722 .pm = pm_sleep_ptr(&apple_nvme_pm_ops),
1723 },
1724 .probe = apple_nvme_probe,
1725 .remove = apple_nvme_remove,
1726 .shutdown = apple_nvme_shutdown,
1727 };
1728 module_platform_driver(apple_nvme_driver);
1729
1730 MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
1731 MODULE_DESCRIPTION("Apple ANS NVM Express device driver");
1732 MODULE_LICENSE("GPL");
1733