[SCSI] zfcp: Remove obsolete erp_dbf trace
[linux-2.6] / drivers / s390 / scsi / zfcp_dbf.c
1 /*
2  * This file is part of the zfcp device driver for
3  * FCP adapters for IBM System z9 and zSeries.
4  *
5  * (C) Copyright IBM Corp. 2002, 2006
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/ctype.h>
23 #include <asm/debug.h>
24 #include "zfcp_ext.h"
25
26 static u32 dbfsize = 4;
27
28 module_param(dbfsize, uint, 0400);
29 MODULE_PARM_DESC(dbfsize,
30                  "number of pages for each debug feature area (default 4)");
31
32 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_OTHER
33
34 static void zfcp_dbf_hexdump(debug_info_t *dbf, void *to, int to_len,
35                              int level, char *from, int from_len)
36 {
37         int offset;
38         struct zfcp_dbf_dump *dump = to;
39         int room = to_len - sizeof(*dump);
40
41         for (offset = 0; offset < from_len; offset += dump->size) {
42                 memset(to, 0, to_len);
43                 strncpy(dump->tag, "dump", ZFCP_DBF_TAG_SIZE);
44                 dump->total_size = from_len;
45                 dump->offset = offset;
46                 dump->size = min(from_len - offset, room);
47                 memcpy(dump->data, from + offset, dump->size);
48                 debug_event(dbf, level, dump, dump->size);
49         }
50 }
51
52 static int
53 zfcp_dbf_stck(char *out_buf, const char *label, unsigned long long stck)
54 {
55         unsigned long long sec;
56         struct timespec dbftime;
57         int len = 0;
58
59         stck -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
60         sec = stck >> 12;
61         do_div(sec, 1000000);
62         dbftime.tv_sec = sec;
63         stck -= (sec * 1000000) << 12;
64         dbftime.tv_nsec = ((stck * 1000) >> 12);
65         len += sprintf(out_buf + len, "%-24s%011lu:%06lu\n",
66                        label, dbftime.tv_sec, dbftime.tv_nsec);
67
68         return len;
69 }
70
71 static int zfcp_dbf_tag(char *out_buf, const char *label, const char *tag)
72 {
73         int len = 0, i;
74
75         len += sprintf(out_buf + len, "%-24s", label);
76         for (i = 0; i < ZFCP_DBF_TAG_SIZE; i++)
77                 len += sprintf(out_buf + len, "%c", tag[i]);
78         len += sprintf(out_buf + len, "\n");
79
80         return len;
81 }
82
83 static int
84 zfcp_dbf_view(char *out_buf, const char *label, const char *format, ...)
85 {
86         va_list arg;
87         int len = 0;
88
89         len += sprintf(out_buf + len, "%-24s", label);
90         va_start(arg, format);
91         len += vsprintf(out_buf + len, format, arg);
92         va_end(arg);
93         len += sprintf(out_buf + len, "\n");
94
95         return len;
96 }
97
98 static void zfcp_dbf_outs(char **buf, const char *s1, const char *s2)
99 {
100         *buf += sprintf(*buf, "%-24s%s\n", s1, s2);
101 }
102
103 static void zfcp_dbf_out(char **buf, const char *s, const char *format, ...)
104 {
105         va_list arg;
106
107         *buf += sprintf(*buf, "%-24s", s);
108         va_start(arg, format);
109         *buf += vsprintf(*buf, format, arg);
110         va_end(arg);
111         *buf += sprintf(*buf, "\n");
112 }
113
114 static int
115 zfcp_dbf_view_dump(char *out_buf, const char *label,
116                    char *buffer, int buflen, int offset, int total_size)
117 {
118         int len = 0;
119
120         if (offset == 0)
121                 len += sprintf(out_buf + len, "%-24s  ", label);
122
123         while (buflen--) {
124                 if (offset > 0) {
125                         if ((offset % 32) == 0)
126                                 len += sprintf(out_buf + len, "\n%-24c  ", ' ');
127                         else if ((offset % 4) == 0)
128                                 len += sprintf(out_buf + len, " ");
129                 }
130                 len += sprintf(out_buf + len, "%02x", *buffer++);
131                 if (++offset == total_size) {
132                         len += sprintf(out_buf + len, "\n");
133                         break;
134                 }
135         }
136
137         if (total_size == 0)
138                 len += sprintf(out_buf + len, "\n");
139
140         return len;
141 }
142
143 static int
144 zfcp_dbf_view_header(debug_info_t * id, struct debug_view *view, int area,
145                      debug_entry_t * entry, char *out_buf)
146 {
147         struct zfcp_dbf_dump *dump = (struct zfcp_dbf_dump *)DEBUG_DATA(entry);
148         int len = 0;
149
150         if (strncmp(dump->tag, "dump", ZFCP_DBF_TAG_SIZE) != 0) {
151                 len += zfcp_dbf_stck(out_buf + len, "timestamp",
152                                      entry->id.stck);
153                 len += zfcp_dbf_view(out_buf + len, "cpu", "%02i",
154                                      entry->id.fields.cpuid);
155         } else {
156                 len += zfcp_dbf_view_dump(out_buf + len, NULL,
157                                           dump->data,
158                                           dump->size,
159                                           dump->offset, dump->total_size);
160                 if ((dump->offset + dump->size) == dump->total_size)
161                         len += sprintf(out_buf + len, "\n");
162         }
163
164         return len;
165 }
166
167 void zfcp_hba_dbf_event_fsf_response(struct zfcp_fsf_req *fsf_req)
168 {
169         struct zfcp_adapter *adapter = fsf_req->adapter;
170         struct fsf_qtcb *qtcb = fsf_req->qtcb;
171         union fsf_prot_status_qual *prot_status_qual =
172             &qtcb->prefix.prot_status_qual;
173         union fsf_status_qual *fsf_status_qual = &qtcb->header.fsf_status_qual;
174         struct scsi_cmnd *scsi_cmnd;
175         struct zfcp_port *port;
176         struct zfcp_unit *unit;
177         struct zfcp_send_els *send_els;
178         struct zfcp_hba_dbf_record *rec = &adapter->hba_dbf_buf;
179         struct zfcp_hba_dbf_record_response *response = &rec->type.response;
180         int level;
181         unsigned long flags;
182
183         spin_lock_irqsave(&adapter->hba_dbf_lock, flags);
184         memset(rec, 0, sizeof(struct zfcp_hba_dbf_record));
185         strncpy(rec->tag, "resp", ZFCP_DBF_TAG_SIZE);
186
187         if ((qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
188             (qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
189                 strncpy(rec->tag2, "perr", ZFCP_DBF_TAG_SIZE);
190                 level = 1;
191         } else if (qtcb->header.fsf_status != FSF_GOOD) {
192                 strncpy(rec->tag2, "ferr", ZFCP_DBF_TAG_SIZE);
193                 level = 1;
194         } else if ((fsf_req->fsf_command == FSF_QTCB_OPEN_PORT_WITH_DID) ||
195                    (fsf_req->fsf_command == FSF_QTCB_OPEN_LUN)) {
196                 strncpy(rec->tag2, "open", ZFCP_DBF_TAG_SIZE);
197                 level = 4;
198         } else if (qtcb->header.log_length) {
199                 strncpy(rec->tag2, "qtcb", ZFCP_DBF_TAG_SIZE);
200                 level = 5;
201         } else {
202                 strncpy(rec->tag2, "norm", ZFCP_DBF_TAG_SIZE);
203                 level = 6;
204         }
205
206         response->fsf_command = fsf_req->fsf_command;
207         response->fsf_reqid = (unsigned long)fsf_req;
208         response->fsf_seqno = fsf_req->seq_no;
209         response->fsf_issued = fsf_req->issued;
210         response->fsf_prot_status = qtcb->prefix.prot_status;
211         response->fsf_status = qtcb->header.fsf_status;
212         memcpy(response->fsf_prot_status_qual,
213                prot_status_qual, FSF_PROT_STATUS_QUAL_SIZE);
214         memcpy(response->fsf_status_qual,
215                fsf_status_qual, FSF_STATUS_QUALIFIER_SIZE);
216         response->fsf_req_status = fsf_req->status;
217         response->sbal_first = fsf_req->sbal_first;
218         response->sbal_curr = fsf_req->sbal_curr;
219         response->sbal_last = fsf_req->sbal_last;
220         response->pool = fsf_req->pool != NULL;
221         response->erp_action = (unsigned long)fsf_req->erp_action;
222
223         switch (fsf_req->fsf_command) {
224         case FSF_QTCB_FCP_CMND:
225                 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
226                         break;
227                 scsi_cmnd = (struct scsi_cmnd *)fsf_req->data;
228                 if (scsi_cmnd != NULL) {
229                         response->data.send_fcp.scsi_cmnd
230                             = (unsigned long)scsi_cmnd;
231                         response->data.send_fcp.scsi_serial
232                             = scsi_cmnd->serial_number;
233                 }
234                 break;
235
236         case FSF_QTCB_OPEN_PORT_WITH_DID:
237         case FSF_QTCB_CLOSE_PORT:
238         case FSF_QTCB_CLOSE_PHYSICAL_PORT:
239                 port = (struct zfcp_port *)fsf_req->data;
240                 response->data.port.wwpn = port->wwpn;
241                 response->data.port.d_id = port->d_id;
242                 response->data.port.port_handle = qtcb->header.port_handle;
243                 break;
244
245         case FSF_QTCB_OPEN_LUN:
246         case FSF_QTCB_CLOSE_LUN:
247                 unit = (struct zfcp_unit *)fsf_req->data;
248                 port = unit->port;
249                 response->data.unit.wwpn = port->wwpn;
250                 response->data.unit.fcp_lun = unit->fcp_lun;
251                 response->data.unit.port_handle = qtcb->header.port_handle;
252                 response->data.unit.lun_handle = qtcb->header.lun_handle;
253                 break;
254
255         case FSF_QTCB_SEND_ELS:
256                 send_els = (struct zfcp_send_els *)fsf_req->data;
257                 response->data.send_els.d_id = qtcb->bottom.support.d_id;
258                 response->data.send_els.ls_code = send_els->ls_code >> 24;
259                 break;
260
261         case FSF_QTCB_ABORT_FCP_CMND:
262         case FSF_QTCB_SEND_GENERIC:
263         case FSF_QTCB_EXCHANGE_CONFIG_DATA:
264         case FSF_QTCB_EXCHANGE_PORT_DATA:
265         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
266         case FSF_QTCB_UPLOAD_CONTROL_FILE:
267                 break;
268         }
269
270         debug_event(adapter->hba_dbf, level,
271                     rec, sizeof(struct zfcp_hba_dbf_record));
272
273         /* have fcp channel microcode fixed to use as little as possible */
274         if (fsf_req->fsf_command != FSF_QTCB_FCP_CMND) {
275                 /* adjust length skipping trailing zeros */
276                 char *buf = (char *)qtcb + qtcb->header.log_start;
277                 int len = qtcb->header.log_length;
278                 for (; len && !buf[len - 1]; len--);
279                 zfcp_dbf_hexdump(adapter->hba_dbf, rec, sizeof(*rec), level,
280                                  buf, len);
281         }
282
283         spin_unlock_irqrestore(&adapter->hba_dbf_lock, flags);
284 }
285
286 void
287 zfcp_hba_dbf_event_fsf_unsol(const char *tag, struct zfcp_adapter *adapter,
288                              struct fsf_status_read_buffer *status_buffer)
289 {
290         struct zfcp_hba_dbf_record *rec = &adapter->hba_dbf_buf;
291         unsigned long flags;
292
293         spin_lock_irqsave(&adapter->hba_dbf_lock, flags);
294         memset(rec, 0, sizeof(struct zfcp_hba_dbf_record));
295         strncpy(rec->tag, "stat", ZFCP_DBF_TAG_SIZE);
296         strncpy(rec->tag2, tag, ZFCP_DBF_TAG_SIZE);
297
298         rec->type.status.failed = adapter->status_read_failed;
299         if (status_buffer != NULL) {
300                 rec->type.status.status_type = status_buffer->status_type;
301                 rec->type.status.status_subtype = status_buffer->status_subtype;
302                 memcpy(&rec->type.status.queue_designator,
303                        &status_buffer->queue_designator,
304                        sizeof(struct fsf_queue_designator));
305
306                 switch (status_buffer->status_type) {
307                 case FSF_STATUS_READ_SENSE_DATA_AVAIL:
308                         rec->type.status.payload_size =
309                             ZFCP_DBF_UNSOL_PAYLOAD_SENSE_DATA_AVAIL;
310                         break;
311
312                 case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
313                         rec->type.status.payload_size =
314                             ZFCP_DBF_UNSOL_PAYLOAD_BIT_ERROR_THRESHOLD;
315                         break;
316
317                 case FSF_STATUS_READ_LINK_DOWN:
318                         switch (status_buffer->status_subtype) {
319                         case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
320                         case FSF_STATUS_READ_SUB_FDISC_FAILED:
321                                 rec->type.status.payload_size =
322                                         sizeof(struct fsf_link_down_info);
323                         }
324                         break;
325
326                 case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
327                         rec->type.status.payload_size =
328                             ZFCP_DBF_UNSOL_PAYLOAD_FEATURE_UPDATE_ALERT;
329                         break;
330                 }
331                 memcpy(&rec->type.status.payload,
332                        &status_buffer->payload, rec->type.status.payload_size);
333         }
334
335         debug_event(adapter->hba_dbf, 2,
336                     rec, sizeof(struct zfcp_hba_dbf_record));
337         spin_unlock_irqrestore(&adapter->hba_dbf_lock, flags);
338 }
339
340 void
341 zfcp_hba_dbf_event_qdio(struct zfcp_adapter *adapter, unsigned int status,
342                         unsigned int qdio_error, unsigned int siga_error,
343                         int sbal_index, int sbal_count)
344 {
345         struct zfcp_hba_dbf_record *rec = &adapter->hba_dbf_buf;
346         unsigned long flags;
347
348         spin_lock_irqsave(&adapter->hba_dbf_lock, flags);
349         memset(rec, 0, sizeof(struct zfcp_hba_dbf_record));
350         strncpy(rec->tag, "qdio", ZFCP_DBF_TAG_SIZE);
351         rec->type.qdio.status = status;
352         rec->type.qdio.qdio_error = qdio_error;
353         rec->type.qdio.siga_error = siga_error;
354         rec->type.qdio.sbal_index = sbal_index;
355         rec->type.qdio.sbal_count = sbal_count;
356         debug_event(adapter->hba_dbf, 0,
357                     rec, sizeof(struct zfcp_hba_dbf_record));
358         spin_unlock_irqrestore(&adapter->hba_dbf_lock, flags);
359 }
360
361 static int
362 zfcp_hba_dbf_view_response(char *out_buf,
363                            struct zfcp_hba_dbf_record_response *rec)
364 {
365         int len = 0;
366
367         len += zfcp_dbf_view(out_buf + len, "fsf_command", "0x%08x",
368                              rec->fsf_command);
369         len += zfcp_dbf_view(out_buf + len, "fsf_reqid", "0x%0Lx",
370                              rec->fsf_reqid);
371         len += zfcp_dbf_view(out_buf + len, "fsf_seqno", "0x%08x",
372                              rec->fsf_seqno);
373         len += zfcp_dbf_stck(out_buf + len, "fsf_issued", rec->fsf_issued);
374         len += zfcp_dbf_view(out_buf + len, "fsf_prot_status", "0x%08x",
375                              rec->fsf_prot_status);
376         len += zfcp_dbf_view(out_buf + len, "fsf_status", "0x%08x",
377                              rec->fsf_status);
378         len += zfcp_dbf_view_dump(out_buf + len, "fsf_prot_status_qual",
379                                   rec->fsf_prot_status_qual,
380                                   FSF_PROT_STATUS_QUAL_SIZE,
381                                   0, FSF_PROT_STATUS_QUAL_SIZE);
382         len += zfcp_dbf_view_dump(out_buf + len, "fsf_status_qual",
383                                   rec->fsf_status_qual,
384                                   FSF_STATUS_QUALIFIER_SIZE,
385                                   0, FSF_STATUS_QUALIFIER_SIZE);
386         len += zfcp_dbf_view(out_buf + len, "fsf_req_status", "0x%08x",
387                              rec->fsf_req_status);
388         len += zfcp_dbf_view(out_buf + len, "sbal_first", "0x%02x",
389                              rec->sbal_first);
390         len += zfcp_dbf_view(out_buf + len, "sbal_curr", "0x%02x",
391                              rec->sbal_curr);
392         len += zfcp_dbf_view(out_buf + len, "sbal_last", "0x%02x",
393                              rec->sbal_last);
394         len += zfcp_dbf_view(out_buf + len, "pool", "0x%02x", rec->pool);
395
396         switch (rec->fsf_command) {
397         case FSF_QTCB_FCP_CMND:
398                 if (rec->fsf_req_status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
399                         break;
400                 len += zfcp_dbf_view(out_buf + len, "scsi_cmnd", "0x%0Lx",
401                                      rec->data.send_fcp.scsi_cmnd);
402                 len += zfcp_dbf_view(out_buf + len, "scsi_serial", "0x%016Lx",
403                                      rec->data.send_fcp.scsi_serial);
404                 break;
405
406         case FSF_QTCB_OPEN_PORT_WITH_DID:
407         case FSF_QTCB_CLOSE_PORT:
408         case FSF_QTCB_CLOSE_PHYSICAL_PORT:
409                 len += zfcp_dbf_view(out_buf + len, "wwpn", "0x%016Lx",
410                                      rec->data.port.wwpn);
411                 len += zfcp_dbf_view(out_buf + len, "d_id", "0x%06x",
412                                      rec->data.port.d_id);
413                 len += zfcp_dbf_view(out_buf + len, "port_handle", "0x%08x",
414                                      rec->data.port.port_handle);
415                 break;
416
417         case FSF_QTCB_OPEN_LUN:
418         case FSF_QTCB_CLOSE_LUN:
419                 len += zfcp_dbf_view(out_buf + len, "wwpn", "0x%016Lx",
420                                      rec->data.unit.wwpn);
421                 len += zfcp_dbf_view(out_buf + len, "fcp_lun", "0x%016Lx",
422                                      rec->data.unit.fcp_lun);
423                 len += zfcp_dbf_view(out_buf + len, "port_handle", "0x%08x",
424                                      rec->data.unit.port_handle);
425                 len += zfcp_dbf_view(out_buf + len, "lun_handle", "0x%08x",
426                                      rec->data.unit.lun_handle);
427                 break;
428
429         case FSF_QTCB_SEND_ELS:
430                 len += zfcp_dbf_view(out_buf + len, "d_id", "0x%06x",
431                                      rec->data.send_els.d_id);
432                 len += zfcp_dbf_view(out_buf + len, "ls_code", "0x%02x",
433                                      rec->data.send_els.ls_code);
434                 break;
435
436         case FSF_QTCB_ABORT_FCP_CMND:
437         case FSF_QTCB_SEND_GENERIC:
438         case FSF_QTCB_EXCHANGE_CONFIG_DATA:
439         case FSF_QTCB_EXCHANGE_PORT_DATA:
440         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
441         case FSF_QTCB_UPLOAD_CONTROL_FILE:
442                 break;
443         }
444
445         return len;
446 }
447
448 static int
449 zfcp_hba_dbf_view_status(char *out_buf, struct zfcp_hba_dbf_record_status *rec)
450 {
451         int len = 0;
452
453         len += zfcp_dbf_view(out_buf + len, "failed", "0x%02x", rec->failed);
454         len += zfcp_dbf_view(out_buf + len, "status_type", "0x%08x",
455                              rec->status_type);
456         len += zfcp_dbf_view(out_buf + len, "status_subtype", "0x%08x",
457                              rec->status_subtype);
458         len += zfcp_dbf_view_dump(out_buf + len, "queue_designator",
459                                   (char *)&rec->queue_designator,
460                                   sizeof(struct fsf_queue_designator),
461                                   0, sizeof(struct fsf_queue_designator));
462         len += zfcp_dbf_view_dump(out_buf + len, "payload",
463                                   (char *)&rec->payload,
464                                   rec->payload_size, 0, rec->payload_size);
465
466         return len;
467 }
468
469 static int
470 zfcp_hba_dbf_view_qdio(char *out_buf, struct zfcp_hba_dbf_record_qdio *rec)
471 {
472         int len = 0;
473
474         len += zfcp_dbf_view(out_buf + len, "status", "0x%08x", rec->status);
475         len += zfcp_dbf_view(out_buf + len, "qdio_error", "0x%08x",
476                              rec->qdio_error);
477         len += zfcp_dbf_view(out_buf + len, "siga_error", "0x%08x",
478                              rec->siga_error);
479         len += zfcp_dbf_view(out_buf + len, "sbal_index", "0x%02x",
480                              rec->sbal_index);
481         len += zfcp_dbf_view(out_buf + len, "sbal_count", "0x%02x",
482                              rec->sbal_count);
483
484         return len;
485 }
486
487 static int
488 zfcp_hba_dbf_view_format(debug_info_t * id, struct debug_view *view,
489                          char *out_buf, const char *in_buf)
490 {
491         struct zfcp_hba_dbf_record *rec = (struct zfcp_hba_dbf_record *)in_buf;
492         int len = 0;
493
494         if (strncmp(rec->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
495                 return 0;
496
497         len += zfcp_dbf_tag(out_buf + len, "tag", rec->tag);
498         if (isalpha(rec->tag2[0]))
499                 len += zfcp_dbf_tag(out_buf + len, "tag2", rec->tag2);
500         if (strncmp(rec->tag, "resp", ZFCP_DBF_TAG_SIZE) == 0)
501                 len += zfcp_hba_dbf_view_response(out_buf + len,
502                                                   &rec->type.response);
503         else if (strncmp(rec->tag, "stat", ZFCP_DBF_TAG_SIZE) == 0)
504                 len += zfcp_hba_dbf_view_status(out_buf + len,
505                                                 &rec->type.status);
506         else if (strncmp(rec->tag, "qdio", ZFCP_DBF_TAG_SIZE) == 0)
507                 len += zfcp_hba_dbf_view_qdio(out_buf + len, &rec->type.qdio);
508
509         len += sprintf(out_buf + len, "\n");
510
511         return len;
512 }
513
514 static struct debug_view zfcp_hba_dbf_view = {
515         "structured",
516         NULL,
517         &zfcp_dbf_view_header,
518         &zfcp_hba_dbf_view_format,
519         NULL,
520         NULL
521 };
522
523 static const char *zfcp_rec_dbf_tags[] = {
524         [ZFCP_REC_DBF_ID_THREAD] = "thread",
525         [ZFCP_REC_DBF_ID_TARGET] = "target",
526         [ZFCP_REC_DBF_ID_TRIGGER] = "trigger",
527         [ZFCP_REC_DBF_ID_ACTION] = "action",
528 };
529
530 static const char *zfcp_rec_dbf_ids[] = {
531         [1]     = "new",
532         [2]     = "ready",
533         [3]     = "kill",
534         [4]     = "down sleep",
535         [5]     = "down wakeup",
536         [6]     = "down sleep ecd",
537         [7]     = "down wakeup ecd",
538         [8]     = "down sleep epd",
539         [9]     = "down wakeup epd",
540         [10]    = "online",
541         [11]    = "operational",
542         [12]    = "scsi slave destroy",
543         [13]    = "propagate failed adapter",
544         [14]    = "propagate failed port",
545         [15]    = "block adapter",
546         [16]    = "unblock adapter",
547         [17]    = "block port",
548         [18]    = "unblock port",
549         [19]    = "block unit",
550         [20]    = "unblock unit",
551         [21]    = "unit recovery failed",
552         [22]    = "port recovery failed",
553         [23]    = "adapter recovery failed",
554         [24]    = "qdio queues down",
555         [25]    = "p2p failed",
556         [26]    = "nameserver lookup failed",
557         [27]    = "nameserver port failed",
558         [28]    = "link up",
559         [29]    = "link down",
560         [30]    = "link up status read",
561         [31]    = "open port failed",
562         [32]    = "open port failed",
563         [33]    = "close port",
564         [34]    = "open unit failed",
565         [35]    = "exclusive open unit failed",
566         [36]    = "shared open unit failed",
567         [37]    = "link down",
568         [38]    = "link down status read no link",
569         [39]    = "link down status read fdisc login",
570         [40]    = "link down status read firmware update",
571         [41]    = "link down status read unknown reason",
572         [42]    = "link down ecd incomplete",
573         [43]    = "link down epd incomplete",
574         [44]    = "sysfs adapter recovery",
575         [45]    = "sysfs port recovery",
576         [46]    = "sysfs unit recovery",
577         [47]    = "port boxed abort",
578         [48]    = "unit boxed abort",
579         [49]    = "port boxed ct",
580         [50]    = "port boxed close physical",
581         [51]    = "port boxed open unit",
582         [52]    = "port boxed close unit",
583         [53]    = "port boxed fcp",
584         [54]    = "unit boxed fcp",
585         [55]    = "port access denied ct",
586         [56]    = "port access denied els",
587         [57]    = "port access denied open port",
588         [58]    = "port access denied close physical",
589         [59]    = "unit access denied open unit",
590         [60]    = "shared unit access denied open unit",
591         [61]    = "unit access denied fcp",
592         [62]    = "request timeout",
593         [63]    = "adisc link test reject or timeout",
594         [64]    = "adisc link test d_id changed",
595         [65]    = "adisc link test failed",
596         [66]    = "recovery out of memory",
597         [67]    = "adapter recovery repeated after state change",
598         [68]    = "port recovery repeated after state change",
599         [69]    = "unit recovery repeated after state change",
600         [70]    = "port recovery follow-up after successful adapter recovery",
601         [71]    = "adapter recovery escalation after failed adapter recovery",
602         [72]    = "port recovery follow-up after successful physical port "
603                   "recovery",
604         [73]    = "adapter recovery escalation after failed physical port "
605                   "recovery",
606         [74]    = "unit recovery follow-up after successful port recovery",
607         [75]    = "physical port recovery escalation after failed port "
608                   "recovery",
609         [76]    = "port recovery escalation after failed unit recovery",
610         [77]    = "recovery opening nameserver port",
611         [78]    = "duplicate request id",
612         [79]    = "link down",
613         [80]    = "exclusive read-only unit access unsupported",
614         [81]    = "shared read-write unit access unsupported",
615         [82]    = "incoming rscn",
616         [83]    = "incoming plogi",
617         [84]    = "incoming logo",
618         [85]    = "online",
619         [86]    = "offline",
620         [87]    = "ccw device gone",
621         [88]    = "ccw device no path",
622         [89]    = "ccw device operational",
623         [90]    = "ccw device shutdown",
624         [91]    = "sysfs port addition",
625         [92]    = "sysfs port removal",
626         [93]    = "sysfs adapter recovery",
627         [94]    = "sysfs unit addition",
628         [95]    = "sysfs unit removal",
629         [96]    = "sysfs port recovery",
630         [97]    = "sysfs unit recovery",
631         [98]    = "sequence number mismatch",
632         [99]    = "link up",
633         [100]   = "error state",
634         [101]   = "status read physical port closed",
635         [102]   = "link up status read",
636         [103]   = "too many failed status read buffers",
637         [104]   = "port handle not valid abort",
638         [105]   = "lun handle not valid abort",
639         [106]   = "port handle not valid ct",
640         [107]   = "port handle not valid close port",
641         [108]   = "port handle not valid close physical port",
642         [109]   = "port handle not valid open unit",
643         [110]   = "port handle not valid close unit",
644         [111]   = "lun handle not valid close unit",
645         [112]   = "port handle not valid fcp",
646         [113]   = "lun handle not valid fcp",
647         [114]   = "handle mismatch fcp",
648         [115]   = "lun not valid fcp",
649         [116]   = "qdio send failed",
650         [117]   = "version mismatch",
651         [118]   = "incompatible qtcb type",
652         [119]   = "unknown protocol status",
653         [120]   = "unknown fsf command",
654         [121]   = "no recommendation for status qualifier",
655         [122]   = "status read physical port closed in error",
656         [123]   = "fc service class not supported ct",
657         [124]   = "fc service class not supported els",
658         [125]   = "need newer zfcp",
659         [126]   = "need newer microcode",
660         [127]   = "arbitrated loop not supported",
661         [128]   = "unknown topology",
662         [129]   = "qtcb size mismatch",
663         [130]   = "unknown fsf status ecd",
664         [131]   = "fcp request too big",
665         [132]   = "fc service class not supported fcp",
666         [133]   = "data direction not valid fcp",
667         [134]   = "command length not valid fcp",
668         [135]   = "status read act update",
669         [136]   = "status read cfdc update",
670         [137]   = "hbaapi port open",
671         [138]   = "hbaapi unit open",
672         [139]   = "hbaapi unit shutdown",
673         [140]   = "qdio error",
674         [141]   = "scsi host reset",
675         [142]   = "dismissing fsf request for recovery action",
676         [143]   = "recovery action timed out",
677         [144]   = "recovery action gone",
678         [145]   = "recovery action being processed",
679         [146]   = "recovery action ready for next step",
680 };
681
682 static int zfcp_rec_dbf_view_format(debug_info_t *id, struct debug_view *view,
683                                     char *buf, const char *_rec)
684 {
685         struct zfcp_rec_dbf_record *r = (struct zfcp_rec_dbf_record *)_rec;
686         char *p = buf;
687
688         zfcp_dbf_outs(&p, "tag", zfcp_rec_dbf_tags[r->id]);
689         zfcp_dbf_outs(&p, "hint", zfcp_rec_dbf_ids[r->id2]);
690         zfcp_dbf_out(&p, "id", "%d", r->id2);
691         switch (r->id) {
692         case ZFCP_REC_DBF_ID_THREAD:
693                 zfcp_dbf_out(&p, "sema", "%d", r->u.thread.sema);
694                 zfcp_dbf_out(&p, "total", "%d", r->u.thread.total);
695                 zfcp_dbf_out(&p, "ready", "%d", r->u.thread.ready);
696                 zfcp_dbf_out(&p, "running", "%d", r->u.thread.running);
697                 break;
698         case ZFCP_REC_DBF_ID_TARGET:
699                 zfcp_dbf_out(&p, "reference", "0x%016Lx", r->u.target.ref);
700                 zfcp_dbf_out(&p, "status", "0x%08x", r->u.target.status);
701                 zfcp_dbf_out(&p, "erp_count", "%d", r->u.target.erp_count);
702                 zfcp_dbf_out(&p, "d_id", "0x%06x", r->u.target.d_id);
703                 zfcp_dbf_out(&p, "wwpn", "0x%016Lx", r->u.target.wwpn);
704                 zfcp_dbf_out(&p, "fcp_lun", "0x%016Lx", r->u.target.fcp_lun);
705                 break;
706         case ZFCP_REC_DBF_ID_TRIGGER:
707                 zfcp_dbf_out(&p, "reference", "0x%016Lx", r->u.trigger.ref);
708                 zfcp_dbf_out(&p, "erp_action", "0x%016Lx", r->u.trigger.action);
709                 zfcp_dbf_out(&p, "requested", "%d", r->u.trigger.want);
710                 zfcp_dbf_out(&p, "executed", "%d", r->u.trigger.need);
711                 zfcp_dbf_out(&p, "wwpn", "0x%016Lx", r->u.trigger.wwpn);
712                 zfcp_dbf_out(&p, "fcp_lun", "0x%016Lx", r->u.trigger.fcp_lun);
713                 zfcp_dbf_out(&p, "adapter_status", "0x%08x", r->u.trigger.as);
714                 zfcp_dbf_out(&p, "port_status", "0x%08x", r->u.trigger.ps);
715                 zfcp_dbf_out(&p, "unit_status", "0x%08x", r->u.trigger.us);
716                 break;
717         case ZFCP_REC_DBF_ID_ACTION:
718                 zfcp_dbf_out(&p, "erp_action", "0x%016Lx", r->u.action.action);
719                 zfcp_dbf_out(&p, "fsf_req", "0x%016Lx", r->u.action.fsf_req);
720                 zfcp_dbf_out(&p, "status", "0x%08Lx", r->u.action.status);
721                 zfcp_dbf_out(&p, "step", "0x%08Lx", r->u.action.step);
722                 break;
723         }
724         sprintf(p, "\n");
725         return (p - buf) + 1;
726 }
727
728 static struct debug_view zfcp_rec_dbf_view = {
729         "structured",
730         NULL,
731         &zfcp_dbf_view_header,
732         &zfcp_rec_dbf_view_format,
733         NULL,
734         NULL
735 };
736
737 /**
738  * zfcp_rec_dbf_event_thread - trace event related to recovery thread operation
739  * @id2: identifier for event
740  * @adapter: adapter
741  * @lock: non-zero value indicates that erp_lock has not yet been acquired
742  */
743 void zfcp_rec_dbf_event_thread(u8 id2, struct zfcp_adapter *adapter, int lock)
744 {
745         struct zfcp_rec_dbf_record *r = &adapter->rec_dbf_buf;
746         unsigned long flags = 0;
747         struct list_head *entry;
748         unsigned ready = 0, running = 0, total;
749
750         if (lock)
751                 read_lock_irqsave(&adapter->erp_lock, flags);
752         list_for_each(entry, &adapter->erp_ready_head)
753                 ready++;
754         list_for_each(entry, &adapter->erp_running_head)
755                 running++;
756         total = adapter->erp_total_count;
757         if (lock)
758                 read_unlock_irqrestore(&adapter->erp_lock, flags);
759
760         spin_lock_irqsave(&adapter->rec_dbf_lock, flags);
761         memset(r, 0, sizeof(*r));
762         r->id = ZFCP_REC_DBF_ID_THREAD;
763         r->id2 = id2;
764         r->u.thread.sema = atomic_read(&adapter->erp_ready_sem.count);
765         r->u.thread.total = total;
766         r->u.thread.ready = ready;
767         r->u.thread.running = running;
768         debug_event(adapter->rec_dbf, 5, r, sizeof(*r));
769         spin_unlock_irqrestore(&adapter->rec_dbf_lock, flags);
770 }
771
772 static void zfcp_rec_dbf_event_target(u8 id2, u64 ref,
773                                       struct zfcp_adapter *adapter,
774                                       atomic_t *status, atomic_t *erp_count,
775                                       u64 wwpn, u32 d_id, u64 fcp_lun)
776 {
777         struct zfcp_rec_dbf_record *r = &adapter->rec_dbf_buf;
778         unsigned long flags;
779
780         spin_lock_irqsave(&adapter->rec_dbf_lock, flags);
781         memset(r, 0, sizeof(*r));
782         r->id = ZFCP_REC_DBF_ID_TARGET;
783         r->id2 = id2;
784         r->u.target.ref = ref;
785         r->u.target.status = atomic_read(status);
786         r->u.target.wwpn = wwpn;
787         r->u.target.d_id = d_id;
788         r->u.target.fcp_lun = fcp_lun;
789         r->u.target.erp_count = atomic_read(erp_count);
790         debug_event(adapter->rec_dbf, 3, r, sizeof(*r));
791         spin_unlock_irqrestore(&adapter->rec_dbf_lock, flags);
792 }
793
794 /**
795  * zfcp_rec_dbf_event_adapter - trace event for adapter state change
796  * @id: identifier for trigger of state change
797  * @ref: additional reference (e.g. request)
798  * @adapter: adapter
799  */
800 void zfcp_rec_dbf_event_adapter(u8 id, u64 ref, struct zfcp_adapter *adapter)
801 {
802         zfcp_rec_dbf_event_target(id, ref, adapter, &adapter->status,
803                                   &adapter->erp_counter, 0, 0, 0);
804 }
805
806 /**
807  * zfcp_rec_dbf_event_port - trace event for port state change
808  * @id: identifier for trigger of state change
809  * @ref: additional reference (e.g. request)
810  * @port: port
811  */
812 void zfcp_rec_dbf_event_port(u8 id, u64 ref, struct zfcp_port *port)
813 {
814         struct zfcp_adapter *adapter = port->adapter;
815
816         zfcp_rec_dbf_event_target(id, ref, adapter, &port->status,
817                                   &port->erp_counter, port->wwpn, port->d_id,
818                                   0);
819 }
820
821 /**
822  * zfcp_rec_dbf_event_unit - trace event for unit state change
823  * @id: identifier for trigger of state change
824  * @ref: additional reference (e.g. request)
825  * @unit: unit
826  */
827 void zfcp_rec_dbf_event_unit(u8 id, u64 ref, struct zfcp_unit *unit)
828 {
829         struct zfcp_port *port = unit->port;
830         struct zfcp_adapter *adapter = port->adapter;
831
832         zfcp_rec_dbf_event_target(id, ref, adapter, &unit->status,
833                                   &unit->erp_counter, port->wwpn, port->d_id,
834                                   unit->fcp_lun);
835 }
836
837 /**
838  * zfcp_rec_dbf_event_trigger - trace event for triggered error recovery
839  * @id2: identifier for error recovery trigger
840  * @ref: additional reference (e.g. request)
841  * @want: originally requested error recovery action
842  * @need: error recovery action actually initiated
843  * @action: address of error recovery action struct
844  * @adapter: adapter
845  * @port: port
846  * @unit: unit
847  */
848 void zfcp_rec_dbf_event_trigger(u8 id2, u64 ref, u8 want, u8 need, u64 action,
849                                 struct zfcp_adapter *adapter,
850                                 struct zfcp_port *port, struct zfcp_unit *unit)
851 {
852         struct zfcp_rec_dbf_record *r = &adapter->rec_dbf_buf;
853         unsigned long flags;
854
855         spin_lock_irqsave(&adapter->rec_dbf_lock, flags);
856         memset(r, 0, sizeof(*r));
857         r->id = ZFCP_REC_DBF_ID_TRIGGER;
858         r->id2 = id2;
859         r->u.trigger.ref = ref;
860         r->u.trigger.want = want;
861         r->u.trigger.need = need;
862         r->u.trigger.action = action;
863         r->u.trigger.as = atomic_read(&adapter->status);
864         if (port) {
865                 r->u.trigger.ps = atomic_read(&port->status);
866                 r->u.trigger.wwpn = port->wwpn;
867         }
868         if (unit) {
869                 r->u.trigger.us = atomic_read(&unit->status);
870                 r->u.trigger.fcp_lun = unit->fcp_lun;
871         }
872         debug_event(adapter->rec_dbf, action ? 1 : 4, r, sizeof(*r));
873         spin_unlock_irqrestore(&adapter->rec_dbf_lock, flags);
874 }
875
876 /**
877  * zfcp_rec_dbf_event_action - trace event showing progress of recovery action
878  * @id2: identifier
879  * @erp_action: error recovery action struct pointer
880  */
881 void zfcp_rec_dbf_event_action(u8 id2, struct zfcp_erp_action *erp_action)
882 {
883         struct zfcp_adapter *adapter = erp_action->adapter;
884         struct zfcp_rec_dbf_record *r = &adapter->rec_dbf_buf;
885         unsigned long flags;
886
887         spin_lock_irqsave(&adapter->rec_dbf_lock, flags);
888         memset(r, 0, sizeof(*r));
889         r->id = ZFCP_REC_DBF_ID_ACTION;
890         r->id2 = id2;
891         r->u.action.action = (u64)erp_action;
892         r->u.action.status = erp_action->status;
893         r->u.action.step = erp_action->step;
894         r->u.action.fsf_req = (u64)erp_action->fsf_req;
895         debug_event(adapter->rec_dbf, 4, r, sizeof(*r));
896         spin_unlock_irqrestore(&adapter->rec_dbf_lock, flags);
897 }
898
899 static void
900 _zfcp_san_dbf_event_common_ct(const char *tag, struct zfcp_fsf_req *fsf_req,
901                               u32 s_id, u32 d_id, void *buffer, int buflen)
902 {
903         struct zfcp_send_ct *send_ct = (struct zfcp_send_ct *)fsf_req->data;
904         struct zfcp_port *port = send_ct->port;
905         struct zfcp_adapter *adapter = port->adapter;
906         struct ct_hdr *header = (struct ct_hdr *)buffer;
907         struct zfcp_san_dbf_record *rec = &adapter->san_dbf_buf;
908         struct zfcp_san_dbf_record_ct *ct = &rec->type.ct;
909         unsigned long flags;
910
911         spin_lock_irqsave(&adapter->san_dbf_lock, flags);
912         memset(rec, 0, sizeof(struct zfcp_san_dbf_record));
913         strncpy(rec->tag, tag, ZFCP_DBF_TAG_SIZE);
914         rec->fsf_reqid = (unsigned long)fsf_req;
915         rec->fsf_seqno = fsf_req->seq_no;
916         rec->s_id = s_id;
917         rec->d_id = d_id;
918         if (strncmp(tag, "octc", ZFCP_DBF_TAG_SIZE) == 0) {
919                 ct->type.request.cmd_req_code = header->cmd_rsp_code;
920                 ct->type.request.revision = header->revision;
921                 ct->type.request.gs_type = header->gs_type;
922                 ct->type.request.gs_subtype = header->gs_subtype;
923                 ct->type.request.options = header->options;
924                 ct->type.request.max_res_size = header->max_res_size;
925         } else if (strncmp(tag, "rctc", ZFCP_DBF_TAG_SIZE) == 0) {
926                 ct->type.response.cmd_rsp_code = header->cmd_rsp_code;
927                 ct->type.response.revision = header->revision;
928                 ct->type.response.reason_code = header->reason_code;
929                 ct->type.response.reason_code_expl = header->reason_code_expl;
930                 ct->type.response.vendor_unique = header->vendor_unique;
931         }
932         ct->payload_size =
933             min(buflen - (int)sizeof(struct ct_hdr), ZFCP_DBF_CT_PAYLOAD);
934         memcpy(ct->payload, buffer + sizeof(struct ct_hdr), ct->payload_size);
935         debug_event(adapter->san_dbf, 3,
936                     rec, sizeof(struct zfcp_san_dbf_record));
937         spin_unlock_irqrestore(&adapter->san_dbf_lock, flags);
938 }
939
940 void zfcp_san_dbf_event_ct_request(struct zfcp_fsf_req *fsf_req)
941 {
942         struct zfcp_send_ct *ct = (struct zfcp_send_ct *)fsf_req->data;
943         struct zfcp_port *port = ct->port;
944         struct zfcp_adapter *adapter = port->adapter;
945
946         _zfcp_san_dbf_event_common_ct("octc", fsf_req,
947                                       fc_host_port_id(adapter->scsi_host),
948                                       port->d_id, zfcp_sg_to_address(ct->req),
949                                       ct->req->length);
950 }
951
952 void zfcp_san_dbf_event_ct_response(struct zfcp_fsf_req *fsf_req)
953 {
954         struct zfcp_send_ct *ct = (struct zfcp_send_ct *)fsf_req->data;
955         struct zfcp_port *port = ct->port;
956         struct zfcp_adapter *adapter = port->adapter;
957
958         _zfcp_san_dbf_event_common_ct("rctc", fsf_req, port->d_id,
959                                       fc_host_port_id(adapter->scsi_host),
960                                       zfcp_sg_to_address(ct->resp),
961                                       ct->resp->length);
962 }
963
964 static void
965 _zfcp_san_dbf_event_common_els(const char *tag, int level,
966                                struct zfcp_fsf_req *fsf_req, u32 s_id,
967                                u32 d_id, u8 ls_code, void *buffer, int buflen)
968 {
969         struct zfcp_adapter *adapter = fsf_req->adapter;
970         struct zfcp_san_dbf_record *rec = &adapter->san_dbf_buf;
971         unsigned long flags;
972
973         spin_lock_irqsave(&adapter->san_dbf_lock, flags);
974         memset(rec, 0, sizeof(struct zfcp_san_dbf_record));
975         strncpy(rec->tag, tag, ZFCP_DBF_TAG_SIZE);
976         rec->fsf_reqid = (unsigned long)fsf_req;
977         rec->fsf_seqno = fsf_req->seq_no;
978         rec->s_id = s_id;
979         rec->d_id = d_id;
980         rec->type.els.ls_code = ls_code;
981         debug_event(adapter->san_dbf, level, rec, sizeof(*rec));
982         zfcp_dbf_hexdump(adapter->san_dbf, rec, sizeof(*rec), level,
983                          buffer, min(buflen, ZFCP_DBF_ELS_MAX_PAYLOAD));
984         spin_unlock_irqrestore(&adapter->san_dbf_lock, flags);
985 }
986
987 void zfcp_san_dbf_event_els_request(struct zfcp_fsf_req *fsf_req)
988 {
989         struct zfcp_send_els *els = (struct zfcp_send_els *)fsf_req->data;
990
991         _zfcp_san_dbf_event_common_els("oels", 2, fsf_req,
992                                        fc_host_port_id(els->adapter->scsi_host),
993                                        els->d_id,
994                                        *(u8 *) zfcp_sg_to_address(els->req),
995                                        zfcp_sg_to_address(els->req),
996                                        els->req->length);
997 }
998
999 void zfcp_san_dbf_event_els_response(struct zfcp_fsf_req *fsf_req)
1000 {
1001         struct zfcp_send_els *els = (struct zfcp_send_els *)fsf_req->data;
1002
1003         _zfcp_san_dbf_event_common_els("rels", 2, fsf_req, els->d_id,
1004                                        fc_host_port_id(els->adapter->scsi_host),
1005                                        *(u8 *) zfcp_sg_to_address(els->req),
1006                                        zfcp_sg_to_address(els->resp),
1007                                        els->resp->length);
1008 }
1009
1010 void zfcp_san_dbf_event_incoming_els(struct zfcp_fsf_req *fsf_req)
1011 {
1012         struct zfcp_adapter *adapter = fsf_req->adapter;
1013         struct fsf_status_read_buffer *status_buffer =
1014             (struct fsf_status_read_buffer *)fsf_req->data;
1015         int length = (int)status_buffer->length -
1016             (int)((void *)&status_buffer->payload - (void *)status_buffer);
1017
1018         _zfcp_san_dbf_event_common_els("iels", 1, fsf_req, status_buffer->d_id,
1019                                        fc_host_port_id(adapter->scsi_host),
1020                                        *(u8 *) status_buffer->payload,
1021                                        (void *)status_buffer->payload, length);
1022 }
1023
1024 static int
1025 zfcp_san_dbf_view_format(debug_info_t * id, struct debug_view *view,
1026                          char *out_buf, const char *in_buf)
1027 {
1028         struct zfcp_san_dbf_record *rec = (struct zfcp_san_dbf_record *)in_buf;
1029         char *buffer = NULL;
1030         int buflen = 0, total = 0;
1031         int len = 0;
1032
1033         if (strncmp(rec->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
1034                 return 0;
1035
1036         len += zfcp_dbf_tag(out_buf + len, "tag", rec->tag);
1037         len += zfcp_dbf_view(out_buf + len, "fsf_reqid", "0x%0Lx",
1038                              rec->fsf_reqid);
1039         len += zfcp_dbf_view(out_buf + len, "fsf_seqno", "0x%08x",
1040                              rec->fsf_seqno);
1041         len += zfcp_dbf_view(out_buf + len, "s_id", "0x%06x", rec->s_id);
1042         len += zfcp_dbf_view(out_buf + len, "d_id", "0x%06x", rec->d_id);
1043
1044         if (strncmp(rec->tag, "octc", ZFCP_DBF_TAG_SIZE) == 0) {
1045                 len += zfcp_dbf_view(out_buf + len, "cmd_req_code", "0x%04x",
1046                                      rec->type.ct.type.request.cmd_req_code);
1047                 len += zfcp_dbf_view(out_buf + len, "revision", "0x%02x",
1048                                      rec->type.ct.type.request.revision);
1049                 len += zfcp_dbf_view(out_buf + len, "gs_type", "0x%02x",
1050                                      rec->type.ct.type.request.gs_type);
1051                 len += zfcp_dbf_view(out_buf + len, "gs_subtype", "0x%02x",
1052                                      rec->type.ct.type.request.gs_subtype);
1053                 len += zfcp_dbf_view(out_buf + len, "options", "0x%02x",
1054                                      rec->type.ct.type.request.options);
1055                 len += zfcp_dbf_view(out_buf + len, "max_res_size", "0x%04x",
1056                                      rec->type.ct.type.request.max_res_size);
1057                 total = rec->type.ct.payload_size;
1058                 buffer = rec->type.ct.payload;
1059                 buflen = min(total, ZFCP_DBF_CT_PAYLOAD);
1060         } else if (strncmp(rec->tag, "rctc", ZFCP_DBF_TAG_SIZE) == 0) {
1061                 len += zfcp_dbf_view(out_buf + len, "cmd_rsp_code", "0x%04x",
1062                                      rec->type.ct.type.response.cmd_rsp_code);
1063                 len += zfcp_dbf_view(out_buf + len, "revision", "0x%02x",
1064                                      rec->type.ct.type.response.revision);
1065                 len += zfcp_dbf_view(out_buf + len, "reason_code", "0x%02x",
1066                                      rec->type.ct.type.response.reason_code);
1067                 len +=
1068                     zfcp_dbf_view(out_buf + len, "reason_code_expl", "0x%02x",
1069                                   rec->type.ct.type.response.reason_code_expl);
1070                 len +=
1071                     zfcp_dbf_view(out_buf + len, "vendor_unique", "0x%02x",
1072                                   rec->type.ct.type.response.vendor_unique);
1073                 total = rec->type.ct.payload_size;
1074                 buffer = rec->type.ct.payload;
1075                 buflen = min(total, ZFCP_DBF_CT_PAYLOAD);
1076         } else if (strncmp(rec->tag, "oels", ZFCP_DBF_TAG_SIZE) == 0 ||
1077                    strncmp(rec->tag, "rels", ZFCP_DBF_TAG_SIZE) == 0 ||
1078                    strncmp(rec->tag, "iels", ZFCP_DBF_TAG_SIZE) == 0) {
1079                 len += zfcp_dbf_view(out_buf + len, "ls_code", "0x%02x",
1080                                      rec->type.els.ls_code);
1081                 total = rec->type.els.payload_size;
1082                 buffer = rec->type.els.payload;
1083                 buflen = min(total, ZFCP_DBF_ELS_PAYLOAD);
1084         }
1085
1086         len += zfcp_dbf_view_dump(out_buf + len, "payload",
1087                                   buffer, buflen, 0, total);
1088
1089         if (buflen == total)
1090                 len += sprintf(out_buf + len, "\n");
1091
1092         return len;
1093 }
1094
1095 static struct debug_view zfcp_san_dbf_view = {
1096         "structured",
1097         NULL,
1098         &zfcp_dbf_view_header,
1099         &zfcp_san_dbf_view_format,
1100         NULL,
1101         NULL
1102 };
1103
1104 static void
1105 _zfcp_scsi_dbf_event_common(const char *tag, const char *tag2, int level,
1106                             struct zfcp_adapter *adapter,
1107                             struct scsi_cmnd *scsi_cmnd,
1108                             struct zfcp_fsf_req *fsf_req,
1109                             unsigned long old_req_id)
1110 {
1111         struct zfcp_scsi_dbf_record *rec = &adapter->scsi_dbf_buf;
1112         struct zfcp_dbf_dump *dump = (struct zfcp_dbf_dump *)rec;
1113         unsigned long flags;
1114         struct fcp_rsp_iu *fcp_rsp;
1115         char *fcp_rsp_info = NULL, *fcp_sns_info = NULL;
1116         int offset = 0, buflen = 0;
1117
1118         spin_lock_irqsave(&adapter->scsi_dbf_lock, flags);
1119         do {
1120                 memset(rec, 0, sizeof(struct zfcp_scsi_dbf_record));
1121                 if (offset == 0) {
1122                         strncpy(rec->tag, tag, ZFCP_DBF_TAG_SIZE);
1123                         strncpy(rec->tag2, tag2, ZFCP_DBF_TAG_SIZE);
1124                         if (scsi_cmnd != NULL) {
1125                                 if (scsi_cmnd->device) {
1126                                         rec->scsi_id = scsi_cmnd->device->id;
1127                                         rec->scsi_lun = scsi_cmnd->device->lun;
1128                                 }
1129                                 rec->scsi_result = scsi_cmnd->result;
1130                                 rec->scsi_cmnd = (unsigned long)scsi_cmnd;
1131                                 rec->scsi_serial = scsi_cmnd->serial_number;
1132                                 memcpy(rec->scsi_opcode, &scsi_cmnd->cmnd,
1133                                         min((int)scsi_cmnd->cmd_len,
1134                                                 ZFCP_DBF_SCSI_OPCODE));
1135                                 rec->scsi_retries = scsi_cmnd->retries;
1136                                 rec->scsi_allowed = scsi_cmnd->allowed;
1137                         }
1138                         if (fsf_req != NULL) {
1139                                 fcp_rsp = (struct fcp_rsp_iu *)
1140                                     &(fsf_req->qtcb->bottom.io.fcp_rsp);
1141                                 fcp_rsp_info =
1142                                     zfcp_get_fcp_rsp_info_ptr(fcp_rsp);
1143                                 fcp_sns_info =
1144                                     zfcp_get_fcp_sns_info_ptr(fcp_rsp);
1145
1146                                 rec->type.fcp.rsp_validity =
1147                                     fcp_rsp->validity.value;
1148                                 rec->type.fcp.rsp_scsi_status =
1149                                     fcp_rsp->scsi_status;
1150                                 rec->type.fcp.rsp_resid = fcp_rsp->fcp_resid;
1151                                 if (fcp_rsp->validity.bits.fcp_rsp_len_valid)
1152                                         rec->type.fcp.rsp_code =
1153                                             *(fcp_rsp_info + 3);
1154                                 if (fcp_rsp->validity.bits.fcp_sns_len_valid) {
1155                                         buflen = min((int)fcp_rsp->fcp_sns_len,
1156                                                      ZFCP_DBF_SCSI_MAX_FCP_SNS_INFO);
1157                                         rec->type.fcp.sns_info_len = buflen;
1158                                         memcpy(rec->type.fcp.sns_info,
1159                                                fcp_sns_info,
1160                                                min(buflen,
1161                                                    ZFCP_DBF_SCSI_FCP_SNS_INFO));
1162                                         offset += min(buflen,
1163                                                       ZFCP_DBF_SCSI_FCP_SNS_INFO);
1164                                 }
1165
1166                                 rec->fsf_reqid = (unsigned long)fsf_req;
1167                                 rec->fsf_seqno = fsf_req->seq_no;
1168                                 rec->fsf_issued = fsf_req->issued;
1169                         }
1170                         rec->type.old_fsf_reqid = old_req_id;
1171                 } else {
1172                         strncpy(dump->tag, "dump", ZFCP_DBF_TAG_SIZE);
1173                         dump->total_size = buflen;
1174                         dump->offset = offset;
1175                         dump->size = min(buflen - offset,
1176                                          (int)sizeof(struct
1177                                                      zfcp_scsi_dbf_record) -
1178                                          (int)sizeof(struct zfcp_dbf_dump));
1179                         memcpy(dump->data, fcp_sns_info + offset, dump->size);
1180                         offset += dump->size;
1181                 }
1182                 debug_event(adapter->scsi_dbf, level,
1183                             rec, sizeof(struct zfcp_scsi_dbf_record));
1184         } while (offset < buflen);
1185         spin_unlock_irqrestore(&adapter->scsi_dbf_lock, flags);
1186 }
1187
1188 void
1189 zfcp_scsi_dbf_event_result(const char *tag, int level,
1190                            struct zfcp_adapter *adapter,
1191                            struct scsi_cmnd *scsi_cmnd,
1192                            struct zfcp_fsf_req *fsf_req)
1193 {
1194         _zfcp_scsi_dbf_event_common("rslt", tag, level,
1195                         adapter, scsi_cmnd, fsf_req, 0);
1196 }
1197
1198 void
1199 zfcp_scsi_dbf_event_abort(const char *tag, struct zfcp_adapter *adapter,
1200                           struct scsi_cmnd *scsi_cmnd,
1201                           struct zfcp_fsf_req *new_fsf_req,
1202                           unsigned long old_req_id)
1203 {
1204         _zfcp_scsi_dbf_event_common("abrt", tag, 1,
1205                         adapter, scsi_cmnd, new_fsf_req, old_req_id);
1206 }
1207
1208 void
1209 zfcp_scsi_dbf_event_devreset(const char *tag, u8 flag, struct zfcp_unit *unit,
1210                              struct scsi_cmnd *scsi_cmnd)
1211 {
1212         struct zfcp_adapter *adapter = unit->port->adapter;
1213
1214         _zfcp_scsi_dbf_event_common(flag == FCP_TARGET_RESET ? "trst" : "lrst",
1215                         tag, 1, adapter, scsi_cmnd, NULL, 0);
1216 }
1217
1218 static int
1219 zfcp_scsi_dbf_view_format(debug_info_t * id, struct debug_view *view,
1220                           char *out_buf, const char *in_buf)
1221 {
1222         struct zfcp_scsi_dbf_record *rec =
1223             (struct zfcp_scsi_dbf_record *)in_buf;
1224         int len = 0;
1225
1226         if (strncmp(rec->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
1227                 return 0;
1228
1229         len += zfcp_dbf_tag(out_buf + len, "tag", rec->tag);
1230         len += zfcp_dbf_tag(out_buf + len, "tag2", rec->tag2);
1231         len += zfcp_dbf_view(out_buf + len, "scsi_id", "0x%08x", rec->scsi_id);
1232         len += zfcp_dbf_view(out_buf + len, "scsi_lun", "0x%08x",
1233                              rec->scsi_lun);
1234         len += zfcp_dbf_view(out_buf + len, "scsi_result", "0x%08x",
1235                              rec->scsi_result);
1236         len += zfcp_dbf_view(out_buf + len, "scsi_cmnd", "0x%0Lx",
1237                              rec->scsi_cmnd);
1238         len += zfcp_dbf_view(out_buf + len, "scsi_serial", "0x%016Lx",
1239                              rec->scsi_serial);
1240         len += zfcp_dbf_view_dump(out_buf + len, "scsi_opcode",
1241                                   rec->scsi_opcode,
1242                                   ZFCP_DBF_SCSI_OPCODE,
1243                                   0, ZFCP_DBF_SCSI_OPCODE);
1244         len += zfcp_dbf_view(out_buf + len, "scsi_retries", "0x%02x",
1245                              rec->scsi_retries);
1246         len += zfcp_dbf_view(out_buf + len, "scsi_allowed", "0x%02x",
1247                              rec->scsi_allowed);
1248         if (strncmp(rec->tag, "abrt", ZFCP_DBF_TAG_SIZE) == 0) {
1249                 len += zfcp_dbf_view(out_buf + len, "old_fsf_reqid", "0x%0Lx",
1250                                      rec->type.old_fsf_reqid);
1251         }
1252         len += zfcp_dbf_view(out_buf + len, "fsf_reqid", "0x%0Lx",
1253                              rec->fsf_reqid);
1254         len += zfcp_dbf_view(out_buf + len, "fsf_seqno", "0x%08x",
1255                              rec->fsf_seqno);
1256         len += zfcp_dbf_stck(out_buf + len, "fsf_issued", rec->fsf_issued);
1257         if (strncmp(rec->tag, "rslt", ZFCP_DBF_TAG_SIZE) == 0) {
1258                 len +=
1259                     zfcp_dbf_view(out_buf + len, "fcp_rsp_validity", "0x%02x",
1260                                   rec->type.fcp.rsp_validity);
1261                 len +=
1262                     zfcp_dbf_view(out_buf + len, "fcp_rsp_scsi_status",
1263                                   "0x%02x", rec->type.fcp.rsp_scsi_status);
1264                 len +=
1265                     zfcp_dbf_view(out_buf + len, "fcp_rsp_resid", "0x%08x",
1266                                   rec->type.fcp.rsp_resid);
1267                 len +=
1268                     zfcp_dbf_view(out_buf + len, "fcp_rsp_code", "0x%08x",
1269                                   rec->type.fcp.rsp_code);
1270                 len +=
1271                     zfcp_dbf_view(out_buf + len, "fcp_sns_info_len", "0x%08x",
1272                                   rec->type.fcp.sns_info_len);
1273                 len +=
1274                     zfcp_dbf_view_dump(out_buf + len, "fcp_sns_info",
1275                                        rec->type.fcp.sns_info,
1276                                        min((int)rec->type.fcp.sns_info_len,
1277                                            ZFCP_DBF_SCSI_FCP_SNS_INFO), 0,
1278                                        rec->type.fcp.sns_info_len);
1279         }
1280
1281         len += sprintf(out_buf + len, "\n");
1282
1283         return len;
1284 }
1285
1286 static struct debug_view zfcp_scsi_dbf_view = {
1287         "structured",
1288         NULL,
1289         &zfcp_dbf_view_header,
1290         &zfcp_scsi_dbf_view_format,
1291         NULL,
1292         NULL
1293 };
1294
1295 /**
1296  * zfcp_adapter_debug_register - registers debug feature for an adapter
1297  * @adapter: pointer to adapter for which debug features should be registered
1298  * return: -ENOMEM on error, 0 otherwise
1299  */
1300 int zfcp_adapter_debug_register(struct zfcp_adapter *adapter)
1301 {
1302         char dbf_name[DEBUG_MAX_NAME_LEN];
1303
1304         /* debug feature area which records recovery activity */
1305         sprintf(dbf_name, "zfcp_%s_rec", zfcp_get_busid_by_adapter(adapter));
1306         adapter->rec_dbf = debug_register(dbf_name, dbfsize, 1,
1307                                           sizeof(struct zfcp_rec_dbf_record));
1308         if (!adapter->rec_dbf)
1309                 goto failed;
1310         debug_register_view(adapter->rec_dbf, &debug_hex_ascii_view);
1311         debug_register_view(adapter->rec_dbf, &zfcp_rec_dbf_view);
1312         debug_set_level(adapter->rec_dbf, 3);
1313
1314         /* debug feature area which records HBA (FSF and QDIO) conditions */
1315         sprintf(dbf_name, "zfcp_%s_hba", zfcp_get_busid_by_adapter(adapter));
1316         adapter->hba_dbf = debug_register(dbf_name, dbfsize, 1,
1317                                           sizeof(struct zfcp_hba_dbf_record));
1318         if (!adapter->hba_dbf)
1319                 goto failed;
1320         debug_register_view(adapter->hba_dbf, &debug_hex_ascii_view);
1321         debug_register_view(adapter->hba_dbf, &zfcp_hba_dbf_view);
1322         debug_set_level(adapter->hba_dbf, 3);
1323
1324         /* debug feature area which records SAN command failures and recovery */
1325         sprintf(dbf_name, "zfcp_%s_san", zfcp_get_busid_by_adapter(adapter));
1326         adapter->san_dbf = debug_register(dbf_name, dbfsize, 1,
1327                                           sizeof(struct zfcp_san_dbf_record));
1328         if (!adapter->san_dbf)
1329                 goto failed;
1330         debug_register_view(adapter->san_dbf, &debug_hex_ascii_view);
1331         debug_register_view(adapter->san_dbf, &zfcp_san_dbf_view);
1332         debug_set_level(adapter->san_dbf, 6);
1333
1334         /* debug feature area which records SCSI command failures and recovery */
1335         sprintf(dbf_name, "zfcp_%s_scsi", zfcp_get_busid_by_adapter(adapter));
1336         adapter->scsi_dbf = debug_register(dbf_name, dbfsize, 1,
1337                                            sizeof(struct zfcp_scsi_dbf_record));
1338         if (!adapter->scsi_dbf)
1339                 goto failed;
1340         debug_register_view(adapter->scsi_dbf, &debug_hex_ascii_view);
1341         debug_register_view(adapter->scsi_dbf, &zfcp_scsi_dbf_view);
1342         debug_set_level(adapter->scsi_dbf, 3);
1343
1344         return 0;
1345
1346  failed:
1347         zfcp_adapter_debug_unregister(adapter);
1348
1349         return -ENOMEM;
1350 }
1351
1352 /**
1353  * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter
1354  * @adapter: pointer to adapter for which debug features should be unregistered
1355  */
1356 void zfcp_adapter_debug_unregister(struct zfcp_adapter *adapter)
1357 {
1358         debug_unregister(adapter->scsi_dbf);
1359         debug_unregister(adapter->san_dbf);
1360         debug_unregister(adapter->hba_dbf);
1361         debug_unregister(adapter->rec_dbf);
1362         adapter->scsi_dbf = NULL;
1363         adapter->san_dbf = NULL;
1364         adapter->hba_dbf = NULL;
1365         adapter->rec_dbf = NULL;
1366 }
1367
1368 #undef ZFCP_LOG_AREA