GNU Linux-libre 4.4.283-gnu1
[releases.git] / drivers / s390 / scsi / zfcp_dbf.c
1 /*
2  * zfcp device driver
3  *
4  * Debug traces for zfcp.
5  *
6  * Copyright IBM Corp. 2002, 2018
7  */
8
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/module.h>
13 #include <linux/ctype.h>
14 #include <linux/slab.h>
15 #include <asm/debug.h>
16 #include "zfcp_dbf.h"
17 #include "zfcp_ext.h"
18 #include "zfcp_fc.h"
19
20 static u32 dbfsize = 4;
21
22 module_param(dbfsize, uint, 0400);
23 MODULE_PARM_DESC(dbfsize,
24                  "number of pages for each debug feature area (default 4)");
25
26 static u32 dbflevel = 3;
27
28 module_param(dbflevel, uint, 0400);
29 MODULE_PARM_DESC(dbflevel,
30                  "log level for each debug feature area "
31                  "(default 3, range 0..6)");
32
33 static inline unsigned int zfcp_dbf_plen(unsigned int offset)
34 {
35         return sizeof(struct zfcp_dbf_pay) + offset - ZFCP_DBF_PAY_MAX_REC;
36 }
37
38 static inline
39 void zfcp_dbf_pl_write(struct zfcp_dbf *dbf, void *data, u16 length, char *area,
40                        u64 req_id)
41 {
42         struct zfcp_dbf_pay *pl = &dbf->pay_buf;
43         u16 offset = 0, rec_length;
44
45         spin_lock(&dbf->pay_lock);
46         memset(pl, 0, sizeof(*pl));
47         pl->fsf_req_id = req_id;
48         memcpy(pl->area, area, ZFCP_DBF_TAG_LEN);
49
50         while (offset < length) {
51                 rec_length = min((u16) ZFCP_DBF_PAY_MAX_REC,
52                                  (u16) (length - offset));
53                 memcpy(pl->data, data + offset, rec_length);
54                 debug_event(dbf->pay, 1, pl, zfcp_dbf_plen(rec_length));
55
56                 offset += rec_length;
57                 pl->counter++;
58         }
59
60         spin_unlock(&dbf->pay_lock);
61 }
62
63 /**
64  * zfcp_dbf_hba_fsf_res - trace event for fsf responses
65  * @tag: tag indicating which kind of unsolicited status has been received
66  * @req: request for which a response was received
67  */
68 void zfcp_dbf_hba_fsf_res(char *tag, int level, struct zfcp_fsf_req *req)
69 {
70         struct zfcp_dbf *dbf = req->adapter->dbf;
71         struct fsf_qtcb_prefix *q_pref = &req->qtcb->prefix;
72         struct fsf_qtcb_header *q_head = &req->qtcb->header;
73         struct zfcp_dbf_hba *rec = &dbf->hba_buf;
74         unsigned long flags;
75
76         spin_lock_irqsave(&dbf->hba_lock, flags);
77         memset(rec, 0, sizeof(*rec));
78
79         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
80         rec->id = ZFCP_DBF_HBA_RES;
81         rec->fsf_req_id = req->req_id;
82         rec->fsf_req_status = req->status;
83         rec->fsf_cmd = req->fsf_command;
84         rec->fsf_seq_no = req->seq_no;
85         rec->u.res.req_issued = req->issued;
86         rec->u.res.prot_status = q_pref->prot_status;
87         rec->u.res.fsf_status = q_head->fsf_status;
88         rec->u.res.port_handle = q_head->port_handle;
89         rec->u.res.lun_handle = q_head->lun_handle;
90
91         memcpy(rec->u.res.prot_status_qual, &q_pref->prot_status_qual,
92                FSF_PROT_STATUS_QUAL_SIZE);
93         memcpy(rec->u.res.fsf_status_qual, &q_head->fsf_status_qual,
94                FSF_STATUS_QUALIFIER_SIZE);
95
96         rec->pl_len = q_head->log_length;
97         zfcp_dbf_pl_write(dbf, (char *)q_pref + q_head->log_start,
98                           rec->pl_len, "fsf_res", req->req_id);
99
100         debug_event(dbf->hba, level, rec, sizeof(*rec));
101         spin_unlock_irqrestore(&dbf->hba_lock, flags);
102 }
103
104 /**
105  * zfcp_dbf_hba_fsf_uss - trace event for an unsolicited status buffer
106  * @tag: tag indicating which kind of unsolicited status has been received
107  * @req: request providing the unsolicited status
108  */
109 void zfcp_dbf_hba_fsf_uss(char *tag, struct zfcp_fsf_req *req)
110 {
111         struct zfcp_dbf *dbf = req->adapter->dbf;
112         struct fsf_status_read_buffer *srb = req->data;
113         struct zfcp_dbf_hba *rec = &dbf->hba_buf;
114         unsigned long flags;
115
116         spin_lock_irqsave(&dbf->hba_lock, flags);
117         memset(rec, 0, sizeof(*rec));
118
119         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
120         rec->id = ZFCP_DBF_HBA_USS;
121         rec->fsf_req_id = req->req_id;
122         rec->fsf_req_status = req->status;
123         rec->fsf_cmd = req->fsf_command;
124
125         if (!srb)
126                 goto log;
127
128         rec->u.uss.status_type = srb->status_type;
129         rec->u.uss.status_subtype = srb->status_subtype;
130         rec->u.uss.d_id = ntoh24(srb->d_id);
131         rec->u.uss.lun = srb->fcp_lun;
132         memcpy(&rec->u.uss.queue_designator, &srb->queue_designator,
133                sizeof(rec->u.uss.queue_designator));
134
135         /* status read buffer payload length */
136         rec->pl_len = (!srb->length) ? 0 : srb->length -
137                         offsetof(struct fsf_status_read_buffer, payload);
138
139         if (rec->pl_len)
140                 zfcp_dbf_pl_write(dbf, srb->payload.data, rec->pl_len,
141                                   "fsf_uss", req->req_id);
142 log:
143         debug_event(dbf->hba, 2, rec, sizeof(*rec));
144         spin_unlock_irqrestore(&dbf->hba_lock, flags);
145 }
146
147 /**
148  * zfcp_dbf_hba_bit_err - trace event for bit error conditions
149  * @tag: tag indicating which kind of unsolicited status has been received
150  * @req: request which caused the bit_error condition
151  */
152 void zfcp_dbf_hba_bit_err(char *tag, struct zfcp_fsf_req *req)
153 {
154         struct zfcp_dbf *dbf = req->adapter->dbf;
155         struct zfcp_dbf_hba *rec = &dbf->hba_buf;
156         struct fsf_status_read_buffer *sr_buf = req->data;
157         unsigned long flags;
158
159         spin_lock_irqsave(&dbf->hba_lock, flags);
160         memset(rec, 0, sizeof(*rec));
161
162         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
163         rec->id = ZFCP_DBF_HBA_BIT;
164         rec->fsf_req_id = req->req_id;
165         rec->fsf_req_status = req->status;
166         rec->fsf_cmd = req->fsf_command;
167         memcpy(&rec->u.be, &sr_buf->payload.bit_error,
168                sizeof(struct fsf_bit_error_payload));
169
170         debug_event(dbf->hba, 1, rec, sizeof(*rec));
171         spin_unlock_irqrestore(&dbf->hba_lock, flags);
172 }
173
174 /**
175  * zfcp_dbf_hba_def_err - trace event for deferred error messages
176  * @adapter: pointer to struct zfcp_adapter
177  * @req_id: request id which caused the deferred error message
178  * @scount: number of sbals incl. the signaling sbal
179  * @pl: array of all involved sbals
180  */
181 void zfcp_dbf_hba_def_err(struct zfcp_adapter *adapter, u64 req_id, u16 scount,
182                           void **pl)
183 {
184         struct zfcp_dbf *dbf = adapter->dbf;
185         struct zfcp_dbf_pay *payload = &dbf->pay_buf;
186         unsigned long flags;
187         u16 length;
188
189         if (!pl)
190                 return;
191
192         spin_lock_irqsave(&dbf->pay_lock, flags);
193         memset(payload, 0, sizeof(*payload));
194
195         memcpy(payload->area, "def_err", 7);
196         payload->fsf_req_id = req_id;
197         payload->counter = 0;
198         length = min((u16)sizeof(struct qdio_buffer),
199                      (u16)ZFCP_DBF_PAY_MAX_REC);
200
201         while (payload->counter < scount && (char *)pl[payload->counter]) {
202                 memcpy(payload->data, (char *)pl[payload->counter], length);
203                 debug_event(dbf->pay, 1, payload, zfcp_dbf_plen(length));
204                 payload->counter++;
205         }
206
207         spin_unlock_irqrestore(&dbf->pay_lock, flags);
208 }
209
210 /**
211  * zfcp_dbf_hba_basic - trace event for basic adapter events
212  * @adapter: pointer to struct zfcp_adapter
213  */
214 void zfcp_dbf_hba_basic(char *tag, struct zfcp_adapter *adapter)
215 {
216         struct zfcp_dbf *dbf = adapter->dbf;
217         struct zfcp_dbf_hba *rec = &dbf->hba_buf;
218         unsigned long flags;
219
220         spin_lock_irqsave(&dbf->hba_lock, flags);
221         memset(rec, 0, sizeof(*rec));
222
223         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
224         rec->id = ZFCP_DBF_HBA_BASIC;
225
226         debug_event(dbf->hba, 1, rec, sizeof(*rec));
227         spin_unlock_irqrestore(&dbf->hba_lock, flags);
228 }
229
230 static void zfcp_dbf_set_common(struct zfcp_dbf_rec *rec,
231                                 struct zfcp_adapter *adapter,
232                                 struct zfcp_port *port,
233                                 struct scsi_device *sdev)
234 {
235         rec->adapter_status = atomic_read(&adapter->status);
236         if (port) {
237                 rec->port_status = atomic_read(&port->status);
238                 rec->wwpn = port->wwpn;
239                 rec->d_id = port->d_id;
240         }
241         if (sdev) {
242                 rec->lun_status = atomic_read(&sdev_to_zfcp(sdev)->status);
243                 rec->lun = zfcp_scsi_dev_lun(sdev);
244         } else
245                 rec->lun = ZFCP_DBF_INVALID_LUN;
246 }
247
248 /**
249  * zfcp_dbf_rec_trig - trace event related to triggered recovery
250  * @tag: identifier for event
251  * @adapter: adapter on which the erp_action should run
252  * @port: remote port involved in the erp_action
253  * @sdev: scsi device involved in the erp_action
254  * @want: wanted erp_action
255  * @need: required erp_action
256  *
257  * The adapter->erp_lock has to be held.
258  */
259 void zfcp_dbf_rec_trig(char *tag, struct zfcp_adapter *adapter,
260                        struct zfcp_port *port, struct scsi_device *sdev,
261                        u8 want, u8 need)
262 {
263         struct zfcp_dbf *dbf = adapter->dbf;
264         struct zfcp_dbf_rec *rec = &dbf->rec_buf;
265         struct list_head *entry;
266         unsigned long flags;
267
268         spin_lock_irqsave(&dbf->rec_lock, flags);
269         memset(rec, 0, sizeof(*rec));
270
271         rec->id = ZFCP_DBF_REC_TRIG;
272         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
273         zfcp_dbf_set_common(rec, adapter, port, sdev);
274
275         list_for_each(entry, &adapter->erp_ready_head)
276                 rec->u.trig.ready++;
277
278         list_for_each(entry, &adapter->erp_running_head)
279                 rec->u.trig.running++;
280
281         rec->u.trig.want = want;
282         rec->u.trig.need = need;
283
284         debug_event(dbf->rec, 1, rec, sizeof(*rec));
285         spin_unlock_irqrestore(&dbf->rec_lock, flags);
286 }
287
288 /**
289  * zfcp_dbf_rec_trig_lock - trace event related to triggered recovery with lock
290  * @tag: identifier for event
291  * @adapter: adapter on which the erp_action should run
292  * @port: remote port involved in the erp_action
293  * @sdev: scsi device involved in the erp_action
294  * @want: wanted erp_action
295  * @need: required erp_action
296  *
297  * The adapter->erp_lock must not be held.
298  */
299 void zfcp_dbf_rec_trig_lock(char *tag, struct zfcp_adapter *adapter,
300                             struct zfcp_port *port, struct scsi_device *sdev,
301                             u8 want, u8 need)
302 {
303         unsigned long flags;
304
305         read_lock_irqsave(&adapter->erp_lock, flags);
306         zfcp_dbf_rec_trig(tag, adapter, port, sdev, want, need);
307         read_unlock_irqrestore(&adapter->erp_lock, flags);
308 }
309
310 /**
311  * zfcp_dbf_rec_run_lvl - trace event related to running recovery
312  * @level: trace level to be used for event
313  * @tag: identifier for event
314  * @erp: erp_action running
315  */
316 void zfcp_dbf_rec_run_lvl(int level, char *tag, struct zfcp_erp_action *erp)
317 {
318         struct zfcp_dbf *dbf = erp->adapter->dbf;
319         struct zfcp_dbf_rec *rec = &dbf->rec_buf;
320         unsigned long flags;
321
322         spin_lock_irqsave(&dbf->rec_lock, flags);
323         memset(rec, 0, sizeof(*rec));
324
325         rec->id = ZFCP_DBF_REC_RUN;
326         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
327         zfcp_dbf_set_common(rec, erp->adapter, erp->port, erp->sdev);
328
329         rec->u.run.fsf_req_id = erp->fsf_req_id;
330         rec->u.run.rec_status = erp->status;
331         rec->u.run.rec_step = erp->step;
332         rec->u.run.rec_action = erp->action;
333
334         if (erp->sdev)
335                 rec->u.run.rec_count =
336                         atomic_read(&sdev_to_zfcp(erp->sdev)->erp_counter);
337         else if (erp->port)
338                 rec->u.run.rec_count = atomic_read(&erp->port->erp_counter);
339         else
340                 rec->u.run.rec_count = atomic_read(&erp->adapter->erp_counter);
341
342         debug_event(dbf->rec, level, rec, sizeof(*rec));
343         spin_unlock_irqrestore(&dbf->rec_lock, flags);
344 }
345
346 /**
347  * zfcp_dbf_rec_run - trace event related to running recovery
348  * @tag: identifier for event
349  * @erp: erp_action running
350  */
351 void zfcp_dbf_rec_run(char *tag, struct zfcp_erp_action *erp)
352 {
353         zfcp_dbf_rec_run_lvl(1, tag, erp);
354 }
355
356 /**
357  * zfcp_dbf_rec_run_wka - trace wka port event with info like running recovery
358  * @tag: identifier for event
359  * @wka_port: well known address port
360  * @req_id: request ID to correlate with potential HBA trace record
361  */
362 void zfcp_dbf_rec_run_wka(char *tag, struct zfcp_fc_wka_port *wka_port,
363                           u64 req_id)
364 {
365         struct zfcp_dbf *dbf = wka_port->adapter->dbf;
366         struct zfcp_dbf_rec *rec = &dbf->rec_buf;
367         unsigned long flags;
368
369         spin_lock_irqsave(&dbf->rec_lock, flags);
370         memset(rec, 0, sizeof(*rec));
371
372         rec->id = ZFCP_DBF_REC_RUN;
373         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
374         rec->port_status = wka_port->status;
375         rec->d_id = wka_port->d_id;
376         rec->lun = ZFCP_DBF_INVALID_LUN;
377
378         rec->u.run.fsf_req_id = req_id;
379         rec->u.run.rec_status = ~0;
380         rec->u.run.rec_step = ~0;
381         rec->u.run.rec_action = ~0;
382         rec->u.run.rec_count = ~0;
383
384         debug_event(dbf->rec, 1, rec, sizeof(*rec));
385         spin_unlock_irqrestore(&dbf->rec_lock, flags);
386 }
387
388 static inline
389 void zfcp_dbf_san(char *tag, struct zfcp_dbf *dbf,
390                   char *paytag, struct scatterlist *sg, u8 id, u16 len,
391                   u64 req_id, u32 d_id, u16 cap_len)
392 {
393         struct zfcp_dbf_san *rec = &dbf->san_buf;
394         u16 rec_len;
395         unsigned long flags;
396         struct zfcp_dbf_pay *payload = &dbf->pay_buf;
397         u16 pay_sum = 0;
398
399         spin_lock_irqsave(&dbf->san_lock, flags);
400         memset(rec, 0, sizeof(*rec));
401
402         rec->id = id;
403         rec->fsf_req_id = req_id;
404         rec->d_id = d_id;
405         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
406         rec->pl_len = len; /* full length even if we cap pay below */
407         if (!sg)
408                 goto out;
409         rec_len = min_t(unsigned int, sg->length, ZFCP_DBF_SAN_MAX_PAYLOAD);
410         memcpy(rec->payload, sg_virt(sg), rec_len); /* part of 1st sg entry */
411         if (len <= rec_len)
412                 goto out; /* skip pay record if full content in rec->payload */
413
414         /* if (len > rec_len):
415          * dump data up to cap_len ignoring small duplicate in rec->payload
416          */
417         spin_lock(&dbf->pay_lock);
418         memset(payload, 0, sizeof(*payload));
419         memcpy(payload->area, paytag, ZFCP_DBF_TAG_LEN);
420         payload->fsf_req_id = req_id;
421         payload->counter = 0;
422         for (; sg && pay_sum < cap_len; sg = sg_next(sg)) {
423                 u16 pay_len, offset = 0;
424
425                 while (offset < sg->length && pay_sum < cap_len) {
426                         pay_len = min((u16)ZFCP_DBF_PAY_MAX_REC,
427                                       (u16)(sg->length - offset));
428                         /* cap_len <= pay_sum < cap_len+ZFCP_DBF_PAY_MAX_REC */
429                         memcpy(payload->data, sg_virt(sg) + offset, pay_len);
430                         debug_event(dbf->pay, 1, payload,
431                                     zfcp_dbf_plen(pay_len));
432                         payload->counter++;
433                         offset += pay_len;
434                         pay_sum += pay_len;
435                 }
436         }
437         spin_unlock(&dbf->pay_lock);
438
439 out:
440         debug_event(dbf->san, 1, rec, sizeof(*rec));
441         spin_unlock_irqrestore(&dbf->san_lock, flags);
442 }
443
444 /**
445  * zfcp_dbf_san_req - trace event for issued SAN request
446  * @tag: identifier for event
447  * @fsf_req: request containing issued CT data
448  * d_id: destination ID
449  */
450 void zfcp_dbf_san_req(char *tag, struct zfcp_fsf_req *fsf, u32 d_id)
451 {
452         struct zfcp_dbf *dbf = fsf->adapter->dbf;
453         struct zfcp_fsf_ct_els *ct_els = fsf->data;
454         u16 length;
455
456         length = (u16)zfcp_qdio_real_bytes(ct_els->req);
457         zfcp_dbf_san(tag, dbf, "san_req", ct_els->req, ZFCP_DBF_SAN_REQ,
458                      length, fsf->req_id, d_id, length);
459 }
460
461 static u16 zfcp_dbf_san_res_cap_len_if_gpn_ft(char *tag,
462                                               struct zfcp_fsf_req *fsf,
463                                               u16 len)
464 {
465         struct zfcp_fsf_ct_els *ct_els = fsf->data;
466         struct fc_ct_hdr *reqh = sg_virt(ct_els->req);
467         struct fc_ns_gid_ft *reqn = (struct fc_ns_gid_ft *)(reqh + 1);
468         struct scatterlist *resp_entry = ct_els->resp;
469         struct fc_ct_hdr *resph;
470         struct fc_gpn_ft_resp *acc;
471         int max_entries, x, last = 0;
472
473         if (!(memcmp(tag, "fsscth2", 7) == 0
474               && ct_els->d_id == FC_FID_DIR_SERV
475               && reqh->ct_rev == FC_CT_REV
476               && reqh->ct_in_id[0] == 0
477               && reqh->ct_in_id[1] == 0
478               && reqh->ct_in_id[2] == 0
479               && reqh->ct_fs_type == FC_FST_DIR
480               && reqh->ct_fs_subtype == FC_NS_SUBTYPE
481               && reqh->ct_options == 0
482               && reqh->_ct_resvd1 == 0
483               && reqh->ct_cmd == FC_NS_GPN_FT
484               /* reqh->ct_mr_size can vary so do not match but read below */
485               && reqh->_ct_resvd2 == 0
486               && reqh->ct_reason == 0
487               && reqh->ct_explan == 0
488               && reqh->ct_vendor == 0
489               && reqn->fn_resvd == 0
490               && reqn->fn_domain_id_scope == 0
491               && reqn->fn_area_id_scope == 0
492               && reqn->fn_fc4_type == FC_TYPE_FCP))
493                 return len; /* not GPN_FT response so do not cap */
494
495         acc = sg_virt(resp_entry);
496
497         /* cap all but accept CT responses to at least the CT header */
498         resph = (struct fc_ct_hdr *)acc;
499         if ((ct_els->status) ||
500             (resph->ct_cmd != cpu_to_be16(FC_FS_ACC)))
501                 return max(FC_CT_HDR_LEN, ZFCP_DBF_SAN_MAX_PAYLOAD);
502
503         max_entries = (reqh->ct_mr_size * 4 / sizeof(struct fc_gpn_ft_resp))
504                 + 1 /* zfcp_fc_scan_ports: bytes correct, entries off-by-one
505                      * to account for header as 1st pseudo "entry" */;
506
507         /* the basic CT_IU preamble is the same size as one entry in the GPN_FT
508          * response, allowing us to skip special handling for it - just skip it
509          */
510         for (x = 1; x < max_entries && !last; x++) {
511                 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
512                         acc++;
513                 else
514                         acc = sg_virt(++resp_entry);
515
516                 last = acc->fp_flags & FC_NS_FID_LAST;
517         }
518         len = min(len, (u16)(x * sizeof(struct fc_gpn_ft_resp)));
519         return len; /* cap after last entry */
520 }
521
522 /**
523  * zfcp_dbf_san_res - trace event for received SAN request
524  * @tag: identifier for event
525  * @fsf_req: request containing issued CT data
526  */
527 void zfcp_dbf_san_res(char *tag, struct zfcp_fsf_req *fsf)
528 {
529         struct zfcp_dbf *dbf = fsf->adapter->dbf;
530         struct zfcp_fsf_ct_els *ct_els = fsf->data;
531         u16 length;
532
533         length = (u16)zfcp_qdio_real_bytes(ct_els->resp);
534         zfcp_dbf_san(tag, dbf, "san_res", ct_els->resp, ZFCP_DBF_SAN_RES,
535                      length, fsf->req_id, ct_els->d_id,
536                      zfcp_dbf_san_res_cap_len_if_gpn_ft(tag, fsf, length));
537 }
538
539 /**
540  * zfcp_dbf_san_in_els - trace event for incoming ELS
541  * @tag: identifier for event
542  * @fsf_req: request containing issued CT data
543  */
544 void zfcp_dbf_san_in_els(char *tag, struct zfcp_fsf_req *fsf)
545 {
546         struct zfcp_dbf *dbf = fsf->adapter->dbf;
547         struct fsf_status_read_buffer *srb =
548                 (struct fsf_status_read_buffer *) fsf->data;
549         u16 length;
550         struct scatterlist sg;
551
552         length = (u16)(srb->length -
553                         offsetof(struct fsf_status_read_buffer, payload));
554         sg_init_one(&sg, srb->payload.data, length);
555         zfcp_dbf_san(tag, dbf, "san_els", &sg, ZFCP_DBF_SAN_ELS, length,
556                      fsf->req_id, ntoh24(srb->d_id), length);
557 }
558
559 /**
560  * zfcp_dbf_scsi - trace event for scsi commands
561  * @tag: identifier for event
562  * @sc: pointer to struct scsi_cmnd
563  * @fsf: pointer to struct zfcp_fsf_req
564  */
565 void zfcp_dbf_scsi(char *tag, int level, struct scsi_cmnd *sc,
566                    struct zfcp_fsf_req *fsf)
567 {
568         struct zfcp_adapter *adapter =
569                 (struct zfcp_adapter *) sc->device->host->hostdata[0];
570         struct zfcp_dbf *dbf = adapter->dbf;
571         struct zfcp_dbf_scsi *rec = &dbf->scsi_buf;
572         struct fcp_resp_with_ext *fcp_rsp;
573         struct fcp_resp_rsp_info *fcp_rsp_info;
574         unsigned long flags;
575
576         spin_lock_irqsave(&dbf->scsi_lock, flags);
577         memset(rec, 0, sizeof(*rec));
578
579         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
580         rec->id = ZFCP_DBF_SCSI_CMND;
581         rec->scsi_result = sc->result;
582         rec->scsi_retries = sc->retries;
583         rec->scsi_allowed = sc->allowed;
584         rec->scsi_id = sc->device->id;
585         rec->scsi_lun = (u32)sc->device->lun;
586         rec->scsi_lun_64_hi = (u32)(sc->device->lun >> 32);
587         rec->host_scribble = (unsigned long)sc->host_scribble;
588
589         memcpy(rec->scsi_opcode, sc->cmnd,
590                min((int)sc->cmd_len, ZFCP_DBF_SCSI_OPCODE));
591
592         if (fsf) {
593                 rec->fsf_req_id = fsf->req_id;
594                 rec->pl_len = FCP_RESP_WITH_EXT;
595                 fcp_rsp = (struct fcp_resp_with_ext *)
596                                 &(fsf->qtcb->bottom.io.fcp_rsp);
597                 /* mandatory parts of FCP_RSP IU in this SCSI record */
598                 memcpy(&rec->fcp_rsp, fcp_rsp, FCP_RESP_WITH_EXT);
599                 if (fcp_rsp->resp.fr_flags & FCP_RSP_LEN_VAL) {
600                         fcp_rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
601                         rec->fcp_rsp_info = fcp_rsp_info->rsp_code;
602                         rec->pl_len += be32_to_cpu(fcp_rsp->ext.fr_rsp_len);
603                 }
604                 if (fcp_rsp->resp.fr_flags & FCP_SNS_LEN_VAL) {
605                         rec->pl_len += be32_to_cpu(fcp_rsp->ext.fr_sns_len);
606                 }
607                 /* complete FCP_RSP IU in associated PAYload record
608                  * but only if there are optional parts
609                  */
610                 if (fcp_rsp->resp.fr_flags != 0)
611                         zfcp_dbf_pl_write(
612                                 dbf, fcp_rsp,
613                                 /* at least one full PAY record
614                                  * but not beyond hardware response field
615                                  */
616                                 min_t(u16, max_t(u16, rec->pl_len,
617                                                  ZFCP_DBF_PAY_MAX_REC),
618                                       FSF_FCP_RSP_SIZE),
619                                 "fcp_riu", fsf->req_id);
620         }
621
622         debug_event(dbf->scsi, level, rec, sizeof(*rec));
623         spin_unlock_irqrestore(&dbf->scsi_lock, flags);
624 }
625
626 /**
627  * zfcp_dbf_scsi_eh() - Trace event for special cases of scsi_eh callbacks.
628  * @tag: Identifier for event.
629  * @adapter: Pointer to zfcp adapter as context for this event.
630  * @scsi_id: SCSI ID/target to indicate scope of task management function (TMF).
631  * @ret: Return value of calling function.
632  *
633  * This SCSI trace variant does not depend on any of:
634  * scsi_cmnd, zfcp_fsf_req, scsi_device.
635  */
636 void zfcp_dbf_scsi_eh(char *tag, struct zfcp_adapter *adapter,
637                       unsigned int scsi_id, int ret)
638 {
639         struct zfcp_dbf *dbf = adapter->dbf;
640         struct zfcp_dbf_scsi *rec = &dbf->scsi_buf;
641         unsigned long flags;
642         static int const level = 1;
643
644         if (unlikely(!debug_level_enabled(adapter->dbf->scsi, level)))
645                 return;
646
647         spin_lock_irqsave(&dbf->scsi_lock, flags);
648         memset(rec, 0, sizeof(*rec));
649
650         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
651         rec->id = ZFCP_DBF_SCSI_CMND;
652         rec->scsi_result = ret; /* re-use field, int is 4 bytes and fits */
653         rec->scsi_retries = ~0;
654         rec->scsi_allowed = ~0;
655         rec->fcp_rsp_info = ~0;
656         rec->scsi_id = scsi_id;
657         rec->scsi_lun = (u32)ZFCP_DBF_INVALID_LUN;
658         rec->scsi_lun_64_hi = (u32)(ZFCP_DBF_INVALID_LUN >> 32);
659         rec->host_scribble = ~0;
660         memset(rec->scsi_opcode, 0xff, ZFCP_DBF_SCSI_OPCODE);
661
662         debug_event(dbf->scsi, level, rec, sizeof(*rec));
663         spin_unlock_irqrestore(&dbf->scsi_lock, flags);
664 }
665
666 static debug_info_t *zfcp_dbf_reg(const char *name, int size, int rec_size)
667 {
668         struct debug_info *d;
669
670         d = debug_register(name, size, 1, rec_size);
671         if (!d)
672                 return NULL;
673
674         debug_register_view(d, &debug_hex_ascii_view);
675         debug_set_level(d, dbflevel);
676
677         return d;
678 }
679
680 static void zfcp_dbf_unregister(struct zfcp_dbf *dbf)
681 {
682         if (!dbf)
683                 return;
684
685         debug_unregister(dbf->scsi);
686         debug_unregister(dbf->san);
687         debug_unregister(dbf->hba);
688         debug_unregister(dbf->pay);
689         debug_unregister(dbf->rec);
690         kfree(dbf);
691 }
692
693 /**
694  * zfcp_adapter_debug_register - registers debug feature for an adapter
695  * @adapter: pointer to adapter for which debug features should be registered
696  * return: -ENOMEM on error, 0 otherwise
697  */
698 int zfcp_dbf_adapter_register(struct zfcp_adapter *adapter)
699 {
700         char name[DEBUG_MAX_NAME_LEN];
701         struct zfcp_dbf *dbf;
702
703         dbf = kzalloc(sizeof(struct zfcp_dbf), GFP_KERNEL);
704         if (!dbf)
705                 return -ENOMEM;
706
707         spin_lock_init(&dbf->pay_lock);
708         spin_lock_init(&dbf->hba_lock);
709         spin_lock_init(&dbf->san_lock);
710         spin_lock_init(&dbf->scsi_lock);
711         spin_lock_init(&dbf->rec_lock);
712
713         /* debug feature area which records recovery activity */
714         sprintf(name, "zfcp_%s_rec", dev_name(&adapter->ccw_device->dev));
715         dbf->rec = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_rec));
716         if (!dbf->rec)
717                 goto err_out;
718
719         /* debug feature area which records HBA (FSF and QDIO) conditions */
720         sprintf(name, "zfcp_%s_hba", dev_name(&adapter->ccw_device->dev));
721         dbf->hba = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_hba));
722         if (!dbf->hba)
723                 goto err_out;
724
725         /* debug feature area which records payload info */
726         sprintf(name, "zfcp_%s_pay", dev_name(&adapter->ccw_device->dev));
727         dbf->pay = zfcp_dbf_reg(name, dbfsize * 2, sizeof(struct zfcp_dbf_pay));
728         if (!dbf->pay)
729                 goto err_out;
730
731         /* debug feature area which records SAN command failures and recovery */
732         sprintf(name, "zfcp_%s_san", dev_name(&adapter->ccw_device->dev));
733         dbf->san = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_san));
734         if (!dbf->san)
735                 goto err_out;
736
737         /* debug feature area which records SCSI command failures and recovery */
738         sprintf(name, "zfcp_%s_scsi", dev_name(&adapter->ccw_device->dev));
739         dbf->scsi = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_scsi));
740         if (!dbf->scsi)
741                 goto err_out;
742
743         adapter->dbf = dbf;
744
745         return 0;
746 err_out:
747         zfcp_dbf_unregister(dbf);
748         return -ENOMEM;
749 }
750
751 /**
752  * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter
753  * @adapter: pointer to adapter for which debug features should be unregistered
754  */
755 void zfcp_dbf_adapter_unregister(struct zfcp_adapter *adapter)
756 {
757         struct zfcp_dbf *dbf = adapter->dbf;
758
759         adapter->dbf = NULL;
760         zfcp_dbf_unregister(dbf);
761 }
762