apple.c 43 KB

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