blob: 3cd87f85f7024996ce51f6c7b404f9820972345c [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);
187 debug_set_level(device->debug_area, DBF_EMERG);
188 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;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
208 if (device->debug_area != NULL) {
209 debug_unregister(device->debug_area);
210 device->debug_area = NULL;
211 }
212 device->state = DASD_STATE_KNOWN;
Horst Hummel8f617012006-08-30 14:33:33 +0200213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216/*
217 * Do the initial analysis. The do_analysis function may return
218 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
219 * until the discipline decides to continue the startup sequence
220 * by calling the function dasd_change_state. The eckd disciplines
221 * uses this to start a ccw that detects the format. The completion
222 * interrupt for this detection ccw uses the kernel event daemon to
223 * trigger the call to dasd_change_state. All this is done in the
224 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800225 * After the analysis ccw is done (do_analysis returned 0) the block
226 * device is setup.
227 * In case the analysis returns an error, the device setup is stopped
228 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
Horst Hummel8f617012006-08-30 14:33:33 +0200230static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231dasd_state_basic_to_ready(struct dasd_device * device)
232{
233 int rc;
234
235 rc = 0;
236 if (device->discipline->do_analysis != NULL)
237 rc = device->discipline->do_analysis(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800238 if (rc) {
239 if (rc != -EAGAIN)
240 device->state = DASD_STATE_UNFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return rc;
Horst Hummel90f00942006-03-07 21:55:39 -0800242 }
243 /* make disk known with correct capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 dasd_setup_queue(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800245 set_capacity(device->gdp, device->blocks << device->s2b_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 device->state = DASD_STATE_READY;
Horst Hummel90f00942006-03-07 21:55:39 -0800247 rc = dasd_scan_partitions(device);
248 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 device->state = DASD_STATE_BASIC;
Horst Hummel90f00942006-03-07 21:55:39 -0800250 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/*
254 * Remove device from block device layer. Destroy dirty buffers.
255 * Forget format information. Check if the target level is basic
256 * and if it is create fake disk for formatting.
257 */
Horst Hummel8f617012006-08-30 14:33:33 +0200258static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259dasd_state_ready_to_basic(struct dasd_device * device)
260{
Horst Hummel8f617012006-08-30 14:33:33 +0200261 int rc;
262
263 rc = dasd_flush_ccw_queue(device, 0);
264 if (rc)
265 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dasd_destroy_partitions(device);
267 dasd_flush_request_queue(device);
268 device->blocks = 0;
269 device->bp_block = 0;
270 device->s2b_shift = 0;
271 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/*
Horst Hummel90f00942006-03-07 21:55:39 -0800276 * Back to basic.
277 */
Horst Hummel8f617012006-08-30 14:33:33 +0200278static int
Horst Hummel90f00942006-03-07 21:55:39 -0800279dasd_state_unfmt_to_basic(struct dasd_device * device)
280{
281 device->state = DASD_STATE_BASIC;
Horst Hummel8f617012006-08-30 14:33:33 +0200282 return 0;
Horst Hummel90f00942006-03-07 21:55:39 -0800283}
284
285/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * Make the device online and schedule the bottom half to start
287 * the requeueing of requests from the linux request queue to the
288 * ccw queue.
289 */
Horst Hummel8f617012006-08-30 14:33:33 +0200290static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291dasd_state_ready_to_online(struct dasd_device * device)
292{
293 device->state = DASD_STATE_ONLINE;
294 dasd_schedule_bh(device);
295 return 0;
296}
297
298/*
299 * Stop the requeueing of requests again.
300 */
Horst Hummel8f617012006-08-30 14:33:33 +0200301static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302dasd_state_online_to_ready(struct dasd_device * device)
303{
304 device->state = DASD_STATE_READY;
Horst Hummel8f617012006-08-30 14:33:33 +0200305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/*
309 * Device startup state changes.
310 */
Horst Hummel8f617012006-08-30 14:33:33 +0200311static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312dasd_increase_state(struct dasd_device *device)
313{
314 int rc;
315
316 rc = 0;
317 if (device->state == DASD_STATE_NEW &&
318 device->target >= DASD_STATE_KNOWN)
319 rc = dasd_state_new_to_known(device);
320
321 if (!rc &&
322 device->state == DASD_STATE_KNOWN &&
323 device->target >= DASD_STATE_BASIC)
324 rc = dasd_state_known_to_basic(device);
325
326 if (!rc &&
327 device->state == DASD_STATE_BASIC &&
328 device->target >= DASD_STATE_READY)
329 rc = dasd_state_basic_to_ready(device);
330
331 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700332 device->state == DASD_STATE_UNFMT &&
333 device->target > DASD_STATE_UNFMT)
334 rc = -EPERM;
335
336 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 device->state == DASD_STATE_READY &&
338 device->target >= DASD_STATE_ONLINE)
339 rc = dasd_state_ready_to_online(device);
340
341 return rc;
342}
343
344/*
345 * Device shutdown state changes.
346 */
Horst Hummel8f617012006-08-30 14:33:33 +0200347static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348dasd_decrease_state(struct dasd_device *device)
349{
Horst Hummel8f617012006-08-30 14:33:33 +0200350 int rc;
351
352 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (device->state == DASD_STATE_ONLINE &&
354 device->target <= DASD_STATE_READY)
Horst Hummel8f617012006-08-30 14:33:33 +0200355 rc = dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200356
Horst Hummel8f617012006-08-30 14:33:33 +0200357 if (!rc &&
358 device->state == DASD_STATE_READY &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200360 rc = dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800361
Horst Hummel8f617012006-08-30 14:33:33 +0200362 if (!rc &&
363 device->state == DASD_STATE_UNFMT &&
Horst Hummel90f00942006-03-07 21:55:39 -0800364 device->target <= DASD_STATE_BASIC)
Horst Hummel8f617012006-08-30 14:33:33 +0200365 rc = dasd_state_unfmt_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800366
Horst Hummel8f617012006-08-30 14:33:33 +0200367 if (!rc &&
368 device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 device->target <= DASD_STATE_KNOWN)
Horst Hummel8f617012006-08-30 14:33:33 +0200370 rc = dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200371
Horst Hummel8f617012006-08-30 14:33:33 +0200372 if (!rc &&
373 device->state == DASD_STATE_KNOWN &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 device->target <= DASD_STATE_NEW)
Horst Hummel8f617012006-08-30 14:33:33 +0200375 rc = dasd_state_known_to_new(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Horst Hummel8f617012006-08-30 14:33:33 +0200377 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
381 * This is the main startup/shutdown routine.
382 */
383static void
384dasd_change_state(struct dasd_device *device)
385{
386 int rc;
387
388 if (device->state == device->target)
389 /* Already where we want to go today... */
390 return;
391 if (device->state < device->target)
392 rc = dasd_increase_state(device);
393 else
394 rc = dasd_decrease_state(device);
395 if (rc && rc != -EAGAIN)
396 device->target = device->state;
397
398 if (device->state == device->target)
399 wake_up(&dasd_init_waitq);
400}
401
402/*
403 * Kick starter for devices that did not complete the startup/shutdown
404 * procedure or were sleeping because of a pending state.
405 * dasd_kick_device will schedule a call do do_kick_device to the kernel
406 * event daemon.
407 */
408static void
409do_kick_device(void *data)
410{
411 struct dasd_device *device;
412
413 device = (struct dasd_device *) data;
414 dasd_change_state(device);
415 dasd_schedule_bh(device);
416 dasd_put_device(device);
417}
418
419void
420dasd_kick_device(struct dasd_device *device)
421{
422 dasd_get_device(device);
423 /* queue call to dasd_kick_device to the kernel event daemon. */
424 schedule_work(&device->kick_work);
425}
426
427/*
428 * Set the target state for a device and starts the state change.
429 */
430void
431dasd_set_target_state(struct dasd_device *device, int target)
432{
433 /* If we are in probeonly mode stop at DASD_STATE_READY. */
434 if (dasd_probeonly && target > DASD_STATE_READY)
435 target = DASD_STATE_READY;
436 if (device->target != target) {
437 if (device->state == target)
438 wake_up(&dasd_init_waitq);
439 device->target = target;
440 }
441 if (device->state != device->target)
442 dasd_change_state(device);
443}
444
445/*
446 * Enable devices with device numbers in [from..to].
447 */
448static inline int
449_wait_for_device(struct dasd_device *device)
450{
451 return (device->state == device->target);
452}
453
454void
455dasd_enable_device(struct dasd_device *device)
456{
457 dasd_set_target_state(device, DASD_STATE_ONLINE);
458 if (device->state <= DASD_STATE_KNOWN)
459 /* No discipline for device found. */
460 dasd_set_target_state(device, DASD_STATE_NEW);
461 /* Now wait for the devices to come up. */
462 wait_event(dasd_init_waitq, _wait_for_device(device));
463}
464
465/*
466 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
467 */
468#ifdef CONFIG_DASD_PROFILE
469
470struct dasd_profile_info_t dasd_global_profile;
471unsigned int dasd_profile_level = DASD_PROFILE_OFF;
472
473/*
474 * Increments counter in global and local profiling structures.
475 */
476#define dasd_profile_counter(value, counter, device) \
477{ \
478 int index; \
479 for (index = 0; index < 31 && value >> (2+index); index++); \
480 dasd_global_profile.counter[index]++; \
481 device->profile.counter[index]++; \
482}
483
484/*
485 * Add profiling information for cqr before execution.
486 */
487static inline void
488dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
489 struct request *req)
490{
491 struct list_head *l;
492 unsigned int counter;
493
494 if (dasd_profile_level != DASD_PROFILE_ON)
495 return;
496
497 /* count the length of the chanq for statistics */
498 counter = 0;
499 list_for_each(l, &device->ccw_queue)
500 if (++counter >= 31)
501 break;
502 dasd_global_profile.dasd_io_nr_req[counter]++;
503 device->profile.dasd_io_nr_req[counter]++;
504}
505
506/*
507 * Add profiling information for cqr after execution.
508 */
509static inline void
510dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
511 struct request *req)
512{
513 long strtime, irqtime, endtime, tottime; /* in microseconds */
514 long tottimeps, sectors;
515
516 if (dasd_profile_level != DASD_PROFILE_ON)
517 return;
518
519 sectors = req->nr_sectors;
520 if (!cqr->buildclk || !cqr->startclk ||
521 !cqr->stopclk || !cqr->endclk ||
522 !sectors)
523 return;
524
525 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
526 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
527 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
528 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
529 tottimeps = tottime / sectors;
530
531 if (!dasd_global_profile.dasd_io_reqs)
532 memset(&dasd_global_profile, 0,
533 sizeof (struct dasd_profile_info_t));
534 dasd_global_profile.dasd_io_reqs++;
535 dasd_global_profile.dasd_io_sects += sectors;
536
537 if (!device->profile.dasd_io_reqs)
538 memset(&device->profile, 0,
539 sizeof (struct dasd_profile_info_t));
540 device->profile.dasd_io_reqs++;
541 device->profile.dasd_io_sects += sectors;
542
543 dasd_profile_counter(sectors, dasd_io_secs, device);
544 dasd_profile_counter(tottime, dasd_io_times, device);
545 dasd_profile_counter(tottimeps, dasd_io_timps, device);
546 dasd_profile_counter(strtime, dasd_io_time1, device);
547 dasd_profile_counter(irqtime, dasd_io_time2, device);
548 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
549 dasd_profile_counter(endtime, dasd_io_time3, device);
550}
551#else
552#define dasd_profile_start(device, cqr, req) do {} while (0)
553#define dasd_profile_end(device, cqr, req) do {} while (0)
554#endif /* CONFIG_DASD_PROFILE */
555
556/*
557 * Allocate memory for a channel program with 'cplength' channel
558 * command words and 'datasize' additional space. There are two
559 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
560 * memory and 2) dasd_smalloc_request uses the static ccw memory
561 * that gets allocated for each device.
562 */
563struct dasd_ccw_req *
564dasd_kmalloc_request(char *magic, int cplength, int datasize,
565 struct dasd_device * device)
566{
567 struct dasd_ccw_req *cqr;
568
569 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100570 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
571 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800573 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (cqr == NULL)
575 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 cqr->cpaddr = NULL;
577 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800578 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 GFP_ATOMIC | GFP_DMA);
580 if (cqr->cpaddr == NULL) {
581 kfree(cqr);
582 return ERR_PTR(-ENOMEM);
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 cqr->data = NULL;
586 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800587 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800589 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 kfree(cqr);
591 return ERR_PTR(-ENOMEM);
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 strncpy((char *) &cqr->magic, magic, 4);
595 ASCEBC((char *) &cqr->magic, 4);
596 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
597 dasd_get_device(device);
598 return cqr;
599}
600
601struct dasd_ccw_req *
602dasd_smalloc_request(char *magic, int cplength, int datasize,
603 struct dasd_device * device)
604{
605 unsigned long flags;
606 struct dasd_ccw_req *cqr;
607 char *data;
608 int size;
609
610 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100611 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
612 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
615 if (cplength > 0)
616 size += cplength * sizeof(struct ccw1);
617 if (datasize > 0)
618 size += datasize;
619 spin_lock_irqsave(&device->mem_lock, flags);
620 cqr = (struct dasd_ccw_req *)
621 dasd_alloc_chunk(&device->ccw_chunks, size);
622 spin_unlock_irqrestore(&device->mem_lock, flags);
623 if (cqr == NULL)
624 return ERR_PTR(-ENOMEM);
625 memset(cqr, 0, sizeof(struct dasd_ccw_req));
626 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
627 cqr->cpaddr = NULL;
628 if (cplength > 0) {
629 cqr->cpaddr = (struct ccw1 *) data;
630 data += cplength*sizeof(struct ccw1);
631 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
632 }
633 cqr->data = NULL;
634 if (datasize > 0) {
635 cqr->data = data;
636 memset(cqr->data, 0, datasize);
637 }
638 strncpy((char *) &cqr->magic, magic, 4);
639 ASCEBC((char *) &cqr->magic, 4);
640 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
641 dasd_get_device(device);
642 return cqr;
643}
644
645/*
646 * Free memory of a channel program. This function needs to free all the
647 * idal lists that might have been created by dasd_set_cda and the
648 * struct dasd_ccw_req itself.
649 */
650void
651dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
652{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800653#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct ccw1 *ccw;
655
656 /* Clear any idals used for the request. */
657 ccw = cqr->cpaddr;
658 do {
659 clear_normalized_cda(ccw);
660 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
661#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800662 kfree(cqr->cpaddr);
663 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 kfree(cqr);
665 dasd_put_device(device);
666}
667
668void
669dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
670{
671 unsigned long flags;
672
673 spin_lock_irqsave(&device->mem_lock, flags);
674 dasd_free_chunk(&device->ccw_chunks, cqr);
675 spin_unlock_irqrestore(&device->mem_lock, flags);
676 dasd_put_device(device);
677}
678
679/*
680 * Check discipline magic in cqr.
681 */
682static inline int
683dasd_check_cqr(struct dasd_ccw_req *cqr)
684{
685 struct dasd_device *device;
686
687 if (cqr == NULL)
688 return -EINVAL;
689 device = cqr->device;
690 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
691 DEV_MESSAGE(KERN_WARNING, device,
692 " dasd_ccw_req 0x%08x magic doesn't match"
693 " discipline 0x%08x",
694 cqr->magic,
695 *(unsigned int *) device->discipline->name);
696 return -EINVAL;
697 }
698 return 0;
699}
700
701/*
702 * Terminate the current i/o and set the request to clear_pending.
703 * Timer keeps device runnig.
704 * ccw_device_clear can fail if the i/o subsystem
705 * is in a bad mood.
706 */
707int
708dasd_term_IO(struct dasd_ccw_req * cqr)
709{
710 struct dasd_device *device;
711 int retries, rc;
712
713 /* Check the cqr */
714 rc = dasd_check_cqr(cqr);
715 if (rc)
716 return rc;
717 retries = 0;
718 device = (struct dasd_device *) cqr->device;
719 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
720 rc = ccw_device_clear(device->cdev, (long) cqr);
721 switch (rc) {
722 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800723 cqr->retries--;
724 cqr->status = DASD_CQR_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +0200726 cqr->starttime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 DBF_DEV_EVENT(DBF_DEBUG, device,
728 "terminate cqr %p successful",
729 cqr);
730 break;
731 case -ENODEV:
732 DBF_DEV_EVENT(DBF_ERR, device, "%s",
733 "device gone, retry");
734 break;
735 case -EIO:
736 DBF_DEV_EVENT(DBF_ERR, device, "%s",
737 "I/O error, retry");
738 break;
739 case -EINVAL:
740 case -EBUSY:
741 DBF_DEV_EVENT(DBF_ERR, device, "%s",
742 "device busy, retry later");
743 break;
744 default:
745 DEV_MESSAGE(KERN_ERR, device,
746 "line %d unknown RC=%d, please "
747 "report to linux390@de.ibm.com",
748 __LINE__, rc);
749 BUG();
750 break;
751 }
752 retries++;
753 }
754 dasd_schedule_bh(device);
755 return rc;
756}
757
758/*
759 * Start the i/o. This start_IO can fail if the channel is really busy.
760 * In that case set up a timer to start the request later.
761 */
762int
763dasd_start_IO(struct dasd_ccw_req * cqr)
764{
765 struct dasd_device *device;
766 int rc;
767
768 /* Check the cqr */
769 rc = dasd_check_cqr(cqr);
770 if (rc)
771 return rc;
772 device = (struct dasd_device *) cqr->device;
773 if (cqr->retries < 0) {
774 DEV_MESSAGE(KERN_DEBUG, device,
775 "start_IO: request %p (%02x/%i) - no retry left.",
776 cqr, cqr->status, cqr->retries);
777 cqr->status = DASD_CQR_FAILED;
778 return -EIO;
779 }
780 cqr->startclk = get_clock();
781 cqr->starttime = jiffies;
782 cqr->retries--;
783 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
784 cqr->lpm, 0);
785 switch (rc) {
786 case 0:
787 cqr->status = DASD_CQR_IN_IO;
788 DBF_DEV_EVENT(DBF_DEBUG, device,
789 "start_IO: request %p started successful",
790 cqr);
791 break;
792 case -EBUSY:
793 DBF_DEV_EVENT(DBF_ERR, device, "%s",
794 "start_IO: device busy, retry later");
795 break;
796 case -ETIMEDOUT:
797 DBF_DEV_EVENT(DBF_ERR, device, "%s",
798 "start_IO: request timeout, retry later");
799 break;
800 case -EACCES:
801 /* -EACCES indicates that the request used only a
802 * subset of the available pathes and all these
803 * pathes are gone.
804 * Do a retry with all available pathes.
805 */
806 cqr->lpm = LPM_ANYPATH;
807 DBF_DEV_EVENT(DBF_ERR, device, "%s",
808 "start_IO: selected pathes gone,"
809 " retry on all pathes");
810 break;
811 case -ENODEV:
812 case -EIO:
813 DBF_DEV_EVENT(DBF_ERR, device, "%s",
814 "start_IO: device gone, retry");
815 break;
816 default:
817 DEV_MESSAGE(KERN_ERR, device,
818 "line %d unknown RC=%d, please report"
819 " to linux390@de.ibm.com", __LINE__, rc);
820 BUG();
821 break;
822 }
823 return rc;
824}
825
826/*
827 * Timeout function for dasd devices. This is used for different purposes
828 * 1) missing interrupt handler for normal operation
829 * 2) delayed start of request where start_IO failed with -EBUSY
830 * 3) timeout for missing state change interrupts
831 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
832 * DASD_CQR_QUEUED for 2) and 3).
833 */
834static void
835dasd_timeout_device(unsigned long ptr)
836{
837 unsigned long flags;
838 struct dasd_device *device;
839
840 device = (struct dasd_device *) ptr;
841 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
842 /* re-activate request queue */
843 device->stopped &= ~DASD_STOPPED_PENDING;
844 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
845 dasd_schedule_bh(device);
846}
847
848/*
849 * Setup timeout for a device in jiffies.
850 */
851void
852dasd_set_timer(struct dasd_device *device, int expires)
853{
854 if (expires == 0) {
855 if (timer_pending(&device->timer))
856 del_timer(&device->timer);
857 return;
858 }
859 if (timer_pending(&device->timer)) {
860 if (mod_timer(&device->timer, jiffies + expires))
861 return;
862 }
863 device->timer.function = dasd_timeout_device;
864 device->timer.data = (unsigned long) device;
865 device->timer.expires = jiffies + expires;
866 add_timer(&device->timer);
867}
868
869/*
870 * Clear timeout for a device.
871 */
872void
873dasd_clear_timer(struct dasd_device *device)
874{
875 if (timer_pending(&device->timer))
876 del_timer(&device->timer);
877}
878
879static void
880dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
881{
882 struct dasd_ccw_req *cqr;
883 struct dasd_device *device;
884
885 cqr = (struct dasd_ccw_req *) intparm;
886 if (cqr->status != DASD_CQR_IN_IO) {
887 MESSAGE(KERN_DEBUG,
888 "invalid status in handle_killed_request: "
889 "bus_id %s, status %02x",
890 cdev->dev.bus_id, cqr->status);
891 return;
892 }
893
894 device = (struct dasd_device *) cqr->device;
895 if (device == NULL ||
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200896 device != dasd_device_from_cdev_locked(cdev) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
898 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
899 cdev->dev.bus_id);
900 return;
901 }
902
903 /* Schedule request to be retried. */
904 cqr->status = DASD_CQR_QUEUED;
905
906 dasd_clear_timer(device);
907 dasd_schedule_bh(device);
908 dasd_put_device(device);
909}
910
911static void
912dasd_handle_state_change_pending(struct dasd_device *device)
913{
914 struct dasd_ccw_req *cqr;
915 struct list_head *l, *n;
916
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800917 /* First of all start sense subsystem status request. */
918 dasd_eer_snss(device);
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 device->stopped &= ~DASD_STOPPED_PENDING;
921
922 /* restart all 'running' IO on queue */
923 list_for_each_safe(l, n, &device->ccw_queue) {
924 cqr = list_entry(l, struct dasd_ccw_req, list);
925 if (cqr->status == DASD_CQR_IN_IO) {
926 cqr->status = DASD_CQR_QUEUED;
927 }
928 }
929 dasd_clear_timer(device);
930 dasd_schedule_bh(device);
931}
932
933/*
934 * Interrupt handler for "normal" ssch-io based dasd devices.
935 */
936void
937dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
938 struct irb *irb)
939{
940 struct dasd_ccw_req *cqr, *next;
941 struct dasd_device *device;
942 unsigned long long now;
943 int expires;
944 dasd_era_t era;
945 char mask;
946
947 if (IS_ERR(irb)) {
948 switch (PTR_ERR(irb)) {
949 case -EIO:
950 dasd_handle_killed_request(cdev, intparm);
951 break;
952 case -ETIMEDOUT:
953 printk(KERN_WARNING"%s(%s): request timed out\n",
954 __FUNCTION__, cdev->dev.bus_id);
955 //FIXME - dasd uses own timeout interface...
956 break;
957 default:
958 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
959 __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
960 }
961 return;
962 }
963
964 now = get_clock();
965
966 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
967 cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
968 (unsigned int) intparm);
969
970 /* first of all check for state change pending interrupt */
971 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
972 if ((irb->scsw.dstat & mask) == mask) {
Martin Schwidefskya00bfd72006-09-20 15:59:05 +0200973 device = dasd_device_from_cdev_locked(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!IS_ERR(device)) {
975 dasd_handle_state_change_pending(device);
976 dasd_put_device(device);
977 }
978 return;
979 }
980
981 cqr = (struct dasd_ccw_req *) intparm;
982
983 /* check for unsolicited interrupts */
984 if (cqr == NULL) {
985 MESSAGE(KERN_DEBUG,
986 "unsolicited interrupt received: bus_id %s",
987 cdev->dev.bus_id);
988 return;
989 }
990
991 device = (struct dasd_device *) cqr->device;
992 if (device == NULL ||
993 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
994 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
995 cdev->dev.bus_id);
996 return;
997 }
998
999 /* Check for clear pending */
1000 if (cqr->status == DASD_CQR_CLEAR &&
1001 irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1002 cqr->status = DASD_CQR_QUEUED;
1003 dasd_clear_timer(device);
Horst Hummel8f617012006-08-30 14:33:33 +02001004 wake_up(&dasd_flush_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 dasd_schedule_bh(device);
1006 return;
1007 }
1008
1009 /* check status - the request might have been killed by dyn detach */
1010 if (cqr->status != DASD_CQR_IN_IO) {
1011 MESSAGE(KERN_DEBUG,
1012 "invalid status: bus_id %s, status %02x",
1013 cdev->dev.bus_id, cqr->status);
1014 return;
1015 }
1016 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
1017 ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
1018
1019 /* Find out the appropriate era_action. */
Horst Hummel138c0142006-06-29 14:58:12 +02001020 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 era = dasd_era_fatal;
1022 else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
1023 irb->scsw.cstat == 0 &&
1024 !irb->esw.esw0.erw.cons)
1025 era = dasd_era_none;
1026 else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
1027 era = dasd_era_fatal; /* don't recover this request */
1028 else if (irb->esw.esw0.erw.cons)
1029 era = device->discipline->examine_error(cqr, irb);
Horst Hummel138c0142006-06-29 14:58:12 +02001030 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 era = dasd_era_recover;
1032
1033 DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
1034 expires = 0;
1035 if (era == dasd_era_none) {
1036 cqr->status = DASD_CQR_DONE;
1037 cqr->stopclk = now;
1038 /* Start first request on queue if possible -> fast_io. */
1039 if (cqr->list.next != &device->ccw_queue) {
1040 next = list_entry(cqr->list.next,
1041 struct dasd_ccw_req, list);
1042 if ((next->status == DASD_CQR_QUEUED) &&
1043 (!device->stopped)) {
1044 if (device->discipline->start_IO(next) == 0)
1045 expires = next->expires;
1046 else
1047 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1048 "Interrupt fastpath "
1049 "failed!");
1050 }
1051 }
1052 } else { /* error */
1053 memcpy(&cqr->irb, irb, sizeof (struct irb));
1054#ifdef ERP_DEBUG
1055 /* dump sense data */
1056 dasd_log_sense(cqr, irb);
1057#endif
1058 switch (era) {
1059 case dasd_era_fatal:
1060 cqr->status = DASD_CQR_FAILED;
1061 cqr->stopclk = now;
1062 break;
1063 case dasd_era_recover:
1064 cqr->status = DASD_CQR_ERROR;
1065 break;
1066 default:
1067 BUG();
1068 }
1069 }
1070 if (expires != 0)
1071 dasd_set_timer(device, expires);
1072 else
1073 dasd_clear_timer(device);
1074 dasd_schedule_bh(device);
1075}
1076
1077/*
1078 * posts the buffer_cache about a finalized request
1079 */
1080static inline void
1081dasd_end_request(struct request *req, int uptodate)
1082{
1083 if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1084 BUG();
1085 add_disk_randomness(req->rq_disk);
Tejun Heo8ffdc652006-01-06 09:49:03 +01001086 end_that_request_last(req, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089/*
1090 * Process finished error recovery ccw.
1091 */
1092static inline void
1093__dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1094{
1095 dasd_erp_fn_t erp_fn;
1096
1097 if (cqr->status == DASD_CQR_DONE)
1098 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1099 else
1100 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1101 erp_fn = device->discipline->erp_postaction(cqr);
1102 erp_fn(cqr);
1103}
1104
1105/*
1106 * Process ccw request queue.
1107 */
1108static inline void
1109__dasd_process_ccw_queue(struct dasd_device * device,
1110 struct list_head *final_queue)
1111{
1112 struct list_head *l, *n;
1113 struct dasd_ccw_req *cqr;
1114 dasd_erp_fn_t erp_fn;
1115
1116restart:
1117 /* Process request with final status. */
1118 list_for_each_safe(l, n, &device->ccw_queue) {
1119 cqr = list_entry(l, struct dasd_ccw_req, list);
1120 /* Stop list processing at the first non-final request. */
1121 if (cqr->status != DASD_CQR_DONE &&
1122 cqr->status != DASD_CQR_FAILED &&
1123 cqr->status != DASD_CQR_ERROR)
1124 break;
1125 /* Process requests with DASD_CQR_ERROR */
1126 if (cqr->status == DASD_CQR_ERROR) {
1127 if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1128 cqr->status = DASD_CQR_FAILED;
1129 cqr->stopclk = get_clock();
1130 } else {
1131 if (cqr->irb.esw.esw0.erw.cons) {
1132 erp_fn = device->discipline->
1133 erp_action(cqr);
1134 erp_fn(cqr);
1135 } else
1136 dasd_default_erp_action(cqr);
1137 }
1138 goto restart;
1139 }
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001140
1141 /* First of all call extended error reporting. */
1142 if (dasd_eer_enabled(device) &&
1143 cqr->status == DASD_CQR_FAILED) {
1144 dasd_eer_write(device, cqr, DASD_EER_FATALERROR);
1145
1146 /* restart request */
1147 cqr->status = DASD_CQR_QUEUED;
1148 cqr->retries = 255;
1149 device->stopped |= DASD_STOPPED_QUIESCE;
1150 goto restart;
1151 }
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* Process finished ERP request. */
1154 if (cqr->refers) {
1155 __dasd_process_erp(device, cqr);
1156 goto restart;
1157 }
1158
1159 /* Rechain finished requests to final queue */
1160 cqr->endclk = get_clock();
1161 list_move_tail(&cqr->list, final_queue);
1162 }
1163}
1164
1165static void
1166dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1167{
1168 struct request *req;
1169 struct dasd_device *device;
1170 int status;
1171
1172 req = (struct request *) data;
1173 device = cqr->device;
1174 dasd_profile_end(device, cqr, req);
1175 status = cqr->device->discipline->free_cp(cqr,req);
1176 spin_lock_irq(&device->request_queue_lock);
1177 dasd_end_request(req, status);
1178 spin_unlock_irq(&device->request_queue_lock);
1179}
1180
1181
1182/*
1183 * Fetch requests from the block device queue.
1184 */
1185static inline void
1186__dasd_process_blk_queue(struct dasd_device * device)
1187{
1188 request_queue_t *queue;
1189 struct request *req;
1190 struct dasd_ccw_req *cqr;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001191 int nr_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 queue = device->request_queue;
1194 /* No queue ? Then there is nothing to do. */
1195 if (queue == NULL)
1196 return;
1197
1198 /*
1199 * We requeue request from the block device queue to the ccw
1200 * queue only in two states. In state DASD_STATE_READY the
1201 * partition detection is done and we need to requeue requests
1202 * for that. State DASD_STATE_ONLINE is normal block device
1203 * operation.
1204 */
1205 if (device->state != DASD_STATE_READY &&
1206 device->state != DASD_STATE_ONLINE)
1207 return;
1208 nr_queued = 0;
1209 /* Now we try to fetch requests from the request queue */
1210 list_for_each_entry(cqr, &device->ccw_queue, list)
1211 if (cqr->status == DASD_CQR_QUEUED)
1212 nr_queued++;
1213 while (!blk_queue_plugged(queue) &&
1214 elv_next_request(queue) &&
1215 nr_queued < DASD_CHANQ_MAX_SIZE) {
1216 req = elv_next_request(queue);
Horst Hummelf24acd42005-05-01 08:58:59 -07001217
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001218 if (device->features & DASD_FEATURE_READONLY &&
1219 rq_data_dir(req) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 DBF_DEV_EVENT(DBF_ERR, device,
1221 "Rejecting write request %p",
1222 req);
1223 blkdev_dequeue_request(req);
1224 dasd_end_request(req, 0);
1225 continue;
1226 }
1227 if (device->stopped & DASD_STOPPED_DC_EIO) {
1228 blkdev_dequeue_request(req);
1229 dasd_end_request(req, 0);
1230 continue;
1231 }
1232 cqr = device->discipline->build_cp(device, req);
1233 if (IS_ERR(cqr)) {
1234 if (PTR_ERR(cqr) == -ENOMEM)
1235 break; /* terminate request queue loop */
1236 DBF_DEV_EVENT(DBF_ERR, device,
1237 "CCW creation failed (rc=%ld) "
1238 "on request %p",
1239 PTR_ERR(cqr), req);
1240 blkdev_dequeue_request(req);
1241 dasd_end_request(req, 0);
1242 continue;
1243 }
1244 cqr->callback = dasd_end_request_cb;
1245 cqr->callback_data = (void *) req;
1246 cqr->status = DASD_CQR_QUEUED;
1247 blkdev_dequeue_request(req);
1248 list_add_tail(&cqr->list, &device->ccw_queue);
1249 dasd_profile_start(device, cqr, req);
1250 nr_queued++;
1251 }
1252}
1253
1254/*
1255 * Take a look at the first request on the ccw queue and check
1256 * if it reached its expire time. If so, terminate the IO.
1257 */
1258static inline void
1259__dasd_check_expire(struct dasd_device * device)
1260{
1261 struct dasd_ccw_req *cqr;
1262
1263 if (list_empty(&device->ccw_queue))
1264 return;
1265 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1266 if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
1267 if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
Horst Hummel8f617012006-08-30 14:33:33 +02001268 DEV_MESSAGE(KERN_ERR, device,
1269 "internal error - timeout (%is) expired "
1270 "for cqr %p (%i retries left)",
1271 (cqr->expires/HZ), cqr, cqr->retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (device->discipline->term_IO(cqr) != 0)
1273 /* Hmpf, try again in 1/10 sec */
1274 dasd_set_timer(device, 10);
1275 }
1276 }
1277}
1278
1279/*
1280 * Take a look at the first request on the ccw queue and check
1281 * if it needs to be started.
1282 */
1283static inline void
1284__dasd_start_head(struct dasd_device * device)
1285{
1286 struct dasd_ccw_req *cqr;
1287 int rc;
1288
1289 if (list_empty(&device->ccw_queue))
1290 return;
1291 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001292 if (cqr->status != DASD_CQR_QUEUED)
1293 return;
1294 /* Non-temporary stop condition will trigger fail fast */
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001295 if (device->stopped & ~DASD_STOPPED_PENDING &&
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001296 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1297 (!dasd_eer_enabled(device))) {
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001298 cqr->status = DASD_CQR_FAILED;
1299 dasd_schedule_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001300 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001301 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001302 /* Don't try to start requests if device is stopped */
1303 if (device->stopped)
1304 return;
1305
1306 rc = device->discipline->start_IO(cqr);
1307 if (rc == 0)
1308 dasd_set_timer(device, cqr->expires);
1309 else if (rc == -EACCES) {
1310 dasd_schedule_bh(device);
1311 } else
1312 /* Hmpf, try again in 1/2 sec */
1313 dasd_set_timer(device, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Horst Hummel8f617012006-08-30 14:33:33 +02001316static inline int
1317_wait_for_clear(struct dasd_ccw_req *cqr)
1318{
1319 return (cqr->status == DASD_CQR_QUEUED);
1320}
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322/*
Horst Hummel8f617012006-08-30 14:33:33 +02001323 * Remove all requests from the ccw queue (all = '1') or only block device
1324 * requests in case all = '0'.
1325 * Take care of the erp-chain (chained via cqr->refers) and remove either
1326 * the whole erp-chain or none of the erp-requests.
1327 * If a request is currently running, term_IO is called and the request
1328 * is re-queued. Prior to removing the terminated request we need to wait
1329 * for the clear-interrupt.
1330 * In case termination is not possible we stop processing and just finishing
1331 * the already moved requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 */
Horst Hummel8f617012006-08-30 14:33:33 +02001333static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334dasd_flush_ccw_queue(struct dasd_device * device, int all)
1335{
Horst Hummel8f617012006-08-30 14:33:33 +02001336 struct dasd_ccw_req *cqr, *orig, *n;
1337 int rc, i;
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct list_head flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 INIT_LIST_HEAD(&flush_queue);
1342 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel8f617012006-08-30 14:33:33 +02001343 rc = 0;
1344restart:
1345 list_for_each_entry_safe(cqr, n, &device->ccw_queue, list) {
1346 /* get original request of erp request-chain */
1347 for (orig = cqr; orig->refers != NULL; orig = orig->refers);
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /* Flush all request or only block device requests? */
Horst Hummel8f617012006-08-30 14:33:33 +02001350 if (all == 0 && cqr->callback != dasd_end_request_cb &&
1351 orig->callback != dasd_end_request_cb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 continue;
Horst Hummel8f617012006-08-30 14:33:33 +02001353 }
1354 /* Check status and move request to flush_queue */
1355 switch (cqr->status) {
1356 case DASD_CQR_IN_IO:
1357 rc = device->discipline->term_IO(cqr);
1358 if (rc) {
1359 /* unable to terminate requeust */
1360 DEV_MESSAGE(KERN_ERR, device,
1361 "dasd flush ccw_queue is unable "
1362 " to terminate request %p",
1363 cqr);
1364 /* stop flush processing */
1365 goto finished;
1366 }
1367 break;
1368 case DASD_CQR_QUEUED:
1369 case DASD_CQR_ERROR:
1370 /* set request to FAILED */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 cqr->stopclk = get_clock();
Horst Hummel8f617012006-08-30 14:33:33 +02001372 cqr->status = DASD_CQR_FAILED;
1373 break;
1374 default: /* do not touch the others */
1375 break;
1376 }
1377 /* Rechain request (including erp chain) */
1378 for (i = 0; cqr != NULL; cqr = cqr->refers, i++) {
1379 cqr->endclk = get_clock();
1380 list_move_tail(&cqr->list, &flush_queue);
1381 }
1382 if (i > 1)
1383 /* moved more than one request - need to restart */
1384 goto restart;
1385 }
1386
1387finished:
1388 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1389 /* Now call the callback function of flushed requests */
1390restart_cb:
1391 list_for_each_entry_safe(cqr, n, &flush_queue, list) {
1392 if (cqr->status == DASD_CQR_CLEAR) {
1393 /* wait for clear interrupt! */
1394 wait_event(dasd_flush_wq, _wait_for_clear(cqr));
1395 cqr->status = DASD_CQR_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397 /* Process finished ERP request. */
1398 if (cqr->refers) {
1399 __dasd_process_erp(device, cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001400 /* restart list_for_xx loop since dasd_process_erp
1401 * might remove multiple elements */
1402 goto restart_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Horst Hummel8f617012006-08-30 14:33:33 +02001404 /* call the callback function */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 cqr->endclk = get_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (cqr->callback != NULL)
1407 (cqr->callback)(cqr, cqr->callback_data);
1408 }
Horst Hummel8f617012006-08-30 14:33:33 +02001409 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
1412/*
1413 * Acquire the device lock and process queues for the device.
1414 */
1415static void
1416dasd_tasklet(struct dasd_device * device)
1417{
1418 struct list_head final_queue;
1419 struct list_head *l, *n;
1420 struct dasd_ccw_req *cqr;
1421
1422 atomic_set (&device->tasklet_scheduled, 0);
1423 INIT_LIST_HEAD(&final_queue);
1424 spin_lock_irq(get_ccwdev_lock(device->cdev));
1425 /* Check expire time of first request on the ccw queue. */
1426 __dasd_check_expire(device);
1427 /* Finish off requests on ccw queue */
1428 __dasd_process_ccw_queue(device, &final_queue);
1429 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1430 /* Now call the callback function of requests with final status */
1431 list_for_each_safe(l, n, &final_queue) {
1432 cqr = list_entry(l, struct dasd_ccw_req, list);
Horst Hummelc2ba4442006-02-01 03:06:37 -08001433 list_del_init(&cqr->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (cqr->callback != NULL)
1435 (cqr->callback)(cqr, cqr->callback_data);
1436 }
1437 spin_lock_irq(&device->request_queue_lock);
1438 spin_lock(get_ccwdev_lock(device->cdev));
1439 /* Get new request from the block device request queue */
1440 __dasd_process_blk_queue(device);
1441 /* Now check if the head of the ccw queue needs to be started. */
1442 __dasd_start_head(device);
1443 spin_unlock(get_ccwdev_lock(device->cdev));
1444 spin_unlock_irq(&device->request_queue_lock);
1445 dasd_put_device(device);
1446}
1447
1448/*
1449 * Schedules a call to dasd_tasklet over the device tasklet.
1450 */
1451void
1452dasd_schedule_bh(struct dasd_device * device)
1453{
1454 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001455 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return;
1457 dasd_get_device(device);
1458 tasklet_hi_schedule(&device->tasklet);
1459}
1460
1461/*
1462 * Queue a request to the head of the ccw_queue. Start the I/O if
1463 * possible.
1464 */
1465void
1466dasd_add_request_head(struct dasd_ccw_req *req)
1467{
1468 struct dasd_device *device;
1469 unsigned long flags;
1470
1471 device = req->device;
1472 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1473 req->status = DASD_CQR_QUEUED;
1474 req->device = device;
1475 list_add(&req->list, &device->ccw_queue);
1476 /* let the bh start the request to keep them in order */
1477 dasd_schedule_bh(device);
1478 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1479}
1480
1481/*
1482 * Queue a request to the tail of the ccw_queue. Start the I/O if
1483 * possible.
1484 */
1485void
1486dasd_add_request_tail(struct dasd_ccw_req *req)
1487{
1488 struct dasd_device *device;
1489 unsigned long flags;
1490
1491 device = req->device;
1492 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1493 req->status = DASD_CQR_QUEUED;
1494 req->device = device;
1495 list_add_tail(&req->list, &device->ccw_queue);
1496 /* let the bh start the request to keep them in order */
1497 dasd_schedule_bh(device);
1498 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1499}
1500
1501/*
1502 * Wakeup callback.
1503 */
1504static void
1505dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1506{
1507 wake_up((wait_queue_head_t *) data);
1508}
1509
1510static inline int
1511_wait_for_wakeup(struct dasd_ccw_req *cqr)
1512{
1513 struct dasd_device *device;
1514 int rc;
1515
1516 device = cqr->device;
1517 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001518 rc = ((cqr->status == DASD_CQR_DONE ||
1519 cqr->status == DASD_CQR_FAILED) &&
1520 list_empty(&cqr->list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1522 return rc;
1523}
1524
1525/*
1526 * Attempts to start a special ccw queue and waits for its completion.
1527 */
1528int
1529dasd_sleep_on(struct dasd_ccw_req * cqr)
1530{
1531 wait_queue_head_t wait_q;
1532 struct dasd_device *device;
1533 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 device = cqr->device;
1536 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel138c0142006-06-29 14:58:12 +02001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 init_waitqueue_head (&wait_q);
1539 cqr->callback = dasd_wakeup_cb;
1540 cqr->callback_data = (void *) &wait_q;
1541 cqr->status = DASD_CQR_QUEUED;
1542 list_add_tail(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* let the bh start the request to keep them in order */
1545 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1548
1549 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /* Request status is either done or failed. */
1552 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1553 return rc;
1554}
1555
1556/*
1557 * Attempts to start a special ccw queue and wait interruptible
1558 * for its completion.
1559 */
1560int
1561dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1562{
1563 wait_queue_head_t wait_q;
1564 struct dasd_device *device;
1565 int rc, finished;
1566
1567 device = cqr->device;
1568 spin_lock_irq(get_ccwdev_lock(device->cdev));
1569
1570 init_waitqueue_head (&wait_q);
1571 cqr->callback = dasd_wakeup_cb;
1572 cqr->callback_data = (void *) &wait_q;
1573 cqr->status = DASD_CQR_QUEUED;
1574 list_add_tail(&cqr->list, &device->ccw_queue);
1575
1576 /* let the bh start the request to keep them in order */
1577 dasd_schedule_bh(device);
1578 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1579
1580 finished = 0;
1581 while (!finished) {
1582 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1583 if (rc != -ERESTARTSYS) {
Horst Hummelc2ba4442006-02-01 03:06:37 -08001584 /* Request is final (done or failed) */
1585 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 break;
1587 }
1588 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001589 switch (cqr->status) {
1590 case DASD_CQR_IN_IO:
1591 /* terminate runnig cqr */
1592 if (device->discipline->term_IO) {
1593 cqr->retries = -1;
1594 device->discipline->term_IO(cqr);
Horst Hummel8f617012006-08-30 14:33:33 +02001595 /* wait (non-interruptible) for final status
1596 * because signal ist still pending */
Horst Hummelc2ba4442006-02-01 03:06:37 -08001597 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1598 wait_event(wait_q, _wait_for_wakeup(cqr));
1599 spin_lock_irq(get_ccwdev_lock(device->cdev));
1600 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1601 finished = 1;
1602 }
1603 break;
1604 case DASD_CQR_QUEUED:
1605 /* request */
1606 list_del_init(&cqr->list);
1607 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 finished = 1;
Horst Hummelc2ba4442006-02-01 03:06:37 -08001609 break;
1610 default:
1611 /* cqr with 'non-interruptable' status - just wait */
1612 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1615 }
1616 return rc;
1617}
1618
1619/*
1620 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1621 * for eckd devices) the currently running request has to be terminated
1622 * and be put back to status queued, before the special request is added
1623 * to the head of the queue. Then the special request is waited on normally.
1624 */
1625static inline int
1626_dasd_term_running_cqr(struct dasd_device *device)
1627{
1628 struct dasd_ccw_req *cqr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 if (list_empty(&device->ccw_queue))
1631 return 0;
1632 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Horst Hummel8f617012006-08-30 14:33:33 +02001633 return device->discipline->term_IO(cqr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636int
1637dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1638{
1639 wait_queue_head_t wait_q;
1640 struct dasd_device *device;
1641 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 device = cqr->device;
1644 spin_lock_irq(get_ccwdev_lock(device->cdev));
1645 rc = _dasd_term_running_cqr(device);
1646 if (rc) {
1647 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1648 return rc;
1649 }
Horst Hummel138c0142006-06-29 14:58:12 +02001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 init_waitqueue_head (&wait_q);
1652 cqr->callback = dasd_wakeup_cb;
1653 cqr->callback_data = (void *) &wait_q;
1654 cqr->status = DASD_CQR_QUEUED;
1655 list_add(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /* let the bh start the request to keep them in order */
1658 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1661
1662 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /* Request status is either done or failed. */
1665 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1666 return rc;
1667}
1668
1669/*
1670 * Cancels a request that was started with dasd_sleep_on_req.
1671 * This is useful to timeout requests. The request will be
1672 * terminated if it is currently in i/o.
1673 * Returns 1 if the request has been terminated.
1674 */
1675int
1676dasd_cancel_req(struct dasd_ccw_req *cqr)
1677{
1678 struct dasd_device *device = cqr->device;
1679 unsigned long flags;
1680 int rc;
1681
1682 rc = 0;
1683 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1684 switch (cqr->status) {
1685 case DASD_CQR_QUEUED:
1686 /* request was not started - just set to failed */
1687 cqr->status = DASD_CQR_FAILED;
1688 break;
1689 case DASD_CQR_IN_IO:
1690 /* request in IO - terminate IO and release again */
1691 if (device->discipline->term_IO(cqr) != 0)
1692 /* what to do if unable to terminate ??????
1693 e.g. not _IN_IO */
1694 cqr->status = DASD_CQR_FAILED;
1695 cqr->stopclk = get_clock();
1696 rc = 1;
1697 break;
1698 case DASD_CQR_DONE:
1699 case DASD_CQR_FAILED:
1700 /* already finished - do nothing */
1701 break;
1702 default:
1703 DEV_MESSAGE(KERN_ALERT, device,
1704 "invalid status %02x in request",
1705 cqr->status);
1706 BUG();
1707
1708 }
1709 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1710 dasd_schedule_bh(device);
1711 return rc;
1712}
1713
1714/*
1715 * SECTION: Block device operations (request queue, partitions, open, release).
1716 */
1717
1718/*
1719 * Dasd request queue function. Called from ll_rw_blk.c
1720 */
1721static void
1722do_dasd_request(request_queue_t * queue)
1723{
1724 struct dasd_device *device;
1725
1726 device = (struct dasd_device *) queue->queuedata;
1727 spin_lock(get_ccwdev_lock(device->cdev));
1728 /* Get new request from the block device request queue */
1729 __dasd_process_blk_queue(device);
1730 /* Now check if the head of the ccw queue needs to be started. */
1731 __dasd_start_head(device);
1732 spin_unlock(get_ccwdev_lock(device->cdev));
1733}
1734
1735/*
1736 * Allocate and initialize request queue and default I/O scheduler.
1737 */
1738static int
1739dasd_alloc_queue(struct dasd_device * device)
1740{
1741 int rc;
1742
1743 device->request_queue = blk_init_queue(do_dasd_request,
1744 &device->request_queue_lock);
1745 if (device->request_queue == NULL)
1746 return -ENOMEM;
1747
1748 device->request_queue->queuedata = device;
1749
1750 elevator_exit(device->request_queue->elevator);
1751 rc = elevator_init(device->request_queue, "deadline");
1752 if (rc) {
1753 blk_cleanup_queue(device->request_queue);
1754 return rc;
1755 }
1756 return 0;
1757}
1758
1759/*
1760 * Allocate and initialize request queue.
1761 */
1762static void
1763dasd_setup_queue(struct dasd_device * device)
1764{
1765 int max;
1766
1767 blk_queue_hardsect_size(device->request_queue, device->bp_block);
1768 max = device->discipline->max_blocks << device->s2b_shift;
1769 blk_queue_max_sectors(device->request_queue, max);
1770 blk_queue_max_phys_segments(device->request_queue, -1L);
1771 blk_queue_max_hw_segments(device->request_queue, -1L);
1772 blk_queue_max_segment_size(device->request_queue, -1L);
1773 blk_queue_segment_boundary(device->request_queue, -1L);
Heiko Carstensed68cb32006-01-14 13:21:05 -08001774 blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777/*
1778 * Deactivate and free request queue.
1779 */
1780static void
1781dasd_free_queue(struct dasd_device * device)
1782{
1783 if (device->request_queue) {
1784 blk_cleanup_queue(device->request_queue);
1785 device->request_queue = NULL;
1786 }
1787}
1788
1789/*
1790 * Flush request on the request queue.
1791 */
1792static void
1793dasd_flush_request_queue(struct dasd_device * device)
1794{
1795 struct request *req;
1796
1797 if (!device->request_queue)
1798 return;
Horst Hummel138c0142006-06-29 14:58:12 +02001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 spin_lock_irq(&device->request_queue_lock);
Horst Hummel8f617012006-08-30 14:33:33 +02001801 while ((req = elv_next_request(device->request_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 blkdev_dequeue_request(req);
Horst Hummelebc45992006-08-09 10:30:22 +02001803 dasd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 spin_unlock_irq(&device->request_queue_lock);
1806}
1807
1808static int
1809dasd_open(struct inode *inp, struct file *filp)
1810{
1811 struct gendisk *disk = inp->i_bdev->bd_disk;
1812 struct dasd_device *device = disk->private_data;
1813 int rc;
1814
1815 atomic_inc(&device->open_count);
1816 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1817 rc = -ENODEV;
1818 goto unlock;
1819 }
1820
1821 if (!try_module_get(device->discipline->owner)) {
1822 rc = -EINVAL;
1823 goto unlock;
1824 }
1825
1826 if (dasd_probeonly) {
1827 DEV_MESSAGE(KERN_INFO, device, "%s",
1828 "No access to device due to probeonly mode");
1829 rc = -EPERM;
1830 goto out;
1831 }
1832
Horst Hummel90f00942006-03-07 21:55:39 -08001833 if (device->state <= DASD_STATE_BASIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1835 " Cannot open unrecognized device");
1836 rc = -ENODEV;
1837 goto out;
1838 }
1839
1840 return 0;
1841
1842out:
1843 module_put(device->discipline->owner);
1844unlock:
1845 atomic_dec(&device->open_count);
1846 return rc;
1847}
1848
1849static int
1850dasd_release(struct inode *inp, struct file *filp)
1851{
1852 struct gendisk *disk = inp->i_bdev->bd_disk;
1853 struct dasd_device *device = disk->private_data;
1854
1855 atomic_dec(&device->open_count);
1856 module_put(device->discipline->owner);
1857 return 0;
1858}
1859
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001860/*
1861 * Return disk geometry.
1862 */
1863static int
1864dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1865{
1866 struct dasd_device *device;
1867
1868 device = bdev->bd_disk->private_data;
1869 if (!device)
1870 return -ENODEV;
1871
1872 if (!device->discipline ||
1873 !device->discipline->fill_geometry)
1874 return -EINVAL;
1875
1876 device->discipline->fill_geometry(device, geo);
1877 geo->start = get_start_sect(bdev) >> device->s2b_shift;
1878 return 0;
1879}
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881struct block_device_operations
1882dasd_device_operations = {
1883 .owner = THIS_MODULE,
1884 .open = dasd_open,
1885 .release = dasd_release,
1886 .ioctl = dasd_ioctl,
Christoph Hellwig82620372006-01-09 20:52:12 -08001887 .compat_ioctl = dasd_compat_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001888 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889};
1890
1891
1892static void
1893dasd_exit(void)
1894{
1895#ifdef CONFIG_PROC_FS
1896 dasd_proc_exit();
1897#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001898 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07001899 if (dasd_page_cache != NULL) {
1900 kmem_cache_destroy(dasd_page_cache);
1901 dasd_page_cache = NULL;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 dasd_gendisk_exit();
1904 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (dasd_debug_area != NULL) {
1906 debug_unregister(dasd_debug_area);
1907 dasd_debug_area = NULL;
1908 }
1909}
1910
1911/*
1912 * SECTION: common functions for ccw_driver use
1913 */
1914
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001915/*
1916 * Initial attempt at a probe function. this can be simplified once
1917 * the other detection code is gone.
1918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919int
1920dasd_generic_probe (struct ccw_device *cdev,
1921 struct dasd_discipline *discipline)
1922{
1923 int ret;
1924
Horst Hummel40545572006-06-29 15:08:18 +02001925 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
1926 if (ret) {
1927 printk(KERN_WARNING
1928 "dasd_generic_probe: could not set ccw-device options "
1929 "for %s\n", cdev->dev.bus_id);
1930 return ret;
1931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 ret = dasd_add_sysfs_files(cdev);
1933 if (ret) {
1934 printk(KERN_WARNING
1935 "dasd_generic_probe: could not add sysfs entries "
1936 "for %s\n", cdev->dev.bus_id);
Horst Hummel40545572006-06-29 15:08:18 +02001937 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Horst Hummel40545572006-06-29 15:08:18 +02001939 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Horst Hummel40545572006-06-29 15:08:18 +02001941 /*
1942 * Automatically online either all dasd devices (dasd_autodetect)
1943 * or all devices specified with dasd= parameters during
1944 * initial probe.
1945 */
1946 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
1947 (dasd_autodetect && dasd_busid_known(cdev->dev.bus_id) != 0))
1948 ret = ccw_device_set_online(cdev);
1949 if (ret)
1950 printk(KERN_WARNING
1951 "dasd_generic_probe: could not initially online "
1952 "ccw-device %s\n", cdev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return ret;
1954}
1955
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001956/*
1957 * This will one day be called from a global not_oper handler.
1958 * It is also used by driver_unregister during module unload.
1959 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960void
1961dasd_generic_remove (struct ccw_device *cdev)
1962{
1963 struct dasd_device *device;
1964
Horst Hummel59afda72005-05-16 21:53:39 -07001965 cdev->handler = NULL;
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 dasd_remove_sysfs_files(cdev);
1968 device = dasd_device_from_cdev(cdev);
1969 if (IS_ERR(device))
1970 return;
1971 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1972 /* Already doing offline processing */
1973 dasd_put_device(device);
1974 return;
1975 }
1976 /*
1977 * This device is removed unconditionally. Set offline
1978 * flag to prevent dasd_open from opening it while it is
1979 * no quite down yet.
1980 */
1981 dasd_set_target_state(device, DASD_STATE_NEW);
1982 /* dasd_delete_device destroys the device reference. */
1983 dasd_delete_device(device);
1984}
1985
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001986/*
1987 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001989 * or the user has started activation through sysfs.
1990 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991int
1992dasd_generic_set_online (struct ccw_device *cdev,
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001993 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001996 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001998 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07001999
Horst Hummel40545572006-06-29 15:08:18 +02002000 /* first online clears initial online feature flag */
2001 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 device = dasd_create_device(cdev);
2003 if (IS_ERR(device))
2004 return PTR_ERR(device);
2005
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002006 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07002007 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (!dasd_diag_discipline_pointer) {
2009 printk (KERN_WARNING
2010 "dasd_generic couldn't online device %s "
2011 "- discipline DIAG not available\n",
2012 cdev->dev.bus_id);
2013 dasd_delete_device(device);
2014 return -ENODEV;
2015 }
2016 discipline = dasd_diag_discipline_pointer;
2017 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002018 if (!try_module_get(base_discipline->owner)) {
2019 dasd_delete_device(device);
2020 return -EINVAL;
2021 }
2022 if (!try_module_get(discipline->owner)) {
2023 module_put(base_discipline->owner);
2024 dasd_delete_device(device);
2025 return -EINVAL;
2026 }
2027 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 device->discipline = discipline;
2029
2030 rc = discipline->check_device(device);
2031 if (rc) {
2032 printk (KERN_WARNING
2033 "dasd_generic couldn't online device %s "
2034 "with discipline %s rc=%i\n",
2035 cdev->dev.bus_id, discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08002036 module_put(discipline->owner);
2037 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 dasd_delete_device(device);
2039 return rc;
2040 }
2041
2042 dasd_set_target_state(device, DASD_STATE_ONLINE);
2043 if (device->state <= DASD_STATE_KNOWN) {
2044 printk (KERN_WARNING
2045 "dasd_generic discipline not found for %s\n",
2046 cdev->dev.bus_id);
2047 rc = -ENODEV;
2048 dasd_set_target_state(device, DASD_STATE_NEW);
2049 dasd_delete_device(device);
2050 } else
2051 pr_debug("dasd_generic device %s found\n",
2052 cdev->dev.bus_id);
2053
2054 /* FIXME: we have to wait for the root device but we don't want
2055 * to wait for each single device but for all at once. */
2056 wait_event(dasd_init_waitq, _wait_for_device(device));
2057
2058 dasd_put_device(device);
2059
2060 return rc;
2061}
2062
2063int
2064dasd_generic_set_offline (struct ccw_device *cdev)
2065{
2066 struct dasd_device *device;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002067 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 device = dasd_device_from_cdev(cdev);
2070 if (IS_ERR(device))
2071 return PTR_ERR(device);
2072 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2073 /* Already doing offline processing */
2074 dasd_put_device(device);
2075 return 0;
2076 }
2077 /*
2078 * We must make sure that this device is currently not in use.
2079 * The open_count is increased for every opener, that includes
2080 * the blkdev_get in dasd_scan_partitions. We are only interested
2081 * in the other openers.
2082 */
2083 max_count = device->bdev ? 0 : -1;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002084 open_count = (int) atomic_read(&device->open_count);
2085 if (open_count > max_count) {
2086 if (open_count > 0)
2087 printk (KERN_WARNING "Can't offline dasd device with "
2088 "open count = %i.\n",
2089 open_count);
2090 else
2091 printk (KERN_WARNING "%s",
2092 "Can't offline dasd device due to internal "
2093 "use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2095 dasd_put_device(device);
2096 return -EBUSY;
2097 }
2098 dasd_set_target_state(device, DASD_STATE_NEW);
2099 /* dasd_delete_device destroys the device reference. */
2100 dasd_delete_device(device);
2101
2102 return 0;
2103}
2104
2105int
2106dasd_generic_notify(struct ccw_device *cdev, int event)
2107{
2108 struct dasd_device *device;
2109 struct dasd_ccw_req *cqr;
2110 unsigned long flags;
2111 int ret;
2112
2113 device = dasd_device_from_cdev(cdev);
2114 if (IS_ERR(device))
2115 return 0;
2116 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
2117 ret = 0;
2118 switch (event) {
2119 case CIO_GONE:
2120 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002121 /* First of all call extended error reporting. */
2122 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (device->state < DASD_STATE_BASIC)
2125 break;
2126 /* Device is active. We want to keep it. */
2127 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
2128 list_for_each_entry(cqr, &device->ccw_queue, list)
2129 if (cqr->status == DASD_CQR_IN_IO)
2130 cqr->status = DASD_CQR_FAILED;
2131 device->stopped |= DASD_STOPPED_DC_EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 } else {
2133 list_for_each_entry(cqr, &device->ccw_queue, list)
2134 if (cqr->status == DASD_CQR_IN_IO) {
2135 cqr->status = DASD_CQR_QUEUED;
2136 cqr->retries++;
2137 }
2138 device->stopped |= DASD_STOPPED_DC_WAIT;
2139 dasd_set_timer(device, 0);
2140 }
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002141 dasd_schedule_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 ret = 1;
2143 break;
2144 case CIO_OPER:
2145 /* FIXME: add a sanity check. */
2146 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
2147 dasd_schedule_bh(device);
2148 ret = 1;
2149 break;
2150 }
2151 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2152 dasd_put_device(device);
2153 return ret;
2154}
2155
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157static int __init
2158dasd_init(void)
2159{
2160 int rc;
2161
2162 init_waitqueue_head(&dasd_init_waitq);
Horst Hummel8f617012006-08-30 14:33:33 +02002163 init_waitqueue_head(&dasd_flush_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Michael Holzheu66a464d2005-06-25 14:55:33 -07002166 dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (dasd_debug_area == NULL) {
2168 rc = -ENOMEM;
2169 goto failed;
2170 }
2171 debug_register_view(dasd_debug_area, &debug_sprintf_view);
2172 debug_set_level(dasd_debug_area, DBF_EMERG);
2173
2174 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2175
2176 dasd_diag_discipline_pointer = NULL;
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 rc = dasd_devmap_init();
2179 if (rc)
2180 goto failed;
2181 rc = dasd_gendisk_init();
2182 if (rc)
2183 goto failed;
2184 rc = dasd_parse();
2185 if (rc)
2186 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002187 rc = dasd_eer_init();
2188 if (rc)
2189 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#ifdef CONFIG_PROC_FS
2191 rc = dasd_proc_init();
2192 if (rc)
2193 goto failed;
2194#endif
2195
2196 return 0;
2197failed:
2198 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2199 dasd_exit();
2200 return rc;
2201}
2202
2203module_init(dasd_init);
2204module_exit(dasd_exit);
2205
2206EXPORT_SYMBOL(dasd_debug_area);
2207EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2208
2209EXPORT_SYMBOL(dasd_add_request_head);
2210EXPORT_SYMBOL(dasd_add_request_tail);
2211EXPORT_SYMBOL(dasd_cancel_req);
2212EXPORT_SYMBOL(dasd_clear_timer);
2213EXPORT_SYMBOL(dasd_enable_device);
2214EXPORT_SYMBOL(dasd_int_handler);
2215EXPORT_SYMBOL(dasd_kfree_request);
2216EXPORT_SYMBOL(dasd_kick_device);
2217EXPORT_SYMBOL(dasd_kmalloc_request);
2218EXPORT_SYMBOL(dasd_schedule_bh);
2219EXPORT_SYMBOL(dasd_set_target_state);
2220EXPORT_SYMBOL(dasd_set_timer);
2221EXPORT_SYMBOL(dasd_sfree_request);
2222EXPORT_SYMBOL(dasd_sleep_on);
2223EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2224EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2225EXPORT_SYMBOL(dasd_smalloc_request);
2226EXPORT_SYMBOL(dasd_start_IO);
2227EXPORT_SYMBOL(dasd_term_IO);
2228
2229EXPORT_SYMBOL_GPL(dasd_generic_probe);
2230EXPORT_SYMBOL_GPL(dasd_generic_remove);
2231EXPORT_SYMBOL_GPL(dasd_generic_notify);
2232EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2233EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234