Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-2.6-microblaze.git] / drivers / bus / mhi / pci_generic.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * MHI PCI driver - MHI over PCI controller driver
4  *
5  * This module is a generic driver for registering MHI-over-PCI devices,
6  * such as PCIe QCOM modems.
7  *
8  * Copyright (C) 2020 Linaro Ltd <loic.poulain@linaro.org>
9  */
10
11 #include <linux/aer.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/mhi.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/timer.h>
19 #include <linux/workqueue.h>
20
21 #define MHI_PCI_DEFAULT_BAR_NUM 0
22
23 #define MHI_POST_RESET_DELAY_MS 500
24
25 #define HEALTH_CHECK_PERIOD (HZ * 2)
26
27 /**
28  * struct mhi_pci_dev_info - MHI PCI device specific information
29  * @config: MHI controller configuration
30  * @name: name of the PCI module
31  * @fw: firmware path (if any)
32  * @edl: emergency download mode firmware path (if any)
33  * @bar_num: PCI base address register to use for MHI MMIO register space
34  * @dma_data_width: DMA transfer word size (32 or 64 bits)
35  * @mru_default: default MRU size for MBIM network packets
36  * @sideband_wake: Devices using dedicated sideband GPIO for wakeup instead
37  *                 of inband wake support (such as sdx24)
38  */
39 struct mhi_pci_dev_info {
40         const struct mhi_controller_config *config;
41         const char *name;
42         const char *fw;
43         const char *edl;
44         unsigned int bar_num;
45         unsigned int dma_data_width;
46         unsigned int mru_default;
47         bool sideband_wake;
48 };
49
50 #define MHI_CHANNEL_CONFIG_UL(ch_num, ch_name, el_count, ev_ring) \
51         {                                               \
52                 .num = ch_num,                          \
53                 .name = ch_name,                        \
54                 .num_elements = el_count,               \
55                 .event_ring = ev_ring,                  \
56                 .dir = DMA_TO_DEVICE,                   \
57                 .ee_mask = BIT(MHI_EE_AMSS),            \
58                 .pollcfg = 0,                           \
59                 .doorbell = MHI_DB_BRST_DISABLE,        \
60                 .lpm_notify = false,                    \
61                 .offload_channel = false,               \
62                 .doorbell_mode_switch = false,          \
63         }                                               \
64
65 #define MHI_CHANNEL_CONFIG_DL(ch_num, ch_name, el_count, ev_ring) \
66         {                                               \
67                 .num = ch_num,                          \
68                 .name = ch_name,                        \
69                 .num_elements = el_count,               \
70                 .event_ring = ev_ring,                  \
71                 .dir = DMA_FROM_DEVICE,                 \
72                 .ee_mask = BIT(MHI_EE_AMSS),            \
73                 .pollcfg = 0,                           \
74                 .doorbell = MHI_DB_BRST_DISABLE,        \
75                 .lpm_notify = false,                    \
76                 .offload_channel = false,               \
77                 .doorbell_mode_switch = false,          \
78         }
79
80 #define MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(ch_num, ch_name, el_count, ev_ring) \
81         {                                               \
82                 .num = ch_num,                          \
83                 .name = ch_name,                        \
84                 .num_elements = el_count,               \
85                 .event_ring = ev_ring,                  \
86                 .dir = DMA_FROM_DEVICE,                 \
87                 .ee_mask = BIT(MHI_EE_AMSS),            \
88                 .pollcfg = 0,                           \
89                 .doorbell = MHI_DB_BRST_DISABLE,        \
90                 .lpm_notify = false,                    \
91                 .offload_channel = false,               \
92                 .doorbell_mode_switch = false,          \
93                 .auto_queue = true,                     \
94         }
95
96 #define MHI_EVENT_CONFIG_CTRL(ev_ring, el_count) \
97         {                                       \
98                 .num_elements = el_count,       \
99                 .irq_moderation_ms = 0,         \
100                 .irq = (ev_ring) + 1,           \
101                 .priority = 1,                  \
102                 .mode = MHI_DB_BRST_DISABLE,    \
103                 .data_type = MHI_ER_CTRL,       \
104                 .hardware_event = false,        \
105                 .client_managed = false,        \
106                 .offload_channel = false,       \
107         }
108
109 #define MHI_CHANNEL_CONFIG_HW_UL(ch_num, ch_name, el_count, ev_ring) \
110         {                                               \
111                 .num = ch_num,                          \
112                 .name = ch_name,                        \
113                 .num_elements = el_count,               \
114                 .event_ring = ev_ring,                  \
115                 .dir = DMA_TO_DEVICE,                   \
116                 .ee_mask = BIT(MHI_EE_AMSS),            \
117                 .pollcfg = 0,                           \
118                 .doorbell = MHI_DB_BRST_ENABLE, \
119                 .lpm_notify = false,                    \
120                 .offload_channel = false,               \
121                 .doorbell_mode_switch = true,           \
122         }                                               \
123
124 #define MHI_CHANNEL_CONFIG_HW_DL(ch_num, ch_name, el_count, ev_ring) \
125         {                                               \
126                 .num = ch_num,                          \
127                 .name = ch_name,                        \
128                 .num_elements = el_count,               \
129                 .event_ring = ev_ring,                  \
130                 .dir = DMA_FROM_DEVICE,                 \
131                 .ee_mask = BIT(MHI_EE_AMSS),            \
132                 .pollcfg = 0,                           \
133                 .doorbell = MHI_DB_BRST_ENABLE, \
134                 .lpm_notify = false,                    \
135                 .offload_channel = false,               \
136                 .doorbell_mode_switch = true,           \
137         }
138
139 #define MHI_CHANNEL_CONFIG_UL_SBL(ch_num, ch_name, el_count, ev_ring) \
140         {                                               \
141                 .num = ch_num,                          \
142                 .name = ch_name,                        \
143                 .num_elements = el_count,               \
144                 .event_ring = ev_ring,                  \
145                 .dir = DMA_TO_DEVICE,                   \
146                 .ee_mask = BIT(MHI_EE_SBL),             \
147                 .pollcfg = 0,                           \
148                 .doorbell = MHI_DB_BRST_DISABLE,        \
149                 .lpm_notify = false,                    \
150                 .offload_channel = false,               \
151                 .doorbell_mode_switch = false,          \
152         }                                               \
153
154 #define MHI_CHANNEL_CONFIG_DL_SBL(ch_num, ch_name, el_count, ev_ring) \
155         {                                               \
156                 .num = ch_num,                          \
157                 .name = ch_name,                        \
158                 .num_elements = el_count,               \
159                 .event_ring = ev_ring,                  \
160                 .dir = DMA_FROM_DEVICE,                 \
161                 .ee_mask = BIT(MHI_EE_SBL),             \
162                 .pollcfg = 0,                           \
163                 .doorbell = MHI_DB_BRST_DISABLE,        \
164                 .lpm_notify = false,                    \
165                 .offload_channel = false,               \
166                 .doorbell_mode_switch = false,          \
167         }
168
169 #define MHI_CHANNEL_CONFIG_UL_FP(ch_num, ch_name, el_count, ev_ring) \
170         {                                               \
171                 .num = ch_num,                          \
172                 .name = ch_name,                        \
173                 .num_elements = el_count,               \
174                 .event_ring = ev_ring,                  \
175                 .dir = DMA_TO_DEVICE,                   \
176                 .ee_mask = BIT(MHI_EE_FP),              \
177                 .pollcfg = 0,                           \
178                 .doorbell = MHI_DB_BRST_DISABLE,        \
179                 .lpm_notify = false,                    \
180                 .offload_channel = false,               \
181                 .doorbell_mode_switch = false,          \
182         }                                               \
183
184 #define MHI_CHANNEL_CONFIG_DL_FP(ch_num, ch_name, el_count, ev_ring) \
185         {                                               \
186                 .num = ch_num,                          \
187                 .name = ch_name,                        \
188                 .num_elements = el_count,               \
189                 .event_ring = ev_ring,                  \
190                 .dir = DMA_FROM_DEVICE,                 \
191                 .ee_mask = BIT(MHI_EE_FP),              \
192                 .pollcfg = 0,                           \
193                 .doorbell = MHI_DB_BRST_DISABLE,        \
194                 .lpm_notify = false,                    \
195                 .offload_channel = false,               \
196                 .doorbell_mode_switch = false,          \
197         }
198
199 #define MHI_EVENT_CONFIG_DATA(ev_ring, el_count) \
200         {                                       \
201                 .num_elements = el_count,       \
202                 .irq_moderation_ms = 5,         \
203                 .irq = (ev_ring) + 1,           \
204                 .priority = 1,                  \
205                 .mode = MHI_DB_BRST_DISABLE,    \
206                 .data_type = MHI_ER_DATA,       \
207                 .hardware_event = false,        \
208                 .client_managed = false,        \
209                 .offload_channel = false,       \
210         }
211
212 #define MHI_EVENT_CONFIG_HW_DATA(ev_ring, el_count, ch_num) \
213         {                                       \
214                 .num_elements = el_count,       \
215                 .irq_moderation_ms = 1,         \
216                 .irq = (ev_ring) + 1,           \
217                 .priority = 1,                  \
218                 .mode = MHI_DB_BRST_DISABLE,    \
219                 .data_type = MHI_ER_DATA,       \
220                 .hardware_event = true,         \
221                 .client_managed = false,        \
222                 .offload_channel = false,       \
223                 .channel = ch_num,              \
224         }
225
226 static const struct mhi_channel_config modem_qcom_v1_mhi_channels[] = {
227         MHI_CHANNEL_CONFIG_UL(4, "DIAG", 16, 1),
228         MHI_CHANNEL_CONFIG_DL(5, "DIAG", 16, 1),
229         MHI_CHANNEL_CONFIG_UL(12, "MBIM", 4, 0),
230         MHI_CHANNEL_CONFIG_DL(13, "MBIM", 4, 0),
231         MHI_CHANNEL_CONFIG_UL(14, "QMI", 4, 0),
232         MHI_CHANNEL_CONFIG_DL(15, "QMI", 4, 0),
233         MHI_CHANNEL_CONFIG_UL(20, "IPCR", 8, 0),
234         MHI_CHANNEL_CONFIG_DL_AUTOQUEUE(21, "IPCR", 8, 0),
235         MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0),
236         MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0),
237         MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0", 128, 2),
238         MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0", 128, 3),
239 };
240
241 static struct mhi_event_config modem_qcom_v1_mhi_events[] = {
242         /* first ring is control+data ring */
243         MHI_EVENT_CONFIG_CTRL(0, 64),
244         /* DIAG dedicated event ring */
245         MHI_EVENT_CONFIG_DATA(1, 128),
246         /* Hardware channels request dedicated hardware event rings */
247         MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
248         MHI_EVENT_CONFIG_HW_DATA(3, 2048, 101)
249 };
250
251 static const struct mhi_controller_config modem_qcom_v1_mhiv_config = {
252         .max_channels = 128,
253         .timeout_ms = 8000,
254         .num_channels = ARRAY_SIZE(modem_qcom_v1_mhi_channels),
255         .ch_cfg = modem_qcom_v1_mhi_channels,
256         .num_events = ARRAY_SIZE(modem_qcom_v1_mhi_events),
257         .event_cfg = modem_qcom_v1_mhi_events,
258 };
259
260 static const struct mhi_pci_dev_info mhi_qcom_sdx65_info = {
261         .name = "qcom-sdx65m",
262         .fw = "qcom/sdx65m/xbl.elf",
263         .edl = "qcom/sdx65m/edl.mbn",
264         .config = &modem_qcom_v1_mhiv_config,
265         .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
266         .dma_data_width = 32,
267         .sideband_wake = false,
268 };
269
270 static const struct mhi_pci_dev_info mhi_qcom_sdx55_info = {
271         .name = "qcom-sdx55m",
272         .fw = "qcom/sdx55m/sbl1.mbn",
273         .edl = "qcom/sdx55m/edl.mbn",
274         .config = &modem_qcom_v1_mhiv_config,
275         .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
276         .dma_data_width = 32,
277         .mru_default = 32768,
278         .sideband_wake = false,
279 };
280
281 static const struct mhi_pci_dev_info mhi_qcom_sdx24_info = {
282         .name = "qcom-sdx24",
283         .edl = "qcom/prog_firehose_sdx24.mbn",
284         .config = &modem_qcom_v1_mhiv_config,
285         .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
286         .dma_data_width = 32,
287         .sideband_wake = true,
288 };
289
290 static const struct mhi_channel_config mhi_quectel_em1xx_channels[] = {
291         MHI_CHANNEL_CONFIG_UL(0, "NMEA", 32, 0),
292         MHI_CHANNEL_CONFIG_DL(1, "NMEA", 32, 0),
293         MHI_CHANNEL_CONFIG_UL_SBL(2, "SAHARA", 32, 0),
294         MHI_CHANNEL_CONFIG_DL_SBL(3, "SAHARA", 32, 0),
295         MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1),
296         MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1),
297         MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0),
298         MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0),
299         MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
300         MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
301         /* The EDL firmware is a flash-programmer exposing firehose protocol */
302         MHI_CHANNEL_CONFIG_UL_FP(34, "FIREHOSE", 32, 0),
303         MHI_CHANNEL_CONFIG_DL_FP(35, "FIREHOSE", 32, 0),
304         MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2),
305         MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3),
306 };
307
308 static struct mhi_event_config mhi_quectel_em1xx_events[] = {
309         MHI_EVENT_CONFIG_CTRL(0, 128),
310         MHI_EVENT_CONFIG_DATA(1, 128),
311         MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
312         MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101)
313 };
314
315 static const struct mhi_controller_config modem_quectel_em1xx_config = {
316         .max_channels = 128,
317         .timeout_ms = 20000,
318         .num_channels = ARRAY_SIZE(mhi_quectel_em1xx_channels),
319         .ch_cfg = mhi_quectel_em1xx_channels,
320         .num_events = ARRAY_SIZE(mhi_quectel_em1xx_events),
321         .event_cfg = mhi_quectel_em1xx_events,
322 };
323
324 static const struct mhi_pci_dev_info mhi_quectel_em1xx_info = {
325         .name = "quectel-em1xx",
326         .edl = "qcom/prog_firehose_sdx24.mbn",
327         .config = &modem_quectel_em1xx_config,
328         .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
329         .dma_data_width = 32,
330         .sideband_wake = true,
331 };
332
333 static const struct mhi_channel_config mhi_foxconn_sdx55_channels[] = {
334         MHI_CHANNEL_CONFIG_UL(0, "LOOPBACK", 32, 0),
335         MHI_CHANNEL_CONFIG_DL(1, "LOOPBACK", 32, 0),
336         MHI_CHANNEL_CONFIG_UL(4, "DIAG", 32, 1),
337         MHI_CHANNEL_CONFIG_DL(5, "DIAG", 32, 1),
338         MHI_CHANNEL_CONFIG_UL(12, "MBIM", 32, 0),
339         MHI_CHANNEL_CONFIG_DL(13, "MBIM", 32, 0),
340         MHI_CHANNEL_CONFIG_UL(32, "DUN", 32, 0),
341         MHI_CHANNEL_CONFIG_DL(33, "DUN", 32, 0),
342         MHI_CHANNEL_CONFIG_HW_UL(100, "IP_HW0_MBIM", 128, 2),
343         MHI_CHANNEL_CONFIG_HW_DL(101, "IP_HW0_MBIM", 128, 3),
344 };
345
346 static struct mhi_event_config mhi_foxconn_sdx55_events[] = {
347         MHI_EVENT_CONFIG_CTRL(0, 128),
348         MHI_EVENT_CONFIG_DATA(1, 128),
349         MHI_EVENT_CONFIG_HW_DATA(2, 1024, 100),
350         MHI_EVENT_CONFIG_HW_DATA(3, 1024, 101)
351 };
352
353 static const struct mhi_controller_config modem_foxconn_sdx55_config = {
354         .max_channels = 128,
355         .timeout_ms = 20000,
356         .num_channels = ARRAY_SIZE(mhi_foxconn_sdx55_channels),
357         .ch_cfg = mhi_foxconn_sdx55_channels,
358         .num_events = ARRAY_SIZE(mhi_foxconn_sdx55_events),
359         .event_cfg = mhi_foxconn_sdx55_events,
360 };
361
362 static const struct mhi_pci_dev_info mhi_foxconn_sdx55_info = {
363         .name = "foxconn-sdx55",
364         .fw = "qcom/sdx55m/sbl1.mbn",
365         .edl = "qcom/sdx55m/edl.mbn",
366         .config = &modem_foxconn_sdx55_config,
367         .bar_num = MHI_PCI_DEFAULT_BAR_NUM,
368         .dma_data_width = 32,
369         .sideband_wake = false,
370 };
371
372 static const struct pci_device_id mhi_pci_id_table[] = {
373         { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0306),
374                 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx55_info },
375         { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0304),
376                 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx24_info },
377         { PCI_DEVICE(0x1eac, 0x1001), /* EM120R-GL (sdx24) */
378                 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
379         { PCI_DEVICE(0x1eac, 0x1002), /* EM160R-GL (sdx24) */
380                 .driver_data = (kernel_ulong_t) &mhi_quectel_em1xx_info },
381         { PCI_DEVICE(PCI_VENDOR_ID_QCOM, 0x0308),
382                 .driver_data = (kernel_ulong_t) &mhi_qcom_sdx65_info },
383         /* T99W175 (sdx55), Both for eSIM and Non-eSIM */
384         { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0ab),
385                 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
386         /* DW5930e (sdx55), With eSIM, It's also T99W175 */
387         { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b0),
388                 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
389         /* DW5930e (sdx55), Non-eSIM, It's also T99W175 */
390         { PCI_DEVICE(PCI_VENDOR_ID_FOXCONN, 0xe0b1),
391                 .driver_data = (kernel_ulong_t) &mhi_foxconn_sdx55_info },
392         {  }
393 };
394 MODULE_DEVICE_TABLE(pci, mhi_pci_id_table);
395
396 enum mhi_pci_device_status {
397         MHI_PCI_DEV_STARTED,
398         MHI_PCI_DEV_SUSPENDED,
399 };
400
401 struct mhi_pci_device {
402         struct mhi_controller mhi_cntrl;
403         struct pci_saved_state *pci_state;
404         struct work_struct recovery_work;
405         struct timer_list health_check_timer;
406         unsigned long status;
407 };
408
409 static int mhi_pci_read_reg(struct mhi_controller *mhi_cntrl,
410                             void __iomem *addr, u32 *out)
411 {
412         *out = readl(addr);
413         return 0;
414 }
415
416 static void mhi_pci_write_reg(struct mhi_controller *mhi_cntrl,
417                               void __iomem *addr, u32 val)
418 {
419         writel(val, addr);
420 }
421
422 static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl,
423                               enum mhi_callback cb)
424 {
425         struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
426
427         /* Nothing to do for now */
428         switch (cb) {
429         case MHI_CB_FATAL_ERROR:
430         case MHI_CB_SYS_ERROR:
431                 dev_warn(&pdev->dev, "firmware crashed (%u)\n", cb);
432                 pm_runtime_forbid(&pdev->dev);
433                 break;
434         case MHI_CB_EE_MISSION_MODE:
435                 pm_runtime_allow(&pdev->dev);
436                 break;
437         default:
438                 break;
439         }
440 }
441
442 static void mhi_pci_wake_get_nop(struct mhi_controller *mhi_cntrl, bool force)
443 {
444         /* no-op */
445 }
446
447 static void mhi_pci_wake_put_nop(struct mhi_controller *mhi_cntrl, bool override)
448 {
449         /* no-op */
450 }
451
452 static void mhi_pci_wake_toggle_nop(struct mhi_controller *mhi_cntrl)
453 {
454         /* no-op */
455 }
456
457 static bool mhi_pci_is_alive(struct mhi_controller *mhi_cntrl)
458 {
459         struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
460         u16 vendor = 0;
461
462         if (pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor))
463                 return false;
464
465         if (vendor == (u16) ~0 || vendor == 0)
466                 return false;
467
468         return true;
469 }
470
471 static int mhi_pci_claim(struct mhi_controller *mhi_cntrl,
472                          unsigned int bar_num, u64 dma_mask)
473 {
474         struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
475         int err;
476
477         err = pci_assign_resource(pdev, bar_num);
478         if (err)
479                 return err;
480
481         err = pcim_enable_device(pdev);
482         if (err) {
483                 dev_err(&pdev->dev, "failed to enable pci device: %d\n", err);
484                 return err;
485         }
486
487         err = pcim_iomap_regions(pdev, 1 << bar_num, pci_name(pdev));
488         if (err) {
489                 dev_err(&pdev->dev, "failed to map pci region: %d\n", err);
490                 return err;
491         }
492         mhi_cntrl->regs = pcim_iomap_table(pdev)[bar_num];
493
494         err = pci_set_dma_mask(pdev, dma_mask);
495         if (err) {
496                 dev_err(&pdev->dev, "Cannot set proper DMA mask\n");
497                 return err;
498         }
499
500         err = pci_set_consistent_dma_mask(pdev, dma_mask);
501         if (err) {
502                 dev_err(&pdev->dev, "set consistent dma mask failed\n");
503                 return err;
504         }
505
506         pci_set_master(pdev);
507
508         return 0;
509 }
510
511 static int mhi_pci_get_irqs(struct mhi_controller *mhi_cntrl,
512                             const struct mhi_controller_config *mhi_cntrl_config)
513 {
514         struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
515         int nr_vectors, i;
516         int *irq;
517
518         /*
519          * Alloc one MSI vector for BHI + one vector per event ring, ideally...
520          * No explicit pci_free_irq_vectors required, done by pcim_release.
521          */
522         mhi_cntrl->nr_irqs = 1 + mhi_cntrl_config->num_events;
523
524         nr_vectors = pci_alloc_irq_vectors(pdev, 1, mhi_cntrl->nr_irqs, PCI_IRQ_MSI);
525         if (nr_vectors < 0) {
526                 dev_err(&pdev->dev, "Error allocating MSI vectors %d\n",
527                         nr_vectors);
528                 return nr_vectors;
529         }
530
531         if (nr_vectors < mhi_cntrl->nr_irqs) {
532                 dev_warn(&pdev->dev, "using shared MSI\n");
533
534                 /* Patch msi vectors, use only one (shared) */
535                 for (i = 0; i < mhi_cntrl_config->num_events; i++)
536                         mhi_cntrl_config->event_cfg[i].irq = 0;
537                 mhi_cntrl->nr_irqs = 1;
538         }
539
540         irq = devm_kcalloc(&pdev->dev, mhi_cntrl->nr_irqs, sizeof(int), GFP_KERNEL);
541         if (!irq)
542                 return -ENOMEM;
543
544         for (i = 0; i < mhi_cntrl->nr_irqs; i++) {
545                 int vector = i >= nr_vectors ? (nr_vectors - 1) : i;
546
547                 irq[i] = pci_irq_vector(pdev, vector);
548         }
549
550         mhi_cntrl->irq = irq;
551
552         return 0;
553 }
554
555 static int mhi_pci_runtime_get(struct mhi_controller *mhi_cntrl)
556 {
557         /* The runtime_get() MHI callback means:
558          *    Do whatever is requested to leave M3.
559          */
560         return pm_runtime_get(mhi_cntrl->cntrl_dev);
561 }
562
563 static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl)
564 {
565         /* The runtime_put() MHI callback means:
566          *    Device can be moved in M3 state.
567          */
568         pm_runtime_mark_last_busy(mhi_cntrl->cntrl_dev);
569         pm_runtime_put(mhi_cntrl->cntrl_dev);
570 }
571
572 static void mhi_pci_recovery_work(struct work_struct *work)
573 {
574         struct mhi_pci_device *mhi_pdev = container_of(work, struct mhi_pci_device,
575                                                        recovery_work);
576         struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
577         struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev);
578         int err;
579
580         dev_warn(&pdev->dev, "device recovery started\n");
581
582         del_timer(&mhi_pdev->health_check_timer);
583         pm_runtime_forbid(&pdev->dev);
584
585         /* Clean up MHI state */
586         if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
587                 mhi_power_down(mhi_cntrl, false);
588                 mhi_unprepare_after_power_down(mhi_cntrl);
589         }
590
591         pci_set_power_state(pdev, PCI_D0);
592         pci_load_saved_state(pdev, mhi_pdev->pci_state);
593         pci_restore_state(pdev);
594
595         if (!mhi_pci_is_alive(mhi_cntrl))
596                 goto err_try_reset;
597
598         err = mhi_prepare_for_power_up(mhi_cntrl);
599         if (err)
600                 goto err_try_reset;
601
602         err = mhi_sync_power_up(mhi_cntrl);
603         if (err)
604                 goto err_unprepare;
605
606         dev_dbg(&pdev->dev, "Recovery completed\n");
607
608         set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
609         mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
610         return;
611
612 err_unprepare:
613         mhi_unprepare_after_power_down(mhi_cntrl);
614 err_try_reset:
615         if (pci_reset_function(pdev))
616                 dev_err(&pdev->dev, "Recovery failed\n");
617 }
618
619 static void health_check(struct timer_list *t)
620 {
621         struct mhi_pci_device *mhi_pdev = from_timer(mhi_pdev, t, health_check_timer);
622         struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
623
624         if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
625                         test_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
626                 return;
627
628         if (!mhi_pci_is_alive(mhi_cntrl)) {
629                 dev_err(mhi_cntrl->cntrl_dev, "Device died\n");
630                 queue_work(system_long_wq, &mhi_pdev->recovery_work);
631                 return;
632         }
633
634         /* reschedule in two seconds */
635         mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
636 }
637
638 static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
639 {
640         const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data;
641         const struct mhi_controller_config *mhi_cntrl_config;
642         struct mhi_pci_device *mhi_pdev;
643         struct mhi_controller *mhi_cntrl;
644         int err;
645
646         dev_dbg(&pdev->dev, "MHI PCI device found: %s\n", info->name);
647
648         /* mhi_pdev.mhi_cntrl must be zero-initialized */
649         mhi_pdev = devm_kzalloc(&pdev->dev, sizeof(*mhi_pdev), GFP_KERNEL);
650         if (!mhi_pdev)
651                 return -ENOMEM;
652
653         INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work);
654         timer_setup(&mhi_pdev->health_check_timer, health_check, 0);
655
656         mhi_cntrl_config = info->config;
657         mhi_cntrl = &mhi_pdev->mhi_cntrl;
658
659         mhi_cntrl->cntrl_dev = &pdev->dev;
660         mhi_cntrl->iova_start = 0;
661         mhi_cntrl->iova_stop = (dma_addr_t)DMA_BIT_MASK(info->dma_data_width);
662         mhi_cntrl->fw_image = info->fw;
663         mhi_cntrl->edl_image = info->edl;
664
665         mhi_cntrl->read_reg = mhi_pci_read_reg;
666         mhi_cntrl->write_reg = mhi_pci_write_reg;
667         mhi_cntrl->status_cb = mhi_pci_status_cb;
668         mhi_cntrl->runtime_get = mhi_pci_runtime_get;
669         mhi_cntrl->runtime_put = mhi_pci_runtime_put;
670         mhi_cntrl->mru = info->mru_default;
671
672         if (info->sideband_wake) {
673                 mhi_cntrl->wake_get = mhi_pci_wake_get_nop;
674                 mhi_cntrl->wake_put = mhi_pci_wake_put_nop;
675                 mhi_cntrl->wake_toggle = mhi_pci_wake_toggle_nop;
676         }
677
678         err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width));
679         if (err)
680                 return err;
681
682         err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config);
683         if (err)
684                 return err;
685
686         pci_set_drvdata(pdev, mhi_pdev);
687
688         /* Have stored pci confspace at hand for restore in sudden PCI error.
689          * cache the state locally and discard the PCI core one.
690          */
691         pci_save_state(pdev);
692         mhi_pdev->pci_state = pci_store_saved_state(pdev);
693         pci_load_saved_state(pdev, NULL);
694
695         pci_enable_pcie_error_reporting(pdev);
696
697         err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config);
698         if (err)
699                 goto err_disable_reporting;
700
701         /* MHI bus does not power up the controller by default */
702         err = mhi_prepare_for_power_up(mhi_cntrl);
703         if (err) {
704                 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
705                 goto err_unregister;
706         }
707
708         err = mhi_sync_power_up(mhi_cntrl);
709         if (err) {
710                 dev_err(&pdev->dev, "failed to power up MHI controller\n");
711                 goto err_unprepare;
712         }
713
714         set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
715
716         /* start health check */
717         mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
718
719         /* Only allow runtime-suspend if PME capable (for wakeup) */
720         if (pci_pme_capable(pdev, PCI_D3hot)) {
721                 pm_runtime_set_autosuspend_delay(&pdev->dev, 2000);
722                 pm_runtime_use_autosuspend(&pdev->dev);
723                 pm_runtime_mark_last_busy(&pdev->dev);
724                 pm_runtime_put_noidle(&pdev->dev);
725         }
726
727         return 0;
728
729 err_unprepare:
730         mhi_unprepare_after_power_down(mhi_cntrl);
731 err_unregister:
732         mhi_unregister_controller(mhi_cntrl);
733 err_disable_reporting:
734         pci_disable_pcie_error_reporting(pdev);
735
736         return err;
737 }
738
739 static void mhi_pci_remove(struct pci_dev *pdev)
740 {
741         struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
742         struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
743
744         del_timer_sync(&mhi_pdev->health_check_timer);
745         cancel_work_sync(&mhi_pdev->recovery_work);
746
747         if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
748                 mhi_power_down(mhi_cntrl, true);
749                 mhi_unprepare_after_power_down(mhi_cntrl);
750         }
751
752         /* balancing probe put_noidle */
753         if (pci_pme_capable(pdev, PCI_D3hot))
754                 pm_runtime_get_noresume(&pdev->dev);
755
756         mhi_unregister_controller(mhi_cntrl);
757         pci_disable_pcie_error_reporting(pdev);
758 }
759
760 static void mhi_pci_shutdown(struct pci_dev *pdev)
761 {
762         mhi_pci_remove(pdev);
763         pci_set_power_state(pdev, PCI_D3hot);
764 }
765
766 static void mhi_pci_reset_prepare(struct pci_dev *pdev)
767 {
768         struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
769         struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
770
771         dev_info(&pdev->dev, "reset\n");
772
773         del_timer(&mhi_pdev->health_check_timer);
774
775         /* Clean up MHI state */
776         if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
777                 mhi_power_down(mhi_cntrl, false);
778                 mhi_unprepare_after_power_down(mhi_cntrl);
779         }
780
781         /* cause internal device reset */
782         mhi_soc_reset(mhi_cntrl);
783
784         /* Be sure device reset has been executed */
785         msleep(MHI_POST_RESET_DELAY_MS);
786 }
787
788 static void mhi_pci_reset_done(struct pci_dev *pdev)
789 {
790         struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
791         struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
792         int err;
793
794         /* Restore initial known working PCI state */
795         pci_load_saved_state(pdev, mhi_pdev->pci_state);
796         pci_restore_state(pdev);
797
798         /* Is device status available ? */
799         if (!mhi_pci_is_alive(mhi_cntrl)) {
800                 dev_err(&pdev->dev, "reset failed\n");
801                 return;
802         }
803
804         err = mhi_prepare_for_power_up(mhi_cntrl);
805         if (err) {
806                 dev_err(&pdev->dev, "failed to prepare MHI controller\n");
807                 return;
808         }
809
810         err = mhi_sync_power_up(mhi_cntrl);
811         if (err) {
812                 dev_err(&pdev->dev, "failed to power up MHI controller\n");
813                 mhi_unprepare_after_power_down(mhi_cntrl);
814                 return;
815         }
816
817         set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status);
818         mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
819 }
820
821 static pci_ers_result_t mhi_pci_error_detected(struct pci_dev *pdev,
822                                                pci_channel_state_t state)
823 {
824         struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
825         struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
826
827         dev_err(&pdev->dev, "PCI error detected, state = %u\n", state);
828
829         if (state == pci_channel_io_perm_failure)
830                 return PCI_ERS_RESULT_DISCONNECT;
831
832         /* Clean up MHI state */
833         if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
834                 mhi_power_down(mhi_cntrl, false);
835                 mhi_unprepare_after_power_down(mhi_cntrl);
836         } else {
837                 /* Nothing to do */
838                 return PCI_ERS_RESULT_RECOVERED;
839         }
840
841         pci_disable_device(pdev);
842
843         return PCI_ERS_RESULT_NEED_RESET;
844 }
845
846 static pci_ers_result_t mhi_pci_slot_reset(struct pci_dev *pdev)
847 {
848         if (pci_enable_device(pdev)) {
849                 dev_err(&pdev->dev, "Cannot re-enable PCI device after reset.\n");
850                 return PCI_ERS_RESULT_DISCONNECT;
851         }
852
853         return PCI_ERS_RESULT_RECOVERED;
854 }
855
856 static void mhi_pci_io_resume(struct pci_dev *pdev)
857 {
858         struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev);
859
860         dev_err(&pdev->dev, "PCI slot reset done\n");
861
862         queue_work(system_long_wq, &mhi_pdev->recovery_work);
863 }
864
865 static const struct pci_error_handlers mhi_pci_err_handler = {
866         .error_detected = mhi_pci_error_detected,
867         .slot_reset = mhi_pci_slot_reset,
868         .resume = mhi_pci_io_resume,
869         .reset_prepare = mhi_pci_reset_prepare,
870         .reset_done = mhi_pci_reset_done,
871 };
872
873 static int  __maybe_unused mhi_pci_runtime_suspend(struct device *dev)
874 {
875         struct pci_dev *pdev = to_pci_dev(dev);
876         struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
877         struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
878         int err;
879
880         if (test_and_set_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
881                 return 0;
882
883         del_timer(&mhi_pdev->health_check_timer);
884         cancel_work_sync(&mhi_pdev->recovery_work);
885
886         if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
887                         mhi_cntrl->ee != MHI_EE_AMSS)
888                 goto pci_suspend; /* Nothing to do at MHI level */
889
890         /* Transition to M3 state */
891         err = mhi_pm_suspend(mhi_cntrl);
892         if (err) {
893                 dev_err(&pdev->dev, "failed to suspend device: %d\n", err);
894                 clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status);
895                 return -EBUSY;
896         }
897
898 pci_suspend:
899         pci_disable_device(pdev);
900         pci_wake_from_d3(pdev, true);
901
902         return 0;
903 }
904
905 static int __maybe_unused mhi_pci_runtime_resume(struct device *dev)
906 {
907         struct pci_dev *pdev = to_pci_dev(dev);
908         struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
909         struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
910         int err;
911
912         if (!test_and_clear_bit(MHI_PCI_DEV_SUSPENDED, &mhi_pdev->status))
913                 return 0;
914
915         err = pci_enable_device(pdev);
916         if (err)
917                 goto err_recovery;
918
919         pci_set_master(pdev);
920         pci_wake_from_d3(pdev, false);
921
922         if (!test_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status) ||
923                         mhi_cntrl->ee != MHI_EE_AMSS)
924                 return 0; /* Nothing to do at MHI level */
925
926         /* Exit M3, transition to M0 state */
927         err = mhi_pm_resume(mhi_cntrl);
928         if (err) {
929                 dev_err(&pdev->dev, "failed to resume device: %d\n", err);
930                 goto err_recovery;
931         }
932
933         /* Resume health check */
934         mod_timer(&mhi_pdev->health_check_timer, jiffies + HEALTH_CHECK_PERIOD);
935
936         /* It can be a remote wakeup (no mhi runtime_get), update access time */
937         pm_runtime_mark_last_busy(dev);
938
939         return 0;
940
941 err_recovery:
942         /* Do not fail to not mess up our PCI device state, the device likely
943          * lost power (d3cold) and we simply need to reset it from the recovery
944          * procedure, trigger the recovery asynchronously to prevent system
945          * suspend exit delaying.
946          */
947         queue_work(system_long_wq, &mhi_pdev->recovery_work);
948         pm_runtime_mark_last_busy(dev);
949
950         return 0;
951 }
952
953 static int  __maybe_unused mhi_pci_suspend(struct device *dev)
954 {
955         pm_runtime_disable(dev);
956         return mhi_pci_runtime_suspend(dev);
957 }
958
959 static int __maybe_unused mhi_pci_resume(struct device *dev)
960 {
961         int ret;
962
963         /* Depending the platform, device may have lost power (d3cold), we need
964          * to resume it now to check its state and recover when necessary.
965          */
966         ret = mhi_pci_runtime_resume(dev);
967         pm_runtime_enable(dev);
968
969         return ret;
970 }
971
972 static int __maybe_unused mhi_pci_freeze(struct device *dev)
973 {
974         struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
975         struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl;
976
977         /* We want to stop all operations, hibernation does not guarantee that
978          * device will be in the same state as before freezing, especially if
979          * the intermediate restore kernel reinitializes MHI device with new
980          * context.
981          */
982         if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) {
983                 mhi_power_down(mhi_cntrl, false);
984                 mhi_unprepare_after_power_down(mhi_cntrl);
985         }
986
987         return 0;
988 }
989
990 static int __maybe_unused mhi_pci_restore(struct device *dev)
991 {
992         struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev);
993
994         /* Reinitialize the device */
995         queue_work(system_long_wq, &mhi_pdev->recovery_work);
996
997         return 0;
998 }
999
1000 static const struct dev_pm_ops mhi_pci_pm_ops = {
1001         SET_RUNTIME_PM_OPS(mhi_pci_runtime_suspend, mhi_pci_runtime_resume, NULL)
1002 #ifdef CONFIG_PM_SLEEP
1003         .suspend = mhi_pci_suspend,
1004         .resume = mhi_pci_resume,
1005         .freeze = mhi_pci_freeze,
1006         .thaw = mhi_pci_restore,
1007         .restore = mhi_pci_restore,
1008 #endif
1009 };
1010
1011 static struct pci_driver mhi_pci_driver = {
1012         .name           = "mhi-pci-generic",
1013         .id_table       = mhi_pci_id_table,
1014         .probe          = mhi_pci_probe,
1015         .remove         = mhi_pci_remove,
1016         .shutdown       = mhi_pci_shutdown,
1017         .err_handler    = &mhi_pci_err_handler,
1018         .driver.pm      = &mhi_pci_pm_ops
1019 };
1020 module_pci_driver(mhi_pci_driver);
1021
1022 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
1023 MODULE_DESCRIPTION("Modem Host Interface (MHI) PCI controller driver");
1024 MODULE_LICENSE("GPL");