1 /* 2 * drivers/s390/cio/css.c 3 * driver for channel subsystem 4 * $Revision: 1.85 $ 5 * 6 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, 7 * IBM Corporation 8 * Author(s): Arnd Bergmann (arndb@de.ibm.com) 9 * Cornelia Huck (cohuck@de.ibm.com) 10 */ 11 #include <linux/module.h> 12 #include <linux/init.h> 13 #include <linux/device.h> 14 #include <linux/slab.h> 15 #include <linux/errno.h> 16 #include <linux/list.h> 17 18 #include "css.h" 19 #include "cio.h" 20 #include "cio_debug.h" 21 #include "ioasm.h" 22 #include "chsc.h" 23 24 unsigned int highest_subchannel; 25 int need_rescan = 0; 26 int css_init_done = 0; 27 28 struct pgid global_pgid; 29 int css_characteristics_avail = 0; 30 31 struct device css_bus_device = { 32 .bus_id = "css0", 33 }; 34 35 static struct subchannel * 36 css_alloc_subchannel(int irq) 37 { 38 struct subchannel *sch; 39 int ret; 40 41 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA); 42 if (sch == NULL) 43 return ERR_PTR(-ENOMEM); 44 ret = cio_validate_subchannel (sch, irq); 45 if (ret < 0) { 46 kfree(sch); 47 return ERR_PTR(ret); 48 } 49 if (irq > highest_subchannel) 50 highest_subchannel = irq; 51 52 if (sch->st != SUBCHANNEL_TYPE_IO) { 53 /* For now we ignore all non-io subchannels. */ 54 kfree(sch); 55 return ERR_PTR(-EINVAL); 56 } 57 58 /* 59 * Set intparm to subchannel address. 60 * This is fine even on 64bit since the subchannel is always located 61 * under 2G. 62 */ 63 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch; 64 ret = cio_modify(sch); 65 if (ret) { 66 kfree(sch); 67 return ERR_PTR(ret); 68 } 69 return sch; 70 } 71 72 static void 73 css_free_subchannel(struct subchannel *sch) 74 { 75 if (sch) { 76 /* Reset intparm to zeroes. */ 77 sch->schib.pmcw.intparm = 0; 78 cio_modify(sch); 79 kfree(sch); 80 } 81 82 } 83 84 static void 85 css_subchannel_release(struct device *dev) 86 { 87 struct subchannel *sch; 88 89 sch = to_subchannel(dev); 90 if (!cio_is_console(sch->irq)) 91 kfree(sch); 92 } 93 94 extern int css_get_ssd_info(struct subchannel *sch); 95 96 static int 97 css_register_subchannel(struct subchannel *sch) 98 { 99 int ret; 100 101 /* Initialize the subchannel structure */ 102 sch->dev.parent = &css_bus_device; 103 sch->dev.bus = &css_bus_type; 104 sch->dev.release = &css_subchannel_release; 105 106 /* make it known to the system */ 107 ret = device_register(&sch->dev); 108 if (ret) 109 printk (KERN_WARNING "%s: could not register %s\n", 110 __func__, sch->dev.bus_id); 111 else 112 css_get_ssd_info(sch); 113 return ret; 114 } 115 116 int 117 css_probe_device(int irq) 118 { 119 int ret; 120 struct subchannel *sch; 121 122 sch = css_alloc_subchannel(irq); 123 if (IS_ERR(sch)) 124 return PTR_ERR(sch); 125 ret = css_register_subchannel(sch); 126 if (ret) 127 css_free_subchannel(sch); 128 return ret; 129 } 130 131 static int 132 check_subchannel(struct device * dev, void * data) 133 { 134 struct subchannel *sch; 135 int irq = (unsigned long)data; 136 137 sch = to_subchannel(dev); 138 return (sch->irq == irq); 139 } 140 141 struct subchannel * 142 get_subchannel_by_schid(int irq) 143 { 144 struct device *dev; 145 146 dev = bus_find_device(&css_bus_type, NULL, 147 (void *)(unsigned long)irq, check_subchannel); 148 149 return dev ? to_subchannel(dev) : NULL; 150 } 151 152 153 static inline int 154 css_get_subchannel_status(struct subchannel *sch, int schid) 155 { 156 struct schib schib; 157 int cc; 158 159 cc = stsch(schid, &schib); 160 if (cc) 161 return CIO_GONE; 162 if (!schib.pmcw.dnv) 163 return CIO_GONE; 164 if (sch && sch->schib.pmcw.dnv && 165 (schib.pmcw.dev != sch->schib.pmcw.dev)) 166 return CIO_REVALIDATE; 167 if (sch && !sch->lpm) 168 return CIO_NO_PATH; 169 return CIO_OPER; 170 } 171 172 static int 173 css_evaluate_subchannel(int irq, int slow) 174 { 175 int event, ret, disc; 176 struct subchannel *sch; 177 unsigned long flags; 178 179 sch = get_subchannel_by_schid(irq); 180 disc = sch ? device_is_disconnected(sch) : 0; 181 if (disc && slow) { 182 if (sch) 183 put_device(&sch->dev); 184 return 0; /* Already processed. */ 185 } 186 /* 187 * We've got a machine check, so running I/O won't get an interrupt. 188 * Kill any pending timers. 189 */ 190 if (sch) 191 device_kill_pending_timer(sch); 192 if (!disc && !slow) { 193 if (sch) 194 put_device(&sch->dev); 195 return -EAGAIN; /* Will be done on the slow path. */ 196 } 197 event = css_get_subchannel_status(sch, irq); 198 CIO_MSG_EVENT(4, "Evaluating schid %04x, event %d, %s, %s path.\n", 199 irq, event, sch?(disc?"disconnected":"normal"):"unknown", 200 slow?"slow":"fast"); 201 switch (event) { 202 case CIO_NO_PATH: 203 case CIO_GONE: 204 if (!sch) { 205 /* Never used this subchannel. Ignore. */ 206 ret = 0; 207 break; 208 } 209 if (disc && (event == CIO_NO_PATH)) { 210 /* 211 * Uargh, hack again. Because we don't get a machine 212 * check on configure on, our path bookkeeping can 213 * be out of date here (it's fine while we only do 214 * logical varying or get chsc machine checks). We 215 * need to force reprobing or we might miss devices 216 * coming operational again. It won't do harm in real 217 * no path situations. 218 */ 219 spin_lock_irqsave(&sch->lock, flags); 220 device_trigger_reprobe(sch); 221 spin_unlock_irqrestore(&sch->lock, flags); 222 ret = 0; 223 break; 224 } 225 if (sch->driver && sch->driver->notify && 226 sch->driver->notify(&sch->dev, event)) { 227 cio_disable_subchannel(sch); 228 device_set_disconnected(sch); 229 ret = 0; 230 break; 231 } 232 /* 233 * Unregister subchannel. 234 * The device will be killed automatically. 235 */ 236 cio_disable_subchannel(sch); 237 device_unregister(&sch->dev); 238 /* Reset intparm to zeroes. */ 239 sch->schib.pmcw.intparm = 0; 240 cio_modify(sch); 241 put_device(&sch->dev); 242 ret = 0; 243 break; 244 case CIO_REVALIDATE: 245 /* 246 * Revalidation machine check. Sick. 247 * We don't notify the driver since we have to throw the device 248 * away in any case. 249 */ 250 if (!disc) { 251 device_unregister(&sch->dev); 252 /* Reset intparm to zeroes. */ 253 sch->schib.pmcw.intparm = 0; 254 cio_modify(sch); 255 put_device(&sch->dev); 256 ret = css_probe_device(irq); 257 } else { 258 /* 259 * We can't immediately deregister the disconnected 260 * device since it might block. 261 */ 262 spin_lock_irqsave(&sch->lock, flags); 263 device_trigger_reprobe(sch); 264 spin_unlock_irqrestore(&sch->lock, flags); 265 ret = 0; 266 } 267 break; 268 case CIO_OPER: 269 if (disc) { 270 spin_lock_irqsave(&sch->lock, flags); 271 /* Get device operational again. */ 272 device_trigger_reprobe(sch); 273 spin_unlock_irqrestore(&sch->lock, flags); 274 } 275 ret = sch ? 0 : css_probe_device(irq); 276 break; 277 default: 278 BUG(); 279 ret = 0; 280 } 281 return ret; 282 } 283 284 static void 285 css_rescan_devices(void) 286 { 287 int irq, ret; 288 289 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) { 290 ret = css_evaluate_subchannel(irq, 1); 291 /* No more memory. It doesn't make sense to continue. No 292 * panic because this can happen in midflight and just 293 * because we can't use a new device is no reason to crash 294 * the system. */ 295 if (ret == -ENOMEM) 296 break; 297 /* -ENXIO indicates that there are no more subchannels. */ 298 if (ret == -ENXIO) 299 break; 300 } 301 } 302 303 struct slow_subchannel { 304 struct list_head slow_list; 305 unsigned long schid; 306 }; 307 308 static LIST_HEAD(slow_subchannels_head); 309 static DEFINE_SPINLOCK(slow_subchannel_lock); 310 311 static void 312 css_trigger_slow_path(void) 313 { 314 CIO_TRACE_EVENT(4, "slowpath"); 315 316 if (need_rescan) { 317 need_rescan = 0; 318 css_rescan_devices(); 319 return; 320 } 321 322 spin_lock_irq(&slow_subchannel_lock); 323 while (!list_empty(&slow_subchannels_head)) { 324 struct slow_subchannel *slow_sch = 325 list_entry(slow_subchannels_head.next, 326 struct slow_subchannel, slow_list); 327 328 list_del_init(slow_subchannels_head.next); 329 spin_unlock_irq(&slow_subchannel_lock); 330 css_evaluate_subchannel(slow_sch->schid, 1); 331 spin_lock_irq(&slow_subchannel_lock); 332 kfree(slow_sch); 333 } 334 spin_unlock_irq(&slow_subchannel_lock); 335 } 336 337 typedef void (*workfunc)(void *); 338 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL); 339 struct workqueue_struct *slow_path_wq; 340 341 /* 342 * Rescan for new devices. FIXME: This is slow. 343 * This function is called when we have lost CRWs due to overflows and we have 344 * to do subchannel housekeeping. 345 */ 346 void 347 css_reiterate_subchannels(void) 348 { 349 css_clear_subchannel_slow_list(); 350 need_rescan = 1; 351 } 352 353 /* 354 * Called from the machine check handler for subchannel report words. 355 */ 356 int 357 css_process_crw(int irq) 358 { 359 int ret; 360 361 CIO_CRW_EVENT(2, "source is subchannel %04X\n", irq); 362 363 if (need_rescan) 364 /* We need to iterate all subchannels anyway. */ 365 return -EAGAIN; 366 /* 367 * Since we are always presented with IPI in the CRW, we have to 368 * use stsch() to find out if the subchannel in question has come 369 * or gone. 370 */ 371 ret = css_evaluate_subchannel(irq, 0); 372 if (ret == -EAGAIN) { 373 if (css_enqueue_subchannel_slow(irq)) { 374 css_clear_subchannel_slow_list(); 375 need_rescan = 1; 376 } 377 } 378 return ret; 379 } 380 381 static void __init 382 css_generate_pgid(void) 383 { 384 /* Let's build our path group ID here. */ 385 if (css_characteristics_avail && css_general_characteristics.mcss) 386 global_pgid.cpu_addr = 0x8000; 387 else { 388 #ifdef CONFIG_SMP 389 global_pgid.cpu_addr = hard_smp_processor_id(); 390 #else 391 global_pgid.cpu_addr = 0; 392 #endif 393 } 394 global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident; 395 global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine; 396 global_pgid.tod_high = (__u32) (get_clock() >> 32); 397 } 398 399 /* 400 * Now that the driver core is running, we can setup our channel subsystem. 401 * The struct subchannel's are created during probing (except for the 402 * static console subchannel). 403 */ 404 static int __init 405 init_channel_subsystem (void) 406 { 407 int ret, irq; 408 409 if (chsc_determine_css_characteristics() == 0) 410 css_characteristics_avail = 1; 411 412 css_generate_pgid(); 413 414 if ((ret = bus_register(&css_bus_type))) 415 goto out; 416 if ((ret = device_register (&css_bus_device))) 417 goto out_bus; 418 419 css_init_done = 1; 420 421 ctl_set_bit(6, 28); 422 423 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) { 424 struct subchannel *sch; 425 426 if (cio_is_console(irq)) 427 sch = cio_get_console_subchannel(); 428 else { 429 sch = css_alloc_subchannel(irq); 430 if (IS_ERR(sch)) 431 ret = PTR_ERR(sch); 432 else 433 ret = 0; 434 if (ret == -ENOMEM) 435 panic("Out of memory in " 436 "init_channel_subsystem\n"); 437 /* -ENXIO: no more subchannels. */ 438 if (ret == -ENXIO) 439 break; 440 if (ret) 441 continue; 442 } 443 /* 444 * We register ALL valid subchannels in ioinfo, even those 445 * that have been present before init_channel_subsystem. 446 * These subchannels can't have been registered yet (kmalloc 447 * not working) so we do it now. This is true e.g. for the 448 * console subchannel. 449 */ 450 css_register_subchannel(sch); 451 } 452 return 0; 453 454 out_bus: 455 bus_unregister(&css_bus_type); 456 out: 457 return ret; 458 } 459 460 /* 461 * find a driver for a subchannel. They identify by the subchannel 462 * type with the exception that the console subchannel driver has its own 463 * subchannel type although the device is an i/o subchannel 464 */ 465 static int 466 css_bus_match (struct device *dev, struct device_driver *drv) 467 { 468 struct subchannel *sch = container_of (dev, struct subchannel, dev); 469 struct css_driver *driver = container_of (drv, struct css_driver, drv); 470 471 if (sch->st == driver->subchannel_type) 472 return 1; 473 474 return 0; 475 } 476 477 struct bus_type css_bus_type = { 478 .name = "css", 479 .match = &css_bus_match, 480 }; 481 482 subsys_initcall(init_channel_subsystem); 483 484 /* 485 * Register root devices for some drivers. The release function must not be 486 * in the device drivers, so we do it here. 487 */ 488 static void 489 s390_root_dev_release(struct device *dev) 490 { 491 kfree(dev); 492 } 493 494 struct device * 495 s390_root_dev_register(const char *name) 496 { 497 struct device *dev; 498 int ret; 499 500 if (!strlen(name)) 501 return ERR_PTR(-EINVAL); 502 dev = kmalloc(sizeof(struct device), GFP_KERNEL); 503 if (!dev) 504 return ERR_PTR(-ENOMEM); 505 memset(dev, 0, sizeof(struct device)); 506 strncpy(dev->bus_id, name, min(strlen(name), (size_t)BUS_ID_SIZE)); 507 dev->release = s390_root_dev_release; 508 ret = device_register(dev); 509 if (ret) { 510 kfree(dev); 511 return ERR_PTR(ret); 512 } 513 return dev; 514 } 515 516 void 517 s390_root_dev_unregister(struct device *dev) 518 { 519 if (dev) 520 device_unregister(dev); 521 } 522 523 int 524 css_enqueue_subchannel_slow(unsigned long schid) 525 { 526 struct slow_subchannel *new_slow_sch; 527 unsigned long flags; 528 529 new_slow_sch = kmalloc(sizeof(struct slow_subchannel), GFP_ATOMIC); 530 if (!new_slow_sch) 531 return -ENOMEM; 532 memset(new_slow_sch, 0, sizeof(struct slow_subchannel)); 533 new_slow_sch->schid = schid; 534 spin_lock_irqsave(&slow_subchannel_lock, flags); 535 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head); 536 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 537 return 0; 538 } 539 540 void 541 css_clear_subchannel_slow_list(void) 542 { 543 unsigned long flags; 544 545 spin_lock_irqsave(&slow_subchannel_lock, flags); 546 while (!list_empty(&slow_subchannels_head)) { 547 struct slow_subchannel *slow_sch = 548 list_entry(slow_subchannels_head.next, 549 struct slow_subchannel, slow_list); 550 551 list_del_init(slow_subchannels_head.next); 552 kfree(slow_sch); 553 } 554 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 555 } 556 557 558 559 int 560 css_slow_subchannels_exist(void) 561 { 562 return (!list_empty(&slow_subchannels_head)); 563 } 564 565 MODULE_LICENSE("GPL"); 566 EXPORT_SYMBOL(css_bus_type); 567 EXPORT_SYMBOL(s390_root_dev_register); 568 EXPORT_SYMBOL(s390_root_dev_unregister); 569 EXPORT_SYMBOL_GPL(css_characteristics_avail); 570