blob: fb4fd45e87443c8a36171a63099e418e9e0bdb1b [file] [log] [blame]
Ian Munsief204e0b2014-10-08 19:55:02 +11001/*
2 * Copyright 2014 IBM Corp.
3 *
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 */
9
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/pci-bridge.h> /* for struct pci_controller */
23#include <asm/pnv-pci.h>
Ryan Grimm62fa19d2015-01-19 11:52:51 -060024#include <asm/io.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110025
26#include "cxl.h"
Daniel Axtens9e8df8a2015-08-14 17:41:26 +100027#include <misc/cxl.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110028
29
30#define CXL_PCI_VSEC_ID 0x1280
31#define CXL_VSEC_MIN_SIZE 0x80
32
33#define CXL_READ_VSEC_LENGTH(dev, vsec, dest) \
34 { \
35 pci_read_config_word(dev, vsec + 0x6, dest); \
36 *dest >>= 4; \
37 }
38#define CXL_READ_VSEC_NAFUS(dev, vsec, dest) \
39 pci_read_config_byte(dev, vsec + 0x8, dest)
40
41#define CXL_READ_VSEC_STATUS(dev, vsec, dest) \
42 pci_read_config_byte(dev, vsec + 0x9, dest)
43#define CXL_STATUS_SECOND_PORT 0x80
44#define CXL_STATUS_MSI_X_FULL 0x40
45#define CXL_STATUS_MSI_X_SINGLE 0x20
46#define CXL_STATUS_FLASH_RW 0x08
47#define CXL_STATUS_FLASH_RO 0x04
48#define CXL_STATUS_LOADABLE_AFU 0x02
49#define CXL_STATUS_LOADABLE_PSL 0x01
50/* If we see these features we won't try to use the card */
51#define CXL_UNSUPPORTED_FEATURES \
52 (CXL_STATUS_MSI_X_FULL | CXL_STATUS_MSI_X_SINGLE)
53
54#define CXL_READ_VSEC_MODE_CONTROL(dev, vsec, dest) \
55 pci_read_config_byte(dev, vsec + 0xa, dest)
56#define CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val) \
57 pci_write_config_byte(dev, vsec + 0xa, val)
58#define CXL_VSEC_PROTOCOL_MASK 0xe0
59#define CXL_VSEC_PROTOCOL_1024TB 0x80
60#define CXL_VSEC_PROTOCOL_512TB 0x40
61#define CXL_VSEC_PROTOCOL_256TB 0x20 /* Power 8 uses this */
62#define CXL_VSEC_PROTOCOL_ENABLE 0x01
63
64#define CXL_READ_VSEC_PSL_REVISION(dev, vsec, dest) \
65 pci_read_config_word(dev, vsec + 0xc, dest)
66#define CXL_READ_VSEC_CAIA_MINOR(dev, vsec, dest) \
67 pci_read_config_byte(dev, vsec + 0xe, dest)
68#define CXL_READ_VSEC_CAIA_MAJOR(dev, vsec, dest) \
69 pci_read_config_byte(dev, vsec + 0xf, dest)
70#define CXL_READ_VSEC_BASE_IMAGE(dev, vsec, dest) \
71 pci_read_config_word(dev, vsec + 0x10, dest)
72
73#define CXL_READ_VSEC_IMAGE_STATE(dev, vsec, dest) \
74 pci_read_config_byte(dev, vsec + 0x13, dest)
75#define CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, val) \
76 pci_write_config_byte(dev, vsec + 0x13, val)
77#define CXL_VSEC_USER_IMAGE_LOADED 0x80 /* RO */
78#define CXL_VSEC_PERST_LOADS_IMAGE 0x20 /* RW */
79#define CXL_VSEC_PERST_SELECT_USER 0x10 /* RW */
80
81#define CXL_READ_VSEC_AFU_DESC_OFF(dev, vsec, dest) \
82 pci_read_config_dword(dev, vsec + 0x20, dest)
83#define CXL_READ_VSEC_AFU_DESC_SIZE(dev, vsec, dest) \
84 pci_read_config_dword(dev, vsec + 0x24, dest)
85#define CXL_READ_VSEC_PS_OFF(dev, vsec, dest) \
86 pci_read_config_dword(dev, vsec + 0x28, dest)
87#define CXL_READ_VSEC_PS_SIZE(dev, vsec, dest) \
88 pci_read_config_dword(dev, vsec + 0x2c, dest)
89
90
91/* This works a little different than the p1/p2 register accesses to make it
92 * easier to pull out individual fields */
Christophe Lombardcbffa3a2016-03-04 12:26:35 +010093#define AFUD_READ(afu, off) in_be64(afu->native->afu_desc_mmio + off)
94#define AFUD_READ_LE(afu, off) in_le64(afu->native->afu_desc_mmio + off)
Ian Munsief204e0b2014-10-08 19:55:02 +110095#define EXTRACT_PPC_BIT(val, bit) (!!(val & PPC_BIT(bit)))
96#define EXTRACT_PPC_BITS(val, bs, be) ((val & PPC_BITMASK(bs, be)) >> PPC_BITLSHIFT(be))
97
98#define AFUD_READ_INFO(afu) AFUD_READ(afu, 0x0)
99#define AFUD_NUM_INTS_PER_PROC(val) EXTRACT_PPC_BITS(val, 0, 15)
100#define AFUD_NUM_PROCS(val) EXTRACT_PPC_BITS(val, 16, 31)
101#define AFUD_NUM_CRS(val) EXTRACT_PPC_BITS(val, 32, 47)
102#define AFUD_MULTIMODE(val) EXTRACT_PPC_BIT(val, 48)
103#define AFUD_PUSH_BLOCK_TRANSFER(val) EXTRACT_PPC_BIT(val, 55)
104#define AFUD_DEDICATED_PROCESS(val) EXTRACT_PPC_BIT(val, 59)
105#define AFUD_AFU_DIRECTED(val) EXTRACT_PPC_BIT(val, 61)
106#define AFUD_TIME_SLICED(val) EXTRACT_PPC_BIT(val, 63)
107#define AFUD_READ_CR(afu) AFUD_READ(afu, 0x20)
108#define AFUD_CR_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
109#define AFUD_READ_CR_OFF(afu) AFUD_READ(afu, 0x28)
110#define AFUD_READ_PPPSA(afu) AFUD_READ(afu, 0x30)
111#define AFUD_PPPSA_PP(val) EXTRACT_PPC_BIT(val, 6)
112#define AFUD_PPPSA_PSA(val) EXTRACT_PPC_BIT(val, 7)
113#define AFUD_PPPSA_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
114#define AFUD_READ_PPPSA_OFF(afu) AFUD_READ(afu, 0x38)
115#define AFUD_READ_EB(afu) AFUD_READ(afu, 0x40)
116#define AFUD_EB_LEN(val) EXTRACT_PPC_BITS(val, 8, 63)
117#define AFUD_READ_EB_OFF(afu) AFUD_READ(afu, 0x48)
118
Vaishali Thakkarf47f9662015-07-19 22:53:52 +0530119static const struct pci_device_id cxl_pci_tbl[] = {
Ian Munsief204e0b2014-10-08 19:55:02 +1100120 { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0477), },
121 { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x044b), },
122 { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x04cf), },
Uma Krishnan68adb7b2015-12-07 16:03:32 -0600123 { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0601), },
Ian Munsief204e0b2014-10-08 19:55:02 +1100124 { PCI_DEVICE_CLASS(0x120000, ~0), },
125
126 { }
127};
128MODULE_DEVICE_TABLE(pci, cxl_pci_tbl);
129
130
131/*
132 * Mostly using these wrappers to avoid confusion:
133 * priv 1 is BAR2, while priv 2 is BAR0
134 */
135static inline resource_size_t p1_base(struct pci_dev *dev)
136{
137 return pci_resource_start(dev, 2);
138}
139
140static inline resource_size_t p1_size(struct pci_dev *dev)
141{
142 return pci_resource_len(dev, 2);
143}
144
145static inline resource_size_t p2_base(struct pci_dev *dev)
146{
147 return pci_resource_start(dev, 0);
148}
149
150static inline resource_size_t p2_size(struct pci_dev *dev)
151{
152 return pci_resource_len(dev, 0);
153}
154
155static int find_cxl_vsec(struct pci_dev *dev)
156{
157 int vsec = 0;
158 u16 val;
159
160 while ((vsec = pci_find_next_ext_capability(dev, vsec, PCI_EXT_CAP_ID_VNDR))) {
161 pci_read_config_word(dev, vsec + 0x4, &val);
162 if (val == CXL_PCI_VSEC_ID)
163 return vsec;
164 }
165 return 0;
166
167}
168
169static void dump_cxl_config_space(struct pci_dev *dev)
170{
171 int vsec;
172 u32 val;
173
174 dev_info(&dev->dev, "dump_cxl_config_space\n");
175
176 pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &val);
177 dev_info(&dev->dev, "BAR0: %#.8x\n", val);
178 pci_read_config_dword(dev, PCI_BASE_ADDRESS_1, &val);
179 dev_info(&dev->dev, "BAR1: %#.8x\n", val);
180 pci_read_config_dword(dev, PCI_BASE_ADDRESS_2, &val);
181 dev_info(&dev->dev, "BAR2: %#.8x\n", val);
182 pci_read_config_dword(dev, PCI_BASE_ADDRESS_3, &val);
183 dev_info(&dev->dev, "BAR3: %#.8x\n", val);
184 pci_read_config_dword(dev, PCI_BASE_ADDRESS_4, &val);
185 dev_info(&dev->dev, "BAR4: %#.8x\n", val);
186 pci_read_config_dword(dev, PCI_BASE_ADDRESS_5, &val);
187 dev_info(&dev->dev, "BAR5: %#.8x\n", val);
188
189 dev_info(&dev->dev, "p1 regs: %#llx, len: %#llx\n",
190 p1_base(dev), p1_size(dev));
191 dev_info(&dev->dev, "p2 regs: %#llx, len: %#llx\n",
Michael Neulingf2931062015-06-18 15:15:10 +1000192 p2_base(dev), p2_size(dev));
Ian Munsief204e0b2014-10-08 19:55:02 +1100193 dev_info(&dev->dev, "BAR 4/5: %#llx, len: %#llx\n",
194 pci_resource_start(dev, 4), pci_resource_len(dev, 4));
195
196 if (!(vsec = find_cxl_vsec(dev)))
197 return;
198
199#define show_reg(name, what) \
200 dev_info(&dev->dev, "cxl vsec: %30s: %#x\n", name, what)
201
202 pci_read_config_dword(dev, vsec + 0x0, &val);
203 show_reg("Cap ID", (val >> 0) & 0xffff);
204 show_reg("Cap Ver", (val >> 16) & 0xf);
205 show_reg("Next Cap Ptr", (val >> 20) & 0xfff);
206 pci_read_config_dword(dev, vsec + 0x4, &val);
207 show_reg("VSEC ID", (val >> 0) & 0xffff);
208 show_reg("VSEC Rev", (val >> 16) & 0xf);
209 show_reg("VSEC Length", (val >> 20) & 0xfff);
210 pci_read_config_dword(dev, vsec + 0x8, &val);
211 show_reg("Num AFUs", (val >> 0) & 0xff);
212 show_reg("Status", (val >> 8) & 0xff);
213 show_reg("Mode Control", (val >> 16) & 0xff);
214 show_reg("Reserved", (val >> 24) & 0xff);
215 pci_read_config_dword(dev, vsec + 0xc, &val);
216 show_reg("PSL Rev", (val >> 0) & 0xffff);
217 show_reg("CAIA Ver", (val >> 16) & 0xffff);
218 pci_read_config_dword(dev, vsec + 0x10, &val);
219 show_reg("Base Image Rev", (val >> 0) & 0xffff);
220 show_reg("Reserved", (val >> 16) & 0x0fff);
221 show_reg("Image Control", (val >> 28) & 0x3);
222 show_reg("Reserved", (val >> 30) & 0x1);
223 show_reg("Image Loaded", (val >> 31) & 0x1);
224
225 pci_read_config_dword(dev, vsec + 0x14, &val);
226 show_reg("Reserved", val);
227 pci_read_config_dword(dev, vsec + 0x18, &val);
228 show_reg("Reserved", val);
229 pci_read_config_dword(dev, vsec + 0x1c, &val);
230 show_reg("Reserved", val);
231
232 pci_read_config_dword(dev, vsec + 0x20, &val);
233 show_reg("AFU Descriptor Offset", val);
234 pci_read_config_dword(dev, vsec + 0x24, &val);
235 show_reg("AFU Descriptor Size", val);
236 pci_read_config_dword(dev, vsec + 0x28, &val);
237 show_reg("Problem State Offset", val);
238 pci_read_config_dword(dev, vsec + 0x2c, &val);
239 show_reg("Problem State Size", val);
240
241 pci_read_config_dword(dev, vsec + 0x30, &val);
242 show_reg("Reserved", val);
243 pci_read_config_dword(dev, vsec + 0x34, &val);
244 show_reg("Reserved", val);
245 pci_read_config_dword(dev, vsec + 0x38, &val);
246 show_reg("Reserved", val);
247 pci_read_config_dword(dev, vsec + 0x3c, &val);
248 show_reg("Reserved", val);
249
250 pci_read_config_dword(dev, vsec + 0x40, &val);
251 show_reg("PSL Programming Port", val);
252 pci_read_config_dword(dev, vsec + 0x44, &val);
253 show_reg("PSL Programming Control", val);
254
255 pci_read_config_dword(dev, vsec + 0x48, &val);
256 show_reg("Reserved", val);
257 pci_read_config_dword(dev, vsec + 0x4c, &val);
258 show_reg("Reserved", val);
259
260 pci_read_config_dword(dev, vsec + 0x50, &val);
261 show_reg("Flash Address Register", val);
262 pci_read_config_dword(dev, vsec + 0x54, &val);
263 show_reg("Flash Size Register", val);
264 pci_read_config_dword(dev, vsec + 0x58, &val);
265 show_reg("Flash Status/Control Register", val);
266 pci_read_config_dword(dev, vsec + 0x58, &val);
267 show_reg("Flash Data Port", val);
268
269#undef show_reg
270}
271
272static void dump_afu_descriptor(struct cxl_afu *afu)
273{
Michael Neulingbfcdc8f2015-05-27 16:07:06 +1000274 u64 val, afu_cr_num, afu_cr_off, afu_cr_len;
275 int i;
Ian Munsief204e0b2014-10-08 19:55:02 +1100276
277#define show_reg(name, what) \
278 dev_info(&afu->dev, "afu desc: %30s: %#llx\n", name, what)
279
280 val = AFUD_READ_INFO(afu);
281 show_reg("num_ints_per_process", AFUD_NUM_INTS_PER_PROC(val));
282 show_reg("num_of_processes", AFUD_NUM_PROCS(val));
283 show_reg("num_of_afu_CRs", AFUD_NUM_CRS(val));
284 show_reg("req_prog_mode", val & 0xffffULL);
Michael Neulingbfcdc8f2015-05-27 16:07:06 +1000285 afu_cr_num = AFUD_NUM_CRS(val);
Ian Munsief204e0b2014-10-08 19:55:02 +1100286
287 val = AFUD_READ(afu, 0x8);
288 show_reg("Reserved", val);
289 val = AFUD_READ(afu, 0x10);
290 show_reg("Reserved", val);
291 val = AFUD_READ(afu, 0x18);
292 show_reg("Reserved", val);
293
294 val = AFUD_READ_CR(afu);
295 show_reg("Reserved", (val >> (63-7)) & 0xff);
296 show_reg("AFU_CR_len", AFUD_CR_LEN(val));
Michael Neulingbfcdc8f2015-05-27 16:07:06 +1000297 afu_cr_len = AFUD_CR_LEN(val) * 256;
Ian Munsief204e0b2014-10-08 19:55:02 +1100298
299 val = AFUD_READ_CR_OFF(afu);
Michael Neulingbfcdc8f2015-05-27 16:07:06 +1000300 afu_cr_off = val;
Ian Munsief204e0b2014-10-08 19:55:02 +1100301 show_reg("AFU_CR_offset", val);
302
303 val = AFUD_READ_PPPSA(afu);
304 show_reg("PerProcessPSA_control", (val >> (63-7)) & 0xff);
305 show_reg("PerProcessPSA Length", AFUD_PPPSA_LEN(val));
306
307 val = AFUD_READ_PPPSA_OFF(afu);
308 show_reg("PerProcessPSA_offset", val);
309
310 val = AFUD_READ_EB(afu);
311 show_reg("Reserved", (val >> (63-7)) & 0xff);
312 show_reg("AFU_EB_len", AFUD_EB_LEN(val));
313
314 val = AFUD_READ_EB_OFF(afu);
315 show_reg("AFU_EB_offset", val);
316
Michael Neulingbfcdc8f2015-05-27 16:07:06 +1000317 for (i = 0; i < afu_cr_num; i++) {
318 val = AFUD_READ_LE(afu, afu_cr_off + i * afu_cr_len);
319 show_reg("CR Vendor", val & 0xffff);
320 show_reg("CR Device", (val >> 16) & 0xffff);
321 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100322#undef show_reg
323}
324
325static int init_implementation_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
326{
327 struct device_node *np;
328 const __be32 *prop;
329 u64 psl_dsnctl;
330 u64 chipid;
331
Ryan Grimm6f963ec2015-01-28 20:16:04 -0600332 if (!(np = pnv_pci_get_phb_node(dev)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100333 return -ENODEV;
334
335 while (np && !(prop = of_get_property(np, "ibm,chip-id", NULL)))
336 np = of_get_next_parent(np);
337 if (!np)
338 return -ENODEV;
339 chipid = be32_to_cpup(prop);
340 of_node_put(np);
341
342 /* Tell PSL where to route data to */
343 psl_dsnctl = 0x02E8900002000000ULL | (chipid << (63-5));
344 cxl_p1_write(adapter, CXL_PSL_DSNDCTL, psl_dsnctl);
345 cxl_p1_write(adapter, CXL_PSL_RESLCKTO, 0x20000000200ULL);
346 /* snoop write mask */
347 cxl_p1_write(adapter, CXL_PSL_SNWRALLOC, 0x00000000FFFFFFFFULL);
348 /* set fir_accum */
349 cxl_p1_write(adapter, CXL_PSL_FIR_CNTL, 0x0800000000000000ULL);
350 /* for debugging with trace arrays */
351 cxl_p1_write(adapter, CXL_PSL_TRACE, 0x0000FF7C00000000ULL);
352
353 return 0;
354}
355
Philippe Bergheaud390fd592015-08-28 09:37:36 +0200356#define TBSYNC_CNT(n) (((u64)n & 0x7) << (63-6))
357#define _2048_250MHZ_CYCLES 1
358
359static int cxl_setup_psl_timebase(struct cxl *adapter, struct pci_dev *dev)
360{
361 u64 psl_tb;
362 int delta;
363 unsigned int retry = 0;
364 struct device_node *np;
365
366 if (!(np = pnv_pci_get_phb_node(dev)))
367 return -ENODEV;
368
369 /* Do not fail when CAPP timebase sync is not supported by OPAL */
370 of_node_get(np);
371 if (! of_get_property(np, "ibm,capp-timebase-sync", NULL)) {
372 of_node_put(np);
373 pr_err("PSL: Timebase sync: OPAL support missing\n");
374 return 0;
375 }
376 of_node_put(np);
377
378 /*
379 * Setup PSL Timebase Control and Status register
380 * with the recommended Timebase Sync Count value
381 */
382 cxl_p1_write(adapter, CXL_PSL_TB_CTLSTAT,
383 TBSYNC_CNT(2 * _2048_250MHZ_CYCLES));
384
385 /* Enable PSL Timebase */
386 cxl_p1_write(adapter, CXL_PSL_Control, 0x0000000000000000);
387 cxl_p1_write(adapter, CXL_PSL_Control, CXL_PSL_Control_tb);
388
389 /* Wait until CORE TB and PSL TB difference <= 16usecs */
390 do {
391 msleep(1);
392 if (retry++ > 5) {
393 pr_err("PSL: Timebase sync: giving up!\n");
394 return -EIO;
395 }
396 psl_tb = cxl_p1_read(adapter, CXL_PSL_Timebase);
397 delta = mftb() - psl_tb;
398 if (delta < 0)
399 delta = -delta;
400 } while (cputime_to_usecs(delta) > 16);
401
402 return 0;
403}
404
Ian Munsief204e0b2014-10-08 19:55:02 +1100405static int init_implementation_afu_regs(struct cxl_afu *afu)
406{
407 /* read/write masks for this slice */
408 cxl_p1n_write(afu, CXL_PSL_APCALLOC_A, 0xFFFFFFFEFEFEFEFEULL);
409 /* APC read/write masks for this slice */
410 cxl_p1n_write(afu, CXL_PSL_COALLOC_A, 0xFF000000FEFEFEFEULL);
411 /* for debugging with trace arrays */
412 cxl_p1n_write(afu, CXL_PSL_SLICE_TRACE, 0x0000FFFF00000000ULL);
Ian Munsied6a6af22014-12-08 19:17:59 +1100413 cxl_p1n_write(afu, CXL_PSL_RXCTL_A, CXL_PSL_RXCTL_AFUHP_4S);
Ian Munsief204e0b2014-10-08 19:55:02 +1100414
415 return 0;
416}
417
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100418int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq,
419 unsigned int virq)
Ian Munsief204e0b2014-10-08 19:55:02 +1100420{
421 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
422
423 return pnv_cxl_ioda_msi_setup(dev, hwirq, virq);
424}
425
Ryan Grimm4beb5422015-01-19 11:52:48 -0600426int cxl_update_image_control(struct cxl *adapter)
427{
428 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
429 int rc;
430 int vsec;
431 u8 image_state;
432
433 if (!(vsec = find_cxl_vsec(dev))) {
434 dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
435 return -ENODEV;
436 }
437
438 if ((rc = CXL_READ_VSEC_IMAGE_STATE(dev, vsec, &image_state))) {
439 dev_err(&dev->dev, "failed to read image state: %i\n", rc);
440 return rc;
441 }
442
443 if (adapter->perst_loads_image)
444 image_state |= CXL_VSEC_PERST_LOADS_IMAGE;
445 else
446 image_state &= ~CXL_VSEC_PERST_LOADS_IMAGE;
447
448 if (adapter->perst_select_user)
449 image_state |= CXL_VSEC_PERST_SELECT_USER;
450 else
451 image_state &= ~CXL_VSEC_PERST_SELECT_USER;
452
453 if ((rc = CXL_WRITE_VSEC_IMAGE_STATE(dev, vsec, image_state))) {
454 dev_err(&dev->dev, "failed to update image control: %i\n", rc);
455 return rc;
456 }
457
458 return 0;
459}
460
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100461int cxl_pci_alloc_one_irq(struct cxl *adapter)
Ian Munsief204e0b2014-10-08 19:55:02 +1100462{
463 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
464
465 return pnv_cxl_alloc_hwirqs(dev, 1);
466}
467
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100468void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq)
Ian Munsief204e0b2014-10-08 19:55:02 +1100469{
470 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
471
472 return pnv_cxl_release_hwirqs(dev, hwirq, 1);
473}
474
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100475int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
476 struct cxl *adapter, unsigned int num)
Ian Munsief204e0b2014-10-08 19:55:02 +1100477{
478 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
479
480 return pnv_cxl_alloc_hwirq_ranges(irqs, dev, num);
481}
482
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100483void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs,
484 struct cxl *adapter)
Ian Munsief204e0b2014-10-08 19:55:02 +1100485{
486 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
487
488 pnv_cxl_release_hwirq_ranges(irqs, dev);
489}
490
491static int setup_cxl_bars(struct pci_dev *dev)
492{
493 /* Safety check in case we get backported to < 3.17 without M64 */
494 if ((p1_base(dev) < 0x100000000ULL) ||
495 (p2_base(dev) < 0x100000000ULL)) {
496 dev_err(&dev->dev, "ABORTING: M32 BAR assignment incompatible with CXL\n");
497 return -ENODEV;
498 }
499
500 /*
501 * BAR 4/5 has a special meaning for CXL and must be programmed with a
502 * special value corresponding to the CXL protocol address range.
503 * For POWER 8 that means bits 48:49 must be set to 10
504 */
505 pci_write_config_dword(dev, PCI_BASE_ADDRESS_4, 0x00000000);
506 pci_write_config_dword(dev, PCI_BASE_ADDRESS_5, 0x00020000);
507
508 return 0;
509}
510
511/* pciex node: ibm,opal-m64-window = <0x3d058 0x0 0x3d058 0x0 0x8 0x0>; */
512static int switch_card_to_cxl(struct pci_dev *dev)
513{
514 int vsec;
515 u8 val;
516 int rc;
517
518 dev_info(&dev->dev, "switch card to CXL\n");
519
520 if (!(vsec = find_cxl_vsec(dev))) {
521 dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
522 return -ENODEV;
523 }
524
525 if ((rc = CXL_READ_VSEC_MODE_CONTROL(dev, vsec, &val))) {
526 dev_err(&dev->dev, "failed to read current mode control: %i", rc);
527 return rc;
528 }
529 val &= ~CXL_VSEC_PROTOCOL_MASK;
530 val |= CXL_VSEC_PROTOCOL_256TB | CXL_VSEC_PROTOCOL_ENABLE;
531 if ((rc = CXL_WRITE_VSEC_MODE_CONTROL(dev, vsec, val))) {
532 dev_err(&dev->dev, "failed to enable CXL protocol: %i", rc);
533 return rc;
534 }
535 /*
536 * The CAIA spec (v0.12 11.6 Bi-modal Device Support) states
537 * we must wait 100ms after this mode switch before touching
538 * PCIe config space.
539 */
540 msleep(100);
541
542 return 0;
543}
544
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100545static int pci_map_slice_regs(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
Ian Munsief204e0b2014-10-08 19:55:02 +1100546{
547 u64 p1n_base, p2n_base, afu_desc;
548 const u64 p1n_size = 0x100;
549 const u64 p2n_size = 0x1000;
550
551 p1n_base = p1_base(dev) + 0x10000 + (afu->slice * p1n_size);
552 p2n_base = p2_base(dev) + (afu->slice * p2n_size);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100553 afu->psn_phys = p2_base(dev) + (adapter->native->ps_off + (afu->slice * adapter->ps_size));
554 afu_desc = p2_base(dev) + adapter->native->afu_desc_off + (afu->slice * adapter->native->afu_desc_size);
Ian Munsief204e0b2014-10-08 19:55:02 +1100555
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100556 if (!(afu->native->p1n_mmio = ioremap(p1n_base, p1n_size)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100557 goto err;
558 if (!(afu->p2n_mmio = ioremap(p2n_base, p2n_size)))
559 goto err1;
560 if (afu_desc) {
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100561 if (!(afu->native->afu_desc_mmio = ioremap(afu_desc, adapter->native->afu_desc_size)))
Ian Munsief204e0b2014-10-08 19:55:02 +1100562 goto err2;
563 }
564
565 return 0;
566err2:
567 iounmap(afu->p2n_mmio);
568err1:
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100569 iounmap(afu->native->p1n_mmio);
Ian Munsief204e0b2014-10-08 19:55:02 +1100570err:
571 dev_err(&afu->dev, "Error mapping AFU MMIO regions\n");
572 return -ENOMEM;
573}
574
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100575static void pci_unmap_slice_regs(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100576{
Daniel Axtens575e6982015-08-14 17:41:21 +1000577 if (afu->p2n_mmio) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100578 iounmap(afu->p2n_mmio);
Daniel Axtens575e6982015-08-14 17:41:21 +1000579 afu->p2n_mmio = NULL;
580 }
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100581 if (afu->native->p1n_mmio) {
582 iounmap(afu->native->p1n_mmio);
583 afu->native->p1n_mmio = NULL;
Daniel Axtens575e6982015-08-14 17:41:21 +1000584 }
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100585 if (afu->native->afu_desc_mmio) {
586 iounmap(afu->native->afu_desc_mmio);
587 afu->native->afu_desc_mmio = NULL;
Daniel Axtens575e6982015-08-14 17:41:21 +1000588 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100589}
590
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100591void cxl_pci_release_afu(struct device *dev)
Ian Munsief204e0b2014-10-08 19:55:02 +1100592{
593 struct cxl_afu *afu = to_cxl_afu(dev);
594
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100595 pr_devel("%s\n", __func__);
Ian Munsief204e0b2014-10-08 19:55:02 +1100596
Johannes Thumshirnbd664f82015-07-09 09:39:42 +0200597 idr_destroy(&afu->contexts_idr);
Daniel Axtens051557722015-08-14 17:41:19 +1000598 cxl_release_spa(afu);
599
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100600 kfree(afu->native);
Ian Munsief204e0b2014-10-08 19:55:02 +1100601 kfree(afu);
602}
603
Ian Munsief204e0b2014-10-08 19:55:02 +1100604/* Expects AFU struct to have recently been zeroed out */
605static int cxl_read_afu_descriptor(struct cxl_afu *afu)
606{
607 u64 val;
608
609 val = AFUD_READ_INFO(afu);
610 afu->pp_irqs = AFUD_NUM_INTS_PER_PROC(val);
611 afu->max_procs_virtualised = AFUD_NUM_PROCS(val);
Ian Munsieb087e612015-02-04 19:09:01 +1100612 afu->crs_num = AFUD_NUM_CRS(val);
Ian Munsief204e0b2014-10-08 19:55:02 +1100613
614 if (AFUD_AFU_DIRECTED(val))
615 afu->modes_supported |= CXL_MODE_DIRECTED;
616 if (AFUD_DEDICATED_PROCESS(val))
617 afu->modes_supported |= CXL_MODE_DEDICATED;
618 if (AFUD_TIME_SLICED(val))
619 afu->modes_supported |= CXL_MODE_TIME_SLICED;
620
621 val = AFUD_READ_PPPSA(afu);
622 afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;
623 afu->psa = AFUD_PPPSA_PSA(val);
624 if ((afu->pp_psa = AFUD_PPPSA_PP(val)))
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100625 afu->native->pp_offset = AFUD_READ_PPPSA_OFF(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100626
Ian Munsieb087e612015-02-04 19:09:01 +1100627 val = AFUD_READ_CR(afu);
628 afu->crs_len = AFUD_CR_LEN(val) * 256;
629 afu->crs_offset = AFUD_READ_CR_OFF(afu);
630
Vaibhav Jaine36f6fe2015-05-22 10:56:05 +0530631
632 /* eb_len is in multiple of 4K */
633 afu->eb_len = AFUD_EB_LEN(AFUD_READ_EB(afu)) * 4096;
634 afu->eb_offset = AFUD_READ_EB_OFF(afu);
635
636 /* eb_off is 4K aligned so lower 12 bits are always zero */
637 if (EXTRACT_PPC_BITS(afu->eb_offset, 0, 11) != 0) {
638 dev_warn(&afu->dev,
639 "Invalid AFU error buffer offset %Lx\n",
640 afu->eb_offset);
641 dev_info(&afu->dev,
642 "Ignoring AFU error buffer in the descriptor\n");
643 /* indicate that no afu buffer exists */
644 afu->eb_len = 0;
645 }
646
Ian Munsief204e0b2014-10-08 19:55:02 +1100647 return 0;
648}
649
650static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
651{
Frederic Barrat5be587b2016-03-04 12:26:28 +0100652 int i, rc;
653 u32 val;
Ian Munsie3d5be032015-02-04 19:09:02 +1100654
Ian Munsief204e0b2014-10-08 19:55:02 +1100655 if (afu->psa && afu->adapter->ps_size <
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100656 (afu->native->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100657 dev_err(&afu->dev, "per-process PSA can't fit inside the PSA!\n");
658 return -ENODEV;
659 }
660
661 if (afu->pp_psa && (afu->pp_size < PAGE_SIZE))
662 dev_warn(&afu->dev, "AFU uses < PAGE_SIZE per-process PSA!");
663
Ian Munsie3d5be032015-02-04 19:09:02 +1100664 for (i = 0; i < afu->crs_num; i++) {
Frederic Barrat5be587b2016-03-04 12:26:28 +0100665 rc = cxl_ops->afu_cr_read32(afu, i, 0, &val);
666 if (rc || val == 0) {
Ian Munsie3d5be032015-02-04 19:09:02 +1100667 dev_err(&afu->dev, "ABORTING: AFU configuration record %i is invalid\n", i);
668 return -EINVAL;
669 }
670 }
671
Ian Munsief204e0b2014-10-08 19:55:02 +1100672 return 0;
673}
674
675static int sanitise_afu_regs(struct cxl_afu *afu)
676{
677 u64 reg;
678
679 /*
680 * Clear out any regs that contain either an IVTE or address or may be
681 * waiting on an acknowledgement to try to be a bit safer as we bring
682 * it online
683 */
684 reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
685 if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
Rasmus Villemoesde369532015-06-11 13:27:52 +0200686 dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#016llx\n", reg);
Frederic Barrat5be587b2016-03-04 12:26:28 +0100687 if (cxl_ops->afu_reset(afu))
Ian Munsief204e0b2014-10-08 19:55:02 +1100688 return -EIO;
689 if (cxl_afu_disable(afu))
690 return -EIO;
691 if (cxl_psl_purge(afu))
692 return -EIO;
693 }
694 cxl_p1n_write(afu, CXL_PSL_SPAP_An, 0x0000000000000000);
695 cxl_p1n_write(afu, CXL_PSL_IVTE_Limit_An, 0x0000000000000000);
696 cxl_p1n_write(afu, CXL_PSL_IVTE_Offset_An, 0x0000000000000000);
697 cxl_p1n_write(afu, CXL_PSL_AMBAR_An, 0x0000000000000000);
698 cxl_p1n_write(afu, CXL_PSL_SPOffset_An, 0x0000000000000000);
699 cxl_p1n_write(afu, CXL_HAURP_An, 0x0000000000000000);
700 cxl_p2n_write(afu, CXL_CSRP_An, 0x0000000000000000);
701 cxl_p2n_write(afu, CXL_AURP1_An, 0x0000000000000000);
702 cxl_p2n_write(afu, CXL_AURP0_An, 0x0000000000000000);
703 cxl_p2n_write(afu, CXL_SSTP1_An, 0x0000000000000000);
704 cxl_p2n_write(afu, CXL_SSTP0_An, 0x0000000000000000);
705 reg = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
706 if (reg) {
Rasmus Villemoesde369532015-06-11 13:27:52 +0200707 dev_warn(&afu->dev, "AFU had pending DSISR: %#016llx\n", reg);
Ian Munsief204e0b2014-10-08 19:55:02 +1100708 if (reg & CXL_PSL_DSISR_TRANS)
709 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
710 else
711 cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
712 }
713 reg = cxl_p1n_read(afu, CXL_PSL_SERR_An);
714 if (reg) {
715 if (reg & ~0xffff)
Rasmus Villemoesde369532015-06-11 13:27:52 +0200716 dev_warn(&afu->dev, "AFU had pending SERR: %#016llx\n", reg);
Ian Munsief204e0b2014-10-08 19:55:02 +1100717 cxl_p1n_write(afu, CXL_PSL_SERR_An, reg & ~0xffff);
718 }
719 reg = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
720 if (reg) {
Rasmus Villemoesde369532015-06-11 13:27:52 +0200721 dev_warn(&afu->dev, "AFU had pending error status: %#016llx\n", reg);
Ian Munsief204e0b2014-10-08 19:55:02 +1100722 cxl_p2n_write(afu, CXL_PSL_ErrStat_An, reg);
723 }
724
725 return 0;
726}
727
Vaibhav Jaine36f6fe2015-05-22 10:56:05 +0530728#define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
729/*
730 * afu_eb_read:
731 * Called from sysfs and reads the afu error info buffer. The h/w only supports
732 * 4/8 bytes aligned access. So in case the requested offset/count arent 8 byte
733 * aligned the function uses a bounce buffer which can be max PAGE_SIZE.
734 */
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100735ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
Vaibhav Jaine36f6fe2015-05-22 10:56:05 +0530736 loff_t off, size_t count)
737{
738 loff_t aligned_start, aligned_end;
739 size_t aligned_length;
740 void *tbuf;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100741 const void __iomem *ebuf = afu->native->afu_desc_mmio + afu->eb_offset;
Vaibhav Jaine36f6fe2015-05-22 10:56:05 +0530742
743 if (count == 0 || off < 0 || (size_t)off >= afu->eb_len)
744 return 0;
745
746 /* calculate aligned read window */
747 count = min((size_t)(afu->eb_len - off), count);
748 aligned_start = round_down(off, 8);
749 aligned_end = round_up(off + count, 8);
750 aligned_length = aligned_end - aligned_start;
751
752 /* max we can copy in one read is PAGE_SIZE */
753 if (aligned_length > ERR_BUFF_MAX_COPY_SIZE) {
754 aligned_length = ERR_BUFF_MAX_COPY_SIZE;
755 count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
756 }
757
758 /* use bounce buffer for copy */
759 tbuf = (void *)__get_free_page(GFP_TEMPORARY);
760 if (!tbuf)
761 return -ENOMEM;
762
763 /* perform aligned read from the mmio region */
764 memcpy_fromio(tbuf, ebuf + aligned_start, aligned_length);
765 memcpy(buf, tbuf + (off & 0x7), count);
766
767 free_page((unsigned long)tbuf);
768
769 return count;
770}
771
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100772static int pci_configure_afu(struct cxl_afu *afu, struct cxl *adapter, struct pci_dev *dev)
Ian Munsief204e0b2014-10-08 19:55:02 +1100773{
Ian Munsief204e0b2014-10-08 19:55:02 +1100774 int rc;
775
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100776 if ((rc = pci_map_slice_regs(afu, adapter, dev)))
Daniel Axtensd76427b2015-08-14 17:41:23 +1000777 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100778
779 if ((rc = sanitise_afu_regs(afu)))
Daniel Axtensd76427b2015-08-14 17:41:23 +1000780 goto err1;
Ian Munsief204e0b2014-10-08 19:55:02 +1100781
782 /* We need to reset the AFU before we can read the AFU descriptor */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100783 if ((rc = cxl_ops->afu_reset(afu)))
Daniel Axtensd76427b2015-08-14 17:41:23 +1000784 goto err1;
Ian Munsief204e0b2014-10-08 19:55:02 +1100785
786 if (cxl_verbose)
787 dump_afu_descriptor(afu);
788
789 if ((rc = cxl_read_afu_descriptor(afu)))
Daniel Axtensd76427b2015-08-14 17:41:23 +1000790 goto err1;
Ian Munsief204e0b2014-10-08 19:55:02 +1100791
792 if ((rc = cxl_afu_descriptor_looks_ok(afu)))
Daniel Axtensd76427b2015-08-14 17:41:23 +1000793 goto err1;
Ian Munsief204e0b2014-10-08 19:55:02 +1100794
795 if ((rc = init_implementation_afu_regs(afu)))
Daniel Axtensd76427b2015-08-14 17:41:23 +1000796 goto err1;
Ian Munsief204e0b2014-10-08 19:55:02 +1100797
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100798 if ((rc = cxl_native_register_serr_irq(afu)))
Daniel Axtensd76427b2015-08-14 17:41:23 +1000799 goto err1;
Ian Munsief204e0b2014-10-08 19:55:02 +1100800
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100801 if ((rc = cxl_native_register_psl_irq(afu)))
Daniel Axtensd76427b2015-08-14 17:41:23 +1000802 goto err2;
803
804 return 0;
805
806err2:
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100807 cxl_native_release_serr_irq(afu);
Daniel Axtensd76427b2015-08-14 17:41:23 +1000808err1:
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100809 pci_unmap_slice_regs(afu);
Daniel Axtensd76427b2015-08-14 17:41:23 +1000810 return rc;
811}
812
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100813static void pci_deconfigure_afu(struct cxl_afu *afu)
Daniel Axtensd76427b2015-08-14 17:41:23 +1000814{
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100815 cxl_native_release_psl_irq(afu);
816 cxl_native_release_serr_irq(afu);
817 pci_unmap_slice_regs(afu);
Daniel Axtensd76427b2015-08-14 17:41:23 +1000818}
819
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100820static int pci_init_afu(struct cxl *adapter, int slice, struct pci_dev *dev)
Daniel Axtensd76427b2015-08-14 17:41:23 +1000821{
822 struct cxl_afu *afu;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100823 int rc = -ENOMEM;
Daniel Axtensd76427b2015-08-14 17:41:23 +1000824
825 afu = cxl_alloc_afu(adapter, slice);
826 if (!afu)
827 return -ENOMEM;
828
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100829 afu->native = kzalloc(sizeof(struct cxl_afu_native), GFP_KERNEL);
830 if (!afu->native)
831 goto err_free_afu;
832
833 mutex_init(&afu->native->spa_mutex);
834
Daniel Axtensd76427b2015-08-14 17:41:23 +1000835 rc = dev_set_name(&afu->dev, "afu%i.%i", adapter->adapter_num, slice);
836 if (rc)
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100837 goto err_free_native;
Daniel Axtensd76427b2015-08-14 17:41:23 +1000838
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100839 rc = pci_configure_afu(afu, adapter, dev);
Daniel Axtensd76427b2015-08-14 17:41:23 +1000840 if (rc)
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100841 goto err_free_native;
Ian Munsief204e0b2014-10-08 19:55:02 +1100842
843 /* Don't care if this fails */
844 cxl_debugfs_afu_add(afu);
845
846 /*
847 * After we call this function we must not free the afu directly, even
848 * if it returns an error!
849 */
850 if ((rc = cxl_register_afu(afu)))
851 goto err_put1;
852
853 if ((rc = cxl_sysfs_afu_add(afu)))
854 goto err_put1;
855
Ian Munsief204e0b2014-10-08 19:55:02 +1100856 adapter->afu[afu->slice] = afu;
857
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000858 if ((rc = cxl_pci_vphb_add(afu)))
859 dev_info(&afu->dev, "Can't register vPHB\n");
860
Ian Munsief204e0b2014-10-08 19:55:02 +1100861 return 0;
862
Ian Munsief204e0b2014-10-08 19:55:02 +1100863err_put1:
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100864 pci_deconfigure_afu(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100865 cxl_debugfs_afu_remove(afu);
Daniel Axtensd76427b2015-08-14 17:41:23 +1000866 device_unregister(&afu->dev);
Ian Munsief204e0b2014-10-08 19:55:02 +1100867 return rc;
Daniel Axtensd76427b2015-08-14 17:41:23 +1000868
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100869err_free_native:
870 kfree(afu->native);
871err_free_afu:
Daniel Axtensd76427b2015-08-14 17:41:23 +1000872 kfree(afu);
873 return rc;
874
Ian Munsief204e0b2014-10-08 19:55:02 +1100875}
876
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100877static void cxl_pci_remove_afu(struct cxl_afu *afu)
Ian Munsief204e0b2014-10-08 19:55:02 +1100878{
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100879 pr_devel("%s\n", __func__);
Ian Munsief204e0b2014-10-08 19:55:02 +1100880
881 if (!afu)
882 return;
883
884 cxl_sysfs_afu_remove(afu);
885 cxl_debugfs_afu_remove(afu);
886
887 spin_lock(&afu->adapter->afu_list_lock);
888 afu->adapter->afu[afu->slice] = NULL;
889 spin_unlock(&afu->adapter->afu_list_lock);
890
891 cxl_context_detach_all(afu);
Frederic Barrat5be587b2016-03-04 12:26:28 +0100892 cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
Ian Munsief204e0b2014-10-08 19:55:02 +1100893
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100894 pci_deconfigure_afu(afu);
Ian Munsief204e0b2014-10-08 19:55:02 +1100895 device_unregister(&afu->dev);
896}
897
Frederic Barrat2b04cf32016-03-04 12:26:29 +0100898int cxl_pci_reset(struct cxl *adapter)
Ryan Grimm62fa19d2015-01-19 11:52:51 -0600899{
900 struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
901 int rc;
Ryan Grimm62fa19d2015-01-19 11:52:51 -0600902
Daniel Axtens13e68d82015-08-14 17:41:25 +1000903 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;
907 }
908
Ryan Grimm62fa19d2015-01-19 11:52:51 -0600909 dev_info(&dev->dev, "CXL reset\n");
910
Ryan Grimm62fa19d2015-01-19 11:52:51 -0600911 /* 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;
917 }
918
Ryan Grimm62fa19d2015-01-19 11:52:51 -0600919 return rc;
920}
Ian Munsief204e0b2014-10-08 19:55:02 +1100921
922static int cxl_map_adapter_regs(struct cxl *adapter, struct pci_dev *dev)
923{
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;
928
Rasmus Villemoesde369532015-06-11 13:27:52 +0200929 pr_devel("cxl_map_adapter_regs: p1: %#016llx %#llx, p2: %#016llx %#llx",
Ian Munsief204e0b2014-10-08 19:55:02 +1100930 p1_base(dev), p1_size(dev), p2_base(dev), p2_size(dev));
931
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100932 if (!(adapter->native->p1_mmio = ioremap(p1_base(dev), p1_size(dev))))
Ian Munsief204e0b2014-10-08 19:55:02 +1100933 goto err3;
934
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100935 if (!(adapter->native->p2_mmio = ioremap(p2_base(dev), p2_size(dev))))
Ian Munsief204e0b2014-10-08 19:55:02 +1100936 goto err4;
937
938 return 0;
939
940err4:
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100941 iounmap(adapter->native->p1_mmio);
942 adapter->native->p1_mmio = NULL;
Ian Munsief204e0b2014-10-08 19:55:02 +1100943err3:
944 pci_release_region(dev, 0);
945err2:
946 pci_release_region(dev, 2);
947err1:
948 return -ENOMEM;
949}
950
951static void cxl_unmap_adapter_regs(struct cxl *adapter)
952{
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100953 if (adapter->native->p1_mmio) {
954 iounmap(adapter->native->p1_mmio);
955 adapter->native->p1_mmio = NULL;
Daniel Axtens575e6982015-08-14 17:41:21 +1000956 pci_release_region(to_pci_dev(adapter->dev.parent), 2);
957 }
Christophe Lombardcbffa3a2016-03-04 12:26:35 +0100958 if (adapter->native->p2_mmio) {
959 iounmap(adapter->native->p2_mmio);
960 adapter->native->p2_mmio = NULL;
Daniel Axtens575e6982015-08-14 17:41:21 +1000961 pci_release_region(to_pci_dev(adapter->dev.parent), 0);
962 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100963}
964
965static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev)
966{
967 int vsec;
968 u32 afu_desc_off, afu_desc_size;
969 u32 ps_off, ps_size;
970 u16 vseclen;
971 u8 image_state;
972
973 if (!(vsec = find_cxl_vsec(dev))) {
Ian Munsiebee30c72015-05-27 16:07:04 +1000974 dev_err(&dev->dev, "ABORTING: CXL VSEC not found!\n");
Ian Munsief204e0b2014-10-08 19:55:02 +1100975 return -ENODEV;
976 }
977
978 CXL_READ_VSEC_LENGTH(dev, vsec, &vseclen);
979 if (vseclen < CXL_VSEC_MIN_SIZE) {
Ian Munsiebee30c72015-05-27 16:07:04 +1000980 dev_err(&dev->dev, "ABORTING: CXL VSEC too short\n");
Ian Munsief204e0b2014-10-08 19:55:02 +1100981 return -EINVAL;
982 }
983
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);
Ryan Grimm4beb5422015-01-19 11:52:48 -0600991 adapter->perst_select_user = !!(image_state & CXL_VSEC_USER_IMAGE_LOADED);
Ian Munsief204e0b2014-10-08 19:55:02 +1100992
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);
998
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 ;-) */
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001001 adapter->native->ps_off = ps_off * 64 * 1024;
Ian Munsief204e0b2014-10-08 19:55:02 +11001002 adapter->ps_size = ps_size * 64 * 1024;
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001003 adapter->native->afu_desc_off = afu_desc_off * 64 * 1024;
1004 adapter->native->afu_desc_size = afu_desc_size * 64 * 1024;
Ian Munsief204e0b2014-10-08 19:55:02 +11001005
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;
1008
1009 return 0;
1010}
1011
Philippe Bergheaudd79e6802015-10-02 15:23:33 +10001012/*
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.
1016 *
1017 * The upper nibble of the PSL revision is used to distinguish between
1018 * different cards. The affected ones have it set to 0.
1019 */
1020static void cxl_fixup_malformed_tlp(struct cxl *adapter, struct pci_dev *dev)
1021{
1022 int aer;
1023 u32 data;
1024
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);
1036}
1037
Ian Munsief204e0b2014-10-08 19:55:02 +11001038static int cxl_vsec_looks_ok(struct cxl *adapter, struct pci_dev *dev)
1039{
1040 if (adapter->vsec_status & CXL_STATUS_SECOND_PORT)
1041 return -EBUSY;
1042
1043 if (adapter->vsec_status & CXL_UNSUPPORTED_FEATURES) {
Ian Munsiebee30c72015-05-27 16:07:04 +10001044 dev_err(&dev->dev, "ABORTING: CXL requires unsupported features\n");
Ian Munsief204e0b2014-10-08 19:55:02 +11001045 return -EINVAL;
1046 }
1047
1048 if (!adapter->slices) {
1049 /* Once we support dynamic reprogramming we can use the card if
1050 * it supports loadable AFUs */
Ian Munsiebee30c72015-05-27 16:07:04 +10001051 dev_err(&dev->dev, "ABORTING: Device has no AFUs\n");
Ian Munsief204e0b2014-10-08 19:55:02 +11001052 return -EINVAL;
1053 }
1054
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001055 if (!adapter->native->afu_desc_off || !adapter->native->afu_desc_size) {
Ian Munsiebee30c72015-05-27 16:07:04 +10001056 dev_err(&dev->dev, "ABORTING: VSEC shows no AFU descriptors\n");
Ian Munsief204e0b2014-10-08 19:55:02 +11001057 return -EINVAL;
1058 }
1059
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001060 if (adapter->ps_size > p2_size(dev) - adapter->native->ps_off) {
Ian Munsiebee30c72015-05-27 16:07:04 +10001061 dev_err(&dev->dev, "ABORTING: Problem state size larger than "
Ian Munsief204e0b2014-10-08 19:55:02 +11001062 "available in BAR2: 0x%llx > 0x%llx\n",
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001063 adapter->ps_size, p2_size(dev) - adapter->native->ps_off);
Ian Munsief204e0b2014-10-08 19:55:02 +11001064 return -EINVAL;
1065 }
1066
1067 return 0;
1068}
1069
1070static void cxl_release_adapter(struct device *dev)
1071{
1072 struct cxl *adapter = to_cxl_adapter(dev);
1073
1074 pr_devel("cxl_release_adapter\n");
1075
Daniel Axtensc044c412015-08-14 17:41:22 +10001076 cxl_remove_adapter_nr(adapter);
1077
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001078 kfree(adapter->native);
Ian Munsief204e0b2014-10-08 19:55:02 +11001079 kfree(adapter);
1080}
1081
Philippe Bergheaud390fd592015-08-28 09:37:36 +02001082#define CXL_PSL_ErrIVTE_tberror (0x1ull << (63-31))
1083
Ian Munsief204e0b2014-10-08 19:55:02 +11001084static int sanitise_adapter_regs(struct cxl *adapter)
1085{
Philippe Bergheaud390fd592015-08-28 09:37:36 +02001086 /* Clear PSL tberror bit by writing 1 to it */
1087 cxl_p1_write(adapter, CXL_PSL_ErrIVTE, CXL_PSL_ErrIVTE_tberror);
Ian Munsief204e0b2014-10-08 19:55:02 +11001088 return cxl_tlb_slb_invalidate(adapter);
1089}
1090
Daniel Axtensc044c412015-08-14 17:41:22 +10001091/* This should contain *only* operations that can safely be done in
1092 * both creation and recovery.
1093 */
1094static int cxl_configure_adapter(struct cxl *adapter, struct pci_dev *dev)
Ian Munsief204e0b2014-10-08 19:55:02 +11001095{
Ian Munsief204e0b2014-10-08 19:55:02 +11001096 int rc;
1097
Daniel Axtensc044c412015-08-14 17:41:22 +10001098 adapter->dev.parent = &dev->dev;
1099 adapter->dev.release = cxl_release_adapter;
1100 pci_set_drvdata(dev, adapter);
Ian Munsief204e0b2014-10-08 19:55:02 +11001101
Daniel Axtensc044c412015-08-14 17:41:22 +10001102 rc = pci_enable_device(dev);
1103 if (rc) {
1104 dev_err(&dev->dev, "pci_enable_device failed: %i\n", rc);
1105 return rc;
1106 }
Ian Munsief204e0b2014-10-08 19:55:02 +11001107
Ian Munsiebee30c72015-05-27 16:07:04 +10001108 if ((rc = cxl_read_vsec(adapter, dev)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001109 return rc;
Ian Munsiebee30c72015-05-27 16:07:04 +10001110
1111 if ((rc = cxl_vsec_looks_ok(adapter, dev)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001112 return rc;
Ian Munsiebee30c72015-05-27 16:07:04 +10001113
Philippe Bergheaudd79e6802015-10-02 15:23:33 +10001114 cxl_fixup_malformed_tlp(adapter, dev);
1115
Ian Munsiebee30c72015-05-27 16:07:04 +10001116 if ((rc = setup_cxl_bars(dev)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001117 return rc;
Ian Munsiebee30c72015-05-27 16:07:04 +10001118
Ian Munsief204e0b2014-10-08 19:55:02 +11001119 if ((rc = switch_card_to_cxl(dev)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001120 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +11001121
Ryan Grimm4beb5422015-01-19 11:52:48 -06001122 if ((rc = cxl_update_image_control(adapter)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001123 return rc;
Ryan Grimm4beb5422015-01-19 11:52:48 -06001124
Ian Munsief204e0b2014-10-08 19:55:02 +11001125 if ((rc = cxl_map_adapter_regs(adapter, dev)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001126 return rc;
Ian Munsief204e0b2014-10-08 19:55:02 +11001127
1128 if ((rc = sanitise_adapter_regs(adapter)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001129 goto err;
Ian Munsief204e0b2014-10-08 19:55:02 +11001130
1131 if ((rc = init_implementation_adapter_regs(adapter, dev)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001132 goto err;
Ian Munsief204e0b2014-10-08 19:55:02 +11001133
Ryan Grimm1212aa12015-01-19 11:52:50 -06001134 if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_CAPI)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001135 goto err;
Ian Munsief204e0b2014-10-08 19:55:02 +11001136
Ryan Grimm1212aa12015-01-19 11:52:50 -06001137 /* If recovery happened, the last step is to turn on snooping.
1138 * In the non-recovery case this has no effect */
Daniel Axtensc044c412015-08-14 17:41:22 +10001139 if ((rc = pnv_phb_to_cxl_mode(dev, OPAL_PHB_CAPI_MODE_SNOOP_ON)))
1140 goto err;
Ryan Grimm1212aa12015-01-19 11:52:50 -06001141
Philippe Bergheaud390fd592015-08-28 09:37:36 +02001142 if ((rc = cxl_setup_psl_timebase(adapter, dev)))
1143 goto err;
1144
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001145 if ((rc = cxl_native_register_psl_err_irq(adapter)))
Daniel Axtensc044c412015-08-14 17:41:22 +10001146 goto err;
1147
1148 return 0;
1149
1150err:
1151 cxl_unmap_adapter_regs(adapter);
1152 return rc;
1153
1154}
1155
1156static void cxl_deconfigure_adapter(struct cxl *adapter)
1157{
1158 struct pci_dev *pdev = to_pci_dev(adapter->dev.parent);
1159
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001160 cxl_native_release_psl_err_irq(adapter);
Daniel Axtensc044c412015-08-14 17:41:22 +10001161 cxl_unmap_adapter_regs(adapter);
1162
1163 pci_disable_device(pdev);
1164}
1165
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001166static struct cxl *cxl_pci_init_adapter(struct pci_dev *dev)
Daniel Axtensc044c412015-08-14 17:41:22 +10001167{
1168 struct cxl *adapter;
1169 int rc;
1170
1171 adapter = cxl_alloc_adapter();
1172 if (!adapter)
1173 return ERR_PTR(-ENOMEM);
1174
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001175 adapter->native = kzalloc(sizeof(struct cxl_native), GFP_KERNEL);
1176 if (!adapter->native) {
1177 rc = -ENOMEM;
1178 goto err_release;
1179 }
1180
Daniel Axtensc044c412015-08-14 17:41:22 +10001181 /* Set defaults for parameters which need to persist over
1182 * configure/reconfigure
1183 */
1184 adapter->perst_loads_image = true;
Daniel Axtens13e68d82015-08-14 17:41:25 +10001185 adapter->perst_same_image = false;
Daniel Axtensc044c412015-08-14 17:41:22 +10001186
1187 rc = cxl_configure_adapter(adapter, dev);
1188 if (rc) {
1189 pci_disable_device(dev);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001190 goto err_release;
Daniel Axtensc044c412015-08-14 17:41:22 +10001191 }
Ian Munsief204e0b2014-10-08 19:55:02 +11001192
1193 /* Don't care if this one fails: */
1194 cxl_debugfs_adapter_add(adapter);
1195
1196 /*
1197 * After we call this function we must not free the adapter directly,
1198 * even if it returns an error!
1199 */
1200 if ((rc = cxl_register_adapter(adapter)))
1201 goto err_put1;
1202
1203 if ((rc = cxl_sysfs_adapter_add(adapter)))
1204 goto err_put1;
1205
1206 return adapter;
1207
1208err_put1:
Daniel Axtensc044c412015-08-14 17:41:22 +10001209 /* This should mirror cxl_remove_adapter, except without the
1210 * sysfs parts
1211 */
Ian Munsief204e0b2014-10-08 19:55:02 +11001212 cxl_debugfs_adapter_remove(adapter);
Daniel Axtensc044c412015-08-14 17:41:22 +10001213 cxl_deconfigure_adapter(adapter);
1214 device_unregister(&adapter->dev);
Ian Munsief204e0b2014-10-08 19:55:02 +11001215 return ERR_PTR(rc);
Christophe Lombardcbffa3a2016-03-04 12:26:35 +01001216
1217err_release:
1218 cxl_release_adapter(&adapter->dev);
1219 return ERR_PTR(rc);
Ian Munsief204e0b2014-10-08 19:55:02 +11001220}
1221
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001222static void cxl_pci_remove_adapter(struct cxl *adapter)
Ian Munsief204e0b2014-10-08 19:55:02 +11001223{
Daniel Axtensc044c412015-08-14 17:41:22 +10001224 pr_devel("cxl_remove_adapter\n");
Ian Munsief204e0b2014-10-08 19:55:02 +11001225
1226 cxl_sysfs_adapter_remove(adapter);
1227 cxl_debugfs_adapter_remove(adapter);
Daniel Axtensc044c412015-08-14 17:41:22 +10001228
1229 cxl_deconfigure_adapter(adapter);
Ian Munsief204e0b2014-10-08 19:55:02 +11001230
1231 device_unregister(&adapter->dev);
Ian Munsief204e0b2014-10-08 19:55:02 +11001232}
1233
1234static int cxl_probe(struct pci_dev *dev, const struct pci_device_id *id)
1235{
1236 struct cxl *adapter;
1237 int slice;
1238 int rc;
1239
Ian Munsief204e0b2014-10-08 19:55:02 +11001240 if (cxl_verbose)
1241 dump_cxl_config_space(dev);
1242
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001243 adapter = cxl_pci_init_adapter(dev);
Ian Munsief204e0b2014-10-08 19:55:02 +11001244 if (IS_ERR(adapter)) {
1245 dev_err(&dev->dev, "cxl_init_adapter failed: %li\n", PTR_ERR(adapter));
1246 return PTR_ERR(adapter);
1247 }
1248
1249 for (slice = 0; slice < adapter->slices; slice++) {
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001250 if ((rc = pci_init_afu(adapter, slice, dev))) {
Ian Munsief204e0b2014-10-08 19:55:02 +11001251 dev_err(&dev->dev, "AFU %i failed to initialise: %i\n", slice, rc);
Daniel Axtensd76427b2015-08-14 17:41:23 +10001252 continue;
1253 }
1254
1255 rc = cxl_afu_select_best_mode(adapter->afu[slice]);
1256 if (rc)
1257 dev_err(&dev->dev, "AFU %i failed to start: %i\n", slice, rc);
Ian Munsief204e0b2014-10-08 19:55:02 +11001258 }
1259
1260 return 0;
1261}
1262
1263static void cxl_remove(struct pci_dev *dev)
1264{
1265 struct cxl *adapter = pci_get_drvdata(dev);
Michael Neuling6f7f0b32015-05-27 16:07:18 +10001266 struct cxl_afu *afu;
1267 int i;
Ian Munsief204e0b2014-10-08 19:55:02 +11001268
Ian Munsief204e0b2014-10-08 19:55:02 +11001269 /*
1270 * Lock to prevent someone grabbing a ref through the adapter list as
1271 * we are removing it
1272 */
Michael Neuling6f7f0b32015-05-27 16:07:18 +10001273 for (i = 0; i < adapter->slices; i++) {
1274 afu = adapter->afu[i];
1275 cxl_pci_vphb_remove(afu);
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001276 cxl_pci_remove_afu(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +10001277 }
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001278 cxl_pci_remove_adapter(adapter);
Ian Munsief204e0b2014-10-08 19:55:02 +11001279}
1280
Daniel Axtens9e8df8a2015-08-14 17:41:26 +10001281static pci_ers_result_t cxl_vphb_error_detected(struct cxl_afu *afu,
1282 pci_channel_state_t state)
1283{
1284 struct pci_dev *afu_dev;
1285 pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
1286 pci_ers_result_t afu_result = PCI_ERS_RESULT_NEED_RESET;
1287
1288 /* There should only be one entry, but go through the list
1289 * anyway
1290 */
1291 list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
1292 if (!afu_dev->driver)
1293 continue;
1294
1295 afu_dev->error_state = state;
1296
1297 if (afu_dev->driver->err_handler)
1298 afu_result = afu_dev->driver->err_handler->error_detected(afu_dev,
1299 state);
1300 /* Disconnect trumps all, NONE trumps NEED_RESET */
1301 if (afu_result == PCI_ERS_RESULT_DISCONNECT)
1302 result = PCI_ERS_RESULT_DISCONNECT;
1303 else if ((afu_result == PCI_ERS_RESULT_NONE) &&
1304 (result == PCI_ERS_RESULT_NEED_RESET))
1305 result = PCI_ERS_RESULT_NONE;
1306 }
1307 return result;
1308}
1309
1310static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
1311 pci_channel_state_t state)
1312{
1313 struct cxl *adapter = pci_get_drvdata(pdev);
1314 struct cxl_afu *afu;
1315 pci_ers_result_t result = PCI_ERS_RESULT_NEED_RESET;
1316 int i;
1317
1318 /* At this point, we could still have an interrupt pending.
1319 * Let's try to get them out of the way before they do
1320 * anything we don't like.
1321 */
1322 schedule();
1323
1324 /* If we're permanently dead, give up. */
1325 if (state == pci_channel_io_perm_failure) {
1326 /* Tell the AFU drivers; but we don't care what they
1327 * say, we're going away.
1328 */
1329 for (i = 0; i < adapter->slices; i++) {
1330 afu = adapter->afu[i];
1331 cxl_vphb_error_detected(afu, state);
1332 }
1333 return PCI_ERS_RESULT_DISCONNECT;
1334 }
1335
1336 /* Are we reflashing?
1337 *
1338 * If we reflash, we could come back as something entirely
1339 * different, including a non-CAPI card. As such, by default
1340 * we don't participate in the process. We'll be unbound and
1341 * the slot re-probed. (TODO: check EEH doesn't blindly rebind
1342 * us!)
1343 *
1344 * However, this isn't the entire story: for reliablity
1345 * reasons, we usually want to reflash the FPGA on PERST in
1346 * order to get back to a more reliable known-good state.
1347 *
1348 * This causes us a bit of a problem: if we reflash we can't
1349 * trust that we'll come back the same - we could have a new
1350 * image and been PERSTed in order to load that
1351 * image. However, most of the time we actually *will* come
1352 * back the same - for example a regular EEH event.
1353 *
1354 * Therefore, we allow the user to assert that the image is
1355 * indeed the same and that we should continue on into EEH
1356 * anyway.
1357 */
1358 if (adapter->perst_loads_image && !adapter->perst_same_image) {
1359 /* TODO take the PHB out of CXL mode */
1360 dev_info(&pdev->dev, "reflashing, so opting out of EEH!\n");
1361 return PCI_ERS_RESULT_NONE;
1362 }
1363
1364 /*
1365 * At this point, we want to try to recover. We'll always
1366 * need a complete slot reset: we don't trust any other reset.
1367 *
1368 * Now, we go through each AFU:
1369 * - We send the driver, if bound, an error_detected callback.
1370 * We expect it to clean up, but it can also tell us to give
1371 * up and permanently detach the card. To simplify things, if
1372 * any bound AFU driver doesn't support EEH, we give up on EEH.
1373 *
1374 * - We detach all contexts associated with the AFU. This
1375 * does not free them, but puts them into a CLOSED state
1376 * which causes any the associated files to return useful
1377 * errors to userland. It also unmaps, but does not free,
1378 * any IRQs.
1379 *
1380 * - We clean up our side: releasing and unmapping resources we hold
1381 * so we can wire them up again when the hardware comes back up.
1382 *
1383 * Driver authors should note:
1384 *
1385 * - Any contexts you create in your kernel driver (except
1386 * those associated with anonymous file descriptors) are
1387 * your responsibility to free and recreate. Likewise with
1388 * any attached resources.
1389 *
1390 * - We will take responsibility for re-initialising the
1391 * device context (the one set up for you in
1392 * cxl_pci_enable_device_hook and accessed through
1393 * cxl_get_context). If you've attached IRQs or other
1394 * resources to it, they remains yours to free.
1395 *
1396 * You can call the same functions to release resources as you
1397 * normally would: we make sure that these functions continue
1398 * to work when the hardware is down.
1399 *
1400 * Two examples:
1401 *
1402 * 1) If you normally free all your resources at the end of
1403 * each request, or if you use anonymous FDs, your
1404 * error_detected callback can simply set a flag to tell
1405 * your driver not to start any new calls. You can then
1406 * clear the flag in the resume callback.
1407 *
1408 * 2) If you normally allocate your resources on startup:
1409 * * Set a flag in error_detected as above.
1410 * * Let CXL detach your contexts.
1411 * * In slot_reset, free the old resources and allocate new ones.
1412 * * In resume, clear the flag to allow things to start.
1413 */
1414 for (i = 0; i < adapter->slices; i++) {
1415 afu = adapter->afu[i];
1416
1417 result = cxl_vphb_error_detected(afu, state);
1418
1419 /* Only continue if everyone agrees on NEED_RESET */
1420 if (result != PCI_ERS_RESULT_NEED_RESET)
1421 return result;
1422
1423 cxl_context_detach_all(afu);
Frederic Barrat5be587b2016-03-04 12:26:28 +01001424 cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001425 pci_deconfigure_afu(afu);
Daniel Axtens9e8df8a2015-08-14 17:41:26 +10001426 }
1427 cxl_deconfigure_adapter(adapter);
1428
1429 return result;
1430}
1431
1432static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
1433{
1434 struct cxl *adapter = pci_get_drvdata(pdev);
1435 struct cxl_afu *afu;
1436 struct cxl_context *ctx;
1437 struct pci_dev *afu_dev;
1438 pci_ers_result_t afu_result = PCI_ERS_RESULT_RECOVERED;
1439 pci_ers_result_t result = PCI_ERS_RESULT_RECOVERED;
1440 int i;
1441
1442 if (cxl_configure_adapter(adapter, pdev))
1443 goto err;
1444
1445 for (i = 0; i < adapter->slices; i++) {
1446 afu = adapter->afu[i];
1447
Frederic Barrat2b04cf32016-03-04 12:26:29 +01001448 if (pci_configure_afu(afu, adapter, pdev))
Daniel Axtens9e8df8a2015-08-14 17:41:26 +10001449 goto err;
1450
1451 if (cxl_afu_select_best_mode(afu))
1452 goto err;
1453
1454 cxl_pci_vphb_reconfigure(afu);
1455
1456 list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
1457 /* Reset the device context.
1458 * TODO: make this less disruptive
1459 */
1460 ctx = cxl_get_context(afu_dev);
1461
1462 if (ctx && cxl_release_context(ctx))
1463 goto err;
1464
1465 ctx = cxl_dev_context_init(afu_dev);
1466 if (!ctx)
1467 goto err;
1468
1469 afu_dev->dev.archdata.cxl_ctx = ctx;
1470
Frederic Barrat5be587b2016-03-04 12:26:28 +01001471 if (cxl_ops->afu_check_and_enable(afu))
Daniel Axtens9e8df8a2015-08-14 17:41:26 +10001472 goto err;
1473
1474 afu_dev->error_state = pci_channel_io_normal;
1475
1476 /* If there's a driver attached, allow it to
1477 * chime in on recovery. Drivers should check
1478 * if everything has come back OK, but
1479 * shouldn't start new work until we call
1480 * their resume function.
1481 */
1482 if (!afu_dev->driver)
1483 continue;
1484
1485 if (afu_dev->driver->err_handler &&
1486 afu_dev->driver->err_handler->slot_reset)
1487 afu_result = afu_dev->driver->err_handler->slot_reset(afu_dev);
1488
1489 if (afu_result == PCI_ERS_RESULT_DISCONNECT)
1490 result = PCI_ERS_RESULT_DISCONNECT;
1491 }
1492 }
1493 return result;
1494
1495err:
1496 /* All the bits that happen in both error_detected and cxl_remove
1497 * should be idempotent, so we don't need to worry about leaving a mix
1498 * of unconfigured and reconfigured resources.
1499 */
1500 dev_err(&pdev->dev, "EEH recovery failed. Asking to be disconnected.\n");
1501 return PCI_ERS_RESULT_DISCONNECT;
1502}
1503
1504static void cxl_pci_resume(struct pci_dev *pdev)
1505{
1506 struct cxl *adapter = pci_get_drvdata(pdev);
1507 struct cxl_afu *afu;
1508 struct pci_dev *afu_dev;
1509 int i;
1510
1511 /* Everything is back now. Drivers should restart work now.
1512 * This is not the place to be checking if everything came back up
1513 * properly, because there's no return value: do that in slot_reset.
1514 */
1515 for (i = 0; i < adapter->slices; i++) {
1516 afu = adapter->afu[i];
1517
1518 list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
1519 if (afu_dev->driver && afu_dev->driver->err_handler &&
1520 afu_dev->driver->err_handler->resume)
1521 afu_dev->driver->err_handler->resume(afu_dev);
1522 }
1523 }
1524}
1525
1526static const struct pci_error_handlers cxl_err_handler = {
1527 .error_detected = cxl_pci_error_detected,
1528 .slot_reset = cxl_pci_slot_reset,
1529 .resume = cxl_pci_resume,
1530};
1531
Ian Munsief204e0b2014-10-08 19:55:02 +11001532struct pci_driver cxl_pci_driver = {
1533 .name = "cxl-pci",
1534 .id_table = cxl_pci_tbl,
1535 .probe = cxl_probe,
1536 .remove = cxl_remove,
Michael Neulingaa707752015-05-27 16:07:02 +10001537 .shutdown = cxl_remove,
Daniel Axtens9e8df8a2015-08-14 17:41:26 +10001538 .err_handler = &cxl_err_handler,
Ian Munsief204e0b2014-10-08 19:55:02 +11001539};