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