xref: /openbmc/linux/drivers/s390/block/dasd_fba.c (revision 11a163f2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  * Bugreports.to..: <Linux390@de.ibm.com>
5  * Copyright IBM Corp. 1999, 2009
6  */
7 
8 #define KMSG_COMPONENT "dasd-fba"
9 
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/ccwdev.h>
24 
25 #include "dasd_int.h"
26 #include "dasd_fba.h"
27 
28 #ifdef PRINTK_HEADER
29 #undef PRINTK_HEADER
30 #endif				/* PRINTK_HEADER */
31 #define PRINTK_HEADER "dasd(fba):"
32 
33 #define FBA_DEFAULT_RETRIES 32
34 
35 #define DASD_FBA_CCW_WRITE 0x41
36 #define DASD_FBA_CCW_READ 0x42
37 #define DASD_FBA_CCW_LOCATE 0x43
38 #define DASD_FBA_CCW_DEFINE_EXTENT 0x63
39 
40 MODULE_LICENSE("GPL");
41 
42 static struct dasd_discipline dasd_fba_discipline;
43 static void *dasd_fba_zero_page;
44 
45 struct dasd_fba_private {
46 	struct dasd_fba_characteristics rdc_data;
47 };
48 
49 static struct ccw_device_id dasd_fba_ids[] = {
50 	{ CCW_DEVICE_DEVTYPE (0x6310, 0, 0x9336, 0), .driver_info = 0x1},
51 	{ CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3370, 0), .driver_info = 0x2},
52 	{ /* end of list */ },
53 };
54 
55 MODULE_DEVICE_TABLE(ccw, dasd_fba_ids);
56 
57 static struct ccw_driver dasd_fba_driver; /* see below */
58 static int
59 dasd_fba_probe(struct ccw_device *cdev)
60 {
61 	return dasd_generic_probe(cdev, &dasd_fba_discipline);
62 }
63 
64 static int
65 dasd_fba_set_online(struct ccw_device *cdev)
66 {
67 	return dasd_generic_set_online(cdev, &dasd_fba_discipline);
68 }
69 
70 static struct ccw_driver dasd_fba_driver = {
71 	.driver = {
72 		.name	= "dasd-fba",
73 		.owner	= THIS_MODULE,
74 	},
75 	.ids         = dasd_fba_ids,
76 	.probe       = dasd_fba_probe,
77 	.remove      = dasd_generic_remove,
78 	.set_offline = dasd_generic_set_offline,
79 	.set_online  = dasd_fba_set_online,
80 	.notify      = dasd_generic_notify,
81 	.path_event  = dasd_generic_path_event,
82 	.freeze      = dasd_generic_pm_freeze,
83 	.thaw	     = dasd_generic_restore_device,
84 	.restore     = dasd_generic_restore_device,
85 	.int_class   = IRQIO_DAS,
86 };
87 
88 static void
89 define_extent(struct ccw1 * ccw, struct DE_fba_data *data, int rw,
90 	      int blksize, int beg, int nr)
91 {
92 	ccw->cmd_code = DASD_FBA_CCW_DEFINE_EXTENT;
93 	ccw->flags = 0;
94 	ccw->count = 16;
95 	ccw->cda = (__u32) __pa(data);
96 	memset(data, 0, sizeof (struct DE_fba_data));
97 	if (rw == WRITE)
98 		(data->mask).perm = 0x0;
99 	else if (rw == READ)
100 		(data->mask).perm = 0x1;
101 	else
102 		data->mask.perm = 0x2;
103 	data->blk_size = blksize;
104 	data->ext_loc = beg;
105 	data->ext_end = nr - 1;
106 }
107 
108 static void
109 locate_record(struct ccw1 * ccw, struct LO_fba_data *data, int rw,
110 	      int block_nr, int block_ct)
111 {
112 	ccw->cmd_code = DASD_FBA_CCW_LOCATE;
113 	ccw->flags = 0;
114 	ccw->count = 8;
115 	ccw->cda = (__u32) __pa(data);
116 	memset(data, 0, sizeof (struct LO_fba_data));
117 	if (rw == WRITE)
118 		data->operation.cmd = 0x5;
119 	else if (rw == READ)
120 		data->operation.cmd = 0x6;
121 	else
122 		data->operation.cmd = 0x8;
123 	data->blk_nr = block_nr;
124 	data->blk_ct = block_ct;
125 }
126 
127 static int
128 dasd_fba_check_characteristics(struct dasd_device *device)
129 {
130 	struct dasd_fba_private *private = device->private;
131 	struct ccw_device *cdev = device->cdev;
132 	struct dasd_block *block;
133 	int readonly, rc;
134 
135 	if (!private) {
136 		private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
137 		if (!private) {
138 			dev_warn(&device->cdev->dev,
139 				 "Allocating memory for private DASD "
140 				 "data failed\n");
141 			return -ENOMEM;
142 		}
143 		device->private = private;
144 	} else {
145 		memset(private, 0, sizeof(*private));
146 	}
147 	block = dasd_alloc_block();
148 	if (IS_ERR(block)) {
149 		DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s", "could not allocate "
150 				"dasd block structure");
151 		device->private = NULL;
152 		kfree(private);
153 		return PTR_ERR(block);
154 	}
155 	device->block = block;
156 	block->base = device;
157 
158 	/* Read Device Characteristics */
159 	rc = dasd_generic_read_dev_chars(device, DASD_FBA_MAGIC,
160 					 &private->rdc_data, 32);
161 	if (rc) {
162 		DBF_EVENT_DEVID(DBF_WARNING, cdev, "Read device "
163 				"characteristics returned error %d", rc);
164 		device->block = NULL;
165 		dasd_free_block(block);
166 		device->private = NULL;
167 		kfree(private);
168 		return rc;
169 	}
170 
171 	device->default_expires = DASD_EXPIRES;
172 	device->default_retries = FBA_DEFAULT_RETRIES;
173 	dasd_path_set_opm(device, LPM_ANYPATH);
174 
175 	readonly = dasd_device_is_ro(device);
176 	if (readonly)
177 		set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
178 
179 	/* FBA supports discard, set the according feature bit */
180 	dasd_set_feature(cdev, DASD_FEATURE_DISCARD, 1);
181 
182 	dev_info(&device->cdev->dev,
183 		 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
184 		 "and %d B/blk%s\n",
185 		 cdev->id.dev_type,
186 		 cdev->id.dev_model,
187 		 cdev->id.cu_type,
188 		 cdev->id.cu_model,
189 		 ((private->rdc_data.blk_bdsa *
190 		   (private->rdc_data.blk_size >> 9)) >> 11),
191 		 private->rdc_data.blk_size,
192 		 readonly ? ", read-only device" : "");
193 	return 0;
194 }
195 
196 static int dasd_fba_do_analysis(struct dasd_block *block)
197 {
198 	struct dasd_fba_private *private = block->base->private;
199 	int sb, rc;
200 
201 	rc = dasd_check_blocksize(private->rdc_data.blk_size);
202 	if (rc) {
203 		DBF_DEV_EVENT(DBF_WARNING, block->base, "unknown blocksize %d",
204 			    private->rdc_data.blk_size);
205 		return rc;
206 	}
207 	block->blocks = private->rdc_data.blk_bdsa;
208 	block->bp_block = private->rdc_data.blk_size;
209 	block->s2b_shift = 0;	/* bits to shift 512 to get a block */
210 	for (sb = 512; sb < private->rdc_data.blk_size; sb = sb << 1)
211 		block->s2b_shift++;
212 	return 0;
213 }
214 
215 static int dasd_fba_fill_geometry(struct dasd_block *block,
216 				  struct hd_geometry *geo)
217 {
218 	if (dasd_check_blocksize(block->bp_block) != 0)
219 		return -EINVAL;
220 	geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
221 	geo->heads = 16;
222 	geo->sectors = 128 >> block->s2b_shift;
223 	return 0;
224 }
225 
226 static dasd_erp_fn_t
227 dasd_fba_erp_action(struct dasd_ccw_req * cqr)
228 {
229 	return dasd_default_erp_action;
230 }
231 
232 static dasd_erp_fn_t
233 dasd_fba_erp_postaction(struct dasd_ccw_req * cqr)
234 {
235 	if (cqr->function == dasd_default_erp_action)
236 		return dasd_default_erp_postaction;
237 
238 	DBF_DEV_EVENT(DBF_WARNING, cqr->startdev, "unknown ERP action %p",
239 		    cqr->function);
240 	return NULL;
241 }
242 
243 static void dasd_fba_check_for_device_change(struct dasd_device *device,
244 					     struct dasd_ccw_req *cqr,
245 					     struct irb *irb)
246 {
247 	char mask;
248 
249 	/* first of all check for state change pending interrupt */
250 	mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
251 	if ((irb->scsw.cmd.dstat & mask) == mask)
252 		dasd_generic_handle_state_change(device);
253 };
254 
255 
256 /*
257  * Builds a CCW with no data payload
258  */
259 static void ccw_write_no_data(struct ccw1 *ccw)
260 {
261 	ccw->cmd_code = DASD_FBA_CCW_WRITE;
262 	ccw->flags |= CCW_FLAG_SLI;
263 	ccw->count = 0;
264 }
265 
266 /*
267  * Builds a CCW that writes only zeroes.
268  */
269 static void ccw_write_zero(struct ccw1 *ccw, int count)
270 {
271 	ccw->cmd_code = DASD_FBA_CCW_WRITE;
272 	ccw->flags |= CCW_FLAG_SLI;
273 	ccw->count = count;
274 	ccw->cda = (__u32) (addr_t) dasd_fba_zero_page;
275 }
276 
277 /*
278  * Helper function to count the amount of necessary CCWs within a given range
279  * with 4k alignment and command chaining in mind.
280  */
281 static int count_ccws(sector_t first_rec, sector_t last_rec,
282 		      unsigned int blocks_per_page)
283 {
284 	sector_t wz_stop = 0, d_stop = 0;
285 	int cur_pos = 0;
286 	int count = 0;
287 
288 	if (first_rec % blocks_per_page != 0) {
289 		wz_stop = first_rec + blocks_per_page -
290 			(first_rec % blocks_per_page) - 1;
291 		if (wz_stop > last_rec)
292 			wz_stop = last_rec;
293 		cur_pos = wz_stop - first_rec + 1;
294 		count++;
295 	}
296 
297 	if (last_rec - (first_rec + cur_pos) + 1 >= blocks_per_page) {
298 		if ((last_rec - blocks_per_page + 1) % blocks_per_page != 0)
299 			d_stop = last_rec - ((last_rec - blocks_per_page + 1) %
300 					     blocks_per_page);
301 		else
302 			d_stop = last_rec;
303 
304 		cur_pos += d_stop - (first_rec + cur_pos) + 1;
305 		count++;
306 	}
307 
308 	if (cur_pos == 0 || first_rec + cur_pos - 1 < last_rec)
309 		count++;
310 
311 	return count;
312 }
313 
314 /*
315  * This function builds a CCW request for block layer discard requests.
316  * Each page in the z/VM hypervisor that represents certain records of an FBA
317  * device will be padded with zeros. This is a special behaviour of the WRITE
318  * command which is triggered when no data payload is added to the CCW.
319  *
320  * Note: Due to issues in some z/VM versions, we can't fully utilise this
321  * special behaviour. We have to keep a 4k (or 8 block) alignment in mind to
322  * work around those issues and write actual zeroes to the unaligned parts in
323  * the request. This workaround might be removed in the future.
324  */
325 static struct dasd_ccw_req *dasd_fba_build_cp_discard(
326 						struct dasd_device *memdev,
327 						struct dasd_block *block,
328 						struct request *req)
329 {
330 	struct LO_fba_data *LO_data;
331 	struct dasd_ccw_req *cqr;
332 	struct ccw1 *ccw;
333 
334 	sector_t wz_stop = 0, d_stop = 0;
335 	sector_t first_rec, last_rec;
336 
337 	unsigned int blksize = block->bp_block;
338 	unsigned int blocks_per_page;
339 	int wz_count = 0;
340 	int d_count = 0;
341 	int cur_pos = 0; /* Current position within the extent */
342 	int count = 0;
343 	int cplength;
344 	int datasize;
345 	int nr_ccws;
346 
347 	first_rec = blk_rq_pos(req) >> block->s2b_shift;
348 	last_rec =
349 		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
350 	count = last_rec - first_rec + 1;
351 
352 	blocks_per_page = BLOCKS_PER_PAGE(blksize);
353 	nr_ccws = count_ccws(first_rec, last_rec, blocks_per_page);
354 
355 	/* define extent + nr_ccws * locate record + nr_ccws * single CCW */
356 	cplength = 1 + 2 * nr_ccws;
357 	datasize = sizeof(struct DE_fba_data) +
358 		nr_ccws * (sizeof(struct LO_fba_data) + sizeof(struct ccw1));
359 
360 	cqr = dasd_smalloc_request(DASD_FBA_MAGIC, cplength, datasize, memdev,
361 				   blk_mq_rq_to_pdu(req));
362 	if (IS_ERR(cqr))
363 		return cqr;
364 
365 	ccw = cqr->cpaddr;
366 
367 	define_extent(ccw++, cqr->data, WRITE, blksize, first_rec, count);
368 	LO_data = cqr->data + sizeof(struct DE_fba_data);
369 
370 	/* First part is not aligned. Calculate range to write zeroes. */
371 	if (first_rec % blocks_per_page != 0) {
372 		wz_stop = first_rec + blocks_per_page -
373 			(first_rec % blocks_per_page) - 1;
374 		if (wz_stop > last_rec)
375 			wz_stop = last_rec;
376 		wz_count = wz_stop - first_rec + 1;
377 
378 		ccw[-1].flags |= CCW_FLAG_CC;
379 		locate_record(ccw++, LO_data++, WRITE, cur_pos, wz_count);
380 
381 		ccw[-1].flags |= CCW_FLAG_CC;
382 		ccw_write_zero(ccw++, wz_count * blksize);
383 
384 		cur_pos = wz_count;
385 	}
386 
387 	/* We can do proper discard when we've got at least blocks_per_page blocks. */
388 	if (last_rec - (first_rec + cur_pos) + 1 >= blocks_per_page) {
389 		/* is last record at page boundary? */
390 		if ((last_rec - blocks_per_page + 1) % blocks_per_page != 0)
391 			d_stop = last_rec - ((last_rec - blocks_per_page + 1) %
392 					     blocks_per_page);
393 		else
394 			d_stop = last_rec;
395 
396 		d_count = d_stop - (first_rec + cur_pos) + 1;
397 
398 		ccw[-1].flags |= CCW_FLAG_CC;
399 		locate_record(ccw++, LO_data++, WRITE, cur_pos, d_count);
400 
401 		ccw[-1].flags |= CCW_FLAG_CC;
402 		ccw_write_no_data(ccw++);
403 
404 		cur_pos += d_count;
405 	}
406 
407 	/* We might still have some bits left which need to be zeroed. */
408 	if (cur_pos == 0 || first_rec + cur_pos - 1 < last_rec) {
409 		if (d_stop != 0)
410 			wz_count = last_rec - d_stop;
411 		else if (wz_stop != 0)
412 			wz_count = last_rec - wz_stop;
413 		else
414 			wz_count = count;
415 
416 		ccw[-1].flags |= CCW_FLAG_CC;
417 		locate_record(ccw++, LO_data++, WRITE, cur_pos, wz_count);
418 
419 		ccw[-1].flags |= CCW_FLAG_CC;
420 		ccw_write_zero(ccw++, wz_count * blksize);
421 	}
422 
423 	if (blk_noretry_request(req) ||
424 	    block->base->features & DASD_FEATURE_FAILFAST)
425 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
426 
427 	cqr->startdev = memdev;
428 	cqr->memdev = memdev;
429 	cqr->block = block;
430 	cqr->expires = memdev->default_expires * HZ;	/* default 5 minutes */
431 	cqr->retries = memdev->default_retries;
432 	cqr->buildclk = get_tod_clock();
433 	cqr->status = DASD_CQR_FILLED;
434 
435 	return cqr;
436 }
437 
438 static struct dasd_ccw_req *dasd_fba_build_cp_regular(
439 						struct dasd_device *memdev,
440 						struct dasd_block *block,
441 						struct request *req)
442 {
443 	struct dasd_fba_private *private = block->base->private;
444 	unsigned long *idaws;
445 	struct LO_fba_data *LO_data;
446 	struct dasd_ccw_req *cqr;
447 	struct ccw1 *ccw;
448 	struct req_iterator iter;
449 	struct bio_vec bv;
450 	char *dst;
451 	int count, cidaw, cplength, datasize;
452 	sector_t recid, first_rec, last_rec;
453 	unsigned int blksize, off;
454 	unsigned char cmd;
455 
456 	if (rq_data_dir(req) == READ) {
457 		cmd = DASD_FBA_CCW_READ;
458 	} else if (rq_data_dir(req) == WRITE) {
459 		cmd = DASD_FBA_CCW_WRITE;
460 	} else
461 		return ERR_PTR(-EINVAL);
462 	blksize = block->bp_block;
463 	/* Calculate record id of first and last block. */
464 	first_rec = blk_rq_pos(req) >> block->s2b_shift;
465 	last_rec =
466 		(blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
467 	/* Check struct bio and count the number of blocks for the request. */
468 	count = 0;
469 	cidaw = 0;
470 	rq_for_each_segment(bv, req, iter) {
471 		if (bv.bv_len & (blksize - 1))
472 			/* Fba can only do full blocks. */
473 			return ERR_PTR(-EINVAL);
474 		count += bv.bv_len >> (block->s2b_shift + 9);
475 		if (idal_is_needed (page_address(bv.bv_page), bv.bv_len))
476 			cidaw += bv.bv_len / blksize;
477 	}
478 	/* Paranoia. */
479 	if (count != last_rec - first_rec + 1)
480 		return ERR_PTR(-EINVAL);
481 	/* 1x define extent + 1x locate record + number of blocks */
482 	cplength = 2 + count;
483 	/* 1x define extent + 1x locate record */
484 	datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) +
485 		cidaw * sizeof(unsigned long);
486 	/*
487 	 * Find out number of additional locate record ccws if the device
488 	 * can't do data chaining.
489 	 */
490 	if (private->rdc_data.mode.bits.data_chain == 0) {
491 		cplength += count - 1;
492 		datasize += (count - 1)*sizeof(struct LO_fba_data);
493 	}
494 	/* Allocate the ccw request. */
495 	cqr = dasd_smalloc_request(DASD_FBA_MAGIC, cplength, datasize, memdev,
496 				   blk_mq_rq_to_pdu(req));
497 	if (IS_ERR(cqr))
498 		return cqr;
499 	ccw = cqr->cpaddr;
500 	/* First ccw is define extent. */
501 	define_extent(ccw++, cqr->data, rq_data_dir(req),
502 		      block->bp_block, blk_rq_pos(req), blk_rq_sectors(req));
503 	/* Build locate_record + read/write ccws. */
504 	idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data));
505 	LO_data = (struct LO_fba_data *) (idaws + cidaw);
506 	/* Locate record for all blocks for smart devices. */
507 	if (private->rdc_data.mode.bits.data_chain != 0) {
508 		ccw[-1].flags |= CCW_FLAG_CC;
509 		locate_record(ccw++, LO_data++, rq_data_dir(req), 0, count);
510 	}
511 	recid = first_rec;
512 	rq_for_each_segment(bv, req, iter) {
513 		dst = page_address(bv.bv_page) + bv.bv_offset;
514 		if (dasd_page_cache) {
515 			char *copy = kmem_cache_alloc(dasd_page_cache,
516 						      GFP_DMA | __GFP_NOWARN);
517 			if (copy && rq_data_dir(req) == WRITE)
518 				memcpy(copy + bv.bv_offset, dst, bv.bv_len);
519 			if (copy)
520 				dst = copy + bv.bv_offset;
521 		}
522 		for (off = 0; off < bv.bv_len; off += blksize) {
523 			/* Locate record for stupid devices. */
524 			if (private->rdc_data.mode.bits.data_chain == 0) {
525 				ccw[-1].flags |= CCW_FLAG_CC;
526 				locate_record(ccw, LO_data++,
527 					      rq_data_dir(req),
528 					      recid - first_rec, 1);
529 				ccw->flags = CCW_FLAG_CC;
530 				ccw++;
531 			} else {
532 				if (recid > first_rec)
533 					ccw[-1].flags |= CCW_FLAG_DC;
534 				else
535 					ccw[-1].flags |= CCW_FLAG_CC;
536 			}
537 			ccw->cmd_code = cmd;
538 			ccw->count = block->bp_block;
539 			if (idal_is_needed(dst, blksize)) {
540 				ccw->cda = (__u32)(addr_t) idaws;
541 				ccw->flags = CCW_FLAG_IDA;
542 				idaws = idal_create_words(idaws, dst, blksize);
543 			} else {
544 				ccw->cda = (__u32)(addr_t) dst;
545 				ccw->flags = 0;
546 			}
547 			ccw++;
548 			dst += blksize;
549 			recid++;
550 		}
551 	}
552 	if (blk_noretry_request(req) ||
553 	    block->base->features & DASD_FEATURE_FAILFAST)
554 		set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
555 	cqr->startdev = memdev;
556 	cqr->memdev = memdev;
557 	cqr->block = block;
558 	cqr->expires = memdev->default_expires * HZ;	/* default 5 minutes */
559 	cqr->retries = memdev->default_retries;
560 	cqr->buildclk = get_tod_clock();
561 	cqr->status = DASD_CQR_FILLED;
562 	return cqr;
563 }
564 
565 static struct dasd_ccw_req *dasd_fba_build_cp(struct dasd_device *memdev,
566 					      struct dasd_block *block,
567 					      struct request *req)
568 {
569 	if (req_op(req) == REQ_OP_DISCARD || req_op(req) == REQ_OP_WRITE_ZEROES)
570 		return dasd_fba_build_cp_discard(memdev, block, req);
571 	else
572 		return dasd_fba_build_cp_regular(memdev, block, req);
573 }
574 
575 static int
576 dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req)
577 {
578 	struct dasd_fba_private *private = cqr->block->base->private;
579 	struct ccw1 *ccw;
580 	struct req_iterator iter;
581 	struct bio_vec bv;
582 	char *dst, *cda;
583 	unsigned int blksize, off;
584 	int status;
585 
586 	if (!dasd_page_cache)
587 		goto out;
588 	blksize = cqr->block->bp_block;
589 	ccw = cqr->cpaddr;
590 	/* Skip over define extent & locate record. */
591 	ccw++;
592 	if (private->rdc_data.mode.bits.data_chain != 0)
593 		ccw++;
594 	rq_for_each_segment(bv, req, iter) {
595 		dst = page_address(bv.bv_page) + bv.bv_offset;
596 		for (off = 0; off < bv.bv_len; off += blksize) {
597 			/* Skip locate record. */
598 			if (private->rdc_data.mode.bits.data_chain == 0)
599 				ccw++;
600 			if (dst) {
601 				if (ccw->flags & CCW_FLAG_IDA)
602 					cda = *((char **)((addr_t) ccw->cda));
603 				else
604 					cda = (char *)((addr_t) ccw->cda);
605 				if (dst != cda) {
606 					if (rq_data_dir(req) == READ)
607 						memcpy(dst, cda, bv.bv_len);
608 					kmem_cache_free(dasd_page_cache,
609 					    (void *)((addr_t)cda & PAGE_MASK));
610 				}
611 				dst = NULL;
612 			}
613 			ccw++;
614 		}
615 	}
616 out:
617 	status = cqr->status == DASD_CQR_DONE;
618 	dasd_sfree_request(cqr, cqr->memdev);
619 	return status;
620 }
621 
622 static void dasd_fba_handle_terminated_request(struct dasd_ccw_req *cqr)
623 {
624 	if (cqr->retries < 0)
625 		cqr->status = DASD_CQR_FAILED;
626 	else
627 		cqr->status = DASD_CQR_FILLED;
628 };
629 
630 static int
631 dasd_fba_fill_info(struct dasd_device * device,
632 		   struct dasd_information2_t * info)
633 {
634 	struct dasd_fba_private *private = device->private;
635 
636 	info->label_block = 1;
637 	info->FBA_layout = 1;
638 	info->format = DASD_FORMAT_LDL;
639 	info->characteristics_size = sizeof(private->rdc_data);
640 	memcpy(info->characteristics, &private->rdc_data,
641 	       sizeof(private->rdc_data));
642 	info->confdata_size = 0;
643 	return 0;
644 }
645 
646 static void
647 dasd_fba_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
648 			char *reason)
649 {
650 	u64 *sense;
651 
652 	sense = (u64 *) dasd_get_sense(irb);
653 	if (sense) {
654 		DBF_DEV_EVENT(DBF_EMERG, device,
655 			      "%s: %s %02x%02x%02x %016llx %016llx %016llx "
656 			      "%016llx", reason,
657 			      scsw_is_tm(&irb->scsw) ? "t" : "c",
658 			      scsw_cc(&irb->scsw), scsw_cstat(&irb->scsw),
659 			      scsw_dstat(&irb->scsw), sense[0], sense[1],
660 			      sense[2], sense[3]);
661 	} else {
662 		DBF_DEV_EVENT(DBF_EMERG, device, "%s",
663 			      "SORRY - NO VALID SENSE AVAILABLE\n");
664 	}
665 }
666 
667 
668 static void
669 dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
670 		    struct irb *irb)
671 {
672 	char *page;
673 	struct ccw1 *act, *end, *last;
674 	int len, sl, sct, count;
675 
676 	page = (char *) get_zeroed_page(GFP_ATOMIC);
677 	if (page == NULL) {
678 		DBF_DEV_EVENT(DBF_WARNING, device, "%s",
679 			    "No memory to dump sense data");
680 		return;
681 	}
682 	len = sprintf(page, PRINTK_HEADER
683 		      " I/O status report for device %s:\n",
684 		      dev_name(&device->cdev->dev));
685 	len += sprintf(page + len, PRINTK_HEADER
686 		       " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
687 		       irb->scsw.cmd.cstat, irb->scsw.cmd.dstat);
688 	len += sprintf(page + len, PRINTK_HEADER
689 		       " device %s: Failing CCW: %p\n",
690 		       dev_name(&device->cdev->dev),
691 		       (void *) (addr_t) irb->scsw.cmd.cpa);
692 	if (irb->esw.esw0.erw.cons) {
693 		for (sl = 0; sl < 4; sl++) {
694 			len += sprintf(page + len, PRINTK_HEADER
695 				       " Sense(hex) %2d-%2d:",
696 				       (8 * sl), ((8 * sl) + 7));
697 
698 			for (sct = 0; sct < 8; sct++) {
699 				len += sprintf(page + len, " %02x",
700 					       irb->ecw[8 * sl + sct]);
701 			}
702 			len += sprintf(page + len, "\n");
703 		}
704 	} else {
705 		len += sprintf(page + len, PRINTK_HEADER
706 			       " SORRY - NO VALID SENSE AVAILABLE\n");
707 	}
708 	printk(KERN_ERR "%s", page);
709 
710 	/* dump the Channel Program */
711 	/* print first CCWs (maximum 8) */
712 	act = req->cpaddr;
713         for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
714 	end = min(act + 8, last);
715 	len = sprintf(page, PRINTK_HEADER " Related CP in req: %p\n", req);
716 	while (act <= end) {
717 		len += sprintf(page + len, PRINTK_HEADER
718 			       " CCW %p: %08X %08X DAT:",
719 			       act, ((int *) act)[0], ((int *) act)[1]);
720 		for (count = 0; count < 32 && count < act->count;
721 		     count += sizeof(int))
722 			len += sprintf(page + len, " %08X",
723 				       ((int *) (addr_t) act->cda)
724 				       [(count>>2)]);
725 		len += sprintf(page + len, "\n");
726 		act++;
727 	}
728 	printk(KERN_ERR "%s", page);
729 
730 
731 	/* print failing CCW area */
732 	len = 0;
733 	if (act <  ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2) {
734 		act = ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2;
735 		len += sprintf(page + len, PRINTK_HEADER "......\n");
736 	}
737 	end = min((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa + 2, last);
738 	while (act <= end) {
739 		len += sprintf(page + len, PRINTK_HEADER
740 			       " CCW %p: %08X %08X DAT:",
741 			       act, ((int *) act)[0], ((int *) act)[1]);
742 		for (count = 0; count < 32 && count < act->count;
743 		     count += sizeof(int))
744 			len += sprintf(page + len, " %08X",
745 				       ((int *) (addr_t) act->cda)
746 				       [(count>>2)]);
747 		len += sprintf(page + len, "\n");
748 		act++;
749 	}
750 
751 	/* print last CCWs */
752 	if (act <  last - 2) {
753 		act = last - 2;
754 		len += sprintf(page + len, PRINTK_HEADER "......\n");
755 	}
756 	while (act <= last) {
757 		len += sprintf(page + len, PRINTK_HEADER
758 			       " CCW %p: %08X %08X DAT:",
759 			       act, ((int *) act)[0], ((int *) act)[1]);
760 		for (count = 0; count < 32 && count < act->count;
761 		     count += sizeof(int))
762 			len += sprintf(page + len, " %08X",
763 				       ((int *) (addr_t) act->cda)
764 				       [(count>>2)]);
765 		len += sprintf(page + len, "\n");
766 		act++;
767 	}
768 	if (len > 0)
769 		printk(KERN_ERR "%s", page);
770 	free_page((unsigned long) page);
771 }
772 
773 /*
774  * Initialize block layer request queue.
775  */
776 static void dasd_fba_setup_blk_queue(struct dasd_block *block)
777 {
778 	unsigned int logical_block_size = block->bp_block;
779 	struct request_queue *q = block->request_queue;
780 	unsigned int max_bytes, max_discard_sectors;
781 	int max;
782 
783 	max = DASD_FBA_MAX_BLOCKS << block->s2b_shift;
784 	blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
785 	q->limits.max_dev_sectors = max;
786 	blk_queue_logical_block_size(q, logical_block_size);
787 	blk_queue_max_hw_sectors(q, max);
788 	blk_queue_max_segments(q, USHRT_MAX);
789 	/* With page sized segments each segment can be translated into one idaw/tidaw */
790 	blk_queue_max_segment_size(q, PAGE_SIZE);
791 	blk_queue_segment_boundary(q, PAGE_SIZE - 1);
792 
793 	q->limits.discard_granularity = logical_block_size;
794 	q->limits.discard_alignment = PAGE_SIZE;
795 
796 	/* Calculate max_discard_sectors and make it PAGE aligned */
797 	max_bytes = USHRT_MAX * logical_block_size;
798 	max_bytes = ALIGN_DOWN(max_bytes, PAGE_SIZE);
799 	max_discard_sectors = max_bytes / logical_block_size;
800 
801 	blk_queue_max_discard_sectors(q, max_discard_sectors);
802 	blk_queue_max_write_zeroes_sectors(q, max_discard_sectors);
803 	blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
804 }
805 
806 static struct dasd_discipline dasd_fba_discipline = {
807 	.owner = THIS_MODULE,
808 	.name = "FBA ",
809 	.ebcname = "FBA ",
810 	.check_device = dasd_fba_check_characteristics,
811 	.do_analysis = dasd_fba_do_analysis,
812 	.verify_path = dasd_generic_verify_path,
813 	.setup_blk_queue = dasd_fba_setup_blk_queue,
814 	.fill_geometry = dasd_fba_fill_geometry,
815 	.start_IO = dasd_start_IO,
816 	.term_IO = dasd_term_IO,
817 	.handle_terminated_request = dasd_fba_handle_terminated_request,
818 	.erp_action = dasd_fba_erp_action,
819 	.erp_postaction = dasd_fba_erp_postaction,
820 	.check_for_device_change = dasd_fba_check_for_device_change,
821 	.build_cp = dasd_fba_build_cp,
822 	.free_cp = dasd_fba_free_cp,
823 	.dump_sense = dasd_fba_dump_sense,
824 	.dump_sense_dbf = dasd_fba_dump_sense_dbf,
825 	.fill_info = dasd_fba_fill_info,
826 };
827 
828 static int __init
829 dasd_fba_init(void)
830 {
831 	int ret;
832 
833 	ASCEBC(dasd_fba_discipline.ebcname, 4);
834 
835 	dasd_fba_zero_page = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
836 	if (!dasd_fba_zero_page)
837 		return -ENOMEM;
838 
839 	ret = ccw_driver_register(&dasd_fba_driver);
840 	if (!ret)
841 		wait_for_device_probe();
842 
843 	return ret;
844 }
845 
846 static void __exit
847 dasd_fba_cleanup(void)
848 {
849 	ccw_driver_unregister(&dasd_fba_driver);
850 	free_page((unsigned long)dasd_fba_zero_page);
851 }
852 
853 module_init(dasd_fba_init);
854 module_exit(dasd_fba_cleanup);
855