]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/s390/scsi/zfcp_dbf.c
[SCSI] zfcp: Add qtcb dump to hba debug trace
[linux-2.6-omap-h63xx.git] / drivers / s390 / scsi / zfcp_dbf.c
1 /*
2  * This file is part of the zfcp device driver for
3  * FCP adapters for IBM System z9 and zSeries.
4  *
5  * (C) Copyright IBM Corp. 2002, 2006
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/ctype.h>
23 #include <asm/debug.h>
24 #include "zfcp_ext.h"
25
26 static u32 dbfsize = 4;
27
28 module_param(dbfsize, uint, 0400);
29 MODULE_PARM_DESC(dbfsize,
30                  "number of pages for each debug feature area (default 4)");
31
32 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_OTHER
33
34 static void zfcp_dbf_hexdump(debug_info_t *dbf, void *to, int to_len,
35                              int level, char *from, int from_len)
36 {
37         int offset;
38         struct zfcp_dbf_dump *dump = to;
39         int room = to_len - sizeof(*dump);
40
41         for (offset = 0; offset < from_len; offset += dump->size) {
42                 memset(to, 0, to_len);
43                 strncpy(dump->tag, "dump", ZFCP_DBF_TAG_SIZE);
44                 dump->total_size = from_len;
45                 dump->offset = offset;
46                 dump->size = min(from_len - offset, room);
47                 memcpy(dump->data, from + offset, dump->size);
48                 debug_event(dbf, level, dump, dump->size);
49         }
50 }
51
52 static int
53 zfcp_dbf_stck(char *out_buf, const char *label, unsigned long long stck)
54 {
55         unsigned long long sec;
56         struct timespec dbftime;
57         int len = 0;
58
59         stck -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
60         sec = stck >> 12;
61         do_div(sec, 1000000);
62         dbftime.tv_sec = sec;
63         stck -= (sec * 1000000) << 12;
64         dbftime.tv_nsec = ((stck * 1000) >> 12);
65         len += sprintf(out_buf + len, "%-24s%011lu:%06lu\n",
66                        label, dbftime.tv_sec, dbftime.tv_nsec);
67
68         return len;
69 }
70
71 static int zfcp_dbf_tag(char *out_buf, const char *label, const char *tag)
72 {
73         int len = 0, i;
74
75         len += sprintf(out_buf + len, "%-24s", label);
76         for (i = 0; i < ZFCP_DBF_TAG_SIZE; i++)
77                 len += sprintf(out_buf + len, "%c", tag[i]);
78         len += sprintf(out_buf + len, "\n");
79
80         return len;
81 }
82
83 static int
84 zfcp_dbf_view(char *out_buf, const char *label, const char *format, ...)
85 {
86         va_list arg;
87         int len = 0;
88
89         len += sprintf(out_buf + len, "%-24s", label);
90         va_start(arg, format);
91         len += vsprintf(out_buf + len, format, arg);
92         va_end(arg);
93         len += sprintf(out_buf + len, "\n");
94
95         return len;
96 }
97
98 static int
99 zfcp_dbf_view_dump(char *out_buf, const char *label,
100                    char *buffer, int buflen, int offset, int total_size)
101 {
102         int len = 0;
103
104         if (offset == 0)
105                 len += sprintf(out_buf + len, "%-24s  ", label);
106
107         while (buflen--) {
108                 if (offset > 0) {
109                         if ((offset % 32) == 0)
110                                 len += sprintf(out_buf + len, "\n%-24c  ", ' ');
111                         else if ((offset % 4) == 0)
112                                 len += sprintf(out_buf + len, " ");
113                 }
114                 len += sprintf(out_buf + len, "%02x", *buffer++);
115                 if (++offset == total_size) {
116                         len += sprintf(out_buf + len, "\n");
117                         break;
118                 }
119         }
120
121         if (total_size == 0)
122                 len += sprintf(out_buf + len, "\n");
123
124         return len;
125 }
126
127 static int
128 zfcp_dbf_view_header(debug_info_t * id, struct debug_view *view, int area,
129                      debug_entry_t * entry, char *out_buf)
130 {
131         struct zfcp_dbf_dump *dump = (struct zfcp_dbf_dump *)DEBUG_DATA(entry);
132         int len = 0;
133
134         if (strncmp(dump->tag, "dump", ZFCP_DBF_TAG_SIZE) != 0) {
135                 len += zfcp_dbf_stck(out_buf + len, "timestamp",
136                                      entry->id.stck);
137                 len += zfcp_dbf_view(out_buf + len, "cpu", "%02i",
138                                      entry->id.fields.cpuid);
139         } else {
140                 len += zfcp_dbf_view_dump(out_buf + len, NULL,
141                                           dump->data,
142                                           dump->size,
143                                           dump->offset, dump->total_size);
144                 if ((dump->offset + dump->size) == dump->total_size)
145                         len += sprintf(out_buf + len, "\n");
146         }
147
148         return len;
149 }
150
151 void zfcp_hba_dbf_event_fsf_response(struct zfcp_fsf_req *fsf_req)
152 {
153         struct zfcp_adapter *adapter = fsf_req->adapter;
154         struct fsf_qtcb *qtcb = fsf_req->qtcb;
155         union fsf_prot_status_qual *prot_status_qual =
156             &qtcb->prefix.prot_status_qual;
157         union fsf_status_qual *fsf_status_qual = &qtcb->header.fsf_status_qual;
158         struct scsi_cmnd *scsi_cmnd;
159         struct zfcp_port *port;
160         struct zfcp_unit *unit;
161         struct zfcp_send_els *send_els;
162         struct zfcp_hba_dbf_record *rec = &adapter->hba_dbf_buf;
163         struct zfcp_hba_dbf_record_response *response = &rec->type.response;
164         int level;
165         unsigned long flags;
166
167         spin_lock_irqsave(&adapter->hba_dbf_lock, flags);
168         memset(rec, 0, sizeof(struct zfcp_hba_dbf_record));
169         strncpy(rec->tag, "resp", ZFCP_DBF_TAG_SIZE);
170
171         if ((qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
172             (qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
173                 strncpy(rec->tag2, "perr", ZFCP_DBF_TAG_SIZE);
174                 level = 1;
175         } else if (qtcb->header.fsf_status != FSF_GOOD) {
176                 strncpy(rec->tag2, "ferr", ZFCP_DBF_TAG_SIZE);
177                 level = 1;
178         } else if ((fsf_req->fsf_command == FSF_QTCB_OPEN_PORT_WITH_DID) ||
179                    (fsf_req->fsf_command == FSF_QTCB_OPEN_LUN)) {
180                 strncpy(rec->tag2, "open", ZFCP_DBF_TAG_SIZE);
181                 level = 4;
182         } else if (qtcb->header.log_length) {
183                 strncpy(rec->tag2, "qtcb", ZFCP_DBF_TAG_SIZE);
184                 level = 5;
185         } else {
186                 strncpy(rec->tag2, "norm", ZFCP_DBF_TAG_SIZE);
187                 level = 6;
188         }
189
190         response->fsf_command = fsf_req->fsf_command;
191         response->fsf_reqid = (unsigned long)fsf_req;
192         response->fsf_seqno = fsf_req->seq_no;
193         response->fsf_issued = fsf_req->issued;
194         response->fsf_prot_status = qtcb->prefix.prot_status;
195         response->fsf_status = qtcb->header.fsf_status;
196         memcpy(response->fsf_prot_status_qual,
197                prot_status_qual, FSF_PROT_STATUS_QUAL_SIZE);
198         memcpy(response->fsf_status_qual,
199                fsf_status_qual, FSF_STATUS_QUALIFIER_SIZE);
200         response->fsf_req_status = fsf_req->status;
201         response->sbal_first = fsf_req->sbal_first;
202         response->sbal_curr = fsf_req->sbal_curr;
203         response->sbal_last = fsf_req->sbal_last;
204         response->pool = fsf_req->pool != NULL;
205         response->erp_action = (unsigned long)fsf_req->erp_action;
206
207         switch (fsf_req->fsf_command) {
208         case FSF_QTCB_FCP_CMND:
209                 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
210                         break;
211                 scsi_cmnd = (struct scsi_cmnd *)fsf_req->data;
212                 if (scsi_cmnd != NULL) {
213                         response->data.send_fcp.scsi_cmnd
214                             = (unsigned long)scsi_cmnd;
215                         response->data.send_fcp.scsi_serial
216                             = scsi_cmnd->serial_number;
217                 }
218                 break;
219
220         case FSF_QTCB_OPEN_PORT_WITH_DID:
221         case FSF_QTCB_CLOSE_PORT:
222         case FSF_QTCB_CLOSE_PHYSICAL_PORT:
223                 port = (struct zfcp_port *)fsf_req->data;
224                 response->data.port.wwpn = port->wwpn;
225                 response->data.port.d_id = port->d_id;
226                 response->data.port.port_handle = qtcb->header.port_handle;
227                 break;
228
229         case FSF_QTCB_OPEN_LUN:
230         case FSF_QTCB_CLOSE_LUN:
231                 unit = (struct zfcp_unit *)fsf_req->data;
232                 port = unit->port;
233                 response->data.unit.wwpn = port->wwpn;
234                 response->data.unit.fcp_lun = unit->fcp_lun;
235                 response->data.unit.port_handle = qtcb->header.port_handle;
236                 response->data.unit.lun_handle = qtcb->header.lun_handle;
237                 break;
238
239         case FSF_QTCB_SEND_ELS:
240                 send_els = (struct zfcp_send_els *)fsf_req->data;
241                 response->data.send_els.d_id = qtcb->bottom.support.d_id;
242                 response->data.send_els.ls_code = send_els->ls_code >> 24;
243                 break;
244
245         case FSF_QTCB_ABORT_FCP_CMND:
246         case FSF_QTCB_SEND_GENERIC:
247         case FSF_QTCB_EXCHANGE_CONFIG_DATA:
248         case FSF_QTCB_EXCHANGE_PORT_DATA:
249         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
250         case FSF_QTCB_UPLOAD_CONTROL_FILE:
251                 break;
252         }
253
254         debug_event(adapter->hba_dbf, level,
255                     rec, sizeof(struct zfcp_hba_dbf_record));
256
257         /* have fcp channel microcode fixed to use as little as possible */
258         if (fsf_req->fsf_command != FSF_QTCB_FCP_CMND) {
259                 /* adjust length skipping trailing zeros */
260                 char *buf = (char *)qtcb + qtcb->header.log_start;
261                 int len = qtcb->header.log_length;
262                 for (; len && !buf[len - 1]; len--);
263                 zfcp_dbf_hexdump(adapter->hba_dbf, rec, sizeof(*rec), level,
264                                  buf, len);
265         }
266
267         spin_unlock_irqrestore(&adapter->hba_dbf_lock, flags);
268 }
269
270 void
271 zfcp_hba_dbf_event_fsf_unsol(const char *tag, struct zfcp_adapter *adapter,
272                              struct fsf_status_read_buffer *status_buffer)
273 {
274         struct zfcp_hba_dbf_record *rec = &adapter->hba_dbf_buf;
275         unsigned long flags;
276
277         spin_lock_irqsave(&adapter->hba_dbf_lock, flags);
278         memset(rec, 0, sizeof(struct zfcp_hba_dbf_record));
279         strncpy(rec->tag, "stat", ZFCP_DBF_TAG_SIZE);
280         strncpy(rec->tag2, tag, ZFCP_DBF_TAG_SIZE);
281
282         rec->type.status.failed = adapter->status_read_failed;
283         if (status_buffer != NULL) {
284                 rec->type.status.status_type = status_buffer->status_type;
285                 rec->type.status.status_subtype = status_buffer->status_subtype;
286                 memcpy(&rec->type.status.queue_designator,
287                        &status_buffer->queue_designator,
288                        sizeof(struct fsf_queue_designator));
289
290                 switch (status_buffer->status_type) {
291                 case FSF_STATUS_READ_SENSE_DATA_AVAIL:
292                         rec->type.status.payload_size =
293                             ZFCP_DBF_UNSOL_PAYLOAD_SENSE_DATA_AVAIL;
294                         break;
295
296                 case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
297                         rec->type.status.payload_size =
298                             ZFCP_DBF_UNSOL_PAYLOAD_BIT_ERROR_THRESHOLD;
299                         break;
300
301                 case FSF_STATUS_READ_LINK_DOWN:
302                         switch (status_buffer->status_subtype) {
303                         case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
304                         case FSF_STATUS_READ_SUB_FDISC_FAILED:
305                                 rec->type.status.payload_size =
306                                         sizeof(struct fsf_link_down_info);
307                         }
308                         break;
309
310                 case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
311                         rec->type.status.payload_size =
312                             ZFCP_DBF_UNSOL_PAYLOAD_FEATURE_UPDATE_ALERT;
313                         break;
314                 }
315                 memcpy(&rec->type.status.payload,
316                        &status_buffer->payload, rec->type.status.payload_size);
317         }
318
319         debug_event(adapter->hba_dbf, 2,
320                     rec, sizeof(struct zfcp_hba_dbf_record));
321         spin_unlock_irqrestore(&adapter->hba_dbf_lock, flags);
322 }
323
324 void
325 zfcp_hba_dbf_event_qdio(struct zfcp_adapter *adapter, unsigned int status,
326                         unsigned int qdio_error, unsigned int siga_error,
327                         int sbal_index, int sbal_count)
328 {
329         struct zfcp_hba_dbf_record *rec = &adapter->hba_dbf_buf;
330         unsigned long flags;
331
332         spin_lock_irqsave(&adapter->hba_dbf_lock, flags);
333         memset(rec, 0, sizeof(struct zfcp_hba_dbf_record));
334         strncpy(rec->tag, "qdio", ZFCP_DBF_TAG_SIZE);
335         rec->type.qdio.status = status;
336         rec->type.qdio.qdio_error = qdio_error;
337         rec->type.qdio.siga_error = siga_error;
338         rec->type.qdio.sbal_index = sbal_index;
339         rec->type.qdio.sbal_count = sbal_count;
340         debug_event(adapter->hba_dbf, 0,
341                     rec, sizeof(struct zfcp_hba_dbf_record));
342         spin_unlock_irqrestore(&adapter->hba_dbf_lock, flags);
343 }
344
345 static int
346 zfcp_hba_dbf_view_response(char *out_buf,
347                            struct zfcp_hba_dbf_record_response *rec)
348 {
349         int len = 0;
350
351         len += zfcp_dbf_view(out_buf + len, "fsf_command", "0x%08x",
352                              rec->fsf_command);
353         len += zfcp_dbf_view(out_buf + len, "fsf_reqid", "0x%0Lx",
354                              rec->fsf_reqid);
355         len += zfcp_dbf_view(out_buf + len, "fsf_seqno", "0x%08x",
356                              rec->fsf_seqno);
357         len += zfcp_dbf_stck(out_buf + len, "fsf_issued", rec->fsf_issued);
358         len += zfcp_dbf_view(out_buf + len, "fsf_prot_status", "0x%08x",
359                              rec->fsf_prot_status);
360         len += zfcp_dbf_view(out_buf + len, "fsf_status", "0x%08x",
361                              rec->fsf_status);
362         len += zfcp_dbf_view_dump(out_buf + len, "fsf_prot_status_qual",
363                                   rec->fsf_prot_status_qual,
364                                   FSF_PROT_STATUS_QUAL_SIZE,
365                                   0, FSF_PROT_STATUS_QUAL_SIZE);
366         len += zfcp_dbf_view_dump(out_buf + len, "fsf_status_qual",
367                                   rec->fsf_status_qual,
368                                   FSF_STATUS_QUALIFIER_SIZE,
369                                   0, FSF_STATUS_QUALIFIER_SIZE);
370         len += zfcp_dbf_view(out_buf + len, "fsf_req_status", "0x%08x",
371                              rec->fsf_req_status);
372         len += zfcp_dbf_view(out_buf + len, "sbal_first", "0x%02x",
373                              rec->sbal_first);
374         len += zfcp_dbf_view(out_buf + len, "sbal_curr", "0x%02x",
375                              rec->sbal_curr);
376         len += zfcp_dbf_view(out_buf + len, "sbal_last", "0x%02x",
377                              rec->sbal_last);
378         len += zfcp_dbf_view(out_buf + len, "pool", "0x%02x", rec->pool);
379
380         switch (rec->fsf_command) {
381         case FSF_QTCB_FCP_CMND:
382                 if (rec->fsf_req_status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
383                         break;
384                 len += zfcp_dbf_view(out_buf + len, "scsi_cmnd", "0x%0Lx",
385                                      rec->data.send_fcp.scsi_cmnd);
386                 len += zfcp_dbf_view(out_buf + len, "scsi_serial", "0x%016Lx",
387                                      rec->data.send_fcp.scsi_serial);
388                 break;
389
390         case FSF_QTCB_OPEN_PORT_WITH_DID:
391         case FSF_QTCB_CLOSE_PORT:
392         case FSF_QTCB_CLOSE_PHYSICAL_PORT:
393                 len += zfcp_dbf_view(out_buf + len, "wwpn", "0x%016Lx",
394                                      rec->data.port.wwpn);
395                 len += zfcp_dbf_view(out_buf + len, "d_id", "0x%06x",
396                                      rec->data.port.d_id);
397                 len += zfcp_dbf_view(out_buf + len, "port_handle", "0x%08x",
398                                      rec->data.port.port_handle);
399                 break;
400
401         case FSF_QTCB_OPEN_LUN:
402         case FSF_QTCB_CLOSE_LUN:
403                 len += zfcp_dbf_view(out_buf + len, "wwpn", "0x%016Lx",
404                                      rec->data.unit.wwpn);
405                 len += zfcp_dbf_view(out_buf + len, "fcp_lun", "0x%016Lx",
406                                      rec->data.unit.fcp_lun);
407                 len += zfcp_dbf_view(out_buf + len, "port_handle", "0x%08x",
408                                      rec->data.unit.port_handle);
409                 len += zfcp_dbf_view(out_buf + len, "lun_handle", "0x%08x",
410                                      rec->data.unit.lun_handle);
411                 break;
412
413         case FSF_QTCB_SEND_ELS:
414                 len += zfcp_dbf_view(out_buf + len, "d_id", "0x%06x",
415                                      rec->data.send_els.d_id);
416                 len += zfcp_dbf_view(out_buf + len, "ls_code", "0x%02x",
417                                      rec->data.send_els.ls_code);
418                 break;
419
420         case FSF_QTCB_ABORT_FCP_CMND:
421         case FSF_QTCB_SEND_GENERIC:
422         case FSF_QTCB_EXCHANGE_CONFIG_DATA:
423         case FSF_QTCB_EXCHANGE_PORT_DATA:
424         case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
425         case FSF_QTCB_UPLOAD_CONTROL_FILE:
426                 break;
427         }
428
429         return len;
430 }
431
432 static int
433 zfcp_hba_dbf_view_status(char *out_buf, struct zfcp_hba_dbf_record_status *rec)
434 {
435         int len = 0;
436
437         len += zfcp_dbf_view(out_buf + len, "failed", "0x%02x", rec->failed);
438         len += zfcp_dbf_view(out_buf + len, "status_type", "0x%08x",
439                              rec->status_type);
440         len += zfcp_dbf_view(out_buf + len, "status_subtype", "0x%08x",
441                              rec->status_subtype);
442         len += zfcp_dbf_view_dump(out_buf + len, "queue_designator",
443                                   (char *)&rec->queue_designator,
444                                   sizeof(struct fsf_queue_designator),
445                                   0, sizeof(struct fsf_queue_designator));
446         len += zfcp_dbf_view_dump(out_buf + len, "payload",
447                                   (char *)&rec->payload,
448                                   rec->payload_size, 0, rec->payload_size);
449
450         return len;
451 }
452
453 static int
454 zfcp_hba_dbf_view_qdio(char *out_buf, struct zfcp_hba_dbf_record_qdio *rec)
455 {
456         int len = 0;
457
458         len += zfcp_dbf_view(out_buf + len, "status", "0x%08x", rec->status);
459         len += zfcp_dbf_view(out_buf + len, "qdio_error", "0x%08x",
460                              rec->qdio_error);
461         len += zfcp_dbf_view(out_buf + len, "siga_error", "0x%08x",
462                              rec->siga_error);
463         len += zfcp_dbf_view(out_buf + len, "sbal_index", "0x%02x",
464                              rec->sbal_index);
465         len += zfcp_dbf_view(out_buf + len, "sbal_count", "0x%02x",
466                              rec->sbal_count);
467
468         return len;
469 }
470
471 static int
472 zfcp_hba_dbf_view_format(debug_info_t * id, struct debug_view *view,
473                          char *out_buf, const char *in_buf)
474 {
475         struct zfcp_hba_dbf_record *rec = (struct zfcp_hba_dbf_record *)in_buf;
476         int len = 0;
477
478         if (strncmp(rec->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
479                 return 0;
480
481         len += zfcp_dbf_tag(out_buf + len, "tag", rec->tag);
482         if (isalpha(rec->tag2[0]))
483                 len += zfcp_dbf_tag(out_buf + len, "tag2", rec->tag2);
484         if (strncmp(rec->tag, "resp", ZFCP_DBF_TAG_SIZE) == 0)
485                 len += zfcp_hba_dbf_view_response(out_buf + len,
486                                                   &rec->type.response);
487         else if (strncmp(rec->tag, "stat", ZFCP_DBF_TAG_SIZE) == 0)
488                 len += zfcp_hba_dbf_view_status(out_buf + len,
489                                                 &rec->type.status);
490         else if (strncmp(rec->tag, "qdio", ZFCP_DBF_TAG_SIZE) == 0)
491                 len += zfcp_hba_dbf_view_qdio(out_buf + len, &rec->type.qdio);
492
493         len += sprintf(out_buf + len, "\n");
494
495         return len;
496 }
497
498 static struct debug_view zfcp_hba_dbf_view = {
499         "structured",
500         NULL,
501         &zfcp_dbf_view_header,
502         &zfcp_hba_dbf_view_format,
503         NULL,
504         NULL
505 };
506
507 static void
508 _zfcp_san_dbf_event_common_ct(const char *tag, struct zfcp_fsf_req *fsf_req,
509                               u32 s_id, u32 d_id, void *buffer, int buflen)
510 {
511         struct zfcp_send_ct *send_ct = (struct zfcp_send_ct *)fsf_req->data;
512         struct zfcp_port *port = send_ct->port;
513         struct zfcp_adapter *adapter = port->adapter;
514         struct ct_hdr *header = (struct ct_hdr *)buffer;
515         struct zfcp_san_dbf_record *rec = &adapter->san_dbf_buf;
516         struct zfcp_san_dbf_record_ct *ct = &rec->type.ct;
517         unsigned long flags;
518
519         spin_lock_irqsave(&adapter->san_dbf_lock, flags);
520         memset(rec, 0, sizeof(struct zfcp_san_dbf_record));
521         strncpy(rec->tag, tag, ZFCP_DBF_TAG_SIZE);
522         rec->fsf_reqid = (unsigned long)fsf_req;
523         rec->fsf_seqno = fsf_req->seq_no;
524         rec->s_id = s_id;
525         rec->d_id = d_id;
526         if (strncmp(tag, "octc", ZFCP_DBF_TAG_SIZE) == 0) {
527                 ct->type.request.cmd_req_code = header->cmd_rsp_code;
528                 ct->type.request.revision = header->revision;
529                 ct->type.request.gs_type = header->gs_type;
530                 ct->type.request.gs_subtype = header->gs_subtype;
531                 ct->type.request.options = header->options;
532                 ct->type.request.max_res_size = header->max_res_size;
533         } else if (strncmp(tag, "rctc", ZFCP_DBF_TAG_SIZE) == 0) {
534                 ct->type.response.cmd_rsp_code = header->cmd_rsp_code;
535                 ct->type.response.revision = header->revision;
536                 ct->type.response.reason_code = header->reason_code;
537                 ct->type.response.reason_code_expl = header->reason_code_expl;
538                 ct->type.response.vendor_unique = header->vendor_unique;
539         }
540         ct->payload_size =
541             min(buflen - (int)sizeof(struct ct_hdr), ZFCP_DBF_CT_PAYLOAD);
542         memcpy(ct->payload, buffer + sizeof(struct ct_hdr), ct->payload_size);
543         debug_event(adapter->san_dbf, 3,
544                     rec, sizeof(struct zfcp_san_dbf_record));
545         spin_unlock_irqrestore(&adapter->san_dbf_lock, flags);
546 }
547
548 void zfcp_san_dbf_event_ct_request(struct zfcp_fsf_req *fsf_req)
549 {
550         struct zfcp_send_ct *ct = (struct zfcp_send_ct *)fsf_req->data;
551         struct zfcp_port *port = ct->port;
552         struct zfcp_adapter *adapter = port->adapter;
553
554         _zfcp_san_dbf_event_common_ct("octc", fsf_req,
555                                       fc_host_port_id(adapter->scsi_host),
556                                       port->d_id, zfcp_sg_to_address(ct->req),
557                                       ct->req->length);
558 }
559
560 void zfcp_san_dbf_event_ct_response(struct zfcp_fsf_req *fsf_req)
561 {
562         struct zfcp_send_ct *ct = (struct zfcp_send_ct *)fsf_req->data;
563         struct zfcp_port *port = ct->port;
564         struct zfcp_adapter *adapter = port->adapter;
565
566         _zfcp_san_dbf_event_common_ct("rctc", fsf_req, port->d_id,
567                                       fc_host_port_id(adapter->scsi_host),
568                                       zfcp_sg_to_address(ct->resp),
569                                       ct->resp->length);
570 }
571
572 static void
573 _zfcp_san_dbf_event_common_els(const char *tag, int level,
574                                struct zfcp_fsf_req *fsf_req, u32 s_id,
575                                u32 d_id, u8 ls_code, void *buffer, int buflen)
576 {
577         struct zfcp_adapter *adapter = fsf_req->adapter;
578         struct zfcp_san_dbf_record *rec = &adapter->san_dbf_buf;
579         unsigned long flags;
580
581         spin_lock_irqsave(&adapter->san_dbf_lock, flags);
582         memset(rec, 0, sizeof(struct zfcp_san_dbf_record));
583         strncpy(rec->tag, tag, ZFCP_DBF_TAG_SIZE);
584         rec->fsf_reqid = (unsigned long)fsf_req;
585         rec->fsf_seqno = fsf_req->seq_no;
586         rec->s_id = s_id;
587         rec->d_id = d_id;
588         rec->type.els.ls_code = ls_code;
589         debug_event(adapter->san_dbf, level, rec, sizeof(*rec));
590         zfcp_dbf_hexdump(adapter->san_dbf, rec, sizeof(*rec), level,
591                          buffer, min(buflen, ZFCP_DBF_ELS_MAX_PAYLOAD));
592         spin_unlock_irqrestore(&adapter->san_dbf_lock, flags);
593 }
594
595 void zfcp_san_dbf_event_els_request(struct zfcp_fsf_req *fsf_req)
596 {
597         struct zfcp_send_els *els = (struct zfcp_send_els *)fsf_req->data;
598
599         _zfcp_san_dbf_event_common_els("oels", 2, fsf_req,
600                                        fc_host_port_id(els->adapter->scsi_host),
601                                        els->d_id,
602                                        *(u8 *) zfcp_sg_to_address(els->req),
603                                        zfcp_sg_to_address(els->req),
604                                        els->req->length);
605 }
606
607 void zfcp_san_dbf_event_els_response(struct zfcp_fsf_req *fsf_req)
608 {
609         struct zfcp_send_els *els = (struct zfcp_send_els *)fsf_req->data;
610
611         _zfcp_san_dbf_event_common_els("rels", 2, fsf_req, els->d_id,
612                                        fc_host_port_id(els->adapter->scsi_host),
613                                        *(u8 *) zfcp_sg_to_address(els->req),
614                                        zfcp_sg_to_address(els->resp),
615                                        els->resp->length);
616 }
617
618 void zfcp_san_dbf_event_incoming_els(struct zfcp_fsf_req *fsf_req)
619 {
620         struct zfcp_adapter *adapter = fsf_req->adapter;
621         struct fsf_status_read_buffer *status_buffer =
622             (struct fsf_status_read_buffer *)fsf_req->data;
623         int length = (int)status_buffer->length -
624             (int)((void *)&status_buffer->payload - (void *)status_buffer);
625
626         _zfcp_san_dbf_event_common_els("iels", 1, fsf_req, status_buffer->d_id,
627                                        fc_host_port_id(adapter->scsi_host),
628                                        *(u8 *) status_buffer->payload,
629                                        (void *)status_buffer->payload, length);
630 }
631
632 static int
633 zfcp_san_dbf_view_format(debug_info_t * id, struct debug_view *view,
634                          char *out_buf, const char *in_buf)
635 {
636         struct zfcp_san_dbf_record *rec = (struct zfcp_san_dbf_record *)in_buf;
637         char *buffer = NULL;
638         int buflen = 0, total = 0;
639         int len = 0;
640
641         if (strncmp(rec->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
642                 return 0;
643
644         len += zfcp_dbf_tag(out_buf + len, "tag", rec->tag);
645         len += zfcp_dbf_view(out_buf + len, "fsf_reqid", "0x%0Lx",
646                              rec->fsf_reqid);
647         len += zfcp_dbf_view(out_buf + len, "fsf_seqno", "0x%08x",
648                              rec->fsf_seqno);
649         len += zfcp_dbf_view(out_buf + len, "s_id", "0x%06x", rec->s_id);
650         len += zfcp_dbf_view(out_buf + len, "d_id", "0x%06x", rec->d_id);
651
652         if (strncmp(rec->tag, "octc", ZFCP_DBF_TAG_SIZE) == 0) {
653                 len += zfcp_dbf_view(out_buf + len, "cmd_req_code", "0x%04x",
654                                      rec->type.ct.type.request.cmd_req_code);
655                 len += zfcp_dbf_view(out_buf + len, "revision", "0x%02x",
656                                      rec->type.ct.type.request.revision);
657                 len += zfcp_dbf_view(out_buf + len, "gs_type", "0x%02x",
658                                      rec->type.ct.type.request.gs_type);
659                 len += zfcp_dbf_view(out_buf + len, "gs_subtype", "0x%02x",
660                                      rec->type.ct.type.request.gs_subtype);
661                 len += zfcp_dbf_view(out_buf + len, "options", "0x%02x",
662                                      rec->type.ct.type.request.options);
663                 len += zfcp_dbf_view(out_buf + len, "max_res_size", "0x%04x",
664                                      rec->type.ct.type.request.max_res_size);
665                 total = rec->type.ct.payload_size;
666                 buffer = rec->type.ct.payload;
667                 buflen = min(total, ZFCP_DBF_CT_PAYLOAD);
668         } else if (strncmp(rec->tag, "rctc", ZFCP_DBF_TAG_SIZE) == 0) {
669                 len += zfcp_dbf_view(out_buf + len, "cmd_rsp_code", "0x%04x",
670                                      rec->type.ct.type.response.cmd_rsp_code);
671                 len += zfcp_dbf_view(out_buf + len, "revision", "0x%02x",
672                                      rec->type.ct.type.response.revision);
673                 len += zfcp_dbf_view(out_buf + len, "reason_code", "0x%02x",
674                                      rec->type.ct.type.response.reason_code);
675                 len +=
676                     zfcp_dbf_view(out_buf + len, "reason_code_expl", "0x%02x",
677                                   rec->type.ct.type.response.reason_code_expl);
678                 len +=
679                     zfcp_dbf_view(out_buf + len, "vendor_unique", "0x%02x",
680                                   rec->type.ct.type.response.vendor_unique);
681                 total = rec->type.ct.payload_size;
682                 buffer = rec->type.ct.payload;
683                 buflen = min(total, ZFCP_DBF_CT_PAYLOAD);
684         } else if (strncmp(rec->tag, "oels", ZFCP_DBF_TAG_SIZE) == 0 ||
685                    strncmp(rec->tag, "rels", ZFCP_DBF_TAG_SIZE) == 0 ||
686                    strncmp(rec->tag, "iels", ZFCP_DBF_TAG_SIZE) == 0) {
687                 len += zfcp_dbf_view(out_buf + len, "ls_code", "0x%02x",
688                                      rec->type.els.ls_code);
689                 total = rec->type.els.payload_size;
690                 buffer = rec->type.els.payload;
691                 buflen = min(total, ZFCP_DBF_ELS_PAYLOAD);
692         }
693
694         len += zfcp_dbf_view_dump(out_buf + len, "payload",
695                                   buffer, buflen, 0, total);
696
697         if (buflen == total)
698                 len += sprintf(out_buf + len, "\n");
699
700         return len;
701 }
702
703 static struct debug_view zfcp_san_dbf_view = {
704         "structured",
705         NULL,
706         &zfcp_dbf_view_header,
707         &zfcp_san_dbf_view_format,
708         NULL,
709         NULL
710 };
711
712 static void
713 _zfcp_scsi_dbf_event_common(const char *tag, const char *tag2, int level,
714                             struct zfcp_adapter *adapter,
715                             struct scsi_cmnd *scsi_cmnd,
716                             struct zfcp_fsf_req *fsf_req,
717                             unsigned long old_req_id)
718 {
719         struct zfcp_scsi_dbf_record *rec = &adapter->scsi_dbf_buf;
720         struct zfcp_dbf_dump *dump = (struct zfcp_dbf_dump *)rec;
721         unsigned long flags;
722         struct fcp_rsp_iu *fcp_rsp;
723         char *fcp_rsp_info = NULL, *fcp_sns_info = NULL;
724         int offset = 0, buflen = 0;
725
726         spin_lock_irqsave(&adapter->scsi_dbf_lock, flags);
727         do {
728                 memset(rec, 0, sizeof(struct zfcp_scsi_dbf_record));
729                 if (offset == 0) {
730                         strncpy(rec->tag, tag, ZFCP_DBF_TAG_SIZE);
731                         strncpy(rec->tag2, tag2, ZFCP_DBF_TAG_SIZE);
732                         if (scsi_cmnd != NULL) {
733                                 if (scsi_cmnd->device) {
734                                         rec->scsi_id = scsi_cmnd->device->id;
735                                         rec->scsi_lun = scsi_cmnd->device->lun;
736                                 }
737                                 rec->scsi_result = scsi_cmnd->result;
738                                 rec->scsi_cmnd = (unsigned long)scsi_cmnd;
739                                 rec->scsi_serial = scsi_cmnd->serial_number;
740                                 memcpy(rec->scsi_opcode, &scsi_cmnd->cmnd,
741                                         min((int)scsi_cmnd->cmd_len,
742                                                 ZFCP_DBF_SCSI_OPCODE));
743                                 rec->scsi_retries = scsi_cmnd->retries;
744                                 rec->scsi_allowed = scsi_cmnd->allowed;
745                         }
746                         if (fsf_req != NULL) {
747                                 fcp_rsp = (struct fcp_rsp_iu *)
748                                     &(fsf_req->qtcb->bottom.io.fcp_rsp);
749                                 fcp_rsp_info =
750                                     zfcp_get_fcp_rsp_info_ptr(fcp_rsp);
751                                 fcp_sns_info =
752                                     zfcp_get_fcp_sns_info_ptr(fcp_rsp);
753
754                                 rec->type.fcp.rsp_validity =
755                                     fcp_rsp->validity.value;
756                                 rec->type.fcp.rsp_scsi_status =
757                                     fcp_rsp->scsi_status;
758                                 rec->type.fcp.rsp_resid = fcp_rsp->fcp_resid;
759                                 if (fcp_rsp->validity.bits.fcp_rsp_len_valid)
760                                         rec->type.fcp.rsp_code =
761                                             *(fcp_rsp_info + 3);
762                                 if (fcp_rsp->validity.bits.fcp_sns_len_valid) {
763                                         buflen = min((int)fcp_rsp->fcp_sns_len,
764                                                      ZFCP_DBF_SCSI_MAX_FCP_SNS_INFO);
765                                         rec->type.fcp.sns_info_len = buflen;
766                                         memcpy(rec->type.fcp.sns_info,
767                                                fcp_sns_info,
768                                                min(buflen,
769                                                    ZFCP_DBF_SCSI_FCP_SNS_INFO));
770                                         offset += min(buflen,
771                                                       ZFCP_DBF_SCSI_FCP_SNS_INFO);
772                                 }
773
774                                 rec->fsf_reqid = (unsigned long)fsf_req;
775                                 rec->fsf_seqno = fsf_req->seq_no;
776                                 rec->fsf_issued = fsf_req->issued;
777                         }
778                         rec->type.old_fsf_reqid = old_req_id;
779                 } else {
780                         strncpy(dump->tag, "dump", ZFCP_DBF_TAG_SIZE);
781                         dump->total_size = buflen;
782                         dump->offset = offset;
783                         dump->size = min(buflen - offset,
784                                          (int)sizeof(struct
785                                                      zfcp_scsi_dbf_record) -
786                                          (int)sizeof(struct zfcp_dbf_dump));
787                         memcpy(dump->data, fcp_sns_info + offset, dump->size);
788                         offset += dump->size;
789                 }
790                 debug_event(adapter->scsi_dbf, level,
791                             rec, sizeof(struct zfcp_scsi_dbf_record));
792         } while (offset < buflen);
793         spin_unlock_irqrestore(&adapter->scsi_dbf_lock, flags);
794 }
795
796 void
797 zfcp_scsi_dbf_event_result(const char *tag, int level,
798                            struct zfcp_adapter *adapter,
799                            struct scsi_cmnd *scsi_cmnd,
800                            struct zfcp_fsf_req *fsf_req)
801 {
802         _zfcp_scsi_dbf_event_common("rslt", tag, level,
803                         adapter, scsi_cmnd, fsf_req, 0);
804 }
805
806 void
807 zfcp_scsi_dbf_event_abort(const char *tag, struct zfcp_adapter *adapter,
808                           struct scsi_cmnd *scsi_cmnd,
809                           struct zfcp_fsf_req *new_fsf_req,
810                           unsigned long old_req_id)
811 {
812         _zfcp_scsi_dbf_event_common("abrt", tag, 1,
813                         adapter, scsi_cmnd, new_fsf_req, old_req_id);
814 }
815
816 void
817 zfcp_scsi_dbf_event_devreset(const char *tag, u8 flag, struct zfcp_unit *unit,
818                              struct scsi_cmnd *scsi_cmnd)
819 {
820         struct zfcp_adapter *adapter = unit->port->adapter;
821
822         _zfcp_scsi_dbf_event_common(flag == FCP_TARGET_RESET ? "trst" : "lrst",
823                         tag, 1, adapter, scsi_cmnd, NULL, 0);
824 }
825
826 static int
827 zfcp_scsi_dbf_view_format(debug_info_t * id, struct debug_view *view,
828                           char *out_buf, const char *in_buf)
829 {
830         struct zfcp_scsi_dbf_record *rec =
831             (struct zfcp_scsi_dbf_record *)in_buf;
832         int len = 0;
833
834         if (strncmp(rec->tag, "dump", ZFCP_DBF_TAG_SIZE) == 0)
835                 return 0;
836
837         len += zfcp_dbf_tag(out_buf + len, "tag", rec->tag);
838         len += zfcp_dbf_tag(out_buf + len, "tag2", rec->tag2);
839         len += zfcp_dbf_view(out_buf + len, "scsi_id", "0x%08x", rec->scsi_id);
840         len += zfcp_dbf_view(out_buf + len, "scsi_lun", "0x%08x",
841                              rec->scsi_lun);
842         len += zfcp_dbf_view(out_buf + len, "scsi_result", "0x%08x",
843                              rec->scsi_result);
844         len += zfcp_dbf_view(out_buf + len, "scsi_cmnd", "0x%0Lx",
845                              rec->scsi_cmnd);
846         len += zfcp_dbf_view(out_buf + len, "scsi_serial", "0x%016Lx",
847                              rec->scsi_serial);
848         len += zfcp_dbf_view_dump(out_buf + len, "scsi_opcode",
849                                   rec->scsi_opcode,
850                                   ZFCP_DBF_SCSI_OPCODE,
851                                   0, ZFCP_DBF_SCSI_OPCODE);
852         len += zfcp_dbf_view(out_buf + len, "scsi_retries", "0x%02x",
853                              rec->scsi_retries);
854         len += zfcp_dbf_view(out_buf + len, "scsi_allowed", "0x%02x",
855                              rec->scsi_allowed);
856         if (strncmp(rec->tag, "abrt", ZFCP_DBF_TAG_SIZE) == 0) {
857                 len += zfcp_dbf_view(out_buf + len, "old_fsf_reqid", "0x%0Lx",
858                                      rec->type.old_fsf_reqid);
859         }
860         len += zfcp_dbf_view(out_buf + len, "fsf_reqid", "0x%0Lx",
861                              rec->fsf_reqid);
862         len += zfcp_dbf_view(out_buf + len, "fsf_seqno", "0x%08x",
863                              rec->fsf_seqno);
864         len += zfcp_dbf_stck(out_buf + len, "fsf_issued", rec->fsf_issued);
865         if (strncmp(rec->tag, "rslt", ZFCP_DBF_TAG_SIZE) == 0) {
866                 len +=
867                     zfcp_dbf_view(out_buf + len, "fcp_rsp_validity", "0x%02x",
868                                   rec->type.fcp.rsp_validity);
869                 len +=
870                     zfcp_dbf_view(out_buf + len, "fcp_rsp_scsi_status",
871                                   "0x%02x", rec->type.fcp.rsp_scsi_status);
872                 len +=
873                     zfcp_dbf_view(out_buf + len, "fcp_rsp_resid", "0x%08x",
874                                   rec->type.fcp.rsp_resid);
875                 len +=
876                     zfcp_dbf_view(out_buf + len, "fcp_rsp_code", "0x%08x",
877                                   rec->type.fcp.rsp_code);
878                 len +=
879                     zfcp_dbf_view(out_buf + len, "fcp_sns_info_len", "0x%08x",
880                                   rec->type.fcp.sns_info_len);
881                 len +=
882                     zfcp_dbf_view_dump(out_buf + len, "fcp_sns_info",
883                                        rec->type.fcp.sns_info,
884                                        min((int)rec->type.fcp.sns_info_len,
885                                            ZFCP_DBF_SCSI_FCP_SNS_INFO), 0,
886                                        rec->type.fcp.sns_info_len);
887         }
888
889         len += sprintf(out_buf + len, "\n");
890
891         return len;
892 }
893
894 static struct debug_view zfcp_scsi_dbf_view = {
895         "structured",
896         NULL,
897         &zfcp_dbf_view_header,
898         &zfcp_scsi_dbf_view_format,
899         NULL,
900         NULL
901 };
902
903 /**
904  * zfcp_adapter_debug_register - registers debug feature for an adapter
905  * @adapter: pointer to adapter for which debug features should be registered
906  * return: -ENOMEM on error, 0 otherwise
907  */
908 int zfcp_adapter_debug_register(struct zfcp_adapter *adapter)
909 {
910         char dbf_name[DEBUG_MAX_NAME_LEN];
911
912         /* debug feature area which records recovery activity */
913         sprintf(dbf_name, "zfcp_%s_erp", zfcp_get_busid_by_adapter(adapter));
914         adapter->erp_dbf = debug_register(dbf_name, dbfsize, 2,
915                                           sizeof(struct zfcp_erp_dbf_record));
916         if (!adapter->erp_dbf)
917                 goto failed;
918         debug_register_view(adapter->erp_dbf, &debug_hex_ascii_view);
919         debug_set_level(adapter->erp_dbf, 3);
920
921         /* debug feature area which records HBA (FSF and QDIO) conditions */
922         sprintf(dbf_name, "zfcp_%s_hba", zfcp_get_busid_by_adapter(adapter));
923         adapter->hba_dbf = debug_register(dbf_name, dbfsize, 1,
924                                           sizeof(struct zfcp_hba_dbf_record));
925         if (!adapter->hba_dbf)
926                 goto failed;
927         debug_register_view(adapter->hba_dbf, &debug_hex_ascii_view);
928         debug_register_view(adapter->hba_dbf, &zfcp_hba_dbf_view);
929         debug_set_level(adapter->hba_dbf, 3);
930
931         /* debug feature area which records SAN command failures and recovery */
932         sprintf(dbf_name, "zfcp_%s_san", zfcp_get_busid_by_adapter(adapter));
933         adapter->san_dbf = debug_register(dbf_name, dbfsize, 1,
934                                           sizeof(struct zfcp_san_dbf_record));
935         if (!adapter->san_dbf)
936                 goto failed;
937         debug_register_view(adapter->san_dbf, &debug_hex_ascii_view);
938         debug_register_view(adapter->san_dbf, &zfcp_san_dbf_view);
939         debug_set_level(adapter->san_dbf, 6);
940
941         /* debug feature area which records SCSI command failures and recovery */
942         sprintf(dbf_name, "zfcp_%s_scsi", zfcp_get_busid_by_adapter(adapter));
943         adapter->scsi_dbf = debug_register(dbf_name, dbfsize, 1,
944                                            sizeof(struct zfcp_scsi_dbf_record));
945         if (!adapter->scsi_dbf)
946                 goto failed;
947         debug_register_view(adapter->scsi_dbf, &debug_hex_ascii_view);
948         debug_register_view(adapter->scsi_dbf, &zfcp_scsi_dbf_view);
949         debug_set_level(adapter->scsi_dbf, 3);
950
951         return 0;
952
953  failed:
954         zfcp_adapter_debug_unregister(adapter);
955
956         return -ENOMEM;
957 }
958
959 /**
960  * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter
961  * @adapter: pointer to adapter for which debug features should be unregistered
962  */
963 void zfcp_adapter_debug_unregister(struct zfcp_adapter *adapter)
964 {
965         debug_unregister(adapter->scsi_dbf);
966         debug_unregister(adapter->san_dbf);
967         debug_unregister(adapter->hba_dbf);
968         debug_unregister(adapter->erp_dbf);
969         adapter->scsi_dbf = NULL;
970         adapter->san_dbf = NULL;
971         adapter->hba_dbf = NULL;
972         adapter->erp_dbf = NULL;
973 }
974
975 #undef ZFCP_LOG_AREA