cxgb4: Add debugfs facility to inject FL starvation
[linux-2.6-microblaze.git] / drivers / net / ethernet / chelsio / cxgb4 / cxgb4_debugfs.c
1 /*
2  * This file is part of the Chelsio T4 Ethernet driver for Linux.
3  *
4  * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34
35 #include <linux/seq_file.h>
36 #include <linux/debugfs.h>
37 #include <linux/string_helpers.h>
38 #include <linux/sort.h>
39 #include <linux/ctype.h>
40
41 #include "cxgb4.h"
42 #include "t4_regs.h"
43 #include "t4_values.h"
44 #include "t4fw_api.h"
45 #include "cxgb4_debugfs.h"
46 #include "clip_tbl.h"
47 #include "l2t.h"
48
49 /* generic seq_file support for showing a table of size rows x width. */
50 static void *seq_tab_get_idx(struct seq_tab *tb, loff_t pos)
51 {
52         pos -= tb->skip_first;
53         return pos >= tb->rows ? NULL : &tb->data[pos * tb->width];
54 }
55
56 static void *seq_tab_start(struct seq_file *seq, loff_t *pos)
57 {
58         struct seq_tab *tb = seq->private;
59
60         if (tb->skip_first && *pos == 0)
61                 return SEQ_START_TOKEN;
62
63         return seq_tab_get_idx(tb, *pos);
64 }
65
66 static void *seq_tab_next(struct seq_file *seq, void *v, loff_t *pos)
67 {
68         v = seq_tab_get_idx(seq->private, *pos + 1);
69         if (v)
70                 ++*pos;
71         return v;
72 }
73
74 static void seq_tab_stop(struct seq_file *seq, void *v)
75 {
76 }
77
78 static int seq_tab_show(struct seq_file *seq, void *v)
79 {
80         const struct seq_tab *tb = seq->private;
81
82         return tb->show(seq, v, ((char *)v - tb->data) / tb->width);
83 }
84
85 static const struct seq_operations seq_tab_ops = {
86         .start = seq_tab_start,
87         .next  = seq_tab_next,
88         .stop  = seq_tab_stop,
89         .show  = seq_tab_show
90 };
91
92 struct seq_tab *seq_open_tab(struct file *f, unsigned int rows,
93                              unsigned int width, unsigned int have_header,
94                              int (*show)(struct seq_file *seq, void *v, int i))
95 {
96         struct seq_tab *p;
97
98         p = __seq_open_private(f, &seq_tab_ops, sizeof(*p) + rows * width);
99         if (p) {
100                 p->show = show;
101                 p->rows = rows;
102                 p->width = width;
103                 p->skip_first = have_header != 0;
104         }
105         return p;
106 }
107
108 /* Trim the size of a seq_tab to the supplied number of rows.  The operation is
109  * irreversible.
110  */
111 static int seq_tab_trim(struct seq_tab *p, unsigned int new_rows)
112 {
113         if (new_rows > p->rows)
114                 return -EINVAL;
115         p->rows = new_rows;
116         return 0;
117 }
118
119 static int cim_la_show(struct seq_file *seq, void *v, int idx)
120 {
121         if (v == SEQ_START_TOKEN)
122                 seq_puts(seq, "Status   Data      PC     LS0Stat  LS0Addr "
123                          "            LS0Data\n");
124         else {
125                 const u32 *p = v;
126
127                 seq_printf(seq,
128                            "  %02x  %x%07x %x%07x %08x %08x %08x%08x%08x%08x\n",
129                            (p[0] >> 4) & 0xff, p[0] & 0xf, p[1] >> 4,
130                            p[1] & 0xf, p[2] >> 4, p[2] & 0xf, p[3], p[4], p[5],
131                            p[6], p[7]);
132         }
133         return 0;
134 }
135
136 static int cim_la_show_3in1(struct seq_file *seq, void *v, int idx)
137 {
138         if (v == SEQ_START_TOKEN) {
139                 seq_puts(seq, "Status   Data      PC\n");
140         } else {
141                 const u32 *p = v;
142
143                 seq_printf(seq, "  %02x   %08x %08x\n", p[5] & 0xff, p[6],
144                            p[7]);
145                 seq_printf(seq, "  %02x   %02x%06x %02x%06x\n",
146                            (p[3] >> 8) & 0xff, p[3] & 0xff, p[4] >> 8,
147                            p[4] & 0xff, p[5] >> 8);
148                 seq_printf(seq, "  %02x   %x%07x %x%07x\n", (p[0] >> 4) & 0xff,
149                            p[0] & 0xf, p[1] >> 4, p[1] & 0xf, p[2] >> 4);
150         }
151         return 0;
152 }
153
154 static int cim_la_open(struct inode *inode, struct file *file)
155 {
156         int ret;
157         unsigned int cfg;
158         struct seq_tab *p;
159         struct adapter *adap = inode->i_private;
160
161         ret = t4_cim_read(adap, UP_UP_DBG_LA_CFG_A, 1, &cfg);
162         if (ret)
163                 return ret;
164
165         p = seq_open_tab(file, adap->params.cim_la_size / 8, 8 * sizeof(u32), 1,
166                          cfg & UPDBGLACAPTPCONLY_F ?
167                          cim_la_show_3in1 : cim_la_show);
168         if (!p)
169                 return -ENOMEM;
170
171         ret = t4_cim_read_la(adap, (u32 *)p->data, NULL);
172         if (ret)
173                 seq_release_private(inode, file);
174         return ret;
175 }
176
177 static const struct file_operations cim_la_fops = {
178         .owner   = THIS_MODULE,
179         .open    = cim_la_open,
180         .read    = seq_read,
181         .llseek  = seq_lseek,
182         .release = seq_release_private
183 };
184
185 static int cim_qcfg_show(struct seq_file *seq, void *v)
186 {
187         static const char * const qname[] = {
188                 "TP0", "TP1", "ULP", "SGE0", "SGE1", "NC-SI",
189                 "ULP0", "ULP1", "ULP2", "ULP3", "SGE", "NC-SI",
190                 "SGE0-RX", "SGE1-RX"
191         };
192
193         int i;
194         struct adapter *adap = seq->private;
195         u16 base[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
196         u16 size[CIM_NUM_IBQ + CIM_NUM_OBQ_T5];
197         u32 stat[(4 * (CIM_NUM_IBQ + CIM_NUM_OBQ_T5))];
198         u16 thres[CIM_NUM_IBQ];
199         u32 obq_wr_t4[2 * CIM_NUM_OBQ], *wr;
200         u32 obq_wr_t5[2 * CIM_NUM_OBQ_T5];
201         u32 *p = stat;
202         int cim_num_obq = is_t4(adap->params.chip) ?
203                                 CIM_NUM_OBQ : CIM_NUM_OBQ_T5;
204
205         i = t4_cim_read(adap, is_t4(adap->params.chip) ? UP_IBQ_0_RDADDR_A :
206                         UP_IBQ_0_SHADOW_RDADDR_A,
207                         ARRAY_SIZE(stat), stat);
208         if (!i) {
209                 if (is_t4(adap->params.chip)) {
210                         i = t4_cim_read(adap, UP_OBQ_0_REALADDR_A,
211                                         ARRAY_SIZE(obq_wr_t4), obq_wr_t4);
212                                 wr = obq_wr_t4;
213                 } else {
214                         i = t4_cim_read(adap, UP_OBQ_0_SHADOW_REALADDR_A,
215                                         ARRAY_SIZE(obq_wr_t5), obq_wr_t5);
216                                 wr = obq_wr_t5;
217                 }
218         }
219         if (i)
220                 return i;
221
222         t4_read_cimq_cfg(adap, base, size, thres);
223
224         seq_printf(seq,
225                    "  Queue  Base  Size Thres  RdPtr WrPtr  SOP  EOP Avail\n");
226         for (i = 0; i < CIM_NUM_IBQ; i++, p += 4)
227                 seq_printf(seq, "%7s %5x %5u %5u %6x  %4x %4u %4u %5u\n",
228                            qname[i], base[i], size[i], thres[i],
229                            IBQRDADDR_G(p[0]), IBQWRADDR_G(p[1]),
230                            QUESOPCNT_G(p[3]), QUEEOPCNT_G(p[3]),
231                            QUEREMFLITS_G(p[2]) * 16);
232         for ( ; i < CIM_NUM_IBQ + cim_num_obq; i++, p += 4, wr += 2)
233                 seq_printf(seq, "%7s %5x %5u %12x  %4x %4u %4u %5u\n",
234                            qname[i], base[i], size[i],
235                            QUERDADDR_G(p[0]) & 0x3fff, wr[0] - base[i],
236                            QUESOPCNT_G(p[3]), QUEEOPCNT_G(p[3]),
237                            QUEREMFLITS_G(p[2]) * 16);
238         return 0;
239 }
240
241 static int cim_qcfg_open(struct inode *inode, struct file *file)
242 {
243         return single_open(file, cim_qcfg_show, inode->i_private);
244 }
245
246 static const struct file_operations cim_qcfg_fops = {
247         .owner   = THIS_MODULE,
248         .open    = cim_qcfg_open,
249         .read    = seq_read,
250         .llseek  = seq_lseek,
251         .release = single_release,
252 };
253
254 static int cimq_show(struct seq_file *seq, void *v, int idx)
255 {
256         const u32 *p = v;
257
258         seq_printf(seq, "%#06x: %08x %08x %08x %08x\n", idx * 16, p[0], p[1],
259                    p[2], p[3]);
260         return 0;
261 }
262
263 static int cim_ibq_open(struct inode *inode, struct file *file)
264 {
265         int ret;
266         struct seq_tab *p;
267         unsigned int qid = (uintptr_t)inode->i_private & 7;
268         struct adapter *adap = inode->i_private - qid;
269
270         p = seq_open_tab(file, CIM_IBQ_SIZE, 4 * sizeof(u32), 0, cimq_show);
271         if (!p)
272                 return -ENOMEM;
273
274         ret = t4_read_cim_ibq(adap, qid, (u32 *)p->data, CIM_IBQ_SIZE * 4);
275         if (ret < 0)
276                 seq_release_private(inode, file);
277         else
278                 ret = 0;
279         return ret;
280 }
281
282 static const struct file_operations cim_ibq_fops = {
283         .owner   = THIS_MODULE,
284         .open    = cim_ibq_open,
285         .read    = seq_read,
286         .llseek  = seq_lseek,
287         .release = seq_release_private
288 };
289
290 static int cim_obq_open(struct inode *inode, struct file *file)
291 {
292         int ret;
293         struct seq_tab *p;
294         unsigned int qid = (uintptr_t)inode->i_private & 7;
295         struct adapter *adap = inode->i_private - qid;
296
297         p = seq_open_tab(file, 6 * CIM_OBQ_SIZE, 4 * sizeof(u32), 0, cimq_show);
298         if (!p)
299                 return -ENOMEM;
300
301         ret = t4_read_cim_obq(adap, qid, (u32 *)p->data, 6 * CIM_OBQ_SIZE * 4);
302         if (ret < 0) {
303                 seq_release_private(inode, file);
304         } else {
305                 seq_tab_trim(p, ret / 4);
306                 ret = 0;
307         }
308         return ret;
309 }
310
311 static const struct file_operations cim_obq_fops = {
312         .owner   = THIS_MODULE,
313         .open    = cim_obq_open,
314         .read    = seq_read,
315         .llseek  = seq_lseek,
316         .release = seq_release_private
317 };
318
319 struct field_desc {
320         const char *name;
321         unsigned int start;
322         unsigned int width;
323 };
324
325 static void field_desc_show(struct seq_file *seq, u64 v,
326                             const struct field_desc *p)
327 {
328         char buf[32];
329         int line_size = 0;
330
331         while (p->name) {
332                 u64 mask = (1ULL << p->width) - 1;
333                 int len = scnprintf(buf, sizeof(buf), "%s: %llu", p->name,
334                                     ((unsigned long long)v >> p->start) & mask);
335
336                 if (line_size + len >= 79) {
337                         line_size = 8;
338                         seq_puts(seq, "\n        ");
339                 }
340                 seq_printf(seq, "%s ", buf);
341                 line_size += len + 1;
342                 p++;
343         }
344         seq_putc(seq, '\n');
345 }
346
347 static struct field_desc tp_la0[] = {
348         { "RcfOpCodeOut", 60, 4 },
349         { "State", 56, 4 },
350         { "WcfState", 52, 4 },
351         { "RcfOpcSrcOut", 50, 2 },
352         { "CRxError", 49, 1 },
353         { "ERxError", 48, 1 },
354         { "SanityFailed", 47, 1 },
355         { "SpuriousMsg", 46, 1 },
356         { "FlushInputMsg", 45, 1 },
357         { "FlushInputCpl", 44, 1 },
358         { "RssUpBit", 43, 1 },
359         { "RssFilterHit", 42, 1 },
360         { "Tid", 32, 10 },
361         { "InitTcb", 31, 1 },
362         { "LineNumber", 24, 7 },
363         { "Emsg", 23, 1 },
364         { "EdataOut", 22, 1 },
365         { "Cmsg", 21, 1 },
366         { "CdataOut", 20, 1 },
367         { "EreadPdu", 19, 1 },
368         { "CreadPdu", 18, 1 },
369         { "TunnelPkt", 17, 1 },
370         { "RcfPeerFin", 16, 1 },
371         { "RcfReasonOut", 12, 4 },
372         { "TxCchannel", 10, 2 },
373         { "RcfTxChannel", 8, 2 },
374         { "RxEchannel", 6, 2 },
375         { "RcfRxChannel", 5, 1 },
376         { "RcfDataOutSrdy", 4, 1 },
377         { "RxDvld", 3, 1 },
378         { "RxOoDvld", 2, 1 },
379         { "RxCongestion", 1, 1 },
380         { "TxCongestion", 0, 1 },
381         { NULL }
382 };
383
384 static int tp_la_show(struct seq_file *seq, void *v, int idx)
385 {
386         const u64 *p = v;
387
388         field_desc_show(seq, *p, tp_la0);
389         return 0;
390 }
391
392 static int tp_la_show2(struct seq_file *seq, void *v, int idx)
393 {
394         const u64 *p = v;
395
396         if (idx)
397                 seq_putc(seq, '\n');
398         field_desc_show(seq, p[0], tp_la0);
399         if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
400                 field_desc_show(seq, p[1], tp_la0);
401         return 0;
402 }
403
404 static int tp_la_show3(struct seq_file *seq, void *v, int idx)
405 {
406         static struct field_desc tp_la1[] = {
407                 { "CplCmdIn", 56, 8 },
408                 { "CplCmdOut", 48, 8 },
409                 { "ESynOut", 47, 1 },
410                 { "EAckOut", 46, 1 },
411                 { "EFinOut", 45, 1 },
412                 { "ERstOut", 44, 1 },
413                 { "SynIn", 43, 1 },
414                 { "AckIn", 42, 1 },
415                 { "FinIn", 41, 1 },
416                 { "RstIn", 40, 1 },
417                 { "DataIn", 39, 1 },
418                 { "DataInVld", 38, 1 },
419                 { "PadIn", 37, 1 },
420                 { "RxBufEmpty", 36, 1 },
421                 { "RxDdp", 35, 1 },
422                 { "RxFbCongestion", 34, 1 },
423                 { "TxFbCongestion", 33, 1 },
424                 { "TxPktSumSrdy", 32, 1 },
425                 { "RcfUlpType", 28, 4 },
426                 { "Eread", 27, 1 },
427                 { "Ebypass", 26, 1 },
428                 { "Esave", 25, 1 },
429                 { "Static0", 24, 1 },
430                 { "Cread", 23, 1 },
431                 { "Cbypass", 22, 1 },
432                 { "Csave", 21, 1 },
433                 { "CPktOut", 20, 1 },
434                 { "RxPagePoolFull", 18, 2 },
435                 { "RxLpbkPkt", 17, 1 },
436                 { "TxLpbkPkt", 16, 1 },
437                 { "RxVfValid", 15, 1 },
438                 { "SynLearned", 14, 1 },
439                 { "SetDelEntry", 13, 1 },
440                 { "SetInvEntry", 12, 1 },
441                 { "CpcmdDvld", 11, 1 },
442                 { "CpcmdSave", 10, 1 },
443                 { "RxPstructsFull", 8, 2 },
444                 { "EpcmdDvld", 7, 1 },
445                 { "EpcmdFlush", 6, 1 },
446                 { "EpcmdTrimPrefix", 5, 1 },
447                 { "EpcmdTrimPostfix", 4, 1 },
448                 { "ERssIp4Pkt", 3, 1 },
449                 { "ERssIp6Pkt", 2, 1 },
450                 { "ERssTcpUdpPkt", 1, 1 },
451                 { "ERssFceFipPkt", 0, 1 },
452                 { NULL }
453         };
454         static struct field_desc tp_la2[] = {
455                 { "CplCmdIn", 56, 8 },
456                 { "MpsVfVld", 55, 1 },
457                 { "MpsPf", 52, 3 },
458                 { "MpsVf", 44, 8 },
459                 { "SynIn", 43, 1 },
460                 { "AckIn", 42, 1 },
461                 { "FinIn", 41, 1 },
462                 { "RstIn", 40, 1 },
463                 { "DataIn", 39, 1 },
464                 { "DataInVld", 38, 1 },
465                 { "PadIn", 37, 1 },
466                 { "RxBufEmpty", 36, 1 },
467                 { "RxDdp", 35, 1 },
468                 { "RxFbCongestion", 34, 1 },
469                 { "TxFbCongestion", 33, 1 },
470                 { "TxPktSumSrdy", 32, 1 },
471                 { "RcfUlpType", 28, 4 },
472                 { "Eread", 27, 1 },
473                 { "Ebypass", 26, 1 },
474                 { "Esave", 25, 1 },
475                 { "Static0", 24, 1 },
476                 { "Cread", 23, 1 },
477                 { "Cbypass", 22, 1 },
478                 { "Csave", 21, 1 },
479                 { "CPktOut", 20, 1 },
480                 { "RxPagePoolFull", 18, 2 },
481                 { "RxLpbkPkt", 17, 1 },
482                 { "TxLpbkPkt", 16, 1 },
483                 { "RxVfValid", 15, 1 },
484                 { "SynLearned", 14, 1 },
485                 { "SetDelEntry", 13, 1 },
486                 { "SetInvEntry", 12, 1 },
487                 { "CpcmdDvld", 11, 1 },
488                 { "CpcmdSave", 10, 1 },
489                 { "RxPstructsFull", 8, 2 },
490                 { "EpcmdDvld", 7, 1 },
491                 { "EpcmdFlush", 6, 1 },
492                 { "EpcmdTrimPrefix", 5, 1 },
493                 { "EpcmdTrimPostfix", 4, 1 },
494                 { "ERssIp4Pkt", 3, 1 },
495                 { "ERssIp6Pkt", 2, 1 },
496                 { "ERssTcpUdpPkt", 1, 1 },
497                 { "ERssFceFipPkt", 0, 1 },
498                 { NULL }
499         };
500         const u64 *p = v;
501
502         if (idx)
503                 seq_putc(seq, '\n');
504         field_desc_show(seq, p[0], tp_la0);
505         if (idx < (TPLA_SIZE / 2 - 1) || p[1] != ~0ULL)
506                 field_desc_show(seq, p[1], (p[0] & BIT(17)) ? tp_la2 : tp_la1);
507         return 0;
508 }
509
510 static int tp_la_open(struct inode *inode, struct file *file)
511 {
512         struct seq_tab *p;
513         struct adapter *adap = inode->i_private;
514
515         switch (DBGLAMODE_G(t4_read_reg(adap, TP_DBG_LA_CONFIG_A))) {
516         case 2:
517                 p = seq_open_tab(file, TPLA_SIZE / 2, 2 * sizeof(u64), 0,
518                                  tp_la_show2);
519                 break;
520         case 3:
521                 p = seq_open_tab(file, TPLA_SIZE / 2, 2 * sizeof(u64), 0,
522                                  tp_la_show3);
523                 break;
524         default:
525                 p = seq_open_tab(file, TPLA_SIZE, sizeof(u64), 0, tp_la_show);
526         }
527         if (!p)
528                 return -ENOMEM;
529
530         t4_tp_read_la(adap, (u64 *)p->data, NULL);
531         return 0;
532 }
533
534 static ssize_t tp_la_write(struct file *file, const char __user *buf,
535                            size_t count, loff_t *pos)
536 {
537         int err;
538         char s[32];
539         unsigned long val;
540         size_t size = min(sizeof(s) - 1, count);
541         struct adapter *adap = file_inode(file)->i_private;
542
543         if (copy_from_user(s, buf, size))
544                 return -EFAULT;
545         s[size] = '\0';
546         err = kstrtoul(s, 0, &val);
547         if (err)
548                 return err;
549         if (val > 0xffff)
550                 return -EINVAL;
551         adap->params.tp.la_mask = val << 16;
552         t4_set_reg_field(adap, TP_DBG_LA_CONFIG_A, 0xffff0000U,
553                          adap->params.tp.la_mask);
554         return count;
555 }
556
557 static const struct file_operations tp_la_fops = {
558         .owner   = THIS_MODULE,
559         .open    = tp_la_open,
560         .read    = seq_read,
561         .llseek  = seq_lseek,
562         .release = seq_release_private,
563         .write   = tp_la_write
564 };
565
566 static int ulprx_la_show(struct seq_file *seq, void *v, int idx)
567 {
568         const u32 *p = v;
569
570         if (v == SEQ_START_TOKEN)
571                 seq_puts(seq, "      Pcmd        Type   Message"
572                          "                Data\n");
573         else
574                 seq_printf(seq, "%08x%08x  %4x  %08x  %08x%08x%08x%08x\n",
575                            p[1], p[0], p[2], p[3], p[7], p[6], p[5], p[4]);
576         return 0;
577 }
578
579 static int ulprx_la_open(struct inode *inode, struct file *file)
580 {
581         struct seq_tab *p;
582         struct adapter *adap = inode->i_private;
583
584         p = seq_open_tab(file, ULPRX_LA_SIZE, 8 * sizeof(u32), 1,
585                          ulprx_la_show);
586         if (!p)
587                 return -ENOMEM;
588
589         t4_ulprx_read_la(adap, (u32 *)p->data);
590         return 0;
591 }
592
593 static const struct file_operations ulprx_la_fops = {
594         .owner   = THIS_MODULE,
595         .open    = ulprx_la_open,
596         .read    = seq_read,
597         .llseek  = seq_lseek,
598         .release = seq_release_private
599 };
600
601 /* Show the PM memory stats.  These stats include:
602  *
603  * TX:
604  *   Read: memory read operation
605  *   Write Bypass: cut-through
606  *   Bypass + mem: cut-through and save copy
607  *
608  * RX:
609  *   Read: memory read
610  *   Write Bypass: cut-through
611  *   Flush: payload trim or drop
612  */
613 static int pm_stats_show(struct seq_file *seq, void *v)
614 {
615         static const char * const tx_pm_stats[] = {
616                 "Read:", "Write bypass:", "Write mem:", "Bypass + mem:"
617         };
618         static const char * const rx_pm_stats[] = {
619                 "Read:", "Write bypass:", "Write mem:", "Flush:"
620         };
621
622         int i;
623         u32 tx_cnt[PM_NSTATS], rx_cnt[PM_NSTATS];
624         u64 tx_cyc[PM_NSTATS], rx_cyc[PM_NSTATS];
625         struct adapter *adap = seq->private;
626
627         t4_pmtx_get_stats(adap, tx_cnt, tx_cyc);
628         t4_pmrx_get_stats(adap, rx_cnt, rx_cyc);
629
630         seq_printf(seq, "%13s %10s  %20s\n", " ", "Tx pcmds", "Tx bytes");
631         for (i = 0; i < PM_NSTATS - 1; i++)
632                 seq_printf(seq, "%-13s %10u  %20llu\n",
633                            tx_pm_stats[i], tx_cnt[i], tx_cyc[i]);
634
635         seq_printf(seq, "%13s %10s  %20s\n", " ", "Rx pcmds", "Rx bytes");
636         for (i = 0; i < PM_NSTATS - 1; i++)
637                 seq_printf(seq, "%-13s %10u  %20llu\n",
638                            rx_pm_stats[i], rx_cnt[i], rx_cyc[i]);
639         return 0;
640 }
641
642 static int pm_stats_open(struct inode *inode, struct file *file)
643 {
644         return single_open(file, pm_stats_show, inode->i_private);
645 }
646
647 static ssize_t pm_stats_clear(struct file *file, const char __user *buf,
648                               size_t count, loff_t *pos)
649 {
650         struct adapter *adap = file_inode(file)->i_private;
651
652         t4_write_reg(adap, PM_RX_STAT_CONFIG_A, 0);
653         t4_write_reg(adap, PM_TX_STAT_CONFIG_A, 0);
654         return count;
655 }
656
657 static const struct file_operations pm_stats_debugfs_fops = {
658         .owner   = THIS_MODULE,
659         .open    = pm_stats_open,
660         .read    = seq_read,
661         .llseek  = seq_lseek,
662         .release = single_release,
663         .write   = pm_stats_clear
664 };
665
666 static int cctrl_tbl_show(struct seq_file *seq, void *v)
667 {
668         static const char * const dec_fac[] = {
669                 "0.5", "0.5625", "0.625", "0.6875", "0.75", "0.8125", "0.875",
670                 "0.9375" };
671
672         int i;
673         u16 (*incr)[NCCTRL_WIN];
674         struct adapter *adap = seq->private;
675
676         incr = kmalloc(sizeof(*incr) * NMTUS, GFP_KERNEL);
677         if (!incr)
678                 return -ENOMEM;
679
680         t4_read_cong_tbl(adap, incr);
681
682         for (i = 0; i < NCCTRL_WIN; ++i) {
683                 seq_printf(seq, "%2d: %4u %4u %4u %4u %4u %4u %4u %4u\n", i,
684                            incr[0][i], incr[1][i], incr[2][i], incr[3][i],
685                            incr[4][i], incr[5][i], incr[6][i], incr[7][i]);
686                 seq_printf(seq, "%8u %4u %4u %4u %4u %4u %4u %4u %5u %s\n",
687                            incr[8][i], incr[9][i], incr[10][i], incr[11][i],
688                            incr[12][i], incr[13][i], incr[14][i], incr[15][i],
689                            adap->params.a_wnd[i],
690                            dec_fac[adap->params.b_wnd[i]]);
691         }
692
693         kfree(incr);
694         return 0;
695 }
696
697 DEFINE_SIMPLE_DEBUGFS_FILE(cctrl_tbl);
698
699 /* Format a value in a unit that differs from the value's native unit by the
700  * given factor.
701  */
702 static char *unit_conv(char *buf, size_t len, unsigned int val,
703                        unsigned int factor)
704 {
705         unsigned int rem = val % factor;
706
707         if (rem == 0) {
708                 snprintf(buf, len, "%u", val / factor);
709         } else {
710                 while (rem % 10 == 0)
711                         rem /= 10;
712                 snprintf(buf, len, "%u.%u", val / factor, rem);
713         }
714         return buf;
715 }
716
717 static int clk_show(struct seq_file *seq, void *v)
718 {
719         char buf[32];
720         struct adapter *adap = seq->private;
721         unsigned int cclk_ps = 1000000000 / adap->params.vpd.cclk;  /* in ps */
722         u32 res = t4_read_reg(adap, TP_TIMER_RESOLUTION_A);
723         unsigned int tre = TIMERRESOLUTION_G(res);
724         unsigned int dack_re = DELAYEDACKRESOLUTION_G(res);
725         unsigned long long tp_tick_us = (cclk_ps << tre) / 1000000; /* in us */
726
727         seq_printf(seq, "Core clock period: %s ns\n",
728                    unit_conv(buf, sizeof(buf), cclk_ps, 1000));
729         seq_printf(seq, "TP timer tick: %s us\n",
730                    unit_conv(buf, sizeof(buf), (cclk_ps << tre), 1000000));
731         seq_printf(seq, "TCP timestamp tick: %s us\n",
732                    unit_conv(buf, sizeof(buf),
733                              (cclk_ps << TIMESTAMPRESOLUTION_G(res)), 1000000));
734         seq_printf(seq, "DACK tick: %s us\n",
735                    unit_conv(buf, sizeof(buf), (cclk_ps << dack_re), 1000000));
736         seq_printf(seq, "DACK timer: %u us\n",
737                    ((cclk_ps << dack_re) / 1000000) *
738                    t4_read_reg(adap, TP_DACK_TIMER_A));
739         seq_printf(seq, "Retransmit min: %llu us\n",
740                    tp_tick_us * t4_read_reg(adap, TP_RXT_MIN_A));
741         seq_printf(seq, "Retransmit max: %llu us\n",
742                    tp_tick_us * t4_read_reg(adap, TP_RXT_MAX_A));
743         seq_printf(seq, "Persist timer min: %llu us\n",
744                    tp_tick_us * t4_read_reg(adap, TP_PERS_MIN_A));
745         seq_printf(seq, "Persist timer max: %llu us\n",
746                    tp_tick_us * t4_read_reg(adap, TP_PERS_MAX_A));
747         seq_printf(seq, "Keepalive idle timer: %llu us\n",
748                    tp_tick_us * t4_read_reg(adap, TP_KEEP_IDLE_A));
749         seq_printf(seq, "Keepalive interval: %llu us\n",
750                    tp_tick_us * t4_read_reg(adap, TP_KEEP_INTVL_A));
751         seq_printf(seq, "Initial SRTT: %llu us\n",
752                    tp_tick_us * INITSRTT_G(t4_read_reg(adap, TP_INIT_SRTT_A)));
753         seq_printf(seq, "FINWAIT2 timer: %llu us\n",
754                    tp_tick_us * t4_read_reg(adap, TP_FINWAIT2_TIMER_A));
755
756         return 0;
757 }
758
759 DEFINE_SIMPLE_DEBUGFS_FILE(clk);
760
761 /* Firmware Device Log dump. */
762 static const char * const devlog_level_strings[] = {
763         [FW_DEVLOG_LEVEL_EMERG]         = "EMERG",
764         [FW_DEVLOG_LEVEL_CRIT]          = "CRIT",
765         [FW_DEVLOG_LEVEL_ERR]           = "ERR",
766         [FW_DEVLOG_LEVEL_NOTICE]        = "NOTICE",
767         [FW_DEVLOG_LEVEL_INFO]          = "INFO",
768         [FW_DEVLOG_LEVEL_DEBUG]         = "DEBUG"
769 };
770
771 static const char * const devlog_facility_strings[] = {
772         [FW_DEVLOG_FACILITY_CORE]       = "CORE",
773         [FW_DEVLOG_FACILITY_SCHED]      = "SCHED",
774         [FW_DEVLOG_FACILITY_TIMER]      = "TIMER",
775         [FW_DEVLOG_FACILITY_RES]        = "RES",
776         [FW_DEVLOG_FACILITY_HW]         = "HW",
777         [FW_DEVLOG_FACILITY_FLR]        = "FLR",
778         [FW_DEVLOG_FACILITY_DMAQ]       = "DMAQ",
779         [FW_DEVLOG_FACILITY_PHY]        = "PHY",
780         [FW_DEVLOG_FACILITY_MAC]        = "MAC",
781         [FW_DEVLOG_FACILITY_PORT]       = "PORT",
782         [FW_DEVLOG_FACILITY_VI]         = "VI",
783         [FW_DEVLOG_FACILITY_FILTER]     = "FILTER",
784         [FW_DEVLOG_FACILITY_ACL]        = "ACL",
785         [FW_DEVLOG_FACILITY_TM]         = "TM",
786         [FW_DEVLOG_FACILITY_QFC]        = "QFC",
787         [FW_DEVLOG_FACILITY_DCB]        = "DCB",
788         [FW_DEVLOG_FACILITY_ETH]        = "ETH",
789         [FW_DEVLOG_FACILITY_OFLD]       = "OFLD",
790         [FW_DEVLOG_FACILITY_RI]         = "RI",
791         [FW_DEVLOG_FACILITY_ISCSI]      = "ISCSI",
792         [FW_DEVLOG_FACILITY_FCOE]       = "FCOE",
793         [FW_DEVLOG_FACILITY_FOISCSI]    = "FOISCSI",
794         [FW_DEVLOG_FACILITY_FOFCOE]     = "FOFCOE"
795 };
796
797 /* Information gathered by Device Log Open routine for the display routine.
798  */
799 struct devlog_info {
800         unsigned int nentries;          /* number of entries in log[] */
801         unsigned int first;             /* first [temporal] entry in log[] */
802         struct fw_devlog_e log[0];      /* Firmware Device Log */
803 };
804
805 /* Dump a Firmaware Device Log entry.
806  */
807 static int devlog_show(struct seq_file *seq, void *v)
808 {
809         if (v == SEQ_START_TOKEN)
810                 seq_printf(seq, "%10s  %15s  %8s  %8s  %s\n",
811                            "Seq#", "Tstamp", "Level", "Facility", "Message");
812         else {
813                 struct devlog_info *dinfo = seq->private;
814                 int fidx = (uintptr_t)v - 2;
815                 unsigned long index;
816                 struct fw_devlog_e *e;
817
818                 /* Get a pointer to the log entry to display.  Skip unused log
819                  * entries.
820                  */
821                 index = dinfo->first + fidx;
822                 if (index >= dinfo->nentries)
823                         index -= dinfo->nentries;
824                 e = &dinfo->log[index];
825                 if (e->timestamp == 0)
826                         return 0;
827
828                 /* Print the message.  This depends on the firmware using
829                  * exactly the same formating strings as the kernel so we may
830                  * eventually have to put a format interpreter in here ...
831                  */
832                 seq_printf(seq, "%10d  %15llu  %8s  %8s  ",
833                            e->seqno, e->timestamp,
834                            (e->level < ARRAY_SIZE(devlog_level_strings)
835                             ? devlog_level_strings[e->level]
836                             : "UNKNOWN"),
837                            (e->facility < ARRAY_SIZE(devlog_facility_strings)
838                             ? devlog_facility_strings[e->facility]
839                             : "UNKNOWN"));
840                 seq_printf(seq, e->fmt, e->params[0], e->params[1],
841                            e->params[2], e->params[3], e->params[4],
842                            e->params[5], e->params[6], e->params[7]);
843         }
844         return 0;
845 }
846
847 /* Sequential File Operations for Device Log.
848  */
849 static inline void *devlog_get_idx(struct devlog_info *dinfo, loff_t pos)
850 {
851         if (pos > dinfo->nentries)
852                 return NULL;
853
854         return (void *)(uintptr_t)(pos + 1);
855 }
856
857 static void *devlog_start(struct seq_file *seq, loff_t *pos)
858 {
859         struct devlog_info *dinfo = seq->private;
860
861         return (*pos
862                 ? devlog_get_idx(dinfo, *pos)
863                 : SEQ_START_TOKEN);
864 }
865
866 static void *devlog_next(struct seq_file *seq, void *v, loff_t *pos)
867 {
868         struct devlog_info *dinfo = seq->private;
869
870         (*pos)++;
871         return devlog_get_idx(dinfo, *pos);
872 }
873
874 static void devlog_stop(struct seq_file *seq, void *v)
875 {
876 }
877
878 static const struct seq_operations devlog_seq_ops = {
879         .start = devlog_start,
880         .next  = devlog_next,
881         .stop  = devlog_stop,
882         .show  = devlog_show
883 };
884
885 /* Set up for reading the firmware's device log.  We read the entire log here
886  * and then display it incrementally in devlog_show().
887  */
888 static int devlog_open(struct inode *inode, struct file *file)
889 {
890         struct adapter *adap = inode->i_private;
891         struct devlog_params *dparams = &adap->params.devlog;
892         struct devlog_info *dinfo;
893         unsigned int index;
894         u32 fseqno;
895         int ret;
896
897         /* If we don't know where the log is we can't do anything.
898          */
899         if (dparams->start == 0)
900                 return -ENXIO;
901
902         /* Allocate the space to read in the firmware's device log and set up
903          * for the iterated call to our display function.
904          */
905         dinfo = __seq_open_private(file, &devlog_seq_ops,
906                                    sizeof(*dinfo) + dparams->size);
907         if (!dinfo)
908                 return -ENOMEM;
909
910         /* Record the basic log buffer information and read in the raw log.
911          */
912         dinfo->nentries = (dparams->size / sizeof(struct fw_devlog_e));
913         dinfo->first = 0;
914         spin_lock(&adap->win0_lock);
915         ret = t4_memory_rw(adap, adap->params.drv_memwin, dparams->memtype,
916                            dparams->start, dparams->size, (__be32 *)dinfo->log,
917                            T4_MEMORY_READ);
918         spin_unlock(&adap->win0_lock);
919         if (ret) {
920                 seq_release_private(inode, file);
921                 return ret;
922         }
923
924         /* Translate log multi-byte integral elements into host native format
925          * and determine where the first entry in the log is.
926          */
927         for (fseqno = ~((u32)0), index = 0; index < dinfo->nentries; index++) {
928                 struct fw_devlog_e *e = &dinfo->log[index];
929                 int i;
930                 __u32 seqno;
931
932                 if (e->timestamp == 0)
933                         continue;
934
935                 e->timestamp = (__force __be64)be64_to_cpu(e->timestamp);
936                 seqno = be32_to_cpu(e->seqno);
937                 for (i = 0; i < 8; i++)
938                         e->params[i] =
939                                 (__force __be32)be32_to_cpu(e->params[i]);
940
941                 if (seqno < fseqno) {
942                         fseqno = seqno;
943                         dinfo->first = index;
944                 }
945         }
946         return 0;
947 }
948
949 static const struct file_operations devlog_fops = {
950         .owner   = THIS_MODULE,
951         .open    = devlog_open,
952         .read    = seq_read,
953         .llseek  = seq_lseek,
954         .release = seq_release_private
955 };
956
957 static int mbox_show(struct seq_file *seq, void *v)
958 {
959         static const char * const owner[] = { "none", "FW", "driver",
960                                               "unknown" };
961
962         int i;
963         unsigned int mbox = (uintptr_t)seq->private & 7;
964         struct adapter *adap = seq->private - mbox;
965         void __iomem *addr = adap->regs + PF_REG(mbox, CIM_PF_MAILBOX_DATA_A);
966         unsigned int ctrl_reg = (is_t4(adap->params.chip)
967                                  ? CIM_PF_MAILBOX_CTRL_A
968                                  : CIM_PF_MAILBOX_CTRL_SHADOW_COPY_A);
969         void __iomem *ctrl = adap->regs + PF_REG(mbox, ctrl_reg);
970
971         i = MBOWNER_G(readl(ctrl));
972         seq_printf(seq, "mailbox owned by %s\n\n", owner[i]);
973
974         for (i = 0; i < MBOX_LEN; i += 8)
975                 seq_printf(seq, "%016llx\n",
976                            (unsigned long long)readq(addr + i));
977         return 0;
978 }
979
980 static int mbox_open(struct inode *inode, struct file *file)
981 {
982         return single_open(file, mbox_show, inode->i_private);
983 }
984
985 static ssize_t mbox_write(struct file *file, const char __user *buf,
986                           size_t count, loff_t *pos)
987 {
988         int i;
989         char c = '\n', s[256];
990         unsigned long long data[8];
991         const struct inode *ino;
992         unsigned int mbox;
993         struct adapter *adap;
994         void __iomem *addr;
995         void __iomem *ctrl;
996
997         if (count > sizeof(s) - 1 || !count)
998                 return -EINVAL;
999         if (copy_from_user(s, buf, count))
1000                 return -EFAULT;
1001         s[count] = '\0';
1002
1003         if (sscanf(s, "%llx %llx %llx %llx %llx %llx %llx %llx%c", &data[0],
1004                    &data[1], &data[2], &data[3], &data[4], &data[5], &data[6],
1005                    &data[7], &c) < 8 || c != '\n')
1006                 return -EINVAL;
1007
1008         ino = file_inode(file);
1009         mbox = (uintptr_t)ino->i_private & 7;
1010         adap = ino->i_private - mbox;
1011         addr = adap->regs + PF_REG(mbox, CIM_PF_MAILBOX_DATA_A);
1012         ctrl = addr + MBOX_LEN;
1013
1014         if (MBOWNER_G(readl(ctrl)) != X_MBOWNER_PL)
1015                 return -EBUSY;
1016
1017         for (i = 0; i < 8; i++)
1018                 writeq(data[i], addr + 8 * i);
1019
1020         writel(MBMSGVALID_F | MBOWNER_V(X_MBOWNER_FW), ctrl);
1021         return count;
1022 }
1023
1024 static const struct file_operations mbox_debugfs_fops = {
1025         .owner   = THIS_MODULE,
1026         .open    = mbox_open,
1027         .read    = seq_read,
1028         .llseek  = seq_lseek,
1029         .release = single_release,
1030         .write   = mbox_write
1031 };
1032
1033 static ssize_t flash_read(struct file *file, char __user *buf, size_t count,
1034                           loff_t *ppos)
1035 {
1036         loff_t pos = *ppos;
1037         loff_t avail = file_inode(file)->i_size;
1038         struct adapter *adap = file->private_data;
1039
1040         if (pos < 0)
1041                 return -EINVAL;
1042         if (pos >= avail)
1043                 return 0;
1044         if (count > avail - pos)
1045                 count = avail - pos;
1046
1047         while (count) {
1048                 size_t len;
1049                 int ret, ofst;
1050                 u8 data[256];
1051
1052                 ofst = pos & 3;
1053                 len = min(count + ofst, sizeof(data));
1054                 ret = t4_read_flash(adap, pos - ofst, (len + 3) / 4,
1055                                     (u32 *)data, 1);
1056                 if (ret)
1057                         return ret;
1058
1059                 len -= ofst;
1060                 if (copy_to_user(buf, data + ofst, len))
1061                         return -EFAULT;
1062
1063                 buf += len;
1064                 pos += len;
1065                 count -= len;
1066         }
1067         count = pos - *ppos;
1068         *ppos = pos;
1069         return count;
1070 }
1071
1072 static const struct file_operations flash_debugfs_fops = {
1073         .owner   = THIS_MODULE,
1074         .open    = mem_open,
1075         .read    = flash_read,
1076 };
1077
1078 static inline void tcamxy2valmask(u64 x, u64 y, u8 *addr, u64 *mask)
1079 {
1080         *mask = x | y;
1081         y = (__force u64)cpu_to_be64(y);
1082         memcpy(addr, (char *)&y + 2, ETH_ALEN);
1083 }
1084
1085 static int mps_tcam_show(struct seq_file *seq, void *v)
1086 {
1087         if (v == SEQ_START_TOKEN)
1088                 seq_puts(seq, "Idx  Ethernet address     Mask     Vld Ports PF"
1089                          "  VF              Replication             "
1090                          "P0 P1 P2 P3  ML\n");
1091         else {
1092                 u64 mask;
1093                 u8 addr[ETH_ALEN];
1094                 struct adapter *adap = seq->private;
1095                 unsigned int idx = (uintptr_t)v - 2;
1096                 u64 tcamy = t4_read_reg64(adap, MPS_CLS_TCAM_Y_L(idx));
1097                 u64 tcamx = t4_read_reg64(adap, MPS_CLS_TCAM_X_L(idx));
1098                 u32 cls_lo = t4_read_reg(adap, MPS_CLS_SRAM_L(idx));
1099                 u32 cls_hi = t4_read_reg(adap, MPS_CLS_SRAM_H(idx));
1100                 u32 rplc[4] = {0, 0, 0, 0};
1101
1102                 if (tcamx & tcamy) {
1103                         seq_printf(seq, "%3u         -\n", idx);
1104                         goto out;
1105                 }
1106
1107                 if (cls_lo & REPLICATE_F) {
1108                         struct fw_ldst_cmd ldst_cmd;
1109                         int ret;
1110
1111                         memset(&ldst_cmd, 0, sizeof(ldst_cmd));
1112                         ldst_cmd.op_to_addrspace =
1113                                 htonl(FW_CMD_OP_V(FW_LDST_CMD) |
1114                                       FW_CMD_REQUEST_F |
1115                                       FW_CMD_READ_F |
1116                                       FW_LDST_CMD_ADDRSPACE_V(
1117                                               FW_LDST_ADDRSPC_MPS));
1118                         ldst_cmd.cycles_to_len16 = htonl(FW_LEN16(ldst_cmd));
1119                         ldst_cmd.u.mps.fid_ctl =
1120                                 htons(FW_LDST_CMD_FID_V(FW_LDST_MPS_RPLC) |
1121                                       FW_LDST_CMD_CTL_V(idx));
1122                         ret = t4_wr_mbox(adap, adap->mbox, &ldst_cmd,
1123                                          sizeof(ldst_cmd), &ldst_cmd);
1124                         if (ret)
1125                                 dev_warn(adap->pdev_dev, "Can't read MPS "
1126                                          "replication map for idx %d: %d\n",
1127                                          idx, -ret);
1128                         else {
1129                                 rplc[0] = ntohl(ldst_cmd.u.mps.rplc31_0);
1130                                 rplc[1] = ntohl(ldst_cmd.u.mps.rplc63_32);
1131                                 rplc[2] = ntohl(ldst_cmd.u.mps.rplc95_64);
1132                                 rplc[3] = ntohl(ldst_cmd.u.mps.rplc127_96);
1133                         }
1134                 }
1135
1136                 tcamxy2valmask(tcamx, tcamy, addr, &mask);
1137                 seq_printf(seq, "%3u %02x:%02x:%02x:%02x:%02x:%02x %012llx"
1138                            "%3c   %#x%4u%4d",
1139                            idx, addr[0], addr[1], addr[2], addr[3], addr[4],
1140                            addr[5], (unsigned long long)mask,
1141                            (cls_lo & SRAM_VLD_F) ? 'Y' : 'N', PORTMAP_G(cls_hi),
1142                            PF_G(cls_lo),
1143                            (cls_lo & VF_VALID_F) ? VF_G(cls_lo) : -1);
1144                 if (cls_lo & REPLICATE_F)
1145                         seq_printf(seq, " %08x %08x %08x %08x",
1146                                    rplc[3], rplc[2], rplc[1], rplc[0]);
1147                 else
1148                         seq_printf(seq, "%36c", ' ');
1149                 seq_printf(seq, "%4u%3u%3u%3u %#x\n",
1150                            SRAM_PRIO0_G(cls_lo), SRAM_PRIO1_G(cls_lo),
1151                            SRAM_PRIO2_G(cls_lo), SRAM_PRIO3_G(cls_lo),
1152                            (cls_lo >> MULTILISTEN0_S) & 0xf);
1153         }
1154 out:    return 0;
1155 }
1156
1157 static inline void *mps_tcam_get_idx(struct seq_file *seq, loff_t pos)
1158 {
1159         struct adapter *adap = seq->private;
1160         int max_mac_addr = is_t4(adap->params.chip) ?
1161                                 NUM_MPS_CLS_SRAM_L_INSTANCES :
1162                                 NUM_MPS_T5_CLS_SRAM_L_INSTANCES;
1163         return ((pos <= max_mac_addr) ? (void *)(uintptr_t)(pos + 1) : NULL);
1164 }
1165
1166 static void *mps_tcam_start(struct seq_file *seq, loff_t *pos)
1167 {
1168         return *pos ? mps_tcam_get_idx(seq, *pos) : SEQ_START_TOKEN;
1169 }
1170
1171 static void *mps_tcam_next(struct seq_file *seq, void *v, loff_t *pos)
1172 {
1173         ++*pos;
1174         return mps_tcam_get_idx(seq, *pos);
1175 }
1176
1177 static void mps_tcam_stop(struct seq_file *seq, void *v)
1178 {
1179 }
1180
1181 static const struct seq_operations mps_tcam_seq_ops = {
1182         .start = mps_tcam_start,
1183         .next  = mps_tcam_next,
1184         .stop  = mps_tcam_stop,
1185         .show  = mps_tcam_show
1186 };
1187
1188 static int mps_tcam_open(struct inode *inode, struct file *file)
1189 {
1190         int res = seq_open(file, &mps_tcam_seq_ops);
1191
1192         if (!res) {
1193                 struct seq_file *seq = file->private_data;
1194
1195                 seq->private = inode->i_private;
1196         }
1197         return res;
1198 }
1199
1200 static const struct file_operations mps_tcam_debugfs_fops = {
1201         .owner   = THIS_MODULE,
1202         .open    = mps_tcam_open,
1203         .read    = seq_read,
1204         .llseek  = seq_lseek,
1205         .release = seq_release,
1206 };
1207
1208 /* Display various sensor information.
1209  */
1210 static int sensors_show(struct seq_file *seq, void *v)
1211 {
1212         struct adapter *adap = seq->private;
1213         u32 param[7], val[7];
1214         int ret;
1215
1216         /* Note that if the sensors haven't been initialized and turned on
1217          * we'll get values of 0, so treat those as "<unknown>" ...
1218          */
1219         param[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
1220                     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
1221                     FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_TMP));
1222         param[1] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
1223                     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DIAG) |
1224                     FW_PARAMS_PARAM_Y_V(FW_PARAM_DEV_DIAG_VDD));
1225         ret = t4_query_params(adap, adap->mbox, adap->fn, 0, 2,
1226                               param, val);
1227
1228         if (ret < 0 || val[0] == 0)
1229                 seq_puts(seq, "Temperature: <unknown>\n");
1230         else
1231                 seq_printf(seq, "Temperature: %dC\n", val[0]);
1232
1233         if (ret < 0 || val[1] == 0)
1234                 seq_puts(seq, "Core VDD:    <unknown>\n");
1235         else
1236                 seq_printf(seq, "Core VDD:    %dmV\n", val[1]);
1237
1238         return 0;
1239 }
1240
1241 DEFINE_SIMPLE_DEBUGFS_FILE(sensors);
1242
1243 #if IS_ENABLED(CONFIG_IPV6)
1244 static int clip_tbl_open(struct inode *inode, struct file *file)
1245 {
1246         return single_open(file, clip_tbl_show, inode->i_private);
1247 }
1248
1249 static const struct file_operations clip_tbl_debugfs_fops = {
1250         .owner   = THIS_MODULE,
1251         .open    = clip_tbl_open,
1252         .read    = seq_read,
1253         .llseek  = seq_lseek,
1254         .release = single_release
1255 };
1256 #endif
1257
1258 /*RSS Table.
1259  */
1260
1261 static int rss_show(struct seq_file *seq, void *v, int idx)
1262 {
1263         u16 *entry = v;
1264
1265         seq_printf(seq, "%4d:  %4u  %4u  %4u  %4u  %4u  %4u  %4u  %4u\n",
1266                    idx * 8, entry[0], entry[1], entry[2], entry[3], entry[4],
1267                    entry[5], entry[6], entry[7]);
1268         return 0;
1269 }
1270
1271 static int rss_open(struct inode *inode, struct file *file)
1272 {
1273         int ret;
1274         struct seq_tab *p;
1275         struct adapter *adap = inode->i_private;
1276
1277         p = seq_open_tab(file, RSS_NENTRIES / 8, 8 * sizeof(u16), 0, rss_show);
1278         if (!p)
1279                 return -ENOMEM;
1280
1281         ret = t4_read_rss(adap, (u16 *)p->data);
1282         if (ret)
1283                 seq_release_private(inode, file);
1284
1285         return ret;
1286 }
1287
1288 static const struct file_operations rss_debugfs_fops = {
1289         .owner   = THIS_MODULE,
1290         .open    = rss_open,
1291         .read    = seq_read,
1292         .llseek  = seq_lseek,
1293         .release = seq_release_private
1294 };
1295
1296 /* RSS Configuration.
1297  */
1298
1299 /* Small utility function to return the strings "yes" or "no" if the supplied
1300  * argument is non-zero.
1301  */
1302 static const char *yesno(int x)
1303 {
1304         static const char *yes = "yes";
1305         static const char *no = "no";
1306
1307         return x ? yes : no;
1308 }
1309
1310 static int rss_config_show(struct seq_file *seq, void *v)
1311 {
1312         struct adapter *adapter = seq->private;
1313         static const char * const keymode[] = {
1314                 "global",
1315                 "global and per-VF scramble",
1316                 "per-PF and per-VF scramble",
1317                 "per-VF and per-VF scramble",
1318         };
1319         u32 rssconf;
1320
1321         rssconf = t4_read_reg(adapter, TP_RSS_CONFIG_A);
1322         seq_printf(seq, "TP_RSS_CONFIG: %#x\n", rssconf);
1323         seq_printf(seq, "  Tnl4TupEnIpv6: %3s\n", yesno(rssconf &
1324                                                         TNL4TUPENIPV6_F));
1325         seq_printf(seq, "  Tnl2TupEnIpv6: %3s\n", yesno(rssconf &
1326                                                         TNL2TUPENIPV6_F));
1327         seq_printf(seq, "  Tnl4TupEnIpv4: %3s\n", yesno(rssconf &
1328                                                         TNL4TUPENIPV4_F));
1329         seq_printf(seq, "  Tnl2TupEnIpv4: %3s\n", yesno(rssconf &
1330                                                         TNL2TUPENIPV4_F));
1331         seq_printf(seq, "  TnlTcpSel:     %3s\n", yesno(rssconf & TNLTCPSEL_F));
1332         seq_printf(seq, "  TnlIp6Sel:     %3s\n", yesno(rssconf & TNLIP6SEL_F));
1333         seq_printf(seq, "  TnlVrtSel:     %3s\n", yesno(rssconf & TNLVRTSEL_F));
1334         seq_printf(seq, "  TnlMapEn:      %3s\n", yesno(rssconf & TNLMAPEN_F));
1335         seq_printf(seq, "  OfdHashSave:   %3s\n", yesno(rssconf &
1336                                                         OFDHASHSAVE_F));
1337         seq_printf(seq, "  OfdVrtSel:     %3s\n", yesno(rssconf & OFDVRTSEL_F));
1338         seq_printf(seq, "  OfdMapEn:      %3s\n", yesno(rssconf & OFDMAPEN_F));
1339         seq_printf(seq, "  OfdLkpEn:      %3s\n", yesno(rssconf & OFDLKPEN_F));
1340         seq_printf(seq, "  Syn4TupEnIpv6: %3s\n", yesno(rssconf &
1341                                                         SYN4TUPENIPV6_F));
1342         seq_printf(seq, "  Syn2TupEnIpv6: %3s\n", yesno(rssconf &
1343                                                         SYN2TUPENIPV6_F));
1344         seq_printf(seq, "  Syn4TupEnIpv4: %3s\n", yesno(rssconf &
1345                                                         SYN4TUPENIPV4_F));
1346         seq_printf(seq, "  Syn2TupEnIpv4: %3s\n", yesno(rssconf &
1347                                                         SYN2TUPENIPV4_F));
1348         seq_printf(seq, "  Syn4TupEnIpv6: %3s\n", yesno(rssconf &
1349                                                         SYN4TUPENIPV6_F));
1350         seq_printf(seq, "  SynIp6Sel:     %3s\n", yesno(rssconf & SYNIP6SEL_F));
1351         seq_printf(seq, "  SynVrt6Sel:    %3s\n", yesno(rssconf & SYNVRTSEL_F));
1352         seq_printf(seq, "  SynMapEn:      %3s\n", yesno(rssconf & SYNMAPEN_F));
1353         seq_printf(seq, "  SynLkpEn:      %3s\n", yesno(rssconf & SYNLKPEN_F));
1354         seq_printf(seq, "  ChnEn:         %3s\n", yesno(rssconf &
1355                                                         CHANNELENABLE_F));
1356         seq_printf(seq, "  PrtEn:         %3s\n", yesno(rssconf &
1357                                                         PORTENABLE_F));
1358         seq_printf(seq, "  TnlAllLkp:     %3s\n", yesno(rssconf &
1359                                                         TNLALLLOOKUP_F));
1360         seq_printf(seq, "  VrtEn:         %3s\n", yesno(rssconf &
1361                                                         VIRTENABLE_F));
1362         seq_printf(seq, "  CngEn:         %3s\n", yesno(rssconf &
1363                                                         CONGESTIONENABLE_F));
1364         seq_printf(seq, "  HashToeplitz:  %3s\n", yesno(rssconf &
1365                                                         HASHTOEPLITZ_F));
1366         seq_printf(seq, "  Udp4En:        %3s\n", yesno(rssconf & UDPENABLE_F));
1367         seq_printf(seq, "  Disable:       %3s\n", yesno(rssconf & DISABLE_F));
1368
1369         seq_puts(seq, "\n");
1370
1371         rssconf = t4_read_reg(adapter, TP_RSS_CONFIG_TNL_A);
1372         seq_printf(seq, "TP_RSS_CONFIG_TNL: %#x\n", rssconf);
1373         seq_printf(seq, "  MaskSize:      %3d\n", MASKSIZE_G(rssconf));
1374         seq_printf(seq, "  MaskFilter:    %3d\n", MASKFILTER_G(rssconf));
1375         if (CHELSIO_CHIP_VERSION(adapter->params.chip) > CHELSIO_T5) {
1376                 seq_printf(seq, "  HashAll:     %3s\n",
1377                            yesno(rssconf & HASHALL_F));
1378                 seq_printf(seq, "  HashEth:     %3s\n",
1379                            yesno(rssconf & HASHETH_F));
1380         }
1381         seq_printf(seq, "  UseWireCh:     %3s\n", yesno(rssconf & USEWIRECH_F));
1382
1383         seq_puts(seq, "\n");
1384
1385         rssconf = t4_read_reg(adapter, TP_RSS_CONFIG_OFD_A);
1386         seq_printf(seq, "TP_RSS_CONFIG_OFD: %#x\n", rssconf);
1387         seq_printf(seq, "  MaskSize:      %3d\n", MASKSIZE_G(rssconf));
1388         seq_printf(seq, "  RRCplMapEn:    %3s\n", yesno(rssconf &
1389                                                         RRCPLMAPEN_F));
1390         seq_printf(seq, "  RRCplQueWidth: %3d\n", RRCPLQUEWIDTH_G(rssconf));
1391
1392         seq_puts(seq, "\n");
1393
1394         rssconf = t4_read_reg(adapter, TP_RSS_CONFIG_SYN_A);
1395         seq_printf(seq, "TP_RSS_CONFIG_SYN: %#x\n", rssconf);
1396         seq_printf(seq, "  MaskSize:      %3d\n", MASKSIZE_G(rssconf));
1397         seq_printf(seq, "  UseWireCh:     %3s\n", yesno(rssconf & USEWIRECH_F));
1398
1399         seq_puts(seq, "\n");
1400
1401         rssconf = t4_read_reg(adapter, TP_RSS_CONFIG_VRT_A);
1402         seq_printf(seq, "TP_RSS_CONFIG_VRT: %#x\n", rssconf);
1403         if (CHELSIO_CHIP_VERSION(adapter->params.chip) > CHELSIO_T5) {
1404                 seq_printf(seq, "  KeyWrAddrX:     %3d\n",
1405                            KEYWRADDRX_G(rssconf));
1406                 seq_printf(seq, "  KeyExtend:      %3s\n",
1407                            yesno(rssconf & KEYEXTEND_F));
1408         }
1409         seq_printf(seq, "  VfRdRg:        %3s\n", yesno(rssconf & VFRDRG_F));
1410         seq_printf(seq, "  VfRdEn:        %3s\n", yesno(rssconf & VFRDEN_F));
1411         seq_printf(seq, "  VfPerrEn:      %3s\n", yesno(rssconf & VFPERREN_F));
1412         seq_printf(seq, "  KeyPerrEn:     %3s\n", yesno(rssconf & KEYPERREN_F));
1413         seq_printf(seq, "  DisVfVlan:     %3s\n", yesno(rssconf &
1414                                                         DISABLEVLAN_F));
1415         seq_printf(seq, "  EnUpSwt:       %3s\n", yesno(rssconf & ENABLEUP0_F));
1416         seq_printf(seq, "  HashDelay:     %3d\n", HASHDELAY_G(rssconf));
1417         if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5)
1418                 seq_printf(seq, "  VfWrAddr:      %3d\n", VFWRADDR_G(rssconf));
1419         seq_printf(seq, "  KeyMode:       %s\n", keymode[KEYMODE_G(rssconf)]);
1420         seq_printf(seq, "  VfWrEn:        %3s\n", yesno(rssconf & VFWREN_F));
1421         seq_printf(seq, "  KeyWrEn:       %3s\n", yesno(rssconf & KEYWREN_F));
1422         seq_printf(seq, "  KeyWrAddr:     %3d\n", KEYWRADDR_G(rssconf));
1423
1424         seq_puts(seq, "\n");
1425
1426         rssconf = t4_read_reg(adapter, TP_RSS_CONFIG_CNG_A);
1427         seq_printf(seq, "TP_RSS_CONFIG_CNG: %#x\n", rssconf);
1428         seq_printf(seq, "  ChnCount3:     %3s\n", yesno(rssconf & CHNCOUNT3_F));
1429         seq_printf(seq, "  ChnCount2:     %3s\n", yesno(rssconf & CHNCOUNT2_F));
1430         seq_printf(seq, "  ChnCount1:     %3s\n", yesno(rssconf & CHNCOUNT1_F));
1431         seq_printf(seq, "  ChnCount0:     %3s\n", yesno(rssconf & CHNCOUNT0_F));
1432         seq_printf(seq, "  ChnUndFlow3:   %3s\n", yesno(rssconf &
1433                                                         CHNUNDFLOW3_F));
1434         seq_printf(seq, "  ChnUndFlow2:   %3s\n", yesno(rssconf &
1435                                                         CHNUNDFLOW2_F));
1436         seq_printf(seq, "  ChnUndFlow1:   %3s\n", yesno(rssconf &
1437                                                         CHNUNDFLOW1_F));
1438         seq_printf(seq, "  ChnUndFlow0:   %3s\n", yesno(rssconf &
1439                                                         CHNUNDFLOW0_F));
1440         seq_printf(seq, "  RstChn3:       %3s\n", yesno(rssconf & RSTCHN3_F));
1441         seq_printf(seq, "  RstChn2:       %3s\n", yesno(rssconf & RSTCHN2_F));
1442         seq_printf(seq, "  RstChn1:       %3s\n", yesno(rssconf & RSTCHN1_F));
1443         seq_printf(seq, "  RstChn0:       %3s\n", yesno(rssconf & RSTCHN0_F));
1444         seq_printf(seq, "  UpdVld:        %3s\n", yesno(rssconf & UPDVLD_F));
1445         seq_printf(seq, "  Xoff:          %3s\n", yesno(rssconf & XOFF_F));
1446         seq_printf(seq, "  UpdChn3:       %3s\n", yesno(rssconf & UPDCHN3_F));
1447         seq_printf(seq, "  UpdChn2:       %3s\n", yesno(rssconf & UPDCHN2_F));
1448         seq_printf(seq, "  UpdChn1:       %3s\n", yesno(rssconf & UPDCHN1_F));
1449         seq_printf(seq, "  UpdChn0:       %3s\n", yesno(rssconf & UPDCHN0_F));
1450         seq_printf(seq, "  Queue:         %3d\n", QUEUE_G(rssconf));
1451
1452         return 0;
1453 }
1454
1455 DEFINE_SIMPLE_DEBUGFS_FILE(rss_config);
1456
1457 /* RSS Secret Key.
1458  */
1459
1460 static int rss_key_show(struct seq_file *seq, void *v)
1461 {
1462         u32 key[10];
1463
1464         t4_read_rss_key(seq->private, key);
1465         seq_printf(seq, "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1466                    key[9], key[8], key[7], key[6], key[5], key[4], key[3],
1467                    key[2], key[1], key[0]);
1468         return 0;
1469 }
1470
1471 static int rss_key_open(struct inode *inode, struct file *file)
1472 {
1473         return single_open(file, rss_key_show, inode->i_private);
1474 }
1475
1476 static ssize_t rss_key_write(struct file *file, const char __user *buf,
1477                              size_t count, loff_t *pos)
1478 {
1479         int i, j;
1480         u32 key[10];
1481         char s[100], *p;
1482         struct adapter *adap = file_inode(file)->i_private;
1483
1484         if (count > sizeof(s) - 1)
1485                 return -EINVAL;
1486         if (copy_from_user(s, buf, count))
1487                 return -EFAULT;
1488         for (i = count; i > 0 && isspace(s[i - 1]); i--)
1489                 ;
1490         s[i] = '\0';
1491
1492         for (p = s, i = 9; i >= 0; i--) {
1493                 key[i] = 0;
1494                 for (j = 0; j < 8; j++, p++) {
1495                         if (!isxdigit(*p))
1496                                 return -EINVAL;
1497                         key[i] = (key[i] << 4) | hex2val(*p);
1498                 }
1499         }
1500
1501         t4_write_rss_key(adap, key, -1);
1502         return count;
1503 }
1504
1505 static const struct file_operations rss_key_debugfs_fops = {
1506         .owner   = THIS_MODULE,
1507         .open    = rss_key_open,
1508         .read    = seq_read,
1509         .llseek  = seq_lseek,
1510         .release = single_release,
1511         .write   = rss_key_write
1512 };
1513
1514 /* PF RSS Configuration.
1515  */
1516
1517 struct rss_pf_conf {
1518         u32 rss_pf_map;
1519         u32 rss_pf_mask;
1520         u32 rss_pf_config;
1521 };
1522
1523 static int rss_pf_config_show(struct seq_file *seq, void *v, int idx)
1524 {
1525         struct rss_pf_conf *pfconf;
1526
1527         if (v == SEQ_START_TOKEN) {
1528                 /* use the 0th entry to dump the PF Map Index Size */
1529                 pfconf = seq->private + offsetof(struct seq_tab, data);
1530                 seq_printf(seq, "PF Map Index Size = %d\n\n",
1531                            LKPIDXSIZE_G(pfconf->rss_pf_map));
1532
1533                 seq_puts(seq, "     RSS              PF   VF    Hash Tuple Enable         Default\n");
1534                 seq_puts(seq, "     Enable       IPF Mask Mask  IPv6      IPv4      UDP   Queue\n");
1535                 seq_puts(seq, " PF  Map Chn Prt  Map Size Size  Four Two  Four Two  Four  Ch1  Ch0\n");
1536         } else {
1537                 #define G_PFnLKPIDX(map, n) \
1538                         (((map) >> PF1LKPIDX_S*(n)) & PF0LKPIDX_M)
1539                 #define G_PFnMSKSIZE(mask, n) \
1540                         (((mask) >> PF1MSKSIZE_S*(n)) & PF1MSKSIZE_M)
1541
1542                 pfconf = v;
1543                 seq_printf(seq, "%3d  %3s %3s %3s  %3d  %3d  %3d   %3s %3s   %3s %3s   %3s  %3d  %3d\n",
1544                            idx,
1545                            yesno(pfconf->rss_pf_config & MAPENABLE_F),
1546                            yesno(pfconf->rss_pf_config & CHNENABLE_F),
1547                            yesno(pfconf->rss_pf_config & PRTENABLE_F),
1548                            G_PFnLKPIDX(pfconf->rss_pf_map, idx),
1549                            G_PFnMSKSIZE(pfconf->rss_pf_mask, idx),
1550                            IVFWIDTH_G(pfconf->rss_pf_config),
1551                            yesno(pfconf->rss_pf_config & IP6FOURTUPEN_F),
1552                            yesno(pfconf->rss_pf_config & IP6TWOTUPEN_F),
1553                            yesno(pfconf->rss_pf_config & IP4FOURTUPEN_F),
1554                            yesno(pfconf->rss_pf_config & IP4TWOTUPEN_F),
1555                            yesno(pfconf->rss_pf_config & UDPFOURTUPEN_F),
1556                            CH1DEFAULTQUEUE_G(pfconf->rss_pf_config),
1557                            CH0DEFAULTQUEUE_G(pfconf->rss_pf_config));
1558
1559                 #undef G_PFnLKPIDX
1560                 #undef G_PFnMSKSIZE
1561         }
1562         return 0;
1563 }
1564
1565 static int rss_pf_config_open(struct inode *inode, struct file *file)
1566 {
1567         struct adapter *adapter = inode->i_private;
1568         struct seq_tab *p;
1569         u32 rss_pf_map, rss_pf_mask;
1570         struct rss_pf_conf *pfconf;
1571         int pf;
1572
1573         p = seq_open_tab(file, 8, sizeof(*pfconf), 1, rss_pf_config_show);
1574         if (!p)
1575                 return -ENOMEM;
1576
1577         pfconf = (struct rss_pf_conf *)p->data;
1578         rss_pf_map = t4_read_rss_pf_map(adapter);
1579         rss_pf_mask = t4_read_rss_pf_mask(adapter);
1580         for (pf = 0; pf < 8; pf++) {
1581                 pfconf[pf].rss_pf_map = rss_pf_map;
1582                 pfconf[pf].rss_pf_mask = rss_pf_mask;
1583                 t4_read_rss_pf_config(adapter, pf, &pfconf[pf].rss_pf_config);
1584         }
1585         return 0;
1586 }
1587
1588 static const struct file_operations rss_pf_config_debugfs_fops = {
1589         .owner   = THIS_MODULE,
1590         .open    = rss_pf_config_open,
1591         .read    = seq_read,
1592         .llseek  = seq_lseek,
1593         .release = seq_release_private
1594 };
1595
1596 /* VF RSS Configuration.
1597  */
1598
1599 struct rss_vf_conf {
1600         u32 rss_vf_vfl;
1601         u32 rss_vf_vfh;
1602 };
1603
1604 static int rss_vf_config_show(struct seq_file *seq, void *v, int idx)
1605 {
1606         if (v == SEQ_START_TOKEN) {
1607                 seq_puts(seq, "     RSS                     Hash Tuple Enable\n");
1608                 seq_puts(seq, "     Enable   IVF  Dis  Enb  IPv6      IPv4      UDP    Def  Secret Key\n");
1609                 seq_puts(seq, " VF  Chn Prt  Map  VLAN  uP  Four Two  Four Two  Four   Que  Idx       Hash\n");
1610         } else {
1611                 struct rss_vf_conf *vfconf = v;
1612
1613                 seq_printf(seq, "%3d  %3s %3s  %3d   %3s %3s   %3s %3s   %3s  %3s   %3s  %4d  %3d %#10x\n",
1614                            idx,
1615                            yesno(vfconf->rss_vf_vfh & VFCHNEN_F),
1616                            yesno(vfconf->rss_vf_vfh & VFPRTEN_F),
1617                            VFLKPIDX_G(vfconf->rss_vf_vfh),
1618                            yesno(vfconf->rss_vf_vfh & VFVLNEX_F),
1619                            yesno(vfconf->rss_vf_vfh & VFUPEN_F),
1620                            yesno(vfconf->rss_vf_vfh & VFIP4FOURTUPEN_F),
1621                            yesno(vfconf->rss_vf_vfh & VFIP6TWOTUPEN_F),
1622                            yesno(vfconf->rss_vf_vfh & VFIP4FOURTUPEN_F),
1623                            yesno(vfconf->rss_vf_vfh & VFIP4TWOTUPEN_F),
1624                            yesno(vfconf->rss_vf_vfh & ENABLEUDPHASH_F),
1625                            DEFAULTQUEUE_G(vfconf->rss_vf_vfh),
1626                            KEYINDEX_G(vfconf->rss_vf_vfh),
1627                            vfconf->rss_vf_vfl);
1628         }
1629         return 0;
1630 }
1631
1632 static int rss_vf_config_open(struct inode *inode, struct file *file)
1633 {
1634         struct adapter *adapter = inode->i_private;
1635         struct seq_tab *p;
1636         struct rss_vf_conf *vfconf;
1637         int vf;
1638
1639         p = seq_open_tab(file, 128, sizeof(*vfconf), 1, rss_vf_config_show);
1640         if (!p)
1641                 return -ENOMEM;
1642
1643         vfconf = (struct rss_vf_conf *)p->data;
1644         for (vf = 0; vf < 128; vf++) {
1645                 t4_read_rss_vf_config(adapter, vf, &vfconf[vf].rss_vf_vfl,
1646                                       &vfconf[vf].rss_vf_vfh);
1647         }
1648         return 0;
1649 }
1650
1651 static const struct file_operations rss_vf_config_debugfs_fops = {
1652         .owner   = THIS_MODULE,
1653         .open    = rss_vf_config_open,
1654         .read    = seq_read,
1655         .llseek  = seq_lseek,
1656         .release = seq_release_private
1657 };
1658
1659 /**
1660  * ethqset2pinfo - return port_info of an Ethernet Queue Set
1661  * @adap: the adapter
1662  * @qset: Ethernet Queue Set
1663  */
1664 static inline struct port_info *ethqset2pinfo(struct adapter *adap, int qset)
1665 {
1666         int pidx;
1667
1668         for_each_port(adap, pidx) {
1669                 struct port_info *pi = adap2pinfo(adap, pidx);
1670
1671                 if (qset >= pi->first_qset &&
1672                     qset < pi->first_qset + pi->nqsets)
1673                         return pi;
1674         }
1675
1676         /* should never happen! */
1677         BUG_ON(1);
1678         return NULL;
1679 }
1680
1681 static int sge_qinfo_show(struct seq_file *seq, void *v)
1682 {
1683         struct adapter *adap = seq->private;
1684         int eth_entries = DIV_ROUND_UP(adap->sge.ethqsets, 4);
1685         int toe_entries = DIV_ROUND_UP(adap->sge.ofldqsets, 4);
1686         int rdma_entries = DIV_ROUND_UP(adap->sge.rdmaqs, 4);
1687         int ciq_entries = DIV_ROUND_UP(adap->sge.rdmaciqs, 4);
1688         int ctrl_entries = DIV_ROUND_UP(MAX_CTRL_QUEUES, 4);
1689         int i, r = (uintptr_t)v - 1;
1690         int toe_idx = r - eth_entries;
1691         int rdma_idx = toe_idx - toe_entries;
1692         int ciq_idx = rdma_idx - rdma_entries;
1693         int ctrl_idx =  ciq_idx - ciq_entries;
1694         int fq_idx =  ctrl_idx - ctrl_entries;
1695
1696         if (r)
1697                 seq_putc(seq, '\n');
1698
1699 #define S3(fmt_spec, s, v) \
1700 do { \
1701         seq_printf(seq, "%-12s", s); \
1702         for (i = 0; i < n; ++i) \
1703                 seq_printf(seq, " %16" fmt_spec, v); \
1704                 seq_putc(seq, '\n'); \
1705 } while (0)
1706 #define S(s, v) S3("s", s, v)
1707 #define T(s, v) S3("u", s, tx[i].v)
1708 #define R(s, v) S3("u", s, rx[i].v)
1709
1710         if (r < eth_entries) {
1711                 int base_qset = r * 4;
1712                 const struct sge_eth_rxq *rx = &adap->sge.ethrxq[base_qset];
1713                 const struct sge_eth_txq *tx = &adap->sge.ethtxq[base_qset];
1714                 int n = min(4, adap->sge.ethqsets - 4 * r);
1715
1716                 S("QType:", "Ethernet");
1717                 S("Interface:",
1718                   rx[i].rspq.netdev ? rx[i].rspq.netdev->name : "N/A");
1719                 T("TxQ ID:", q.cntxt_id);
1720                 T("TxQ size:", q.size);
1721                 T("TxQ inuse:", q.in_use);
1722                 T("TxQ CIDX:", q.cidx);
1723                 T("TxQ PIDX:", q.pidx);
1724 #ifdef CONFIG_CHELSIO_T4_DCB
1725                 T("DCB Prio:", dcb_prio);
1726                 S3("u", "DCB PGID:",
1727                    (ethqset2pinfo(adap, base_qset + i)->dcb.pgid >>
1728                     4*(7-tx[i].dcb_prio)) & 0xf);
1729                 S3("u", "DCB PFC:",
1730                    (ethqset2pinfo(adap, base_qset + i)->dcb.pfcen >>
1731                     1*(7-tx[i].dcb_prio)) & 0x1);
1732 #endif
1733                 R("RspQ ID:", rspq.abs_id);
1734                 R("RspQ size:", rspq.size);
1735                 R("RspQE size:", rspq.iqe_len);
1736                 R("RspQ CIDX:", rspq.cidx);
1737                 R("RspQ Gen:", rspq.gen);
1738                 S3("u", "Intr delay:", qtimer_val(adap, &rx[i].rspq));
1739                 S3("u", "Intr pktcnt:",
1740                    adap->sge.counter_val[rx[i].rspq.pktcnt_idx]);
1741                 R("FL ID:", fl.cntxt_id);
1742                 R("FL size:", fl.size - 8);
1743                 R("FL pend:", fl.pend_cred);
1744                 R("FL avail:", fl.avail);
1745                 R("FL PIDX:", fl.pidx);
1746                 R("FL CIDX:", fl.cidx);
1747         } else if (toe_idx < toe_entries) {
1748                 const struct sge_ofld_rxq *rx = &adap->sge.ofldrxq[toe_idx * 4];
1749                 const struct sge_ofld_txq *tx = &adap->sge.ofldtxq[toe_idx * 4];
1750                 int n = min(4, adap->sge.ofldqsets - 4 * toe_idx);
1751
1752                 S("QType:", "TOE");
1753                 T("TxQ ID:", q.cntxt_id);
1754                 T("TxQ size:", q.size);
1755                 T("TxQ inuse:", q.in_use);
1756                 T("TxQ CIDX:", q.cidx);
1757                 T("TxQ PIDX:", q.pidx);
1758                 R("RspQ ID:", rspq.abs_id);
1759                 R("RspQ size:", rspq.size);
1760                 R("RspQE size:", rspq.iqe_len);
1761                 R("RspQ CIDX:", rspq.cidx);
1762                 R("RspQ Gen:", rspq.gen);
1763                 S3("u", "Intr delay:", qtimer_val(adap, &rx[i].rspq));
1764                 S3("u", "Intr pktcnt:",
1765                    adap->sge.counter_val[rx[i].rspq.pktcnt_idx]);
1766                 R("FL ID:", fl.cntxt_id);
1767                 R("FL size:", fl.size - 8);
1768                 R("FL pend:", fl.pend_cred);
1769                 R("FL avail:", fl.avail);
1770                 R("FL PIDX:", fl.pidx);
1771                 R("FL CIDX:", fl.cidx);
1772         } else if (rdma_idx < rdma_entries) {
1773                 const struct sge_ofld_rxq *rx =
1774                                 &adap->sge.rdmarxq[rdma_idx * 4];
1775                 int n = min(4, adap->sge.rdmaqs - 4 * rdma_idx);
1776
1777                 S("QType:", "RDMA-CPL");
1778                 S("Interface:",
1779                   rx[i].rspq.netdev ? rx[i].rspq.netdev->name : "N/A");
1780                 R("RspQ ID:", rspq.abs_id);
1781                 R("RspQ size:", rspq.size);
1782                 R("RspQE size:", rspq.iqe_len);
1783                 R("RspQ CIDX:", rspq.cidx);
1784                 R("RspQ Gen:", rspq.gen);
1785                 S3("u", "Intr delay:", qtimer_val(adap, &rx[i].rspq));
1786                 S3("u", "Intr pktcnt:",
1787                    adap->sge.counter_val[rx[i].rspq.pktcnt_idx]);
1788                 R("FL ID:", fl.cntxt_id);
1789                 R("FL size:", fl.size - 8);
1790                 R("FL pend:", fl.pend_cred);
1791                 R("FL avail:", fl.avail);
1792                 R("FL PIDX:", fl.pidx);
1793                 R("FL CIDX:", fl.cidx);
1794         } else if (ciq_idx < ciq_entries) {
1795                 const struct sge_ofld_rxq *rx = &adap->sge.rdmaciq[ciq_idx * 4];
1796                 int n = min(4, adap->sge.rdmaciqs - 4 * ciq_idx);
1797
1798                 S("QType:", "RDMA-CIQ");
1799                 S("Interface:",
1800                   rx[i].rspq.netdev ? rx[i].rspq.netdev->name : "N/A");
1801                 R("RspQ ID:", rspq.abs_id);
1802                 R("RspQ size:", rspq.size);
1803                 R("RspQE size:", rspq.iqe_len);
1804                 R("RspQ CIDX:", rspq.cidx);
1805                 R("RspQ Gen:", rspq.gen);
1806                 S3("u", "Intr delay:", qtimer_val(adap, &rx[i].rspq));
1807                 S3("u", "Intr pktcnt:",
1808                    adap->sge.counter_val[rx[i].rspq.pktcnt_idx]);
1809         } else if (ctrl_idx < ctrl_entries) {
1810                 const struct sge_ctrl_txq *tx = &adap->sge.ctrlq[ctrl_idx * 4];
1811                 int n = min(4, adap->params.nports - 4 * ctrl_idx);
1812
1813                 S("QType:", "Control");
1814                 T("TxQ ID:", q.cntxt_id);
1815                 T("TxQ size:", q.size);
1816                 T("TxQ inuse:", q.in_use);
1817                 T("TxQ CIDX:", q.cidx);
1818                 T("TxQ PIDX:", q.pidx);
1819         } else if (fq_idx == 0) {
1820                 const struct sge_rspq *evtq = &adap->sge.fw_evtq;
1821
1822                 seq_printf(seq, "%-12s %16s\n", "QType:", "FW event queue");
1823                 seq_printf(seq, "%-12s %16u\n", "RspQ ID:", evtq->abs_id);
1824                 seq_printf(seq, "%-12s %16u\n", "RspQ size:", evtq->size);
1825                 seq_printf(seq, "%-12s %16u\n", "RspQE size:", evtq->iqe_len);
1826                 seq_printf(seq, "%-12s %16u\n", "RspQ CIDX:", evtq->cidx);
1827                 seq_printf(seq, "%-12s %16u\n", "RspQ Gen:", evtq->gen);
1828                 seq_printf(seq, "%-12s %16u\n", "Intr delay:",
1829                            qtimer_val(adap, evtq));
1830                 seq_printf(seq, "%-12s %16u\n", "Intr pktcnt:",
1831                            adap->sge.counter_val[evtq->pktcnt_idx]);
1832         }
1833 #undef R
1834 #undef T
1835 #undef S
1836 #undef S3
1837 return 0;
1838 }
1839
1840 static int sge_queue_entries(const struct adapter *adap)
1841 {
1842         return DIV_ROUND_UP(adap->sge.ethqsets, 4) +
1843                DIV_ROUND_UP(adap->sge.ofldqsets, 4) +
1844                DIV_ROUND_UP(adap->sge.rdmaqs, 4) +
1845                DIV_ROUND_UP(adap->sge.rdmaciqs, 4) +
1846                DIV_ROUND_UP(MAX_CTRL_QUEUES, 4) + 1;
1847 }
1848
1849 static void *sge_queue_start(struct seq_file *seq, loff_t *pos)
1850 {
1851         int entries = sge_queue_entries(seq->private);
1852
1853         return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
1854 }
1855
1856 static void sge_queue_stop(struct seq_file *seq, void *v)
1857 {
1858 }
1859
1860 static void *sge_queue_next(struct seq_file *seq, void *v, loff_t *pos)
1861 {
1862         int entries = sge_queue_entries(seq->private);
1863
1864         ++*pos;
1865         return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
1866 }
1867
1868 static const struct seq_operations sge_qinfo_seq_ops = {
1869         .start = sge_queue_start,
1870         .next  = sge_queue_next,
1871         .stop  = sge_queue_stop,
1872         .show  = sge_qinfo_show
1873 };
1874
1875 static int sge_qinfo_open(struct inode *inode, struct file *file)
1876 {
1877         int res = seq_open(file, &sge_qinfo_seq_ops);
1878
1879         if (!res) {
1880                 struct seq_file *seq = file->private_data;
1881
1882                 seq->private = inode->i_private;
1883         }
1884         return res;
1885 }
1886
1887 static const struct file_operations sge_qinfo_debugfs_fops = {
1888         .owner   = THIS_MODULE,
1889         .open    = sge_qinfo_open,
1890         .read    = seq_read,
1891         .llseek  = seq_lseek,
1892         .release = seq_release,
1893 };
1894
1895 int mem_open(struct inode *inode, struct file *file)
1896 {
1897         unsigned int mem;
1898         struct adapter *adap;
1899
1900         file->private_data = inode->i_private;
1901
1902         mem = (uintptr_t)file->private_data & 0x3;
1903         adap = file->private_data - mem;
1904
1905         (void)t4_fwcache(adap, FW_PARAM_DEV_FWCACHE_FLUSH);
1906
1907         return 0;
1908 }
1909
1910 static ssize_t mem_read(struct file *file, char __user *buf, size_t count,
1911                         loff_t *ppos)
1912 {
1913         loff_t pos = *ppos;
1914         loff_t avail = file_inode(file)->i_size;
1915         unsigned int mem = (uintptr_t)file->private_data & 3;
1916         struct adapter *adap = file->private_data - mem;
1917         __be32 *data;
1918         int ret;
1919
1920         if (pos < 0)
1921                 return -EINVAL;
1922         if (pos >= avail)
1923                 return 0;
1924         if (count > avail - pos)
1925                 count = avail - pos;
1926
1927         data = t4_alloc_mem(count);
1928         if (!data)
1929                 return -ENOMEM;
1930
1931         spin_lock(&adap->win0_lock);
1932         ret = t4_memory_rw(adap, 0, mem, pos, count, data, T4_MEMORY_READ);
1933         spin_unlock(&adap->win0_lock);
1934         if (ret) {
1935                 t4_free_mem(data);
1936                 return ret;
1937         }
1938         ret = copy_to_user(buf, data, count);
1939
1940         t4_free_mem(data);
1941         if (ret)
1942                 return -EFAULT;
1943
1944         *ppos = pos + count;
1945         return count;
1946 }
1947 static const struct file_operations mem_debugfs_fops = {
1948         .owner   = THIS_MODULE,
1949         .open    = simple_open,
1950         .read    = mem_read,
1951         .llseek  = default_llseek,
1952 };
1953
1954 static void add_debugfs_mem(struct adapter *adap, const char *name,
1955                             unsigned int idx, unsigned int size_mb)
1956 {
1957         debugfs_create_file_size(name, S_IRUSR, adap->debugfs_root,
1958                                  (void *)adap + idx, &mem_debugfs_fops,
1959                                  size_mb << 20);
1960 }
1961
1962 static int blocked_fl_open(struct inode *inode, struct file *file)
1963 {
1964         file->private_data = inode->i_private;
1965         return 0;
1966 }
1967
1968 static ssize_t blocked_fl_read(struct file *filp, char __user *ubuf,
1969                                size_t count, loff_t *ppos)
1970 {
1971         int len;
1972         const struct adapter *adap = filp->private_data;
1973         char *buf;
1974         ssize_t size = (adap->sge.egr_sz + 3) / 4 +
1975                         adap->sge.egr_sz / 32 + 2; /* includes ,/\n/\0 */
1976
1977         buf = kzalloc(size, GFP_KERNEL);
1978         if (!buf)
1979                 return -ENOMEM;
1980
1981         len = snprintf(buf, size - 1, "%*pb\n",
1982                        adap->sge.egr_sz, adap->sge.blocked_fl);
1983         len += sprintf(buf + len, "\n");
1984         size = simple_read_from_buffer(ubuf, count, ppos, buf, len);
1985         t4_free_mem(buf);
1986         return size;
1987 }
1988
1989 static ssize_t blocked_fl_write(struct file *filp, const char __user *ubuf,
1990                                 size_t count, loff_t *ppos)
1991 {
1992         int err;
1993         unsigned long *t;
1994         struct adapter *adap = filp->private_data;
1995
1996         t = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz), sizeof(long), GFP_KERNEL);
1997         if (!t)
1998                 return -ENOMEM;
1999
2000         err = bitmap_parse_user(ubuf, count, t, adap->sge.egr_sz);
2001         if (err)
2002                 return err;
2003
2004         bitmap_copy(adap->sge.blocked_fl, t, adap->sge.egr_sz);
2005         t4_free_mem(t);
2006         return count;
2007 }
2008
2009 static const struct file_operations blocked_fl_fops = {
2010         .owner   = THIS_MODULE,
2011         .open    = blocked_fl_open,
2012         .read    = blocked_fl_read,
2013         .write   = blocked_fl_write,
2014         .llseek  = generic_file_llseek,
2015 };
2016
2017 /* Add an array of Debug FS files.
2018  */
2019 void add_debugfs_files(struct adapter *adap,
2020                        struct t4_debugfs_entry *files,
2021                        unsigned int nfiles)
2022 {
2023         int i;
2024
2025         /* debugfs support is best effort */
2026         for (i = 0; i < nfiles; i++)
2027                 debugfs_create_file(files[i].name, files[i].mode,
2028                                     adap->debugfs_root,
2029                                     (void *)adap + files[i].data,
2030                                     files[i].ops);
2031 }
2032
2033 int t4_setup_debugfs(struct adapter *adap)
2034 {
2035         int i;
2036         u32 size;
2037         struct dentry *de;
2038
2039         static struct t4_debugfs_entry t4_debugfs_files[] = {
2040                 { "cim_la", &cim_la_fops, S_IRUSR, 0 },
2041                 { "cim_qcfg", &cim_qcfg_fops, S_IRUSR, 0 },
2042                 { "clk", &clk_debugfs_fops, S_IRUSR, 0 },
2043                 { "devlog", &devlog_fops, S_IRUSR, 0 },
2044                 { "mbox0", &mbox_debugfs_fops, S_IRUSR | S_IWUSR, 0 },
2045                 { "mbox1", &mbox_debugfs_fops, S_IRUSR | S_IWUSR, 1 },
2046                 { "mbox2", &mbox_debugfs_fops, S_IRUSR | S_IWUSR, 2 },
2047                 { "mbox3", &mbox_debugfs_fops, S_IRUSR | S_IWUSR, 3 },
2048                 { "mbox4", &mbox_debugfs_fops, S_IRUSR | S_IWUSR, 4 },
2049                 { "mbox5", &mbox_debugfs_fops, S_IRUSR | S_IWUSR, 5 },
2050                 { "mbox6", &mbox_debugfs_fops, S_IRUSR | S_IWUSR, 6 },
2051                 { "mbox7", &mbox_debugfs_fops, S_IRUSR | S_IWUSR, 7 },
2052                 { "l2t", &t4_l2t_fops, S_IRUSR, 0},
2053                 { "mps_tcam", &mps_tcam_debugfs_fops, S_IRUSR, 0 },
2054                 { "rss", &rss_debugfs_fops, S_IRUSR, 0 },
2055                 { "rss_config", &rss_config_debugfs_fops, S_IRUSR, 0 },
2056                 { "rss_key", &rss_key_debugfs_fops, S_IRUSR, 0 },
2057                 { "rss_pf_config", &rss_pf_config_debugfs_fops, S_IRUSR, 0 },
2058                 { "rss_vf_config", &rss_vf_config_debugfs_fops, S_IRUSR, 0 },
2059                 { "sge_qinfo", &sge_qinfo_debugfs_fops, S_IRUSR, 0 },
2060                 { "ibq_tp0",  &cim_ibq_fops, S_IRUSR, 0 },
2061                 { "ibq_tp1",  &cim_ibq_fops, S_IRUSR, 1 },
2062                 { "ibq_ulp",  &cim_ibq_fops, S_IRUSR, 2 },
2063                 { "ibq_sge0", &cim_ibq_fops, S_IRUSR, 3 },
2064                 { "ibq_sge1", &cim_ibq_fops, S_IRUSR, 4 },
2065                 { "ibq_ncsi", &cim_ibq_fops, S_IRUSR, 5 },
2066                 { "obq_ulp0", &cim_obq_fops, S_IRUSR, 0 },
2067                 { "obq_ulp1", &cim_obq_fops, S_IRUSR, 1 },
2068                 { "obq_ulp2", &cim_obq_fops, S_IRUSR, 2 },
2069                 { "obq_ulp3", &cim_obq_fops, S_IRUSR, 3 },
2070                 { "obq_sge",  &cim_obq_fops, S_IRUSR, 4 },
2071                 { "obq_ncsi", &cim_obq_fops, S_IRUSR, 5 },
2072                 { "tp_la", &tp_la_fops, S_IRUSR, 0 },
2073                 { "ulprx_la", &ulprx_la_fops, S_IRUSR, 0 },
2074                 { "sensors", &sensors_debugfs_fops, S_IRUSR, 0 },
2075                 { "pm_stats", &pm_stats_debugfs_fops, S_IRUSR, 0 },
2076                 { "cctrl", &cctrl_tbl_debugfs_fops, S_IRUSR, 0 },
2077 #if IS_ENABLED(CONFIG_IPV6)
2078                 { "clip_tbl", &clip_tbl_debugfs_fops, S_IRUSR, 0 },
2079 #endif
2080                 { "blocked_fl", &blocked_fl_fops, S_IRUSR | S_IWUSR, 0 },
2081         };
2082
2083         /* Debug FS nodes common to all T5 and later adapters.
2084          */
2085         static struct t4_debugfs_entry t5_debugfs_files[] = {
2086                 { "obq_sge_rx_q0", &cim_obq_fops, S_IRUSR, 6 },
2087                 { "obq_sge_rx_q1", &cim_obq_fops, S_IRUSR, 7 },
2088         };
2089
2090         add_debugfs_files(adap,
2091                           t4_debugfs_files,
2092                           ARRAY_SIZE(t4_debugfs_files));
2093         if (!is_t4(adap->params.chip))
2094                 add_debugfs_files(adap,
2095                                   t5_debugfs_files,
2096                                   ARRAY_SIZE(t5_debugfs_files));
2097
2098         i = t4_read_reg(adap, MA_TARGET_MEM_ENABLE_A);
2099         if (i & EDRAM0_ENABLE_F) {
2100                 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
2101                 add_debugfs_mem(adap, "edc0", MEM_EDC0, EDRAM0_SIZE_G(size));
2102         }
2103         if (i & EDRAM1_ENABLE_F) {
2104                 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
2105                 add_debugfs_mem(adap, "edc1", MEM_EDC1, EDRAM1_SIZE_G(size));
2106         }
2107         if (is_t4(adap->params.chip)) {
2108                 size = t4_read_reg(adap, MA_EXT_MEMORY_BAR_A);
2109                 if (i & EXT_MEM_ENABLE_F)
2110                         add_debugfs_mem(adap, "mc", MEM_MC,
2111                                         EXT_MEM_SIZE_G(size));
2112         } else {
2113                 if (i & EXT_MEM0_ENABLE_F) {
2114                         size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
2115                         add_debugfs_mem(adap, "mc0", MEM_MC0,
2116                                         EXT_MEM0_SIZE_G(size));
2117                 }
2118                 if (i & EXT_MEM1_ENABLE_F) {
2119                         size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
2120                         add_debugfs_mem(adap, "mc1", MEM_MC1,
2121                                         EXT_MEM1_SIZE_G(size));
2122                 }
2123         }
2124
2125         de = debugfs_create_file_size("flash", S_IRUSR, adap->debugfs_root, adap,
2126                                       &flash_debugfs_fops, adap->params.sf_size);
2127
2128         return 0;
2129 }