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