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