tcp: fix lockdep splat in tcp_snd_una_update()
[linux/fpc-iii.git] / drivers / misc / cxl / pci.c
blob2844e975bf79b1a5ccb9455bd52a23c1176046f5
1 /*
2 * Copyright 2014 IBM Corp.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
10 #include <linux/pci_regs.h>
11 #include <linux/pci_ids.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sort.h>
17 #include <linux/pci.h>
18 #include <linux/of.h>
19 #include <linux/delay.h>
20 #include <asm/opal.h>
21 #include <asm/msi_bitmap.h>
22 #include <asm/pnv-pci.h>
23 #include <asm/io.h>
25 #include "cxl.h"
26 #include <misc/cxl.h>
29 #define CXL_PCI_VSEC_ID 0x1280
30 #define CXL_VSEC_MIN_SIZE 0x80
32 #define CXL_READ_VSEC_LENGTH(dev, vsec, dest) \
33 { \
34 pci_read_config_word(dev, vsec + 0x6, dest); \
35 *dest >>= 4; \
37 #define CXL_READ_VSEC_NAFUS(dev, vsec, dest) \
38 pci_read_config_byte(dev, vsec + 0x8, dest)
40 #define CXL_READ_VSEC_STATUS(dev, vsec, dest) \
41 pci_read_config_byte(dev, vsec + 0x9, dest)
42 #define CXL_STATUS_SECOND_PORT 0x80
43 #define CXL_STATUS_MSI_X_FULL 0x40
44 #define CXL_STATUS_MSI_X_SINGLE 0x20
45 #define CXL_STATUS_FLASH_RW 0x08
46 #define CXL_STATUS_FLASH_RO 0x04
47 #define CXL_STATUS_LOADABLE_AFU 0x02
48 #define CXL_STATUS_LOADABLE_PSL 0x01
49 /* If we see these features we won't try to use the card */
50 #define CXL_UNSUPPORTED_FEATURES \
51 (CXL_STATUS_MSI_X_FULL | CXL_STATUS_MSI_X_SINGLE)
53 #define CXL_READ_VSEC_MODE_CONTROL(dev, vsec, dest) \
54 pci_read_config_byte(dev, vsec + 0xa, dest)
55 #define CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val) \
56 pci_write_config_byte(dev, vsec + 0xa, val)
57 #define CXL_VSEC_PROTOCOL_MASK 0xe0
58 #define CXL_VSEC_PROTOCOL_1024TB 0x80
59 #define CXL_VSEC_PROTOCOL_512TB 0x40
60 #define CXL_VSEC_PROTOCOL_256TB 0x20 /* Power 8 uses this */
61 #define CXL_VSEC_PROTOCOL_ENABLE 0x01
63 #define CXL_READ_VSEC_PSL_REVISION(dev, vsec, dest) \
64 pci_read_config_word(dev, vsec + 0xc, dest)
65 #define CXL_READ_VSEC_CAIA_MINOR(dev, vsec, dest) \
66 pci_read_config_byte(dev, vsec + 0xe, dest)
67 #define CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, dest) \
68 pci_read_config_byte(dev, vsec + 0xf, dest)
69 #define CXL_READ_VSEC_BASE_IMAGE(dev, vsec, dest) \
70 pci_read_config_word(dev, vsec + 0x10, dest)
72 #define CXL_READ_VSEC_IMAGE_STATE(dev, vsec, dest) \
73 pci_read_config_byte(dev, vsec + 0x13, dest)
74 #define CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, val) \
75 pci_write_config_byte(dev, vsec + 0x13, val)
76 #define CXL_VSEC_USER_IMAGE_LOADED 0x80 /* RO */
77 #define CXL_VSEC_PERST_LOADS_IMAGE 0x20 /* RW */
78 #define CXL_VSEC_PERST_SELECT_USER 0x10 /* RW */
80 #define CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, dest) \
81 pci_read_config_dword(dev, vsec + 0x20, dest)
82 #define CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, dest) \
83 pci_read_config_dword(dev, vsec + 0x24, dest)
84 #define CXL_READ_VSEC_PS_OFF(dev, vsec, dest) \
85 pci_read_config_dword(dev, vsec + 0x28, dest)
86 #define CXL_READ_VSEC_PS_SIZE(dev, vsec, dest) \
87 pci_read_config_dword(dev, vsec + 0x2c, dest)
90 /* This works a little different than the p1/p2 register accesses to make it
91 * easier to pull out individual fields */
92 #define AFUD_READ(afu, off) in_be64(afu->native->afu_desc_mmio + off)
93 #define AFUD_READ_LE(afu, off) in_le64(afu->native->afu_desc_mmio + off)
94 #define EXTRACT_PPC_BIT(val, bit) (!!(val & PPC_BIT(bit)))
95 #define EXTRACT_PPC_BITS(val, bs, be) ((val & PPC_BITMASK(bs, be)) >> PPC_BITLSHIFT(be))
97 #define AFUD_READ_INFO(afu) AFUD_READ(afu, 0x0)
98 #define AFUD_NUM_INTS_PER_PROC(val) EXTRACT_PPC_BITS(val, 0, 15)
99 #define AFUD_NUM_PROCS(val) EXTRACT_PPC_BITS(val, 16, 31)
100 #define AFUD_NUM_CRS(val) EXTRACT_PPC_BITS(val, 32, 47)
101 #define AFUD_MULTIMODE(val) EXTRACT_PPC_BIT(val, 48)
102 #define AFUD_PUSH_BLOCK_TRANSFER(val) EXTRACT_PPC_BIT(val, 55)
103 #define AFUD_DEDICATED_PROCESS(val) EXTRACT_PPC_BIT(val, 59)
104 #define AFUD_AFU_DIRECTED(val) EXTRACT_PPC_BIT(val, 61)
105 #define AFUD_TIME_SLICED(val) EXTRACT_PPC_BIT(val, 63)
106 #define AFUD_READ_CR(afu) AFUD_READ(afu, 0x20)
107 #define AFUD_CR_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
108 #define AFUD_READ_CR_OFF(afu) AFUD_READ(afu, 0x28)
109 #define AFUD_READ_PPPSA(afu) AFUD_READ(afu, 0x30)
110 #define AFUD_PPPSA_PP(val) EXTRACT_PPC_BIT(val, 6)
111 #define AFUD_PPPSA_PSA(val) EXTRACT_PPC_BIT(val, 7)
112 #define AFUD_PPPSA_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
113 #define AFUD_READ_PPPSA_OFF(afu) AFUD_READ(afu, 0x38)
114 #define AFUD_READ_EB(afu) AFUD_READ(afu, 0x40)
115 #define AFUD_EB_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
116 #define AFUD_READ_EB_OFF(afu) AFUD_READ(afu, 0x48)
118 static const struct pci_device_id cxl_pci_tbl[] = {
119 { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0477), },
120 { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x044b), },
121 { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x04cf), },
122 { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0601), },
123 { PCI_DEVICE_CLASS(0x120000, ~0), },
127 MODULE_DEVICE_TABLE(pci, cxl_pci_tbl);
131 * Mostly using these wrappers to avoid confusion:
132 * priv 1 is BAR2, while priv 2 is BAR0
134 static inline resource_size_t p1_base(struct pci_dev *dev)
136 return pci_resource_start(dev, 2);
139 static inline resource_size_t p1_size(struct pci_dev *dev)
141 return pci_resource_len(dev, 2);
144 static inline resource_size_t p2_base(struct pci_dev *dev)
146 return pci_resource_start(dev, 0);
149 static inline resource_size_t p2_size(struct pci_dev *dev)
151 return pci_resource_len(dev, 0);
154 static int find_cxl_vsec(struct pci_dev *dev)
156 int vsec = 0;
157 u16 val;
159 while ((vsec = pci_find_next_ext_capability(dev, vsec, PCI_EXT_CAP_ID_VNDR))) {
160 pci_read_config_word(dev, vsec + 0x4, &val);
161 if (val == CXL_PCI_VSEC_ID)
162 return vsec;
164 return 0;
168 static void dump_cxl_config_space(struct pci_dev *dev)
170 int vsec;
171 u32 val;
173 dev_info(&dev->dev, "dump_cxl_config_space\n");
175 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &val);
176 dev_info(&dev->dev, "BAR0: %#.8x\n", val);
177 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &val);
178 dev_info(&dev->dev, "BAR1: %#.8x\n", val);
179 pci_read_config_dword(dev, PCI_BASE_ADDRESS_2, &val);
180 dev_info(&dev->dev, "BAR2: %#.8x\n", val);
181 pci_read_config_dword(dev, PCI_BASE_ADDRESS_3, &val);
182 dev_info(&dev->dev, "BAR3: %#.8x\n", val);
183 pci_read_config_dword(dev, PCI_BASE_ADDRESS_4, &val);
184 dev_info(&dev->dev, "BAR4: %#.8x\n", val);
185 pci_read_config_dword(dev, PCI_BASE_ADDRESS_5, &val);
186 dev_info(&dev->dev, "BAR5: %#.8x\n", val);
188 dev_info(&dev->dev, "p1 regs: %#llx, len: %#llx\n",
189 p1_base(dev), p1_size(dev));
190 dev_info(&dev->dev, "p2 regs: %#llx, len: %#llx\n",
191 p2_base(dev), p2_size(dev));
192 dev_info(&dev->dev, "BAR 4/5: %#llx, len: %#llx\n",
193 pci_resource_start(dev, 4), pci_resource_len(dev, 4));
195 if (!(vsec = find_cxl_vsec(dev)))
196 return;
198 #define show_reg(name, what) \
199 dev_info(&dev->dev, "cxl vsec: %30s: %#x\n", name, what)
201 pci_read_config_dword(dev, vsec + 0x0, &val);
202 show_reg("Cap ID", (val >> 0) & 0xffff);
203 show_reg("Cap Ver", (val >> 16) & 0xf);
204 show_reg("Next Cap Ptr", (val >> 20) & 0xfff);
205 pci_read_config_dword(dev, vsec + 0x4, &val);
206 show_reg("VSEC ID", (val >> 0) & 0xffff);
207 show_reg("VSEC Rev", (val >> 16) & 0xf);
208 show_reg("VSEC Length", (val >> 20) & 0xfff);
209 pci_read_config_dword(dev, vsec + 0x8, &val);
210 show_reg("Num AFUs", (val >> 0) & 0xff);
211 show_reg("Status", (val >> 8) & 0xff);
212 show_reg("Mode Control", (val >> 16) & 0xff);
213 show_reg("Reserved", (val >> 24) & 0xff);
214 pci_read_config_dword(dev, vsec + 0xc, &val);
215 show_reg("PSL Rev", (val >> 0) & 0xffff);
216 show_reg("CAIA Ver", (val >> 16) & 0xffff);
217 pci_read_config_dword(dev, vsec + 0x10, &val);
218 show_reg("Base Image Rev", (val >> 0) & 0xffff);
219 show_reg("Reserved", (val >> 16) & 0x0fff);
220 show_reg("Image Control", (val >> 28) & 0x3);
221 show_reg("Reserved", (val >> 30) & 0x1);
222 show_reg("Image Loaded", (val >> 31) & 0x1);
224 pci_read_config_dword(dev, vsec + 0x14, &val);
225 show_reg("Reserved", val);
226 pci_read_config_dword(dev, vsec + 0x18, &val);
227 show_reg("Reserved", val);
228 pci_read_config_dword(dev, vsec + 0x1c, &val);
229 show_reg("Reserved", val);
231 pci_read_config_dword(dev, vsec + 0x20, &val);
232 show_reg("AFU Descriptor Offset", val);
233 pci_read_config_dword(dev, vsec + 0x24, &val);
234 show_reg("AFU Descriptor Size", val);
235 pci_read_config_dword(dev, vsec + 0x28, &val);
236 show_reg("Problem State Offset", val);
237 pci_read_config_dword(dev, vsec + 0x2c, &val);
238 show_reg("Problem State Size", val);
240 pci_read_config_dword(dev, vsec + 0x30, &val);
241 show_reg("Reserved", val);
242 pci_read_config_dword(dev, vsec + 0x34, &val);
243 show_reg("Reserved", val);
244 pci_read_config_dword(dev, vsec + 0x38, &val);
245 show_reg("Reserved", val);
246 pci_read_config_dword(dev, vsec + 0x3c, &val);
247 show_reg("Reserved", val);
249 pci_read_config_dword(dev, vsec + 0x40, &val);
250 show_reg("PSL Programming Port", val);
251 pci_read_config_dword(dev, vsec + 0x44, &val);
252 show_reg("PSL Programming Control", val);
254 pci_read_config_dword(dev, vsec + 0x48, &val);
255 show_reg("Reserved", val);
256 pci_read_config_dword(dev, vsec + 0x4c, &val);
257 show_reg("Reserved", val);
259 pci_read_config_dword(dev, vsec + 0x50, &val);
260 show_reg("Flash Address Register", val);
261 pci_read_config_dword(dev, vsec + 0x54, &val);
262 show_reg("Flash Size Register", val);
263 pci_read_config_dword(dev, vsec + 0x58, &val);
264 show_reg("Flash Status/Control Register", val);
265 pci_read_config_dword(dev, vsec + 0x58, &val);
266 show_reg("Flash Data Port", val);
268 #undef show_reg
271 static void dump_afu_descriptor(struct cxl_afu *afu)
273 u64 val, afu_cr_num, afu_cr_off, afu_cr_len;
274 int i;
276 #define show_reg(name, what) \
277 dev_info(&afu->dev, "afu desc: %30s: %#llx\n", name, what)
279 val = AFUD_READ_INFO(afu);
280 show_reg("num_ints_per_process", AFUD_NUM_INTS_PER_PROC(val));
281 show_reg("num_of_processes", AFUD_NUM_PROCS(val));
282 show_reg("num_of_afu_CRs", AFUD_NUM_CRS(val));
283 show_reg("req_prog_mode", val & 0xffffULL);
284 afu_cr_num = AFUD_NUM_CRS(val);
286 val = AFUD_READ(afu, 0x8);
287 show_reg("Reserved", val);
288 val = AFUD_READ(afu, 0x10);
289 show_reg("Reserved", val);
290 val = AFUD_READ(afu, 0x18);
291 show_reg("Reserved", val);
293 val = AFUD_READ_CR(afu);
294 show_reg("Reserved", (val >> (63-7)) & 0xff);
295 show_reg("AFU_CR_len", AFUD_CR_LEN(val));
296 afu_cr_len = AFUD_CR_LEN(val) * 256;
298 val = AFUD_READ_CR_OFF(afu);
299 afu_cr_off = val;
300 show_reg("AFU_CR_offset", val);
302 val = AFUD_READ_PPPSA(afu);
303 show_reg("PerProcessPSA_control", (val >> (63-7)) & 0xff);
304 show_reg("PerProcessPSA Length", AFUD_PPPSA_LEN(val));
306 val = AFUD_READ_PPPSA_OFF(afu);
307 show_reg("PerProcessPSA_offset", val);
309 val = AFUD_READ_EB(afu);
310 show_reg("Reserved", (val >> (63-7)) & 0xff);
311 show_reg("AFU_EB_len", AFUD_EB_LEN(val));
313 val = AFUD_READ_EB_OFF(afu);
314 show_reg("AFU_EB_offset", val);
316 for (i = 0; i < afu_cr_num; i++) {
317 val = AFUD_READ_LE(afu, afu_cr_off + i * afu_cr_len);
318 show_reg("CR Vendor", val & 0xffff);
319 show_reg("CR Device", (val >> 16) & 0xffff);
321 #undef show_reg
324 static int init_implementation_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
326 struct device_node *np;
327 const __be32 *prop;
328 u64 psl_dsnctl;
329 u64 chipid;
331 if (!(np = pnv_pci_get_phb_node(dev)))
332 return -ENODEV;
334 while (np && !(prop = of_get_property(np, "ibm,chip-id", NULL)))
335 np = of_get_next_parent(np);
336 if (!np)
337 return -ENODEV;
338 chipid = be32_to_cpup(prop);
339 of_node_put(np);
341 /* Tell PSL where to route data to */
342 psl_dsnctl = 0x02E8900002000000ULL | (chipid << (63-5));
343 cxl_p1_write(adapter, CXL_PSL_DSNDCTL, psl_dsnctl);
344 cxl_p1_write(adapter, CXL_PSL_RESLCKTO, 0x20000000200ULL);
345 /* snoop write mask */
346 cxl_p1_write(adapter, CXL_PSL_SNWRALLOC, 0x00000000FFFFFFFFULL);
347 /* set fir_accum */
348 cxl_p1_write(adapter, CXL_PSL_FIR_CNTL, 0x0800000000000000ULL);
349 /* for debugging with trace arrays */
350 cxl_p1_write(adapter, CXL_PSL_TRACE, 0x0000FF7C00000000ULL);
352 return 0;
355 #define TBSYNC_CNT(n) (((u64)n & 0x7) << (63-6))
356 #define _2048_250MHZ_CYCLES 1
358 static int cxl_setup_psl_timebase(struct cxl *adapter, struct pci_dev *dev)
360 u64 psl_tb;
361 int delta;
362 unsigned int retry = 0;
363 struct device_node *np;
365 if (!(np = pnv_pci_get_phb_node(dev)))
366 return -ENODEV;
368 /* Do not fail when CAPP timebase sync is not supported by OPAL */
369 of_node_get(np);
370 if (! of_get_property(np, "ibm,capp-timebase-sync", NULL)) {
371 of_node_put(np);
372 pr_err("PSL: Timebase sync: OPAL support missing\n");
373 return 0;
375 of_node_put(np);
378 * Setup PSL Timebase Control and Status register
379 * with the recommended Timebase Sync Count value
381 cxl_p1_write(adapter, CXL_PSL_TB_CTLSTAT,
382 TBSYNC_CNT(2 * _2048_250MHZ_CYCLES));
384 /* Enable PSL Timebase */
385 cxl_p1_write(adapter, CXL_PSL_Control, 0x0000000000000000);
386 cxl_p1_write(adapter, CXL_PSL_Control, CXL_PSL_Control_tb);
388 /* Wait until CORE TB and PSL TB difference <= 16usecs */
389 do {
390 msleep(1);
391 if (retry++ > 5) {
392 pr_err("PSL: Timebase sync: giving up!\n");
393 return -EIO;
395 psl_tb = cxl_p1_read(adapter, CXL_PSL_Timebase);
396 delta = mftb() - psl_tb;
397 if (delta < 0)
398 delta = -delta;
399 } while (tb_to_ns(delta) > 16000);
401 return 0;
404 static int init_implementation_afu_regs(struct cxl_afu *afu)
406 /* read/write masks for this slice */
407 cxl_p1n_write(afu, CXL_PSL_APCALLOC_A, 0xFFFFFFFEFEFEFEFEULL);
408 /* APC read/write masks for this slice */
409 cxl_p1n_write(afu, CXL_PSL_COALLOC_A, 0xFF000000FEFEFEFEULL);
410 /* for debugging with trace arrays */
411 cxl_p1n_write(afu, CXL_PSL_SLICE_TRACE, 0x0000FFFF00000000ULL);
412 cxl_p1n_write(afu, CXL_PSL_RXCTL_A, CXL_PSL_RXCTL_AFUHP_4S);
414 return 0;
417 int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq,
418 unsigned int virq)
420 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
422 return pnv_cxl_ioda_msi_setup(dev, hwirq, virq);
425 int cxl_update_image_control(struct cxl *adapter)
427 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
428 int rc;
429 int vsec;
430 u8 image_state;
432 if (!(vsec = find_cxl_vsec(dev))) {
433 dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
434 return -ENODEV;
437 if ((rc = CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state))) {
438 dev_err(&dev->dev, "failed to read image state: %i\n", rc);
439 return rc;
442 if (adapter->perst_loads_image)
443 image_state |= CXL_VSEC_PERST_LOADS_IMAGE;
444 else
445 image_state &= ~CXL_VSEC_PERST_LOADS_IMAGE;
447 if (adapter->perst_select_user)
448 image_state |= CXL_VSEC_PERST_SELECT_USER;
449 else
450 image_state &= ~CXL_VSEC_PERST_SELECT_USER;
452 if ((rc = CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, image_state))) {
453 dev_err(&dev->dev, "failed to update image control: %i\n", rc);
454 return rc;
457 return 0;
460 int cxl_pci_alloc_one_irq(struct cxl *adapter)
462 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
464 return pnv_cxl_alloc_hwirqs(dev, 1);
467 void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq)
469 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
471 return pnv_cxl_release_hwirqs(dev, hwirq, 1);
474 int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
475 struct cxl *adapter, unsigned int num)
477 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
479 return pnv_cxl_alloc_hwirq_ranges(irqs, dev, num);
482 void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs,
483 struct cxl *adapter)
485 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
487 pnv_cxl_release_hwirq_ranges(irqs, dev);
490 static int setup_cxl_bars(struct pci_dev *dev)
492 /* Safety check in case we get backported to < 3.17 without M64 */
493 if ((p1_base(dev) < 0x100000000ULL) ||
494 (p2_base(dev) < 0x100000000ULL)) {
495 dev_err(&dev->dev, "ABORTING: M32 BAR assignment incompatible with CXL\n");
496 return -ENODEV;
500 * BAR 4/5 has a special meaning for CXL and must be programmed with a
501 * special value corresponding to the CXL protocol address range.
502 * For POWER 8 that means bits 48:49 must be set to 10
504 pci_write_config_dword(dev, PCI_BASE_ADDRESS_4, 0x00000000);
505 pci_write_config_dword(dev, PCI_BASE_ADDRESS_5, 0x00020000);
507 return 0;
510 /* pciex node: ibm,opal-m64-window = <0x3d058 0x0 0x3d058 0x0 0x8 0x0>; */
511 static int switch_card_to_cxl(struct pci_dev *dev)
513 int vsec;
514 u8 val;
515 int rc;
517 dev_info(&dev->dev, "switch card to CXL\n");
519 if (!(vsec = find_cxl_vsec(dev))) {
520 dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
521 return -ENODEV;
524 if ((rc = CXL_READ_VSEC_MODE_CONTROL(dev, vsec, &val))) {
525 dev_err(&dev->dev, "failed to read current mode control: %i", rc);
526 return rc;
528 val &= ~CXL_VSEC_PROTOCOL_MASK;
529 val |= CXL_VSEC_PROTOCOL_256TB | CXL_VSEC_PROTOCOL_ENABLE;
530 if ((rc = CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val))) {
531 dev_err(&dev->dev, "failed to enable CXL protocol: %i", rc);
532 return rc;
535 * The CAIA spec (v0.12 11.6 Bi-modal Device Support) states
536 * we must wait 100ms after this mode switch before touching
537 * PCIe config space.
539 msleep(100);
541 return 0;
544 static int pci_map_slice_regs(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
546 u64 p1n_base, p2n_base, afu_desc;
547 const u64 p1n_size = 0x100;
548 const u64 p2n_size = 0x1000;
550 p1n_base = p1_base(dev) + 0x10000 + (afu->slice * p1n_size);
551 p2n_base = p2_base(dev) + (afu->slice * p2n_size);
552 afu->psn_phys = p2_base(dev) + (adapter->native->ps_off + (afu->slice * adapter->ps_size));
553 afu_desc = p2_base(dev) + adapter->native->afu_desc_off + (afu->slice * adapter->native->afu_desc_size);
555 if (!(afu->native->p1n_mmio = ioremap(p1n_base, p1n_size)))
556 goto err;
557 if (!(afu->p2n_mmio = ioremap(p2n_base, p2n_size)))
558 goto err1;
559 if (afu_desc) {
560 if (!(afu->native->afu_desc_mmio = ioremap(afu_desc, adapter->native->afu_desc_size)))
561 goto err2;
564 return 0;
565 err2:
566 iounmap(afu->p2n_mmio);
567 err1:
568 iounmap(afu->native->p1n_mmio);
569 err:
570 dev_err(&afu->dev, "Error mapping AFU MMIO regions\n");
571 return -ENOMEM;
574 static void pci_unmap_slice_regs(struct cxl_afu *afu)
576 if (afu->p2n_mmio) {
577 iounmap(afu->p2n_mmio);
578 afu->p2n_mmio = NULL;
580 if (afu->native->p1n_mmio) {
581 iounmap(afu->native->p1n_mmio);
582 afu->native->p1n_mmio = NULL;
584 if (afu->native->afu_desc_mmio) {
585 iounmap(afu->native->afu_desc_mmio);
586 afu->native->afu_desc_mmio = NULL;
590 void cxl_pci_release_afu(struct device *dev)
592 struct cxl_afu *afu = to_cxl_afu(dev);
594 pr_devel("%s\n", __func__);
596 idr_destroy(&afu->contexts_idr);
597 cxl_release_spa(afu);
599 kfree(afu->native);
600 kfree(afu);
603 /* Expects AFU struct to have recently been zeroed out */
604 static int cxl_read_afu_descriptor(struct cxl_afu *afu)
606 u64 val;
608 val = AFUD_READ_INFO(afu);
609 afu->pp_irqs = AFUD_NUM_INTS_PER_PROC(val);
610 afu->max_procs_virtualised = AFUD_NUM_PROCS(val);
611 afu->crs_num = AFUD_NUM_CRS(val);
613 if (AFUD_AFU_DIRECTED(val))
614 afu->modes_supported |= CXL_MODE_DIRECTED;
615 if (AFUD_DEDICATED_PROCESS(val))
616 afu->modes_supported |= CXL_MODE_DEDICATED;
617 if (AFUD_TIME_SLICED(val))
618 afu->modes_supported |= CXL_MODE_TIME_SLICED;
620 val = AFUD_READ_PPPSA(afu);
621 afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;
622 afu->psa = AFUD_PPPSA_PSA(val);
623 if ((afu->pp_psa = AFUD_PPPSA_PP(val)))
624 afu->native->pp_offset = AFUD_READ_PPPSA_OFF(afu);
626 val = AFUD_READ_CR(afu);
627 afu->crs_len = AFUD_CR_LEN(val) * 256;
628 afu->crs_offset = AFUD_READ_CR_OFF(afu);
631 /* eb_len is in multiple of 4K */
632 afu->eb_len = AFUD_EB_LEN(AFUD_READ_EB(afu)) * 4096;
633 afu->eb_offset = AFUD_READ_EB_OFF(afu);
635 /* eb_off is 4K aligned so lower 12 bits are always zero */
636 if (EXTRACT_PPC_BITS(afu->eb_offset, 0, 11) != 0) {
637 dev_warn(&afu->dev,
638 "Invalid AFU error buffer offset %Lx\n",
639 afu->eb_offset);
640 dev_info(&afu->dev,
641 "Ignoring AFU error buffer in the descriptor\n");
642 /* indicate that no afu buffer exists */
643 afu->eb_len = 0;
646 return 0;
649 static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
651 int i, rc;
652 u32 val;
654 if (afu->psa && afu->adapter->ps_size <
655 (afu->native->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
656 dev_err(&afu->dev, "per-process PSA can't fit inside the PSA!\n");
657 return -ENODEV;
660 if (afu->pp_psa && (afu->pp_size < PAGE_SIZE))
661 dev_warn(&afu->dev, "AFU uses < PAGE_SIZE per-process PSA!");
663 for (i = 0; i < afu->crs_num; i++) {
664 rc = cxl_ops->afu_cr_read32(afu, i, 0, &val);
665 if (rc || val == 0) {
666 dev_err(&afu->dev, "ABORTING: AFU configuration record %i is invalid\n", i);
667 return -EINVAL;
671 return 0;
674 static int sanitise_afu_regs(struct cxl_afu *afu)
676 u64 reg;
679 * Clear out any regs that contain either an IVTE or address or may be
680 * waiting on an acknowledgement to try to be a bit safer as we bring
681 * it online
683 reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
684 if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
685 dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#016llx\n", reg);
686 if (cxl_ops->afu_reset(afu))
687 return -EIO;
688 if (cxl_afu_disable(afu))
689 return -EIO;
690 if (cxl_psl_purge(afu))
691 return -EIO;
693 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
694 cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, 0x0000000000000000);
695 cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An, 0x0000000000000000);
696 cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
697 cxl_p1n_write(afu, CXL_PSL_SPOffset_An, 0x0000000000000000);
698 cxl_p1n_write(afu, CXL_HAURP_An, 0x0000000000000000);
699 cxl_p2n_write(afu, CXL_CSRP_An, 0x0000000000000000);
700 cxl_p2n_write(afu, CXL_AURP1_An, 0x0000000000000000);
701 cxl_p2n_write(afu, CXL_AURP0_An, 0x0000000000000000);
702 cxl_p2n_write(afu, CXL_SSTP1_An, 0x0000000000000000);
703 cxl_p2n_write(afu, CXL_SSTP0_An, 0x0000000000000000);
704 reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
705 if (reg) {
706 dev_warn(&afu->dev, "AFU had pending DSISR: %#016llx\n", reg);
707 if (reg & CXL_PSL_DSISR_TRANS)
708 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
709 else
710 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
712 reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
713 if (reg) {
714 if (reg & ~0xffff)
715 dev_warn(&afu->dev, "AFU had pending SERR: %#016llx\n", reg);
716 cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
718 reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
719 if (reg) {
720 dev_warn(&afu->dev, "AFU had pending error status: %#016llx\n", reg);
721 cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
724 return 0;
727 #define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
729 * afu_eb_read:
730 * Called from sysfs and reads the afu error info buffer. The h/w only supports
731 * 4/8 bytes aligned access. So in case the requested offset/count arent 8 byte
732 * aligned the function uses a bounce buffer which can be max PAGE_SIZE.
734 ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
735 loff_t off, size_t count)
737 loff_t aligned_start, aligned_end;
738 size_t aligned_length;
739 void *tbuf;
740 const void __iomem *ebuf = afu->native->afu_desc_mmio + afu->eb_offset;
742 if (count == 0 || off < 0 || (size_t)off >= afu->eb_len)
743 return 0;
745 /* calculate aligned read window */
746 count = min((size_t)(afu->eb_len - off), count);
747 aligned_start = round_down(off, 8);
748 aligned_end = round_up(off + count, 8);
749 aligned_length = aligned_end - aligned_start;
751 /* max we can copy in one read is PAGE_SIZE */
752 if (aligned_length > ERR_BUFF_MAX_COPY_SIZE) {
753 aligned_length = ERR_BUFF_MAX_COPY_SIZE;
754 count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
757 /* use bounce buffer for copy */
758 tbuf = (void *)__get_free_page(GFP_TEMPORARY);
759 if (!tbuf)
760 return -ENOMEM;
762 /* perform aligned read from the mmio region */
763 memcpy_fromio(tbuf, ebuf + aligned_start, aligned_length);
764 memcpy(buf, tbuf + (off & 0x7), count);
766 free_page((unsigned long)tbuf);
768 return count;
771 static int pci_configure_afu(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
773 int rc;
775 if ((rc = pci_map_slice_regs(afu, adapter, dev)))
776 return rc;
778 if ((rc = sanitise_afu_regs(afu)))
779 goto err1;
781 /* We need to reset the AFU before we can read the AFU descriptor */
782 if ((rc = cxl_ops->afu_reset(afu)))
783 goto err1;
785 if (cxl_verbose)
786 dump_afu_descriptor(afu);
788 if ((rc = cxl_read_afu_descriptor(afu)))
789 goto err1;
791 if ((rc = cxl_afu_descriptor_looks_ok(afu)))
792 goto err1;
794 if ((rc = init_implementation_afu_regs(afu)))
795 goto err1;
797 if ((rc = cxl_native_register_serr_irq(afu)))
798 goto err1;
800 if ((rc = cxl_native_register_psl_irq(afu)))
801 goto err2;
803 return 0;
805 err2:
806 cxl_native_release_serr_irq(afu);
807 err1:
808 pci_unmap_slice_regs(afu);
809 return rc;
812 static void pci_deconfigure_afu(struct cxl_afu *afu)
814 cxl_native_release_psl_irq(afu);
815 cxl_native_release_serr_irq(afu);
816 pci_unmap_slice_regs(afu);
819 static int pci_init_afu(struct cxl *adapter, int slice, struct pci_dev *dev)
821 struct cxl_afu *afu;
822 int rc = -ENOMEM;
824 afu = cxl_alloc_afu(adapter, slice);
825 if (!afu)
826 return -ENOMEM;
828 afu->native = kzalloc(sizeof(struct cxl_afu_native), GFP_KERNEL);
829 if (!afu->native)
830 goto err_free_afu;
832 mutex_init(&afu->native->spa_mutex);
834 rc = dev_set_name(&afu->dev, "afu%i.%i", adapter->adapter_num, slice);
835 if (rc)
836 goto err_free_native;
838 rc = pci_configure_afu(afu, adapter, dev);
839 if (rc)
840 goto err_free_native;
842 /* Don't care if this fails */
843 cxl_debugfs_afu_add(afu);
846 * After we call this function we must not free the afu directly, even
847 * if it returns an error!
849 if ((rc = cxl_register_afu(afu)))
850 goto err_put1;
852 if ((rc = cxl_sysfs_afu_add(afu)))
853 goto err_put1;
855 adapter->afu[afu->slice] = afu;
857 if ((rc = cxl_pci_vphb_add(afu)))
858 dev_info(&afu->dev, "Can't register vPHB\n");
860 return 0;
862 err_put1:
863 pci_deconfigure_afu(afu);
864 cxl_debugfs_afu_remove(afu);
865 device_unregister(&afu->dev);
866 return rc;
868 err_free_native:
869 kfree(afu->native);
870 err_free_afu:
871 kfree(afu);
872 return rc;
876 static void cxl_pci_remove_afu(struct cxl_afu *afu)
878 pr_devel("%s\n", __func__);
880 if (!afu)
881 return;
883 cxl_pci_vphb_remove(afu);
884 cxl_sysfs_afu_remove(afu);
885 cxl_debugfs_afu_remove(afu);
887 spin_lock(&afu->adapter->afu_list_lock);
888 afu->adapter->afu[afu->slice] = NULL;
889 spin_unlock(&afu->adapter->afu_list_lock);
891 cxl_context_detach_all(afu);
892 cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
894 pci_deconfigure_afu(afu);
895 device_unregister(&afu->dev);
898 int cxl_pci_reset(struct cxl *adapter)
900 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
901 int rc;
903 if (adapter->perst_same_image) {
904 dev_warn(&dev->dev,
905 "cxl: refusing to reset/reflash when perst_reloads_same_image is set.\n");
906 return -EINVAL;
909 dev_info(&dev->dev, "CXL reset\n");
911 /* pcie_warm_reset requests a fundamental pci reset which includes a
912 * PERST assert/deassert. PERST triggers a loading of the image
913 * if "user" or "factory" is selected in sysfs */
914 if ((rc = pci_set_pcie_reset_state(dev, pcie_warm_reset))) {
915 dev_err(&dev->dev, "cxl: pcie_warm_reset failed\n");
916 return rc;
919 return rc;
922 static int cxl_map_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
924 if (pci_request_region(dev, 2, "priv 2 regs"))
925 goto err1;
926 if (pci_request_region(dev, 0, "priv 1 regs"))
927 goto err2;
929 pr_devel("cxl_map_adapter_regs: p1: %#016llx %#llx, p2: %#016llx %#llx",
930 p1_base(dev), p1_size(dev), p2_base(dev), p2_size(dev));
932 if (!(adapter->native->p1_mmio = ioremap(p1_base(dev), p1_size(dev))))
933 goto err3;
935 if (!(adapter->native->p2_mmio = ioremap(p2_base(dev), p2_size(dev))))
936 goto err4;
938 return 0;
940 err4:
941 iounmap(adapter->native->p1_mmio);
942 adapter->native->p1_mmio = NULL;
943 err3:
944 pci_release_region(dev, 0);
945 err2:
946 pci_release_region(dev, 2);
947 err1:
948 return -ENOMEM;
951 static void cxl_unmap_adapter_regs(struct cxl *adapter)
953 if (adapter->native->p1_mmio) {
954 iounmap(adapter->native->p1_mmio);
955 adapter->native->p1_mmio = NULL;
956 pci_release_region(to_pci_dev(adapter->dev.parent), 2);
958 if (adapter->native->p2_mmio) {
959 iounmap(adapter->native->p2_mmio);
960 adapter->native->p2_mmio = NULL;
961 pci_release_region(to_pci_dev(adapter->dev.parent), 0);
965 static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev)
967 int vsec;
968 u32 afu_desc_off, afu_desc_size;
969 u32 ps_off, ps_size;
970 u16 vseclen;
971 u8 image_state;
973 if (!(vsec = find_cxl_vsec(dev))) {
974 dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
975 return -ENODEV;
978 CXL_READ_VSEC_LENGTH(dev, vsec, &vseclen);
979 if (vseclen < CXL_VSEC_MIN_SIZE) {
980 dev_err(&dev->dev, "ABORTING: CXL VSEC too short\n");
981 return -EINVAL;
984 CXL_READ_VSEC_STATUS(dev, vsec, &adapter->vsec_status);
985 CXL_READ_VSEC_PSL_REVISION(dev, vsec, &adapter->psl_rev);
986 CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, &adapter->caia_major);
987 CXL_READ_VSEC_CAIA_MINOR(dev, vsec, &adapter->caia_minor);
988 CXL_READ_VSEC_BASE_IMAGE(dev, vsec, &adapter->base_image);
989 CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state);
990 adapter->user_image_loaded = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
991 adapter->perst_select_user = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
993 CXL_READ_VSEC_NAFUS(dev, vsec, &adapter->slices);
994 CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, &afu_desc_off);
995 CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, &afu_desc_size);
996 CXL_READ_VSEC_PS_OFF(dev, vsec, &ps_off);
997 CXL_READ_VSEC_PS_SIZE(dev, vsec, &ps_size);
999 /* Convert everything to bytes, because there is NO WAY I'd look at the
1000 * code a month later and forget what units these are in ;-) */
1001 adapter->native->ps_off = ps_off * 64 * 1024;
1002 adapter->ps_size = ps_size * 64 * 1024;
1003 adapter->native->afu_desc_off = afu_desc_off * 64 * 1024;
1004 adapter->native->afu_desc_size = afu_desc_size * 64 * 1024;
1006 /* Total IRQs - 1 PSL ERROR - #AFU*(1 slice error + 1 DSI) */
1007 adapter->user_irqs = pnv_cxl_get_irq_count(dev) - 1 - 2*adapter->slices;
1009 return 0;
1013 * Workaround a PCIe Host Bridge defect on some cards, that can cause
1014 * malformed Transaction Layer Packet (TLP) errors to be erroneously
1015 * reported. Mask this error in the Uncorrectable Error Mask Register.
1017 * The upper nibble of the PSL revision is used to distinguish between
1018 * different cards. The affected ones have it set to 0.
1020 static void cxl_fixup_malformed_tlp(struct cxl *adapter, struct pci_dev *dev)
1022 int aer;
1023 u32 data;
1025 if (adapter->psl_rev & 0xf000)
1026 return;
1027 if (!(aer = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)))
1028 return;
1029 pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, &data);
1030 if (data & PCI_ERR_UNC_MALF_TLP)
1031 if (data & PCI_ERR_UNC_INTN)
1032 return;
1033 data |= PCI_ERR_UNC_MALF_TLP;
1034 data |= PCI_ERR_UNC_INTN;
1035 pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, data);
1038 static int cxl_vsec_looks_ok(struct cxl *adapter, struct pci_dev *dev)
1040 if (adapter->vsec_status & CXL_STATUS_SECOND_PORT)
1041 return -EBUSY;
1043 if (adapter->vsec_status & CXL_UNSUPPORTED_FEATURES) {
1044 dev_err(&dev->dev, "ABORTING: CXL requires unsupported features\n");
1045 return -EINVAL;
1048 if (!adapter->slices) {
1049 /* Once we support dynamic reprogramming we can use the card if
1050 * it supports loadable AFUs */
1051 dev_err(&dev->dev, "ABORTING: Device has no AFUs\n");
1052 return -EINVAL;
1055 if (!adapter->native->afu_desc_off || !adapter->native->afu_desc_size) {
1056 dev_err(&dev->dev, "ABORTING: VSEC shows no AFU descriptors\n");
1057 return -EINVAL;
1060 if (adapter->ps_size > p2_size(dev) - adapter->native->ps_off) {
1061 dev_err(&dev->dev, "ABORTING: Problem state size larger than "
1062 "available in BAR2: 0x%llx > 0x%llx\n",
1063 adapter->ps_size, p2_size(dev) - adapter->native->ps_off);
1064 return -EINVAL;
1067 return 0;
1070 ssize_t cxl_pci_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len)
1072 return pci_read_vpd(to_pci_dev(adapter->dev.parent), 0, len, buf);
1075 static void cxl_release_adapter(struct device *dev)
1077 struct cxl *adapter = to_cxl_adapter(dev);
1079 pr_devel("cxl_release_adapter\n");
1081 cxl_remove_adapter_nr(adapter);
1083 kfree(adapter->native);
1084 kfree(adapter);
1087 #define CXL_PSL_ErrIVTE_tberror (0x1ull << (63-31))
1089 static int sanitise_adapter_regs(struct cxl *adapter)
1091 /* Clear PSL tberror bit by writing 1 to it */
1092 cxl_p1_write(adapter, CXL_PSL_ErrIVTE, CXL_PSL_ErrIVTE_tberror);
1093 return cxl_tlb_slb_invalidate(adapter);
1096 /* This should contain *only* operations that can safely be done in
1097 * both creation and recovery.
1099 static int cxl_configure_adapter(struct cxl *adapter, struct pci_dev *dev)
1101 int rc;
1103 adapter->dev.parent = &dev->dev;
1104 adapter->dev.release = cxl_release_adapter;
1105 pci_set_drvdata(dev, adapter);
1107 rc = pci_enable_device(dev);
1108 if (rc) {
1109 dev_err(&dev->dev, "pci_enable_device failed: %i\n", rc);
1110 return rc;
1113 if ((rc = cxl_read_vsec(adapter, dev)))
1114 return rc;
1116 if ((rc = cxl_vsec_looks_ok(adapter, dev)))
1117 return rc;
1119 cxl_fixup_malformed_tlp(adapter, dev);
1121 if ((rc = setup_cxl_bars(dev)))
1122 return rc;
1124 if ((rc = switch_card_to_cxl(dev)))
1125 return rc;
1127 if ((rc = cxl_update_image_control(adapter)))
1128 return rc;
1130 if ((rc = cxl_map_adapter_regs(adapter, dev)))
1131 return rc;
1133 if ((rc = sanitise_adapter_regs(adapter)))
1134 goto err;
1136 if ((rc = init_implementation_adapter_regs(adapter, dev)))
1137 goto err;
1139 if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_CAPI)))
1140 goto err;
1142 /* If recovery happened, the last step is to turn on snooping.
1143 * In the non-recovery case this has no effect */
1144 if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_SNOOP_ON)))
1145 goto err;
1147 if ((rc = cxl_setup_psl_timebase(adapter, dev)))
1148 goto err;
1150 if ((rc = cxl_native_register_psl_err_irq(adapter)))
1151 goto err;
1153 return 0;
1155 err:
1156 cxl_unmap_adapter_regs(adapter);
1157 return rc;
1161 static void cxl_deconfigure_adapter(struct cxl *adapter)
1163 struct pci_dev *pdev = to_pci_dev(adapter->dev.parent);
1165 cxl_native_release_psl_err_irq(adapter);
1166 cxl_unmap_adapter_regs(adapter);
1168 pci_disable_device(pdev);
1171 static struct cxl *cxl_pci_init_adapter(struct pci_dev *dev)
1173 struct cxl *adapter;
1174 int rc;
1176 adapter = cxl_alloc_adapter();
1177 if (!adapter)
1178 return ERR_PTR(-ENOMEM);
1180 adapter->native = kzalloc(sizeof(struct cxl_native), GFP_KERNEL);
1181 if (!adapter->native) {
1182 rc = -ENOMEM;
1183 goto err_release;
1186 /* Set defaults for parameters which need to persist over
1187 * configure/reconfigure
1189 adapter->perst_loads_image = true;
1190 adapter->perst_same_image = false;
1192 rc = cxl_configure_adapter(adapter, dev);
1193 if (rc) {
1194 pci_disable_device(dev);
1195 goto err_release;
1198 /* Don't care if this one fails: */
1199 cxl_debugfs_adapter_add(adapter);
1202 * After we call this function we must not free the adapter directly,
1203 * even if it returns an error!
1205 if ((rc = cxl_register_adapter(adapter)))
1206 goto err_put1;
1208 if ((rc = cxl_sysfs_adapter_add(adapter)))
1209 goto err_put1;
1211 return adapter;
1213 err_put1:
1214 /* This should mirror cxl_remove_adapter, except without the
1215 * sysfs parts
1217 cxl_debugfs_adapter_remove(adapter);
1218 cxl_deconfigure_adapter(adapter);
1219 device_unregister(&adapter->dev);
1220 return ERR_PTR(rc);
1222 err_release:
1223 cxl_release_adapter(&adapter->dev);
1224 return ERR_PTR(rc);
1227 static void cxl_pci_remove_adapter(struct cxl *adapter)
1229 pr_devel("cxl_remove_adapter\n");
1231 cxl_sysfs_adapter_remove(adapter);
1232 cxl_debugfs_adapter_remove(adapter);
1234 cxl_deconfigure_adapter(adapter);
1236 device_unregister(&adapter->dev);
1239 static int cxl_probe(struct pci_dev *dev, const struct pci_device_id *id)
1241 struct cxl *adapter;
1242 int slice;
1243 int rc;
1245 if (cxl_pci_is_vphb_device(dev)) {
1246 dev_dbg(&dev->dev, "cxl_init_adapter: Ignoring cxl vphb device\n");
1247 return -ENODEV;
1250 if (cxl_verbose)
1251 dump_cxl_config_space(dev);
1253 adapter = cxl_pci_init_adapter(dev);
1254 if (IS_ERR(adapter)) {
1255 dev_err(&dev->dev, "cxl_init_adapter failed: %li\n", PTR_ERR(adapter));
1256 return PTR_ERR(adapter);
1259 for (slice = 0; slice < adapter->slices; slice++) {
1260 if ((rc = pci_init_afu(adapter, slice, dev))) {
1261 dev_err(&dev->dev, "AFU %i failed to initialise: %i\n", slice, rc);
1262 continue;
1265 rc = cxl_afu_select_best_mode(adapter->afu[slice]);
1266 if (rc)
1267 dev_err(&dev->dev, "AFU %i failed to start: %i\n", slice, rc);
1270 return 0;
1273 static void cxl_remove(struct pci_dev *dev)
1275 struct cxl *adapter = pci_get_drvdata(dev);
1276 struct cxl_afu *afu;
1277 int i;
1280 * Lock to prevent someone grabbing a ref through the adapter list as
1281 * we are removing it
1283 for (i = 0; i < adapter->slices; i++) {
1284 afu = adapter->afu[i];
1285 cxl_pci_remove_afu(afu);
1287 cxl_pci_remove_adapter(adapter);
1290 static pci_ers_result_t cxl_vphb_error_detected(struct cxl_afu *afu,
1291 pci_channel_state_t state)
1293 struct pci_dev *afu_dev;
1294 pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
1295 pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
1297 /* There should only be one entry, but go through the list
1298 * anyway
1300 list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
1301 if (!afu_dev->driver)
1302 continue;
1304 afu_dev->error_state = state;
1306 if (afu_dev->driver->err_handler)
1307 afu_result = afu_dev->driver->err_handler->error_detected(afu_dev,
1308 state);
1309 /* Disconnect trumps all, NONE trumps NEED_RESET */
1310 if (afu_result == PCI_ERS_RESULT_DISCONNECT)
1311 result = PCI_ERS_RESULT_DISCONNECT;
1312 else if ((afu_result == PCI_ERS_RESULT_NONE) &&
1313 (result == PCI_ERS_RESULT_NEED_RESET))
1314 result = PCI_ERS_RESULT_NONE;
1316 return result;
1319 static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
1320 pci_channel_state_t state)
1322 struct cxl *adapter = pci_get_drvdata(pdev);
1323 struct cxl_afu *afu;
1324 pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
1325 int i;
1327 /* At this point, we could still have an interrupt pending.
1328 * Let's try to get them out of the way before they do
1329 * anything we don't like.
1331 schedule();
1333 /* If we're permanently dead, give up. */
1334 if (state == pci_channel_io_perm_failure) {
1335 /* Tell the AFU drivers; but we don't care what they
1336 * say, we're going away.
1338 for (i = 0; i < adapter->slices; i++) {
1339 afu = adapter->afu[i];
1340 cxl_vphb_error_detected(afu, state);
1342 return PCI_ERS_RESULT_DISCONNECT;
1345 /* Are we reflashing?
1347 * If we reflash, we could come back as something entirely
1348 * different, including a non-CAPI card. As such, by default
1349 * we don't participate in the process. We'll be unbound and
1350 * the slot re-probed. (TODO: check EEH doesn't blindly rebind
1351 * us!)
1353 * However, this isn't the entire story: for reliablity
1354 * reasons, we usually want to reflash the FPGA on PERST in
1355 * order to get back to a more reliable known-good state.
1357 * This causes us a bit of a problem: if we reflash we can't
1358 * trust that we'll come back the same - we could have a new
1359 * image and been PERSTed in order to load that
1360 * image. However, most of the time we actually *will* come
1361 * back the same - for example a regular EEH event.
1363 * Therefore, we allow the user to assert that the image is
1364 * indeed the same and that we should continue on into EEH
1365 * anyway.
1367 if (adapter->perst_loads_image && !adapter->perst_same_image) {
1368 /* TODO take the PHB out of CXL mode */
1369 dev_info(&pdev->dev, "reflashing, so opting out of EEH!\n");
1370 return PCI_ERS_RESULT_NONE;
1374 * At this point, we want to try to recover. We'll always
1375 * need a complete slot reset: we don't trust any other reset.
1377 * Now, we go through each AFU:
1378 * - We send the driver, if bound, an error_detected callback.
1379 * We expect it to clean up, but it can also tell us to give
1380 * up and permanently detach the card. To simplify things, if
1381 * any bound AFU driver doesn't support EEH, we give up on EEH.
1383 * - We detach all contexts associated with the AFU. This
1384 * does not free them, but puts them into a CLOSED state
1385 * which causes any the associated files to return useful
1386 * errors to userland. It also unmaps, but does not free,
1387 * any IRQs.
1389 * - We clean up our side: releasing and unmapping resources we hold
1390 * so we can wire them up again when the hardware comes back up.
1392 * Driver authors should note:
1394 * - Any contexts you create in your kernel driver (except
1395 * those associated with anonymous file descriptors) are
1396 * your responsibility to free and recreate. Likewise with
1397 * any attached resources.
1399 * - We will take responsibility for re-initialising the
1400 * device context (the one set up for you in
1401 * cxl_pci_enable_device_hook and accessed through
1402 * cxl_get_context). If you've attached IRQs or other
1403 * resources to it, they remains yours to free.
1405 * You can call the same functions to release resources as you
1406 * normally would: we make sure that these functions continue
1407 * to work when the hardware is down.
1409 * Two examples:
1411 * 1) If you normally free all your resources at the end of
1412 * each request, or if you use anonymous FDs, your
1413 * error_detected callback can simply set a flag to tell
1414 * your driver not to start any new calls. You can then
1415 * clear the flag in the resume callback.
1417 * 2) If you normally allocate your resources on startup:
1418 * * Set a flag in error_detected as above.
1419 * * Let CXL detach your contexts.
1420 * * In slot_reset, free the old resources and allocate new ones.
1421 * * In resume, clear the flag to allow things to start.
1423 for (i = 0; i < adapter->slices; i++) {
1424 afu = adapter->afu[i];
1426 result = cxl_vphb_error_detected(afu, state);
1428 /* Only continue if everyone agrees on NEED_RESET */
1429 if (result != PCI_ERS_RESULT_NEED_RESET)
1430 return result;
1432 cxl_context_detach_all(afu);
1433 cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
1434 pci_deconfigure_afu(afu);
1436 cxl_deconfigure_adapter(adapter);
1438 return result;
1441 static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
1443 struct cxl *adapter = pci_get_drvdata(pdev);
1444 struct cxl_afu *afu;
1445 struct cxl_context *ctx;
1446 struct pci_dev *afu_dev;
1447 pci_ers_result_t afu_result = PCI_ERS_RESULT_RECOVERED;
1448 pci_ers_result_t result = PCI_ERS_RESULT_RECOVERED;
1449 int i;
1451 if (cxl_configure_adapter(adapter, pdev))
1452 goto err;
1454 for (i = 0; i < adapter->slices; i++) {
1455 afu = adapter->afu[i];
1457 if (pci_configure_afu(afu, adapter, pdev))
1458 goto err;
1460 if (cxl_afu_select_best_mode(afu))
1461 goto err;
1463 list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
1464 /* Reset the device context.
1465 * TODO: make this less disruptive
1467 ctx = cxl_get_context(afu_dev);
1469 if (ctx && cxl_release_context(ctx))
1470 goto err;
1472 ctx = cxl_dev_context_init(afu_dev);
1473 if (!ctx)
1474 goto err;
1476 afu_dev->dev.archdata.cxl_ctx = ctx;
1478 if (cxl_ops->afu_check_and_enable(afu))
1479 goto err;
1481 afu_dev->error_state = pci_channel_io_normal;
1483 /* If there's a driver attached, allow it to
1484 * chime in on recovery. Drivers should check
1485 * if everything has come back OK, but
1486 * shouldn't start new work until we call
1487 * their resume function.
1489 if (!afu_dev->driver)
1490 continue;
1492 if (afu_dev->driver->err_handler &&
1493 afu_dev->driver->err_handler->slot_reset)
1494 afu_result = afu_dev->driver->err_handler->slot_reset(afu_dev);
1496 if (afu_result == PCI_ERS_RESULT_DISCONNECT)
1497 result = PCI_ERS_RESULT_DISCONNECT;
1500 return result;
1502 err:
1503 /* All the bits that happen in both error_detected and cxl_remove
1504 * should be idempotent, so we don't need to worry about leaving a mix
1505 * of unconfigured and reconfigured resources.
1507 dev_err(&pdev->dev, "EEH recovery failed. Asking to be disconnected.\n");
1508 return PCI_ERS_RESULT_DISCONNECT;
1511 static void cxl_pci_resume(struct pci_dev *pdev)
1513 struct cxl *adapter = pci_get_drvdata(pdev);
1514 struct cxl_afu *afu;
1515 struct pci_dev *afu_dev;
1516 int i;
1518 /* Everything is back now. Drivers should restart work now.
1519 * This is not the place to be checking if everything came back up
1520 * properly, because there's no return value: do that in slot_reset.
1522 for (i = 0; i < adapter->slices; i++) {
1523 afu = adapter->afu[i];
1525 list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
1526 if (afu_dev->driver && afu_dev->driver->err_handler &&
1527 afu_dev->driver->err_handler->resume)
1528 afu_dev->driver->err_handler->resume(afu_dev);
1533 static const struct pci_error_handlers cxl_err_handler = {
1534 .error_detected = cxl_pci_error_detected,
1535 .slot_reset = cxl_pci_slot_reset,
1536 .resume = cxl_pci_resume,
1539 struct pci_driver cxl_pci_driver = {
1540 .name = "cxl-pci",
1541 .id_table = cxl_pci_tbl,
1542 .probe = cxl_probe,
1543 .remove = cxl_remove,
1544 .shutdown = cxl_remove,
1545 .err_handler = &cxl_err_handler,