Fix SSD TRIM on UGREEN UASP USB SATA adapter/enclosure
Tags: raspberrypi, linux
Published: 2022-09-22 17:25:58

I've finally figured out how to fix SSD TRIM on the newer UGREEN USB SATA [adapter](https://www.amazon.com.au/UGREEN-Adapter-Cable-Converter-Drives/dp/B07SCY623F/) and [enclosure](https://www.amazon.com.au/gp/product/B06XWRRMYX/), both on my machine and Raspberry Pi-s!

We need to:

1. Set 'unmap' on `/sys/block/sda/device/scsi_disk/0:0:0:0/provisioning_mode` (the standard fix)
2. Set '2147450880' on `/sys/block/sda/queue/discard_max_bytes`

Without the 2nd fix, `fstrim` would only work on small partitions.

## Permanent fix via UDEV Rules

Add these udev rules to `/etc/udev/rules.d/99-ugreen-sata-adapter-trim-enable.rules`:

```text
# UGREEN SATA USB adapter cable - ASMedia Technology Inc. Ugreen Storage Device
ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="225c", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"
ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="225c", SUBSYSTEM=="block", ATTR{queue/discard_max_bytes}="2147450880"

# UGREEN SATA Enclosure - ASMedia Technology Inc. ASM1153 SATA 3Gb/s bridge
ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="1153", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"
ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="1153", SUBSYSTEM=="block", ATTR{queue/discard_max_bytes}="2147450880"
```

💡: You may need to change `idProduct` to your own - check `lsusb`.

e.g. Here're mine:
```bash
# `lsusb` output:
...
Bus 002 Device 034: ID 174c:225c ASMedia Technology Inc. Ugreen Storage Device
...
Bus 002 Device 041: ID 174c:1153 ASMedia Technology Inc. ASM1153 SATA 3Gb/s bridge
```

## Where did 2147450880 come from?

It seems `fstrim` can still fail on large partitions, with the default `discard_max_bytes` value too big (4294966784) - we need to set it to the maximum the adapter can handle.

2147450880 is this maximum in my case, calculated from 2 attributes from the adapter:
1. `Maximum unmap LBA count` (from `sg_vpd`)
2. `Logical block length` (from `sg_readcap`)

Here's a small snippet to show yours:

```bash
sudo su -
apt install sg3-utils

# Device to check
device=sdh
# e.g. lba_count=4194240
lba_count=$(sg_vpd --all /dev/$device|grep 'Maximum unmap LBA count'|grep -Po '(?<=: )(.*)(?=$)')
# e.g. block_length=512
block_length=$(sg_readcap --long /dev/$device|grep 'Logical block length'|grep -Po '(?<=\=)(.*)(?= bytes)')
# e.g. 2147450880
echo $(( $lba_count * $block_length ))
```

## Symptoms addressed by fix

This fix will be relevant to you if you encounter these errors:

```bash
# fstrim -v /
fstrim: /: the discard operation is not supported
fstrim: /: FITRIM ioctl failed: Operation not permitted
```

And in `/var/log/syslog` / `journalctl`:

```text
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#12 FAILED Result: hostbyte=DID_OK driverbyte=DRIVER_OK cmd_age=0s
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#12 Sense Key : Illegal Request [current] 
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#12 Add. Sense: Invalid field in cdb
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#12 CDB: Unmap/Read sub-channel 42 00 00 00 00 00 00 00 18 00
Sep 22 20:16:19 LINUX kernel: print_req_error: 6703 callbacks suppressed
Sep 22 20:16:19 LINUX kernel: blk_update_request: critical target error, dev sdg, sector 39667712 op 0x3:(DISCARD) flags 0x4000 phys_seg 1 prio class 0
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#13 FAILED Result: hostbyte=DID_OK driverbyte=DRIVER_OK cmd_age=0s
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#13 Sense Key : Illegal Request [current] 
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#13 Add. Sense: Invalid field in cdb
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#13 CDB: Unmap/Read sub-channel 42 00 00 00 00 00 00 00 18 00
Sep 22 20:16:19 LINUX kernel: blk_update_request: critical target error, dev sdg, sector 48056312 op 0x3:(DISCARD) flags 0x4000 phys_seg 1 prio class 0
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#14 FAILED Result: hostbyte=DID_OK driverbyte=DRIVER_OK cmd_age=0s
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#14 Sense Key : Illegal Request [current] 
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#14 Add. Sense: Invalid field in cdb
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#14 CDB: Unmap/Read sub-channel 42 00 00 00 00 00 00 00 18 00
Sep 22 20:16:19 LINUX kernel: blk_update_request: critical target error, dev sdg, sector 56444912 op 0x3:(DISCARD) flags 0x4000 phys_seg 1 prio class 0
Sep 22 20:16:19 LINUX kernel: blk_update_request: critical target error, dev sdg, sector 89999312 op 0x3:(DISCARD) flags 0x0 phys_seg 1 prio class 0
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#15 FAILED Result: hostbyte=DID_OK driverbyte=DRIVER_OK cmd_age=0s
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#15 Sense Key : Illegal Request [current] 
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#15 Add. Sense: Invalid field in cdb
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#15 CDB: Unmap/Read sub-channel 42 00 00 00 00 00 00 00 18 00
Sep 22 20:16:19 LINUX kernel: blk_update_request: critical target error, dev sdg, sector 64833512 op 0x3:(DISCARD) flags 0x4000 phys_seg 1 prio class 0
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#16 FAILED Result: hostbyte=DID_OK driverbyte=DRIVER_OK cmd_age=0s
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#16 Sense Key : Illegal Request [current] 
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#16 Add. Sense: Invalid field in cdb
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#16 CDB: Unmap/Read sub-channel 42 00 00 00 00 00 00 00 18 00
Sep 22 20:16:19 LINUX kernel: blk_update_request: critical target error, dev sdg, sector 73222112 op 0x3:(DISCARD) flags 0x4000 phys_seg 1 prio class 0
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#17 FAILED Result: hostbyte=DID_OK driverbyte=DRIVER_OK cmd_age=0s
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#17 Sense Key : Illegal Request [current] 
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#17 Add. Sense: Invalid field in cdb
Sep 22 20:16:19 LINUX kernel: sd 7:0:0:0: [sdg] tag#17 CDB: Unmap/Read sub-channel 42 00 00 00 00 00 00 00 18 00
Sep 22 20:16:19 LINUX kernel: blk_update_request: critical target error, dev sdg, sector 81610712 op 0x3:(DISCARD) flags 0x4000 phys_seg 1 prio class 0
Sep 22 20:16:19 LINUX kernel: BTRFS warning (device sdg3): failed to trim 1 device(s), last error -95
```



## Credit

This is only possible thanks to [this post](https://www.jeffgeerling.com/blog/2020/enabling-trim-on-external-ssd-on-raspberry-pi) from Jeff Geerling (he's awesome 👍)