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