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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> // for kernel parameters |
| 17 | #include <linux/kmod.h> // for requesting modules |
| 18 | #include <linux/spinlock.h> // for locks |
| 19 | #include <linux/vmalloc.h> |
| 20 | #include <linux/list.h> |
| 21 | |
| 22 | #include <asm/types.h> // for variable types |
| 23 | |
| 24 | #define TAPE_DBF_AREA tape_core_dbf |
| 25 | |
| 26 | #include "tape.h" |
| 27 | #include "tape_std.h" |
| 28 | |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 29 | #define LONG_BUSY_TIMEOUT 180 /* seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | static void __tape_do_irq (struct ccw_device *, unsigned long, struct irb *); |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 32 | static void tape_delayed_next_request(struct work_struct *); |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 33 | static void tape_long_busy_timeout(unsigned long data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * One list to contain all tape devices of all disciplines, so |
| 37 | * we can assign the devices to minor numbers of the same major |
| 38 | * The list is protected by the rwlock |
| 39 | */ |
Denis Cheng | c11ca97 | 2008-01-26 14:11:13 +0100 | [diff] [blame] | 40 | static LIST_HEAD(tape_device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static DEFINE_RWLOCK(tape_device_lock); |
| 42 | |
| 43 | /* |
| 44 | * Pointer to debug area. |
| 45 | */ |
| 46 | debug_info_t *TAPE_DBF_AREA = NULL; |
| 47 | EXPORT_SYMBOL(TAPE_DBF_AREA); |
| 48 | |
| 49 | /* |
| 50 | * Printable strings for tape enumerations. |
| 51 | */ |
| 52 | const char *tape_state_verbose[TS_SIZE] = |
| 53 | { |
| 54 | [TS_UNUSED] = "UNUSED", |
| 55 | [TS_IN_USE] = "IN_USE", |
| 56 | [TS_BLKUSE] = "BLKUSE", |
| 57 | [TS_INIT] = "INIT ", |
| 58 | [TS_NOT_OPER] = "NOT_OP" |
| 59 | }; |
| 60 | |
| 61 | const char *tape_op_verbose[TO_SIZE] = |
| 62 | { |
| 63 | [TO_BLOCK] = "BLK", [TO_BSB] = "BSB", |
| 64 | [TO_BSF] = "BSF", [TO_DSE] = "DSE", |
| 65 | [TO_FSB] = "FSB", [TO_FSF] = "FSF", |
| 66 | [TO_LBL] = "LBL", [TO_NOP] = "NOP", |
| 67 | [TO_RBA] = "RBA", [TO_RBI] = "RBI", |
| 68 | [TO_RFO] = "RFO", [TO_REW] = "REW", |
| 69 | [TO_RUN] = "RUN", [TO_WRI] = "WRI", |
| 70 | [TO_WTM] = "WTM", [TO_MSEN] = "MSN", |
| 71 | [TO_LOAD] = "LOA", [TO_READ_CONFIG] = "RCF", |
| 72 | [TO_READ_ATTMSG] = "RAT", |
| 73 | [TO_DIS] = "DIS", [TO_ASSIGN] = "ASS", |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 74 | [TO_UNASSIGN] = "UAS", [TO_CRYPT_ON] = "CON", |
| 75 | [TO_CRYPT_OFF] = "COF", [TO_KEKL_SET] = "KLS", |
Michael Holzheu | e296306 | 2007-05-04 18:47:53 +0200 | [diff] [blame] | 76 | [TO_KEKL_QUERY] = "KLQ",[TO_RDC] = "RDC", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Cornelia Huck | f455adc | 2008-05-15 16:52:37 +0200 | [diff] [blame] | 79 | static int devid_to_int(struct ccw_dev_id *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Cornelia Huck | f455adc | 2008-05-15 16:52:37 +0200 | [diff] [blame] | 81 | return dev_id->devno + (dev_id->ssid << 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Some channel attached tape specific attributes. |
| 86 | * |
| 87 | * FIXME: In the future the first_minor and blocksize attribute should be |
| 88 | * replaced by a link to the cdev tree. |
| 89 | */ |
| 90 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 91 | 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] | 92 | { |
| 93 | struct tape_device *tdev; |
| 94 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 95 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->medium_state); |
| 97 | } |
| 98 | |
| 99 | static |
| 100 | DEVICE_ATTR(medium_state, 0444, tape_medium_state_show, NULL); |
| 101 | |
| 102 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 103 | 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] | 104 | { |
| 105 | struct tape_device *tdev; |
| 106 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 107 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->first_minor); |
| 109 | } |
| 110 | |
| 111 | static |
| 112 | DEVICE_ATTR(first_minor, 0444, tape_first_minor_show, NULL); |
| 113 | |
| 114 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 115 | tape_state_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | struct tape_device *tdev; |
| 118 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 119 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return scnprintf(buf, PAGE_SIZE, "%s\n", (tdev->first_minor < 0) ? |
| 121 | "OFFLINE" : tape_state_verbose[tdev->tape_state]); |
| 122 | } |
| 123 | |
| 124 | static |
| 125 | DEVICE_ATTR(state, 0444, tape_state_show, NULL); |
| 126 | |
| 127 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 128 | tape_operation_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | struct tape_device *tdev; |
| 131 | ssize_t rc; |
| 132 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 133 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | if (tdev->first_minor < 0) |
| 135 | return scnprintf(buf, PAGE_SIZE, "N/A\n"); |
| 136 | |
| 137 | spin_lock_irq(get_ccwdev_lock(tdev->cdev)); |
| 138 | if (list_empty(&tdev->req_queue)) |
| 139 | rc = scnprintf(buf, PAGE_SIZE, "---\n"); |
| 140 | else { |
| 141 | struct tape_request *req; |
| 142 | |
| 143 | req = list_entry(tdev->req_queue.next, struct tape_request, |
| 144 | list); |
| 145 | rc = scnprintf(buf,PAGE_SIZE, "%s\n", tape_op_verbose[req->op]); |
| 146 | } |
| 147 | spin_unlock_irq(get_ccwdev_lock(tdev->cdev)); |
| 148 | return rc; |
| 149 | } |
| 150 | |
| 151 | static |
| 152 | DEVICE_ATTR(operation, 0444, tape_operation_show, NULL); |
| 153 | |
| 154 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 155 | tape_blocksize_show(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | struct tape_device *tdev; |
| 158 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 159 | tdev = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | return scnprintf(buf, PAGE_SIZE, "%i\n", tdev->char_data.block_size); |
| 162 | } |
| 163 | |
| 164 | static |
| 165 | DEVICE_ATTR(blocksize, 0444, tape_blocksize_show, NULL); |
| 166 | |
| 167 | static struct attribute *tape_attrs[] = { |
| 168 | &dev_attr_medium_state.attr, |
| 169 | &dev_attr_first_minor.attr, |
| 170 | &dev_attr_state.attr, |
| 171 | &dev_attr_operation.attr, |
| 172 | &dev_attr_blocksize.attr, |
| 173 | NULL |
| 174 | }; |
| 175 | |
| 176 | static struct attribute_group tape_attr_group = { |
| 177 | .attrs = tape_attrs, |
| 178 | }; |
| 179 | |
| 180 | /* |
| 181 | * Tape state functions |
| 182 | */ |
| 183 | void |
| 184 | tape_state_set(struct tape_device *device, enum tape_state newstate) |
| 185 | { |
| 186 | const char *str; |
| 187 | |
| 188 | if (device->tape_state == TS_NOT_OPER) { |
| 189 | DBF_EVENT(3, "ts_set err: not oper\n"); |
| 190 | return; |
| 191 | } |
| 192 | DBF_EVENT(4, "ts. dev: %x\n", device->first_minor); |
Peter Oberparleiter | f976069 | 2006-04-10 22:53:49 -0700 | [diff] [blame] | 193 | DBF_EVENT(4, "old ts:\t\n"); |
| 194 | if (device->tape_state < TS_SIZE && device->tape_state >=0 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | str = tape_state_verbose[device->tape_state]; |
| 196 | else |
| 197 | str = "UNKNOWN TS"; |
| 198 | DBF_EVENT(4, "%s\n", str); |
| 199 | DBF_EVENT(4, "new ts:\t\n"); |
Peter Oberparleiter | f976069 | 2006-04-10 22:53:49 -0700 | [diff] [blame] | 200 | if (newstate < TS_SIZE && newstate >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | str = tape_state_verbose[newstate]; |
| 202 | else |
| 203 | str = "UNKNOWN TS"; |
| 204 | DBF_EVENT(4, "%s\n", str); |
| 205 | device->tape_state = newstate; |
| 206 | wake_up(&device->state_change_wq); |
| 207 | } |
| 208 | |
| 209 | void |
| 210 | tape_med_state_set(struct tape_device *device, enum tape_medium_state newstate) |
| 211 | { |
| 212 | if (device->medium_state == newstate) |
| 213 | return; |
| 214 | switch(newstate){ |
| 215 | case MS_UNLOADED: |
| 216 | device->tape_generic_status |= GMT_DR_OPEN(~0); |
Michael Holzheu | 53f8c57 | 2009-09-11 10:29:02 +0200 | [diff] [blame] | 217 | if (device->medium_state == MS_LOADED) |
Michael Holzheu | 59e3692 | 2009-09-11 10:29:07 +0200 | [diff] [blame] | 218 | pr_info("%s: The tape cartridge has been successfully " |
| 219 | "unloaded\n", dev_name(&device->cdev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | break; |
| 221 | case MS_LOADED: |
| 222 | device->tape_generic_status &= ~GMT_DR_OPEN(~0); |
Michael Holzheu | 53f8c57 | 2009-09-11 10:29:02 +0200 | [diff] [blame] | 223 | if (device->medium_state == MS_UNLOADED) |
Michael Holzheu | 59e3692 | 2009-09-11 10:29:07 +0200 | [diff] [blame] | 224 | pr_info("%s: A tape cartridge has been mounted\n", |
| 225 | dev_name(&device->cdev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | break; |
| 227 | default: |
| 228 | // print nothing |
| 229 | break; |
| 230 | } |
| 231 | device->medium_state = newstate; |
| 232 | wake_up(&device->state_change_wq); |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Stop running ccw. Has to be called with the device lock held. |
| 237 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 238 | static int |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 239 | __tape_cancel_io(struct tape_device *device, struct tape_request *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
| 241 | int retries; |
| 242 | int rc; |
| 243 | |
| 244 | /* Check if interrupt has already been processed */ |
| 245 | if (request->callback == NULL) |
| 246 | return 0; |
| 247 | |
| 248 | rc = 0; |
| 249 | for (retries = 0; retries < 5; retries++) { |
| 250 | rc = ccw_device_clear(device->cdev, (long) request); |
| 251 | |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 252 | switch (rc) { |
| 253 | case 0: |
| 254 | request->status = TAPE_REQUEST_DONE; |
| 255 | return 0; |
| 256 | case -EBUSY: |
| 257 | request->status = TAPE_REQUEST_CANCEL; |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 258 | schedule_delayed_work(&device->tape_dnr, 0); |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 259 | return 0; |
| 260 | case -ENODEV: |
| 261 | DBF_EXCEPTION(2, "device gone, retry\n"); |
| 262 | break; |
| 263 | case -EIO: |
| 264 | DBF_EXCEPTION(2, "I/O error, retry\n"); |
| 265 | break; |
| 266 | default: |
| 267 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return rc; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Add device into the sorted list, giving it the first |
| 276 | * available minor number. |
| 277 | */ |
| 278 | static int |
| 279 | tape_assign_minor(struct tape_device *device) |
| 280 | { |
| 281 | struct tape_device *tmp; |
| 282 | int minor; |
| 283 | |
| 284 | minor = 0; |
| 285 | write_lock(&tape_device_lock); |
| 286 | list_for_each_entry(tmp, &tape_device_list, node) { |
| 287 | if (minor < tmp->first_minor) |
| 288 | break; |
| 289 | minor += TAPE_MINORS_PER_DEV; |
| 290 | } |
| 291 | if (minor >= 256) { |
| 292 | write_unlock(&tape_device_lock); |
| 293 | return -ENODEV; |
| 294 | } |
| 295 | device->first_minor = minor; |
| 296 | list_add_tail(&device->node, &tmp->node); |
| 297 | write_unlock(&tape_device_lock); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | /* remove device from the list */ |
| 302 | static void |
| 303 | tape_remove_minor(struct tape_device *device) |
| 304 | { |
| 305 | write_lock(&tape_device_lock); |
| 306 | list_del_init(&device->node); |
| 307 | device->first_minor = -1; |
| 308 | write_unlock(&tape_device_lock); |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Set a device online. |
| 313 | * |
| 314 | * This function is called by the common I/O layer to move a device from the |
| 315 | * detected but offline into the online state. |
| 316 | * If we return an error (RC < 0) the device remains in the offline state. This |
| 317 | * can happen if the device is assigned somewhere else, for example. |
| 318 | */ |
| 319 | int |
| 320 | tape_generic_online(struct tape_device *device, |
| 321 | struct tape_discipline *discipline) |
| 322 | { |
| 323 | int rc; |
| 324 | |
| 325 | DBF_LH(6, "tape_enable_device(%p, %p)\n", device, discipline); |
| 326 | |
| 327 | if (device->tape_state != TS_INIT) { |
| 328 | DBF_LH(3, "Tapestate not INIT (%d)\n", device->tape_state); |
| 329 | return -EINVAL; |
| 330 | } |
| 331 | |
Michael Holzheu | cced1dd | 2007-02-05 21:18:26 +0100 | [diff] [blame] | 332 | init_timer(&device->lb_timeout); |
| 333 | device->lb_timeout.function = tape_long_busy_timeout; |
| 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | /* Let the discipline have a go at the device. */ |
| 336 | device->discipline = discipline; |
| 337 | if (!try_module_get(discipline->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | rc = discipline->setup_device(device); |
| 342 | if (rc) |
| 343 | goto out; |
| 344 | rc = tape_assign_minor(device); |
| 345 | if (rc) |
| 346 | goto out_discipline; |
| 347 | |
| 348 | rc = tapechar_setup_device(device); |
| 349 | if (rc) |
| 350 | goto out_minor; |
| 351 | rc = tapeblock_setup_device(device); |
| 352 | if (rc) |
| 353 | goto out_char; |
| 354 | |
| 355 | tape_state_set(device, TS_UNUSED); |
| 356 | |
| 357 | DBF_LH(3, "(%08x): Drive set online\n", device->cdev_id); |
| 358 | |
| 359 | return 0; |
| 360 | |
| 361 | out_char: |
| 362 | tapechar_cleanup_device(device); |
Roel Kluin | 68d36bd | 2009-09-11 10:28:55 +0200 | [diff] [blame] | 363 | out_minor: |
| 364 | tape_remove_minor(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | out_discipline: |
| 366 | device->discipline->cleanup_device(device); |
| 367 | device->discipline = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | out: |
| 369 | module_put(discipline->owner); |
| 370 | return rc; |
| 371 | } |
| 372 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 373 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | tape_cleanup_device(struct tape_device *device) |
| 375 | { |
| 376 | tapeblock_cleanup_device(device); |
| 377 | tapechar_cleanup_device(device); |
| 378 | device->discipline->cleanup_device(device); |
| 379 | module_put(device->discipline->owner); |
| 380 | tape_remove_minor(device); |
| 381 | tape_med_state_set(device, MS_UNKNOWN); |
| 382 | } |
| 383 | |
| 384 | /* |
Frank Munzert | 3ef32e62 | 2009-06-16 10:30:39 +0200 | [diff] [blame] | 385 | * Suspend device. |
| 386 | * |
| 387 | * Called by the common I/O layer if the drive should be suspended on user |
| 388 | * request. We refuse to suspend if the device is loaded or in use for the |
| 389 | * following reason: |
| 390 | * While the Linux guest is suspended, it might be logged off which causes |
| 391 | * devices to be detached. Tape devices are automatically rewound and unloaded |
| 392 | * during DETACH processing (unless the tape device was attached with the |
| 393 | * NOASSIGN or MULTIUSER option). After rewind/unload, there is no way to |
| 394 | * resume the original state of the tape device, since we would need to |
| 395 | * manually re-load the cartridge which was active at suspend time. |
| 396 | */ |
| 397 | int tape_generic_pm_suspend(struct ccw_device *cdev) |
| 398 | { |
| 399 | struct tape_device *device; |
| 400 | |
Martin Schwidefsky | 4f0076f | 2009-06-22 12:08:19 +0200 | [diff] [blame] | 401 | device = dev_get_drvdata(&cdev->dev); |
Frank Munzert | 3ef32e62 | 2009-06-16 10:30:39 +0200 | [diff] [blame] | 402 | if (!device) { |
| 403 | return -ENODEV; |
| 404 | } |
| 405 | |
| 406 | DBF_LH(3, "(%08x): tape_generic_pm_suspend(%p)\n", |
| 407 | device->cdev_id, device); |
| 408 | |
| 409 | if (device->medium_state != MS_UNLOADED) { |
| 410 | pr_err("A cartridge is loaded in tape device %s, " |
| 411 | "refusing to suspend\n", dev_name(&cdev->dev)); |
| 412 | return -EBUSY; |
| 413 | } |
| 414 | |
| 415 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 416 | switch (device->tape_state) { |
| 417 | case TS_INIT: |
| 418 | case TS_NOT_OPER: |
| 419 | case TS_UNUSED: |
| 420 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 421 | break; |
| 422 | default: |
| 423 | pr_err("Tape device %s is busy, refusing to " |
| 424 | "suspend\n", dev_name(&cdev->dev)); |
| 425 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 426 | return -EBUSY; |
| 427 | } |
| 428 | |
| 429 | DBF_LH(3, "(%08x): Drive suspended.\n", device->cdev_id); |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | * Set device offline. |
| 435 | * |
| 436 | * Called by the common I/O layer if the drive should set offline on user |
| 437 | * request. We may prevent this by returning an error. |
| 438 | * Manual offline is only allowed while the drive is not in use. |
| 439 | */ |
| 440 | int |
Frank Munzert | 4d7a3cd | 2009-04-23 13:58:09 +0200 | [diff] [blame] | 441 | tape_generic_offline(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
Frank Munzert | 4d7a3cd | 2009-04-23 13:58:09 +0200 | [diff] [blame] | 443 | struct tape_device *device; |
| 444 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 445 | device = dev_get_drvdata(&cdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | if (!device) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return -ENODEV; |
| 448 | } |
| 449 | |
| 450 | DBF_LH(3, "(%08x): tape_generic_offline(%p)\n", |
| 451 | device->cdev_id, device); |
| 452 | |
| 453 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 454 | switch (device->tape_state) { |
| 455 | case TS_INIT: |
| 456 | case TS_NOT_OPER: |
| 457 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 458 | break; |
| 459 | case TS_UNUSED: |
| 460 | tape_state_set(device, TS_INIT); |
| 461 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 462 | tape_cleanup_device(device); |
| 463 | break; |
| 464 | default: |
| 465 | DBF_EVENT(3, "(%08x): Set offline failed " |
| 466 | "- drive in use.\n", |
| 467 | device->cdev_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 469 | return -EBUSY; |
| 470 | } |
| 471 | |
| 472 | DBF_LH(3, "(%08x): Drive set offline.\n", device->cdev_id); |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Allocate memory for a new device structure. |
| 478 | */ |
| 479 | static struct tape_device * |
| 480 | tape_alloc_device(void) |
| 481 | { |
| 482 | struct tape_device *device; |
| 483 | |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 484 | device = kzalloc(sizeof(struct tape_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | if (device == NULL) { |
| 486 | DBF_EXCEPTION(2, "ti:no mem\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | return ERR_PTR(-ENOMEM); |
| 488 | } |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 489 | device->modeset_byte = kmalloc(1, GFP_KERNEL | GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | if (device->modeset_byte == NULL) { |
| 491 | DBF_EXCEPTION(2, "ti:no mem\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | kfree(device); |
| 493 | return ERR_PTR(-ENOMEM); |
| 494 | } |
| 495 | INIT_LIST_HEAD(&device->req_queue); |
| 496 | INIT_LIST_HEAD(&device->node); |
| 497 | init_waitqueue_head(&device->state_change_wq); |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 498 | init_waitqueue_head(&device->wait_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | device->tape_state = TS_INIT; |
| 500 | device->medium_state = MS_UNKNOWN; |
| 501 | *device->modeset_byte = 0; |
| 502 | device->first_minor = -1; |
| 503 | atomic_set(&device->ref_count, 1); |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 504 | INIT_DELAYED_WORK(&device->tape_dnr, tape_delayed_next_request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | return device; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Get a reference to an existing device structure. This will automatically |
| 511 | * increment the reference count. |
| 512 | */ |
| 513 | struct tape_device * |
| 514 | tape_get_device_reference(struct tape_device *device) |
| 515 | { |
| 516 | DBF_EVENT(4, "tape_get_device_reference(%p) = %i\n", device, |
| 517 | atomic_inc_return(&device->ref_count)); |
| 518 | |
| 519 | return device; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Decrease the reference counter of a devices structure. If the |
| 524 | * reference counter reaches zero free the device structure. |
| 525 | * The function returns a NULL pointer to be used by the caller |
| 526 | * for clearing reference pointers. |
| 527 | */ |
| 528 | struct tape_device * |
| 529 | tape_put_device(struct tape_device *device) |
| 530 | { |
| 531 | int remain; |
| 532 | |
| 533 | remain = atomic_dec_return(&device->ref_count); |
| 534 | if (remain > 0) { |
| 535 | DBF_EVENT(4, "tape_put_device(%p) -> %i\n", device, remain); |
| 536 | } else { |
| 537 | if (remain < 0) { |
| 538 | DBF_EVENT(4, "put device without reference\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } else { |
| 540 | DBF_EVENT(4, "tape_free_device(%p)\n", device); |
| 541 | kfree(device->modeset_byte); |
| 542 | kfree(device); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * Find tape device by a device index. |
| 551 | */ |
| 552 | struct tape_device * |
| 553 | tape_get_device(int devindex) |
| 554 | { |
| 555 | struct tape_device *device, *tmp; |
| 556 | |
| 557 | device = ERR_PTR(-ENODEV); |
| 558 | read_lock(&tape_device_lock); |
| 559 | list_for_each_entry(tmp, &tape_device_list, node) { |
| 560 | if (tmp->first_minor / TAPE_MINORS_PER_DEV == devindex) { |
| 561 | device = tape_get_device_reference(tmp); |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | read_unlock(&tape_device_lock); |
| 566 | return device; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Driverfs tape probe function. |
| 571 | */ |
| 572 | int |
| 573 | tape_generic_probe(struct ccw_device *cdev) |
| 574 | { |
| 575 | struct tape_device *device; |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 576 | int ret; |
Cornelia Huck | f455adc | 2008-05-15 16:52:37 +0200 | [diff] [blame] | 577 | struct ccw_dev_id dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | device = tape_alloc_device(); |
| 580 | if (IS_ERR(device)) |
| 581 | return -ENODEV; |
Peter Oberparleiter | 454e1fa | 2009-12-07 12:51:30 +0100 | [diff] [blame^] | 582 | ccw_device_set_options(cdev, CCWDEV_DO_PATHGROUP | |
| 583 | CCWDEV_DO_MULTIPATH); |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 584 | ret = sysfs_create_group(&cdev->dev.kobj, &tape_attr_group); |
| 585 | if (ret) { |
| 586 | tape_put_device(device); |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 587 | return ret; |
| 588 | } |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 589 | dev_set_drvdata(&cdev->dev, device); |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 590 | cdev->handler = __tape_do_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | device->cdev = cdev; |
Cornelia Huck | f455adc | 2008-05-15 16:52:37 +0200 | [diff] [blame] | 592 | ccw_device_get_id(cdev, &dev_id); |
| 593 | device->cdev_id = devid_to_int(&dev_id); |
Heiko Carstens | d7cf0d5 | 2006-07-18 13:46:58 +0200 | [diff] [blame] | 594 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 597 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | __tape_discard_requests(struct tape_device *device) |
| 599 | { |
| 600 | struct tape_request * request; |
| 601 | struct list_head * l, *n; |
| 602 | |
| 603 | list_for_each_safe(l, n, &device->req_queue) { |
| 604 | request = list_entry(l, struct tape_request, list); |
| 605 | if (request->status == TAPE_REQUEST_IN_IO) |
| 606 | request->status = TAPE_REQUEST_DONE; |
| 607 | list_del(&request->list); |
| 608 | |
| 609 | /* Decrease ref_count for removed request. */ |
| 610 | request->device = tape_put_device(device); |
| 611 | request->rc = -EIO; |
| 612 | if (request->callback != NULL) |
| 613 | request->callback(request, request->callback_data); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Driverfs tape remove function. |
| 619 | * |
| 620 | * This function is called whenever the common I/O layer detects the device |
| 621 | * gone. This can happen at any time and we cannot refuse. |
| 622 | */ |
| 623 | void |
| 624 | tape_generic_remove(struct ccw_device *cdev) |
| 625 | { |
| 626 | struct tape_device * device; |
| 627 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 628 | device = dev_get_drvdata(&cdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | if (!device) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | return; |
| 631 | } |
| 632 | DBF_LH(3, "(%08x): tape_generic_remove(%p)\n", device->cdev_id, cdev); |
| 633 | |
| 634 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 635 | switch (device->tape_state) { |
| 636 | case TS_INIT: |
| 637 | tape_state_set(device, TS_NOT_OPER); |
| 638 | case TS_NOT_OPER: |
| 639 | /* |
| 640 | * Nothing to do. |
| 641 | */ |
| 642 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 643 | break; |
| 644 | case TS_UNUSED: |
| 645 | /* |
| 646 | * Need only to release the device. |
| 647 | */ |
| 648 | tape_state_set(device, TS_NOT_OPER); |
| 649 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 650 | tape_cleanup_device(device); |
| 651 | break; |
| 652 | default: |
| 653 | /* |
| 654 | * There may be requests on the queue. We will not get |
| 655 | * an interrupt for a request that was running. So we |
| 656 | * just post them all as I/O errors. |
| 657 | */ |
| 658 | DBF_EVENT(3, "(%08x): Drive in use vanished!\n", |
| 659 | device->cdev_id); |
Michael Holzheu | 59e3692 | 2009-09-11 10:29:07 +0200 | [diff] [blame] | 660 | pr_warning("%s: A tape unit was detached while in " |
| 661 | "use\n", dev_name(&device->cdev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | tape_state_set(device, TS_NOT_OPER); |
| 663 | __tape_discard_requests(device); |
| 664 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 665 | tape_cleanup_device(device); |
| 666 | } |
| 667 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 668 | if (!dev_get_drvdata(&cdev->dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | sysfs_remove_group(&cdev->dev.kobj, &tape_attr_group); |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 670 | dev_set_drvdata(&cdev->dev, tape_put_device(dev_get_drvdata(&cdev->dev))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Allocate a new tape ccw request |
| 676 | */ |
| 677 | struct tape_request * |
| 678 | tape_alloc_request(int cplength, int datasize) |
| 679 | { |
| 680 | struct tape_request *request; |
| 681 | |
Stoyan Gaydarov | 6aa0d3a | 2009-03-26 15:24:47 +0100 | [diff] [blame] | 682 | BUG_ON(datasize > PAGE_SIZE || (cplength*sizeof(struct ccw1)) > PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
| 684 | DBF_LH(6, "tape_alloc_request(%d, %d)\n", cplength, datasize); |
| 685 | |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 686 | request = kzalloc(sizeof(struct tape_request), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | if (request == NULL) { |
| 688 | DBF_EXCEPTION(1, "cqra nomem\n"); |
| 689 | return ERR_PTR(-ENOMEM); |
| 690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | /* allocate channel program */ |
| 692 | if (cplength > 0) { |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 693 | request->cpaddr = kcalloc(cplength, sizeof(struct ccw1), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | GFP_ATOMIC | GFP_DMA); |
| 695 | if (request->cpaddr == NULL) { |
| 696 | DBF_EXCEPTION(1, "cqra nomem\n"); |
| 697 | kfree(request); |
| 698 | return ERR_PTR(-ENOMEM); |
| 699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
| 701 | /* alloc small kernel buffer */ |
| 702 | if (datasize > 0) { |
Eric Sesterhenn | 88abaab | 2006-03-24 03:15:31 -0800 | [diff] [blame] | 703 | request->cpdata = kzalloc(datasize, GFP_KERNEL | GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | if (request->cpdata == NULL) { |
| 705 | DBF_EXCEPTION(1, "cqra nomem\n"); |
Jesper Juhl | 17fd682 | 2005-11-07 01:01:30 -0800 | [diff] [blame] | 706 | kfree(request->cpaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | kfree(request); |
| 708 | return ERR_PTR(-ENOMEM); |
| 709 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
| 711 | DBF_LH(6, "New request %p(%p/%p)\n", request, request->cpaddr, |
| 712 | request->cpdata); |
| 713 | |
| 714 | return request; |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * Free tape ccw request |
| 719 | */ |
| 720 | void |
| 721 | tape_free_request (struct tape_request * request) |
| 722 | { |
| 723 | DBF_LH(6, "Free request %p\n", request); |
| 724 | |
| 725 | if (request->device != NULL) { |
| 726 | request->device = tape_put_device(request->device); |
| 727 | } |
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); |
| 842 | device->lb_timeout.data = (unsigned long) tape_put_device(device); |
| 843 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 844 | } |
| 845 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 846 | static void |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 847 | __tape_end_request( |
| 848 | struct tape_device * device, |
| 849 | struct tape_request * request, |
| 850 | int rc) |
| 851 | { |
| 852 | DBF_LH(6, "__tape_end_request(%p, %p, %i)\n", device, request, rc); |
| 853 | if (request) { |
| 854 | request->rc = rc; |
| 855 | request->status = TAPE_REQUEST_DONE; |
| 856 | |
| 857 | /* Remove from request queue. */ |
| 858 | list_del(&request->list); |
| 859 | |
| 860 | /* Do callback. */ |
| 861 | if (request->callback != NULL) |
| 862 | request->callback(request, request->callback_data); |
| 863 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
| 865 | /* Start next request. */ |
| 866 | if (!list_empty(&device->req_queue)) |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 867 | __tape_start_next_request(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | * Write sense data to dbf |
| 872 | */ |
| 873 | void |
| 874 | tape_dump_sense_dbf(struct tape_device *device, struct tape_request *request, |
| 875 | struct irb *irb) |
| 876 | { |
| 877 | unsigned int *sptr; |
| 878 | const char* op; |
| 879 | |
| 880 | if (request != NULL) |
| 881 | op = tape_op_verbose[request->op]; |
| 882 | else |
| 883 | op = "---"; |
| 884 | DBF_EVENT(3, "DSTAT : %02x CSTAT: %02x\n", |
Peter Oberparleiter | 23d805b | 2008-07-14 09:58:50 +0200 | [diff] [blame] | 885 | irb->scsw.cmd.dstat, irb->scsw.cmd.cstat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | DBF_EVENT(3, "DEVICE: %08x OP\t: %s\n", device->cdev_id, op); |
| 887 | sptr = (unsigned int *) irb->ecw; |
| 888 | DBF_EVENT(3, "%08x %08x\n", sptr[0], sptr[1]); |
| 889 | DBF_EVENT(3, "%08x %08x\n", sptr[2], sptr[3]); |
| 890 | DBF_EVENT(3, "%08x %08x\n", sptr[4], sptr[5]); |
| 891 | DBF_EVENT(3, "%08x %08x\n", sptr[6], sptr[7]); |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * I/O helper function. Adds the request to the request queue |
| 896 | * and starts it if the tape is idle. Has to be called with |
| 897 | * the device lock held. |
| 898 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 899 | static int |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 900 | __tape_start_request(struct tape_device *device, struct tape_request *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | { |
| 902 | int rc; |
| 903 | |
| 904 | switch (request->op) { |
| 905 | case TO_MSEN: |
| 906 | case TO_ASSIGN: |
| 907 | case TO_UNASSIGN: |
| 908 | case TO_READ_ATTMSG: |
Michael Holzheu | e296306 | 2007-05-04 18:47:53 +0200 | [diff] [blame] | 909 | case TO_RDC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | if (device->tape_state == TS_INIT) |
| 911 | break; |
| 912 | if (device->tape_state == TS_UNUSED) |
| 913 | break; |
| 914 | default: |
| 915 | if (device->tape_state == TS_BLKUSE) |
| 916 | break; |
| 917 | if (device->tape_state != TS_IN_USE) |
| 918 | return -ENODEV; |
| 919 | } |
| 920 | |
| 921 | /* Increase use count of device for the added request. */ |
| 922 | request->device = tape_get_device_reference(device); |
| 923 | |
| 924 | if (list_empty(&device->req_queue)) { |
| 925 | /* No other requests are on the queue. Start this one. */ |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 926 | rc = __tape_start_io(device, request); |
| 927 | if (rc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | return rc; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | DBF_LH(5, "Request %p added for execution.\n", request); |
| 931 | list_add(&request->list, &device->req_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } else { |
| 933 | DBF_LH(5, "Request %p add to queue.\n", request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | request->status = TAPE_REQUEST_QUEUED; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 935 | list_add_tail(&request->list, &device->req_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | } |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Add the request to the request queue, try to start it if the |
| 942 | * tape is idle. Return without waiting for end of i/o. |
| 943 | */ |
| 944 | int |
| 945 | tape_do_io_async(struct tape_device *device, struct tape_request *request) |
| 946 | { |
| 947 | int rc; |
| 948 | |
| 949 | DBF_LH(6, "tape_do_io_async(%p, %p)\n", device, request); |
| 950 | |
| 951 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 952 | /* Add request to request queue and try to start it. */ |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 953 | rc = __tape_start_request(device, request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 955 | return rc; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * tape_do_io/__tape_wake_up |
| 960 | * Add the request to the request queue, try to start it if the |
| 961 | * tape is idle and wait uninterruptible for its completion. |
| 962 | */ |
| 963 | static void |
| 964 | __tape_wake_up(struct tape_request *request, void *data) |
| 965 | { |
| 966 | request->callback = NULL; |
| 967 | wake_up((wait_queue_head_t *) data); |
| 968 | } |
| 969 | |
| 970 | int |
| 971 | tape_do_io(struct tape_device *device, struct tape_request *request) |
| 972 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | int rc; |
| 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 976 | /* Setup callback */ |
| 977 | request->callback = __tape_wake_up; |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 978 | request->callback_data = &device->wait_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | /* Add request to request queue and try to start it. */ |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 980 | rc = __tape_start_request(device, request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 982 | if (rc) |
| 983 | return rc; |
| 984 | /* Request added to the queue. Wait for its completion. */ |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 985 | wait_event(device->wait_queue, (request->callback == NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | /* Get rc from request */ |
| 987 | return request->rc; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * tape_do_io_interruptible/__tape_wake_up_interruptible |
| 992 | * Add the request to the request queue, try to start it if the |
| 993 | * tape is idle and wait uninterruptible for its completion. |
| 994 | */ |
| 995 | static void |
| 996 | __tape_wake_up_interruptible(struct tape_request *request, void *data) |
| 997 | { |
| 998 | request->callback = NULL; |
| 999 | wake_up_interruptible((wait_queue_head_t *) data); |
| 1000 | } |
| 1001 | |
| 1002 | int |
| 1003 | tape_do_io_interruptible(struct tape_device *device, |
| 1004 | struct tape_request *request) |
| 1005 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | int rc; |
| 1007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 1009 | /* Setup callback */ |
| 1010 | request->callback = __tape_wake_up_interruptible; |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 1011 | request->callback_data = &device->wait_queue; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1012 | rc = __tape_start_request(device, request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 1014 | if (rc) |
| 1015 | return rc; |
| 1016 | /* Request added to the queue. Wait for its completion. */ |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 1017 | rc = wait_event_interruptible(device->wait_queue, |
| 1018 | (request->callback == NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | if (rc != -ERESTARTSYS) |
| 1020 | /* Request finished normally. */ |
| 1021 | return request->rc; |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | /* Interrupted by a signal. We have to stop the current request. */ |
| 1024 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1025 | rc = __tape_cancel_io(device, request); |
| 1026 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | if (rc == 0) { |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1028 | /* Wait for the interrupt that acknowledges the halt. */ |
| 1029 | do { |
| 1030 | rc = wait_event_interruptible( |
Martin Schwidefsky | 4657fb8 | 2008-05-30 10:03:33 +0200 | [diff] [blame] | 1031 | device->wait_queue, |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1032 | (request->callback == NULL) |
| 1033 | ); |
Michael Holzheu | 4cd190a | 2006-03-24 03:15:27 -0800 | [diff] [blame] | 1034 | } while (rc == -ERESTARTSYS); |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | DBF_EVENT(3, "IO stopped on %08x\n", device->cdev_id); |
| 1037 | rc = -ERESTARTSYS; |
| 1038 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | return rc; |
| 1040 | } |
| 1041 | |
| 1042 | /* |
Michael Holzheu | 5f38433 | 2006-03-24 03:15:28 -0800 | [diff] [blame] | 1043 | * Stop running ccw. |
| 1044 | */ |
| 1045 | int |
| 1046 | tape_cancel_io(struct tape_device *device, struct tape_request *request) |
| 1047 | { |
| 1048 | int rc; |
| 1049 | |
| 1050 | spin_lock_irq(get_ccwdev_lock(device->cdev)); |
| 1051 | rc = __tape_cancel_io(device, request); |
| 1052 | spin_unlock_irq(get_ccwdev_lock(device->cdev)); |
| 1053 | return rc; |
| 1054 | } |
| 1055 | |
| 1056 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | * Tape interrupt routine, called from the ccw_device layer |
| 1058 | */ |
| 1059 | static void |
| 1060 | __tape_do_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb) |
| 1061 | { |
| 1062 | struct tape_device *device; |
| 1063 | struct tape_request *request; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | int rc; |
| 1065 | |
Greg Kroah-Hartman | dff59b6 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 1066 | device = dev_get_drvdata(&cdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | if (device == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | return; |
| 1069 | } |
| 1070 | request = (struct tape_request *) intparm; |
| 1071 | |
| 1072 | DBF_LH(6, "__tape_do_irq(device=%p, request=%p)\n", device, request); |
| 1073 | |
| 1074 | /* On special conditions irb is an error pointer */ |
| 1075 | if (IS_ERR(irb)) { |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1076 | /* FIXME: What to do with the request? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | switch (PTR_ERR(irb)) { |
| 1078 | case -ETIMEDOUT: |
Carsten Otte | ab640db | 2009-03-26 15:24:38 +0100 | [diff] [blame] | 1079 | DBF_LH(1, "(%s): Request timed out\n", |
Kay Sievers | 2a0217d | 2008-10-10 21:33:09 +0200 | [diff] [blame] | 1080 | dev_name(&cdev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | case -EIO: |
Stefan Bader | 4111796 | 2005-07-27 11:45:04 -0700 | [diff] [blame] | 1082 | __tape_end_request(device, request, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | break; |
| 1084 | default: |
Carsten Otte | ab640db | 2009-03-26 15:24:38 +0100 | [diff] [blame] | 1085 | DBF_LH(1, "(%s): Unexpected i/o error %li\n", |
Kay Sievers | 2a0217d | 2008-10-10 21:33:09 +0200 | [diff] [blame] | 1086 | dev_name(&cdev->dev), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | PTR_ERR(irb)); |
| 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)) { |
| 1121 | device->lb_timeout.data = (unsigned long) |
| 1122 | tape_put_device(device); |
| 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 = |
| 1177 | (unsigned long)tape_get_device_reference(device); |
| 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); |
| 1330 | EXPORT_SYMBOL(tape_get_device_reference); |
| 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); |