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