patch-2_6_7-vs1_9_1_12
[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.11 $
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         set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
71         dasd_get_device(device);
72         return cqr;
73 }
74
75 void
76 dasd_free_erp_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
77 {
78         unsigned long flags;
79
80         if (cqr->dstat != NULL)
81                 kfree(cqr->dstat);
82         debug_text_event(dasd_debug_area, 1, "FREE");
83         debug_int_event(dasd_debug_area, 1, (long) cqr);
84         spin_lock_irqsave(&device->mem_lock, flags);
85         dasd_free_chunk(&device->erp_chunks, cqr);
86         spin_unlock_irqrestore(&device->mem_lock, flags);
87         atomic_dec(&device->ref_count);
88 }
89
90
91 /*
92  * dasd_default_erp_action just retries the current cqr
93  */
94 struct dasd_ccw_req *
95 dasd_default_erp_action(struct dasd_ccw_req * cqr)
96 {
97         struct dasd_device *device;
98
99         device = cqr->device;
100
101         /* just retry - there is nothing to save ... I got no sense data.... */
102         if (cqr->retries > 0) {
103                 DEV_MESSAGE (KERN_DEBUG, device, 
104                              "default ERP called (%i retries left)",
105                              cqr->retries);
106                 cqr->status = DASD_CQR_QUEUED;
107         } else {
108                 DEV_MESSAGE (KERN_WARNING, device, "%s",
109                              "default ERP called (NO retry left)");
110                 
111                 cqr->status = DASD_CQR_FAILED;
112                 cqr->stopclk = get_clock ();
113         }
114         return cqr;
115 }                               /* end dasd_default_erp_action */
116
117 /*
118  * DESCRIPTION
119  *   Frees all ERPs of the current ERP Chain and set the status
120  *   of the original CQR either to DASD_CQR_DONE if ERP was successful
121  *   or to DASD_CQR_FAILED if ERP was NOT successful.
122  *   NOTE: This function is only called if no discipline postaction
123  *         is available
124  *
125  * PARAMETER
126  *   erp                current erp_head
127  *
128  * RETURN VALUES
129  *   cqr                pointer to the original CQR
130  */
131 struct dasd_ccw_req *
132 dasd_default_erp_postaction(struct dasd_ccw_req * cqr)
133 {
134         struct dasd_device *device;
135         int success;
136
137         if (cqr->refers == NULL || cqr->function == NULL)
138                 BUG();
139
140         device = cqr->device;
141         success = cqr->status == DASD_CQR_DONE;
142
143         /* free all ERPs - but NOT the original cqr */
144         while (cqr->refers != NULL) {
145                 struct dasd_ccw_req *refers;
146
147                 refers = cqr->refers;
148                 /* remove the request from the device queue */
149                 list_del(&cqr->list);
150                 /* free the finished erp request */
151                 dasd_free_erp_request(cqr, device);
152                 cqr = refers;
153         }
154
155         /* set corresponding status to original cqr */
156         if (success)
157                 cqr->status = DASD_CQR_DONE;
158         else {
159                 cqr->status = DASD_CQR_FAILED;
160                 cqr->stopclk = get_clock();
161         }
162
163         return cqr;
164
165 }                               /* end default_erp_postaction */
166
167 /*
168  * Print the hex dump of the memory used by a request. This includes
169  * all error recovery ccws that have been chained in from of the 
170  * real request.
171  */
172 static inline void
173 hex_dump_memory(struct dasd_device *device, void *data, int len)
174 {
175         int *pint;
176
177         pint = (int *) data;
178         while (len > 0) {
179                 DEV_MESSAGE(KERN_ERR, device, "%p: %08x %08x %08x %08x",
180                             pint, pint[0], pint[1], pint[2], pint[3]);
181                 pint += 4;
182                 len -= 16;
183         }
184 }
185
186 void
187 dasd_log_sense(struct dasd_ccw_req *cqr, struct irb *irb)
188 {
189         struct dasd_device *device;
190
191         device = cqr->device;
192         /* dump sense data */
193         if (device->discipline && device->discipline->dump_sense)
194                 device->discipline->dump_sense(device, cqr, irb);
195 }
196
197 void
198 dasd_log_ccw(struct dasd_ccw_req * cqr, int caller, __u32 cpa)
199 {
200         struct dasd_device *device;
201         struct dasd_ccw_req *lcqr;
202         struct ccw1 *ccw;
203         int cplength;
204
205         device = cqr->device;
206         /* log the channel program */
207         for (lcqr = cqr; lcqr != NULL; lcqr = lcqr->refers) {
208                 DEV_MESSAGE(KERN_ERR, device,
209                             "(%s) ERP chain report for req: %p",
210                             caller == 0 ? "EXAMINE" : "ACTION", lcqr);
211                 hex_dump_memory(device, lcqr, sizeof(struct dasd_ccw_req));
212
213                 cplength = 1;
214                 ccw = lcqr->cpaddr;
215                 while (ccw++->flags & (CCW_FLAG_DC | CCW_FLAG_CC))
216                         cplength++;
217
218                 if (cplength > 40) {    /* log only parts of the CP */
219                         DEV_MESSAGE(KERN_ERR, device, "%s",
220                                     "Start of channel program:");
221                         hex_dump_memory(device, lcqr->cpaddr,
222                                         40*sizeof(struct ccw1));
223
224                         DEV_MESSAGE(KERN_ERR, device, "%s",
225                                     "End of channel program:");
226                         hex_dump_memory(device, lcqr->cpaddr + cplength - 10,
227                                         10*sizeof(struct ccw1));
228                 } else {        /* log the whole CP */
229                         DEV_MESSAGE(KERN_ERR, device, "%s",
230                                     "Channel program (complete):");
231                         hex_dump_memory(device, lcqr->cpaddr,
232                                         cplength*sizeof(struct ccw1));
233                 }
234
235                 if (lcqr != cqr)
236                         continue;
237
238                 /*
239                  * Log bytes arround failed CCW but only if we did
240                  * not log the whole CP of the CCW is outside the
241                  * logged CP. 
242                  */
243                 if (cplength > 40 ||
244                     ((addr_t) cpa < (addr_t) lcqr->cpaddr &&
245                      (addr_t) cpa > (addr_t) (lcqr->cpaddr + cplength + 4))) {
246                         
247                         DEV_MESSAGE(KERN_ERR, device,
248                                     "Failed CCW (%p) (area):",
249                                     (void *) (long) cpa);
250                         hex_dump_memory(device, cqr->cpaddr - 10,
251                                         20*sizeof(struct ccw1));
252                 }
253         }
254
255 }                               /* end log_erp_chain */
256
257 EXPORT_SYMBOL(dasd_default_erp_action);
258 EXPORT_SYMBOL(dasd_default_erp_postaction);
259 EXPORT_SYMBOL(dasd_alloc_erp_request);
260 EXPORT_SYMBOL(dasd_free_erp_request);
261 EXPORT_SYMBOL(dasd_log_sense);
262 EXPORT_SYMBOL(dasd_log_ccw);