| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Character device driver for extended error reporting. |
| 4 |
* |
| 5 |
* Copyright IBM Corp. 2005 |
| 6 |
* extended error reporting for DASD ECKD devices |
| 7 |
* Author(s): Stefan Weinhuber <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/export.h> |
| 11 |
#include <linux/init.h> |
| 12 |
#include <linux/fs.h> |
| 13 |
#include <linux/kernel.h> |
| 14 |
#include <linux/miscdevice.h> |
| 15 |
#include <linux/module.h> |
| 16 |
#include <linux/moduleparam.h> |
| 17 |
#include <linux/device.h> |
| 18 |
#include <linux/poll.h> |
| 19 |
#include <linux/mutex.h> |
| 20 |
#include <linux/err.h> |
| 21 |
#include <linux/slab.h> |
| 22 |
|
| 23 |
#include <linux/uaccess.h> |
| 24 |
#include <linux/atomic.h> |
| 25 |
#include <asm/ebcdic.h> |
| 26 |
|
| 27 |
#include "dasd_int.h" |
| 28 |
#include "dasd_eckd.h" |
| 29 |
|
| 30 |
/* |
| 31 |
* SECTION: the internal buffer |
| 32 |
*/ |
| 33 |
|
| 34 |
/* |
| 35 |
* The internal buffer is meant to store obaque blobs of data, so it does |
| 36 |
* not know of higher level concepts like triggers. |
| 37 |
* It consists of a number of pages that are used as a ringbuffer. Each data |
| 38 |
* blob is stored in a simple record that consists of an integer, which |
| 39 |
* contains the size of the following data, and the data bytes themselfes. |
| 40 |
* |
| 41 |
* To allow for multiple independent readers we create one internal buffer |
| 42 |
* each time the device is opened and destroy the buffer when the file is |
| 43 |
* closed again. The number of pages used for this buffer is determined by |
| 44 |
* the module parmeter eer_pages. |
| 45 |
* |
| 46 |
* One record can be written to a buffer by using the functions |
| 47 |
* - dasd_eer_start_record (one time per record to write the size to the |
| 48 |
* buffer and reserve the space for the data) |
| 49 |
* - dasd_eer_write_buffer (one or more times per record to write the data) |
| 50 |
* The data can be written in several steps but you will have to compute |
| 51 |
* the total size up front for the invocation of dasd_eer_start_record. |
| 52 |
* If the ringbuffer is full, dasd_eer_start_record will remove the required |
| 53 |
* number of old records. |
| 54 |
* |
| 55 |
* A record is typically read in two steps, first read the integer that |
| 56 |
* specifies the size of the following data, then read the data. |
| 57 |
* Both can be done by |
| 58 |
* - dasd_eer_read_buffer |
| 59 |
* |
| 60 |
* For all mentioned functions you need to get the bufferlock first and keep |
| 61 |
* it until a complete record is written or read. |
| 62 |
* |
| 63 |
* All information necessary to keep track of an internal buffer is kept in |
| 64 |
* a struct eerbuffer. The buffer specific to a file pointer is strored in |
| 65 |
* the private_data field of that file. To be able to write data to all |
| 66 |
* existing buffers, each buffer is also added to the bufferlist. |
| 67 |
* If the user does not want to read a complete record in one go, we have to |
| 68 |
* keep track of the rest of the record. residual stores the number of bytes |
| 69 |
* that are still to deliver. If the rest of the record is invalidated between |
| 70 |
* two reads then residual will be set to -1 so that the next read will fail. |
| 71 |
* All entries in the eerbuffer structure are protected with the bufferlock. |
| 72 |
* To avoid races between writing to a buffer on the one side and creating |
| 73 |
* and destroying buffers on the other side, the bufferlock must also be used |
| 74 |
* to protect the bufferlist. |
| 75 |
*/ |
| 76 |
|
| 77 |
static int eer_pages = 5; |
| 78 |
module_param(eer_pages, int, S_IRUGO|S_IWUSR); |
| 79 |
|
| 80 |
struct eerbuffer { |
| 81 |
struct list_head list; |
| 82 |
char **buffer; |
| 83 |
int buffersize; |
| 84 |
int buffer_page_count; |
| 85 |
int head; |
| 86 |
int tail; |
| 87 |
int residual; |
| 88 |
}; |
| 89 |
|
| 90 |
static LIST_HEAD(bufferlist); |
| 91 |
static DEFINE_SPINLOCK(bufferlock); |
| 92 |
static DECLARE_WAIT_QUEUE_HEAD(dasd_eer_read_wait_queue); |
| 93 |
|
| 94 |
/* |
| 95 |
* How many free bytes are available on the buffer. |
| 96 |
* Needs to be called with bufferlock held. |
| 97 |
*/ |
| 98 |
static int dasd_eer_get_free_bytes(struct eerbuffer *eerb) |
| 99 |
{ |
| 100 |
if (eerb->head < eerb->tail) |
| 101 |
return eerb->tail - eerb->head - 1; |
| 102 |
return eerb->buffersize - eerb->head + eerb->tail -1; |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
* How many bytes of buffer space are used. |
| 107 |
* Needs to be called with bufferlock held. |
| 108 |
*/ |
| 109 |
static int dasd_eer_get_filled_bytes(struct eerbuffer *eerb) |
| 110 |
{ |
| 111 |
|
| 112 |
if (eerb->head >= eerb->tail) |
| 113 |
return eerb->head - eerb->tail; |
| 114 |
return eerb->buffersize - eerb->tail + eerb->head; |
| 115 |
} |
| 116 |
|
| 117 |
/* |
| 118 |
* The dasd_eer_write_buffer function just copies count bytes of data |
| 119 |
* to the buffer. Make sure to call dasd_eer_start_record first, to |
| 120 |
* make sure that enough free space is available. |
| 121 |
* Needs to be called with bufferlock held. |
| 122 |
*/ |
| 123 |
static void dasd_eer_write_buffer(struct eerbuffer *eerb, |
| 124 |
char *data, int count) |
| 125 |
{ |
| 126 |
|
| 127 |
unsigned long headindex,localhead; |
| 128 |
unsigned long rest, len; |
| 129 |
char *nextdata; |
| 130 |
|
| 131 |
nextdata = data; |
| 132 |
rest = count; |
| 133 |
while (rest > 0) { |
| 134 |
headindex = eerb->head / PAGE_SIZE; |
| 135 |
localhead = eerb->head % PAGE_SIZE; |
| 136 |
len = min(rest, PAGE_SIZE - localhead); |
| 137 |
memcpy(eerb->buffer[headindex]+localhead, nextdata, len); |
| 138 |
nextdata += len; |
| 139 |
rest -= len; |
| 140 |
eerb->head += len; |
| 141 |
if (eerb->head == eerb->buffersize) |
| 142 |
eerb->head = 0; /* wrap around */ |
| 143 |
BUG_ON(eerb->head > eerb->buffersize); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/* |
| 148 |
* Needs to be called with bufferlock held. |
| 149 |
*/ |
| 150 |
static int dasd_eer_read_buffer(struct eerbuffer *eerb, char *data, int count) |
| 151 |
{ |
| 152 |
|
| 153 |
unsigned long tailindex,localtail; |
| 154 |
unsigned long rest, len, finalcount; |
| 155 |
char *nextdata; |
| 156 |
|
| 157 |
finalcount = min(count, dasd_eer_get_filled_bytes(eerb)); |
| 158 |
nextdata = data; |
| 159 |
rest = finalcount; |
| 160 |
while (rest > 0) { |
| 161 |
tailindex = eerb->tail / PAGE_SIZE; |
| 162 |
localtail = eerb->tail % PAGE_SIZE; |
| 163 |
len = min(rest, PAGE_SIZE - localtail); |
| 164 |
memcpy(nextdata, eerb->buffer[tailindex] + localtail, len); |
| 165 |
nextdata += len; |
| 166 |
rest -= len; |
| 167 |
eerb->tail += len; |
| 168 |
if (eerb->tail == eerb->buffersize) |
| 169 |
eerb->tail = 0; /* wrap around */ |
| 170 |
BUG_ON(eerb->tail > eerb->buffersize); |
| 171 |
} |
| 172 |
return finalcount; |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* Whenever you want to write a blob of data to the internal buffer you |
| 177 |
* have to start by using this function first. It will write the number |
| 178 |
* of bytes that will be written to the buffer. If necessary it will remove |
| 179 |
* old records to make room for the new one. |
| 180 |
* Needs to be called with bufferlock held. |
| 181 |
*/ |
| 182 |
static int dasd_eer_start_record(struct eerbuffer *eerb, int count) |
| 183 |
{ |
| 184 |
int tailcount; |
| 185 |
|
| 186 |
if (count + sizeof(count) > eerb->buffersize) |
| 187 |
return -ENOMEM; |
| 188 |
while (dasd_eer_get_free_bytes(eerb) < count + sizeof(count)) { |
| 189 |
if (eerb->residual > 0) { |
| 190 |
eerb->tail += eerb->residual; |
| 191 |
if (eerb->tail >= eerb->buffersize) |
| 192 |
eerb->tail -= eerb->buffersize; |
| 193 |
eerb->residual = -1; |
| 194 |
} |
| 195 |
dasd_eer_read_buffer(eerb, (char *) &tailcount, |
| 196 |
sizeof(tailcount)); |
| 197 |
eerb->tail += tailcount; |
| 198 |
if (eerb->tail >= eerb->buffersize) |
| 199 |
eerb->tail -= eerb->buffersize; |
| 200 |
} |
| 201 |
dasd_eer_write_buffer(eerb, (char*) &count, sizeof(count)); |
| 202 |
|
| 203 |
return 0; |
| 204 |
}; |
| 205 |
|
| 206 |
/* |
| 207 |
* Release pages that are not used anymore. |
| 208 |
*/ |
| 209 |
static void dasd_eer_free_buffer_pages(char **buf, int no_pages) |
| 210 |
{ |
| 211 |
int i; |
| 212 |
|
| 213 |
for (i = 0; i < no_pages; i++) |
| 214 |
free_page((unsigned long) buf[i]); |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* Allocate a new set of memory pages. |
| 219 |
*/ |
| 220 |
static int dasd_eer_allocate_buffer_pages(char **buf, int no_pages) |
| 221 |
{ |
| 222 |
int i; |
| 223 |
|
| 224 |
for (i = 0; i < no_pages; i++) { |
| 225 |
buf[i] = (char *) get_zeroed_page(GFP_KERNEL); |
| 226 |
if (!buf[i]) { |
| 227 |
dasd_eer_free_buffer_pages(buf, i); |
| 228 |
return -ENOMEM; |
| 229 |
} |
| 230 |
} |
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
/* |
| 235 |
* SECTION: The extended error reporting functionality |
| 236 |
*/ |
| 237 |
|
| 238 |
/* |
| 239 |
* When a DASD device driver wants to report an error, it calls the |
| 240 |
* function dasd_eer_write and gives the respective trigger ID as |
| 241 |
* parameter. Currently there are four kinds of triggers: |
| 242 |
* |
| 243 |
* DASD_EER_FATALERROR: all kinds of unrecoverable I/O problems |
| 244 |
* DASD_EER_PPRCSUSPEND: PPRC was suspended |
| 245 |
* DASD_EER_NOPATH: There is no path to the device left. |
| 246 |
* DASD_EER_STATECHANGE: The state of the device has changed. |
| 247 |
* |
| 248 |
* For the first three triggers all required information can be supplied by |
| 249 |
* the caller. For these triggers a record is written by the function |
| 250 |
* dasd_eer_write_standard_trigger. |
| 251 |
* |
| 252 |
* The DASD_EER_STATECHANGE trigger is special since a sense subsystem |
| 253 |
* status ccw need to be executed to gather the necessary sense data first. |
| 254 |
* The dasd_eer_snss function will queue the SNSS request and the request |
| 255 |
* callback will then call dasd_eer_write with the DASD_EER_STATCHANGE |
| 256 |
* trigger. |
| 257 |
* |
| 258 |
* To avoid memory allocations at runtime, the necessary memory is allocated |
| 259 |
* when the extended error reporting is enabled for a device (by |
| 260 |
* dasd_eer_probe). There is one sense subsystem status request for each |
| 261 |
* eer enabled DASD device. The presence of the cqr in device->eer_cqr |
| 262 |
* indicates that eer is enable for the device. The use of the snss request |
| 263 |
* is protected by the DASD_FLAG_EER_IN_USE bit. When this flag indicates |
| 264 |
* that the cqr is currently in use, dasd_eer_snss cannot start a second |
| 265 |
* request but sets the DASD_FLAG_EER_SNSS flag instead. The callback of |
| 266 |
* the SNSS request will check the bit and call dasd_eer_snss again. |
| 267 |
*/ |
| 268 |
|
| 269 |
#define SNSS_DATA_SIZE 44 |
| 270 |
|
| 271 |
#define DASD_EER_BUSID_SIZE 10 |
| 272 |
struct dasd_eer_header { |
| 273 |
__u32 total_size; |
| 274 |
__u32 trigger; |
| 275 |
__u64 tv_sec; |
| 276 |
__u64 tv_usec; |
| 277 |
char busid[DASD_EER_BUSID_SIZE]; |
| 278 |
} __attribute__ ((packed)); |
| 279 |
|
| 280 |
/* |
| 281 |
* The following function can be used for those triggers that have |
| 282 |
* all necessary data available when the function is called. |
| 283 |
* If the parameter cqr is not NULL, the chain of requests will be searched |
| 284 |
* for valid sense data, and all valid sense data sets will be added to |
| 285 |
* the triggers data. |
| 286 |
*/ |
| 287 |
static void dasd_eer_write_standard_trigger(struct dasd_device *device, |
| 288 |
struct dasd_ccw_req *cqr, |
| 289 |
int trigger) |
| 290 |
{ |
| 291 |
struct dasd_ccw_req *temp_cqr; |
| 292 |
int data_size; |
| 293 |
struct timespec64 ts; |
| 294 |
struct dasd_eer_header header; |
| 295 |
unsigned long flags; |
| 296 |
struct eerbuffer *eerb; |
| 297 |
char *sense; |
| 298 |
|
| 299 |
/* go through cqr chain and count the valid sense data sets */ |
| 300 |
data_size = 0; |
| 301 |
for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers) |
| 302 |
if (dasd_get_sense(&temp_cqr->irb)) |
| 303 |
data_size += 32; |
| 304 |
|
| 305 |
header.total_size = sizeof(header) + data_size + 4; /* "EOR" */ |
| 306 |
header.trigger = trigger; |
| 307 |
ktime_get_real_ts64(&ts); |
| 308 |
header.tv_sec = ts.tv_sec; |
| 309 |
header.tv_usec = ts.tv_nsec / NSEC_PER_USEC; |
| 310 |
strscpy(header.busid, dev_name(&device->cdev->dev), |
| 311 |
DASD_EER_BUSID_SIZE); |
| 312 |
|
| 313 |
spin_lock_irqsave(&bufferlock, flags); |
| 314 |
list_for_each_entry(eerb, &bufferlist, list) { |
| 315 |
dasd_eer_start_record(eerb, header.total_size); |
| 316 |
dasd_eer_write_buffer(eerb, (char *) &header, sizeof(header)); |
| 317 |
for (temp_cqr = cqr; temp_cqr; temp_cqr = temp_cqr->refers) { |
| 318 |
sense = dasd_get_sense(&temp_cqr->irb); |
| 319 |
if (sense) |
| 320 |
dasd_eer_write_buffer(eerb, sense, 32); |
| 321 |
} |
| 322 |
dasd_eer_write_buffer(eerb, "EOR", 4); |
| 323 |
} |
| 324 |
spin_unlock_irqrestore(&bufferlock, flags); |
| 325 |
wake_up_interruptible(&dasd_eer_read_wait_queue); |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
* This function writes a DASD_EER_STATECHANGE trigger. |
| 330 |
*/ |
| 331 |
static void dasd_eer_write_snss_trigger(struct dasd_device *device, |
| 332 |
struct dasd_ccw_req *cqr, |
| 333 |
int trigger) |
| 334 |
{ |
| 335 |
int data_size; |
| 336 |
int snss_rc; |
| 337 |
struct timespec64 ts; |
| 338 |
struct dasd_eer_header header; |
| 339 |
unsigned long flags; |
| 340 |
struct eerbuffer *eerb; |
| 341 |
|
| 342 |
snss_rc = (cqr->status == DASD_CQR_DONE) ? 0 : -EIO; |
| 343 |
if (snss_rc) |
| 344 |
data_size = 0; |
| 345 |
else |
| 346 |
data_size = SNSS_DATA_SIZE; |
| 347 |
|
| 348 |
header.total_size = sizeof(header) + data_size + 4; /* "EOR" */ |
| 349 |
header.trigger = DASD_EER_STATECHANGE; |
| 350 |
ktime_get_real_ts64(&ts); |
| 351 |
header.tv_sec = ts.tv_sec; |
| 352 |
header.tv_usec = ts.tv_nsec / NSEC_PER_USEC; |
| 353 |
strscpy(header.busid, dev_name(&device->cdev->dev), |
| 354 |
DASD_EER_BUSID_SIZE); |
| 355 |
|
| 356 |
spin_lock_irqsave(&bufferlock, flags); |
| 357 |
list_for_each_entry(eerb, &bufferlist, list) { |
| 358 |
dasd_eer_start_record(eerb, header.total_size); |
| 359 |
dasd_eer_write_buffer(eerb, (char *) &header , sizeof(header)); |
| 360 |
if (!snss_rc) |
| 361 |
dasd_eer_write_buffer(eerb, cqr->data, SNSS_DATA_SIZE); |
| 362 |
dasd_eer_write_buffer(eerb, "EOR", 4); |
| 363 |
} |
| 364 |
spin_unlock_irqrestore(&bufferlock, flags); |
| 365 |
wake_up_interruptible(&dasd_eer_read_wait_queue); |
| 366 |
} |
| 367 |
|
| 368 |
/* |
| 369 |
* This function is called for all triggers. It calls the appropriate |
| 370 |
* function that writes the actual trigger records. |
| 371 |
*/ |
| 372 |
void dasd_eer_write(struct dasd_device *device, struct dasd_ccw_req *cqr, |
| 373 |
unsigned int id) |
| 374 |
{ |
| 375 |
if (!device->eer_cqr) |
| 376 |
return; |
| 377 |
switch (id) { |
| 378 |
case DASD_EER_FATALERROR: |
| 379 |
case DASD_EER_PPRCSUSPEND: |
| 380 |
dasd_eer_write_standard_trigger(device, cqr, id); |
| 381 |
break; |
| 382 |
case DASD_EER_NOPATH: |
| 383 |
case DASD_EER_NOSPC: |
| 384 |
case DASD_EER_AUTOQUIESCE: |
| 385 |
dasd_eer_write_standard_trigger(device, NULL, id); |
| 386 |
break; |
| 387 |
case DASD_EER_STATECHANGE: |
| 388 |
dasd_eer_write_snss_trigger(device, cqr, id); |
| 389 |
break; |
| 390 |
default: /* unknown trigger, so we write it without any sense data */ |
| 391 |
dasd_eer_write_standard_trigger(device, NULL, id); |
| 392 |
break; |
| 393 |
} |
| 394 |
} |
| 395 |
EXPORT_SYMBOL(dasd_eer_write); |
| 396 |
|
| 397 |
/* |
| 398 |
* Start a sense subsystem status request. |
| 399 |
* Needs to be called with the device held. |
| 400 |
*/ |
| 401 |
void dasd_eer_snss(struct dasd_device *device) |
| 402 |
{ |
| 403 |
struct dasd_ccw_req *cqr; |
| 404 |
|
| 405 |
cqr = device->eer_cqr; |
| 406 |
if (!cqr) /* Device not eer enabled. */ |
| 407 |
return; |
| 408 |
if (test_and_set_bit(DASD_FLAG_EER_IN_USE, &device->flags)) { |
| 409 |
/* Sense subsystem status request in use. */ |
| 410 |
set_bit(DASD_FLAG_EER_SNSS, &device->flags); |
| 411 |
return; |
| 412 |
} |
| 413 |
/* cdev is already locked, can't use dasd_add_request_head */ |
| 414 |
clear_bit(DASD_FLAG_EER_SNSS, &device->flags); |
| 415 |
cqr->status = DASD_CQR_QUEUED; |
| 416 |
list_add(&cqr->devlist, &device->ccw_queue); |
| 417 |
dasd_schedule_device_bh(device); |
| 418 |
} |
| 419 |
|
| 420 |
/* |
| 421 |
* Callback function for use with sense subsystem status request. |
| 422 |
*/ |
| 423 |
static void dasd_eer_snss_cb(struct dasd_ccw_req *cqr, void *data) |
| 424 |
{ |
| 425 |
struct dasd_device *device = cqr->startdev; |
| 426 |
unsigned long flags; |
| 427 |
|
| 428 |
dasd_eer_write(device, cqr, DASD_EER_STATECHANGE); |
| 429 |
spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags); |
| 430 |
if (device->eer_cqr == cqr) { |
| 431 |
clear_bit(DASD_FLAG_EER_IN_USE, &device->flags); |
| 432 |
if (test_bit(DASD_FLAG_EER_SNSS, &device->flags)) |
| 433 |
/* Another SNSS has been requested in the meantime. */ |
| 434 |
dasd_eer_snss(device); |
| 435 |
cqr = NULL; |
| 436 |
} |
| 437 |
spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags); |
| 438 |
if (cqr) |
| 439 |
/* |
| 440 |
* Extended error recovery has been switched off while |
| 441 |
* the SNSS request was running. It could even have |
| 442 |
* been switched off and on again in which case there |
| 443 |
* is a new ccw in device->eer_cqr. Free the "old" |
| 444 |
* snss request now. |
| 445 |
*/ |
| 446 |
dasd_sfree_request(cqr, device); |
| 447 |
} |
| 448 |
|
| 449 |
/* |
| 450 |
* Enable error reporting on a given device. |
| 451 |
*/ |
| 452 |
int dasd_eer_enable(struct dasd_device *device) |
| 453 |
{ |
| 454 |
struct dasd_ccw_req *cqr = NULL; |
| 455 |
unsigned long flags; |
| 456 |
struct ccw1 *ccw; |
| 457 |
int rc = 0; |
| 458 |
|
| 459 |
spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags); |
| 460 |
if (device->eer_cqr) |
| 461 |
goto out; |
| 462 |
else if (!device->discipline || |
| 463 |
strcmp(device->discipline->name, "ECKD")) |
| 464 |
rc = -EMEDIUMTYPE; |
| 465 |
else if (test_bit(DASD_FLAG_OFFLINE, &device->flags)) |
| 466 |
rc = -EBUSY; |
| 467 |
|
| 468 |
if (rc) |
| 469 |
goto out; |
| 470 |
|
| 471 |
cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* SNSS */, |
| 472 |
SNSS_DATA_SIZE, device, NULL); |
| 473 |
if (IS_ERR(cqr)) { |
| 474 |
rc = -ENOMEM; |
| 475 |
cqr = NULL; |
| 476 |
goto out; |
| 477 |
} |
| 478 |
|
| 479 |
cqr->startdev = device; |
| 480 |
cqr->retries = 255; |
| 481 |
cqr->expires = 10 * HZ; |
| 482 |
clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags); |
| 483 |
set_bit(DASD_CQR_ALLOW_SLOCK, &cqr->flags); |
| 484 |
|
| 485 |
ccw = cqr->cpaddr; |
| 486 |
ccw->cmd_code = DASD_ECKD_CCW_SNSS; |
| 487 |
ccw->count = SNSS_DATA_SIZE; |
| 488 |
ccw->flags = 0; |
| 489 |
ccw->cda = virt_to_dma32(cqr->data); |
| 490 |
|
| 491 |
cqr->buildclk = get_tod_clock(); |
| 492 |
cqr->status = DASD_CQR_FILLED; |
| 493 |
cqr->callback = dasd_eer_snss_cb; |
| 494 |
|
| 495 |
if (!device->eer_cqr) { |
| 496 |
device->eer_cqr = cqr; |
| 497 |
cqr = NULL; |
| 498 |
} |
| 499 |
|
| 500 |
out: |
| 501 |
spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags); |
| 502 |
|
| 503 |
if (cqr) |
| 504 |
dasd_sfree_request(cqr, device); |
| 505 |
|
| 506 |
return rc; |
| 507 |
} |
| 508 |
|
| 509 |
/* |
| 510 |
* Disable error reporting on a given device. |
| 511 |
*/ |
| 512 |
void dasd_eer_disable(struct dasd_device *device) |
| 513 |
{ |
| 514 |
struct dasd_ccw_req *cqr; |
| 515 |
unsigned long flags; |
| 516 |
int in_use; |
| 517 |
|
| 518 |
if (!device->eer_cqr) |
| 519 |
return; |
| 520 |
spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags); |
| 521 |
cqr = device->eer_cqr; |
| 522 |
device->eer_cqr = NULL; |
| 523 |
clear_bit(DASD_FLAG_EER_SNSS, &device->flags); |
| 524 |
in_use = test_and_clear_bit(DASD_FLAG_EER_IN_USE, &device->flags); |
| 525 |
spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags); |
| 526 |
if (cqr && !in_use) |
| 527 |
dasd_sfree_request(cqr, device); |
| 528 |
} |
| 529 |
|
| 530 |
/* |
| 531 |
* SECTION: the device operations |
| 532 |
*/ |
| 533 |
|
| 534 |
/* |
| 535 |
* On the one side we need a lock to access our internal buffer, on the |
| 536 |
* other side a copy_to_user can sleep. So we need to copy the data we have |
| 537 |
* to transfer in a readbuffer, which is protected by the readbuffer_mutex. |
| 538 |
*/ |
| 539 |
static char readbuffer[PAGE_SIZE]; |
| 540 |
static DEFINE_MUTEX(readbuffer_mutex); |
| 541 |
|
| 542 |
static int dasd_eer_open(struct inode *inp, struct file *filp) |
| 543 |
{ |
| 544 |
struct eerbuffer *eerb; |
| 545 |
unsigned long flags; |
| 546 |
|
| 547 |
eerb = kzalloc_obj(struct eerbuffer); |
| 548 |
if (!eerb) |
| 549 |
return -ENOMEM; |
| 550 |
eerb->buffer_page_count = eer_pages; |
| 551 |
if (eerb->buffer_page_count < 1 || |
| 552 |
eerb->buffer_page_count > INT_MAX / PAGE_SIZE) { |
| 553 |
kfree(eerb); |
| 554 |
DBF_EVENT(DBF_WARNING, "can't open device since module " |
| 555 |
"parameter eer_pages is smaller than 1 or" |
| 556 |
" bigger than %d", (int)(INT_MAX / PAGE_SIZE)); |
| 557 |
return -EINVAL; |
| 558 |
} |
| 559 |
eerb->buffersize = eerb->buffer_page_count * PAGE_SIZE; |
| 560 |
eerb->buffer = kmalloc_array(eerb->buffer_page_count, sizeof(char *), |
| 561 |
GFP_KERNEL); |
| 562 |
if (!eerb->buffer) { |
| 563 |
kfree(eerb); |
| 564 |
return -ENOMEM; |
| 565 |
} |
| 566 |
if (dasd_eer_allocate_buffer_pages(eerb->buffer, |
| 567 |
eerb->buffer_page_count)) { |
| 568 |
kfree(eerb->buffer); |
| 569 |
kfree(eerb); |
| 570 |
return -ENOMEM; |
| 571 |
} |
| 572 |
filp->private_data = eerb; |
| 573 |
spin_lock_irqsave(&bufferlock, flags); |
| 574 |
list_add(&eerb->list, &bufferlist); |
| 575 |
spin_unlock_irqrestore(&bufferlock, flags); |
| 576 |
|
| 577 |
return nonseekable_open(inp,filp); |
| 578 |
} |
| 579 |
|
| 580 |
static int dasd_eer_close(struct inode *inp, struct file *filp) |
| 581 |
{ |
| 582 |
struct eerbuffer *eerb; |
| 583 |
unsigned long flags; |
| 584 |
|
| 585 |
eerb = (struct eerbuffer *) filp->private_data; |
| 586 |
spin_lock_irqsave(&bufferlock, flags); |
| 587 |
list_del(&eerb->list); |
| 588 |
spin_unlock_irqrestore(&bufferlock, flags); |
| 589 |
dasd_eer_free_buffer_pages(eerb->buffer, eerb->buffer_page_count); |
| 590 |
kfree(eerb->buffer); |
| 591 |
kfree(eerb); |
| 592 |
|
| 593 |
return 0; |
| 594 |
} |
| 595 |
|
| 596 |
static ssize_t dasd_eer_read(struct file *filp, char __user *buf, |
| 597 |
size_t count, loff_t *ppos) |
| 598 |
{ |
| 599 |
int tc,rc; |
| 600 |
int tailcount,effective_count; |
| 601 |
unsigned long flags; |
| 602 |
struct eerbuffer *eerb; |
| 603 |
|
| 604 |
eerb = (struct eerbuffer *) filp->private_data; |
| 605 |
if (mutex_lock_interruptible(&readbuffer_mutex)) |
| 606 |
return -ERESTARTSYS; |
| 607 |
|
| 608 |
spin_lock_irqsave(&bufferlock, flags); |
| 609 |
|
| 610 |
if (eerb->residual < 0) { /* the remainder of this record */ |
| 611 |
/* has been deleted */ |
| 612 |
eerb->residual = 0; |
| 613 |
spin_unlock_irqrestore(&bufferlock, flags); |
| 614 |
mutex_unlock(&readbuffer_mutex); |
| 615 |
return -EIO; |
| 616 |
} else if (eerb->residual > 0) { |
| 617 |
/* OK we still have a second half of a record to deliver */ |
| 618 |
effective_count = min(eerb->residual, (int) count); |
| 619 |
eerb->residual -= effective_count; |
| 620 |
} else { |
| 621 |
tc = 0; |
| 622 |
while (!tc) { |
| 623 |
tc = dasd_eer_read_buffer(eerb, (char *) &tailcount, |
| 624 |
sizeof(tailcount)); |
| 625 |
if (!tc) { |
| 626 |
/* no data available */ |
| 627 |
spin_unlock_irqrestore(&bufferlock, flags); |
| 628 |
mutex_unlock(&readbuffer_mutex); |
| 629 |
if (filp->f_flags & O_NONBLOCK) |
| 630 |
return -EAGAIN; |
| 631 |
rc = wait_event_interruptible( |
| 632 |
dasd_eer_read_wait_queue, |
| 633 |
eerb->head != eerb->tail); |
| 634 |
if (rc) |
| 635 |
return rc; |
| 636 |
if (mutex_lock_interruptible(&readbuffer_mutex)) |
| 637 |
return -ERESTARTSYS; |
| 638 |
spin_lock_irqsave(&bufferlock, flags); |
| 639 |
} |
| 640 |
} |
| 641 |
WARN_ON(tc != sizeof(tailcount)); |
| 642 |
effective_count = min(tailcount,(int)count); |
| 643 |
eerb->residual = tailcount - effective_count; |
| 644 |
} |
| 645 |
|
| 646 |
tc = dasd_eer_read_buffer(eerb, readbuffer, effective_count); |
| 647 |
WARN_ON(tc != effective_count); |
| 648 |
|
| 649 |
spin_unlock_irqrestore(&bufferlock, flags); |
| 650 |
|
| 651 |
if (copy_to_user(buf, readbuffer, effective_count)) { |
| 652 |
mutex_unlock(&readbuffer_mutex); |
| 653 |
return -EFAULT; |
| 654 |
} |
| 655 |
|
| 656 |
mutex_unlock(&readbuffer_mutex); |
| 657 |
return effective_count; |
| 658 |
} |
| 659 |
|
| 660 |
static __poll_t dasd_eer_poll(struct file *filp, poll_table *ptable) |
| 661 |
{ |
| 662 |
__poll_t mask; |
| 663 |
unsigned long flags; |
| 664 |
struct eerbuffer *eerb; |
| 665 |
|
| 666 |
eerb = (struct eerbuffer *) filp->private_data; |
| 667 |
poll_wait(filp, &dasd_eer_read_wait_queue, ptable); |
| 668 |
spin_lock_irqsave(&bufferlock, flags); |
| 669 |
if (eerb->head != eerb->tail) |
| 670 |
mask = EPOLLIN | EPOLLRDNORM ; |
| 671 |
else |
| 672 |
mask = 0; |
| 673 |
spin_unlock_irqrestore(&bufferlock, flags); |
| 674 |
return mask; |
| 675 |
} |
| 676 |
|
| 677 |
static const struct file_operations dasd_eer_fops = { |
| 678 |
.open = &dasd_eer_open, |
| 679 |
.release = &dasd_eer_close, |
| 680 |
.read = &dasd_eer_read, |
| 681 |
.poll = &dasd_eer_poll, |
| 682 |
.owner = THIS_MODULE, |
| 683 |
.llseek = noop_llseek, |
| 684 |
}; |
| 685 |
|
| 686 |
static struct miscdevice *dasd_eer_dev = NULL; |
| 687 |
|
| 688 |
int __init dasd_eer_init(void) |
| 689 |
{ |
| 690 |
int rc; |
| 691 |
|
| 692 |
dasd_eer_dev = kzalloc_obj(*dasd_eer_dev); |
| 693 |
if (!dasd_eer_dev) |
| 694 |
return -ENOMEM; |
| 695 |
|
| 696 |
dasd_eer_dev->minor = MISC_DYNAMIC_MINOR; |
| 697 |
dasd_eer_dev->name = "dasd_eer"; |
| 698 |
dasd_eer_dev->fops = &dasd_eer_fops; |
| 699 |
|
| 700 |
rc = misc_register(dasd_eer_dev); |
| 701 |
if (rc) { |
| 702 |
kfree(dasd_eer_dev); |
| 703 |
dasd_eer_dev = NULL; |
| 704 |
DBF_EVENT(DBF_ERR, "%s", "dasd_eer_init could not " |
| 705 |
"register misc device"); |
| 706 |
return rc; |
| 707 |
} |
| 708 |
|
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
void dasd_eer_exit(void) |
| 713 |
{ |
| 714 |
if (dasd_eer_dev) { |
| 715 |
misc_deregister(dasd_eer_dev); |
| 716 |
kfree(dasd_eer_dev); |
| 717 |
dasd_eer_dev = NULL; |
| 718 |
} |
| 719 |
} |