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