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