blob: 79ffef6bfaf8a02a802659392e599bbd11f1e468 [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);
1267 if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
1268 if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
Horst Hummel8f617012006-08-30 14:33:33 +02001269 DEV_MESSAGE(KERN_ERR, device,
1270 "internal error - timeout (%is) expired "
1271 "for cqr %p (%i retries left)",
1272 (cqr->expires/HZ), cqr, cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (device->discipline->term_IO(cqr) != 0)
1274 /* Hmpf, try again in 1/10 sec */
1275 dasd_set_timer(device, 10);
1276 }
1277 }
1278}
1279
1280/*
1281 * Take a look at the first request on the ccw queue and check
1282 * if it needs to be started.
1283 */
1284static inline void
1285__dasd_start_head(struct dasd_device * device)
1286{
1287 struct dasd_ccw_req *cqr;
1288 int rc;
1289
1290 if (list_empty(&device->ccw_queue))
1291 return;
1292 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001293 if (cqr->status != DASD_CQR_QUEUED)
1294 return;
1295 /* Non-temporary stop condition will trigger fail fast */
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001296 if (device->stopped & ~DASD_STOPPED_PENDING &&
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001297 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1298 (!dasd_eer_enabled(device))) {
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001299 cqr->status = DASD_CQR_FAILED;
1300 dasd_schedule_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001301 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001302 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001303 /* Don't try to start requests if device is stopped */
1304 if (device->stopped)
1305 return;
1306
1307 rc = device->discipline->start_IO(cqr);
1308 if (rc == 0)
1309 dasd_set_timer(device, cqr->expires);
1310 else if (rc == -EACCES) {
1311 dasd_schedule_bh(device);
1312 } else
1313 /* Hmpf, try again in 1/2 sec */
1314 dasd_set_timer(device, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Horst Hummel8f617012006-08-30 14:33:33 +02001317static inline int
1318_wait_for_clear(struct dasd_ccw_req *cqr)
1319{
1320 return (cqr->status == DASD_CQR_QUEUED);
1321}
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323/*
Horst Hummel8f617012006-08-30 14:33:33 +02001324 * Remove all requests from the ccw queue (all = '1') or only block device
1325 * requests in case all = '0'.
1326 * Take care of the erp-chain (chained via cqr->refers) and remove either
1327 * the whole erp-chain or none of the erp-requests.
1328 * If a request is currently running, term_IO is called and the request
1329 * is re-queued. Prior to removing the terminated request we need to wait
1330 * for the clear-interrupt.
1331 * In case termination is not possible we stop processing and just finishing
1332 * the already moved requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 */
Horst Hummel8f617012006-08-30 14:33:33 +02001334static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335dasd_flush_ccw_queue(struct dasd_device * device, int all)
1336{
Horst Hummel8f617012006-08-30 14:33:33 +02001337 struct dasd_ccw_req *cqr, *orig, *n;
1338 int rc, i;
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 INIT_LIST_HEAD(&flush_queue);
1343 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001344 rc = 0;
1345restart:
1346 list_for_each_entry_safe(cqr, n, &device->ccw_queue, list) {
1347 /* get original request of erp request-chain */
1348 for (orig = cqr; orig->refers != NULL; orig = orig->refers);
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /* Flush all request or only block device requests? */
Horst Hummel8f617012006-08-30 14:33:33 +02001351 if (all == 0 && cqr->callback != dasd_end_request_cb &&
1352 orig->callback != dasd_end_request_cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 continue;
Horst Hummel8f617012006-08-30 14:33:33 +02001354 }
1355 /* Check status and move request to flush_queue */
1356 switch (cqr->status) {
1357 case DASD_CQR_IN_IO:
1358 rc = device->discipline->term_IO(cqr);
1359 if (rc) {
1360 /* unable to terminate requeust */
1361 DEV_MESSAGE(KERN_ERR, device,
1362 "dasd flush ccw_queue is unable "
1363 " to terminate request %p",
1364 cqr);
1365 /* stop flush processing */
1366 goto finished;
1367 }
1368 break;
1369 case DASD_CQR_QUEUED:
1370 case DASD_CQR_ERROR:
1371 /* set request to FAILED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001373 cqr->status = DASD_CQR_FAILED;
1374 break;
1375 default: /* do not touch the others */
1376 break;
1377 }
1378 /* Rechain request (including erp chain) */
1379 for (i = 0; cqr != NULL; cqr = cqr->refers, i++) {
1380 cqr->endclk = get_clock();
1381 list_move_tail(&cqr->list, &flush_queue);
1382 }
1383 if (i > 1)
1384 /* moved more than one request - need to restart */
1385 goto restart;
1386 }
1387
1388finished:
1389 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1390 /* Now call the callback function of flushed requests */
1391restart_cb:
1392 list_for_each_entry_safe(cqr, n, &flush_queue, list) {
1393 if (cqr->status == DASD_CQR_CLEAR) {
1394 /* wait for clear interrupt! */
1395 wait_event(dasd_flush_wq, _wait_for_clear(cqr));
1396 cqr->status = DASD_CQR_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 /* Process finished ERP request. */
1399 if (cqr->refers) {
1400 __dasd_process_erp(device, cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001401 /* restart list_for_xx loop since dasd_process_erp
1402 * might remove multiple elements */
1403 goto restart_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
Horst Hummel8f617012006-08-30 14:33:33 +02001405 /* call the callback function */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 cqr->endclk = get_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (cqr->callback != NULL)
1408 (cqr->callback)(cqr, cqr->callback_data);
1409 }
Horst Hummel8f617012006-08-30 14:33:33 +02001410 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
1413/*
1414 * Acquire the device lock and process queues for the device.
1415 */
1416static void
1417dasd_tasklet(struct dasd_device * device)
1418{
1419 struct list_head final_queue;
1420 struct list_head *l, *n;
1421 struct dasd_ccw_req *cqr;
1422
1423 atomic_set (&device->tasklet_scheduled, 0);
1424 INIT_LIST_HEAD(&final_queue);
1425 spin_lock_irq(get_ccwdev_lock(device->cdev));
1426 /* Check expire time of first request on the ccw queue. */
1427 __dasd_check_expire(device);
1428 /* Finish off requests on ccw queue */
1429 __dasd_process_ccw_queue(device, &final_queue);
1430 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1431 /* Now call the callback function of requests with final status */
1432 list_for_each_safe(l, n, &final_queue) {
1433 cqr = list_entry(l, struct dasd_ccw_req, list);
Horst Hummelc2ba4442006-02-01 03:06:37 -08001434 list_del_init(&cqr->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (cqr->callback != NULL)
1436 (cqr->callback)(cqr, cqr->callback_data);
1437 }
1438 spin_lock_irq(&device->request_queue_lock);
1439 spin_lock(get_ccwdev_lock(device->cdev));
1440 /* Get new request from the block device request queue */
1441 __dasd_process_blk_queue(device);
1442 /* Now check if the head of the ccw queue needs to be started. */
1443 __dasd_start_head(device);
1444 spin_unlock(get_ccwdev_lock(device->cdev));
1445 spin_unlock_irq(&device->request_queue_lock);
1446 dasd_put_device(device);
1447}
1448
1449/*
1450 * Schedules a call to dasd_tasklet over the device tasklet.
1451 */
1452void
1453dasd_schedule_bh(struct dasd_device * device)
1454{
1455 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001456 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return;
1458 dasd_get_device(device);
1459 tasklet_hi_schedule(&device->tasklet);
1460}
1461
1462/*
1463 * Queue a request to the head of the ccw_queue. Start the I/O if
1464 * possible.
1465 */
1466void
1467dasd_add_request_head(struct dasd_ccw_req *req)
1468{
1469 struct dasd_device *device;
1470 unsigned long flags;
1471
1472 device = req->device;
1473 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1474 req->status = DASD_CQR_QUEUED;
1475 req->device = device;
1476 list_add(&req->list, &device->ccw_queue);
1477 /* let the bh start the request to keep them in order */
1478 dasd_schedule_bh(device);
1479 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1480}
1481
1482/*
1483 * Queue a request to the tail of the ccw_queue. Start the I/O if
1484 * possible.
1485 */
1486void
1487dasd_add_request_tail(struct dasd_ccw_req *req)
1488{
1489 struct dasd_device *device;
1490 unsigned long flags;
1491
1492 device = req->device;
1493 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1494 req->status = DASD_CQR_QUEUED;
1495 req->device = device;
1496 list_add_tail(&req->list, &device->ccw_queue);
1497 /* let the bh start the request to keep them in order */
1498 dasd_schedule_bh(device);
1499 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1500}
1501
1502/*
1503 * Wakeup callback.
1504 */
1505static void
1506dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1507{
1508 wake_up((wait_queue_head_t *) data);
1509}
1510
1511static inline int
1512_wait_for_wakeup(struct dasd_ccw_req *cqr)
1513{
1514 struct dasd_device *device;
1515 int rc;
1516
1517 device = cqr->device;
1518 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001519 rc = ((cqr->status == DASD_CQR_DONE ||
1520 cqr->status == DASD_CQR_FAILED) &&
1521 list_empty(&cqr->list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1523 return rc;
1524}
1525
1526/*
1527 * Attempts to start a special ccw queue and waits for its completion.
1528 */
1529int
1530dasd_sleep_on(struct dasd_ccw_req * cqr)
1531{
1532 wait_queue_head_t wait_q;
1533 struct dasd_device *device;
1534 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 device = cqr->device;
1537 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel138c0142006-06-29 14:58:12 +02001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 init_waitqueue_head (&wait_q);
1540 cqr->callback = dasd_wakeup_cb;
1541 cqr->callback_data = (void *) &wait_q;
1542 cqr->status = DASD_CQR_QUEUED;
1543 list_add_tail(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /* let the bh start the request to keep them in order */
1546 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1549
1550 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* Request status is either done or failed. */
1553 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1554 return rc;
1555}
1556
1557/*
1558 * Attempts to start a special ccw queue and wait interruptible
1559 * for its completion.
1560 */
1561int
1562dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1563{
1564 wait_queue_head_t wait_q;
1565 struct dasd_device *device;
1566 int rc, finished;
1567
1568 device = cqr->device;
1569 spin_lock_irq(get_ccwdev_lock(device->cdev));
1570
1571 init_waitqueue_head (&wait_q);
1572 cqr->callback = dasd_wakeup_cb;
1573 cqr->callback_data = (void *) &wait_q;
1574 cqr->status = DASD_CQR_QUEUED;
1575 list_add_tail(&cqr->list, &device->ccw_queue);
1576
1577 /* let the bh start the request to keep them in order */
1578 dasd_schedule_bh(device);
1579 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1580
1581 finished = 0;
1582 while (!finished) {
1583 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1584 if (rc != -ERESTARTSYS) {
Horst Hummelc2ba4442006-02-01 03:06:37 -08001585 /* Request is final (done or failed) */
1586 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 break;
1588 }
1589 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001590 switch (cqr->status) {
1591 case DASD_CQR_IN_IO:
1592 /* terminate runnig cqr */
1593 if (device->discipline->term_IO) {
1594 cqr->retries = -1;
1595 device->discipline->term_IO(cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001596 /* wait (non-interruptible) for final status
1597 * because signal ist still pending */
Horst Hummelc2ba4442006-02-01 03:06:37 -08001598 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1599 wait_event(wait_q, _wait_for_wakeup(cqr));
1600 spin_lock_irq(get_ccwdev_lock(device->cdev));
1601 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1602 finished = 1;
1603 }
1604 break;
1605 case DASD_CQR_QUEUED:
1606 /* request */
1607 list_del_init(&cqr->list);
1608 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 finished = 1;
Horst Hummelc2ba4442006-02-01 03:06:37 -08001610 break;
1611 default:
1612 /* cqr with 'non-interruptable' status - just wait */
1613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1616 }
1617 return rc;
1618}
1619
1620/*
1621 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1622 * for eckd devices) the currently running request has to be terminated
1623 * and be put back to status queued, before the special request is added
1624 * to the head of the queue. Then the special request is waited on normally.
1625 */
1626static inline int
1627_dasd_term_running_cqr(struct dasd_device *device)
1628{
1629 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 if (list_empty(&device->ccw_queue))
1632 return 0;
1633 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Horst Hummel8f617012006-08-30 14:33:33 +02001634 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637int
1638dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1639{
1640 wait_queue_head_t wait_q;
1641 struct dasd_device *device;
1642 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 device = cqr->device;
1645 spin_lock_irq(get_ccwdev_lock(device->cdev));
1646 rc = _dasd_term_running_cqr(device);
1647 if (rc) {
1648 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1649 return rc;
1650 }
Horst Hummel138c0142006-06-29 14:58:12 +02001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 init_waitqueue_head (&wait_q);
1653 cqr->callback = dasd_wakeup_cb;
1654 cqr->callback_data = (void *) &wait_q;
1655 cqr->status = DASD_CQR_QUEUED;
1656 list_add(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /* let the bh start the request to keep them in order */
1659 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1662
1663 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /* Request status is either done or failed. */
1666 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1667 return rc;
1668}
1669
1670/*
1671 * Cancels a request that was started with dasd_sleep_on_req.
1672 * This is useful to timeout requests. The request will be
1673 * terminated if it is currently in i/o.
1674 * Returns 1 if the request has been terminated.
1675 */
1676int
1677dasd_cancel_req(struct dasd_ccw_req *cqr)
1678{
1679 struct dasd_device *device = cqr->device;
1680 unsigned long flags;
1681 int rc;
1682
1683 rc = 0;
1684 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1685 switch (cqr->status) {
1686 case DASD_CQR_QUEUED:
1687 /* request was not started - just set to failed */
1688 cqr->status = DASD_CQR_FAILED;
1689 break;
1690 case DASD_CQR_IN_IO:
1691 /* request in IO - terminate IO and release again */
1692 if (device->discipline->term_IO(cqr) != 0)
1693 /* what to do if unable to terminate ??????
1694 e.g. not _IN_IO */
1695 cqr->status = DASD_CQR_FAILED;
1696 cqr->stopclk = get_clock();
1697 rc = 1;
1698 break;
1699 case DASD_CQR_DONE:
1700 case DASD_CQR_FAILED:
1701 /* already finished - do nothing */
1702 break;
1703 default:
1704 DEV_MESSAGE(KERN_ALERT, device,
1705 "invalid status %02x in request",
1706 cqr->status);
1707 BUG();
1708
1709 }
1710 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1711 dasd_schedule_bh(device);
1712 return rc;
1713}
1714
1715/*
1716 * SECTION: Block device operations (request queue, partitions, open, release).
1717 */
1718
1719/*
1720 * Dasd request queue function. Called from ll_rw_blk.c
1721 */
1722static void
1723do_dasd_request(request_queue_t * queue)
1724{
1725 struct dasd_device *device;
1726
1727 device = (struct dasd_device *) queue->queuedata;
1728 spin_lock(get_ccwdev_lock(device->cdev));
1729 /* Get new request from the block device request queue */
1730 __dasd_process_blk_queue(device);
1731 /* Now check if the head of the ccw queue needs to be started. */
1732 __dasd_start_head(device);
1733 spin_unlock(get_ccwdev_lock(device->cdev));
1734}
1735
1736/*
1737 * Allocate and initialize request queue and default I/O scheduler.
1738 */
1739static int
1740dasd_alloc_queue(struct dasd_device * device)
1741{
1742 int rc;
1743
1744 device->request_queue = blk_init_queue(do_dasd_request,
1745 &device->request_queue_lock);
1746 if (device->request_queue == NULL)
1747 return -ENOMEM;
1748
1749 device->request_queue->queuedata = device;
1750
1751 elevator_exit(device->request_queue->elevator);
1752 rc = elevator_init(device->request_queue, "deadline");
1753 if (rc) {
1754 blk_cleanup_queue(device->request_queue);
1755 return rc;
1756 }
1757 return 0;
1758}
1759
1760/*
1761 * Allocate and initialize request queue.
1762 */
1763static void
1764dasd_setup_queue(struct dasd_device * device)
1765{
1766 int max;
1767
1768 blk_queue_hardsect_size(device->request_queue, device->bp_block);
1769 max = device->discipline->max_blocks << device->s2b_shift;
1770 blk_queue_max_sectors(device->request_queue, max);
1771 blk_queue_max_phys_segments(device->request_queue, -1L);
1772 blk_queue_max_hw_segments(device->request_queue, -1L);
1773 blk_queue_max_segment_size(device->request_queue, -1L);
1774 blk_queue_segment_boundary(device->request_queue, -1L);
Heiko Carstensed68cb32006-01-14 13:21:05 -08001775 blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778/*
1779 * Deactivate and free request queue.
1780 */
1781static void
1782dasd_free_queue(struct dasd_device * device)
1783{
1784 if (device->request_queue) {
1785 blk_cleanup_queue(device->request_queue);
1786 device->request_queue = NULL;
1787 }
1788}
1789
1790/*
1791 * Flush request on the request queue.
1792 */
1793static void
1794dasd_flush_request_queue(struct dasd_device * device)
1795{
1796 struct request *req;
1797
1798 if (!device->request_queue)
1799 return;
Horst Hummel138c0142006-06-29 14:58:12 +02001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 spin_lock_irq(&device->request_queue_lock);
Horst Hummel8f617012006-08-30 14:33:33 +02001802 while ((req = elv_next_request(device->request_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 blkdev_dequeue_request(req);
Horst Hummelebc45992006-08-09 10:30:22 +02001804 dasd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806 spin_unlock_irq(&device->request_queue_lock);
1807}
1808
1809static int
1810dasd_open(struct inode *inp, struct file *filp)
1811{
1812 struct gendisk *disk = inp->i_bdev->bd_disk;
1813 struct dasd_device *device = disk->private_data;
1814 int rc;
1815
1816 atomic_inc(&device->open_count);
1817 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1818 rc = -ENODEV;
1819 goto unlock;
1820 }
1821
1822 if (!try_module_get(device->discipline->owner)) {
1823 rc = -EINVAL;
1824 goto unlock;
1825 }
1826
1827 if (dasd_probeonly) {
1828 DEV_MESSAGE(KERN_INFO, device, "%s",
1829 "No access to device due to probeonly mode");
1830 rc = -EPERM;
1831 goto out;
1832 }
1833
Horst Hummel90f00942006-03-07 21:55:39 -08001834 if (device->state <= DASD_STATE_BASIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1836 " Cannot open unrecognized device");
1837 rc = -ENODEV;
1838 goto out;
1839 }
1840
1841 return 0;
1842
1843out:
1844 module_put(device->discipline->owner);
1845unlock:
1846 atomic_dec(&device->open_count);
1847 return rc;
1848}
1849
1850static int
1851dasd_release(struct inode *inp, struct file *filp)
1852{
1853 struct gendisk *disk = inp->i_bdev->bd_disk;
1854 struct dasd_device *device = disk->private_data;
1855
1856 atomic_dec(&device->open_count);
1857 module_put(device->discipline->owner);
1858 return 0;
1859}
1860
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001861/*
1862 * Return disk geometry.
1863 */
1864static int
1865dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1866{
1867 struct dasd_device *device;
1868
1869 device = bdev->bd_disk->private_data;
1870 if (!device)
1871 return -ENODEV;
1872
1873 if (!device->discipline ||
1874 !device->discipline->fill_geometry)
1875 return -EINVAL;
1876
1877 device->discipline->fill_geometry(device, geo);
1878 geo->start = get_start_sect(bdev) >> device->s2b_shift;
1879 return 0;
1880}
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882struct block_device_operations
1883dasd_device_operations = {
1884 .owner = THIS_MODULE,
1885 .open = dasd_open,
1886 .release = dasd_release,
1887 .ioctl = dasd_ioctl,
Christoph Hellwig82620372006-01-09 20:52:12 -08001888 .compat_ioctl = dasd_compat_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001889 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890};
1891
1892
1893static void
1894dasd_exit(void)
1895{
1896#ifdef CONFIG_PROC_FS
1897 dasd_proc_exit();
1898#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001899 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07001900 if (dasd_page_cache != NULL) {
1901 kmem_cache_destroy(dasd_page_cache);
1902 dasd_page_cache = NULL;
1903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 dasd_gendisk_exit();
1905 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (dasd_debug_area != NULL) {
1907 debug_unregister(dasd_debug_area);
1908 dasd_debug_area = NULL;
1909 }
1910}
1911
1912/*
1913 * SECTION: common functions for ccw_driver use
1914 */
1915
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001916/*
1917 * Initial attempt at a probe function. this can be simplified once
1918 * the other detection code is gone.
1919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920int
1921dasd_generic_probe (struct ccw_device *cdev,
1922 struct dasd_discipline *discipline)
1923{
1924 int ret;
1925
Horst Hummel40545572006-06-29 15:08:18 +02001926 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
1927 if (ret) {
1928 printk(KERN_WARNING
1929 "dasd_generic_probe: could not set ccw-device options "
1930 "for %s\n", cdev->dev.bus_id);
1931 return ret;
1932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 ret = dasd_add_sysfs_files(cdev);
1934 if (ret) {
1935 printk(KERN_WARNING
1936 "dasd_generic_probe: could not add sysfs entries "
1937 "for %s\n", cdev->dev.bus_id);
Horst Hummel40545572006-06-29 15:08:18 +02001938 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Horst Hummel40545572006-06-29 15:08:18 +02001940 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Horst Hummel40545572006-06-29 15:08:18 +02001942 /*
1943 * Automatically online either all dasd devices (dasd_autodetect)
1944 * or all devices specified with dasd= parameters during
1945 * initial probe.
1946 */
1947 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
1948 (dasd_autodetect && dasd_busid_known(cdev->dev.bus_id) != 0))
1949 ret = ccw_device_set_online(cdev);
1950 if (ret)
1951 printk(KERN_WARNING
1952 "dasd_generic_probe: could not initially online "
1953 "ccw-device %s\n", cdev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 return ret;
1955}
1956
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001957/*
1958 * This will one day be called from a global not_oper handler.
1959 * It is also used by driver_unregister during module unload.
1960 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961void
1962dasd_generic_remove (struct ccw_device *cdev)
1963{
1964 struct dasd_device *device;
1965
Horst Hummel59afda72005-05-16 21:53:39 -07001966 cdev->handler = NULL;
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 dasd_remove_sysfs_files(cdev);
1969 device = dasd_device_from_cdev(cdev);
1970 if (IS_ERR(device))
1971 return;
1972 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1973 /* Already doing offline processing */
1974 dasd_put_device(device);
1975 return;
1976 }
1977 /*
1978 * This device is removed unconditionally. Set offline
1979 * flag to prevent dasd_open from opening it while it is
1980 * no quite down yet.
1981 */
1982 dasd_set_target_state(device, DASD_STATE_NEW);
1983 /* dasd_delete_device destroys the device reference. */
1984 dasd_delete_device(device);
1985}
1986
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001987/*
1988 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001990 * or the user has started activation through sysfs.
1991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992int
1993dasd_generic_set_online (struct ccw_device *cdev,
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001994 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001997 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001999 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07002000
Horst Hummel40545572006-06-29 15:08:18 +02002001 /* first online clears initial online feature flag */
2002 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 device = dasd_create_device(cdev);
2004 if (IS_ERR(device))
2005 return PTR_ERR(device);
2006
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002007 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002008 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (!dasd_diag_discipline_pointer) {
2010 printk (KERN_WARNING
2011 "dasd_generic couldn't online device %s "
2012 "- discipline DIAG not available\n",
2013 cdev->dev.bus_id);
2014 dasd_delete_device(device);
2015 return -ENODEV;
2016 }
2017 discipline = dasd_diag_discipline_pointer;
2018 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002019 if (!try_module_get(base_discipline->owner)) {
2020 dasd_delete_device(device);
2021 return -EINVAL;
2022 }
2023 if (!try_module_get(discipline->owner)) {
2024 module_put(base_discipline->owner);
2025 dasd_delete_device(device);
2026 return -EINVAL;
2027 }
2028 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 device->discipline = discipline;
2030
2031 rc = discipline->check_device(device);
2032 if (rc) {
2033 printk (KERN_WARNING
2034 "dasd_generic couldn't online device %s "
2035 "with discipline %s rc=%i\n",
2036 cdev->dev.bus_id, discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002037 module_put(discipline->owner);
2038 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 dasd_delete_device(device);
2040 return rc;
2041 }
2042
2043 dasd_set_target_state(device, DASD_STATE_ONLINE);
2044 if (device->state <= DASD_STATE_KNOWN) {
2045 printk (KERN_WARNING
2046 "dasd_generic discipline not found for %s\n",
2047 cdev->dev.bus_id);
2048 rc = -ENODEV;
2049 dasd_set_target_state(device, DASD_STATE_NEW);
2050 dasd_delete_device(device);
2051 } else
2052 pr_debug("dasd_generic device %s found\n",
2053 cdev->dev.bus_id);
2054
2055 /* FIXME: we have to wait for the root device but we don't want
2056 * to wait for each single device but for all at once. */
2057 wait_event(dasd_init_waitq, _wait_for_device(device));
2058
2059 dasd_put_device(device);
2060
2061 return rc;
2062}
2063
2064int
2065dasd_generic_set_offline (struct ccw_device *cdev)
2066{
2067 struct dasd_device *device;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002068 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 device = dasd_device_from_cdev(cdev);
2071 if (IS_ERR(device))
2072 return PTR_ERR(device);
2073 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2074 /* Already doing offline processing */
2075 dasd_put_device(device);
2076 return 0;
2077 }
2078 /*
2079 * We must make sure that this device is currently not in use.
2080 * The open_count is increased for every opener, that includes
2081 * the blkdev_get in dasd_scan_partitions. We are only interested
2082 * in the other openers.
2083 */
2084 max_count = device->bdev ? 0 : -1;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002085 open_count = (int) atomic_read(&device->open_count);
2086 if (open_count > max_count) {
2087 if (open_count > 0)
2088 printk (KERN_WARNING "Can't offline dasd device with "
2089 "open count = %i.\n",
2090 open_count);
2091 else
2092 printk (KERN_WARNING "%s",
2093 "Can't offline dasd device due to internal "
2094 "use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2096 dasd_put_device(device);
2097 return -EBUSY;
2098 }
2099 dasd_set_target_state(device, DASD_STATE_NEW);
2100 /* dasd_delete_device destroys the device reference. */
2101 dasd_delete_device(device);
2102
2103 return 0;
2104}
2105
2106int
2107dasd_generic_notify(struct ccw_device *cdev, int event)
2108{
2109 struct dasd_device *device;
2110 struct dasd_ccw_req *cqr;
2111 unsigned long flags;
2112 int ret;
2113
2114 device = dasd_device_from_cdev(cdev);
2115 if (IS_ERR(device))
2116 return 0;
2117 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
2118 ret = 0;
2119 switch (event) {
2120 case CIO_GONE:
2121 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002122 /* First of all call extended error reporting. */
2123 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (device->state < DASD_STATE_BASIC)
2126 break;
2127 /* Device is active. We want to keep it. */
2128 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
2129 list_for_each_entry(cqr, &device->ccw_queue, list)
2130 if (cqr->status == DASD_CQR_IN_IO)
2131 cqr->status = DASD_CQR_FAILED;
2132 device->stopped |= DASD_STOPPED_DC_EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 } else {
2134 list_for_each_entry(cqr, &device->ccw_queue, list)
2135 if (cqr->status == DASD_CQR_IN_IO) {
2136 cqr->status = DASD_CQR_QUEUED;
2137 cqr->retries++;
2138 }
2139 device->stopped |= DASD_STOPPED_DC_WAIT;
2140 dasd_set_timer(device, 0);
2141 }
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002142 dasd_schedule_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 ret = 1;
2144 break;
2145 case CIO_OPER:
2146 /* FIXME: add a sanity check. */
2147 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
2148 dasd_schedule_bh(device);
2149 ret = 1;
2150 break;
2151 }
2152 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2153 dasd_put_device(device);
2154 return ret;
2155}
2156
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158static int __init
2159dasd_init(void)
2160{
2161 int rc;
2162
2163 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002164 init_waitqueue_head(&dasd_flush_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Michael Holzheu66a464d2005-06-25 14:55:33 -07002167 dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (dasd_debug_area == NULL) {
2169 rc = -ENOMEM;
2170 goto failed;
2171 }
2172 debug_register_view(dasd_debug_area, &debug_sprintf_view);
Horst Hummelb0035f12006-09-20 15:59:07 +02002173 debug_set_level(dasd_debug_area, DBF_WARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2176
2177 dasd_diag_discipline_pointer = NULL;
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 rc = dasd_devmap_init();
2180 if (rc)
2181 goto failed;
2182 rc = dasd_gendisk_init();
2183 if (rc)
2184 goto failed;
2185 rc = dasd_parse();
2186 if (rc)
2187 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002188 rc = dasd_eer_init();
2189 if (rc)
2190 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#ifdef CONFIG_PROC_FS
2192 rc = dasd_proc_init();
2193 if (rc)
2194 goto failed;
2195#endif
2196
2197 return 0;
2198failed:
2199 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2200 dasd_exit();
2201 return rc;
2202}
2203
2204module_init(dasd_init);
2205module_exit(dasd_exit);
2206
2207EXPORT_SYMBOL(dasd_debug_area);
2208EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2209
2210EXPORT_SYMBOL(dasd_add_request_head);
2211EXPORT_SYMBOL(dasd_add_request_tail);
2212EXPORT_SYMBOL(dasd_cancel_req);
2213EXPORT_SYMBOL(dasd_clear_timer);
2214EXPORT_SYMBOL(dasd_enable_device);
2215EXPORT_SYMBOL(dasd_int_handler);
2216EXPORT_SYMBOL(dasd_kfree_request);
2217EXPORT_SYMBOL(dasd_kick_device);
2218EXPORT_SYMBOL(dasd_kmalloc_request);
2219EXPORT_SYMBOL(dasd_schedule_bh);
2220EXPORT_SYMBOL(dasd_set_target_state);
2221EXPORT_SYMBOL(dasd_set_timer);
2222EXPORT_SYMBOL(dasd_sfree_request);
2223EXPORT_SYMBOL(dasd_sleep_on);
2224EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2225EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2226EXPORT_SYMBOL(dasd_smalloc_request);
2227EXPORT_SYMBOL(dasd_start_IO);
2228EXPORT_SYMBOL(dasd_term_IO);
2229
2230EXPORT_SYMBOL_GPL(dasd_generic_probe);
2231EXPORT_SYMBOL_GPL(dasd_generic_remove);
2232EXPORT_SYMBOL_GPL(dasd_generic_notify);
2233EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2234EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235