+void setup_qos_device(void)
+{
+ kern_return_t status = kIOReturnError;
+ io_iterator_t iterator = IO_OBJECT_NULL;
+
+ if(io_qos_timeout_ms <= 0)
+ return;
+
+ printf ( "*** setup_qos_device *** \n" );
+
+ status = IOServiceGetMatchingServices ( kIOMasterPortDefault, IOServiceMatching ( kIONVMeANS2ControllerString ), &iterator );
+
+ if ( status != kIOReturnSuccess )
+ return;
+
+ if ( iterator != IO_OBJECT_NULL ) {
+ printf ( "Found NVMe ANS2 Device \n" );
+ qos_device = kNVMeDeviceANS2;
+ return;
+ }
+
+ status = IOServiceGetMatchingServices ( kIOMasterPortDefault, IOServiceMatching ( kIONVMeANS2EmbeddedControllerString ), &iterator );
+
+ if ( status != kIOReturnSuccess )
+ return;
+
+ if ( iterator != IO_OBJECT_NULL ) {
+ printf ( "Found NVMe ANS2 Embedded Device \n" );
+ qos_device = kNVMeDeviceANS2;
+ return;
+ }
+
+ status= IOServiceGetMatchingServices ( kIOMasterPortDefault, IOServiceMatching ( kIONVMeControllerString ), &iterator );
+
+ if ( status != kIOReturnSuccess )
+ return;
+
+ if ( iterator != IO_OBJECT_NULL ) {
+ printf ( "Found NVMe Device \n" );
+ qos_device = kNVMeDevice;
+ return;
+ }
+
+ printf ( "NVMe Device not found, not setting qos timeout\n" );
+ qos_device = kDefaultDevice;
+ return;
+}
+
+void assertASP(CFRunLoopTimerRef timer, void *info )
+{
+ char command [ 1024 ];
+
+ if(qos_device == kDefaultDevice)
+ return;
+
+ printf("assertASP. Timeout of IO exceeds = %d msec\n", io_qos_timeout_ms);
+
+ // kNVMe_ANS2_Force_Assert_offset = 0x13EC, // GP59
+ // kNVMe_Force_Assert_Offset = 0x550,
+
+ if(qos_device == kNVMeDeviceANS2)
+ snprintf ( command, sizeof ( command ), "/usr/local/bin/nvmectl-tool.py -a WriteRegister32 $((0x13EC)) 0xFFFF" );
+ else if(qos_device == kNVMeDevice)
+ snprintf ( command, sizeof ( command ), "/usr/local/bin/nvmectl-tool.py -a WriteRegister32 $((0x550)) 0xFFFF" );
+ else
+ return;
+
+ // Assert ASP
+ printf("Command : %s\n", command);
+ system_cmd(command);
+
+ // Panic the system as well
+ panic("IO time > QoS timeout");
+
+ return;
+}
+
+void start_qos_timer(void)
+{
+ float timeout_sec;
+
+ if(io_qos_timeout_ms <= 0)
+ return;
+
+ timeout_sec = (float)io_qos_timeout_ms/1000;
+
+ // Schedule a "timeout" delayed task that checks IO's which take > timeout sec to complete
+ runLoopTimer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent()+timeout_sec, 0, 0, 0, assertASP, NULL);
+ CFRunLoopAddTimer(CFRunLoopGetMain(), runLoopTimer, kCFRunLoopDefaultMode);
+}
+
+void stop_qos_timer(void)
+{
+ if(runLoopTimer == NULL)
+ return;
+
+ CFRunLoopTimerInvalidate(runLoopTimer);
+ CFRunLoopRemoveTimer(CFRunLoopGetMain(), runLoopTimer, kCFRunLoopDefaultMode);
+ CFRelease(runLoopTimer);
+}