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