]> Git Repo - linux.git/blob - drivers/scsi/lpfc/lpfc_debugfs.c
x86/MCE/AMD: Fix the thresholding machinery initialization order
[linux.git] / drivers / scsi / lpfc / lpfc_debugfs.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
6  * Copyright (C) 2007-2015 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  *                                                                 *
10  * This program is free software; you can redistribute it and/or   *
11  * modify it under the terms of version 2 of the GNU General       *
12  * Public License as published by the Free Software Foundation.    *
13  * This program is distributed in the hope that it will be useful. *
14  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
15  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
16  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
17  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
18  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
19  * more details, a copy of which can be found in the file COPYING  *
20  * included with this package.                                     *
21  *******************************************************************/
22
23 #include <linux/blkdev.h>
24 #include <linux/delay.h>
25 #include <linux/module.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/idr.h>
28 #include <linux/interrupt.h>
29 #include <linux/kthread.h>
30 #include <linux/slab.h>
31 #include <linux/pci.h>
32 #include <linux/spinlock.h>
33 #include <linux/ctype.h>
34
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_device.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_transport_fc.h>
39 #include <scsi/fc/fc_fs.h>
40
41 #include <linux/nvme-fc-driver.h>
42
43 #include "lpfc_hw4.h"
44 #include "lpfc_hw.h"
45 #include "lpfc_sli.h"
46 #include "lpfc_sli4.h"
47 #include "lpfc_nl.h"
48 #include "lpfc_disc.h"
49 #include "lpfc.h"
50 #include "lpfc_scsi.h"
51 #include "lpfc_nvme.h"
52 #include "lpfc_nvmet.h"
53 #include "lpfc_logmsg.h"
54 #include "lpfc_crtn.h"
55 #include "lpfc_vport.h"
56 #include "lpfc_version.h"
57 #include "lpfc_compat.h"
58 #include "lpfc_debugfs.h"
59 #include "lpfc_bsg.h"
60
61 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
62 /*
63  * debugfs interface
64  *
65  * To access this interface the user should:
66  * # mount -t debugfs none /sys/kernel/debug
67  *
68  * The lpfc debugfs directory hierarchy is:
69  * /sys/kernel/debug/lpfc/fnX/vportY
70  * where X is the lpfc hba function unique_id
71  * where Y is the vport VPI on that hba
72  *
73  * Debugging services available per vport:
74  * discovery_trace
75  * This is an ACSII readable file that contains a trace of the last
76  * lpfc_debugfs_max_disc_trc events that happened on a specific vport.
77  * See lpfc_debugfs.h for different categories of  discovery events.
78  * To enable the discovery trace, the following module parameters must be set:
79  * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
80  * lpfc_debugfs_max_disc_trc=X   Where X is the event trace depth for
81  *                               EACH vport. X MUST also be a power of 2.
82  * lpfc_debugfs_mask_disc_trc=Y  Where Y is an event mask as defined in
83  *                               lpfc_debugfs.h .
84  *
85  * slow_ring_trace
86  * This is an ACSII readable file that contains a trace of the last
87  * lpfc_debugfs_max_slow_ring_trc events that happened on a specific HBA.
88  * To enable the slow ring trace, the following module parameters must be set:
89  * lpfc_debugfs_enable=1         Turns on lpfc debugfs filesystem support
90  * lpfc_debugfs_max_slow_ring_trc=X   Where X is the event trace depth for
91  *                               the HBA. X MUST also be a power of 2.
92  */
93 static int lpfc_debugfs_enable = 1;
94 module_param(lpfc_debugfs_enable, int, S_IRUGO);
95 MODULE_PARM_DESC(lpfc_debugfs_enable, "Enable debugfs services");
96
97 /* This MUST be a power of 2 */
98 static int lpfc_debugfs_max_disc_trc;
99 module_param(lpfc_debugfs_max_disc_trc, int, S_IRUGO);
100 MODULE_PARM_DESC(lpfc_debugfs_max_disc_trc,
101         "Set debugfs discovery trace depth");
102
103 /* This MUST be a power of 2 */
104 static int lpfc_debugfs_max_slow_ring_trc;
105 module_param(lpfc_debugfs_max_slow_ring_trc, int, S_IRUGO);
106 MODULE_PARM_DESC(lpfc_debugfs_max_slow_ring_trc,
107         "Set debugfs slow ring trace depth");
108
109 /* This MUST be a power of 2 */
110 static int lpfc_debugfs_max_nvmeio_trc;
111 module_param(lpfc_debugfs_max_nvmeio_trc, int, 0444);
112 MODULE_PARM_DESC(lpfc_debugfs_max_nvmeio_trc,
113                  "Set debugfs NVME IO trace depth");
114
115 static int lpfc_debugfs_mask_disc_trc;
116 module_param(lpfc_debugfs_mask_disc_trc, int, S_IRUGO);
117 MODULE_PARM_DESC(lpfc_debugfs_mask_disc_trc,
118         "Set debugfs discovery trace mask");
119
120 #include <linux/debugfs.h>
121
122 static atomic_t lpfc_debugfs_seq_trc_cnt = ATOMIC_INIT(0);
123 static unsigned long lpfc_debugfs_start_time = 0L;
124
125 /* iDiag */
126 static struct lpfc_idiag idiag;
127
128 /**
129  * lpfc_debugfs_disc_trc_data - Dump discovery logging to a buffer
130  * @vport: The vport to gather the log info from.
131  * @buf: The buffer to dump log into.
132  * @size: The maximum amount of data to process.
133  *
134  * Description:
135  * This routine gathers the lpfc discovery debugfs data from the @vport and
136  * dumps it to @buf up to @size number of bytes. It will start at the next entry
137  * in the log and process the log until the end of the buffer. Then it will
138  * gather from the beginning of the log and process until the current entry.
139  *
140  * Notes:
141  * Discovery logging will be disabled while while this routine dumps the log.
142  *
143  * Return Value:
144  * This routine returns the amount of bytes that were dumped into @buf and will
145  * not exceed @size.
146  **/
147 static int
148 lpfc_debugfs_disc_trc_data(struct lpfc_vport *vport, char *buf, int size)
149 {
150         int i, index, len, enable;
151         uint32_t ms;
152         struct lpfc_debugfs_trc *dtp;
153         char *buffer;
154
155         buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
156         if (!buffer)
157                 return 0;
158
159         enable = lpfc_debugfs_enable;
160         lpfc_debugfs_enable = 0;
161
162         len = 0;
163         index = (atomic_read(&vport->disc_trc_cnt) + 1) &
164                 (lpfc_debugfs_max_disc_trc - 1);
165         for (i = index; i < lpfc_debugfs_max_disc_trc; i++) {
166                 dtp = vport->disc_trc + i;
167                 if (!dtp->fmt)
168                         continue;
169                 ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
170                 snprintf(buffer,
171                         LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
172                         dtp->seq_cnt, ms, dtp->fmt);
173                 len +=  snprintf(buf+len, size-len, buffer,
174                         dtp->data1, dtp->data2, dtp->data3);
175         }
176         for (i = 0; i < index; i++) {
177                 dtp = vport->disc_trc + i;
178                 if (!dtp->fmt)
179                         continue;
180                 ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
181                 snprintf(buffer,
182                         LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
183                         dtp->seq_cnt, ms, dtp->fmt);
184                 len +=  snprintf(buf+len, size-len, buffer,
185                         dtp->data1, dtp->data2, dtp->data3);
186         }
187
188         lpfc_debugfs_enable = enable;
189         kfree(buffer);
190
191         return len;
192 }
193
194 /**
195  * lpfc_debugfs_slow_ring_trc_data - Dump slow ring logging to a buffer
196  * @phba: The HBA to gather the log info from.
197  * @buf: The buffer to dump log into.
198  * @size: The maximum amount of data to process.
199  *
200  * Description:
201  * This routine gathers the lpfc slow ring debugfs data from the @phba and
202  * dumps it to @buf up to @size number of bytes. It will start at the next entry
203  * in the log and process the log until the end of the buffer. Then it will
204  * gather from the beginning of the log and process until the current entry.
205  *
206  * Notes:
207  * Slow ring logging will be disabled while while this routine dumps the log.
208  *
209  * Return Value:
210  * This routine returns the amount of bytes that were dumped into @buf and will
211  * not exceed @size.
212  **/
213 static int
214 lpfc_debugfs_slow_ring_trc_data(struct lpfc_hba *phba, char *buf, int size)
215 {
216         int i, index, len, enable;
217         uint32_t ms;
218         struct lpfc_debugfs_trc *dtp;
219         char *buffer;
220
221         buffer = kmalloc(LPFC_DEBUG_TRC_ENTRY_SIZE, GFP_KERNEL);
222         if (!buffer)
223                 return 0;
224
225         enable = lpfc_debugfs_enable;
226         lpfc_debugfs_enable = 0;
227
228         len = 0;
229         index = (atomic_read(&phba->slow_ring_trc_cnt) + 1) &
230                 (lpfc_debugfs_max_slow_ring_trc - 1);
231         for (i = index; i < lpfc_debugfs_max_slow_ring_trc; i++) {
232                 dtp = phba->slow_ring_trc + i;
233                 if (!dtp->fmt)
234                         continue;
235                 ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
236                 snprintf(buffer,
237                         LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
238                         dtp->seq_cnt, ms, dtp->fmt);
239                 len +=  snprintf(buf+len, size-len, buffer,
240                         dtp->data1, dtp->data2, dtp->data3);
241         }
242         for (i = 0; i < index; i++) {
243                 dtp = phba->slow_ring_trc + i;
244                 if (!dtp->fmt)
245                         continue;
246                 ms = jiffies_to_msecs(dtp->jif - lpfc_debugfs_start_time);
247                 snprintf(buffer,
248                         LPFC_DEBUG_TRC_ENTRY_SIZE, "%010d:%010d ms:%s\n",
249                         dtp->seq_cnt, ms, dtp->fmt);
250                 len +=  snprintf(buf+len, size-len, buffer,
251                         dtp->data1, dtp->data2, dtp->data3);
252         }
253
254         lpfc_debugfs_enable = enable;
255         kfree(buffer);
256
257         return len;
258 }
259
260 static int lpfc_debugfs_last_hbq = -1;
261
262 /**
263  * lpfc_debugfs_hbqinfo_data - Dump host buffer queue info to a buffer
264  * @phba: The HBA to gather host buffer info from.
265  * @buf: The buffer to dump log into.
266  * @size: The maximum amount of data to process.
267  *
268  * Description:
269  * This routine dumps the host buffer queue info from the @phba to @buf up to
270  * @size number of bytes. A header that describes the current hbq state will be
271  * dumped to @buf first and then info on each hbq entry will be dumped to @buf
272  * until @size bytes have been dumped or all the hbq info has been dumped.
273  *
274  * Notes:
275  * This routine will rotate through each configured HBQ each time called.
276  *
277  * Return Value:
278  * This routine returns the amount of bytes that were dumped into @buf and will
279  * not exceed @size.
280  **/
281 static int
282 lpfc_debugfs_hbqinfo_data(struct lpfc_hba *phba, char *buf, int size)
283 {
284         int len = 0;
285         int i, j, found, posted, low;
286         uint32_t phys, raw_index, getidx;
287         struct lpfc_hbq_init *hip;
288         struct hbq_s *hbqs;
289         struct lpfc_hbq_entry *hbqe;
290         struct lpfc_dmabuf *d_buf;
291         struct hbq_dmabuf *hbq_buf;
292
293         if (phba->sli_rev != 3)
294                 return 0;
295
296         spin_lock_irq(&phba->hbalock);
297
298         /* toggle between multiple hbqs, if any */
299         i = lpfc_sli_hbq_count();
300         if (i > 1) {
301                  lpfc_debugfs_last_hbq++;
302                  if (lpfc_debugfs_last_hbq >= i)
303                         lpfc_debugfs_last_hbq = 0;
304         }
305         else
306                 lpfc_debugfs_last_hbq = 0;
307
308         i = lpfc_debugfs_last_hbq;
309
310         len +=  snprintf(buf+len, size-len, "HBQ %d Info\n", i);
311
312         hbqs =  &phba->hbqs[i];
313         posted = 0;
314         list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list)
315                 posted++;
316
317         hip =  lpfc_hbq_defs[i];
318         len +=  snprintf(buf+len, size-len,
319                 "idx:%d prof:%d rn:%d bufcnt:%d icnt:%d acnt:%d posted %d\n",
320                 hip->hbq_index, hip->profile, hip->rn,
321                 hip->buffer_count, hip->init_count, hip->add_count, posted);
322
323         raw_index = phba->hbq_get[i];
324         getidx = le32_to_cpu(raw_index);
325         len +=  snprintf(buf+len, size-len,
326                 "entries:%d bufcnt:%d Put:%d nPut:%d localGet:%d hbaGet:%d\n",
327                 hbqs->entry_count, hbqs->buffer_count, hbqs->hbqPutIdx,
328                 hbqs->next_hbqPutIdx, hbqs->local_hbqGetIdx, getidx);
329
330         hbqe = (struct lpfc_hbq_entry *) phba->hbqs[i].hbq_virt;
331         for (j=0; j<hbqs->entry_count; j++) {
332                 len +=  snprintf(buf+len, size-len,
333                         "%03d: %08x %04x %05x ", j,
334                         le32_to_cpu(hbqe->bde.addrLow),
335                         le32_to_cpu(hbqe->bde.tus.w),
336                         le32_to_cpu(hbqe->buffer_tag));
337                 i = 0;
338                 found = 0;
339
340                 /* First calculate if slot has an associated posted buffer */
341                 low = hbqs->hbqPutIdx - posted;
342                 if (low >= 0) {
343                         if ((j >= hbqs->hbqPutIdx) || (j < low)) {
344                                 len +=  snprintf(buf+len, size-len, "Unused\n");
345                                 goto skipit;
346                         }
347                 }
348                 else {
349                         if ((j >= hbqs->hbqPutIdx) &&
350                                 (j < (hbqs->entry_count+low))) {
351                                 len +=  snprintf(buf+len, size-len, "Unused\n");
352                                 goto skipit;
353                         }
354                 }
355
356                 /* Get the Buffer info for the posted buffer */
357                 list_for_each_entry(d_buf, &hbqs->hbq_buffer_list, list) {
358                         hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
359                         phys = ((uint64_t)hbq_buf->dbuf.phys & 0xffffffff);
360                         if (phys == le32_to_cpu(hbqe->bde.addrLow)) {
361                                 len +=  snprintf(buf+len, size-len,
362                                         "Buf%d: %p %06x\n", i,
363                                         hbq_buf->dbuf.virt, hbq_buf->tag);
364                                 found = 1;
365                                 break;
366                         }
367                         i++;
368                 }
369                 if (!found) {
370                         len +=  snprintf(buf+len, size-len, "No DMAinfo?\n");
371                 }
372 skipit:
373                 hbqe++;
374                 if (len > LPFC_HBQINFO_SIZE - 54)
375                         break;
376         }
377         spin_unlock_irq(&phba->hbalock);
378         return len;
379 }
380
381 static int lpfc_debugfs_last_hba_slim_off;
382
383 /**
384  * lpfc_debugfs_dumpHBASlim_data - Dump HBA SLIM info to a buffer
385  * @phba: The HBA to gather SLIM info from.
386  * @buf: The buffer to dump log into.
387  * @size: The maximum amount of data to process.
388  *
389  * Description:
390  * This routine dumps the current contents of HBA SLIM for the HBA associated
391  * with @phba to @buf up to @size bytes of data. This is the raw HBA SLIM data.
392  *
393  * Notes:
394  * This routine will only dump up to 1024 bytes of data each time called and
395  * should be called multiple times to dump the entire HBA SLIM.
396  *
397  * Return Value:
398  * This routine returns the amount of bytes that were dumped into @buf and will
399  * not exceed @size.
400  **/
401 static int
402 lpfc_debugfs_dumpHBASlim_data(struct lpfc_hba *phba, char *buf, int size)
403 {
404         int len = 0;
405         int i, off;
406         uint32_t *ptr;
407         char *buffer;
408
409         buffer = kmalloc(1024, GFP_KERNEL);
410         if (!buffer)
411                 return 0;
412
413         off = 0;
414         spin_lock_irq(&phba->hbalock);
415
416         len +=  snprintf(buf+len, size-len, "HBA SLIM\n");
417         lpfc_memcpy_from_slim(buffer,
418                 phba->MBslimaddr + lpfc_debugfs_last_hba_slim_off, 1024);
419
420         ptr = (uint32_t *)&buffer[0];
421         off = lpfc_debugfs_last_hba_slim_off;
422
423         /* Set it up for the next time */
424         lpfc_debugfs_last_hba_slim_off += 1024;
425         if (lpfc_debugfs_last_hba_slim_off >= 4096)
426                 lpfc_debugfs_last_hba_slim_off = 0;
427
428         i = 1024;
429         while (i > 0) {
430                 len +=  snprintf(buf+len, size-len,
431                 "%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
432                 off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
433                 *(ptr+5), *(ptr+6), *(ptr+7));
434                 ptr += 8;
435                 i -= (8 * sizeof(uint32_t));
436                 off += (8 * sizeof(uint32_t));
437         }
438
439         spin_unlock_irq(&phba->hbalock);
440         kfree(buffer);
441
442         return len;
443 }
444
445 /**
446  * lpfc_debugfs_dumpHostSlim_data - Dump host SLIM info to a buffer
447  * @phba: The HBA to gather Host SLIM info from.
448  * @buf: The buffer to dump log into.
449  * @size: The maximum amount of data to process.
450  *
451  * Description:
452  * This routine dumps the current contents of host SLIM for the host associated
453  * with @phba to @buf up to @size bytes of data. The dump will contain the
454  * Mailbox, PCB, Rings, and Registers that are located in host memory.
455  *
456  * Return Value:
457  * This routine returns the amount of bytes that were dumped into @buf and will
458  * not exceed @size.
459  **/
460 static int
461 lpfc_debugfs_dumpHostSlim_data(struct lpfc_hba *phba, char *buf, int size)
462 {
463         int len = 0;
464         int i, off;
465         uint32_t word0, word1, word2, word3;
466         uint32_t *ptr;
467         struct lpfc_pgp *pgpp;
468         struct lpfc_sli *psli = &phba->sli;
469         struct lpfc_sli_ring *pring;
470
471         off = 0;
472         spin_lock_irq(&phba->hbalock);
473
474         len +=  snprintf(buf+len, size-len, "SLIM Mailbox\n");
475         ptr = (uint32_t *)phba->slim2p.virt;
476         i = sizeof(MAILBOX_t);
477         while (i > 0) {
478                 len +=  snprintf(buf+len, size-len,
479                 "%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
480                 off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
481                 *(ptr+5), *(ptr+6), *(ptr+7));
482                 ptr += 8;
483                 i -= (8 * sizeof(uint32_t));
484                 off += (8 * sizeof(uint32_t));
485         }
486
487         len +=  snprintf(buf+len, size-len, "SLIM PCB\n");
488         ptr = (uint32_t *)phba->pcb;
489         i = sizeof(PCB_t);
490         while (i > 0) {
491                 len +=  snprintf(buf+len, size-len,
492                 "%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
493                 off, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4),
494                 *(ptr+5), *(ptr+6), *(ptr+7));
495                 ptr += 8;
496                 i -= (8 * sizeof(uint32_t));
497                 off += (8 * sizeof(uint32_t));
498         }
499
500         if (phba->sli_rev <= LPFC_SLI_REV3) {
501                 for (i = 0; i < 4; i++) {
502                         pgpp = &phba->port_gp[i];
503                         pring = &psli->sli3_ring[i];
504                         len +=  snprintf(buf+len, size-len,
505                                          "Ring %d: CMD GetInx:%d "
506                                          "(Max:%d Next:%d "
507                                          "Local:%d flg:x%x)  "
508                                          "RSP PutInx:%d Max:%d\n",
509                                          i, pgpp->cmdGetInx,
510                                          pring->sli.sli3.numCiocb,
511                                          pring->sli.sli3.next_cmdidx,
512                                          pring->sli.sli3.local_getidx,
513                                          pring->flag, pgpp->rspPutInx,
514                                          pring->sli.sli3.numRiocb);
515                 }
516
517                 word0 = readl(phba->HAregaddr);
518                 word1 = readl(phba->CAregaddr);
519                 word2 = readl(phba->HSregaddr);
520                 word3 = readl(phba->HCregaddr);
521                 len +=  snprintf(buf+len, size-len, "HA:%08x CA:%08x HS:%08x "
522                                  "HC:%08x\n", word0, word1, word2, word3);
523         }
524         spin_unlock_irq(&phba->hbalock);
525         return len;
526 }
527
528 /**
529  * lpfc_debugfs_nodelist_data - Dump target node list to a buffer
530  * @vport: The vport to gather target node info from.
531  * @buf: The buffer to dump log into.
532  * @size: The maximum amount of data to process.
533  *
534  * Description:
535  * This routine dumps the current target node list associated with @vport to
536  * @buf up to @size bytes of data. Each node entry in the dump will contain a
537  * node state, DID, WWPN, WWNN, RPI, flags, type, and other useful fields.
538  *
539  * Return Value:
540  * This routine returns the amount of bytes that were dumped into @buf and will
541  * not exceed @size.
542  **/
543 static int
544 lpfc_debugfs_nodelist_data(struct lpfc_vport *vport, char *buf, int size)
545 {
546         int len = 0;
547         int i, iocnt, outio, cnt;
548         struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
549         struct lpfc_hba  *phba = vport->phba;
550         struct lpfc_nodelist *ndlp;
551         unsigned char *statep;
552         struct nvme_fc_local_port *localport;
553         struct nvme_fc_remote_port *nrport = NULL;
554         struct lpfc_nvme_rport *rport;
555
556         cnt = (LPFC_NODELIST_SIZE / LPFC_NODELIST_ENTRY_SIZE);
557         outio = 0;
558
559         len += snprintf(buf+len, size-len, "\nFCP Nodelist Entries ...\n");
560         spin_lock_irq(shost->host_lock);
561         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
562                 iocnt = 0;
563                 if (!cnt) {
564                         len +=  snprintf(buf+len, size-len,
565                                 "Missing Nodelist Entries\n");
566                         break;
567                 }
568                 cnt--;
569                 switch (ndlp->nlp_state) {
570                 case NLP_STE_UNUSED_NODE:
571                         statep = "UNUSED";
572                         break;
573                 case NLP_STE_PLOGI_ISSUE:
574                         statep = "PLOGI ";
575                         break;
576                 case NLP_STE_ADISC_ISSUE:
577                         statep = "ADISC ";
578                         break;
579                 case NLP_STE_REG_LOGIN_ISSUE:
580                         statep = "REGLOG";
581                         break;
582                 case NLP_STE_PRLI_ISSUE:
583                         statep = "PRLI  ";
584                         break;
585                 case NLP_STE_LOGO_ISSUE:
586                         statep = "LOGO  ";
587                         break;
588                 case NLP_STE_UNMAPPED_NODE:
589                         statep = "UNMAP ";
590                         iocnt = 1;
591                         break;
592                 case NLP_STE_MAPPED_NODE:
593                         statep = "MAPPED";
594                         iocnt = 1;
595                         break;
596                 case NLP_STE_NPR_NODE:
597                         statep = "NPR   ";
598                         break;
599                 default:
600                         statep = "UNKNOWN";
601                 }
602                 len += snprintf(buf+len, size-len, "%s DID:x%06x ",
603                                 statep, ndlp->nlp_DID);
604                 len += snprintf(buf+len, size-len,
605                                 "WWPN x%llx ",
606                                 wwn_to_u64(ndlp->nlp_portname.u.wwn));
607                 len += snprintf(buf+len, size-len,
608                                 "WWNN x%llx ",
609                                 wwn_to_u64(ndlp->nlp_nodename.u.wwn));
610                 if (ndlp->nlp_flag & NLP_RPI_REGISTERED)
611                         len += snprintf(buf+len, size-len, "RPI:%03d ",
612                                         ndlp->nlp_rpi);
613                 else
614                         len += snprintf(buf+len, size-len, "RPI:none ");
615                 len +=  snprintf(buf+len, size-len, "flag:x%08x ",
616                         ndlp->nlp_flag);
617                 if (!ndlp->nlp_type)
618                         len += snprintf(buf+len, size-len, "UNKNOWN_TYPE ");
619                 if (ndlp->nlp_type & NLP_FC_NODE)
620                         len += snprintf(buf+len, size-len, "FC_NODE ");
621                 if (ndlp->nlp_type & NLP_FABRIC) {
622                         len += snprintf(buf+len, size-len, "FABRIC ");
623                         iocnt = 0;
624                 }
625                 if (ndlp->nlp_type & NLP_FCP_TARGET)
626                         len += snprintf(buf+len, size-len, "FCP_TGT sid:%d ",
627                                 ndlp->nlp_sid);
628                 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
629                         len += snprintf(buf+len, size-len, "FCP_INITIATOR ");
630                 if (ndlp->nlp_type & NLP_NVME_TARGET)
631                         len += snprintf(buf + len,
632                                         size - len, "NVME_TGT sid:%d ",
633                                         NLP_NO_SID);
634                 if (ndlp->nlp_type & NLP_NVME_INITIATOR)
635                         len += snprintf(buf + len,
636                                         size - len, "NVME_INITIATOR ");
637                 len += snprintf(buf+len, size-len, "usgmap:%x ",
638                         ndlp->nlp_usg_map);
639                 len += snprintf(buf+len, size-len, "refcnt:%x",
640                         kref_read(&ndlp->kref));
641                 if (iocnt) {
642                         i = atomic_read(&ndlp->cmd_pending);
643                         len += snprintf(buf + len, size - len,
644                                         " OutIO:x%x Qdepth x%x",
645                                         i, ndlp->cmd_qdepth);
646                         outio += i;
647                 }
648                 len +=  snprintf(buf+len, size-len, "\n");
649         }
650         spin_unlock_irq(shost->host_lock);
651
652         len += snprintf(buf + len, size - len,
653                         "\nOutstanding IO x%x\n",  outio);
654
655         if (phba->nvmet_support && phba->targetport && (vport == phba->pport)) {
656                 len += snprintf(buf + len, size - len,
657                                 "\nNVME Targetport Entry ...\n");
658
659                 /* Port state is only one of two values for now. */
660                 if (phba->targetport->port_id)
661                         statep = "REGISTERED";
662                 else
663                         statep = "INIT";
664                 len += snprintf(buf + len, size - len,
665                                 "TGT WWNN x%llx WWPN x%llx State %s\n",
666                                 wwn_to_u64(vport->fc_nodename.u.wwn),
667                                 wwn_to_u64(vport->fc_portname.u.wwn),
668                                 statep);
669                 len += snprintf(buf + len, size - len,
670                                 "    Targetport DID x%06x\n",
671                                 phba->targetport->port_id);
672                 goto out_exit;
673         }
674
675         len += snprintf(buf + len, size - len,
676                                 "\nNVME Lport/Rport Entries ...\n");
677
678         localport = vport->localport;
679         if (!localport)
680                 goto out_exit;
681
682         spin_lock_irq(shost->host_lock);
683
684         /* Port state is only one of two values for now. */
685         if (localport->port_id)
686                 statep = "ONLINE";
687         else
688                 statep = "UNKNOWN ";
689
690         len += snprintf(buf + len, size - len,
691                         "Lport DID x%06x PortState %s\n",
692                         localport->port_id, statep);
693
694         len += snprintf(buf + len, size - len, "\tRport List:\n");
695         list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
696                 /* local short-hand pointer. */
697                 spin_lock(&phba->hbalock);
698                 rport = lpfc_ndlp_get_nrport(ndlp);
699                 if (rport)
700                         nrport = rport->remoteport;
701                 spin_unlock(&phba->hbalock);
702                 if (!nrport)
703                         continue;
704
705                 /* Port state is only one of two values for now. */
706                 switch (nrport->port_state) {
707                 case FC_OBJSTATE_ONLINE:
708                         statep = "ONLINE";
709                         break;
710                 case FC_OBJSTATE_UNKNOWN:
711                         statep = "UNKNOWN ";
712                         break;
713                 default:
714                         statep = "UNSUPPORTED";
715                         break;
716                 }
717
718                 /* Tab in to show lport ownership. */
719                 len += snprintf(buf + len, size - len,
720                                 "\t%s Port ID:x%06x ",
721                                 statep, nrport->port_id);
722                 len += snprintf(buf + len, size - len, "WWPN x%llx ",
723                                 nrport->port_name);
724                 len += snprintf(buf + len, size - len, "WWNN x%llx ",
725                                 nrport->node_name);
726
727                 /* An NVME rport can have multiple roles. */
728                 if (nrport->port_role & FC_PORT_ROLE_NVME_INITIATOR)
729                         len +=  snprintf(buf + len, size - len,
730                                          "INITIATOR ");
731                 if (nrport->port_role & FC_PORT_ROLE_NVME_TARGET)
732                         len +=  snprintf(buf + len, size - len,
733                                          "TARGET ");
734                 if (nrport->port_role & FC_PORT_ROLE_NVME_DISCOVERY)
735                         len +=  snprintf(buf + len, size - len,
736                                          "DISCSRVC ");
737                 if (nrport->port_role & ~(FC_PORT_ROLE_NVME_INITIATOR |
738                                           FC_PORT_ROLE_NVME_TARGET |
739                                           FC_PORT_ROLE_NVME_DISCOVERY))
740                         len +=  snprintf(buf + len, size - len,
741                                          "UNKNOWN ROLE x%x",
742                                          nrport->port_role);
743                 /* Terminate the string. */
744                 len +=  snprintf(buf + len, size - len, "\n");
745         }
746
747         spin_unlock_irq(shost->host_lock);
748  out_exit:
749         return len;
750 }
751
752 /**
753  * lpfc_debugfs_nvmestat_data - Dump target node list to a buffer
754  * @vport: The vport to gather target node info from.
755  * @buf: The buffer to dump log into.
756  * @size: The maximum amount of data to process.
757  *
758  * Description:
759  * This routine dumps the NVME statistics associated with @vport
760  *
761  * Return Value:
762  * This routine returns the amount of bytes that were dumped into @buf and will
763  * not exceed @size.
764  **/
765 static int
766 lpfc_debugfs_nvmestat_data(struct lpfc_vport *vport, char *buf, int size)
767 {
768         struct lpfc_hba   *phba = vport->phba;
769         struct lpfc_nvmet_tgtport *tgtp;
770         struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
771         struct nvme_fc_local_port *localport;
772         struct lpfc_nvme_ctrl_stat *cstat;
773         struct lpfc_nvme_lport *lport;
774         uint64_t data1, data2, data3;
775         uint64_t tot, totin, totout;
776         int cnt, i, maxch;
777         int len = 0;
778
779         if (phba->nvmet_support) {
780                 if (!phba->targetport)
781                         return len;
782                 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
783                 len += snprintf(buf + len, size - len,
784                                 "\nNVME Targetport Statistics\n");
785
786                 len += snprintf(buf + len, size - len,
787                                 "LS: Rcv %08x Drop %08x Abort %08x\n",
788                                 atomic_read(&tgtp->rcv_ls_req_in),
789                                 atomic_read(&tgtp->rcv_ls_req_drop),
790                                 atomic_read(&tgtp->xmt_ls_abort));
791                 if (atomic_read(&tgtp->rcv_ls_req_in) !=
792                     atomic_read(&tgtp->rcv_ls_req_out)) {
793                         len += snprintf(buf + len, size - len,
794                                         "Rcv LS: in %08x != out %08x\n",
795                                         atomic_read(&tgtp->rcv_ls_req_in),
796                                         atomic_read(&tgtp->rcv_ls_req_out));
797                 }
798
799                 len += snprintf(buf + len, size - len,
800                                 "LS: Xmt %08x Drop %08x Cmpl %08x\n",
801                                 atomic_read(&tgtp->xmt_ls_rsp),
802                                 atomic_read(&tgtp->xmt_ls_drop),
803                                 atomic_read(&tgtp->xmt_ls_rsp_cmpl));
804
805                 len += snprintf(buf + len, size - len,
806                                 "LS: RSP Abort %08x xb %08x Err %08x\n",
807                                 atomic_read(&tgtp->xmt_ls_rsp_aborted),
808                                 atomic_read(&tgtp->xmt_ls_rsp_xb_set),
809                                 atomic_read(&tgtp->xmt_ls_rsp_error));
810
811                 len += snprintf(buf + len, size - len,
812                                 "FCP: Rcv %08x Defer %08x Release %08x "
813                                 "Drop %08x\n",
814                                 atomic_read(&tgtp->rcv_fcp_cmd_in),
815                                 atomic_read(&tgtp->rcv_fcp_cmd_defer),
816                                 atomic_read(&tgtp->xmt_fcp_release),
817                                 atomic_read(&tgtp->rcv_fcp_cmd_drop));
818
819                 if (atomic_read(&tgtp->rcv_fcp_cmd_in) !=
820                     atomic_read(&tgtp->rcv_fcp_cmd_out)) {
821                         len += snprintf(buf + len, size - len,
822                                         "Rcv FCP: in %08x != out %08x\n",
823                                         atomic_read(&tgtp->rcv_fcp_cmd_in),
824                                         atomic_read(&tgtp->rcv_fcp_cmd_out));
825                 }
826
827                 len += snprintf(buf + len, size - len,
828                                 "FCP Rsp: read %08x readrsp %08x "
829                                 "write %08x rsp %08x\n",
830                                 atomic_read(&tgtp->xmt_fcp_read),
831                                 atomic_read(&tgtp->xmt_fcp_read_rsp),
832                                 atomic_read(&tgtp->xmt_fcp_write),
833                                 atomic_read(&tgtp->xmt_fcp_rsp));
834
835                 len += snprintf(buf + len, size - len,
836                                 "FCP Rsp Cmpl: %08x err %08x drop %08x\n",
837                                 atomic_read(&tgtp->xmt_fcp_rsp_cmpl),
838                                 atomic_read(&tgtp->xmt_fcp_rsp_error),
839                                 atomic_read(&tgtp->xmt_fcp_rsp_drop));
840
841                 len += snprintf(buf + len, size - len,
842                                 "FCP Rsp Abort: %08x xb %08x xricqe  %08x\n",
843                                 atomic_read(&tgtp->xmt_fcp_rsp_aborted),
844                                 atomic_read(&tgtp->xmt_fcp_rsp_xb_set),
845                                 atomic_read(&tgtp->xmt_fcp_xri_abort_cqe));
846
847                 len += snprintf(buf + len, size - len,
848                                 "ABORT: Xmt %08x Cmpl %08x\n",
849                                 atomic_read(&tgtp->xmt_fcp_abort),
850                                 atomic_read(&tgtp->xmt_fcp_abort_cmpl));
851
852                 len += snprintf(buf + len, size - len,
853                                 "ABORT: Sol %08x  Usol %08x Err %08x Cmpl %08x",
854                                 atomic_read(&tgtp->xmt_abort_sol),
855                                 atomic_read(&tgtp->xmt_abort_unsol),
856                                 atomic_read(&tgtp->xmt_abort_rsp),
857                                 atomic_read(&tgtp->xmt_abort_rsp_error));
858
859                 len +=  snprintf(buf + len, size - len, "\n");
860
861                 cnt = 0;
862                 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
863                 list_for_each_entry_safe(ctxp, next_ctxp,
864                                 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
865                                 list) {
866                         cnt++;
867                 }
868                 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
869                 if (cnt) {
870                         len += snprintf(buf + len, size - len,
871                                         "ABORT: %d ctx entries\n", cnt);
872                         spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
873                         list_for_each_entry_safe(ctxp, next_ctxp,
874                                     &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
875                                     list) {
876                                 if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ))
877                                         break;
878                                 len += snprintf(buf + len, size - len,
879                                                 "Entry: oxid %x state %x "
880                                                 "flag %x\n",
881                                                 ctxp->oxid, ctxp->state,
882                                                 ctxp->flag);
883                         }
884                         spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
885                 }
886
887                 /* Calculate outstanding IOs */
888                 tot = atomic_read(&tgtp->rcv_fcp_cmd_drop);
889                 tot += atomic_read(&tgtp->xmt_fcp_release);
890                 tot = atomic_read(&tgtp->rcv_fcp_cmd_in) - tot;
891
892                 len += snprintf(buf + len, size - len,
893                                 "IO_CTX: %08x  WAIT: cur %08x tot %08x\n"
894                                 "CTX Outstanding %08llx\n",
895                                 phba->sli4_hba.nvmet_xri_cnt,
896                                 phba->sli4_hba.nvmet_io_wait_cnt,
897                                 phba->sli4_hba.nvmet_io_wait_total,
898                                 tot);
899         } else {
900                 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
901                         return len;
902
903                 localport = vport->localport;
904                 if (!localport)
905                         return len;
906                 lport = (struct lpfc_nvme_lport *)localport->private;
907                 if (!lport)
908                         return len;
909
910                 len += snprintf(buf + len, size - len,
911                                 "\nNVME Lport Statistics\n");
912
913                 len += snprintf(buf + len, size - len,
914                                 "LS: Xmt %016x Cmpl %016x\n",
915                                 atomic_read(&lport->fc4NvmeLsRequests),
916                                 atomic_read(&lport->fc4NvmeLsCmpls));
917
918                 if (phba->cfg_nvme_io_channel < 32)
919                         maxch = phba->cfg_nvme_io_channel;
920                 else
921                         maxch = 32;
922                 totin = 0;
923                 totout = 0;
924                 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
925                         cstat = &lport->cstat[i];
926                         tot = atomic_read(&cstat->fc4NvmeIoCmpls);
927                         totin += tot;
928                         data1 = atomic_read(&cstat->fc4NvmeInputRequests);
929                         data2 = atomic_read(&cstat->fc4NvmeOutputRequests);
930                         data3 = atomic_read(&cstat->fc4NvmeControlRequests);
931                         totout += (data1 + data2 + data3);
932
933                         /* Limit to 32, debugfs display buffer limitation */
934                         if (i >= 32)
935                                 continue;
936
937                         len += snprintf(buf + len, PAGE_SIZE - len,
938                                         "FCP (%d): Rd %016llx Wr %016llx "
939                                         "IO %016llx ",
940                                         i, data1, data2, data3);
941                         len += snprintf(buf + len, PAGE_SIZE - len,
942                                         "Cmpl %016llx OutIO %016llx\n",
943                                         tot, ((data1 + data2 + data3) - tot));
944                 }
945                 len += snprintf(buf + len, PAGE_SIZE - len,
946                                 "Total FCP Cmpl %016llx Issue %016llx "
947                                 "OutIO %016llx\n",
948                                 totin, totout, totout - totin);
949
950                 len += snprintf(buf + len, size - len,
951                                 "LS Xmt Err: Abrt %08x Err %08x  "
952                                 "Cmpl Err: xb %08x Err %08x\n",
953                                 atomic_read(&lport->xmt_ls_abort),
954                                 atomic_read(&lport->xmt_ls_err),
955                                 atomic_read(&lport->cmpl_ls_xb),
956                                 atomic_read(&lport->cmpl_ls_err));
957
958                 len += snprintf(buf + len, size - len,
959                                 "FCP Xmt Err: noxri %06x nondlp %06x "
960                                 "qdepth %06x wqerr %06x err %06x Abrt %06x\n",
961                                 atomic_read(&lport->xmt_fcp_noxri),
962                                 atomic_read(&lport->xmt_fcp_bad_ndlp),
963                                 atomic_read(&lport->xmt_fcp_qdepth),
964                                 atomic_read(&lport->xmt_fcp_wqerr),
965                                 atomic_read(&lport->xmt_fcp_err),
966                                 atomic_read(&lport->xmt_fcp_abort));
967
968                 len += snprintf(buf + len, size - len,
969                                 "FCP Cmpl Err: xb %08x Err %08x\n",
970                                 atomic_read(&lport->cmpl_fcp_xb),
971                                 atomic_read(&lport->cmpl_fcp_err));
972
973         }
974
975         return len;
976 }
977
978
979 /**
980  * lpfc_debugfs_nvmektime_data - Dump target node list to a buffer
981  * @vport: The vport to gather target node info from.
982  * @buf: The buffer to dump log into.
983  * @size: The maximum amount of data to process.
984  *
985  * Description:
986  * This routine dumps the NVME statistics associated with @vport
987  *
988  * Return Value:
989  * This routine returns the amount of bytes that were dumped into @buf and will
990  * not exceed @size.
991  **/
992 static int
993 lpfc_debugfs_nvmektime_data(struct lpfc_vport *vport, char *buf, int size)
994 {
995         struct lpfc_hba   *phba = vport->phba;
996         int len = 0;
997
998         if (phba->nvmet_support == 0) {
999                 /* NVME Initiator */
1000                 len += snprintf(buf + len, PAGE_SIZE - len,
1001                                 "ktime %s: Total Samples: %lld\n",
1002                                 (phba->ktime_on ?  "Enabled" : "Disabled"),
1003                                 phba->ktime_data_samples);
1004                 if (phba->ktime_data_samples == 0)
1005                         return len;
1006
1007                 len += snprintf(
1008                         buf + len, PAGE_SIZE - len,
1009                         "Segment 1: Last NVME Cmd cmpl "
1010                         "done -to- Start of next NVME cnd (in driver)\n");
1011                 len += snprintf(
1012                         buf + len, PAGE_SIZE - len,
1013                         "avg:%08lld min:%08lld max %08lld\n",
1014                         div_u64(phba->ktime_seg1_total,
1015                                 phba->ktime_data_samples),
1016                         phba->ktime_seg1_min,
1017                         phba->ktime_seg1_max);
1018                 len += snprintf(
1019                         buf + len, PAGE_SIZE - len,
1020                         "Segment 2: Driver start of NVME cmd "
1021                         "-to- Firmware WQ doorbell\n");
1022                 len += snprintf(
1023                         buf + len, PAGE_SIZE - len,
1024                         "avg:%08lld min:%08lld max %08lld\n",
1025                         div_u64(phba->ktime_seg2_total,
1026                                 phba->ktime_data_samples),
1027                         phba->ktime_seg2_min,
1028                         phba->ktime_seg2_max);
1029                 len += snprintf(
1030                         buf + len, PAGE_SIZE - len,
1031                         "Segment 3: Firmware WQ doorbell -to- "
1032                         "MSI-X ISR cmpl\n");
1033                 len += snprintf(
1034                         buf + len, PAGE_SIZE - len,
1035                         "avg:%08lld min:%08lld max %08lld\n",
1036                         div_u64(phba->ktime_seg3_total,
1037                                 phba->ktime_data_samples),
1038                         phba->ktime_seg3_min,
1039                         phba->ktime_seg3_max);
1040                 len += snprintf(
1041                         buf + len, PAGE_SIZE - len,
1042                         "Segment 4: MSI-X ISR cmpl -to- "
1043                         "NVME cmpl done\n");
1044                 len += snprintf(
1045                         buf + len, PAGE_SIZE - len,
1046                         "avg:%08lld min:%08lld max %08lld\n",
1047                         div_u64(phba->ktime_seg4_total,
1048                                 phba->ktime_data_samples),
1049                         phba->ktime_seg4_min,
1050                         phba->ktime_seg4_max);
1051                 len += snprintf(
1052                         buf + len, PAGE_SIZE - len,
1053                         "Total IO avg time: %08lld\n",
1054                         div_u64(phba->ktime_seg1_total +
1055                         phba->ktime_seg2_total  +
1056                         phba->ktime_seg3_total +
1057                         phba->ktime_seg4_total,
1058                         phba->ktime_data_samples));
1059                 return len;
1060         }
1061
1062         /* NVME Target */
1063         len += snprintf(buf + len, PAGE_SIZE-len,
1064                         "ktime %s: Total Samples: %lld %lld\n",
1065                         (phba->ktime_on ? "Enabled" : "Disabled"),
1066                         phba->ktime_data_samples,
1067                         phba->ktime_status_samples);
1068         if (phba->ktime_data_samples == 0)
1069                 return len;
1070
1071         len += snprintf(buf + len, PAGE_SIZE-len,
1072                         "Segment 1: MSI-X ISR Rcv cmd -to- "
1073                         "cmd pass to NVME Layer\n");
1074         len += snprintf(buf + len, PAGE_SIZE-len,
1075                         "avg:%08lld min:%08lld max %08lld\n",
1076                         div_u64(phba->ktime_seg1_total,
1077                                 phba->ktime_data_samples),
1078                         phba->ktime_seg1_min,
1079                         phba->ktime_seg1_max);
1080         len += snprintf(buf + len, PAGE_SIZE-len,
1081                         "Segment 2: cmd pass to NVME Layer- "
1082                         "-to- Driver rcv cmd OP (action)\n");
1083         len += snprintf(buf + len, PAGE_SIZE-len,
1084                         "avg:%08lld min:%08lld max %08lld\n",
1085                         div_u64(phba->ktime_seg2_total,
1086                                 phba->ktime_data_samples),
1087                         phba->ktime_seg2_min,
1088                         phba->ktime_seg2_max);
1089         len += snprintf(buf + len, PAGE_SIZE-len,
1090                         "Segment 3: Driver rcv cmd OP -to- "
1091                         "Firmware WQ doorbell: cmd\n");
1092         len += snprintf(buf + len, PAGE_SIZE-len,
1093                         "avg:%08lld min:%08lld max %08lld\n",
1094                         div_u64(phba->ktime_seg3_total,
1095                                 phba->ktime_data_samples),
1096                         phba->ktime_seg3_min,
1097                         phba->ktime_seg3_max);
1098         len += snprintf(buf + len, PAGE_SIZE-len,
1099                         "Segment 4: Firmware WQ doorbell: cmd "
1100                         "-to- MSI-X ISR for cmd cmpl\n");
1101         len += snprintf(buf + len, PAGE_SIZE-len,
1102                         "avg:%08lld min:%08lld max %08lld\n",
1103                         div_u64(phba->ktime_seg4_total,
1104                                 phba->ktime_data_samples),
1105                         phba->ktime_seg4_min,
1106                         phba->ktime_seg4_max);
1107         len += snprintf(buf + len, PAGE_SIZE-len,
1108                         "Segment 5: MSI-X ISR for cmd cmpl "
1109                         "-to- NVME layer passed cmd done\n");
1110         len += snprintf(buf + len, PAGE_SIZE-len,
1111                         "avg:%08lld min:%08lld max %08lld\n",
1112                         div_u64(phba->ktime_seg5_total,
1113                                 phba->ktime_data_samples),
1114                         phba->ktime_seg5_min,
1115                         phba->ktime_seg5_max);
1116
1117         if (phba->ktime_status_samples == 0) {
1118                 len += snprintf(buf + len, PAGE_SIZE-len,
1119                                 "Total: cmd received by MSI-X ISR "
1120                                 "-to- cmd completed on wire\n");
1121                 len += snprintf(buf + len, PAGE_SIZE-len,
1122                                 "avg:%08lld min:%08lld "
1123                                 "max %08lld\n",
1124                                 div_u64(phba->ktime_seg10_total,
1125                                         phba->ktime_data_samples),
1126                                 phba->ktime_seg10_min,
1127                                 phba->ktime_seg10_max);
1128                 return len;
1129         }
1130
1131         len += snprintf(buf + len, PAGE_SIZE-len,
1132                         "Segment 6: NVME layer passed cmd done "
1133                         "-to- Driver rcv rsp status OP\n");
1134         len += snprintf(buf + len, PAGE_SIZE-len,
1135                         "avg:%08lld min:%08lld max %08lld\n",
1136                         div_u64(phba->ktime_seg6_total,
1137                                 phba->ktime_status_samples),
1138                         phba->ktime_seg6_min,
1139                         phba->ktime_seg6_max);
1140         len += snprintf(buf + len, PAGE_SIZE-len,
1141                         "Segment 7: Driver rcv rsp status OP "
1142                         "-to- Firmware WQ doorbell: status\n");
1143         len += snprintf(buf + len, PAGE_SIZE-len,
1144                         "avg:%08lld min:%08lld max %08lld\n",
1145                         div_u64(phba->ktime_seg7_total,
1146                                 phba->ktime_status_samples),
1147                         phba->ktime_seg7_min,
1148                         phba->ktime_seg7_max);
1149         len += snprintf(buf + len, PAGE_SIZE-len,
1150                         "Segment 8: Firmware WQ doorbell: status"
1151                         " -to- MSI-X ISR for status cmpl\n");
1152         len += snprintf(buf + len, PAGE_SIZE-len,
1153                         "avg:%08lld min:%08lld max %08lld\n",
1154                         div_u64(phba->ktime_seg8_total,
1155                                 phba->ktime_status_samples),
1156                         phba->ktime_seg8_min,
1157                         phba->ktime_seg8_max);
1158         len += snprintf(buf + len, PAGE_SIZE-len,
1159                         "Segment 9: MSI-X ISR for status cmpl  "
1160                         "-to- NVME layer passed status done\n");
1161         len += snprintf(buf + len, PAGE_SIZE-len,
1162                         "avg:%08lld min:%08lld max %08lld\n",
1163                         div_u64(phba->ktime_seg9_total,
1164                                 phba->ktime_status_samples),
1165                         phba->ktime_seg9_min,
1166                         phba->ktime_seg9_max);
1167         len += snprintf(buf + len, PAGE_SIZE-len,
1168                         "Total: cmd received by MSI-X ISR -to- "
1169                         "cmd completed on wire\n");
1170         len += snprintf(buf + len, PAGE_SIZE-len,
1171                         "avg:%08lld min:%08lld max %08lld\n",
1172                         div_u64(phba->ktime_seg10_total,
1173                                 phba->ktime_status_samples),
1174                         phba->ktime_seg10_min,
1175                         phba->ktime_seg10_max);
1176         return len;
1177 }
1178
1179 /**
1180  * lpfc_debugfs_nvmeio_trc_data - Dump NVME IO trace list to a buffer
1181  * @phba: The phba to gather target node info from.
1182  * @buf: The buffer to dump log into.
1183  * @size: The maximum amount of data to process.
1184  *
1185  * Description:
1186  * This routine dumps the NVME IO trace associated with @phba
1187  *
1188  * Return Value:
1189  * This routine returns the amount of bytes that were dumped into @buf and will
1190  * not exceed @size.
1191  **/
1192 static int
1193 lpfc_debugfs_nvmeio_trc_data(struct lpfc_hba *phba, char *buf, int size)
1194 {
1195         struct lpfc_debugfs_nvmeio_trc *dtp;
1196         int i, state, index, skip;
1197         int len = 0;
1198
1199         state = phba->nvmeio_trc_on;
1200
1201         index = (atomic_read(&phba->nvmeio_trc_cnt) + 1) &
1202                 (phba->nvmeio_trc_size - 1);
1203         skip = phba->nvmeio_trc_output_idx;
1204
1205         len += snprintf(buf + len, size - len,
1206                         "%s IO Trace %s: next_idx %d skip %d size %d\n",
1207                         (phba->nvmet_support ? "NVME" : "NVMET"),
1208                         (state ? "Enabled" : "Disabled"),
1209                         index, skip, phba->nvmeio_trc_size);
1210
1211         if (!phba->nvmeio_trc || state)
1212                 return len;
1213
1214         /* trace MUST bhe off to continue */
1215
1216         for (i = index; i < phba->nvmeio_trc_size; i++) {
1217                 if (skip) {
1218                         skip--;
1219                         continue;
1220                 }
1221                 dtp = phba->nvmeio_trc + i;
1222                 phba->nvmeio_trc_output_idx++;
1223
1224                 if (!dtp->fmt)
1225                         continue;
1226
1227                 len +=  snprintf(buf + len, size - len, dtp->fmt,
1228                         dtp->data1, dtp->data2, dtp->data3);
1229
1230                 if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1231                         phba->nvmeio_trc_output_idx = 0;
1232                         len += snprintf(buf + len, size - len,
1233                                         "Trace Complete\n");
1234                         goto out;
1235                 }
1236
1237                 if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1238                         len += snprintf(buf + len, size - len,
1239                                         "Trace Continue (%d of %d)\n",
1240                                         phba->nvmeio_trc_output_idx,
1241                                         phba->nvmeio_trc_size);
1242                         goto out;
1243                 }
1244         }
1245         for (i = 0; i < index; i++) {
1246                 if (skip) {
1247                         skip--;
1248                         continue;
1249                 }
1250                 dtp = phba->nvmeio_trc + i;
1251                 phba->nvmeio_trc_output_idx++;
1252
1253                 if (!dtp->fmt)
1254                         continue;
1255
1256                 len +=  snprintf(buf + len, size - len, dtp->fmt,
1257                         dtp->data1, dtp->data2, dtp->data3);
1258
1259                 if (phba->nvmeio_trc_output_idx >= phba->nvmeio_trc_size) {
1260                         phba->nvmeio_trc_output_idx = 0;
1261                         len += snprintf(buf + len, size - len,
1262                                         "Trace Complete\n");
1263                         goto out;
1264                 }
1265
1266                 if (len >= (size - LPFC_DEBUG_OUT_LINE_SZ)) {
1267                         len += snprintf(buf + len, size - len,
1268                                         "Trace Continue (%d of %d)\n",
1269                                         phba->nvmeio_trc_output_idx,
1270                                         phba->nvmeio_trc_size);
1271                         goto out;
1272                 }
1273         }
1274
1275         len += snprintf(buf + len, size - len,
1276                         "Trace Done\n");
1277 out:
1278         return len;
1279 }
1280
1281 /**
1282  * lpfc_debugfs_cpucheck_data - Dump target node list to a buffer
1283  * @vport: The vport to gather target node info from.
1284  * @buf: The buffer to dump log into.
1285  * @size: The maximum amount of data to process.
1286  *
1287  * Description:
1288  * This routine dumps the NVME statistics associated with @vport
1289  *
1290  * Return Value:
1291  * This routine returns the amount of bytes that were dumped into @buf and will
1292  * not exceed @size.
1293  **/
1294 static int
1295 lpfc_debugfs_cpucheck_data(struct lpfc_vport *vport, char *buf, int size)
1296 {
1297         struct lpfc_hba   *phba = vport->phba;
1298         int i;
1299         int len = 0;
1300         uint32_t tot_xmt = 0;
1301         uint32_t tot_rcv = 0;
1302         uint32_t tot_cmpl = 0;
1303         uint32_t tot_ccmpl = 0;
1304
1305         if (phba->nvmet_support == 0) {
1306                 /* NVME Initiator */
1307                 len += snprintf(buf + len, PAGE_SIZE - len,
1308                                 "CPUcheck %s\n",
1309                                 (phba->cpucheck_on & LPFC_CHECK_NVME_IO ?
1310                                         "Enabled" : "Disabled"));
1311                 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
1312                         if (i >= LPFC_CHECK_CPU_CNT)
1313                                 break;
1314                         len += snprintf(buf + len, PAGE_SIZE - len,
1315                                         "%02d: xmit x%08x cmpl x%08x\n",
1316                                         i, phba->cpucheck_xmt_io[i],
1317                                         phba->cpucheck_cmpl_io[i]);
1318                         tot_xmt += phba->cpucheck_xmt_io[i];
1319                         tot_cmpl += phba->cpucheck_cmpl_io[i];
1320                 }
1321                 len += snprintf(buf + len, PAGE_SIZE - len,
1322                                 "tot:xmit x%08x cmpl x%08x\n",
1323                                 tot_xmt, tot_cmpl);
1324                 return len;
1325         }
1326
1327         /* NVME Target */
1328         len += snprintf(buf + len, PAGE_SIZE - len,
1329                         "CPUcheck %s ",
1330                         (phba->cpucheck_on & LPFC_CHECK_NVMET_IO ?
1331                                 "IO Enabled - " : "IO Disabled - "));
1332         len += snprintf(buf + len, PAGE_SIZE - len,
1333                         "%s\n",
1334                         (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV ?
1335                                 "Rcv Enabled\n" : "Rcv Disabled\n"));
1336         for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
1337                 if (i >= LPFC_CHECK_CPU_CNT)
1338                         break;
1339                 len += snprintf(buf + len, PAGE_SIZE - len,
1340                                 "%02d: xmit x%08x ccmpl x%08x "
1341                                 "cmpl x%08x rcv x%08x\n",
1342                                 i, phba->cpucheck_xmt_io[i],
1343                                 phba->cpucheck_ccmpl_io[i],
1344                                 phba->cpucheck_cmpl_io[i],
1345                                 phba->cpucheck_rcv_io[i]);
1346                 tot_xmt += phba->cpucheck_xmt_io[i];
1347                 tot_rcv += phba->cpucheck_rcv_io[i];
1348                 tot_cmpl += phba->cpucheck_cmpl_io[i];
1349                 tot_ccmpl += phba->cpucheck_ccmpl_io[i];
1350         }
1351         len += snprintf(buf + len, PAGE_SIZE - len,
1352                         "tot:xmit x%08x ccmpl x%08x cmpl x%08x rcv x%08x\n",
1353                         tot_xmt, tot_ccmpl, tot_cmpl, tot_rcv);
1354         return len;
1355 }
1356
1357 #endif
1358
1359 /**
1360  * lpfc_debugfs_disc_trc - Store discovery trace log
1361  * @vport: The vport to associate this trace string with for retrieval.
1362  * @mask: Log entry classification.
1363  * @fmt: Format string to be displayed when dumping the log.
1364  * @data1: 1st data parameter to be applied to @fmt.
1365  * @data2: 2nd data parameter to be applied to @fmt.
1366  * @data3: 3rd data parameter to be applied to @fmt.
1367  *
1368  * Description:
1369  * This routine is used by the driver code to add a debugfs log entry to the
1370  * discovery trace buffer associated with @vport. Only entries with a @mask that
1371  * match the current debugfs discovery mask will be saved. Entries that do not
1372  * match will be thrown away. @fmt, @data1, @data2, and @data3 are used like
1373  * printf when displaying the log.
1374  **/
1375 inline void
1376 lpfc_debugfs_disc_trc(struct lpfc_vport *vport, int mask, char *fmt,
1377         uint32_t data1, uint32_t data2, uint32_t data3)
1378 {
1379 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1380         struct lpfc_debugfs_trc *dtp;
1381         int index;
1382
1383         if (!(lpfc_debugfs_mask_disc_trc & mask))
1384                 return;
1385
1386         if (!lpfc_debugfs_enable || !lpfc_debugfs_max_disc_trc ||
1387                 !vport || !vport->disc_trc)
1388                 return;
1389
1390         index = atomic_inc_return(&vport->disc_trc_cnt) &
1391                 (lpfc_debugfs_max_disc_trc - 1);
1392         dtp = vport->disc_trc + index;
1393         dtp->fmt = fmt;
1394         dtp->data1 = data1;
1395         dtp->data2 = data2;
1396         dtp->data3 = data3;
1397         dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1398         dtp->jif = jiffies;
1399 #endif
1400         return;
1401 }
1402
1403 /**
1404  * lpfc_debugfs_slow_ring_trc - Store slow ring trace log
1405  * @phba: The phba to associate this trace string with for retrieval.
1406  * @fmt: Format string to be displayed when dumping the log.
1407  * @data1: 1st data parameter to be applied to @fmt.
1408  * @data2: 2nd data parameter to be applied to @fmt.
1409  * @data3: 3rd data parameter to be applied to @fmt.
1410  *
1411  * Description:
1412  * This routine is used by the driver code to add a debugfs log entry to the
1413  * discovery trace buffer associated with @vport. @fmt, @data1, @data2, and
1414  * @data3 are used like printf when displaying the log.
1415  **/
1416 inline void
1417 lpfc_debugfs_slow_ring_trc(struct lpfc_hba *phba, char *fmt,
1418         uint32_t data1, uint32_t data2, uint32_t data3)
1419 {
1420 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1421         struct lpfc_debugfs_trc *dtp;
1422         int index;
1423
1424         if (!lpfc_debugfs_enable || !lpfc_debugfs_max_slow_ring_trc ||
1425                 !phba || !phba->slow_ring_trc)
1426                 return;
1427
1428         index = atomic_inc_return(&phba->slow_ring_trc_cnt) &
1429                 (lpfc_debugfs_max_slow_ring_trc - 1);
1430         dtp = phba->slow_ring_trc + index;
1431         dtp->fmt = fmt;
1432         dtp->data1 = data1;
1433         dtp->data2 = data2;
1434         dtp->data3 = data3;
1435         dtp->seq_cnt = atomic_inc_return(&lpfc_debugfs_seq_trc_cnt);
1436         dtp->jif = jiffies;
1437 #endif
1438         return;
1439 }
1440
1441 /**
1442  * lpfc_debugfs_nvme_trc - Store NVME/NVMET trace log
1443  * @phba: The phba to associate this trace string with for retrieval.
1444  * @fmt: Format string to be displayed when dumping the log.
1445  * @data1: 1st data parameter to be applied to @fmt.
1446  * @data2: 2nd data parameter to be applied to @fmt.
1447  * @data3: 3rd data parameter to be applied to @fmt.
1448  *
1449  * Description:
1450  * This routine is used by the driver code to add a debugfs log entry to the
1451  * nvme trace buffer associated with @phba. @fmt, @data1, @data2, and
1452  * @data3 are used like printf when displaying the log.
1453  **/
1454 inline void
1455 lpfc_debugfs_nvme_trc(struct lpfc_hba *phba, char *fmt,
1456                       uint16_t data1, uint16_t data2, uint32_t data3)
1457 {
1458 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1459         struct lpfc_debugfs_nvmeio_trc *dtp;
1460         int index;
1461
1462         if (!phba->nvmeio_trc_on || !phba->nvmeio_trc)
1463                 return;
1464
1465         index = atomic_inc_return(&phba->nvmeio_trc_cnt) &
1466                 (phba->nvmeio_trc_size - 1);
1467         dtp = phba->nvmeio_trc + index;
1468         dtp->fmt = fmt;
1469         dtp->data1 = data1;
1470         dtp->data2 = data2;
1471         dtp->data3 = data3;
1472 #endif
1473 }
1474
1475 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1476 /**
1477  * lpfc_debugfs_disc_trc_open - Open the discovery trace log
1478  * @inode: The inode pointer that contains a vport pointer.
1479  * @file: The file pointer to attach the log output.
1480  *
1481  * Description:
1482  * This routine is the entry point for the debugfs open file operation. It gets
1483  * the vport from the i_private field in @inode, allocates the necessary buffer
1484  * for the log, fills the buffer from the in-memory log for this vport, and then
1485  * returns a pointer to that log in the private_data field in @file.
1486  *
1487  * Returns:
1488  * This function returns zero if successful. On error it will return a negative
1489  * error value.
1490  **/
1491 static int
1492 lpfc_debugfs_disc_trc_open(struct inode *inode, struct file *file)
1493 {
1494         struct lpfc_vport *vport = inode->i_private;
1495         struct lpfc_debug *debug;
1496         int size;
1497         int rc = -ENOMEM;
1498
1499         if (!lpfc_debugfs_max_disc_trc) {
1500                  rc = -ENOSPC;
1501                 goto out;
1502         }
1503
1504         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1505         if (!debug)
1506                 goto out;
1507
1508         /* Round to page boundary */
1509         size =  (lpfc_debugfs_max_disc_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
1510         size = PAGE_ALIGN(size);
1511
1512         debug->buffer = kmalloc(size, GFP_KERNEL);
1513         if (!debug->buffer) {
1514                 kfree(debug);
1515                 goto out;
1516         }
1517
1518         debug->len = lpfc_debugfs_disc_trc_data(vport, debug->buffer, size);
1519         file->private_data = debug;
1520
1521         rc = 0;
1522 out:
1523         return rc;
1524 }
1525
1526 /**
1527  * lpfc_debugfs_slow_ring_trc_open - Open the Slow Ring trace log
1528  * @inode: The inode pointer that contains a vport pointer.
1529  * @file: The file pointer to attach the log output.
1530  *
1531  * Description:
1532  * This routine is the entry point for the debugfs open file operation. It gets
1533  * the vport from the i_private field in @inode, allocates the necessary buffer
1534  * for the log, fills the buffer from the in-memory log for this vport, and then
1535  * returns a pointer to that log in the private_data field in @file.
1536  *
1537  * Returns:
1538  * This function returns zero if successful. On error it will return a negative
1539  * error value.
1540  **/
1541 static int
1542 lpfc_debugfs_slow_ring_trc_open(struct inode *inode, struct file *file)
1543 {
1544         struct lpfc_hba *phba = inode->i_private;
1545         struct lpfc_debug *debug;
1546         int size;
1547         int rc = -ENOMEM;
1548
1549         if (!lpfc_debugfs_max_slow_ring_trc) {
1550                  rc = -ENOSPC;
1551                 goto out;
1552         }
1553
1554         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1555         if (!debug)
1556                 goto out;
1557
1558         /* Round to page boundary */
1559         size =  (lpfc_debugfs_max_slow_ring_trc * LPFC_DEBUG_TRC_ENTRY_SIZE);
1560         size = PAGE_ALIGN(size);
1561
1562         debug->buffer = kmalloc(size, GFP_KERNEL);
1563         if (!debug->buffer) {
1564                 kfree(debug);
1565                 goto out;
1566         }
1567
1568         debug->len = lpfc_debugfs_slow_ring_trc_data(phba, debug->buffer, size);
1569         file->private_data = debug;
1570
1571         rc = 0;
1572 out:
1573         return rc;
1574 }
1575
1576 /**
1577  * lpfc_debugfs_hbqinfo_open - Open the hbqinfo debugfs buffer
1578  * @inode: The inode pointer that contains a vport pointer.
1579  * @file: The file pointer to attach the log output.
1580  *
1581  * Description:
1582  * This routine is the entry point for the debugfs open file operation. It gets
1583  * the vport from the i_private field in @inode, allocates the necessary buffer
1584  * for the log, fills the buffer from the in-memory log for this vport, and then
1585  * returns a pointer to that log in the private_data field in @file.
1586  *
1587  * Returns:
1588  * This function returns zero if successful. On error it will return a negative
1589  * error value.
1590  **/
1591 static int
1592 lpfc_debugfs_hbqinfo_open(struct inode *inode, struct file *file)
1593 {
1594         struct lpfc_hba *phba = inode->i_private;
1595         struct lpfc_debug *debug;
1596         int rc = -ENOMEM;
1597
1598         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1599         if (!debug)
1600                 goto out;
1601
1602         /* Round to page boundary */
1603         debug->buffer = kmalloc(LPFC_HBQINFO_SIZE, GFP_KERNEL);
1604         if (!debug->buffer) {
1605                 kfree(debug);
1606                 goto out;
1607         }
1608
1609         debug->len = lpfc_debugfs_hbqinfo_data(phba, debug->buffer,
1610                 LPFC_HBQINFO_SIZE);
1611         file->private_data = debug;
1612
1613         rc = 0;
1614 out:
1615         return rc;
1616 }
1617
1618 /**
1619  * lpfc_debugfs_dumpHBASlim_open - Open the Dump HBA SLIM debugfs buffer
1620  * @inode: The inode pointer that contains a vport pointer.
1621  * @file: The file pointer to attach the log output.
1622  *
1623  * Description:
1624  * This routine is the entry point for the debugfs open file operation. It gets
1625  * the vport from the i_private field in @inode, allocates the necessary buffer
1626  * for the log, fills the buffer from the in-memory log for this vport, and then
1627  * returns a pointer to that log in the private_data field in @file.
1628  *
1629  * Returns:
1630  * This function returns zero if successful. On error it will return a negative
1631  * error value.
1632  **/
1633 static int
1634 lpfc_debugfs_dumpHBASlim_open(struct inode *inode, struct file *file)
1635 {
1636         struct lpfc_hba *phba = inode->i_private;
1637         struct lpfc_debug *debug;
1638         int rc = -ENOMEM;
1639
1640         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1641         if (!debug)
1642                 goto out;
1643
1644         /* Round to page boundary */
1645         debug->buffer = kmalloc(LPFC_DUMPHBASLIM_SIZE, GFP_KERNEL);
1646         if (!debug->buffer) {
1647                 kfree(debug);
1648                 goto out;
1649         }
1650
1651         debug->len = lpfc_debugfs_dumpHBASlim_data(phba, debug->buffer,
1652                 LPFC_DUMPHBASLIM_SIZE);
1653         file->private_data = debug;
1654
1655         rc = 0;
1656 out:
1657         return rc;
1658 }
1659
1660 /**
1661  * lpfc_debugfs_dumpHostSlim_open - Open the Dump Host SLIM debugfs buffer
1662  * @inode: The inode pointer that contains a vport pointer.
1663  * @file: The file pointer to attach the log output.
1664  *
1665  * Description:
1666  * This routine is the entry point for the debugfs open file operation. It gets
1667  * the vport from the i_private field in @inode, allocates the necessary buffer
1668  * for the log, fills the buffer from the in-memory log for this vport, and then
1669  * returns a pointer to that log in the private_data field in @file.
1670  *
1671  * Returns:
1672  * This function returns zero if successful. On error it will return a negative
1673  * error value.
1674  **/
1675 static int
1676 lpfc_debugfs_dumpHostSlim_open(struct inode *inode, struct file *file)
1677 {
1678         struct lpfc_hba *phba = inode->i_private;
1679         struct lpfc_debug *debug;
1680         int rc = -ENOMEM;
1681
1682         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1683         if (!debug)
1684                 goto out;
1685
1686         /* Round to page boundary */
1687         debug->buffer = kmalloc(LPFC_DUMPHOSTSLIM_SIZE, GFP_KERNEL);
1688         if (!debug->buffer) {
1689                 kfree(debug);
1690                 goto out;
1691         }
1692
1693         debug->len = lpfc_debugfs_dumpHostSlim_data(phba, debug->buffer,
1694                 LPFC_DUMPHOSTSLIM_SIZE);
1695         file->private_data = debug;
1696
1697         rc = 0;
1698 out:
1699         return rc;
1700 }
1701
1702 static int
1703 lpfc_debugfs_dumpData_open(struct inode *inode, struct file *file)
1704 {
1705         struct lpfc_debug *debug;
1706         int rc = -ENOMEM;
1707
1708         if (!_dump_buf_data)
1709                 return -EBUSY;
1710
1711         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1712         if (!debug)
1713                 goto out;
1714
1715         /* Round to page boundary */
1716         pr_err("9059 BLKGRD:  %s: _dump_buf_data=0x%p\n",
1717                         __func__, _dump_buf_data);
1718         debug->buffer = _dump_buf_data;
1719         if (!debug->buffer) {
1720                 kfree(debug);
1721                 goto out;
1722         }
1723
1724         debug->len = (1 << _dump_buf_data_order) << PAGE_SHIFT;
1725         file->private_data = debug;
1726
1727         rc = 0;
1728 out:
1729         return rc;
1730 }
1731
1732 static int
1733 lpfc_debugfs_dumpDif_open(struct inode *inode, struct file *file)
1734 {
1735         struct lpfc_debug *debug;
1736         int rc = -ENOMEM;
1737
1738         if (!_dump_buf_dif)
1739                 return -EBUSY;
1740
1741         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1742         if (!debug)
1743                 goto out;
1744
1745         /* Round to page boundary */
1746         pr_err("9060 BLKGRD: %s: _dump_buf_dif=0x%p file=%pD\n",
1747                         __func__, _dump_buf_dif, file);
1748         debug->buffer = _dump_buf_dif;
1749         if (!debug->buffer) {
1750                 kfree(debug);
1751                 goto out;
1752         }
1753
1754         debug->len = (1 << _dump_buf_dif_order) << PAGE_SHIFT;
1755         file->private_data = debug;
1756
1757         rc = 0;
1758 out:
1759         return rc;
1760 }
1761
1762 static ssize_t
1763 lpfc_debugfs_dumpDataDif_write(struct file *file, const char __user *buf,
1764                   size_t nbytes, loff_t *ppos)
1765 {
1766         /*
1767          * The Data/DIF buffers only save one failing IO
1768          * The write op is used as a reset mechanism after an IO has
1769          * already been saved to the next one can be saved
1770          */
1771         spin_lock(&_dump_buf_lock);
1772
1773         memset((void *)_dump_buf_data, 0,
1774                         ((1 << PAGE_SHIFT) << _dump_buf_data_order));
1775         memset((void *)_dump_buf_dif, 0,
1776                         ((1 << PAGE_SHIFT) << _dump_buf_dif_order));
1777
1778         _dump_buf_done = 0;
1779
1780         spin_unlock(&_dump_buf_lock);
1781
1782         return nbytes;
1783 }
1784
1785 static ssize_t
1786 lpfc_debugfs_dif_err_read(struct file *file, char __user *buf,
1787         size_t nbytes, loff_t *ppos)
1788 {
1789         struct dentry *dent = file->f_path.dentry;
1790         struct lpfc_hba *phba = file->private_data;
1791         char cbuf[32];
1792         uint64_t tmp = 0;
1793         int cnt = 0;
1794
1795         if (dent == phba->debug_writeGuard)
1796                 cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wgrd_cnt);
1797         else if (dent == phba->debug_writeApp)
1798                 cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wapp_cnt);
1799         else if (dent == phba->debug_writeRef)
1800                 cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_wref_cnt);
1801         else if (dent == phba->debug_readGuard)
1802                 cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rgrd_cnt);
1803         else if (dent == phba->debug_readApp)
1804                 cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rapp_cnt);
1805         else if (dent == phba->debug_readRef)
1806                 cnt = snprintf(cbuf, 32, "%u\n", phba->lpfc_injerr_rref_cnt);
1807         else if (dent == phba->debug_InjErrNPortID)
1808                 cnt = snprintf(cbuf, 32, "0x%06x\n", phba->lpfc_injerr_nportid);
1809         else if (dent == phba->debug_InjErrWWPN) {
1810                 memcpy(&tmp, &phba->lpfc_injerr_wwpn, sizeof(struct lpfc_name));
1811                 tmp = cpu_to_be64(tmp);
1812                 cnt = snprintf(cbuf, 32, "0x%016llx\n", tmp);
1813         } else if (dent == phba->debug_InjErrLBA) {
1814                 if (phba->lpfc_injerr_lba == (sector_t)(-1))
1815                         cnt = snprintf(cbuf, 32, "off\n");
1816                 else
1817                         cnt = snprintf(cbuf, 32, "0x%llx\n",
1818                                  (uint64_t) phba->lpfc_injerr_lba);
1819         } else
1820                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1821                          "0547 Unknown debugfs error injection entry\n");
1822
1823         return simple_read_from_buffer(buf, nbytes, ppos, &cbuf, cnt);
1824 }
1825
1826 static ssize_t
1827 lpfc_debugfs_dif_err_write(struct file *file, const char __user *buf,
1828         size_t nbytes, loff_t *ppos)
1829 {
1830         struct dentry *dent = file->f_path.dentry;
1831         struct lpfc_hba *phba = file->private_data;
1832         char dstbuf[33];
1833         uint64_t tmp = 0;
1834         int size;
1835
1836         memset(dstbuf, 0, 33);
1837         size = (nbytes < 32) ? nbytes : 32;
1838         if (copy_from_user(dstbuf, buf, size))
1839                 return 0;
1840
1841         if (dent == phba->debug_InjErrLBA) {
1842                 if ((buf[0] == 'o') && (buf[1] == 'f') && (buf[2] == 'f'))
1843                         tmp = (uint64_t)(-1);
1844         }
1845
1846         if ((tmp == 0) && (kstrtoull(dstbuf, 0, &tmp)))
1847                 return 0;
1848
1849         if (dent == phba->debug_writeGuard)
1850                 phba->lpfc_injerr_wgrd_cnt = (uint32_t)tmp;
1851         else if (dent == phba->debug_writeApp)
1852                 phba->lpfc_injerr_wapp_cnt = (uint32_t)tmp;
1853         else if (dent == phba->debug_writeRef)
1854                 phba->lpfc_injerr_wref_cnt = (uint32_t)tmp;
1855         else if (dent == phba->debug_readGuard)
1856                 phba->lpfc_injerr_rgrd_cnt = (uint32_t)tmp;
1857         else if (dent == phba->debug_readApp)
1858                 phba->lpfc_injerr_rapp_cnt = (uint32_t)tmp;
1859         else if (dent == phba->debug_readRef)
1860                 phba->lpfc_injerr_rref_cnt = (uint32_t)tmp;
1861         else if (dent == phba->debug_InjErrLBA)
1862                 phba->lpfc_injerr_lba = (sector_t)tmp;
1863         else if (dent == phba->debug_InjErrNPortID)
1864                 phba->lpfc_injerr_nportid = (uint32_t)(tmp & Mask_DID);
1865         else if (dent == phba->debug_InjErrWWPN) {
1866                 tmp = cpu_to_be64(tmp);
1867                 memcpy(&phba->lpfc_injerr_wwpn, &tmp, sizeof(struct lpfc_name));
1868         } else
1869                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1870                          "0548 Unknown debugfs error injection entry\n");
1871
1872         return nbytes;
1873 }
1874
1875 static int
1876 lpfc_debugfs_dif_err_release(struct inode *inode, struct file *file)
1877 {
1878         return 0;
1879 }
1880
1881 /**
1882  * lpfc_debugfs_nodelist_open - Open the nodelist debugfs file
1883  * @inode: The inode pointer that contains a vport pointer.
1884  * @file: The file pointer to attach the log output.
1885  *
1886  * Description:
1887  * This routine is the entry point for the debugfs open file operation. It gets
1888  * the vport from the i_private field in @inode, allocates the necessary buffer
1889  * for the log, fills the buffer from the in-memory log for this vport, and then
1890  * returns a pointer to that log in the private_data field in @file.
1891  *
1892  * Returns:
1893  * This function returns zero if successful. On error it will return a negative
1894  * error value.
1895  **/
1896 static int
1897 lpfc_debugfs_nodelist_open(struct inode *inode, struct file *file)
1898 {
1899         struct lpfc_vport *vport = inode->i_private;
1900         struct lpfc_debug *debug;
1901         int rc = -ENOMEM;
1902
1903         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
1904         if (!debug)
1905                 goto out;
1906
1907         /* Round to page boundary */
1908         debug->buffer = kmalloc(LPFC_NODELIST_SIZE, GFP_KERNEL);
1909         if (!debug->buffer) {
1910                 kfree(debug);
1911                 goto out;
1912         }
1913
1914         debug->len = lpfc_debugfs_nodelist_data(vport, debug->buffer,
1915                 LPFC_NODELIST_SIZE);
1916         file->private_data = debug;
1917
1918         rc = 0;
1919 out:
1920         return rc;
1921 }
1922
1923 /**
1924  * lpfc_debugfs_lseek - Seek through a debugfs file
1925  * @file: The file pointer to seek through.
1926  * @off: The offset to seek to or the amount to seek by.
1927  * @whence: Indicates how to seek.
1928  *
1929  * Description:
1930  * This routine is the entry point for the debugfs lseek file operation. The
1931  * @whence parameter indicates whether @off is the offset to directly seek to,
1932  * or if it is a value to seek forward or reverse by. This function figures out
1933  * what the new offset of the debugfs file will be and assigns that value to the
1934  * f_pos field of @file.
1935  *
1936  * Returns:
1937  * This function returns the new offset if successful and returns a negative
1938  * error if unable to process the seek.
1939  **/
1940 static loff_t
1941 lpfc_debugfs_lseek(struct file *file, loff_t off, int whence)
1942 {
1943         struct lpfc_debug *debug = file->private_data;
1944         return fixed_size_llseek(file, off, whence, debug->len);
1945 }
1946
1947 /**
1948  * lpfc_debugfs_read - Read a debugfs file
1949  * @file: The file pointer to read from.
1950  * @buf: The buffer to copy the data to.
1951  * @nbytes: The number of bytes to read.
1952  * @ppos: The position in the file to start reading from.
1953  *
1954  * Description:
1955  * This routine reads data from from the buffer indicated in the private_data
1956  * field of @file. It will start reading at @ppos and copy up to @nbytes of
1957  * data to @buf.
1958  *
1959  * Returns:
1960  * This function returns the amount of data that was read (this could be less
1961  * than @nbytes if the end of the file was reached) or a negative error value.
1962  **/
1963 static ssize_t
1964 lpfc_debugfs_read(struct file *file, char __user *buf,
1965                   size_t nbytes, loff_t *ppos)
1966 {
1967         struct lpfc_debug *debug = file->private_data;
1968
1969         return simple_read_from_buffer(buf, nbytes, ppos, debug->buffer,
1970                                        debug->len);
1971 }
1972
1973 /**
1974  * lpfc_debugfs_release - Release the buffer used to store debugfs file data
1975  * @inode: The inode pointer that contains a vport pointer. (unused)
1976  * @file: The file pointer that contains the buffer to release.
1977  *
1978  * Description:
1979  * This routine frees the buffer that was allocated when the debugfs file was
1980  * opened.
1981  *
1982  * Returns:
1983  * This function returns zero.
1984  **/
1985 static int
1986 lpfc_debugfs_release(struct inode *inode, struct file *file)
1987 {
1988         struct lpfc_debug *debug = file->private_data;
1989
1990         kfree(debug->buffer);
1991         kfree(debug);
1992
1993         return 0;
1994 }
1995
1996 static int
1997 lpfc_debugfs_dumpDataDif_release(struct inode *inode, struct file *file)
1998 {
1999         struct lpfc_debug *debug = file->private_data;
2000
2001         debug->buffer = NULL;
2002         kfree(debug);
2003
2004         return 0;
2005 }
2006
2007
2008 static int
2009 lpfc_debugfs_nvmestat_open(struct inode *inode, struct file *file)
2010 {
2011         struct lpfc_vport *vport = inode->i_private;
2012         struct lpfc_debug *debug;
2013         int rc = -ENOMEM;
2014
2015         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2016         if (!debug)
2017                 goto out;
2018
2019          /* Round to page boundary */
2020         debug->buffer = kmalloc(LPFC_NVMESTAT_SIZE, GFP_KERNEL);
2021         if (!debug->buffer) {
2022                 kfree(debug);
2023                 goto out;
2024         }
2025
2026         debug->len = lpfc_debugfs_nvmestat_data(vport, debug->buffer,
2027                 LPFC_NVMESTAT_SIZE);
2028
2029         debug->i_private = inode->i_private;
2030         file->private_data = debug;
2031
2032         rc = 0;
2033 out:
2034         return rc;
2035 }
2036
2037 static ssize_t
2038 lpfc_debugfs_nvmestat_write(struct file *file, const char __user *buf,
2039                             size_t nbytes, loff_t *ppos)
2040 {
2041         struct lpfc_debug *debug = file->private_data;
2042         struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2043         struct lpfc_hba   *phba = vport->phba;
2044         struct lpfc_nvmet_tgtport *tgtp;
2045         char mybuf[64];
2046         char *pbuf;
2047
2048         if (!phba->targetport)
2049                 return -ENXIO;
2050
2051         if (nbytes > 64)
2052                 nbytes = 64;
2053
2054         memset(mybuf, 0, sizeof(mybuf));
2055
2056         if (copy_from_user(mybuf, buf, nbytes))
2057                 return -EFAULT;
2058         pbuf = &mybuf[0];
2059
2060         tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2061         if ((strncmp(pbuf, "reset", strlen("reset")) == 0) ||
2062             (strncmp(pbuf, "zero", strlen("zero")) == 0)) {
2063                 atomic_set(&tgtp->rcv_ls_req_in, 0);
2064                 atomic_set(&tgtp->rcv_ls_req_out, 0);
2065                 atomic_set(&tgtp->rcv_ls_req_drop, 0);
2066                 atomic_set(&tgtp->xmt_ls_abort, 0);
2067                 atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
2068                 atomic_set(&tgtp->xmt_ls_rsp, 0);
2069                 atomic_set(&tgtp->xmt_ls_drop, 0);
2070                 atomic_set(&tgtp->xmt_ls_rsp_error, 0);
2071                 atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
2072
2073                 atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
2074                 atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
2075                 atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
2076                 atomic_set(&tgtp->xmt_fcp_drop, 0);
2077                 atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
2078                 atomic_set(&tgtp->xmt_fcp_read, 0);
2079                 atomic_set(&tgtp->xmt_fcp_write, 0);
2080                 atomic_set(&tgtp->xmt_fcp_rsp, 0);
2081                 atomic_set(&tgtp->xmt_fcp_release, 0);
2082                 atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
2083                 atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
2084                 atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
2085
2086                 atomic_set(&tgtp->xmt_fcp_abort, 0);
2087                 atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
2088                 atomic_set(&tgtp->xmt_abort_sol, 0);
2089                 atomic_set(&tgtp->xmt_abort_unsol, 0);
2090                 atomic_set(&tgtp->xmt_abort_rsp, 0);
2091                 atomic_set(&tgtp->xmt_abort_rsp_error, 0);
2092         }
2093         return nbytes;
2094 }
2095
2096 static int
2097 lpfc_debugfs_nvmektime_open(struct inode *inode, struct file *file)
2098 {
2099         struct lpfc_vport *vport = inode->i_private;
2100         struct lpfc_debug *debug;
2101         int rc = -ENOMEM;
2102
2103         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2104         if (!debug)
2105                 goto out;
2106
2107          /* Round to page boundary */
2108         debug->buffer = kmalloc(LPFC_NVMEKTIME_SIZE, GFP_KERNEL);
2109         if (!debug->buffer) {
2110                 kfree(debug);
2111                 goto out;
2112         }
2113
2114         debug->len = lpfc_debugfs_nvmektime_data(vport, debug->buffer,
2115                 LPFC_NVMEKTIME_SIZE);
2116
2117         debug->i_private = inode->i_private;
2118         file->private_data = debug;
2119
2120         rc = 0;
2121 out:
2122         return rc;
2123 }
2124
2125 static ssize_t
2126 lpfc_debugfs_nvmektime_write(struct file *file, const char __user *buf,
2127                              size_t nbytes, loff_t *ppos)
2128 {
2129         struct lpfc_debug *debug = file->private_data;
2130         struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2131         struct lpfc_hba   *phba = vport->phba;
2132         char mybuf[64];
2133         char *pbuf;
2134
2135         if (nbytes > 64)
2136                 nbytes = 64;
2137
2138         memset(mybuf, 0, sizeof(mybuf));
2139
2140         if (copy_from_user(mybuf, buf, nbytes))
2141                 return -EFAULT;
2142         pbuf = &mybuf[0];
2143
2144         if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2145                 phba->ktime_data_samples = 0;
2146                 phba->ktime_status_samples = 0;
2147                 phba->ktime_seg1_total = 0;
2148                 phba->ktime_seg1_max = 0;
2149                 phba->ktime_seg1_min = 0xffffffff;
2150                 phba->ktime_seg2_total = 0;
2151                 phba->ktime_seg2_max = 0;
2152                 phba->ktime_seg2_min = 0xffffffff;
2153                 phba->ktime_seg3_total = 0;
2154                 phba->ktime_seg3_max = 0;
2155                 phba->ktime_seg3_min = 0xffffffff;
2156                 phba->ktime_seg4_total = 0;
2157                 phba->ktime_seg4_max = 0;
2158                 phba->ktime_seg4_min = 0xffffffff;
2159                 phba->ktime_seg5_total = 0;
2160                 phba->ktime_seg5_max = 0;
2161                 phba->ktime_seg5_min = 0xffffffff;
2162                 phba->ktime_seg6_total = 0;
2163                 phba->ktime_seg6_max = 0;
2164                 phba->ktime_seg6_min = 0xffffffff;
2165                 phba->ktime_seg7_total = 0;
2166                 phba->ktime_seg7_max = 0;
2167                 phba->ktime_seg7_min = 0xffffffff;
2168                 phba->ktime_seg8_total = 0;
2169                 phba->ktime_seg8_max = 0;
2170                 phba->ktime_seg8_min = 0xffffffff;
2171                 phba->ktime_seg9_total = 0;
2172                 phba->ktime_seg9_max = 0;
2173                 phba->ktime_seg9_min = 0xffffffff;
2174                 phba->ktime_seg10_total = 0;
2175                 phba->ktime_seg10_max = 0;
2176                 phba->ktime_seg10_min = 0xffffffff;
2177
2178                 phba->ktime_on = 1;
2179                 return strlen(pbuf);
2180         } else if ((strncmp(pbuf, "off",
2181                    sizeof("off") - 1) == 0)) {
2182                 phba->ktime_on = 0;
2183                 return strlen(pbuf);
2184         } else if ((strncmp(pbuf, "zero",
2185                    sizeof("zero") - 1) == 0)) {
2186                 phba->ktime_data_samples = 0;
2187                 phba->ktime_status_samples = 0;
2188                 phba->ktime_seg1_total = 0;
2189                 phba->ktime_seg1_max = 0;
2190                 phba->ktime_seg1_min = 0xffffffff;
2191                 phba->ktime_seg2_total = 0;
2192                 phba->ktime_seg2_max = 0;
2193                 phba->ktime_seg2_min = 0xffffffff;
2194                 phba->ktime_seg3_total = 0;
2195                 phba->ktime_seg3_max = 0;
2196                 phba->ktime_seg3_min = 0xffffffff;
2197                 phba->ktime_seg4_total = 0;
2198                 phba->ktime_seg4_max = 0;
2199                 phba->ktime_seg4_min = 0xffffffff;
2200                 phba->ktime_seg5_total = 0;
2201                 phba->ktime_seg5_max = 0;
2202                 phba->ktime_seg5_min = 0xffffffff;
2203                 phba->ktime_seg6_total = 0;
2204                 phba->ktime_seg6_max = 0;
2205                 phba->ktime_seg6_min = 0xffffffff;
2206                 phba->ktime_seg7_total = 0;
2207                 phba->ktime_seg7_max = 0;
2208                 phba->ktime_seg7_min = 0xffffffff;
2209                 phba->ktime_seg8_total = 0;
2210                 phba->ktime_seg8_max = 0;
2211                 phba->ktime_seg8_min = 0xffffffff;
2212                 phba->ktime_seg9_total = 0;
2213                 phba->ktime_seg9_max = 0;
2214                 phba->ktime_seg9_min = 0xffffffff;
2215                 phba->ktime_seg10_total = 0;
2216                 phba->ktime_seg10_max = 0;
2217                 phba->ktime_seg10_min = 0xffffffff;
2218                 return strlen(pbuf);
2219         }
2220         return -EINVAL;
2221 }
2222
2223 static int
2224 lpfc_debugfs_nvmeio_trc_open(struct inode *inode, struct file *file)
2225 {
2226         struct lpfc_hba *phba = inode->i_private;
2227         struct lpfc_debug *debug;
2228         int rc = -ENOMEM;
2229
2230         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2231         if (!debug)
2232                 goto out;
2233
2234          /* Round to page boundary */
2235         debug->buffer = kmalloc(LPFC_NVMEIO_TRC_SIZE, GFP_KERNEL);
2236         if (!debug->buffer) {
2237                 kfree(debug);
2238                 goto out;
2239         }
2240
2241         debug->len = lpfc_debugfs_nvmeio_trc_data(phba, debug->buffer,
2242                 LPFC_NVMEIO_TRC_SIZE);
2243
2244         debug->i_private = inode->i_private;
2245         file->private_data = debug;
2246
2247         rc = 0;
2248 out:
2249         return rc;
2250 }
2251
2252 static ssize_t
2253 lpfc_debugfs_nvmeio_trc_write(struct file *file, const char __user *buf,
2254                               size_t nbytes, loff_t *ppos)
2255 {
2256         struct lpfc_debug *debug = file->private_data;
2257         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2258         int i;
2259         unsigned long sz;
2260         char mybuf[64];
2261         char *pbuf;
2262
2263         if (nbytes > 64)
2264                 nbytes = 64;
2265
2266         memset(mybuf, 0, sizeof(mybuf));
2267
2268         if (copy_from_user(mybuf, buf, nbytes))
2269                 return -EFAULT;
2270         pbuf = &mybuf[0];
2271
2272         if ((strncmp(pbuf, "off", sizeof("off") - 1) == 0)) {
2273                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2274                                 "0570 nvmeio_trc_off\n");
2275                 phba->nvmeio_trc_output_idx = 0;
2276                 phba->nvmeio_trc_on = 0;
2277                 return strlen(pbuf);
2278         } else if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2279                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2280                                 "0571 nvmeio_trc_on\n");
2281                 phba->nvmeio_trc_output_idx = 0;
2282                 phba->nvmeio_trc_on = 1;
2283                 return strlen(pbuf);
2284         }
2285
2286         /* We must be off to allocate the trace buffer */
2287         if (phba->nvmeio_trc_on != 0)
2288                 return -EINVAL;
2289
2290         /* If not on or off, the parameter is the trace buffer size */
2291         i = kstrtoul(pbuf, 0, &sz);
2292         if (i)
2293                 return -EINVAL;
2294         phba->nvmeio_trc_size = (uint32_t)sz;
2295
2296         /* It must be a power of 2 - round down */
2297         i = 0;
2298         while (sz > 1) {
2299                 sz = sz >> 1;
2300                 i++;
2301         }
2302         sz = (1 << i);
2303         if (phba->nvmeio_trc_size != sz)
2304                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2305                                 "0572 nvmeio_trc_size changed to %ld\n",
2306                                 sz);
2307         phba->nvmeio_trc_size = (uint32_t)sz;
2308
2309         /* If one previously exists, free it */
2310         kfree(phba->nvmeio_trc);
2311
2312         /* Allocate new trace buffer and initialize */
2313         phba->nvmeio_trc = kzalloc((sizeof(struct lpfc_debugfs_nvmeio_trc) *
2314                                     sz), GFP_KERNEL);
2315         if (!phba->nvmeio_trc) {
2316                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2317                                 "0573 Cannot create debugfs "
2318                                 "nvmeio_trc buffer\n");
2319                 return -ENOMEM;
2320         }
2321         atomic_set(&phba->nvmeio_trc_cnt, 0);
2322         phba->nvmeio_trc_on = 0;
2323         phba->nvmeio_trc_output_idx = 0;
2324
2325         return strlen(pbuf);
2326 }
2327
2328 static int
2329 lpfc_debugfs_cpucheck_open(struct inode *inode, struct file *file)
2330 {
2331         struct lpfc_vport *vport = inode->i_private;
2332         struct lpfc_debug *debug;
2333         int rc = -ENOMEM;
2334
2335         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2336         if (!debug)
2337                 goto out;
2338
2339          /* Round to page boundary */
2340         debug->buffer = kmalloc(LPFC_CPUCHECK_SIZE, GFP_KERNEL);
2341         if (!debug->buffer) {
2342                 kfree(debug);
2343                 goto out;
2344         }
2345
2346         debug->len = lpfc_debugfs_cpucheck_data(vport, debug->buffer,
2347                 LPFC_NVMEKTIME_SIZE);
2348
2349         debug->i_private = inode->i_private;
2350         file->private_data = debug;
2351
2352         rc = 0;
2353 out:
2354         return rc;
2355 }
2356
2357 static ssize_t
2358 lpfc_debugfs_cpucheck_write(struct file *file, const char __user *buf,
2359                             size_t nbytes, loff_t *ppos)
2360 {
2361         struct lpfc_debug *debug = file->private_data;
2362         struct lpfc_vport *vport = (struct lpfc_vport *)debug->i_private;
2363         struct lpfc_hba   *phba = vport->phba;
2364         char mybuf[64];
2365         char *pbuf;
2366         int i;
2367
2368         if (nbytes > 64)
2369                 nbytes = 64;
2370
2371         memset(mybuf, 0, sizeof(mybuf));
2372
2373         if (copy_from_user(mybuf, buf, nbytes))
2374                 return -EFAULT;
2375         pbuf = &mybuf[0];
2376
2377         if ((strncmp(pbuf, "on", sizeof("on") - 1) == 0)) {
2378                 if (phba->nvmet_support)
2379                         phba->cpucheck_on |= LPFC_CHECK_NVMET_IO;
2380                 else
2381                         phba->cpucheck_on |= LPFC_CHECK_NVME_IO;
2382                 return strlen(pbuf);
2383         } else if ((strncmp(pbuf, "rcv",
2384                    sizeof("rcv") - 1) == 0)) {
2385                 if (phba->nvmet_support)
2386                         phba->cpucheck_on |= LPFC_CHECK_NVMET_RCV;
2387                 else
2388                         return -EINVAL;
2389                 return strlen(pbuf);
2390         } else if ((strncmp(pbuf, "off",
2391                    sizeof("off") - 1) == 0)) {
2392                 phba->cpucheck_on = LPFC_CHECK_OFF;
2393                 return strlen(pbuf);
2394         } else if ((strncmp(pbuf, "zero",
2395                    sizeof("zero") - 1) == 0)) {
2396                 for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
2397                         if (i >= LPFC_CHECK_CPU_CNT)
2398                                 break;
2399                         phba->cpucheck_rcv_io[i] = 0;
2400                         phba->cpucheck_xmt_io[i] = 0;
2401                         phba->cpucheck_cmpl_io[i] = 0;
2402                         phba->cpucheck_ccmpl_io[i] = 0;
2403                 }
2404                 return strlen(pbuf);
2405         }
2406         return -EINVAL;
2407 }
2408
2409 /*
2410  * ---------------------------------
2411  * iDiag debugfs file access methods
2412  * ---------------------------------
2413  *
2414  * All access methods are through the proper SLI4 PCI function's debugfs
2415  * iDiag directory:
2416  *
2417  *     /sys/kernel/debug/lpfc/fn<#>/iDiag
2418  */
2419
2420 /**
2421  * lpfc_idiag_cmd_get - Get and parse idiag debugfs comands from user space
2422  * @buf: The pointer to the user space buffer.
2423  * @nbytes: The number of bytes in the user space buffer.
2424  * @idiag_cmd: pointer to the idiag command struct.
2425  *
2426  * This routine reads data from debugfs user space buffer and parses the
2427  * buffer for getting the idiag command and arguments. The while space in
2428  * between the set of data is used as the parsing separator.
2429  *
2430  * This routine returns 0 when successful, it returns proper error code
2431  * back to the user space in error conditions.
2432  */
2433 static int lpfc_idiag_cmd_get(const char __user *buf, size_t nbytes,
2434                               struct lpfc_idiag_cmd *idiag_cmd)
2435 {
2436         char mybuf[64];
2437         char *pbuf, *step_str;
2438         int i;
2439         size_t bsize;
2440
2441         memset(mybuf, 0, sizeof(mybuf));
2442         memset(idiag_cmd, 0, sizeof(*idiag_cmd));
2443         bsize = min(nbytes, (sizeof(mybuf)-1));
2444
2445         if (copy_from_user(mybuf, buf, bsize))
2446                 return -EFAULT;
2447         pbuf = &mybuf[0];
2448         step_str = strsep(&pbuf, "\t ");
2449
2450         /* The opcode must present */
2451         if (!step_str)
2452                 return -EINVAL;
2453
2454         idiag_cmd->opcode = simple_strtol(step_str, NULL, 0);
2455         if (idiag_cmd->opcode == 0)
2456                 return -EINVAL;
2457
2458         for (i = 0; i < LPFC_IDIAG_CMD_DATA_SIZE; i++) {
2459                 step_str = strsep(&pbuf, "\t ");
2460                 if (!step_str)
2461                         return i;
2462                 idiag_cmd->data[i] = simple_strtol(step_str, NULL, 0);
2463         }
2464         return i;
2465 }
2466
2467 /**
2468  * lpfc_idiag_open - idiag open debugfs
2469  * @inode: The inode pointer that contains a pointer to phba.
2470  * @file: The file pointer to attach the file operation.
2471  *
2472  * Description:
2473  * This routine is the entry point for the debugfs open file operation. It
2474  * gets the reference to phba from the i_private field in @inode, it then
2475  * allocates buffer for the file operation, performs the necessary PCI config
2476  * space read into the allocated buffer according to the idiag user command
2477  * setup, and then returns a pointer to buffer in the private_data field in
2478  * @file.
2479  *
2480  * Returns:
2481  * This function returns zero if successful. On error it will return an
2482  * negative error value.
2483  **/
2484 static int
2485 lpfc_idiag_open(struct inode *inode, struct file *file)
2486 {
2487         struct lpfc_debug *debug;
2488
2489         debug = kmalloc(sizeof(*debug), GFP_KERNEL);
2490         if (!debug)
2491                 return -ENOMEM;
2492
2493         debug->i_private = inode->i_private;
2494         debug->buffer = NULL;
2495         file->private_data = debug;
2496
2497         return 0;
2498 }
2499
2500 /**
2501  * lpfc_idiag_release - Release idiag access file operation
2502  * @inode: The inode pointer that contains a vport pointer. (unused)
2503  * @file: The file pointer that contains the buffer to release.
2504  *
2505  * Description:
2506  * This routine is the generic release routine for the idiag access file
2507  * operation, it frees the buffer that was allocated when the debugfs file
2508  * was opened.
2509  *
2510  * Returns:
2511  * This function returns zero.
2512  **/
2513 static int
2514 lpfc_idiag_release(struct inode *inode, struct file *file)
2515 {
2516         struct lpfc_debug *debug = file->private_data;
2517
2518         /* Free the buffers to the file operation */
2519         kfree(debug->buffer);
2520         kfree(debug);
2521
2522         return 0;
2523 }
2524
2525 /**
2526  * lpfc_idiag_cmd_release - Release idiag cmd access file operation
2527  * @inode: The inode pointer that contains a vport pointer. (unused)
2528  * @file: The file pointer that contains the buffer to release.
2529  *
2530  * Description:
2531  * This routine frees the buffer that was allocated when the debugfs file
2532  * was opened. It also reset the fields in the idiag command struct in the
2533  * case of command for write operation.
2534  *
2535  * Returns:
2536  * This function returns zero.
2537  **/
2538 static int
2539 lpfc_idiag_cmd_release(struct inode *inode, struct file *file)
2540 {
2541         struct lpfc_debug *debug = file->private_data;
2542
2543         if (debug->op == LPFC_IDIAG_OP_WR) {
2544                 switch (idiag.cmd.opcode) {
2545                 case LPFC_IDIAG_CMD_PCICFG_WR:
2546                 case LPFC_IDIAG_CMD_PCICFG_ST:
2547                 case LPFC_IDIAG_CMD_PCICFG_CL:
2548                 case LPFC_IDIAG_CMD_QUEACC_WR:
2549                 case LPFC_IDIAG_CMD_QUEACC_ST:
2550                 case LPFC_IDIAG_CMD_QUEACC_CL:
2551                         memset(&idiag, 0, sizeof(idiag));
2552                         break;
2553                 default:
2554                         break;
2555                 }
2556         }
2557
2558         /* Free the buffers to the file operation */
2559         kfree(debug->buffer);
2560         kfree(debug);
2561
2562         return 0;
2563 }
2564
2565 /**
2566  * lpfc_idiag_pcicfg_read - idiag debugfs read pcicfg
2567  * @file: The file pointer to read from.
2568  * @buf: The buffer to copy the data to.
2569  * @nbytes: The number of bytes to read.
2570  * @ppos: The position in the file to start reading from.
2571  *
2572  * Description:
2573  * This routine reads data from the @phba pci config space according to the
2574  * idiag command, and copies to user @buf. Depending on the PCI config space
2575  * read command setup, it does either a single register read of a byte
2576  * (8 bits), a word (16 bits), or a dword (32 bits) or browsing through all
2577  * registers from the 4K extended PCI config space.
2578  *
2579  * Returns:
2580  * This function returns the amount of data that was read (this could be less
2581  * than @nbytes if the end of the file was reached) or a negative error value.
2582  **/
2583 static ssize_t
2584 lpfc_idiag_pcicfg_read(struct file *file, char __user *buf, size_t nbytes,
2585                        loff_t *ppos)
2586 {
2587         struct lpfc_debug *debug = file->private_data;
2588         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2589         int offset_label, offset, len = 0, index = LPFC_PCI_CFG_RD_SIZE;
2590         int where, count;
2591         char *pbuffer;
2592         struct pci_dev *pdev;
2593         uint32_t u32val;
2594         uint16_t u16val;
2595         uint8_t u8val;
2596
2597         pdev = phba->pcidev;
2598         if (!pdev)
2599                 return 0;
2600
2601         /* This is a user read operation */
2602         debug->op = LPFC_IDIAG_OP_RD;
2603
2604         if (!debug->buffer)
2605                 debug->buffer = kmalloc(LPFC_PCI_CFG_SIZE, GFP_KERNEL);
2606         if (!debug->buffer)
2607                 return 0;
2608         pbuffer = debug->buffer;
2609
2610         if (*ppos)
2611                 return 0;
2612
2613         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
2614                 where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
2615                 count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
2616         } else
2617                 return 0;
2618
2619         /* Read single PCI config space register */
2620         switch (count) {
2621         case SIZE_U8: /* byte (8 bits) */
2622                 pci_read_config_byte(pdev, where, &u8val);
2623                 len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2624                                 "%03x: %02x\n", where, u8val);
2625                 break;
2626         case SIZE_U16: /* word (16 bits) */
2627                 pci_read_config_word(pdev, where, &u16val);
2628                 len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2629                                 "%03x: %04x\n", where, u16val);
2630                 break;
2631         case SIZE_U32: /* double word (32 bits) */
2632                 pci_read_config_dword(pdev, where, &u32val);
2633                 len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2634                                 "%03x: %08x\n", where, u32val);
2635                 break;
2636         case LPFC_PCI_CFG_BROWSE: /* browse all */
2637                 goto pcicfg_browse;
2638                 break;
2639         default:
2640                 /* illegal count */
2641                 len = 0;
2642                 break;
2643         }
2644         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2645
2646 pcicfg_browse:
2647
2648         /* Browse all PCI config space registers */
2649         offset_label = idiag.offset.last_rd;
2650         offset = offset_label;
2651
2652         /* Read PCI config space */
2653         len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2654                         "%03x: ", offset_label);
2655         while (index > 0) {
2656                 pci_read_config_dword(pdev, offset, &u32val);
2657                 len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2658                                 "%08x ", u32val);
2659                 offset += sizeof(uint32_t);
2660                 if (offset >= LPFC_PCI_CFG_SIZE) {
2661                         len += snprintf(pbuffer+len,
2662                                         LPFC_PCI_CFG_SIZE-len, "\n");
2663                         break;
2664                 }
2665                 index -= sizeof(uint32_t);
2666                 if (!index)
2667                         len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2668                                         "\n");
2669                 else if (!(index % (8 * sizeof(uint32_t)))) {
2670                         offset_label += (8 * sizeof(uint32_t));
2671                         len += snprintf(pbuffer+len, LPFC_PCI_CFG_SIZE-len,
2672                                         "\n%03x: ", offset_label);
2673                 }
2674         }
2675
2676         /* Set up the offset for next portion of pci cfg read */
2677         if (index == 0) {
2678                 idiag.offset.last_rd += LPFC_PCI_CFG_RD_SIZE;
2679                 if (idiag.offset.last_rd >= LPFC_PCI_CFG_SIZE)
2680                         idiag.offset.last_rd = 0;
2681         } else
2682                 idiag.offset.last_rd = 0;
2683
2684         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2685 }
2686
2687 /**
2688  * lpfc_idiag_pcicfg_write - Syntax check and set up idiag pcicfg commands
2689  * @file: The file pointer to read from.
2690  * @buf: The buffer to copy the user data from.
2691  * @nbytes: The number of bytes to get.
2692  * @ppos: The position in the file to start reading from.
2693  *
2694  * This routine get the debugfs idiag command struct from user space and
2695  * then perform the syntax check for PCI config space read or write command
2696  * accordingly. In the case of PCI config space read command, it sets up
2697  * the command in the idiag command struct for the debugfs read operation.
2698  * In the case of PCI config space write operation, it executes the write
2699  * operation into the PCI config space accordingly.
2700  *
2701  * It returns the @nbytges passing in from debugfs user space when successful.
2702  * In case of error conditions, it returns proper error code back to the user
2703  * space.
2704  */
2705 static ssize_t
2706 lpfc_idiag_pcicfg_write(struct file *file, const char __user *buf,
2707                         size_t nbytes, loff_t *ppos)
2708 {
2709         struct lpfc_debug *debug = file->private_data;
2710         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2711         uint32_t where, value, count;
2712         uint32_t u32val;
2713         uint16_t u16val;
2714         uint8_t u8val;
2715         struct pci_dev *pdev;
2716         int rc;
2717
2718         pdev = phba->pcidev;
2719         if (!pdev)
2720                 return -EFAULT;
2721
2722         /* This is a user write operation */
2723         debug->op = LPFC_IDIAG_OP_WR;
2724
2725         rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
2726         if (rc < 0)
2727                 return rc;
2728
2729         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_RD) {
2730                 /* Sanity check on PCI config read command line arguments */
2731                 if (rc != LPFC_PCI_CFG_RD_CMD_ARG)
2732                         goto error_out;
2733                 /* Read command from PCI config space, set up command fields */
2734                 where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
2735                 count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
2736                 if (count == LPFC_PCI_CFG_BROWSE) {
2737                         if (where % sizeof(uint32_t))
2738                                 goto error_out;
2739                         /* Starting offset to browse */
2740                         idiag.offset.last_rd = where;
2741                 } else if ((count != sizeof(uint8_t)) &&
2742                            (count != sizeof(uint16_t)) &&
2743                            (count != sizeof(uint32_t)))
2744                         goto error_out;
2745                 if (count == sizeof(uint8_t)) {
2746                         if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
2747                                 goto error_out;
2748                         if (where % sizeof(uint8_t))
2749                                 goto error_out;
2750                 }
2751                 if (count == sizeof(uint16_t)) {
2752                         if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
2753                                 goto error_out;
2754                         if (where % sizeof(uint16_t))
2755                                 goto error_out;
2756                 }
2757                 if (count == sizeof(uint32_t)) {
2758                         if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
2759                                 goto error_out;
2760                         if (where % sizeof(uint32_t))
2761                                 goto error_out;
2762                 }
2763         } else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR ||
2764                    idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST ||
2765                    idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
2766                 /* Sanity check on PCI config write command line arguments */
2767                 if (rc != LPFC_PCI_CFG_WR_CMD_ARG)
2768                         goto error_out;
2769                 /* Write command to PCI config space, read-modify-write */
2770                 where = idiag.cmd.data[IDIAG_PCICFG_WHERE_INDX];
2771                 count = idiag.cmd.data[IDIAG_PCICFG_COUNT_INDX];
2772                 value = idiag.cmd.data[IDIAG_PCICFG_VALUE_INDX];
2773                 /* Sanity checks */
2774                 if ((count != sizeof(uint8_t)) &&
2775                     (count != sizeof(uint16_t)) &&
2776                     (count != sizeof(uint32_t)))
2777                         goto error_out;
2778                 if (count == sizeof(uint8_t)) {
2779                         if (where > LPFC_PCI_CFG_SIZE - sizeof(uint8_t))
2780                                 goto error_out;
2781                         if (where % sizeof(uint8_t))
2782                                 goto error_out;
2783                         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
2784                                 pci_write_config_byte(pdev, where,
2785                                                       (uint8_t)value);
2786                         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
2787                                 rc = pci_read_config_byte(pdev, where, &u8val);
2788                                 if (!rc) {
2789                                         u8val |= (uint8_t)value;
2790                                         pci_write_config_byte(pdev, where,
2791                                                               u8val);
2792                                 }
2793                         }
2794                         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
2795                                 rc = pci_read_config_byte(pdev, where, &u8val);
2796                                 if (!rc) {
2797                                         u8val &= (uint8_t)(~value);
2798                                         pci_write_config_byte(pdev, where,
2799                                                               u8val);
2800                                 }
2801                         }
2802                 }
2803                 if (count == sizeof(uint16_t)) {
2804                         if (where > LPFC_PCI_CFG_SIZE - sizeof(uint16_t))
2805                                 goto error_out;
2806                         if (where % sizeof(uint16_t))
2807                                 goto error_out;
2808                         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
2809                                 pci_write_config_word(pdev, where,
2810                                                       (uint16_t)value);
2811                         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
2812                                 rc = pci_read_config_word(pdev, where, &u16val);
2813                                 if (!rc) {
2814                                         u16val |= (uint16_t)value;
2815                                         pci_write_config_word(pdev, where,
2816                                                               u16val);
2817                                 }
2818                         }
2819                         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
2820                                 rc = pci_read_config_word(pdev, where, &u16val);
2821                                 if (!rc) {
2822                                         u16val &= (uint16_t)(~value);
2823                                         pci_write_config_word(pdev, where,
2824                                                               u16val);
2825                                 }
2826                         }
2827                 }
2828                 if (count == sizeof(uint32_t)) {
2829                         if (where > LPFC_PCI_CFG_SIZE - sizeof(uint32_t))
2830                                 goto error_out;
2831                         if (where % sizeof(uint32_t))
2832                                 goto error_out;
2833                         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_WR)
2834                                 pci_write_config_dword(pdev, where, value);
2835                         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_ST) {
2836                                 rc = pci_read_config_dword(pdev, where,
2837                                                            &u32val);
2838                                 if (!rc) {
2839                                         u32val |= value;
2840                                         pci_write_config_dword(pdev, where,
2841                                                                u32val);
2842                                 }
2843                         }
2844                         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_PCICFG_CL) {
2845                                 rc = pci_read_config_dword(pdev, where,
2846                                                            &u32val);
2847                                 if (!rc) {
2848                                         u32val &= ~value;
2849                                         pci_write_config_dword(pdev, where,
2850                                                                u32val);
2851                                 }
2852                         }
2853                 }
2854         } else
2855                 /* All other opecodes are illegal for now */
2856                 goto error_out;
2857
2858         return nbytes;
2859 error_out:
2860         memset(&idiag, 0, sizeof(idiag));
2861         return -EINVAL;
2862 }
2863
2864 /**
2865  * lpfc_idiag_baracc_read - idiag debugfs pci bar access read
2866  * @file: The file pointer to read from.
2867  * @buf: The buffer to copy the data to.
2868  * @nbytes: The number of bytes to read.
2869  * @ppos: The position in the file to start reading from.
2870  *
2871  * Description:
2872  * This routine reads data from the @phba pci bar memory mapped space
2873  * according to the idiag command, and copies to user @buf.
2874  *
2875  * Returns:
2876  * This function returns the amount of data that was read (this could be less
2877  * than @nbytes if the end of the file was reached) or a negative error value.
2878  **/
2879 static ssize_t
2880 lpfc_idiag_baracc_read(struct file *file, char __user *buf, size_t nbytes,
2881                        loff_t *ppos)
2882 {
2883         struct lpfc_debug *debug = file->private_data;
2884         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
2885         int offset_label, offset, offset_run, len = 0, index;
2886         int bar_num, acc_range, bar_size;
2887         char *pbuffer;
2888         void __iomem *mem_mapped_bar;
2889         uint32_t if_type;
2890         struct pci_dev *pdev;
2891         uint32_t u32val;
2892
2893         pdev = phba->pcidev;
2894         if (!pdev)
2895                 return 0;
2896
2897         /* This is a user read operation */
2898         debug->op = LPFC_IDIAG_OP_RD;
2899
2900         if (!debug->buffer)
2901                 debug->buffer = kmalloc(LPFC_PCI_BAR_RD_BUF_SIZE, GFP_KERNEL);
2902         if (!debug->buffer)
2903                 return 0;
2904         pbuffer = debug->buffer;
2905
2906         if (*ppos)
2907                 return 0;
2908
2909         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
2910                 bar_num   = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
2911                 offset    = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
2912                 acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
2913                 bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
2914         } else
2915                 return 0;
2916
2917         if (acc_range == 0)
2918                 return 0;
2919
2920         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
2921         if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
2922                 if (bar_num == IDIAG_BARACC_BAR_0)
2923                         mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
2924                 else if (bar_num == IDIAG_BARACC_BAR_1)
2925                         mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
2926                 else if (bar_num == IDIAG_BARACC_BAR_2)
2927                         mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
2928                 else
2929                         return 0;
2930         } else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
2931                 if (bar_num == IDIAG_BARACC_BAR_0)
2932                         mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
2933                 else
2934                         return 0;
2935         } else
2936                 return 0;
2937
2938         /* Read single PCI bar space register */
2939         if (acc_range == SINGLE_WORD) {
2940                 offset_run = offset;
2941                 u32val = readl(mem_mapped_bar + offset_run);
2942                 len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
2943                                 "%05x: %08x\n", offset_run, u32val);
2944         } else
2945                 goto baracc_browse;
2946
2947         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
2948
2949 baracc_browse:
2950
2951         /* Browse all PCI bar space registers */
2952         offset_label = idiag.offset.last_rd;
2953         offset_run = offset_label;
2954
2955         /* Read PCI bar memory mapped space */
2956         len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
2957                         "%05x: ", offset_label);
2958         index = LPFC_PCI_BAR_RD_SIZE;
2959         while (index > 0) {
2960                 u32val = readl(mem_mapped_bar + offset_run);
2961                 len += snprintf(pbuffer+len, LPFC_PCI_BAR_RD_BUF_SIZE-len,
2962                                 "%08x ", u32val);
2963                 offset_run += sizeof(uint32_t);
2964                 if (acc_range == LPFC_PCI_BAR_BROWSE) {
2965                         if (offset_run >= bar_size) {
2966                                 len += snprintf(pbuffer+len,
2967                                         LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
2968                                 break;
2969                         }
2970                 } else {
2971                         if (offset_run >= offset +
2972                             (acc_range * sizeof(uint32_t))) {
2973                                 len += snprintf(pbuffer+len,
2974                                         LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
2975                                 break;
2976                         }
2977                 }
2978                 index -= sizeof(uint32_t);
2979                 if (!index)
2980                         len += snprintf(pbuffer+len,
2981                                         LPFC_PCI_BAR_RD_BUF_SIZE-len, "\n");
2982                 else if (!(index % (8 * sizeof(uint32_t)))) {
2983                         offset_label += (8 * sizeof(uint32_t));
2984                         len += snprintf(pbuffer+len,
2985                                         LPFC_PCI_BAR_RD_BUF_SIZE-len,
2986                                         "\n%05x: ", offset_label);
2987                 }
2988         }
2989
2990         /* Set up the offset for next portion of pci bar read */
2991         if (index == 0) {
2992                 idiag.offset.last_rd += LPFC_PCI_BAR_RD_SIZE;
2993                 if (acc_range == LPFC_PCI_BAR_BROWSE) {
2994                         if (idiag.offset.last_rd >= bar_size)
2995                                 idiag.offset.last_rd = 0;
2996                 } else {
2997                         if (offset_run >= offset +
2998                             (acc_range * sizeof(uint32_t)))
2999                                 idiag.offset.last_rd = offset;
3000                 }
3001         } else {
3002                 if (acc_range == LPFC_PCI_BAR_BROWSE)
3003                         idiag.offset.last_rd = 0;
3004                 else
3005                         idiag.offset.last_rd = offset;
3006         }
3007
3008         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3009 }
3010
3011 /**
3012  * lpfc_idiag_baracc_write - Syntax check and set up idiag bar access commands
3013  * @file: The file pointer to read from.
3014  * @buf: The buffer to copy the user data from.
3015  * @nbytes: The number of bytes to get.
3016  * @ppos: The position in the file to start reading from.
3017  *
3018  * This routine get the debugfs idiag command struct from user space and
3019  * then perform the syntax check for PCI bar memory mapped space read or
3020  * write command accordingly. In the case of PCI bar memory mapped space
3021  * read command, it sets up the command in the idiag command struct for
3022  * the debugfs read operation. In the case of PCI bar memorpy mapped space
3023  * write operation, it executes the write operation into the PCI bar memory
3024  * mapped space accordingly.
3025  *
3026  * It returns the @nbytges passing in from debugfs user space when successful.
3027  * In case of error conditions, it returns proper error code back to the user
3028  * space.
3029  */
3030 static ssize_t
3031 lpfc_idiag_baracc_write(struct file *file, const char __user *buf,
3032                         size_t nbytes, loff_t *ppos)
3033 {
3034         struct lpfc_debug *debug = file->private_data;
3035         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3036         uint32_t bar_num, bar_size, offset, value, acc_range;
3037         struct pci_dev *pdev;
3038         void __iomem *mem_mapped_bar;
3039         uint32_t if_type;
3040         uint32_t u32val;
3041         int rc;
3042
3043         pdev = phba->pcidev;
3044         if (!pdev)
3045                 return -EFAULT;
3046
3047         /* This is a user write operation */
3048         debug->op = LPFC_IDIAG_OP_WR;
3049
3050         rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3051         if (rc < 0)
3052                 return rc;
3053
3054         if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
3055         bar_num = idiag.cmd.data[IDIAG_BARACC_BAR_NUM_INDX];
3056
3057         if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3058                 if ((bar_num != IDIAG_BARACC_BAR_0) &&
3059                     (bar_num != IDIAG_BARACC_BAR_1) &&
3060                     (bar_num != IDIAG_BARACC_BAR_2))
3061                         goto error_out;
3062         } else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3063                 if (bar_num != IDIAG_BARACC_BAR_0)
3064                         goto error_out;
3065         } else
3066                 goto error_out;
3067
3068         if (if_type == LPFC_SLI_INTF_IF_TYPE_0) {
3069                 if (bar_num == IDIAG_BARACC_BAR_0) {
3070                         idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3071                                 LPFC_PCI_IF0_BAR0_SIZE;
3072                         mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3073                 } else if (bar_num == IDIAG_BARACC_BAR_1) {
3074                         idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3075                                 LPFC_PCI_IF0_BAR1_SIZE;
3076                         mem_mapped_bar = phba->sli4_hba.ctrl_regs_memmap_p;
3077                 } else if (bar_num == IDIAG_BARACC_BAR_2) {
3078                         idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3079                                 LPFC_PCI_IF0_BAR2_SIZE;
3080                         mem_mapped_bar = phba->sli4_hba.drbl_regs_memmap_p;
3081                 } else
3082                         goto error_out;
3083         } else if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
3084                 if (bar_num == IDIAG_BARACC_BAR_0) {
3085                         idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX] =
3086                                 LPFC_PCI_IF2_BAR0_SIZE;
3087                         mem_mapped_bar = phba->sli4_hba.conf_regs_memmap_p;
3088                 } else
3089                         goto error_out;
3090         } else
3091                 goto error_out;
3092
3093         offset = idiag.cmd.data[IDIAG_BARACC_OFF_SET_INDX];
3094         if (offset % sizeof(uint32_t))
3095                 goto error_out;
3096
3097         bar_size = idiag.cmd.data[IDIAG_BARACC_BAR_SZE_INDX];
3098         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_RD) {
3099                 /* Sanity check on PCI config read command line arguments */
3100                 if (rc != LPFC_PCI_BAR_RD_CMD_ARG)
3101                         goto error_out;
3102                 acc_range = idiag.cmd.data[IDIAG_BARACC_ACC_MOD_INDX];
3103                 if (acc_range == LPFC_PCI_BAR_BROWSE) {
3104                         if (offset > bar_size - sizeof(uint32_t))
3105                                 goto error_out;
3106                         /* Starting offset to browse */
3107                         idiag.offset.last_rd = offset;
3108                 } else if (acc_range > SINGLE_WORD) {
3109                         if (offset + acc_range * sizeof(uint32_t) > bar_size)
3110                                 goto error_out;
3111                         /* Starting offset to browse */
3112                         idiag.offset.last_rd = offset;
3113                 } else if (acc_range != SINGLE_WORD)
3114                         goto error_out;
3115         } else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR ||
3116                    idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST ||
3117                    idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3118                 /* Sanity check on PCI bar write command line arguments */
3119                 if (rc != LPFC_PCI_BAR_WR_CMD_ARG)
3120                         goto error_out;
3121                 /* Write command to PCI bar space, read-modify-write */
3122                 acc_range = SINGLE_WORD;
3123                 value = idiag.cmd.data[IDIAG_BARACC_REG_VAL_INDX];
3124                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_WR) {
3125                         writel(value, mem_mapped_bar + offset);
3126                         readl(mem_mapped_bar + offset);
3127                 }
3128                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_ST) {
3129                         u32val = readl(mem_mapped_bar + offset);
3130                         u32val |= value;
3131                         writel(u32val, mem_mapped_bar + offset);
3132                         readl(mem_mapped_bar + offset);
3133                 }
3134                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_BARACC_CL) {
3135                         u32val = readl(mem_mapped_bar + offset);
3136                         u32val &= ~value;
3137                         writel(u32val, mem_mapped_bar + offset);
3138                         readl(mem_mapped_bar + offset);
3139                 }
3140         } else
3141                 /* All other opecodes are illegal for now */
3142                 goto error_out;
3143
3144         return nbytes;
3145 error_out:
3146         memset(&idiag, 0, sizeof(idiag));
3147         return -EINVAL;
3148 }
3149
3150 static int
3151 __lpfc_idiag_print_wq(struct lpfc_queue *qp, char *wqtype,
3152                         char *pbuffer, int len)
3153 {
3154         if (!qp)
3155                 return len;
3156
3157         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3158                         "\t\t%s WQ info: ", wqtype);
3159         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3160                         "AssocCQID[%04d]: WQ-STAT[oflow:x%x posted:x%llx]\n",
3161                         qp->assoc_qid, qp->q_cnt_1,
3162                         (unsigned long long)qp->q_cnt_4);
3163         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3164                         "\t\tWQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3165                         "HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]",
3166                         qp->queue_id, qp->entry_count,
3167                         qp->entry_size, qp->host_index,
3168                         qp->hba_index, qp->entry_repost);
3169         len +=  snprintf(pbuffer + len,
3170                         LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3171         return len;
3172 }
3173
3174 static int
3175 lpfc_idiag_wqs_for_cq(struct lpfc_hba *phba, char *wqtype, char *pbuffer,
3176                 int *len, int max_cnt, int cq_id)
3177 {
3178         struct lpfc_queue *qp;
3179         int qidx;
3180
3181         for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
3182                 qp = phba->sli4_hba.fcp_wq[qidx];
3183                 if (qp->assoc_qid != cq_id)
3184                         continue;
3185                 *len = __lpfc_idiag_print_wq(qp, wqtype, pbuffer, *len);
3186                 if (*len >= max_cnt)
3187                         return 1;
3188         }
3189         for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
3190                 qp = phba->sli4_hba.nvme_wq[qidx];
3191                 if (qp->assoc_qid != cq_id)
3192                         continue;
3193                 *len = __lpfc_idiag_print_wq(qp, wqtype, pbuffer, *len);
3194                 if (*len >= max_cnt)
3195                         return 1;
3196         }
3197         return 0;
3198 }
3199
3200 static int
3201 __lpfc_idiag_print_cq(struct lpfc_queue *qp, char *cqtype,
3202                         char *pbuffer, int len)
3203 {
3204         if (!qp)
3205                 return len;
3206
3207         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3208                         "\t%s CQ info: ", cqtype);
3209         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3210                         "AssocEQID[%02d]: CQ STAT[max:x%x relw:x%x "
3211                         "xabt:x%x wq:x%llx]\n",
3212                         qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3213                         qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3214         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3215                         "\tCQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3216                         "HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]",
3217                         qp->queue_id, qp->entry_count,
3218                         qp->entry_size, qp->host_index,
3219                         qp->hba_index, qp->entry_repost);
3220
3221         len +=  snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3222
3223         return len;
3224 }
3225
3226 static int
3227 __lpfc_idiag_print_rqpair(struct lpfc_queue *qp, struct lpfc_queue *datqp,
3228                         char *rqtype, char *pbuffer, int len)
3229 {
3230         if (!qp || !datqp)
3231                 return len;
3232
3233         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3234                         "\t\t%s RQ info: ", rqtype);
3235         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3236                         "AssocCQID[%02d]: RQ-STAT[nopost:x%x nobuf:x%x "
3237                         "posted:x%x rcv:x%llx]\n",
3238                         qp->assoc_qid, qp->q_cnt_1, qp->q_cnt_2,
3239                         qp->q_cnt_3, (unsigned long long)qp->q_cnt_4);
3240         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3241                         "\t\tHQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3242                         "HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]\n",
3243                         qp->queue_id, qp->entry_count, qp->entry_size,
3244                         qp->host_index, qp->hba_index, qp->entry_repost);
3245         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3246                         "\t\tDQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3247                         "HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]\n",
3248                         datqp->queue_id, datqp->entry_count,
3249                         datqp->entry_size, datqp->host_index,
3250                         datqp->hba_index, datqp->entry_repost);
3251         return len;
3252 }
3253
3254 static int
3255 lpfc_idiag_cqs_for_eq(struct lpfc_hba *phba, char *pbuffer,
3256                 int *len, int max_cnt, int eqidx, int eq_id)
3257 {
3258         struct lpfc_queue *qp;
3259         int qidx, rc;
3260
3261         for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++) {
3262                 qp = phba->sli4_hba.fcp_cq[qidx];
3263                 if (qp->assoc_qid != eq_id)
3264                         continue;
3265
3266                 *len = __lpfc_idiag_print_cq(qp, "FCP", pbuffer, *len);
3267
3268                 /* Reset max counter */
3269                 qp->CQ_max_cqe = 0;
3270
3271                 if (*len >= max_cnt)
3272                         return 1;
3273
3274                 rc = lpfc_idiag_wqs_for_cq(phba, "FCP", pbuffer, len,
3275                                 max_cnt, qp->queue_id);
3276                 if (rc)
3277                         return 1;
3278         }
3279
3280         for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++) {
3281                 qp = phba->sli4_hba.nvme_cq[qidx];
3282                 if (qp->assoc_qid != eq_id)
3283                         continue;
3284
3285                 *len = __lpfc_idiag_print_cq(qp, "NVME", pbuffer, *len);
3286
3287                 /* Reset max counter */
3288                 qp->CQ_max_cqe = 0;
3289
3290                 if (*len >= max_cnt)
3291                         return 1;
3292
3293                 rc = lpfc_idiag_wqs_for_cq(phba, "NVME", pbuffer, len,
3294                                 max_cnt, qp->queue_id);
3295                 if (rc)
3296                         return 1;
3297         }
3298
3299         if ((eqidx < phba->cfg_nvmet_mrq) && phba->nvmet_support) {
3300                 /* NVMET CQset */
3301                 qp = phba->sli4_hba.nvmet_cqset[eqidx];
3302                 *len = __lpfc_idiag_print_cq(qp, "NVMET CQset", pbuffer, *len);
3303
3304                 /* Reset max counter */
3305                 qp->CQ_max_cqe = 0;
3306
3307                 if (*len >= max_cnt)
3308                         return 1;
3309
3310                 /* RQ header */
3311                 qp = phba->sli4_hba.nvmet_mrq_hdr[eqidx];
3312                 *len = __lpfc_idiag_print_rqpair(qp,
3313                                 phba->sli4_hba.nvmet_mrq_data[eqidx],
3314                                 "NVMET MRQ", pbuffer, *len);
3315
3316                 if (*len >= max_cnt)
3317                         return 1;
3318         }
3319
3320         return 0;
3321 }
3322
3323 static int
3324 __lpfc_idiag_print_eq(struct lpfc_queue *qp, char *eqtype,
3325                         char *pbuffer, int len)
3326 {
3327         if (!qp)
3328                 return len;
3329
3330         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3331                         "\n%s EQ info: EQ-STAT[max:x%x noE:x%x "
3332                         "cqe_proc:x%x eqe_proc:x%llx eqd %d]\n",
3333                         eqtype, qp->q_cnt_1, qp->q_cnt_2, qp->q_cnt_3,
3334                         (unsigned long long)qp->q_cnt_4, qp->q_mode);
3335         len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3336                         "EQID[%02d], QE-CNT[%04d], QE-SZ[%04d], "
3337                         "HST-IDX[%04d], PRT-IDX[%04d], PST[%03d]",
3338                         qp->queue_id, qp->entry_count, qp->entry_size,
3339                         qp->host_index, qp->hba_index, qp->entry_repost);
3340         len +=  snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len, "\n");
3341
3342         return len;
3343 }
3344
3345 /**
3346  * lpfc_idiag_queinfo_read - idiag debugfs read queue information
3347  * @file: The file pointer to read from.
3348  * @buf: The buffer to copy the data to.
3349  * @nbytes: The number of bytes to read.
3350  * @ppos: The position in the file to start reading from.
3351  *
3352  * Description:
3353  * This routine reads data from the @phba SLI4 PCI function queue information,
3354  * and copies to user @buf.
3355  * This routine only returns 1 EQs worth of information. It remembers the last
3356  * EQ read and jumps to the next EQ. Thus subsequent calls to queInfo will
3357  * retrieve all EQs allocated for the phba.
3358  *
3359  * Returns:
3360  * This function returns the amount of data that was read (this could be less
3361  * than @nbytes if the end of the file was reached) or a negative error value.
3362  **/
3363 static ssize_t
3364 lpfc_idiag_queinfo_read(struct file *file, char __user *buf, size_t nbytes,
3365                         loff_t *ppos)
3366 {
3367         struct lpfc_debug *debug = file->private_data;
3368         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3369         char *pbuffer;
3370         int max_cnt, rc, x, len = 0;
3371         struct lpfc_queue *qp = NULL;
3372
3373         if (!debug->buffer)
3374                 debug->buffer = kmalloc(LPFC_QUE_INFO_GET_BUF_SIZE, GFP_KERNEL);
3375         if (!debug->buffer)
3376                 return 0;
3377         pbuffer = debug->buffer;
3378         max_cnt = LPFC_QUE_INFO_GET_BUF_SIZE - 256;
3379
3380         if (*ppos)
3381                 return 0;
3382
3383         spin_lock_irq(&phba->hbalock);
3384
3385         /* Fast-path event queue */
3386         if (phba->sli4_hba.hba_eq && phba->io_channel_irqs) {
3387
3388                 x = phba->lpfc_idiag_last_eq;
3389                 if (phba->cfg_fof && (x >= phba->io_channel_irqs)) {
3390                         phba->lpfc_idiag_last_eq = 0;
3391                         goto fof;
3392                 }
3393                 phba->lpfc_idiag_last_eq++;
3394                 if (phba->lpfc_idiag_last_eq >= phba->io_channel_irqs)
3395                         if (phba->cfg_fof == 0)
3396                                 phba->lpfc_idiag_last_eq = 0;
3397
3398                 len += snprintf(pbuffer + len, LPFC_QUE_INFO_GET_BUF_SIZE - len,
3399                                         "EQ %d out of %d HBA EQs\n",
3400                                         x, phba->io_channel_irqs);
3401
3402                 /* Fast-path EQ */
3403                 qp = phba->sli4_hba.hba_eq[x];
3404                 if (!qp)
3405                         goto out;
3406
3407                 len = __lpfc_idiag_print_eq(qp, "HBA", pbuffer, len);
3408
3409                 /* Reset max counter */
3410                 qp->EQ_max_eqe = 0;
3411
3412                 if (len >= max_cnt)
3413                         goto too_big;
3414
3415                 /* will dump both fcp and nvme cqs/wqs for the eq */
3416                 rc = lpfc_idiag_cqs_for_eq(phba, pbuffer, &len,
3417                         max_cnt, x, qp->queue_id);
3418                 if (rc)
3419                         goto too_big;
3420
3421                 /* Only EQ 0 has slow path CQs configured */
3422                 if (x)
3423                         goto out;
3424
3425                 /* Slow-path mailbox CQ */
3426                 qp = phba->sli4_hba.mbx_cq;
3427                 len = __lpfc_idiag_print_cq(qp, "MBX", pbuffer, len);
3428                 if (len >= max_cnt)
3429                         goto too_big;
3430
3431                 /* Slow-path MBOX MQ */
3432                 qp = phba->sli4_hba.mbx_wq;
3433                 len = __lpfc_idiag_print_wq(qp, "MBX", pbuffer, len);
3434                 if (len >= max_cnt)
3435                         goto too_big;
3436
3437                 /* Slow-path ELS response CQ */
3438                 qp = phba->sli4_hba.els_cq;
3439                 len = __lpfc_idiag_print_cq(qp, "ELS", pbuffer, len);
3440                 /* Reset max counter */
3441                 if (qp)
3442                         qp->CQ_max_cqe = 0;
3443                 if (len >= max_cnt)
3444                         goto too_big;
3445
3446                 /* Slow-path ELS WQ */
3447                 qp = phba->sli4_hba.els_wq;
3448                 len = __lpfc_idiag_print_wq(qp, "ELS", pbuffer, len);
3449                 if (len >= max_cnt)
3450                         goto too_big;
3451
3452                 qp = phba->sli4_hba.hdr_rq;
3453                 len = __lpfc_idiag_print_rqpair(qp, phba->sli4_hba.dat_rq,
3454                                                 "ELS RQpair", pbuffer, len);
3455                 if (len >= max_cnt)
3456                         goto too_big;
3457
3458                 /* Slow-path NVME LS response CQ */
3459                 qp = phba->sli4_hba.nvmels_cq;
3460                 len = __lpfc_idiag_print_cq(qp, "NVME LS",
3461                                                 pbuffer, len);
3462                 /* Reset max counter */
3463                 if (qp)
3464                         qp->CQ_max_cqe = 0;
3465                 if (len >= max_cnt)
3466                         goto too_big;
3467
3468                 /* Slow-path NVME LS WQ */
3469                 qp = phba->sli4_hba.nvmels_wq;
3470                 len = __lpfc_idiag_print_wq(qp, "NVME LS",
3471                                                 pbuffer, len);
3472                 if (len >= max_cnt)
3473                         goto too_big;
3474
3475                 goto out;
3476         }
3477
3478 fof:
3479         if (phba->cfg_fof) {
3480                 /* FOF EQ */
3481                 qp = phba->sli4_hba.fof_eq;
3482                 len = __lpfc_idiag_print_eq(qp, "FOF", pbuffer, len);
3483
3484                 /* Reset max counter */
3485                 if (qp)
3486                         qp->EQ_max_eqe = 0;
3487
3488                 if (len >= max_cnt)
3489                         goto too_big;
3490
3491                 /* OAS CQ */
3492                 qp = phba->sli4_hba.oas_cq;
3493                 len = __lpfc_idiag_print_cq(qp, "OAS", pbuffer, len);
3494                 /* Reset max counter */
3495                 if (qp)
3496                         qp->CQ_max_cqe = 0;
3497                 if (len >= max_cnt)
3498                         goto too_big;
3499
3500                 /* OAS WQ */
3501                 qp = phba->sli4_hba.oas_wq;
3502                 len = __lpfc_idiag_print_wq(qp, "OAS", pbuffer, len);
3503                 if (len >= max_cnt)
3504                         goto too_big;
3505         }
3506
3507         spin_unlock_irq(&phba->hbalock);
3508         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3509
3510 too_big:
3511         len +=  snprintf(pbuffer + len,
3512                 LPFC_QUE_INFO_GET_BUF_SIZE - len, "Truncated ...\n");
3513 out:
3514         spin_unlock_irq(&phba->hbalock);
3515         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3516 }
3517
3518 /**
3519  * lpfc_idiag_que_param_check - queue access command parameter sanity check
3520  * @q: The pointer to queue structure.
3521  * @index: The index into a queue entry.
3522  * @count: The number of queue entries to access.
3523  *
3524  * Description:
3525  * The routine performs sanity check on device queue access method commands.
3526  *
3527  * Returns:
3528  * This function returns -EINVAL when fails the sanity check, otherwise, it
3529  * returns 0.
3530  **/
3531 static int
3532 lpfc_idiag_que_param_check(struct lpfc_queue *q, int index, int count)
3533 {
3534         /* Only support single entry read or browsing */
3535         if ((count != 1) && (count != LPFC_QUE_ACC_BROWSE))
3536                 return -EINVAL;
3537         if (index > q->entry_count - 1)
3538                 return -EINVAL;
3539         return 0;
3540 }
3541
3542 /**
3543  * lpfc_idiag_queacc_read_qe - read a single entry from the given queue index
3544  * @pbuffer: The pointer to buffer to copy the read data into.
3545  * @pque: The pointer to the queue to be read.
3546  * @index: The index into the queue entry.
3547  *
3548  * Description:
3549  * This routine reads out a single entry from the given queue's index location
3550  * and copies it into the buffer provided.
3551  *
3552  * Returns:
3553  * This function returns 0 when it fails, otherwise, it returns the length of
3554  * the data read into the buffer provided.
3555  **/
3556 static int
3557 lpfc_idiag_queacc_read_qe(char *pbuffer, int len, struct lpfc_queue *pque,
3558                           uint32_t index)
3559 {
3560         int offset, esize;
3561         uint32_t *pentry;
3562
3563         if (!pbuffer || !pque)
3564                 return 0;
3565
3566         esize = pque->entry_size;
3567         len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
3568                         "QE-INDEX[%04d]:\n", index);
3569
3570         offset = 0;
3571         pentry = pque->qe[index].address;
3572         while (esize > 0) {
3573                 len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len,
3574                                 "%08x ", *pentry);
3575                 pentry++;
3576                 offset += sizeof(uint32_t);
3577                 esize -= sizeof(uint32_t);
3578                 if (esize > 0 && !(offset % (4 * sizeof(uint32_t))))
3579                         len += snprintf(pbuffer+len,
3580                                         LPFC_QUE_ACC_BUF_SIZE-len, "\n");
3581         }
3582         len += snprintf(pbuffer+len, LPFC_QUE_ACC_BUF_SIZE-len, "\n");
3583
3584         return len;
3585 }
3586
3587 /**
3588  * lpfc_idiag_queacc_read - idiag debugfs read port queue
3589  * @file: The file pointer to read from.
3590  * @buf: The buffer to copy the data to.
3591  * @nbytes: The number of bytes to read.
3592  * @ppos: The position in the file to start reading from.
3593  *
3594  * Description:
3595  * This routine reads data from the @phba device queue memory according to the
3596  * idiag command, and copies to user @buf. Depending on the queue dump read
3597  * command setup, it does either a single queue entry read or browing through
3598  * all entries of the queue.
3599  *
3600  * Returns:
3601  * This function returns the amount of data that was read (this could be less
3602  * than @nbytes if the end of the file was reached) or a negative error value.
3603  **/
3604 static ssize_t
3605 lpfc_idiag_queacc_read(struct file *file, char __user *buf, size_t nbytes,
3606                        loff_t *ppos)
3607 {
3608         struct lpfc_debug *debug = file->private_data;
3609         uint32_t last_index, index, count;
3610         struct lpfc_queue *pque = NULL;
3611         char *pbuffer;
3612         int len = 0;
3613
3614         /* This is a user read operation */
3615         debug->op = LPFC_IDIAG_OP_RD;
3616
3617         if (!debug->buffer)
3618                 debug->buffer = kmalloc(LPFC_QUE_ACC_BUF_SIZE, GFP_KERNEL);
3619         if (!debug->buffer)
3620                 return 0;
3621         pbuffer = debug->buffer;
3622
3623         if (*ppos)
3624                 return 0;
3625
3626         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
3627                 index = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
3628                 count = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
3629                 pque = (struct lpfc_queue *)idiag.ptr_private;
3630         } else
3631                 return 0;
3632
3633         /* Browse the queue starting from index */
3634         if (count == LPFC_QUE_ACC_BROWSE)
3635                 goto que_browse;
3636
3637         /* Read a single entry from the queue */
3638         len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
3639
3640         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3641
3642 que_browse:
3643
3644         /* Browse all entries from the queue */
3645         last_index = idiag.offset.last_rd;
3646         index = last_index;
3647
3648         while (len < LPFC_QUE_ACC_SIZE - pque->entry_size) {
3649                 len = lpfc_idiag_queacc_read_qe(pbuffer, len, pque, index);
3650                 index++;
3651                 if (index > pque->entry_count - 1)
3652                         break;
3653         }
3654
3655         /* Set up the offset for next portion of pci cfg read */
3656         if (index > pque->entry_count - 1)
3657                 index = 0;
3658         idiag.offset.last_rd = index;
3659
3660         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
3661 }
3662
3663 /**
3664  * lpfc_idiag_queacc_write - Syntax check and set up idiag queacc commands
3665  * @file: The file pointer to read from.
3666  * @buf: The buffer to copy the user data from.
3667  * @nbytes: The number of bytes to get.
3668  * @ppos: The position in the file to start reading from.
3669  *
3670  * This routine get the debugfs idiag command struct from user space and then
3671  * perform the syntax check for port queue read (dump) or write (set) command
3672  * accordingly. In the case of port queue read command, it sets up the command
3673  * in the idiag command struct for the following debugfs read operation. In
3674  * the case of port queue write operation, it executes the write operation
3675  * into the port queue entry accordingly.
3676  *
3677  * It returns the @nbytges passing in from debugfs user space when successful.
3678  * In case of error conditions, it returns proper error code back to the user
3679  * space.
3680  **/
3681 static ssize_t
3682 lpfc_idiag_queacc_write(struct file *file, const char __user *buf,
3683                         size_t nbytes, loff_t *ppos)
3684 {
3685         struct lpfc_debug *debug = file->private_data;
3686         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
3687         uint32_t qidx, quetp, queid, index, count, offset, value;
3688         uint32_t *pentry;
3689         struct lpfc_queue *pque, *qp;
3690         int rc;
3691
3692         /* This is a user write operation */
3693         debug->op = LPFC_IDIAG_OP_WR;
3694
3695         rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
3696         if (rc < 0)
3697                 return rc;
3698
3699         /* Get and sanity check on command feilds */
3700         quetp  = idiag.cmd.data[IDIAG_QUEACC_QUETP_INDX];
3701         queid  = idiag.cmd.data[IDIAG_QUEACC_QUEID_INDX];
3702         index  = idiag.cmd.data[IDIAG_QUEACC_INDEX_INDX];
3703         count  = idiag.cmd.data[IDIAG_QUEACC_COUNT_INDX];
3704         offset = idiag.cmd.data[IDIAG_QUEACC_OFFST_INDX];
3705         value  = idiag.cmd.data[IDIAG_QUEACC_VALUE_INDX];
3706
3707         /* Sanity check on command line arguments */
3708         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
3709             idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
3710             idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
3711                 if (rc != LPFC_QUE_ACC_WR_CMD_ARG)
3712                         goto error_out;
3713                 if (count != 1)
3714                         goto error_out;
3715         } else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
3716                 if (rc != LPFC_QUE_ACC_RD_CMD_ARG)
3717                         goto error_out;
3718         } else
3719                 goto error_out;
3720
3721         switch (quetp) {
3722         case LPFC_IDIAG_EQ:
3723                 /* HBA event queue */
3724                 if (phba->sli4_hba.hba_eq) {
3725                         for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
3726                                 qp = phba->sli4_hba.hba_eq[qidx];
3727                                 if (qp && qp->queue_id == queid) {
3728                                         /* Sanity check */
3729                                         rc = lpfc_idiag_que_param_check(qp,
3730                                                 index, count);
3731                                         if (rc)
3732                                                 goto error_out;
3733                                         idiag.ptr_private = qp;
3734                                         goto pass_check;
3735                                 }
3736                         }
3737                 }
3738                 goto error_out;
3739                 break;
3740         case LPFC_IDIAG_CQ:
3741                 /* MBX complete queue */
3742                 if (phba->sli4_hba.mbx_cq &&
3743                     phba->sli4_hba.mbx_cq->queue_id == queid) {
3744                         /* Sanity check */
3745                         rc = lpfc_idiag_que_param_check(
3746                                         phba->sli4_hba.mbx_cq, index, count);
3747                         if (rc)
3748                                 goto error_out;
3749                         idiag.ptr_private = phba->sli4_hba.mbx_cq;
3750                         goto pass_check;
3751                 }
3752                 /* ELS complete queue */
3753                 if (phba->sli4_hba.els_cq &&
3754                     phba->sli4_hba.els_cq->queue_id == queid) {
3755                         /* Sanity check */
3756                         rc = lpfc_idiag_que_param_check(
3757                                         phba->sli4_hba.els_cq, index, count);
3758                         if (rc)
3759                                 goto error_out;
3760                         idiag.ptr_private = phba->sli4_hba.els_cq;
3761                         goto pass_check;
3762                 }
3763                 /* NVME LS complete queue */
3764                 if (phba->sli4_hba.nvmels_cq &&
3765                     phba->sli4_hba.nvmels_cq->queue_id == queid) {
3766                         /* Sanity check */
3767                         rc = lpfc_idiag_que_param_check(
3768                                         phba->sli4_hba.nvmels_cq, index, count);
3769                         if (rc)
3770                                 goto error_out;
3771                         idiag.ptr_private = phba->sli4_hba.nvmels_cq;
3772                         goto pass_check;
3773                 }
3774                 /* FCP complete queue */
3775                 if (phba->sli4_hba.fcp_cq) {
3776                         for (qidx = 0; qidx < phba->cfg_fcp_io_channel;
3777                                                                 qidx++) {
3778                                 qp = phba->sli4_hba.fcp_cq[qidx];
3779                                 if (qp && qp->queue_id == queid) {
3780                                         /* Sanity check */
3781                                         rc = lpfc_idiag_que_param_check(
3782                                                 qp, index, count);
3783                                         if (rc)
3784                                                 goto error_out;
3785                                         idiag.ptr_private = qp;
3786                                         goto pass_check;
3787                                 }
3788                         }
3789                 }
3790                 /* NVME complete queue */
3791                 if (phba->sli4_hba.nvme_cq) {
3792                         qidx = 0;
3793                         do {
3794                                 if (phba->sli4_hba.nvme_cq[qidx] &&
3795                                     phba->sli4_hba.nvme_cq[qidx]->queue_id ==
3796                                     queid) {
3797                                         /* Sanity check */
3798                                         rc = lpfc_idiag_que_param_check(
3799                                                 phba->sli4_hba.nvme_cq[qidx],
3800                                                 index, count);
3801                                         if (rc)
3802                                                 goto error_out;
3803                                         idiag.ptr_private =
3804                                                 phba->sli4_hba.nvme_cq[qidx];
3805                                         goto pass_check;
3806                                 }
3807                         } while (++qidx < phba->cfg_nvme_io_channel);
3808                 }
3809                 goto error_out;
3810                 break;
3811         case LPFC_IDIAG_MQ:
3812                 /* MBX work queue */
3813                 if (phba->sli4_hba.mbx_wq &&
3814                     phba->sli4_hba.mbx_wq->queue_id == queid) {
3815                         /* Sanity check */
3816                         rc = lpfc_idiag_que_param_check(
3817                                         phba->sli4_hba.mbx_wq, index, count);
3818                         if (rc)
3819                                 goto error_out;
3820                         idiag.ptr_private = phba->sli4_hba.mbx_wq;
3821                         goto pass_check;
3822                 }
3823                 goto error_out;
3824                 break;
3825         case LPFC_IDIAG_WQ:
3826                 /* ELS work queue */
3827                 if (phba->sli4_hba.els_wq &&
3828                     phba->sli4_hba.els_wq->queue_id == queid) {
3829                         /* Sanity check */
3830                         rc = lpfc_idiag_que_param_check(
3831                                         phba->sli4_hba.els_wq, index, count);
3832                         if (rc)
3833                                 goto error_out;
3834                         idiag.ptr_private = phba->sli4_hba.els_wq;
3835                         goto pass_check;
3836                 }
3837                 /* NVME LS work queue */
3838                 if (phba->sli4_hba.nvmels_wq &&
3839                     phba->sli4_hba.nvmels_wq->queue_id == queid) {
3840                         /* Sanity check */
3841                         rc = lpfc_idiag_que_param_check(
3842                                         phba->sli4_hba.nvmels_wq, index, count);
3843                         if (rc)
3844                                 goto error_out;
3845                         idiag.ptr_private = phba->sli4_hba.nvmels_wq;
3846                         goto pass_check;
3847                 }
3848                 /* FCP work queue */
3849                 if (phba->sli4_hba.fcp_wq) {
3850                         for (qidx = 0; qidx < phba->cfg_fcp_io_channel;
3851                                                                 qidx++) {
3852                                 qp = phba->sli4_hba.fcp_wq[qidx];
3853                                 if (qp && qp->queue_id == queid) {
3854                                         /* Sanity check */
3855                                         rc = lpfc_idiag_que_param_check(
3856                                                 qp, index, count);
3857                                         if (rc)
3858                                                 goto error_out;
3859                                         idiag.ptr_private = qp;
3860                                         goto pass_check;
3861                                 }
3862                         }
3863                 }
3864                 /* NVME work queue */
3865                 if (phba->sli4_hba.nvme_wq) {
3866                         for (qidx = 0; qidx < phba->cfg_nvme_io_channel;
3867                                                                 qidx++) {
3868                                 qp = phba->sli4_hba.nvme_wq[qidx];
3869                                 if (qp && qp->queue_id == queid) {
3870                                         /* Sanity check */
3871                                         rc = lpfc_idiag_que_param_check(
3872                                                 qp, index, count);
3873                                         if (rc)
3874                                                 goto error_out;
3875                                         idiag.ptr_private = qp;
3876                                         goto pass_check;
3877                                 }
3878                         }
3879                 }
3880
3881                 /* NVME work queues */
3882                 if (phba->sli4_hba.nvme_wq) {
3883                         for (qidx = 0; qidx < phba->cfg_nvme_io_channel;
3884                                 qidx++) {
3885                                 if (!phba->sli4_hba.nvme_wq[qidx])
3886                                         continue;
3887                                 if (phba->sli4_hba.nvme_wq[qidx]->queue_id ==
3888                                     queid) {
3889                                         /* Sanity check */
3890                                         rc = lpfc_idiag_que_param_check(
3891                                                 phba->sli4_hba.nvme_wq[qidx],
3892                                                 index, count);
3893                                         if (rc)
3894                                                 goto error_out;
3895                                         idiag.ptr_private =
3896                                                 phba->sli4_hba.nvme_wq[qidx];
3897                                         goto pass_check;
3898                                 }
3899                         }
3900                 }
3901                 goto error_out;
3902                 break;
3903         case LPFC_IDIAG_RQ:
3904                 /* HDR queue */
3905                 if (phba->sli4_hba.hdr_rq &&
3906                     phba->sli4_hba.hdr_rq->queue_id == queid) {
3907                         /* Sanity check */
3908                         rc = lpfc_idiag_que_param_check(
3909                                         phba->sli4_hba.hdr_rq, index, count);
3910                         if (rc)
3911                                 goto error_out;
3912                         idiag.ptr_private = phba->sli4_hba.hdr_rq;
3913                         goto pass_check;
3914                 }
3915                 /* DAT queue */
3916                 if (phba->sli4_hba.dat_rq &&
3917                     phba->sli4_hba.dat_rq->queue_id == queid) {
3918                         /* Sanity check */
3919                         rc = lpfc_idiag_que_param_check(
3920                                         phba->sli4_hba.dat_rq, index, count);
3921                         if (rc)
3922                                 goto error_out;
3923                         idiag.ptr_private = phba->sli4_hba.dat_rq;
3924                         goto pass_check;
3925                 }
3926                 goto error_out;
3927                 break;
3928         default:
3929                 goto error_out;
3930                 break;
3931         }
3932
3933 pass_check:
3934
3935         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_RD) {
3936                 if (count == LPFC_QUE_ACC_BROWSE)
3937                         idiag.offset.last_rd = index;
3938         }
3939
3940         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR ||
3941             idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST ||
3942             idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL) {
3943                 /* Additional sanity checks on write operation */
3944                 pque = (struct lpfc_queue *)idiag.ptr_private;
3945                 if (offset > pque->entry_size/sizeof(uint32_t) - 1)
3946                         goto error_out;
3947                 pentry = pque->qe[index].address;
3948                 pentry += offset;
3949                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_WR)
3950                         *pentry = value;
3951                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_ST)
3952                         *pentry |= value;
3953                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_QUEACC_CL)
3954                         *pentry &= ~value;
3955         }
3956         return nbytes;
3957
3958 error_out:
3959         /* Clean out command structure on command error out */
3960         memset(&idiag, 0, sizeof(idiag));
3961         return -EINVAL;
3962 }
3963
3964 /**
3965  * lpfc_idiag_drbacc_read_reg - idiag debugfs read a doorbell register
3966  * @phba: The pointer to hba structure.
3967  * @pbuffer: The pointer to the buffer to copy the data to.
3968  * @len: The lenght of bytes to copied.
3969  * @drbregid: The id to doorbell registers.
3970  *
3971  * Description:
3972  * This routine reads a doorbell register and copies its content to the
3973  * user buffer pointed to by @pbuffer.
3974  *
3975  * Returns:
3976  * This function returns the amount of data that was copied into @pbuffer.
3977  **/
3978 static int
3979 lpfc_idiag_drbacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
3980                            int len, uint32_t drbregid)
3981 {
3982
3983         if (!pbuffer)
3984                 return 0;
3985
3986         switch (drbregid) {
3987         case LPFC_DRB_EQ:
3988                 len += snprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE-len,
3989                                 "EQ-DRB-REG: 0x%08x\n",
3990                                 readl(phba->sli4_hba.EQDBregaddr));
3991                 break;
3992         case LPFC_DRB_CQ:
3993                 len += snprintf(pbuffer + len, LPFC_DRB_ACC_BUF_SIZE - len,
3994                                 "CQ-DRB-REG: 0x%08x\n",
3995                                 readl(phba->sli4_hba.CQDBregaddr));
3996                 break;
3997         case LPFC_DRB_MQ:
3998                 len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
3999                                 "MQ-DRB-REG:   0x%08x\n",
4000                                 readl(phba->sli4_hba.MQDBregaddr));
4001                 break;
4002         case LPFC_DRB_WQ:
4003                 len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4004                                 "WQ-DRB-REG:   0x%08x\n",
4005                                 readl(phba->sli4_hba.WQDBregaddr));
4006                 break;
4007         case LPFC_DRB_RQ:
4008                 len += snprintf(pbuffer+len, LPFC_DRB_ACC_BUF_SIZE-len,
4009                                 "RQ-DRB-REG:   0x%08x\n",
4010                                 readl(phba->sli4_hba.RQDBregaddr));
4011                 break;
4012         default:
4013                 break;
4014         }
4015
4016         return len;
4017 }
4018
4019 /**
4020  * lpfc_idiag_drbacc_read - idiag debugfs read port doorbell
4021  * @file: The file pointer to read from.
4022  * @buf: The buffer to copy the data to.
4023  * @nbytes: The number of bytes to read.
4024  * @ppos: The position in the file to start reading from.
4025  *
4026  * Description:
4027  * This routine reads data from the @phba device doorbell register according
4028  * to the idiag command, and copies to user @buf. Depending on the doorbell
4029  * register read command setup, it does either a single doorbell register
4030  * read or dump all doorbell registers.
4031  *
4032  * Returns:
4033  * This function returns the amount of data that was read (this could be less
4034  * than @nbytes if the end of the file was reached) or a negative error value.
4035  **/
4036 static ssize_t
4037 lpfc_idiag_drbacc_read(struct file *file, char __user *buf, size_t nbytes,
4038                        loff_t *ppos)
4039 {
4040         struct lpfc_debug *debug = file->private_data;
4041         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4042         uint32_t drb_reg_id, i;
4043         char *pbuffer;
4044         int len = 0;
4045
4046         /* This is a user read operation */
4047         debug->op = LPFC_IDIAG_OP_RD;
4048
4049         if (!debug->buffer)
4050                 debug->buffer = kmalloc(LPFC_DRB_ACC_BUF_SIZE, GFP_KERNEL);
4051         if (!debug->buffer)
4052                 return 0;
4053         pbuffer = debug->buffer;
4054
4055         if (*ppos)
4056                 return 0;
4057
4058         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD)
4059                 drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4060         else
4061                 return 0;
4062
4063         if (drb_reg_id == LPFC_DRB_ACC_ALL)
4064                 for (i = 1; i <= LPFC_DRB_MAX; i++)
4065                         len = lpfc_idiag_drbacc_read_reg(phba,
4066                                                          pbuffer, len, i);
4067         else
4068                 len = lpfc_idiag_drbacc_read_reg(phba,
4069                                                  pbuffer, len, drb_reg_id);
4070
4071         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4072 }
4073
4074 /**
4075  * lpfc_idiag_drbacc_write - Syntax check and set up idiag drbacc commands
4076  * @file: The file pointer to read from.
4077  * @buf: The buffer to copy the user data from.
4078  * @nbytes: The number of bytes to get.
4079  * @ppos: The position in the file to start reading from.
4080  *
4081  * This routine get the debugfs idiag command struct from user space and then
4082  * perform the syntax check for port doorbell register read (dump) or write
4083  * (set) command accordingly. In the case of port queue read command, it sets
4084  * up the command in the idiag command struct for the following debugfs read
4085  * operation. In the case of port doorbell register write operation, it
4086  * executes the write operation into the port doorbell register accordingly.
4087  *
4088  * It returns the @nbytges passing in from debugfs user space when successful.
4089  * In case of error conditions, it returns proper error code back to the user
4090  * space.
4091  **/
4092 static ssize_t
4093 lpfc_idiag_drbacc_write(struct file *file, const char __user *buf,
4094                         size_t nbytes, loff_t *ppos)
4095 {
4096         struct lpfc_debug *debug = file->private_data;
4097         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4098         uint32_t drb_reg_id, value, reg_val = 0;
4099         void __iomem *drb_reg;
4100         int rc;
4101
4102         /* This is a user write operation */
4103         debug->op = LPFC_IDIAG_OP_WR;
4104
4105         rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4106         if (rc < 0)
4107                 return rc;
4108
4109         /* Sanity check on command line arguments */
4110         drb_reg_id = idiag.cmd.data[IDIAG_DRBACC_REGID_INDX];
4111         value = idiag.cmd.data[IDIAG_DRBACC_VALUE_INDX];
4112
4113         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4114             idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4115             idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4116                 if (rc != LPFC_DRB_ACC_WR_CMD_ARG)
4117                         goto error_out;
4118                 if (drb_reg_id > LPFC_DRB_MAX)
4119                         goto error_out;
4120         } else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_RD) {
4121                 if (rc != LPFC_DRB_ACC_RD_CMD_ARG)
4122                         goto error_out;
4123                 if ((drb_reg_id > LPFC_DRB_MAX) &&
4124                     (drb_reg_id != LPFC_DRB_ACC_ALL))
4125                         goto error_out;
4126         } else
4127                 goto error_out;
4128
4129         /* Perform the write access operation */
4130         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR ||
4131             idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST ||
4132             idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4133                 switch (drb_reg_id) {
4134                 case LPFC_DRB_EQ:
4135                         drb_reg = phba->sli4_hba.EQDBregaddr;
4136                         break;
4137                 case LPFC_DRB_CQ:
4138                         drb_reg = phba->sli4_hba.CQDBregaddr;
4139                         break;
4140                 case LPFC_DRB_MQ:
4141                         drb_reg = phba->sli4_hba.MQDBregaddr;
4142                         break;
4143                 case LPFC_DRB_WQ:
4144                         drb_reg = phba->sli4_hba.WQDBregaddr;
4145                         break;
4146                 case LPFC_DRB_RQ:
4147                         drb_reg = phba->sli4_hba.RQDBregaddr;
4148                         break;
4149                 default:
4150                         goto error_out;
4151                 }
4152
4153                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_WR)
4154                         reg_val = value;
4155                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_ST) {
4156                         reg_val = readl(drb_reg);
4157                         reg_val |= value;
4158                 }
4159                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_DRBACC_CL) {
4160                         reg_val = readl(drb_reg);
4161                         reg_val &= ~value;
4162                 }
4163                 writel(reg_val, drb_reg);
4164                 readl(drb_reg); /* flush */
4165         }
4166         return nbytes;
4167
4168 error_out:
4169         /* Clean out command structure on command error out */
4170         memset(&idiag, 0, sizeof(idiag));
4171         return -EINVAL;
4172 }
4173
4174 /**
4175  * lpfc_idiag_ctlacc_read_reg - idiag debugfs read a control registers
4176  * @phba: The pointer to hba structure.
4177  * @pbuffer: The pointer to the buffer to copy the data to.
4178  * @len: The lenght of bytes to copied.
4179  * @drbregid: The id to doorbell registers.
4180  *
4181  * Description:
4182  * This routine reads a control register and copies its content to the
4183  * user buffer pointed to by @pbuffer.
4184  *
4185  * Returns:
4186  * This function returns the amount of data that was copied into @pbuffer.
4187  **/
4188 static int
4189 lpfc_idiag_ctlacc_read_reg(struct lpfc_hba *phba, char *pbuffer,
4190                            int len, uint32_t ctlregid)
4191 {
4192
4193         if (!pbuffer)
4194                 return 0;
4195
4196         switch (ctlregid) {
4197         case LPFC_CTL_PORT_SEM:
4198                 len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4199                                 "Port SemReg:   0x%08x\n",
4200                                 readl(phba->sli4_hba.conf_regs_memmap_p +
4201                                       LPFC_CTL_PORT_SEM_OFFSET));
4202                 break;
4203         case LPFC_CTL_PORT_STA:
4204                 len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4205                                 "Port StaReg:   0x%08x\n",
4206                                 readl(phba->sli4_hba.conf_regs_memmap_p +
4207                                       LPFC_CTL_PORT_STA_OFFSET));
4208                 break;
4209         case LPFC_CTL_PORT_CTL:
4210                 len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4211                                 "Port CtlReg:   0x%08x\n",
4212                                 readl(phba->sli4_hba.conf_regs_memmap_p +
4213                                       LPFC_CTL_PORT_CTL_OFFSET));
4214                 break;
4215         case LPFC_CTL_PORT_ER1:
4216                 len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4217                                 "Port Er1Reg:   0x%08x\n",
4218                                 readl(phba->sli4_hba.conf_regs_memmap_p +
4219                                       LPFC_CTL_PORT_ER1_OFFSET));
4220                 break;
4221         case LPFC_CTL_PORT_ER2:
4222                 len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4223                                 "Port Er2Reg:   0x%08x\n",
4224                                 readl(phba->sli4_hba.conf_regs_memmap_p +
4225                                       LPFC_CTL_PORT_ER2_OFFSET));
4226                 break;
4227         case LPFC_CTL_PDEV_CTL:
4228                 len += snprintf(pbuffer+len, LPFC_CTL_ACC_BUF_SIZE-len,
4229                                 "PDev CtlReg:   0x%08x\n",
4230                                 readl(phba->sli4_hba.conf_regs_memmap_p +
4231                                       LPFC_CTL_PDEV_CTL_OFFSET));
4232                 break;
4233         default:
4234                 break;
4235         }
4236         return len;
4237 }
4238
4239 /**
4240  * lpfc_idiag_ctlacc_read - idiag debugfs read port and device control register
4241  * @file: The file pointer to read from.
4242  * @buf: The buffer to copy the data to.
4243  * @nbytes: The number of bytes to read.
4244  * @ppos: The position in the file to start reading from.
4245  *
4246  * Description:
4247  * This routine reads data from the @phba port and device registers according
4248  * to the idiag command, and copies to user @buf.
4249  *
4250  * Returns:
4251  * This function returns the amount of data that was read (this could be less
4252  * than @nbytes if the end of the file was reached) or a negative error value.
4253  **/
4254 static ssize_t
4255 lpfc_idiag_ctlacc_read(struct file *file, char __user *buf, size_t nbytes,
4256                        loff_t *ppos)
4257 {
4258         struct lpfc_debug *debug = file->private_data;
4259         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4260         uint32_t ctl_reg_id, i;
4261         char *pbuffer;
4262         int len = 0;
4263
4264         /* This is a user read operation */
4265         debug->op = LPFC_IDIAG_OP_RD;
4266
4267         if (!debug->buffer)
4268                 debug->buffer = kmalloc(LPFC_CTL_ACC_BUF_SIZE, GFP_KERNEL);
4269         if (!debug->buffer)
4270                 return 0;
4271         pbuffer = debug->buffer;
4272
4273         if (*ppos)
4274                 return 0;
4275
4276         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD)
4277                 ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4278         else
4279                 return 0;
4280
4281         if (ctl_reg_id == LPFC_CTL_ACC_ALL)
4282                 for (i = 1; i <= LPFC_CTL_MAX; i++)
4283                         len = lpfc_idiag_ctlacc_read_reg(phba,
4284                                                          pbuffer, len, i);
4285         else
4286                 len = lpfc_idiag_ctlacc_read_reg(phba,
4287                                                  pbuffer, len, ctl_reg_id);
4288
4289         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4290 }
4291
4292 /**
4293  * lpfc_idiag_ctlacc_write - Syntax check and set up idiag ctlacc commands
4294  * @file: The file pointer to read from.
4295  * @buf: The buffer to copy the user data from.
4296  * @nbytes: The number of bytes to get.
4297  * @ppos: The position in the file to start reading from.
4298  *
4299  * This routine get the debugfs idiag command struct from user space and then
4300  * perform the syntax check for port and device control register read (dump)
4301  * or write (set) command accordingly.
4302  *
4303  * It returns the @nbytges passing in from debugfs user space when successful.
4304  * In case of error conditions, it returns proper error code back to the user
4305  * space.
4306  **/
4307 static ssize_t
4308 lpfc_idiag_ctlacc_write(struct file *file, const char __user *buf,
4309                         size_t nbytes, loff_t *ppos)
4310 {
4311         struct lpfc_debug *debug = file->private_data;
4312         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4313         uint32_t ctl_reg_id, value, reg_val = 0;
4314         void __iomem *ctl_reg;
4315         int rc;
4316
4317         /* This is a user write operation */
4318         debug->op = LPFC_IDIAG_OP_WR;
4319
4320         rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4321         if (rc < 0)
4322                 return rc;
4323
4324         /* Sanity check on command line arguments */
4325         ctl_reg_id = idiag.cmd.data[IDIAG_CTLACC_REGID_INDX];
4326         value = idiag.cmd.data[IDIAG_CTLACC_VALUE_INDX];
4327
4328         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4329             idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4330             idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4331                 if (rc != LPFC_CTL_ACC_WR_CMD_ARG)
4332                         goto error_out;
4333                 if (ctl_reg_id > LPFC_CTL_MAX)
4334                         goto error_out;
4335         } else if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_RD) {
4336                 if (rc != LPFC_CTL_ACC_RD_CMD_ARG)
4337                         goto error_out;
4338                 if ((ctl_reg_id > LPFC_CTL_MAX) &&
4339                     (ctl_reg_id != LPFC_CTL_ACC_ALL))
4340                         goto error_out;
4341         } else
4342                 goto error_out;
4343
4344         /* Perform the write access operation */
4345         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR ||
4346             idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST ||
4347             idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4348                 switch (ctl_reg_id) {
4349                 case LPFC_CTL_PORT_SEM:
4350                         ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4351                                         LPFC_CTL_PORT_SEM_OFFSET;
4352                         break;
4353                 case LPFC_CTL_PORT_STA:
4354                         ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4355                                         LPFC_CTL_PORT_STA_OFFSET;
4356                         break;
4357                 case LPFC_CTL_PORT_CTL:
4358                         ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4359                                         LPFC_CTL_PORT_CTL_OFFSET;
4360                         break;
4361                 case LPFC_CTL_PORT_ER1:
4362                         ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4363                                         LPFC_CTL_PORT_ER1_OFFSET;
4364                         break;
4365                 case LPFC_CTL_PORT_ER2:
4366                         ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4367                                         LPFC_CTL_PORT_ER2_OFFSET;
4368                         break;
4369                 case LPFC_CTL_PDEV_CTL:
4370                         ctl_reg = phba->sli4_hba.conf_regs_memmap_p +
4371                                         LPFC_CTL_PDEV_CTL_OFFSET;
4372                         break;
4373                 default:
4374                         goto error_out;
4375                 }
4376
4377                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_WR)
4378                         reg_val = value;
4379                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_ST) {
4380                         reg_val = readl(ctl_reg);
4381                         reg_val |= value;
4382                 }
4383                 if (idiag.cmd.opcode == LPFC_IDIAG_CMD_CTLACC_CL) {
4384                         reg_val = readl(ctl_reg);
4385                         reg_val &= ~value;
4386                 }
4387                 writel(reg_val, ctl_reg);
4388                 readl(ctl_reg); /* flush */
4389         }
4390         return nbytes;
4391
4392 error_out:
4393         /* Clean out command structure on command error out */
4394         memset(&idiag, 0, sizeof(idiag));
4395         return -EINVAL;
4396 }
4397
4398 /**
4399  * lpfc_idiag_mbxacc_get_setup - idiag debugfs get mailbox access setup
4400  * @phba: Pointer to HBA context object.
4401  * @pbuffer: Pointer to data buffer.
4402  *
4403  * Description:
4404  * This routine gets the driver mailbox access debugfs setup information.
4405  *
4406  * Returns:
4407  * This function returns the amount of data that was read (this could be less
4408  * than @nbytes if the end of the file was reached) or a negative error value.
4409  **/
4410 static int
4411 lpfc_idiag_mbxacc_get_setup(struct lpfc_hba *phba, char *pbuffer)
4412 {
4413         uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
4414         int len = 0;
4415
4416         mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
4417         mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
4418         mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
4419         mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
4420
4421         len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4422                         "mbx_dump_map: 0x%08x\n", mbx_dump_map);
4423         len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4424                         "mbx_dump_cnt: %04d\n", mbx_dump_cnt);
4425         len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4426                         "mbx_word_cnt: %04d\n", mbx_word_cnt);
4427         len += snprintf(pbuffer+len, LPFC_MBX_ACC_BUF_SIZE-len,
4428                         "mbx_mbox_cmd: 0x%02x\n", mbx_mbox_cmd);
4429
4430         return len;
4431 }
4432
4433 /**
4434  * lpfc_idiag_mbxacc_read - idiag debugfs read on mailbox access
4435  * @file: The file pointer to read from.
4436  * @buf: The buffer to copy the data to.
4437  * @nbytes: The number of bytes to read.
4438  * @ppos: The position in the file to start reading from.
4439  *
4440  * Description:
4441  * This routine reads data from the @phba driver mailbox access debugfs setup
4442  * information.
4443  *
4444  * Returns:
4445  * This function returns the amount of data that was read (this could be less
4446  * than @nbytes if the end of the file was reached) or a negative error value.
4447  **/
4448 static ssize_t
4449 lpfc_idiag_mbxacc_read(struct file *file, char __user *buf, size_t nbytes,
4450                        loff_t *ppos)
4451 {
4452         struct lpfc_debug *debug = file->private_data;
4453         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4454         char *pbuffer;
4455         int len = 0;
4456
4457         /* This is a user read operation */
4458         debug->op = LPFC_IDIAG_OP_RD;
4459
4460         if (!debug->buffer)
4461                 debug->buffer = kmalloc(LPFC_MBX_ACC_BUF_SIZE, GFP_KERNEL);
4462         if (!debug->buffer)
4463                 return 0;
4464         pbuffer = debug->buffer;
4465
4466         if (*ppos)
4467                 return 0;
4468
4469         if ((idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP) &&
4470             (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP))
4471                 return 0;
4472
4473         len = lpfc_idiag_mbxacc_get_setup(phba, pbuffer);
4474
4475         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4476 }
4477
4478 /**
4479  * lpfc_idiag_mbxacc_write - Syntax check and set up idiag mbxacc commands
4480  * @file: The file pointer to read from.
4481  * @buf: The buffer to copy the user data from.
4482  * @nbytes: The number of bytes to get.
4483  * @ppos: The position in the file to start reading from.
4484  *
4485  * This routine get the debugfs idiag command struct from user space and then
4486  * perform the syntax check for driver mailbox command (dump) and sets up the
4487  * necessary states in the idiag command struct accordingly.
4488  *
4489  * It returns the @nbytges passing in from debugfs user space when successful.
4490  * In case of error conditions, it returns proper error code back to the user
4491  * space.
4492  **/
4493 static ssize_t
4494 lpfc_idiag_mbxacc_write(struct file *file, const char __user *buf,
4495                         size_t nbytes, loff_t *ppos)
4496 {
4497         struct lpfc_debug *debug = file->private_data;
4498         uint32_t mbx_dump_map, mbx_dump_cnt, mbx_word_cnt, mbx_mbox_cmd;
4499         int rc;
4500
4501         /* This is a user write operation */
4502         debug->op = LPFC_IDIAG_OP_WR;
4503
4504         rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4505         if (rc < 0)
4506                 return rc;
4507
4508         /* Sanity check on command line arguments */
4509         mbx_mbox_cmd = idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
4510         mbx_dump_map = idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
4511         mbx_dump_cnt = idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
4512         mbx_word_cnt = idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
4513
4514         if (idiag.cmd.opcode == LPFC_IDIAG_CMD_MBXACC_DP) {
4515                 if (!(mbx_dump_map & LPFC_MBX_DMP_MBX_ALL))
4516                         goto error_out;
4517                 if ((mbx_dump_map & ~LPFC_MBX_DMP_MBX_ALL) &&
4518                     (mbx_dump_map != LPFC_MBX_DMP_ALL))
4519                         goto error_out;
4520                 if (mbx_word_cnt > sizeof(MAILBOX_t))
4521                         goto error_out;
4522         } else if (idiag.cmd.opcode == LPFC_IDIAG_BSG_MBXACC_DP) {
4523                 if (!(mbx_dump_map & LPFC_BSG_DMP_MBX_ALL))
4524                         goto error_out;
4525                 if ((mbx_dump_map & ~LPFC_BSG_DMP_MBX_ALL) &&
4526                     (mbx_dump_map != LPFC_MBX_DMP_ALL))
4527                         goto error_out;
4528                 if (mbx_word_cnt > (BSG_MBOX_SIZE)/4)
4529                         goto error_out;
4530                 if (mbx_mbox_cmd != 0x9b)
4531                         goto error_out;
4532         } else
4533                 goto error_out;
4534
4535         if (mbx_word_cnt == 0)
4536                 goto error_out;
4537         if (rc != LPFC_MBX_DMP_ARG)
4538                 goto error_out;
4539         if (mbx_mbox_cmd & ~0xff)
4540                 goto error_out;
4541
4542         /* condition for stop mailbox dump */
4543         if (mbx_dump_cnt == 0)
4544                 goto reset_out;
4545
4546         return nbytes;
4547
4548 reset_out:
4549         /* Clean out command structure on command error out */
4550         memset(&idiag, 0, sizeof(idiag));
4551         return nbytes;
4552
4553 error_out:
4554         /* Clean out command structure on command error out */
4555         memset(&idiag, 0, sizeof(idiag));
4556         return -EINVAL;
4557 }
4558
4559 /**
4560  * lpfc_idiag_extacc_avail_get - get the available extents information
4561  * @phba: pointer to lpfc hba data structure.
4562  * @pbuffer: pointer to internal buffer.
4563  * @len: length into the internal buffer data has been copied.
4564  *
4565  * Description:
4566  * This routine is to get the available extent information.
4567  *
4568  * Returns:
4569  * overall lenth of the data read into the internal buffer.
4570  **/
4571 static int
4572 lpfc_idiag_extacc_avail_get(struct lpfc_hba *phba, char *pbuffer, int len)
4573 {
4574         uint16_t ext_cnt, ext_size;
4575
4576         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4577                         "\nAvailable Extents Information:\n");
4578
4579         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4580                         "\tPort Available VPI extents: ");
4581         lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VPI,
4582                                        &ext_cnt, &ext_size);
4583         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4584                         "Count %3d, Size %3d\n", ext_cnt, ext_size);
4585
4586         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4587                         "\tPort Available VFI extents: ");
4588         lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_VFI,
4589                                        &ext_cnt, &ext_size);
4590         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4591                         "Count %3d, Size %3d\n", ext_cnt, ext_size);
4592
4593         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4594                         "\tPort Available RPI extents: ");
4595         lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_RPI,
4596                                        &ext_cnt, &ext_size);
4597         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4598                         "Count %3d, Size %3d\n", ext_cnt, ext_size);
4599
4600         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4601                         "\tPort Available XRI extents: ");
4602         lpfc_sli4_get_avail_extnt_rsrc(phba, LPFC_RSC_TYPE_FCOE_XRI,
4603                                        &ext_cnt, &ext_size);
4604         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4605                         "Count %3d, Size %3d\n", ext_cnt, ext_size);
4606
4607         return len;
4608 }
4609
4610 /**
4611  * lpfc_idiag_extacc_alloc_get - get the allocated extents information
4612  * @phba: pointer to lpfc hba data structure.
4613  * @pbuffer: pointer to internal buffer.
4614  * @len: length into the internal buffer data has been copied.
4615  *
4616  * Description:
4617  * This routine is to get the allocated extent information.
4618  *
4619  * Returns:
4620  * overall lenth of the data read into the internal buffer.
4621  **/
4622 static int
4623 lpfc_idiag_extacc_alloc_get(struct lpfc_hba *phba, char *pbuffer, int len)
4624 {
4625         uint16_t ext_cnt, ext_size;
4626         int rc;
4627
4628         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4629                         "\nAllocated Extents Information:\n");
4630
4631         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4632                         "\tHost Allocated VPI extents: ");
4633         rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VPI,
4634                                             &ext_cnt, &ext_size);
4635         if (!rc)
4636                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4637                                 "Port %d Extent %3d, Size %3d\n",
4638                                 phba->brd_no, ext_cnt, ext_size);
4639         else
4640                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4641                                 "N/A\n");
4642
4643         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4644                         "\tHost Allocated VFI extents: ");
4645         rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_VFI,
4646                                             &ext_cnt, &ext_size);
4647         if (!rc)
4648                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4649                                 "Port %d Extent %3d, Size %3d\n",
4650                                 phba->brd_no, ext_cnt, ext_size);
4651         else
4652                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4653                                 "N/A\n");
4654
4655         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4656                         "\tHost Allocated RPI extents: ");
4657         rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_RPI,
4658                                             &ext_cnt, &ext_size);
4659         if (!rc)
4660                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4661                                 "Port %d Extent %3d, Size %3d\n",
4662                                 phba->brd_no, ext_cnt, ext_size);
4663         else
4664                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4665                                 "N/A\n");
4666
4667         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4668                         "\tHost Allocated XRI extents: ");
4669         rc = lpfc_sli4_get_allocated_extnts(phba, LPFC_RSC_TYPE_FCOE_XRI,
4670                                             &ext_cnt, &ext_size);
4671         if (!rc)
4672                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4673                                 "Port %d Extent %3d, Size %3d\n",
4674                                 phba->brd_no, ext_cnt, ext_size);
4675         else
4676                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4677                                 "N/A\n");
4678
4679         return len;
4680 }
4681
4682 /**
4683  * lpfc_idiag_extacc_drivr_get - get driver extent information
4684  * @phba: pointer to lpfc hba data structure.
4685  * @pbuffer: pointer to internal buffer.
4686  * @len: length into the internal buffer data has been copied.
4687  *
4688  * Description:
4689  * This routine is to get the driver extent information.
4690  *
4691  * Returns:
4692  * overall lenth of the data read into the internal buffer.
4693  **/
4694 static int
4695 lpfc_idiag_extacc_drivr_get(struct lpfc_hba *phba, char *pbuffer, int len)
4696 {
4697         struct lpfc_rsrc_blks *rsrc_blks;
4698         int index;
4699
4700         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4701                         "\nDriver Extents Information:\n");
4702
4703         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4704                         "\tVPI extents:\n");
4705         index = 0;
4706         list_for_each_entry(rsrc_blks, &phba->lpfc_vpi_blk_list, list) {
4707                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4708                                 "\t\tBlock %3d: Start %4d, Count %4d\n",
4709                                 index, rsrc_blks->rsrc_start,
4710                                 rsrc_blks->rsrc_size);
4711                 index++;
4712         }
4713         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4714                         "\tVFI extents:\n");
4715         index = 0;
4716         list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_vfi_blk_list,
4717                             list) {
4718                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4719                                 "\t\tBlock %3d: Start %4d, Count %4d\n",
4720                                 index, rsrc_blks->rsrc_start,
4721                                 rsrc_blks->rsrc_size);
4722                 index++;
4723         }
4724
4725         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4726                         "\tRPI extents:\n");
4727         index = 0;
4728         list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_rpi_blk_list,
4729                             list) {
4730                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4731                                 "\t\tBlock %3d: Start %4d, Count %4d\n",
4732                                 index, rsrc_blks->rsrc_start,
4733                                 rsrc_blks->rsrc_size);
4734                 index++;
4735         }
4736
4737         len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4738                         "\tXRI extents:\n");
4739         index = 0;
4740         list_for_each_entry(rsrc_blks, &phba->sli4_hba.lpfc_xri_blk_list,
4741                             list) {
4742                 len += snprintf(pbuffer+len, LPFC_EXT_ACC_BUF_SIZE-len,
4743                                 "\t\tBlock %3d: Start %4d, Count %4d\n",
4744                                 index, rsrc_blks->rsrc_start,
4745                                 rsrc_blks->rsrc_size);
4746                 index++;
4747         }
4748
4749         return len;
4750 }
4751
4752 /**
4753  * lpfc_idiag_extacc_write - Syntax check and set up idiag extacc commands
4754  * @file: The file pointer to read from.
4755  * @buf: The buffer to copy the user data from.
4756  * @nbytes: The number of bytes to get.
4757  * @ppos: The position in the file to start reading from.
4758  *
4759  * This routine get the debugfs idiag command struct from user space and then
4760  * perform the syntax check for extent information access commands and sets
4761  * up the necessary states in the idiag command struct accordingly.
4762  *
4763  * It returns the @nbytges passing in from debugfs user space when successful.
4764  * In case of error conditions, it returns proper error code back to the user
4765  * space.
4766  **/
4767 static ssize_t
4768 lpfc_idiag_extacc_write(struct file *file, const char __user *buf,
4769                         size_t nbytes, loff_t *ppos)
4770 {
4771         struct lpfc_debug *debug = file->private_data;
4772         uint32_t ext_map;
4773         int rc;
4774
4775         /* This is a user write operation */
4776         debug->op = LPFC_IDIAG_OP_WR;
4777
4778         rc = lpfc_idiag_cmd_get(buf, nbytes, &idiag.cmd);
4779         if (rc < 0)
4780                 return rc;
4781
4782         ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
4783
4784         if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
4785                 goto error_out;
4786         if (rc != LPFC_EXT_ACC_CMD_ARG)
4787                 goto error_out;
4788         if (!(ext_map & LPFC_EXT_ACC_ALL))
4789                 goto error_out;
4790
4791         return nbytes;
4792 error_out:
4793         /* Clean out command structure on command error out */
4794         memset(&idiag, 0, sizeof(idiag));
4795         return -EINVAL;
4796 }
4797
4798 /**
4799  * lpfc_idiag_extacc_read - idiag debugfs read access to extent information
4800  * @file: The file pointer to read from.
4801  * @buf: The buffer to copy the data to.
4802  * @nbytes: The number of bytes to read.
4803  * @ppos: The position in the file to start reading from.
4804  *
4805  * Description:
4806  * This routine reads data from the proper extent information according to
4807  * the idiag command, and copies to user @buf.
4808  *
4809  * Returns:
4810  * This function returns the amount of data that was read (this could be less
4811  * than @nbytes if the end of the file was reached) or a negative error value.
4812  **/
4813 static ssize_t
4814 lpfc_idiag_extacc_read(struct file *file, char __user *buf, size_t nbytes,
4815                        loff_t *ppos)
4816 {
4817         struct lpfc_debug *debug = file->private_data;
4818         struct lpfc_hba *phba = (struct lpfc_hba *)debug->i_private;
4819         char *pbuffer;
4820         uint32_t ext_map;
4821         int len = 0;
4822
4823         /* This is a user read operation */
4824         debug->op = LPFC_IDIAG_OP_RD;
4825
4826         if (!debug->buffer)
4827                 debug->buffer = kmalloc(LPFC_EXT_ACC_BUF_SIZE, GFP_KERNEL);
4828         if (!debug->buffer)
4829                 return 0;
4830         pbuffer = debug->buffer;
4831         if (*ppos)
4832                 return 0;
4833         if (idiag.cmd.opcode != LPFC_IDIAG_CMD_EXTACC_RD)
4834                 return 0;
4835
4836         ext_map = idiag.cmd.data[IDIAG_EXTACC_EXMAP_INDX];
4837         if (ext_map & LPFC_EXT_ACC_AVAIL)
4838                 len = lpfc_idiag_extacc_avail_get(phba, pbuffer, len);
4839         if (ext_map & LPFC_EXT_ACC_ALLOC)
4840                 len = lpfc_idiag_extacc_alloc_get(phba, pbuffer, len);
4841         if (ext_map & LPFC_EXT_ACC_DRIVR)
4842                 len = lpfc_idiag_extacc_drivr_get(phba, pbuffer, len);
4843
4844         return simple_read_from_buffer(buf, nbytes, ppos, pbuffer, len);
4845 }
4846
4847 #undef lpfc_debugfs_op_disc_trc
4848 static const struct file_operations lpfc_debugfs_op_disc_trc = {
4849         .owner =        THIS_MODULE,
4850         .open =         lpfc_debugfs_disc_trc_open,
4851         .llseek =       lpfc_debugfs_lseek,
4852         .read =         lpfc_debugfs_read,
4853         .release =      lpfc_debugfs_release,
4854 };
4855
4856 #undef lpfc_debugfs_op_nodelist
4857 static const struct file_operations lpfc_debugfs_op_nodelist = {
4858         .owner =        THIS_MODULE,
4859         .open =         lpfc_debugfs_nodelist_open,
4860         .llseek =       lpfc_debugfs_lseek,
4861         .read =         lpfc_debugfs_read,
4862         .release =      lpfc_debugfs_release,
4863 };
4864
4865 #undef lpfc_debugfs_op_hbqinfo
4866 static const struct file_operations lpfc_debugfs_op_hbqinfo = {
4867         .owner =        THIS_MODULE,
4868         .open =         lpfc_debugfs_hbqinfo_open,
4869         .llseek =       lpfc_debugfs_lseek,
4870         .read =         lpfc_debugfs_read,
4871         .release =      lpfc_debugfs_release,
4872 };
4873
4874 #undef lpfc_debugfs_op_dumpHBASlim
4875 static const struct file_operations lpfc_debugfs_op_dumpHBASlim = {
4876         .owner =        THIS_MODULE,
4877         .open =         lpfc_debugfs_dumpHBASlim_open,
4878         .llseek =       lpfc_debugfs_lseek,
4879         .read =         lpfc_debugfs_read,
4880         .release =      lpfc_debugfs_release,
4881 };
4882
4883 #undef lpfc_debugfs_op_dumpHostSlim
4884 static const struct file_operations lpfc_debugfs_op_dumpHostSlim = {
4885         .owner =        THIS_MODULE,
4886         .open =         lpfc_debugfs_dumpHostSlim_open,
4887         .llseek =       lpfc_debugfs_lseek,
4888         .read =         lpfc_debugfs_read,
4889         .release =      lpfc_debugfs_release,
4890 };
4891
4892 #undef lpfc_debugfs_op_nvmestat
4893 static const struct file_operations lpfc_debugfs_op_nvmestat = {
4894         .owner =        THIS_MODULE,
4895         .open =         lpfc_debugfs_nvmestat_open,
4896         .llseek =       lpfc_debugfs_lseek,
4897         .read =         lpfc_debugfs_read,
4898         .write =        lpfc_debugfs_nvmestat_write,
4899         .release =      lpfc_debugfs_release,
4900 };
4901
4902 #undef lpfc_debugfs_op_nvmektime
4903 static const struct file_operations lpfc_debugfs_op_nvmektime = {
4904         .owner =        THIS_MODULE,
4905         .open =         lpfc_debugfs_nvmektime_open,
4906         .llseek =       lpfc_debugfs_lseek,
4907         .read =         lpfc_debugfs_read,
4908         .write =        lpfc_debugfs_nvmektime_write,
4909         .release =      lpfc_debugfs_release,
4910 };
4911
4912 #undef lpfc_debugfs_op_nvmeio_trc
4913 static const struct file_operations lpfc_debugfs_op_nvmeio_trc = {
4914         .owner =        THIS_MODULE,
4915         .open =         lpfc_debugfs_nvmeio_trc_open,
4916         .llseek =       lpfc_debugfs_lseek,
4917         .read =         lpfc_debugfs_read,
4918         .write =        lpfc_debugfs_nvmeio_trc_write,
4919         .release =      lpfc_debugfs_release,
4920 };
4921
4922 #undef lpfc_debugfs_op_cpucheck
4923 static const struct file_operations lpfc_debugfs_op_cpucheck = {
4924         .owner =        THIS_MODULE,
4925         .open =         lpfc_debugfs_cpucheck_open,
4926         .llseek =       lpfc_debugfs_lseek,
4927         .read =         lpfc_debugfs_read,
4928         .write =        lpfc_debugfs_cpucheck_write,
4929         .release =      lpfc_debugfs_release,
4930 };
4931
4932 #undef lpfc_debugfs_op_dumpData
4933 static const struct file_operations lpfc_debugfs_op_dumpData = {
4934         .owner =        THIS_MODULE,
4935         .open =         lpfc_debugfs_dumpData_open,
4936         .llseek =       lpfc_debugfs_lseek,
4937         .read =         lpfc_debugfs_read,
4938         .write =        lpfc_debugfs_dumpDataDif_write,
4939         .release =      lpfc_debugfs_dumpDataDif_release,
4940 };
4941
4942 #undef lpfc_debugfs_op_dumpDif
4943 static const struct file_operations lpfc_debugfs_op_dumpDif = {
4944         .owner =        THIS_MODULE,
4945         .open =         lpfc_debugfs_dumpDif_open,
4946         .llseek =       lpfc_debugfs_lseek,
4947         .read =         lpfc_debugfs_read,
4948         .write =        lpfc_debugfs_dumpDataDif_write,
4949         .release =      lpfc_debugfs_dumpDataDif_release,
4950 };
4951
4952 #undef lpfc_debugfs_op_dif_err
4953 static const struct file_operations lpfc_debugfs_op_dif_err = {
4954         .owner =        THIS_MODULE,
4955         .open =         simple_open,
4956         .llseek =       lpfc_debugfs_lseek,
4957         .read =         lpfc_debugfs_dif_err_read,
4958         .write =        lpfc_debugfs_dif_err_write,
4959         .release =      lpfc_debugfs_dif_err_release,
4960 };
4961
4962 #undef lpfc_debugfs_op_slow_ring_trc
4963 static const struct file_operations lpfc_debugfs_op_slow_ring_trc = {
4964         .owner =        THIS_MODULE,
4965         .open =         lpfc_debugfs_slow_ring_trc_open,
4966         .llseek =       lpfc_debugfs_lseek,
4967         .read =         lpfc_debugfs_read,
4968         .release =      lpfc_debugfs_release,
4969 };
4970
4971 static struct dentry *lpfc_debugfs_root = NULL;
4972 static atomic_t lpfc_debugfs_hba_count;
4973
4974 /*
4975  * File operations for the iDiag debugfs
4976  */
4977 #undef lpfc_idiag_op_pciCfg
4978 static const struct file_operations lpfc_idiag_op_pciCfg = {
4979         .owner =        THIS_MODULE,
4980         .open =         lpfc_idiag_open,
4981         .llseek =       lpfc_debugfs_lseek,
4982         .read =         lpfc_idiag_pcicfg_read,
4983         .write =        lpfc_idiag_pcicfg_write,
4984         .release =      lpfc_idiag_cmd_release,
4985 };
4986
4987 #undef lpfc_idiag_op_barAcc
4988 static const struct file_operations lpfc_idiag_op_barAcc = {
4989         .owner =        THIS_MODULE,
4990         .open =         lpfc_idiag_open,
4991         .llseek =       lpfc_debugfs_lseek,
4992         .read =         lpfc_idiag_baracc_read,
4993         .write =        lpfc_idiag_baracc_write,
4994         .release =      lpfc_idiag_cmd_release,
4995 };
4996
4997 #undef lpfc_idiag_op_queInfo
4998 static const struct file_operations lpfc_idiag_op_queInfo = {
4999         .owner =        THIS_MODULE,
5000         .open =         lpfc_idiag_open,
5001         .read =         lpfc_idiag_queinfo_read,
5002         .release =      lpfc_idiag_release,
5003 };
5004
5005 #undef lpfc_idiag_op_queAcc
5006 static const struct file_operations lpfc_idiag_op_queAcc = {
5007         .owner =        THIS_MODULE,
5008         .open =         lpfc_idiag_open,
5009         .llseek =       lpfc_debugfs_lseek,
5010         .read =         lpfc_idiag_queacc_read,
5011         .write =        lpfc_idiag_queacc_write,
5012         .release =      lpfc_idiag_cmd_release,
5013 };
5014
5015 #undef lpfc_idiag_op_drbAcc
5016 static const struct file_operations lpfc_idiag_op_drbAcc = {
5017         .owner =        THIS_MODULE,
5018         .open =         lpfc_idiag_open,
5019         .llseek =       lpfc_debugfs_lseek,
5020         .read =         lpfc_idiag_drbacc_read,
5021         .write =        lpfc_idiag_drbacc_write,
5022         .release =      lpfc_idiag_cmd_release,
5023 };
5024
5025 #undef lpfc_idiag_op_ctlAcc
5026 static const struct file_operations lpfc_idiag_op_ctlAcc = {
5027         .owner =        THIS_MODULE,
5028         .open =         lpfc_idiag_open,
5029         .llseek =       lpfc_debugfs_lseek,
5030         .read =         lpfc_idiag_ctlacc_read,
5031         .write =        lpfc_idiag_ctlacc_write,
5032         .release =      lpfc_idiag_cmd_release,
5033 };
5034
5035 #undef lpfc_idiag_op_mbxAcc
5036 static const struct file_operations lpfc_idiag_op_mbxAcc = {
5037         .owner =        THIS_MODULE,
5038         .open =         lpfc_idiag_open,
5039         .llseek =       lpfc_debugfs_lseek,
5040         .read =         lpfc_idiag_mbxacc_read,
5041         .write =        lpfc_idiag_mbxacc_write,
5042         .release =      lpfc_idiag_cmd_release,
5043 };
5044
5045 #undef lpfc_idiag_op_extAcc
5046 static const struct file_operations lpfc_idiag_op_extAcc = {
5047         .owner =        THIS_MODULE,
5048         .open =         lpfc_idiag_open,
5049         .llseek =       lpfc_debugfs_lseek,
5050         .read =         lpfc_idiag_extacc_read,
5051         .write =        lpfc_idiag_extacc_write,
5052         .release =      lpfc_idiag_cmd_release,
5053 };
5054
5055 #endif
5056
5057 /* lpfc_idiag_mbxacc_dump_bsg_mbox - idiag debugfs dump bsg mailbox command
5058  * @phba: Pointer to HBA context object.
5059  * @dmabuf: Pointer to a DMA buffer descriptor.
5060  *
5061  * Description:
5062  * This routine dump a bsg pass-through non-embedded mailbox command with
5063  * external buffer.
5064  **/
5065 void
5066 lpfc_idiag_mbxacc_dump_bsg_mbox(struct lpfc_hba *phba, enum nemb_type nemb_tp,
5067                                 enum mbox_type mbox_tp, enum dma_type dma_tp,
5068                                 enum sta_type sta_tp,
5069                                 struct lpfc_dmabuf *dmabuf, uint32_t ext_buf)
5070 {
5071 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5072         uint32_t *mbx_mbox_cmd, *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt;
5073         char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5074         int len = 0;
5075         uint32_t do_dump = 0;
5076         uint32_t *pword;
5077         uint32_t i;
5078
5079         if (idiag.cmd.opcode != LPFC_IDIAG_BSG_MBXACC_DP)
5080                 return;
5081
5082         mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5083         mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5084         mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5085         mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5086
5087         if (!(*mbx_dump_map & LPFC_MBX_DMP_ALL) ||
5088             (*mbx_dump_cnt == 0) ||
5089             (*mbx_word_cnt == 0))
5090                 return;
5091
5092         if (*mbx_mbox_cmd != 0x9B)
5093                 return;
5094
5095         if ((mbox_tp == mbox_rd) && (dma_tp == dma_mbox)) {
5096                 if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_MBX) {
5097                         do_dump |= LPFC_BSG_DMP_MBX_RD_MBX;
5098                         pr_err("\nRead mbox command (x%x), "
5099                                "nemb:0x%x, extbuf_cnt:%d:\n",
5100                                sta_tp, nemb_tp, ext_buf);
5101                 }
5102         }
5103         if ((mbox_tp == mbox_rd) && (dma_tp == dma_ebuf)) {
5104                 if (*mbx_dump_map & LPFC_BSG_DMP_MBX_RD_BUF) {
5105                         do_dump |= LPFC_BSG_DMP_MBX_RD_BUF;
5106                         pr_err("\nRead mbox buffer (x%x), "
5107                                "nemb:0x%x, extbuf_seq:%d:\n",
5108                                sta_tp, nemb_tp, ext_buf);
5109                 }
5110         }
5111         if ((mbox_tp == mbox_wr) && (dma_tp == dma_mbox)) {
5112                 if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_MBX) {
5113                         do_dump |= LPFC_BSG_DMP_MBX_WR_MBX;
5114                         pr_err("\nWrite mbox command (x%x), "
5115                                "nemb:0x%x, extbuf_cnt:%d:\n",
5116                                sta_tp, nemb_tp, ext_buf);
5117                 }
5118         }
5119         if ((mbox_tp == mbox_wr) && (dma_tp == dma_ebuf)) {
5120                 if (*mbx_dump_map & LPFC_BSG_DMP_MBX_WR_BUF) {
5121                         do_dump |= LPFC_BSG_DMP_MBX_WR_BUF;
5122                         pr_err("\nWrite mbox buffer (x%x), "
5123                                "nemb:0x%x, extbuf_seq:%d:\n",
5124                                sta_tp, nemb_tp, ext_buf);
5125                 }
5126         }
5127
5128         /* dump buffer content */
5129         if (do_dump) {
5130                 pword = (uint32_t *)dmabuf->virt;
5131                 for (i = 0; i < *mbx_word_cnt; i++) {
5132                         if (!(i % 8)) {
5133                                 if (i != 0)
5134                                         pr_err("%s\n", line_buf);
5135                                 len = 0;
5136                                 len += snprintf(line_buf+len,
5137                                                 LPFC_MBX_ACC_LBUF_SZ-len,
5138                                                 "%03d: ", i);
5139                         }
5140                         len += snprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5141                                         "%08x ", (uint32_t)*pword);
5142                         pword++;
5143                 }
5144                 if ((i - 1) % 8)
5145                         pr_err("%s\n", line_buf);
5146                 (*mbx_dump_cnt)--;
5147         }
5148
5149         /* Clean out command structure on reaching dump count */
5150         if (*mbx_dump_cnt == 0)
5151                 memset(&idiag, 0, sizeof(idiag));
5152         return;
5153 #endif
5154 }
5155
5156 /* lpfc_idiag_mbxacc_dump_issue_mbox - idiag debugfs dump issue mailbox command
5157  * @phba: Pointer to HBA context object.
5158  * @dmabuf: Pointer to a DMA buffer descriptor.
5159  *
5160  * Description:
5161  * This routine dump a pass-through non-embedded mailbox command from issue
5162  * mailbox command.
5163  **/
5164 void
5165 lpfc_idiag_mbxacc_dump_issue_mbox(struct lpfc_hba *phba, MAILBOX_t *pmbox)
5166 {
5167 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5168         uint32_t *mbx_dump_map, *mbx_dump_cnt, *mbx_word_cnt, *mbx_mbox_cmd;
5169         char line_buf[LPFC_MBX_ACC_LBUF_SZ];
5170         int len = 0;
5171         uint32_t *pword;
5172         uint8_t *pbyte;
5173         uint32_t i, j;
5174
5175         if (idiag.cmd.opcode != LPFC_IDIAG_CMD_MBXACC_DP)
5176                 return;
5177
5178         mbx_mbox_cmd = &idiag.cmd.data[IDIAG_MBXACC_MBCMD_INDX];
5179         mbx_dump_map = &idiag.cmd.data[IDIAG_MBXACC_DPMAP_INDX];
5180         mbx_dump_cnt = &idiag.cmd.data[IDIAG_MBXACC_DPCNT_INDX];
5181         mbx_word_cnt = &idiag.cmd.data[IDIAG_MBXACC_WDCNT_INDX];
5182
5183         if (!(*mbx_dump_map & LPFC_MBX_DMP_MBX_ALL) ||
5184             (*mbx_dump_cnt == 0) ||
5185             (*mbx_word_cnt == 0))
5186                 return;
5187
5188         if ((*mbx_mbox_cmd != LPFC_MBX_ALL_CMD) &&
5189             (*mbx_mbox_cmd != pmbox->mbxCommand))
5190                 return;
5191
5192         /* dump buffer content */
5193         if (*mbx_dump_map & LPFC_MBX_DMP_MBX_WORD) {
5194                 pr_err("Mailbox command:0x%x dump by word:\n",
5195                        pmbox->mbxCommand);
5196                 pword = (uint32_t *)pmbox;
5197                 for (i = 0; i < *mbx_word_cnt; i++) {
5198                         if (!(i % 8)) {
5199                                 if (i != 0)
5200                                         pr_err("%s\n", line_buf);
5201                                 len = 0;
5202                                 memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5203                                 len += snprintf(line_buf+len,
5204                                                 LPFC_MBX_ACC_LBUF_SZ-len,
5205                                                 "%03d: ", i);
5206                         }
5207                         len += snprintf(line_buf+len, LPFC_MBX_ACC_LBUF_SZ-len,
5208                                         "%08x ",
5209                                         ((uint32_t)*pword) & 0xffffffff);
5210                         pword++;
5211                 }
5212                 if ((i - 1) % 8)
5213                         pr_err("%s\n", line_buf);
5214                 pr_err("\n");
5215         }
5216         if (*mbx_dump_map & LPFC_MBX_DMP_MBX_BYTE) {
5217                 pr_err("Mailbox command:0x%x dump by byte:\n",
5218                        pmbox->mbxCommand);
5219                 pbyte = (uint8_t *)pmbox;
5220                 for (i = 0; i < *mbx_word_cnt; i++) {
5221                         if (!(i % 8)) {
5222                                 if (i != 0)
5223                                         pr_err("%s\n", line_buf);
5224                                 len = 0;
5225                                 memset(line_buf, 0, LPFC_MBX_ACC_LBUF_SZ);
5226                                 len += snprintf(line_buf+len,
5227                                                 LPFC_MBX_ACC_LBUF_SZ-len,
5228                                                 "%03d: ", i);
5229                         }
5230                         for (j = 0; j < 4; j++) {
5231                                 len += snprintf(line_buf+len,
5232                                                 LPFC_MBX_ACC_LBUF_SZ-len,
5233                                                 "%02x",
5234                                                 ((uint8_t)*pbyte) & 0xff);
5235                                 pbyte++;
5236                         }
5237                         len += snprintf(line_buf+len,
5238                                         LPFC_MBX_ACC_LBUF_SZ-len, " ");
5239                 }
5240                 if ((i - 1) % 8)
5241                         pr_err("%s\n", line_buf);
5242                 pr_err("\n");
5243         }
5244         (*mbx_dump_cnt)--;
5245
5246         /* Clean out command structure on reaching dump count */
5247         if (*mbx_dump_cnt == 0)
5248                 memset(&idiag, 0, sizeof(idiag));
5249         return;
5250 #endif
5251 }
5252
5253 /**
5254  * lpfc_debugfs_initialize - Initialize debugfs for a vport
5255  * @vport: The vport pointer to initialize.
5256  *
5257  * Description:
5258  * When Debugfs is configured this routine sets up the lpfc debugfs file system.
5259  * If not already created, this routine will create the lpfc directory, and
5260  * lpfcX directory (for this HBA), and vportX directory for this vport. It will
5261  * also create each file used to access lpfc specific debugfs information.
5262  **/
5263 inline void
5264 lpfc_debugfs_initialize(struct lpfc_vport *vport)
5265 {
5266 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5267         struct lpfc_hba   *phba = vport->phba;
5268         char name[64];
5269         uint32_t num, i;
5270         bool pport_setup = false;
5271
5272         if (!lpfc_debugfs_enable)
5273                 return;
5274
5275         /* Setup lpfc root directory */
5276         if (!lpfc_debugfs_root) {
5277                 lpfc_debugfs_root = debugfs_create_dir("lpfc", NULL);
5278                 atomic_set(&lpfc_debugfs_hba_count, 0);
5279                 if (!lpfc_debugfs_root) {
5280                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5281                                          "0408 Cannot create debugfs root\n");
5282                         goto debug_failed;
5283                 }
5284         }
5285         if (!lpfc_debugfs_start_time)
5286                 lpfc_debugfs_start_time = jiffies;
5287
5288         /* Setup funcX directory for specific HBA PCI function */
5289         snprintf(name, sizeof(name), "fn%d", phba->brd_no);
5290         if (!phba->hba_debugfs_root) {
5291                 pport_setup = true;
5292                 phba->hba_debugfs_root =
5293                         debugfs_create_dir(name, lpfc_debugfs_root);
5294                 if (!phba->hba_debugfs_root) {
5295                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5296                                          "0412 Cannot create debugfs hba\n");
5297                         goto debug_failed;
5298                 }
5299                 atomic_inc(&lpfc_debugfs_hba_count);
5300                 atomic_set(&phba->debugfs_vport_count, 0);
5301
5302                 /* Setup hbqinfo */
5303                 snprintf(name, sizeof(name), "hbqinfo");
5304                 phba->debug_hbqinfo =
5305                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5306                                  phba->hba_debugfs_root,
5307                                  phba, &lpfc_debugfs_op_hbqinfo);
5308                 if (!phba->debug_hbqinfo) {
5309                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5310                                 "0411 Cannot create debugfs hbqinfo\n");
5311                         goto debug_failed;
5312                 }
5313
5314                 /* Setup dumpHBASlim */
5315                 if (phba->sli_rev < LPFC_SLI_REV4) {
5316                         snprintf(name, sizeof(name), "dumpHBASlim");
5317                         phba->debug_dumpHBASlim =
5318                                 debugfs_create_file(name,
5319                                         S_IFREG|S_IRUGO|S_IWUSR,
5320                                         phba->hba_debugfs_root,
5321                                         phba, &lpfc_debugfs_op_dumpHBASlim);
5322                         if (!phba->debug_dumpHBASlim) {
5323                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5324                                                  "0413 Cannot create debugfs "
5325                                                 "dumpHBASlim\n");
5326                                 goto debug_failed;
5327                         }
5328                 } else
5329                         phba->debug_dumpHBASlim = NULL;
5330
5331                 /* Setup dumpHostSlim */
5332                 if (phba->sli_rev < LPFC_SLI_REV4) {
5333                         snprintf(name, sizeof(name), "dumpHostSlim");
5334                         phba->debug_dumpHostSlim =
5335                                 debugfs_create_file(name,
5336                                         S_IFREG|S_IRUGO|S_IWUSR,
5337                                         phba->hba_debugfs_root,
5338                                         phba, &lpfc_debugfs_op_dumpHostSlim);
5339                         if (!phba->debug_dumpHostSlim) {
5340                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5341                                                  "0414 Cannot create debugfs "
5342                                                  "dumpHostSlim\n");
5343                                 goto debug_failed;
5344                         }
5345                 } else
5346                         phba->debug_dumpHostSlim = NULL;
5347
5348                 /* Setup dumpData */
5349                 snprintf(name, sizeof(name), "dumpData");
5350                 phba->debug_dumpData =
5351                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5352                                  phba->hba_debugfs_root,
5353                                  phba, &lpfc_debugfs_op_dumpData);
5354                 if (!phba->debug_dumpData) {
5355                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5356                                 "0800 Cannot create debugfs dumpData\n");
5357                         goto debug_failed;
5358                 }
5359
5360                 /* Setup dumpDif */
5361                 snprintf(name, sizeof(name), "dumpDif");
5362                 phba->debug_dumpDif =
5363                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5364                                  phba->hba_debugfs_root,
5365                                  phba, &lpfc_debugfs_op_dumpDif);
5366                 if (!phba->debug_dumpDif) {
5367                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5368                                 "0801 Cannot create debugfs dumpDif\n");
5369                         goto debug_failed;
5370                 }
5371
5372                 /* Setup DIF Error Injections */
5373                 snprintf(name, sizeof(name), "InjErrLBA");
5374                 phba->debug_InjErrLBA =
5375                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5376                         phba->hba_debugfs_root,
5377                         phba, &lpfc_debugfs_op_dif_err);
5378                 if (!phba->debug_InjErrLBA) {
5379                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5380                                 "0807 Cannot create debugfs InjErrLBA\n");
5381                         goto debug_failed;
5382                 }
5383                 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
5384
5385                 snprintf(name, sizeof(name), "InjErrNPortID");
5386                 phba->debug_InjErrNPortID =
5387                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5388                         phba->hba_debugfs_root,
5389                         phba, &lpfc_debugfs_op_dif_err);
5390                 if (!phba->debug_InjErrNPortID) {
5391                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5392                                 "0809 Cannot create debugfs InjErrNPortID\n");
5393                         goto debug_failed;
5394                 }
5395
5396                 snprintf(name, sizeof(name), "InjErrWWPN");
5397                 phba->debug_InjErrWWPN =
5398                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5399                         phba->hba_debugfs_root,
5400                         phba, &lpfc_debugfs_op_dif_err);
5401                 if (!phba->debug_InjErrWWPN) {
5402                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5403                                 "0810 Cannot create debugfs InjErrWWPN\n");
5404                         goto debug_failed;
5405                 }
5406
5407                 snprintf(name, sizeof(name), "writeGuardInjErr");
5408                 phba->debug_writeGuard =
5409                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5410                         phba->hba_debugfs_root,
5411                         phba, &lpfc_debugfs_op_dif_err);
5412                 if (!phba->debug_writeGuard) {
5413                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5414                                 "0802 Cannot create debugfs writeGuard\n");
5415                         goto debug_failed;
5416                 }
5417
5418                 snprintf(name, sizeof(name), "writeAppInjErr");
5419                 phba->debug_writeApp =
5420                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5421                         phba->hba_debugfs_root,
5422                         phba, &lpfc_debugfs_op_dif_err);
5423                 if (!phba->debug_writeApp) {
5424                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5425                                 "0803 Cannot create debugfs writeApp\n");
5426                         goto debug_failed;
5427                 }
5428
5429                 snprintf(name, sizeof(name), "writeRefInjErr");
5430                 phba->debug_writeRef =
5431                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5432                         phba->hba_debugfs_root,
5433                         phba, &lpfc_debugfs_op_dif_err);
5434                 if (!phba->debug_writeRef) {
5435                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5436                                 "0804 Cannot create debugfs writeRef\n");
5437                         goto debug_failed;
5438                 }
5439
5440                 snprintf(name, sizeof(name), "readGuardInjErr");
5441                 phba->debug_readGuard =
5442                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5443                         phba->hba_debugfs_root,
5444                         phba, &lpfc_debugfs_op_dif_err);
5445                 if (!phba->debug_readGuard) {
5446                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5447                                 "0808 Cannot create debugfs readGuard\n");
5448                         goto debug_failed;
5449                 }
5450
5451                 snprintf(name, sizeof(name), "readAppInjErr");
5452                 phba->debug_readApp =
5453                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5454                         phba->hba_debugfs_root,
5455                         phba, &lpfc_debugfs_op_dif_err);
5456                 if (!phba->debug_readApp) {
5457                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5458                                 "0805 Cannot create debugfs readApp\n");
5459                         goto debug_failed;
5460                 }
5461
5462                 snprintf(name, sizeof(name), "readRefInjErr");
5463                 phba->debug_readRef =
5464                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5465                         phba->hba_debugfs_root,
5466                         phba, &lpfc_debugfs_op_dif_err);
5467                 if (!phba->debug_readRef) {
5468                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5469                                 "0806 Cannot create debugfs readApp\n");
5470                         goto debug_failed;
5471                 }
5472
5473                 /* Setup slow ring trace */
5474                 if (lpfc_debugfs_max_slow_ring_trc) {
5475                         num = lpfc_debugfs_max_slow_ring_trc - 1;
5476                         if (num & lpfc_debugfs_max_slow_ring_trc) {
5477                                 /* Change to be a power of 2 */
5478                                 num = lpfc_debugfs_max_slow_ring_trc;
5479                                 i = 0;
5480                                 while (num > 1) {
5481                                         num = num >> 1;
5482                                         i++;
5483                                 }
5484                                 lpfc_debugfs_max_slow_ring_trc = (1 << i);
5485                                 pr_err("lpfc_debugfs_max_disc_trc changed to "
5486                                        "%d\n", lpfc_debugfs_max_disc_trc);
5487                         }
5488                 }
5489
5490                 snprintf(name, sizeof(name), "slow_ring_trace");
5491                 phba->debug_slow_ring_trc =
5492                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5493                                  phba->hba_debugfs_root,
5494                                  phba, &lpfc_debugfs_op_slow_ring_trc);
5495                 if (!phba->debug_slow_ring_trc) {
5496                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5497                                          "0415 Cannot create debugfs "
5498                                          "slow_ring_trace\n");
5499                         goto debug_failed;
5500                 }
5501                 if (!phba->slow_ring_trc) {
5502                         phba->slow_ring_trc = kmalloc(
5503                                 (sizeof(struct lpfc_debugfs_trc) *
5504                                 lpfc_debugfs_max_slow_ring_trc),
5505                                 GFP_KERNEL);
5506                         if (!phba->slow_ring_trc) {
5507                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5508                                                  "0416 Cannot create debugfs "
5509                                                  "slow_ring buffer\n");
5510                                 goto debug_failed;
5511                         }
5512                         atomic_set(&phba->slow_ring_trc_cnt, 0);
5513                         memset(phba->slow_ring_trc, 0,
5514                                 (sizeof(struct lpfc_debugfs_trc) *
5515                                 lpfc_debugfs_max_slow_ring_trc));
5516                 }
5517
5518                 snprintf(name, sizeof(name), "nvmeio_trc");
5519                 phba->debug_nvmeio_trc =
5520                         debugfs_create_file(name, 0644,
5521                                             phba->hba_debugfs_root,
5522                                             phba, &lpfc_debugfs_op_nvmeio_trc);
5523                 if (!phba->debug_nvmeio_trc) {
5524                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5525                                          "0574 No create debugfs nvmeio_trc\n");
5526                         goto debug_failed;
5527                 }
5528
5529                 atomic_set(&phba->nvmeio_trc_cnt, 0);
5530                 if (lpfc_debugfs_max_nvmeio_trc) {
5531                         num = lpfc_debugfs_max_nvmeio_trc - 1;
5532                         if (num & lpfc_debugfs_max_disc_trc) {
5533                                 /* Change to be a power of 2 */
5534                                 num = lpfc_debugfs_max_nvmeio_trc;
5535                                 i = 0;
5536                                 while (num > 1) {
5537                                         num = num >> 1;
5538                                         i++;
5539                                 }
5540                                 lpfc_debugfs_max_nvmeio_trc = (1 << i);
5541                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5542                                                 "0575 lpfc_debugfs_max_nvmeio_trc "
5543                                                 "changed to %d\n",
5544                                                 lpfc_debugfs_max_nvmeio_trc);
5545                         }
5546                         phba->nvmeio_trc_size = lpfc_debugfs_max_nvmeio_trc;
5547
5548                         /* Allocate trace buffer and initialize */
5549                         phba->nvmeio_trc = kzalloc(
5550                                 (sizeof(struct lpfc_debugfs_nvmeio_trc) *
5551                                 phba->nvmeio_trc_size), GFP_KERNEL);
5552
5553                         if (!phba->nvmeio_trc) {
5554                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5555                                                 "0576 Cannot create debugfs "
5556                                                 "nvmeio_trc buffer\n");
5557                                 goto nvmeio_off;
5558                         }
5559                         phba->nvmeio_trc_on = 1;
5560                         phba->nvmeio_trc_output_idx = 0;
5561                         phba->nvmeio_trc = NULL;
5562                 } else {
5563 nvmeio_off:
5564                         phba->nvmeio_trc_size = 0;
5565                         phba->nvmeio_trc_on = 0;
5566                         phba->nvmeio_trc_output_idx = 0;
5567                         phba->nvmeio_trc = NULL;
5568                 }
5569         }
5570
5571         snprintf(name, sizeof(name), "vport%d", vport->vpi);
5572         if (!vport->vport_debugfs_root) {
5573                 vport->vport_debugfs_root =
5574                         debugfs_create_dir(name, phba->hba_debugfs_root);
5575                 if (!vport->vport_debugfs_root) {
5576                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5577                                          "0417 Can't create debugfs\n");
5578                         goto debug_failed;
5579                 }
5580                 atomic_inc(&phba->debugfs_vport_count);
5581         }
5582
5583         if (lpfc_debugfs_max_disc_trc) {
5584                 num = lpfc_debugfs_max_disc_trc - 1;
5585                 if (num & lpfc_debugfs_max_disc_trc) {
5586                         /* Change to be a power of 2 */
5587                         num = lpfc_debugfs_max_disc_trc;
5588                         i = 0;
5589                         while (num > 1) {
5590                                 num = num >> 1;
5591                                 i++;
5592                         }
5593                         lpfc_debugfs_max_disc_trc = (1 << i);
5594                         pr_err("lpfc_debugfs_max_disc_trc changed to %d\n",
5595                                lpfc_debugfs_max_disc_trc);
5596                 }
5597         }
5598
5599         vport->disc_trc = kzalloc(
5600                 (sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_disc_trc),
5601                 GFP_KERNEL);
5602
5603         if (!vport->disc_trc) {
5604                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5605                                  "0418 Cannot create debugfs disc trace "
5606                                  "buffer\n");
5607                 goto debug_failed;
5608         }
5609         atomic_set(&vport->disc_trc_cnt, 0);
5610
5611         snprintf(name, sizeof(name), "discovery_trace");
5612         vport->debug_disc_trc =
5613                 debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5614                                  vport->vport_debugfs_root,
5615                                  vport, &lpfc_debugfs_op_disc_trc);
5616         if (!vport->debug_disc_trc) {
5617                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5618                                  "0419 Cannot create debugfs "
5619                                  "discovery_trace\n");
5620                 goto debug_failed;
5621         }
5622         snprintf(name, sizeof(name), "nodelist");
5623         vport->debug_nodelist =
5624                 debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5625                                  vport->vport_debugfs_root,
5626                                  vport, &lpfc_debugfs_op_nodelist);
5627         if (!vport->debug_nodelist) {
5628                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5629                                  "2985 Can't create debugfs nodelist\n");
5630                 goto debug_failed;
5631         }
5632
5633         snprintf(name, sizeof(name), "nvmestat");
5634         vport->debug_nvmestat =
5635                 debugfs_create_file(name, 0644,
5636                                     vport->vport_debugfs_root,
5637                                     vport, &lpfc_debugfs_op_nvmestat);
5638         if (!vport->debug_nvmestat) {
5639                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5640                                  "0811 Cannot create debugfs nvmestat\n");
5641                 goto debug_failed;
5642         }
5643
5644         snprintf(name, sizeof(name), "nvmektime");
5645         vport->debug_nvmektime =
5646                 debugfs_create_file(name, 0644,
5647                                     vport->vport_debugfs_root,
5648                                     vport, &lpfc_debugfs_op_nvmektime);
5649         if (!vport->debug_nvmektime) {
5650                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5651                                  "0815 Cannot create debugfs nvmektime\n");
5652                 goto debug_failed;
5653         }
5654
5655         snprintf(name, sizeof(name), "cpucheck");
5656         vport->debug_cpucheck =
5657                 debugfs_create_file(name, 0644,
5658                                     vport->vport_debugfs_root,
5659                                     vport, &lpfc_debugfs_op_cpucheck);
5660         if (!vport->debug_cpucheck) {
5661                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5662                                  "0819 Cannot create debugfs cpucheck\n");
5663                 goto debug_failed;
5664         }
5665
5666         /*
5667          * The following section is for additional directories/files for the
5668          * physical port.
5669          */
5670
5671         if (!pport_setup)
5672                 goto debug_failed;
5673
5674         /*
5675          * iDiag debugfs root entry points for SLI4 device only
5676          */
5677         if (phba->sli_rev < LPFC_SLI_REV4)
5678                 goto debug_failed;
5679
5680         snprintf(name, sizeof(name), "iDiag");
5681         if (!phba->idiag_root) {
5682                 phba->idiag_root =
5683                         debugfs_create_dir(name, phba->hba_debugfs_root);
5684                 if (!phba->idiag_root) {
5685                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5686                                          "2922 Can't create idiag debugfs\n");
5687                         goto debug_failed;
5688                 }
5689                 /* Initialize iDiag data structure */
5690                 memset(&idiag, 0, sizeof(idiag));
5691         }
5692
5693         /* iDiag read PCI config space */
5694         snprintf(name, sizeof(name), "pciCfg");
5695         if (!phba->idiag_pci_cfg) {
5696                 phba->idiag_pci_cfg =
5697                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5698                                 phba->idiag_root, phba, &lpfc_idiag_op_pciCfg);
5699                 if (!phba->idiag_pci_cfg) {
5700                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5701                                          "2923 Can't create idiag debugfs\n");
5702                         goto debug_failed;
5703                 }
5704                 idiag.offset.last_rd = 0;
5705         }
5706
5707         /* iDiag PCI BAR access */
5708         snprintf(name, sizeof(name), "barAcc");
5709         if (!phba->idiag_bar_acc) {
5710                 phba->idiag_bar_acc =
5711                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5712                                 phba->idiag_root, phba, &lpfc_idiag_op_barAcc);
5713                 if (!phba->idiag_bar_acc) {
5714                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5715                                         "3056 Can't create idiag debugfs\n");
5716                         goto debug_failed;
5717                 }
5718                 idiag.offset.last_rd = 0;
5719         }
5720
5721         /* iDiag get PCI function queue information */
5722         snprintf(name, sizeof(name), "queInfo");
5723         if (!phba->idiag_que_info) {
5724                 phba->idiag_que_info =
5725                         debugfs_create_file(name, S_IFREG|S_IRUGO,
5726                         phba->idiag_root, phba, &lpfc_idiag_op_queInfo);
5727                 if (!phba->idiag_que_info) {
5728                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5729                                          "2924 Can't create idiag debugfs\n");
5730                         goto debug_failed;
5731                 }
5732         }
5733
5734         /* iDiag access PCI function queue */
5735         snprintf(name, sizeof(name), "queAcc");
5736         if (!phba->idiag_que_acc) {
5737                 phba->idiag_que_acc =
5738                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5739                                 phba->idiag_root, phba, &lpfc_idiag_op_queAcc);
5740                 if (!phba->idiag_que_acc) {
5741                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5742                                          "2926 Can't create idiag debugfs\n");
5743                         goto debug_failed;
5744                 }
5745         }
5746
5747         /* iDiag access PCI function doorbell registers */
5748         snprintf(name, sizeof(name), "drbAcc");
5749         if (!phba->idiag_drb_acc) {
5750                 phba->idiag_drb_acc =
5751                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5752                                 phba->idiag_root, phba, &lpfc_idiag_op_drbAcc);
5753                 if (!phba->idiag_drb_acc) {
5754                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5755                                          "2927 Can't create idiag debugfs\n");
5756                         goto debug_failed;
5757                 }
5758         }
5759
5760         /* iDiag access PCI function control registers */
5761         snprintf(name, sizeof(name), "ctlAcc");
5762         if (!phba->idiag_ctl_acc) {
5763                 phba->idiag_ctl_acc =
5764                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5765                                 phba->idiag_root, phba, &lpfc_idiag_op_ctlAcc);
5766                 if (!phba->idiag_ctl_acc) {
5767                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5768                                          "2981 Can't create idiag debugfs\n");
5769                         goto debug_failed;
5770                 }
5771         }
5772
5773         /* iDiag access mbox commands */
5774         snprintf(name, sizeof(name), "mbxAcc");
5775         if (!phba->idiag_mbx_acc) {
5776                 phba->idiag_mbx_acc =
5777                         debugfs_create_file(name, S_IFREG|S_IRUGO|S_IWUSR,
5778                                 phba->idiag_root, phba, &lpfc_idiag_op_mbxAcc);
5779                 if (!phba->idiag_mbx_acc) {
5780                         lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5781                                         "2980 Can't create idiag debugfs\n");
5782                         goto debug_failed;
5783                 }
5784         }
5785
5786         /* iDiag extents access commands */
5787         if (phba->sli4_hba.extents_in_use) {
5788                 snprintf(name, sizeof(name), "extAcc");
5789                 if (!phba->idiag_ext_acc) {
5790                         phba->idiag_ext_acc =
5791                                 debugfs_create_file(name,
5792                                                     S_IFREG|S_IRUGO|S_IWUSR,
5793                                                     phba->idiag_root, phba,
5794                                                     &lpfc_idiag_op_extAcc);
5795                         if (!phba->idiag_ext_acc) {
5796                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
5797                                                 "2986 Cant create "
5798                                                 "idiag debugfs\n");
5799                                 goto debug_failed;
5800                         }
5801                 }
5802         }
5803
5804 debug_failed:
5805         return;
5806 #endif
5807 }
5808
5809 /**
5810  * lpfc_debugfs_terminate -  Tear down debugfs infrastructure for this vport
5811  * @vport: The vport pointer to remove from debugfs.
5812  *
5813  * Description:
5814  * When Debugfs is configured this routine removes debugfs file system elements
5815  * that are specific to this vport. It also checks to see if there are any
5816  * users left for the debugfs directories associated with the HBA and driver. If
5817  * this is the last user of the HBA directory or driver directory then it will
5818  * remove those from the debugfs infrastructure as well.
5819  **/
5820 inline void
5821 lpfc_debugfs_terminate(struct lpfc_vport *vport)
5822 {
5823 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5824         struct lpfc_hba   *phba = vport->phba;
5825
5826         kfree(vport->disc_trc);
5827         vport->disc_trc = NULL;
5828
5829         debugfs_remove(vport->debug_disc_trc); /* discovery_trace */
5830         vport->debug_disc_trc = NULL;
5831
5832         debugfs_remove(vport->debug_nodelist); /* nodelist */
5833         vport->debug_nodelist = NULL;
5834
5835         debugfs_remove(vport->debug_nvmestat); /* nvmestat */
5836         vport->debug_nvmestat = NULL;
5837
5838         debugfs_remove(vport->debug_nvmektime); /* nvmektime */
5839         vport->debug_nvmektime = NULL;
5840
5841         debugfs_remove(vport->debug_cpucheck); /* cpucheck */
5842         vport->debug_cpucheck = NULL;
5843
5844         if (vport->vport_debugfs_root) {
5845                 debugfs_remove(vport->vport_debugfs_root); /* vportX */
5846                 vport->vport_debugfs_root = NULL;
5847                 atomic_dec(&phba->debugfs_vport_count);
5848         }
5849
5850         if (atomic_read(&phba->debugfs_vport_count) == 0) {
5851
5852                 debugfs_remove(phba->debug_hbqinfo); /* hbqinfo */
5853                 phba->debug_hbqinfo = NULL;
5854
5855                 debugfs_remove(phba->debug_dumpHBASlim); /* HBASlim */
5856                 phba->debug_dumpHBASlim = NULL;
5857
5858                 debugfs_remove(phba->debug_dumpHostSlim); /* HostSlim */
5859                 phba->debug_dumpHostSlim = NULL;
5860
5861                 debugfs_remove(phba->debug_dumpData); /* dumpData */
5862                 phba->debug_dumpData = NULL;
5863
5864                 debugfs_remove(phba->debug_dumpDif); /* dumpDif */
5865                 phba->debug_dumpDif = NULL;
5866
5867                 debugfs_remove(phba->debug_InjErrLBA); /* InjErrLBA */
5868                 phba->debug_InjErrLBA = NULL;
5869
5870                 debugfs_remove(phba->debug_InjErrNPortID);
5871                 phba->debug_InjErrNPortID = NULL;
5872
5873                 debugfs_remove(phba->debug_InjErrWWPN); /* InjErrWWPN */
5874                 phba->debug_InjErrWWPN = NULL;
5875
5876                 debugfs_remove(phba->debug_writeGuard); /* writeGuard */
5877                 phba->debug_writeGuard = NULL;
5878
5879                 debugfs_remove(phba->debug_writeApp); /* writeApp */
5880                 phba->debug_writeApp = NULL;
5881
5882                 debugfs_remove(phba->debug_writeRef); /* writeRef */
5883                 phba->debug_writeRef = NULL;
5884
5885                 debugfs_remove(phba->debug_readGuard); /* readGuard */
5886                 phba->debug_readGuard = NULL;
5887
5888                 debugfs_remove(phba->debug_readApp); /* readApp */
5889                 phba->debug_readApp = NULL;
5890
5891                 debugfs_remove(phba->debug_readRef); /* readRef */
5892                 phba->debug_readRef = NULL;
5893
5894                 kfree(phba->slow_ring_trc);
5895                 phba->slow_ring_trc = NULL;
5896
5897                 /* slow_ring_trace */
5898                 debugfs_remove(phba->debug_slow_ring_trc);
5899                 phba->debug_slow_ring_trc = NULL;
5900
5901                 debugfs_remove(phba->debug_nvmeio_trc);
5902                 phba->debug_nvmeio_trc = NULL;
5903
5904                 kfree(phba->nvmeio_trc);
5905                 phba->nvmeio_trc = NULL;
5906
5907                 /*
5908                  * iDiag release
5909                  */
5910                 if (phba->sli_rev == LPFC_SLI_REV4) {
5911                         /* iDiag extAcc */
5912                         debugfs_remove(phba->idiag_ext_acc);
5913                         phba->idiag_ext_acc = NULL;
5914
5915                         /* iDiag mbxAcc */
5916                         debugfs_remove(phba->idiag_mbx_acc);
5917                         phba->idiag_mbx_acc = NULL;
5918
5919                         /* iDiag ctlAcc */
5920                         debugfs_remove(phba->idiag_ctl_acc);
5921                         phba->idiag_ctl_acc = NULL;
5922
5923                         /* iDiag drbAcc */
5924                         debugfs_remove(phba->idiag_drb_acc);
5925                         phba->idiag_drb_acc = NULL;
5926
5927                         /* iDiag queAcc */
5928                         debugfs_remove(phba->idiag_que_acc);
5929                         phba->idiag_que_acc = NULL;
5930
5931                         /* iDiag queInfo */
5932                         debugfs_remove(phba->idiag_que_info);
5933                         phba->idiag_que_info = NULL;
5934
5935                         /* iDiag barAcc */
5936                         debugfs_remove(phba->idiag_bar_acc);
5937                         phba->idiag_bar_acc = NULL;
5938
5939                         /* iDiag pciCfg */
5940                         debugfs_remove(phba->idiag_pci_cfg);
5941                         phba->idiag_pci_cfg = NULL;
5942
5943                         /* Finally remove the iDiag debugfs root */
5944                         debugfs_remove(phba->idiag_root);
5945                         phba->idiag_root = NULL;
5946                 }
5947
5948                 if (phba->hba_debugfs_root) {
5949                         debugfs_remove(phba->hba_debugfs_root); /* fnX */
5950                         phba->hba_debugfs_root = NULL;
5951                         atomic_dec(&lpfc_debugfs_hba_count);
5952                 }
5953
5954                 if (atomic_read(&lpfc_debugfs_hba_count) == 0) {
5955                         debugfs_remove(lpfc_debugfs_root); /* lpfc */
5956                         lpfc_debugfs_root = NULL;
5957                 }
5958         }
5959 #endif
5960         return;
5961 }
5962
5963 /*
5964  * Driver debug utility routines outside of debugfs. The debug utility
5965  * routines implemented here is intended to be used in the instrumented
5966  * debug driver for debugging host or port issues.
5967  */
5968
5969 /**
5970  * lpfc_debug_dump_all_queues - dump all the queues with a hba
5971  * @phba: Pointer to HBA context object.
5972  *
5973  * This function dumps entries of all the queues asociated with the @phba.
5974  **/
5975 void
5976 lpfc_debug_dump_all_queues(struct lpfc_hba *phba)
5977 {
5978         int idx;
5979
5980         /*
5981          * Dump Work Queues (WQs)
5982          */
5983         lpfc_debug_dump_wq(phba, DUMP_MBX, 0);
5984         lpfc_debug_dump_wq(phba, DUMP_ELS, 0);
5985         lpfc_debug_dump_wq(phba, DUMP_NVMELS, 0);
5986
5987         for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
5988                 lpfc_debug_dump_wq(phba, DUMP_FCP, idx);
5989
5990         for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
5991                 lpfc_debug_dump_wq(phba, DUMP_NVME, idx);
5992
5993         lpfc_debug_dump_hdr_rq(phba);
5994         lpfc_debug_dump_dat_rq(phba);
5995         /*
5996          * Dump Complete Queues (CQs)
5997          */
5998         lpfc_debug_dump_cq(phba, DUMP_MBX, 0);
5999         lpfc_debug_dump_cq(phba, DUMP_ELS, 0);
6000         lpfc_debug_dump_cq(phba, DUMP_NVMELS, 0);
6001
6002         for (idx = 0; idx < phba->cfg_fcp_io_channel; idx++)
6003                 lpfc_debug_dump_cq(phba, DUMP_FCP, idx);
6004
6005         for (idx = 0; idx < phba->cfg_nvme_io_channel; idx++)
6006                 lpfc_debug_dump_cq(phba, DUMP_NVME, idx);
6007
6008         /*
6009          * Dump Event Queues (EQs)
6010          */
6011         for (idx = 0; idx < phba->io_channel_irqs; idx++)
6012                 lpfc_debug_dump_hba_eq(phba, idx);
6013 }
This page took 0.391515 seconds and 4 git commands to generate.