1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2019 Joyent, Inc.
25 * Copyright 2024 Oxide Computer Company
26 */
27
28 /*
29 * PCIe Initialization
30 * -------------------
31 *
32 * The PCIe subsystem is split about and initializes itself in a couple of
33 * different places. This is due to the platform-specific nature of initializing
34 * resources and the nature of the SPARC PROM and how that influenced the
35 * subsystem. Note that traditional PCI (mostly seen these days in Virtual
36 * Machines) follows most of the same basic path outlined here, but skips a
37 * large chunk of PCIe-specific initialization.
38 *
39 * First, there is an initial device discovery phase that is taken care of by
40 * the platform. This is where we discover the set of devices that are present
41 * at system power on. These devices may or may not be hot-pluggable. In
42 * particular, this happens in a platform-specific way right now. In general, we
43 * expect most discovery to be driven by scanning each bus, device, and
44 * function, and seeing what actually exists and responds to configuration space
45 * reads. This is driven via pci_boot.c on x86. This may be seeded by something
46 * like device tree, a PROM, supplemented with ACPI, or by knowledge that the
47 * underlying platform has.
48 *
49 * As a part of this discovery process, the full set of resources that exist in
50 * the system for PCIe are:
51 *
52 * o PCI buses
53 * o Prefetchable Memory
54 * o Non-prefetchable memory
55 * o I/O ports
56 *
57 * This process is driven by a platform's PCI platform Resource Discovery (PRD)
58 * module. The PRD definitions can be found in <sys/plat/pci_prd.h> and are used
59 * to discover these resources, which will be converted into the initial set of
60 * the standard properties in the system: 'regs', 'available', 'ranges', etc.
61 * Currently it is up to platform-specific code (which should ideally be
62 * consolidated at some point) to set up all these properties.
63 *
64 * As a part of the discovery process, the platform code will create a device
65 * node (dev_info_t) for each discovered function and will create a PCIe nexus
66 * for each overall root complex that exists in the system. Most root complexes
67 * will have multiple root ports, each of which is the foundation of an
68 * independent PCIe bus due to the point-to-point nature of PCIe. When a root
69 * complex is found, a nexus driver such as npe (Nexus for PCIe Express) is
70 * attached. In the case of a non-PCIe-capable system this is where the older
71 * pci nexus driver would be used instead.
72 *
73 * To track data about a given device on a bus, a 'pcie_bus_t' structure is
74 * created for and assigned to every PCIe-based dev_info_t. This can be used to
75 * find the root port and get basic information about the device, its faults,
76 * and related information. This contains pointers to the corresponding root
77 * port as well.
78 *
79 * A root complex has its pcie_bus_t initialized as part of the device discovery
80 * process. That is, because we're trying to bootstrap the actual tree and most
81 * platforms don't have a representation for this that's explicitly
82 * discoverable, this is created manually. See callers of pcie_rc_init_bus().
83 *
84 * For other devices, bridges, and switches, the process is split into two.
85 * There is an initial pcie_bus_t that is created which will exist before we go
86 * through the actual driver attachment process. For example, on x86 this is
87 * done as part of the device and function discovery. The second pass of
88 * initialization is done only after the nexus driver actually is attached and
89 * it goes through and finishes processing all of its children.
90 *
91 * Child Initialization
92 * --------------------
93 *
94 * Generally speaking, the platform will first enumerate all PCIe devices that
95 * are in the sytem before it actually creates a device tree. This is part of
96 * the bus/device/function scanning that is performed and from that dev_info_t
97 * nodes are created for each discovered device and are inserted into the
98 * broader device tree. Later in boot, the actual device tree is walked and the
99 * nodes go through the standard dev_info_t initialization process (DS_PROTO,
100 * DS_LINKED, DS_BOUND, etc.).
101 *
102 * PCIe-specific initialization can roughly be broken into the following pieces:
103 *
104 * 1. Platform initial discovery and resource assignment
105 * 2. The pcie_bus_t initialization
106 * 3. Nexus driver child initialization
107 * 4. Fabric initialization
108 * 5. Device driver-specific initialization
109 *
110 * The first part of this (1) and (2) are discussed in the previous section.
111 * Part (1) in particular is a combination of the PRD (platform resource
112 * discovery) and general device initialization. After this, because we have a
113 * device tree, most of the standard nexus initialization happens.
114 *
115 * (5) is somewhat simple, so let's get into it before we discuss (3) and (4).
116 * This is the last thing that is called and that happens after all of the
117 * others are done. This is the logic that occurs in a driver's attach(9E) entry
118 * point. This is always device-specific and generally speaking should not be
119 * manipulating standard PCIe registers directly on their own. For example, the
120 * MSI/MSI-X, AER, Serial Number, etc. capabilities will be automatically dealt
121 * with by the framework in (3) and (4) below. In many cases, particularly
122 * things that are part of (4), adjusting them in the individual driver is not
123 * safe.
124 *
125 * Finally, let's talk about (3) and (4) as these are related. The NDI provides
126 * for a standard hook for a nexus to initialize its children. In our platforms,
127 * there are basically two possible PCIe nexus drivers: there is the generic
128 * pcieb -- PCIe bridge -- driver which is used for standard root ports,
129 * switches, etc. Then there is the platform-specific primary nexus driver,
130 * which is being slowly consolidated into a single one where it makes sense. An
131 * example of this is npe.
132 *
133 * Each of these has a child initialization function which is called from their
134 * DDI_CTLOPS_INITCHILD operation on the bus_ctl function pointer. This goes
135 * through and initializes a large number of different pieces of PCIe-based
136 * settings through the common pcie_initchild() function. This takes care of
137 * things like:
138 *
139 * o Advanced Error Reporting
140 * o Alternative Routing
141 * o Capturing information around link speed, width, serial numbers, etc.
142 * o Setting common properties around aborts
143 *
144 * There are a few caveats with this that need to be kept in mind:
145 *
146 * o A dev_info_t indicates a specific function. This means that a
147 * multi-function device will not all be initialized at the same time and
148 * there is no guarantee that all children will be initialized before one of
149 * them is attached.
150 * o A child is only initialized if we have found a driver that matches an
151 * alias in the dev_info_t's compatible array property. While a lot of
152 * multi-function devices are often multiple instances of the same thing
153 * (e.g. a multi-port NIC with a function / NIC), this is not always the
154 * case and one cannot make any assumptions here.
155 *
156 * This in turn leads to the next form of initialization that takes place in the
157 * case of (4). This is where we take care of things that need to be consistent
158 * across either entire devices or more generally across an entire root port and
159 * all of its children. There are a few different examples of this:
160 *
161 * o Setting the maximum packet size
162 * o Determining the tag width
163 *
164 * Note that features which are only based on function 0, such as ASPM (Active
165 * State Power Management), hardware autonomous width disable, etc. ultimately
166 * do not go through this path today. There are some implications here in that
167 * today several of these things are captured on functions which may not have
168 * any control here. This is an area of needed improvement.
169 *
170 * The settings in (4) are initialized in a common way, via
171 * pcie_fabric_setup(). This is called into from two different parts of
172 * the stack:
173 *
174 * 1. When we attach a root port, which is driven by pcieb.
175 * 2. When we have a hotplug event that adds a device.
176 *
177 * In general here we are going to use the term 'fabric' to refer to everything
178 * that is downstream of a root port. This corresponds to what the PCIe
179 * specification calls a 'hierarchy domain'. Strictly speaking, this is fine
180 * until peer-to-peer requests begin to happen that cause you to need to forward
181 * things across root ports. At that point the scope of the fabric increases and
182 * these settings become more complicated. We currently optimize for the much
183 * more common case, which is that each root port is effectively independent
184 * from a PCIe transaction routing perspective.
185 *
186 * Put differently, we use the term 'fabric' to refer to a set of PCIe devices
187 * that can route transactions to one another, which is generally constrained to
188 * everything under a root port and that root ports are independent. If this
189 * constraint changes, then all one needs to do is replace the discussion of the
190 * root port below with the broader root complex and system.
191 *
192 * A challenge with these settings is that once they're set and devices are
193 * actively making requests, we cannot really change them without resetting the
194 * links and cancelling all outstanding transactions via device resets. Because
195 * this is not something that we want to do, we instead look at how and when we
196 * set this to constrain what's going on.
197 *
198 * Because of this we basically say that if a given fabric has more than one
199 * hot-plug capable device that's encountered, then we have to use safe defaults
200 * (which we can allow an operator to tune eventually via pcieadm). If we have a
201 * mix of non-hotpluggable slots with downstream endpoints present and
202 * hot-pluggable slots, then we're in this case. If we don't have hot-pluggable
203 * slots, then we can have an arbitrarily complex setup. Let's look at a few of
204 * these visually:
205 *
206 * In the following diagrams, RP stands for Root Port, EP stands for Endpoint.
207 * If something is hot-pluggable, then we label it with (HP).
208 *
209 * (1) RP --> EP
210 * (2) RP --> Switch --> EP
211 * +--> EP
212 * +--> EP
213 *
214 * (3) RP --> Switch --> EP
215 * +--> EP
216 * +--> Switch --> EP
217 * +--> EP
218 * +--> EP
219 *
220 *
221 * (4) RP (HP) --> EP
222 * (5) RP (HP) --> Switch --> EP
223 * +--> EP
224 * +--> EP
225 *
226 * (6) RP --> Switch (HP) --> EP
227 * (7) RP (HP) --> Switch (HP) --> EP
228 *
229 * If we look at all of these, these are all cases where it's safe for us to set
230 * things based on all devices. (1), (2), and (3) are straightforward because
231 * they have no hot-pluggable elements. This means that nothing should come/go
232 * on the system and we can set up fabric-wide properties as part of the root
233 * port.
234 *
235 * Case (4) is the most standard one that we encounter for hot-plug. Here you
236 * have a root port directly connected to an endpoint. The most common example
237 * would be an NVMe device plugged into a root port. Case (5) is interesting to
238 * highlight. While there is a switch and multiple endpoints there, they are
239 * showing up as a unit. This ends up being a weirder variant of (4), but it is
240 * safe for us to set advanced properties because we can figure out what the
241 * total set should be.
242 *
243 * Now, the more interesting bits here are (6) and (7). The reason that (6)
244 * works is that ultimately there is only a single down-stream port here that is
245 * hot-pluggable and all non-hotpluggable ports do not have a device present,
246 * which suggests that they will never have a device present. (7) also could be
247 * made to work by making the observation that if there's truly only one
248 * endpoint in a fabric, it doesn't matter how many switches there are that are
249 * hot-pluggable. This would only hold if we can assume for some reason that no
250 * other endpoints could be added.
251 *
252 * In turn, let's look at several cases that we believe aren't safe:
253 *
254 * (8) RP --> Switch --> EP
255 * +--> EP
256 * (HP) +--> EP
257 *
258 * (9) RP --> Switch (HP) +--> EP
259 * (HP) +--> EP
260 *
261 * (10) RP (HP) --> Switch (HP) +--> EP
262 * (HP) +--> EP
263 *
264 * All of these are situations where it's much more explicitly unsafe. Let's
265 * take (8). The problem here is that the devices on the non-hotpluggable
266 * downstream switches are always there and we should assume all device drivers
267 * will be active and performing I/O when the hot-pluggable slot changes. If the
268 * hot-pluggable slot has a lower max payload size, then we're mostly out of
269 * luck. The case of (9) is very similar to (8), just that we have more hot-plug
270 * capable slots.
271 *
272 * Finally (10) is a case of multiple instances of hotplug. (9) and (10) are the
273 * more general case of (6) and (7). While we can try to detect (6) and (7) more
274 * generally or try to make it safe, we're going to start with a simpler form of
275 * detection for this, which roughly follows the following rules:
276 *
277 * o If there are no hot-pluggable slots in an entire fabric, then we can set
278 * all fabric properties based on device capabilities.
279 * o If we encounter a hot-pluggable slot, we can only set fabric properties
280 * based on device capabilities if:
281 *
282 * 1. The hotpluggable slot is a root port.
283 * 2. There are no other hotpluggable devices downstream of it.
284 *
285 * Otherwise, if neither of the above is true, then we must use the basic PCIe
286 * defaults for various fabric-wide properties (discussed below). Even in these
287 * more complicated cases, device-specific properties such as the configuration
288 * of AERs, ASPM, etc. are still handled in the general pcie_init_bus() and
289 * related discussed earlier here.
290 *
291 * Because the only fabrics that we'll change are those that correspond to root
292 * ports, we will only call into the actual fabric feature setup when one of
293 * those changes. This has the side effect of simplifying locking. When we make
294 * changes here we need to be able to hold the entire device tree under the root
295 * port (including the root port and its parent). This is much harder to do
296 * safely when starting in the middle of the tree.
297 *
298 * Handling of Specific Properties
299 * -------------------------------
300 *
301 * This section goes into the rationale behind how we initialize and program
302 * various parts of the PCIe stack.
303 *
304 * 5-, 8-, 10- AND 14-BIT TAGS
305 *
306 * Tags are part of PCIe transactions and when combined with a device identifier
307 * are used to uniquely identify a transaction. In PCIe parlance, a Requester
308 * (someone who initiates a PCIe request) sets a unique tag in the request and
309 * the Completer (someone who processes and responds to a PCIe request) echoes
310 * the tag back. This means that a requester generally is responsible for
311 * ensuring that they don't reuse a tag between transactions.
312 *
313 * Thus the number of tags that a device has relates to the number of
314 * outstanding transactions that it can have, which are usually tied to the
315 * number of outstanding DMA transfers. The size of these transactions is also
316 * then scoped by the handling of the Maximum Packet Payload.
317 *
318 * In PCIe 1.0, devices default to a 5-bit tag. There was also an option to
319 * support an 8-bit tag. The 8-bit extended tag did not distinguish between a
320 * Requester or Completer. There was a bit to indicate device support of 8-bit
321 * tags in the Device Capabilities Register of the PCIe Capability and a
322 * separate bit to enable it in the Device Control Register of the PCIe
323 * Capability.
324 *
325 * In PCIe 4.0, support for a 10-bit tag was added. The specification broke
326 * apart the support bit into multiple pieces. In particular, in the Device
327 * Capabilities 2 register of the PCIe Capability there is a separate bit to
328 * indicate whether the device supports 10-bit completions and 10-bit requests.
329 * All PCIe 4.0 compliant devices are required to support 10-bit tags if they
330 * operate at 16.0 GT/s speed (a PCIe Gen 4 compliant device does not have to
331 * operate at Gen 4 speeds).
332 *
333 * This allows a device to support 10-bit completions but not 10-bit requests.
334 * A device that supports 10-bit requests is required to support 10-bit
335 * completions. There is no ability to enable or disable 10-bit completion
336 * support in the Device Capabilities 2 register. There is only a bit to enable
337 * 10-bit requests. This distinction makes our life easier as this means that as
338 * long as the entire fabric supports 10-bit completions, it doesn't matter if
339 * not all devices support 10-bit requests and we can enable them as required.
340 * More on this in a bit.
341 *
342 * In PCIe 6.0, another set of bits was added for 14-bit tags. These follow the
343 * same pattern as the 10-bit tags. The biggest difference is that the
344 * capabilities and control for these are found in the Device Capabilities 3
345 * and Device Control 3 register of the Device 3 Extended Capability. Similar to
346 * what we see with 10-bit tags, requesters are required to support the
347 * completer capability. The only control bit is for whether or not they enable
348 * a 14-bit requester.
349 *
350 * PCIe switches which sit between root ports and endpoints and show up to
351 * software as a set of bridges. Bridges generally don't have to know about tags
352 * as they are usually neither requesters or completers (unless directly talking
353 * to the bridge instance). That is they are generally required to forward
354 * packets without modifying them. This works until we deal with switch error
355 * handling. At that point, the switch may try to interpret the transaction and
356 * if it doesn't understand the tagging scheme in use, return the transaction to
357 * with the wrong tag and also an incorrectly diagnosed error (usually a
358 * malformed TLP).
359 *
360 * With all this, we construct a somewhat simple policy of how and when we
361 * enable extended tags:
362 *
363 * o If we have a complex hotplug-capable fabric (based on the discussion
364 * earlier in fabric-specific settings), then we cannot enable any of the
365 * 8-bit, 10-bit, and 14-bit tagging features. This is due to the issues
366 * with intermediate PCIe switches and related.
367 *
368 * o If every device supports 8-bit capable tags, then we will go through and
369 * enable those everywhere.
370 *
371 * o If every device supports 10-bit capable completions, then we will enable
372 * 10-bit requester on every device that supports it.
373 *
374 * o If every device supports 14-bit capable completions, then we will enable
375 * 14-bit requesters on every device that supports it.
376 *
377 * This is the simpler end of the policy and one that is relatively easy to
378 * implement. While we could attempt to relax the constraint that every device
379 * in the fabric implement these features by making assumptions about peer-to-
380 * peer requests (that is devices at the same layer in the tree won't talk to
381 * one another), that is a lot of complexity. For now, we leave such an
382 * implementation to those who need it in the future.
383 *
384 * MAX PAYLOAD SIZE
385 *
386 * When performing transactions on the PCIe bus, a given transaction has a
387 * maximum allowed size. This size is called the MPS or 'Maximum Payload Size'.
388 * A given device reports its maximum supported size in the Device Capabilities
389 * register of the PCIe Capability. It is then set in the Device Control
390 * register.
391 *
392 * One of the challenges with this value is that different functions of a device
393 * have independent values, but strictly speaking are required to actually have
394 * the same value programmed in all of them lest device behavior goes awry. When
395 * a device has the ARI (alternative routing ID) capability enabled, then only
396 * function 0 controls the actual payload size.
397 *
398 * The settings for this need to be consistent throughout the fabric. A
399 * Transmitter is not allowed to create a TLP that exceeds its maximum packet
400 * size and a Receiver is not allowed to receive a packet that exceeds its
401 * maximum packet size. In all of these cases, this would result in something
402 * like a malformed TLP error.
403 *
404 * Effectively, this means that everything on a given fabric must have the same
405 * value programmed in its Device Control register for this value. While in the
406 * case of tags, switches generally weren't completers or requesters, here every
407 * device along the path is subject to this. This makes the actual value that we
408 * set throughout the fabric even more important and the constraints of hotplug
409 * even worse to deal with.
410 *
411 * Because a hotplug device can be inserted with any packet size, if we hit
412 * anything other than the simple hotplug cases discussed in the fabric-specific
413 * settings section, then we must use the smallest size of 128 byte payloads.
414 * This is because a device could be plugged in that supports something smaller
415 * than we had otherwise set. If there are other active devices, those could not
416 * be changed without quiescing the entire fabric. As such our algorithm is as
417 * follows:
418 *
419 * 1. Scan the entire fabric, keeping track of the smallest seen MPS in the
420 * Device Capabilities Register.
421 * 2. If we have a complex fabric, program each Device Control register with
422 * a 128 byte maximum payload size, otherwise, program it with the
423 * discovered value.
424 *
425 *
426 * MAX READ REQUEST SIZE
427 *
428 * The maximum read request size (mrrs) is a much more confusing thing when
429 * compared to the maximum payload size counterpart. The maximum payload size
430 * (MPS) above is what restricts the actual size of a TLP. The mrrs value
431 * is used to control part of the behavior of Memory Read Request, which is not
432 * strictly speaking subject to the MPS. A PCIe device is allowed to respond to
433 * a Memory Read Request with less bytes than were actually requested in a
434 * single completion. In general, the default size that a root complex and its
435 * root port will reply to are based around the length of a cache line.
436 *
437 * What this ultimately controls is the number of requests that the Requester
438 * has to make and trades off bandwidth, bus sharing, and related here. For
439 * example, if the maximum read request size is 4 KiB, then the requester would
440 * only issue a single read request asking for 4 KiB. It would still receive
441 * these as multiple packets in units of the MPS. If however, the maximum read
442 * request was only say 512 B, then it would need to make 8 separate requests,
443 * potentially increasing latency. On the other hand, if systems are relying on
444 * total requests for QoS, then it's important to set it to something that's
445 * closer to the actual MPS.
446 *
447 * Traditionally, the OS has not been the most straightforward about this. It's
448 * important to remember that setting this up is also somewhat in the realm of
449 * system firmware. Due to the PCI Firmware specification, the firmware may have
450 * set up a value for not just the MRRS but also the MPS. As such, our logic
451 * basically left the MRRS alone and used whatever the device had there as long
452 * as we weren't shrinking the device's MPS. If we were, then we'd set it to the
453 * MPS. If the device was a root port, then it was just left at a system wide
454 * and PCIe default of 512 bytes.
455 *
456 * If we survey firmware (which isn't easy due to its nature), we have seen most
457 * cases where the firmware just doesn't do anything and leaves it to the
458 * device's default, which is basically just the PCIe default, unless it has a
459 * specific knowledge of something like say wanting to do something for an NVMe
460 * device. The same is generally true of other systems, leaving it at its
461 * default unless otherwise set by a device driver.
462 *
463 * Because this value doesn't really have the same constraints as other fabric
464 * properties, this becomes much simpler and we instead opt to set it as part of
465 * the device node initialization. In addition, there are no real rules about
466 * different functions having different values here as it doesn't really impact
467 * the TLP processing the same way that the MPS does.
468 *
469 * While we should add a fuller way of setting this and allowing operator
470 * override of the MRRS based on things like device class, etc. that is driven
471 * by pcieadm, that is left to the future. For now we opt to that all devices
472 * are kept at their default (512 bytes or whatever firmware left behind) and we
473 * ensure that root ports always have the mrrs set to 512.
474 */
475
476 #include <sys/sysmacros.h>
477 #include <sys/types.h>
478 #include <sys/kmem.h>
479 #include <sys/modctl.h>
480 #include <sys/ddi.h>
481 #include <sys/sunddi.h>
482 #include <sys/sunndi.h>
483 #include <sys/fm/protocol.h>
484 #include <sys/fm/util.h>
485 #include <sys/promif.h>
486 #include <sys/disp.h>
487 #include <sys/stat.h>
488 #include <sys/file.h>
489 #include <sys/pci_cap.h>
490 #include <sys/pci_impl.h>
491 #include <sys/pcie_impl.h>
492 #include <sys/hotplug/pci/pcie_hp.h>
493 #include <sys/hotplug/pci/pciehpc.h>
494 #include <sys/hotplug/pci/pcishpc.h>
495 #include <sys/hotplug/pci/pcicfg.h>
496 #include <sys/pci_cfgacc.h>
497 #include <sys/sysevent.h>
498 #include <sys/sysevent/eventdefs.h>
499 #include <sys/sysevent/pcie.h>
500
501 /* Local functions prototypes */
502 static void pcie_init_pfd(dev_info_t *);
503 static void pcie_fini_pfd(dev_info_t *);
504
505 #ifdef DEBUG
506 uint_t pcie_debug_flags = 0;
507 static void pcie_print_bus(pcie_bus_t *bus_p);
508 void pcie_dbg(char *fmt, ...);
509 #endif /* DEBUG */
510
511 /* Variable to control default PCI-Express config settings */
512 ushort_t pcie_command_default =
513 PCI_COMM_SERR_ENABLE |
514 PCI_COMM_WAIT_CYC_ENAB |
515 PCI_COMM_PARITY_DETECT |
516 PCI_COMM_ME |
517 PCI_COMM_MAE |
518 PCI_COMM_IO;
519
520 /* xxx_fw are bits that are controlled by FW and should not be modified */
521 ushort_t pcie_command_default_fw =
522 PCI_COMM_SPEC_CYC |
523 PCI_COMM_MEMWR_INVAL |
524 PCI_COMM_PALETTE_SNOOP |
525 PCI_COMM_WAIT_CYC_ENAB |
526 0xF800; /* Reserved Bits */
527
528 ushort_t pcie_bdg_command_default_fw =
529 PCI_BCNF_BCNTRL_ISA_ENABLE |
530 PCI_BCNF_BCNTRL_VGA_ENABLE |
531 0xF000; /* Reserved Bits */
532
533 /* PCI-Express Base error defaults */
534 ushort_t pcie_base_err_default =
535 PCIE_DEVCTL_CE_REPORTING_EN |
536 PCIE_DEVCTL_NFE_REPORTING_EN |
537 PCIE_DEVCTL_FE_REPORTING_EN |
538 PCIE_DEVCTL_UR_REPORTING_EN;
539
540 /*
541 * This contains default values and masks that are used to manipulate the device
542 * control register and ensure that it is in a normal state. The mask controls
543 * things that are managed by pcie_fabric_setup(), firmware, or other sources
544 * and therefore should be preserved unless we're explicitly trying to change
545 * it.
546 */
547 uint16_t pcie_devctl_default = PCIE_DEVCTL_RO_EN | PCIE_DEVCTL_MAX_READ_REQ_512;
548 uint16_t pcie_devctl_default_mask = PCIE_DEVCTL_MAX_READ_REQ_MASK |
549 PCIE_DEVCTL_MAX_PAYLOAD_MASK | PCIE_DEVCTL_EXT_TAG_FIELD_EN;
550
551 /* PCI-Express AER Root Control Register */
552 #define PCIE_ROOT_SYS_ERR (PCIE_ROOTCTL_SYS_ERR_ON_CE_EN | \
553 PCIE_ROOTCTL_SYS_ERR_ON_NFE_EN | \
554 PCIE_ROOTCTL_SYS_ERR_ON_FE_EN)
555
556 ushort_t pcie_root_ctrl_default =
557 PCIE_ROOTCTL_SYS_ERR_ON_CE_EN |
558 PCIE_ROOTCTL_SYS_ERR_ON_NFE_EN |
559 PCIE_ROOTCTL_SYS_ERR_ON_FE_EN;
560
561 /* PCI-Express Root Error Command Register */
562 ushort_t pcie_root_error_cmd_default =
563 PCIE_AER_RE_CMD_CE_REP_EN |
564 PCIE_AER_RE_CMD_NFE_REP_EN |
565 PCIE_AER_RE_CMD_FE_REP_EN;
566
567 /* ECRC settings in the PCIe AER Control Register */
568 uint32_t pcie_ecrc_value =
569 PCIE_AER_CTL_ECRC_GEN_ENA |
570 PCIE_AER_CTL_ECRC_CHECK_ENA;
571
572 /*
573 * If a particular platform wants to disable certain errors such as UR/MA,
574 * instead of using #defines have the platform's PCIe Root Complex driver set
575 * these masks using the pcie_get_XXX_mask and pcie_set_XXX_mask functions. For
576 * x86 the closest thing to a PCIe root complex driver is NPE. For SPARC the
577 * closest PCIe root complex driver is PX.
578 *
579 * pcie_serr_disable_flag : disable SERR only (in RCR and command reg) x86
580 * systems may want to disable SERR in general. For root ports, enabling SERR
581 * causes NMIs which are not handled and results in a watchdog timeout error.
582 */
583 uint32_t pcie_aer_uce_mask = 0; /* AER UE Mask */
584 uint32_t pcie_aer_ce_mask = 0; /* AER CE Mask */
585 uint32_t pcie_aer_suce_mask = 0; /* AER Secondary UE Mask */
586 uint32_t pcie_serr_disable_flag = 0; /* Disable SERR */
587
588 /* Default severities needed for eversholt. Error handling doesn't care */
589 uint32_t pcie_aer_uce_severity = PCIE_AER_UCE_MTLP | PCIE_AER_UCE_RO | \
590 PCIE_AER_UCE_FCP | PCIE_AER_UCE_SD | PCIE_AER_UCE_DLP | \
591 PCIE_AER_UCE_TRAINING;
592 uint32_t pcie_aer_suce_severity = PCIE_AER_SUCE_SERR_ASSERT | \
593 PCIE_AER_SUCE_UC_ADDR_ERR | PCIE_AER_SUCE_UC_ATTR_ERR | \
594 PCIE_AER_SUCE_USC_MSG_DATA_ERR;
595
596 int pcie_disable_ari = 0;
597
598 /*
599 * On some platforms, such as the AMD B450 chipset, we've seen an odd
600 * relationship between enabling link bandwidth notifications and AERs about
601 * ECRC errors. This provides a mechanism to disable it.
602 */
603 int pcie_disable_lbw = 0;
604
605 /*
606 * Amount of time to wait for an in-progress retraining. The default is to try
607 * 500 times in 10ms chunks, thus a total of 5s.
608 */
609 uint32_t pcie_link_retrain_count = 500;
610 uint32_t pcie_link_retrain_delay_ms = 10;
611
612 taskq_t *pcie_link_tq;
613 kmutex_t pcie_link_tq_mutex;
614
615 static int pcie_link_bw_intr(dev_info_t *);
616 static void pcie_capture_speeds(dev_info_t *);
617
618 dev_info_t *pcie_get_rc_dip(dev_info_t *dip);
619
620 /*
621 * modload support
622 */
623
624 static struct modlmisc modlmisc = {
625 &mod_miscops, /* Type of module */
626 "PCI Express Framework Module"
627 };
628
629 static struct modlinkage modlinkage = {
630 MODREV_1,
631 (void *)&modlmisc,
632 NULL
633 };
634
635 /*
636 * Global Variables needed for a non-atomic version of ddi_fm_ereport_post.
637 * Currently used to send the pci.fabric ereports whose payload depends on the
638 * type of PCI device it is being sent for.
639 */
640 char *pcie_nv_buf;
641 nv_alloc_t *pcie_nvap;
642 nvlist_t *pcie_nvl;
643
644 int
_init(void)645 _init(void)
646 {
647 int rval;
648
649 pcie_nv_buf = kmem_alloc(ERPT_DATA_SZ, KM_SLEEP);
650 pcie_nvap = fm_nva_xcreate(pcie_nv_buf, ERPT_DATA_SZ);
651 pcie_nvl = fm_nvlist_create(pcie_nvap);
652 mutex_init(&pcie_link_tq_mutex, NULL, MUTEX_DRIVER, NULL);
653
654 if ((rval = mod_install(&modlinkage)) != 0) {
655 mutex_destroy(&pcie_link_tq_mutex);
656 fm_nvlist_destroy(pcie_nvl, FM_NVA_RETAIN);
657 fm_nva_xdestroy(pcie_nvap);
658 kmem_free(pcie_nv_buf, ERPT_DATA_SZ);
659 }
660 return (rval);
661 }
662
663 int
_fini()664 _fini()
665 {
666 int rval;
667
668 if ((rval = mod_remove(&modlinkage)) == 0) {
669 if (pcie_link_tq != NULL) {
670 taskq_destroy(pcie_link_tq);
671 }
672 mutex_destroy(&pcie_link_tq_mutex);
673 fm_nvlist_destroy(pcie_nvl, FM_NVA_RETAIN);
674 fm_nva_xdestroy(pcie_nvap);
675 kmem_free(pcie_nv_buf, ERPT_DATA_SZ);
676 }
677 return (rval);
678 }
679
680 int
_info(struct modinfo * modinfop)681 _info(struct modinfo *modinfop)
682 {
683 return (mod_info(&modlinkage, modinfop));
684 }
685
686 /* ARGSUSED */
687 int
pcie_init(dev_info_t * dip,caddr_t arg)688 pcie_init(dev_info_t *dip, caddr_t arg)
689 {
690 int ret = DDI_SUCCESS;
691
692 /*
693 * Our _init function is too early to create a taskq. Create the pcie
694 * link management taskq here now instead.
695 */
696 mutex_enter(&pcie_link_tq_mutex);
697 if (pcie_link_tq == NULL) {
698 pcie_link_tq = taskq_create("pcie_link", 1, minclsyspri, 0, 0,
699 0);
700 }
701 mutex_exit(&pcie_link_tq_mutex);
702
703
704 /*
705 * Create a "devctl" minor node to support DEVCTL_DEVICE_*
706 * and DEVCTL_BUS_* ioctls to this bus.
707 */
708 if ((ret = ddi_create_minor_node(dip, "devctl", S_IFCHR,
709 PCI_MINOR_NUM(ddi_get_instance(dip), PCI_DEVCTL_MINOR),
710 DDI_NT_NEXUS, 0)) != DDI_SUCCESS) {
711 PCIE_DBG("Failed to create devctl minor node for %s%d\n",
712 ddi_driver_name(dip), ddi_get_instance(dip));
713
714 return (ret);
715 }
716
717 if ((ret = pcie_hp_init(dip, arg)) != DDI_SUCCESS) {
718 /*
719 * On some x86 platforms, we observed unexpected hotplug
720 * initialization failures in recent years. The known cause
721 * is a hardware issue: while the problem PCI bridges have
722 * the Hotplug Capable registers set, the machine actually
723 * does not implement the expected ACPI object.
724 *
725 * We don't want to stop PCI driver attach and system boot
726 * just because of this hotplug initialization failure.
727 * Continue with a debug message printed.
728 */
729 PCIE_DBG("%s%d: Failed setting hotplug framework\n",
730 ddi_driver_name(dip), ddi_get_instance(dip));
731
732 #if defined(__sparc)
733 ddi_remove_minor_node(dip, "devctl");
734
735 return (ret);
736 #endif /* defined(__sparc) */
737 }
738
739 return (DDI_SUCCESS);
740 }
741
742 /* ARGSUSED */
743 int
pcie_uninit(dev_info_t * dip)744 pcie_uninit(dev_info_t *dip)
745 {
746 int ret = DDI_SUCCESS;
747
748 if (pcie_ari_is_enabled(dip) == PCIE_ARI_FORW_ENABLED)
749 (void) pcie_ari_disable(dip);
750
751 if ((ret = pcie_hp_uninit(dip)) != DDI_SUCCESS) {
752 PCIE_DBG("Failed to uninitialize hotplug for %s%d\n",
753 ddi_driver_name(dip), ddi_get_instance(dip));
754
755 return (ret);
756 }
757
758 if (pcie_link_bw_supported(dip)) {
759 (void) pcie_link_bw_disable(dip);
760 }
761
762 ddi_remove_minor_node(dip, "devctl");
763
764 return (ret);
765 }
766
767 /*
768 * PCIe module interface for enabling hotplug interrupt.
769 *
770 * It should be called after pcie_init() is done and bus driver's
771 * interrupt handlers have being attached.
772 */
773 int
pcie_hpintr_enable(dev_info_t * dip)774 pcie_hpintr_enable(dev_info_t *dip)
775 {
776 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
777 pcie_hp_ctrl_t *ctrl_p = PCIE_GET_HP_CTRL(dip);
778
779 if (PCIE_IS_PCIE_HOTPLUG_ENABLED(bus_p)) {
780 (void) (ctrl_p->hc_ops.enable_hpc_intr)(ctrl_p);
781 } else if (PCIE_IS_PCI_HOTPLUG_ENABLED(bus_p)) {
782 (void) pcishpc_enable_irqs(ctrl_p);
783 }
784 return (DDI_SUCCESS);
785 }
786
787 /*
788 * PCIe module interface for disabling hotplug interrupt.
789 *
790 * It should be called before pcie_uninit() is called and bus driver's
791 * interrupt handlers is dettached.
792 */
793 int
pcie_hpintr_disable(dev_info_t * dip)794 pcie_hpintr_disable(dev_info_t *dip)
795 {
796 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
797 pcie_hp_ctrl_t *ctrl_p = PCIE_GET_HP_CTRL(dip);
798
799 if (PCIE_IS_PCIE_HOTPLUG_ENABLED(bus_p)) {
800 (void) (ctrl_p->hc_ops.disable_hpc_intr)(ctrl_p);
801 } else if (PCIE_IS_PCI_HOTPLUG_ENABLED(bus_p)) {
802 (void) pcishpc_disable_irqs(ctrl_p);
803 }
804 return (DDI_SUCCESS);
805 }
806
807 /* ARGSUSED */
808 int
pcie_intr(dev_info_t * dip)809 pcie_intr(dev_info_t *dip)
810 {
811 int hp, lbw;
812
813 hp = pcie_hp_intr(dip);
814 lbw = pcie_link_bw_intr(dip);
815
816 if (hp == DDI_INTR_CLAIMED || lbw == DDI_INTR_CLAIMED) {
817 return (DDI_INTR_CLAIMED);
818 }
819
820 return (DDI_INTR_UNCLAIMED);
821 }
822
823 /* ARGSUSED */
824 int
pcie_open(dev_info_t * dip,dev_t * devp,int flags,int otyp,cred_t * credp)825 pcie_open(dev_info_t *dip, dev_t *devp, int flags, int otyp, cred_t *credp)
826 {
827 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
828
829 /*
830 * Make sure the open is for the right file type.
831 */
832 if (otyp != OTYP_CHR)
833 return (EINVAL);
834
835 /*
836 * Handle the open by tracking the device state.
837 */
838 if ((bus_p->bus_soft_state == PCI_SOFT_STATE_OPEN_EXCL) ||
839 ((flags & FEXCL) &&
840 (bus_p->bus_soft_state != PCI_SOFT_STATE_CLOSED))) {
841 return (EBUSY);
842 }
843
844 if (flags & FEXCL)
845 bus_p->bus_soft_state = PCI_SOFT_STATE_OPEN_EXCL;
846 else
847 bus_p->bus_soft_state = PCI_SOFT_STATE_OPEN;
848
849 return (0);
850 }
851
852 /* ARGSUSED */
853 int
pcie_close(dev_info_t * dip,dev_t dev,int flags,int otyp,cred_t * credp)854 pcie_close(dev_info_t *dip, dev_t dev, int flags, int otyp, cred_t *credp)
855 {
856 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
857
858 if (otyp != OTYP_CHR)
859 return (EINVAL);
860
861 bus_p->bus_soft_state = PCI_SOFT_STATE_CLOSED;
862
863 return (0);
864 }
865
866 /* ARGSUSED */
867 int
pcie_ioctl(dev_info_t * dip,dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)868 pcie_ioctl(dev_info_t *dip, dev_t dev, int cmd, intptr_t arg, int mode,
869 cred_t *credp, int *rvalp)
870 {
871 struct devctl_iocdata *dcp;
872 uint_t bus_state;
873 int rv = DDI_SUCCESS;
874
875 /*
876 * We can use the generic implementation for devctl ioctl
877 */
878 switch (cmd) {
879 case DEVCTL_DEVICE_GETSTATE:
880 case DEVCTL_DEVICE_ONLINE:
881 case DEVCTL_DEVICE_OFFLINE:
882 case DEVCTL_BUS_GETSTATE:
883 return (ndi_devctl_ioctl(dip, cmd, arg, mode, 0));
884 default:
885 break;
886 }
887
888 /*
889 * read devctl ioctl data
890 */
891 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
892 return (EFAULT);
893
894 switch (cmd) {
895 case DEVCTL_BUS_QUIESCE:
896 if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
897 if (bus_state == BUS_QUIESCED)
898 break;
899 (void) ndi_set_bus_state(dip, BUS_QUIESCED);
900 break;
901 case DEVCTL_BUS_UNQUIESCE:
902 if (ndi_get_bus_state(dip, &bus_state) == NDI_SUCCESS)
903 if (bus_state == BUS_ACTIVE)
904 break;
905 (void) ndi_set_bus_state(dip, BUS_ACTIVE);
906 break;
907 case DEVCTL_BUS_RESET:
908 case DEVCTL_BUS_RESETALL:
909 case DEVCTL_DEVICE_RESET:
910 rv = ENOTSUP;
911 break;
912 default:
913 rv = ENOTTY;
914 }
915
916 ndi_dc_freehdl(dcp);
917 return (rv);
918 }
919
920 /* ARGSUSED */
921 int
pcie_prop_op(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int flags,char * name,caddr_t valuep,int * lengthp)922 pcie_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
923 int flags, char *name, caddr_t valuep, int *lengthp)
924 {
925 if (dev == DDI_DEV_T_ANY)
926 goto skip;
927
928 if (PCIE_IS_HOTPLUG_CAPABLE(dip) &&
929 strcmp(name, "pci-occupant") == 0) {
930 int pci_dev = PCI_MINOR_NUM_TO_PCI_DEVNUM(getminor(dev));
931
932 pcie_hp_create_occupant_props(dip, dev, pci_dev);
933 }
934
935 skip:
936 return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
937 }
938
939 int
pcie_init_cfghdl(dev_info_t * cdip)940 pcie_init_cfghdl(dev_info_t *cdip)
941 {
942 pcie_bus_t *bus_p;
943 ddi_acc_handle_t eh = NULL;
944
945 bus_p = PCIE_DIP2BUS(cdip);
946 if (bus_p == NULL)
947 return (DDI_FAILURE);
948
949 /* Create an config access special to error handling */
950 if (pci_config_setup(cdip, &eh) != DDI_SUCCESS) {
951 cmn_err(CE_WARN, "Cannot setup config access"
952 " for BDF 0x%x\n", bus_p->bus_bdf);
953 return (DDI_FAILURE);
954 }
955
956 bus_p->bus_cfg_hdl = eh;
957 return (DDI_SUCCESS);
958 }
959
960 void
pcie_fini_cfghdl(dev_info_t * cdip)961 pcie_fini_cfghdl(dev_info_t *cdip)
962 {
963 pcie_bus_t *bus_p = PCIE_DIP2BUS(cdip);
964
965 pci_config_teardown(&bus_p->bus_cfg_hdl);
966 }
967
968 void
pcie_determine_serial(dev_info_t * dip)969 pcie_determine_serial(dev_info_t *dip)
970 {
971 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
972 ddi_acc_handle_t h;
973 uint16_t cap;
974 uchar_t serial[8];
975 uint32_t low, high;
976
977 if (!PCIE_IS_PCIE(bus_p))
978 return;
979
980 h = bus_p->bus_cfg_hdl;
981
982 if ((PCI_CAP_LOCATE(h, PCI_CAP_XCFG_SPC(PCIE_EXT_CAP_ID_SER), &cap)) ==
983 DDI_FAILURE)
984 return;
985
986 high = PCI_XCAP_GET32(h, 0, cap, PCIE_SER_SID_UPPER_DW);
987 low = PCI_XCAP_GET32(h, 0, cap, PCIE_SER_SID_LOWER_DW);
988
989 /*
990 * Here, we're trying to figure out if we had an invalid PCIe read. From
991 * looking at the contents of the value, it can be hard to tell the
992 * difference between a value that has all 1s correctly versus if we had
993 * an error. In this case, we only assume it's invalid if both register
994 * reads are invalid. We also only use 32-bit reads as we're not sure if
995 * all devices will support these as 64-bit reads, while we know that
996 * they'll support these as 32-bit reads.
997 */
998 if (high == PCI_EINVAL32 && low == PCI_EINVAL32)
999 return;
1000
1001 serial[0] = low & 0xff;
1002 serial[1] = (low >> 8) & 0xff;
1003 serial[2] = (low >> 16) & 0xff;
1004 serial[3] = (low >> 24) & 0xff;
1005 serial[4] = high & 0xff;
1006 serial[5] = (high >> 8) & 0xff;
1007 serial[6] = (high >> 16) & 0xff;
1008 serial[7] = (high >> 24) & 0xff;
1009
1010 (void) ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, "pcie-serial",
1011 serial, sizeof (serial));
1012 }
1013
1014 static void
pcie_determine_aspm(dev_info_t * dip)1015 pcie_determine_aspm(dev_info_t *dip)
1016 {
1017 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
1018 uint32_t linkcap;
1019 uint16_t linkctl;
1020
1021 if (!PCIE_IS_PCIE(bus_p))
1022 return;
1023
1024 linkcap = PCIE_CAP_GET(32, bus_p, PCIE_LINKCAP);
1025 linkctl = PCIE_CAP_GET(16, bus_p, PCIE_LINKCTL);
1026
1027 switch (linkcap & PCIE_LINKCAP_ASPM_SUP_MASK) {
1028 case PCIE_LINKCAP_ASPM_SUP_L0S:
1029 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1030 "pcie-aspm-support", "l0s");
1031 break;
1032 case PCIE_LINKCAP_ASPM_SUP_L1:
1033 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1034 "pcie-aspm-support", "l1");
1035 break;
1036 case PCIE_LINKCAP_ASPM_SUP_L0S_L1:
1037 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1038 "pcie-aspm-support", "l0s,l1");
1039 break;
1040 default:
1041 return;
1042 }
1043
1044 switch (linkctl & PCIE_LINKCTL_ASPM_CTL_MASK) {
1045 case PCIE_LINKCTL_ASPM_CTL_DIS:
1046 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1047 "pcie-aspm-state", "disabled");
1048 break;
1049 case PCIE_LINKCTL_ASPM_CTL_L0S:
1050 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1051 "pcie-aspm-state", "l0s");
1052 break;
1053 case PCIE_LINKCTL_ASPM_CTL_L1:
1054 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1055 "pcie-aspm-state", "l1");
1056 break;
1057 case PCIE_LINKCTL_ASPM_CTL_L0S_L1:
1058 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1059 "pcie-aspm-state", "l0s,l1");
1060 break;
1061 }
1062 }
1063
1064 /*
1065 * PCI-Express child device initialization. Note, this only will be called on a
1066 * device or function if we actually attach a device driver to it.
1067 *
1068 * This function enables generic pci-express interrupts and error handling.
1069 * Note, tagging, the max packet size, and related are all set up before this
1070 * point and is performed in pcie_fabric_setup().
1071 *
1072 * @param pdip root dip (root nexus's dip)
1073 * @param cdip child's dip (device's dip)
1074 * @return DDI_SUCCESS or DDI_FAILURE
1075 */
1076 /* ARGSUSED */
1077 int
pcie_initchild(dev_info_t * cdip)1078 pcie_initchild(dev_info_t *cdip)
1079 {
1080 uint16_t tmp16, reg16;
1081 pcie_bus_t *bus_p;
1082
1083 bus_p = PCIE_DIP2BUS(cdip);
1084 if (bus_p == NULL) {
1085 PCIE_DBG("%s: BUS not found.\n",
1086 ddi_driver_name(cdip));
1087
1088 return (DDI_FAILURE);
1089 }
1090
1091 if (pcie_init_cfghdl(cdip) != DDI_SUCCESS)
1092 return (DDI_FAILURE);
1093
1094 /* Clear the device's status register */
1095 reg16 = PCIE_GET(16, bus_p, PCI_CONF_STAT);
1096 PCIE_PUT(16, bus_p, PCI_CONF_STAT, reg16);
1097
1098 /* Setup the device's command register */
1099 reg16 = PCIE_GET(16, bus_p, PCI_CONF_COMM);
1100 tmp16 = (reg16 & pcie_command_default_fw) | pcie_command_default;
1101
1102 if (pcie_serr_disable_flag && PCIE_IS_PCIE(bus_p))
1103 tmp16 &= ~PCI_COMM_SERR_ENABLE;
1104
1105 PCIE_PUT(16, bus_p, PCI_CONF_COMM, tmp16);
1106 PCIE_DBG_CFG(cdip, bus_p, "COMMAND", 16, PCI_CONF_COMM, reg16);
1107
1108 /*
1109 * If the device has a bus control register then program it
1110 * based on the settings in the command register.
1111 */
1112 if (PCIE_IS_BDG(bus_p)) {
1113 /* Clear the device's secondary status register */
1114 reg16 = PCIE_GET(16, bus_p, PCI_BCNF_SEC_STATUS);
1115 PCIE_PUT(16, bus_p, PCI_BCNF_SEC_STATUS, reg16);
1116
1117 /* Setup the device's secondary command register */
1118 reg16 = PCIE_GET(16, bus_p, PCI_BCNF_BCNTRL);
1119 tmp16 = (reg16 & pcie_bdg_command_default_fw);
1120
1121 tmp16 |= PCI_BCNF_BCNTRL_SERR_ENABLE;
1122 /*
1123 * Workaround for this Nvidia bridge. Don't enable the SERR
1124 * enable bit in the bridge control register as it could lead to
1125 * bogus NMIs.
1126 */
1127 if (bus_p->bus_dev_ven_id == 0x037010DE)
1128 tmp16 &= ~PCI_BCNF_BCNTRL_SERR_ENABLE;
1129
1130 if (pcie_command_default & PCI_COMM_PARITY_DETECT)
1131 tmp16 |= PCI_BCNF_BCNTRL_PARITY_ENABLE;
1132
1133 /*
1134 * Enable Master Abort Mode only if URs have not been masked.
1135 * For PCI and PCIe-PCI bridges, enabling this bit causes a
1136 * Master Aborts/UR to be forwarded as a UR/TA or SERR. If this
1137 * bit is masked, posted requests are dropped and non-posted
1138 * requests are returned with -1.
1139 */
1140 if (pcie_aer_uce_mask & PCIE_AER_UCE_UR)
1141 tmp16 &= ~PCI_BCNF_BCNTRL_MAST_AB_MODE;
1142 else
1143 tmp16 |= PCI_BCNF_BCNTRL_MAST_AB_MODE;
1144 PCIE_PUT(16, bus_p, PCI_BCNF_BCNTRL, tmp16);
1145 PCIE_DBG_CFG(cdip, bus_p, "SEC CMD", 16, PCI_BCNF_BCNTRL,
1146 reg16);
1147 }
1148
1149 if (PCIE_IS_PCIE(bus_p)) {
1150 /*
1151 * Get the device control register into an initial state that
1152 * makes sense. The maximum payload, tagging, and related will
1153 * be dealt with in pcie_fabric_setup().
1154 */
1155 reg16 = PCIE_CAP_GET(16, bus_p, PCIE_DEVCTL);
1156 tmp16 = (reg16 & pcie_devctl_default_mask) |
1157 (pcie_devctl_default & ~pcie_devctl_default_mask);
1158 PCIE_CAP_PUT(16, bus_p, PCIE_DEVCTL, tmp16);
1159 PCIE_DBG_CAP(cdip, bus_p, "DEVCTL", 16, PCIE_DEVCTL, reg16);
1160
1161 /* Enable PCIe errors */
1162 pcie_enable_errors(cdip);
1163
1164 pcie_determine_serial(cdip);
1165
1166 pcie_determine_aspm(cdip);
1167
1168 pcie_capture_speeds(cdip);
1169 }
1170
1171 bus_p->bus_ari = B_FALSE;
1172 if ((pcie_ari_is_enabled(ddi_get_parent(cdip))
1173 == PCIE_ARI_FORW_ENABLED) && (pcie_ari_device(cdip)
1174 == PCIE_ARI_DEVICE)) {
1175 bus_p->bus_ari = B_TRUE;
1176 }
1177
1178 return (DDI_SUCCESS);
1179 }
1180
1181 static void
pcie_init_pfd(dev_info_t * dip)1182 pcie_init_pfd(dev_info_t *dip)
1183 {
1184 pf_data_t *pfd_p = PCIE_ZALLOC(pf_data_t);
1185 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
1186
1187 PCIE_DIP2PFD(dip) = pfd_p;
1188
1189 pfd_p->pe_bus_p = bus_p;
1190 pfd_p->pe_severity_flags = 0;
1191 pfd_p->pe_severity_mask = 0;
1192 pfd_p->pe_orig_severity_flags = 0;
1193 pfd_p->pe_lock = B_FALSE;
1194 pfd_p->pe_valid = B_FALSE;
1195
1196 /* Allocate the root fault struct for both RC and RP */
1197 if (PCIE_IS_ROOT(bus_p)) {
1198 PCIE_ROOT_FAULT(pfd_p) = PCIE_ZALLOC(pf_root_fault_t);
1199 PCIE_ROOT_FAULT(pfd_p)->scan_bdf = PCIE_INVALID_BDF;
1200 PCIE_ROOT_EH_SRC(pfd_p) = PCIE_ZALLOC(pf_root_eh_src_t);
1201 }
1202
1203 PCI_ERR_REG(pfd_p) = PCIE_ZALLOC(pf_pci_err_regs_t);
1204 PFD_AFFECTED_DEV(pfd_p) = PCIE_ZALLOC(pf_affected_dev_t);
1205 PFD_AFFECTED_DEV(pfd_p)->pe_affected_bdf = PCIE_INVALID_BDF;
1206
1207 if (PCIE_IS_BDG(bus_p))
1208 PCI_BDG_ERR_REG(pfd_p) = PCIE_ZALLOC(pf_pci_bdg_err_regs_t);
1209
1210 if (PCIE_IS_PCIE(bus_p)) {
1211 PCIE_ERR_REG(pfd_p) = PCIE_ZALLOC(pf_pcie_err_regs_t);
1212
1213 if (PCIE_IS_RP(bus_p))
1214 PCIE_RP_REG(pfd_p) =
1215 PCIE_ZALLOC(pf_pcie_rp_err_regs_t);
1216
1217 PCIE_ADV_REG(pfd_p) = PCIE_ZALLOC(pf_pcie_adv_err_regs_t);
1218 PCIE_ADV_REG(pfd_p)->pcie_ue_tgt_bdf = PCIE_INVALID_BDF;
1219
1220 if (PCIE_IS_RP(bus_p)) {
1221 PCIE_ADV_RP_REG(pfd_p) =
1222 PCIE_ZALLOC(pf_pcie_adv_rp_err_regs_t);
1223 PCIE_ADV_RP_REG(pfd_p)->pcie_rp_ce_src_id =
1224 PCIE_INVALID_BDF;
1225 PCIE_ADV_RP_REG(pfd_p)->pcie_rp_ue_src_id =
1226 PCIE_INVALID_BDF;
1227 } else if (PCIE_IS_PCIE_BDG(bus_p)) {
1228 PCIE_ADV_BDG_REG(pfd_p) =
1229 PCIE_ZALLOC(pf_pcie_adv_bdg_err_regs_t);
1230 PCIE_ADV_BDG_REG(pfd_p)->pcie_sue_tgt_bdf =
1231 PCIE_INVALID_BDF;
1232 }
1233
1234 if (PCIE_IS_PCIE_BDG(bus_p) && PCIE_IS_PCIX(bus_p)) {
1235 PCIX_BDG_ERR_REG(pfd_p) =
1236 PCIE_ZALLOC(pf_pcix_bdg_err_regs_t);
1237
1238 if (PCIX_ECC_VERSION_CHECK(bus_p)) {
1239 PCIX_BDG_ECC_REG(pfd_p, 0) =
1240 PCIE_ZALLOC(pf_pcix_ecc_regs_t);
1241 PCIX_BDG_ECC_REG(pfd_p, 1) =
1242 PCIE_ZALLOC(pf_pcix_ecc_regs_t);
1243 }
1244 }
1245
1246 PCIE_SLOT_REG(pfd_p) = PCIE_ZALLOC(pf_pcie_slot_regs_t);
1247 PCIE_SLOT_REG(pfd_p)->pcie_slot_regs_valid = B_FALSE;
1248 PCIE_SLOT_REG(pfd_p)->pcie_slot_cap = 0;
1249 PCIE_SLOT_REG(pfd_p)->pcie_slot_control = 0;
1250 PCIE_SLOT_REG(pfd_p)->pcie_slot_status = 0;
1251
1252 } else if (PCIE_IS_PCIX(bus_p)) {
1253 if (PCIE_IS_BDG(bus_p)) {
1254 PCIX_BDG_ERR_REG(pfd_p) =
1255 PCIE_ZALLOC(pf_pcix_bdg_err_regs_t);
1256
1257 if (PCIX_ECC_VERSION_CHECK(bus_p)) {
1258 PCIX_BDG_ECC_REG(pfd_p, 0) =
1259 PCIE_ZALLOC(pf_pcix_ecc_regs_t);
1260 PCIX_BDG_ECC_REG(pfd_p, 1) =
1261 PCIE_ZALLOC(pf_pcix_ecc_regs_t);
1262 }
1263 } else {
1264 PCIX_ERR_REG(pfd_p) = PCIE_ZALLOC(pf_pcix_err_regs_t);
1265
1266 if (PCIX_ECC_VERSION_CHECK(bus_p))
1267 PCIX_ECC_REG(pfd_p) =
1268 PCIE_ZALLOC(pf_pcix_ecc_regs_t);
1269 }
1270 }
1271 }
1272
1273 static void
pcie_fini_pfd(dev_info_t * dip)1274 pcie_fini_pfd(dev_info_t *dip)
1275 {
1276 pf_data_t *pfd_p = PCIE_DIP2PFD(dip);
1277 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
1278
1279 if (PCIE_IS_PCIE(bus_p)) {
1280 if (PCIE_IS_PCIE_BDG(bus_p) && PCIE_IS_PCIX(bus_p)) {
1281 if (PCIX_ECC_VERSION_CHECK(bus_p)) {
1282 kmem_free(PCIX_BDG_ECC_REG(pfd_p, 0),
1283 sizeof (pf_pcix_ecc_regs_t));
1284 kmem_free(PCIX_BDG_ECC_REG(pfd_p, 1),
1285 sizeof (pf_pcix_ecc_regs_t));
1286 }
1287
1288 kmem_free(PCIX_BDG_ERR_REG(pfd_p),
1289 sizeof (pf_pcix_bdg_err_regs_t));
1290 }
1291
1292 if (PCIE_IS_RP(bus_p))
1293 kmem_free(PCIE_ADV_RP_REG(pfd_p),
1294 sizeof (pf_pcie_adv_rp_err_regs_t));
1295 else if (PCIE_IS_PCIE_BDG(bus_p))
1296 kmem_free(PCIE_ADV_BDG_REG(pfd_p),
1297 sizeof (pf_pcie_adv_bdg_err_regs_t));
1298
1299 kmem_free(PCIE_ADV_REG(pfd_p),
1300 sizeof (pf_pcie_adv_err_regs_t));
1301
1302 if (PCIE_IS_RP(bus_p))
1303 kmem_free(PCIE_RP_REG(pfd_p),
1304 sizeof (pf_pcie_rp_err_regs_t));
1305
1306 kmem_free(PCIE_ERR_REG(pfd_p), sizeof (pf_pcie_err_regs_t));
1307 } else if (PCIE_IS_PCIX(bus_p)) {
1308 if (PCIE_IS_BDG(bus_p)) {
1309 if (PCIX_ECC_VERSION_CHECK(bus_p)) {
1310 kmem_free(PCIX_BDG_ECC_REG(pfd_p, 0),
1311 sizeof (pf_pcix_ecc_regs_t));
1312 kmem_free(PCIX_BDG_ECC_REG(pfd_p, 1),
1313 sizeof (pf_pcix_ecc_regs_t));
1314 }
1315
1316 kmem_free(PCIX_BDG_ERR_REG(pfd_p),
1317 sizeof (pf_pcix_bdg_err_regs_t));
1318 } else {
1319 if (PCIX_ECC_VERSION_CHECK(bus_p))
1320 kmem_free(PCIX_ECC_REG(pfd_p),
1321 sizeof (pf_pcix_ecc_regs_t));
1322
1323 kmem_free(PCIX_ERR_REG(pfd_p),
1324 sizeof (pf_pcix_err_regs_t));
1325 }
1326 }
1327
1328 if (PCIE_IS_BDG(bus_p))
1329 kmem_free(PCI_BDG_ERR_REG(pfd_p),
1330 sizeof (pf_pci_bdg_err_regs_t));
1331
1332 kmem_free(PFD_AFFECTED_DEV(pfd_p), sizeof (pf_affected_dev_t));
1333 kmem_free(PCI_ERR_REG(pfd_p), sizeof (pf_pci_err_regs_t));
1334
1335 if (PCIE_IS_ROOT(bus_p)) {
1336 kmem_free(PCIE_ROOT_FAULT(pfd_p), sizeof (pf_root_fault_t));
1337 kmem_free(PCIE_ROOT_EH_SRC(pfd_p), sizeof (pf_root_eh_src_t));
1338 }
1339
1340 kmem_free(PCIE_DIP2PFD(dip), sizeof (pf_data_t));
1341
1342 PCIE_DIP2PFD(dip) = NULL;
1343 }
1344
1345
1346 /*
1347 * Special functions to allocate pf_data_t's for PCIe root complexes.
1348 * Note: Root Complex not Root Port
1349 */
1350 void
pcie_rc_init_pfd(dev_info_t * dip,pf_data_t * pfd_p)1351 pcie_rc_init_pfd(dev_info_t *dip, pf_data_t *pfd_p)
1352 {
1353 pfd_p->pe_bus_p = PCIE_DIP2DOWNBUS(dip);
1354 pfd_p->pe_severity_flags = 0;
1355 pfd_p->pe_severity_mask = 0;
1356 pfd_p->pe_orig_severity_flags = 0;
1357 pfd_p->pe_lock = B_FALSE;
1358 pfd_p->pe_valid = B_FALSE;
1359
1360 PCIE_ROOT_FAULT(pfd_p) = PCIE_ZALLOC(pf_root_fault_t);
1361 PCIE_ROOT_FAULT(pfd_p)->scan_bdf = PCIE_INVALID_BDF;
1362 PCIE_ROOT_EH_SRC(pfd_p) = PCIE_ZALLOC(pf_root_eh_src_t);
1363 PCI_ERR_REG(pfd_p) = PCIE_ZALLOC(pf_pci_err_regs_t);
1364 PFD_AFFECTED_DEV(pfd_p) = PCIE_ZALLOC(pf_affected_dev_t);
1365 PFD_AFFECTED_DEV(pfd_p)->pe_affected_bdf = PCIE_INVALID_BDF;
1366 PCI_BDG_ERR_REG(pfd_p) = PCIE_ZALLOC(pf_pci_bdg_err_regs_t);
1367 PCIE_ERR_REG(pfd_p) = PCIE_ZALLOC(pf_pcie_err_regs_t);
1368 PCIE_RP_REG(pfd_p) = PCIE_ZALLOC(pf_pcie_rp_err_regs_t);
1369 PCIE_ADV_REG(pfd_p) = PCIE_ZALLOC(pf_pcie_adv_err_regs_t);
1370 PCIE_ADV_RP_REG(pfd_p) = PCIE_ZALLOC(pf_pcie_adv_rp_err_regs_t);
1371 PCIE_ADV_RP_REG(pfd_p)->pcie_rp_ce_src_id = PCIE_INVALID_BDF;
1372 PCIE_ADV_RP_REG(pfd_p)->pcie_rp_ue_src_id = PCIE_INVALID_BDF;
1373
1374 PCIE_ADV_REG(pfd_p)->pcie_ue_sev = pcie_aer_uce_severity;
1375 }
1376
1377 void
pcie_rc_fini_pfd(pf_data_t * pfd_p)1378 pcie_rc_fini_pfd(pf_data_t *pfd_p)
1379 {
1380 kmem_free(PCIE_ADV_RP_REG(pfd_p), sizeof (pf_pcie_adv_rp_err_regs_t));
1381 kmem_free(PCIE_ADV_REG(pfd_p), sizeof (pf_pcie_adv_err_regs_t));
1382 kmem_free(PCIE_RP_REG(pfd_p), sizeof (pf_pcie_rp_err_regs_t));
1383 kmem_free(PCIE_ERR_REG(pfd_p), sizeof (pf_pcie_err_regs_t));
1384 kmem_free(PCI_BDG_ERR_REG(pfd_p), sizeof (pf_pci_bdg_err_regs_t));
1385 kmem_free(PFD_AFFECTED_DEV(pfd_p), sizeof (pf_affected_dev_t));
1386 kmem_free(PCI_ERR_REG(pfd_p), sizeof (pf_pci_err_regs_t));
1387 kmem_free(PCIE_ROOT_FAULT(pfd_p), sizeof (pf_root_fault_t));
1388 kmem_free(PCIE_ROOT_EH_SRC(pfd_p), sizeof (pf_root_eh_src_t));
1389 }
1390
1391 /*
1392 * init pcie_bus_t for root complex
1393 *
1394 * Only a few of the fields in bus_t is valid for root complex.
1395 * The fields that are bracketed are initialized in this routine:
1396 *
1397 * dev_info_t * <bus_dip>
1398 * dev_info_t * bus_rp_dip
1399 * ddi_acc_handle_t bus_cfg_hdl
1400 * uint_t <bus_fm_flags>
1401 * pcie_req_id_t bus_bdf
1402 * pcie_req_id_t bus_rp_bdf
1403 * uint32_t bus_dev_ven_id
1404 * uint8_t bus_rev_id
1405 * uint8_t <bus_hdr_type>
1406 * uint16_t <bus_dev_type>
1407 * uint8_t bus_bdg_secbus
1408 * uint16_t bus_pcie_off
1409 * uint16_t <bus_aer_off>
1410 * uint16_t bus_pcix_off
1411 * uint16_t bus_ecc_ver
1412 * pci_bus_range_t bus_bus_range
1413 * ppb_ranges_t * bus_addr_ranges
1414 * int bus_addr_entries
1415 * pci_regspec_t * bus_assigned_addr
1416 * int bus_assigned_entries
1417 * pf_data_t * bus_pfd
1418 * pcie_domain_t * <bus_dom>
1419 * int bus_mps
1420 * uint64_t bus_cfgacc_base
1421 * void * bus_plat_private
1422 */
1423 void
pcie_rc_init_bus(dev_info_t * dip)1424 pcie_rc_init_bus(dev_info_t *dip)
1425 {
1426 pcie_bus_t *bus_p;
1427
1428 bus_p = (pcie_bus_t *)kmem_zalloc(sizeof (pcie_bus_t), KM_SLEEP);
1429 bus_p->bus_dip = dip;
1430 bus_p->bus_dev_type = PCIE_PCIECAP_DEV_TYPE_RC_PSEUDO;
1431 bus_p->bus_hdr_type = PCI_HEADER_ONE;
1432
1433 /* Fake that there are AER logs */
1434 bus_p->bus_aer_off = (uint16_t)-1;
1435
1436 /* Needed only for handle lookup */
1437 atomic_or_uint(&bus_p->bus_fm_flags, PF_FM_READY);
1438
1439 ndi_set_bus_private(dip, B_FALSE, DEVI_PORT_TYPE_PCI, bus_p);
1440
1441 PCIE_BUS2DOM(bus_p) = PCIE_ZALLOC(pcie_domain_t);
1442 }
1443
1444 void
pcie_rc_fini_bus(dev_info_t * dip)1445 pcie_rc_fini_bus(dev_info_t *dip)
1446 {
1447 pcie_bus_t *bus_p = PCIE_DIP2DOWNBUS(dip);
1448 ndi_set_bus_private(dip, B_FALSE, 0, NULL);
1449 kmem_free(PCIE_BUS2DOM(bus_p), sizeof (pcie_domain_t));
1450 kmem_free(bus_p, sizeof (pcie_bus_t));
1451 }
1452
1453 static int
pcie_width_to_int(pcie_link_width_t width)1454 pcie_width_to_int(pcie_link_width_t width)
1455 {
1456 switch (width) {
1457 case PCIE_LINK_WIDTH_X1:
1458 return (1);
1459 case PCIE_LINK_WIDTH_X2:
1460 return (2);
1461 case PCIE_LINK_WIDTH_X4:
1462 return (4);
1463 case PCIE_LINK_WIDTH_X8:
1464 return (8);
1465 case PCIE_LINK_WIDTH_X12:
1466 return (12);
1467 case PCIE_LINK_WIDTH_X16:
1468 return (16);
1469 case PCIE_LINK_WIDTH_X32:
1470 return (32);
1471 default:
1472 return (0);
1473 }
1474 }
1475
1476 /*
1477 * Return the speed in Transfers / second. This is a signed quantity to match
1478 * the ndi/ddi property interfaces.
1479 */
1480 static int64_t
pcie_speed_to_int(pcie_link_speed_t speed)1481 pcie_speed_to_int(pcie_link_speed_t speed)
1482 {
1483 switch (speed) {
1484 case PCIE_LINK_SPEED_2_5:
1485 return (2500000000LL);
1486 case PCIE_LINK_SPEED_5:
1487 return (5000000000LL);
1488 case PCIE_LINK_SPEED_8:
1489 return (8000000000LL);
1490 case PCIE_LINK_SPEED_16:
1491 return (16000000000LL);
1492 case PCIE_LINK_SPEED_32:
1493 return (32000000000LL);
1494 case PCIE_LINK_SPEED_64:
1495 return (64000000000LL);
1496 default:
1497 return (0);
1498 }
1499 }
1500
1501 /*
1502 * Translate the recorded speed information into devinfo properties.
1503 */
1504 static void
pcie_speeds_to_devinfo(dev_info_t * dip,pcie_bus_t * bus_p)1505 pcie_speeds_to_devinfo(dev_info_t *dip, pcie_bus_t *bus_p)
1506 {
1507 if (bus_p->bus_max_width != PCIE_LINK_WIDTH_UNKNOWN) {
1508 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1509 "pcie-link-maximum-width",
1510 pcie_width_to_int(bus_p->bus_max_width));
1511 }
1512
1513 if (bus_p->bus_cur_width != PCIE_LINK_WIDTH_UNKNOWN) {
1514 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
1515 "pcie-link-current-width",
1516 pcie_width_to_int(bus_p->bus_cur_width));
1517 }
1518
1519 if (bus_p->bus_cur_speed != PCIE_LINK_SPEED_UNKNOWN) {
1520 (void) ndi_prop_update_int64(DDI_DEV_T_NONE, dip,
1521 "pcie-link-current-speed",
1522 pcie_speed_to_int(bus_p->bus_cur_speed));
1523 }
1524
1525 if (bus_p->bus_max_speed != PCIE_LINK_SPEED_UNKNOWN) {
1526 (void) ndi_prop_update_int64(DDI_DEV_T_NONE, dip,
1527 "pcie-link-maximum-speed",
1528 pcie_speed_to_int(bus_p->bus_max_speed));
1529 }
1530
1531 if (bus_p->bus_target_speed != PCIE_LINK_SPEED_UNKNOWN) {
1532 (void) ndi_prop_update_int64(DDI_DEV_T_NONE, dip,
1533 "pcie-link-target-speed",
1534 pcie_speed_to_int(bus_p->bus_target_speed));
1535 }
1536
1537 if ((bus_p->bus_speed_flags & PCIE_LINK_F_ADMIN_TARGET) != 0) {
1538 (void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
1539 "pcie-link-admin-target-speed");
1540 }
1541
1542 if (bus_p->bus_sup_speed != PCIE_LINK_SPEED_UNKNOWN) {
1543 int64_t speeds[PCIE_NSPEEDS];
1544 uint_t nspeeds = 0;
1545
1546 if (bus_p->bus_sup_speed & PCIE_LINK_SPEED_2_5) {
1547 speeds[nspeeds++] =
1548 pcie_speed_to_int(PCIE_LINK_SPEED_2_5);
1549 }
1550
1551 if (bus_p->bus_sup_speed & PCIE_LINK_SPEED_5) {
1552 speeds[nspeeds++] =
1553 pcie_speed_to_int(PCIE_LINK_SPEED_5);
1554 }
1555
1556 if (bus_p->bus_sup_speed & PCIE_LINK_SPEED_8) {
1557 speeds[nspeeds++] =
1558 pcie_speed_to_int(PCIE_LINK_SPEED_8);
1559 }
1560
1561 if (bus_p->bus_sup_speed & PCIE_LINK_SPEED_16) {
1562 speeds[nspeeds++] =
1563 pcie_speed_to_int(PCIE_LINK_SPEED_16);
1564 }
1565
1566 if (bus_p->bus_sup_speed & PCIE_LINK_SPEED_32) {
1567 speeds[nspeeds++] =
1568 pcie_speed_to_int(PCIE_LINK_SPEED_32);
1569 }
1570
1571 if (bus_p->bus_sup_speed & PCIE_LINK_SPEED_64) {
1572 speeds[nspeeds++] =
1573 pcie_speed_to_int(PCIE_LINK_SPEED_64);
1574 }
1575
1576 (void) ndi_prop_update_int64_array(DDI_DEV_T_NONE, dip,
1577 "pcie-link-supported-speeds", speeds, nspeeds);
1578 }
1579 }
1580
1581 /*
1582 * We need to capture the supported, maximum, and current device speed and
1583 * width. The way that this has been done has changed over time.
1584 *
1585 * Prior to PCIe Gen 3, there were only current and supported speed fields.
1586 * These were found in the link status and link capabilities registers of the
1587 * PCI express capability. With the change to PCIe Gen 3, the information in the
1588 * link capabilities changed to the maximum value. The supported speeds vector
1589 * was moved to the link capabilities 2 register.
1590 *
1591 * Now, a device may not implement some of these registers. To determine whether
1592 * or not it's here, we have to do the following. First, we need to check the
1593 * revision of the PCI express capability. The link capabilities 2 register did
1594 * not exist prior to version 2 of this capability. If a modern device does not
1595 * implement it, it is supposed to return zero for the register.
1596 */
1597 static void
pcie_capture_speeds(dev_info_t * dip)1598 pcie_capture_speeds(dev_info_t *dip)
1599 {
1600 uint16_t vers, status;
1601 uint32_t cap, cap2, ctl2;
1602 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
1603 dev_info_t *rcdip;
1604
1605 if (!PCIE_IS_PCIE(bus_p))
1606 return;
1607
1608 rcdip = pcie_get_rc_dip(dip);
1609 if (bus_p->bus_cfg_hdl == NULL) {
1610 vers = pci_cfgacc_get16(rcdip, bus_p->bus_bdf,
1611 bus_p->bus_pcie_off + PCIE_PCIECAP);
1612 } else {
1613 vers = PCIE_CAP_GET(16, bus_p, PCIE_PCIECAP);
1614 }
1615 if (vers == PCI_EINVAL16)
1616 return;
1617 vers &= PCIE_PCIECAP_VER_MASK;
1618
1619 /*
1620 * Verify the capability's version.
1621 */
1622 switch (vers) {
1623 case PCIE_PCIECAP_VER_1_0:
1624 cap2 = 0;
1625 ctl2 = 0;
1626 break;
1627 case PCIE_PCIECAP_VER_2_0:
1628 if (bus_p->bus_cfg_hdl == NULL) {
1629 cap2 = pci_cfgacc_get32(rcdip, bus_p->bus_bdf,
1630 bus_p->bus_pcie_off + PCIE_LINKCAP2);
1631 ctl2 = pci_cfgacc_get16(rcdip, bus_p->bus_bdf,
1632 bus_p->bus_pcie_off + PCIE_LINKCTL2);
1633 } else {
1634 cap2 = PCIE_CAP_GET(32, bus_p, PCIE_LINKCAP2);
1635 ctl2 = PCIE_CAP_GET(16, bus_p, PCIE_LINKCTL2);
1636 }
1637 if (cap2 == PCI_EINVAL32)
1638 cap2 = 0;
1639 if (ctl2 == PCI_EINVAL16)
1640 ctl2 = 0;
1641 break;
1642 default:
1643 /* Don't try and handle an unknown version */
1644 return;
1645 }
1646
1647 if (bus_p->bus_cfg_hdl == NULL) {
1648 status = pci_cfgacc_get16(rcdip, bus_p->bus_bdf,
1649 bus_p->bus_pcie_off + PCIE_LINKSTS);
1650 cap = pci_cfgacc_get32(rcdip, bus_p->bus_bdf,
1651 bus_p->bus_pcie_off + PCIE_LINKCAP);
1652 } else {
1653 status = PCIE_CAP_GET(16, bus_p, PCIE_LINKSTS);
1654 cap = PCIE_CAP_GET(32, bus_p, PCIE_LINKCAP);
1655 }
1656 if (status == PCI_EINVAL16 || cap == PCI_EINVAL32)
1657 return;
1658
1659 mutex_enter(&bus_p->bus_speed_mutex);
1660
1661 switch (status & PCIE_LINKSTS_SPEED_MASK) {
1662 case PCIE_LINKSTS_SPEED_2_5:
1663 bus_p->bus_cur_speed = PCIE_LINK_SPEED_2_5;
1664 break;
1665 case PCIE_LINKSTS_SPEED_5:
1666 bus_p->bus_cur_speed = PCIE_LINK_SPEED_5;
1667 break;
1668 case PCIE_LINKSTS_SPEED_8:
1669 bus_p->bus_cur_speed = PCIE_LINK_SPEED_8;
1670 break;
1671 case PCIE_LINKSTS_SPEED_16:
1672 bus_p->bus_cur_speed = PCIE_LINK_SPEED_16;
1673 break;
1674 case PCIE_LINKSTS_SPEED_32:
1675 bus_p->bus_cur_speed = PCIE_LINK_SPEED_32;
1676 break;
1677 case PCIE_LINKSTS_SPEED_64:
1678 bus_p->bus_cur_speed = PCIE_LINK_SPEED_64;
1679 break;
1680 default:
1681 bus_p->bus_cur_speed = PCIE_LINK_SPEED_UNKNOWN;
1682 break;
1683 }
1684
1685 switch (status & PCIE_LINKSTS_NEG_WIDTH_MASK) {
1686 case PCIE_LINKSTS_NEG_WIDTH_X1:
1687 bus_p->bus_cur_width = PCIE_LINK_WIDTH_X1;
1688 break;
1689 case PCIE_LINKSTS_NEG_WIDTH_X2:
1690 bus_p->bus_cur_width = PCIE_LINK_WIDTH_X2;
1691 break;
1692 case PCIE_LINKSTS_NEG_WIDTH_X4:
1693 bus_p->bus_cur_width = PCIE_LINK_WIDTH_X4;
1694 break;
1695 case PCIE_LINKSTS_NEG_WIDTH_X8:
1696 bus_p->bus_cur_width = PCIE_LINK_WIDTH_X8;
1697 break;
1698 case PCIE_LINKSTS_NEG_WIDTH_X12:
1699 bus_p->bus_cur_width = PCIE_LINK_WIDTH_X12;
1700 break;
1701 case PCIE_LINKSTS_NEG_WIDTH_X16:
1702 bus_p->bus_cur_width = PCIE_LINK_WIDTH_X16;
1703 break;
1704 case PCIE_LINKSTS_NEG_WIDTH_X32:
1705 bus_p->bus_cur_width = PCIE_LINK_WIDTH_X32;
1706 break;
1707 default:
1708 bus_p->bus_cur_width = PCIE_LINK_WIDTH_UNKNOWN;
1709 break;
1710 }
1711
1712 switch (cap & PCIE_LINKCAP_MAX_WIDTH_MASK) {
1713 case PCIE_LINKCAP_MAX_WIDTH_X1:
1714 bus_p->bus_max_width = PCIE_LINK_WIDTH_X1;
1715 break;
1716 case PCIE_LINKCAP_MAX_WIDTH_X2:
1717 bus_p->bus_max_width = PCIE_LINK_WIDTH_X2;
1718 break;
1719 case PCIE_LINKCAP_MAX_WIDTH_X4:
1720 bus_p->bus_max_width = PCIE_LINK_WIDTH_X4;
1721 break;
1722 case PCIE_LINKCAP_MAX_WIDTH_X8:
1723 bus_p->bus_max_width = PCIE_LINK_WIDTH_X8;
1724 break;
1725 case PCIE_LINKCAP_MAX_WIDTH_X12:
1726 bus_p->bus_max_width = PCIE_LINK_WIDTH_X12;
1727 break;
1728 case PCIE_LINKCAP_MAX_WIDTH_X16:
1729 bus_p->bus_max_width = PCIE_LINK_WIDTH_X16;
1730 break;
1731 case PCIE_LINKCAP_MAX_WIDTH_X32:
1732 bus_p->bus_max_width = PCIE_LINK_WIDTH_X32;
1733 break;
1734 default:
1735 bus_p->bus_max_width = PCIE_LINK_WIDTH_UNKNOWN;
1736 break;
1737 }
1738
1739 /*
1740 * If we have the Link Capabilities 2, then we can get the supported
1741 * speeds from it and treat the bits in Link Capabilities 1 as the
1742 * maximum. If we don't, then we need to follow the Implementation Note
1743 * in the standard under Link Capabilities 2. Effectively, this means
1744 * that if the value of 10b is set in Link Capabilities register, that
1745 * it supports both 2.5 and 5 GT/s speeds.
1746 */
1747 if (cap2 != 0) {
1748 if (cap2 & PCIE_LINKCAP2_SPEED_2_5)
1749 bus_p->bus_sup_speed |= PCIE_LINK_SPEED_2_5;
1750 if (cap2 & PCIE_LINKCAP2_SPEED_5)
1751 bus_p->bus_sup_speed |= PCIE_LINK_SPEED_5;
1752 if (cap2 & PCIE_LINKCAP2_SPEED_8)
1753 bus_p->bus_sup_speed |= PCIE_LINK_SPEED_8;
1754 if (cap2 & PCIE_LINKCAP2_SPEED_16)
1755 bus_p->bus_sup_speed |= PCIE_LINK_SPEED_16;
1756 if (cap2 & PCIE_LINKCAP2_SPEED_32)
1757 bus_p->bus_sup_speed |= PCIE_LINK_SPEED_32;
1758 if (cap2 & PCIE_LINKCAP2_SPEED_64)
1759 bus_p->bus_sup_speed |= PCIE_LINK_SPEED_64;
1760
1761 switch (cap & PCIE_LINKCAP_MAX_SPEED_MASK) {
1762 case PCIE_LINKCAP_MAX_SPEED_2_5:
1763 bus_p->bus_max_speed = PCIE_LINK_SPEED_2_5;
1764 break;
1765 case PCIE_LINKCAP_MAX_SPEED_5:
1766 bus_p->bus_max_speed = PCIE_LINK_SPEED_5;
1767 break;
1768 case PCIE_LINKCAP_MAX_SPEED_8:
1769 bus_p->bus_max_speed = PCIE_LINK_SPEED_8;
1770 break;
1771 case PCIE_LINKCAP_MAX_SPEED_16:
1772 bus_p->bus_max_speed = PCIE_LINK_SPEED_16;
1773 break;
1774 case PCIE_LINKCAP_MAX_SPEED_32:
1775 bus_p->bus_max_speed = PCIE_LINK_SPEED_32;
1776 break;
1777 case PCIE_LINKCAP_MAX_SPEED_64:
1778 bus_p->bus_max_speed = PCIE_LINK_SPEED_64;
1779 break;
1780 default:
1781 bus_p->bus_max_speed = PCIE_LINK_SPEED_UNKNOWN;
1782 break;
1783 }
1784 } else {
1785 if (cap & PCIE_LINKCAP_MAX_SPEED_5) {
1786 bus_p->bus_max_speed = PCIE_LINK_SPEED_5;
1787 bus_p->bus_sup_speed = PCIE_LINK_SPEED_2_5 |
1788 PCIE_LINK_SPEED_5;
1789 } else if (cap & PCIE_LINKCAP_MAX_SPEED_2_5) {
1790 bus_p->bus_max_speed = PCIE_LINK_SPEED_2_5;
1791 bus_p->bus_sup_speed = PCIE_LINK_SPEED_2_5;
1792 }
1793 }
1794
1795 switch (ctl2 & PCIE_LINKCTL2_TARGET_SPEED_MASK) {
1796 case PCIE_LINKCTL2_TARGET_SPEED_2_5:
1797 bus_p->bus_target_speed = PCIE_LINK_SPEED_2_5;
1798 break;
1799 case PCIE_LINKCTL2_TARGET_SPEED_5:
1800 bus_p->bus_target_speed = PCIE_LINK_SPEED_5;
1801 break;
1802 case PCIE_LINKCTL2_TARGET_SPEED_8:
1803 bus_p->bus_target_speed = PCIE_LINK_SPEED_8;
1804 break;
1805 case PCIE_LINKCTL2_TARGET_SPEED_16:
1806 bus_p->bus_target_speed = PCIE_LINK_SPEED_16;
1807 break;
1808 case PCIE_LINKCTL2_TARGET_SPEED_32:
1809 bus_p->bus_target_speed = PCIE_LINK_SPEED_32;
1810 break;
1811 case PCIE_LINKCTL2_TARGET_SPEED_64:
1812 bus_p->bus_target_speed = PCIE_LINK_SPEED_64;
1813 break;
1814 default:
1815 bus_p->bus_target_speed = PCIE_LINK_SPEED_UNKNOWN;
1816 break;
1817 }
1818
1819 pcie_speeds_to_devinfo(dip, bus_p);
1820 mutex_exit(&bus_p->bus_speed_mutex);
1821 }
1822
1823 /*
1824 * partially init pcie_bus_t for device (dip,bdf) for accessing pci
1825 * config space
1826 *
1827 * This routine is invoked during boot, either after creating a devinfo node
1828 * (x86 case) or during px driver attach (sparc case); it is also invoked
1829 * in hotplug context after a devinfo node is created.
1830 *
1831 * The fields that are bracketed are initialized if flag PCIE_BUS_INITIAL
1832 * is set:
1833 *
1834 * dev_info_t * <bus_dip>
1835 * dev_info_t * <bus_rp_dip>
1836 * ddi_acc_handle_t bus_cfg_hdl
1837 * uint_t bus_fm_flags
1838 * pcie_req_id_t <bus_bdf>
1839 * pcie_req_id_t <bus_rp_bdf>
1840 * uint32_t <bus_dev_ven_id>
1841 * uint8_t <bus_rev_id>
1842 * uint8_t <bus_hdr_type>
1843 * uint16_t <bus_dev_type>
1844 * uint8_t <bus_bdg_secbus
1845 * uint16_t <bus_pcie_off>
1846 * uint16_t <bus_aer_off>
1847 * uint16_t <bus_pcix_off>
1848 * uint16_t <bus_ecc_ver>
1849 * pci_bus_range_t bus_bus_range
1850 * ppb_ranges_t * bus_addr_ranges
1851 * int bus_addr_entries
1852 * pci_regspec_t * bus_assigned_addr
1853 * int bus_assigned_entries
1854 * pf_data_t * bus_pfd
1855 * pcie_domain_t * bus_dom
1856 * int bus_mps
1857 * uint64_t bus_cfgacc_base
1858 * void * bus_plat_private
1859 *
1860 * The fields that are bracketed are initialized if flag PCIE_BUS_FINAL
1861 * is set:
1862 *
1863 * dev_info_t * bus_dip
1864 * dev_info_t * bus_rp_dip
1865 * ddi_acc_handle_t bus_cfg_hdl
1866 * uint_t bus_fm_flags
1867 * pcie_req_id_t bus_bdf
1868 * pcie_req_id_t bus_rp_bdf
1869 * uint32_t bus_dev_ven_id
1870 * uint8_t bus_rev_id
1871 * uint8_t bus_hdr_type
1872 * uint16_t bus_dev_type
1873 * uint8_t <bus_bdg_secbus>
1874 * uint16_t bus_pcie_off
1875 * uint16_t bus_aer_off
1876 * uint16_t bus_pcix_off
1877 * uint16_t bus_ecc_ver
1878 * pci_bus_range_t <bus_bus_range>
1879 * ppb_ranges_t * <bus_addr_ranges>
1880 * int <bus_addr_entries>
1881 * pci_regspec_t * <bus_assigned_addr>
1882 * int <bus_assigned_entries>
1883 * pf_data_t * <bus_pfd>
1884 * pcie_domain_t * bus_dom
1885 * int bus_mps
1886 * uint64_t bus_cfgacc_base
1887 * void * <bus_plat_private>
1888 */
1889
1890 pcie_bus_t *
pcie_init_bus(dev_info_t * dip,pcie_req_id_t bdf,uint8_t flags)1891 pcie_init_bus(dev_info_t *dip, pcie_req_id_t bdf, uint8_t flags)
1892 {
1893 uint16_t status, base, baseptr, num_cap;
1894 uint32_t capid;
1895 int range_size;
1896 pcie_bus_t *bus_p = NULL;
1897 dev_info_t *rcdip;
1898 dev_info_t *pdip;
1899 const char *errstr = NULL;
1900
1901 if (!(flags & PCIE_BUS_INITIAL))
1902 goto initial_done;
1903
1904 bus_p = kmem_zalloc(sizeof (pcie_bus_t), KM_SLEEP);
1905
1906 bus_p->bus_dip = dip;
1907 bus_p->bus_bdf = bdf;
1908
1909 rcdip = pcie_get_rc_dip(dip);
1910 ASSERT(rcdip != NULL);
1911
1912 /* Save the Vendor ID, Device ID and revision ID */
1913 bus_p->bus_dev_ven_id = pci_cfgacc_get32(rcdip, bdf, PCI_CONF_VENID);
1914 bus_p->bus_rev_id = pci_cfgacc_get8(rcdip, bdf, PCI_CONF_REVID);
1915 /* Save the Header Type */
1916 bus_p->bus_hdr_type = pci_cfgacc_get8(rcdip, bdf, PCI_CONF_HEADER);
1917 bus_p->bus_hdr_type &= PCI_HEADER_TYPE_M;
1918
1919 /*
1920 * Figure out the device type and all the relavant capability offsets
1921 */
1922 /* set default value */
1923 bus_p->bus_dev_type = PCIE_PCIECAP_DEV_TYPE_PCI_PSEUDO;
1924
1925 status = pci_cfgacc_get16(rcdip, bdf, PCI_CONF_STAT);
1926 if (status == PCI_CAP_EINVAL16 || !(status & PCI_STAT_CAP))
1927 goto caps_done; /* capability not supported */
1928
1929 /* Relevant conventional capabilities first */
1930
1931 /* Conventional caps: PCI_CAP_ID_PCI_E, PCI_CAP_ID_PCIX */
1932 num_cap = 2;
1933
1934 switch (bus_p->bus_hdr_type) {
1935 case PCI_HEADER_ZERO:
1936 baseptr = PCI_CONF_CAP_PTR;
1937 break;
1938 case PCI_HEADER_PPB:
1939 baseptr = PCI_BCNF_CAP_PTR;
1940 break;
1941 case PCI_HEADER_CARDBUS:
1942 baseptr = PCI_CBUS_CAP_PTR;
1943 break;
1944 default:
1945 cmn_err(CE_WARN, "%s: unexpected pci header type:%x",
1946 __func__, bus_p->bus_hdr_type);
1947 goto caps_done;
1948 }
1949
1950 base = baseptr;
1951 for (base = pci_cfgacc_get8(rcdip, bdf, base); base && num_cap;
1952 base = pci_cfgacc_get8(rcdip, bdf, base + PCI_CAP_NEXT_PTR)) {
1953 capid = pci_cfgacc_get8(rcdip, bdf, base);
1954 uint16_t pcap;
1955
1956 switch (capid) {
1957 case PCI_CAP_ID_PCI_E:
1958 bus_p->bus_pcie_off = base;
1959 pcap = pci_cfgacc_get16(rcdip, bdf, base +
1960 PCIE_PCIECAP);
1961 bus_p->bus_dev_type = pcap & PCIE_PCIECAP_DEV_TYPE_MASK;
1962 bus_p->bus_pcie_vers = pcap & PCIE_PCIECAP_VER_MASK;
1963
1964 /* Check and save PCIe hotplug capability information */
1965 if ((PCIE_IS_RP(bus_p) || PCIE_IS_SWD(bus_p)) &&
1966 (pci_cfgacc_get16(rcdip, bdf, base + PCIE_PCIECAP)
1967 & PCIE_PCIECAP_SLOT_IMPL) &&
1968 (pci_cfgacc_get32(rcdip, bdf, base + PCIE_SLOTCAP)
1969 & PCIE_SLOTCAP_HP_CAPABLE))
1970 bus_p->bus_hp_sup_modes |= PCIE_NATIVE_HP_MODE;
1971
1972 num_cap--;
1973 break;
1974 case PCI_CAP_ID_PCIX:
1975 bus_p->bus_pcix_off = base;
1976 if (PCIE_IS_BDG(bus_p))
1977 bus_p->bus_ecc_ver =
1978 pci_cfgacc_get16(rcdip, bdf, base +
1979 PCI_PCIX_SEC_STATUS) & PCI_PCIX_VER_MASK;
1980 else
1981 bus_p->bus_ecc_ver =
1982 pci_cfgacc_get16(rcdip, bdf, base +
1983 PCI_PCIX_COMMAND) & PCI_PCIX_VER_MASK;
1984 num_cap--;
1985 break;
1986 default:
1987 break;
1988 }
1989 }
1990
1991 /* Check and save PCI hotplug (SHPC) capability information */
1992 if (PCIE_IS_BDG(bus_p)) {
1993 base = baseptr;
1994 for (base = pci_cfgacc_get8(rcdip, bdf, base);
1995 base; base = pci_cfgacc_get8(rcdip, bdf,
1996 base + PCI_CAP_NEXT_PTR)) {
1997 capid = pci_cfgacc_get8(rcdip, bdf, base);
1998 if (capid == PCI_CAP_ID_PCI_HOTPLUG) {
1999 bus_p->bus_pci_hp_off = base;
2000 bus_p->bus_hp_sup_modes |= PCIE_PCI_HP_MODE;
2001 break;
2002 }
2003 }
2004 }
2005
2006 /* Then, relevant extended capabilities */
2007
2008 if (!PCIE_IS_PCIE(bus_p))
2009 goto caps_done;
2010
2011 /* Extended caps: PCIE_EXT_CAP_ID_AER */
2012 for (base = PCIE_EXT_CAP; base; base = (capid >>
2013 PCIE_EXT_CAP_NEXT_PTR_SHIFT) & PCIE_EXT_CAP_NEXT_PTR_MASK) {
2014 capid = pci_cfgacc_get32(rcdip, bdf, base);
2015 if (capid == PCI_CAP_EINVAL32)
2016 break;
2017 switch ((capid >> PCIE_EXT_CAP_ID_SHIFT) &
2018 PCIE_EXT_CAP_ID_MASK) {
2019 case PCIE_EXT_CAP_ID_AER:
2020 bus_p->bus_aer_off = base;
2021 break;
2022 case PCIE_EXT_CAP_ID_DEV3:
2023 bus_p->bus_dev3_off = base;
2024 break;
2025 }
2026 }
2027
2028 caps_done:
2029 /* save RP dip and RP bdf */
2030 if (PCIE_IS_RP(bus_p)) {
2031 bus_p->bus_rp_dip = dip;
2032 bus_p->bus_rp_bdf = bus_p->bus_bdf;
2033
2034 bus_p->bus_fab = PCIE_ZALLOC(pcie_fabric_data_t);
2035 } else {
2036 for (pdip = ddi_get_parent(dip); pdip;
2037 pdip = ddi_get_parent(pdip)) {
2038 pcie_bus_t *parent_bus_p = PCIE_DIP2BUS(pdip);
2039
2040 /*
2041 * If RP dip and RP bdf in parent's bus_t have
2042 * been initialized, simply use these instead of
2043 * continuing up to the RC.
2044 */
2045 if (parent_bus_p->bus_rp_dip != NULL) {
2046 bus_p->bus_rp_dip = parent_bus_p->bus_rp_dip;
2047 bus_p->bus_rp_bdf = parent_bus_p->bus_rp_bdf;
2048 break;
2049 }
2050
2051 /*
2052 * When debugging be aware that some NVIDIA x86
2053 * architectures have 2 nodes for each RP, One at Bus
2054 * 0x0 and one at Bus 0x80. The requester is from Bus
2055 * 0x80
2056 */
2057 if (PCIE_IS_ROOT(parent_bus_p)) {
2058 bus_p->bus_rp_dip = pdip;
2059 bus_p->bus_rp_bdf = parent_bus_p->bus_bdf;
2060 break;
2061 }
2062 }
2063 }
2064
2065 bus_p->bus_soft_state = PCI_SOFT_STATE_CLOSED;
2066 (void) atomic_swap_uint(&bus_p->bus_fm_flags, 0);
2067
2068 ndi_set_bus_private(dip, B_TRUE, DEVI_PORT_TYPE_PCI, (void *)bus_p);
2069
2070 if (PCIE_IS_HOTPLUG_CAPABLE(dip))
2071 (void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip,
2072 "hotplug-capable");
2073
2074 initial_done:
2075 if (!(flags & PCIE_BUS_FINAL))
2076 goto final_done;
2077
2078 /* already initialized? */
2079 bus_p = PCIE_DIP2BUS(dip);
2080
2081 /* Save the Range information if device is a switch/bridge */
2082 if (PCIE_IS_BDG(bus_p)) {
2083 /* get "bus_range" property */
2084 range_size = sizeof (pci_bus_range_t);
2085 if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2086 "bus-range", (caddr_t)&bus_p->bus_bus_range, &range_size)
2087 != DDI_PROP_SUCCESS) {
2088 errstr = "Cannot find \"bus-range\" property";
2089 cmn_err(CE_WARN,
2090 "PCIE init err info failed BDF 0x%x:%s\n",
2091 bus_p->bus_bdf, errstr);
2092 }
2093
2094 /* get secondary bus number */
2095 rcdip = pcie_get_rc_dip(dip);
2096 ASSERT(rcdip != NULL);
2097
2098 bus_p->bus_bdg_secbus = pci_cfgacc_get8(rcdip,
2099 bus_p->bus_bdf, PCI_BCNF_SECBUS);
2100
2101 /* Get "ranges" property */
2102 if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2103 "ranges", (caddr_t)&bus_p->bus_addr_ranges,
2104 &bus_p->bus_addr_entries) != DDI_PROP_SUCCESS)
2105 bus_p->bus_addr_entries = 0;
2106 bus_p->bus_addr_entries /= sizeof (ppb_ranges_t);
2107 }
2108
2109 /* save "assigned-addresses" property array, ignore failues */
2110 if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2111 "assigned-addresses", (caddr_t)&bus_p->bus_assigned_addr,
2112 &bus_p->bus_assigned_entries) == DDI_PROP_SUCCESS)
2113 bus_p->bus_assigned_entries /= sizeof (pci_regspec_t);
2114 else
2115 bus_p->bus_assigned_entries = 0;
2116
2117 pcie_init_pfd(dip);
2118
2119 pcie_init_plat(dip);
2120
2121 pcie_capture_speeds(dip);
2122
2123 final_done:
2124
2125 PCIE_DBG("Add %s(dip 0x%p, bdf 0x%x, secbus 0x%x)\n",
2126 ddi_driver_name(dip), (void *)dip, bus_p->bus_bdf,
2127 bus_p->bus_bdg_secbus);
2128 #ifdef DEBUG
2129 if (bus_p != NULL) {
2130 pcie_print_bus(bus_p);
2131 }
2132 #endif
2133
2134 return (bus_p);
2135 }
2136
2137 /*
2138 * Invoked before destroying devinfo node, mostly during hotplug
2139 * operation to free pcie_bus_t data structure
2140 */
2141 /* ARGSUSED */
2142 void
pcie_fini_bus(dev_info_t * dip,uint8_t flags)2143 pcie_fini_bus(dev_info_t *dip, uint8_t flags)
2144 {
2145 pcie_bus_t *bus_p = PCIE_DIP2UPBUS(dip);
2146 ASSERT(bus_p);
2147
2148 if (flags & PCIE_BUS_INITIAL) {
2149 pcie_fini_plat(dip);
2150 pcie_fini_pfd(dip);
2151
2152 if (PCIE_IS_RP(bus_p)) {
2153 kmem_free(bus_p->bus_fab, sizeof (pcie_fabric_data_t));
2154 bus_p->bus_fab = NULL;
2155 }
2156
2157 kmem_free(bus_p->bus_assigned_addr,
2158 (sizeof (pci_regspec_t) * bus_p->bus_assigned_entries));
2159 kmem_free(bus_p->bus_addr_ranges,
2160 (sizeof (ppb_ranges_t) * bus_p->bus_addr_entries));
2161 /* zero out the fields that have been destroyed */
2162 bus_p->bus_assigned_addr = NULL;
2163 bus_p->bus_addr_ranges = NULL;
2164 bus_p->bus_assigned_entries = 0;
2165 bus_p->bus_addr_entries = 0;
2166 }
2167
2168 if (flags & PCIE_BUS_FINAL) {
2169 if (PCIE_IS_HOTPLUG_CAPABLE(dip)) {
2170 (void) ndi_prop_remove(DDI_DEV_T_NONE, dip,
2171 "hotplug-capable");
2172 }
2173
2174 ndi_set_bus_private(dip, B_TRUE, 0, NULL);
2175 kmem_free(bus_p, sizeof (pcie_bus_t));
2176 }
2177 }
2178
2179 int
pcie_postattach_child(dev_info_t * cdip)2180 pcie_postattach_child(dev_info_t *cdip)
2181 {
2182 pcie_bus_t *bus_p = PCIE_DIP2BUS(cdip);
2183
2184 if (!bus_p)
2185 return (DDI_FAILURE);
2186
2187 return (pcie_enable_ce(cdip));
2188 }
2189
2190 /*
2191 * PCI-Express child device de-initialization.
2192 * This function disables generic pci-express interrupts and error
2193 * handling.
2194 */
2195 void
pcie_uninitchild(dev_info_t * cdip)2196 pcie_uninitchild(dev_info_t *cdip)
2197 {
2198 pcie_disable_errors(cdip);
2199 pcie_fini_cfghdl(cdip);
2200 pcie_fini_dom(cdip);
2201 }
2202
2203 /*
2204 * find the root complex dip
2205 */
2206 dev_info_t *
pcie_get_rc_dip(dev_info_t * dip)2207 pcie_get_rc_dip(dev_info_t *dip)
2208 {
2209 dev_info_t *rcdip;
2210 pcie_bus_t *rc_bus_p;
2211
2212 for (rcdip = ddi_get_parent(dip); rcdip;
2213 rcdip = ddi_get_parent(rcdip)) {
2214 rc_bus_p = PCIE_DIP2BUS(rcdip);
2215 if (rc_bus_p && PCIE_IS_RC(rc_bus_p))
2216 break;
2217 }
2218
2219 return (rcdip);
2220 }
2221
2222 boolean_t
pcie_is_pci_device(dev_info_t * dip)2223 pcie_is_pci_device(dev_info_t *dip)
2224 {
2225 dev_info_t *pdip;
2226 char *device_type;
2227
2228 pdip = ddi_get_parent(dip);
2229 if (pdip == NULL)
2230 return (B_FALSE);
2231
2232 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
2233 "device_type", &device_type) != DDI_PROP_SUCCESS)
2234 return (B_FALSE);
2235
2236 if (strcmp(device_type, "pciex") != 0 &&
2237 strcmp(device_type, "pci") != 0) {
2238 ddi_prop_free(device_type);
2239 return (B_FALSE);
2240 }
2241
2242 ddi_prop_free(device_type);
2243 return (B_TRUE);
2244 }
2245
2246 typedef struct {
2247 boolean_t init;
2248 uint8_t flags;
2249 } pcie_bus_arg_t;
2250
2251 /*ARGSUSED*/
2252 static int
pcie_fab_do_init_fini(dev_info_t * dip,void * arg)2253 pcie_fab_do_init_fini(dev_info_t *dip, void *arg)
2254 {
2255 pcie_req_id_t bdf;
2256 pcie_bus_arg_t *bus_arg = (pcie_bus_arg_t *)arg;
2257
2258 if (!pcie_is_pci_device(dip))
2259 goto out;
2260
2261 if (bus_arg->init) {
2262 if (pcie_get_bdf_from_dip(dip, &bdf) != DDI_SUCCESS)
2263 goto out;
2264
2265 (void) pcie_init_bus(dip, bdf, bus_arg->flags);
2266 } else {
2267 (void) pcie_fini_bus(dip, bus_arg->flags);
2268 }
2269
2270 return (DDI_WALK_CONTINUE);
2271
2272 out:
2273 return (DDI_WALK_PRUNECHILD);
2274 }
2275
2276 void
pcie_fab_init_bus(dev_info_t * rcdip,uint8_t flags)2277 pcie_fab_init_bus(dev_info_t *rcdip, uint8_t flags)
2278 {
2279 dev_info_t *dip = ddi_get_child(rcdip);
2280 pcie_bus_arg_t arg;
2281
2282 arg.init = B_TRUE;
2283 arg.flags = flags;
2284
2285 ndi_devi_enter(rcdip);
2286 ddi_walk_devs(dip, pcie_fab_do_init_fini, &arg);
2287 ndi_devi_exit(rcdip);
2288 }
2289
2290 void
pcie_fab_fini_bus(dev_info_t * rcdip,uint8_t flags)2291 pcie_fab_fini_bus(dev_info_t *rcdip, uint8_t flags)
2292 {
2293 dev_info_t *dip = ddi_get_child(rcdip);
2294 pcie_bus_arg_t arg;
2295
2296 arg.init = B_FALSE;
2297 arg.flags = flags;
2298
2299 ndi_devi_enter(rcdip);
2300 ddi_walk_devs(dip, pcie_fab_do_init_fini, &arg);
2301 ndi_devi_exit(rcdip);
2302 }
2303
2304 void
pcie_enable_errors(dev_info_t * dip)2305 pcie_enable_errors(dev_info_t *dip)
2306 {
2307 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
2308 uint16_t reg16, tmp16;
2309 uint32_t reg32, tmp32;
2310
2311 ASSERT(bus_p);
2312
2313 /*
2314 * Clear any pending errors
2315 */
2316 pcie_clear_errors(dip);
2317
2318 if (!PCIE_IS_PCIE(bus_p))
2319 return;
2320
2321 /*
2322 * Enable Baseline Error Handling but leave CE reporting off (poweron
2323 * default).
2324 */
2325 if ((reg16 = PCIE_CAP_GET(16, bus_p, PCIE_DEVCTL)) !=
2326 PCI_CAP_EINVAL16) {
2327 tmp16 = (reg16 & pcie_devctl_default_mask) |
2328 (pcie_devctl_default & ~pcie_devctl_default_mask) |
2329 (pcie_base_err_default & ~PCIE_DEVCTL_CE_REPORTING_EN);
2330
2331 PCIE_CAP_PUT(16, bus_p, PCIE_DEVCTL, tmp16);
2332 PCIE_DBG_CAP(dip, bus_p, "DEVCTL", 16, PCIE_DEVCTL, reg16);
2333 }
2334
2335 /* Enable Root Port Baseline Error Receiving */
2336 if (PCIE_IS_ROOT(bus_p) &&
2337 (reg16 = PCIE_CAP_GET(16, bus_p, PCIE_ROOTCTL)) !=
2338 PCI_CAP_EINVAL16) {
2339
2340 tmp16 = pcie_serr_disable_flag ?
2341 (pcie_root_ctrl_default & ~PCIE_ROOT_SYS_ERR) :
2342 pcie_root_ctrl_default;
2343 PCIE_CAP_PUT(16, bus_p, PCIE_ROOTCTL, tmp16);
2344 PCIE_DBG_CAP(dip, bus_p, "ROOT DEVCTL", 16, PCIE_ROOTCTL,
2345 reg16);
2346 }
2347
2348 /*
2349 * Enable PCI-Express Advanced Error Handling if Exists
2350 */
2351 if (!PCIE_HAS_AER(bus_p))
2352 return;
2353
2354 /* Set Uncorrectable Severity */
2355 if ((reg32 = PCIE_AER_GET(32, bus_p, PCIE_AER_UCE_SERV)) !=
2356 PCI_CAP_EINVAL32) {
2357 tmp32 = pcie_aer_uce_severity;
2358
2359 PCIE_AER_PUT(32, bus_p, PCIE_AER_UCE_SERV, tmp32);
2360 PCIE_DBG_AER(dip, bus_p, "AER UCE SEV", 32, PCIE_AER_UCE_SERV,
2361 reg32);
2362 }
2363
2364 /* Enable Uncorrectable errors */
2365 if ((reg32 = PCIE_AER_GET(32, bus_p, PCIE_AER_UCE_MASK)) !=
2366 PCI_CAP_EINVAL32) {
2367 tmp32 = pcie_aer_uce_mask;
2368
2369 PCIE_AER_PUT(32, bus_p, PCIE_AER_UCE_MASK, tmp32);
2370 PCIE_DBG_AER(dip, bus_p, "AER UCE MASK", 32, PCIE_AER_UCE_MASK,
2371 reg32);
2372 }
2373
2374 /* Enable ECRC generation and checking */
2375 if ((reg32 = PCIE_AER_GET(32, bus_p, PCIE_AER_CTL)) !=
2376 PCI_CAP_EINVAL32) {
2377 tmp32 = reg32 | pcie_ecrc_value;
2378 PCIE_AER_PUT(32, bus_p, PCIE_AER_CTL, tmp32);
2379 PCIE_DBG_AER(dip, bus_p, "AER CTL", 32, PCIE_AER_CTL, reg32);
2380 }
2381
2382 /* Enable Secondary Uncorrectable errors if this is a bridge */
2383 if (!PCIE_IS_PCIE_BDG(bus_p))
2384 goto root;
2385
2386 /* Set Uncorrectable Severity */
2387 if ((reg32 = PCIE_AER_GET(32, bus_p, PCIE_AER_SUCE_SERV)) !=
2388 PCI_CAP_EINVAL32) {
2389 tmp32 = pcie_aer_suce_severity;
2390
2391 PCIE_AER_PUT(32, bus_p, PCIE_AER_SUCE_SERV, tmp32);
2392 PCIE_DBG_AER(dip, bus_p, "AER SUCE SEV", 32, PCIE_AER_SUCE_SERV,
2393 reg32);
2394 }
2395
2396 if ((reg32 = PCIE_AER_GET(32, bus_p, PCIE_AER_SUCE_MASK)) !=
2397 PCI_CAP_EINVAL32) {
2398 PCIE_AER_PUT(32, bus_p, PCIE_AER_SUCE_MASK, pcie_aer_suce_mask);
2399 PCIE_DBG_AER(dip, bus_p, "AER SUCE MASK", 32,
2400 PCIE_AER_SUCE_MASK, reg32);
2401 }
2402
2403 root:
2404 /*
2405 * Enable Root Control this is a Root device
2406 */
2407 if (!PCIE_IS_ROOT(bus_p))
2408 return;
2409
2410 if ((reg16 = PCIE_AER_GET(16, bus_p, PCIE_AER_RE_CMD)) !=
2411 PCI_CAP_EINVAL16) {
2412 PCIE_AER_PUT(16, bus_p, PCIE_AER_RE_CMD,
2413 pcie_root_error_cmd_default);
2414 PCIE_DBG_AER(dip, bus_p, "AER Root Err Cmd", 16,
2415 PCIE_AER_RE_CMD, reg16);
2416 }
2417 }
2418
2419 /*
2420 * This function is used for enabling CE reporting and setting the AER CE mask.
2421 * When called from outside the pcie module it should always be preceded by
2422 * a call to pcie_enable_errors.
2423 */
2424 int
pcie_enable_ce(dev_info_t * dip)2425 pcie_enable_ce(dev_info_t *dip)
2426 {
2427 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
2428 uint16_t device_sts, device_ctl;
2429 uint32_t tmp_pcie_aer_ce_mask;
2430
2431 if (!PCIE_IS_PCIE(bus_p))
2432 return (DDI_SUCCESS);
2433
2434 /*
2435 * The "pcie_ce_mask" property is used to control both the CE reporting
2436 * enable field in the device control register and the AER CE mask. We
2437 * leave CE reporting disabled if pcie_ce_mask is set to -1.
2438 */
2439
2440 tmp_pcie_aer_ce_mask = (uint32_t)ddi_prop_get_int(DDI_DEV_T_ANY, dip,
2441 DDI_PROP_DONTPASS, "pcie_ce_mask", pcie_aer_ce_mask);
2442
2443 if (tmp_pcie_aer_ce_mask == (uint32_t)-1) {
2444 /*
2445 * Nothing to do since CE reporting has already been disabled.
2446 */
2447 return (DDI_SUCCESS);
2448 }
2449
2450 if (PCIE_HAS_AER(bus_p)) {
2451 /* Enable AER CE */
2452 PCIE_AER_PUT(32, bus_p, PCIE_AER_CE_MASK, tmp_pcie_aer_ce_mask);
2453 PCIE_DBG_AER(dip, bus_p, "AER CE MASK", 32, PCIE_AER_CE_MASK,
2454 0);
2455
2456 /* Clear any pending AER CE errors */
2457 PCIE_AER_PUT(32, bus_p, PCIE_AER_CE_STS, -1);
2458 }
2459
2460 /* clear any pending CE errors */
2461 if ((device_sts = PCIE_CAP_GET(16, bus_p, PCIE_DEVSTS)) !=
2462 PCI_CAP_EINVAL16)
2463 PCIE_CAP_PUT(16, bus_p, PCIE_DEVSTS,
2464 device_sts & (~PCIE_DEVSTS_CE_DETECTED));
2465
2466 /* Enable CE reporting */
2467 device_ctl = PCIE_CAP_GET(16, bus_p, PCIE_DEVCTL);
2468 PCIE_CAP_PUT(16, bus_p, PCIE_DEVCTL,
2469 (device_ctl & (~PCIE_DEVCTL_ERR_MASK)) | pcie_base_err_default);
2470 PCIE_DBG_CAP(dip, bus_p, "DEVCTL", 16, PCIE_DEVCTL, device_ctl);
2471
2472 return (DDI_SUCCESS);
2473 }
2474
2475 /* ARGSUSED */
2476 void
pcie_disable_errors(dev_info_t * dip)2477 pcie_disable_errors(dev_info_t *dip)
2478 {
2479 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
2480 uint16_t device_ctl;
2481 uint32_t aer_reg;
2482
2483 if (!PCIE_IS_PCIE(bus_p))
2484 return;
2485
2486 /*
2487 * Disable PCI-Express Baseline Error Handling
2488 */
2489 device_ctl = PCIE_CAP_GET(16, bus_p, PCIE_DEVCTL);
2490 device_ctl &= ~PCIE_DEVCTL_ERR_MASK;
2491 PCIE_CAP_PUT(16, bus_p, PCIE_DEVCTL, device_ctl);
2492
2493 /*
2494 * Disable PCI-Express Advanced Error Handling if Exists
2495 */
2496 if (!PCIE_HAS_AER(bus_p))
2497 goto root;
2498
2499 /* Disable Uncorrectable errors */
2500 PCIE_AER_PUT(32, bus_p, PCIE_AER_UCE_MASK, PCIE_AER_UCE_BITS);
2501
2502 /* Disable Correctable errors */
2503 PCIE_AER_PUT(32, bus_p, PCIE_AER_CE_MASK, PCIE_AER_CE_BITS);
2504
2505 /* Disable ECRC generation and checking */
2506 if ((aer_reg = PCIE_AER_GET(32, bus_p, PCIE_AER_CTL)) !=
2507 PCI_CAP_EINVAL32) {
2508 aer_reg &= ~(PCIE_AER_CTL_ECRC_GEN_ENA |
2509 PCIE_AER_CTL_ECRC_CHECK_ENA);
2510
2511 PCIE_AER_PUT(32, bus_p, PCIE_AER_CTL, aer_reg);
2512 }
2513 /*
2514 * Disable Secondary Uncorrectable errors if this is a bridge
2515 */
2516 if (!PCIE_IS_PCIE_BDG(bus_p))
2517 goto root;
2518
2519 PCIE_AER_PUT(32, bus_p, PCIE_AER_SUCE_MASK, PCIE_AER_SUCE_BITS);
2520
2521 root:
2522 /*
2523 * disable Root Control this is a Root device
2524 */
2525 if (!PCIE_IS_ROOT(bus_p))
2526 return;
2527
2528 if (!pcie_serr_disable_flag) {
2529 device_ctl = PCIE_CAP_GET(16, bus_p, PCIE_ROOTCTL);
2530 device_ctl &= ~PCIE_ROOT_SYS_ERR;
2531 PCIE_CAP_PUT(16, bus_p, PCIE_ROOTCTL, device_ctl);
2532 }
2533
2534 if (!PCIE_HAS_AER(bus_p))
2535 return;
2536
2537 if ((device_ctl = PCIE_CAP_GET(16, bus_p, PCIE_AER_RE_CMD)) !=
2538 PCI_CAP_EINVAL16) {
2539 device_ctl &= ~pcie_root_error_cmd_default;
2540 PCIE_CAP_PUT(16, bus_p, PCIE_AER_RE_CMD, device_ctl);
2541 }
2542 }
2543
2544 /*
2545 * Extract bdf from "reg" property.
2546 */
2547 int
pcie_get_bdf_from_dip(dev_info_t * dip,pcie_req_id_t * bdf)2548 pcie_get_bdf_from_dip(dev_info_t *dip, pcie_req_id_t *bdf)
2549 {
2550 pci_regspec_t *regspec;
2551 int reglen;
2552
2553 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2554 "reg", (int **)®spec, (uint_t *)®len) != DDI_SUCCESS)
2555 return (DDI_FAILURE);
2556
2557 if (reglen < (sizeof (pci_regspec_t) / sizeof (int))) {
2558 ddi_prop_free(regspec);
2559 return (DDI_FAILURE);
2560 }
2561
2562 /* Get phys_hi from first element. All have same bdf. */
2563 *bdf = (regspec->pci_phys_hi & (PCI_REG_BDFR_M ^ PCI_REG_REG_M)) >> 8;
2564
2565 ddi_prop_free(regspec);
2566 return (DDI_SUCCESS);
2567 }
2568
2569 dev_info_t *
pcie_get_my_childs_dip(dev_info_t * dip,dev_info_t * rdip)2570 pcie_get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
2571 {
2572 dev_info_t *cdip = rdip;
2573
2574 for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
2575 ;
2576
2577 return (cdip);
2578 }
2579
2580 uint32_t
pcie_get_bdf_for_dma_xfer(dev_info_t * dip,dev_info_t * rdip)2581 pcie_get_bdf_for_dma_xfer(dev_info_t *dip, dev_info_t *rdip)
2582 {
2583 dev_info_t *cdip;
2584
2585 /*
2586 * As part of the probing, the PCI fcode interpreter may setup a DMA
2587 * request if a given card has a fcode on it using dip and rdip of the
2588 * hotplug connector i.e, dip and rdip of px/pcieb driver. In this
2589 * case, return a invalid value for the bdf since we cannot get to the
2590 * bdf value of the actual device which will be initiating this DMA.
2591 */
2592 if (rdip == dip)
2593 return (PCIE_INVALID_BDF);
2594
2595 cdip = pcie_get_my_childs_dip(dip, rdip);
2596
2597 /*
2598 * For a given rdip, return the bdf value of dip's (px or pcieb)
2599 * immediate child or secondary bus-id if dip is a PCIe2PCI bridge.
2600 *
2601 * XXX - For now, return a invalid bdf value for all PCI and PCI-X
2602 * devices since this needs more work.
2603 */
2604 return (PCI_GET_PCIE2PCI_SECBUS(cdip) ?
2605 PCIE_INVALID_BDF : PCI_GET_BDF(cdip));
2606 }
2607
2608 uint32_t
pcie_get_aer_uce_mask()2609 pcie_get_aer_uce_mask()
2610 {
2611 return (pcie_aer_uce_mask);
2612 }
2613 uint32_t
pcie_get_aer_ce_mask()2614 pcie_get_aer_ce_mask()
2615 {
2616 return (pcie_aer_ce_mask);
2617 }
2618 uint32_t
pcie_get_aer_suce_mask()2619 pcie_get_aer_suce_mask()
2620 {
2621 return (pcie_aer_suce_mask);
2622 }
2623 uint32_t
pcie_get_serr_mask()2624 pcie_get_serr_mask()
2625 {
2626 return (pcie_serr_disable_flag);
2627 }
2628
2629 void
pcie_set_aer_uce_mask(uint32_t mask)2630 pcie_set_aer_uce_mask(uint32_t mask)
2631 {
2632 pcie_aer_uce_mask = mask;
2633 if (mask & PCIE_AER_UCE_UR)
2634 pcie_base_err_default &= ~PCIE_DEVCTL_UR_REPORTING_EN;
2635 else
2636 pcie_base_err_default |= PCIE_DEVCTL_UR_REPORTING_EN;
2637
2638 if (mask & PCIE_AER_UCE_ECRC)
2639 pcie_ecrc_value = 0;
2640 }
2641
2642 void
pcie_set_aer_ce_mask(uint32_t mask)2643 pcie_set_aer_ce_mask(uint32_t mask)
2644 {
2645 pcie_aer_ce_mask = mask;
2646 }
2647 void
pcie_set_aer_suce_mask(uint32_t mask)2648 pcie_set_aer_suce_mask(uint32_t mask)
2649 {
2650 pcie_aer_suce_mask = mask;
2651 }
2652 void
pcie_set_serr_mask(uint32_t mask)2653 pcie_set_serr_mask(uint32_t mask)
2654 {
2655 pcie_serr_disable_flag = mask;
2656 }
2657
2658 /*
2659 * Is the rdip a child of dip. Used for checking certain CTLOPS from bubbling
2660 * up erronously. Ex. ISA ctlops to a PCI-PCI Bridge.
2661 */
2662 boolean_t
pcie_is_child(dev_info_t * dip,dev_info_t * rdip)2663 pcie_is_child(dev_info_t *dip, dev_info_t *rdip)
2664 {
2665 dev_info_t *cdip = ddi_get_child(dip);
2666 for (; cdip; cdip = ddi_get_next_sibling(cdip))
2667 if (cdip == rdip)
2668 break;
2669 return (cdip != NULL);
2670 }
2671
2672 boolean_t
pcie_is_link_disabled(dev_info_t * dip)2673 pcie_is_link_disabled(dev_info_t *dip)
2674 {
2675 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
2676
2677 if (PCIE_IS_PCIE(bus_p)) {
2678 if (PCIE_CAP_GET(16, bus_p, PCIE_LINKCTL) &
2679 PCIE_LINKCTL_LINK_DISABLE)
2680 return (B_TRUE);
2681 }
2682 return (B_FALSE);
2683 }
2684
2685 /*
2686 * Determines if there are any root ports attached to a root complex.
2687 *
2688 * dip - dip of root complex
2689 *
2690 * Returns - DDI_SUCCESS if there is at least one root port otherwise
2691 * DDI_FAILURE.
2692 */
2693 int
pcie_root_port(dev_info_t * dip)2694 pcie_root_port(dev_info_t *dip)
2695 {
2696 int port_type;
2697 uint16_t cap_ptr;
2698 ddi_acc_handle_t config_handle;
2699 dev_info_t *cdip = ddi_get_child(dip);
2700
2701 /*
2702 * Determine if any of the children of the passed in dip
2703 * are root ports.
2704 */
2705 for (; cdip; cdip = ddi_get_next_sibling(cdip)) {
2706
2707 if (pci_config_setup(cdip, &config_handle) != DDI_SUCCESS)
2708 continue;
2709
2710 if ((PCI_CAP_LOCATE(config_handle, PCI_CAP_ID_PCI_E,
2711 &cap_ptr)) == DDI_FAILURE) {
2712 pci_config_teardown(&config_handle);
2713 continue;
2714 }
2715
2716 port_type = PCI_CAP_GET16(config_handle, 0, cap_ptr,
2717 PCIE_PCIECAP) & PCIE_PCIECAP_DEV_TYPE_MASK;
2718
2719 pci_config_teardown(&config_handle);
2720
2721 if (port_type == PCIE_PCIECAP_DEV_TYPE_ROOT)
2722 return (DDI_SUCCESS);
2723 }
2724
2725 /* No root ports were found */
2726
2727 return (DDI_FAILURE);
2728 }
2729
2730 /*
2731 * Function that determines if a device a PCIe device.
2732 *
2733 * dip - dip of device.
2734 *
2735 * returns - DDI_SUCCESS if device is a PCIe device, otherwise DDI_FAILURE.
2736 */
2737 int
pcie_dev(dev_info_t * dip)2738 pcie_dev(dev_info_t *dip)
2739 {
2740 /* get parent device's device_type property */
2741 char *device_type;
2742 int rc = DDI_FAILURE;
2743 dev_info_t *pdip = ddi_get_parent(dip);
2744
2745 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip,
2746 DDI_PROP_DONTPASS, "device_type", &device_type)
2747 != DDI_PROP_SUCCESS) {
2748 return (DDI_FAILURE);
2749 }
2750
2751 if (strcmp(device_type, "pciex") == 0)
2752 rc = DDI_SUCCESS;
2753 else
2754 rc = DDI_FAILURE;
2755
2756 ddi_prop_free(device_type);
2757 return (rc);
2758 }
2759
2760 void
pcie_set_rber_fatal(dev_info_t * dip,boolean_t val)2761 pcie_set_rber_fatal(dev_info_t *dip, boolean_t val)
2762 {
2763 pcie_bus_t *bus_p = PCIE_DIP2UPBUS(dip);
2764 bus_p->bus_pfd->pe_rber_fatal = val;
2765 }
2766
2767 /*
2768 * Return parent Root Port's pe_rber_fatal value.
2769 */
2770 boolean_t
pcie_get_rber_fatal(dev_info_t * dip)2771 pcie_get_rber_fatal(dev_info_t *dip)
2772 {
2773 pcie_bus_t *bus_p = PCIE_DIP2UPBUS(dip);
2774 pcie_bus_t *rp_bus_p = PCIE_DIP2UPBUS(bus_p->bus_rp_dip);
2775 return (rp_bus_p->bus_pfd->pe_rber_fatal);
2776 }
2777
2778 int
pcie_ari_supported(dev_info_t * dip)2779 pcie_ari_supported(dev_info_t *dip)
2780 {
2781 uint32_t devcap2;
2782 uint16_t pciecap;
2783 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
2784 uint8_t dev_type;
2785
2786 PCIE_DBG("pcie_ari_supported: dip=%p\n", dip);
2787
2788 if (bus_p == NULL)
2789 return (PCIE_ARI_FORW_NOT_SUPPORTED);
2790
2791 dev_type = bus_p->bus_dev_type;
2792
2793 if ((dev_type != PCIE_PCIECAP_DEV_TYPE_DOWN) &&
2794 (dev_type != PCIE_PCIECAP_DEV_TYPE_ROOT))
2795 return (PCIE_ARI_FORW_NOT_SUPPORTED);
2796
2797 if (pcie_disable_ari) {
2798 PCIE_DBG("pcie_ari_supported: dip=%p: ARI Disabled\n", dip);
2799 return (PCIE_ARI_FORW_NOT_SUPPORTED);
2800 }
2801
2802 pciecap = PCIE_CAP_GET(16, bus_p, PCIE_PCIECAP);
2803
2804 if ((pciecap & PCIE_PCIECAP_VER_MASK) < PCIE_PCIECAP_VER_2_0) {
2805 PCIE_DBG("pcie_ari_supported: dip=%p: Not 2.0\n", dip);
2806 return (PCIE_ARI_FORW_NOT_SUPPORTED);
2807 }
2808
2809 devcap2 = PCIE_CAP_GET(32, bus_p, PCIE_DEVCAP2);
2810
2811 PCIE_DBG("pcie_ari_supported: dip=%p: DevCap2=0x%x\n",
2812 dip, devcap2);
2813
2814 if (devcap2 & PCIE_DEVCAP2_ARI_FORWARD) {
2815 PCIE_DBG("pcie_ari_supported: "
2816 "dip=%p: ARI Forwarding is supported\n", dip);
2817 return (PCIE_ARI_FORW_SUPPORTED);
2818 }
2819 return (PCIE_ARI_FORW_NOT_SUPPORTED);
2820 }
2821
2822 int
pcie_ari_enable(dev_info_t * dip)2823 pcie_ari_enable(dev_info_t *dip)
2824 {
2825 uint16_t devctl2;
2826 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
2827
2828 PCIE_DBG("pcie_ari_enable: dip=%p\n", dip);
2829
2830 if (pcie_ari_supported(dip) == PCIE_ARI_FORW_NOT_SUPPORTED)
2831 return (DDI_FAILURE);
2832
2833 devctl2 = PCIE_CAP_GET(16, bus_p, PCIE_DEVCTL2);
2834 devctl2 |= PCIE_DEVCTL2_ARI_FORWARD_EN;
2835 PCIE_CAP_PUT(16, bus_p, PCIE_DEVCTL2, devctl2);
2836
2837 PCIE_DBG("pcie_ari_enable: dip=%p: writing 0x%x to DevCtl2\n",
2838 dip, devctl2);
2839
2840 return (DDI_SUCCESS);
2841 }
2842
2843 int
pcie_ari_disable(dev_info_t * dip)2844 pcie_ari_disable(dev_info_t *dip)
2845 {
2846 uint16_t devctl2;
2847 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
2848
2849 PCIE_DBG("pcie_ari_disable: dip=%p\n", dip);
2850
2851 if (pcie_ari_supported(dip) == PCIE_ARI_FORW_NOT_SUPPORTED)
2852 return (DDI_FAILURE);
2853
2854 devctl2 = PCIE_CAP_GET(16, bus_p, PCIE_DEVCTL2);
2855 devctl2 &= ~PCIE_DEVCTL2_ARI_FORWARD_EN;
2856 PCIE_CAP_PUT(16, bus_p, PCIE_DEVCTL2, devctl2);
2857
2858 PCIE_DBG("pcie_ari_disable: dip=%p: writing 0x%x to DevCtl2\n",
2859 dip, devctl2);
2860
2861 return (DDI_SUCCESS);
2862 }
2863
2864 int
pcie_ari_is_enabled(dev_info_t * dip)2865 pcie_ari_is_enabled(dev_info_t *dip)
2866 {
2867 uint16_t devctl2;
2868 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
2869
2870 PCIE_DBG("pcie_ari_is_enabled: dip=%p\n", dip);
2871
2872 if (pcie_ari_supported(dip) == PCIE_ARI_FORW_NOT_SUPPORTED)
2873 return (PCIE_ARI_FORW_DISABLED);
2874
2875 devctl2 = PCIE_CAP_GET(32, bus_p, PCIE_DEVCTL2);
2876
2877 PCIE_DBG("pcie_ari_is_enabled: dip=%p: DevCtl2=0x%x\n",
2878 dip, devctl2);
2879
2880 if (devctl2 & PCIE_DEVCTL2_ARI_FORWARD_EN) {
2881 PCIE_DBG("pcie_ari_is_enabled: "
2882 "dip=%p: ARI Forwarding is enabled\n", dip);
2883 return (PCIE_ARI_FORW_ENABLED);
2884 }
2885
2886 return (PCIE_ARI_FORW_DISABLED);
2887 }
2888
2889 int
pcie_ari_device(dev_info_t * dip)2890 pcie_ari_device(dev_info_t *dip)
2891 {
2892 ddi_acc_handle_t handle;
2893 uint16_t cap_ptr;
2894
2895 PCIE_DBG("pcie_ari_device: dip=%p\n", dip);
2896
2897 /*
2898 * XXX - This function may be called before the bus_p structure
2899 * has been populated. This code can be changed to remove
2900 * pci_config_setup()/pci_config_teardown() when the RFE
2901 * to populate the bus_p structures early in boot is putback.
2902 */
2903
2904 /* First make sure it is a PCIe device */
2905
2906 if (pci_config_setup(dip, &handle) != DDI_SUCCESS)
2907 return (PCIE_NOT_ARI_DEVICE);
2908
2909 if ((PCI_CAP_LOCATE(handle, PCI_CAP_ID_PCI_E, &cap_ptr))
2910 != DDI_SUCCESS) {
2911 pci_config_teardown(&handle);
2912 return (PCIE_NOT_ARI_DEVICE);
2913 }
2914
2915 /* Locate the ARI Capability */
2916
2917 if ((PCI_CAP_LOCATE(handle, PCI_CAP_XCFG_SPC(PCIE_EXT_CAP_ID_ARI),
2918 &cap_ptr)) == DDI_FAILURE) {
2919 pci_config_teardown(&handle);
2920 return (PCIE_NOT_ARI_DEVICE);
2921 }
2922
2923 /* ARI Capability was found so it must be a ARI device */
2924 PCIE_DBG("pcie_ari_device: ARI Device dip=%p\n", dip);
2925
2926 pci_config_teardown(&handle);
2927 return (PCIE_ARI_DEVICE);
2928 }
2929
2930 int
pcie_ari_get_next_function(dev_info_t * dip,int * func)2931 pcie_ari_get_next_function(dev_info_t *dip, int *func)
2932 {
2933 uint32_t val;
2934 uint16_t cap_ptr, next_function;
2935 ddi_acc_handle_t handle;
2936
2937 /*
2938 * XXX - This function may be called before the bus_p structure
2939 * has been populated. This code can be changed to remove
2940 * pci_config_setup()/pci_config_teardown() when the RFE
2941 * to populate the bus_p structures early in boot is putback.
2942 */
2943
2944 if (pci_config_setup(dip, &handle) != DDI_SUCCESS)
2945 return (DDI_FAILURE);
2946
2947 if ((PCI_CAP_LOCATE(handle,
2948 PCI_CAP_XCFG_SPC(PCIE_EXT_CAP_ID_ARI), &cap_ptr)) == DDI_FAILURE) {
2949 pci_config_teardown(&handle);
2950 return (DDI_FAILURE);
2951 }
2952
2953 val = PCI_CAP_GET32(handle, 0, cap_ptr, PCIE_ARI_CAP);
2954
2955 next_function = (val >> PCIE_ARI_CAP_NEXT_FUNC_SHIFT) &
2956 PCIE_ARI_CAP_NEXT_FUNC_MASK;
2957
2958 pci_config_teardown(&handle);
2959
2960 *func = next_function;
2961
2962 return (DDI_SUCCESS);
2963 }
2964
2965 dev_info_t *
pcie_func_to_dip(dev_info_t * dip,pcie_req_id_t function)2966 pcie_func_to_dip(dev_info_t *dip, pcie_req_id_t function)
2967 {
2968 pcie_req_id_t child_bdf;
2969 dev_info_t *cdip;
2970
2971 for (cdip = ddi_get_child(dip); cdip;
2972 cdip = ddi_get_next_sibling(cdip)) {
2973
2974 if (pcie_get_bdf_from_dip(cdip, &child_bdf) == DDI_FAILURE)
2975 return (NULL);
2976
2977 if ((child_bdf & PCIE_REQ_ID_ARI_FUNC_MASK) == function)
2978 return (cdip);
2979 }
2980 return (NULL);
2981 }
2982
2983 #ifdef DEBUG
2984
2985 static void
pcie_print_bus(pcie_bus_t * bus_p)2986 pcie_print_bus(pcie_bus_t *bus_p)
2987 {
2988 pcie_dbg("\tbus_dip = 0x%p\n", bus_p->bus_dip);
2989 pcie_dbg("\tbus_fm_flags = 0x%x\n", bus_p->bus_fm_flags);
2990
2991 pcie_dbg("\tbus_bdf = 0x%x\n", bus_p->bus_bdf);
2992 pcie_dbg("\tbus_dev_ven_id = 0x%x\n", bus_p->bus_dev_ven_id);
2993 pcie_dbg("\tbus_rev_id = 0x%x\n", bus_p->bus_rev_id);
2994 pcie_dbg("\tbus_hdr_type = 0x%x\n", bus_p->bus_hdr_type);
2995 pcie_dbg("\tbus_dev_type = 0x%x\n", bus_p->bus_dev_type);
2996 pcie_dbg("\tbus_bdg_secbus = 0x%x\n", bus_p->bus_bdg_secbus);
2997 pcie_dbg("\tbus_pcie_off = 0x%x\n", bus_p->bus_pcie_off);
2998 pcie_dbg("\tbus_aer_off = 0x%x\n", bus_p->bus_aer_off);
2999 pcie_dbg("\tbus_pcix_off = 0x%x\n", bus_p->bus_pcix_off);
3000 pcie_dbg("\tbus_ecc_ver = 0x%x\n", bus_p->bus_ecc_ver);
3001 }
3002
3003 /*
3004 * For debugging purposes set pcie_dbg_print != 0 to see printf messages
3005 * during interrupt.
3006 *
3007 * When a proper solution is in place this code will disappear.
3008 * Potential solutions are:
3009 * o circular buffers
3010 * o taskq to print at lower pil
3011 */
3012 int pcie_dbg_print = 0;
3013 void
pcie_dbg(char * fmt,...)3014 pcie_dbg(char *fmt, ...)
3015 {
3016 va_list ap;
3017
3018 if (!pcie_debug_flags) {
3019 return;
3020 }
3021 va_start(ap, fmt);
3022 if (servicing_interrupt()) {
3023 if (pcie_dbg_print) {
3024 prom_vprintf(fmt, ap);
3025 }
3026 } else {
3027 prom_vprintf(fmt, ap);
3028 }
3029 va_end(ap);
3030 }
3031 #endif /* DEBUG */
3032
3033 boolean_t
pcie_link_bw_supported(dev_info_t * dip)3034 pcie_link_bw_supported(dev_info_t *dip)
3035 {
3036 uint32_t linkcap;
3037 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
3038
3039 if (!PCIE_IS_PCIE(bus_p)) {
3040 return (B_FALSE);
3041 }
3042
3043 if (!PCIE_IS_RP(bus_p) && !PCIE_IS_SWD(bus_p)) {
3044 return (B_FALSE);
3045 }
3046
3047 linkcap = PCIE_CAP_GET(32, bus_p, PCIE_LINKCAP);
3048 return ((linkcap & PCIE_LINKCAP_LINK_BW_NOTIFY_CAP) != 0);
3049 }
3050
3051 int
pcie_link_bw_enable(dev_info_t * dip)3052 pcie_link_bw_enable(dev_info_t *dip)
3053 {
3054 uint16_t linkctl;
3055 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
3056
3057 if (pcie_disable_lbw != 0) {
3058 return (DDI_FAILURE);
3059 }
3060
3061 if (!pcie_link_bw_supported(dip)) {
3062 return (DDI_FAILURE);
3063 }
3064
3065 mutex_init(&bus_p->bus_lbw_mutex, NULL, MUTEX_DRIVER, NULL);
3066 cv_init(&bus_p->bus_lbw_cv, NULL, CV_DRIVER, NULL);
3067 linkctl = PCIE_CAP_GET(16, bus_p, PCIE_LINKCTL);
3068 linkctl |= PCIE_LINKCTL_LINK_BW_INTR_EN;
3069 linkctl |= PCIE_LINKCTL_LINK_AUTO_BW_INTR_EN;
3070 PCIE_CAP_PUT(16, bus_p, PCIE_LINKCTL, linkctl);
3071
3072 bus_p->bus_lbw_pbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
3073 bus_p->bus_lbw_cbuf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
3074 bus_p->bus_lbw_state |= PCIE_LBW_S_ENABLED;
3075
3076 return (DDI_SUCCESS);
3077 }
3078
3079 int
pcie_link_bw_disable(dev_info_t * dip)3080 pcie_link_bw_disable(dev_info_t *dip)
3081 {
3082 uint16_t linkctl;
3083 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
3084
3085 if ((bus_p->bus_lbw_state & PCIE_LBW_S_ENABLED) == 0) {
3086 return (DDI_FAILURE);
3087 }
3088
3089 mutex_enter(&bus_p->bus_lbw_mutex);
3090 while ((bus_p->bus_lbw_state &
3091 (PCIE_LBW_S_DISPATCHED | PCIE_LBW_S_RUNNING)) != 0) {
3092 cv_wait(&bus_p->bus_lbw_cv, &bus_p->bus_lbw_mutex);
3093 }
3094 mutex_exit(&bus_p->bus_lbw_mutex);
3095
3096 linkctl = PCIE_CAP_GET(16, bus_p, PCIE_LINKCTL);
3097 linkctl &= ~PCIE_LINKCTL_LINK_BW_INTR_EN;
3098 linkctl &= ~PCIE_LINKCTL_LINK_AUTO_BW_INTR_EN;
3099 PCIE_CAP_PUT(16, bus_p, PCIE_LINKCTL, linkctl);
3100
3101 bus_p->bus_lbw_state &= ~PCIE_LBW_S_ENABLED;
3102 kmem_free(bus_p->bus_lbw_pbuf, MAXPATHLEN);
3103 kmem_free(bus_p->bus_lbw_cbuf, MAXPATHLEN);
3104 bus_p->bus_lbw_pbuf = NULL;
3105 bus_p->bus_lbw_cbuf = NULL;
3106
3107 mutex_destroy(&bus_p->bus_lbw_mutex);
3108 cv_destroy(&bus_p->bus_lbw_cv);
3109
3110 return (DDI_SUCCESS);
3111 }
3112
3113 void
pcie_link_bw_taskq(void * arg)3114 pcie_link_bw_taskq(void *arg)
3115 {
3116 dev_info_t *dip = arg;
3117 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
3118 dev_info_t *cdip;
3119 boolean_t again;
3120 sysevent_t *se;
3121 sysevent_value_t se_val;
3122 sysevent_id_t eid;
3123 sysevent_attr_list_t *ev_attr_list;
3124
3125 top:
3126 ndi_devi_enter(dip);
3127 se = NULL;
3128 ev_attr_list = NULL;
3129 mutex_enter(&bus_p->bus_lbw_mutex);
3130 bus_p->bus_lbw_state &= ~PCIE_LBW_S_DISPATCHED;
3131 bus_p->bus_lbw_state |= PCIE_LBW_S_RUNNING;
3132 mutex_exit(&bus_p->bus_lbw_mutex);
3133
3134 /*
3135 * Update our own speeds as we've likely changed something.
3136 */
3137 pcie_capture_speeds(dip);
3138
3139 /*
3140 * Walk our children. We only care about updating this on function 0
3141 * because the PCIe specification requires that these all be the same
3142 * otherwise.
3143 */
3144 for (cdip = ddi_get_child(dip); cdip != NULL;
3145 cdip = ddi_get_next_sibling(cdip)) {
3146 pcie_bus_t *cbus_p = PCIE_DIP2BUS(cdip);
3147
3148 if (cbus_p == NULL) {
3149 continue;
3150 }
3151
3152 if ((cbus_p->bus_bdf & PCIE_REQ_ID_FUNC_MASK) != 0) {
3153 continue;
3154 }
3155
3156 /*
3157 * It's possible that this can fire while a child is otherwise
3158 * only partially constructed. Therefore, if we don't have the
3159 * config handle, don't bother updating the child.
3160 */
3161 if (cbus_p->bus_cfg_hdl == NULL) {
3162 continue;
3163 }
3164
3165 pcie_capture_speeds(cdip);
3166 break;
3167 }
3168
3169 se = sysevent_alloc(EC_PCIE, ESC_PCIE_LINK_STATE,
3170 ILLUMOS_KERN_PUB "pcie", SE_SLEEP);
3171
3172 (void) ddi_pathname(dip, bus_p->bus_lbw_pbuf);
3173 se_val.value_type = SE_DATA_TYPE_STRING;
3174 se_val.value.sv_string = bus_p->bus_lbw_pbuf;
3175 if (sysevent_add_attr(&ev_attr_list, PCIE_EV_DETECTOR_PATH, &se_val,
3176 SE_SLEEP) != 0) {
3177 ndi_devi_exit(dip);
3178 goto err;
3179 }
3180
3181 if (cdip != NULL) {
3182 (void) ddi_pathname(cdip, bus_p->bus_lbw_cbuf);
3183
3184 se_val.value_type = SE_DATA_TYPE_STRING;
3185 se_val.value.sv_string = bus_p->bus_lbw_cbuf;
3186
3187 /*
3188 * If this fails, that's OK. We'd rather get the event off and
3189 * there's a chance that there may not be anything there for us.
3190 */
3191 (void) sysevent_add_attr(&ev_attr_list, PCIE_EV_CHILD_PATH,
3192 &se_val, SE_SLEEP);
3193 }
3194
3195 ndi_devi_exit(dip);
3196
3197 /*
3198 * Before we generate and send down a sysevent, we need to tell the
3199 * system that parts of the devinfo cache need to be invalidated. While
3200 * the function below takes several args, it ignores them all. Because
3201 * this is a global invalidation, we don't bother trying to do much more
3202 * than requesting a global invalidation, lest we accidentally kick off
3203 * several in a row.
3204 */
3205 ddi_prop_cache_invalidate(DDI_DEV_T_NONE, NULL, NULL, 0);
3206
3207 if (sysevent_attach_attributes(se, ev_attr_list) != 0) {
3208 goto err;
3209 }
3210 ev_attr_list = NULL;
3211
3212 if (log_sysevent(se, SE_SLEEP, &eid) != 0) {
3213 goto err;
3214 }
3215
3216 err:
3217 sysevent_free_attr(ev_attr_list);
3218 sysevent_free(se);
3219
3220 mutex_enter(&bus_p->bus_lbw_mutex);
3221 bus_p->bus_lbw_state &= ~PCIE_LBW_S_RUNNING;
3222 cv_broadcast(&bus_p->bus_lbw_cv);
3223 again = (bus_p->bus_lbw_state & PCIE_LBW_S_DISPATCHED) != 0;
3224 mutex_exit(&bus_p->bus_lbw_mutex);
3225
3226 if (again) {
3227 goto top;
3228 }
3229 }
3230
3231 int
pcie_link_bw_intr(dev_info_t * dip)3232 pcie_link_bw_intr(dev_info_t *dip)
3233 {
3234 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
3235 uint16_t linksts;
3236 uint16_t flags = PCIE_LINKSTS_LINK_BW_MGMT | PCIE_LINKSTS_AUTO_BW;
3237 hrtime_t now;
3238
3239 if ((bus_p->bus_lbw_state & PCIE_LBW_S_ENABLED) == 0) {
3240 return (DDI_INTR_UNCLAIMED);
3241 }
3242
3243 linksts = PCIE_CAP_GET(16, bus_p, PCIE_LINKSTS);
3244 if ((linksts & flags) == 0) {
3245 return (DDI_INTR_UNCLAIMED);
3246 }
3247
3248 now = gethrtime();
3249
3250 /*
3251 * Check if we've already dispatched this event. If we have already
3252 * dispatched it, then there's nothing else to do, we coalesce multiple
3253 * events.
3254 */
3255 mutex_enter(&bus_p->bus_lbw_mutex);
3256 bus_p->bus_lbw_nevents++;
3257 bus_p->bus_lbw_last_ts = now;
3258 if ((bus_p->bus_lbw_state & PCIE_LBW_S_DISPATCHED) == 0) {
3259 if ((bus_p->bus_lbw_state & PCIE_LBW_S_RUNNING) == 0) {
3260 taskq_dispatch_ent(pcie_link_tq, pcie_link_bw_taskq,
3261 dip, 0, &bus_p->bus_lbw_ent);
3262 }
3263
3264 bus_p->bus_lbw_state |= PCIE_LBW_S_DISPATCHED;
3265 }
3266 mutex_exit(&bus_p->bus_lbw_mutex);
3267
3268 PCIE_CAP_PUT(16, bus_p, PCIE_LINKSTS, flags);
3269 return (DDI_INTR_CLAIMED);
3270 }
3271
3272 int
pcie_link_set_target(dev_info_t * dip,pcie_link_speed_t speed)3273 pcie_link_set_target(dev_info_t *dip, pcie_link_speed_t speed)
3274 {
3275 uint16_t ctl2, rval;
3276 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
3277
3278 if (!PCIE_IS_PCIE(bus_p)) {
3279 return (ENOTSUP);
3280 }
3281
3282 if (!PCIE_IS_RP(bus_p) && !PCIE_IS_SWD(bus_p)) {
3283 return (ENOTSUP);
3284 }
3285
3286 if (bus_p->bus_pcie_vers < 2) {
3287 return (ENOTSUP);
3288 }
3289
3290 switch (speed) {
3291 case PCIE_LINK_SPEED_2_5:
3292 rval = PCIE_LINKCTL2_TARGET_SPEED_2_5;
3293 break;
3294 case PCIE_LINK_SPEED_5:
3295 rval = PCIE_LINKCTL2_TARGET_SPEED_5;
3296 break;
3297 case PCIE_LINK_SPEED_8:
3298 rval = PCIE_LINKCTL2_TARGET_SPEED_8;
3299 break;
3300 case PCIE_LINK_SPEED_16:
3301 rval = PCIE_LINKCTL2_TARGET_SPEED_16;
3302 break;
3303 case PCIE_LINK_SPEED_32:
3304 rval = PCIE_LINKCTL2_TARGET_SPEED_32;
3305 break;
3306 case PCIE_LINK_SPEED_64:
3307 rval = PCIE_LINKCTL2_TARGET_SPEED_64;
3308 break;
3309 default:
3310 return (EINVAL);
3311 }
3312
3313 mutex_enter(&bus_p->bus_speed_mutex);
3314 if ((bus_p->bus_sup_speed & speed) == 0) {
3315 mutex_exit(&bus_p->bus_speed_mutex);
3316 return (ENOTSUP);
3317 }
3318
3319 bus_p->bus_target_speed = speed;
3320 bus_p->bus_speed_flags |= PCIE_LINK_F_ADMIN_TARGET;
3321
3322 ctl2 = PCIE_CAP_GET(16, bus_p, PCIE_LINKCTL2);
3323 ctl2 &= ~PCIE_LINKCTL2_TARGET_SPEED_MASK;
3324 ctl2 |= rval;
3325 PCIE_CAP_PUT(16, bus_p, PCIE_LINKCTL2, ctl2);
3326 mutex_exit(&bus_p->bus_speed_mutex);
3327
3328 /*
3329 * Make sure our updates have been reflected in devinfo.
3330 */
3331 pcie_capture_speeds(dip);
3332
3333 return (0);
3334 }
3335
3336 int
pcie_link_retrain(dev_info_t * dip)3337 pcie_link_retrain(dev_info_t *dip)
3338 {
3339 uint16_t ctl;
3340 pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
3341
3342 if (!PCIE_IS_PCIE(bus_p)) {
3343 return (ENOTSUP);
3344 }
3345
3346 if (!PCIE_IS_RP(bus_p) && !PCIE_IS_SWD(bus_p)) {
3347 return (ENOTSUP);
3348 }
3349
3350 /*
3351 * The PCIe specification suggests that we make sure that the link isn't
3352 * in training before issuing this command in case there was a state
3353 * machine transition prior to when we got here. We wait and then go
3354 * ahead and issue the command anyways.
3355 */
3356 for (uint32_t i = 0; i < pcie_link_retrain_count; i++) {
3357 uint16_t sts;
3358
3359 sts = PCIE_CAP_GET(16, bus_p, PCIE_LINKSTS);
3360 if ((sts & PCIE_LINKSTS_LINK_TRAINING) == 0)
3361 break;
3362 delay(drv_usectohz(pcie_link_retrain_delay_ms * 1000));
3363 }
3364
3365 ctl = PCIE_CAP_GET(16, bus_p, PCIE_LINKCTL);
3366 ctl |= PCIE_LINKCTL_RETRAIN_LINK;
3367 PCIE_CAP_PUT(16, bus_p, PCIE_LINKCTL, ctl);
3368
3369 /*
3370 * Wait again to see if it clears before returning to the user.
3371 */
3372 for (uint32_t i = 0; i < pcie_link_retrain_count; i++) {
3373 uint16_t sts;
3374
3375 sts = PCIE_CAP_GET(16, bus_p, PCIE_LINKSTS);
3376 if ((sts & PCIE_LINKSTS_LINK_TRAINING) == 0)
3377 break;
3378 delay(drv_usectohz(pcie_link_retrain_delay_ms * 1000));
3379 }
3380
3381 return (0);
3382 }
3383
3384 /*
3385 * Here we're going through and grabbing information about a given PCIe device.
3386 * Our situation is a little bit complicated at this point. This gets invoked
3387 * both during early initialization and during hotplug events. We cannot rely on
3388 * the device node having been fully set up, that is, while the pcie_bus_t
3389 * normally contains a ddi_acc_handle_t for configuration space, that may not be
3390 * valid yet as this can occur before child initialization or we may be dealing
3391 * with a function that will never have a handle.
3392 *
3393 * However, we should always have a fully furnished pcie_bus_t, which means that
3394 * we can get its bdf and use that to access the devices configuration space.
3395 */
3396 static int
pcie_fabric_feature_scan(dev_info_t * dip,void * arg)3397 pcie_fabric_feature_scan(dev_info_t *dip, void *arg)
3398 {
3399 pcie_bus_t *bus_p;
3400 uint32_t devcap;
3401 uint16_t mps;
3402 dev_info_t *rcdip;
3403 pcie_fabric_data_t *fab = arg;
3404
3405 /*
3406 * Skip over non-PCIe devices. If we encounter something here, we don't
3407 * bother going through any of its children because we don't have reason
3408 * to believe that a PCIe device that this will impact will exist below
3409 * this. While it is possible that there's a PCIe fabric downstream an
3410 * intermediate old PCI/PCI-X bus, at that point, we'll still trigger
3411 * our complex fabric detection and use the minimums.
3412 *
3413 * The reason this doesn't trigger an immediate flagging as a complex
3414 * case like the one below is because we could be scanning a device that
3415 * is a nexus driver and has children already (albeit that would be
3416 * somewhat surprising as we don't anticipate being called at this
3417 * point).
3418 */
3419 if (pcie_dev(dip) != DDI_SUCCESS) {
3420 return (DDI_WALK_PRUNECHILD);
3421 }
3422
3423 /*
3424 * If we fail to find a pcie_bus_t for some reason, that's somewhat
3425 * surprising. We log this fact and set the complex flag and indicate it
3426 * was because of this case. This immediately transitions us to a
3427 * "complex" case which means use the minimal, safe, settings.
3428 */
3429 bus_p = PCIE_DIP2BUS(dip);
3430 if (bus_p == NULL) {
3431 dev_err(dip, CE_WARN, "failed to find associated pcie_bus_t "
3432 "during fabric scan");
3433 fab->pfd_flags |= PCIE_FABRIC_F_COMPLEX;
3434 return (DDI_WALK_TERMINATE);
3435 }
3436
3437 /*
3438 * In a similar case, there is hardware out there which is a PCIe
3439 * device, but does not advertise a PCIe capability. An example of this
3440 * is the IDT Tsi382A which can hide its PCIe capability. If this is
3441 * the case, we immediately terminate scanning and flag this as a
3442 * 'complex' case which causes us to use guaranteed safe settings.
3443 */
3444 if (bus_p->bus_pcie_off == 0) {
3445 dev_err(dip, CE_WARN, "encountered PCIe device without PCIe "
3446 "capability");
3447 fab->pfd_flags |= PCIE_FABRIC_F_COMPLEX;
3448 return (DDI_WALK_TERMINATE);
3449 }
3450
3451 rcdip = pcie_get_rc_dip(dip);
3452
3453 /*
3454 * First, start by determining what the device's tagging and max packet
3455 * size is. All PCIe devices will always have the 8-bit tag information
3456 * as this has existed since PCIe 1.0. 10-bit tagging requires a V2
3457 * PCIe capability. 14-bit requires the DEV3 cap. If we are missing a
3458 * version or capability, then we always treat that as lacking the bits
3459 * in the fabric.
3460 */
3461 ASSERT3U(bus_p->bus_pcie_off, !=, 0);
3462 devcap = pci_cfgacc_get32(rcdip, bus_p->bus_bdf, bus_p->bus_pcie_off +
3463 PCIE_DEVCAP);
3464 mps = devcap & PCIE_DEVCAP_MAX_PAYLOAD_MASK;
3465 if (mps < fab->pfd_mps_found) {
3466 fab->pfd_mps_found = mps;
3467 }
3468
3469 if ((devcap & PCIE_DEVCAP_EXT_TAG_8BIT) == 0) {
3470 fab->pfd_tag_found &= ~PCIE_TAG_8B;
3471 }
3472
3473 if (bus_p->bus_pcie_vers == PCIE_PCIECAP_VER_2_0) {
3474 uint32_t devcap2 = pci_cfgacc_get32(rcdip, bus_p->bus_bdf,
3475 bus_p->bus_pcie_off + PCIE_DEVCAP2);
3476 if ((devcap2 & PCIE_DEVCAP2_10B_TAG_COMP_SUP) == 0) {
3477 fab->pfd_tag_found &= ~PCIE_TAG_10B_COMP;
3478 }
3479 } else {
3480 fab->pfd_tag_found &= ~PCIE_TAG_10B_COMP;
3481 }
3482
3483 if (bus_p->bus_dev3_off != 0) {
3484 uint32_t devcap3 = pci_cfgacc_get32(rcdip, bus_p->bus_bdf,
3485 bus_p->bus_dev3_off + PCIE_DEVCAP3);
3486 if ((devcap3 & PCIE_DEVCAP3_14B_TAG_COMP_SUP) == 0) {
3487 fab->pfd_tag_found &= ~PCIE_TAG_14B_COMP;
3488 }
3489 } else {
3490 fab->pfd_tag_found &= ~PCIE_TAG_14B_COMP;
3491 }
3492
3493 /*
3494 * Now that we have captured device information, we must go and ask
3495 * questions of the topology here. The big theory statement enumerates
3496 * several types of cases. The big question we need to answer is have we
3497 * encountered a hotpluggable bridge that means we need to mark this as
3498 * complex.
3499 *
3500 * The big theory statement notes several different kinds of hotplug
3501 * topologies that exist that we can theoretically support. Right now we
3502 * opt to keep our lives simple and focus solely on (4) and (5). These
3503 * can both be summarized by a single, fairly straightforward rule:
3504 *
3505 * The only allowed hotpluggable entity is a root port.
3506 *
3507 * The reason that this can work and detect cases like (6), (7), and our
3508 * other invalid ones is that the hotplug code will scan and find all
3509 * children before we are called into here.
3510 */
3511 if (bus_p->bus_hp_sup_modes != 0) {
3512 /*
3513 * We opt to terminate in this case because there's no value in
3514 * scanning the rest of the tree at this point.
3515 */
3516 if (!PCIE_IS_RP(bus_p)) {
3517 fab->pfd_flags |= PCIE_FABRIC_F_COMPLEX;
3518 return (DDI_WALK_TERMINATE);
3519 }
3520
3521 fab->pfd_flags |= PCIE_FABRIC_F_RP_HP;
3522 }
3523
3524 /*
3525 * As our walk starts at a root port, we need to make sure that we don't
3526 * pick up any of its siblings and their children as those would be
3527 * different PCIe fabric domains for us to scan. In many hardware
3528 * platforms multiple root ports are all at the same level in the tree.
3529 */
3530 if (bus_p->bus_rp_dip == dip) {
3531 return (DDI_WALK_PRUNESIB);
3532 }
3533
3534 return (DDI_WALK_CONTINUE);
3535 }
3536
3537 static int
pcie_fabric_feature_set(dev_info_t * dip,void * arg)3538 pcie_fabric_feature_set(dev_info_t *dip, void *arg)
3539 {
3540 pcie_bus_t *bus_p;
3541 dev_info_t *rcdip;
3542 pcie_fabric_data_t *fab = arg;
3543 uint32_t devcap, devctl;
3544
3545 if (pcie_dev(dip) != DDI_SUCCESS) {
3546 return (DDI_WALK_PRUNECHILD);
3547 }
3548
3549 /*
3550 * The missing bus_t sent us into the complex case previously. We still
3551 * need to make sure all devices have values we expect here and thus
3552 * don't terminate like the above. The same is true for the case where
3553 * there is no PCIe capability.
3554 */
3555 bus_p = PCIE_DIP2BUS(dip);
3556 if (bus_p == NULL || bus_p->bus_pcie_off == 0) {
3557 return (DDI_WALK_CONTINUE);
3558 }
3559 rcdip = pcie_get_rc_dip(dip);
3560
3561 devcap = pci_cfgacc_get32(rcdip, bus_p->bus_bdf, bus_p->bus_pcie_off +
3562 PCIE_DEVCAP);
3563 devctl = pci_cfgacc_get16(rcdip, bus_p->bus_bdf, bus_p->bus_pcie_off +
3564 PCIE_DEVCTL);
3565
3566 if ((devcap & PCIE_DEVCAP_EXT_TAG_8BIT) != 0 &&
3567 (fab->pfd_tag_act & PCIE_TAG_8B) != 0) {
3568 devctl |= PCIE_DEVCTL_EXT_TAG_FIELD_EN;
3569 }
3570
3571 devctl &= ~PCIE_DEVCTL_MAX_PAYLOAD_MASK;
3572 ASSERT0(fab->pfd_mps_act & ~PCIE_DEVCAP_MAX_PAYLOAD_MASK);
3573 devctl |= fab->pfd_mps_act << PCIE_DEVCTL_MAX_PAYLOAD_SHIFT;
3574
3575 pci_cfgacc_put16(rcdip, bus_p->bus_bdf, bus_p->bus_pcie_off +
3576 PCIE_DEVCTL, devctl);
3577
3578 if (bus_p->bus_pcie_vers == PCIE_PCIECAP_VER_2_0 &&
3579 (fab->pfd_tag_act & PCIE_TAG_10B_COMP) != 0) {
3580 uint32_t devcap2 = pci_cfgacc_get32(rcdip, bus_p->bus_bdf,
3581 bus_p->bus_pcie_off + PCIE_DEVCAP2);
3582
3583 if ((devcap2 & PCIE_DEVCAP2_10B_TAG_REQ_SUP) == 0) {
3584 uint16_t devctl2 = pci_cfgacc_get16(rcdip,
3585 bus_p->bus_bdf, bus_p->bus_pcie_off + PCIE_DEVCTL2);
3586 devctl2 |= PCIE_DEVCTL2_10B_TAG_REQ_EN;
3587 pci_cfgacc_put16(rcdip, bus_p->bus_bdf,
3588 bus_p->bus_pcie_off + PCIE_DEVCTL2, devctl2);
3589 }
3590 }
3591
3592 if (bus_p->bus_dev3_off != 0 &&
3593 (fab->pfd_tag_act & PCIE_TAG_14B_COMP) != 0) {
3594 uint32_t devcap3 = pci_cfgacc_get32(rcdip, bus_p->bus_bdf,
3595 bus_p->bus_dev3_off + PCIE_DEVCAP3);
3596
3597 if ((devcap3 & PCIE_DEVCAP3_14B_TAG_REQ_SUP) == 0) {
3598 uint16_t devctl3 = pci_cfgacc_get16(rcdip,
3599 bus_p->bus_bdf, bus_p->bus_dev3_off + PCIE_DEVCTL3);
3600 devctl3 |= PCIE_DEVCTL3_14B_TAG_REQ_EN;
3601 pci_cfgacc_put16(rcdip, bus_p->bus_bdf,
3602 bus_p->bus_pcie_off + PCIE_DEVCTL2, devctl3);
3603 }
3604 }
3605
3606 /*
3607 * As our walk starts at a root port, we need to make sure that we don't
3608 * pick up any of its siblings and their children as those would be
3609 * different PCIe fabric domains for us to scan. In many hardware
3610 * platforms multiple root ports are all at the same level in the tree.
3611 */
3612 if (bus_p->bus_rp_dip == dip) {
3613 return (DDI_WALK_PRUNESIB);
3614 }
3615
3616 return (DDI_WALK_CONTINUE);
3617 }
3618
3619 /*
3620 * This is used to scan and determine the total set of PCIe fabric settings that
3621 * we should have in the system for everything downstream of this specified root
3622 * port. Note, it is only really safe to call this while working from the
3623 * perspective of a root port as we will be walking down the entire device tree.
3624 *
3625 * However, our callers, particularly hoptlug, don't have all the information
3626 * we'd like. In particular, we need to check that:
3627 *
3628 * o This is actually a PCIe device.
3629 * o That this is a root port (see the big theory statement to understand this
3630 * constraint).
3631 */
3632 void
pcie_fabric_setup(dev_info_t * dip)3633 pcie_fabric_setup(dev_info_t *dip)
3634 {
3635 pcie_bus_t *bus_p;
3636 pcie_fabric_data_t *fab;
3637 dev_info_t *pdip;
3638
3639 bus_p = PCIE_DIP2BUS(dip);
3640 if (bus_p == NULL || !PCIE_IS_RP(bus_p)) {
3641 return;
3642 }
3643
3644 VERIFY3P(bus_p->bus_fab, !=, NULL);
3645 fab = bus_p->bus_fab;
3646
3647 /*
3648 * For us to call ddi_walk_devs(), our parent needs to be held.
3649 * ddi_walk_devs() will take care of grabbing our dip as part of its
3650 * walk before we iterate over our children.
3651 *
3652 * A reasonable question to ask here is why is it safe to ask for our
3653 * parent? In this case, because we have entered here through some
3654 * thread that's operating on us whether as part of attach or a hotplug
3655 * event, our dip somewhat by definition has to be valid. If we were
3656 * looking at our dip's children and then asking them for a parent, then
3657 * that would be a race condition.
3658 */
3659 pdip = ddi_get_parent(dip);
3660 VERIFY3P(pdip, !=, NULL);
3661 ndi_devi_enter(pdip);
3662 fab->pfd_flags |= PCIE_FABRIC_F_SCANNING;
3663
3664 /*
3665 * Reinitialize the tracking structure to basically set the maximum
3666 * caps. These will be chipped away during the scan.
3667 */
3668 fab->pfd_mps_found = PCIE_DEVCAP_MAX_PAYLOAD_4096;
3669 fab->pfd_tag_found = PCIE_TAG_ALL;
3670 fab->pfd_flags &= ~PCIE_FABRIC_F_COMPLEX;
3671
3672 ddi_walk_devs(dip, pcie_fabric_feature_scan, fab);
3673
3674 if ((fab->pfd_flags & PCIE_FABRIC_F_COMPLEX) != 0) {
3675 fab->pfd_tag_act = PCIE_TAG_5B;
3676 fab->pfd_mps_act = PCIE_DEVCAP_MAX_PAYLOAD_128;
3677 } else {
3678 fab->pfd_tag_act = fab->pfd_tag_found;
3679 fab->pfd_mps_act = fab->pfd_mps_found;
3680 }
3681
3682 ddi_walk_devs(dip, pcie_fabric_feature_set, fab);
3683
3684 fab->pfd_flags &= ~PCIE_FABRIC_F_SCANNING;
3685 ndi_devi_exit(pdip);
3686 }
3687