[SCSI] zfcp: Redesign of the debug tracing for SAN records.
[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 static inline
531 void zfcp_dbf_san(char *tag, struct zfcp_dbf *dbf, void *data, u8 id, u16 len,
532                   u64 req_id, u32 d_id)
533 {
534         struct zfcp_dbf_san *rec = &dbf->san_buf;
535         u16 rec_len;
536         unsigned long flags;
537
538         spin_lock_irqsave(&dbf->san_lock, flags);
539         memset(rec, 0, sizeof(*rec));
540
541         rec->id = id;
542         rec->fsf_req_id = req_id;
543         rec->d_id = d_id;
544         rec_len = min(len, (u16)ZFCP_DBF_SAN_MAX_PAYLOAD);
545         memcpy(rec->payload, data, rec_len);
546         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
547
548         debug_event(dbf->san, 1, rec, sizeof(*rec));
549         spin_unlock_irqrestore(&dbf->san_lock, flags);
550 }
551
552 /**
553  * zfcp_dbf_san_req - trace event for issued SAN request
554  * @tag: indentifier for event
555  * @fsf_req: request containing issued CT data
556  * d_id: destination ID
557  */
558 void zfcp_dbf_san_req(char *tag, struct zfcp_fsf_req *fsf, u32 d_id)
559 {
560         struct zfcp_dbf *dbf = fsf->adapter->dbf;
561         struct zfcp_fsf_ct_els *ct_els = fsf->data;
562         u16 length;
563
564         length = (u16)(ct_els->req->length + FC_CT_HDR_LEN);
565         zfcp_dbf_san(tag, dbf, sg_virt(ct_els->req), ZFCP_DBF_SAN_REQ, length,
566                      fsf->req_id, d_id);
567 }
568
569 /**
570  * zfcp_dbf_san_res - trace event for received SAN request
571  * @tag: indentifier for event
572  * @fsf_req: request containing issued CT data
573  */
574 void zfcp_dbf_san_res(char *tag, struct zfcp_fsf_req *fsf)
575 {
576         struct zfcp_dbf *dbf = fsf->adapter->dbf;
577         struct zfcp_fsf_ct_els *ct_els = fsf->data;
578         u16 length;
579
580         length = (u16)(ct_els->resp->length + FC_CT_HDR_LEN);
581         zfcp_dbf_san(tag, dbf, sg_virt(ct_els->resp), ZFCP_DBF_SAN_RES, length,
582                      fsf->req_id, 0);
583 }
584
585 /**
586  * zfcp_dbf_san_in_els - trace event for incoming ELS
587  * @tag: indentifier for event
588  * @fsf_req: request containing issued CT data
589  */
590 void zfcp_dbf_san_in_els(char *tag, struct zfcp_fsf_req *fsf)
591 {
592         struct zfcp_dbf *dbf = fsf->adapter->dbf;
593         struct fsf_status_read_buffer *srb =
594                 (struct fsf_status_read_buffer *) fsf->data;
595         u16 length;
596
597         length = (u16)(srb->length -
598                         offsetof(struct fsf_status_read_buffer, payload));
599         zfcp_dbf_san(tag, dbf, srb->payload.data, ZFCP_DBF_SAN_ELS, length,
600                      fsf->req_id, ntoh24(srb->d_id));
601 }
602
603 void _zfcp_dbf_scsi(const char *tag, const char *tag2, int level,
604                     struct zfcp_dbf *dbf, struct scsi_cmnd *scsi_cmnd,
605                     struct zfcp_fsf_req *fsf_req, unsigned long old_req_id)
606 {
607         struct zfcp_dbf_scsi_record *rec = &dbf->scsi_buf;
608         struct zfcp_dbf_dump *dump = (struct zfcp_dbf_dump *)rec;
609         unsigned long flags;
610         struct fcp_resp_with_ext *fcp_rsp;
611         struct fcp_resp_rsp_info *fcp_rsp_info = NULL;
612         char *fcp_sns_info = NULL;
613         int offset = 0, buflen = 0;
614
615         spin_lock_irqsave(&dbf->scsi_lock, flags);
616         do {
617                 memset(rec, 0, sizeof(*rec));
618                 if (offset == 0) {
619                         strncpy(rec->tag, tag, ZFCP_DBF_TAG_SIZE);
620                         strncpy(rec->tag2, tag2, ZFCP_DBF_TAG_SIZE);
621                         if (scsi_cmnd != NULL) {
622                                 if (scsi_cmnd->device) {
623                                         rec->scsi_id = scsi_cmnd->device->id;
624                                         rec->scsi_lun = scsi_cmnd->device->lun;
625                                 }
626                                 rec->scsi_result = scsi_cmnd->result;
627                                 rec->scsi_cmnd = (unsigned long)scsi_cmnd;
628                                 memcpy(rec->scsi_opcode, scsi_cmnd->cmnd,
629                                         min((int)scsi_cmnd->cmd_len,
630                                                 ZFCP_DBF_SCSI_OPCODE));
631                                 rec->scsi_retries = scsi_cmnd->retries;
632                                 rec->scsi_allowed = scsi_cmnd->allowed;
633                         }
634                         if (fsf_req != NULL) {
635                                 fcp_rsp = (struct fcp_resp_with_ext *)
636                                         &(fsf_req->qtcb->bottom.io.fcp_rsp);
637                                 fcp_rsp_info = (struct fcp_resp_rsp_info *)
638                                         &fcp_rsp[1];
639                                 fcp_sns_info = (char *) &fcp_rsp[1];
640                                 if (fcp_rsp->resp.fr_flags & FCP_RSP_LEN_VAL)
641                                         fcp_sns_info += fcp_rsp->ext.fr_sns_len;
642
643                                 rec->rsp_validity = fcp_rsp->resp.fr_flags;
644                                 rec->rsp_scsi_status = fcp_rsp->resp.fr_status;
645                                 rec->rsp_resid = fcp_rsp->ext.fr_resid;
646                                 if (fcp_rsp->resp.fr_flags & FCP_RSP_LEN_VAL)
647                                         rec->rsp_code = fcp_rsp_info->rsp_code;
648                                 if (fcp_rsp->resp.fr_flags & FCP_SNS_LEN_VAL) {
649                                         buflen = min(fcp_rsp->ext.fr_sns_len,
650                                            (u32)ZFCP_DBF_SCSI_MAX_FCP_SNS_INFO);
651                                         rec->sns_info_len = buflen;
652                                         memcpy(rec->sns_info, fcp_sns_info,
653                                                min(buflen,
654                                                    ZFCP_DBF_SCSI_FCP_SNS_INFO));
655                                         offset += min(buflen,
656                                                       ZFCP_DBF_SCSI_FCP_SNS_INFO);
657                                 }
658
659                                 rec->fsf_reqid = fsf_req->req_id;
660                                 rec->fsf_seqno = fsf_req->seq_no;
661                                 rec->fsf_issued = fsf_req->issued;
662                         }
663                         rec->old_fsf_reqid = old_req_id;
664                 } else {
665                         strncpy(dump->tag, "dump", ZFCP_DBF_TAG_SIZE);
666                         dump->total_size = buflen;
667                         dump->offset = offset;
668                         dump->size = min(buflen - offset,
669                                          (int)sizeof(struct
670                                                      zfcp_dbf_scsi_record) -
671                                          (int)sizeof(struct zfcp_dbf_dump));
672                         memcpy(dump->data, fcp_sns_info + offset, dump->size);
673                         offset += dump->size;
674                 }
675                 debug_event(dbf->scsi, level, rec, sizeof(*rec));
676         } while (offset < buflen);
677         spin_unlock_irqrestore(&dbf->scsi_lock, flags);
678 }
679
680 static int zfcp_dbf_scsi_view_format(debug_info_t *id, struct debug_view *view,
681                                      char *out_buf, const char *in_buf)
682 {
683         struct zfcp_dbf_scsi_record *r = (struct zfcp_dbf_scsi_record *)in_buf;
684         struct timespec t;
685         char *p = out_buf;
686
687         if (strncmp(r->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
688                 return 0;
689
690         zfcp_dbf_tag(&p, "tag", r->tag);
691         zfcp_dbf_tag(&p, "tag2", r->tag2);
692         zfcp_dbf_out(&p, "scsi_id", "0x%08x", r->scsi_id);
693         zfcp_dbf_out(&p, "scsi_lun", "0x%08x", r->scsi_lun);
694         zfcp_dbf_out(&p, "scsi_result", "0x%08x", r->scsi_result);
695         zfcp_dbf_out(&p, "scsi_cmnd", "0x%0Lx", r->scsi_cmnd);
696         zfcp_dbf_outd(&p, "scsi_opcode", r->scsi_opcode, ZFCP_DBF_SCSI_OPCODE,
697                       0, ZFCP_DBF_SCSI_OPCODE);
698         zfcp_dbf_out(&p, "scsi_retries", "0x%02x", r->scsi_retries);
699         zfcp_dbf_out(&p, "scsi_allowed", "0x%02x", r->scsi_allowed);
700         if (strncmp(r->tag, "abrt", ZFCP_DBF_TAG_SIZE) == 0)
701                 zfcp_dbf_out(&p, "old_fsf_reqid", "0x%0Lx", r->old_fsf_reqid);
702         zfcp_dbf_out(&p, "fsf_reqid", "0x%0Lx", r->fsf_reqid);
703         zfcp_dbf_out(&p, "fsf_seqno", "0x%08x", r->fsf_seqno);
704         stck_to_timespec(r->fsf_issued, &t);
705         zfcp_dbf_out(&p, "fsf_issued", "%011lu:%06lu", t.tv_sec, t.tv_nsec);
706
707         if (strncmp(r->tag, "rslt", ZFCP_DBF_TAG_SIZE) == 0) {
708                 zfcp_dbf_out(&p, "fcp_rsp_validity", "0x%02x", r->rsp_validity);
709                 zfcp_dbf_out(&p, "fcp_rsp_scsi_status", "0x%02x",
710                              r->rsp_scsi_status);
711                 zfcp_dbf_out(&p, "fcp_rsp_resid", "0x%08x", r->rsp_resid);
712                 zfcp_dbf_out(&p, "fcp_rsp_code", "0x%08x", r->rsp_code);
713                 zfcp_dbf_out(&p, "fcp_sns_info_len", "0x%08x", r->sns_info_len);
714                 zfcp_dbf_outd(&p, "fcp_sns_info", r->sns_info,
715                               min((int)r->sns_info_len,
716                               ZFCP_DBF_SCSI_FCP_SNS_INFO), 0,
717                               r->sns_info_len);
718         }
719         p += sprintf(p, "\n");
720         return p - out_buf;
721 }
722
723 static struct debug_view zfcp_dbf_scsi_view = {
724         .name = "structured",
725         .header_proc = zfcp_dbf_view_header,
726         .format_proc = zfcp_dbf_scsi_view_format,
727 };
728
729 static debug_info_t *zfcp_dbf_reg(const char *name, int level,
730                                   struct debug_view *view, int size)
731 {
732         struct debug_info *d;
733
734         d = debug_register(name, dbfsize, level, size);
735         if (!d)
736                 return NULL;
737
738         debug_register_view(d, &debug_hex_ascii_view);
739         debug_register_view(d, view);
740         debug_set_level(d, level);
741
742         return d;
743 }
744
745 /**
746  * zfcp_adapter_debug_register - registers debug feature for an adapter
747  * @adapter: pointer to adapter for which debug features should be registered
748  * return: -ENOMEM on error, 0 otherwise
749  */
750 int zfcp_dbf_adapter_register(struct zfcp_adapter *adapter)
751 {
752         char dbf_name[DEBUG_MAX_NAME_LEN];
753         struct zfcp_dbf *dbf;
754
755         dbf = kzalloc(sizeof(struct zfcp_dbf), GFP_KERNEL);
756         if (!dbf)
757                 return -ENOMEM;
758
759         dbf->adapter = adapter;
760
761         spin_lock_init(&dbf->hba_lock);
762         spin_lock_init(&dbf->san_lock);
763         spin_lock_init(&dbf->scsi_lock);
764         spin_lock_init(&dbf->rec_lock);
765
766         /* debug feature area which records recovery activity */
767         sprintf(dbf_name, "zfcp_%s_rec", dev_name(&adapter->ccw_device->dev));
768         dbf->rec = zfcp_dbf_reg(dbf_name, 3, NULL, sizeof(struct zfcp_dbf_rec));
769         if (!dbf->rec)
770                 goto err_out;
771
772         /* debug feature area which records HBA (FSF and QDIO) conditions */
773         sprintf(dbf_name, "zfcp_%s_hba", dev_name(&adapter->ccw_device->dev));
774         dbf->hba = zfcp_dbf_reg(dbf_name, 3, &zfcp_dbf_hba_view,
775                                 sizeof(struct zfcp_dbf_hba_record));
776         if (!dbf->hba)
777                 goto err_out;
778
779         /* debug feature area which records SAN command failures and recovery */
780         sprintf(dbf_name, "zfcp_%s_san", dev_name(&adapter->ccw_device->dev));
781         dbf->san = zfcp_dbf_reg(dbf_name, 3, NULL, sizeof(struct zfcp_dbf_san));
782         if (!dbf->san)
783                 goto err_out;
784
785         /* debug feature area which records SCSI command failures and recovery */
786         sprintf(dbf_name, "zfcp_%s_scsi", dev_name(&adapter->ccw_device->dev));
787         dbf->scsi = zfcp_dbf_reg(dbf_name, 3, &zfcp_dbf_scsi_view,
788                                  sizeof(struct zfcp_dbf_scsi_record));
789         if (!dbf->scsi)
790                 goto err_out;
791
792         adapter->dbf = dbf;
793         return 0;
794
795 err_out:
796         zfcp_dbf_adapter_unregister(dbf);
797         return -ENOMEM;
798 }
799
800 /**
801  * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter
802  * @dbf: pointer to dbf for which debug features should be unregistered
803  */
804 void zfcp_dbf_adapter_unregister(struct zfcp_dbf *dbf)
805 {
806         if (!dbf)
807                 return;
808         debug_unregister(dbf->scsi);
809         debug_unregister(dbf->san);
810         debug_unregister(dbf->hba);
811         debug_unregister(dbf->rec);
812         dbf->adapter->dbf = NULL;
813         kfree(dbf);
814 }
815