| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Lorenz Brun | fba5da0 | 2022-12-15 11:20:47 +0000 | [diff] [blame] | 4 | package nvme |
| 5 | |
| 6 | // SecureEraseType specifices what type of secure erase should be performed by |
| 7 | // by the controller. The zero value requests no secure erase. |
| 8 | type SecureEraseType uint8 |
| 9 | |
| 10 | const ( |
| 11 | // SecureEraseTypeNone specifies that no secure erase operation is |
| 12 | // requested. |
| 13 | SecureEraseTypeNone SecureEraseType = 0 |
| 14 | // SecureEraseTypeUserData specifies that all user data should be securely |
| 15 | // erased. The controller is allowed to perform a cryptographic erase |
| 16 | // instead. |
| 17 | SecureEraseTypeUserData SecureEraseType = 1 |
| 18 | // SecureEraseTypeCryptographic specifies that the encryption key for user |
| 19 | // data should be erased. This in turn causes all current user data to |
| 20 | // become unreadable. |
| 21 | SecureEraseTypeCryptographic SecureEraseType = 2 |
| 22 | ) |
| 23 | |
| 24 | // ProtectionInformationType selects the type of end-to-end protection tags to |
| 25 | // use. NVMe supports the same types as T10 DIF (SCSI). |
| 26 | type ProtectionInformationType uint8 |
| 27 | |
| 28 | const ( |
| 29 | ProtectionInformationTypeNone ProtectionInformationType = 0 |
| 30 | ProtectionInformationType1 ProtectionInformationType = 1 |
| 31 | ProtectionInformationType2 ProtectionInformationType = 2 |
| 32 | ProtectionInformationType3 ProtectionInformationType = 3 |
| 33 | ) |
| 34 | |
| 35 | type FormatRequest struct { |
| 36 | // NamespaceID contains the ID of the namespace to format. |
| 37 | // NamespaceGlobal formats all namespaces. |
| 38 | NamespaceID uint32 |
| 39 | // SecureEraseSettings specifies the type of secure erase to perform. |
| 40 | SecureEraseSettings SecureEraseType |
| 41 | // ProtectionInformationLocation selects where protection information is |
| 42 | // transmitted. If true, it is transmitted as the first 8 bytes of metadata. |
| 43 | // If false, it is transmitted as the last 8 bytes of metadata. |
| 44 | ProtectionInformationLocation bool |
| 45 | // ProtectionInformation specifies the type of T10 DIF Protection |
| 46 | // Information to use. |
| 47 | ProtectionInformation ProtectionInformationType |
| 48 | // MetadataInline selects whether metadata is transferred as part of an |
| 49 | // extended data LBA. If false, metadata is returned in a separate buffer. |
| 50 | // If true, metadata is appended to the data buffer. |
| 51 | MetadataInline bool |
| 52 | // LBAFormat specifies the LBA format to use. This needs to be selected |
| 53 | // from the list of supported LBA formats in the Identify response. |
| 54 | LBAFormat uint8 |
| 55 | } |
| 56 | |
| 57 | // Format performs a low-level format of the NVM media. This is used for |
| 58 | // changing the block and/or metadata size. This command causes all data |
| 59 | // on the specified namespace to be lost. By setting SecureEraseSettings |
| 60 | // to the appropriate value it can also be used to securely erase data. |
| 61 | // See also the Sanitize command for just wiping the device. |
| 62 | func (d *Device) Format(req *FormatRequest) error { |
| 63 | var cdw10 uint32 |
| 64 | cdw10 |= uint32(req.SecureEraseSettings&0x7) << 9 |
| 65 | cdw10 |= uint32(req.ProtectionInformation&0x7) << 5 |
| 66 | cdw10 |= uint32(req.LBAFormat & 0x7) |
| 67 | if req.ProtectionInformationLocation { |
| 68 | cdw10 |= 1 << 8 |
| 69 | } |
| 70 | if req.MetadataInline { |
| 71 | cdw10 |= 1 << 4 |
| 72 | } |
| 73 | return d.RawCommand(&Command{ |
| 74 | Opcode: 0x80, |
| 75 | NamespaceID: req.NamespaceID, |
| 76 | CDW10: cdw10, |
| 77 | }) |
| 78 | } |