Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/s390/char/tape_core.c |
| 3 | * basic function of the tape device driver |
| 4 | * |
| 5 | * S390 and zSeries version |
Frank Munzert | 3ef32e62 | 2009-06-16 10:30:39 +0200 | [diff] [blame] | 6 | * Copyright IBM Corp. 2001, 2009 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Author(s): Carsten Otte <cotte@de.ibm.com> |
| 8 | * Michael Holzheu <holzheu@de.ibm.com> |
| 9 | * Tuan Ngo-Anh <ngoanh@de.ibm.com> |
| 10 | * Martin Schwidefsky <schwidefsky@de.ibm.com> |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 11 | * Stefan Bader <shbader@de.ibm.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
Carsten Otte | ab640db | 2009-03-26 15:24:38 +0100 | [diff] [blame] | 14 | #define KMSG_COMPONENT "tape" |
Michael Holzheu | bb50991 | 2009-12-18 17:43:21 +0100 | [diff] [blame] | 15 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> // for kernel parameters |
| 19 | #include <linux/kmod.h> // for requesting modules |
| 20 | #include <linux/spinlock.h> // for locks |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/list.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <asm/types.h> // for variable types |
| 26 | |
| 27 | #define TAPE_DBF_AREA tape_core_dbf |
| 28 | |
| 29 | #include "tape.h" |
| 30 | #include "tape_std.h" |
| 31 | |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 32 | #define LONG_BUSY_TIMEOUT 180 /* seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *); |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 35 | static void tape_delayed_next_request(struct work_struct *); |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 36 | static void tape_long_busy_timeout(unsigned long data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * One list to contain all tape devices of all disciplines, so |
| 40 | * we can assign the devices to minor numbers of the same major |
| 41 | * The list is protected by the rwlock |
| 42 | */ |
Denis Cheng | c11ca97 | 2008-01-26 14:11:13 +0100 | [diff] [blame] | 43 | static LIST_HEAD(tape_device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static DEFINE_RWLOCK(tape_device_lock); |
| 45 | |
| 46 | /* |
| 47 | * Pointer to debug area. |
| 48 | */ |
| 49 | debug_info_t *TAPE_DBF_AREA = NULL; |
| 50 | EXPORT_SYMBOL(TAPE_DBF_AREA); |
| 51 | |
| 52 | /* |
| 53 | * Printable strings for tape enumerations. |
| 54 | */ |
| 55 | const char *tape_state_verbose[TS_SIZE] = |
| 56 | { |
| 57 | [TS_UNUSED] = "UNUSED", |
| 58 | [TS_IN_USE] = "IN_USE", |
| 59 | [TS_BLKUSE] = "BLKUSE", |
| 60 | [TS_INIT] = "INIT ", |
| 61 | [TS_NOT_OPER] = "NOT_OP" |
| 62 | }; |
| 63 | |
| 64 | const char *tape_op_verbose[TO_SIZE] = |
| 65 | { |
| 66 | [TO_BLOCK] = "BLK", [TO_BSB] = "BSB", |
| 67 | [TO_BSF] = "BSF", [TO_DSE] = "DSE", |
| 68 | [TO_FSB] = "FSB", [TO_FSF] = "FSF", |
| 69 | [TO_LBL] = "LBL", [TO_NOP] = "NOP", |
| 70 | [TO_RBA] = "RBA", [TO_RBI] = "RBI", |
| 71 | [TO_RFO] = "RFO", [TO_REW] = "REW", |
| 72 | [TO_RUN] = "RUN", [TO_WRI] = "WRI", |
| 73 | [TO_WTM] = "WTM", [TO_MSEN] = "MSN", |
| 74 | [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF", |
| 75 | [TO_READ_ATTMSG] = "RAT", |
| 76 | [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS", |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 77 | [TO_UNASSIGN] = "UAS", [TO_CRYPT_ON] = "CON", |
| 78 | [TO_CRYPT_OFF] = "COF", [TO_KEKL_SET] = "KLS", |
Michael Holzheu | e296306 | 2007-05-04 18:47:53 +0200 | [diff] [blame] | 79 | [TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Cornelia Huck | f455adc | 2008-05-15 16:52:37 +0200 | [diff] [blame] | 82 | static int devid_to_int(struct ccw_dev_id *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
Cornelia Huck | f455adc | 2008-05-15 16:52:37 +0200 | [diff] [blame] | 84 | return dev_id->devno + (dev_id->ssid << 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Some channel attached tape specific attributes. |
| 89 | * |
| 90 | * FIXME: In the future the first_minor and blocksize attribute should be |
| 91 | * replaced by a link to the cdev tree. |
| 92 | */ |
| 93 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 94 | tape_medium_state_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
| 96 | struct tape_device *tdev; |
| 97 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 98 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state); |
| 100 | } |
| 101 | |
| 102 | static |
| 103 | DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL); |
| 104 | |
| 105 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 106 | tape_first_minor_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
| 108 | struct tape_device *tdev; |
| 109 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 110 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor); |
| 112 | } |
| 113 | |
| 114 | static |
| 115 | DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL); |
| 116 | |
| 117 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 118 | tape_state_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
| 120 | struct tape_device *tdev; |
| 121 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 122 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ? |
| 124 | "OFFLINE" : tape_state_verbose[tdev->tape_state]); |
| 125 | } |
| 126 | |
| 127 | static |
| 128 | DEVICE_ATTR(state, 0444, tape_state_show, NULL); |
| 129 | |
| 130 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 131 | tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | struct tape_device *tdev; |
| 134 | ssize_t rc; |
| 135 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 136 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | if (tdev->first_minor < 0) |
| 138 | return scnprintf(buf, PAGE_SIZE, "N/A\n"); |
| 139 | |
| 140 | spin_lock_irq(get_ccwdev_lock(tdev->cdev)); |
| 141 | if (list_empty(&tdev->req_queue)) |
| 142 | rc = scnprintf(buf, PAGE_SIZE, "---\n"); |
| 143 | else { |
| 144 | struct tape_request *req; |
| 145 | |
| 146 | req = list_entry(tdev->req_queue.next, struct tape_request, |
| 147 | list); |
| 148 | rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]); |
| 149 | } |
| 150 | spin_unlock_irq(get_ccwdev_lock(tdev->cdev)); |
| 151 | return rc; |
| 152 | } |
| 153 | |
| 154 | static |
| 155 | DEVICE_ATTR(operation, 0444, tape_operation_show, NULL); |
| 156 | |
| 157 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 158 | tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | struct tape_device *tdev; |
| 161 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 162 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size); |
| 165 | } |
| 166 | |
| 167 | static |
| 168 | DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL); |
| 169 | |
| 170 | static struct attribute *tape_attrs[] = { |
| 171 | &dev_attr_medium_state.attr, |
| 172 | &dev_attr_first_minor.attr, |
| 173 | &dev_attr_state.attr, |
| 174 | &dev_attr_operation.attr, |
| 175 | &dev_attr_blocksize.attr, |
| 176 | NULL |
| 177 | }; |
| 178 | |
| 179 | static struct attribute_group tape_attr_group = { |
| 180 | .attrs = tape_attrs, |
| 181 | }; |
| 182 | |
| 183 | /* |
| 184 | * Tape state functions |
| 185 | */ |
| 186 | void |
| 187 | tape_state_set(struct tape_device *device, enum tape_state newstate) |
| 188 | { |
| 189 | const char *str; |
| 190 | |
| 191 | if (device->tape_state == TS_NOT_OPER) { |
| 192 | DBF_EVENT(3, "ts_set err: not oper\n"); |
| 193 | return; |
| 194 | } |
| 195 | DBF_EVENT(4, "ts. dev: %x\n", device->first_minor); |
Peter Oberparleiter | f976069 | 2006-04-10 22:53:49 -0700 | [diff] [blame] | 196 | DBF_EVENT(4, "old ts:\t\n"); |
| 197 | if (device->tape_state < TS_SIZE && device->tape_state >=0 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | str = tape_state_verbose[device->tape_state]; |
| 199 | else |
| 200 | str = "UNKNOWN TS"; |
| 201 | DBF_EVENT(4, "%s\n", str); |
| 202 | DBF_EVENT(4, "new ts:\t\n"); |
Peter Oberparleiter | f976069 | 2006-04-10 22:53:49 -0700 | [diff] [blame] | 203 | if (newstate < TS_SIZE && newstate >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | str = tape_state_verbose[newstate]; |
| 205 | else |
| 206 | str = "UNKNOWN TS"; |
| 207 | DBF_EVENT(4, "%s\n", str); |
| 208 | device->tape_state = newstate; |
| 209 | wake_up(&device->state_change_wq); |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate) |
| 214 | { |
| 215 | if (device->medium_state == newstate) |
| 216 | return; |
| 217 | switch(newstate){ |
| 218 | case MS_UNLOADED: |
| 219 | device->tape_generic_status |= GMT_DR_OPEN(~0); |
Michael Holzheu | 53f8c57 | 2009-09-11 10:29:02 +0200 | [diff] [blame] | 220 | if (device->medium_state == MS_LOADED) |
Michael Holzheu | 59e3692 | 2009-09-11 10:29:07 +0200 | [diff] [blame] | 221 | pr_info("%s: The tape cartridge has been successfully " |
| 222 | "unloaded\n", dev_name(&device->cdev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | break; |
| 224 | case MS_LOADED: |
| 225 | device->tape_generic_status &= ~GMT_DR_OPEN(~0); |
Michael Holzheu | 53f8c57 | 2009-09-11 10:29:02 +0200 | [diff] [blame] | 226 | if (device->medium_state == MS_UNLOADED) |
Michael Holzheu | 59e3692 | 2009-09-11 10:29:07 +0200 | [diff] [blame] | 227 | pr_info("%s: A tape cartridge has been mounted\n", |
| 228 | dev_name(&device->cdev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | break; |
| 230 | default: |
| 231 | // print nothing |
| 232 | break; |
| 233 | } |
| 234 | device->medium_state = newstate; |
| 235 | wake_up(&device->state_change_wq); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Stop running ccw. Has to be called with the device lock held. |
| 240 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 241 | static int |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 242 | __tape_cancel_io(struct tape_device *device, struct tape_request *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
| 244 | int retries; |
| 245 | int rc; |
| 246 | |
| 247 | /* Check if interrupt has already been processed */ |
| 248 | if (request->callback == NULL) |
| 249 | return 0; |
| 250 | |
| 251 | rc = 0; |
| 252 | for (retries = 0; retries < 5; retries++) { |
| 253 | rc = ccw_device_clear(device->cdev, (long) request); |
| 254 | |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 255 | switch (rc) { |
| 256 | case 0: |
| 257 | request->status = TAPE_REQUEST_DONE; |
| 258 | return 0; |
| 259 | case -EBUSY: |
| 260 | request->status = TAPE_REQUEST_CANCEL; |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 261 | schedule_delayed_work(&device->tape_dnr, 0); |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 262 | return 0; |
| 263 | case -ENODEV: |
| 264 | DBF_EXCEPTION(2, "device gone, retry\n"); |
| 265 | break; |
| 266 | case -EIO: |
| 267 | DBF_EXCEPTION(2, "I/O error, retry\n"); |
| 268 | break; |
| 269 | default: |
| 270 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return rc; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Add device into the sorted list, giving it the first |
| 279 | * available minor number. |
| 280 | */ |
| 281 | static int |
| 282 | tape_assign_minor(struct tape_device *device) |
| 283 | { |
| 284 | struct tape_device *tmp; |
| 285 | int minor; |
| 286 | |
| 287 | minor = 0; |
| 288 | write_lock(&tape_device_lock); |
| 289 | list_for_each_entry(tmp, &tape_device_list, node) { |
| 290 | if (minor < tmp->first_minor) |
| 291 | break; |
| 292 | minor += TAPE_MINORS_PER_DEV; |
| 293 | } |
| 294 | if (minor >= 256) { |
| 295 | write_unlock(&tape_device_lock); |
| 296 | return -ENODEV; |
| 297 | } |
| 298 | device->first_minor = minor; |
| 299 | list_add_tail(&device->node, &tmp->node); |
| 300 | write_unlock(&tape_device_lock); |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /* remove device from the list */ |
| 305 | static void |
| 306 | tape_remove_minor(struct tape_device *device) |
| 307 | { |
| 308 | write_lock(&tape_device_lock); |
| 309 | list_del_init(&device->node); |
| 310 | device->first_minor = -1; |
| 311 | write_unlock(&tape_device_lock); |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Set a device online. |
| 316 | * |
| 317 | * This function is called by the common I/O layer to move a device from the |
| 318 | * detected but offline into the online state. |
| 319 | * If we return an error (RC < 0) the device remains in the offline state. This |
| 320 | * can happen if the device is assigned somewhere else, for example. |
| 321 | */ |
| 322 | int |
| 323 | tape_generic_online(struct tape_device *device, |
| 324 | struct tape_discipline *discipline) |
| 325 | { |
| 326 | int rc; |
| 327 | |
| 328 | DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline); |
| 329 | |
| 330 | if (device->tape_state != TS_INIT) { |
| 331 | DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state); |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 335 | init_timer(&device->lb_timeout); |
| 336 | device->lb_timeout.function = tape_long_busy_timeout; |
| 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | /* Let the discipline have a go at the device. */ |
| 339 | device->discipline = discipline; |
| 340 | if (!try_module_get(discipline->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return -EINVAL; |
| 342 | } |
| 343 | |
| 344 | rc = discipline->setup_device(device); |
| 345 | if (rc) |
| 346 | goto out; |
| 347 | rc = tape_assign_minor(device); |
| 348 | if (rc) |
| 349 | goto out_discipline; |
| 350 | |
| 351 | rc = tapechar_setup_device(device); |
| 352 | if (rc) |
| 353 | goto out_minor; |
| 354 | rc = tapeblock_setup_device(device); |
| 355 | if (rc) |
| 356 | goto out_char; |
| 357 | |
| 358 | tape_state_set(device, TS_UNUSED); |
| 359 | |
| 360 | DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id); |
| 361 | |
| 362 | return 0; |
| 363 | |
| 364 | out_char: |
| 365 | tapechar_cleanup_device(device); |
Roel Kluin | 68d36bd | 2009-09-11 10:28:55 +0200 | [diff] [blame] | 366 | out_minor: |
| 367 | tape_remove_minor(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | out_discipline: |
| 369 | device->discipline->cleanup_device(device); |
| 370 | device->discipline = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | out: |
| 372 | module_put(discipline->owner); |
| 373 | return rc; |
| 374 | } |
| 375 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 376 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | tape_cleanup_device(struct tape_device *device) |
| 378 | { |
| 379 | tapeblock_cleanup_device(device); |
| 380 | tapechar_cleanup_device(device); |
| 381 | device->discipline->cleanup_device(device); |
| 382 | module_put(device->discipline->owner); |
| 383 | tape_remove_minor(device); |
| 384 | tape_med_state_set(device, MS_UNKNOWN); |
| 385 | } |
| 386 | |
| 387 | /* |
Frank Munzert | 3ef32e62 | 2009-06-16 10:30:39 +0200 | [diff] [blame] | 388 | * Suspend device. |
| 389 | * |
| 390 | * Called by the common I/O layer if the drive should be suspended on user |
| 391 | * request. We refuse to suspend if the device is loaded or in use for the |
| 392 | * following reason: |
| 393 | * While the Linux guest is suspended, it might be logged off which causes |
| 394 | * devices to be detached. Tape devices are automatically rewound and unloaded |
| 395 | * during DETACH processing (unless the tape device was attached with the |
| 396 | * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to |
| 397 | * resume the original state of the tape device, since we would need to |
| 398 | * manually re-load the cartridge which was active at suspend time. |
| 399 | */ |
| 400 | int tape_generic_pm_suspend(struct ccw_device *cdev) |
| 401 | { |
| 402 | struct tape_device *device; |
| 403 | |
Martin Schwidefsky | 4f0076f | 2009-06-22 12:08:19 +0200 | [diff] [blame] | 404 | device = dev_get_drvdata(&cdev->dev); |
Frank Munzert | 3ef32e62 | 2009-06-16 10:30:39 +0200 | [diff] [blame] | 405 | if (!device) { |
| 406 | return -ENODEV; |
| 407 | } |
| 408 | |
| 409 | DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n", |
| 410 | device->cdev_id, device); |
| 411 | |
| 412 | if (device->medium_state != MS_UNLOADED) { |
| 413 | pr_err("A cartridge is loaded in tape device %s, " |
| 414 | "refusing to suspend\n", dev_name(&cdev->dev)); |
| 415 | return -EBUSY; |
| 416 | } |
| 417 | |
| 418 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 419 | switch (device->tape_state) { |
| 420 | case TS_INIT: |
| 421 | case TS_NOT_OPER: |
| 422 | case TS_UNUSED: |
| 423 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 424 | break; |
| 425 | default: |
| 426 | pr_err("Tape device %s is busy, refusing to " |
| 427 | "suspend\n", dev_name(&cdev->dev)); |
| 428 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 429 | return -EBUSY; |
| 430 | } |
| 431 | |
| 432 | DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | * Set device offline. |
| 438 | * |
| 439 | * Called by the common I/O layer if the drive should set offline on user |
| 440 | * request. We may prevent this by returning an error. |
| 441 | * Manual offline is only allowed while the drive is not in use. |
| 442 | */ |
| 443 | int |
Frank Munzert | 4d7a3cd | 2009-04-23 13:58:09 +0200 | [diff] [blame] | 444 | tape_generic_offline(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
Frank Munzert | 4d7a3cd | 2009-04-23 13:58:09 +0200 | [diff] [blame] | 446 | struct tape_device *device; |
| 447 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 448 | device = dev_get_drvdata(&cdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | if (!device) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | return -ENODEV; |
| 451 | } |
| 452 | |
| 453 | DBF_LH(3, "(%08x): tape_generic_offline(%p)\n", |
| 454 | device->cdev_id, device); |
| 455 | |
| 456 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 457 | switch (device->tape_state) { |
| 458 | case TS_INIT: |
| 459 | case TS_NOT_OPER: |
| 460 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 461 | break; |
| 462 | case TS_UNUSED: |
| 463 | tape_state_set(device, TS_INIT); |
| 464 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 465 | tape_cleanup_device(device); |
| 466 | break; |
| 467 | default: |
| 468 | DBF_EVENT(3, "(%08x): Set offline failed " |
| 469 | "- drive in use.\n", |
| 470 | device->cdev_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 472 | return -EBUSY; |
| 473 | } |
| 474 | |
| 475 | DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id); |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * Allocate memory for a new device structure. |
| 481 | */ |
| 482 | static struct tape_device * |
| 483 | tape_alloc_device(void) |
| 484 | { |
| 485 | struct tape_device *device; |
| 486 | |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 487 | device = kzalloc(sizeof(struct tape_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | if (device == NULL) { |
| 489 | DBF_EXCEPTION(2, "ti:no mem\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return ERR_PTR(-ENOMEM); |
| 491 | } |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 492 | device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | if (device->modeset_byte == NULL) { |
| 494 | DBF_EXCEPTION(2, "ti:no mem\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | kfree(device); |
| 496 | return ERR_PTR(-ENOMEM); |
| 497 | } |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 498 | mutex_init(&device->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | INIT_LIST_HEAD(&device->req_queue); |
| 500 | INIT_LIST_HEAD(&device->node); |
| 501 | init_waitqueue_head(&device->state_change_wq); |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 502 | init_waitqueue_head(&device->wait_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | device->tape_state = TS_INIT; |
| 504 | device->medium_state = MS_UNKNOWN; |
| 505 | *device->modeset_byte = 0; |
| 506 | device->first_minor = -1; |
| 507 | atomic_set(&device->ref_count, 1); |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 508 | INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
| 510 | return device; |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Get a reference to an existing device structure. This will automatically |
| 515 | * increment the reference count. |
| 516 | */ |
| 517 | struct tape_device * |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 518 | tape_get_device(struct tape_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | { |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 520 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 522 | count = atomic_inc_return(&device->ref_count); |
| 523 | DBF_EVENT(4, "tape_get_device(%p) = %i\n", device, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | return device; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Decrease the reference counter of a devices structure. If the |
| 529 | * reference counter reaches zero free the device structure. |
| 530 | * The function returns a NULL pointer to be used by the caller |
| 531 | * for clearing reference pointers. |
| 532 | */ |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 533 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | tape_put_device(struct tape_device *device) |
| 535 | { |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 536 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 538 | count = atomic_dec_return(&device->ref_count); |
| 539 | DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, count); |
| 540 | BUG_ON(count < 0); |
| 541 | if (count == 0) { |
| 542 | kfree(device->modeset_byte); |
| 543 | kfree(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Find tape device by a device index. |
| 549 | */ |
| 550 | struct tape_device * |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 551 | tape_find_device(int devindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | struct tape_device *device, *tmp; |
| 554 | |
| 555 | device = ERR_PTR(-ENODEV); |
| 556 | read_lock(&tape_device_lock); |
| 557 | list_for_each_entry(tmp, &tape_device_list, node) { |
| 558 | if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) { |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 559 | device = tape_get_device(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | } |
| 563 | read_unlock(&tape_device_lock); |
| 564 | return device; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * Driverfs tape probe function. |
| 569 | */ |
| 570 | int |
| 571 | tape_generic_probe(struct ccw_device *cdev) |
| 572 | { |
| 573 | struct tape_device *device; |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 574 | int ret; |
Cornelia Huck | f455adc | 2008-05-15 16:52:37 +0200 | [diff] [blame] | 575 | struct ccw_dev_id dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | device = tape_alloc_device(); |
| 578 | if (IS_ERR(device)) |
| 579 | return -ENODEV; |
Peter Oberparleiter | 454e1fa | 2009-12-07 12:51:30 +0100 | [diff] [blame] | 580 | ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP | |
| 581 | CCWDEV_DO_MULTIPATH); |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 582 | ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group); |
| 583 | if (ret) { |
| 584 | tape_put_device(device); |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 585 | return ret; |
| 586 | } |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 587 | dev_set_drvdata(&cdev->dev, device); |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 588 | cdev->handler = __tape_do_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | device->cdev = cdev; |
Cornelia Huck | f455adc | 2008-05-15 16:52:37 +0200 | [diff] [blame] | 590 | ccw_device_get_id(cdev, &dev_id); |
| 591 | device->cdev_id = devid_to_int(&dev_id); |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 592 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 595 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | __tape_discard_requests(struct tape_device *device) |
| 597 | { |
| 598 | struct tape_request * request; |
| 599 | struct list_head * l, *n; |
| 600 | |
| 601 | list_for_each_safe(l, n, &device->req_queue) { |
| 602 | request = list_entry(l, struct tape_request, list); |
| 603 | if (request->status == TAPE_REQUEST_IN_IO) |
| 604 | request->status = TAPE_REQUEST_DONE; |
| 605 | list_del(&request->list); |
| 606 | |
| 607 | /* Decrease ref_count for removed request. */ |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 608 | request->device = NULL; |
| 609 | tape_put_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | request->rc = -EIO; |
| 611 | if (request->callback != NULL) |
| 612 | request->callback(request, request->callback_data); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * Driverfs tape remove function. |
| 618 | * |
| 619 | * This function is called whenever the common I/O layer detects the device |
| 620 | * gone. This can happen at any time and we cannot refuse. |
| 621 | */ |
| 622 | void |
| 623 | tape_generic_remove(struct ccw_device *cdev) |
| 624 | { |
| 625 | struct tape_device * device; |
| 626 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 627 | device = dev_get_drvdata(&cdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | if (!device) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev); |
| 632 | |
| 633 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 634 | switch (device->tape_state) { |
| 635 | case TS_INIT: |
| 636 | tape_state_set(device, TS_NOT_OPER); |
| 637 | case TS_NOT_OPER: |
| 638 | /* |
| 639 | * Nothing to do. |
| 640 | */ |
| 641 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 642 | break; |
| 643 | case TS_UNUSED: |
| 644 | /* |
| 645 | * Need only to release the device. |
| 646 | */ |
| 647 | tape_state_set(device, TS_NOT_OPER); |
| 648 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 649 | tape_cleanup_device(device); |
| 650 | break; |
| 651 | default: |
| 652 | /* |
| 653 | * There may be requests on the queue. We will not get |
| 654 | * an interrupt for a request that was running. So we |
| 655 | * just post them all as I/O errors. |
| 656 | */ |
| 657 | DBF_EVENT(3, "(%08x): Drive in use vanished!\n", |
| 658 | device->cdev_id); |
Michael Holzheu | 59e3692 | 2009-09-11 10:29:07 +0200 | [diff] [blame] | 659 | pr_warning("%s: A tape unit was detached while in " |
| 660 | "use\n", dev_name(&device->cdev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | tape_state_set(device, TS_NOT_OPER); |
| 662 | __tape_discard_requests(device); |
| 663 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 664 | tape_cleanup_device(device); |
| 665 | } |
| 666 | |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 667 | device = dev_get_drvdata(&cdev->dev); |
| 668 | if (device) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group); |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 670 | dev_set_drvdata(&cdev->dev, NULL); |
| 671 | tape_put_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * Allocate a new tape ccw request |
| 677 | */ |
| 678 | struct tape_request * |
| 679 | tape_alloc_request(int cplength, int datasize) |
| 680 | { |
| 681 | struct tape_request *request; |
| 682 | |
Stoyan Gaydarov | 6aa0d3a | 2009-03-26 15:24:47 +0100 | [diff] [blame] | 683 | BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
| 685 | DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize); |
| 686 | |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 687 | request = kzalloc(sizeof(struct tape_request), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | if (request == NULL) { |
| 689 | DBF_EXCEPTION(1, "cqra nomem\n"); |
| 690 | return ERR_PTR(-ENOMEM); |
| 691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | /* allocate channel program */ |
| 693 | if (cplength > 0) { |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 694 | request->cpaddr = kcalloc(cplength, sizeof(struct ccw1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | GFP_ATOMIC | GFP_DMA); |
| 696 | if (request->cpaddr == NULL) { |
| 697 | DBF_EXCEPTION(1, "cqra nomem\n"); |
| 698 | kfree(request); |
| 699 | return ERR_PTR(-ENOMEM); |
| 700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } |
| 702 | /* alloc small kernel buffer */ |
| 703 | if (datasize > 0) { |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 704 | request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | if (request->cpdata == NULL) { |
| 706 | DBF_EXCEPTION(1, "cqra nomem\n"); |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame] | 707 | kfree(request->cpaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | kfree(request); |
| 709 | return ERR_PTR(-ENOMEM); |
| 710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr, |
| 713 | request->cpdata); |
| 714 | |
| 715 | return request; |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * Free tape ccw request |
| 720 | */ |
| 721 | void |
| 722 | tape_free_request (struct tape_request * request) |
| 723 | { |
| 724 | DBF_LH(6, "Free request %p\n", request); |
| 725 | |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 726 | if (request->device) |
| 727 | tape_put_device(request->device); |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame] | 728 | kfree(request->cpdata); |
| 729 | kfree(request->cpaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | kfree(request); |
| 731 | } |
| 732 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 733 | static int |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 734 | __tape_start_io(struct tape_device *device, struct tape_request *request) |
| 735 | { |
| 736 | int rc; |
| 737 | |
| 738 | #ifdef CONFIG_S390_TAPE_BLOCK |
| 739 | if (request->op == TO_BLOCK) |
| 740 | device->discipline->check_locate(device, request); |
| 741 | #endif |
| 742 | rc = ccw_device_start( |
| 743 | device->cdev, |
| 744 | request->cpaddr, |
| 745 | (unsigned long) request, |
| 746 | 0x00, |
| 747 | request->options |
| 748 | ); |
| 749 | if (rc == 0) { |
| 750 | request->status = TAPE_REQUEST_IN_IO; |
| 751 | } else if (rc == -EBUSY) { |
| 752 | /* The common I/O subsystem is currently busy. Retry later. */ |
| 753 | request->status = TAPE_REQUEST_QUEUED; |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 754 | schedule_delayed_work(&device->tape_dnr, 0); |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 755 | rc = 0; |
| 756 | } else { |
| 757 | /* Start failed. Remove request and indicate failure. */ |
| 758 | DBF_EVENT(1, "tape: start request failed with RC = %i\n", rc); |
| 759 | } |
| 760 | return rc; |
| 761 | } |
| 762 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 763 | static void |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 764 | __tape_start_next_request(struct tape_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { |
| 766 | struct list_head *l, *n; |
| 767 | struct tape_request *request; |
| 768 | int rc; |
| 769 | |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 770 | DBF_LH(6, "__tape_start_next_request(%p)\n", device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | /* |
| 772 | * Try to start each request on request queue until one is |
| 773 | * started successful. |
| 774 | */ |
| 775 | list_for_each_safe(l, n, &device->req_queue) { |
| 776 | request = list_entry(l, struct tape_request, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 778 | /* |
| 779 | * Avoid race condition if bottom-half was triggered more than |
| 780 | * once. |
| 781 | */ |
| 782 | if (request->status == TAPE_REQUEST_IN_IO) |
| 783 | return; |
Michael Holzheu | 5f38433 | 2006-03-24 03:15:28 -0800 | [diff] [blame] | 784 | /* |
| 785 | * Request has already been stopped. We have to wait until |
| 786 | * the request is removed from the queue in the interrupt |
| 787 | * handling. |
| 788 | */ |
| 789 | if (request->status == TAPE_REQUEST_DONE) |
| 790 | return; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * We wanted to cancel the request but the common I/O layer |
| 794 | * was busy at that time. This can only happen if this |
| 795 | * function is called by delayed_next_request. |
| 796 | * Otherwise we start the next request on the queue. |
| 797 | */ |
| 798 | if (request->status == TAPE_REQUEST_CANCEL) { |
| 799 | rc = __tape_cancel_io(device, request); |
| 800 | } else { |
| 801 | rc = __tape_start_io(device, request); |
| 802 | } |
| 803 | if (rc == 0) |
| 804 | return; |
| 805 | |
| 806 | /* Set ending status. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | request->rc = rc; |
| 808 | request->status = TAPE_REQUEST_DONE; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 809 | |
| 810 | /* Remove from request queue. */ |
| 811 | list_del(&request->list); |
| 812 | |
| 813 | /* Do callback. */ |
| 814 | if (request->callback != NULL) |
| 815 | request->callback(request, request->callback_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
| 819 | static void |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 820 | tape_delayed_next_request(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 822 | struct tape_device *device = |
| 823 | container_of(work, struct tape_device, tape_dnr.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 825 | DBF_LH(6, "tape_delayed_next_request(%p)\n", device); |
| 826 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 827 | __tape_start_next_request(device); |
| 828 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 829 | } |
| 830 | |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 831 | static void tape_long_busy_timeout(unsigned long data) |
| 832 | { |
| 833 | struct tape_request *request; |
| 834 | struct tape_device *device; |
| 835 | |
| 836 | device = (struct tape_device *) data; |
| 837 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 838 | request = list_entry(device->req_queue.next, struct tape_request, list); |
Stoyan Gaydarov | 6aa0d3a | 2009-03-26 15:24:47 +0100 | [diff] [blame] | 839 | BUG_ON(request->status != TAPE_REQUEST_LONG_BUSY); |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 840 | DBF_LH(6, "%08x: Long busy timeout.\n", device->cdev_id); |
| 841 | __tape_start_next_request(device); |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 842 | device->lb_timeout.data = 0UL; |
| 843 | tape_put_device(device); |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 844 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 845 | } |
| 846 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 847 | static void |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 848 | __tape_end_request( |
| 849 | struct tape_device * device, |
| 850 | struct tape_request * request, |
| 851 | int rc) |
| 852 | { |
| 853 | DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc); |
| 854 | if (request) { |
| 855 | request->rc = rc; |
| 856 | request->status = TAPE_REQUEST_DONE; |
| 857 | |
| 858 | /* Remove from request queue. */ |
| 859 | list_del(&request->list); |
| 860 | |
| 861 | /* Do callback. */ |
| 862 | if (request->callback != NULL) |
| 863 | request->callback(request, request->callback_data); |
| 864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
| 866 | /* Start next request. */ |
| 867 | if (!list_empty(&device->req_queue)) |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 868 | __tape_start_next_request(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | * Write sense data to dbf |
| 873 | */ |
| 874 | void |
| 875 | tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request, |
| 876 | struct irb *irb) |
| 877 | { |
| 878 | unsigned int *sptr; |
| 879 | const char* op; |
| 880 | |
| 881 | if (request != NULL) |
| 882 | op = tape_op_verbose[request->op]; |
| 883 | else |
| 884 | op = "---"; |
| 885 | DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n", |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 886 | irb->scsw.cmd.dstat, irb->scsw.cmd.cstat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op); |
| 888 | sptr = (unsigned int *) irb->ecw; |
| 889 | DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]); |
| 890 | DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]); |
| 891 | DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]); |
| 892 | DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]); |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * I/O helper function. Adds the request to the request queue |
| 897 | * and starts it if the tape is idle. Has to be called with |
| 898 | * the device lock held. |
| 899 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 900 | static int |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 901 | __tape_start_request(struct tape_device *device, struct tape_request *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | { |
| 903 | int rc; |
| 904 | |
| 905 | switch (request->op) { |
| 906 | case TO_MSEN: |
| 907 | case TO_ASSIGN: |
| 908 | case TO_UNASSIGN: |
| 909 | case TO_READ_ATTMSG: |
Michael Holzheu | e296306 | 2007-05-04 18:47:53 +0200 | [diff] [blame] | 910 | case TO_RDC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if (device->tape_state == TS_INIT) |
| 912 | break; |
| 913 | if (device->tape_state == TS_UNUSED) |
| 914 | break; |
| 915 | default: |
| 916 | if (device->tape_state == TS_BLKUSE) |
| 917 | break; |
| 918 | if (device->tape_state != TS_IN_USE) |
| 919 | return -ENODEV; |
| 920 | } |
| 921 | |
| 922 | /* Increase use count of device for the added request. */ |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 923 | request->device = tape_get_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
| 925 | if (list_empty(&device->req_queue)) { |
| 926 | /* No other requests are on the queue. Start this one. */ |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 927 | rc = __tape_start_io(device, request); |
| 928 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | return rc; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | DBF_LH(5, "Request %p added for execution.\n", request); |
| 932 | list_add(&request->list, &device->req_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } else { |
| 934 | DBF_LH(5, "Request %p add to queue.\n", request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | request->status = TAPE_REQUEST_QUEUED; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 936 | list_add_tail(&request->list, &device->req_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * Add the request to the request queue, try to start it if the |
| 943 | * tape is idle. Return without waiting for end of i/o. |
| 944 | */ |
| 945 | int |
| 946 | tape_do_io_async(struct tape_device *device, struct tape_request *request) |
| 947 | { |
| 948 | int rc; |
| 949 | |
| 950 | DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request); |
| 951 | |
| 952 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 953 | /* Add request to request queue and try to start it. */ |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 954 | rc = __tape_start_request(device, request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 956 | return rc; |
| 957 | } |
| 958 | |
| 959 | /* |
| 960 | * tape_do_io/__tape_wake_up |
| 961 | * Add the request to the request queue, try to start it if the |
| 962 | * tape is idle and wait uninterruptible for its completion. |
| 963 | */ |
| 964 | static void |
| 965 | __tape_wake_up(struct tape_request *request, void *data) |
| 966 | { |
| 967 | request->callback = NULL; |
| 968 | wake_up((wait_queue_head_t *) data); |
| 969 | } |
| 970 | |
| 971 | int |
| 972 | tape_do_io(struct tape_device *device, struct tape_request *request) |
| 973 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | int rc; |
| 975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 977 | /* Setup callback */ |
| 978 | request->callback = __tape_wake_up; |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 979 | request->callback_data = &device->wait_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | /* Add request to request queue and try to start it. */ |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 981 | rc = __tape_start_request(device, request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 983 | if (rc) |
| 984 | return rc; |
| 985 | /* Request added to the queue. Wait for its completion. */ |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 986 | wait_event(device->wait_queue, (request->callback == NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | /* Get rc from request */ |
| 988 | return request->rc; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * tape_do_io_interruptible/__tape_wake_up_interruptible |
| 993 | * Add the request to the request queue, try to start it if the |
| 994 | * tape is idle and wait uninterruptible for its completion. |
| 995 | */ |
| 996 | static void |
| 997 | __tape_wake_up_interruptible(struct tape_request *request, void *data) |
| 998 | { |
| 999 | request->callback = NULL; |
| 1000 | wake_up_interruptible((wait_queue_head_t *) data); |
| 1001 | } |
| 1002 | |
| 1003 | int |
| 1004 | tape_do_io_interruptible(struct tape_device *device, |
| 1005 | struct tape_request *request) |
| 1006 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | int rc; |
| 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 1010 | /* Setup callback */ |
| 1011 | request->callback = __tape_wake_up_interruptible; |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 1012 | request->callback_data = &device->wait_queue; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1013 | rc = __tape_start_request(device, request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 1015 | if (rc) |
| 1016 | return rc; |
| 1017 | /* Request added to the queue. Wait for its completion. */ |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 1018 | rc = wait_event_interruptible(device->wait_queue, |
| 1019 | (request->callback == NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (rc != -ERESTARTSYS) |
| 1021 | /* Request finished normally. */ |
| 1022 | return request->rc; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | /* Interrupted by a signal. We have to stop the current request. */ |
| 1025 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1026 | rc = __tape_cancel_io(device, request); |
| 1027 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | if (rc == 0) { |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1029 | /* Wait for the interrupt that acknowledges the halt. */ |
| 1030 | do { |
| 1031 | rc = wait_event_interruptible( |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 1032 | device->wait_queue, |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1033 | (request->callback == NULL) |
| 1034 | ); |
Michael Holzheu | 4cd190a | 2006-03-24 03:15:27 -0800 | [diff] [blame] | 1035 | } while (rc == -ERESTARTSYS); |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id); |
| 1038 | rc = -ERESTARTSYS; |
| 1039 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | return rc; |
| 1041 | } |
| 1042 | |
| 1043 | /* |
Michael Holzheu | 5f38433 | 2006-03-24 03:15:28 -0800 | [diff] [blame] | 1044 | * Stop running ccw. |
| 1045 | */ |
| 1046 | int |
| 1047 | tape_cancel_io(struct tape_device *device, struct tape_request *request) |
| 1048 | { |
| 1049 | int rc; |
| 1050 | |
| 1051 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 1052 | rc = __tape_cancel_io(device, request); |
| 1053 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 1054 | return rc; |
| 1055 | } |
| 1056 | |
| 1057 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | * Tape interrupt routine, called from the ccw_device layer |
| 1059 | */ |
| 1060 | static void |
| 1061 | __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb) |
| 1062 | { |
| 1063 | struct tape_device *device; |
| 1064 | struct tape_request *request; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | int rc; |
| 1066 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1067 | device = dev_get_drvdata(&cdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | if (device == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | return; |
| 1070 | } |
| 1071 | request = (struct tape_request *) intparm; |
| 1072 | |
| 1073 | DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request); |
| 1074 | |
| 1075 | /* On special conditions irb is an error pointer */ |
| 1076 | if (IS_ERR(irb)) { |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1077 | /* FIXME: What to do with the request? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | switch (PTR_ERR(irb)) { |
| 1079 | case -ETIMEDOUT: |
Sebastian Ott | f2166bb | 2010-10-29 16:50:44 +0200 | [diff] [blame^] | 1080 | DBF_LH(1, "(%08x): Request timed out\n", |
| 1081 | device->cdev_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | case -EIO: |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1083 | __tape_end_request(device, request, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | break; |
| 1085 | default: |
Sebastian Ott | f2166bb | 2010-10-29 16:50:44 +0200 | [diff] [blame^] | 1086 | DBF_LH(1, "(%08x): Unexpected i/o error %li\n", |
| 1087 | device->cdev_id, PTR_ERR(irb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | } |
| 1089 | return; |
| 1090 | } |
| 1091 | |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1092 | /* |
| 1093 | * If the condition code is not zero and the start function bit is |
| 1094 | * still set, this is an deferred error and the last start I/O did |
Stefan Bader | 842d3fb | 2006-03-24 03:15:26 -0800 | [diff] [blame] | 1095 | * not succeed. At this point the condition that caused the deferred |
| 1096 | * error might still apply. So we just schedule the request to be |
| 1097 | * started later. |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1098 | */ |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 1099 | if (irb->scsw.cmd.cc != 0 && |
| 1100 | (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) && |
Michael Holzheu | 5f38433 | 2006-03-24 03:15:28 -0800 | [diff] [blame] | 1101 | (request->status == TAPE_REQUEST_IN_IO)) { |
| 1102 | DBF_EVENT(3,"(%08x): deferred cc=%i, fctl=%i. restarting\n", |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 1103 | device->cdev_id, irb->scsw.cmd.cc, irb->scsw.cmd.fctl); |
Stefan Bader | 842d3fb | 2006-03-24 03:15:26 -0800 | [diff] [blame] | 1104 | request->status = TAPE_REQUEST_QUEUED; |
Michael Holzheu | 5f38433 | 2006-03-24 03:15:28 -0800 | [diff] [blame] | 1105 | schedule_delayed_work(&device->tape_dnr, HZ); |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1106 | return; |
| 1107 | } |
| 1108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | /* May be an unsolicited irq */ |
| 1110 | if(request != NULL) |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 1111 | request->rescnt = irb->scsw.cmd.count; |
| 1112 | else if ((irb->scsw.cmd.dstat == 0x85 || irb->scsw.cmd.dstat == 0x80) && |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 1113 | !list_empty(&device->req_queue)) { |
| 1114 | /* Not Ready to Ready after long busy ? */ |
| 1115 | struct tape_request *req; |
| 1116 | req = list_entry(device->req_queue.next, |
| 1117 | struct tape_request, list); |
| 1118 | if (req->status == TAPE_REQUEST_LONG_BUSY) { |
| 1119 | DBF_EVENT(3, "(%08x): del timer\n", device->cdev_id); |
| 1120 | if (del_timer(&device->lb_timeout)) { |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 1121 | device->lb_timeout.data = 0UL; |
| 1122 | tape_put_device(device); |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 1123 | __tape_start_next_request(device); |
| 1124 | } |
| 1125 | return; |
| 1126 | } |
| 1127 | } |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 1128 | if (irb->scsw.cmd.dstat != 0x0c) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | /* Set the 'ONLINE' flag depending on sense byte 1 */ |
| 1130 | if(*(((__u8 *) irb->ecw) + 1) & SENSE_DRIVE_ONLINE) |
| 1131 | device->tape_generic_status |= GMT_ONLINE(~0); |
| 1132 | else |
| 1133 | device->tape_generic_status &= ~GMT_ONLINE(~0); |
| 1134 | |
| 1135 | /* |
| 1136 | * Any request that does not come back with channel end |
| 1137 | * and device end is unusual. Log the sense data. |
| 1138 | */ |
| 1139 | DBF_EVENT(3,"-- Tape Interrupthandler --\n"); |
| 1140 | tape_dump_sense_dbf(device, request, irb); |
| 1141 | } else { |
| 1142 | /* Upon normal completion the device _is_ online */ |
| 1143 | device->tape_generic_status |= GMT_ONLINE(~0); |
| 1144 | } |
| 1145 | if (device->tape_state == TS_NOT_OPER) { |
| 1146 | DBF_EVENT(6, "tape:device is not operational\n"); |
| 1147 | return; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * Request that were canceled still come back with an interrupt. |
| 1152 | * To detect these request the state will be set to TAPE_REQUEST_DONE. |
| 1153 | */ |
| 1154 | if(request != NULL && request->status == TAPE_REQUEST_DONE) { |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1155 | __tape_end_request(device, request, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | return; |
| 1157 | } |
| 1158 | |
| 1159 | rc = device->discipline->irq(device, request, irb); |
| 1160 | /* |
| 1161 | * rc < 0 : request finished unsuccessfully. |
| 1162 | * rc == TAPE_IO_SUCCESS: request finished successfully. |
| 1163 | * rc == TAPE_IO_PENDING: request is still running. Ignore rc. |
| 1164 | * rc == TAPE_IO_RETRY: request finished but needs another go. |
| 1165 | * rc == TAPE_IO_STOP: request needs to get terminated. |
| 1166 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | switch (rc) { |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1168 | case TAPE_IO_SUCCESS: |
| 1169 | /* Upon normal completion the device _is_ online */ |
| 1170 | device->tape_generic_status |= GMT_ONLINE(~0); |
| 1171 | __tape_end_request(device, request, rc); |
| 1172 | break; |
| 1173 | case TAPE_IO_PENDING: |
| 1174 | break; |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 1175 | case TAPE_IO_LONG_BUSY: |
| 1176 | device->lb_timeout.data = |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 1177 | (unsigned long) tape_get_device(device); |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 1178 | device->lb_timeout.expires = jiffies + |
| 1179 | LONG_BUSY_TIMEOUT * HZ; |
| 1180 | DBF_EVENT(3, "(%08x): add timer\n", device->cdev_id); |
| 1181 | add_timer(&device->lb_timeout); |
| 1182 | request->status = TAPE_REQUEST_LONG_BUSY; |
| 1183 | break; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1184 | case TAPE_IO_RETRY: |
| 1185 | rc = __tape_start_io(device, request); |
| 1186 | if (rc) |
| 1187 | __tape_end_request(device, request, rc); |
| 1188 | break; |
| 1189 | case TAPE_IO_STOP: |
| 1190 | rc = __tape_cancel_io(device, request); |
| 1191 | if (rc) |
| 1192 | __tape_end_request(device, request, rc); |
| 1193 | break; |
| 1194 | default: |
| 1195 | if (rc > 0) { |
| 1196 | DBF_EVENT(6, "xunknownrc\n"); |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1197 | __tape_end_request(device, request, -EIO); |
| 1198 | } else { |
| 1199 | __tape_end_request(device, request, rc); |
| 1200 | } |
| 1201 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * Tape device open function used by tape_char & tape_block frontends. |
| 1207 | */ |
| 1208 | int |
| 1209 | tape_open(struct tape_device *device) |
| 1210 | { |
| 1211 | int rc; |
| 1212 | |
Frank Munzert | b3c21e4 | 2008-10-28 11:10:19 +0100 | [diff] [blame] | 1213 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | if (device->tape_state == TS_NOT_OPER) { |
| 1215 | DBF_EVENT(6, "TAPE:nodev\n"); |
| 1216 | rc = -ENODEV; |
| 1217 | } else if (device->tape_state == TS_IN_USE) { |
| 1218 | DBF_EVENT(6, "TAPE:dbusy\n"); |
| 1219 | rc = -EBUSY; |
| 1220 | } else if (device->tape_state == TS_BLKUSE) { |
| 1221 | DBF_EVENT(6, "TAPE:dbusy\n"); |
| 1222 | rc = -EBUSY; |
| 1223 | } else if (device->discipline != NULL && |
| 1224 | !try_module_get(device->discipline->owner)) { |
| 1225 | DBF_EVENT(6, "TAPE:nodisc\n"); |
| 1226 | rc = -ENODEV; |
| 1227 | } else { |
| 1228 | tape_state_set(device, TS_IN_USE); |
| 1229 | rc = 0; |
| 1230 | } |
Frank Munzert | b3c21e4 | 2008-10-28 11:10:19 +0100 | [diff] [blame] | 1231 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | return rc; |
| 1233 | } |
| 1234 | |
| 1235 | /* |
| 1236 | * Tape device release function used by tape_char & tape_block frontends. |
| 1237 | */ |
| 1238 | int |
| 1239 | tape_release(struct tape_device *device) |
| 1240 | { |
Frank Munzert | b3c21e4 | 2008-10-28 11:10:19 +0100 | [diff] [blame] | 1241 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | if (device->tape_state == TS_IN_USE) |
| 1243 | tape_state_set(device, TS_UNUSED); |
| 1244 | module_put(device->discipline->owner); |
Frank Munzert | b3c21e4 | 2008-10-28 11:10:19 +0100 | [diff] [blame] | 1245 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Execute a magnetic tape command a number of times. |
| 1251 | */ |
| 1252 | int |
| 1253 | tape_mtop(struct tape_device *device, int mt_op, int mt_count) |
| 1254 | { |
| 1255 | tape_mtop_fn fn; |
| 1256 | int rc; |
| 1257 | |
| 1258 | DBF_EVENT(6, "TAPE:mtio\n"); |
| 1259 | DBF_EVENT(6, "TAPE:ioop: %x\n", mt_op); |
| 1260 | DBF_EVENT(6, "TAPE:arg: %x\n", mt_count); |
| 1261 | |
| 1262 | if (mt_op < 0 || mt_op >= TAPE_NR_MTOPS) |
| 1263 | return -EINVAL; |
| 1264 | fn = device->discipline->mtop_array[mt_op]; |
| 1265 | if (fn == NULL) |
| 1266 | return -EINVAL; |
| 1267 | |
| 1268 | /* We assume that the backends can handle count up to 500. */ |
| 1269 | if (mt_op == MTBSR || mt_op == MTFSR || mt_op == MTFSF || |
| 1270 | mt_op == MTBSF || mt_op == MTFSFM || mt_op == MTBSFM) { |
| 1271 | rc = 0; |
| 1272 | for (; mt_count > 500; mt_count -= 500) |
| 1273 | if ((rc = fn(device, 500)) != 0) |
| 1274 | break; |
| 1275 | if (rc == 0) |
| 1276 | rc = fn(device, mt_count); |
| 1277 | } else |
| 1278 | rc = fn(device, mt_count); |
| 1279 | return rc; |
| 1280 | |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | * Tape init function. |
| 1285 | */ |
| 1286 | static int |
| 1287 | tape_init (void) |
| 1288 | { |
Michael Holzheu | 66a464d | 2005-06-25 14:55:33 -0700 | [diff] [blame] | 1289 | TAPE_DBF_AREA = debug_register ( "tape", 2, 2, 4*sizeof(long)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | debug_register_view(TAPE_DBF_AREA, &debug_sprintf_view); |
| 1291 | #ifdef DBF_LIKE_HELL |
| 1292 | debug_set_level(TAPE_DBF_AREA, 6); |
| 1293 | #endif |
Heiko Carstens | e018ba1 | 2006-02-01 03:06:31 -0800 | [diff] [blame] | 1294 | DBF_EVENT(3, "tape init\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | tape_proc_init(); |
| 1296 | tapechar_init (); |
| 1297 | tapeblock_init (); |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | * Tape exit function. |
| 1303 | */ |
| 1304 | static void |
| 1305 | tape_exit(void) |
| 1306 | { |
| 1307 | DBF_EVENT(6, "tape exit\n"); |
| 1308 | |
| 1309 | /* Get rid of the frontends */ |
| 1310 | tapechar_exit(); |
| 1311 | tapeblock_exit(); |
| 1312 | tape_proc_cleanup(); |
| 1313 | debug_unregister (TAPE_DBF_AREA); |
| 1314 | } |
| 1315 | |
| 1316 | MODULE_AUTHOR("(C) 2001 IBM Deutschland Entwicklung GmbH by Carsten Otte and " |
| 1317 | "Michael Holzheu (cotte@de.ibm.com,holzheu@de.ibm.com)"); |
Heiko Carstens | e018ba1 | 2006-02-01 03:06:31 -0800 | [diff] [blame] | 1318 | MODULE_DESCRIPTION("Linux on zSeries channel attached tape device driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | MODULE_LICENSE("GPL"); |
| 1320 | |
| 1321 | module_init(tape_init); |
| 1322 | module_exit(tape_exit); |
| 1323 | |
| 1324 | EXPORT_SYMBOL(tape_generic_remove); |
| 1325 | EXPORT_SYMBOL(tape_generic_probe); |
| 1326 | EXPORT_SYMBOL(tape_generic_online); |
| 1327 | EXPORT_SYMBOL(tape_generic_offline); |
Frank Munzert | 3ef32e62 | 2009-06-16 10:30:39 +0200 | [diff] [blame] | 1328 | EXPORT_SYMBOL(tape_generic_pm_suspend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | EXPORT_SYMBOL(tape_put_device); |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 1330 | EXPORT_SYMBOL(tape_get_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | EXPORT_SYMBOL(tape_state_verbose); |
| 1332 | EXPORT_SYMBOL(tape_op_verbose); |
| 1333 | EXPORT_SYMBOL(tape_state_set); |
| 1334 | EXPORT_SYMBOL(tape_med_state_set); |
| 1335 | EXPORT_SYMBOL(tape_alloc_request); |
| 1336 | EXPORT_SYMBOL(tape_free_request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | EXPORT_SYMBOL(tape_dump_sense_dbf); |
| 1338 | EXPORT_SYMBOL(tape_do_io); |
| 1339 | EXPORT_SYMBOL(tape_do_io_async); |
| 1340 | EXPORT_SYMBOL(tape_do_io_interruptible); |
Michael Holzheu | 5f38433 | 2006-03-24 03:15:28 -0800 | [diff] [blame] | 1341 | EXPORT_SYMBOL(tape_cancel_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | EXPORT_SYMBOL(tape_mtop); |