ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / s390 / block / dasd_erp.c
1 /*
2  * File...........: linux/drivers/s390/block/dasd.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *                  Horst Hummel <Horst.Hummel@de.ibm.com>
5  *                  Carsten Otte <Cotte@de.ibm.com>
6  *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
9  *
10  * $Revision: 1.10 $
11  */
12
13 #include <linux/config.h>
14 #include <linux/ctype.h>
15 #include <linux/init.h>
16
17 #include <asm/debug.h>
18 #include <asm/ebcdic.h>
19 #include <asm/uaccess.h>
20
21 /* This is ugly... */
22 #define PRINTK_HEADER "dasd_erp:"
23
24 #include "dasd_int.h"
25
26 struct dasd_ccw_req *
27 dasd_alloc_erp_request(char *magic, int cplength, int datasize,
28                        struct dasd_device * device)
29 {
30         unsigned long flags;
31         struct dasd_ccw_req *cqr;
32         char *data;
33         int size;
34
35         /* Sanity checks */
36         if ( magic == NULL || datasize > PAGE_SIZE ||
37              (cplength*sizeof(struct ccw1)) > PAGE_SIZE)
38                 BUG();
39         debug_text_event ( dasd_debug_area, 1, "ALLC");
40         debug_text_event ( dasd_debug_area, 1, magic);
41         debug_int_event ( dasd_debug_area, 1, cplength);
42         debug_int_event ( dasd_debug_area, 1, datasize);
43
44         size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
45         if (cplength > 0)
46                 size += cplength * sizeof(struct ccw1);
47         if (datasize > 0)
48                 size += datasize;
49         spin_lock_irqsave(&device->mem_lock, flags);
50         cqr = (struct dasd_ccw_req *)
51                 dasd_alloc_chunk(&device->erp_chunks, size);
52         spin_unlock_irqrestore(&device->mem_lock, flags);
53         if (cqr == NULL)
54                 return ERR_PTR(-ENOMEM);
55         memset(cqr, 0, sizeof(struct dasd_ccw_req));
56         data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
57         cqr->cpaddr = NULL;
58         if (cplength > 0) {
59                 cqr->cpaddr = (struct ccw1 *) data;
60                 data += cplength*sizeof(struct ccw1);
61                 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
62         }
63         cqr->data = NULL;
64         if (datasize > 0) {
65                 cqr->data = data;
66                 memset(cqr->data, 0, datasize);
67         }
68         strncpy((char *) &cqr->magic, magic, 4);
69         ASCEBC((char *) &cqr->magic, 4);
70         atomic_inc(&device->ref_count);
71         return cqr;
72 }
73
74 void
75 dasd_free_erp_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
76 {
77         unsigned long flags;
78
79         if (cqr->dstat != NULL)
80                 kfree(cqr->dstat);
81         debug_text_event(dasd_debug_area, 1, "FREE");
82         debug_int_event(dasd_debug_area, 1, (long) cqr);
83         spin_lock_irqsave(&device->mem_lock, flags);
84         dasd_free_chunk(&device->erp_chunks, cqr);
85         spin_unlock_irqrestore(&device->mem_lock, flags);
86         atomic_dec(&device->ref_count);
87 }
88
89
90 /*
91  * dasd_default_erp_action just retries the current cqr
92  */
93 struct dasd_ccw_req *
94 dasd_default_erp_action(struct dasd_ccw_req * cqr)
95 {
96         struct dasd_device *device;
97
98         device = cqr->device;
99
100         /* just retry - there is nothing to save ... I got no sense data.... */
101         if (cqr->retries > 0) {
102                 DEV_MESSAGE (KERN_DEBUG, device, 
103                              "default ERP called (%i retries left)",
104                              cqr->retries);
105                 cqr->status = DASD_CQR_QUEUED;
106         } else {
107                 DEV_MESSAGE (KERN_WARNING, device, "%s",
108                              "default ERP called (NO retry left)");
109                 
110                 cqr->status = DASD_CQR_FAILED;
111                 cqr->stopclk = get_clock ();
112         }
113         return cqr;
114 }                               /* end dasd_default_erp_action */
115
116 /*
117  * DESCRIPTION
118  *   Frees all ERPs of the current ERP Chain and set the status
119  *   of the original CQR either to DASD_CQR_DONE if ERP was successful
120  *   or to DASD_CQR_FAILED if ERP was NOT successful.
121  *   NOTE: This function is only called if no discipline postaction
122  *         is available
123  *
124  * PARAMETER
125  *   erp                current erp_head
126  *
127  * RETURN VALUES
128  *   cqr                pointer to the original CQR
129  */
130 struct dasd_ccw_req *
131 dasd_default_erp_postaction(struct dasd_ccw_req * cqr)
132 {
133         struct dasd_device *device;
134         int success;
135
136         if (cqr->refers == NULL || cqr->function == NULL)
137                 BUG();
138
139         device = cqr->device;
140         success = cqr->status == DASD_CQR_DONE;
141
142         /* free all ERPs - but NOT the original cqr */
143         while (cqr->refers != NULL) {
144                 struct dasd_ccw_req *refers;
145
146                 refers = cqr->refers;
147                 /* remove the request from the device queue */
148                 list_del(&cqr->list);
149                 /* free the finished erp request */
150                 dasd_free_erp_request(cqr, device);
151                 cqr = refers;
152         }
153
154         /* set corresponding status to original cqr */
155         if (success)
156                 cqr->status = DASD_CQR_DONE;
157         else {
158                 cqr->status = DASD_CQR_FAILED;
159                 cqr->stopclk = get_clock();
160         }
161
162         return cqr;
163
164 }                               /* end default_erp_postaction */
165
166 /*
167  * Print the hex dump of the memory used by a request. This includes
168  * all error recovery ccws that have been chained in from of the 
169  * real request.
170  */
171 static inline void
172 hex_dump_memory(struct dasd_device *device, void *data, int len)
173 {
174         int *pint;
175
176         pint = (int *) data;
177         while (len > 0) {
178                 DEV_MESSAGE(KERN_ERR, device, "%p: %08x %08x %08x %08x",
179                             pint, pint[0], pint[1], pint[2], pint[3]);
180                 pint += 4;
181                 len -= 16;
182         }
183 }
184
185 void
186 dasd_log_sense(struct dasd_ccw_req *cqr, struct irb *irb)
187 {
188         struct dasd_device *device;
189
190         device = cqr->device;
191         /* dump sense data */
192         if (device->discipline && device->discipline->dump_sense)
193                 device->discipline->dump_sense(device, cqr, irb);
194 }
195
196 void
197 dasd_log_ccw(struct dasd_ccw_req * cqr, int caller, __u32 cpa)
198 {
199         struct dasd_device *device;
200         struct dasd_ccw_req *lcqr;
201         struct ccw1 *ccw;
202         int cplength;
203
204         device = cqr->device;
205         /* log the channel program */
206         for (lcqr = cqr; lcqr != NULL; lcqr = lcqr->refers) {
207                 DEV_MESSAGE(KERN_ERR, device,
208                             "(%s) ERP chain report for req: %p",
209                             caller == 0 ? "EXAMINE" : "ACTION", lcqr);
210                 hex_dump_memory(device, lcqr, sizeof(struct dasd_ccw_req));
211
212                 cplength = 1;
213                 ccw = lcqr->cpaddr;
214                 while (ccw++->flags & (CCW_FLAG_DC | CCW_FLAG_CC))
215                         cplength++;
216
217                 if (cplength > 40) {    /* log only parts of the CP */
218                         DEV_MESSAGE(KERN_ERR, device, "%s",
219                                     "Start of channel program:");
220                         hex_dump_memory(device, lcqr->cpaddr,
221                                         40*sizeof(struct ccw1));
222
223                         DEV_MESSAGE(KERN_ERR, device, "%s",
224                                     "End of channel program:");
225                         hex_dump_memory(device, lcqr->cpaddr + cplength - 10,
226                                         10*sizeof(struct ccw1));
227                 } else {        /* log the whole CP */
228                         DEV_MESSAGE(KERN_ERR, device, "%s",
229                                     "Channel program (complete):");
230                         hex_dump_memory(device, lcqr->cpaddr,
231                                         cplength*sizeof(struct ccw1));
232                 }
233
234                 if (lcqr != cqr)
235                         continue;
236
237                 /*
238                  * Log bytes arround failed CCW but only if we did
239                  * not log the whole CP of the CCW is outside the
240                  * logged CP. 
241                  */
242                 if (cplength > 40 ||
243                     ((addr_t) cpa < (addr_t) lcqr->cpaddr &&
244                      (addr_t) cpa > (addr_t) (lcqr->cpaddr + cplength + 4))) {
245                         
246                         DEV_MESSAGE(KERN_ERR, device,
247                                     "Failed CCW (%p) (area):",
248                                     (void *) (long) cpa);
249                         hex_dump_memory(device, cqr->cpaddr - 10,
250                                         20*sizeof(struct ccw1));
251                 }
252         }
253
254 }                               /* end log_erp_chain */
255
256 EXPORT_SYMBOL(dasd_default_erp_action);
257 EXPORT_SYMBOL(dasd_default_erp_postaction);
258 EXPORT_SYMBOL(dasd_alloc_erp_request);
259 EXPORT_SYMBOL(dasd_free_erp_request);
260 EXPORT_SYMBOL(dasd_log_sense);
261 EXPORT_SYMBOL(dasd_log_ccw);