Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/s390/cio/device_fsm.c |
| 3 | * finite state machine for device handling |
| 4 | * |
| 5 | * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, |
| 6 | * IBM Corporation |
Cornelia Huck | 4ce3b30 | 2006-01-14 13:21:04 -0800 | [diff] [blame] | 7 | * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Martin Schwidefsky (schwidefsky@de.ibm.com) |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 13 | #include <linux/jiffies.h> |
| 14 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include <asm/ccwdev.h> |
Cornelia Huck | 4c24da7 | 2005-09-03 15:58:01 -0700 | [diff] [blame] | 17 | #include <asm/cio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include "cio.h" |
| 20 | #include "cio_debug.h" |
| 21 | #include "css.h" |
| 22 | #include "device.h" |
| 23 | #include "chsc.h" |
| 24 | #include "ioasm.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | int |
| 27 | device_is_online(struct subchannel *sch) |
| 28 | { |
| 29 | struct ccw_device *cdev; |
| 30 | |
| 31 | if (!sch->dev.driver_data) |
| 32 | return 0; |
| 33 | cdev = sch->dev.driver_data; |
| 34 | return (cdev->private->state == DEV_STATE_ONLINE); |
| 35 | } |
| 36 | |
| 37 | int |
| 38 | device_is_disconnected(struct subchannel *sch) |
| 39 | { |
| 40 | struct ccw_device *cdev; |
| 41 | |
| 42 | if (!sch->dev.driver_data) |
| 43 | return 0; |
| 44 | cdev = sch->dev.driver_data; |
| 45 | return (cdev->private->state == DEV_STATE_DISCONNECTED || |
| 46 | cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | device_set_disconnected(struct subchannel *sch) |
| 51 | { |
| 52 | struct ccw_device *cdev; |
| 53 | |
| 54 | if (!sch->dev.driver_data) |
| 55 | return; |
| 56 | cdev = sch->dev.driver_data; |
| 57 | ccw_device_set_timeout(cdev, 0); |
| 58 | cdev->private->flags.fake_irb = 0; |
| 59 | cdev->private->state = DEV_STATE_DISCONNECTED; |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | device_set_waiting(struct subchannel *sch) |
| 64 | { |
| 65 | struct ccw_device *cdev; |
| 66 | |
| 67 | if (!sch->dev.driver_data) |
| 68 | return; |
| 69 | cdev = sch->dev.driver_data; |
| 70 | ccw_device_set_timeout(cdev, 10*HZ); |
| 71 | cdev->private->state = DEV_STATE_WAIT4IO; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Timeout function. It just triggers a DEV_EVENT_TIMEOUT. |
| 76 | */ |
| 77 | static void |
| 78 | ccw_device_timeout(unsigned long data) |
| 79 | { |
| 80 | struct ccw_device *cdev; |
| 81 | |
| 82 | cdev = (struct ccw_device *) data; |
| 83 | spin_lock_irq(cdev->ccwlock); |
| 84 | dev_fsm_event(cdev, DEV_EVENT_TIMEOUT); |
| 85 | spin_unlock_irq(cdev->ccwlock); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Set timeout |
| 90 | */ |
| 91 | void |
| 92 | ccw_device_set_timeout(struct ccw_device *cdev, int expires) |
| 93 | { |
| 94 | if (expires == 0) { |
| 95 | del_timer(&cdev->private->timer); |
| 96 | return; |
| 97 | } |
| 98 | if (timer_pending(&cdev->private->timer)) { |
| 99 | if (mod_timer(&cdev->private->timer, jiffies + expires)) |
| 100 | return; |
| 101 | } |
| 102 | cdev->private->timer.function = ccw_device_timeout; |
| 103 | cdev->private->timer.data = (unsigned long) cdev; |
| 104 | cdev->private->timer.expires = jiffies + expires; |
| 105 | add_timer(&cdev->private->timer); |
| 106 | } |
| 107 | |
| 108 | /* Kill any pending timers after machine check. */ |
| 109 | void |
| 110 | device_kill_pending_timer(struct subchannel *sch) |
| 111 | { |
| 112 | struct ccw_device *cdev; |
| 113 | |
| 114 | if (!sch->dev.driver_data) |
| 115 | return; |
| 116 | cdev = sch->dev.driver_data; |
| 117 | ccw_device_set_timeout(cdev, 0); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Cancel running i/o. This is called repeatedly since halt/clear are |
| 122 | * asynchronous operations. We do one try with cio_cancel, two tries |
| 123 | * with cio_halt, 255 tries with cio_clear. If everythings fails panic. |
| 124 | * Returns 0 if device now idle, -ENODEV for device not operational and |
| 125 | * -EBUSY if an interrupt is expected (either from halt/clear or from a |
| 126 | * status pending). |
| 127 | */ |
| 128 | int |
| 129 | ccw_device_cancel_halt_clear(struct ccw_device *cdev) |
| 130 | { |
| 131 | struct subchannel *sch; |
| 132 | int ret; |
| 133 | |
| 134 | sch = to_subchannel(cdev->dev.parent); |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 135 | ret = stsch(sch->schid, &sch->schib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (ret || !sch->schib.pmcw.dnv) |
| 137 | return -ENODEV; |
| 138 | if (!sch->schib.pmcw.ena || sch->schib.scsw.actl == 0) |
| 139 | /* Not operational or no activity -> done. */ |
| 140 | return 0; |
| 141 | /* Stage 1: cancel io. */ |
| 142 | if (!(sch->schib.scsw.actl & SCSW_ACTL_HALT_PEND) && |
| 143 | !(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) { |
| 144 | ret = cio_cancel(sch); |
| 145 | if (ret != -EINVAL) |
| 146 | return ret; |
| 147 | /* cancel io unsuccessful. From now on it is asynchronous. */ |
| 148 | cdev->private->iretry = 3; /* 3 halt retries. */ |
| 149 | } |
| 150 | if (!(sch->schib.scsw.actl & SCSW_ACTL_CLEAR_PEND)) { |
| 151 | /* Stage 2: halt io. */ |
| 152 | if (cdev->private->iretry) { |
| 153 | cdev->private->iretry--; |
| 154 | ret = cio_halt(sch); |
| 155 | return (ret == 0) ? -EBUSY : ret; |
| 156 | } |
| 157 | /* halt io unsuccessful. */ |
| 158 | cdev->private->iretry = 255; /* 255 clear retries. */ |
| 159 | } |
| 160 | /* Stage 3: clear io. */ |
| 161 | if (cdev->private->iretry) { |
| 162 | cdev->private->iretry--; |
| 163 | ret = cio_clear (sch); |
| 164 | return (ret == 0) ? -EBUSY : ret; |
| 165 | } |
| 166 | panic("Can't stop i/o on subchannel.\n"); |
| 167 | } |
| 168 | |
| 169 | static int |
| 170 | ccw_device_handle_oper(struct ccw_device *cdev) |
| 171 | { |
| 172 | struct subchannel *sch; |
| 173 | |
| 174 | sch = to_subchannel(cdev->dev.parent); |
| 175 | cdev->private->flags.recog_done = 1; |
| 176 | /* |
| 177 | * Check if cu type and device type still match. If |
| 178 | * not, it is certainly another device and we have to |
| 179 | * de- and re-register. Also check here for non-matching devno. |
| 180 | */ |
| 181 | if (cdev->id.cu_type != cdev->private->senseid.cu_type || |
| 182 | cdev->id.cu_model != cdev->private->senseid.cu_model || |
| 183 | cdev->id.dev_type != cdev->private->senseid.dev_type || |
| 184 | cdev->id.dev_model != cdev->private->senseid.dev_model || |
| 185 | cdev->private->devno != sch->schib.pmcw.dev) { |
| 186 | PREPARE_WORK(&cdev->private->kick_work, |
| 187 | ccw_device_do_unreg_rereg, (void *)cdev); |
| 188 | queue_work(ccw_device_work, &cdev->private->kick_work); |
| 189 | return 0; |
| 190 | } |
| 191 | cdev->private->flags.donotify = 1; |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * The machine won't give us any notification by machine check if a chpid has |
| 197 | * been varied online on the SE so we have to find out by magic (i. e. driving |
| 198 | * the channel subsystem to device selection and updating our path masks). |
| 199 | */ |
| 200 | static inline void |
| 201 | __recover_lost_chpids(struct subchannel *sch, int old_lpm) |
| 202 | { |
| 203 | int mask, i; |
| 204 | |
| 205 | for (i = 0; i<8; i++) { |
| 206 | mask = 0x80 >> i; |
| 207 | if (!(sch->lpm & mask)) |
| 208 | continue; |
| 209 | if (old_lpm & mask) |
| 210 | continue; |
| 211 | chpid_is_actually_online(sch->schib.pmcw.chpid[i]); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Stop device recognition. |
| 217 | */ |
| 218 | static void |
| 219 | ccw_device_recog_done(struct ccw_device *cdev, int state) |
| 220 | { |
| 221 | struct subchannel *sch; |
| 222 | int notify, old_lpm, same_dev; |
| 223 | |
| 224 | sch = to_subchannel(cdev->dev.parent); |
| 225 | |
| 226 | ccw_device_set_timeout(cdev, 0); |
| 227 | cio_disable_subchannel(sch); |
| 228 | /* |
| 229 | * Now that we tried recognition, we have performed device selection |
| 230 | * through ssch() and the path information is up to date. |
| 231 | */ |
| 232 | old_lpm = sch->lpm; |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 233 | stsch(sch->schid, &sch->schib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | sch->lpm = sch->schib.pmcw.pim & |
| 235 | sch->schib.pmcw.pam & |
| 236 | sch->schib.pmcw.pom & |
| 237 | sch->opm; |
Cornelia Huck | 4ffa923 | 2005-07-29 14:03:37 -0700 | [diff] [blame] | 238 | /* Check since device may again have become not operational. */ |
| 239 | if (!sch->schib.pmcw.dnv) |
| 240 | state = DEV_STATE_NOT_OPER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) |
| 242 | /* Force reprobe on all chpids. */ |
| 243 | old_lpm = 0; |
| 244 | if (sch->lpm != old_lpm) |
| 245 | __recover_lost_chpids(sch, old_lpm); |
| 246 | if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) { |
| 247 | if (state == DEV_STATE_NOT_OPER) { |
| 248 | cdev->private->flags.recog_done = 1; |
| 249 | cdev->private->state = DEV_STATE_DISCONNECTED; |
| 250 | return; |
| 251 | } |
| 252 | /* Boxed devices don't need extra treatment. */ |
| 253 | } |
| 254 | notify = 0; |
| 255 | same_dev = 0; /* Keep the compiler quiet... */ |
| 256 | switch (state) { |
| 257 | case DEV_STATE_NOT_OPER: |
| 258 | CIO_DEBUG(KERN_WARNING, 2, |
Cornelia Huck | fb6958a | 2006-01-06 00:19:25 -0800 | [diff] [blame] | 259 | "SenseID : unknown device %04x on subchannel " |
| 260 | "0.%x.%04x\n", cdev->private->devno, |
| 261 | sch->schid.ssid, sch->schid.sch_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | break; |
| 263 | case DEV_STATE_OFFLINE: |
| 264 | if (cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID) { |
| 265 | same_dev = ccw_device_handle_oper(cdev); |
| 266 | notify = 1; |
| 267 | } |
| 268 | /* fill out sense information */ |
| 269 | cdev->id = (struct ccw_device_id) { |
| 270 | .cu_type = cdev->private->senseid.cu_type, |
| 271 | .cu_model = cdev->private->senseid.cu_model, |
| 272 | .dev_type = cdev->private->senseid.dev_type, |
| 273 | .dev_model = cdev->private->senseid.dev_model, |
| 274 | }; |
| 275 | if (notify) { |
| 276 | cdev->private->state = DEV_STATE_OFFLINE; |
| 277 | if (same_dev) { |
| 278 | /* Get device online again. */ |
| 279 | ccw_device_online(cdev); |
| 280 | wake_up(&cdev->private->wait_q); |
| 281 | } |
| 282 | return; |
| 283 | } |
| 284 | /* Issue device info message. */ |
Cornelia Huck | fb6958a | 2006-01-06 00:19:25 -0800 | [diff] [blame] | 285 | CIO_DEBUG(KERN_INFO, 2, "SenseID : device 0.%x.%04x reports: " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | "CU Type/Mod = %04X/%02X, Dev Type/Mod = " |
Cornelia Huck | fb6958a | 2006-01-06 00:19:25 -0800 | [diff] [blame] | 287 | "%04X/%02X\n", |
| 288 | cdev->private->ssid, cdev->private->devno, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | cdev->id.cu_type, cdev->id.cu_model, |
| 290 | cdev->id.dev_type, cdev->id.dev_model); |
| 291 | break; |
| 292 | case DEV_STATE_BOXED: |
| 293 | CIO_DEBUG(KERN_WARNING, 2, |
Cornelia Huck | fb6958a | 2006-01-06 00:19:25 -0800 | [diff] [blame] | 294 | "SenseID : boxed device %04x on subchannel " |
| 295 | "0.%x.%04x\n", cdev->private->devno, |
| 296 | sch->schid.ssid, sch->schid.sch_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | cdev->private->state = state; |
| 300 | io_subchannel_recog_done(cdev); |
| 301 | if (state != DEV_STATE_NOT_OPER) |
| 302 | wake_up(&cdev->private->wait_q); |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Function called from device_id.c after sense id has completed. |
| 307 | */ |
| 308 | void |
| 309 | ccw_device_sense_id_done(struct ccw_device *cdev, int err) |
| 310 | { |
| 311 | switch (err) { |
| 312 | case 0: |
| 313 | ccw_device_recog_done(cdev, DEV_STATE_OFFLINE); |
| 314 | break; |
| 315 | case -ETIME: /* Sense id stopped by timeout. */ |
| 316 | ccw_device_recog_done(cdev, DEV_STATE_BOXED); |
| 317 | break; |
| 318 | default: |
| 319 | ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER); |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | static void |
| 325 | ccw_device_oper_notify(void *data) |
| 326 | { |
| 327 | struct ccw_device *cdev; |
| 328 | struct subchannel *sch; |
| 329 | int ret; |
| 330 | |
| 331 | cdev = (struct ccw_device *)data; |
| 332 | sch = to_subchannel(cdev->dev.parent); |
| 333 | ret = (sch->driver && sch->driver->notify) ? |
| 334 | sch->driver->notify(&sch->dev, CIO_OPER) : 0; |
| 335 | if (!ret) |
| 336 | /* Driver doesn't want device back. */ |
| 337 | ccw_device_do_unreg_rereg((void *)cdev); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 338 | else { |
| 339 | /* Reenable channel measurements, if needed. */ |
| 340 | cmf_reenable(cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | wake_up(&cdev->private->wait_q); |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Finished with online/offline processing. |
| 347 | */ |
| 348 | static void |
| 349 | ccw_device_done(struct ccw_device *cdev, int state) |
| 350 | { |
| 351 | struct subchannel *sch; |
| 352 | |
| 353 | sch = to_subchannel(cdev->dev.parent); |
| 354 | |
| 355 | if (state != DEV_STATE_ONLINE) |
| 356 | cio_disable_subchannel(sch); |
| 357 | |
| 358 | /* Reset device status. */ |
| 359 | memset(&cdev->private->irb, 0, sizeof(struct irb)); |
| 360 | |
| 361 | cdev->private->state = state; |
| 362 | |
| 363 | |
| 364 | if (state == DEV_STATE_BOXED) |
| 365 | CIO_DEBUG(KERN_WARNING, 2, |
| 366 | "Boxed device %04x on subchannel %04x\n", |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 367 | cdev->private->devno, sch->schid.sch_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | if (cdev->private->flags.donotify) { |
| 370 | cdev->private->flags.donotify = 0; |
| 371 | PREPARE_WORK(&cdev->private->kick_work, ccw_device_oper_notify, |
| 372 | (void *)cdev); |
| 373 | queue_work(ccw_device_notify_work, &cdev->private->kick_work); |
| 374 | } |
| 375 | wake_up(&cdev->private->wait_q); |
| 376 | |
| 377 | if (css_init_done && state != DEV_STATE_ONLINE) |
| 378 | put_device (&cdev->dev); |
| 379 | } |
| 380 | |
Cornelia Huck | 7e56081 | 2006-07-12 16:40:19 +0200 | [diff] [blame^] | 381 | static inline int cmp_pgid(struct pgid *p1, struct pgid *p2) |
| 382 | { |
| 383 | char *c1; |
| 384 | char *c2; |
| 385 | |
| 386 | c1 = (char *)p1; |
| 387 | c2 = (char *)p2; |
| 388 | |
| 389 | return memcmp(c1 + 1, c2 + 1, sizeof(struct pgid) - 1); |
| 390 | } |
| 391 | |
| 392 | static void __ccw_device_get_common_pgid(struct ccw_device *cdev) |
| 393 | { |
| 394 | int i; |
| 395 | int last; |
| 396 | |
| 397 | last = 0; |
| 398 | for (i = 0; i < 8; i++) { |
| 399 | if (cdev->private->pgid[i].inf.ps.state1 == SNID_STATE1_RESET) |
| 400 | /* No PGID yet */ |
| 401 | continue; |
| 402 | if (cdev->private->pgid[last].inf.ps.state1 == |
| 403 | SNID_STATE1_RESET) { |
| 404 | /* First non-zero PGID */ |
| 405 | last = i; |
| 406 | continue; |
| 407 | } |
| 408 | if (cmp_pgid(&cdev->private->pgid[i], |
| 409 | &cdev->private->pgid[last]) == 0) |
| 410 | /* Non-conflicting PGIDs */ |
| 411 | continue; |
| 412 | |
| 413 | /* PGID mismatch, can't pathgroup. */ |
| 414 | CIO_MSG_EVENT(0, "SNID - pgid mismatch for device " |
| 415 | "0.%x.%04x, can't pathgroup\n", |
| 416 | cdev->private->ssid, cdev->private->devno); |
| 417 | cdev->private->options.pgroup = 0; |
| 418 | return; |
| 419 | } |
| 420 | if (cdev->private->pgid[last].inf.ps.state1 == |
| 421 | SNID_STATE1_RESET) |
| 422 | /* No previous pgid found */ |
| 423 | memcpy(&cdev->private->pgid[0], &css[0]->global_pgid, |
| 424 | sizeof(struct pgid)); |
| 425 | else |
| 426 | /* Use existing pgid */ |
| 427 | memcpy(&cdev->private->pgid[0], &cdev->private->pgid[last], |
| 428 | sizeof(struct pgid)); |
| 429 | } |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | /* |
| 432 | * Function called from device_pgid.c after sense path ground has completed. |
| 433 | */ |
| 434 | void |
| 435 | ccw_device_sense_pgid_done(struct ccw_device *cdev, int err) |
| 436 | { |
| 437 | struct subchannel *sch; |
| 438 | |
| 439 | sch = to_subchannel(cdev->dev.parent); |
| 440 | switch (err) { |
Cornelia Huck | 7e56081 | 2006-07-12 16:40:19 +0200 | [diff] [blame^] | 441 | case -EOPNOTSUPP: /* path grouping not supported, use nop instead. */ |
| 442 | cdev->private->options.pgroup = 0; |
| 443 | break; |
| 444 | case 0: /* success */ |
| 445 | case -EACCES: /* partial success, some paths not operational */ |
| 446 | /* Check if all pgids are equal or 0. */ |
| 447 | __ccw_device_get_common_pgid(cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | break; |
| 449 | case -ETIME: /* Sense path group id stopped by timeout. */ |
| 450 | case -EUSERS: /* device is reserved for someone else. */ |
| 451 | ccw_device_done(cdev, DEV_STATE_BOXED); |
Cornelia Huck | 7e56081 | 2006-07-12 16:40:19 +0200 | [diff] [blame^] | 452 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | default: |
| 454 | ccw_device_done(cdev, DEV_STATE_NOT_OPER); |
Cornelia Huck | 7e56081 | 2006-07-12 16:40:19 +0200 | [diff] [blame^] | 455 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
Cornelia Huck | 7e56081 | 2006-07-12 16:40:19 +0200 | [diff] [blame^] | 457 | /* Start Path Group verification. */ |
| 458 | sch->vpm = 0; /* Start with no path groups set. */ |
| 459 | cdev->private->state = DEV_STATE_VERIFY; |
| 460 | ccw_device_verify_start(cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* |
| 464 | * Start device recognition. |
| 465 | */ |
| 466 | int |
| 467 | ccw_device_recognition(struct ccw_device *cdev) |
| 468 | { |
| 469 | struct subchannel *sch; |
| 470 | int ret; |
| 471 | |
| 472 | if ((cdev->private->state != DEV_STATE_NOT_OPER) && |
| 473 | (cdev->private->state != DEV_STATE_BOXED)) |
| 474 | return -EINVAL; |
| 475 | sch = to_subchannel(cdev->dev.parent); |
| 476 | ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc); |
| 477 | if (ret != 0) |
| 478 | /* Couldn't enable the subchannel for i/o. Sick device. */ |
| 479 | return ret; |
| 480 | |
| 481 | /* After 60s the device recognition is considered to have failed. */ |
| 482 | ccw_device_set_timeout(cdev, 60*HZ); |
| 483 | |
| 484 | /* |
| 485 | * We used to start here with a sense pgid to find out whether a device |
| 486 | * is locked by someone else. Unfortunately, the sense pgid command |
| 487 | * code has other meanings on devices predating the path grouping |
| 488 | * algorithm, so we start with sense id and box the device after an |
| 489 | * timeout (or if sense pgid during path verification detects the device |
| 490 | * is locked, as may happen on newer devices). |
| 491 | */ |
| 492 | cdev->private->flags.recog_done = 0; |
| 493 | cdev->private->state = DEV_STATE_SENSE_ID; |
| 494 | ccw_device_sense_id_start(cdev); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Handle timeout in device recognition. |
| 500 | */ |
| 501 | static void |
| 502 | ccw_device_recog_timeout(struct ccw_device *cdev, enum dev_event dev_event) |
| 503 | { |
| 504 | int ret; |
| 505 | |
| 506 | ret = ccw_device_cancel_halt_clear(cdev); |
| 507 | switch (ret) { |
| 508 | case 0: |
| 509 | ccw_device_recog_done(cdev, DEV_STATE_BOXED); |
| 510 | break; |
| 511 | case -ENODEV: |
| 512 | ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER); |
| 513 | break; |
| 514 | default: |
| 515 | ccw_device_set_timeout(cdev, 3*HZ); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | |
| 520 | static void |
| 521 | ccw_device_nopath_notify(void *data) |
| 522 | { |
| 523 | struct ccw_device *cdev; |
| 524 | struct subchannel *sch; |
| 525 | int ret; |
| 526 | |
| 527 | cdev = (struct ccw_device *)data; |
| 528 | sch = to_subchannel(cdev->dev.parent); |
| 529 | /* Extra sanity. */ |
| 530 | if (sch->lpm) |
| 531 | return; |
| 532 | ret = (sch->driver && sch->driver->notify) ? |
| 533 | sch->driver->notify(&sch->dev, CIO_NO_PATH) : 0; |
| 534 | if (!ret) { |
| 535 | if (get_device(&sch->dev)) { |
| 536 | /* Driver doesn't want to keep device. */ |
| 537 | cio_disable_subchannel(sch); |
| 538 | if (get_device(&cdev->dev)) { |
| 539 | PREPARE_WORK(&cdev->private->kick_work, |
| 540 | ccw_device_call_sch_unregister, |
| 541 | (void *)cdev); |
| 542 | queue_work(ccw_device_work, |
| 543 | &cdev->private->kick_work); |
| 544 | } else |
| 545 | put_device(&sch->dev); |
| 546 | } |
| 547 | } else { |
| 548 | cio_disable_subchannel(sch); |
| 549 | ccw_device_set_timeout(cdev, 0); |
| 550 | cdev->private->flags.fake_irb = 0; |
| 551 | cdev->private->state = DEV_STATE_DISCONNECTED; |
| 552 | wake_up(&cdev->private->wait_q); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | void |
| 557 | ccw_device_verify_done(struct ccw_device *cdev, int err) |
| 558 | { |
| 559 | cdev->private->flags.doverify = 0; |
| 560 | switch (err) { |
| 561 | case -EOPNOTSUPP: /* path grouping not supported, just set online. */ |
| 562 | cdev->private->options.pgroup = 0; |
| 563 | case 0: |
| 564 | ccw_device_done(cdev, DEV_STATE_ONLINE); |
| 565 | /* Deliver fake irb to device driver, if needed. */ |
| 566 | if (cdev->private->flags.fake_irb) { |
| 567 | memset(&cdev->private->irb, 0, sizeof(struct irb)); |
| 568 | cdev->private->irb.scsw = (struct scsw) { |
| 569 | .cc = 1, |
| 570 | .fctl = SCSW_FCTL_START_FUNC, |
| 571 | .actl = SCSW_ACTL_START_PEND, |
| 572 | .stctl = SCSW_STCTL_STATUS_PEND, |
| 573 | }; |
| 574 | cdev->private->flags.fake_irb = 0; |
| 575 | if (cdev->handler) |
| 576 | cdev->handler(cdev, cdev->private->intparm, |
| 577 | &cdev->private->irb); |
| 578 | memset(&cdev->private->irb, 0, sizeof(struct irb)); |
| 579 | } |
| 580 | break; |
| 581 | case -ETIME: |
| 582 | ccw_device_done(cdev, DEV_STATE_BOXED); |
| 583 | break; |
| 584 | default: |
| 585 | PREPARE_WORK(&cdev->private->kick_work, |
| 586 | ccw_device_nopath_notify, (void *)cdev); |
| 587 | queue_work(ccw_device_notify_work, &cdev->private->kick_work); |
| 588 | ccw_device_done(cdev, DEV_STATE_NOT_OPER); |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Get device online. |
| 595 | */ |
| 596 | int |
| 597 | ccw_device_online(struct ccw_device *cdev) |
| 598 | { |
| 599 | struct subchannel *sch; |
| 600 | int ret; |
| 601 | |
| 602 | if ((cdev->private->state != DEV_STATE_OFFLINE) && |
| 603 | (cdev->private->state != DEV_STATE_BOXED)) |
| 604 | return -EINVAL; |
| 605 | sch = to_subchannel(cdev->dev.parent); |
| 606 | if (css_init_done && !get_device(&cdev->dev)) |
| 607 | return -ENODEV; |
| 608 | ret = cio_enable_subchannel(sch, sch->schib.pmcw.isc); |
| 609 | if (ret != 0) { |
| 610 | /* Couldn't enable the subchannel for i/o. Sick device. */ |
| 611 | if (ret == -ENODEV) |
| 612 | dev_fsm_event(cdev, DEV_EVENT_NOTOPER); |
| 613 | return ret; |
| 614 | } |
| 615 | /* Do we want to do path grouping? */ |
| 616 | if (!cdev->private->options.pgroup) { |
Cornelia Huck | 7e56081 | 2006-07-12 16:40:19 +0200 | [diff] [blame^] | 617 | /* Start initial path verification. */ |
| 618 | cdev->private->state = DEV_STATE_VERIFY; |
| 619 | ccw_device_verify_start(cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | /* Do a SensePGID first. */ |
| 623 | cdev->private->state = DEV_STATE_SENSE_PGID; |
| 624 | ccw_device_sense_pgid_start(cdev); |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | void |
| 629 | ccw_device_disband_done(struct ccw_device *cdev, int err) |
| 630 | { |
| 631 | switch (err) { |
| 632 | case 0: |
| 633 | ccw_device_done(cdev, DEV_STATE_OFFLINE); |
| 634 | break; |
| 635 | case -ETIME: |
| 636 | ccw_device_done(cdev, DEV_STATE_BOXED); |
| 637 | break; |
| 638 | default: |
| 639 | ccw_device_done(cdev, DEV_STATE_NOT_OPER); |
| 640 | break; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Shutdown device. |
| 646 | */ |
| 647 | int |
| 648 | ccw_device_offline(struct ccw_device *cdev) |
| 649 | { |
| 650 | struct subchannel *sch; |
| 651 | |
| 652 | sch = to_subchannel(cdev->dev.parent); |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 653 | if (stsch(sch->schid, &sch->schib) || !sch->schib.pmcw.dnv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return -ENODEV; |
| 655 | if (cdev->private->state != DEV_STATE_ONLINE) { |
| 656 | if (sch->schib.scsw.actl != 0) |
| 657 | return -EBUSY; |
| 658 | return -EINVAL; |
| 659 | } |
| 660 | if (sch->schib.scsw.actl != 0) |
| 661 | return -EBUSY; |
| 662 | /* Are we doing path grouping? */ |
| 663 | if (!cdev->private->options.pgroup) { |
| 664 | /* No, set state offline immediately. */ |
Cornelia Huck | 7e56081 | 2006-07-12 16:40:19 +0200 | [diff] [blame^] | 665 | sch->vpm = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | ccw_device_done(cdev, DEV_STATE_OFFLINE); |
| 667 | return 0; |
| 668 | } |
| 669 | /* Start Set Path Group commands. */ |
| 670 | cdev->private->state = DEV_STATE_DISBAND_PGID; |
| 671 | ccw_device_disband_start(cdev); |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Handle timeout in device online/offline process. |
| 677 | */ |
| 678 | static void |
| 679 | ccw_device_onoff_timeout(struct ccw_device *cdev, enum dev_event dev_event) |
| 680 | { |
| 681 | int ret; |
| 682 | |
| 683 | ret = ccw_device_cancel_halt_clear(cdev); |
| 684 | switch (ret) { |
| 685 | case 0: |
| 686 | ccw_device_done(cdev, DEV_STATE_BOXED); |
| 687 | break; |
| 688 | case -ENODEV: |
| 689 | ccw_device_done(cdev, DEV_STATE_NOT_OPER); |
| 690 | break; |
| 691 | default: |
| 692 | ccw_device_set_timeout(cdev, 3*HZ); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * Handle not oper event in device recognition. |
| 698 | */ |
| 699 | static void |
| 700 | ccw_device_recog_notoper(struct ccw_device *cdev, enum dev_event dev_event) |
| 701 | { |
| 702 | ccw_device_recog_done(cdev, DEV_STATE_NOT_OPER); |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * Handle not operational event while offline. |
| 707 | */ |
| 708 | static void |
| 709 | ccw_device_offline_notoper(struct ccw_device *cdev, enum dev_event dev_event) |
| 710 | { |
| 711 | struct subchannel *sch; |
| 712 | |
| 713 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 714 | sch = to_subchannel(cdev->dev.parent); |
| 715 | if (get_device(&cdev->dev)) { |
| 716 | PREPARE_WORK(&cdev->private->kick_work, |
| 717 | ccw_device_call_sch_unregister, (void *)cdev); |
| 718 | queue_work(ccw_device_work, &cdev->private->kick_work); |
| 719 | } |
| 720 | wake_up(&cdev->private->wait_q); |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Handle not operational event while online. |
| 725 | */ |
| 726 | static void |
| 727 | ccw_device_online_notoper(struct ccw_device *cdev, enum dev_event dev_event) |
| 728 | { |
| 729 | struct subchannel *sch; |
| 730 | |
| 731 | sch = to_subchannel(cdev->dev.parent); |
| 732 | if (sch->driver->notify && |
| 733 | sch->driver->notify(&sch->dev, sch->lpm ? CIO_GONE : CIO_NO_PATH)) { |
| 734 | ccw_device_set_timeout(cdev, 0); |
| 735 | cdev->private->flags.fake_irb = 0; |
| 736 | cdev->private->state = DEV_STATE_DISCONNECTED; |
| 737 | wake_up(&cdev->private->wait_q); |
| 738 | return; |
| 739 | } |
| 740 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 741 | cio_disable_subchannel(sch); |
| 742 | if (sch->schib.scsw.actl != 0) { |
| 743 | // FIXME: not-oper indication to device driver ? |
| 744 | ccw_device_call_handler(cdev); |
| 745 | } |
| 746 | if (get_device(&cdev->dev)) { |
| 747 | PREPARE_WORK(&cdev->private->kick_work, |
| 748 | ccw_device_call_sch_unregister, (void *)cdev); |
| 749 | queue_work(ccw_device_work, &cdev->private->kick_work); |
| 750 | } |
| 751 | wake_up(&cdev->private->wait_q); |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * Handle path verification event. |
| 756 | */ |
| 757 | static void |
| 758 | ccw_device_online_verify(struct ccw_device *cdev, enum dev_event dev_event) |
| 759 | { |
| 760 | struct subchannel *sch; |
| 761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | if (cdev->private->state == DEV_STATE_W4SENSE) { |
| 763 | cdev->private->flags.doverify = 1; |
| 764 | return; |
| 765 | } |
| 766 | sch = to_subchannel(cdev->dev.parent); |
| 767 | /* |
| 768 | * Since we might not just be coming from an interrupt from the |
| 769 | * subchannel we have to update the schib. |
| 770 | */ |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 771 | stsch(sch->schid, &sch->schib); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
| 773 | if (sch->schib.scsw.actl != 0 || |
| 774 | (cdev->private->irb.scsw.stctl & SCSW_STCTL_STATUS_PEND)) { |
| 775 | /* |
| 776 | * No final status yet or final status not yet delivered |
| 777 | * to the device driver. Can't do path verfication now, |
| 778 | * delay until final status was delivered. |
| 779 | */ |
| 780 | cdev->private->flags.doverify = 1; |
| 781 | return; |
| 782 | } |
| 783 | /* Device is idle, we can do the path verification. */ |
| 784 | cdev->private->state = DEV_STATE_VERIFY; |
| 785 | ccw_device_verify_start(cdev); |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Got an interrupt for a normal io (state online). |
| 790 | */ |
| 791 | static void |
| 792 | ccw_device_irq(struct ccw_device *cdev, enum dev_event dev_event) |
| 793 | { |
| 794 | struct irb *irb; |
| 795 | |
| 796 | irb = (struct irb *) __LC_IRB; |
| 797 | /* Check for unsolicited interrupt. */ |
| 798 | if ((irb->scsw.stctl == |
| 799 | (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) |
| 800 | && (!irb->scsw.cc)) { |
| 801 | if ((irb->scsw.dstat & DEV_STAT_UNIT_CHECK) && |
| 802 | !irb->esw.esw0.erw.cons) { |
| 803 | /* Unit check but no sense data. Need basic sense. */ |
| 804 | if (ccw_device_do_sense(cdev, irb) != 0) |
| 805 | goto call_handler_unsol; |
Cornelia Huck | e0ec574 | 2006-06-04 02:51:27 -0700 | [diff] [blame] | 806 | memcpy(&cdev->private->irb, irb, sizeof(struct irb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | cdev->private->state = DEV_STATE_W4SENSE; |
| 808 | cdev->private->intparm = 0; |
| 809 | return; |
| 810 | } |
| 811 | call_handler_unsol: |
| 812 | if (cdev->handler) |
| 813 | cdev->handler (cdev, 0, irb); |
| 814 | return; |
| 815 | } |
| 816 | /* Accumulate status and find out if a basic sense is needed. */ |
| 817 | ccw_device_accumulate_irb(cdev, irb); |
| 818 | if (cdev->private->flags.dosense) { |
| 819 | if (ccw_device_do_sense(cdev, irb) == 0) { |
| 820 | cdev->private->state = DEV_STATE_W4SENSE; |
| 821 | } |
| 822 | return; |
| 823 | } |
| 824 | /* Call the handler. */ |
| 825 | if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify) |
| 826 | /* Start delayed path verification. */ |
| 827 | ccw_device_online_verify(cdev, 0); |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Got an timeout in online state. |
| 832 | */ |
| 833 | static void |
| 834 | ccw_device_online_timeout(struct ccw_device *cdev, enum dev_event dev_event) |
| 835 | { |
| 836 | int ret; |
| 837 | |
| 838 | ccw_device_set_timeout(cdev, 0); |
| 839 | ret = ccw_device_cancel_halt_clear(cdev); |
| 840 | if (ret == -EBUSY) { |
| 841 | ccw_device_set_timeout(cdev, 3*HZ); |
| 842 | cdev->private->state = DEV_STATE_TIMEOUT_KILL; |
| 843 | return; |
| 844 | } |
| 845 | if (ret == -ENODEV) { |
| 846 | struct subchannel *sch; |
| 847 | |
| 848 | sch = to_subchannel(cdev->dev.parent); |
| 849 | if (!sch->lpm) { |
| 850 | PREPARE_WORK(&cdev->private->kick_work, |
| 851 | ccw_device_nopath_notify, (void *)cdev); |
| 852 | queue_work(ccw_device_notify_work, |
| 853 | &cdev->private->kick_work); |
| 854 | } else |
| 855 | dev_fsm_event(cdev, DEV_EVENT_NOTOPER); |
| 856 | } else if (cdev->handler) |
| 857 | cdev->handler(cdev, cdev->private->intparm, |
| 858 | ERR_PTR(-ETIMEDOUT)); |
| 859 | } |
| 860 | |
| 861 | /* |
| 862 | * Got an interrupt for a basic sense. |
| 863 | */ |
| 864 | void |
| 865 | ccw_device_w4sense(struct ccw_device *cdev, enum dev_event dev_event) |
| 866 | { |
| 867 | struct irb *irb; |
| 868 | |
| 869 | irb = (struct irb *) __LC_IRB; |
| 870 | /* Check for unsolicited interrupt. */ |
| 871 | if (irb->scsw.stctl == |
| 872 | (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) { |
| 873 | if (irb->scsw.cc == 1) |
| 874 | /* Basic sense hasn't started. Try again. */ |
| 875 | ccw_device_do_sense(cdev, irb); |
| 876 | else { |
| 877 | printk("Huh? %s(%s): unsolicited interrupt...\n", |
| 878 | __FUNCTION__, cdev->dev.bus_id); |
| 879 | if (cdev->handler) |
| 880 | cdev->handler (cdev, 0, irb); |
| 881 | } |
| 882 | return; |
| 883 | } |
Cornelia Huck | 3ba1998 | 2006-03-24 03:15:12 -0800 | [diff] [blame] | 884 | /* |
| 885 | * Check if a halt or clear has been issued in the meanwhile. If yes, |
| 886 | * only deliver the halt/clear interrupt to the device driver as if it |
| 887 | * had killed the original request. |
| 888 | */ |
| 889 | if (irb->scsw.fctl & (SCSW_FCTL_CLEAR_FUNC | SCSW_FCTL_HALT_FUNC)) { |
| 890 | cdev->private->flags.dosense = 0; |
| 891 | memset(&cdev->private->irb, 0, sizeof(struct irb)); |
| 892 | ccw_device_accumulate_irb(cdev, irb); |
| 893 | goto call_handler; |
| 894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | /* Add basic sense info to irb. */ |
| 896 | ccw_device_accumulate_basic_sense(cdev, irb); |
| 897 | if (cdev->private->flags.dosense) { |
| 898 | /* Another basic sense is needed. */ |
| 899 | ccw_device_do_sense(cdev, irb); |
| 900 | return; |
| 901 | } |
Cornelia Huck | 3ba1998 | 2006-03-24 03:15:12 -0800 | [diff] [blame] | 902 | call_handler: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | cdev->private->state = DEV_STATE_ONLINE; |
| 904 | /* Call the handler. */ |
| 905 | if (ccw_device_call_handler(cdev) && cdev->private->flags.doverify) |
| 906 | /* Start delayed path verification. */ |
| 907 | ccw_device_online_verify(cdev, 0); |
| 908 | } |
| 909 | |
| 910 | static void |
| 911 | ccw_device_clear_verify(struct ccw_device *cdev, enum dev_event dev_event) |
| 912 | { |
| 913 | struct irb *irb; |
| 914 | |
| 915 | irb = (struct irb *) __LC_IRB; |
| 916 | /* Accumulate status. We don't do basic sense. */ |
| 917 | ccw_device_accumulate_irb(cdev, irb); |
Cornelia Huck | b4f7b1e | 2006-06-29 15:03:35 +0200 | [diff] [blame] | 918 | /* Remember to clear irb to avoid residuals. */ |
| 919 | memset(&cdev->private->irb, 0, sizeof(struct irb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | /* Try to start delayed device verification. */ |
| 921 | ccw_device_online_verify(cdev, 0); |
| 922 | /* Note: Don't call handler for cio initiated clear! */ |
| 923 | } |
| 924 | |
| 925 | static void |
| 926 | ccw_device_killing_irq(struct ccw_device *cdev, enum dev_event dev_event) |
| 927 | { |
| 928 | struct subchannel *sch; |
| 929 | |
| 930 | sch = to_subchannel(cdev->dev.parent); |
| 931 | ccw_device_set_timeout(cdev, 0); |
| 932 | /* OK, i/o is dead now. Call interrupt handler. */ |
| 933 | cdev->private->state = DEV_STATE_ONLINE; |
| 934 | if (cdev->handler) |
| 935 | cdev->handler(cdev, cdev->private->intparm, |
| 936 | ERR_PTR(-ETIMEDOUT)); |
| 937 | if (!sch->lpm) { |
| 938 | PREPARE_WORK(&cdev->private->kick_work, |
| 939 | ccw_device_nopath_notify, (void *)cdev); |
| 940 | queue_work(ccw_device_notify_work, &cdev->private->kick_work); |
| 941 | } else if (cdev->private->flags.doverify) |
| 942 | /* Start delayed path verification. */ |
| 943 | ccw_device_online_verify(cdev, 0); |
| 944 | } |
| 945 | |
| 946 | static void |
| 947 | ccw_device_killing_timeout(struct ccw_device *cdev, enum dev_event dev_event) |
| 948 | { |
| 949 | int ret; |
| 950 | |
| 951 | ret = ccw_device_cancel_halt_clear(cdev); |
| 952 | if (ret == -EBUSY) { |
| 953 | ccw_device_set_timeout(cdev, 3*HZ); |
| 954 | return; |
| 955 | } |
| 956 | if (ret == -ENODEV) { |
| 957 | struct subchannel *sch; |
| 958 | |
| 959 | sch = to_subchannel(cdev->dev.parent); |
| 960 | if (!sch->lpm) { |
| 961 | PREPARE_WORK(&cdev->private->kick_work, |
| 962 | ccw_device_nopath_notify, (void *)cdev); |
| 963 | queue_work(ccw_device_notify_work, |
| 964 | &cdev->private->kick_work); |
| 965 | } else |
| 966 | dev_fsm_event(cdev, DEV_EVENT_NOTOPER); |
| 967 | return; |
| 968 | } |
| 969 | //FIXME: Can we get here? |
| 970 | cdev->private->state = DEV_STATE_ONLINE; |
| 971 | if (cdev->handler) |
| 972 | cdev->handler(cdev, cdev->private->intparm, |
| 973 | ERR_PTR(-ETIMEDOUT)); |
| 974 | } |
| 975 | |
| 976 | static void |
| 977 | ccw_device_wait4io_irq(struct ccw_device *cdev, enum dev_event dev_event) |
| 978 | { |
| 979 | struct irb *irb; |
| 980 | struct subchannel *sch; |
| 981 | |
| 982 | irb = (struct irb *) __LC_IRB; |
| 983 | /* |
| 984 | * Accumulate status and find out if a basic sense is needed. |
| 985 | * This is fine since we have already adapted the lpm. |
| 986 | */ |
| 987 | ccw_device_accumulate_irb(cdev, irb); |
| 988 | if (cdev->private->flags.dosense) { |
| 989 | if (ccw_device_do_sense(cdev, irb) == 0) { |
| 990 | cdev->private->state = DEV_STATE_W4SENSE; |
| 991 | } |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | /* Iff device is idle, reset timeout. */ |
| 996 | sch = to_subchannel(cdev->dev.parent); |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 997 | if (!stsch(sch->schid, &sch->schib)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | if (sch->schib.scsw.actl == 0) |
| 999 | ccw_device_set_timeout(cdev, 0); |
| 1000 | /* Call the handler. */ |
| 1001 | ccw_device_call_handler(cdev); |
| 1002 | if (!sch->lpm) { |
| 1003 | PREPARE_WORK(&cdev->private->kick_work, |
| 1004 | ccw_device_nopath_notify, (void *)cdev); |
| 1005 | queue_work(ccw_device_notify_work, &cdev->private->kick_work); |
| 1006 | } else if (cdev->private->flags.doverify) |
| 1007 | ccw_device_online_verify(cdev, 0); |
| 1008 | } |
| 1009 | |
| 1010 | static void |
| 1011 | ccw_device_wait4io_timeout(struct ccw_device *cdev, enum dev_event dev_event) |
| 1012 | { |
| 1013 | int ret; |
| 1014 | struct subchannel *sch; |
| 1015 | |
| 1016 | sch = to_subchannel(cdev->dev.parent); |
| 1017 | ccw_device_set_timeout(cdev, 0); |
| 1018 | ret = ccw_device_cancel_halt_clear(cdev); |
| 1019 | if (ret == -EBUSY) { |
| 1020 | ccw_device_set_timeout(cdev, 3*HZ); |
| 1021 | cdev->private->state = DEV_STATE_TIMEOUT_KILL; |
| 1022 | return; |
| 1023 | } |
| 1024 | if (ret == -ENODEV) { |
| 1025 | if (!sch->lpm) { |
| 1026 | PREPARE_WORK(&cdev->private->kick_work, |
| 1027 | ccw_device_nopath_notify, (void *)cdev); |
| 1028 | queue_work(ccw_device_notify_work, |
| 1029 | &cdev->private->kick_work); |
| 1030 | } else |
| 1031 | dev_fsm_event(cdev, DEV_EVENT_NOTOPER); |
| 1032 | return; |
| 1033 | } |
| 1034 | if (cdev->handler) |
| 1035 | cdev->handler(cdev, cdev->private->intparm, |
| 1036 | ERR_PTR(-ETIMEDOUT)); |
| 1037 | if (!sch->lpm) { |
| 1038 | PREPARE_WORK(&cdev->private->kick_work, |
| 1039 | ccw_device_nopath_notify, (void *)cdev); |
| 1040 | queue_work(ccw_device_notify_work, &cdev->private->kick_work); |
| 1041 | } else if (cdev->private->flags.doverify) |
| 1042 | /* Start delayed path verification. */ |
| 1043 | ccw_device_online_verify(cdev, 0); |
| 1044 | } |
| 1045 | |
| 1046 | static void |
| 1047 | ccw_device_wait4io_verify(struct ccw_device *cdev, enum dev_event dev_event) |
| 1048 | { |
| 1049 | /* When the I/O has terminated, we have to start verification. */ |
Cornelia Huck | 7e56081 | 2006-07-12 16:40:19 +0200 | [diff] [blame^] | 1050 | cdev->private->flags.doverify = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | static void |
| 1054 | ccw_device_stlck_done(struct ccw_device *cdev, enum dev_event dev_event) |
| 1055 | { |
| 1056 | struct irb *irb; |
| 1057 | |
| 1058 | switch (dev_event) { |
| 1059 | case DEV_EVENT_INTERRUPT: |
| 1060 | irb = (struct irb *) __LC_IRB; |
| 1061 | /* Check for unsolicited interrupt. */ |
| 1062 | if ((irb->scsw.stctl == |
| 1063 | (SCSW_STCTL_STATUS_PEND | SCSW_STCTL_ALERT_STATUS)) && |
| 1064 | (!irb->scsw.cc)) |
| 1065 | /* FIXME: we should restart stlck here, but this |
| 1066 | * is extremely unlikely ... */ |
| 1067 | goto out_wakeup; |
| 1068 | |
| 1069 | ccw_device_accumulate_irb(cdev, irb); |
| 1070 | /* We don't care about basic sense etc. */ |
| 1071 | break; |
| 1072 | default: /* timeout */ |
| 1073 | break; |
| 1074 | } |
| 1075 | out_wakeup: |
| 1076 | wake_up(&cdev->private->wait_q); |
| 1077 | } |
| 1078 | |
| 1079 | static void |
| 1080 | ccw_device_start_id(struct ccw_device *cdev, enum dev_event dev_event) |
| 1081 | { |
| 1082 | struct subchannel *sch; |
| 1083 | |
| 1084 | sch = to_subchannel(cdev->dev.parent); |
| 1085 | if (cio_enable_subchannel(sch, sch->schib.pmcw.isc) != 0) |
| 1086 | /* Couldn't enable the subchannel for i/o. Sick device. */ |
| 1087 | return; |
| 1088 | |
| 1089 | /* After 60s the device recognition is considered to have failed. */ |
| 1090 | ccw_device_set_timeout(cdev, 60*HZ); |
| 1091 | |
| 1092 | cdev->private->state = DEV_STATE_DISCONNECTED_SENSE_ID; |
| 1093 | ccw_device_sense_id_start(cdev); |
| 1094 | } |
| 1095 | |
| 1096 | void |
| 1097 | device_trigger_reprobe(struct subchannel *sch) |
| 1098 | { |
| 1099 | struct ccw_device *cdev; |
| 1100 | |
| 1101 | if (!sch->dev.driver_data) |
| 1102 | return; |
| 1103 | cdev = sch->dev.driver_data; |
| 1104 | if (cdev->private->state != DEV_STATE_DISCONNECTED) |
| 1105 | return; |
| 1106 | |
| 1107 | /* Update some values. */ |
Cornelia Huck | a8237fc | 2006-01-06 00:19:21 -0800 | [diff] [blame] | 1108 | if (stsch(sch->schid, &sch->schib)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | return; |
| 1110 | |
| 1111 | /* |
| 1112 | * The pim, pam, pom values may not be accurate, but they are the best |
| 1113 | * we have before performing device selection :/ |
| 1114 | */ |
| 1115 | sch->lpm = sch->schib.pmcw.pim & |
| 1116 | sch->schib.pmcw.pam & |
| 1117 | sch->schib.pmcw.pom & |
| 1118 | sch->opm; |
| 1119 | /* Re-set some bits in the pmcw that were lost. */ |
| 1120 | sch->schib.pmcw.isc = 3; |
| 1121 | sch->schib.pmcw.csense = 1; |
| 1122 | sch->schib.pmcw.ena = 0; |
| 1123 | if ((sch->lpm & (sch->lpm - 1)) != 0) |
| 1124 | sch->schib.pmcw.mp = 1; |
| 1125 | sch->schib.pmcw.intparm = (__u32)(unsigned long)sch; |
| 1126 | /* We should also udate ssd info, but this has to wait. */ |
| 1127 | ccw_device_start_id(cdev, 0); |
| 1128 | } |
| 1129 | |
| 1130 | static void |
| 1131 | ccw_device_offline_irq(struct ccw_device *cdev, enum dev_event dev_event) |
| 1132 | { |
| 1133 | struct subchannel *sch; |
| 1134 | |
| 1135 | sch = to_subchannel(cdev->dev.parent); |
| 1136 | /* |
| 1137 | * An interrupt in state offline means a previous disable was not |
| 1138 | * successful. Try again. |
| 1139 | */ |
| 1140 | cio_disable_subchannel(sch); |
| 1141 | } |
| 1142 | |
| 1143 | static void |
| 1144 | ccw_device_change_cmfstate(struct ccw_device *cdev, enum dev_event dev_event) |
| 1145 | { |
| 1146 | retry_set_schib(cdev); |
| 1147 | cdev->private->state = DEV_STATE_ONLINE; |
| 1148 | dev_fsm_event(cdev, dev_event); |
| 1149 | } |
| 1150 | |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1151 | static void ccw_device_update_cmfblock(struct ccw_device *cdev, |
| 1152 | enum dev_event dev_event) |
| 1153 | { |
| 1154 | cmf_retry_copy_block(cdev); |
| 1155 | cdev->private->state = DEV_STATE_ONLINE; |
| 1156 | dev_fsm_event(cdev, dev_event); |
| 1157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
| 1159 | static void |
| 1160 | ccw_device_quiesce_done(struct ccw_device *cdev, enum dev_event dev_event) |
| 1161 | { |
| 1162 | ccw_device_set_timeout(cdev, 0); |
| 1163 | if (dev_event == DEV_EVENT_NOTOPER) |
| 1164 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 1165 | else |
| 1166 | cdev->private->state = DEV_STATE_OFFLINE; |
| 1167 | wake_up(&cdev->private->wait_q); |
| 1168 | } |
| 1169 | |
| 1170 | static void |
| 1171 | ccw_device_quiesce_timeout(struct ccw_device *cdev, enum dev_event dev_event) |
| 1172 | { |
| 1173 | int ret; |
| 1174 | |
| 1175 | ret = ccw_device_cancel_halt_clear(cdev); |
| 1176 | switch (ret) { |
| 1177 | case 0: |
| 1178 | cdev->private->state = DEV_STATE_OFFLINE; |
| 1179 | wake_up(&cdev->private->wait_q); |
| 1180 | break; |
| 1181 | case -ENODEV: |
| 1182 | cdev->private->state = DEV_STATE_NOT_OPER; |
| 1183 | wake_up(&cdev->private->wait_q); |
| 1184 | break; |
| 1185 | default: |
| 1186 | ccw_device_set_timeout(cdev, HZ/10); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * No operation action. This is used e.g. to ignore a timeout event in |
| 1192 | * state offline. |
| 1193 | */ |
| 1194 | static void |
| 1195 | ccw_device_nop(struct ccw_device *cdev, enum dev_event dev_event) |
| 1196 | { |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * Bug operation action. |
| 1201 | */ |
| 1202 | static void |
| 1203 | ccw_device_bug(struct ccw_device *cdev, enum dev_event dev_event) |
| 1204 | { |
| 1205 | printk(KERN_EMERG "dev_jumptable[%i][%i] == NULL\n", |
| 1206 | cdev->private->state, dev_event); |
| 1207 | BUG(); |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * device statemachine |
| 1212 | */ |
| 1213 | fsm_func_t *dev_jumptable[NR_DEV_STATES][NR_DEV_EVENTS] = { |
| 1214 | [DEV_STATE_NOT_OPER] = { |
| 1215 | [DEV_EVENT_NOTOPER] = ccw_device_nop, |
| 1216 | [DEV_EVENT_INTERRUPT] = ccw_device_bug, |
| 1217 | [DEV_EVENT_TIMEOUT] = ccw_device_nop, |
| 1218 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1219 | }, |
| 1220 | [DEV_STATE_SENSE_PGID] = { |
| 1221 | [DEV_EVENT_NOTOPER] = ccw_device_online_notoper, |
| 1222 | [DEV_EVENT_INTERRUPT] = ccw_device_sense_pgid_irq, |
| 1223 | [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout, |
| 1224 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1225 | }, |
| 1226 | [DEV_STATE_SENSE_ID] = { |
| 1227 | [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper, |
| 1228 | [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq, |
| 1229 | [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout, |
| 1230 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1231 | }, |
| 1232 | [DEV_STATE_OFFLINE] = { |
| 1233 | [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper, |
| 1234 | [DEV_EVENT_INTERRUPT] = ccw_device_offline_irq, |
| 1235 | [DEV_EVENT_TIMEOUT] = ccw_device_nop, |
| 1236 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1237 | }, |
| 1238 | [DEV_STATE_VERIFY] = { |
| 1239 | [DEV_EVENT_NOTOPER] = ccw_device_online_notoper, |
| 1240 | [DEV_EVENT_INTERRUPT] = ccw_device_verify_irq, |
| 1241 | [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout, |
| 1242 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1243 | }, |
| 1244 | [DEV_STATE_ONLINE] = { |
| 1245 | [DEV_EVENT_NOTOPER] = ccw_device_online_notoper, |
| 1246 | [DEV_EVENT_INTERRUPT] = ccw_device_irq, |
| 1247 | [DEV_EVENT_TIMEOUT] = ccw_device_online_timeout, |
| 1248 | [DEV_EVENT_VERIFY] = ccw_device_online_verify, |
| 1249 | }, |
| 1250 | [DEV_STATE_W4SENSE] = { |
| 1251 | [DEV_EVENT_NOTOPER] = ccw_device_online_notoper, |
| 1252 | [DEV_EVENT_INTERRUPT] = ccw_device_w4sense, |
| 1253 | [DEV_EVENT_TIMEOUT] = ccw_device_nop, |
| 1254 | [DEV_EVENT_VERIFY] = ccw_device_online_verify, |
| 1255 | }, |
| 1256 | [DEV_STATE_DISBAND_PGID] = { |
| 1257 | [DEV_EVENT_NOTOPER] = ccw_device_online_notoper, |
| 1258 | [DEV_EVENT_INTERRUPT] = ccw_device_disband_irq, |
| 1259 | [DEV_EVENT_TIMEOUT] = ccw_device_onoff_timeout, |
| 1260 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1261 | }, |
| 1262 | [DEV_STATE_BOXED] = { |
| 1263 | [DEV_EVENT_NOTOPER] = ccw_device_offline_notoper, |
| 1264 | [DEV_EVENT_INTERRUPT] = ccw_device_stlck_done, |
| 1265 | [DEV_EVENT_TIMEOUT] = ccw_device_stlck_done, |
| 1266 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1267 | }, |
| 1268 | /* states to wait for i/o completion before doing something */ |
| 1269 | [DEV_STATE_CLEAR_VERIFY] = { |
| 1270 | [DEV_EVENT_NOTOPER] = ccw_device_online_notoper, |
| 1271 | [DEV_EVENT_INTERRUPT] = ccw_device_clear_verify, |
| 1272 | [DEV_EVENT_TIMEOUT] = ccw_device_nop, |
| 1273 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1274 | }, |
| 1275 | [DEV_STATE_TIMEOUT_KILL] = { |
| 1276 | [DEV_EVENT_NOTOPER] = ccw_device_online_notoper, |
| 1277 | [DEV_EVENT_INTERRUPT] = ccw_device_killing_irq, |
| 1278 | [DEV_EVENT_TIMEOUT] = ccw_device_killing_timeout, |
| 1279 | [DEV_EVENT_VERIFY] = ccw_device_nop, //FIXME |
| 1280 | }, |
| 1281 | [DEV_STATE_WAIT4IO] = { |
| 1282 | [DEV_EVENT_NOTOPER] = ccw_device_online_notoper, |
| 1283 | [DEV_EVENT_INTERRUPT] = ccw_device_wait4io_irq, |
| 1284 | [DEV_EVENT_TIMEOUT] = ccw_device_wait4io_timeout, |
| 1285 | [DEV_EVENT_VERIFY] = ccw_device_wait4io_verify, |
| 1286 | }, |
| 1287 | [DEV_STATE_QUIESCE] = { |
| 1288 | [DEV_EVENT_NOTOPER] = ccw_device_quiesce_done, |
| 1289 | [DEV_EVENT_INTERRUPT] = ccw_device_quiesce_done, |
| 1290 | [DEV_EVENT_TIMEOUT] = ccw_device_quiesce_timeout, |
| 1291 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1292 | }, |
| 1293 | /* special states for devices gone not operational */ |
| 1294 | [DEV_STATE_DISCONNECTED] = { |
| 1295 | [DEV_EVENT_NOTOPER] = ccw_device_nop, |
| 1296 | [DEV_EVENT_INTERRUPT] = ccw_device_start_id, |
| 1297 | [DEV_EVENT_TIMEOUT] = ccw_device_bug, |
| 1298 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1299 | }, |
| 1300 | [DEV_STATE_DISCONNECTED_SENSE_ID] = { |
| 1301 | [DEV_EVENT_NOTOPER] = ccw_device_recog_notoper, |
| 1302 | [DEV_EVENT_INTERRUPT] = ccw_device_sense_id_irq, |
| 1303 | [DEV_EVENT_TIMEOUT] = ccw_device_recog_timeout, |
| 1304 | [DEV_EVENT_VERIFY] = ccw_device_nop, |
| 1305 | }, |
| 1306 | [DEV_STATE_CMFCHANGE] = { |
| 1307 | [DEV_EVENT_NOTOPER] = ccw_device_change_cmfstate, |
| 1308 | [DEV_EVENT_INTERRUPT] = ccw_device_change_cmfstate, |
| 1309 | [DEV_EVENT_TIMEOUT] = ccw_device_change_cmfstate, |
| 1310 | [DEV_EVENT_VERIFY] = ccw_device_change_cmfstate, |
| 1311 | }, |
Cornelia Huck | 94bb063 | 2006-06-29 15:08:41 +0200 | [diff] [blame] | 1312 | [DEV_STATE_CMFUPDATE] = { |
| 1313 | [DEV_EVENT_NOTOPER] = ccw_device_update_cmfblock, |
| 1314 | [DEV_EVENT_INTERRUPT] = ccw_device_update_cmfblock, |
| 1315 | [DEV_EVENT_TIMEOUT] = ccw_device_update_cmfblock, |
| 1316 | [DEV_EVENT_VERIFY] = ccw_device_update_cmfblock, |
| 1317 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | }; |
| 1319 | |
| 1320 | /* |
| 1321 | * io_subchannel_irq is called for "real" interrupts or for status |
| 1322 | * pending conditions on msch. |
| 1323 | */ |
| 1324 | void |
| 1325 | io_subchannel_irq (struct device *pdev) |
| 1326 | { |
| 1327 | struct ccw_device *cdev; |
| 1328 | |
| 1329 | cdev = to_subchannel(pdev)->dev.driver_data; |
| 1330 | |
| 1331 | CIO_TRACE_EVENT (3, "IRQ"); |
| 1332 | CIO_TRACE_EVENT (3, pdev->bus_id); |
| 1333 | if (cdev) |
| 1334 | dev_fsm_event(cdev, DEV_EVENT_INTERRUPT); |
| 1335 | } |
| 1336 | |
| 1337 | EXPORT_SYMBOL_GPL(ccw_device_set_timeout); |