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 
zfcp_dbf_plen(unsigned int offset)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
zfcp_dbf_pl_write(struct zfcp_dbf * dbf,void * data,u16 length,char * area,u64 req_id)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 unsolicited status has been received
67  * @req: request for which a response was received
68  */
zfcp_dbf_hba_fsf_res(char * tag,int level,struct zfcp_fsf_req * req)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 = req->fsf_command;
85 	rec->fsf_seq_no = 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 	if (req->fsf_command != FSF_QTCB_FCP_CMND) {
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 
103 	debug_event(dbf->hba, level, rec, sizeof(*rec));
104 	spin_unlock_irqrestore(&dbf->hba_lock, flags);
105 }
106 
107 /**
108  * zfcp_dbf_hba_fsf_uss - trace event for an unsolicited status buffer
109  * @tag: tag indicating which kind of unsolicited status has been received
110  * @req: request providing the unsolicited status
111  */
zfcp_dbf_hba_fsf_uss(char * tag,struct zfcp_fsf_req * req)112 void zfcp_dbf_hba_fsf_uss(char *tag, struct zfcp_fsf_req *req)
113 {
114 	struct zfcp_dbf *dbf = req->adapter->dbf;
115 	struct fsf_status_read_buffer *srb = req->data;
116 	struct zfcp_dbf_hba *rec = &dbf->hba_buf;
117 	static int const level = 2;
118 	unsigned long flags;
119 
120 	if (unlikely(!debug_level_enabled(dbf->hba, level)))
121 		return;
122 
123 	spin_lock_irqsave(&dbf->hba_lock, flags);
124 	memset(rec, 0, sizeof(*rec));
125 
126 	memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
127 	rec->id = ZFCP_DBF_HBA_USS;
128 	rec->fsf_req_id = req->req_id;
129 	rec->fsf_req_status = req->status;
130 	rec->fsf_cmd = req->fsf_command;
131 
132 	if (!srb)
133 		goto log;
134 
135 	rec->u.uss.status_type = srb->status_type;
136 	rec->u.uss.status_subtype = srb->status_subtype;
137 	rec->u.uss.d_id = ntoh24(srb->d_id);
138 	rec->u.uss.lun = srb->fcp_lun;
139 	memcpy(&rec->u.uss.queue_designator, &srb->queue_designator,
140 	       sizeof(rec->u.uss.queue_designator));
141 
142 	/* status read buffer payload length */
143 	rec->pl_len = (!srb->length) ? 0 : srb->length -
144 			offsetof(struct fsf_status_read_buffer, payload);
145 
146 	if (rec->pl_len)
147 		zfcp_dbf_pl_write(dbf, srb->payload.data, rec->pl_len,
148 				  "fsf_uss", req->req_id);
149 log:
150 	debug_event(dbf->hba, level, rec, sizeof(*rec));
151 	spin_unlock_irqrestore(&dbf->hba_lock, flags);
152 }
153 
154 /**
155  * zfcp_dbf_hba_bit_err - trace event for bit error conditions
156  * @tag: tag indicating which kind of unsolicited status has been received
157  * @req: request which caused the bit_error condition
158  */
zfcp_dbf_hba_bit_err(char * tag,struct zfcp_fsf_req * req)159 void zfcp_dbf_hba_bit_err(char *tag, struct zfcp_fsf_req *req)
160 {
161 	struct zfcp_dbf *dbf = req->adapter->dbf;
162 	struct zfcp_dbf_hba *rec = &dbf->hba_buf;
163 	struct fsf_status_read_buffer *sr_buf = req->data;
164 	static int const level = 1;
165 	unsigned long flags;
166 
167 	if (unlikely(!debug_level_enabled(dbf->hba, level)))
168 		return;
169 
170 	spin_lock_irqsave(&dbf->hba_lock, flags);
171 	memset(rec, 0, sizeof(*rec));
172 
173 	memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
174 	rec->id = ZFCP_DBF_HBA_BIT;
175 	rec->fsf_req_id = req->req_id;
176 	rec->fsf_req_status = req->status;
177 	rec->fsf_cmd = req->fsf_command;
178 	memcpy(&rec->u.be, &sr_buf->payload.bit_error,
179 	       sizeof(struct fsf_bit_error_payload));
180 
181 	debug_event(dbf->hba, level, rec, sizeof(*rec));
182 	spin_unlock_irqrestore(&dbf->hba_lock, flags);
183 }
184 
185 /**
186  * zfcp_dbf_hba_def_err - trace event for deferred error messages
187  * @adapter: pointer to struct zfcp_adapter
188  * @req_id: request id which caused the deferred error message
189  * @scount: number of sbals incl. the signaling sbal
190  * @pl: array of all involved sbals
191  */
zfcp_dbf_hba_def_err(struct zfcp_adapter * adapter,u64 req_id,u16 scount,void ** pl)192 void zfcp_dbf_hba_def_err(struct zfcp_adapter *adapter, u64 req_id, u16 scount,
193 			  void **pl)
194 {
195 	struct zfcp_dbf *dbf = adapter->dbf;
196 	struct zfcp_dbf_pay *payload = &dbf->pay_buf;
197 	unsigned long flags;
198 	static int const level = 1;
199 	u16 length;
200 
201 	if (unlikely(!debug_level_enabled(dbf->pay, level)))
202 		return;
203 
204 	if (!pl)
205 		return;
206 
207 	spin_lock_irqsave(&dbf->pay_lock, flags);
208 	memset(payload, 0, sizeof(*payload));
209 
210 	memcpy(payload->area, "def_err", 7);
211 	payload->fsf_req_id = req_id;
212 	payload->counter = 0;
213 	length = min((u16)sizeof(struct qdio_buffer),
214 		     (u16)ZFCP_DBF_PAY_MAX_REC);
215 
216 	while (payload->counter < scount && (char *)pl[payload->counter]) {
217 		memcpy(payload->data, (char *)pl[payload->counter], length);
218 		debug_event(dbf->pay, level, payload, zfcp_dbf_plen(length));
219 		payload->counter++;
220 	}
221 
222 	spin_unlock_irqrestore(&dbf->pay_lock, flags);
223 }
224 
225 /**
226  * zfcp_dbf_hba_basic - trace event for basic adapter events
227  * @adapter: pointer to struct zfcp_adapter
228  */
zfcp_dbf_hba_basic(char * tag,struct zfcp_adapter * adapter)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 
zfcp_dbf_set_common(struct zfcp_dbf_rec * rec,struct zfcp_adapter * adapter,struct zfcp_port * port,struct scsi_device * sdev)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  */
zfcp_dbf_rec_trig(char * tag,struct zfcp_adapter * adapter,struct zfcp_port * port,struct scsi_device * sdev,u8 want,u8 need)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  */
zfcp_dbf_rec_trig_lock(char * tag,struct zfcp_adapter * adapter,struct zfcp_port * port,struct scsi_device * sdev,u8 want,u8 need)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  */
zfcp_dbf_rec_run_lvl(int level,char * tag,struct zfcp_erp_action * erp)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->action;
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  */
zfcp_dbf_rec_run(char * tag,struct zfcp_erp_action * erp)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  */
zfcp_dbf_rec_run_wka(char * tag,struct zfcp_fc_wka_port * wka_port,u64 req_id)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
zfcp_dbf_san(char * tag,struct zfcp_dbf * dbf,char * paytag,struct scatterlist * sg,u8 id,u16 len,u64 req_id,u32 d_id,u16 cap_len)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_req: request containing issued CT data
482  * d_id: destination ID
483  */
zfcp_dbf_san_req(char * tag,struct zfcp_fsf_req * fsf,u32 d_id)484 void zfcp_dbf_san_req(char *tag, struct zfcp_fsf_req *fsf, u32 d_id)
485 {
486 	struct zfcp_dbf *dbf = fsf->adapter->dbf;
487 	struct zfcp_fsf_ct_els *ct_els = fsf->data;
488 	u16 length;
489 
490 	if (unlikely(!debug_level_enabled(dbf->san, ZFCP_DBF_SAN_LEVEL)))
491 		return;
492 
493 	length = (u16)zfcp_qdio_real_bytes(ct_els->req);
494 	zfcp_dbf_san(tag, dbf, "san_req", ct_els->req, ZFCP_DBF_SAN_REQ,
495 		     length, fsf->req_id, d_id, length);
496 }
497 
zfcp_dbf_san_res_cap_len_if_gpn_ft(char * tag,struct zfcp_fsf_req * fsf,u16 len)498 static u16 zfcp_dbf_san_res_cap_len_if_gpn_ft(char *tag,
499 					      struct zfcp_fsf_req *fsf,
500 					      u16 len)
501 {
502 	struct zfcp_fsf_ct_els *ct_els = fsf->data;
503 	struct fc_ct_hdr *reqh = sg_virt(ct_els->req);
504 	struct fc_ns_gid_ft *reqn = (struct fc_ns_gid_ft *)(reqh + 1);
505 	struct scatterlist *resp_entry = ct_els->resp;
506 	struct fc_ct_hdr *resph;
507 	struct fc_gpn_ft_resp *acc;
508 	int max_entries, x, last = 0;
509 
510 	if (!(memcmp(tag, "fsscth2", 7) == 0
511 	      && ct_els->d_id == FC_FID_DIR_SERV
512 	      && reqh->ct_rev == FC_CT_REV
513 	      && reqh->ct_in_id[0] == 0
514 	      && reqh->ct_in_id[1] == 0
515 	      && reqh->ct_in_id[2] == 0
516 	      && reqh->ct_fs_type == FC_FST_DIR
517 	      && reqh->ct_fs_subtype == FC_NS_SUBTYPE
518 	      && reqh->ct_options == 0
519 	      && reqh->_ct_resvd1 == 0
520 	      && reqh->ct_cmd == cpu_to_be16(FC_NS_GPN_FT)
521 	      /* reqh->ct_mr_size can vary so do not match but read below */
522 	      && reqh->_ct_resvd2 == 0
523 	      && reqh->ct_reason == 0
524 	      && reqh->ct_explan == 0
525 	      && reqh->ct_vendor == 0
526 	      && reqn->fn_resvd == 0
527 	      && reqn->fn_domain_id_scope == 0
528 	      && reqn->fn_area_id_scope == 0
529 	      && reqn->fn_fc4_type == FC_TYPE_FCP))
530 		return len; /* not GPN_FT response so do not cap */
531 
532 	acc = sg_virt(resp_entry);
533 
534 	/* cap all but accept CT responses to at least the CT header */
535 	resph = (struct fc_ct_hdr *)acc;
536 	if ((ct_els->status) ||
537 	    (resph->ct_cmd != cpu_to_be16(FC_FS_ACC)))
538 		return max(FC_CT_HDR_LEN, ZFCP_DBF_SAN_MAX_PAYLOAD);
539 
540 	max_entries = (be16_to_cpu(reqh->ct_mr_size) * 4 /
541 		       sizeof(struct fc_gpn_ft_resp))
542 		+ 1 /* zfcp_fc_scan_ports: bytes correct, entries off-by-one
543 		     * to account for header as 1st pseudo "entry" */;
544 
545 	/* the basic CT_IU preamble is the same size as one entry in the GPN_FT
546 	 * response, allowing us to skip special handling for it - just skip it
547 	 */
548 	for (x = 1; x < max_entries && !last; x++) {
549 		if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
550 			acc++;
551 		else
552 			acc = sg_virt(++resp_entry);
553 
554 		last = acc->fp_flags & FC_NS_FID_LAST;
555 	}
556 	len = min(len, (u16)(x * sizeof(struct fc_gpn_ft_resp)));
557 	return len; /* cap after last entry */
558 }
559 
560 /**
561  * zfcp_dbf_san_res - trace event for received SAN request
562  * @tag: identifier for event
563  * @fsf_req: request containing issued CT data
564  */
zfcp_dbf_san_res(char * tag,struct zfcp_fsf_req * fsf)565 void zfcp_dbf_san_res(char *tag, struct zfcp_fsf_req *fsf)
566 {
567 	struct zfcp_dbf *dbf = fsf->adapter->dbf;
568 	struct zfcp_fsf_ct_els *ct_els = fsf->data;
569 	u16 length;
570 
571 	if (unlikely(!debug_level_enabled(dbf->san, ZFCP_DBF_SAN_LEVEL)))
572 		return;
573 
574 	length = (u16)zfcp_qdio_real_bytes(ct_els->resp);
575 	zfcp_dbf_san(tag, dbf, "san_res", ct_els->resp, ZFCP_DBF_SAN_RES,
576 		     length, fsf->req_id, ct_els->d_id,
577 		     zfcp_dbf_san_res_cap_len_if_gpn_ft(tag, fsf, length));
578 }
579 
580 /**
581  * zfcp_dbf_san_in_els - trace event for incoming ELS
582  * @tag: identifier for event
583  * @fsf_req: request containing issued CT data
584  */
zfcp_dbf_san_in_els(char * tag,struct zfcp_fsf_req * fsf)585 void zfcp_dbf_san_in_els(char *tag, struct zfcp_fsf_req *fsf)
586 {
587 	struct zfcp_dbf *dbf = fsf->adapter->dbf;
588 	struct fsf_status_read_buffer *srb =
589 		(struct fsf_status_read_buffer *) fsf->data;
590 	u16 length;
591 	struct scatterlist sg;
592 
593 	if (unlikely(!debug_level_enabled(dbf->san, ZFCP_DBF_SAN_LEVEL)))
594 		return;
595 
596 	length = (u16)(srb->length -
597 			offsetof(struct fsf_status_read_buffer, payload));
598 	sg_init_one(&sg, srb->payload.data, length);
599 	zfcp_dbf_san(tag, dbf, "san_els", &sg, ZFCP_DBF_SAN_ELS, length,
600 		     fsf->req_id, ntoh24(srb->d_id), length);
601 }
602 
603 /**
604  * zfcp_dbf_scsi_common() - Common trace event helper for scsi.
605  * @tag: Identifier for event.
606  * @level: trace level of event.
607  * @sdev: Pointer to SCSI device as context for this event.
608  * @sc: Pointer to SCSI command, or NULL with task management function (TMF).
609  * @fsf: Pointer to FSF request, or NULL.
610  */
zfcp_dbf_scsi_common(char * tag,int level,struct scsi_device * sdev,struct scsi_cmnd * sc,struct zfcp_fsf_req * fsf)611 void zfcp_dbf_scsi_common(char *tag, int level, struct scsi_device *sdev,
612 			  struct scsi_cmnd *sc, struct zfcp_fsf_req *fsf)
613 {
614 	struct zfcp_adapter *adapter =
615 		(struct zfcp_adapter *) sdev->host->hostdata[0];
616 	struct zfcp_dbf *dbf = adapter->dbf;
617 	struct zfcp_dbf_scsi *rec = &dbf->scsi_buf;
618 	struct fcp_resp_with_ext *fcp_rsp;
619 	struct fcp_resp_rsp_info *fcp_rsp_info;
620 	unsigned long flags;
621 
622 	spin_lock_irqsave(&dbf->scsi_lock, flags);
623 	memset(rec, 0, sizeof(*rec));
624 
625 	memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
626 	rec->id = ZFCP_DBF_SCSI_CMND;
627 	if (sc) {
628 		rec->scsi_result = sc->result;
629 		rec->scsi_retries = sc->retries;
630 		rec->scsi_allowed = sc->allowed;
631 		rec->scsi_id = sc->device->id;
632 		rec->scsi_lun = (u32)sc->device->lun;
633 		rec->scsi_lun_64_hi = (u32)(sc->device->lun >> 32);
634 		rec->host_scribble = (unsigned long)sc->host_scribble;
635 
636 		memcpy(rec->scsi_opcode, sc->cmnd,
637 		       min_t(int, sc->cmd_len, ZFCP_DBF_SCSI_OPCODE));
638 	} else {
639 		rec->scsi_result = ~0;
640 		rec->scsi_retries = ~0;
641 		rec->scsi_allowed = ~0;
642 		rec->scsi_id = sdev->id;
643 		rec->scsi_lun = (u32)sdev->lun;
644 		rec->scsi_lun_64_hi = (u32)(sdev->lun >> 32);
645 		rec->host_scribble = ~0;
646 
647 		memset(rec->scsi_opcode, 0xff, ZFCP_DBF_SCSI_OPCODE);
648 	}
649 
650 	if (fsf) {
651 		rec->fsf_req_id = fsf->req_id;
652 		rec->pl_len = FCP_RESP_WITH_EXT;
653 		fcp_rsp = &(fsf->qtcb->bottom.io.fcp_rsp.iu);
654 		/* mandatory parts of FCP_RSP IU in this SCSI record */
655 		memcpy(&rec->fcp_rsp, fcp_rsp, FCP_RESP_WITH_EXT);
656 		if (fcp_rsp->resp.fr_flags & FCP_RSP_LEN_VAL) {
657 			fcp_rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
658 			rec->fcp_rsp_info = fcp_rsp_info->rsp_code;
659 			rec->pl_len += be32_to_cpu(fcp_rsp->ext.fr_rsp_len);
660 		}
661 		if (fcp_rsp->resp.fr_flags & FCP_SNS_LEN_VAL) {
662 			rec->pl_len += be32_to_cpu(fcp_rsp->ext.fr_sns_len);
663 		}
664 		/* complete FCP_RSP IU in associated PAYload record
665 		 * but only if there are optional parts
666 		 */
667 		if (fcp_rsp->resp.fr_flags != 0)
668 			zfcp_dbf_pl_write(
669 				dbf, fcp_rsp,
670 				/* at least one full PAY record
671 				 * but not beyond hardware response field
672 				 */
673 				min_t(u16, max_t(u16, rec->pl_len,
674 						 ZFCP_DBF_PAY_MAX_REC),
675 				      FSF_FCP_RSP_SIZE),
676 				"fcp_riu", fsf->req_id);
677 	}
678 
679 	debug_event(dbf->scsi, level, rec, sizeof(*rec));
680 	spin_unlock_irqrestore(&dbf->scsi_lock, flags);
681 }
682 
683 /**
684  * zfcp_dbf_scsi_eh() - Trace event for special cases of scsi_eh callbacks.
685  * @tag: Identifier for event.
686  * @adapter: Pointer to zfcp adapter as context for this event.
687  * @scsi_id: SCSI ID/target to indicate scope of task management function (TMF).
688  * @ret: Return value of calling function.
689  *
690  * This SCSI trace variant does not depend on any of:
691  * scsi_cmnd, zfcp_fsf_req, scsi_device.
692  */
zfcp_dbf_scsi_eh(char * tag,struct zfcp_adapter * adapter,unsigned int scsi_id,int ret)693 void zfcp_dbf_scsi_eh(char *tag, struct zfcp_adapter *adapter,
694 		      unsigned int scsi_id, int ret)
695 {
696 	struct zfcp_dbf *dbf = adapter->dbf;
697 	struct zfcp_dbf_scsi *rec = &dbf->scsi_buf;
698 	unsigned long flags;
699 	static int const level = 1;
700 
701 	if (unlikely(!debug_level_enabled(adapter->dbf->scsi, level)))
702 		return;
703 
704 	spin_lock_irqsave(&dbf->scsi_lock, flags);
705 	memset(rec, 0, sizeof(*rec));
706 
707 	memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
708 	rec->id = ZFCP_DBF_SCSI_CMND;
709 	rec->scsi_result = ret; /* re-use field, int is 4 bytes and fits */
710 	rec->scsi_retries = ~0;
711 	rec->scsi_allowed = ~0;
712 	rec->fcp_rsp_info = ~0;
713 	rec->scsi_id = scsi_id;
714 	rec->scsi_lun = (u32)ZFCP_DBF_INVALID_LUN;
715 	rec->scsi_lun_64_hi = (u32)(ZFCP_DBF_INVALID_LUN >> 32);
716 	rec->host_scribble = ~0;
717 	memset(rec->scsi_opcode, 0xff, ZFCP_DBF_SCSI_OPCODE);
718 
719 	debug_event(dbf->scsi, level, rec, sizeof(*rec));
720 	spin_unlock_irqrestore(&dbf->scsi_lock, flags);
721 }
722 
zfcp_dbf_reg(const char * name,int size,int rec_size)723 static debug_info_t *zfcp_dbf_reg(const char *name, int size, int rec_size)
724 {
725 	struct debug_info *d;
726 
727 	d = debug_register(name, size, 1, rec_size);
728 	if (!d)
729 		return NULL;
730 
731 	debug_register_view(d, &debug_hex_ascii_view);
732 	debug_set_level(d, dbflevel);
733 
734 	return d;
735 }
736 
zfcp_dbf_unregister(struct zfcp_dbf * dbf)737 static void zfcp_dbf_unregister(struct zfcp_dbf *dbf)
738 {
739 	if (!dbf)
740 		return;
741 
742 	debug_unregister(dbf->scsi);
743 	debug_unregister(dbf->san);
744 	debug_unregister(dbf->hba);
745 	debug_unregister(dbf->pay);
746 	debug_unregister(dbf->rec);
747 	kfree(dbf);
748 }
749 
750 /**
751  * zfcp_adapter_debug_register - registers debug feature for an adapter
752  * @adapter: pointer to adapter for which debug features should be registered
753  * return: -ENOMEM on error, 0 otherwise
754  */
zfcp_dbf_adapter_register(struct zfcp_adapter * adapter)755 int zfcp_dbf_adapter_register(struct zfcp_adapter *adapter)
756 {
757 	char name[DEBUG_MAX_NAME_LEN];
758 	struct zfcp_dbf *dbf;
759 
760 	dbf = kzalloc(sizeof(struct zfcp_dbf), GFP_KERNEL);
761 	if (!dbf)
762 		return -ENOMEM;
763 
764 	spin_lock_init(&dbf->pay_lock);
765 	spin_lock_init(&dbf->hba_lock);
766 	spin_lock_init(&dbf->san_lock);
767 	spin_lock_init(&dbf->scsi_lock);
768 	spin_lock_init(&dbf->rec_lock);
769 
770 	/* debug feature area which records recovery activity */
771 	sprintf(name, "zfcp_%s_rec", dev_name(&adapter->ccw_device->dev));
772 	dbf->rec = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_rec));
773 	if (!dbf->rec)
774 		goto err_out;
775 
776 	/* debug feature area which records HBA (FSF and QDIO) conditions */
777 	sprintf(name, "zfcp_%s_hba", dev_name(&adapter->ccw_device->dev));
778 	dbf->hba = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_hba));
779 	if (!dbf->hba)
780 		goto err_out;
781 
782 	/* debug feature area which records payload info */
783 	sprintf(name, "zfcp_%s_pay", dev_name(&adapter->ccw_device->dev));
784 	dbf->pay = zfcp_dbf_reg(name, dbfsize * 2, sizeof(struct zfcp_dbf_pay));
785 	if (!dbf->pay)
786 		goto err_out;
787 
788 	/* debug feature area which records SAN command failures and recovery */
789 	sprintf(name, "zfcp_%s_san", dev_name(&adapter->ccw_device->dev));
790 	dbf->san = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_san));
791 	if (!dbf->san)
792 		goto err_out;
793 
794 	/* debug feature area which records SCSI command failures and recovery */
795 	sprintf(name, "zfcp_%s_scsi", dev_name(&adapter->ccw_device->dev));
796 	dbf->scsi = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_scsi));
797 	if (!dbf->scsi)
798 		goto err_out;
799 
800 	adapter->dbf = dbf;
801 
802 	return 0;
803 err_out:
804 	zfcp_dbf_unregister(dbf);
805 	return -ENOMEM;
806 }
807 
808 /**
809  * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter
810  * @adapter: pointer to adapter for which debug features should be unregistered
811  */
zfcp_dbf_adapter_unregister(struct zfcp_adapter * adapter)812 void zfcp_dbf_adapter_unregister(struct zfcp_adapter *adapter)
813 {
814 	struct zfcp_dbf *dbf = adapter->dbf;
815 
816 	adapter->dbf = NULL;
817 	zfcp_dbf_unregister(dbf);
818 }
819 
820