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