liquidio: CN23XX queue manipulation
[cascardo/linux.git] / drivers / net / ethernet / cavium / liquidio / cn66xx_device.c
1 /**********************************************************************
2 * Author: Cavium, Inc.
3 *
4 * Contact: support@cavium.com
5 *          Please include "LiquidIO" in the subject.
6 *
7 * Copyright (c) 2003-2015 Cavium, Inc.
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT.  See the GNU General Public License for more
17 * details.
18 *
19 * This file may also be available under a different license from Cavium.
20 * Contact Cavium, Inc. for more information
21 **********************************************************************/
22 #include <linux/pci.h>
23 #include <linux/netdevice.h>
24 #include "liquidio_common.h"
25 #include "octeon_droq.h"
26 #include "octeon_iq.h"
27 #include "response_manager.h"
28 #include "octeon_device.h"
29 #include "octeon_main.h"
30 #include "cn66xx_regs.h"
31 #include "cn66xx_device.h"
32
33 int lio_cn6xxx_soft_reset(struct octeon_device *oct)
34 {
35         octeon_write_csr64(oct, CN6XXX_WIN_WR_MASK_REG, 0xFF);
36
37         dev_dbg(&oct->pci_dev->dev, "BIST enabled for soft reset\n");
38
39         lio_pci_writeq(oct, 1, CN6XXX_CIU_SOFT_BIST);
40         octeon_write_csr64(oct, CN6XXX_SLI_SCRATCH1, 0x1234ULL);
41
42         lio_pci_readq(oct, CN6XXX_CIU_SOFT_RST);
43         lio_pci_writeq(oct, 1, CN6XXX_CIU_SOFT_RST);
44
45         /* make sure that the reset is written before starting timer */
46         mmiowb();
47
48         /* Wait for 10ms as Octeon resets. */
49         mdelay(100);
50
51         if (octeon_read_csr64(oct, CN6XXX_SLI_SCRATCH1) == 0x1234ULL) {
52                 dev_err(&oct->pci_dev->dev, "Soft reset failed\n");
53                 return 1;
54         }
55
56         dev_dbg(&oct->pci_dev->dev, "Reset completed\n");
57         octeon_write_csr64(oct, CN6XXX_WIN_WR_MASK_REG, 0xFF);
58
59         return 0;
60 }
61
62 void lio_cn6xxx_enable_error_reporting(struct octeon_device *oct)
63 {
64         u32 val;
65
66         pci_read_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, &val);
67         if (val & 0x000c0000) {
68                 dev_err(&oct->pci_dev->dev, "PCI-E Link error detected: 0x%08x\n",
69                         val & 0x000c0000);
70         }
71
72         val |= 0xf;          /* Enable Link error reporting */
73
74         dev_dbg(&oct->pci_dev->dev, "Enabling PCI-E error reporting..\n");
75         pci_write_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, val);
76 }
77
78 void lio_cn6xxx_setup_pcie_mps(struct octeon_device *oct,
79                                enum octeon_pcie_mps mps)
80 {
81         u32 val;
82         u64 r64;
83
84         /* Read config register for MPS */
85         pci_read_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, &val);
86
87         if (mps == PCIE_MPS_DEFAULT) {
88                 mps = ((val & (0x7 << 5)) >> 5);
89         } else {
90                 val &= ~(0x7 << 5);  /* Turn off any MPS bits */
91                 val |= (mps << 5);   /* Set MPS */
92                 pci_write_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, val);
93         }
94
95         /* Set MPS in DPI_SLI_PRT0_CFG to the same value. */
96         r64 = lio_pci_readq(oct, CN6XXX_DPI_SLI_PRTX_CFG(oct->pcie_port));
97         r64 |= (mps << 4);
98         lio_pci_writeq(oct, r64, CN6XXX_DPI_SLI_PRTX_CFG(oct->pcie_port));
99 }
100
101 void lio_cn6xxx_setup_pcie_mrrs(struct octeon_device *oct,
102                                 enum octeon_pcie_mrrs mrrs)
103 {
104         u32 val;
105         u64 r64;
106
107         /* Read config register for MRRS */
108         pci_read_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, &val);
109
110         if (mrrs == PCIE_MRRS_DEFAULT) {
111                 mrrs = ((val & (0x7 << 12)) >> 12);
112         } else {
113                 val &= ~(0x7 << 12); /* Turn off any MRRS bits */
114                 val |= (mrrs << 12); /* Set MRRS */
115                 pci_write_config_dword(oct->pci_dev, CN6XXX_PCIE_DEVCTL, val);
116         }
117
118         /* Set MRRS in SLI_S2M_PORT0_CTL to the same value. */
119         r64 = octeon_read_csr64(oct, CN6XXX_SLI_S2M_PORTX_CTL(oct->pcie_port));
120         r64 |= mrrs;
121         octeon_write_csr64(oct, CN6XXX_SLI_S2M_PORTX_CTL(oct->pcie_port), r64);
122
123         /* Set MRRS in DPI_SLI_PRT0_CFG to the same value. */
124         r64 = lio_pci_readq(oct, CN6XXX_DPI_SLI_PRTX_CFG(oct->pcie_port));
125         r64 |= mrrs;
126         lio_pci_writeq(oct, r64, CN6XXX_DPI_SLI_PRTX_CFG(oct->pcie_port));
127 }
128
129 u32 lio_cn6xxx_coprocessor_clock(struct octeon_device *oct)
130 {
131         /* Bits 29:24 of MIO_RST_BOOT holds the ref. clock multiplier
132          * for SLI.
133          */
134         return ((lio_pci_readq(oct, CN6XXX_MIO_RST_BOOT) >> 24) & 0x3f) * 50;
135 }
136
137 u32 lio_cn6xxx_get_oq_ticks(struct octeon_device *oct,
138                             u32 time_intr_in_us)
139 {
140         /* This gives the SLI clock per microsec */
141         u32 oqticks_per_us = lio_cn6xxx_coprocessor_clock(oct);
142
143         /* core clock per us / oq ticks will be fractional. TO avoid that
144          * we use the method below.
145          */
146
147         /* This gives the clock cycles per millisecond */
148         oqticks_per_us *= 1000;
149
150         /* This gives the oq ticks (1024 core clock cycles) per millisecond */
151         oqticks_per_us /= 1024;
152
153         /* time_intr is in microseconds. The next 2 steps gives the oq ticks
154          * corressponding to time_intr.
155          */
156         oqticks_per_us *= time_intr_in_us;
157         oqticks_per_us /= 1000;
158
159         return oqticks_per_us;
160 }
161
162 void lio_cn6xxx_setup_global_input_regs(struct octeon_device *oct)
163 {
164         /* Select Round-Robin Arb, ES, RO, NS for Input Queues */
165         octeon_write_csr(oct, CN6XXX_SLI_PKT_INPUT_CONTROL,
166                          CN6XXX_INPUT_CTL_MASK);
167
168         /* Instruction Read Size - Max 4 instructions per PCIE Read */
169         octeon_write_csr64(oct, CN6XXX_SLI_PKT_INSTR_RD_SIZE,
170                            0xFFFFFFFFFFFFFFFFULL);
171
172         /* Select PCIE Port for all Input rings. */
173         octeon_write_csr64(oct, CN6XXX_SLI_IN_PCIE_PORT,
174                            (oct->pcie_port * 0x5555555555555555ULL));
175 }
176
177 static void lio_cn66xx_setup_pkt_ctl_regs(struct octeon_device *oct)
178 {
179         u64 pktctl;
180
181         struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
182
183         pktctl = octeon_read_csr64(oct, CN6XXX_SLI_PKT_CTL);
184
185         /* 66XX SPECIFIC */
186         if (CFG_GET_OQ_MAX_Q(cn6xxx->conf) <= 4)
187                 /* Disable RING_EN if only upto 4 rings are used. */
188                 pktctl &= ~(1 << 4);
189         else
190                 pktctl |= (1 << 4);
191
192         if (CFG_GET_IS_SLI_BP_ON(cn6xxx->conf))
193                 pktctl |= 0xF;
194         else
195                 /* Disable per-port backpressure. */
196                 pktctl &= ~0xF;
197         octeon_write_csr64(oct, CN6XXX_SLI_PKT_CTL, pktctl);
198 }
199
200 void lio_cn6xxx_setup_global_output_regs(struct octeon_device *oct)
201 {
202         u32 time_threshold;
203         struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
204
205         /* / Select PCI-E Port for all Output queues */
206         octeon_write_csr64(oct, CN6XXX_SLI_PKT_PCIE_PORT64,
207                            (oct->pcie_port * 0x5555555555555555ULL));
208
209         if (CFG_GET_IS_SLI_BP_ON(cn6xxx->conf)) {
210                 octeon_write_csr64(oct, CN6XXX_SLI_OQ_WMARK, 32);
211         } else {
212                 /* / Set Output queue watermark to 0 to disable backpressure */
213                 octeon_write_csr64(oct, CN6XXX_SLI_OQ_WMARK, 0);
214         }
215
216         /* / Select Info Ptr for length & data */
217         octeon_write_csr(oct, CN6XXX_SLI_PKT_IPTR, 0xFFFFFFFF);
218
219         /* / Select Packet count instead of bytes for SLI_PKTi_CNTS[CNT] */
220         octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_BMODE, 0);
221
222         /* Select ES, RO, NS setting from register for Output Queue Packet
223          * Address
224          */
225         octeon_write_csr(oct, CN6XXX_SLI_PKT_DPADDR, 0xFFFFFFFF);
226
227         /* No Relaxed Ordering, No Snoop, 64-bit swap for Output
228          * Queue ScatterList
229          */
230         octeon_write_csr(oct, CN6XXX_SLI_PKT_SLIST_ROR, 0);
231         octeon_write_csr(oct, CN6XXX_SLI_PKT_SLIST_NS, 0);
232
233         /* / ENDIAN_SPECIFIC CHANGES - 0 works for LE. */
234 #ifdef __BIG_ENDIAN_BITFIELD
235         octeon_write_csr64(oct, CN6XXX_SLI_PKT_SLIST_ES64,
236                            0x5555555555555555ULL);
237 #else
238         octeon_write_csr64(oct, CN6XXX_SLI_PKT_SLIST_ES64, 0ULL);
239 #endif
240
241         /* / No Relaxed Ordering, No Snoop, 64-bit swap for Output Queue Data */
242         octeon_write_csr(oct, CN6XXX_SLI_PKT_DATA_OUT_ROR, 0);
243         octeon_write_csr(oct, CN6XXX_SLI_PKT_DATA_OUT_NS, 0);
244         octeon_write_csr64(oct, CN6XXX_SLI_PKT_DATA_OUT_ES64,
245                            0x5555555555555555ULL);
246
247         /* / Set up interrupt packet and time threshold */
248         octeon_write_csr(oct, CN6XXX_SLI_OQ_INT_LEVEL_PKTS,
249                          (u32)CFG_GET_OQ_INTR_PKT(cn6xxx->conf));
250         time_threshold =
251                 lio_cn6xxx_get_oq_ticks(oct, (u32)
252                                         CFG_GET_OQ_INTR_TIME(cn6xxx->conf));
253
254         octeon_write_csr(oct, CN6XXX_SLI_OQ_INT_LEVEL_TIME, time_threshold);
255 }
256
257 static int lio_cn6xxx_setup_device_regs(struct octeon_device *oct)
258 {
259         lio_cn6xxx_setup_pcie_mps(oct, PCIE_MPS_DEFAULT);
260         lio_cn6xxx_setup_pcie_mrrs(oct, PCIE_MRRS_512B);
261         lio_cn6xxx_enable_error_reporting(oct);
262
263         lio_cn6xxx_setup_global_input_regs(oct);
264         lio_cn66xx_setup_pkt_ctl_regs(oct);
265         lio_cn6xxx_setup_global_output_regs(oct);
266
267         /* Default error timeout value should be 0x200000 to avoid host hang
268          * when reads invalid register
269          */
270         octeon_write_csr64(oct, CN6XXX_SLI_WINDOW_CTL, 0x200000ULL);
271         return 0;
272 }
273
274 void lio_cn6xxx_setup_iq_regs(struct octeon_device *oct, u32 iq_no)
275 {
276         struct octeon_instr_queue *iq = oct->instr_queue[iq_no];
277
278         /* Disable Packet-by-Packet mode; No Parse Mode or Skip length */
279         octeon_write_csr64(oct, CN6XXX_SLI_IQ_PKT_INSTR_HDR64(iq_no), 0);
280
281         /* Write the start of the input queue's ring and its size  */
282         octeon_write_csr64(oct, CN6XXX_SLI_IQ_BASE_ADDR64(iq_no),
283                            iq->base_addr_dma);
284         octeon_write_csr(oct, CN6XXX_SLI_IQ_SIZE(iq_no), iq->max_count);
285
286         /* Remember the doorbell & instruction count register addr for this
287          * queue
288          */
289         iq->doorbell_reg = oct->mmio[0].hw_addr + CN6XXX_SLI_IQ_DOORBELL(iq_no);
290         iq->inst_cnt_reg = oct->mmio[0].hw_addr
291                            + CN6XXX_SLI_IQ_INSTR_COUNT(iq_no);
292         dev_dbg(&oct->pci_dev->dev, "InstQ[%d]:dbell reg @ 0x%p instcnt_reg @ 0x%p\n",
293                 iq_no, iq->doorbell_reg, iq->inst_cnt_reg);
294
295         /* Store the current instruction counter
296          * (used in flush_iq calculation)
297          */
298         iq->reset_instr_cnt = readl(iq->inst_cnt_reg);
299 }
300
301 static void lio_cn66xx_setup_iq_regs(struct octeon_device *oct, u32 iq_no)
302 {
303         lio_cn6xxx_setup_iq_regs(oct, iq_no);
304
305         /* Backpressure for this queue - WMARK set to all F's. This effectively
306          * disables the backpressure mechanism.
307          */
308         octeon_write_csr64(oct, CN66XX_SLI_IQ_BP64(iq_no),
309                            (0xFFFFFFFFULL << 32));
310 }
311
312 void lio_cn6xxx_setup_oq_regs(struct octeon_device *oct, u32 oq_no)
313 {
314         u32 intr;
315         struct octeon_droq *droq = oct->droq[oq_no];
316
317         octeon_write_csr64(oct, CN6XXX_SLI_OQ_BASE_ADDR64(oq_no),
318                            droq->desc_ring_dma);
319         octeon_write_csr(oct, CN6XXX_SLI_OQ_SIZE(oq_no), droq->max_count);
320
321         octeon_write_csr(oct, CN6XXX_SLI_OQ_BUFF_INFO_SIZE(oq_no),
322                          (droq->buffer_size | (OCT_RH_SIZE << 16)));
323
324         /* Get the mapped address of the pkt_sent and pkts_credit regs */
325         droq->pkts_sent_reg =
326                 oct->mmio[0].hw_addr + CN6XXX_SLI_OQ_PKTS_SENT(oq_no);
327         droq->pkts_credit_reg =
328                 oct->mmio[0].hw_addr + CN6XXX_SLI_OQ_PKTS_CREDIT(oq_no);
329
330         /* Enable this output queue to generate Packet Timer Interrupt */
331         intr = octeon_read_csr(oct, CN6XXX_SLI_PKT_TIME_INT_ENB);
332         intr |= (1 << oq_no);
333         octeon_write_csr(oct, CN6XXX_SLI_PKT_TIME_INT_ENB, intr);
334
335         /* Enable this output queue to generate Packet Timer Interrupt */
336         intr = octeon_read_csr(oct, CN6XXX_SLI_PKT_CNT_INT_ENB);
337         intr |= (1 << oq_no);
338         octeon_write_csr(oct, CN6XXX_SLI_PKT_CNT_INT_ENB, intr);
339 }
340
341 int lio_cn6xxx_enable_io_queues(struct octeon_device *oct)
342 {
343         u32 mask;
344
345         mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_INSTR_SIZE);
346         mask |= oct->io_qmask.iq64B;
347         octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_SIZE, mask);
348
349         mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB);
350         mask |= oct->io_qmask.iq;
351         octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, mask);
352
353         mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_OUT_ENB);
354         mask |= oct->io_qmask.oq;
355         octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, mask);
356
357         return 0;
358 }
359
360 void lio_cn6xxx_disable_io_queues(struct octeon_device *oct)
361 {
362         int i;
363         u32 mask, loop = HZ;
364         u32 d32;
365
366         /* Reset the Enable bits for Input Queues. */
367         mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB);
368         mask ^= oct->io_qmask.iq;
369         octeon_write_csr(oct, CN6XXX_SLI_PKT_INSTR_ENB, mask);
370
371         /* Wait until hardware indicates that the queues are out of reset. */
372         mask = (u32)oct->io_qmask.iq;
373         d32 = octeon_read_csr(oct, CN6XXX_SLI_PORT_IN_RST_IQ);
374         while (((d32 & mask) != mask) && loop--) {
375                 d32 = octeon_read_csr(oct, CN6XXX_SLI_PORT_IN_RST_IQ);
376                 schedule_timeout_uninterruptible(1);
377         }
378
379         /* Reset the doorbell register for each Input queue. */
380         for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
381                 if (!(oct->io_qmask.iq & (1ULL << i)))
382                         continue;
383                 octeon_write_csr(oct, CN6XXX_SLI_IQ_DOORBELL(i), 0xFFFFFFFF);
384                 d32 = octeon_read_csr(oct, CN6XXX_SLI_IQ_DOORBELL(i));
385         }
386
387         /* Reset the Enable bits for Output Queues. */
388         mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_OUT_ENB);
389         mask ^= oct->io_qmask.oq;
390         octeon_write_csr(oct, CN6XXX_SLI_PKT_OUT_ENB, mask);
391
392         /* Wait until hardware indicates that the queues are out of reset. */
393         loop = HZ;
394         mask = (u32)oct->io_qmask.oq;
395         d32 = octeon_read_csr(oct, CN6XXX_SLI_PORT_IN_RST_OQ);
396         while (((d32 & mask) != mask) && loop--) {
397                 d32 = octeon_read_csr(oct, CN6XXX_SLI_PORT_IN_RST_OQ);
398                 schedule_timeout_uninterruptible(1);
399         }
400         ;
401
402         /* Reset the doorbell register for each Output queue. */
403         /* for (i = 0; i < oct->num_oqs; i++) { */
404         for (i = 0; i < MAX_OCTEON_OUTPUT_QUEUES(oct); i++) {
405                 if (!(oct->io_qmask.oq & (1ULL << i)))
406                         continue;
407                 octeon_write_csr(oct, CN6XXX_SLI_OQ_PKTS_CREDIT(i), 0xFFFFFFFF);
408                 d32 = octeon_read_csr(oct, CN6XXX_SLI_OQ_PKTS_CREDIT(i));
409
410                 d32 = octeon_read_csr(oct, CN6XXX_SLI_OQ_PKTS_SENT(i));
411                 octeon_write_csr(oct, CN6XXX_SLI_OQ_PKTS_SENT(i), d32);
412         }
413
414         d32 = octeon_read_csr(oct, CN6XXX_SLI_PKT_CNT_INT);
415         if (d32)
416                 octeon_write_csr(oct, CN6XXX_SLI_PKT_CNT_INT, d32);
417
418         d32 = octeon_read_csr(oct, CN6XXX_SLI_PKT_TIME_INT);
419         if (d32)
420                 octeon_write_csr(oct, CN6XXX_SLI_PKT_TIME_INT, d32);
421 }
422
423 void
424 lio_cn6xxx_bar1_idx_setup(struct octeon_device *oct,
425                           u64 core_addr,
426                           u32 idx,
427                           int valid)
428 {
429         u64 bar1;
430
431         if (valid == 0) {
432                 bar1 = lio_pci_readq(oct, CN6XXX_BAR1_REG(idx, oct->pcie_port));
433                 lio_pci_writeq(oct, (bar1 & 0xFFFFFFFEULL),
434                                CN6XXX_BAR1_REG(idx, oct->pcie_port));
435                 bar1 = lio_pci_readq(oct, CN6XXX_BAR1_REG(idx, oct->pcie_port));
436                 return;
437         }
438
439         /* Bits 17:4 of the PCI_BAR1_INDEXx stores bits 35:22 of
440          * the Core Addr
441          */
442         lio_pci_writeq(oct, (((core_addr >> 22) << 4) | PCI_BAR1_MASK),
443                        CN6XXX_BAR1_REG(idx, oct->pcie_port));
444
445         bar1 = lio_pci_readq(oct, CN6XXX_BAR1_REG(idx, oct->pcie_port));
446 }
447
448 void lio_cn6xxx_bar1_idx_write(struct octeon_device *oct,
449                                u32 idx,
450                                u32 mask)
451 {
452         lio_pci_writeq(oct, mask, CN6XXX_BAR1_REG(idx, oct->pcie_port));
453 }
454
455 u32 lio_cn6xxx_bar1_idx_read(struct octeon_device *oct, u32 idx)
456 {
457         return (u32)lio_pci_readq(oct, CN6XXX_BAR1_REG(idx, oct->pcie_port));
458 }
459
460 u32
461 lio_cn6xxx_update_read_index(struct octeon_instr_queue *iq)
462 {
463         u32 new_idx = readl(iq->inst_cnt_reg);
464
465         /* The new instr cnt reg is a 32-bit counter that can roll over. We have
466          * noted the counter's initial value at init time into
467          * reset_instr_cnt
468          */
469         if (iq->reset_instr_cnt < new_idx)
470                 new_idx -= iq->reset_instr_cnt;
471         else
472                 new_idx += (0xffffffff - iq->reset_instr_cnt) + 1;
473
474         /* Modulo of the new index with the IQ size will give us
475          * the new index.
476          */
477         new_idx %= iq->max_count;
478
479         return new_idx;
480 }
481
482 void lio_cn6xxx_enable_interrupt(void *chip)
483 {
484         struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)chip;
485         u64 mask = cn6xxx->intr_mask64 | CN6XXX_INTR_DMA0_FORCE;
486
487         /* Enable Interrupt */
488         writeq(mask, cn6xxx->intr_enb_reg64);
489 }
490
491 void lio_cn6xxx_disable_interrupt(void *chip)
492 {
493         struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)chip;
494
495         /* Disable Interrupts */
496         writeq(0, cn6xxx->intr_enb_reg64);
497
498         /* make sure interrupts are really disabled */
499         mmiowb();
500 }
501
502 static void lio_cn6xxx_get_pcie_qlmport(struct octeon_device *oct)
503 {
504         /* CN63xx Pass2 and newer parts implements the SLI_MAC_NUMBER register
505          * to determine the PCIE port #
506          */
507         oct->pcie_port = octeon_read_csr(oct, CN6XXX_SLI_MAC_NUMBER) & 0xff;
508
509         dev_dbg(&oct->pci_dev->dev, "Using PCIE Port %d\n", oct->pcie_port);
510 }
511
512 static void
513 lio_cn6xxx_process_pcie_error_intr(struct octeon_device *oct, u64 intr64)
514 {
515         dev_err(&oct->pci_dev->dev, "Error Intr: 0x%016llx\n",
516                 CVM_CAST64(intr64));
517 }
518
519 static int lio_cn6xxx_process_droq_intr_regs(struct octeon_device *oct)
520 {
521         struct octeon_droq *droq;
522         int oq_no;
523         u32 pkt_count, droq_time_mask, droq_mask, droq_int_enb;
524         u32 droq_cnt_enb, droq_cnt_mask;
525
526         droq_cnt_enb = octeon_read_csr(oct, CN6XXX_SLI_PKT_CNT_INT_ENB);
527         droq_cnt_mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_CNT_INT);
528         droq_mask = droq_cnt_mask & droq_cnt_enb;
529
530         droq_time_mask = octeon_read_csr(oct, CN6XXX_SLI_PKT_TIME_INT);
531         droq_int_enb = octeon_read_csr(oct, CN6XXX_SLI_PKT_TIME_INT_ENB);
532         droq_mask |= (droq_time_mask & droq_int_enb);
533
534         droq_mask &= oct->io_qmask.oq;
535
536         oct->droq_intr = 0;
537
538         /* for (oq_no = 0; oq_no < oct->num_oqs; oq_no++) { */
539         for (oq_no = 0; oq_no < MAX_OCTEON_OUTPUT_QUEUES(oct); oq_no++) {
540                 if (!(droq_mask & (1ULL << oq_no)))
541                         continue;
542
543                 droq = oct->droq[oq_no];
544                 pkt_count = octeon_droq_check_hw_for_pkts(droq);
545                 if (pkt_count) {
546                         oct->droq_intr |= (1ULL << oq_no);
547                         if (droq->ops.poll_mode) {
548                                 u32 value;
549                                 u32 reg;
550
551                                 struct octeon_cn6xxx *cn6xxx =
552                                         (struct octeon_cn6xxx *)oct->chip;
553
554                                 /* disable interrupts for this droq */
555                                 spin_lock
556                                         (&cn6xxx->lock_for_droq_int_enb_reg);
557                                 reg = CN6XXX_SLI_PKT_TIME_INT_ENB;
558                                 value = octeon_read_csr(oct, reg);
559                                 value &= ~(1 << oq_no);
560                                 octeon_write_csr(oct, reg, value);
561                                 reg = CN6XXX_SLI_PKT_CNT_INT_ENB;
562                                 value = octeon_read_csr(oct, reg);
563                                 value &= ~(1 << oq_no);
564                                 octeon_write_csr(oct, reg, value);
565
566                                 /* Ensure that the enable register is written.
567                                  */
568                                 mmiowb();
569
570                                 spin_unlock(&cn6xxx->lock_for_droq_int_enb_reg);
571                         }
572                 }
573         }
574
575         droq_time_mask &= oct->io_qmask.oq;
576         droq_cnt_mask &= oct->io_qmask.oq;
577
578         /* Reset the PKT_CNT/TIME_INT registers. */
579         if (droq_time_mask)
580                 octeon_write_csr(oct, CN6XXX_SLI_PKT_TIME_INT, droq_time_mask);
581
582         if (droq_cnt_mask)      /* reset PKT_CNT register:66xx */
583                 octeon_write_csr(oct, CN6XXX_SLI_PKT_CNT_INT, droq_cnt_mask);
584
585         return 0;
586 }
587
588 irqreturn_t lio_cn6xxx_process_interrupt_regs(void *dev)
589 {
590         struct octeon_device *oct = (struct octeon_device *)dev;
591         struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
592         u64 intr64;
593
594         intr64 = readq(cn6xxx->intr_sum_reg64);
595
596         /* If our device has interrupted, then proceed.
597          * Also check for all f's if interrupt was triggered on an error
598          * and the PCI read fails.
599          */
600         if (!intr64 || (intr64 == 0xFFFFFFFFFFFFFFFFULL))
601                 return IRQ_NONE;
602
603         oct->int_status = 0;
604
605         if (intr64 & CN6XXX_INTR_ERR)
606                 lio_cn6xxx_process_pcie_error_intr(oct, intr64);
607
608         if (intr64 & CN6XXX_INTR_PKT_DATA) {
609                 lio_cn6xxx_process_droq_intr_regs(oct);
610                 oct->int_status |= OCT_DEV_INTR_PKT_DATA;
611         }
612
613         if (intr64 & CN6XXX_INTR_DMA0_FORCE)
614                 oct->int_status |= OCT_DEV_INTR_DMA0_FORCE;
615
616         if (intr64 & CN6XXX_INTR_DMA1_FORCE)
617                 oct->int_status |= OCT_DEV_INTR_DMA1_FORCE;
618
619         /* Clear the current interrupts */
620         writeq(intr64, cn6xxx->intr_sum_reg64);
621
622         return IRQ_HANDLED;
623 }
624
625 void lio_cn6xxx_setup_reg_address(struct octeon_device *oct,
626                                   void *chip,
627                                   struct octeon_reg_list *reg_list)
628 {
629         u8 __iomem *bar0_pciaddr = oct->mmio[0].hw_addr;
630         struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)chip;
631
632         reg_list->pci_win_wr_addr_hi =
633                 (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_ADDR_HI);
634         reg_list->pci_win_wr_addr_lo =
635                 (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_ADDR_LO);
636         reg_list->pci_win_wr_addr =
637                 (u64 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_ADDR64);
638
639         reg_list->pci_win_rd_addr_hi =
640                 (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_ADDR_HI);
641         reg_list->pci_win_rd_addr_lo =
642                 (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_ADDR_LO);
643         reg_list->pci_win_rd_addr =
644                 (u64 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_ADDR64);
645
646         reg_list->pci_win_wr_data_hi =
647                 (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_DATA_HI);
648         reg_list->pci_win_wr_data_lo =
649                 (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_DATA_LO);
650         reg_list->pci_win_wr_data =
651                 (u64 __iomem *)(bar0_pciaddr + CN6XXX_WIN_WR_DATA64);
652
653         reg_list->pci_win_rd_data_hi =
654                 (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_DATA_HI);
655         reg_list->pci_win_rd_data_lo =
656                 (u32 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_DATA_LO);
657         reg_list->pci_win_rd_data =
658                 (u64 __iomem *)(bar0_pciaddr + CN6XXX_WIN_RD_DATA64);
659
660         lio_cn6xxx_get_pcie_qlmport(oct);
661
662         cn6xxx->intr_sum_reg64 = bar0_pciaddr + CN6XXX_SLI_INT_SUM64;
663         cn6xxx->intr_mask64 = CN6XXX_INTR_MASK;
664         cn6xxx->intr_enb_reg64 =
665                 bar0_pciaddr + CN6XXX_SLI_INT_ENB64(oct->pcie_port);
666 }
667
668 int lio_setup_cn66xx_octeon_device(struct octeon_device *oct)
669 {
670         struct octeon_cn6xxx *cn6xxx = (struct octeon_cn6xxx *)oct->chip;
671
672         if (octeon_map_pci_barx(oct, 0, 0))
673                 return 1;
674
675         if (octeon_map_pci_barx(oct, 1, MAX_BAR1_IOREMAP_SIZE)) {
676                 dev_err(&oct->pci_dev->dev, "%s CN66XX BAR1 map failed\n",
677                         __func__);
678                 octeon_unmap_pci_barx(oct, 0);
679                 return 1;
680         }
681
682         spin_lock_init(&cn6xxx->lock_for_droq_int_enb_reg);
683
684         oct->fn_list.setup_iq_regs = lio_cn66xx_setup_iq_regs;
685         oct->fn_list.setup_oq_regs = lio_cn6xxx_setup_oq_regs;
686
687         oct->fn_list.soft_reset = lio_cn6xxx_soft_reset;
688         oct->fn_list.setup_device_regs = lio_cn6xxx_setup_device_regs;
689         oct->fn_list.update_iq_read_idx = lio_cn6xxx_update_read_index;
690
691         oct->fn_list.bar1_idx_setup = lio_cn6xxx_bar1_idx_setup;
692         oct->fn_list.bar1_idx_write = lio_cn6xxx_bar1_idx_write;
693         oct->fn_list.bar1_idx_read = lio_cn6xxx_bar1_idx_read;
694
695         oct->fn_list.process_interrupt_regs = lio_cn6xxx_process_interrupt_regs;
696         oct->fn_list.enable_interrupt = lio_cn6xxx_enable_interrupt;
697         oct->fn_list.disable_interrupt = lio_cn6xxx_disable_interrupt;
698
699         oct->fn_list.enable_io_queues = lio_cn6xxx_enable_io_queues;
700         oct->fn_list.disable_io_queues = lio_cn6xxx_disable_io_queues;
701
702         lio_cn6xxx_setup_reg_address(oct, oct->chip, &oct->reg_list);
703
704         cn6xxx->conf = (struct octeon_config *)
705                        oct_get_config_info(oct, LIO_210SV);
706         if (!cn6xxx->conf) {
707                 dev_err(&oct->pci_dev->dev, "%s No Config found for CN66XX\n",
708                         __func__);
709                 octeon_unmap_pci_barx(oct, 0);
710                 octeon_unmap_pci_barx(oct, 1);
711                 return 1;
712         }
713
714         oct->coproc_clock_rate = 1000000ULL * lio_cn6xxx_coprocessor_clock(oct);
715
716         return 0;
717 }
718
719 int lio_validate_cn6xxx_config_info(struct octeon_device *oct,
720                                     struct octeon_config *conf6xxx)
721 {
722         /* int total_instrs = 0; */
723
724         if (CFG_GET_IQ_MAX_Q(conf6xxx) > CN6XXX_MAX_INPUT_QUEUES) {
725                 dev_err(&oct->pci_dev->dev, "%s: Num IQ (%d) exceeds Max (%d)\n",
726                         __func__, CFG_GET_IQ_MAX_Q(conf6xxx),
727                         CN6XXX_MAX_INPUT_QUEUES);
728                 return 1;
729         }
730
731         if (CFG_GET_OQ_MAX_Q(conf6xxx) > CN6XXX_MAX_OUTPUT_QUEUES) {
732                 dev_err(&oct->pci_dev->dev, "%s: Num OQ (%d) exceeds Max (%d)\n",
733                         __func__, CFG_GET_OQ_MAX_Q(conf6xxx),
734                         CN6XXX_MAX_OUTPUT_QUEUES);
735                 return 1;
736         }
737
738         if (CFG_GET_IQ_INSTR_TYPE(conf6xxx) != OCTEON_32BYTE_INSTR &&
739             CFG_GET_IQ_INSTR_TYPE(conf6xxx) != OCTEON_64BYTE_INSTR) {
740                 dev_err(&oct->pci_dev->dev, "%s: Invalid instr type for IQ\n",
741                         __func__);
742                 return 1;
743         }
744         if (!(CFG_GET_OQ_INFO_PTR(conf6xxx)) ||
745             !(CFG_GET_OQ_REFILL_THRESHOLD(conf6xxx))) {
746                 dev_err(&oct->pci_dev->dev, "%s: Invalid parameter for OQ\n",
747                         __func__);
748                 return 1;
749         }
750
751         if (!(CFG_GET_OQ_INTR_TIME(conf6xxx))) {
752                 dev_err(&oct->pci_dev->dev, "%s: No Time Interrupt for OQ\n",
753                         __func__);
754                 return 1;
755         }
756
757         return 0;
758 }