apple.c 45 KB

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