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