[SCSI] zfcp: Redesign of the debug tracing for recovery actions.
[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, 2009
7  */
8
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/ctype.h>
13 #include <linux/slab.h>
14 #include <asm/debug.h>
15 #include "zfcp_dbf.h"
16 #include "zfcp_ext.h"
17 #include "zfcp_fc.h"
18
19 static u32 dbfsize = 4;
20
21 module_param(dbfsize, uint, 0400);
22 MODULE_PARM_DESC(dbfsize,
23                  "number of pages for each debug feature area (default 4)");
24
25 static void zfcp_dbf_hexdump(debug_info_t *dbf, void *to, int to_len,
26                              int level, char *from, int from_len)
27 {
28         int offset;
29         struct zfcp_dbf_dump *dump = to;
30         int room = to_len - sizeof(*dump);
31
32         for (offset = 0; offset < from_len; offset += dump->size) {
33                 memset(to, 0, to_len);
34                 strncpy(dump->tag, "dump", ZFCP_DBF_TAG_SIZE);
35                 dump->total_size = from_len;
36                 dump->offset = offset;
37                 dump->size = min(from_len - offset, room);
38                 memcpy(dump->data, from + offset, dump->size);
39                 debug_event(dbf, level, dump, dump->size + sizeof(*dump));
40         }
41 }
42
43 static void zfcp_dbf_tag(char **p, const char *label, const char *tag)
44 {
45         int i;
46
47         *p += sprintf(*p, "%-24s", label);
48         for (i = 0; i < ZFCP_DBF_TAG_SIZE; i++)
49                 *p += sprintf(*p, "%c", tag[i]);
50         *p += sprintf(*p, "\n");
51 }
52
53 static void zfcp_dbf_out(char **buf, const char *s, const char *format, ...)
54 {
55         va_list arg;
56
57         *buf += sprintf(*buf, "%-24s", s);
58         va_start(arg, format);
59         *buf += vsprintf(*buf, format, arg);
60         va_end(arg);
61         *buf += sprintf(*buf, "\n");
62 }
63
64 static void zfcp_dbf_outd(char **p, const char *label, char *buffer,
65                           int buflen, int offset, int total_size)
66 {
67         if (!offset)
68                 *p += sprintf(*p, "%-24s  ", label);
69         while (buflen--) {
70                 if (offset > 0) {
71                         if ((offset % 32) == 0)
72                                 *p += sprintf(*p, "\n%-24c  ", ' ');
73                         else if ((offset % 4) == 0)
74                                 *p += sprintf(*p, " ");
75                 }
76                 *p += sprintf(*p, "%02x", *buffer++);
77                 if (++offset == total_size) {
78                         *p += sprintf(*p, "\n");
79                         break;
80                 }
81         }
82         if (!total_size)
83                 *p += sprintf(*p, "\n");
84 }
85
86 static int zfcp_dbf_view_header(debug_info_t *id, struct debug_view *view,
87                                 int area, debug_entry_t *entry, char *out_buf)
88 {
89         struct zfcp_dbf_dump *dump = (struct zfcp_dbf_dump *)DEBUG_DATA(entry);
90         struct timespec t;
91         char *p = out_buf;
92
93         if (strncmp(dump->tag, "dump", ZFCP_DBF_TAG_SIZE) != 0) {
94                 stck_to_timespec(entry->id.stck, &t);
95                 zfcp_dbf_out(&p, "timestamp", "%011lu:%06lu",
96                              t.tv_sec, t.tv_nsec);
97                 zfcp_dbf_out(&p, "cpu", "%02i", entry->id.fields.cpuid);
98         } else  {
99                 zfcp_dbf_outd(&p, "", dump->data, dump->size, dump->offset,
100                               dump->total_size);
101                 if ((dump->offset + dump->size) == dump->total_size)
102                         p += sprintf(p, "\n");
103         }
104         return p - out_buf;
105 }
106
107 void _zfcp_dbf_hba_fsf_response(const char *tag2, int level,
108                                 struct zfcp_fsf_req *fsf_req,
109                                 struct zfcp_dbf *dbf)
110 {
111         struct fsf_qtcb *qtcb = fsf_req->qtcb;
112         union fsf_prot_status_qual *prot_status_qual =
113                                         &qtcb->prefix.prot_status_qual;
114         union fsf_status_qual *fsf_status_qual = &qtcb->header.fsf_status_qual;
115         struct scsi_cmnd *scsi_cmnd;
116         struct zfcp_port *port;
117         struct zfcp_unit *unit;
118         struct zfcp_send_els *send_els;
119         struct zfcp_dbf_hba_record *rec = &dbf->hba_buf;
120         struct zfcp_dbf_hba_record_response *response = &rec->u.response;
121         unsigned long flags;
122
123         spin_lock_irqsave(&dbf->hba_lock, flags);
124         memset(rec, 0, sizeof(*rec));
125         strncpy(rec->tag, "resp", ZFCP_DBF_TAG_SIZE);
126         strncpy(rec->tag2, tag2, ZFCP_DBF_TAG_SIZE);
127
128         response->fsf_command = fsf_req->fsf_command;
129         response->fsf_reqid = fsf_req->req_id;
130         response->fsf_seqno = fsf_req->seq_no;
131         response->fsf_issued = fsf_req->issued;
132         response->fsf_prot_status = qtcb->prefix.prot_status;
133         response->fsf_status = qtcb->header.fsf_status;
134         memcpy(response->fsf_prot_status_qual,
135                prot_status_qual, FSF_PROT_STATUS_QUAL_SIZE);
136         memcpy(response->fsf_status_qual,
137                fsf_status_qual, FSF_STATUS_QUALIFIER_SIZE);
138         response->fsf_req_status = fsf_req->status;
139         response->sbal_first = fsf_req->qdio_req.sbal_first;
140         response->sbal_last = fsf_req->qdio_req.sbal_last;
141         response->sbal_response = fsf_req->qdio_req.sbal_response;
142         response->pool = fsf_req->pool != NULL;
143         response->erp_action = (unsigned long)fsf_req->erp_action;
144
145         switch (fsf_req->fsf_command) {
146         case FSF_QTCB_FCP_CMND:
147                 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
148                         break;
149                 scsi_cmnd = (struct scsi_cmnd *)fsf_req->data;
150                 if (scsi_cmnd) {
151                         response->u.fcp.cmnd = (unsigned long)scsi_cmnd;
152                         response->u.fcp.data_dir =
153                                 qtcb->bottom.io.data_direction;
154                 }
155                 break;
156
157         case FSF_QTCB_OPEN_PORT_WITH_DID:
158         case FSF_QTCB_CLOSE_PORT:
159         case FSF_QTCB_CLOSE_PHYSICAL_PORT:
160                 port = (struct zfcp_port *)fsf_req->data;
161                 response->u.port.wwpn = port->wwpn;
162                 response->u.port.d_id = port->d_id;
163                 response->u.port.port_handle = qtcb->header.port_handle;
164                 break;
165
166         case FSF_QTCB_OPEN_LUN:
167         case FSF_QTCB_CLOSE_LUN:
168                 unit = (struct zfcp_unit *)fsf_req->data;
169                 port = unit->port;
170                 response->u.unit.wwpn = port->wwpn;
171                 response->u.unit.fcp_lun = unit->fcp_lun;
172                 response->u.unit.port_handle = qtcb->header.port_handle;
173                 response->u.unit.lun_handle = qtcb->header.lun_handle;
174                 break;
175
176         case FSF_QTCB_SEND_ELS:
177                 send_els = (struct zfcp_send_els *)fsf_req->data;
178                 response->u.els.d_id = ntoh24(qtcb->bottom.support.d_id);
179                 break;
180
181         case FSF_QTCB_ABORT_FCP_CMND:
182         case FSF_QTCB_SEND_GENERIC:
183         case FSF_QTCB_EXCHANGE_CONFIG_DATA:
184         case FSF_QTCB_EXCHANGE_PORT_DATA:
185         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
186         case FSF_QTCB_UPLOAD_CONTROL_FILE:
187                 break;
188         }
189
190         debug_event(dbf->hba, level, rec, sizeof(*rec));
191
192         /* have fcp channel microcode fixed to use as little as possible */
193         if (fsf_req->fsf_command != FSF_QTCB_FCP_CMND) {
194                 /* adjust length skipping trailing zeros */
195                 char *buf = (char *)qtcb + qtcb->header.log_start;
196                 int len = qtcb->header.log_length;
197                 for (; len && !buf[len - 1]; len--);
198                 zfcp_dbf_hexdump(dbf->hba, rec, sizeof(*rec), level, buf,
199                                  len);
200         }
201
202         spin_unlock_irqrestore(&dbf->hba_lock, flags);
203 }
204
205 void _zfcp_dbf_hba_fsf_unsol(const char *tag, int level, struct zfcp_dbf *dbf,
206                              struct fsf_status_read_buffer *status_buffer)
207 {
208         struct zfcp_dbf_hba_record *rec = &dbf->hba_buf;
209         unsigned long flags;
210
211         spin_lock_irqsave(&dbf->hba_lock, flags);
212         memset(rec, 0, sizeof(*rec));
213         strncpy(rec->tag, "stat", ZFCP_DBF_TAG_SIZE);
214         strncpy(rec->tag2, tag, ZFCP_DBF_TAG_SIZE);
215
216         rec->u.status.failed = atomic_read(&dbf->adapter->stat_miss);
217         if (status_buffer != NULL) {
218                 rec->u.status.status_type = status_buffer->status_type;
219                 rec->u.status.status_subtype = status_buffer->status_subtype;
220                 memcpy(&rec->u.status.queue_designator,
221                        &status_buffer->queue_designator,
222                        sizeof(struct fsf_queue_designator));
223
224                 switch (status_buffer->status_type) {
225                 case FSF_STATUS_READ_SENSE_DATA_AVAIL:
226                         rec->u.status.payload_size =
227                             ZFCP_DBF_UNSOL_PAYLOAD_SENSE_DATA_AVAIL;
228                         break;
229
230                 case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
231                         rec->u.status.payload_size =
232                             ZFCP_DBF_UNSOL_PAYLOAD_BIT_ERROR_THRESHOLD;
233                         break;
234
235                 case FSF_STATUS_READ_LINK_DOWN:
236                         switch (status_buffer->status_subtype) {
237                         case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
238                         case FSF_STATUS_READ_SUB_FDISC_FAILED:
239                                 rec->u.status.payload_size =
240                                         sizeof(struct fsf_link_down_info);
241                         }
242                         break;
243
244                 case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
245                         rec->u.status.payload_size =
246                             ZFCP_DBF_UNSOL_PAYLOAD_FEATURE_UPDATE_ALERT;
247                         break;
248                 }
249                 memcpy(&rec->u.status.payload,
250                        &status_buffer->payload, rec->u.status.payload_size);
251         }
252
253         debug_event(dbf->hba, level, rec, sizeof(*rec));
254         spin_unlock_irqrestore(&dbf->hba_lock, flags);
255 }
256
257 /**
258  * zfcp_dbf_hba_qdio - trace event for QDIO related failure
259  * @qdio: qdio structure affected by this QDIO related event
260  * @qdio_error: as passed by qdio module
261  * @sbal_index: first buffer with error condition, as passed by qdio module
262  * @sbal_count: number of buffers affected, as passed by qdio module
263  */
264 void zfcp_dbf_hba_qdio(struct zfcp_dbf *dbf, unsigned int qdio_error,
265                        int sbal_index, int sbal_count)
266 {
267         struct zfcp_dbf_hba_record *r = &dbf->hba_buf;
268         unsigned long flags;
269
270         spin_lock_irqsave(&dbf->hba_lock, flags);
271         memset(r, 0, sizeof(*r));
272         strncpy(r->tag, "qdio", ZFCP_DBF_TAG_SIZE);
273         r->u.qdio.qdio_error = qdio_error;
274         r->u.qdio.sbal_index = sbal_index;
275         r->u.qdio.sbal_count = sbal_count;
276         debug_event(dbf->hba, 0, r, sizeof(*r));
277         spin_unlock_irqrestore(&dbf->hba_lock, flags);
278 }
279
280 /**
281  * zfcp_dbf_hba_berr - trace event for bit error threshold
282  * @dbf: dbf structure affected by this QDIO related event
283  * @req: fsf request
284  */
285 void zfcp_dbf_hba_berr(struct zfcp_dbf *dbf, struct zfcp_fsf_req *req)
286 {
287         struct zfcp_dbf_hba_record *r = &dbf->hba_buf;
288         struct fsf_status_read_buffer *sr_buf = req->data;
289         struct fsf_bit_error_payload *err = &sr_buf->payload.bit_error;
290         unsigned long flags;
291
292         spin_lock_irqsave(&dbf->hba_lock, flags);
293         memset(r, 0, sizeof(*r));
294         strncpy(r->tag, "berr", ZFCP_DBF_TAG_SIZE);
295         memcpy(&r->u.berr, err, sizeof(struct fsf_bit_error_payload));
296         debug_event(dbf->hba, 0, r, sizeof(*r));
297         spin_unlock_irqrestore(&dbf->hba_lock, flags);
298 }
299 static void zfcp_dbf_hba_view_response(char **p,
300                                        struct zfcp_dbf_hba_record_response *r)
301 {
302         struct timespec t;
303
304         zfcp_dbf_out(p, "fsf_command", "0x%08x", r->fsf_command);
305         zfcp_dbf_out(p, "fsf_reqid", "0x%0Lx", r->fsf_reqid);
306         zfcp_dbf_out(p, "fsf_seqno", "0x%08x", r->fsf_seqno);
307         stck_to_timespec(r->fsf_issued, &t);
308         zfcp_dbf_out(p, "fsf_issued", "%011lu:%06lu", t.tv_sec, t.tv_nsec);
309         zfcp_dbf_out(p, "fsf_prot_status", "0x%08x", r->fsf_prot_status);
310         zfcp_dbf_out(p, "fsf_status", "0x%08x", r->fsf_status);
311         zfcp_dbf_outd(p, "fsf_prot_status_qual", r->fsf_prot_status_qual,
312                       FSF_PROT_STATUS_QUAL_SIZE, 0, FSF_PROT_STATUS_QUAL_SIZE);
313         zfcp_dbf_outd(p, "fsf_status_qual", r->fsf_status_qual,
314                       FSF_STATUS_QUALIFIER_SIZE, 0, FSF_STATUS_QUALIFIER_SIZE);
315         zfcp_dbf_out(p, "fsf_req_status", "0x%08x", r->fsf_req_status);
316         zfcp_dbf_out(p, "sbal_first", "0x%02x", r->sbal_first);
317         zfcp_dbf_out(p, "sbal_last", "0x%02x", r->sbal_last);
318         zfcp_dbf_out(p, "sbal_response", "0x%02x", r->sbal_response);
319         zfcp_dbf_out(p, "pool", "0x%02x", r->pool);
320
321         switch (r->fsf_command) {
322         case FSF_QTCB_FCP_CMND:
323                 if (r->fsf_req_status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
324                         break;
325                 zfcp_dbf_out(p, "data_direction", "0x%04x", r->u.fcp.data_dir);
326                 zfcp_dbf_out(p, "scsi_cmnd", "0x%0Lx", r->u.fcp.cmnd);
327                 *p += sprintf(*p, "\n");
328                 break;
329
330         case FSF_QTCB_OPEN_PORT_WITH_DID:
331         case FSF_QTCB_CLOSE_PORT:
332         case FSF_QTCB_CLOSE_PHYSICAL_PORT:
333                 zfcp_dbf_out(p, "wwpn", "0x%016Lx", r->u.port.wwpn);
334                 zfcp_dbf_out(p, "d_id", "0x%06x", r->u.port.d_id);
335                 zfcp_dbf_out(p, "port_handle", "0x%08x", r->u.port.port_handle);
336                 break;
337
338         case FSF_QTCB_OPEN_LUN:
339         case FSF_QTCB_CLOSE_LUN:
340                 zfcp_dbf_out(p, "wwpn", "0x%016Lx", r->u.unit.wwpn);
341                 zfcp_dbf_out(p, "fcp_lun", "0x%016Lx", r->u.unit.fcp_lun);
342                 zfcp_dbf_out(p, "port_handle", "0x%08x", r->u.unit.port_handle);
343                 zfcp_dbf_out(p, "lun_handle", "0x%08x", r->u.unit.lun_handle);
344                 break;
345
346         case FSF_QTCB_SEND_ELS:
347                 zfcp_dbf_out(p, "d_id", "0x%06x", r->u.els.d_id);
348                 break;
349
350         case FSF_QTCB_ABORT_FCP_CMND:
351         case FSF_QTCB_SEND_GENERIC:
352         case FSF_QTCB_EXCHANGE_CONFIG_DATA:
353         case FSF_QTCB_EXCHANGE_PORT_DATA:
354         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
355         case FSF_QTCB_UPLOAD_CONTROL_FILE:
356                 break;
357         }
358 }
359
360 static void zfcp_dbf_hba_view_status(char **p,
361                                      struct zfcp_dbf_hba_record_status *r)
362 {
363         zfcp_dbf_out(p, "failed", "0x%02x", r->failed);
364         zfcp_dbf_out(p, "status_type", "0x%08x", r->status_type);
365         zfcp_dbf_out(p, "status_subtype", "0x%08x", r->status_subtype);
366         zfcp_dbf_outd(p, "queue_designator", (char *)&r->queue_designator,
367                       sizeof(struct fsf_queue_designator), 0,
368                       sizeof(struct fsf_queue_designator));
369         zfcp_dbf_outd(p, "payload", (char *)&r->payload, r->payload_size, 0,
370                       r->payload_size);
371 }
372
373 static void zfcp_dbf_hba_view_qdio(char **p, struct zfcp_dbf_hba_record_qdio *r)
374 {
375         zfcp_dbf_out(p, "qdio_error", "0x%08x", r->qdio_error);
376         zfcp_dbf_out(p, "sbal_index", "0x%02x", r->sbal_index);
377         zfcp_dbf_out(p, "sbal_count", "0x%02x", r->sbal_count);
378 }
379
380 static void zfcp_dbf_hba_view_berr(char **p, struct fsf_bit_error_payload *r)
381 {
382         zfcp_dbf_out(p, "link_failures", "%d", r->link_failure_error_count);
383         zfcp_dbf_out(p, "loss_of_sync_err", "%d", r->loss_of_sync_error_count);
384         zfcp_dbf_out(p, "loss_of_sig_err", "%d", r->loss_of_signal_error_count);
385         zfcp_dbf_out(p, "prim_seq_err", "%d",
386                      r->primitive_sequence_error_count);
387         zfcp_dbf_out(p, "inval_trans_word_err", "%d",
388                      r->invalid_transmission_word_error_count);
389         zfcp_dbf_out(p, "CRC_errors", "%d", r->crc_error_count);
390         zfcp_dbf_out(p, "prim_seq_event_to", "%d",
391                      r->primitive_sequence_event_timeout_count);
392         zfcp_dbf_out(p, "elast_buf_overrun_err", "%d",
393                      r->elastic_buffer_overrun_error_count);
394         zfcp_dbf_out(p, "adv_rec_buf2buf_cred", "%d",
395                      r->advertised_receive_b2b_credit);
396         zfcp_dbf_out(p, "curr_rec_buf2buf_cred", "%d",
397                      r->current_receive_b2b_credit);
398         zfcp_dbf_out(p, "adv_trans_buf2buf_cred", "%d",
399                      r->advertised_transmit_b2b_credit);
400         zfcp_dbf_out(p, "curr_trans_buf2buf_cred", "%d",
401                      r->current_transmit_b2b_credit);
402 }
403
404 static int zfcp_dbf_hba_view_format(debug_info_t *id, struct debug_view *view,
405                                     char *out_buf, const char *in_buf)
406 {
407         struct zfcp_dbf_hba_record *r = (struct zfcp_dbf_hba_record *)in_buf;
408         char *p = out_buf;
409
410         if (strncmp(r->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
411                 return 0;
412
413         zfcp_dbf_tag(&p, "tag", r->tag);
414         if (isalpha(r->tag2[0]))
415                 zfcp_dbf_tag(&p, "tag2", r->tag2);
416
417         if (strncmp(r->tag, "resp", ZFCP_DBF_TAG_SIZE) == 0)
418                 zfcp_dbf_hba_view_response(&p, &r->u.response);
419         else if (strncmp(r->tag, "stat", ZFCP_DBF_TAG_SIZE) == 0)
420                 zfcp_dbf_hba_view_status(&p, &r->u.status);
421         else if (strncmp(r->tag, "qdio", ZFCP_DBF_TAG_SIZE) == 0)
422                 zfcp_dbf_hba_view_qdio(&p, &r->u.qdio);
423         else if (strncmp(r->tag, "berr", ZFCP_DBF_TAG_SIZE) == 0)
424                 zfcp_dbf_hba_view_berr(&p, &r->u.berr);
425
426         if (strncmp(r->tag, "resp", ZFCP_DBF_TAG_SIZE) != 0)
427                 p += sprintf(p, "\n");
428         return p - out_buf;
429 }
430
431 static struct debug_view zfcp_dbf_hba_view = {
432         .name = "structured",
433         .header_proc = zfcp_dbf_view_header,
434         .format_proc = zfcp_dbf_hba_view_format,
435 };
436
437 static void zfcp_dbf_set_common(struct zfcp_dbf_rec *rec,
438                                 struct zfcp_adapter *adapter,
439                                 struct zfcp_port *port,
440                                 struct scsi_device *sdev)
441 {
442         rec->adapter_status = atomic_read(&adapter->status);
443         if (port) {
444                 rec->port_status = atomic_read(&port->status);
445                 rec->wwpn = port->wwpn;
446                 rec->d_id = port->d_id;
447         }
448         if (sdev) {
449                 rec->lun_status = atomic_read(&sdev_to_zfcp(sdev)->status);
450                 rec->lun = zfcp_scsi_dev_lun(sdev);
451         }
452 }
453
454 /**
455  * zfcp_dbf_rec_trig - trace event related to triggered recovery
456  * @tag: identifier for event
457  * @adapter: adapter on which the erp_action should run
458  * @port: remote port involved in the erp_action
459  * @sdev: scsi device involved in the erp_action
460  * @want: wanted erp_action
461  * @need: required erp_action
462  *
463  * The adapter->erp_lock has to be held.
464  */
465 void zfcp_dbf_rec_trig(char *tag, struct zfcp_adapter *adapter,
466                        struct zfcp_port *port, struct scsi_device *sdev,
467                        u8 want, u8 need)
468 {
469         struct zfcp_dbf *dbf = adapter->dbf;
470         struct zfcp_dbf_rec *rec = &dbf->rec_buf;
471         struct list_head *entry;
472         unsigned long flags;
473
474         spin_lock_irqsave(&dbf->rec_lock, flags);
475         memset(rec, 0, sizeof(*rec));
476
477         rec->id = ZFCP_DBF_REC_TRIG;
478         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
479         zfcp_dbf_set_common(rec, adapter, port, sdev);
480
481         list_for_each(entry, &adapter->erp_ready_head)
482                 rec->u.trig.ready++;
483
484         list_for_each(entry, &adapter->erp_running_head)
485                 rec->u.trig.running++;
486
487         rec->u.trig.want = want;
488         rec->u.trig.need = need;
489
490         debug_event(dbf->rec, 1, rec, sizeof(*rec));
491         spin_unlock_irqrestore(&dbf->rec_lock, flags);
492 }
493
494
495 /**
496  * zfcp_dbf_rec_run - trace event related to running recovery
497  * @tag: identifier for event
498  * @erp: erp_action running
499  */
500 void zfcp_dbf_rec_run(char *tag, struct zfcp_erp_action *erp)
501 {
502         struct zfcp_dbf *dbf = erp->adapter->dbf;
503         struct zfcp_dbf_rec *rec = &dbf->rec_buf;
504         unsigned long flags;
505
506         spin_lock_irqsave(&dbf->rec_lock, flags);
507         memset(rec, 0, sizeof(*rec));
508
509         rec->id = ZFCP_DBF_REC_RUN;
510         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
511         zfcp_dbf_set_common(rec, erp->adapter, erp->port, erp->sdev);
512
513         rec->u.run.fsf_req_id = erp->fsf_req_id;
514         rec->u.run.rec_status = erp->status;
515         rec->u.run.rec_step = erp->step;
516         rec->u.run.rec_action = erp->action;
517
518         if (erp->sdev)
519                 rec->u.run.rec_count =
520                         atomic_read(&sdev_to_zfcp(erp->sdev)->erp_counter);
521         else if (erp->port)
522                 rec->u.run.rec_count = atomic_read(&erp->port->erp_counter);
523         else
524                 rec->u.run.rec_count = atomic_read(&erp->adapter->erp_counter);
525
526         debug_event(dbf->rec, 1, rec, sizeof(*rec));
527         spin_unlock_irqrestore(&dbf->rec_lock, flags);
528 }
529
530 /**
531  * zfcp_dbf_san_ct_request - trace event for issued CT request
532  * @fsf_req: request containing issued CT data
533  * @d_id: destination id where ct request is sent to
534  */
535 void zfcp_dbf_san_ct_request(struct zfcp_fsf_req *fsf_req, u32 d_id)
536 {
537         struct zfcp_fsf_ct_els *ct = (struct zfcp_fsf_ct_els *)fsf_req->data;
538         struct zfcp_adapter *adapter = fsf_req->adapter;
539         struct zfcp_dbf *dbf = adapter->dbf;
540         struct fc_ct_hdr *hdr = sg_virt(ct->req);
541         struct zfcp_dbf_san_record *r = &dbf->san_buf;
542         struct zfcp_dbf_san_record_ct_request *oct = &r->u.ct_req;
543         int level = 3;
544         unsigned long flags;
545
546         spin_lock_irqsave(&dbf->san_lock, flags);
547         memset(r, 0, sizeof(*r));
548         strncpy(r->tag, "octc", ZFCP_DBF_TAG_SIZE);
549         r->fsf_reqid = fsf_req->req_id;
550         r->fsf_seqno = fsf_req->seq_no;
551         oct->d_id = d_id;
552         oct->cmd_req_code = hdr->ct_cmd;
553         oct->revision = hdr->ct_rev;
554         oct->gs_type = hdr->ct_fs_type;
555         oct->gs_subtype = hdr->ct_fs_subtype;
556         oct->options = hdr->ct_options;
557         oct->max_res_size = hdr->ct_mr_size;
558         oct->len = min((int)ct->req->length - (int)sizeof(struct fc_ct_hdr),
559                        ZFCP_DBF_SAN_MAX_PAYLOAD);
560         debug_event(dbf->san, level, r, sizeof(*r));
561         zfcp_dbf_hexdump(dbf->san, r, sizeof(*r), level,
562                          (void *)hdr + sizeof(struct fc_ct_hdr), oct->len);
563         spin_unlock_irqrestore(&dbf->san_lock, flags);
564 }
565
566 /**
567  * zfcp_dbf_san_ct_response - trace event for completion of CT request
568  * @fsf_req: request containing CT response
569  */
570 void zfcp_dbf_san_ct_response(struct zfcp_fsf_req *fsf_req)
571 {
572         struct zfcp_fsf_ct_els *ct = (struct zfcp_fsf_ct_els *)fsf_req->data;
573         struct zfcp_adapter *adapter = fsf_req->adapter;
574         struct fc_ct_hdr *hdr = sg_virt(ct->resp);
575         struct zfcp_dbf *dbf = adapter->dbf;
576         struct zfcp_dbf_san_record *r = &dbf->san_buf;
577         struct zfcp_dbf_san_record_ct_response *rct = &r->u.ct_resp;
578         int level = 3;
579         unsigned long flags;
580
581         spin_lock_irqsave(&dbf->san_lock, flags);
582         memset(r, 0, sizeof(*r));
583         strncpy(r->tag, "rctc", ZFCP_DBF_TAG_SIZE);
584         r->fsf_reqid = fsf_req->req_id;
585         r->fsf_seqno = fsf_req->seq_no;
586         rct->cmd_rsp_code = hdr->ct_cmd;
587         rct->revision = hdr->ct_rev;
588         rct->reason_code = hdr->ct_reason;
589         rct->expl = hdr->ct_explan;
590         rct->vendor_unique = hdr->ct_vendor;
591         rct->max_res_size = hdr->ct_mr_size;
592         rct->len = min((int)ct->resp->length - (int)sizeof(struct fc_ct_hdr),
593                        ZFCP_DBF_SAN_MAX_PAYLOAD);
594         debug_event(dbf->san, level, r, sizeof(*r));
595         zfcp_dbf_hexdump(dbf->san, r, sizeof(*r), level,
596                          (void *)hdr + sizeof(struct fc_ct_hdr), rct->len);
597         spin_unlock_irqrestore(&dbf->san_lock, flags);
598 }
599
600 static void zfcp_dbf_san_els(const char *tag, int level,
601                              struct zfcp_fsf_req *fsf_req, u32 d_id,
602                              void *buffer, int buflen)
603 {
604         struct zfcp_adapter *adapter = fsf_req->adapter;
605         struct zfcp_dbf *dbf = adapter->dbf;
606         struct zfcp_dbf_san_record *rec = &dbf->san_buf;
607         unsigned long flags;
608
609         spin_lock_irqsave(&dbf->san_lock, flags);
610         memset(rec, 0, sizeof(*rec));
611         strncpy(rec->tag, tag, ZFCP_DBF_TAG_SIZE);
612         rec->fsf_reqid = fsf_req->req_id;
613         rec->fsf_seqno = fsf_req->seq_no;
614         rec->u.els.d_id = d_id;
615         debug_event(dbf->san, level, rec, sizeof(*rec));
616         zfcp_dbf_hexdump(dbf->san, rec, sizeof(*rec), level,
617                          buffer, min(buflen, ZFCP_DBF_SAN_MAX_PAYLOAD));
618         spin_unlock_irqrestore(&dbf->san_lock, flags);
619 }
620
621 /**
622  * zfcp_dbf_san_els_request - trace event for issued ELS
623  * @fsf_req: request containing issued ELS
624  */
625 void zfcp_dbf_san_els_request(struct zfcp_fsf_req *fsf_req)
626 {
627         struct zfcp_fsf_ct_els *els = (struct zfcp_fsf_ct_els *)fsf_req->data;
628         u32 d_id = ntoh24(fsf_req->qtcb->bottom.support.d_id);
629
630         zfcp_dbf_san_els("oels", 2, fsf_req, d_id,
631                          sg_virt(els->req), els->req->length);
632 }
633
634 /**
635  * zfcp_dbf_san_els_response - trace event for completed ELS
636  * @fsf_req: request containing ELS response
637  */
638 void zfcp_dbf_san_els_response(struct zfcp_fsf_req *fsf_req)
639 {
640         struct zfcp_fsf_ct_els *els = (struct zfcp_fsf_ct_els *)fsf_req->data;
641         u32 d_id = ntoh24(fsf_req->qtcb->bottom.support.d_id);
642
643         zfcp_dbf_san_els("rels", 2, fsf_req, d_id,
644                                sg_virt(els->resp), els->resp->length);
645 }
646
647 /**
648  * zfcp_dbf_san_incoming_els - trace event for incomig ELS
649  * @fsf_req: request containing unsolicited status buffer with incoming ELS
650  */
651 void zfcp_dbf_san_incoming_els(struct zfcp_fsf_req *fsf_req)
652 {
653         struct fsf_status_read_buffer *buf =
654                         (struct fsf_status_read_buffer *)fsf_req->data;
655         int length = (int)buf->length -
656                      (int)((void *)&buf->payload - (void *)buf);
657
658         zfcp_dbf_san_els("iels", 1, fsf_req, ntoh24(buf->d_id),
659                                (void *)buf->payload.data, length);
660 }
661
662 static int zfcp_dbf_san_view_format(debug_info_t *id, struct debug_view *view,
663                                     char *out_buf, const char *in_buf)
664 {
665         struct zfcp_dbf_san_record *r = (struct zfcp_dbf_san_record *)in_buf;
666         char *p = out_buf;
667
668         if (strncmp(r->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
669                 return 0;
670
671         zfcp_dbf_tag(&p, "tag", r->tag);
672         zfcp_dbf_out(&p, "fsf_reqid", "0x%0Lx", r->fsf_reqid);
673         zfcp_dbf_out(&p, "fsf_seqno", "0x%08x", r->fsf_seqno);
674
675         if (strncmp(r->tag, "octc", ZFCP_DBF_TAG_SIZE) == 0) {
676                 struct zfcp_dbf_san_record_ct_request *ct = &r->u.ct_req;
677                 zfcp_dbf_out(&p, "d_id", "0x%06x", ct->d_id);
678                 zfcp_dbf_out(&p, "cmd_req_code", "0x%04x", ct->cmd_req_code);
679                 zfcp_dbf_out(&p, "revision", "0x%02x", ct->revision);
680                 zfcp_dbf_out(&p, "gs_type", "0x%02x", ct->gs_type);
681                 zfcp_dbf_out(&p, "gs_subtype", "0x%02x", ct->gs_subtype);
682                 zfcp_dbf_out(&p, "options", "0x%02x", ct->options);
683                 zfcp_dbf_out(&p, "max_res_size", "0x%04x", ct->max_res_size);
684         } else if (strncmp(r->tag, "rctc", ZFCP_DBF_TAG_SIZE) == 0) {
685                 struct zfcp_dbf_san_record_ct_response *ct = &r->u.ct_resp;
686                 zfcp_dbf_out(&p, "cmd_rsp_code", "0x%04x", ct->cmd_rsp_code);
687                 zfcp_dbf_out(&p, "revision", "0x%02x", ct->revision);
688                 zfcp_dbf_out(&p, "reason_code", "0x%02x", ct->reason_code);
689                 zfcp_dbf_out(&p, "reason_code_expl", "0x%02x", ct->expl);
690                 zfcp_dbf_out(&p, "vendor_unique", "0x%02x", ct->vendor_unique);
691                 zfcp_dbf_out(&p, "max_res_size", "0x%04x", ct->max_res_size);
692         } else if (strncmp(r->tag, "oels", ZFCP_DBF_TAG_SIZE) == 0 ||
693                    strncmp(r->tag, "rels", ZFCP_DBF_TAG_SIZE) == 0 ||
694                    strncmp(r->tag, "iels", ZFCP_DBF_TAG_SIZE) == 0) {
695                 struct zfcp_dbf_san_record_els *els = &r->u.els;
696                 zfcp_dbf_out(&p, "d_id", "0x%06x", els->d_id);
697         }
698         return p - out_buf;
699 }
700
701 static struct debug_view zfcp_dbf_san_view = {
702         .name = "structured",
703         .header_proc = zfcp_dbf_view_header,
704         .format_proc = zfcp_dbf_san_view_format,
705 };
706
707 void _zfcp_dbf_scsi(const char *tag, const char *tag2, int level,
708                     struct zfcp_dbf *dbf, struct scsi_cmnd *scsi_cmnd,
709                     struct zfcp_fsf_req *fsf_req, unsigned long old_req_id)
710 {
711         struct zfcp_dbf_scsi_record *rec = &dbf->scsi_buf;
712         struct zfcp_dbf_dump *dump = (struct zfcp_dbf_dump *)rec;
713         unsigned long flags;
714         struct fcp_resp_with_ext *fcp_rsp;
715         struct fcp_resp_rsp_info *fcp_rsp_info = NULL;
716         char *fcp_sns_info = NULL;
717         int offset = 0, buflen = 0;
718
719         spin_lock_irqsave(&dbf->scsi_lock, flags);
720         do {
721                 memset(rec, 0, sizeof(*rec));
722                 if (offset == 0) {
723                         strncpy(rec->tag, tag, ZFCP_DBF_TAG_SIZE);
724                         strncpy(rec->tag2, tag2, ZFCP_DBF_TAG_SIZE);
725                         if (scsi_cmnd != NULL) {
726                                 if (scsi_cmnd->device) {
727                                         rec->scsi_id = scsi_cmnd->device->id;
728                                         rec->scsi_lun = scsi_cmnd->device->lun;
729                                 }
730                                 rec->scsi_result = scsi_cmnd->result;
731                                 rec->scsi_cmnd = (unsigned long)scsi_cmnd;
732                                 memcpy(rec->scsi_opcode, scsi_cmnd->cmnd,
733                                         min((int)scsi_cmnd->cmd_len,
734                                                 ZFCP_DBF_SCSI_OPCODE));
735                                 rec->scsi_retries = scsi_cmnd->retries;
736                                 rec->scsi_allowed = scsi_cmnd->allowed;
737                         }
738                         if (fsf_req != NULL) {
739                                 fcp_rsp = (struct fcp_resp_with_ext *)
740                                         &(fsf_req->qtcb->bottom.io.fcp_rsp);
741                                 fcp_rsp_info = (struct fcp_resp_rsp_info *)
742                                         &fcp_rsp[1];
743                                 fcp_sns_info = (char *) &fcp_rsp[1];
744                                 if (fcp_rsp->resp.fr_flags & FCP_RSP_LEN_VAL)
745                                         fcp_sns_info += fcp_rsp->ext.fr_sns_len;
746
747                                 rec->rsp_validity = fcp_rsp->resp.fr_flags;
748                                 rec->rsp_scsi_status = fcp_rsp->resp.fr_status;
749                                 rec->rsp_resid = fcp_rsp->ext.fr_resid;
750                                 if (fcp_rsp->resp.fr_flags & FCP_RSP_LEN_VAL)
751                                         rec->rsp_code = fcp_rsp_info->rsp_code;
752                                 if (fcp_rsp->resp.fr_flags & FCP_SNS_LEN_VAL) {
753                                         buflen = min(fcp_rsp->ext.fr_sns_len,
754                                            (u32)ZFCP_DBF_SCSI_MAX_FCP_SNS_INFO);
755                                         rec->sns_info_len = buflen;
756                                         memcpy(rec->sns_info, fcp_sns_info,
757                                                min(buflen,
758                                                    ZFCP_DBF_SCSI_FCP_SNS_INFO));
759                                         offset += min(buflen,
760                                                       ZFCP_DBF_SCSI_FCP_SNS_INFO);
761                                 }
762
763                                 rec->fsf_reqid = fsf_req->req_id;
764                                 rec->fsf_seqno = fsf_req->seq_no;
765                                 rec->fsf_issued = fsf_req->issued;
766                         }
767                         rec->old_fsf_reqid = old_req_id;
768                 } else {
769                         strncpy(dump->tag, "dump", ZFCP_DBF_TAG_SIZE);
770                         dump->total_size = buflen;
771                         dump->offset = offset;
772                         dump->size = min(buflen - offset,
773                                          (int)sizeof(struct
774                                                      zfcp_dbf_scsi_record) -
775                                          (int)sizeof(struct zfcp_dbf_dump));
776                         memcpy(dump->data, fcp_sns_info + offset, dump->size);
777                         offset += dump->size;
778                 }
779                 debug_event(dbf->scsi, level, rec, sizeof(*rec));
780         } while (offset < buflen);
781         spin_unlock_irqrestore(&dbf->scsi_lock, flags);
782 }
783
784 static int zfcp_dbf_scsi_view_format(debug_info_t *id, struct debug_view *view,
785                                      char *out_buf, const char *in_buf)
786 {
787         struct zfcp_dbf_scsi_record *r = (struct zfcp_dbf_scsi_record *)in_buf;
788         struct timespec t;
789         char *p = out_buf;
790
791         if (strncmp(r->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
792                 return 0;
793
794         zfcp_dbf_tag(&p, "tag", r->tag);
795         zfcp_dbf_tag(&p, "tag2", r->tag2);
796         zfcp_dbf_out(&p, "scsi_id", "0x%08x", r->scsi_id);
797         zfcp_dbf_out(&p, "scsi_lun", "0x%08x", r->scsi_lun);
798         zfcp_dbf_out(&p, "scsi_result", "0x%08x", r->scsi_result);
799         zfcp_dbf_out(&p, "scsi_cmnd", "0x%0Lx", r->scsi_cmnd);
800         zfcp_dbf_outd(&p, "scsi_opcode", r->scsi_opcode, ZFCP_DBF_SCSI_OPCODE,
801                       0, ZFCP_DBF_SCSI_OPCODE);
802         zfcp_dbf_out(&p, "scsi_retries", "0x%02x", r->scsi_retries);
803         zfcp_dbf_out(&p, "scsi_allowed", "0x%02x", r->scsi_allowed);
804         if (strncmp(r->tag, "abrt", ZFCP_DBF_TAG_SIZE) == 0)
805                 zfcp_dbf_out(&p, "old_fsf_reqid", "0x%0Lx", r->old_fsf_reqid);
806         zfcp_dbf_out(&p, "fsf_reqid", "0x%0Lx", r->fsf_reqid);
807         zfcp_dbf_out(&p, "fsf_seqno", "0x%08x", r->fsf_seqno);
808         stck_to_timespec(r->fsf_issued, &t);
809         zfcp_dbf_out(&p, "fsf_issued", "%011lu:%06lu", t.tv_sec, t.tv_nsec);
810
811         if (strncmp(r->tag, "rslt", ZFCP_DBF_TAG_SIZE) == 0) {
812                 zfcp_dbf_out(&p, "fcp_rsp_validity", "0x%02x", r->rsp_validity);
813                 zfcp_dbf_out(&p, "fcp_rsp_scsi_status", "0x%02x",
814                              r->rsp_scsi_status);
815                 zfcp_dbf_out(&p, "fcp_rsp_resid", "0x%08x", r->rsp_resid);
816                 zfcp_dbf_out(&p, "fcp_rsp_code", "0x%08x", r->rsp_code);
817                 zfcp_dbf_out(&p, "fcp_sns_info_len", "0x%08x", r->sns_info_len);
818                 zfcp_dbf_outd(&p, "fcp_sns_info", r->sns_info,
819                               min((int)r->sns_info_len,
820                               ZFCP_DBF_SCSI_FCP_SNS_INFO), 0,
821                               r->sns_info_len);
822         }
823         p += sprintf(p, "\n");
824         return p - out_buf;
825 }
826
827 static struct debug_view zfcp_dbf_scsi_view = {
828         .name = "structured",
829         .header_proc = zfcp_dbf_view_header,
830         .format_proc = zfcp_dbf_scsi_view_format,
831 };
832
833 static debug_info_t *zfcp_dbf_reg(const char *name, int level,
834                                   struct debug_view *view, int size)
835 {
836         struct debug_info *d;
837
838         d = debug_register(name, dbfsize, level, size);
839         if (!d)
840                 return NULL;
841
842         debug_register_view(d, &debug_hex_ascii_view);
843         debug_register_view(d, view);
844         debug_set_level(d, level);
845
846         return d;
847 }
848
849 /**
850  * zfcp_adapter_debug_register - registers debug feature for an adapter
851  * @adapter: pointer to adapter for which debug features should be registered
852  * return: -ENOMEM on error, 0 otherwise
853  */
854 int zfcp_dbf_adapter_register(struct zfcp_adapter *adapter)
855 {
856         char dbf_name[DEBUG_MAX_NAME_LEN];
857         struct zfcp_dbf *dbf;
858
859         dbf = kzalloc(sizeof(struct zfcp_dbf), GFP_KERNEL);
860         if (!dbf)
861                 return -ENOMEM;
862
863         dbf->adapter = adapter;
864
865         spin_lock_init(&dbf->hba_lock);
866         spin_lock_init(&dbf->san_lock);
867         spin_lock_init(&dbf->scsi_lock);
868         spin_lock_init(&dbf->rec_lock);
869
870         /* debug feature area which records recovery activity */
871         sprintf(dbf_name, "zfcp_%s_rec", dev_name(&adapter->ccw_device->dev));
872         dbf->rec = zfcp_dbf_reg(dbf_name, 3, NULL, sizeof(struct zfcp_dbf_rec));
873         if (!dbf->rec)
874                 goto err_out;
875
876         /* debug feature area which records HBA (FSF and QDIO) conditions */
877         sprintf(dbf_name, "zfcp_%s_hba", dev_name(&adapter->ccw_device->dev));
878         dbf->hba = zfcp_dbf_reg(dbf_name, 3, &zfcp_dbf_hba_view,
879                                 sizeof(struct zfcp_dbf_hba_record));
880         if (!dbf->hba)
881                 goto err_out;
882
883         /* debug feature area which records SAN command failures and recovery */
884         sprintf(dbf_name, "zfcp_%s_san", dev_name(&adapter->ccw_device->dev));
885         dbf->san = zfcp_dbf_reg(dbf_name, 6, &zfcp_dbf_san_view,
886                                 sizeof(struct zfcp_dbf_san_record));
887         if (!dbf->san)
888                 goto err_out;
889
890         /* debug feature area which records SCSI command failures and recovery */
891         sprintf(dbf_name, "zfcp_%s_scsi", dev_name(&adapter->ccw_device->dev));
892         dbf->scsi = zfcp_dbf_reg(dbf_name, 3, &zfcp_dbf_scsi_view,
893                                  sizeof(struct zfcp_dbf_scsi_record));
894         if (!dbf->scsi)
895                 goto err_out;
896
897         adapter->dbf = dbf;
898         return 0;
899
900 err_out:
901         zfcp_dbf_adapter_unregister(dbf);
902         return -ENOMEM;
903 }
904
905 /**
906  * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter
907  * @dbf: pointer to dbf for which debug features should be unregistered
908  */
909 void zfcp_dbf_adapter_unregister(struct zfcp_dbf *dbf)
910 {
911         if (!dbf)
912                 return;
913         debug_unregister(dbf->scsi);
914         debug_unregister(dbf->san);
915         debug_unregister(dbf->hba);
916         debug_unregister(dbf->rec);
917         dbf->adapter->dbf = NULL;
918         kfree(dbf);
919 }
920