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