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