Merge git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[deliverable/linux.git] / drivers / s390 / block / dasd_fba.c
1 /*
2 * File...........: linux/drivers/s390/block/dasd_fba.c
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Bugreports.to..: <Linux390@de.ibm.com>
5 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999,2000
6 *
7 */
8
9 #include <linux/config.h>
10 #include <linux/stddef.h>
11 #include <linux/kernel.h>
12 #include <asm/debug.h>
13
14 #include <linux/slab.h>
15 #include <linux/hdreg.h> /* HDIO_GETGEO */
16 #include <linux/bio.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19
20 #include <asm/idals.h>
21 #include <asm/ebcdic.h>
22 #include <asm/io.h>
23 #include <asm/todclk.h>
24 #include <asm/ccwdev.h>
25
26 #include "dasd_int.h"
27 #include "dasd_fba.h"
28
29 #ifdef PRINTK_HEADER
30 #undef PRINTK_HEADER
31 #endif /* PRINTK_HEADER */
32 #define PRINTK_HEADER "dasd(fba):"
33
34 #define DASD_FBA_CCW_WRITE 0x41
35 #define DASD_FBA_CCW_READ 0x42
36 #define DASD_FBA_CCW_LOCATE 0x43
37 #define DASD_FBA_CCW_DEFINE_EXTENT 0x63
38
39 MODULE_LICENSE("GPL");
40
41 static struct dasd_discipline dasd_fba_discipline;
42
43 struct dasd_fba_private {
44 struct dasd_fba_characteristics rdc_data;
45 };
46
47 static struct ccw_device_id dasd_fba_ids[] = {
48 { CCW_DEVICE_DEVTYPE (0x6310, 0, 0x9336, 0), driver_info: 0x1},
49 { CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3370, 0), driver_info: 0x2},
50 { /* end of list */ },
51 };
52
53 MODULE_DEVICE_TABLE(ccw, dasd_fba_ids);
54
55 static struct ccw_driver dasd_fba_driver; /* see below */
56 static int
57 dasd_fba_probe(struct ccw_device *cdev)
58 {
59 return dasd_generic_probe(cdev, &dasd_fba_discipline);
60 }
61
62 static int
63 dasd_fba_set_online(struct ccw_device *cdev)
64 {
65 return dasd_generic_set_online(cdev, &dasd_fba_discipline);
66 }
67
68 static struct ccw_driver dasd_fba_driver = {
69 .name = "dasd-fba",
70 .owner = THIS_MODULE,
71 .ids = dasd_fba_ids,
72 .probe = dasd_fba_probe,
73 .remove = dasd_generic_remove,
74 .set_offline = dasd_generic_set_offline,
75 .set_online = dasd_fba_set_online,
76 .notify = dasd_generic_notify,
77 };
78
79 static inline void
80 define_extent(struct ccw1 * ccw, struct DE_fba_data *data, int rw,
81 int blksize, int beg, int nr)
82 {
83 ccw->cmd_code = DASD_FBA_CCW_DEFINE_EXTENT;
84 ccw->flags = 0;
85 ccw->count = 16;
86 ccw->cda = (__u32) __pa(data);
87 memset(data, 0, sizeof (struct DE_fba_data));
88 if (rw == WRITE)
89 (data->mask).perm = 0x0;
90 else if (rw == READ)
91 (data->mask).perm = 0x1;
92 else
93 data->mask.perm = 0x2;
94 data->blk_size = blksize;
95 data->ext_loc = beg;
96 data->ext_end = nr - 1;
97 }
98
99 static inline void
100 locate_record(struct ccw1 * ccw, struct LO_fba_data *data, int rw,
101 int block_nr, int block_ct)
102 {
103 ccw->cmd_code = DASD_FBA_CCW_LOCATE;
104 ccw->flags = 0;
105 ccw->count = 8;
106 ccw->cda = (__u32) __pa(data);
107 memset(data, 0, sizeof (struct LO_fba_data));
108 if (rw == WRITE)
109 data->operation.cmd = 0x5;
110 else if (rw == READ)
111 data->operation.cmd = 0x6;
112 else
113 data->operation.cmd = 0x8;
114 data->blk_nr = block_nr;
115 data->blk_ct = block_ct;
116 }
117
118 static int
119 dasd_fba_check_characteristics(struct dasd_device *device)
120 {
121 struct dasd_fba_private *private;
122 struct ccw_device *cdev = device->cdev;
123 void *rdc_data;
124 int rc;
125
126 private = (struct dasd_fba_private *) device->private;
127 if (private == NULL) {
128 private = kzalloc(sizeof(struct dasd_fba_private), GFP_KERNEL);
129 if (private == NULL) {
130 DEV_MESSAGE(KERN_WARNING, device, "%s",
131 "memory allocation failed for private "
132 "data");
133 return -ENOMEM;
134 }
135 device->private = (void *) private;
136 }
137 /* Read Device Characteristics */
138 rdc_data = (void *) &(private->rdc_data);
139 rc = read_dev_chars(device->cdev, &rdc_data, 32);
140 if (rc) {
141 DEV_MESSAGE(KERN_WARNING, device,
142 "Read device characteristics returned error %d",
143 rc);
144 return rc;
145 }
146
147 DEV_MESSAGE(KERN_INFO, device,
148 "%04X/%02X(CU:%04X/%02X) %dMB at(%d B/blk)",
149 cdev->id.dev_type,
150 cdev->id.dev_model,
151 cdev->id.cu_type,
152 cdev->id.cu_model,
153 ((private->rdc_data.blk_bdsa *
154 (private->rdc_data.blk_size >> 9)) >> 11),
155 private->rdc_data.blk_size);
156 return 0;
157 }
158
159 static int
160 dasd_fba_do_analysis(struct dasd_device *device)
161 {
162 struct dasd_fba_private *private;
163 int sb, rc;
164
165 private = (struct dasd_fba_private *) device->private;
166 rc = dasd_check_blocksize(private->rdc_data.blk_size);
167 if (rc) {
168 DEV_MESSAGE(KERN_INFO, device, "unknown blocksize %d",
169 private->rdc_data.blk_size);
170 return rc;
171 }
172 device->blocks = private->rdc_data.blk_bdsa;
173 device->bp_block = private->rdc_data.blk_size;
174 device->s2b_shift = 0; /* bits to shift 512 to get a block */
175 for (sb = 512; sb < private->rdc_data.blk_size; sb = sb << 1)
176 device->s2b_shift++;
177 return 0;
178 }
179
180 static int
181 dasd_fba_fill_geometry(struct dasd_device *device, struct hd_geometry *geo)
182 {
183 if (dasd_check_blocksize(device->bp_block) != 0)
184 return -EINVAL;
185 geo->cylinders = (device->blocks << device->s2b_shift) >> 10;
186 geo->heads = 16;
187 geo->sectors = 128 >> device->s2b_shift;
188 return 0;
189 }
190
191 static dasd_era_t
192 dasd_fba_examine_error(struct dasd_ccw_req * cqr, struct irb * irb)
193 {
194 struct dasd_device *device;
195 struct ccw_device *cdev;
196
197 device = (struct dasd_device *) cqr->device;
198 if (irb->scsw.cstat == 0x00 &&
199 irb->scsw.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END))
200 return dasd_era_none;
201
202 cdev = device->cdev;
203 switch (cdev->id.dev_type) {
204 case 0x3370:
205 return dasd_3370_erp_examine(cqr, irb);
206 case 0x9336:
207 return dasd_9336_erp_examine(cqr, irb);
208 default:
209 return dasd_era_recover;
210 }
211 }
212
213 static dasd_erp_fn_t
214 dasd_fba_erp_action(struct dasd_ccw_req * cqr)
215 {
216 return dasd_default_erp_action;
217 }
218
219 static dasd_erp_fn_t
220 dasd_fba_erp_postaction(struct dasd_ccw_req * cqr)
221 {
222 if (cqr->function == dasd_default_erp_action)
223 return dasd_default_erp_postaction;
224
225 DEV_MESSAGE(KERN_WARNING, cqr->device, "unknown ERP action %p",
226 cqr->function);
227 return NULL;
228 }
229
230 static struct dasd_ccw_req *
231 dasd_fba_build_cp(struct dasd_device * device, struct request *req)
232 {
233 struct dasd_fba_private *private;
234 unsigned long *idaws;
235 struct LO_fba_data *LO_data;
236 struct dasd_ccw_req *cqr;
237 struct ccw1 *ccw;
238 struct bio *bio;
239 struct bio_vec *bv;
240 char *dst;
241 int count, cidaw, cplength, datasize;
242 sector_t recid, first_rec, last_rec;
243 unsigned int blksize, off;
244 unsigned char cmd;
245 int i;
246
247 private = (struct dasd_fba_private *) device->private;
248 if (rq_data_dir(req) == READ) {
249 cmd = DASD_FBA_CCW_READ;
250 } else if (rq_data_dir(req) == WRITE) {
251 cmd = DASD_FBA_CCW_WRITE;
252 } else
253 return ERR_PTR(-EINVAL);
254 blksize = device->bp_block;
255 /* Calculate record id of first and last block. */
256 first_rec = req->sector >> device->s2b_shift;
257 last_rec = (req->sector + req->nr_sectors - 1) >> device->s2b_shift;
258 /* Check struct bio and count the number of blocks for the request. */
259 count = 0;
260 cidaw = 0;
261 rq_for_each_bio(bio, req) {
262 bio_for_each_segment(bv, bio, i) {
263 if (bv->bv_len & (blksize - 1))
264 /* Fba can only do full blocks. */
265 return ERR_PTR(-EINVAL);
266 count += bv->bv_len >> (device->s2b_shift + 9);
267 #if defined(CONFIG_64BIT)
268 if (idal_is_needed (page_address(bv->bv_page),
269 bv->bv_len))
270 cidaw += bv->bv_len / blksize;
271 #endif
272 }
273 }
274 /* Paranoia. */
275 if (count != last_rec - first_rec + 1)
276 return ERR_PTR(-EINVAL);
277 /* 1x define extent + 1x locate record + number of blocks */
278 cplength = 2 + count;
279 /* 1x define extent + 1x locate record */
280 datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) +
281 cidaw * sizeof(unsigned long);
282 /*
283 * Find out number of additional locate record ccws if the device
284 * can't do data chaining.
285 */
286 if (private->rdc_data.mode.bits.data_chain == 0) {
287 cplength += count - 1;
288 datasize += (count - 1)*sizeof(struct LO_fba_data);
289 }
290 /* Allocate the ccw request. */
291 cqr = dasd_smalloc_request(dasd_fba_discipline.name,
292 cplength, datasize, device);
293 if (IS_ERR(cqr))
294 return cqr;
295 ccw = cqr->cpaddr;
296 /* First ccw is define extent. */
297 define_extent(ccw++, cqr->data, rq_data_dir(req),
298 device->bp_block, req->sector, req->nr_sectors);
299 /* Build locate_record + read/write ccws. */
300 idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data));
301 LO_data = (struct LO_fba_data *) (idaws + cidaw);
302 /* Locate record for all blocks for smart devices. */
303 if (private->rdc_data.mode.bits.data_chain != 0) {
304 ccw[-1].flags |= CCW_FLAG_CC;
305 locate_record(ccw++, LO_data++, rq_data_dir(req), 0, count);
306 }
307 recid = first_rec;
308 rq_for_each_bio(bio, req) bio_for_each_segment(bv, bio, i) {
309 dst = page_address(bv->bv_page) + bv->bv_offset;
310 if (dasd_page_cache) {
311 char *copy = kmem_cache_alloc(dasd_page_cache,
312 SLAB_DMA | __GFP_NOWARN);
313 if (copy && rq_data_dir(req) == WRITE)
314 memcpy(copy + bv->bv_offset, dst, bv->bv_len);
315 if (copy)
316 dst = copy + bv->bv_offset;
317 }
318 for (off = 0; off < bv->bv_len; off += blksize) {
319 /* Locate record for stupid devices. */
320 if (private->rdc_data.mode.bits.data_chain == 0) {
321 ccw[-1].flags |= CCW_FLAG_CC;
322 locate_record(ccw, LO_data++,
323 rq_data_dir(req),
324 recid - first_rec, 1);
325 ccw->flags = CCW_FLAG_CC;
326 ccw++;
327 } else {
328 if (recid > first_rec)
329 ccw[-1].flags |= CCW_FLAG_DC;
330 else
331 ccw[-1].flags |= CCW_FLAG_CC;
332 }
333 ccw->cmd_code = cmd;
334 ccw->count = device->bp_block;
335 if (idal_is_needed(dst, blksize)) {
336 ccw->cda = (__u32)(addr_t) idaws;
337 ccw->flags = CCW_FLAG_IDA;
338 idaws = idal_create_words(idaws, dst, blksize);
339 } else {
340 ccw->cda = (__u32)(addr_t) dst;
341 ccw->flags = 0;
342 }
343 ccw++;
344 dst += blksize;
345 recid++;
346 }
347 }
348 if (req->flags & REQ_FAILFAST)
349 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
350 cqr->device = device;
351 cqr->expires = 5 * 60 * HZ; /* 5 minutes */
352 cqr->retries = 32;
353 cqr->buildclk = get_clock();
354 cqr->status = DASD_CQR_FILLED;
355 return cqr;
356 }
357
358 static int
359 dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req)
360 {
361 struct dasd_fba_private *private;
362 struct ccw1 *ccw;
363 struct bio *bio;
364 struct bio_vec *bv;
365 char *dst, *cda;
366 unsigned int blksize, off;
367 int i, status;
368
369 if (!dasd_page_cache)
370 goto out;
371 private = (struct dasd_fba_private *) cqr->device->private;
372 blksize = cqr->device->bp_block;
373 ccw = cqr->cpaddr;
374 /* Skip over define extent & locate record. */
375 ccw++;
376 if (private->rdc_data.mode.bits.data_chain != 0)
377 ccw++;
378 rq_for_each_bio(bio, req) bio_for_each_segment(bv, bio, i) {
379 dst = page_address(bv->bv_page) + bv->bv_offset;
380 for (off = 0; off < bv->bv_len; off += blksize) {
381 /* Skip locate record. */
382 if (private->rdc_data.mode.bits.data_chain == 0)
383 ccw++;
384 if (dst) {
385 if (ccw->flags & CCW_FLAG_IDA)
386 cda = *((char **)((addr_t) ccw->cda));
387 else
388 cda = (char *)((addr_t) ccw->cda);
389 if (dst != cda) {
390 if (rq_data_dir(req) == READ)
391 memcpy(dst, cda, bv->bv_len);
392 kmem_cache_free(dasd_page_cache,
393 (void *)((addr_t)cda & PAGE_MASK));
394 }
395 dst = NULL;
396 }
397 ccw++;
398 }
399 }
400 out:
401 status = cqr->status == DASD_CQR_DONE;
402 dasd_sfree_request(cqr, cqr->device);
403 return status;
404 }
405
406 static int
407 dasd_fba_fill_info(struct dasd_device * device,
408 struct dasd_information2_t * info)
409 {
410 info->label_block = 1;
411 info->FBA_layout = 1;
412 info->format = DASD_FORMAT_LDL;
413 info->characteristics_size = sizeof(struct dasd_fba_characteristics);
414 memcpy(info->characteristics,
415 &((struct dasd_fba_private *) device->private)->rdc_data,
416 sizeof (struct dasd_fba_characteristics));
417 info->confdata_size = 0;
418 return 0;
419 }
420
421 static void
422 dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
423 struct irb *irb)
424 {
425 char *page;
426 struct ccw1 *act, *end, *last;
427 int len, sl, sct, count;
428
429 page = (char *) get_zeroed_page(GFP_ATOMIC);
430 if (page == NULL) {
431 DEV_MESSAGE(KERN_ERR, device, " %s",
432 "No memory to dump sense data");
433 return;
434 }
435 len = sprintf(page, KERN_ERR PRINTK_HEADER
436 " I/O status report for device %s:\n",
437 device->cdev->dev.bus_id);
438 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
439 " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
440 irb->scsw.cstat, irb->scsw.dstat);
441 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
442 " device %s: Failing CCW: %p\n",
443 device->cdev->dev.bus_id,
444 (void *) (addr_t) irb->scsw.cpa);
445 if (irb->esw.esw0.erw.cons) {
446 for (sl = 0; sl < 4; sl++) {
447 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
448 " Sense(hex) %2d-%2d:",
449 (8 * sl), ((8 * sl) + 7));
450
451 for (sct = 0; sct < 8; sct++) {
452 len += sprintf(page + len, " %02x",
453 irb->ecw[8 * sl + sct]);
454 }
455 len += sprintf(page + len, "\n");
456 }
457 } else {
458 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
459 " SORRY - NO VALID SENSE AVAILABLE\n");
460 }
461 MESSAGE_LOG(KERN_ERR, "%s",
462 page + sizeof(KERN_ERR PRINTK_HEADER));
463
464 /* dump the Channel Program */
465 /* print first CCWs (maximum 8) */
466 act = req->cpaddr;
467 for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
468 end = min(act + 8, last);
469 len = sprintf(page, KERN_ERR PRINTK_HEADER
470 " Related CP in req: %p\n", req);
471 while (act <= end) {
472 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
473 " CCW %p: %08X %08X DAT:",
474 act, ((int *) act)[0], ((int *) act)[1]);
475 for (count = 0; count < 32 && count < act->count;
476 count += sizeof(int))
477 len += sprintf(page + len, " %08X",
478 ((int *) (addr_t) act->cda)
479 [(count>>2)]);
480 len += sprintf(page + len, "\n");
481 act++;
482 }
483 MESSAGE_LOG(KERN_ERR, "%s",
484 page + sizeof(KERN_ERR PRINTK_HEADER));
485
486
487 /* print failing CCW area */
488 len = 0;
489 if (act < ((struct ccw1 *)(addr_t) irb->scsw.cpa) - 2) {
490 act = ((struct ccw1 *)(addr_t) irb->scsw.cpa) - 2;
491 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
492 }
493 end = min((struct ccw1 *)(addr_t) irb->scsw.cpa + 2, last);
494 while (act <= end) {
495 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
496 " CCW %p: %08X %08X DAT:",
497 act, ((int *) act)[0], ((int *) act)[1]);
498 for (count = 0; count < 32 && count < act->count;
499 count += sizeof(int))
500 len += sprintf(page + len, " %08X",
501 ((int *) (addr_t) act->cda)
502 [(count>>2)]);
503 len += sprintf(page + len, "\n");
504 act++;
505 }
506
507 /* print last CCWs */
508 if (act < last - 2) {
509 act = last - 2;
510 len += sprintf(page + len, KERN_ERR PRINTK_HEADER "......\n");
511 }
512 while (act <= last) {
513 len += sprintf(page + len, KERN_ERR PRINTK_HEADER
514 " CCW %p: %08X %08X DAT:",
515 act, ((int *) act)[0], ((int *) act)[1]);
516 for (count = 0; count < 32 && count < act->count;
517 count += sizeof(int))
518 len += sprintf(page + len, " %08X",
519 ((int *) (addr_t) act->cda)
520 [(count>>2)]);
521 len += sprintf(page + len, "\n");
522 act++;
523 }
524 if (len > 0)
525 MESSAGE_LOG(KERN_ERR, "%s",
526 page + sizeof(KERN_ERR PRINTK_HEADER));
527 free_page((unsigned long) page);
528 }
529
530 /*
531 * max_blocks is dependent on the amount of storage that is available
532 * in the static io buffer for each device. Currently each device has
533 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
534 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
535 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
536 * addition we have one define extent ccw + 16 bytes of data and a
537 * locate record ccw for each block (stupid devices!) + 16 bytes of data.
538 * That makes:
539 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
540 * We want to fit two into the available memory so that we can immediately
541 * start the next request if one finishes off. That makes 100.1 blocks
542 * for one request. Give a little safety and the result is 96.
543 */
544 static struct dasd_discipline dasd_fba_discipline = {
545 .owner = THIS_MODULE,
546 .name = "FBA ",
547 .ebcname = "FBA ",
548 .max_blocks = 96,
549 .check_device = dasd_fba_check_characteristics,
550 .do_analysis = dasd_fba_do_analysis,
551 .fill_geometry = dasd_fba_fill_geometry,
552 .start_IO = dasd_start_IO,
553 .term_IO = dasd_term_IO,
554 .examine_error = dasd_fba_examine_error,
555 .erp_action = dasd_fba_erp_action,
556 .erp_postaction = dasd_fba_erp_postaction,
557 .build_cp = dasd_fba_build_cp,
558 .free_cp = dasd_fba_free_cp,
559 .dump_sense = dasd_fba_dump_sense,
560 .fill_info = dasd_fba_fill_info,
561 };
562
563 static int __init
564 dasd_fba_init(void)
565 {
566 ASCEBC(dasd_fba_discipline.ebcname, 4);
567 return ccw_driver_register(&dasd_fba_driver);
568 }
569
570 static void __exit
571 dasd_fba_cleanup(void)
572 {
573 ccw_driver_unregister(&dasd_fba_driver);
574 }
575
576 module_init(dasd_fba_init);
577 module_exit(dasd_fba_cleanup);
This page took 0.044495 seconds and 6 git commands to generate.