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