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