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