File: dp_sata.go

package info (click to toggle)
golang-github-canonical-go-efilib 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,836 kB
  • sloc: makefile: 3
file content (70 lines) | stat: -rw-r--r-- 2,056 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.

package linux

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"

	efi "github.com/canonical/go-efilib"
)

func handleSATADevicePathNode(state *devicePathBuilderState) error {
	if state.SysfsComponentsRemaining() < 6 {
		return errors.New("invalid path: insufficient components")
	}

	state.AdvanceSysfsPath(6)

	params, err := handleATAPath(state.SysfsPath())
	if err != nil {
		return err
	}

	// Each SATA device is represented in the SCSI layer by setting the
	// channel to the port multiplier port number and the LUN as the LUN (see
	// drivers/ata/libata-scsi.c:ata_scsi_scan_host).

	pmp := params.channel
	if pmp > 0x7fff {
		return errors.New("invalid PMP")
	}

	// The target is always zero for SATA devices, as each port only has
	// a single device.
	if params.target != 0 {
		return errors.New("invalid SCSI target")
	}

	// We need to determine if the device is connected via a port
	// multiplier because we have to set the PMP address to 0xffff
	// if it isn't. Unfortunately, it is zero indexed so checking
	// that it is zero isn't sufficient.
	//
	// The kernel will expose a single host link%d device if there
	// is no port multiplier, or one of more PMP link%d.%d devices
	// if there is a port multiplier attached (see
	// drivers/ata/libata-pmp.c:sata_pmp_init_links and
	// drivers/ata/libata-transport.c:ata_tlink_add).
	_, err = os.Stat(filepath.Join(state.SysfsPath(), "../../../../..", fmt.Sprintf("link%d.%d", params.printId, pmp)))
	switch {
	case os.IsNotExist(err):
		// No port multiplier is connected.
		pmp = 0xffff
	case err != nil:
		return err
	default:
		// A port multiplier is connected.
	}

	state.Path = append(state.Path, &efi.SATADevicePathNode{
		// The kernel provides a one-indexed number and the firmware is zero-indexed.
		HBAPortNumber:            uint16(params.port) - 1,
		PortMultiplierPortNumber: uint16(pmp),
		LUN:                      uint16(params.lun)})
	return nil
}