blob: 492b68bcd7cc0e26be5cb44fc192f417c7402954 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kmod.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/ctype.h>
16#include <linux/major.h>
17#include <linux/slab.h>
18#include <linux/buffer_head.h>
Christoph Hellwiga885c8c2006-01-08 01:02:50 -080019#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/ccwdev.h>
22#include <asm/ebcdic.h>
23#include <asm/idals.h>
24#include <asm/todclk.h>
25
26/* This is ugly... */
27#define PRINTK_HEADER "dasd:"
28
29#include "dasd_int.h"
30/*
31 * SECTION: Constant definitions to be used within this file
32 */
33#define DASD_CHANQ_MAX_SIZE 4
34
35/*
36 * SECTION: exported variables of dasd.c
37 */
38debug_info_t *dasd_debug_area;
39struct dasd_discipline *dasd_diag_discipline_pointer;
40
41MODULE_AUTHOR("Holger Smolinski <Holger.Smolinski@de.ibm.com>");
42MODULE_DESCRIPTION("Linux on S/390 DASD device driver,"
43 " Copyright 2000 IBM Corporation");
44MODULE_SUPPORTED_DEVICE("dasd");
Linus Torvalds1da177e2005-04-16 15:20:36 -070045MODULE_LICENSE("GPL");
46
47/*
48 * SECTION: prototypes for static functions of dasd.c
49 */
50static int dasd_alloc_queue(struct dasd_device * device);
51static void dasd_setup_queue(struct dasd_device * device);
52static void dasd_free_queue(struct dasd_device * device);
53static void dasd_flush_request_queue(struct dasd_device *);
54static void dasd_int_handler(struct ccw_device *, unsigned long, struct irb *);
Horst Hummel8f617012006-08-30 14:33:33 +020055static int dasd_flush_ccw_queue(struct dasd_device *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static void dasd_tasklet(struct dasd_device *);
Al Viro4927b3f2006-12-06 19:18:20 +000057static void do_kick_device(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
60 * SECTION: Operations on the device structure.
61 */
62static wait_queue_head_t dasd_init_waitq;
Horst Hummel8f617012006-08-30 14:33:33 +020063static wait_queue_head_t dasd_flush_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/*
66 * Allocate memory for a new device structure.
67 */
68struct dasd_device *
69dasd_alloc_device(void)
70{
71 struct dasd_device *device;
72
Eric Sesterhenn88abaab2006-03-24 03:15:31 -080073 device = kzalloc(sizeof (struct dasd_device), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (device == NULL)
75 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /* open_count = 0 means device online but not in use */
77 atomic_set(&device->open_count, -1);
78
79 /* Get two pages for normal block device operations. */
80 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
81 if (device->ccw_mem == NULL) {
82 kfree(device);
83 return ERR_PTR(-ENOMEM);
84 }
85 /* Get one page for error recovery. */
86 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
87 if (device->erp_mem == NULL) {
88 free_pages((unsigned long) device->ccw_mem, 1);
89 kfree(device);
90 return ERR_PTR(-ENOMEM);
91 }
92
93 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
94 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
95 spin_lock_init(&device->mem_lock);
96 spin_lock_init(&device->request_queue_lock);
97 atomic_set (&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +020098 tasklet_init(&device->tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 (void (*)(unsigned long)) dasd_tasklet,
100 (unsigned long) device);
101 INIT_LIST_HEAD(&device->ccw_queue);
102 init_timer(&device->timer);
Al Viro4927b3f2006-12-06 19:18:20 +0000103 INIT_WORK(&device->kick_work, do_kick_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 device->state = DASD_STATE_NEW;
105 device->target = DASD_STATE_NEW;
106
107 return device;
108}
109
110/*
111 * Free memory of a device structure.
112 */
113void
114dasd_free_device(struct dasd_device *device)
115{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800116 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 free_page((unsigned long) device->erp_mem);
118 free_pages((unsigned long) device->ccw_mem, 1);
119 kfree(device);
120}
121
122/*
123 * Make a new device known to the system.
124 */
Horst Hummel8f617012006-08-30 14:33:33 +0200125static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126dasd_state_new_to_known(struct dasd_device *device)
127{
128 int rc;
129
130 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200131 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * keep the reference count > 0.
133 */
134 dasd_get_device(device);
135
136 rc = dasd_alloc_queue(device);
137 if (rc) {
138 dasd_put_device(device);
139 return rc;
140 }
141
142 device->state = DASD_STATE_KNOWN;
143 return 0;
144}
145
146/*
147 * Let the system forget about a device.
148 */
Horst Hummel8f617012006-08-30 14:33:33 +0200149static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150dasd_state_known_to_new(struct dasd_device * device)
151{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800152 /* Disable extended error reporting for this device. */
153 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* Forget the discipline information. */
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800155 if (device->discipline)
156 module_put(device->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800158 if (device->base_discipline)
159 module_put(device->base_discipline->owner);
160 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 device->state = DASD_STATE_NEW;
162
163 dasd_free_queue(device);
164
165 /* Give up reference we took in dasd_state_new_to_known. */
166 dasd_put_device(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/*
171 * Request the irq line for the device.
172 */
Horst Hummel8f617012006-08-30 14:33:33 +0200173static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174dasd_state_known_to_basic(struct dasd_device * device)
175{
176 int rc;
177
178 /* Allocate and register gendisk structure. */
179 rc = dasd_gendisk_alloc(device);
180 if (rc)
181 return rc;
182
183 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700184 device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 8 * sizeof (long));
186 debug_register_view(device->debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +0200187 debug_set_level(device->debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
189
190 device->state = DASD_STATE_BASIC;
191 return 0;
192}
193
194/*
195 * Release the irq line for the device. Terminate any running i/o.
196 */
Horst Hummel8f617012006-08-30 14:33:33 +0200197static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198dasd_state_basic_to_known(struct dasd_device * device)
199{
Horst Hummel8f617012006-08-30 14:33:33 +0200200 int rc;
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dasd_gendisk_free(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200203 rc = dasd_flush_ccw_queue(device, 1);
204 if (rc)
205 return rc;
Stefan Weinhuber867dcd02006-10-18 18:30:53 +0200206 dasd_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +0200207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
209 if (device->debug_area != NULL) {
210 debug_unregister(device->debug_area);
211 device->debug_area = NULL;
212 }
213 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
218 * Do the initial analysis. The do_analysis function may return
219 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
220 * until the discipline decides to continue the startup sequence
221 * by calling the function dasd_change_state. The eckd disciplines
222 * uses this to start a ccw that detects the format. The completion
223 * interrupt for this detection ccw uses the kernel event daemon to
224 * trigger the call to dasd_change_state. All this is done in the
225 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800226 * After the analysis ccw is done (do_analysis returned 0) the block
227 * device is setup.
228 * In case the analysis returns an error, the device setup is stopped
229 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Horst Hummel8f617012006-08-30 14:33:33 +0200231static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232dasd_state_basic_to_ready(struct dasd_device * device)
233{
234 int rc;
235
236 rc = 0;
237 if (device->discipline->do_analysis != NULL)
238 rc = device->discipline->do_analysis(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800239 if (rc) {
240 if (rc != -EAGAIN)
241 device->state = DASD_STATE_UNFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return rc;
Horst Hummel90f00942006-03-07 21:55:39 -0800243 }
244 /* make disk known with correct capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dasd_setup_queue(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800246 set_capacity(device->gdp, device->blocks << device->s2b_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 device->state = DASD_STATE_READY;
Horst Hummel90f00942006-03-07 21:55:39 -0800248 rc = dasd_scan_partitions(device);
249 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 device->state = DASD_STATE_BASIC;
Horst Hummel90f00942006-03-07 21:55:39 -0800251 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/*
255 * Remove device from block device layer. Destroy dirty buffers.
256 * Forget format information. Check if the target level is basic
257 * and if it is create fake disk for formatting.
258 */
Horst Hummel8f617012006-08-30 14:33:33 +0200259static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260dasd_state_ready_to_basic(struct dasd_device * device)
261{
Horst Hummel8f617012006-08-30 14:33:33 +0200262 int rc;
263
264 rc = dasd_flush_ccw_queue(device, 0);
265 if (rc)
266 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 dasd_destroy_partitions(device);
268 dasd_flush_request_queue(device);
269 device->blocks = 0;
270 device->bp_block = 0;
271 device->s2b_shift = 0;
272 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/*
Horst Hummel90f00942006-03-07 21:55:39 -0800277 * Back to basic.
278 */
Horst Hummel8f617012006-08-30 14:33:33 +0200279static int
Horst Hummel90f00942006-03-07 21:55:39 -0800280dasd_state_unfmt_to_basic(struct dasd_device * device)
281{
282 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200283 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800284}
285
286/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * Make the device online and schedule the bottom half to start
288 * the requeueing of requests from the linux request queue to the
289 * ccw queue.
290 */
Horst Hummel8f617012006-08-30 14:33:33 +0200291static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292dasd_state_ready_to_online(struct dasd_device * device)
293{
294 device->state = DASD_STATE_ONLINE;
295 dasd_schedule_bh(device);
296 return 0;
297}
298
299/*
300 * Stop the requeueing of requests again.
301 */
Horst Hummel8f617012006-08-30 14:33:33 +0200302static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303dasd_state_online_to_ready(struct dasd_device * device)
304{
305 device->state = DASD_STATE_READY;
Horst Hummel8f617012006-08-30 14:33:33 +0200306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/*
310 * Device startup state changes.
311 */
Horst Hummel8f617012006-08-30 14:33:33 +0200312static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313dasd_increase_state(struct dasd_device *device)
314{
315 int rc;
316
317 rc = 0;
318 if (device->state == DASD_STATE_NEW &&
319 device->target >= DASD_STATE_KNOWN)
320 rc = dasd_state_new_to_known(device);
321
322 if (!rc &&
323 device->state == DASD_STATE_KNOWN &&
324 device->target >= DASD_STATE_BASIC)
325 rc = dasd_state_known_to_basic(device);
326
327 if (!rc &&
328 device->state == DASD_STATE_BASIC &&
329 device->target >= DASD_STATE_READY)
330 rc = dasd_state_basic_to_ready(device);
331
332 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700333 device->state == DASD_STATE_UNFMT &&
334 device->target > DASD_STATE_UNFMT)
335 rc = -EPERM;
336
337 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 device->state == DASD_STATE_READY &&
339 device->target >= DASD_STATE_ONLINE)
340 rc = dasd_state_ready_to_online(device);
341
342 return rc;
343}
344
345/*
346 * Device shutdown state changes.
347 */
Horst Hummel8f617012006-08-30 14:33:33 +0200348static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349dasd_decrease_state(struct dasd_device *device)
350{
Horst Hummel8f617012006-08-30 14:33:33 +0200351 int rc;
352
353 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (device->state == DASD_STATE_ONLINE &&
355 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200356 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200357
Horst Hummel8f617012006-08-30 14:33:33 +0200358 if (!rc &&
359 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200361 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800362
Horst Hummel8f617012006-08-30 14:33:33 +0200363 if (!rc &&
364 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800365 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200366 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800367
Horst Hummel8f617012006-08-30 14:33:33 +0200368 if (!rc &&
369 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200371 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200372
Horst Hummel8f617012006-08-30 14:33:33 +0200373 if (!rc &&
374 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200376 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Horst Hummel8f617012006-08-30 14:33:33 +0200378 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381/*
382 * This is the main startup/shutdown routine.
383 */
384static void
385dasd_change_state(struct dasd_device *device)
386{
387 int rc;
388
389 if (device->state == device->target)
390 /* Already where we want to go today... */
391 return;
392 if (device->state < device->target)
393 rc = dasd_increase_state(device);
394 else
395 rc = dasd_decrease_state(device);
396 if (rc && rc != -EAGAIN)
397 device->target = device->state;
398
399 if (device->state == device->target)
400 wake_up(&dasd_init_waitq);
401}
402
403/*
404 * Kick starter for devices that did not complete the startup/shutdown
405 * procedure or were sleeping because of a pending state.
406 * dasd_kick_device will schedule a call do do_kick_device to the kernel
407 * event daemon.
408 */
409static void
Al Viro4927b3f2006-12-06 19:18:20 +0000410do_kick_device(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Al Viro4927b3f2006-12-06 19:18:20 +0000412 struct dasd_device *device = container_of(work, struct dasd_device, kick_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 dasd_change_state(device);
414 dasd_schedule_bh(device);
415 dasd_put_device(device);
416}
417
418void
419dasd_kick_device(struct dasd_device *device)
420{
421 dasd_get_device(device);
422 /* queue call to dasd_kick_device to the kernel event daemon. */
423 schedule_work(&device->kick_work);
424}
425
426/*
427 * Set the target state for a device and starts the state change.
428 */
429void
430dasd_set_target_state(struct dasd_device *device, int target)
431{
432 /* If we are in probeonly mode stop at DASD_STATE_READY. */
433 if (dasd_probeonly && target > DASD_STATE_READY)
434 target = DASD_STATE_READY;
435 if (device->target != target) {
436 if (device->state == target)
437 wake_up(&dasd_init_waitq);
438 device->target = target;
439 }
440 if (device->state != device->target)
441 dasd_change_state(device);
442}
443
444/*
445 * Enable devices with device numbers in [from..to].
446 */
447static inline int
448_wait_for_device(struct dasd_device *device)
449{
450 return (device->state == device->target);
451}
452
453void
454dasd_enable_device(struct dasd_device *device)
455{
456 dasd_set_target_state(device, DASD_STATE_ONLINE);
457 if (device->state <= DASD_STATE_KNOWN)
458 /* No discipline for device found. */
459 dasd_set_target_state(device, DASD_STATE_NEW);
460 /* Now wait for the devices to come up. */
461 wait_event(dasd_init_waitq, _wait_for_device(device));
462}
463
464/*
465 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
466 */
467#ifdef CONFIG_DASD_PROFILE
468
469struct dasd_profile_info_t dasd_global_profile;
470unsigned int dasd_profile_level = DASD_PROFILE_OFF;
471
472/*
473 * Increments counter in global and local profiling structures.
474 */
475#define dasd_profile_counter(value, counter, device) \
476{ \
477 int index; \
478 for (index = 0; index < 31 && value >> (2+index); index++); \
479 dasd_global_profile.counter[index]++; \
480 device->profile.counter[index]++; \
481}
482
483/*
484 * Add profiling information for cqr before execution.
485 */
486static inline void
487dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
488 struct request *req)
489{
490 struct list_head *l;
491 unsigned int counter;
492
493 if (dasd_profile_level != DASD_PROFILE_ON)
494 return;
495
496 /* count the length of the chanq for statistics */
497 counter = 0;
498 list_for_each(l, &device->ccw_queue)
499 if (++counter >= 31)
500 break;
501 dasd_global_profile.dasd_io_nr_req[counter]++;
502 device->profile.dasd_io_nr_req[counter]++;
503}
504
505/*
506 * Add profiling information for cqr after execution.
507 */
508static inline void
509dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
510 struct request *req)
511{
512 long strtime, irqtime, endtime, tottime; /* in microseconds */
513 long tottimeps, sectors;
514
515 if (dasd_profile_level != DASD_PROFILE_ON)
516 return;
517
518 sectors = req->nr_sectors;
519 if (!cqr->buildclk || !cqr->startclk ||
520 !cqr->stopclk || !cqr->endclk ||
521 !sectors)
522 return;
523
524 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
525 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
526 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
527 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
528 tottimeps = tottime / sectors;
529
530 if (!dasd_global_profile.dasd_io_reqs)
531 memset(&dasd_global_profile, 0,
532 sizeof (struct dasd_profile_info_t));
533 dasd_global_profile.dasd_io_reqs++;
534 dasd_global_profile.dasd_io_sects += sectors;
535
536 if (!device->profile.dasd_io_reqs)
537 memset(&device->profile, 0,
538 sizeof (struct dasd_profile_info_t));
539 device->profile.dasd_io_reqs++;
540 device->profile.dasd_io_sects += sectors;
541
542 dasd_profile_counter(sectors, dasd_io_secs, device);
543 dasd_profile_counter(tottime, dasd_io_times, device);
544 dasd_profile_counter(tottimeps, dasd_io_timps, device);
545 dasd_profile_counter(strtime, dasd_io_time1, device);
546 dasd_profile_counter(irqtime, dasd_io_time2, device);
547 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
548 dasd_profile_counter(endtime, dasd_io_time3, device);
549}
550#else
551#define dasd_profile_start(device, cqr, req) do {} while (0)
552#define dasd_profile_end(device, cqr, req) do {} while (0)
553#endif /* CONFIG_DASD_PROFILE */
554
555/*
556 * Allocate memory for a channel program with 'cplength' channel
557 * command words and 'datasize' additional space. There are two
558 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
559 * memory and 2) dasd_smalloc_request uses the static ccw memory
560 * that gets allocated for each device.
561 */
562struct dasd_ccw_req *
563dasd_kmalloc_request(char *magic, int cplength, int datasize,
564 struct dasd_device * device)
565{
566 struct dasd_ccw_req *cqr;
567
568 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100569 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
570 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800572 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (cqr == NULL)
574 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 cqr->cpaddr = NULL;
576 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800577 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 GFP_ATOMIC | GFP_DMA);
579 if (cqr->cpaddr == NULL) {
580 kfree(cqr);
581 return ERR_PTR(-ENOMEM);
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 cqr->data = NULL;
585 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800586 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800588 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 kfree(cqr);
590 return ERR_PTR(-ENOMEM);
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 strncpy((char *) &cqr->magic, magic, 4);
594 ASCEBC((char *) &cqr->magic, 4);
595 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
596 dasd_get_device(device);
597 return cqr;
598}
599
600struct dasd_ccw_req *
601dasd_smalloc_request(char *magic, int cplength, int datasize,
602 struct dasd_device * device)
603{
604 unsigned long flags;
605 struct dasd_ccw_req *cqr;
606 char *data;
607 int size;
608
609 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100610 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
611 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
614 if (cplength > 0)
615 size += cplength * sizeof(struct ccw1);
616 if (datasize > 0)
617 size += datasize;
618 spin_lock_irqsave(&device->mem_lock, flags);
619 cqr = (struct dasd_ccw_req *)
620 dasd_alloc_chunk(&device->ccw_chunks, size);
621 spin_unlock_irqrestore(&device->mem_lock, flags);
622 if (cqr == NULL)
623 return ERR_PTR(-ENOMEM);
624 memset(cqr, 0, sizeof(struct dasd_ccw_req));
625 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
626 cqr->cpaddr = NULL;
627 if (cplength > 0) {
628 cqr->cpaddr = (struct ccw1 *) data;
629 data += cplength*sizeof(struct ccw1);
630 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
631 }
632 cqr->data = NULL;
633 if (datasize > 0) {
634 cqr->data = data;
635 memset(cqr->data, 0, datasize);
636 }
637 strncpy((char *) &cqr->magic, magic, 4);
638 ASCEBC((char *) &cqr->magic, 4);
639 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
640 dasd_get_device(device);
641 return cqr;
642}
643
644/*
645 * Free memory of a channel program. This function needs to free all the
646 * idal lists that might have been created by dasd_set_cda and the
647 * struct dasd_ccw_req itself.
648 */
649void
650dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
651{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800652#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct ccw1 *ccw;
654
655 /* Clear any idals used for the request. */
656 ccw = cqr->cpaddr;
657 do {
658 clear_normalized_cda(ccw);
659 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
660#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800661 kfree(cqr->cpaddr);
662 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 kfree(cqr);
664 dasd_put_device(device);
665}
666
667void
668dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
669{
670 unsigned long flags;
671
672 spin_lock_irqsave(&device->mem_lock, flags);
673 dasd_free_chunk(&device->ccw_chunks, cqr);
674 spin_unlock_irqrestore(&device->mem_lock, flags);
675 dasd_put_device(device);
676}
677
678/*
679 * Check discipline magic in cqr.
680 */
681static inline int
682dasd_check_cqr(struct dasd_ccw_req *cqr)
683{
684 struct dasd_device *device;
685
686 if (cqr == NULL)
687 return -EINVAL;
688 device = cqr->device;
689 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
690 DEV_MESSAGE(KERN_WARNING, device,
691 " dasd_ccw_req 0x%08x magic doesn't match"
692 " discipline 0x%08x",
693 cqr->magic,
694 *(unsigned int *) device->discipline->name);
695 return -EINVAL;
696 }
697 return 0;
698}
699
700/*
701 * Terminate the current i/o and set the request to clear_pending.
702 * Timer keeps device runnig.
703 * ccw_device_clear can fail if the i/o subsystem
704 * is in a bad mood.
705 */
706int
707dasd_term_IO(struct dasd_ccw_req * cqr)
708{
709 struct dasd_device *device;
710 int retries, rc;
711
712 /* Check the cqr */
713 rc = dasd_check_cqr(cqr);
714 if (rc)
715 return rc;
716 retries = 0;
717 device = (struct dasd_device *) cqr->device;
718 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
719 rc = ccw_device_clear(device->cdev, (long) cqr);
720 switch (rc) {
721 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800722 cqr->retries--;
723 cqr->status = DASD_CQR_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200725 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 DBF_DEV_EVENT(DBF_DEBUG, device,
727 "terminate cqr %p successful",
728 cqr);
729 break;
730 case -ENODEV:
731 DBF_DEV_EVENT(DBF_ERR, device, "%s",
732 "device gone, retry");
733 break;
734 case -EIO:
735 DBF_DEV_EVENT(DBF_ERR, device, "%s",
736 "I/O error, retry");
737 break;
738 case -EINVAL:
739 case -EBUSY:
740 DBF_DEV_EVENT(DBF_ERR, device, "%s",
741 "device busy, retry later");
742 break;
743 default:
744 DEV_MESSAGE(KERN_ERR, device,
745 "line %d unknown RC=%d, please "
746 "report to linux390@de.ibm.com",
747 __LINE__, rc);
748 BUG();
749 break;
750 }
751 retries++;
752 }
753 dasd_schedule_bh(device);
754 return rc;
755}
756
757/*
758 * Start the i/o. This start_IO can fail if the channel is really busy.
759 * In that case set up a timer to start the request later.
760 */
761int
762dasd_start_IO(struct dasd_ccw_req * cqr)
763{
764 struct dasd_device *device;
765 int rc;
766
767 /* Check the cqr */
768 rc = dasd_check_cqr(cqr);
769 if (rc)
770 return rc;
771 device = (struct dasd_device *) cqr->device;
772 if (cqr->retries < 0) {
773 DEV_MESSAGE(KERN_DEBUG, device,
774 "start_IO: request %p (%02x/%i) - no retry left.",
775 cqr, cqr->status, cqr->retries);
776 cqr->status = DASD_CQR_FAILED;
777 return -EIO;
778 }
779 cqr->startclk = get_clock();
780 cqr->starttime = jiffies;
781 cqr->retries--;
782 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
783 cqr->lpm, 0);
784 switch (rc) {
785 case 0:
786 cqr->status = DASD_CQR_IN_IO;
787 DBF_DEV_EVENT(DBF_DEBUG, device,
788 "start_IO: request %p started successful",
789 cqr);
790 break;
791 case -EBUSY:
792 DBF_DEV_EVENT(DBF_ERR, device, "%s",
793 "start_IO: device busy, retry later");
794 break;
795 case -ETIMEDOUT:
796 DBF_DEV_EVENT(DBF_ERR, device, "%s",
797 "start_IO: request timeout, retry later");
798 break;
799 case -EACCES:
800 /* -EACCES indicates that the request used only a
801 * subset of the available pathes and all these
802 * pathes are gone.
803 * Do a retry with all available pathes.
804 */
805 cqr->lpm = LPM_ANYPATH;
806 DBF_DEV_EVENT(DBF_ERR, device, "%s",
807 "start_IO: selected pathes gone,"
808 " retry on all pathes");
809 break;
810 case -ENODEV:
811 case -EIO:
812 DBF_DEV_EVENT(DBF_ERR, device, "%s",
813 "start_IO: device gone, retry");
814 break;
815 default:
816 DEV_MESSAGE(KERN_ERR, device,
817 "line %d unknown RC=%d, please report"
818 " to linux390@de.ibm.com", __LINE__, rc);
819 BUG();
820 break;
821 }
822 return rc;
823}
824
825/*
826 * Timeout function for dasd devices. This is used for different purposes
827 * 1) missing interrupt handler for normal operation
828 * 2) delayed start of request where start_IO failed with -EBUSY
829 * 3) timeout for missing state change interrupts
830 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
831 * DASD_CQR_QUEUED for 2) and 3).
832 */
833static void
834dasd_timeout_device(unsigned long ptr)
835{
836 unsigned long flags;
837 struct dasd_device *device;
838
839 device = (struct dasd_device *) ptr;
840 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
841 /* re-activate request queue */
842 device->stopped &= ~DASD_STOPPED_PENDING;
843 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
844 dasd_schedule_bh(device);
845}
846
847/*
848 * Setup timeout for a device in jiffies.
849 */
850void
851dasd_set_timer(struct dasd_device *device, int expires)
852{
853 if (expires == 0) {
854 if (timer_pending(&device->timer))
855 del_timer(&device->timer);
856 return;
857 }
858 if (timer_pending(&device->timer)) {
859 if (mod_timer(&device->timer, jiffies + expires))
860 return;
861 }
862 device->timer.function = dasd_timeout_device;
863 device->timer.data = (unsigned long) device;
864 device->timer.expires = jiffies + expires;
865 add_timer(&device->timer);
866}
867
868/*
869 * Clear timeout for a device.
870 */
871void
872dasd_clear_timer(struct dasd_device *device)
873{
874 if (timer_pending(&device->timer))
875 del_timer(&device->timer);
876}
877
878static void
879dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
880{
881 struct dasd_ccw_req *cqr;
882 struct dasd_device *device;
883
884 cqr = (struct dasd_ccw_req *) intparm;
885 if (cqr->status != DASD_CQR_IN_IO) {
886 MESSAGE(KERN_DEBUG,
887 "invalid status in handle_killed_request: "
888 "bus_id %s, status %02x",
889 cdev->dev.bus_id, cqr->status);
890 return;
891 }
892
893 device = (struct dasd_device *) cqr->device;
894 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200895 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
897 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
898 cdev->dev.bus_id);
899 return;
900 }
901
902 /* Schedule request to be retried. */
903 cqr->status = DASD_CQR_QUEUED;
904
905 dasd_clear_timer(device);
906 dasd_schedule_bh(device);
907 dasd_put_device(device);
908}
909
910static void
911dasd_handle_state_change_pending(struct dasd_device *device)
912{
913 struct dasd_ccw_req *cqr;
914 struct list_head *l, *n;
915
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800916 /* First of all start sense subsystem status request. */
917 dasd_eer_snss(device);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 device->stopped &= ~DASD_STOPPED_PENDING;
920
921 /* restart all 'running' IO on queue */
922 list_for_each_safe(l, n, &device->ccw_queue) {
923 cqr = list_entry(l, struct dasd_ccw_req, list);
924 if (cqr->status == DASD_CQR_IN_IO) {
925 cqr->status = DASD_CQR_QUEUED;
926 }
927 }
928 dasd_clear_timer(device);
929 dasd_schedule_bh(device);
930}
931
932/*
933 * Interrupt handler for "normal" ssch-io based dasd devices.
934 */
935void
936dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
937 struct irb *irb)
938{
939 struct dasd_ccw_req *cqr, *next;
940 struct dasd_device *device;
941 unsigned long long now;
942 int expires;
943 dasd_era_t era;
944 char mask;
945
946 if (IS_ERR(irb)) {
947 switch (PTR_ERR(irb)) {
948 case -EIO:
949 dasd_handle_killed_request(cdev, intparm);
950 break;
951 case -ETIMEDOUT:
952 printk(KERN_WARNING"%s(%s): request timed out\n",
953 __FUNCTION__, cdev->dev.bus_id);
954 //FIXME - dasd uses own timeout interface...
955 break;
956 default:
957 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
958 __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
959 }
960 return;
961 }
962
963 now = get_clock();
964
965 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
966 cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
967 (unsigned int) intparm);
968
969 /* first of all check for state change pending interrupt */
970 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
971 if ((irb->scsw.dstat & mask) == mask) {
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200972 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!IS_ERR(device)) {
974 dasd_handle_state_change_pending(device);
975 dasd_put_device(device);
976 }
977 return;
978 }
979
980 cqr = (struct dasd_ccw_req *) intparm;
981
982 /* check for unsolicited interrupts */
983 if (cqr == NULL) {
984 MESSAGE(KERN_DEBUG,
985 "unsolicited interrupt received: bus_id %s",
986 cdev->dev.bus_id);
987 return;
988 }
989
990 device = (struct dasd_device *) cqr->device;
991 if (device == NULL ||
992 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
993 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
994 cdev->dev.bus_id);
995 return;
996 }
997
998 /* Check for clear pending */
999 if (cqr->status == DASD_CQR_CLEAR &&
1000 irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1001 cqr->status = DASD_CQR_QUEUED;
1002 dasd_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001003 wake_up(&dasd_flush_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 dasd_schedule_bh(device);
1005 return;
1006 }
1007
1008 /* check status - the request might have been killed by dyn detach */
1009 if (cqr->status != DASD_CQR_IN_IO) {
1010 MESSAGE(KERN_DEBUG,
1011 "invalid status: bus_id %s, status %02x",
1012 cdev->dev.bus_id, cqr->status);
1013 return;
1014 }
1015 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
1016 ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
1017
1018 /* Find out the appropriate era_action. */
Horst Hummel138c0142006-06-29 14:58:12 +02001019 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 era = dasd_era_fatal;
1021 else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1022 irb->scsw.cstat == 0 &&
1023 !irb->esw.esw0.erw.cons)
1024 era = dasd_era_none;
1025 else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
1026 era = dasd_era_fatal; /* don't recover this request */
1027 else if (irb->esw.esw0.erw.cons)
1028 era = device->discipline->examine_error(cqr, irb);
Horst Hummel138c0142006-06-29 14:58:12 +02001029 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 era = dasd_era_recover;
1031
1032 DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
1033 expires = 0;
1034 if (era == dasd_era_none) {
1035 cqr->status = DASD_CQR_DONE;
1036 cqr->stopclk = now;
1037 /* Start first request on queue if possible -> fast_io. */
1038 if (cqr->list.next != &device->ccw_queue) {
1039 next = list_entry(cqr->list.next,
1040 struct dasd_ccw_req, list);
1041 if ((next->status == DASD_CQR_QUEUED) &&
1042 (!device->stopped)) {
1043 if (device->discipline->start_IO(next) == 0)
1044 expires = next->expires;
1045 else
1046 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1047 "Interrupt fastpath "
1048 "failed!");
1049 }
1050 }
1051 } else { /* error */
1052 memcpy(&cqr->irb, irb, sizeof (struct irb));
Horst Hummel9575bf22006-12-08 15:54:15 +01001053 if (device->features & DASD_FEATURE_ERPLOG) {
1054 /* dump sense data */
1055 dasd_log_sense(cqr, irb);
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 switch (era) {
1058 case dasd_era_fatal:
1059 cqr->status = DASD_CQR_FAILED;
1060 cqr->stopclk = now;
1061 break;
1062 case dasd_era_recover:
1063 cqr->status = DASD_CQR_ERROR;
1064 break;
1065 default:
1066 BUG();
1067 }
1068 }
1069 if (expires != 0)
1070 dasd_set_timer(device, expires);
1071 else
1072 dasd_clear_timer(device);
1073 dasd_schedule_bh(device);
1074}
1075
1076/*
1077 * posts the buffer_cache about a finalized request
1078 */
1079static inline void
1080dasd_end_request(struct request *req, int uptodate)
1081{
1082 if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1083 BUG();
1084 add_disk_randomness(req->rq_disk);
Tejun Heo8ffdc652006-01-06 09:49:03 +01001085 end_that_request_last(req, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
1088/*
1089 * Process finished error recovery ccw.
1090 */
1091static inline void
1092__dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1093{
1094 dasd_erp_fn_t erp_fn;
1095
1096 if (cqr->status == DASD_CQR_DONE)
1097 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1098 else
1099 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1100 erp_fn = device->discipline->erp_postaction(cqr);
1101 erp_fn(cqr);
1102}
1103
1104/*
1105 * Process ccw request queue.
1106 */
1107static inline void
1108__dasd_process_ccw_queue(struct dasd_device * device,
1109 struct list_head *final_queue)
1110{
1111 struct list_head *l, *n;
1112 struct dasd_ccw_req *cqr;
1113 dasd_erp_fn_t erp_fn;
1114
1115restart:
1116 /* Process request with final status. */
1117 list_for_each_safe(l, n, &device->ccw_queue) {
1118 cqr = list_entry(l, struct dasd_ccw_req, list);
1119 /* Stop list processing at the first non-final request. */
1120 if (cqr->status != DASD_CQR_DONE &&
1121 cqr->status != DASD_CQR_FAILED &&
1122 cqr->status != DASD_CQR_ERROR)
1123 break;
1124 /* Process requests with DASD_CQR_ERROR */
1125 if (cqr->status == DASD_CQR_ERROR) {
1126 if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1127 cqr->status = DASD_CQR_FAILED;
1128 cqr->stopclk = get_clock();
1129 } else {
1130 if (cqr->irb.esw.esw0.erw.cons) {
1131 erp_fn = device->discipline->
1132 erp_action(cqr);
1133 erp_fn(cqr);
1134 } else
1135 dasd_default_erp_action(cqr);
1136 }
1137 goto restart;
1138 }
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001139
1140 /* First of all call extended error reporting. */
1141 if (dasd_eer_enabled(device) &&
1142 cqr->status == DASD_CQR_FAILED) {
1143 dasd_eer_write(device, cqr, DASD_EER_FATALERROR);
1144
1145 /* restart request */
1146 cqr->status = DASD_CQR_QUEUED;
1147 cqr->retries = 255;
1148 device->stopped |= DASD_STOPPED_QUIESCE;
1149 goto restart;
1150 }
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* Process finished ERP request. */
1153 if (cqr->refers) {
1154 __dasd_process_erp(device, cqr);
1155 goto restart;
1156 }
1157
1158 /* Rechain finished requests to final queue */
1159 cqr->endclk = get_clock();
1160 list_move_tail(&cqr->list, final_queue);
1161 }
1162}
1163
1164static void
1165dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1166{
1167 struct request *req;
1168 struct dasd_device *device;
1169 int status;
1170
1171 req = (struct request *) data;
1172 device = cqr->device;
1173 dasd_profile_end(device, cqr, req);
1174 status = cqr->device->discipline->free_cp(cqr,req);
1175 spin_lock_irq(&device->request_queue_lock);
1176 dasd_end_request(req, status);
1177 spin_unlock_irq(&device->request_queue_lock);
1178}
1179
1180
1181/*
1182 * Fetch requests from the block device queue.
1183 */
1184static inline void
1185__dasd_process_blk_queue(struct dasd_device * device)
1186{
1187 request_queue_t *queue;
1188 struct request *req;
1189 struct dasd_ccw_req *cqr;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001190 int nr_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 queue = device->request_queue;
1193 /* No queue ? Then there is nothing to do. */
1194 if (queue == NULL)
1195 return;
1196
1197 /*
1198 * We requeue request from the block device queue to the ccw
1199 * queue only in two states. In state DASD_STATE_READY the
1200 * partition detection is done and we need to requeue requests
1201 * for that. State DASD_STATE_ONLINE is normal block device
1202 * operation.
1203 */
1204 if (device->state != DASD_STATE_READY &&
1205 device->state != DASD_STATE_ONLINE)
1206 return;
1207 nr_queued = 0;
1208 /* Now we try to fetch requests from the request queue */
1209 list_for_each_entry(cqr, &device->ccw_queue, list)
1210 if (cqr->status == DASD_CQR_QUEUED)
1211 nr_queued++;
1212 while (!blk_queue_plugged(queue) &&
1213 elv_next_request(queue) &&
1214 nr_queued < DASD_CHANQ_MAX_SIZE) {
1215 req = elv_next_request(queue);
Horst Hummelf24acd42005-05-01 08:58:59 -07001216
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001217 if (device->features & DASD_FEATURE_READONLY &&
1218 rq_data_dir(req) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 DBF_DEV_EVENT(DBF_ERR, device,
1220 "Rejecting write request %p",
1221 req);
1222 blkdev_dequeue_request(req);
1223 dasd_end_request(req, 0);
1224 continue;
1225 }
1226 if (device->stopped & DASD_STOPPED_DC_EIO) {
1227 blkdev_dequeue_request(req);
1228 dasd_end_request(req, 0);
1229 continue;
1230 }
1231 cqr = device->discipline->build_cp(device, req);
1232 if (IS_ERR(cqr)) {
1233 if (PTR_ERR(cqr) == -ENOMEM)
1234 break; /* terminate request queue loop */
1235 DBF_DEV_EVENT(DBF_ERR, device,
1236 "CCW creation failed (rc=%ld) "
1237 "on request %p",
1238 PTR_ERR(cqr), req);
1239 blkdev_dequeue_request(req);
1240 dasd_end_request(req, 0);
1241 continue;
1242 }
1243 cqr->callback = dasd_end_request_cb;
1244 cqr->callback_data = (void *) req;
1245 cqr->status = DASD_CQR_QUEUED;
1246 blkdev_dequeue_request(req);
1247 list_add_tail(&cqr->list, &device->ccw_queue);
1248 dasd_profile_start(device, cqr, req);
1249 nr_queued++;
1250 }
1251}
1252
1253/*
1254 * Take a look at the first request on the ccw queue and check
1255 * if it reached its expire time. If so, terminate the IO.
1256 */
1257static inline void
1258__dasd_check_expire(struct dasd_device * device)
1259{
1260 struct dasd_ccw_req *cqr;
1261
1262 if (list_empty(&device->ccw_queue))
1263 return;
1264 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Horst Hummel29145a62006-12-04 15:40:15 +01001265 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1266 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1267 if (device->discipline->term_IO(cqr) != 0) {
1268 /* Hmpf, try again in 5 sec */
1269 dasd_set_timer(device, 5*HZ);
1270 DEV_MESSAGE(KERN_ERR, device,
1271 "internal error - timeout (%is) expired "
1272 "for cqr %p, termination failed, "
1273 "retrying in 5s",
1274 (cqr->expires/HZ), cqr);
1275 } else {
Horst Hummel8f617012006-08-30 14:33:33 +02001276 DEV_MESSAGE(KERN_ERR, device,
1277 "internal error - timeout (%is) expired "
1278 "for cqr %p (%i retries left)",
1279 (cqr->expires/HZ), cqr, cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 }
1282}
1283
1284/*
1285 * Take a look at the first request on the ccw queue and check
1286 * if it needs to be started.
1287 */
1288static inline void
1289__dasd_start_head(struct dasd_device * device)
1290{
1291 struct dasd_ccw_req *cqr;
1292 int rc;
1293
1294 if (list_empty(&device->ccw_queue))
1295 return;
1296 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001297 if (cqr->status != DASD_CQR_QUEUED)
1298 return;
1299 /* Non-temporary stop condition will trigger fail fast */
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001300 if (device->stopped & ~DASD_STOPPED_PENDING &&
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001301 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1302 (!dasd_eer_enabled(device))) {
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001303 cqr->status = DASD_CQR_FAILED;
1304 dasd_schedule_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001305 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001306 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001307 /* Don't try to start requests if device is stopped */
1308 if (device->stopped)
1309 return;
1310
1311 rc = device->discipline->start_IO(cqr);
1312 if (rc == 0)
1313 dasd_set_timer(device, cqr->expires);
1314 else if (rc == -EACCES) {
1315 dasd_schedule_bh(device);
1316 } else
1317 /* Hmpf, try again in 1/2 sec */
1318 dasd_set_timer(device, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Horst Hummel8f617012006-08-30 14:33:33 +02001321static inline int
1322_wait_for_clear(struct dasd_ccw_req *cqr)
1323{
1324 return (cqr->status == DASD_CQR_QUEUED);
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
Horst Hummel8f617012006-08-30 14:33:33 +02001328 * Remove all requests from the ccw queue (all = '1') or only block device
1329 * requests in case all = '0'.
1330 * Take care of the erp-chain (chained via cqr->refers) and remove either
1331 * the whole erp-chain or none of the erp-requests.
1332 * If a request is currently running, term_IO is called and the request
1333 * is re-queued. Prior to removing the terminated request we need to wait
1334 * for the clear-interrupt.
1335 * In case termination is not possible we stop processing and just finishing
1336 * the already moved requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
Horst Hummel8f617012006-08-30 14:33:33 +02001338static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339dasd_flush_ccw_queue(struct dasd_device * device, int all)
1340{
Horst Hummel8f617012006-08-30 14:33:33 +02001341 struct dasd_ccw_req *cqr, *orig, *n;
1342 int rc, i;
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 INIT_LIST_HEAD(&flush_queue);
1347 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001348 rc = 0;
1349restart:
1350 list_for_each_entry_safe(cqr, n, &device->ccw_queue, list) {
1351 /* get original request of erp request-chain */
1352 for (orig = cqr; orig->refers != NULL; orig = orig->refers);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* Flush all request or only block device requests? */
Horst Hummel8f617012006-08-30 14:33:33 +02001355 if (all == 0 && cqr->callback != dasd_end_request_cb &&
1356 orig->callback != dasd_end_request_cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 continue;
Horst Hummel8f617012006-08-30 14:33:33 +02001358 }
1359 /* Check status and move request to flush_queue */
1360 switch (cqr->status) {
1361 case DASD_CQR_IN_IO:
1362 rc = device->discipline->term_IO(cqr);
1363 if (rc) {
1364 /* unable to terminate requeust */
1365 DEV_MESSAGE(KERN_ERR, device,
1366 "dasd flush ccw_queue is unable "
1367 " to terminate request %p",
1368 cqr);
1369 /* stop flush processing */
1370 goto finished;
1371 }
1372 break;
1373 case DASD_CQR_QUEUED:
1374 case DASD_CQR_ERROR:
1375 /* set request to FAILED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001377 cqr->status = DASD_CQR_FAILED;
1378 break;
1379 default: /* do not touch the others */
1380 break;
1381 }
1382 /* Rechain request (including erp chain) */
1383 for (i = 0; cqr != NULL; cqr = cqr->refers, i++) {
1384 cqr->endclk = get_clock();
1385 list_move_tail(&cqr->list, &flush_queue);
1386 }
1387 if (i > 1)
1388 /* moved more than one request - need to restart */
1389 goto restart;
1390 }
1391
1392finished:
1393 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1394 /* Now call the callback function of flushed requests */
1395restart_cb:
1396 list_for_each_entry_safe(cqr, n, &flush_queue, list) {
1397 if (cqr->status == DASD_CQR_CLEAR) {
1398 /* wait for clear interrupt! */
1399 wait_event(dasd_flush_wq, _wait_for_clear(cqr));
1400 cqr->status = DASD_CQR_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402 /* Process finished ERP request. */
1403 if (cqr->refers) {
1404 __dasd_process_erp(device, cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001405 /* restart list_for_xx loop since dasd_process_erp
1406 * might remove multiple elements */
1407 goto restart_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
Horst Hummel8f617012006-08-30 14:33:33 +02001409 /* call the callback function */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 cqr->endclk = get_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (cqr->callback != NULL)
1412 (cqr->callback)(cqr, cqr->callback_data);
1413 }
Horst Hummel8f617012006-08-30 14:33:33 +02001414 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
1417/*
1418 * Acquire the device lock and process queues for the device.
1419 */
1420static void
1421dasd_tasklet(struct dasd_device * device)
1422{
1423 struct list_head final_queue;
1424 struct list_head *l, *n;
1425 struct dasd_ccw_req *cqr;
1426
1427 atomic_set (&device->tasklet_scheduled, 0);
1428 INIT_LIST_HEAD(&final_queue);
1429 spin_lock_irq(get_ccwdev_lock(device->cdev));
1430 /* Check expire time of first request on the ccw queue. */
1431 __dasd_check_expire(device);
1432 /* Finish off requests on ccw queue */
1433 __dasd_process_ccw_queue(device, &final_queue);
1434 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1435 /* Now call the callback function of requests with final status */
1436 list_for_each_safe(l, n, &final_queue) {
1437 cqr = list_entry(l, struct dasd_ccw_req, list);
Horst Hummelc2ba4442006-02-01 03:06:37 -08001438 list_del_init(&cqr->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (cqr->callback != NULL)
1440 (cqr->callback)(cqr, cqr->callback_data);
1441 }
1442 spin_lock_irq(&device->request_queue_lock);
1443 spin_lock(get_ccwdev_lock(device->cdev));
1444 /* Get new request from the block device request queue */
1445 __dasd_process_blk_queue(device);
1446 /* Now check if the head of the ccw queue needs to be started. */
1447 __dasd_start_head(device);
1448 spin_unlock(get_ccwdev_lock(device->cdev));
1449 spin_unlock_irq(&device->request_queue_lock);
1450 dasd_put_device(device);
1451}
1452
1453/*
1454 * Schedules a call to dasd_tasklet over the device tasklet.
1455 */
1456void
1457dasd_schedule_bh(struct dasd_device * device)
1458{
1459 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001460 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return;
1462 dasd_get_device(device);
1463 tasklet_hi_schedule(&device->tasklet);
1464}
1465
1466/*
1467 * Queue a request to the head of the ccw_queue. Start the I/O if
1468 * possible.
1469 */
1470void
1471dasd_add_request_head(struct dasd_ccw_req *req)
1472{
1473 struct dasd_device *device;
1474 unsigned long flags;
1475
1476 device = req->device;
1477 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1478 req->status = DASD_CQR_QUEUED;
1479 req->device = device;
1480 list_add(&req->list, &device->ccw_queue);
1481 /* let the bh start the request to keep them in order */
1482 dasd_schedule_bh(device);
1483 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1484}
1485
1486/*
1487 * Queue a request to the tail of the ccw_queue. Start the I/O if
1488 * possible.
1489 */
1490void
1491dasd_add_request_tail(struct dasd_ccw_req *req)
1492{
1493 struct dasd_device *device;
1494 unsigned long flags;
1495
1496 device = req->device;
1497 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1498 req->status = DASD_CQR_QUEUED;
1499 req->device = device;
1500 list_add_tail(&req->list, &device->ccw_queue);
1501 /* let the bh start the request to keep them in order */
1502 dasd_schedule_bh(device);
1503 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1504}
1505
1506/*
1507 * Wakeup callback.
1508 */
1509static void
1510dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1511{
1512 wake_up((wait_queue_head_t *) data);
1513}
1514
1515static inline int
1516_wait_for_wakeup(struct dasd_ccw_req *cqr)
1517{
1518 struct dasd_device *device;
1519 int rc;
1520
1521 device = cqr->device;
1522 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001523 rc = ((cqr->status == DASD_CQR_DONE ||
1524 cqr->status == DASD_CQR_FAILED) &&
1525 list_empty(&cqr->list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1527 return rc;
1528}
1529
1530/*
1531 * Attempts to start a special ccw queue and waits for its completion.
1532 */
1533int
1534dasd_sleep_on(struct dasd_ccw_req * cqr)
1535{
1536 wait_queue_head_t wait_q;
1537 struct dasd_device *device;
1538 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 device = cqr->device;
1541 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel138c0142006-06-29 14:58:12 +02001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 init_waitqueue_head (&wait_q);
1544 cqr->callback = dasd_wakeup_cb;
1545 cqr->callback_data = (void *) &wait_q;
1546 cqr->status = DASD_CQR_QUEUED;
1547 list_add_tail(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /* let the bh start the request to keep them in order */
1550 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1553
1554 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 /* Request status is either done or failed. */
1557 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1558 return rc;
1559}
1560
1561/*
1562 * Attempts to start a special ccw queue and wait interruptible
1563 * for its completion.
1564 */
1565int
1566dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1567{
1568 wait_queue_head_t wait_q;
1569 struct dasd_device *device;
1570 int rc, finished;
1571
1572 device = cqr->device;
1573 spin_lock_irq(get_ccwdev_lock(device->cdev));
1574
1575 init_waitqueue_head (&wait_q);
1576 cqr->callback = dasd_wakeup_cb;
1577 cqr->callback_data = (void *) &wait_q;
1578 cqr->status = DASD_CQR_QUEUED;
1579 list_add_tail(&cqr->list, &device->ccw_queue);
1580
1581 /* let the bh start the request to keep them in order */
1582 dasd_schedule_bh(device);
1583 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1584
1585 finished = 0;
1586 while (!finished) {
1587 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1588 if (rc != -ERESTARTSYS) {
Horst Hummelc2ba4442006-02-01 03:06:37 -08001589 /* Request is final (done or failed) */
1590 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 break;
1592 }
1593 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001594 switch (cqr->status) {
1595 case DASD_CQR_IN_IO:
1596 /* terminate runnig cqr */
1597 if (device->discipline->term_IO) {
1598 cqr->retries = -1;
1599 device->discipline->term_IO(cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001600 /* wait (non-interruptible) for final status
1601 * because signal ist still pending */
Horst Hummelc2ba4442006-02-01 03:06:37 -08001602 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1603 wait_event(wait_q, _wait_for_wakeup(cqr));
1604 spin_lock_irq(get_ccwdev_lock(device->cdev));
1605 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1606 finished = 1;
1607 }
1608 break;
1609 case DASD_CQR_QUEUED:
1610 /* request */
1611 list_del_init(&cqr->list);
1612 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 finished = 1;
Horst Hummelc2ba4442006-02-01 03:06:37 -08001614 break;
1615 default:
1616 /* cqr with 'non-interruptable' status - just wait */
1617 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1620 }
1621 return rc;
1622}
1623
1624/*
1625 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1626 * for eckd devices) the currently running request has to be terminated
1627 * and be put back to status queued, before the special request is added
1628 * to the head of the queue. Then the special request is waited on normally.
1629 */
1630static inline int
1631_dasd_term_running_cqr(struct dasd_device *device)
1632{
1633 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635 if (list_empty(&device->ccw_queue))
1636 return 0;
1637 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Horst Hummel8f617012006-08-30 14:33:33 +02001638 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641int
1642dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1643{
1644 wait_queue_head_t wait_q;
1645 struct dasd_device *device;
1646 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 device = cqr->device;
1649 spin_lock_irq(get_ccwdev_lock(device->cdev));
1650 rc = _dasd_term_running_cqr(device);
1651 if (rc) {
1652 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1653 return rc;
1654 }
Horst Hummel138c0142006-06-29 14:58:12 +02001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 init_waitqueue_head (&wait_q);
1657 cqr->callback = dasd_wakeup_cb;
1658 cqr->callback_data = (void *) &wait_q;
1659 cqr->status = DASD_CQR_QUEUED;
1660 list_add(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /* let the bh start the request to keep them in order */
1663 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1666
1667 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /* Request status is either done or failed. */
1670 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1671 return rc;
1672}
1673
1674/*
1675 * Cancels a request that was started with dasd_sleep_on_req.
1676 * This is useful to timeout requests. The request will be
1677 * terminated if it is currently in i/o.
1678 * Returns 1 if the request has been terminated.
1679 */
1680int
1681dasd_cancel_req(struct dasd_ccw_req *cqr)
1682{
1683 struct dasd_device *device = cqr->device;
1684 unsigned long flags;
1685 int rc;
1686
1687 rc = 0;
1688 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1689 switch (cqr->status) {
1690 case DASD_CQR_QUEUED:
1691 /* request was not started - just set to failed */
1692 cqr->status = DASD_CQR_FAILED;
1693 break;
1694 case DASD_CQR_IN_IO:
1695 /* request in IO - terminate IO and release again */
1696 if (device->discipline->term_IO(cqr) != 0)
1697 /* what to do if unable to terminate ??????
1698 e.g. not _IN_IO */
1699 cqr->status = DASD_CQR_FAILED;
1700 cqr->stopclk = get_clock();
1701 rc = 1;
1702 break;
1703 case DASD_CQR_DONE:
1704 case DASD_CQR_FAILED:
1705 /* already finished - do nothing */
1706 break;
1707 default:
1708 DEV_MESSAGE(KERN_ALERT, device,
1709 "invalid status %02x in request",
1710 cqr->status);
1711 BUG();
1712
1713 }
1714 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1715 dasd_schedule_bh(device);
1716 return rc;
1717}
1718
1719/*
1720 * SECTION: Block device operations (request queue, partitions, open, release).
1721 */
1722
1723/*
1724 * Dasd request queue function. Called from ll_rw_blk.c
1725 */
1726static void
1727do_dasd_request(request_queue_t * queue)
1728{
1729 struct dasd_device *device;
1730
1731 device = (struct dasd_device *) queue->queuedata;
1732 spin_lock(get_ccwdev_lock(device->cdev));
1733 /* Get new request from the block device request queue */
1734 __dasd_process_blk_queue(device);
1735 /* Now check if the head of the ccw queue needs to be started. */
1736 __dasd_start_head(device);
1737 spin_unlock(get_ccwdev_lock(device->cdev));
1738}
1739
1740/*
1741 * Allocate and initialize request queue and default I/O scheduler.
1742 */
1743static int
1744dasd_alloc_queue(struct dasd_device * device)
1745{
1746 int rc;
1747
1748 device->request_queue = blk_init_queue(do_dasd_request,
1749 &device->request_queue_lock);
1750 if (device->request_queue == NULL)
1751 return -ENOMEM;
1752
1753 device->request_queue->queuedata = device;
1754
1755 elevator_exit(device->request_queue->elevator);
1756 rc = elevator_init(device->request_queue, "deadline");
1757 if (rc) {
1758 blk_cleanup_queue(device->request_queue);
1759 return rc;
1760 }
1761 return 0;
1762}
1763
1764/*
1765 * Allocate and initialize request queue.
1766 */
1767static void
1768dasd_setup_queue(struct dasd_device * device)
1769{
1770 int max;
1771
1772 blk_queue_hardsect_size(device->request_queue, device->bp_block);
1773 max = device->discipline->max_blocks << device->s2b_shift;
1774 blk_queue_max_sectors(device->request_queue, max);
1775 blk_queue_max_phys_segments(device->request_queue, -1L);
1776 blk_queue_max_hw_segments(device->request_queue, -1L);
1777 blk_queue_max_segment_size(device->request_queue, -1L);
1778 blk_queue_segment_boundary(device->request_queue, -1L);
Heiko Carstensed68cb32006-01-14 13:21:05 -08001779 blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
1782/*
1783 * Deactivate and free request queue.
1784 */
1785static void
1786dasd_free_queue(struct dasd_device * device)
1787{
1788 if (device->request_queue) {
1789 blk_cleanup_queue(device->request_queue);
1790 device->request_queue = NULL;
1791 }
1792}
1793
1794/*
1795 * Flush request on the request queue.
1796 */
1797static void
1798dasd_flush_request_queue(struct dasd_device * device)
1799{
1800 struct request *req;
1801
1802 if (!device->request_queue)
1803 return;
Horst Hummel138c0142006-06-29 14:58:12 +02001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 spin_lock_irq(&device->request_queue_lock);
Horst Hummel8f617012006-08-30 14:33:33 +02001806 while ((req = elv_next_request(device->request_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 blkdev_dequeue_request(req);
Horst Hummelebc45992006-08-09 10:30:22 +02001808 dasd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810 spin_unlock_irq(&device->request_queue_lock);
1811}
1812
1813static int
1814dasd_open(struct inode *inp, struct file *filp)
1815{
1816 struct gendisk *disk = inp->i_bdev->bd_disk;
1817 struct dasd_device *device = disk->private_data;
1818 int rc;
1819
1820 atomic_inc(&device->open_count);
1821 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1822 rc = -ENODEV;
1823 goto unlock;
1824 }
1825
1826 if (!try_module_get(device->discipline->owner)) {
1827 rc = -EINVAL;
1828 goto unlock;
1829 }
1830
1831 if (dasd_probeonly) {
1832 DEV_MESSAGE(KERN_INFO, device, "%s",
1833 "No access to device due to probeonly mode");
1834 rc = -EPERM;
1835 goto out;
1836 }
1837
Horst Hummel90f00942006-03-07 21:55:39 -08001838 if (device->state <= DASD_STATE_BASIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1840 " Cannot open unrecognized device");
1841 rc = -ENODEV;
1842 goto out;
1843 }
1844
1845 return 0;
1846
1847out:
1848 module_put(device->discipline->owner);
1849unlock:
1850 atomic_dec(&device->open_count);
1851 return rc;
1852}
1853
1854static int
1855dasd_release(struct inode *inp, struct file *filp)
1856{
1857 struct gendisk *disk = inp->i_bdev->bd_disk;
1858 struct dasd_device *device = disk->private_data;
1859
1860 atomic_dec(&device->open_count);
1861 module_put(device->discipline->owner);
1862 return 0;
1863}
1864
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001865/*
1866 * Return disk geometry.
1867 */
1868static int
1869dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1870{
1871 struct dasd_device *device;
1872
1873 device = bdev->bd_disk->private_data;
1874 if (!device)
1875 return -ENODEV;
1876
1877 if (!device->discipline ||
1878 !device->discipline->fill_geometry)
1879 return -EINVAL;
1880
1881 device->discipline->fill_geometry(device, geo);
1882 geo->start = get_start_sect(bdev) >> device->s2b_shift;
1883 return 0;
1884}
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886struct block_device_operations
1887dasd_device_operations = {
1888 .owner = THIS_MODULE,
1889 .open = dasd_open,
1890 .release = dasd_release,
1891 .ioctl = dasd_ioctl,
Christoph Hellwig82620372006-01-09 20:52:12 -08001892 .compat_ioctl = dasd_compat_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001893 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894};
1895
1896
1897static void
1898dasd_exit(void)
1899{
1900#ifdef CONFIG_PROC_FS
1901 dasd_proc_exit();
1902#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001903 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07001904 if (dasd_page_cache != NULL) {
1905 kmem_cache_destroy(dasd_page_cache);
1906 dasd_page_cache = NULL;
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 dasd_gendisk_exit();
1909 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (dasd_debug_area != NULL) {
1911 debug_unregister(dasd_debug_area);
1912 dasd_debug_area = NULL;
1913 }
1914}
1915
1916/*
1917 * SECTION: common functions for ccw_driver use
1918 */
1919
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001920/*
1921 * Initial attempt at a probe function. this can be simplified once
1922 * the other detection code is gone.
1923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924int
1925dasd_generic_probe (struct ccw_device *cdev,
1926 struct dasd_discipline *discipline)
1927{
1928 int ret;
1929
Horst Hummel40545572006-06-29 15:08:18 +02001930 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
1931 if (ret) {
1932 printk(KERN_WARNING
1933 "dasd_generic_probe: could not set ccw-device options "
1934 "for %s\n", cdev->dev.bus_id);
1935 return ret;
1936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 ret = dasd_add_sysfs_files(cdev);
1938 if (ret) {
1939 printk(KERN_WARNING
1940 "dasd_generic_probe: could not add sysfs entries "
1941 "for %s\n", cdev->dev.bus_id);
Horst Hummel40545572006-06-29 15:08:18 +02001942 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Horst Hummel40545572006-06-29 15:08:18 +02001944 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Horst Hummel40545572006-06-29 15:08:18 +02001946 /*
1947 * Automatically online either all dasd devices (dasd_autodetect)
1948 * or all devices specified with dasd= parameters during
1949 * initial probe.
1950 */
1951 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
1952 (dasd_autodetect && dasd_busid_known(cdev->dev.bus_id) != 0))
1953 ret = ccw_device_set_online(cdev);
1954 if (ret)
1955 printk(KERN_WARNING
1956 "dasd_generic_probe: could not initially online "
1957 "ccw-device %s\n", cdev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return ret;
1959}
1960
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001961/*
1962 * This will one day be called from a global not_oper handler.
1963 * It is also used by driver_unregister during module unload.
1964 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965void
1966dasd_generic_remove (struct ccw_device *cdev)
1967{
1968 struct dasd_device *device;
1969
Horst Hummel59afda72005-05-16 21:53:39 -07001970 cdev->handler = NULL;
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 dasd_remove_sysfs_files(cdev);
1973 device = dasd_device_from_cdev(cdev);
1974 if (IS_ERR(device))
1975 return;
1976 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1977 /* Already doing offline processing */
1978 dasd_put_device(device);
1979 return;
1980 }
1981 /*
1982 * This device is removed unconditionally. Set offline
1983 * flag to prevent dasd_open from opening it while it is
1984 * no quite down yet.
1985 */
1986 dasd_set_target_state(device, DASD_STATE_NEW);
1987 /* dasd_delete_device destroys the device reference. */
1988 dasd_delete_device(device);
1989}
1990
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001991/*
1992 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001994 * or the user has started activation through sysfs.
1995 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996int
1997dasd_generic_set_online (struct ccw_device *cdev,
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001998 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002001 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002003 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002004
Horst Hummel40545572006-06-29 15:08:18 +02002005 /* first online clears initial online feature flag */
2006 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 device = dasd_create_device(cdev);
2008 if (IS_ERR(device))
2009 return PTR_ERR(device);
2010
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002011 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002012 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (!dasd_diag_discipline_pointer) {
2014 printk (KERN_WARNING
2015 "dasd_generic couldn't online device %s "
2016 "- discipline DIAG not available\n",
2017 cdev->dev.bus_id);
2018 dasd_delete_device(device);
2019 return -ENODEV;
2020 }
2021 discipline = dasd_diag_discipline_pointer;
2022 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002023 if (!try_module_get(base_discipline->owner)) {
2024 dasd_delete_device(device);
2025 return -EINVAL;
2026 }
2027 if (!try_module_get(discipline->owner)) {
2028 module_put(base_discipline->owner);
2029 dasd_delete_device(device);
2030 return -EINVAL;
2031 }
2032 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 device->discipline = discipline;
2034
2035 rc = discipline->check_device(device);
2036 if (rc) {
2037 printk (KERN_WARNING
2038 "dasd_generic couldn't online device %s "
2039 "with discipline %s rc=%i\n",
2040 cdev->dev.bus_id, discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002041 module_put(discipline->owner);
2042 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 dasd_delete_device(device);
2044 return rc;
2045 }
2046
2047 dasd_set_target_state(device, DASD_STATE_ONLINE);
2048 if (device->state <= DASD_STATE_KNOWN) {
2049 printk (KERN_WARNING
2050 "dasd_generic discipline not found for %s\n",
2051 cdev->dev.bus_id);
2052 rc = -ENODEV;
2053 dasd_set_target_state(device, DASD_STATE_NEW);
2054 dasd_delete_device(device);
2055 } else
2056 pr_debug("dasd_generic device %s found\n",
2057 cdev->dev.bus_id);
2058
2059 /* FIXME: we have to wait for the root device but we don't want
2060 * to wait for each single device but for all at once. */
2061 wait_event(dasd_init_waitq, _wait_for_device(device));
2062
2063 dasd_put_device(device);
2064
2065 return rc;
2066}
2067
2068int
2069dasd_generic_set_offline (struct ccw_device *cdev)
2070{
2071 struct dasd_device *device;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002072 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 device = dasd_device_from_cdev(cdev);
2075 if (IS_ERR(device))
2076 return PTR_ERR(device);
2077 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2078 /* Already doing offline processing */
2079 dasd_put_device(device);
2080 return 0;
2081 }
2082 /*
2083 * We must make sure that this device is currently not in use.
2084 * The open_count is increased for every opener, that includes
2085 * the blkdev_get in dasd_scan_partitions. We are only interested
2086 * in the other openers.
2087 */
2088 max_count = device->bdev ? 0 : -1;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002089 open_count = (int) atomic_read(&device->open_count);
2090 if (open_count > max_count) {
2091 if (open_count > 0)
2092 printk (KERN_WARNING "Can't offline dasd device with "
2093 "open count = %i.\n",
2094 open_count);
2095 else
2096 printk (KERN_WARNING "%s",
2097 "Can't offline dasd device due to internal "
2098 "use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2100 dasd_put_device(device);
2101 return -EBUSY;
2102 }
2103 dasd_set_target_state(device, DASD_STATE_NEW);
2104 /* dasd_delete_device destroys the device reference. */
2105 dasd_delete_device(device);
2106
2107 return 0;
2108}
2109
2110int
2111dasd_generic_notify(struct ccw_device *cdev, int event)
2112{
2113 struct dasd_device *device;
2114 struct dasd_ccw_req *cqr;
2115 unsigned long flags;
2116 int ret;
2117
2118 device = dasd_device_from_cdev(cdev);
2119 if (IS_ERR(device))
2120 return 0;
2121 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
2122 ret = 0;
2123 switch (event) {
2124 case CIO_GONE:
2125 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002126 /* First of all call extended error reporting. */
2127 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (device->state < DASD_STATE_BASIC)
2130 break;
2131 /* Device is active. We want to keep it. */
2132 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
2133 list_for_each_entry(cqr, &device->ccw_queue, list)
2134 if (cqr->status == DASD_CQR_IN_IO)
2135 cqr->status = DASD_CQR_FAILED;
2136 device->stopped |= DASD_STOPPED_DC_EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 } else {
2138 list_for_each_entry(cqr, &device->ccw_queue, list)
2139 if (cqr->status == DASD_CQR_IN_IO) {
2140 cqr->status = DASD_CQR_QUEUED;
2141 cqr->retries++;
2142 }
2143 device->stopped |= DASD_STOPPED_DC_WAIT;
2144 dasd_set_timer(device, 0);
2145 }
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002146 dasd_schedule_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 ret = 1;
2148 break;
2149 case CIO_OPER:
2150 /* FIXME: add a sanity check. */
2151 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
2152 dasd_schedule_bh(device);
2153 ret = 1;
2154 break;
2155 }
2156 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2157 dasd_put_device(device);
2158 return ret;
2159}
2160
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162static int __init
2163dasd_init(void)
2164{
2165 int rc;
2166
2167 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002168 init_waitqueue_head(&dasd_flush_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Michael Holzheu66a464d2005-06-25 14:55:33 -07002171 dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (dasd_debug_area == NULL) {
2173 rc = -ENOMEM;
2174 goto failed;
2175 }
2176 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002177 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2180
2181 dasd_diag_discipline_pointer = NULL;
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 rc = dasd_devmap_init();
2184 if (rc)
2185 goto failed;
2186 rc = dasd_gendisk_init();
2187 if (rc)
2188 goto failed;
2189 rc = dasd_parse();
2190 if (rc)
2191 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002192 rc = dasd_eer_init();
2193 if (rc)
2194 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195#ifdef CONFIG_PROC_FS
2196 rc = dasd_proc_init();
2197 if (rc)
2198 goto failed;
2199#endif
2200
2201 return 0;
2202failed:
2203 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2204 dasd_exit();
2205 return rc;
2206}
2207
2208module_init(dasd_init);
2209module_exit(dasd_exit);
2210
2211EXPORT_SYMBOL(dasd_debug_area);
2212EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2213
2214EXPORT_SYMBOL(dasd_add_request_head);
2215EXPORT_SYMBOL(dasd_add_request_tail);
2216EXPORT_SYMBOL(dasd_cancel_req);
2217EXPORT_SYMBOL(dasd_clear_timer);
2218EXPORT_SYMBOL(dasd_enable_device);
2219EXPORT_SYMBOL(dasd_int_handler);
2220EXPORT_SYMBOL(dasd_kfree_request);
2221EXPORT_SYMBOL(dasd_kick_device);
2222EXPORT_SYMBOL(dasd_kmalloc_request);
2223EXPORT_SYMBOL(dasd_schedule_bh);
2224EXPORT_SYMBOL(dasd_set_target_state);
2225EXPORT_SYMBOL(dasd_set_timer);
2226EXPORT_SYMBOL(dasd_sfree_request);
2227EXPORT_SYMBOL(dasd_sleep_on);
2228EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2229EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2230EXPORT_SYMBOL(dasd_smalloc_request);
2231EXPORT_SYMBOL(dasd_start_IO);
2232EXPORT_SYMBOL(dasd_term_IO);
2233
2234EXPORT_SYMBOL_GPL(dasd_generic_probe);
2235EXPORT_SYMBOL_GPL(dasd_generic_remove);
2236EXPORT_SYMBOL_GPL(dasd_generic_notify);
2237EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2238EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239