aboutsummaryrefslogtreecommitdiff
path: root/rust/kernel/pci/id.rs
blob: c09125946d9e154372a53acdefd696cec1f4990e (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// SPDX-License-Identifier: GPL-2.0

//! PCI device identifiers and related types.
//!
//! This module contains PCI class codes, Vendor IDs, and supporting types.

use crate::{
    bindings,
    fmt,
    prelude::*, //
};

/// PCI device class codes.
///
/// Each entry contains the full 24-bit PCI class code (base class in bits
/// 23-16, subclass in bits 15-8, programming interface in bits 7-0).
///
/// # Examples
///
/// ```
/// # use kernel::{device::Core, pci::{self, Class}, prelude::*};
/// fn probe_device(pdev: &pci::Device<Core>) -> Result {
///     let pci_class = pdev.pci_class();
///     dev_info!(
///         pdev.as_ref(),
///         "Detected PCI class: {}\n",
///         pci_class
///     );
///     Ok(())
/// }
/// ```
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct Class(u32);

/// PCI class mask constants for matching [`Class`] codes.
#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClassMask {
    /// Match the full 24-bit class code.
    Full = 0xffffff,
    /// Match the upper 16 bits of the class code (base class and subclass only)
    ClassSubclass = 0xffff00,
}

macro_rules! define_all_pci_classes {
    (
        $($variant:ident = $binding:expr,)+
    ) => {
        impl Class {
            $(
                #[allow(missing_docs)]
                pub const $variant: Self = Self(Self::to_24bit_class($binding));
            )+
        }

        impl fmt::Display for Class {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                match self {
                    $(
                        &Self::$variant => write!(f, stringify!($variant)),
                    )+
                    _ => <Self as fmt::Debug>::fmt(self, f),
                }
            }
        }
    };
}

/// Once constructed, a [`Class`] contains a valid PCI class code.
impl Class {
    /// Create a [`Class`] from a raw 24-bit class code.
    #[inline]
    pub(super) fn from_raw(class_code: u32) -> Self {
        Self(class_code)
    }

    /// Get the raw 24-bit class code value.
    #[inline]
    pub const fn as_raw(self) -> u32 {
        self.0
    }

    // Converts a PCI class constant to 24-bit format.
    //
    // Many device drivers use only the upper 16 bits (base class and subclass),
    // but some use the full 24 bits. In order to support both cases, store the
    // class code as a 24-bit value, where 16-bit values are shifted up 8 bits.
    const fn to_24bit_class(val: u32) -> u32 {
        if val > 0xFFFF {
            val
        } else {
            val << 8
        }
    }
}

impl fmt::Debug for Class {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{:06x}", self.0)
    }
}

impl ClassMask {
    /// Get the raw mask value.
    #[inline]
    pub const fn as_raw(self) -> u32 {
        self as u32
    }
}

impl TryFrom<u32> for ClassMask {
    type Error = Error;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            0xffffff => Ok(ClassMask::Full),
            0xffff00 => Ok(ClassMask::ClassSubclass),
            _ => Err(EINVAL),
        }
    }
}

/// PCI vendor IDs.
///
/// Each entry contains the 16-bit PCI vendor ID as assigned by the PCI SIG.
#[derive(Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct Vendor(u16);

macro_rules! define_all_pci_vendors {
    (
        $($variant:ident = $binding:expr,)+
    ) => {
        impl Vendor {
            $(
                #[allow(missing_docs)]
                pub const $variant: Self = Self($binding as u16);
            )+
        }

        impl fmt::Display for Vendor {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                match self {
                    $(
                        &Self::$variant => write!(f, stringify!($variant)),
                    )+
                    _ => <Self as fmt::Debug>::fmt(self, f),
                }
            }
        }
    };
}

/// Once constructed, a `Vendor` contains a valid PCI Vendor ID.
impl Vendor {
    /// Create a Vendor from a raw 16-bit vendor ID.
    #[inline]
    pub(super) fn from_raw(vendor_id: u16) -> Self {
        Self(vendor_id)
    }

    /// Get the raw 16-bit vendor ID value.
    #[inline]
    pub const fn as_raw(self) -> u16 {
        self.0
    }
}

impl fmt::Debug for Vendor {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{:04x}", self.0)
    }
}

define_all_pci_classes! {
    NOT_DEFINED                = bindings::PCI_CLASS_NOT_DEFINED,                // 0x000000
    NOT_DEFINED_VGA            = bindings::PCI_CLASS_NOT_DEFINED_VGA,            // 0x000100

    STORAGE_SCSI               = bindings::PCI_CLASS_STORAGE_SCSI,               // 0x010000
    STORAGE_IDE                = bindings::PCI_CLASS_STORAGE_IDE,                // 0x010100
    STORAGE_FLOPPY             = bindings::PCI_CLASS_STORAGE_FLOPPY,             // 0x010200
    STORAGE_IPI                = bindings::PCI_CLASS_STORAGE_IPI,                // 0x010300
    STORAGE_RAID               = bindings::PCI_CLASS_STORAGE_RAID,               // 0x010400
    STORAGE_SATA               = bindings::PCI_CLASS_STORAGE_SATA,               // 0x010600
    STORAGE_SATA_AHCI          = bindings::PCI_CLASS_STORAGE_SATA_AHCI,          // 0x010601
    STORAGE_SAS                = bindings::PCI_CLASS_STORAGE_SAS,                // 0x010700
    STORAGE_EXPRESS            = bindings::PCI_CLASS_STORAGE_EXPRESS,            // 0x010802
    STORAGE_OTHER              = bindings::PCI_CLASS_STORAGE_OTHER,              // 0x018000

    NETWORK_ETHERNET           = bindings::PCI_CLASS_NETWORK_ETHERNET,           // 0x020000
    NETWORK_TOKEN_RING         = bindings::PCI_CLASS_NETWORK_TOKEN_RING,         // 0x020100
    NETWORK_FDDI               = bindings::PCI_CLASS_NETWORK_FDDI,               // 0x020200
    NETWORK_ATM                = bindings::PCI_CLASS_NETWORK_ATM,                // 0x020300
    NETWORK_OTHER              = bindings::PCI_CLASS_NETWORK_OTHER,              // 0x028000

    DISPLAY_VGA                = bindings::PCI_CLASS_DISPLAY_VGA,                // 0x030000
    DISPLAY_XGA                = bindings::PCI_CLASS_DISPLAY_XGA,                // 0x030100
    DISPLAY_3D                 = bindings::PCI_CLASS_DISPLAY_3D,                 // 0x030200
    DISPLAY_OTHER              = bindings::PCI_CLASS_DISPLAY_OTHER,              // 0x038000

    MULTIMEDIA_VIDEO           = bindings::PCI_CLASS_MULTIMEDIA_VIDEO,           // 0x040000
    MULTIMEDIA_AUDIO           = bindings::PCI_CLASS_MULTIMEDIA_AUDIO,           // 0x040100
    MULTIMEDIA_PHONE           = bindings::PCI_CLASS_MULTIMEDIA_PHONE,           // 0x040200
    MULTIMEDIA_HD_AUDIO        = bindings::PCI_CLASS_MULTIMEDIA_HD_AUDIO,        // 0x040300
    MULTIMEDIA_OTHER           = bindings::PCI_CLASS_MULTIMEDIA_OTHER,           // 0x048000

    MEMORY_RAM                 = bindings::PCI_CLASS_MEMORY_RAM,                 // 0x050000
    MEMORY_FLASH               = bindings::PCI_CLASS_MEMORY_FLASH,               // 0x050100
    MEMORY_CXL                 = bindings::PCI_CLASS_MEMORY_CXL,                 // 0x050200
    MEMORY_OTHER               = bindings::PCI_CLASS_MEMORY_OTHER,               // 0x058000

    BRIDGE_HOST                = bindings::PCI_CLASS_BRIDGE_HOST,                // 0x060000
    BRIDGE_ISA                 = bindings::PCI_CLASS_BRIDGE_ISA,                 // 0x060100
    BRIDGE_EISA                = bindings::PCI_CLASS_BRIDGE_EISA,                // 0x060200
    BRIDGE_MC                  = bindings::PCI_CLASS_BRIDGE_MC,                  // 0x060300
    BRIDGE_PCI_NORMAL          = bindings::PCI_CLASS_BRIDGE_PCI_NORMAL,          // 0x060400
    BRIDGE_PCI_SUBTRACTIVE     = bindings