zfcp: restore: Dont use 0 to indicate invalid LUN in rec trace
[pandora-kernel.git] / drivers / s390 / scsi / zfcp_dbf.c
1 /*
2  * zfcp device driver
3  *
4  * Debug traces for zfcp.
5  *
6  * Copyright IBM Corp. 2002, 2015
7  */
8
9 #define KMSG_COMPONENT "zfcp"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/module.h>
13 #include <linux/ctype.h>
14 #include <linux/slab.h>
15 #include <asm/debug.h>
16 #include "zfcp_dbf.h"
17 #include "zfcp_ext.h"
18 #include "zfcp_fc.h"
19
20 static u32 dbfsize = 4;
21
22 module_param(dbfsize, uint, 0400);
23 MODULE_PARM_DESC(dbfsize,
24                  "number of pages for each debug feature area (default 4)");
25
26 static inline unsigned int zfcp_dbf_plen(unsigned int offset)
27 {
28         return sizeof(struct zfcp_dbf_pay) + offset - ZFCP_DBF_PAY_MAX_REC;
29 }
30
31 static inline
32 void zfcp_dbf_pl_write(struct zfcp_dbf *dbf, void *data, u16 length, char *area,
33                        u64 req_id)
34 {
35         struct zfcp_dbf_pay *pl = &dbf->pay_buf;
36         u16 offset = 0, rec_length;
37
38         spin_lock(&dbf->pay_lock);
39         memset(pl, 0, sizeof(*pl));
40         pl->fsf_req_id = req_id;
41         memcpy(pl->area, area, ZFCP_DBF_TAG_LEN);
42
43         while (offset < length) {
44                 rec_length = min((u16) ZFCP_DBF_PAY_MAX_REC,
45                                  (u16) (length - offset));
46                 memcpy(pl->data, data + offset, rec_length);
47                 debug_event(dbf->pay, 1, pl, zfcp_dbf_plen(rec_length));
48
49                 offset += rec_length;
50                 pl->counter++;
51         }
52
53         spin_unlock(&dbf->pay_lock);
54 }
55
56 /**
57  * zfcp_dbf_hba_fsf_res - trace event for fsf responses
58  * @tag: tag indicating which kind of unsolicited status has been received
59  * @req: request for which a response was received
60  */
61 void zfcp_dbf_hba_fsf_res(char *tag, int level, struct zfcp_fsf_req *req)
62 {
63         struct zfcp_dbf *dbf = req->adapter->dbf;
64         struct fsf_qtcb_prefix *q_pref = &req->qtcb->prefix;
65         struct fsf_qtcb_header *q_head = &req->qtcb->header;
66         struct zfcp_dbf_hba *rec = &dbf->hba_buf;
67         unsigned long flags;
68
69         spin_lock_irqsave(&dbf->hba_lock, flags);
70         memset(rec, 0, sizeof(*rec));
71
72         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
73         rec->id = ZFCP_DBF_HBA_RES;
74         rec->fsf_req_id = req->req_id;
75         rec->fsf_req_status = req->status;
76         rec->fsf_cmd = req->fsf_command;
77         rec->fsf_seq_no = req->seq_no;
78         rec->u.res.req_issued = req->issued;
79         rec->u.res.prot_status = q_pref->prot_status;
80         rec->u.res.fsf_status = q_head->fsf_status;
81
82         memcpy(rec->u.res.prot_status_qual, &q_pref->prot_status_qual,
83                FSF_PROT_STATUS_QUAL_SIZE);
84         memcpy(rec->u.res.fsf_status_qual, &q_head->fsf_status_qual,
85                FSF_STATUS_QUALIFIER_SIZE);
86
87         if (req->fsf_command != FSF_QTCB_FCP_CMND) {
88                 rec->pl_len = q_head->log_length;
89                 zfcp_dbf_pl_write(dbf, (char *)q_pref + q_head->log_start,
90                                   rec->pl_len, "fsf_res", req->req_id);
91         }
92
93         debug_event(dbf->hba, level, rec, sizeof(*rec));
94         spin_unlock_irqrestore(&dbf->hba_lock, flags);
95 }
96
97 /**
98  * zfcp_dbf_hba_fsf_uss - trace event for an unsolicited status buffer
99  * @tag: tag indicating which kind of unsolicited status has been received
100  * @req: request providing the unsolicited status
101  */
102 void zfcp_dbf_hba_fsf_uss(char *tag, struct zfcp_fsf_req *req)
103 {
104         struct zfcp_dbf *dbf = req->adapter->dbf;
105         struct fsf_status_read_buffer *srb = req->data;
106         struct zfcp_dbf_hba *rec = &dbf->hba_buf;
107         unsigned long flags;
108
109         spin_lock_irqsave(&dbf->hba_lock, flags);
110         memset(rec, 0, sizeof(*rec));
111
112         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
113         rec->id = ZFCP_DBF_HBA_USS;
114         rec->fsf_req_id = req->req_id;
115         rec->fsf_req_status = req->status;
116         rec->fsf_cmd = req->fsf_command;
117
118         if (!srb)
119                 goto log;
120
121         rec->u.uss.status_type = srb->status_type;
122         rec->u.uss.status_subtype = srb->status_subtype;
123         rec->u.uss.d_id = ntoh24(srb->d_id);
124         rec->u.uss.lun = srb->fcp_lun;
125         memcpy(&rec->u.uss.queue_designator, &srb->queue_designator,
126                sizeof(rec->u.uss.queue_designator));
127
128         /* status read buffer payload length */
129         rec->pl_len = (!srb->length) ? 0 : srb->length -
130                         offsetof(struct fsf_status_read_buffer, payload);
131
132         if (rec->pl_len)
133                 zfcp_dbf_pl_write(dbf, srb->payload.data, rec->pl_len,
134                                   "fsf_uss", req->req_id);
135 log:
136         debug_event(dbf->hba, 2, rec, sizeof(*rec));
137         spin_unlock_irqrestore(&dbf->hba_lock, flags);
138 }
139
140 /**
141  * zfcp_dbf_hba_bit_err - trace event for bit error conditions
142  * @tag: tag indicating which kind of unsolicited status has been received
143  * @req: request which caused the bit_error condition
144  */
145 void zfcp_dbf_hba_bit_err(char *tag, struct zfcp_fsf_req *req)
146 {
147         struct zfcp_dbf *dbf = req->adapter->dbf;
148         struct zfcp_dbf_hba *rec = &dbf->hba_buf;
149         struct fsf_status_read_buffer *sr_buf = req->data;
150         unsigned long flags;
151
152         spin_lock_irqsave(&dbf->hba_lock, flags);
153         memset(rec, 0, sizeof(*rec));
154
155         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
156         rec->id = ZFCP_DBF_HBA_BIT;
157         rec->fsf_req_id = req->req_id;
158         rec->fsf_req_status = req->status;
159         rec->fsf_cmd = req->fsf_command;
160         memcpy(&rec->u.be, &sr_buf->payload.bit_error,
161                sizeof(struct fsf_bit_error_payload));
162
163         debug_event(dbf->hba, 1, rec, sizeof(*rec));
164         spin_unlock_irqrestore(&dbf->hba_lock, flags);
165 }
166
167 /**
168  * zfcp_dbf_hba_def_err - trace event for deferred error messages
169  * @adapter: pointer to struct zfcp_adapter
170  * @req_id: request id which caused the deferred error message
171  * @scount: number of sbals incl. the signaling sbal
172  * @pl: array of all involved sbals
173  */
174 void zfcp_dbf_hba_def_err(struct zfcp_adapter *adapter, u64 req_id, u16 scount,
175                           void **pl)
176 {
177         struct zfcp_dbf *dbf = adapter->dbf;
178         struct zfcp_dbf_pay *payload = &dbf->pay_buf;
179         unsigned long flags;
180         u16 length;
181
182         if (!pl)
183                 return;
184
185         spin_lock_irqsave(&dbf->pay_lock, flags);
186         memset(payload, 0, sizeof(*payload));
187
188         memcpy(payload->area, "def_err", 7);
189         payload->fsf_req_id = req_id;
190         payload->counter = 0;
191         length = min((u16)sizeof(struct qdio_buffer),
192                      (u16)ZFCP_DBF_PAY_MAX_REC);
193
194         while (payload->counter < scount && (char *)pl[payload->counter]) {
195                 memcpy(payload->data, (char *)pl[payload->counter], length);
196                 debug_event(dbf->pay, 1, payload, zfcp_dbf_plen(length));
197                 payload->counter++;
198         }
199
200         spin_unlock_irqrestore(&dbf->pay_lock, flags);
201 }
202
203 /**
204  * zfcp_dbf_hba_basic - trace event for basic adapter events
205  * @adapter: pointer to struct zfcp_adapter
206  */
207 void zfcp_dbf_hba_basic(char *tag, struct zfcp_adapter *adapter)
208 {
209         struct zfcp_dbf *dbf = adapter->dbf;
210         struct zfcp_dbf_hba *rec = &dbf->hba_buf;
211         unsigned long flags;
212
213         spin_lock_irqsave(&dbf->hba_lock, flags);
214         memset(rec, 0, sizeof(*rec));
215
216         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
217         rec->id = ZFCP_DBF_HBA_BASIC;
218
219         debug_event(dbf->hba, 1, rec, sizeof(*rec));
220         spin_unlock_irqrestore(&dbf->hba_lock, flags);
221 }
222
223 static void zfcp_dbf_set_common(struct zfcp_dbf_rec *rec,
224                                 struct zfcp_adapter *adapter,
225                                 struct zfcp_port *port,
226                                 struct scsi_device *sdev)
227 {
228         rec->adapter_status = atomic_read(&adapter->status);
229         if (port) {
230                 rec->port_status = atomic_read(&port->status);
231                 rec->wwpn = port->wwpn;
232                 rec->d_id = port->d_id;
233         }
234         if (sdev) {
235                 rec->lun_status = atomic_read(&sdev_to_zfcp(sdev)->status);
236                 rec->lun = zfcp_scsi_dev_lun(sdev);
237         } else
238                 rec->lun = ZFCP_DBF_INVALID_LUN;
239 }
240
241 /**
242  * zfcp_dbf_rec_trig - trace event related to triggered recovery
243  * @tag: identifier for event
244  * @adapter: adapter on which the erp_action should run
245  * @port: remote port involved in the erp_action
246  * @sdev: scsi device involved in the erp_action
247  * @want: wanted erp_action
248  * @need: required erp_action
249  *
250  * The adapter->erp_lock has to be held.
251  */
252 void zfcp_dbf_rec_trig(char *tag, struct zfcp_adapter *adapter,
253                        struct zfcp_port *port, struct scsi_device *sdev,
254                        u8 want, u8 need)
255 {
256         struct zfcp_dbf *dbf = adapter->dbf;
257         struct zfcp_dbf_rec *rec = &dbf->rec_buf;
258         struct list_head *entry;
259         unsigned long flags;
260
261         spin_lock_irqsave(&dbf->rec_lock, flags);
262         memset(rec, 0, sizeof(*rec));
263
264         rec->id = ZFCP_DBF_REC_TRIG;
265         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
266         zfcp_dbf_set_common(rec, adapter, port, sdev);
267
268         list_for_each(entry, &adapter->erp_ready_head)
269                 rec->u.trig.ready++;
270
271         list_for_each(entry, &adapter->erp_running_head)
272                 rec->u.trig.running++;
273
274         rec->u.trig.want = want;
275         rec->u.trig.need = need;
276
277         debug_event(dbf->rec, 1, rec, sizeof(*rec));
278         spin_unlock_irqrestore(&dbf->rec_lock, flags);
279 }
280
281
282 /**
283  * zfcp_dbf_rec_run - trace event related to running recovery
284  * @tag: identifier for event
285  * @erp: erp_action running
286  */
287 void zfcp_dbf_rec_run(char *tag, struct zfcp_erp_action *erp)
288 {
289         struct zfcp_dbf *dbf = erp->adapter->dbf;
290         struct zfcp_dbf_rec *rec = &dbf->rec_buf;
291         unsigned long flags;
292
293         spin_lock_irqsave(&dbf->rec_lock, flags);
294         memset(rec, 0, sizeof(*rec));
295
296         rec->id = ZFCP_DBF_REC_RUN;
297         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
298         zfcp_dbf_set_common(rec, erp->adapter, erp->port, erp->sdev);
299
300         rec->u.run.fsf_req_id = erp->fsf_req_id;
301         rec->u.run.rec_status = erp->status;
302         rec->u.run.rec_step = erp->step;
303         rec->u.run.rec_action = erp->action;
304
305         if (erp->sdev)
306                 rec->u.run.rec_count =
307                         atomic_read(&sdev_to_zfcp(erp->sdev)->erp_counter);
308         else if (erp->port)
309                 rec->u.run.rec_count = atomic_read(&erp->port->erp_counter);
310         else
311                 rec->u.run.rec_count = atomic_read(&erp->adapter->erp_counter);
312
313         debug_event(dbf->rec, 1, rec, sizeof(*rec));
314         spin_unlock_irqrestore(&dbf->rec_lock, flags);
315 }
316
317 static inline
318 void zfcp_dbf_san(char *tag, struct zfcp_dbf *dbf, void *data, u8 id, u16 len,
319                   u64 req_id, u32 d_id)
320 {
321         struct zfcp_dbf_san *rec = &dbf->san_buf;
322         u16 rec_len;
323         unsigned long flags;
324
325         spin_lock_irqsave(&dbf->san_lock, flags);
326         memset(rec, 0, sizeof(*rec));
327
328         rec->id = id;
329         rec->fsf_req_id = req_id;
330         rec->d_id = d_id;
331         rec_len = min(len, (u16)ZFCP_DBF_SAN_MAX_PAYLOAD);
332         memcpy(rec->payload, data, rec_len);
333         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
334
335         debug_event(dbf->san, 1, rec, sizeof(*rec));
336         spin_unlock_irqrestore(&dbf->san_lock, flags);
337 }
338
339 /**
340  * zfcp_dbf_san_req - trace event for issued SAN request
341  * @tag: indentifier for event
342  * @fsf_req: request containing issued CT data
343  * d_id: destination ID
344  */
345 void zfcp_dbf_san_req(char *tag, struct zfcp_fsf_req *fsf, u32 d_id)
346 {
347         struct zfcp_dbf *dbf = fsf->adapter->dbf;
348         struct zfcp_fsf_ct_els *ct_els = fsf->data;
349         u16 length;
350
351         length = (u16)(ct_els->req->length + FC_CT_HDR_LEN);
352         zfcp_dbf_san(tag, dbf, sg_virt(ct_els->req), ZFCP_DBF_SAN_REQ, length,
353                      fsf->req_id, d_id);
354 }
355
356 /**
357  * zfcp_dbf_san_res - trace event for received SAN request
358  * @tag: indentifier for event
359  * @fsf_req: request containing issued CT data
360  */
361 void zfcp_dbf_san_res(char *tag, struct zfcp_fsf_req *fsf)
362 {
363         struct zfcp_dbf *dbf = fsf->adapter->dbf;
364         struct zfcp_fsf_ct_els *ct_els = fsf->data;
365         u16 length;
366
367         length = (u16)(ct_els->resp->length + FC_CT_HDR_LEN);
368         zfcp_dbf_san(tag, dbf, sg_virt(ct_els->resp), ZFCP_DBF_SAN_RES, length,
369                      fsf->req_id, 0);
370 }
371
372 /**
373  * zfcp_dbf_san_in_els - trace event for incoming ELS
374  * @tag: indentifier for event
375  * @fsf_req: request containing issued CT data
376  */
377 void zfcp_dbf_san_in_els(char *tag, struct zfcp_fsf_req *fsf)
378 {
379         struct zfcp_dbf *dbf = fsf->adapter->dbf;
380         struct fsf_status_read_buffer *srb =
381                 (struct fsf_status_read_buffer *) fsf->data;
382         u16 length;
383
384         length = (u16)(srb->length -
385                         offsetof(struct fsf_status_read_buffer, payload));
386         zfcp_dbf_san(tag, dbf, srb->payload.data, ZFCP_DBF_SAN_ELS, length,
387                      fsf->req_id, ntoh24(srb->d_id));
388 }
389
390 /**
391  * zfcp_dbf_scsi - trace event for scsi commands
392  * @tag: identifier for event
393  * @sc: pointer to struct scsi_cmnd
394  * @fsf: pointer to struct zfcp_fsf_req
395  */
396 void zfcp_dbf_scsi(char *tag, int level, struct scsi_cmnd *sc,
397                    struct zfcp_fsf_req *fsf)
398 {
399         struct zfcp_adapter *adapter =
400                 (struct zfcp_adapter *) sc->device->host->hostdata[0];
401         struct zfcp_dbf *dbf = adapter->dbf;
402         struct zfcp_dbf_scsi *rec = &dbf->scsi_buf;
403         struct fcp_resp_with_ext *fcp_rsp;
404         struct fcp_resp_rsp_info *fcp_rsp_info;
405         unsigned long flags;
406
407         spin_lock_irqsave(&dbf->scsi_lock, flags);
408         memset(rec, 0, sizeof(*rec));
409
410         memcpy(rec->tag, tag, ZFCP_DBF_TAG_LEN);
411         rec->id = ZFCP_DBF_SCSI_CMND;
412         rec->scsi_result = sc->result;
413         rec->scsi_retries = sc->retries;
414         rec->scsi_allowed = sc->allowed;
415         rec->scsi_id = sc->device->id;
416         rec->scsi_lun = sc->device->lun;
417         rec->host_scribble = (unsigned long)sc->host_scribble;
418
419         memcpy(rec->scsi_opcode, sc->cmnd,
420                min((int)sc->cmd_len, ZFCP_DBF_SCSI_OPCODE));
421
422         if (fsf) {
423                 rec->fsf_req_id = fsf->req_id;
424                 fcp_rsp = (struct fcp_resp_with_ext *)
425                                 &(fsf->qtcb->bottom.io.fcp_rsp);
426                 memcpy(&rec->fcp_rsp, fcp_rsp, FCP_RESP_WITH_EXT);
427                 if (fcp_rsp->resp.fr_flags & FCP_RSP_LEN_VAL) {
428                         fcp_rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
429                         rec->fcp_rsp_info = fcp_rsp_info->rsp_code;
430                 }
431                 if (fcp_rsp->resp.fr_flags & FCP_SNS_LEN_VAL) {
432                         rec->pl_len = min((u16)SCSI_SENSE_BUFFERSIZE,
433                                           (u16)ZFCP_DBF_PAY_MAX_REC);
434                         zfcp_dbf_pl_write(dbf, sc->sense_buffer, rec->pl_len,
435                                           "fcp_sns", fsf->req_id);
436                 }
437         }
438
439         debug_event(dbf->scsi, level, rec, sizeof(*rec));
440         spin_unlock_irqrestore(&dbf->scsi_lock, flags);
441 }
442
443 static debug_info_t *zfcp_dbf_reg(const char *name, int size, int rec_size)
444 {
445         struct debug_info *d;
446
447         d = debug_register(name, size, 1, rec_size);
448         if (!d)
449                 return NULL;
450
451         debug_register_view(d, &debug_hex_ascii_view);
452         debug_set_level(d, 3);
453
454         return d;
455 }
456
457 static void zfcp_dbf_unregister(struct zfcp_dbf *dbf)
458 {
459         if (!dbf)
460                 return;
461
462         debug_unregister(dbf->scsi);
463         debug_unregister(dbf->san);
464         debug_unregister(dbf->hba);
465         debug_unregister(dbf->pay);
466         debug_unregister(dbf->rec);
467         kfree(dbf);
468 }
469
470 /**
471  * zfcp_adapter_debug_register - registers debug feature for an adapter
472  * @adapter: pointer to adapter for which debug features should be registered
473  * return: -ENOMEM on error, 0 otherwise
474  */
475 int zfcp_dbf_adapter_register(struct zfcp_adapter *adapter)
476 {
477         char name[DEBUG_MAX_NAME_LEN];
478         struct zfcp_dbf *dbf;
479
480         dbf = kzalloc(sizeof(struct zfcp_dbf), GFP_KERNEL);
481         if (!dbf)
482                 return -ENOMEM;
483
484         spin_lock_init(&dbf->pay_lock);
485         spin_lock_init(&dbf->hba_lock);
486         spin_lock_init(&dbf->san_lock);
487         spin_lock_init(&dbf->scsi_lock);
488         spin_lock_init(&dbf->rec_lock);
489
490         /* debug feature area which records recovery activity */
491         sprintf(name, "zfcp_%s_rec", dev_name(&adapter->ccw_device->dev));
492         dbf->rec = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_rec));
493         if (!dbf->rec)
494                 goto err_out;
495
496         /* debug feature area which records HBA (FSF and QDIO) conditions */
497         sprintf(name, "zfcp_%s_hba", dev_name(&adapter->ccw_device->dev));
498         dbf->hba = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_hba));
499         if (!dbf->hba)
500                 goto err_out;
501
502         /* debug feature area which records payload info */
503         sprintf(name, "zfcp_%s_pay", dev_name(&adapter->ccw_device->dev));
504         dbf->pay = zfcp_dbf_reg(name, dbfsize * 2, sizeof(struct zfcp_dbf_pay));
505         if (!dbf->pay)
506                 goto err_out;
507
508         /* debug feature area which records SAN command failures and recovery */
509         sprintf(name, "zfcp_%s_san", dev_name(&adapter->ccw_device->dev));
510         dbf->san = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_san));
511         if (!dbf->san)
512                 goto err_out;
513
514         /* debug feature area which records SCSI command failures and recovery */
515         sprintf(name, "zfcp_%s_scsi", dev_name(&adapter->ccw_device->dev));
516         dbf->scsi = zfcp_dbf_reg(name, dbfsize, sizeof(struct zfcp_dbf_scsi));
517         if (!dbf->scsi)
518                 goto err_out;
519
520         adapter->dbf = dbf;
521
522         return 0;
523 err_out:
524         zfcp_dbf_unregister(dbf);
525         return -ENOMEM;
526 }
527
528 /**
529  * zfcp_adapter_debug_unregister - unregisters debug feature for an adapter
530  * @adapter: pointer to adapter for which debug features should be unregistered
531  */
532 void zfcp_dbf_adapter_unregister(struct zfcp_adapter *adapter)
533 {
534         struct zfcp_dbf *dbf = adapter->dbf;
535
536         adapter->dbf = NULL;
537         zfcp_dbf_unregister(dbf);
538 }
539