blob: d8e9b95f0a1a374875c9574cb99af5b0ef5e4968 [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 *);
55static void dasd_flush_ccw_queue(struct dasd_device *, int);
56static 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;
63
64/*
65 * Allocate memory for a new device structure.
66 */
67struct dasd_device *
68dasd_alloc_device(void)
69{
70 struct dasd_device *device;
71
Eric Sesterhenn88abaab2006-03-24 03:15:31 -080072 device = kzalloc(sizeof (struct dasd_device), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (device == NULL)
74 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* open_count = 0 means device online but not in use */
76 atomic_set(&device->open_count, -1);
77
78 /* Get two pages for normal block device operations. */
79 device->ccw_mem = (void *) __get_free_pages(GFP_ATOMIC | GFP_DMA, 1);
80 if (device->ccw_mem == NULL) {
81 kfree(device);
82 return ERR_PTR(-ENOMEM);
83 }
84 /* Get one page for error recovery. */
85 device->erp_mem = (void *) get_zeroed_page(GFP_ATOMIC | GFP_DMA);
86 if (device->erp_mem == NULL) {
87 free_pages((unsigned long) device->ccw_mem, 1);
88 kfree(device);
89 return ERR_PTR(-ENOMEM);
90 }
91
92 dasd_init_chunklist(&device->ccw_chunks, device->ccw_mem, PAGE_SIZE*2);
93 dasd_init_chunklist(&device->erp_chunks, device->erp_mem, PAGE_SIZE);
94 spin_lock_init(&device->mem_lock);
95 spin_lock_init(&device->request_queue_lock);
96 atomic_set (&device->tasklet_scheduled, 0);
Horst Hummel138c0142006-06-29 14:58:12 +020097 tasklet_init(&device->tasklet,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 (void (*)(unsigned long)) dasd_tasklet,
99 (unsigned long) device);
100 INIT_LIST_HEAD(&device->ccw_queue);
101 init_timer(&device->timer);
102 INIT_WORK(&device->kick_work, do_kick_device, device);
103 device->state = DASD_STATE_NEW;
104 device->target = DASD_STATE_NEW;
105
106 return device;
107}
108
109/*
110 * Free memory of a device structure.
111 */
112void
113dasd_free_device(struct dasd_device *device)
114{
Jesper Juhl17fd6822005-11-07 01:01:30 -0800115 kfree(device->private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 free_page((unsigned long) device->erp_mem);
117 free_pages((unsigned long) device->ccw_mem, 1);
118 kfree(device);
119}
120
121/*
122 * Make a new device known to the system.
123 */
124static inline int
125dasd_state_new_to_known(struct dasd_device *device)
126{
127 int rc;
128
129 /*
Horst Hummel138c0142006-06-29 14:58:12 +0200130 * As long as the device is not in state DASD_STATE_NEW we want to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * keep the reference count > 0.
132 */
133 dasd_get_device(device);
134
135 rc = dasd_alloc_queue(device);
136 if (rc) {
137 dasd_put_device(device);
138 return rc;
139 }
140
141 device->state = DASD_STATE_KNOWN;
142 return 0;
143}
144
145/*
146 * Let the system forget about a device.
147 */
148static inline void
149dasd_state_known_to_new(struct dasd_device * device)
150{
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800151 /* Disable extended error reporting for this device. */
152 dasd_eer_disable(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Forget the discipline information. */
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800154 if (device->discipline)
155 module_put(device->discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 device->discipline = NULL;
Peter Oberparleiteraa888612006-02-20 18:28:13 -0800157 if (device->base_discipline)
158 module_put(device->base_discipline->owner);
159 device->base_discipline = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 device->state = DASD_STATE_NEW;
161
162 dasd_free_queue(device);
163
164 /* Give up reference we took in dasd_state_new_to_known. */
165 dasd_put_device(device);
166}
167
168/*
169 * Request the irq line for the device.
170 */
171static inline int
172dasd_state_known_to_basic(struct dasd_device * device)
173{
174 int rc;
175
176 /* Allocate and register gendisk structure. */
177 rc = dasd_gendisk_alloc(device);
178 if (rc)
179 return rc;
180
181 /* register 'device' debug area, used for all DBF_DEV_XXX calls */
Michael Holzheu66a464d2005-06-25 14:55:33 -0700182 device->debug_area = debug_register(device->cdev->dev.bus_id, 1, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 8 * sizeof (long));
184 debug_register_view(device->debug_area, &debug_sprintf_view);
185 debug_set_level(device->debug_area, DBF_EMERG);
186 DBF_DEV_EVENT(DBF_EMERG, device, "%s", "debug area created");
187
188 device->state = DASD_STATE_BASIC;
189 return 0;
190}
191
192/*
193 * Release the irq line for the device. Terminate any running i/o.
194 */
195static inline void
196dasd_state_basic_to_known(struct dasd_device * device)
197{
198 dasd_gendisk_free(device);
199 dasd_flush_ccw_queue(device, 1);
200 DBF_DEV_EVENT(DBF_EMERG, device, "%p debug area deleted", device);
201 if (device->debug_area != NULL) {
202 debug_unregister(device->debug_area);
203 device->debug_area = NULL;
204 }
205 device->state = DASD_STATE_KNOWN;
206}
207
208/*
209 * Do the initial analysis. The do_analysis function may return
210 * -EAGAIN in which case the device keeps the state DASD_STATE_BASIC
211 * until the discipline decides to continue the startup sequence
212 * by calling the function dasd_change_state. The eckd disciplines
213 * uses this to start a ccw that detects the format. The completion
214 * interrupt for this detection ccw uses the kernel event daemon to
215 * trigger the call to dasd_change_state. All this is done in the
216 * discipline code, see dasd_eckd.c.
Horst Hummel90f00942006-03-07 21:55:39 -0800217 * After the analysis ccw is done (do_analysis returned 0) the block
218 * device is setup.
219 * In case the analysis returns an error, the device setup is stopped
220 * (a fake disk was already added to allow formatting).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
222static inline int
223dasd_state_basic_to_ready(struct dasd_device * device)
224{
225 int rc;
226
227 rc = 0;
228 if (device->discipline->do_analysis != NULL)
229 rc = device->discipline->do_analysis(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800230 if (rc) {
231 if (rc != -EAGAIN)
232 device->state = DASD_STATE_UNFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return rc;
Horst Hummel90f00942006-03-07 21:55:39 -0800234 }
235 /* make disk known with correct capacity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 dasd_setup_queue(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800237 set_capacity(device->gdp, device->blocks << device->s2b_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 device->state = DASD_STATE_READY;
Horst Hummel90f00942006-03-07 21:55:39 -0800239 rc = dasd_scan_partitions(device);
240 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 device->state = DASD_STATE_BASIC;
Horst Hummel90f00942006-03-07 21:55:39 -0800242 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245/*
246 * Remove device from block device layer. Destroy dirty buffers.
247 * Forget format information. Check if the target level is basic
248 * and if it is create fake disk for formatting.
249 */
250static inline void
251dasd_state_ready_to_basic(struct dasd_device * device)
252{
253 dasd_flush_ccw_queue(device, 0);
254 dasd_destroy_partitions(device);
255 dasd_flush_request_queue(device);
256 device->blocks = 0;
257 device->bp_block = 0;
258 device->s2b_shift = 0;
259 device->state = DASD_STATE_BASIC;
260}
261
262/*
Horst Hummel90f00942006-03-07 21:55:39 -0800263 * Back to basic.
264 */
265static inline void
266dasd_state_unfmt_to_basic(struct dasd_device * device)
267{
268 device->state = DASD_STATE_BASIC;
269}
270
271/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * Make the device online and schedule the bottom half to start
273 * the requeueing of requests from the linux request queue to the
274 * ccw queue.
275 */
276static inline int
277dasd_state_ready_to_online(struct dasd_device * device)
278{
279 device->state = DASD_STATE_ONLINE;
280 dasd_schedule_bh(device);
281 return 0;
282}
283
284/*
285 * Stop the requeueing of requests again.
286 */
287static inline void
288dasd_state_online_to_ready(struct dasd_device * device)
289{
290 device->state = DASD_STATE_READY;
291}
292
293/*
294 * Device startup state changes.
295 */
296static inline int
297dasd_increase_state(struct dasd_device *device)
298{
299 int rc;
300
301 rc = 0;
302 if (device->state == DASD_STATE_NEW &&
303 device->target >= DASD_STATE_KNOWN)
304 rc = dasd_state_new_to_known(device);
305
306 if (!rc &&
307 device->state == DASD_STATE_KNOWN &&
308 device->target >= DASD_STATE_BASIC)
309 rc = dasd_state_known_to_basic(device);
310
311 if (!rc &&
312 device->state == DASD_STATE_BASIC &&
313 device->target >= DASD_STATE_READY)
314 rc = dasd_state_basic_to_ready(device);
315
316 if (!rc &&
Horst Hummel39ccf952006-04-27 18:40:10 -0700317 device->state == DASD_STATE_UNFMT &&
318 device->target > DASD_STATE_UNFMT)
319 rc = -EPERM;
320
321 if (!rc &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 device->state == DASD_STATE_READY &&
323 device->target >= DASD_STATE_ONLINE)
324 rc = dasd_state_ready_to_online(device);
325
326 return rc;
327}
328
329/*
330 * Device shutdown state changes.
331 */
332static inline int
333dasd_decrease_state(struct dasd_device *device)
334{
335 if (device->state == DASD_STATE_ONLINE &&
336 device->target <= DASD_STATE_READY)
337 dasd_state_online_to_ready(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (device->state == DASD_STATE_READY &&
340 device->target <= DASD_STATE_BASIC)
341 dasd_state_ready_to_basic(device);
Horst Hummel90f00942006-03-07 21:55:39 -0800342
343 if (device->state == DASD_STATE_UNFMT &&
344 device->target <= DASD_STATE_BASIC)
345 dasd_state_unfmt_to_basic(device);
346
347 if (device->state == DASD_STATE_BASIC &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 device->target <= DASD_STATE_KNOWN)
349 dasd_state_basic_to_known(device);
Horst Hummel138c0142006-06-29 14:58:12 +0200350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (device->state == DASD_STATE_KNOWN &&
352 device->target <= DASD_STATE_NEW)
353 dasd_state_known_to_new(device);
354
355 return 0;
356}
357
358/*
359 * This is the main startup/shutdown routine.
360 */
361static void
362dasd_change_state(struct dasd_device *device)
363{
364 int rc;
365
366 if (device->state == device->target)
367 /* Already where we want to go today... */
368 return;
369 if (device->state < device->target)
370 rc = dasd_increase_state(device);
371 else
372 rc = dasd_decrease_state(device);
373 if (rc && rc != -EAGAIN)
374 device->target = device->state;
375
376 if (device->state == device->target)
377 wake_up(&dasd_init_waitq);
378}
379
380/*
381 * Kick starter for devices that did not complete the startup/shutdown
382 * procedure or were sleeping because of a pending state.
383 * dasd_kick_device will schedule a call do do_kick_device to the kernel
384 * event daemon.
385 */
386static void
387do_kick_device(void *data)
388{
389 struct dasd_device *device;
390
391 device = (struct dasd_device *) data;
392 dasd_change_state(device);
393 dasd_schedule_bh(device);
394 dasd_put_device(device);
395}
396
397void
398dasd_kick_device(struct dasd_device *device)
399{
400 dasd_get_device(device);
401 /* queue call to dasd_kick_device to the kernel event daemon. */
402 schedule_work(&device->kick_work);
403}
404
405/*
406 * Set the target state for a device and starts the state change.
407 */
408void
409dasd_set_target_state(struct dasd_device *device, int target)
410{
411 /* If we are in probeonly mode stop at DASD_STATE_READY. */
412 if (dasd_probeonly && target > DASD_STATE_READY)
413 target = DASD_STATE_READY;
414 if (device->target != target) {
415 if (device->state == target)
416 wake_up(&dasd_init_waitq);
417 device->target = target;
418 }
419 if (device->state != device->target)
420 dasd_change_state(device);
421}
422
423/*
424 * Enable devices with device numbers in [from..to].
425 */
426static inline int
427_wait_for_device(struct dasd_device *device)
428{
429 return (device->state == device->target);
430}
431
432void
433dasd_enable_device(struct dasd_device *device)
434{
435 dasd_set_target_state(device, DASD_STATE_ONLINE);
436 if (device->state <= DASD_STATE_KNOWN)
437 /* No discipline for device found. */
438 dasd_set_target_state(device, DASD_STATE_NEW);
439 /* Now wait for the devices to come up. */
440 wait_event(dasd_init_waitq, _wait_for_device(device));
441}
442
443/*
444 * SECTION: device operation (interrupt handler, start i/o, term i/o ...)
445 */
446#ifdef CONFIG_DASD_PROFILE
447
448struct dasd_profile_info_t dasd_global_profile;
449unsigned int dasd_profile_level = DASD_PROFILE_OFF;
450
451/*
452 * Increments counter in global and local profiling structures.
453 */
454#define dasd_profile_counter(value, counter, device) \
455{ \
456 int index; \
457 for (index = 0; index < 31 && value >> (2+index); index++); \
458 dasd_global_profile.counter[index]++; \
459 device->profile.counter[index]++; \
460}
461
462/*
463 * Add profiling information for cqr before execution.
464 */
465static inline void
466dasd_profile_start(struct dasd_device *device, struct dasd_ccw_req * cqr,
467 struct request *req)
468{
469 struct list_head *l;
470 unsigned int counter;
471
472 if (dasd_profile_level != DASD_PROFILE_ON)
473 return;
474
475 /* count the length of the chanq for statistics */
476 counter = 0;
477 list_for_each(l, &device->ccw_queue)
478 if (++counter >= 31)
479 break;
480 dasd_global_profile.dasd_io_nr_req[counter]++;
481 device->profile.dasd_io_nr_req[counter]++;
482}
483
484/*
485 * Add profiling information for cqr after execution.
486 */
487static inline void
488dasd_profile_end(struct dasd_device *device, struct dasd_ccw_req * cqr,
489 struct request *req)
490{
491 long strtime, irqtime, endtime, tottime; /* in microseconds */
492 long tottimeps, sectors;
493
494 if (dasd_profile_level != DASD_PROFILE_ON)
495 return;
496
497 sectors = req->nr_sectors;
498 if (!cqr->buildclk || !cqr->startclk ||
499 !cqr->stopclk || !cqr->endclk ||
500 !sectors)
501 return;
502
503 strtime = ((cqr->startclk - cqr->buildclk) >> 12);
504 irqtime = ((cqr->stopclk - cqr->startclk) >> 12);
505 endtime = ((cqr->endclk - cqr->stopclk) >> 12);
506 tottime = ((cqr->endclk - cqr->buildclk) >> 12);
507 tottimeps = tottime / sectors;
508
509 if (!dasd_global_profile.dasd_io_reqs)
510 memset(&dasd_global_profile, 0,
511 sizeof (struct dasd_profile_info_t));
512 dasd_global_profile.dasd_io_reqs++;
513 dasd_global_profile.dasd_io_sects += sectors;
514
515 if (!device->profile.dasd_io_reqs)
516 memset(&device->profile, 0,
517 sizeof (struct dasd_profile_info_t));
518 device->profile.dasd_io_reqs++;
519 device->profile.dasd_io_sects += sectors;
520
521 dasd_profile_counter(sectors, dasd_io_secs, device);
522 dasd_profile_counter(tottime, dasd_io_times, device);
523 dasd_profile_counter(tottimeps, dasd_io_timps, device);
524 dasd_profile_counter(strtime, dasd_io_time1, device);
525 dasd_profile_counter(irqtime, dasd_io_time2, device);
526 dasd_profile_counter(irqtime / sectors, dasd_io_time2ps, device);
527 dasd_profile_counter(endtime, dasd_io_time3, device);
528}
529#else
530#define dasd_profile_start(device, cqr, req) do {} while (0)
531#define dasd_profile_end(device, cqr, req) do {} while (0)
532#endif /* CONFIG_DASD_PROFILE */
533
534/*
535 * Allocate memory for a channel program with 'cplength' channel
536 * command words and 'datasize' additional space. There are two
537 * variantes: 1) dasd_kmalloc_request uses kmalloc to get the needed
538 * memory and 2) dasd_smalloc_request uses the static ccw memory
539 * that gets allocated for each device.
540 */
541struct dasd_ccw_req *
542dasd_kmalloc_request(char *magic, int cplength, int datasize,
543 struct dasd_device * device)
544{
545 struct dasd_ccw_req *cqr;
546
547 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100548 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
549 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800551 cqr = kzalloc(sizeof(struct dasd_ccw_req), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (cqr == NULL)
553 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 cqr->cpaddr = NULL;
555 if (cplength > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800556 cqr->cpaddr = kcalloc(cplength, sizeof(struct ccw1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 GFP_ATOMIC | GFP_DMA);
558 if (cqr->cpaddr == NULL) {
559 kfree(cqr);
560 return ERR_PTR(-ENOMEM);
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 cqr->data = NULL;
564 if (datasize > 0) {
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800565 cqr->data = kzalloc(datasize, GFP_ATOMIC | GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (cqr->data == NULL) {
Jesper Juhl17fd6822005-11-07 01:01:30 -0800567 kfree(cqr->cpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 kfree(cqr);
569 return ERR_PTR(-ENOMEM);
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 strncpy((char *) &cqr->magic, magic, 4);
573 ASCEBC((char *) &cqr->magic, 4);
574 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
575 dasd_get_device(device);
576 return cqr;
577}
578
579struct dasd_ccw_req *
580dasd_smalloc_request(char *magic, int cplength, int datasize,
581 struct dasd_device * device)
582{
583 unsigned long flags;
584 struct dasd_ccw_req *cqr;
585 char *data;
586 int size;
587
588 /* Sanity checks */
Eric Sesterhenn7ac1e872006-03-24 18:48:13 +0100589 BUG_ON( magic == NULL || datasize > PAGE_SIZE ||
590 (cplength*sizeof(struct ccw1)) > PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 size = (sizeof(struct dasd_ccw_req) + 7L) & -8L;
593 if (cplength > 0)
594 size += cplength * sizeof(struct ccw1);
595 if (datasize > 0)
596 size += datasize;
597 spin_lock_irqsave(&device->mem_lock, flags);
598 cqr = (struct dasd_ccw_req *)
599 dasd_alloc_chunk(&device->ccw_chunks, size);
600 spin_unlock_irqrestore(&device->mem_lock, flags);
601 if (cqr == NULL)
602 return ERR_PTR(-ENOMEM);
603 memset(cqr, 0, sizeof(struct dasd_ccw_req));
604 data = (char *) cqr + ((sizeof(struct dasd_ccw_req) + 7L) & -8L);
605 cqr->cpaddr = NULL;
606 if (cplength > 0) {
607 cqr->cpaddr = (struct ccw1 *) data;
608 data += cplength*sizeof(struct ccw1);
609 memset(cqr->cpaddr, 0, cplength*sizeof(struct ccw1));
610 }
611 cqr->data = NULL;
612 if (datasize > 0) {
613 cqr->data = data;
614 memset(cqr->data, 0, datasize);
615 }
616 strncpy((char *) &cqr->magic, magic, 4);
617 ASCEBC((char *) &cqr->magic, 4);
618 set_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
619 dasd_get_device(device);
620 return cqr;
621}
622
623/*
624 * Free memory of a channel program. This function needs to free all the
625 * idal lists that might have been created by dasd_set_cda and the
626 * struct dasd_ccw_req itself.
627 */
628void
629dasd_kfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
630{
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800631#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct ccw1 *ccw;
633
634 /* Clear any idals used for the request. */
635 ccw = cqr->cpaddr;
636 do {
637 clear_normalized_cda(ccw);
638 } while (ccw++->flags & (CCW_FLAG_CC | CCW_FLAG_DC));
639#endif
Jesper Juhl17fd6822005-11-07 01:01:30 -0800640 kfree(cqr->cpaddr);
641 kfree(cqr->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 kfree(cqr);
643 dasd_put_device(device);
644}
645
646void
647dasd_sfree_request(struct dasd_ccw_req * cqr, struct dasd_device * device)
648{
649 unsigned long flags;
650
651 spin_lock_irqsave(&device->mem_lock, flags);
652 dasd_free_chunk(&device->ccw_chunks, cqr);
653 spin_unlock_irqrestore(&device->mem_lock, flags);
654 dasd_put_device(device);
655}
656
657/*
658 * Check discipline magic in cqr.
659 */
660static inline int
661dasd_check_cqr(struct dasd_ccw_req *cqr)
662{
663 struct dasd_device *device;
664
665 if (cqr == NULL)
666 return -EINVAL;
667 device = cqr->device;
668 if (strncmp((char *) &cqr->magic, device->discipline->ebcname, 4)) {
669 DEV_MESSAGE(KERN_WARNING, device,
670 " dasd_ccw_req 0x%08x magic doesn't match"
671 " discipline 0x%08x",
672 cqr->magic,
673 *(unsigned int *) device->discipline->name);
674 return -EINVAL;
675 }
676 return 0;
677}
678
679/*
680 * Terminate the current i/o and set the request to clear_pending.
681 * Timer keeps device runnig.
682 * ccw_device_clear can fail if the i/o subsystem
683 * is in a bad mood.
684 */
685int
686dasd_term_IO(struct dasd_ccw_req * cqr)
687{
688 struct dasd_device *device;
689 int retries, rc;
690
691 /* Check the cqr */
692 rc = dasd_check_cqr(cqr);
693 if (rc)
694 return rc;
695 retries = 0;
696 device = (struct dasd_device *) cqr->device;
697 while ((retries < 5) && (cqr->status == DASD_CQR_IN_IO)) {
698 rc = ccw_device_clear(device->cdev, (long) cqr);
699 switch (rc) {
700 case 0: /* termination successful */
Horst Hummelc2ba4442006-02-01 03:06:37 -0800701 cqr->retries--;
702 cqr->status = DASD_CQR_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 cqr->stopclk = get_clock();
704 DBF_DEV_EVENT(DBF_DEBUG, device,
705 "terminate cqr %p successful",
706 cqr);
707 break;
708 case -ENODEV:
709 DBF_DEV_EVENT(DBF_ERR, device, "%s",
710 "device gone, retry");
711 break;
712 case -EIO:
713 DBF_DEV_EVENT(DBF_ERR, device, "%s",
714 "I/O error, retry");
715 break;
716 case -EINVAL:
717 case -EBUSY:
718 DBF_DEV_EVENT(DBF_ERR, device, "%s",
719 "device busy, retry later");
720 break;
721 default:
722 DEV_MESSAGE(KERN_ERR, device,
723 "line %d unknown RC=%d, please "
724 "report to linux390@de.ibm.com",
725 __LINE__, rc);
726 BUG();
727 break;
728 }
729 retries++;
730 }
731 dasd_schedule_bh(device);
732 return rc;
733}
734
735/*
736 * Start the i/o. This start_IO can fail if the channel is really busy.
737 * In that case set up a timer to start the request later.
738 */
739int
740dasd_start_IO(struct dasd_ccw_req * cqr)
741{
742 struct dasd_device *device;
743 int rc;
744
745 /* Check the cqr */
746 rc = dasd_check_cqr(cqr);
747 if (rc)
748 return rc;
749 device = (struct dasd_device *) cqr->device;
750 if (cqr->retries < 0) {
751 DEV_MESSAGE(KERN_DEBUG, device,
752 "start_IO: request %p (%02x/%i) - no retry left.",
753 cqr, cqr->status, cqr->retries);
754 cqr->status = DASD_CQR_FAILED;
755 return -EIO;
756 }
757 cqr->startclk = get_clock();
758 cqr->starttime = jiffies;
759 cqr->retries--;
760 rc = ccw_device_start(device->cdev, cqr->cpaddr, (long) cqr,
761 cqr->lpm, 0);
762 switch (rc) {
763 case 0:
764 cqr->status = DASD_CQR_IN_IO;
765 DBF_DEV_EVENT(DBF_DEBUG, device,
766 "start_IO: request %p started successful",
767 cqr);
768 break;
769 case -EBUSY:
770 DBF_DEV_EVENT(DBF_ERR, device, "%s",
771 "start_IO: device busy, retry later");
772 break;
773 case -ETIMEDOUT:
774 DBF_DEV_EVENT(DBF_ERR, device, "%s",
775 "start_IO: request timeout, retry later");
776 break;
777 case -EACCES:
778 /* -EACCES indicates that the request used only a
779 * subset of the available pathes and all these
780 * pathes are gone.
781 * Do a retry with all available pathes.
782 */
783 cqr->lpm = LPM_ANYPATH;
784 DBF_DEV_EVENT(DBF_ERR, device, "%s",
785 "start_IO: selected pathes gone,"
786 " retry on all pathes");
787 break;
788 case -ENODEV:
789 case -EIO:
790 DBF_DEV_EVENT(DBF_ERR, device, "%s",
791 "start_IO: device gone, retry");
792 break;
793 default:
794 DEV_MESSAGE(KERN_ERR, device,
795 "line %d unknown RC=%d, please report"
796 " to linux390@de.ibm.com", __LINE__, rc);
797 BUG();
798 break;
799 }
800 return rc;
801}
802
803/*
804 * Timeout function for dasd devices. This is used for different purposes
805 * 1) missing interrupt handler for normal operation
806 * 2) delayed start of request where start_IO failed with -EBUSY
807 * 3) timeout for missing state change interrupts
808 * The head of the ccw queue will have status DASD_CQR_IN_IO for 1),
809 * DASD_CQR_QUEUED for 2) and 3).
810 */
811static void
812dasd_timeout_device(unsigned long ptr)
813{
814 unsigned long flags;
815 struct dasd_device *device;
816
817 device = (struct dasd_device *) ptr;
818 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
819 /* re-activate request queue */
820 device->stopped &= ~DASD_STOPPED_PENDING;
821 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
822 dasd_schedule_bh(device);
823}
824
825/*
826 * Setup timeout for a device in jiffies.
827 */
828void
829dasd_set_timer(struct dasd_device *device, int expires)
830{
831 if (expires == 0) {
832 if (timer_pending(&device->timer))
833 del_timer(&device->timer);
834 return;
835 }
836 if (timer_pending(&device->timer)) {
837 if (mod_timer(&device->timer, jiffies + expires))
838 return;
839 }
840 device->timer.function = dasd_timeout_device;
841 device->timer.data = (unsigned long) device;
842 device->timer.expires = jiffies + expires;
843 add_timer(&device->timer);
844}
845
846/*
847 * Clear timeout for a device.
848 */
849void
850dasd_clear_timer(struct dasd_device *device)
851{
852 if (timer_pending(&device->timer))
853 del_timer(&device->timer);
854}
855
856static void
857dasd_handle_killed_request(struct ccw_device *cdev, unsigned long intparm)
858{
859 struct dasd_ccw_req *cqr;
860 struct dasd_device *device;
861
862 cqr = (struct dasd_ccw_req *) intparm;
863 if (cqr->status != DASD_CQR_IN_IO) {
864 MESSAGE(KERN_DEBUG,
865 "invalid status in handle_killed_request: "
866 "bus_id %s, status %02x",
867 cdev->dev.bus_id, cqr->status);
868 return;
869 }
870
871 device = (struct dasd_device *) cqr->device;
872 if (device == NULL ||
873 device != dasd_device_from_cdev(cdev) ||
874 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
875 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
876 cdev->dev.bus_id);
877 return;
878 }
879
880 /* Schedule request to be retried. */
881 cqr->status = DASD_CQR_QUEUED;
882
883 dasd_clear_timer(device);
884 dasd_schedule_bh(device);
885 dasd_put_device(device);
886}
887
888static void
889dasd_handle_state_change_pending(struct dasd_device *device)
890{
891 struct dasd_ccw_req *cqr;
892 struct list_head *l, *n;
893
Stefan Weinhuber20c64462006-03-24 03:15:25 -0800894 /* First of all start sense subsystem status request. */
895 dasd_eer_snss(device);
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 device->stopped &= ~DASD_STOPPED_PENDING;
898
899 /* restart all 'running' IO on queue */
900 list_for_each_safe(l, n, &device->ccw_queue) {
901 cqr = list_entry(l, struct dasd_ccw_req, list);
902 if (cqr->status == DASD_CQR_IN_IO) {
903 cqr->status = DASD_CQR_QUEUED;
904 }
905 }
906 dasd_clear_timer(device);
907 dasd_schedule_bh(device);
908}
909
910/*
911 * Interrupt handler for "normal" ssch-io based dasd devices.
912 */
913void
914dasd_int_handler(struct ccw_device *cdev, unsigned long intparm,
915 struct irb *irb)
916{
917 struct dasd_ccw_req *cqr, *next;
918 struct dasd_device *device;
919 unsigned long long now;
920 int expires;
921 dasd_era_t era;
922 char mask;
923
924 if (IS_ERR(irb)) {
925 switch (PTR_ERR(irb)) {
926 case -EIO:
927 dasd_handle_killed_request(cdev, intparm);
928 break;
929 case -ETIMEDOUT:
930 printk(KERN_WARNING"%s(%s): request timed out\n",
931 __FUNCTION__, cdev->dev.bus_id);
932 //FIXME - dasd uses own timeout interface...
933 break;
934 default:
935 printk(KERN_WARNING"%s(%s): unknown error %ld\n",
936 __FUNCTION__, cdev->dev.bus_id, PTR_ERR(irb));
937 }
938 return;
939 }
940
941 now = get_clock();
942
943 DBF_EVENT(DBF_ERR, "Interrupt: bus_id %s CS/DS %04x ip %08x",
944 cdev->dev.bus_id, ((irb->scsw.cstat<<8)|irb->scsw.dstat),
945 (unsigned int) intparm);
946
947 /* first of all check for state change pending interrupt */
948 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
949 if ((irb->scsw.dstat & mask) == mask) {
950 device = dasd_device_from_cdev(cdev);
951 if (!IS_ERR(device)) {
952 dasd_handle_state_change_pending(device);
953 dasd_put_device(device);
954 }
955 return;
956 }
957
958 cqr = (struct dasd_ccw_req *) intparm;
959
960 /* check for unsolicited interrupts */
961 if (cqr == NULL) {
962 MESSAGE(KERN_DEBUG,
963 "unsolicited interrupt received: bus_id %s",
964 cdev->dev.bus_id);
965 return;
966 }
967
968 device = (struct dasd_device *) cqr->device;
969 if (device == NULL ||
970 strncmp(device->discipline->ebcname, (char *) &cqr->magic, 4)) {
971 MESSAGE(KERN_DEBUG, "invalid device in request: bus_id %s",
972 cdev->dev.bus_id);
973 return;
974 }
975
976 /* Check for clear pending */
977 if (cqr->status == DASD_CQR_CLEAR &&
978 irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
979 cqr->status = DASD_CQR_QUEUED;
980 dasd_clear_timer(device);
981 dasd_schedule_bh(device);
982 return;
983 }
984
985 /* check status - the request might have been killed by dyn detach */
986 if (cqr->status != DASD_CQR_IN_IO) {
987 MESSAGE(KERN_DEBUG,
988 "invalid status: bus_id %s, status %02x",
989 cdev->dev.bus_id, cqr->status);
990 return;
991 }
992 DBF_DEV_EVENT(DBF_DEBUG, device, "Int: CS/DS 0x%04x for cqr %p",
993 ((irb->scsw.cstat << 8) | irb->scsw.dstat), cqr);
994
995 /* Find out the appropriate era_action. */
Horst Hummel138c0142006-06-29 14:58:12 +0200996 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 era = dasd_era_fatal;
998 else if (irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END) &&
999 irb->scsw.cstat == 0 &&
1000 !irb->esw.esw0.erw.cons)
1001 era = dasd_era_none;
1002 else if (!test_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags))
1003 era = dasd_era_fatal; /* don't recover this request */
1004 else if (irb->esw.esw0.erw.cons)
1005 era = device->discipline->examine_error(cqr, irb);
Horst Hummel138c0142006-06-29 14:58:12 +02001006 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 era = dasd_era_recover;
1008
1009 DBF_DEV_EVENT(DBF_DEBUG, device, "era_code %d", era);
1010 expires = 0;
1011 if (era == dasd_era_none) {
1012 cqr->status = DASD_CQR_DONE;
1013 cqr->stopclk = now;
1014 /* Start first request on queue if possible -> fast_io. */
1015 if (cqr->list.next != &device->ccw_queue) {
1016 next = list_entry(cqr->list.next,
1017 struct dasd_ccw_req, list);
1018 if ((next->status == DASD_CQR_QUEUED) &&
1019 (!device->stopped)) {
1020 if (device->discipline->start_IO(next) == 0)
1021 expires = next->expires;
1022 else
1023 DEV_MESSAGE(KERN_DEBUG, device, "%s",
1024 "Interrupt fastpath "
1025 "failed!");
1026 }
1027 }
1028 } else { /* error */
1029 memcpy(&cqr->irb, irb, sizeof (struct irb));
1030#ifdef ERP_DEBUG
1031 /* dump sense data */
1032 dasd_log_sense(cqr, irb);
1033#endif
1034 switch (era) {
1035 case dasd_era_fatal:
1036 cqr->status = DASD_CQR_FAILED;
1037 cqr->stopclk = now;
1038 break;
1039 case dasd_era_recover:
1040 cqr->status = DASD_CQR_ERROR;
1041 break;
1042 default:
1043 BUG();
1044 }
1045 }
1046 if (expires != 0)
1047 dasd_set_timer(device, expires);
1048 else
1049 dasd_clear_timer(device);
1050 dasd_schedule_bh(device);
1051}
1052
1053/*
1054 * posts the buffer_cache about a finalized request
1055 */
1056static inline void
1057dasd_end_request(struct request *req, int uptodate)
1058{
1059 if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
1060 BUG();
1061 add_disk_randomness(req->rq_disk);
Tejun Heo8ffdc652006-01-06 09:49:03 +01001062 end_that_request_last(req, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065/*
1066 * Process finished error recovery ccw.
1067 */
1068static inline void
1069__dasd_process_erp(struct dasd_device *device, struct dasd_ccw_req *cqr)
1070{
1071 dasd_erp_fn_t erp_fn;
1072
1073 if (cqr->status == DASD_CQR_DONE)
1074 DBF_DEV_EVENT(DBF_NOTICE, device, "%s", "ERP successful");
1075 else
1076 DEV_MESSAGE(KERN_ERR, device, "%s", "ERP unsuccessful");
1077 erp_fn = device->discipline->erp_postaction(cqr);
1078 erp_fn(cqr);
1079}
1080
1081/*
1082 * Process ccw request queue.
1083 */
1084static inline void
1085__dasd_process_ccw_queue(struct dasd_device * device,
1086 struct list_head *final_queue)
1087{
1088 struct list_head *l, *n;
1089 struct dasd_ccw_req *cqr;
1090 dasd_erp_fn_t erp_fn;
1091
1092restart:
1093 /* Process request with final status. */
1094 list_for_each_safe(l, n, &device->ccw_queue) {
1095 cqr = list_entry(l, struct dasd_ccw_req, list);
1096 /* Stop list processing at the first non-final request. */
1097 if (cqr->status != DASD_CQR_DONE &&
1098 cqr->status != DASD_CQR_FAILED &&
1099 cqr->status != DASD_CQR_ERROR)
1100 break;
1101 /* Process requests with DASD_CQR_ERROR */
1102 if (cqr->status == DASD_CQR_ERROR) {
1103 if (cqr->irb.scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1104 cqr->status = DASD_CQR_FAILED;
1105 cqr->stopclk = get_clock();
1106 } else {
1107 if (cqr->irb.esw.esw0.erw.cons) {
1108 erp_fn = device->discipline->
1109 erp_action(cqr);
1110 erp_fn(cqr);
1111 } else
1112 dasd_default_erp_action(cqr);
1113 }
1114 goto restart;
1115 }
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001116
1117 /* First of all call extended error reporting. */
1118 if (dasd_eer_enabled(device) &&
1119 cqr->status == DASD_CQR_FAILED) {
1120 dasd_eer_write(device, cqr, DASD_EER_FATALERROR);
1121
1122 /* restart request */
1123 cqr->status = DASD_CQR_QUEUED;
1124 cqr->retries = 255;
1125 device->stopped |= DASD_STOPPED_QUIESCE;
1126 goto restart;
1127 }
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Process finished ERP request. */
1130 if (cqr->refers) {
1131 __dasd_process_erp(device, cqr);
1132 goto restart;
1133 }
1134
1135 /* Rechain finished requests to final queue */
1136 cqr->endclk = get_clock();
1137 list_move_tail(&cqr->list, final_queue);
1138 }
1139}
1140
1141static void
1142dasd_end_request_cb(struct dasd_ccw_req * cqr, void *data)
1143{
1144 struct request *req;
1145 struct dasd_device *device;
1146 int status;
1147
1148 req = (struct request *) data;
1149 device = cqr->device;
1150 dasd_profile_end(device, cqr, req);
1151 status = cqr->device->discipline->free_cp(cqr,req);
1152 spin_lock_irq(&device->request_queue_lock);
1153 dasd_end_request(req, status);
1154 spin_unlock_irq(&device->request_queue_lock);
1155}
1156
1157
1158/*
1159 * Fetch requests from the block device queue.
1160 */
1161static inline void
1162__dasd_process_blk_queue(struct dasd_device * device)
1163{
1164 request_queue_t *queue;
1165 struct request *req;
1166 struct dasd_ccw_req *cqr;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001167 int nr_queued;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 queue = device->request_queue;
1170 /* No queue ? Then there is nothing to do. */
1171 if (queue == NULL)
1172 return;
1173
1174 /*
1175 * We requeue request from the block device queue to the ccw
1176 * queue only in two states. In state DASD_STATE_READY the
1177 * partition detection is done and we need to requeue requests
1178 * for that. State DASD_STATE_ONLINE is normal block device
1179 * operation.
1180 */
1181 if (device->state != DASD_STATE_READY &&
1182 device->state != DASD_STATE_ONLINE)
1183 return;
1184 nr_queued = 0;
1185 /* Now we try to fetch requests from the request queue */
1186 list_for_each_entry(cqr, &device->ccw_queue, list)
1187 if (cqr->status == DASD_CQR_QUEUED)
1188 nr_queued++;
1189 while (!blk_queue_plugged(queue) &&
1190 elv_next_request(queue) &&
1191 nr_queued < DASD_CHANQ_MAX_SIZE) {
1192 req = elv_next_request(queue);
Horst Hummelf24acd42005-05-01 08:58:59 -07001193
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001194 if (device->features & DASD_FEATURE_READONLY &&
1195 rq_data_dir(req) == WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 DBF_DEV_EVENT(DBF_ERR, device,
1197 "Rejecting write request %p",
1198 req);
1199 blkdev_dequeue_request(req);
1200 dasd_end_request(req, 0);
1201 continue;
1202 }
1203 if (device->stopped & DASD_STOPPED_DC_EIO) {
1204 blkdev_dequeue_request(req);
1205 dasd_end_request(req, 0);
1206 continue;
1207 }
1208 cqr = device->discipline->build_cp(device, req);
1209 if (IS_ERR(cqr)) {
1210 if (PTR_ERR(cqr) == -ENOMEM)
1211 break; /* terminate request queue loop */
1212 DBF_DEV_EVENT(DBF_ERR, device,
1213 "CCW creation failed (rc=%ld) "
1214 "on request %p",
1215 PTR_ERR(cqr), req);
1216 blkdev_dequeue_request(req);
1217 dasd_end_request(req, 0);
1218 continue;
1219 }
1220 cqr->callback = dasd_end_request_cb;
1221 cqr->callback_data = (void *) req;
1222 cqr->status = DASD_CQR_QUEUED;
1223 blkdev_dequeue_request(req);
1224 list_add_tail(&cqr->list, &device->ccw_queue);
1225 dasd_profile_start(device, cqr, req);
1226 nr_queued++;
1227 }
1228}
1229
1230/*
1231 * Take a look at the first request on the ccw queue and check
1232 * if it reached its expire time. If so, terminate the IO.
1233 */
1234static inline void
1235__dasd_check_expire(struct dasd_device * device)
1236{
1237 struct dasd_ccw_req *cqr;
1238
1239 if (list_empty(&device->ccw_queue))
1240 return;
1241 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1242 if (cqr->status == DASD_CQR_IN_IO && cqr->expires != 0) {
1243 if (time_after_eq(jiffies, cqr->expires + cqr->starttime)) {
1244 if (device->discipline->term_IO(cqr) != 0)
1245 /* Hmpf, try again in 1/10 sec */
1246 dasd_set_timer(device, 10);
1247 }
1248 }
1249}
1250
1251/*
1252 * Take a look at the first request on the ccw queue and check
1253 * if it needs to be started.
1254 */
1255static inline void
1256__dasd_start_head(struct dasd_device * device)
1257{
1258 struct dasd_ccw_req *cqr;
1259 int rc;
1260
1261 if (list_empty(&device->ccw_queue))
1262 return;
1263 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001264 if (cqr->status != DASD_CQR_QUEUED)
1265 return;
1266 /* Non-temporary stop condition will trigger fail fast */
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001267 if (device->stopped & ~DASD_STOPPED_PENDING &&
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001268 test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
1269 (!dasd_eer_enabled(device))) {
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001270 cqr->status = DASD_CQR_FAILED;
1271 dasd_schedule_bh(device);
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001272 return;
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001273 }
Peter Oberparleiter25ee4cf2006-04-10 22:53:47 -07001274 /* Don't try to start requests if device is stopped */
1275 if (device->stopped)
1276 return;
1277
1278 rc = device->discipline->start_IO(cqr);
1279 if (rc == 0)
1280 dasd_set_timer(device, cqr->expires);
1281 else if (rc == -EACCES) {
1282 dasd_schedule_bh(device);
1283 } else
1284 /* Hmpf, try again in 1/2 sec */
1285 dasd_set_timer(device, 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288/*
Horst Hummel138c0142006-06-29 14:58:12 +02001289 * Remove requests from the ccw queue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
1291static void
1292dasd_flush_ccw_queue(struct dasd_device * device, int all)
1293{
1294 struct list_head flush_queue;
1295 struct list_head *l, *n;
1296 struct dasd_ccw_req *cqr;
1297
1298 INIT_LIST_HEAD(&flush_queue);
1299 spin_lock_irq(get_ccwdev_lock(device->cdev));
1300 list_for_each_safe(l, n, &device->ccw_queue) {
1301 cqr = list_entry(l, struct dasd_ccw_req, list);
1302 /* Flush all request or only block device requests? */
1303 if (all == 0 && cqr->callback == dasd_end_request_cb)
1304 continue;
1305 if (cqr->status == DASD_CQR_IN_IO)
1306 device->discipline->term_IO(cqr);
1307 if (cqr->status != DASD_CQR_DONE ||
1308 cqr->status != DASD_CQR_FAILED) {
1309 cqr->status = DASD_CQR_FAILED;
1310 cqr->stopclk = get_clock();
1311 }
1312 /* Process finished ERP request. */
1313 if (cqr->refers) {
1314 __dasd_process_erp(device, cqr);
1315 continue;
1316 }
1317 /* Rechain request on device request queue */
1318 cqr->endclk = get_clock();
1319 list_move_tail(&cqr->list, &flush_queue);
1320 }
1321 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1322 /* Now call the callback function of flushed requests */
1323 list_for_each_safe(l, n, &flush_queue) {
1324 cqr = list_entry(l, struct dasd_ccw_req, list);
1325 if (cqr->callback != NULL)
1326 (cqr->callback)(cqr, cqr->callback_data);
1327 }
1328}
1329
1330/*
1331 * Acquire the device lock and process queues for the device.
1332 */
1333static void
1334dasd_tasklet(struct dasd_device * device)
1335{
1336 struct list_head final_queue;
1337 struct list_head *l, *n;
1338 struct dasd_ccw_req *cqr;
1339
1340 atomic_set (&device->tasklet_scheduled, 0);
1341 INIT_LIST_HEAD(&final_queue);
1342 spin_lock_irq(get_ccwdev_lock(device->cdev));
1343 /* Check expire time of first request on the ccw queue. */
1344 __dasd_check_expire(device);
1345 /* Finish off requests on ccw queue */
1346 __dasd_process_ccw_queue(device, &final_queue);
1347 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1348 /* Now call the callback function of requests with final status */
1349 list_for_each_safe(l, n, &final_queue) {
1350 cqr = list_entry(l, struct dasd_ccw_req, list);
Horst Hummelc2ba4442006-02-01 03:06:37 -08001351 list_del_init(&cqr->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (cqr->callback != NULL)
1353 (cqr->callback)(cqr, cqr->callback_data);
1354 }
1355 spin_lock_irq(&device->request_queue_lock);
1356 spin_lock(get_ccwdev_lock(device->cdev));
1357 /* Get new request from the block device request queue */
1358 __dasd_process_blk_queue(device);
1359 /* Now check if the head of the ccw queue needs to be started. */
1360 __dasd_start_head(device);
1361 spin_unlock(get_ccwdev_lock(device->cdev));
1362 spin_unlock_irq(&device->request_queue_lock);
1363 dasd_put_device(device);
1364}
1365
1366/*
1367 * Schedules a call to dasd_tasklet over the device tasklet.
1368 */
1369void
1370dasd_schedule_bh(struct dasd_device * device)
1371{
1372 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -08001373 if (atomic_cmpxchg (&device->tasklet_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return;
1375 dasd_get_device(device);
1376 tasklet_hi_schedule(&device->tasklet);
1377}
1378
1379/*
1380 * Queue a request to the head of the ccw_queue. Start the I/O if
1381 * possible.
1382 */
1383void
1384dasd_add_request_head(struct dasd_ccw_req *req)
1385{
1386 struct dasd_device *device;
1387 unsigned long flags;
1388
1389 device = req->device;
1390 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1391 req->status = DASD_CQR_QUEUED;
1392 req->device = device;
1393 list_add(&req->list, &device->ccw_queue);
1394 /* let the bh start the request to keep them in order */
1395 dasd_schedule_bh(device);
1396 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1397}
1398
1399/*
1400 * Queue a request to the tail of the ccw_queue. Start the I/O if
1401 * possible.
1402 */
1403void
1404dasd_add_request_tail(struct dasd_ccw_req *req)
1405{
1406 struct dasd_device *device;
1407 unsigned long flags;
1408
1409 device = req->device;
1410 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1411 req->status = DASD_CQR_QUEUED;
1412 req->device = device;
1413 list_add_tail(&req->list, &device->ccw_queue);
1414 /* let the bh start the request to keep them in order */
1415 dasd_schedule_bh(device);
1416 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1417}
1418
1419/*
1420 * Wakeup callback.
1421 */
1422static void
1423dasd_wakeup_cb(struct dasd_ccw_req *cqr, void *data)
1424{
1425 wake_up((wait_queue_head_t *) data);
1426}
1427
1428static inline int
1429_wait_for_wakeup(struct dasd_ccw_req *cqr)
1430{
1431 struct dasd_device *device;
1432 int rc;
1433
1434 device = cqr->device;
1435 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001436 rc = ((cqr->status == DASD_CQR_DONE ||
1437 cqr->status == DASD_CQR_FAILED) &&
1438 list_empty(&cqr->list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1440 return rc;
1441}
1442
1443/*
1444 * Attempts to start a special ccw queue and waits for its completion.
1445 */
1446int
1447dasd_sleep_on(struct dasd_ccw_req * cqr)
1448{
1449 wait_queue_head_t wait_q;
1450 struct dasd_device *device;
1451 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 device = cqr->device;
1454 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummel138c0142006-06-29 14:58:12 +02001455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 init_waitqueue_head (&wait_q);
1457 cqr->callback = dasd_wakeup_cb;
1458 cqr->callback_data = (void *) &wait_q;
1459 cqr->status = DASD_CQR_QUEUED;
1460 list_add_tail(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* let the bh start the request to keep them in order */
1463 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1466
1467 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* Request status is either done or failed. */
1470 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1471 return rc;
1472}
1473
1474/*
1475 * Attempts to start a special ccw queue and wait interruptible
1476 * for its completion.
1477 */
1478int
1479dasd_sleep_on_interruptible(struct dasd_ccw_req * cqr)
1480{
1481 wait_queue_head_t wait_q;
1482 struct dasd_device *device;
1483 int rc, finished;
1484
1485 device = cqr->device;
1486 spin_lock_irq(get_ccwdev_lock(device->cdev));
1487
1488 init_waitqueue_head (&wait_q);
1489 cqr->callback = dasd_wakeup_cb;
1490 cqr->callback_data = (void *) &wait_q;
1491 cqr->status = DASD_CQR_QUEUED;
1492 list_add_tail(&cqr->list, &device->ccw_queue);
1493
1494 /* let the bh start the request to keep them in order */
1495 dasd_schedule_bh(device);
1496 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1497
1498 finished = 0;
1499 while (!finished) {
1500 rc = wait_event_interruptible(wait_q, _wait_for_wakeup(cqr));
1501 if (rc != -ERESTARTSYS) {
Horst Hummelc2ba4442006-02-01 03:06:37 -08001502 /* Request is final (done or failed) */
1503 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 break;
1505 }
1506 spin_lock_irq(get_ccwdev_lock(device->cdev));
Horst Hummelc2ba4442006-02-01 03:06:37 -08001507 switch (cqr->status) {
1508 case DASD_CQR_IN_IO:
1509 /* terminate runnig cqr */
1510 if (device->discipline->term_IO) {
1511 cqr->retries = -1;
1512 device->discipline->term_IO(cqr);
1513 /*nished =
1514 * wait (non-interruptible) for final status
1515 * because signal ist still pending
1516 */
1517 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1518 wait_event(wait_q, _wait_for_wakeup(cqr));
1519 spin_lock_irq(get_ccwdev_lock(device->cdev));
1520 rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO;
1521 finished = 1;
1522 }
1523 break;
1524 case DASD_CQR_QUEUED:
1525 /* request */
1526 list_del_init(&cqr->list);
1527 rc = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 finished = 1;
Horst Hummelc2ba4442006-02-01 03:06:37 -08001529 break;
1530 default:
1531 /* cqr with 'non-interruptable' status - just wait */
1532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1535 }
1536 return rc;
1537}
1538
1539/*
1540 * Whoa nelly now it gets really hairy. For some functions (e.g. steal lock
1541 * for eckd devices) the currently running request has to be terminated
1542 * and be put back to status queued, before the special request is added
1543 * to the head of the queue. Then the special request is waited on normally.
1544 */
1545static inline int
1546_dasd_term_running_cqr(struct dasd_device *device)
1547{
1548 struct dasd_ccw_req *cqr;
1549 int rc;
1550
1551 if (list_empty(&device->ccw_queue))
1552 return 0;
1553 cqr = list_entry(device->ccw_queue.next, struct dasd_ccw_req, list);
1554 rc = device->discipline->term_IO(cqr);
1555 if (rc == 0) {
1556 /* termination successful */
1557 cqr->status = DASD_CQR_QUEUED;
1558 cqr->startclk = cqr->stopclk = 0;
1559 cqr->starttime = 0;
1560 }
1561 return rc;
1562}
1563
1564int
1565dasd_sleep_on_immediatly(struct dasd_ccw_req * cqr)
1566{
1567 wait_queue_head_t wait_q;
1568 struct dasd_device *device;
1569 int rc;
Horst Hummel138c0142006-06-29 14:58:12 +02001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 device = cqr->device;
1572 spin_lock_irq(get_ccwdev_lock(device->cdev));
1573 rc = _dasd_term_running_cqr(device);
1574 if (rc) {
1575 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1576 return rc;
1577 }
Horst Hummel138c0142006-06-29 14:58:12 +02001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 init_waitqueue_head (&wait_q);
1580 cqr->callback = dasd_wakeup_cb;
1581 cqr->callback_data = (void *) &wait_q;
1582 cqr->status = DASD_CQR_QUEUED;
1583 list_add(&cqr->list, &device->ccw_queue);
Horst Hummel138c0142006-06-29 14:58:12 +02001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* let the bh start the request to keep them in order */
1586 dasd_schedule_bh(device);
Horst Hummel138c0142006-06-29 14:58:12 +02001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 spin_unlock_irq(get_ccwdev_lock(device->cdev));
1589
1590 wait_event(wait_q, _wait_for_wakeup(cqr));
Horst Hummel138c0142006-06-29 14:58:12 +02001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* Request status is either done or failed. */
1593 rc = (cqr->status == DASD_CQR_FAILED) ? -EIO : 0;
1594 return rc;
1595}
1596
1597/*
1598 * Cancels a request that was started with dasd_sleep_on_req.
1599 * This is useful to timeout requests. The request will be
1600 * terminated if it is currently in i/o.
1601 * Returns 1 if the request has been terminated.
1602 */
1603int
1604dasd_cancel_req(struct dasd_ccw_req *cqr)
1605{
1606 struct dasd_device *device = cqr->device;
1607 unsigned long flags;
1608 int rc;
1609
1610 rc = 0;
1611 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
1612 switch (cqr->status) {
1613 case DASD_CQR_QUEUED:
1614 /* request was not started - just set to failed */
1615 cqr->status = DASD_CQR_FAILED;
1616 break;
1617 case DASD_CQR_IN_IO:
1618 /* request in IO - terminate IO and release again */
1619 if (device->discipline->term_IO(cqr) != 0)
1620 /* what to do if unable to terminate ??????
1621 e.g. not _IN_IO */
1622 cqr->status = DASD_CQR_FAILED;
1623 cqr->stopclk = get_clock();
1624 rc = 1;
1625 break;
1626 case DASD_CQR_DONE:
1627 case DASD_CQR_FAILED:
1628 /* already finished - do nothing */
1629 break;
1630 default:
1631 DEV_MESSAGE(KERN_ALERT, device,
1632 "invalid status %02x in request",
1633 cqr->status);
1634 BUG();
1635
1636 }
1637 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
1638 dasd_schedule_bh(device);
1639 return rc;
1640}
1641
1642/*
1643 * SECTION: Block device operations (request queue, partitions, open, release).
1644 */
1645
1646/*
1647 * Dasd request queue function. Called from ll_rw_blk.c
1648 */
1649static void
1650do_dasd_request(request_queue_t * queue)
1651{
1652 struct dasd_device *device;
1653
1654 device = (struct dasd_device *) queue->queuedata;
1655 spin_lock(get_ccwdev_lock(device->cdev));
1656 /* Get new request from the block device request queue */
1657 __dasd_process_blk_queue(device);
1658 /* Now check if the head of the ccw queue needs to be started. */
1659 __dasd_start_head(device);
1660 spin_unlock(get_ccwdev_lock(device->cdev));
1661}
1662
1663/*
1664 * Allocate and initialize request queue and default I/O scheduler.
1665 */
1666static int
1667dasd_alloc_queue(struct dasd_device * device)
1668{
1669 int rc;
1670
1671 device->request_queue = blk_init_queue(do_dasd_request,
1672 &device->request_queue_lock);
1673 if (device->request_queue == NULL)
1674 return -ENOMEM;
1675
1676 device->request_queue->queuedata = device;
1677
1678 elevator_exit(device->request_queue->elevator);
1679 rc = elevator_init(device->request_queue, "deadline");
1680 if (rc) {
1681 blk_cleanup_queue(device->request_queue);
1682 return rc;
1683 }
1684 return 0;
1685}
1686
1687/*
1688 * Allocate and initialize request queue.
1689 */
1690static void
1691dasd_setup_queue(struct dasd_device * device)
1692{
1693 int max;
1694
1695 blk_queue_hardsect_size(device->request_queue, device->bp_block);
1696 max = device->discipline->max_blocks << device->s2b_shift;
1697 blk_queue_max_sectors(device->request_queue, max);
1698 blk_queue_max_phys_segments(device->request_queue, -1L);
1699 blk_queue_max_hw_segments(device->request_queue, -1L);
1700 blk_queue_max_segment_size(device->request_queue, -1L);
1701 blk_queue_segment_boundary(device->request_queue, -1L);
Heiko Carstensed68cb32006-01-14 13:21:05 -08001702 blk_queue_ordered(device->request_queue, QUEUE_ORDERED_TAG, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705/*
1706 * Deactivate and free request queue.
1707 */
1708static void
1709dasd_free_queue(struct dasd_device * device)
1710{
1711 if (device->request_queue) {
1712 blk_cleanup_queue(device->request_queue);
1713 device->request_queue = NULL;
1714 }
1715}
1716
1717/*
1718 * Flush request on the request queue.
1719 */
1720static void
1721dasd_flush_request_queue(struct dasd_device * device)
1722{
1723 struct request *req;
1724
1725 if (!device->request_queue)
1726 return;
Horst Hummel138c0142006-06-29 14:58:12 +02001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 spin_lock_irq(&device->request_queue_lock);
1729 while (!list_empty(&device->request_queue->queue_head)) {
1730 req = elv_next_request(device->request_queue);
1731 if (req == NULL)
1732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 blkdev_dequeue_request(req);
Horst Hummelebc45992006-08-09 10:30:22 +02001734 dasd_end_request(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736 spin_unlock_irq(&device->request_queue_lock);
1737}
1738
1739static int
1740dasd_open(struct inode *inp, struct file *filp)
1741{
1742 struct gendisk *disk = inp->i_bdev->bd_disk;
1743 struct dasd_device *device = disk->private_data;
1744 int rc;
1745
1746 atomic_inc(&device->open_count);
1747 if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1748 rc = -ENODEV;
1749 goto unlock;
1750 }
1751
1752 if (!try_module_get(device->discipline->owner)) {
1753 rc = -EINVAL;
1754 goto unlock;
1755 }
1756
1757 if (dasd_probeonly) {
1758 DEV_MESSAGE(KERN_INFO, device, "%s",
1759 "No access to device due to probeonly mode");
1760 rc = -EPERM;
1761 goto out;
1762 }
1763
Horst Hummel90f00942006-03-07 21:55:39 -08001764 if (device->state <= DASD_STATE_BASIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 DBF_DEV_EVENT(DBF_ERR, device, " %s",
1766 " Cannot open unrecognized device");
1767 rc = -ENODEV;
1768 goto out;
1769 }
1770
1771 return 0;
1772
1773out:
1774 module_put(device->discipline->owner);
1775unlock:
1776 atomic_dec(&device->open_count);
1777 return rc;
1778}
1779
1780static int
1781dasd_release(struct inode *inp, struct file *filp)
1782{
1783 struct gendisk *disk = inp->i_bdev->bd_disk;
1784 struct dasd_device *device = disk->private_data;
1785
1786 atomic_dec(&device->open_count);
1787 module_put(device->discipline->owner);
1788 return 0;
1789}
1790
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001791/*
1792 * Return disk geometry.
1793 */
1794static int
1795dasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1796{
1797 struct dasd_device *device;
1798
1799 device = bdev->bd_disk->private_data;
1800 if (!device)
1801 return -ENODEV;
1802
1803 if (!device->discipline ||
1804 !device->discipline->fill_geometry)
1805 return -EINVAL;
1806
1807 device->discipline->fill_geometry(device, geo);
1808 geo->start = get_start_sect(bdev) >> device->s2b_shift;
1809 return 0;
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812struct block_device_operations
1813dasd_device_operations = {
1814 .owner = THIS_MODULE,
1815 .open = dasd_open,
1816 .release = dasd_release,
1817 .ioctl = dasd_ioctl,
Christoph Hellwig82620372006-01-09 20:52:12 -08001818 .compat_ioctl = dasd_compat_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08001819 .getgeo = dasd_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820};
1821
1822
1823static void
1824dasd_exit(void)
1825{
1826#ifdef CONFIG_PROC_FS
1827 dasd_proc_exit();
1828#endif
Stefan Weinhuber20c64462006-03-24 03:15:25 -08001829 dasd_eer_exit();
Horst Hummel6bb0e012005-07-27 11:45:03 -07001830 if (dasd_page_cache != NULL) {
1831 kmem_cache_destroy(dasd_page_cache);
1832 dasd_page_cache = NULL;
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 dasd_gendisk_exit();
1835 dasd_devmap_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (dasd_debug_area != NULL) {
1837 debug_unregister(dasd_debug_area);
1838 dasd_debug_area = NULL;
1839 }
1840}
1841
1842/*
1843 * SECTION: common functions for ccw_driver use
1844 */
1845
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001846/*
1847 * Initial attempt at a probe function. this can be simplified once
1848 * the other detection code is gone.
1849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850int
1851dasd_generic_probe (struct ccw_device *cdev,
1852 struct dasd_discipline *discipline)
1853{
1854 int ret;
1855
Horst Hummel40545572006-06-29 15:08:18 +02001856 ret = ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP);
1857 if (ret) {
1858 printk(KERN_WARNING
1859 "dasd_generic_probe: could not set ccw-device options "
1860 "for %s\n", cdev->dev.bus_id);
1861 return ret;
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 ret = dasd_add_sysfs_files(cdev);
1864 if (ret) {
1865 printk(KERN_WARNING
1866 "dasd_generic_probe: could not add sysfs entries "
1867 "for %s\n", cdev->dev.bus_id);
Horst Hummel40545572006-06-29 15:08:18 +02001868 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
Horst Hummel40545572006-06-29 15:08:18 +02001870 cdev->handler = &dasd_int_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Horst Hummel40545572006-06-29 15:08:18 +02001872 /*
1873 * Automatically online either all dasd devices (dasd_autodetect)
1874 * or all devices specified with dasd= parameters during
1875 * initial probe.
1876 */
1877 if ((dasd_get_feature(cdev, DASD_FEATURE_INITIAL_ONLINE) > 0 ) ||
1878 (dasd_autodetect && dasd_busid_known(cdev->dev.bus_id) != 0))
1879 ret = ccw_device_set_online(cdev);
1880 if (ret)
1881 printk(KERN_WARNING
1882 "dasd_generic_probe: could not initially online "
1883 "ccw-device %s\n", cdev->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return ret;
1885}
1886
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001887/*
1888 * This will one day be called from a global not_oper handler.
1889 * It is also used by driver_unregister during module unload.
1890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891void
1892dasd_generic_remove (struct ccw_device *cdev)
1893{
1894 struct dasd_device *device;
1895
Horst Hummel59afda72005-05-16 21:53:39 -07001896 cdev->handler = NULL;
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 dasd_remove_sysfs_files(cdev);
1899 device = dasd_device_from_cdev(cdev);
1900 if (IS_ERR(device))
1901 return;
1902 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
1903 /* Already doing offline processing */
1904 dasd_put_device(device);
1905 return;
1906 }
1907 /*
1908 * This device is removed unconditionally. Set offline
1909 * flag to prevent dasd_open from opening it while it is
1910 * no quite down yet.
1911 */
1912 dasd_set_target_state(device, DASD_STATE_NEW);
1913 /* dasd_delete_device destroys the device reference. */
1914 dasd_delete_device(device);
1915}
1916
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001917/*
1918 * Activate a device. This is called from dasd_{eckd,fba}_probe() when either
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 * the device is detected for the first time and is supposed to be used
Horst Hummel1c01b8a2006-01-06 00:19:15 -08001920 * or the user has started activation through sysfs.
1921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922int
1923dasd_generic_set_online (struct ccw_device *cdev,
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001924 struct dasd_discipline *base_discipline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926{
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001927 struct dasd_discipline *discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 struct dasd_device *device;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001929 int rc;
Horst Hummelf24acd42005-05-01 08:58:59 -07001930
Horst Hummel40545572006-06-29 15:08:18 +02001931 /* first online clears initial online feature flag */
1932 dasd_set_feature(cdev, DASD_FEATURE_INITIAL_ONLINE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 device = dasd_create_device(cdev);
1934 if (IS_ERR(device))
1935 return PTR_ERR(device);
1936
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001937 discipline = base_discipline;
Horst Hummelc6eb7b72005-09-03 15:57:58 -07001938 if (device->features & DASD_FEATURE_USEDIAG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (!dasd_diag_discipline_pointer) {
1940 printk (KERN_WARNING
1941 "dasd_generic couldn't online device %s "
1942 "- discipline DIAG not available\n",
1943 cdev->dev.bus_id);
1944 dasd_delete_device(device);
1945 return -ENODEV;
1946 }
1947 discipline = dasd_diag_discipline_pointer;
1948 }
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001949 if (!try_module_get(base_discipline->owner)) {
1950 dasd_delete_device(device);
1951 return -EINVAL;
1952 }
1953 if (!try_module_get(discipline->owner)) {
1954 module_put(base_discipline->owner);
1955 dasd_delete_device(device);
1956 return -EINVAL;
1957 }
1958 device->base_discipline = base_discipline;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 device->discipline = discipline;
1960
1961 rc = discipline->check_device(device);
1962 if (rc) {
1963 printk (KERN_WARNING
1964 "dasd_generic couldn't online device %s "
1965 "with discipline %s rc=%i\n",
1966 cdev->dev.bus_id, discipline->name, rc);
Peter Oberparleiteraa888612006-02-20 18:28:13 -08001967 module_put(discipline->owner);
1968 module_put(base_discipline->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 dasd_delete_device(device);
1970 return rc;
1971 }
1972
1973 dasd_set_target_state(device, DASD_STATE_ONLINE);
1974 if (device->state <= DASD_STATE_KNOWN) {
1975 printk (KERN_WARNING
1976 "dasd_generic discipline not found for %s\n",
1977 cdev->dev.bus_id);
1978 rc = -ENODEV;
1979 dasd_set_target_state(device, DASD_STATE_NEW);
1980 dasd_delete_device(device);
1981 } else
1982 pr_debug("dasd_generic device %s found\n",
1983 cdev->dev.bus_id);
1984
1985 /* FIXME: we have to wait for the root device but we don't want
1986 * to wait for each single device but for all at once. */
1987 wait_event(dasd_init_waitq, _wait_for_device(device));
1988
1989 dasd_put_device(device);
1990
1991 return rc;
1992}
1993
1994int
1995dasd_generic_set_offline (struct ccw_device *cdev)
1996{
1997 struct dasd_device *device;
Horst Hummeldafd87a2006-04-10 22:53:47 -07001998 int max_count, open_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 device = dasd_device_from_cdev(cdev);
2001 if (IS_ERR(device))
2002 return PTR_ERR(device);
2003 if (test_and_set_bit(DASD_FLAG_OFFLINE, &device->flags)) {
2004 /* Already doing offline processing */
2005 dasd_put_device(device);
2006 return 0;
2007 }
2008 /*
2009 * We must make sure that this device is currently not in use.
2010 * The open_count is increased for every opener, that includes
2011 * the blkdev_get in dasd_scan_partitions. We are only interested
2012 * in the other openers.
2013 */
2014 max_count = device->bdev ? 0 : -1;
Horst Hummeldafd87a2006-04-10 22:53:47 -07002015 open_count = (int) atomic_read(&device->open_count);
2016 if (open_count > max_count) {
2017 if (open_count > 0)
2018 printk (KERN_WARNING "Can't offline dasd device with "
2019 "open count = %i.\n",
2020 open_count);
2021 else
2022 printk (KERN_WARNING "%s",
2023 "Can't offline dasd device due to internal "
2024 "use\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 clear_bit(DASD_FLAG_OFFLINE, &device->flags);
2026 dasd_put_device(device);
2027 return -EBUSY;
2028 }
2029 dasd_set_target_state(device, DASD_STATE_NEW);
2030 /* dasd_delete_device destroys the device reference. */
2031 dasd_delete_device(device);
2032
2033 return 0;
2034}
2035
2036int
2037dasd_generic_notify(struct ccw_device *cdev, int event)
2038{
2039 struct dasd_device *device;
2040 struct dasd_ccw_req *cqr;
2041 unsigned long flags;
2042 int ret;
2043
2044 device = dasd_device_from_cdev(cdev);
2045 if (IS_ERR(device))
2046 return 0;
2047 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
2048 ret = 0;
2049 switch (event) {
2050 case CIO_GONE:
2051 case CIO_NO_PATH:
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002052 /* First of all call extended error reporting. */
2053 dasd_eer_write(device, NULL, DASD_EER_NOPATH);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (device->state < DASD_STATE_BASIC)
2056 break;
2057 /* Device is active. We want to keep it. */
2058 if (test_bit(DASD_FLAG_DSC_ERROR, &device->flags)) {
2059 list_for_each_entry(cqr, &device->ccw_queue, list)
2060 if (cqr->status == DASD_CQR_IN_IO)
2061 cqr->status = DASD_CQR_FAILED;
2062 device->stopped |= DASD_STOPPED_DC_EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 } else {
2064 list_for_each_entry(cqr, &device->ccw_queue, list)
2065 if (cqr->status == DASD_CQR_IN_IO) {
2066 cqr->status = DASD_CQR_QUEUED;
2067 cqr->retries++;
2068 }
2069 device->stopped |= DASD_STOPPED_DC_WAIT;
2070 dasd_set_timer(device, 0);
2071 }
Horst Hummel1c01b8a2006-01-06 00:19:15 -08002072 dasd_schedule_bh(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 ret = 1;
2074 break;
2075 case CIO_OPER:
2076 /* FIXME: add a sanity check. */
2077 device->stopped &= ~(DASD_STOPPED_DC_WAIT|DASD_STOPPED_DC_EIO);
2078 dasd_schedule_bh(device);
2079 ret = 1;
2080 break;
2081 }
2082 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2083 dasd_put_device(device);
2084 return ret;
2085}
2086
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088static int __init
2089dasd_init(void)
2090{
2091 int rc;
2092
2093 init_waitqueue_head(&dasd_init_waitq);
2094
2095 /* register 'common' DASD debug area, used for all DBF_XXX calls */
Michael Holzheu66a464d2005-06-25 14:55:33 -07002096 dasd_debug_area = debug_register("dasd", 1, 2, 8 * sizeof (long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (dasd_debug_area == NULL) {
2098 rc = -ENOMEM;
2099 goto failed;
2100 }
2101 debug_register_view(dasd_debug_area, &debug_sprintf_view);
2102 debug_set_level(dasd_debug_area, DBF_EMERG);
2103
2104 DBF_EVENT(DBF_EMERG, "%s", "debug area created");
2105
2106 dasd_diag_discipline_pointer = NULL;
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 rc = dasd_devmap_init();
2109 if (rc)
2110 goto failed;
2111 rc = dasd_gendisk_init();
2112 if (rc)
2113 goto failed;
2114 rc = dasd_parse();
2115 if (rc)
2116 goto failed;
Stefan Weinhuber20c64462006-03-24 03:15:25 -08002117 rc = dasd_eer_init();
2118 if (rc)
2119 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120#ifdef CONFIG_PROC_FS
2121 rc = dasd_proc_init();
2122 if (rc)
2123 goto failed;
2124#endif
2125
2126 return 0;
2127failed:
2128 MESSAGE(KERN_INFO, "%s", "initialization not performed due to errors");
2129 dasd_exit();
2130 return rc;
2131}
2132
2133module_init(dasd_init);
2134module_exit(dasd_exit);
2135
2136EXPORT_SYMBOL(dasd_debug_area);
2137EXPORT_SYMBOL(dasd_diag_discipline_pointer);
2138
2139EXPORT_SYMBOL(dasd_add_request_head);
2140EXPORT_SYMBOL(dasd_add_request_tail);
2141EXPORT_SYMBOL(dasd_cancel_req);
2142EXPORT_SYMBOL(dasd_clear_timer);
2143EXPORT_SYMBOL(dasd_enable_device);
2144EXPORT_SYMBOL(dasd_int_handler);
2145EXPORT_SYMBOL(dasd_kfree_request);
2146EXPORT_SYMBOL(dasd_kick_device);
2147EXPORT_SYMBOL(dasd_kmalloc_request);
2148EXPORT_SYMBOL(dasd_schedule_bh);
2149EXPORT_SYMBOL(dasd_set_target_state);
2150EXPORT_SYMBOL(dasd_set_timer);
2151EXPORT_SYMBOL(dasd_sfree_request);
2152EXPORT_SYMBOL(dasd_sleep_on);
2153EXPORT_SYMBOL(dasd_sleep_on_immediatly);
2154EXPORT_SYMBOL(dasd_sleep_on_interruptible);
2155EXPORT_SYMBOL(dasd_smalloc_request);
2156EXPORT_SYMBOL(dasd_start_IO);
2157EXPORT_SYMBOL(dasd_term_IO);
2158
2159EXPORT_SYMBOL_GPL(dasd_generic_probe);
2160EXPORT_SYMBOL_GPL(dasd_generic_remove);
2161EXPORT_SYMBOL_GPL(dasd_generic_notify);
2162EXPORT_SYMBOL_GPL(dasd_generic_set_online);
2163EXPORT_SYMBOL_GPL(dasd_generic_set_offline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164