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