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