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