@@ -195,8 +195,20 @@ struct apple_nvme {
195195
196196 int irq ;
197197 spinlock_t lock ;
198+
199+ /*
200+ * Delayed cache flush handling state
201+ */
202+ struct nvme_ns * flush_ns ;
203+ unsigned long flush_interval ;
204+ unsigned long last_flush ;
205+ struct delayed_work flush_dwork ;
198206};
199207
208+ unsigned int flush_interval = 1000 ;
209+ module_param (flush_interval , uint , 0644 );
210+ MODULE_PARM_DESC (flush_interval , "Grace period in msecs between flushes" );
211+
200212static_assert (sizeof (struct nvme_command ) == 64 );
201213static_assert (sizeof (struct apple_nvmmu_tcb ) == 128 );
202214
@@ -729,6 +741,26 @@ static int apple_nvme_remove_sq(struct apple_nvme *anv)
729741 return nvme_submit_sync_cmd (anv -> ctrl .admin_q , & c , NULL , 0 );
730742}
731743
744+ static bool apple_nvme_delayed_flush (struct apple_nvme * anv , struct nvme_ns * ns ,
745+ struct request * req )
746+ {
747+ if (!anv -> flush_interval || req_op (req ) != REQ_OP_FLUSH )
748+ return false;
749+ if (delayed_work_pending (& anv -> flush_dwork ))
750+ return true;
751+ if (time_before (jiffies , anv -> last_flush + anv -> flush_interval )) {
752+ kblockd_mod_delayed_work_on (WORK_CPU_UNBOUND , & anv -> flush_dwork ,
753+ anv -> flush_interval );
754+ if (WARN_ON_ONCE (anv -> flush_ns && anv -> flush_ns != ns ))
755+ goto out ;
756+ anv -> flush_ns = ns ;
757+ return true;
758+ }
759+ out :
760+ anv -> last_flush = jiffies ;
761+ return false;
762+ }
763+
732764static blk_status_t apple_nvme_queue_rq (struct blk_mq_hw_ctx * hctx ,
733765 const struct blk_mq_queue_data * bd )
734766{
@@ -764,6 +796,12 @@ static blk_status_t apple_nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
764796 }
765797
766798 nvme_start_request (req );
799+
800+ if (apple_nvme_delayed_flush (anv , ns , req )) {
801+ blk_mq_complete_request (req );
802+ return BLK_STS_OK ;
803+ }
804+
767805 apple_nvme_submit_cmd (q , cmnd );
768806 return BLK_STS_OK ;
769807
@@ -1010,25 +1048,37 @@ static void apple_nvme_reset_work(struct work_struct *work)
10101048 ret = apple_rtkit_shutdown (anv -> rtk );
10111049 if (ret )
10121050 goto out ;
1051+
1052+ writel (0 , anv -> mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL );
10131053 }
10141054
1015- writel (0 , anv -> mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL );
1055+ /*
1056+ * Only do the soft-reset if the CPU is not running, which means either we
1057+ * or the previous stage shut it down cleanly.
1058+ */
1059+ if (!(readl (anv -> mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL ) &
1060+ APPLE_ANS_COPROC_CPU_CONTROL_RUN )) {
10161061
1017- ret = reset_control_assert (anv -> reset );
1018- if (ret )
1019- goto out ;
1062+ ret = reset_control_assert (anv -> reset );
1063+ if (ret )
1064+ goto out ;
10201065
1021- ret = apple_rtkit_reinit (anv -> rtk );
1022- if (ret )
1023- goto out ;
1066+ ret = apple_rtkit_reinit (anv -> rtk );
1067+ if (ret )
1068+ goto out ;
10241069
1025- ret = reset_control_deassert (anv -> reset );
1026- if (ret )
1027- goto out ;
1070+ ret = reset_control_deassert (anv -> reset );
1071+ if (ret )
1072+ goto out ;
1073+
1074+ writel (APPLE_ANS_COPROC_CPU_CONTROL_RUN ,
1075+ anv -> mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL );
1076+
1077+ ret = apple_rtkit_boot (anv -> rtk );
1078+ } else {
1079+ ret = apple_rtkit_wake (anv -> rtk );
1080+ }
10281081
1029- writel (APPLE_ANS_COPROC_CPU_CONTROL_RUN ,
1030- anv -> mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL );
1031- ret = apple_rtkit_boot (anv -> rtk );
10321082 if (ret ) {
10331083 dev_err (anv -> dev , "ANS did not boot" );
10341084 goto out ;
@@ -1387,6 +1437,28 @@ static void devm_apple_nvme_mempool_destroy(void *data)
13871437 mempool_destroy (data );
13881438}
13891439
1440+ static void apple_nvme_flush_work (struct work_struct * work )
1441+ {
1442+ struct nvme_command c = { };
1443+ struct apple_nvme * anv ;
1444+ struct nvme_ns * ns ;
1445+ int err ;
1446+
1447+ anv = container_of (work , struct apple_nvme , flush_dwork .work );
1448+ ns = anv -> flush_ns ;
1449+ if (WARN_ON_ONCE (!ns ))
1450+ return ;
1451+
1452+ c .common .opcode = nvme_cmd_flush ;
1453+ c .common .nsid = cpu_to_le32 (anv -> flush_ns -> head -> ns_id );
1454+ err = nvme_submit_sync_cmd (ns -> queue , & c , NULL , 0 );
1455+ if (err ) {
1456+ dev_err (anv -> dev , "Deferred flush failed: %d\n" , err );
1457+ } else {
1458+ anv -> last_flush = jiffies ;
1459+ }
1460+ }
1461+
13901462static int apple_nvme_probe (struct platform_device * pdev )
13911463{
13921464 struct device * dev = & pdev -> dev ;
@@ -1521,12 +1593,21 @@ static int apple_nvme_probe(struct platform_device *pdev)
15211593 goto put_dev ;
15221594 }
15231595
1596+ if (flush_interval ) {
1597+ anv -> flush_interval = msecs_to_jiffies (flush_interval );
1598+ anv -> flush_ns = NULL ;
1599+ anv -> last_flush = jiffies - anv -> flush_interval ;
1600+ }
1601+
1602+ INIT_DELAYED_WORK (& anv -> flush_dwork , apple_nvme_flush_work );
1603+
15241604 nvme_reset_ctrl (& anv -> ctrl );
15251605 async_schedule (apple_nvme_async_probe , anv );
15261606
15271607 return 0 ;
15281608
15291609put_dev :
1610+ apple_nvme_detach_genpd (anv );
15301611 put_device (anv -> dev );
15311612 return ret ;
15321613}
@@ -1542,9 +1623,12 @@ static int apple_nvme_remove(struct platform_device *pdev)
15421623 apple_nvme_disable (anv , true);
15431624 nvme_uninit_ctrl (& anv -> ctrl );
15441625
1545- if (apple_rtkit_is_running (anv -> rtk ))
1626+ if (apple_rtkit_is_running (anv -> rtk )) {
15461627 apple_rtkit_shutdown (anv -> rtk );
15471628
1629+ writel (0 , anv -> mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL );
1630+ }
1631+
15481632 apple_nvme_detach_genpd (anv );
15491633
15501634 return 0 ;
@@ -1554,9 +1638,13 @@ static void apple_nvme_shutdown(struct platform_device *pdev)
15541638{
15551639 struct apple_nvme * anv = platform_get_drvdata (pdev );
15561640
1641+ flush_delayed_work (& anv -> flush_dwork );
15571642 apple_nvme_disable (anv , true);
1558- if (apple_rtkit_is_running (anv -> rtk ))
1643+ if (apple_rtkit_is_running (anv -> rtk )) {
15591644 apple_rtkit_shutdown (anv -> rtk );
1645+
1646+ writel (0 , anv -> mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL );
1647+ }
15601648}
15611649
15621650static int apple_nvme_resume (struct device * dev )
@@ -1573,10 +1661,11 @@ static int apple_nvme_suspend(struct device *dev)
15731661
15741662 apple_nvme_disable (anv , true);
15751663
1576- if (apple_rtkit_is_running (anv -> rtk ))
1664+ if (apple_rtkit_is_running (anv -> rtk )) {
15771665 ret = apple_rtkit_shutdown (anv -> rtk );
15781666
1579- writel (0 , anv -> mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL );
1667+ writel (0 , anv -> mmio_coproc + APPLE_ANS_COPROC_CPU_CONTROL );
1668+ }
15801669
15811670 return ret ;
15821671}
0 commit comments