blob: a2cef57d7bcb2e72bc26366902c41c6b461f498e [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 *);
57static void do_kick_device(void *data);
58
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);
103 INIT_WORK(&device->kick_work, do_kick_device, device);
104 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
410do_kick_device(void *data)
411{
412 struct dasd_device *device;
413
414 device = (struct dasd_device *) data;
415 dasd_change_state(device);
416 dasd_schedule_bh(device);
417 dasd_put_device(device);
418}
419
420void
421dasd_kick_device(struct dasd_device *device)
422{
423 dasd_get_device(device);
424 /* queue call to dasd_kick_device to the kernel event daemon. */
425 schedule_work(&device->kick_work);
426}
427
428/*
429 * Set the target state for a device and starts the state change.
430 */
431void
432dasd_set_target_state(struct dasd_device *device, int target)
433{
434 /* If we are in probeonly mode stop at DASD_STATE_READY. */
435 if (dasd_probeonly && target > DASD_STATE_READY)
436 target = DASD_STATE_READY;
437 if (device->target != target) {
438 if (device->state == target)
439 wake_up(&dasd_init_waitq);
440 device->target = target;
441 }
442 if (device->state != device->target)
443 dasd_change_state(device);
444}
445
446/*
447 * Enable devices with device numbers in [from..to].
448 */
449static inline int
450_wait_for_device(struct dasd_device *device)
451{
452 return (device->state == device->target);
453}
454
455void
456dasd_enable_device(struct dasd_device *device)
457{
458 dasd_set_target_state(device, DASD_STATE_ONLINE);
459 if (device->state <= DASD_STATE_KNOWN)
460 /* No discipline for device found. */
461 dasd_set_target_state(device, DASD_STATE_NEW);
462 /* Now wait for the devices to come up. */
463 wait_event(dasd_init_waitq, _wait_for_device(device));
464}
465
466/*
467 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
468 */
469#ifdef CONFIG_DASD_PROFILE
470
471struct dasd_profile_info_t dasd_global_profile;
472unsigned int dasd_profile_level = DASD_PROFILE_OFF;
473
474/*
475 * Increments counter in global and local profiling structures.
476 */
477#define dasd_profile_counter(value, counter, device) \
478{ \
479 int index; \
480 for (index = 0; index < 31 && value >> (2+index); index++); \
481 dasd_global_profile.counter[index]++; \
482 device->profile.counter[index]++; \
483}
484
485/*
486 * Add profiling information for cqr before execution.
487 */
488static inline void
489dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
490 struct request *req)
491{
492 struct list_head *l;
493 unsigned int counter;
494
495 if (dasd_profile_level != DASD_PROFILE_ON)
496 return;
497
498 /* count the length of the chanq for statistics */
499 counter = 0;
500 list_for_each(l, &device->ccw_queue)
501 if (++counter >= 31)
502 break;
503 dasd_global_profile.dasd_io_nr_req[counter]++;
504 device->profile.dasd_io_nr_req[counter]++;
505}
506
507/*
508 * Add profiling information for cqr after execution.
509 */
510static inline void
511dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
512 struct request *req)
513{
514 long strtime, irqtime, endtime, tottime; /* in microseconds */
515 long tottimeps, sectors;
516
517 if (dasd_profile_level != DASD_PROFILE_ON)
518 return;
519
520 sectors = req->nr_sectors;
521 if (!cqr->buildclk || !cqr->startclk ||
522 !cqr->stopclk || !cqr->endclk ||
523 !sectors)
524 return;
525
526 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
527 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
528 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
529 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
530 tottimeps = tottime / sectors;
531
532 if (!dasd_global_profile.dasd_io_reqs)
533 memset(&dasd_global_profile, 0,
534 sizeof (struct dasd_profile_info_t));
535 dasd_global_profile.dasd_io_reqs++;
536 dasd_global_profile.dasd_io_sects += sectors;
537
538 if (!device->profile.dasd_io_reqs)
539 memset(&device->profile, 0,
540 sizeof (struct dasd_profile_info_t));
541 device->profile.dasd_io_reqs++;
542 device->profile.dasd_io_sects += sectors;
543
544 dasd_profile_counter(sectors, dasd_io_secs, device);
545 dasd_profile_counter(tottime, dasd_io_times, device);
546 dasd_profile_counter(tottimeps, dasd_io_timps, device);
547 dasd_profile_counter(strtime, dasd_io_time1, device);
548 dasd_profile_counter(irqtime, dasd_io_time2, device);
549 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
550 dasd_profile_counter(endtime, dasd_io_time3, device);
551}
552#else
553#define dasd_profile_start(device, cqr, req) do {} while (0)
554#define dasd_profile_end(device, cqr, req) do {} while (0)
555#endif /* CONFIG_DASD_PROFILE */
556
557/*
558 * Allocate memory for a channel program with 'cplength' channel
559 * command words and 'datasize' additional space. There are two
560 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
561 * memory and 2) dasd_smalloc_request uses the static ccw memory
562 * that gets allocated for each device.
563 */
564struct dasd_ccw_req *
565dasd_kmalloc_request(char *magic, int cplength, int datasize,
566 struct dasd_device * device)
567{
568 struct dasd_ccw_req *cqr;
569
570 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100571 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
572 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800574 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (cqr == NULL)
576 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 cqr->cpaddr = NULL;
578 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800579 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 GFP_ATOMIC | GFP_DMA);
581 if (cqr->cpaddr == NULL) {
582 kfree(cqr);
583 return ERR_PTR(-ENOMEM);
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 cqr->data = NULL;
587 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800588 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800590 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 kfree(cqr);
592 return ERR_PTR(-ENOMEM);
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 strncpy((char *) &cqr->magic, magic, 4);
596 ASCEBC((char *) &cqr->magic, 4);
597 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
598 dasd_get_device(device);
599 return cqr;
600}
601
602struct dasd_ccw_req *
603dasd_smalloc_request(char *magic, int cplength, int datasize,
604 struct dasd_device * device)
605{
606 unsigned long flags;
607 struct dasd_ccw_req *cqr;
608 char *data;
609 int size;
610
611 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100612 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
613 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
616 if (cplength > 0)
617 size += cplength * sizeof(struct ccw1);
618 if (datasize > 0)
619 size += datasize;
620 spin_lock_irqsave(&device->mem_lock, flags);
621 cqr = (struct dasd_ccw_req *)
622 dasd_alloc_chunk(&device->ccw_chunks, size);
623 spin_unlock_irqrestore(&device->mem_lock, flags);
624 if (cqr == NULL)
625 return ERR_PTR(-ENOMEM);
626 memset(cqr, 0, sizeof(struct dasd_ccw_req));
627 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
628 cqr->cpaddr = NULL;
629 if (cplength > 0) {
630 cqr->cpaddr = (struct ccw1 *) data;
631 data += cplength*sizeof(struct ccw1);
632 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
633 }
634 cqr->data = NULL;
635 if (datasize > 0) {
636 cqr->data = data;
637 memset(cqr->data, 0, datasize);
638 }
639 strncpy((char *) &cqr->magic, magic, 4);
640 ASCEBC((char *) &cqr->magic, 4);
641 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
642 dasd_get_device(device);
643 return cqr;
644}
645
646/*
647 * Free memory of a channel program. This function needs to free all the
648 * idal lists that might have been created by dasd_set_cda and the
649 * struct dasd_ccw_req itself.
650 */
651void
652dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
653{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800654#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct ccw1 *ccw;
656
657 /* Clear any idals used for the request. */
658 ccw = cqr->cpaddr;
659 do {
660 clear_normalized_cda(ccw);
661 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
662#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800663 kfree(cqr->cpaddr);
664 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 kfree(cqr);
666 dasd_put_device(device);
667}
668
669void
670dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
671{
672 unsigned long flags;
673
674 spin_lock_irqsave(&device->mem_lock, flags);
675 dasd_free_chunk(&device->ccw_chunks, cqr);
676 spin_unlock_irqrestore(&device->mem_lock, flags);
677 dasd_put_device(device);
678}
679
680/*
681 * Check discipline magic in cqr.
682 */
683static inline int
684dasd_check_cqr(struct dasd_ccw_req *cqr)
685{
686 struct dasd_device *device;
687
688 if (cqr == NULL)
689 return -EINVAL;
690 device = cqr->device;
691 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
692 DEV_MESSAGE(KERN_WARNING, device,
693 " dasd_ccw_req 0x%08x magic doesn't match"
694 " discipline 0x%08x",
695 cqr->magic,
696 *(unsigned int *) device->discipline->name);
697 return -EINVAL;
698 }
699 return 0;
700}
701
702/*
703 * Terminate the current i/o and set the request to clear_pending.
704 * Timer keeps device runnig.
705 * ccw_device_clear can fail if the i/o subsystem
706 * is in a bad mood.
707 */
708int
709dasd_term_IO(struct dasd_ccw_req * cqr)
710{
711 struct dasd_device *device;
712 int retries, rc;
713
714 /* Check the cqr */
715 rc = dasd_check_cqr(cqr);
716 if (rc)
717 return rc;
718 retries = 0;
719 device = (struct dasd_device *) cqr->device;
720 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
721 rc = ccw_device_clear(device->cdev, (long) cqr);
722 switch (rc) {
723 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800724 cqr->retries--;
725 cqr->status = DASD_CQR_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200727 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 DBF_DEV_EVENT(DBF_DEBUG, device,
729 "terminate cqr %p successful",
730 cqr);
731 break;
732 case -ENODEV:
733 DBF_DEV_EVENT(DBF_ERR, device, "%s",
734 "device gone, retry");
735 break;
736 case -EIO:
737 DBF_DEV_EVENT(DBF_ERR, device, "%s",
738 "I/O error, retry");
739 break;
740 case -EINVAL:
741 case -EBUSY:
742 DBF_DEV_EVENT(DBF_ERR, device, "%s",
743 "device busy, retry later");
744 break;
745 default:
746 DEV_MESSAGE(KERN_ERR, device,
747 "line %d unknown RC=%d, please "
748 "report to linux390@de.ibm.com",
749 __LINE__, rc);
750 BUG();
751 break;
752 }
753 retries++;
754 }
755 dasd_schedule_bh(device);
756 return rc;
757}
758
759/*
760 * Start the i/o. This start_IO can fail if the channel is really busy.
761 * In that case set up a timer to start the request later.
762 */
763int
764dasd_start_IO(struct dasd_ccw_req * cqr)
765{
766 struct dasd_device *device;
767 int rc;
768
769 /* Check the cqr */
770 rc = dasd_check_cqr(cqr);
771 if (rc)
772 return rc;
773 device = (struct dasd_device *) cqr->device;
774 if (cqr->retries < 0) {
775 DEV_MESSAGE(KERN_DEBUG, device,
776 "start_IO: request %p (%02x/%i) - no retry left.",
777 cqr, cqr->status, cqr->retries);
778 cqr->status = DASD_CQR_FAILED;
779 return -EIO;
780 }
781 cqr->startclk = get_clock();
782 cqr->starttime = jiffies;
783 cqr->retries--;
784 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
785 cqr->lpm, 0);
786 switch (rc) {
787 case 0:
788 cqr->status = DASD_CQR_IN_IO;
789 DBF_DEV_EVENT(DBF_DEBUG, device,
790 "start_IO: request %p started successful",
791 cqr);
792 break;
793 case -EBUSY:
794 DBF_DEV_EVENT(DBF_ERR, device, "%s",
795 "start_IO: device busy, retry later");
796 break;
797 case -ETIMEDOUT:
798 DBF_DEV_EVENT(DBF_ERR, device, "%s",
799 "start_IO: request timeout, retry later");
800 break;
801 case -EACCES:
802 /* -EACCES indicates that the request used only a
803 * subset of the available pathes and all these
804 * pathes are gone.
805 * Do a retry with all available pathes.
806 */
807 cqr->lpm = LPM_ANYPATH;
808 DBF_DEV_EVENT(DBF_ERR, device, "%s",
809 "start_IO: selected pathes gone,"
810 " retry on all pathes");
811 break;
812 case -ENODEV:
813 case -EIO:
814 DBF_DEV_EVENT(DBF_ERR, device, "%s",
815 "start_IO: device gone, retry");
816 break;
817 default:
818 DEV_MESSAGE(KERN_ERR, device,
819 "line %d unknown RC=%d, please report"
820 " to linux390@de.ibm.com", __LINE__, rc);
821 BUG();
822 break;
823 }
824 return rc;
825}
826
827/*
828 * Timeout function for dasd devices. This is used for different purposes
829 * 1) missing interrupt handler for normal operation
830 * 2) delayed start of request where start_IO failed with -EBUSY
831 * 3) timeout for missing state change interrupts
832 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
833 * DASD_CQR_QUEUED for 2) and 3).
834 */
835static void
836dasd_timeout_device(unsigned long ptr)
837{
838 unsigned long flags;
839 struct dasd_device *device;
840
841 device = (struct dasd_device *) ptr;
842 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
843 /* re-activate request queue */
844 device->stopped &= ~DASD_STOPPED_PENDING;
845 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
846 dasd_schedule_bh(device);
847}
848
849/*
850 * Setup timeout for a device in jiffies.
851 */
852void
853dasd_set_timer(struct dasd_device *device, int expires)
854{
855 if (expires == 0) {
856 if (timer_pending(&device->timer))
857 del_timer(&device->timer);
858 return;
859 }
860 if (timer_pending(&device->timer)) {
861 if (mod_timer(&device->timer, jiffies + expires))
862 return;
863 }
864 device->timer.function = dasd_timeout_device;
865 device->timer.data = (unsigned long) device;
866 device->timer.expires = jiffies + expires;
867 add_timer(&device->timer);
868}
869
870/*
871 * Clear timeout for a device.
872 */
873void
874dasd_clear_timer(struct dasd_device *device)
875{
876 if (timer_pending(&device->timer))
877 del_timer(&device->timer);
878}
879
880static void
881dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
882{
883 struct dasd_ccw_req *cqr;
884 struct dasd_device *device;
885
886 cqr = (struct dasd_ccw_req *) intparm;
887 if (cqr->status != DASD_CQR_IN_IO) {
888 MESSAGE(KERN_DEBUG,
889 "invalid status in handle_killed_request: "
890 "bus_id %s, status %02x",
891 cdev->dev.bus_id, cqr->status);
892 return;
893 }
894
895 device = (struct dasd_device *) cqr->device;
896 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200897 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
899 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
900 cdev->dev.bus_id);
901 return;
902 }
903
904 /* Schedule request to be retried. */
905 cqr->status = DASD_CQR_QUEUED;
906
907 dasd_clear_timer(device);
908 dasd_schedule_bh(device);
909 dasd_put_device(device);
910}
911
912static void
913dasd_handle_state_change_pending(struct dasd_device *device)
914{
915 struct dasd_ccw_req *cqr;
916 struct list_head *l, *n;
917
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800918 /* First of all start sense subsystem status request. */
919 dasd_eer_snss(device);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 device->stopped &= ~DASD_STOPPED_PENDING;
922
923 /* restart all 'running' IO on queue */
924 list_for_each_safe(l, n, &device->ccw_queue) {
925 cqr = list_entry(l, struct dasd_ccw_req, list);
926 if (cqr->status == DASD_CQR_IN_IO) {
927 cqr->status = DASD_CQR_QUEUED;
928 }
929 }
930 dasd_clear_timer(device);
931 dasd_schedule_bh(device);
932}
933
934/*
935 * Interrupt handler for "normal" ssch-io based dasd devices.
936 */
937void
938dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
939 struct irb *irb)
940{
941 struct dasd_ccw_req *cqr, *next;
942 struct dasd_device *device;
943 unsigned long long now;
944 int expires;
945 dasd_era_t era;
946 char mask;
947
948 if (IS_ERR(irb)) {
949 switch (PTR_ERR(irb)) {
950 case -EIO:
951 dasd_handle_killed_request(cdev, intparm);
952 break;
953 case -ETIMEDOUT:
954 printk(KERN_WARNING"%s(%s): request timed out\n",
955 __FUNCTION__, cdev->dev.bus_id);
956 //FIXME - dasd uses own timeout interface...
957 break;
958 default:
959 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
960 __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
961 }
962 return;
963 }
964
965 now = get_clock();
966
967 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
968 cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
969 (unsigned int) intparm);
970
971 /* first of all check for state change pending interrupt */
972 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
973 if ((irb->scsw.dstat & mask) == mask) {
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200974 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (!IS_ERR(device)) {
976 dasd_handle_state_change_pending(device);
977 dasd_put_device(device);
978 }
979 return;
980 }
981
982 cqr = (struct dasd_ccw_req *) intparm;
983
984 /* check for unsolicited interrupts */
985 if (cqr == NULL) {
986 MESSAGE(KERN_DEBUG,
987 "unsolicited interrupt received: bus_id %s",
988 cdev->dev.bus_id);
989 return;
990 }
991
992 device = (struct dasd_device *) cqr->device;
993 if (device == NULL ||
994 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
995 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
996 cdev->dev.bus_id);
997 return;
998 }
999
1000 /* Check for clear pending */
1001 if (cqr->status == DASD_CQR_CLEAR &&
1002 irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1003 cqr->status = DASD_CQR_QUEUED;
1004 dasd_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001005 wake_up(&dasd_flush_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 dasd_schedule_bh(device);
1007 return;
1008 }
1009
1010 /* check status - the request might have been killed by dyn detach */
1011 if (cqr->status != DASD_CQR_IN_IO) {
1012 MESSAGE(KERN_DEBUG,
1013 "invalid status: bus_id %s, status %02x",
1014 cdev->dev.bus_id, cqr->status);
1015 return;
1016 }
1017 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
1018 ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
1019
1020 /* Find out the appropriate era_action. */
Horst Hummel138c0142006-06-29 14:58:12 +02001021 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 era = dasd_era_fatal;
1023 else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1024 irb->scsw.cstat == 0 &&
1025 !irb->esw.esw0.erw.cons)
1026 era = dasd_era_none;
1027 else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
1028 era = dasd_era_fatal; /* don't recover this request */
1029 else if (irb->esw.esw0.erw.cons)
1030 era = device->discipline->examine_error(cqr, irb);
Horst Hummel138c0142006-06-29 14:58:12 +02001031 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 era = dasd_era_recover;
1033
1034 DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
1035 expires = 0;
1036 if (era == dasd_era_none) {
1037 cqr->status = DASD_CQR_DONE;
1038 cqr->stopclk = now;
1039 /* Start first request on queue if possible -> fast_io. */
1040 if (cqr->list.next != &device->ccw_queue) {
1041 next = list_entry(cqr->list.next,
1042 struct dasd_ccw_req, list);
1043 if ((next->status == DASD_CQR_QUEUED) &&
1044 (!device->stopped)) {
1045 if (device->discipline->start_IO(next) == 0)
1046 expires = next->expires;
1047 else
1048 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1049 "Interrupt fastpath "
1050 "failed!");
1051 }
1052 }
1053 } else { /* error */
1054 memcpy(&cqr->irb, irb, sizeof (struct irb));
1055#ifdef ERP_DEBUG
1056 /* dump sense data */
1057 dasd_log_sense(cqr, irb);
1058#endif
1059 switch (era) {
1060 case dasd_era_fatal:
1061 cqr->status = DASD_CQR_FAILED;
1062 cqr->stopclk = now;
1063 break;
1064 case dasd_era_recover:
1065 cqr->status = DASD_CQR_ERROR;
1066 break;
1067 default:
1068 BUG();
1069 }
1070 }
1071 if (expires != 0)
1072 dasd_set_timer(device, expires);
1073 else
1074 dasd_clear_timer(device);
1075 dasd_schedule_bh(device);
1076}
1077
1078/*
1079 * posts the buffer_cache about a finalized request
1080 */
1081static inline void
1082dasd_end_request(struct request *req, int uptodate)
1083{
1084 if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1085 BUG();
1086 add_disk_randomness(req->rq_disk);
Tejun Heo8ffdc652006-01-06 09:49:03 +01001087 end_that_request_last(req, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090/*
1091 * Process finished error recovery ccw.
1092 */
1093static inline void
1094__dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1095{
1096 dasd_erp_fn_t erp_fn;
1097
1098 if (cqr->status == DASD_CQR_DONE)
1099 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1100 else
1101 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1102 erp_fn = device->discipline->erp_postaction(cqr);
1103 erp_fn(cqr);
1104}
1105
1106/*
1107 * Process ccw request queue.
1108 */
1109static inline void
1110__dasd_process_ccw_queue(struct dasd_device * device,
1111 struct list_head *final_queue)
1112{
1113 struct list_head *l, *n;
1114 struct dasd_ccw_req *cqr;
1115 dasd_erp_fn_t erp_fn;
1116
1117restart:
1118 /* Process request with final status. */
1119 list_for_each_safe(l, n, &device->ccw_queue) {
1120 cqr = list_entry(l, struct dasd_ccw_req, list);
1121 /* Stop list processing at the first non-final request. */
1122 if (cqr->status != DASD_CQR_DONE &&
1123 cqr->status != DASD_CQR_FAILED &&
1124 cqr->status != DASD_CQR_ERROR)
1125 break;
1126 /* Process requests with DASD_CQR_ERROR */
1127 if (cqr->status == DASD_CQR_ERROR) {
1128 if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1129 cqr->status = DASD_CQR_FAILED;
1130 cqr->stopclk = get_clock();
1131 } else {
1132 if (cqr->irb.esw.esw0.erw.cons) {
1133 erp_fn = device->discipline->
1134 erp_action(cqr);
1135 erp_fn(cqr);
1136 } else
1137 dasd_default_erp_action(cqr);
1138 }
1139 goto restart;
1140 }
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001141
1142 /* First of all call extended error reporting. */
1143 if (dasd_eer_enabled(device) &&
1144 cqr->status == DASD_CQR_FAILED) {
1145 dasd_eer_write(device, cqr, DASD_EER_FATALERROR);
1146
1147 /* restart request */
1148 cqr->status = DASD_CQR_QUEUED;
1149 cqr->retries = 255;
1150 device->stopped |= DASD_STOPPED_QUIESCE;
1151 goto restart;
1152 }
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /* Process finished ERP request. */
1155 if (cqr->refers) {
1156 __dasd_process_erp(device, cqr);
1157 goto restart;
1158 }
1159
1160 /* Rechain finished requests to final queue */
1161 cqr->endclk = get_clock();
1162 list_move_tail(&cqr->list, final_queue);
1163 }
1164}
1165
1166static void
1167dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1168{
1169 struct request *req;
1170 struct dasd_device *device;
1171 int status;
1172
1173 req = (struct request *) data;
1174 device = cqr->device;
1175 dasd_profile_end(device, cqr, req);
1176 status = cqr->device->discipline->free_cp(cqr,req);
1177 spin_lock_irq(&device->request_queue_lock);
1178 dasd_end_request(req, status);
1179 spin_unlock_irq(&device->request_queue_lock);
1180}
1181
1182
1183/*
1184 * Fetch requests from the block device queue.
1185 */
1186static inline void
1187__dasd_process_blk_queue(struct dasd_device * device)
1188{
1189 request_queue_t *queue;
1190 struct request *req;
1191 struct dasd_ccw_req *cqr;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001192 int nr_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 queue = device->request_queue;
1195 /* No queue ? Then there is nothing to do. */
1196 if (queue == NULL)
1197 return;
1198
1199 /*
1200 * We requeue request from the block device queue to the ccw
1201 * queue only in two states. In state DASD_STATE_READY the
1202 * partition detection is done and we need to requeue requests
1203 * for that. State DASD_STATE_ONLINE is normal block device
1204 * operation.
1205 */
1206 if (device->state != DASD_STATE_READY &&
1207 device->state != DASD_STATE_ONLINE)
1208 return;
1209 nr_queued = 0;
1210 /* Now we try to fetch requests from the request queue */
1211 list_for_each_entry(cqr, &device->ccw_queue, list)
1212 if (cqr->status == DASD_CQR_QUEUED)
1213 nr_queued++;
1214 while (!blk_queue_plugged(queue) &&
1215 elv_next_request(queue) &&
1216 nr_queued < DASD_CHANQ_MAX_SIZE) {
1217 req = elv_next_request(queue);
Horst Hummelf24acd42005-05-01 08:58:59 -07001218
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001219 if (device->features & DASD_FEATURE_READONLY &&
1220 rq_data_dir(req) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 DBF_DEV_EVENT(DBF_ERR, device,
1222 "Rejecting write request %p",
1223 req);
1224 blkdev_dequeue_request(req);
1225 dasd_end_request(req, 0);
1226 continue;
1227 }
1228 if (device->stopped & DASD_STOPPED_DC_EIO) {
1229 blkdev_dequeue_request(req);
1230 dasd_end_request(req, 0);
1231 continue;
1232 }
1233 cqr = device->discipline->build_cp(device, req);
1234 if (IS_ERR(cqr)) {
1235 if (PTR_ERR(cqr) == -ENOMEM)
1236 break; /* terminate request queue loop */
1237 DBF_DEV_EVENT(DBF_ERR, device,
1238 "CCW creation failed (rc=%ld) "
1239 "on request %p",
1240 PTR_ERR(cqr), req);
1241 blkdev_dequeue_request(req);
1242 dasd_end_request(req, 0);
1243 continue;
1244 }
1245 cqr->callback = dasd_end_request_cb;
1246 cqr->callback_data = (void *) req;
1247 cqr->status = DASD_CQR_QUEUED;
1248 blkdev_dequeue_request(req);
1249 list_add_tail(&cqr->list, &device->ccw_queue);
1250 dasd_profile_start(device, cqr, req);
1251 nr_queued++;
1252 }
1253}
1254
1255/*
1256 * Take a look at the first request on the ccw queue and check
1257 * if it reached its expire time. If so, terminate the IO.
1258 */
1259static inline void
1260__dasd_check_expire(struct dasd_device * device)
1261{
1262 struct dasd_ccw_req *cqr;
1263
1264 if (list_empty(&device->ccw_queue))
1265 return;
1266 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Horst Hummel29145a62006-12-04 15:40:15 +01001267 if ((cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) &&
1268 (time_after_eq(jiffies, cqr->expires + cqr->starttime))) {
1269 if (device->discipline->term_IO(cqr) != 0) {
1270 /* Hmpf, try again in 5 sec */
1271 dasd_set_timer(device, 5*HZ);
1272 DEV_MESSAGE(KERN_ERR, device,
1273 "internal error - timeout (%is) expired "
1274 "for cqr %p, termination failed, "
1275 "retrying in 5s",
1276 (cqr->expires/HZ), cqr);
1277 } else {
Horst Hummel8f617012006-08-30 14:33:33 +02001278 DEV_MESSAGE(KERN_ERR, device,
1279 "internal error - timeout (%is) expired "
1280 "for cqr %p (%i retries left)",
1281 (cqr->expires/HZ), cqr, cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 }
1284}
1285
1286/*
1287 * Take a look at the first request on the ccw queue and check
1288 * if it needs to be started.
1289 */
1290static inline void
1291__dasd_start_head(struct dasd_device * device)
1292{
1293 struct dasd_ccw_req *cqr;
1294 int rc;
1295
1296 if (list_empty(&device->ccw_queue))
1297 return;
1298 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001299 if (cqr->status != DASD_CQR_QUEUED)
1300 return;
1301 /* Non-temporary stop condition will trigger fail fast */
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001302 if (device->stopped & ~DASD_STOPPED_PENDING &&
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001303 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1304 (!dasd_eer_enabled(device))) {
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001305 cqr->status = DASD_CQR_FAILED;
1306 dasd_schedule_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001307 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001308 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001309 /* Don't try to start requests if device is stopped */
1310 if (device->stopped)
1311 return;
1312
1313 rc = device->discipline->start_IO(cqr);
1314 if (rc == 0)
1315 dasd_set_timer(device, cqr->expires);
1316 else if (rc == -EACCES) {
1317 dasd_schedule_bh(device);
1318 } else
1319 /* Hmpf, try again in 1/2 sec */
1320 dasd_set_timer(device, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Horst Hummel8f617012006-08-30 14:33:33 +02001323static inline int
1324_wait_for_clear(struct dasd_ccw_req *cqr)
1325{
1326 return (cqr->status == DASD_CQR_QUEUED);
1327}
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329/*
Horst Hummel8f617012006-08-30 14:33:33 +02001330 * Remove all requests from the ccw queue (all = '1') or only block device
1331 * requests in case all = '0'.
1332 * Take care of the erp-chain (chained via cqr->refers) and remove either
1333 * the whole erp-chain or none of the erp-requests.
1334 * If a request is currently running, term_IO is called and the request
1335 * is re-queued. Prior to removing the terminated request we need to wait
1336 * for the clear-interrupt.
1337 * In case termination is not possible we stop processing and just finishing
1338 * the already moved requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Horst Hummel8f617012006-08-30 14:33:33 +02001340static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341dasd_flush_ccw_queue(struct dasd_device * device, int all)
1342{
Horst Hummel8f617012006-08-30 14:33:33 +02001343 struct dasd_ccw_req *cqr, *orig, *n;
1344 int rc, i;
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 INIT_LIST_HEAD(&flush_queue);
1349 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001350 rc = 0;
1351restart:
1352 list_for_each_entry_safe(cqr, n, &device->ccw_queue, list) {
1353 /* get original request of erp request-chain */
1354 for (orig = cqr; orig->refers != NULL; orig = orig->refers);
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* Flush all request or only block device requests? */
Horst Hummel8f617012006-08-30 14:33:33 +02001357 if (all == 0 && cqr->callback != dasd_end_request_cb &&
1358 orig->callback != dasd_end_request_cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 continue;
Horst Hummel8f617012006-08-30 14:33:33 +02001360 }
1361 /* Check status and move request to flush_queue */
1362 switch (cqr->status) {
1363 case DASD_CQR_IN_IO:
1364 rc = device->discipline->term_IO(cqr);
1365 if (rc) {
1366 /* unable to terminate requeust */
1367 DEV_MESSAGE(KERN_ERR, device,
1368 "dasd flush ccw_queue is unable "
1369 " to terminate request %p",
1370 cqr);
1371 /* stop flush processing */
1372 goto finished;
1373 }
1374 break;
1375 case DASD_CQR_QUEUED:
1376 case DASD_CQR_ERROR:
1377 /* set request to FAILED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001379 cqr->status = DASD_CQR_FAILED;
1380 break;
1381 default: /* do not touch the others */
1382 break;
1383 }
1384 /* Rechain request (including erp chain) */
1385 for (i = 0; cqr != NULL; cqr = cqr->refers, i++) {
1386 cqr->endclk = get_clock();
1387 list_move_tail(&cqr->list, &flush_queue);
1388 }
1389 if (i > 1)
1390 /* moved more than one request - need to restart */
1391 goto restart;
1392 }
1393
1394finished:
1395 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1396 /* Now call the callback function of flushed requests */
1397restart_cb:
1398 list_for_each_entry_safe(cqr, n, &flush_queue, list) {
1399 if (cqr->status == DASD_CQR_CLEAR) {
1400 /* wait for clear interrupt! */
1401 wait_event(dasd_flush_wq, _wait_for_clear(cqr));
1402 cqr->status = DASD_CQR_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404 /* Process finished ERP request. */
1405 if (cqr->refers) {
1406 __dasd_process_erp(device, cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001407 /* restart list_for_xx loop since dasd_process_erp
1408 * might remove multiple elements */
1409 goto restart_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
Horst Hummel8f617012006-08-30 14:33:33 +02001411 /* call the callback function */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 cqr->endclk = get_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (cqr->callback != NULL)
1414 (cqr->callback)(cqr, cqr->callback_data);
1415 }
Horst Hummel8f617012006-08-30 14:33:33 +02001416 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419/*
1420 * Acquire the device lock and process queues for the device.
1421 */
1422static void
1423dasd_tasklet(struct dasd_device * device)
1424{
1425 struct list_head final_queue;
1426 struct list_head *l, *n;
1427 struct dasd_ccw_req *cqr;
1428
1429 atomic_set (&device->tasklet_scheduled, 0);
1430 INIT_LIST_HEAD(&final_queue);
1431 spin_lock_irq(get_ccwdev_lock(device->cdev));
1432 /* Check expire time of first request on the ccw queue. */
1433 __dasd_check_expire(device);
1434 /* Finish off requests on ccw queue */
1435 __dasd_process_ccw_queue(device, &final_queue);
1436 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1437 /* Now call the callback function of requests with final status */
1438 list_for_each_safe(l, n, &final_queue) {
1439 cqr = list_entry(l, struct dasd_ccw_req, list);
Horst Hummelc2ba4442006-02-01 03:06:37 -08001440 list_del_init(&cqr->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (cqr->callback != NULL)
1442 (cqr->callback)(cqr, cqr->callback_data);
1443 }
1444 spin_lock_irq(&device->request_queue_lock);
1445 spin_lock(get_ccwdev_lock(device->cdev));
1446 /* Get new request from the block device request queue */
1447 __dasd_process_blk_queue(device);
1448 /* Now check if the head of the ccw queue needs to be started. */
1449 __dasd_start_head(device);
1450 spin_unlock(get_ccwdev_lock(device->cdev));
1451 spin_unlock_irq(&device->request_queue_lock);
1452 dasd_put_device(device);
1453}
1454
1455/*
1456 * Schedules a call to dasd_tasklet over the device tasklet.
1457 */
1458void
1459dasd_schedule_bh(struct dasd_device * device)
1460{
1461 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001462 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return;
1464 dasd_get_device(device);
1465 tasklet_hi_schedule(&device->tasklet);
1466}
1467
1468/*
1469 * Queue a request to the head of the ccw_queue. Start the I/O if
1470 * possible.
1471 */
1472void
1473dasd_add_request_head(struct dasd_ccw_req *req)
1474{
1475 struct dasd_device *device;
1476 unsigned long flags;
1477
1478 device = req->device;
1479 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1480 req->status = DASD_CQR_QUEUED;
1481 req->device = device;
1482 list_add(&req->list, &device->ccw_queue);
1483 /* let the bh start the request to keep them in order */
1484 dasd_schedule_bh(device);
1485 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1486}
1487
1488/*
1489 * Queue a request to the tail of the ccw_queue. Start the I/O if
1490 * possible.
1491 */
1492void
1493dasd_add_request_tail(struct dasd_ccw_req *req)
1494{
1495 struct dasd_device *device;
1496 unsigned long flags;
1497
1498 device = req->device;
1499 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1500 req->status = DASD_CQR_QUEUED;
1501 req->device = device;
1502 list_add_tail(&req->list, &device->ccw_queue);
1503 /* let the bh start the request to keep them in order */
1504 dasd_schedule_bh(device);
1505 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1506}
1507
1508/*
1509 * Wakeup callback.
1510 */
1511static void
1512dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1513{
1514 wake_up((wait_queue_head_t *) data);
1515}
1516
1517static inline int
1518_wait_for_wakeup(struct dasd_ccw_req *cqr)
1519{
1520 struct dasd_device *device;
1521 int rc;
1522
1523 device = cqr->device;
1524 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001525 rc = ((cqr->status == DASD_CQR_DONE ||
1526 cqr->status == DASD_CQR_FAILED) &&
1527 list_empty(&cqr->list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1529 return rc;
1530}
1531
1532/*
1533 * Attempts to start a special ccw queue and waits for its completion.
1534 */
1535int
1536dasd_sleep_on(struct dasd_ccw_req * cqr)
1537{
1538 wait_queue_head_t wait_q;
1539 struct dasd_device *device;
1540 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 device = cqr->device;
1543 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel138c0142006-06-29 14:58:12 +02001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 init_waitqueue_head (&wait_q);
1546 cqr->callback = dasd_wakeup_cb;
1547 cqr->callback_data = (void *) &wait_q;
1548 cqr->status = DASD_CQR_QUEUED;
1549 list_add_tail(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /* let the bh start the request to keep them in order */
1552 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1555
1556 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* Request status is either done or failed. */
1559 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1560 return rc;
1561}
1562
1563/*
1564 * Attempts to start a special ccw queue and wait interruptible
1565 * for its completion.
1566 */
1567int
1568dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1569{
1570 wait_queue_head_t wait_q;
1571 struct dasd_device *device;
1572 int rc, finished;
1573
1574 device = cqr->device;
1575 spin_lock_irq(get_ccwdev_lock(device->cdev));
1576
1577 init_waitqueue_head (&wait_q);
1578 cqr->callback = dasd_wakeup_cb;
1579 cqr->callback_data = (void *) &wait_q;
1580 cqr->status = DASD_CQR_QUEUED;
1581 list_add_tail(&cqr->list, &device->ccw_queue);
1582
1583 /* let the bh start the request to keep them in order */
1584 dasd_schedule_bh(device);
1585 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1586
1587 finished = 0;
1588 while (!finished) {
1589 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1590 if (rc != -ERESTARTSYS) {
Horst Hummelc2ba4442006-02-01 03:06:37 -08001591 /* Request is final (done or failed) */
1592 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 break;
1594 }
1595 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001596 switch (cqr->status) {
1597 case DASD_CQR_IN_IO:
1598 /* terminate runnig cqr */
1599 if (device->discipline->term_IO) {
1600 cqr->retries = -1;
1601 device->discipline->term_IO(cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001602 /* wait (non-interruptible) for final status
1603 * because signal ist still pending */
Horst Hummelc2ba4442006-02-01 03:06:37 -08001604 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1605 wait_event(wait_q, _wait_for_wakeup(cqr));
1606 spin_lock_irq(get_ccwdev_lock(device->cdev));
1607 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1608 finished = 1;
1609 }
1610 break;
1611 case DASD_CQR_QUEUED:
1612 /* request */
1613 list_del_init(&cqr->list);
1614 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 finished = 1;
Horst Hummelc2ba4442006-02-01 03:06:37 -08001616 break;
1617 default:
1618 /* cqr with 'non-interruptable' status - just wait */
1619 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1622 }
1623 return rc;
1624}
1625
1626/*
1627 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1628 * for eckd devices) the currently running request has to be terminated
1629 * and be put back to status queued, before the special request is added
1630 * to the head of the queue. Then the special request is waited on normally.
1631 */
1632static inline int
1633_dasd_term_running_cqr(struct dasd_device *device)
1634{
1635 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 if (list_empty(&device->ccw_queue))
1638 return 0;
1639 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Horst Hummel8f617012006-08-30 14:33:33 +02001640 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643int
1644dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1645{
1646 wait_queue_head_t wait_q;
1647 struct dasd_device *device;
1648 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 device = cqr->device;
1651 spin_lock_irq(get_ccwdev_lock(device->cdev));
1652 rc = _dasd_term_running_cqr(device);
1653 if (rc) {
1654 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1655 return rc;
1656 }
Horst Hummel138c0142006-06-29 14:58:12 +02001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 init_waitqueue_head (&wait_q);
1659 cqr->callback = dasd_wakeup_cb;
1660 cqr->callback_data = (void *) &wait_q;
1661 cqr->status = DASD_CQR_QUEUED;
1662 list_add(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /* let the bh start the request to keep them in order */
1665 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1668
1669 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /* Request status is either done or failed. */
1672 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1673 return rc;
1674}
1675
1676/*
1677 * Cancels a request that was started with dasd_sleep_on_req.
1678 * This is useful to timeout requests. The request will be
1679 * terminated if it is currently in i/o.
1680 * Returns 1 if the request has been terminated.
1681 */
1682int
1683dasd_cancel_req(struct dasd_ccw_req *cqr)
1684{
1685 struct dasd_device *device = cqr->device;
1686 unsigned long flags;
1687 int rc;
1688
1689 rc = 0;
1690 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1691 switch (cqr->status) {
1692 case DASD_CQR_QUEUED:
1693 /* request was not started - just set to failed */
1694 cqr->status = DASD_CQR_FAILED;
1695 break;
1696 case DASD_CQR_IN_IO:
1697 /* request in IO - terminate IO and release again */
1698 if (device->discipline->term_IO(cqr) != 0)
1699 /* what to do if unable to terminate ??????
1700 e.g. not _IN_IO */
1701 cqr->status = DASD_CQR_FAILED;
1702 cqr->stopclk = get_clock();
1703 rc = 1;
1704 break;
1705 case DASD_CQR_DONE:
1706 case DASD_CQR_FAILED:
1707 /* already finished - do nothing */
1708 break;
1709 default:
1710 DEV_MESSAGE(KERN_ALERT, device,
1711 "invalid status %02x in request",
1712 cqr->status);
1713 BUG();
1714
1715 }
1716 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1717 dasd_schedule_bh(device);
1718 return rc;
1719}
1720
1721/*
1722 * SECTION: Block device operations (request queue, partitions, open, release).
1723 */
1724
1725/*
1726 * Dasd request queue function. Called from ll_rw_blk.c
1727 */
1728static void
1729do_dasd_request(request_queue_t * queue)
1730{
1731 struct dasd_device *device;
1732
1733 device = (struct dasd_device *) queue->queuedata;
1734 spin_lock(get_ccwdev_lock(device->cdev));
1735 /* Get new request from the block device request queue */
1736 __dasd_process_blk_queue(device);
1737 /* Now check if the head of the ccw queue needs to be started. */
1738 __dasd_start_head(device);
1739 spin_unlock(get_ccwdev_lock(device->cdev));
1740}
1741
1742/*
1743 * Allocate and initialize request queue and default I/O scheduler.
1744 */
1745static int
1746dasd_alloc_queue(struct dasd_device * device)
1747{
1748 int rc;
1749
1750 device->request_queue = blk_init_queue(do_dasd_request,
1751 &device->request_queue_lock);
1752 if (device->request_queue == NULL)
1753 return -ENOMEM;
1754
1755 device->request_queue->queuedata = device;
1756
1757 elevator_exit(device->request_queue->elevator);
1758 rc = elevator_init(device->request_queue, "deadline");
1759 if (rc) {
1760 blk_cleanup_queue(device->request_queue);
1761 return rc;
1762 }
1763 return 0;
1764}
1765
1766/*
1767 * Allocate and initialize request queue.
1768 */
1769static void
1770dasd_setup_queue(struct dasd_device * device)
1771{
1772 int max;
1773
1774 blk_queue_hardsect_size(device->request_queue, device->bp_block);
1775 max = device->discipline->max_blocks << device->s2b_shift;
1776 blk_queue_max_sectors(device->request_queue, max);
1777 blk_queue_max_phys_segments(device->request_queue, -1L);
1778 blk_queue_max_hw_segments(device->request_queue, -1L);
1779 blk_queue_max_segment_size(device->request_queue, -1L);
1780 blk_queue_segment_boundary(device->request_queue, -1L);
Heiko Carstensed68cb32006-01-14 13:21:05 -08001781 blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
1784/*
1785 * Deactivate and free request queue.
1786 */
1787static void
1788dasd_free_queue(struct dasd_device * device)
1789{
1790 if (device->request_queue) {
1791 blk_cleanup_queue(device->request_queue);
1792 device->request_queue = NULL;
1793 }
1794}
1795
1796/*
1797 * Flush request on the request queue.
1798 */
1799static void
1800dasd_flush_request_queue(struct dasd_device * device)
1801{
1802 struct request *req;
1803
1804 if (!device->request_queue)
1805 return;
Horst Hummel138c0142006-06-29 14:58:12 +02001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 spin_lock_irq(&device->request_queue_lock);
Horst Hummel8f617012006-08-30 14:33:33 +02001808 while ((req = elv_next_request(device->request_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 blkdev_dequeue_request(req);
Horst Hummelebc45992006-08-09 10:30:22 +02001810 dasd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 spin_unlock_irq(&device->request_queue_lock);
1813}
1814
1815static int
1816dasd_open(struct inode *inp, struct file *filp)
1817{
1818 struct gendisk *disk = inp->i_bdev->bd_disk;
1819 struct dasd_device *device = disk->private_data;
1820 int rc;
1821
1822 atomic_inc(&device->open_count);
1823 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1824 rc = -ENODEV;
1825 goto unlock;
1826 }
1827
1828 if (!try_module_get(device->discipline->owner)) {
1829 rc = -EINVAL;
1830 goto unlock;
1831 }
1832
1833 if (dasd_probeonly) {
1834 DEV_MESSAGE(KERN_INFO, device, "%s",
1835 "No access to device due to probeonly mode");
1836 rc = -EPERM;
1837 goto out;
1838 }
1839
Horst Hummel90f00942006-03-07 21:55:39 -08001840 if (device->state <= DASD_STATE_BASIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1842 " Cannot open unrecognized device");
1843 rc = -ENODEV;
1844 goto out;
1845 }
1846
1847 return 0;
1848
1849out:
1850 module_put(device->discipline->owner);
1851unlock:
1852 atomic_dec(&device->open_count);
1853 return rc;
1854}
1855
1856static int
1857dasd_release(struct inode *inp, struct file *filp)
1858{
1859 struct gendisk *disk = inp->i_bdev->bd_disk;
1860 struct dasd_device *device = disk->private_data;
1861
1862 atomic_dec(&device->open_count);
1863 module_put(device->discipline->owner);
1864 return 0;
1865}
1866
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001867/*
1868 * Return disk geometry.
1869 */
1870static int
1871dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1872{
1873 struct dasd_device *device;
1874
1875 device = bdev->bd_disk->private_data;
1876 if (!device)
1877 return -ENODEV;
1878
1879 if (!device->discipline ||
1880 !device->discipline->fill_geometry)
1881 return -EINVAL;
1882
1883 device->discipline->fill_geometry(device, geo);
1884 geo->start = get_start_sect(bdev) >> device->s2b_shift;
1885 return 0;
1886}
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888struct block_device_operations
1889dasd_device_operations = {
1890 .owner = THIS_MODULE,
1891 .open = dasd_open,
1892 .release = dasd_release,
1893 .ioctl = dasd_ioctl,
Christoph Hellwig82620372006-01-09 20:52:12 -08001894 .compat_ioctl = dasd_compat_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001895 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896};
1897
1898
1899static void
1900dasd_exit(void)
1901{
1902#ifdef CONFIG_PROC_FS
1903 dasd_proc_exit();
1904#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001905 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07001906 if (dasd_page_cache != NULL) {
1907 kmem_cache_destroy(dasd_page_cache);
1908 dasd_page_cache = NULL;
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 dasd_gendisk_exit();
1911 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (dasd_debug_area != NULL) {
1913 debug_unregister(dasd_debug_area);
1914 dasd_debug_area = NULL;
1915 }
1916}
1917
1918/*
1919 * SECTION: common functions for ccw_driver use
1920 */
1921
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001922/*
1923 * Initial attempt at a probe function. this can be simplified once
1924 * the other detection code is gone.
1925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926int
1927dasd_generic_probe (struct ccw_device *cdev,
1928 struct dasd_discipline *discipline)
1929{
1930 int ret;
1931
Horst Hummel40545572006-06-29 15:08:18 +02001932 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
1933 if (ret) {
1934 printk(KERN_WARNING
1935 "dasd_generic_probe: could not set ccw-device options "
1936 "for %s\n", cdev->dev.bus_id);
1937 return ret;
1938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 ret = dasd_add_sysfs_files(cdev);
1940 if (ret) {
1941 printk(KERN_WARNING
1942 "dasd_generic_probe: could not add sysfs entries "
1943 "for %s\n", cdev->dev.bus_id);
Horst Hummel40545572006-06-29 15:08:18 +02001944 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Horst Hummel40545572006-06-29 15:08:18 +02001946 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Horst Hummel40545572006-06-29 15:08:18 +02001948 /*
1949 * Automatically online either all dasd devices (dasd_autodetect)
1950 * or all devices specified with dasd= parameters during
1951 * initial probe.
1952 */
1953 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
1954 (dasd_autodetect && dasd_busid_known(cdev->dev.bus_id) != 0))
1955 ret = ccw_device_set_online(cdev);
1956 if (ret)
1957 printk(KERN_WARNING
1958 "dasd_generic_probe: could not initially online "
1959 "ccw-device %s\n", cdev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return ret;
1961}
1962
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001963/*
1964 * This will one day be called from a global not_oper handler.
1965 * It is also used by driver_unregister during module unload.
1966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967void
1968dasd_generic_remove (struct ccw_device *cdev)
1969{
1970 struct dasd_device *device;
1971
Horst Hummel59afda72005-05-16 21:53:39 -07001972 cdev->handler = NULL;
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 dasd_remove_sysfs_files(cdev);
1975 device = dasd_device_from_cdev(cdev);
1976 if (IS_ERR(device))
1977 return;
1978 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1979 /* Already doing offline processing */
1980 dasd_put_device(device);
1981 return;
1982 }
1983 /*
1984 * This device is removed unconditionally. Set offline
1985 * flag to prevent dasd_open from opening it while it is
1986 * no quite down yet.
1987 */
1988 dasd_set_target_state(device, DASD_STATE_NEW);
1989 /* dasd_delete_device destroys the device reference. */
1990 dasd_delete_device(device);
1991}
1992
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001993/*
1994 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001996 * or the user has started activation through sysfs.
1997 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998int
1999dasd_generic_set_online (struct ccw_device *cdev,
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002000 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002003 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002005 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002006
Horst Hummel40545572006-06-29 15:08:18 +02002007 /* first online clears initial online feature flag */
2008 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 device = dasd_create_device(cdev);
2010 if (IS_ERR(device))
2011 return PTR_ERR(device);
2012
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002013 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002014 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (!dasd_diag_discipline_pointer) {
2016 printk (KERN_WARNING
2017 "dasd_generic couldn't online device %s "
2018 "- discipline DIAG not available\n",
2019 cdev->dev.bus_id);
2020 dasd_delete_device(device);
2021 return -ENODEV;
2022 }
2023 discipline = dasd_diag_discipline_pointer;
2024 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002025 if (!try_module_get(base_discipline->owner)) {
2026 dasd_delete_device(device);
2027 return -EINVAL;
2028 }
2029 if (!try_module_get(discipline->owner)) {
2030 module_put(base_discipline->owner);
2031 dasd_delete_device(device);
2032 return -EINVAL;
2033 }
2034 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 device->discipline = discipline;
2036
2037 rc = discipline->check_device(device);
2038 if (rc) {
2039 printk (KERN_WARNING
2040 "dasd_generic couldn't online device %s "
2041 "with discipline %s rc=%i\n",
2042 cdev->dev.bus_id, discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002043 module_put(discipline->owner);
2044 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 dasd_delete_device(device);
2046 return rc;
2047 }
2048
2049 dasd_set_target_state(device, DASD_STATE_ONLINE);
2050 if (device->state <= DASD_STATE_KNOWN) {
2051 printk (KERN_WARNING
2052 "dasd_generic discipline not found for %s\n",
2053 cdev->dev.bus_id);
2054 rc = -ENODEV;
2055 dasd_set_target_state(device, DASD_STATE_NEW);
2056 dasd_delete_device(device);
2057 } else
2058 pr_debug("dasd_generic device %s found\n",
2059 cdev->dev.bus_id);
2060
2061 /* FIXME: we have to wait for the root device but we don't want
2062 * to wait for each single device but for all at once. */
2063 wait_event(dasd_init_waitq, _wait_for_device(device));
2064
2065 dasd_put_device(device);
2066
2067 return rc;
2068}
2069
2070int
2071dasd_generic_set_offline (struct ccw_device *cdev)
2072{
2073 struct dasd_device *device;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002074 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 device = dasd_device_from_cdev(cdev);
2077 if (IS_ERR(device))
2078 return PTR_ERR(device);
2079 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2080 /* Already doing offline processing */
2081 dasd_put_device(device);
2082 return 0;
2083 }
2084 /*
2085 * We must make sure that this device is currently not in use.
2086 * The open_count is increased for every opener, that includes
2087 * the blkdev_get in dasd_scan_partitions. We are only interested
2088 * in the other openers.
2089 */
2090 max_count = device->bdev ? 0 : -1;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002091 open_count = (int) atomic_read(&device->open_count);
2092 if (open_count > max_count) {
2093 if (open_count > 0)
2094 printk (KERN_WARNING "Can't offline dasd device with "
2095 "open count = %i.\n",
2096 open_count);
2097 else
2098 printk (KERN_WARNING "%s",
2099 "Can't offline dasd device due to internal "
2100 "use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2102 dasd_put_device(device);
2103 return -EBUSY;
2104 }
2105 dasd_set_target_state(device, DASD_STATE_NEW);
2106 /* dasd_delete_device destroys the device reference. */
2107 dasd_delete_device(device);
2108
2109 return 0;
2110}
2111
2112int
2113dasd_generic_notify(struct ccw_device *cdev, int event)
2114{
2115 struct dasd_device *device;
2116 struct dasd_ccw_req *cqr;
2117 unsigned long flags;
2118 int ret;
2119
2120 device = dasd_device_from_cdev(cdev);
2121 if (IS_ERR(device))
2122 return 0;
2123 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
2124 ret = 0;
2125 switch (event) {
2126 case CIO_GONE:
2127 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002128 /* First of all call extended error reporting. */
2129 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (device->state < DASD_STATE_BASIC)
2132 break;
2133 /* Device is active. We want to keep it. */
2134 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
2135 list_for_each_entry(cqr, &device->ccw_queue, list)
2136 if (cqr->status == DASD_CQR_IN_IO)
2137 cqr->status = DASD_CQR_FAILED;
2138 device->stopped |= DASD_STOPPED_DC_EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 } else {
2140 list_for_each_entry(cqr, &device->ccw_queue, list)
2141 if (cqr->status == DASD_CQR_IN_IO) {
2142 cqr->status = DASD_CQR_QUEUED;
2143 cqr->retries++;
2144 }
2145 device->stopped |= DASD_STOPPED_DC_WAIT;
2146 dasd_set_timer(device, 0);
2147 }
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002148 dasd_schedule_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 ret = 1;
2150 break;
2151 case CIO_OPER:
2152 /* FIXME: add a sanity check. */
2153 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
2154 dasd_schedule_bh(device);
2155 ret = 1;
2156 break;
2157 }
2158 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2159 dasd_put_device(device);
2160 return ret;
2161}
2162
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164static int __init
2165dasd_init(void)
2166{
2167 int rc;
2168
2169 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002170 init_waitqueue_head(&dasd_flush_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Michael Holzheu66a464d2005-06-25 14:55:33 -07002173 dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (dasd_debug_area == NULL) {
2175 rc = -ENOMEM;
2176 goto failed;
2177 }
2178 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002179 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2182
2183 dasd_diag_discipline_pointer = NULL;
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 rc = dasd_devmap_init();
2186 if (rc)
2187 goto failed;
2188 rc = dasd_gendisk_init();
2189 if (rc)
2190 goto failed;
2191 rc = dasd_parse();
2192 if (rc)
2193 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002194 rc = dasd_eer_init();
2195 if (rc)
2196 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197#ifdef CONFIG_PROC_FS
2198 rc = dasd_proc_init();
2199 if (rc)
2200 goto failed;
2201#endif
2202
2203 return 0;
2204failed:
2205 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2206 dasd_exit();
2207 return rc;
2208}
2209
2210module_init(dasd_init);
2211module_exit(dasd_exit);
2212
2213EXPORT_SYMBOL(dasd_debug_area);
2214EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2215
2216EXPORT_SYMBOL(dasd_add_request_head);
2217EXPORT_SYMBOL(dasd_add_request_tail);
2218EXPORT_SYMBOL(dasd_cancel_req);
2219EXPORT_SYMBOL(dasd_clear_timer);
2220EXPORT_SYMBOL(dasd_enable_device);
2221EXPORT_SYMBOL(dasd_int_handler);
2222EXPORT_SYMBOL(dasd_kfree_request);
2223EXPORT_SYMBOL(dasd_kick_device);
2224EXPORT_SYMBOL(dasd_kmalloc_request);
2225EXPORT_SYMBOL(dasd_schedule_bh);
2226EXPORT_SYMBOL(dasd_set_target_state);
2227EXPORT_SYMBOL(dasd_set_timer);
2228EXPORT_SYMBOL(dasd_sfree_request);
2229EXPORT_SYMBOL(dasd_sleep_on);
2230EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2231EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2232EXPORT_SYMBOL(dasd_smalloc_request);
2233EXPORT_SYMBOL(dasd_start_IO);
2234EXPORT_SYMBOL(dasd_term_IO);
2235
2236EXPORT_SYMBOL_GPL(dasd_generic_probe);
2237EXPORT_SYMBOL_GPL(dasd_generic_remove);
2238EXPORT_SYMBOL_GPL(dasd_generic_notify);
2239EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2240EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241