blob: 56007a3e7f110358e27ad74563f24e428cbae473 [file] [log] [blame]
Greg Kroah-Hartman6a55d2c2017-11-14 18:38:00 +01001// SPDX-License-Identifier: GPL-2.0
Horst Hummel138c0142006-06-29 14:58:12 +02002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Bugreports.to..: <Linux390@de.ibm.com>
Stefan Haberlandd41dd122009-06-16 10:30:25 +02005 * Copyright IBM Corp. 1999, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Stefan Haberlandca99dab2009-09-11 10:28:30 +02008#define KMSG_COMPONENT "dasd-fba"
Stefan Haberlandfc19f382009-03-26 15:23:49 +01009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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
Hannes Reinecke1f1ee9a2013-01-30 09:26:12 +000033#define FBA_DEFAULT_RETRIES 32
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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
40MODULE_LICENSE("GPL");
41
42static struct dasd_discipline dasd_fba_discipline;
43
44struct dasd_fba_private {
45 struct dasd_fba_characteristics rdc_data;
46};
47
48static struct ccw_device_id dasd_fba_ids[] = {
Heiko Carstensd2c993d2006-07-12 16:41:55 +020049 { CCW_DEVICE_DEVTYPE (0x6310, 0, 0x9336, 0), .driver_info = 0x1},
50 { CCW_DEVICE_DEVTYPE (0x3880, 0, 0x3370, 0), .driver_info = 0x2},
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 { /* end of list */ },
52};
53
54MODULE_DEVICE_TABLE(ccw, dasd_fba_ids);
55
56static struct ccw_driver dasd_fba_driver; /* see below */
57static int
58dasd_fba_probe(struct ccw_device *cdev)
59{
Horst Hummel40545572006-06-29 15:08:18 +020060 return dasd_generic_probe(cdev, &dasd_fba_discipline);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63static int
64dasd_fba_set_online(struct ccw_device *cdev)
65{
Horst Hummel40545572006-06-29 15:08:18 +020066 return dasd_generic_set_online(cdev, &dasd_fba_discipline);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69static struct ccw_driver dasd_fba_driver = {
Sebastian Ott3bda0582011-03-23 10:16:02 +010070 .driver = {
71 .name = "dasd-fba",
72 .owner = THIS_MODULE,
73 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 .ids = dasd_fba_ids,
75 .probe = dasd_fba_probe,
76 .remove = dasd_generic_remove,
77 .set_offline = dasd_generic_set_offline,
78 .set_online = dasd_fba_set_online,
79 .notify = dasd_generic_notify,
Stefan Weinhubera4d26c62011-01-05 12:48:03 +010080 .path_event = dasd_generic_path_event,
Stefan Haberlandd41dd122009-06-16 10:30:25 +020081 .freeze = dasd_generic_pm_freeze,
82 .thaw = dasd_generic_restore_device,
83 .restore = dasd_generic_restore_device,
Heiko Carstens420f42e2013-01-02 15:18:18 +010084 .int_class = IRQIO_DAS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Heiko Carstens4d284ca2007-02-05 21:18:53 +010087static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070088define_extent(struct ccw1 * ccw, struct DE_fba_data *data, int rw,
89 int blksize, int beg, int nr)
90{
91 ccw->cmd_code = DASD_FBA_CCW_DEFINE_EXTENT;
92 ccw->flags = 0;
93 ccw->count = 16;
94 ccw->cda = (__u32) __pa(data);
95 memset(data, 0, sizeof (struct DE_fba_data));
96 if (rw == WRITE)
97 (data->mask).perm = 0x0;
98 else if (rw == READ)
99 (data->mask).perm = 0x1;
100 else
101 data->mask.perm = 0x2;
102 data->blk_size = blksize;
103 data->ext_loc = beg;
104 data->ext_end = nr - 1;
105}
106
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100107static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108locate_record(struct ccw1 * ccw, struct LO_fba_data *data, int rw,
109 int block_nr, int block_ct)
110{
111 ccw->cmd_code = DASD_FBA_CCW_LOCATE;
112 ccw->flags = 0;
113 ccw->count = 8;
114 ccw->cda = (__u32) __pa(data);
115 memset(data, 0, sizeof (struct LO_fba_data));
116 if (rw == WRITE)
117 data->operation.cmd = 0x5;
118 else if (rw == READ)
119 data->operation.cmd = 0x6;
120 else
121 data->operation.cmd = 0x8;
122 data->blk_nr = block_nr;
123 data->blk_ct = block_ct;
124}
125
126static int
127dasd_fba_check_characteristics(struct dasd_device *device)
128{
Sebastian Ott543691a42016-03-04 10:34:05 +0100129 struct dasd_fba_private *private = device->private;
Horst Hummel138c0142006-06-29 14:58:12 +0200130 struct ccw_device *cdev = device->cdev;
Sebastian Ott543691a42016-03-04 10:34:05 +0100131 struct dasd_block *block;
132 int readonly, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Sebastian Ott92636b12009-06-12 10:26:37 +0200134 if (!private) {
135 private = kzalloc(sizeof(*private), GFP_KERNEL | GFP_DMA);
136 if (!private) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100137 dev_warn(&device->cdev->dev,
138 "Allocating memory for private DASD "
139 "data failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return -ENOMEM;
141 }
Sebastian Ott543691a42016-03-04 10:34:05 +0100142 device->private = private;
Sebastian Ott92636b12009-06-12 10:26:37 +0200143 } else {
144 memset(private, 0, sizeof(*private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100146 block = dasd_alloc_block();
147 if (IS_ERR(block)) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +0100148 DBF_EVENT_DEVID(DBF_WARNING, cdev, "%s", "could not allocate "
149 "dasd block structure");
Cornelia Huck73371942008-07-17 17:16:43 +0200150 device->private = NULL;
151 kfree(private);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100152 return PTR_ERR(block);
153 }
154 device->block = block;
155 block->base = device;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Read Device Characteristics */
Stefan Haberland68b781f2009-09-11 10:28:29 +0200158 rc = dasd_generic_read_dev_chars(device, DASD_FBA_MAGIC,
159 &private->rdc_data, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (rc) {
Stefan Haberlandb8ed5dd2009-12-07 12:51:52 +0100161 DBF_EVENT_DEVID(DBF_WARNING, cdev, "Read device "
162 "characteristics returned error %d", rc);
Cornelia Huck73371942008-07-17 17:16:43 +0200163 device->block = NULL;
164 dasd_free_block(block);
165 device->private = NULL;
166 kfree(private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return rc;
168 }
169
Stefan Haberland7c8faa82010-08-09 18:13:00 +0200170 device->default_expires = DASD_EXPIRES;
Hannes Reinecke1f1ee9a2013-01-30 09:26:12 +0000171 device->default_retries = FBA_DEFAULT_RETRIES;
Stefan Haberlandc9346152016-08-08 15:53:54 +0200172 dasd_path_set_opm(device, LPM_ANYPATH);
Stefan Haberland7c8faa82010-08-09 18:13:00 +0200173
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100174 readonly = dasd_device_is_ro(device);
175 if (readonly)
176 set_bit(DASD_FLAG_DEVICE_RO, &device->flags);
177
Jan Höppner28b841b2016-06-30 13:28:57 +0200178 /* FBA supports discard, set the according feature bit */
179 dasd_set_feature(cdev, DASD_FEATURE_DISCARD, 1);
180
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100181 dev_info(&device->cdev->dev,
182 "New FBA DASD %04X/%02X (CU %04X/%02X) with %d MB "
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100183 "and %d B/blk%s\n",
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100184 cdev->id.dev_type,
185 cdev->id.dev_model,
186 cdev->id.cu_type,
187 cdev->id.cu_model,
188 ((private->rdc_data.blk_bdsa *
189 (private->rdc_data.blk_size >> 9)) >> 11),
Stefan Weinhuber33b62a32010-03-08 12:26:24 +0100190 private->rdc_data.blk_size,
191 readonly ? ", read-only device" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 0;
193}
194
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100195static int dasd_fba_do_analysis(struct dasd_block *block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Sebastian Ott543691a42016-03-04 10:34:05 +0100197 struct dasd_fba_private *private = block->base->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int sb, rc;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 rc = dasd_check_blocksize(private->rdc_data.blk_size);
201 if (rc) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100202 DBF_DEV_EVENT(DBF_WARNING, block->base, "unknown blocksize %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 private->rdc_data.blk_size);
204 return rc;
205 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100206 block->blocks = private->rdc_data.blk_bdsa;
207 block->bp_block = private->rdc_data.blk_size;
208 block->s2b_shift = 0; /* bits to shift 512 to get a block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 for (sb = 512; sb < private->rdc_data.blk_size; sb = sb << 1)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100210 block->s2b_shift++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212}
213
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100214static int dasd_fba_fill_geometry(struct dasd_block *block,
215 struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100217 if (dasd_check_blocksize(block->bp_block) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -EINVAL;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100219 geo->cylinders = (block->blocks << block->s2b_shift) >> 10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 geo->heads = 16;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100221 geo->sectors = 128 >> block->s2b_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static dasd_erp_fn_t
226dasd_fba_erp_action(struct dasd_ccw_req * cqr)
227{
228 return dasd_default_erp_action;
229}
230
231static dasd_erp_fn_t
232dasd_fba_erp_postaction(struct dasd_ccw_req * cqr)
233{
234 if (cqr->function == dasd_default_erp_action)
235 return dasd_default_erp_postaction;
236
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100237 DBF_DEV_EVENT(DBF_WARNING, cqr->startdev, "unknown ERP action %p",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 cqr->function);
239 return NULL;
240}
241
Stefan Weinhuber5a27e602011-01-05 12:48:04 +0100242static void dasd_fba_check_for_device_change(struct dasd_device *device,
243 struct dasd_ccw_req *cqr,
244 struct irb *irb)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100245{
246 char mask;
247
248 /* first of all check for state change pending interrupt */
249 mask = DEV_STAT_ATTENTION | DEV_STAT_DEV_END | DEV_STAT_UNIT_EXCEP;
Stefan Weinhuber5a27e602011-01-05 12:48:04 +0100250 if ((irb->scsw.cmd.dstat & mask) == mask)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100251 dasd_generic_handle_state_change(device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100252};
253
Jan Höppner28b841b2016-06-30 13:28:57 +0200254
255/*
256 * Builds a CCW with no data payload
257 */
258static void ccw_write_no_data(struct ccw1 *ccw)
259{
260 ccw->cmd_code = DASD_FBA_CCW_WRITE;
261 ccw->flags |= CCW_FLAG_SLI;
262 ccw->count = 0;
263}
264
265/*
266 * Builds a CCW that writes only zeroes.
267 */
268static void ccw_write_zero(struct ccw1 *ccw, int count)
269{
270 ccw->cmd_code = DASD_FBA_CCW_WRITE;
271 ccw->flags |= CCW_FLAG_SLI;
272 ccw->count = count;
273 ccw->cda = (__u32) (addr_t) page_to_phys(ZERO_PAGE(0));
274}
275
276/*
277 * Helper function to count the amount of necessary CCWs within a given range
278 * with 4k alignment and command chaining in mind.
279 */
280static int count_ccws(sector_t first_rec, sector_t last_rec,
281 unsigned int blocks_per_page)
282{
283 sector_t wz_stop = 0, d_stop = 0;
284 int cur_pos = 0;
285 int count = 0;
286
287 if (first_rec % blocks_per_page != 0) {
288 wz_stop = first_rec + blocks_per_page -
289 (first_rec % blocks_per_page) - 1;
290 if (wz_stop > last_rec)
291 wz_stop = last_rec;
292 cur_pos = wz_stop - first_rec + 1;
293 count++;
294 }
295
296 if (last_rec - (first_rec + cur_pos) + 1 >= blocks_per_page) {
297 if ((last_rec - blocks_per_page + 1) % blocks_per_page != 0)
298 d_stop = last_rec - ((last_rec - blocks_per_page + 1) %
299 blocks_per_page);
300 else
301 d_stop = last_rec;
302
303 cur_pos += d_stop - (first_rec + cur_pos) + 1;
304 count++;
305 }
306
307 if (cur_pos == 0 || first_rec + cur_pos - 1 < last_rec)
308 count++;
309
310 return count;
311}
312
313/*
314 * This function builds a CCW request for block layer discard requests.
315 * Each page in the z/VM hypervisor that represents certain records of an FBA
316 * device will be padded with zeros. This is a special behaviour of the WRITE
317 * command which is triggered when no data payload is added to the CCW.
318 *
319 * Note: Due to issues in some z/VM versions, we can't fully utilise this
320 * special behaviour. We have to keep a 4k (or 8 block) alignment in mind to
321 * work around those issues and write actual zeroes to the unaligned parts in
322 * the request. This workaround might be removed in the future.
323 */
324static struct dasd_ccw_req *dasd_fba_build_cp_discard(
325 struct dasd_device *memdev,
326 struct dasd_block *block,
327 struct request *req)
328{
329 struct LO_fba_data *LO_data;
330 struct dasd_ccw_req *cqr;
331 struct ccw1 *ccw;
332
333 sector_t wz_stop = 0, d_stop = 0;
334 sector_t first_rec, last_rec;
335
336 unsigned int blksize = block->bp_block;
337 unsigned int blocks_per_page;
338 int wz_count = 0;
339 int d_count = 0;
340 int cur_pos = 0; /* Current position within the extent */
341 int count = 0;
342 int cplength;
343 int datasize;
344 int nr_ccws;
345
346 first_rec = blk_rq_pos(req) >> block->s2b_shift;
347 last_rec =
348 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
349 count = last_rec - first_rec + 1;
350
351 blocks_per_page = BLOCKS_PER_PAGE(blksize);
352 nr_ccws = count_ccws(first_rec, last_rec, blocks_per_page);
353
354 /* define extent + nr_ccws * locate record + nr_ccws * single CCW */
355 cplength = 1 + 2 * nr_ccws;
356 datasize = sizeof(struct DE_fba_data) +
357 nr_ccws * (sizeof(struct LO_fba_data) + sizeof(struct ccw1));
358
Sebastian Ottc5205f22018-06-04 19:07:39 +0200359 cqr = dasd_smalloc_request(DASD_FBA_MAGIC, cplength, datasize, memdev,
360 blk_mq_rq_to_pdu(req));
Jan Höppner28b841b2016-06-30 13:28:57 +0200361 if (IS_ERR(cqr))
362 return cqr;
363
364 ccw = cqr->cpaddr;
365
366 define_extent(ccw++, cqr->data, WRITE, blksize, first_rec, count);
367 LO_data = cqr->data + sizeof(struct DE_fba_data);
368
369 /* First part is not aligned. Calculate range to write zeroes. */
370 if (first_rec % blocks_per_page != 0) {
371 wz_stop = first_rec + blocks_per_page -
372 (first_rec % blocks_per_page) - 1;
373 if (wz_stop > last_rec)
374 wz_stop = last_rec;
375 wz_count = wz_stop - first_rec + 1;
376
377 ccw[-1].flags |= CCW_FLAG_CC;
378 locate_record(ccw++, LO_data++, WRITE, cur_pos, wz_count);
379
380 ccw[-1].flags |= CCW_FLAG_CC;
381 ccw_write_zero(ccw++, wz_count * blksize);
382
383 cur_pos = wz_count;
384 }
385
386 /* We can do proper discard when we've got at least blocks_per_page blocks. */
387 if (last_rec - (first_rec + cur_pos) + 1 >= blocks_per_page) {
388 /* is last record at page boundary? */
389 if ((last_rec - blocks_per_page + 1) % blocks_per_page != 0)
390 d_stop = last_rec - ((last_rec - blocks_per_page + 1) %
391 blocks_per_page);
392 else
393 d_stop = last_rec;
394
395 d_count = d_stop - (first_rec + cur_pos) + 1;
396
397 ccw[-1].flags |= CCW_FLAG_CC;
398 locate_record(ccw++, LO_data++, WRITE, cur_pos, d_count);
399
400 ccw[-1].flags |= CCW_FLAG_CC;
401 ccw_write_no_data(ccw++);
402
403 cur_pos += d_count;
404 }
405
406 /* We might still have some bits left which need to be zeroed. */
407 if (cur_pos == 0 || first_rec + cur_pos - 1 < last_rec) {
408 if (d_stop != 0)
409 wz_count = last_rec - d_stop;
410 else if (wz_stop != 0)
411 wz_count = last_rec - wz_stop;
412 else
413 wz_count = count;
414
415 ccw[-1].flags |= CCW_FLAG_CC;
416 locate_record(ccw++, LO_data++, WRITE, cur_pos, wz_count);
417
418 ccw[-1].flags |= CCW_FLAG_CC;
419 ccw_write_zero(ccw++, wz_count * blksize);
420 }
421
422 if (blk_noretry_request(req) ||
423 block->base->features & DASD_FEATURE_FAILFAST)
424 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
425
426 cqr->startdev = memdev;
427 cqr->memdev = memdev;
428 cqr->block = block;
429 cqr->expires = memdev->default_expires * HZ; /* default 5 minutes */
430 cqr->retries = memdev->default_retries;
431 cqr->buildclk = get_tod_clock();
432 cqr->status = DASD_CQR_FILLED;
433
434 return cqr;
435}
436
437static struct dasd_ccw_req *dasd_fba_build_cp_regular(
438 struct dasd_device *memdev,
439 struct dasd_block *block,
440 struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Sebastian Ott543691a42016-03-04 10:34:05 +0100442 struct dasd_fba_private *private = block->base->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 unsigned long *idaws;
444 struct LO_fba_data *LO_data;
445 struct dasd_ccw_req *cqr;
446 struct ccw1 *ccw;
NeilBrown5705f702007-09-25 12:35:59 +0200447 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800448 struct bio_vec bv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 char *dst;
450 int count, cidaw, cplength, datasize;
451 sector_t recid, first_rec, last_rec;
452 unsigned int blksize, off;
453 unsigned char cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (rq_data_dir(req) == READ) {
456 cmd = DASD_FBA_CCW_READ;
457 } else if (rq_data_dir(req) == WRITE) {
458 cmd = DASD_FBA_CCW_WRITE;
459 } else
460 return ERR_PTR(-EINVAL);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100461 blksize = block->bp_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* Calculate record id of first and last block. */
Tejun Heo83096eb2009-05-07 22:24:39 +0900463 first_rec = blk_rq_pos(req) >> block->s2b_shift;
464 last_rec =
465 (blk_rq_pos(req) + blk_rq_sectors(req) - 1) >> block->s2b_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* Check struct bio and count the number of blocks for the request. */
467 count = 0;
468 cidaw = 0;
NeilBrown5705f702007-09-25 12:35:59 +0200469 rq_for_each_segment(bv, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800470 if (bv.bv_len & (blksize - 1))
Jens Axboe6c92e692007-08-16 13:43:12 +0200471 /* Fba can only do full blocks. */
472 return ERR_PTR(-EINVAL);
Kent Overstreet79886132013-11-23 17:19:00 -0800473 count += bv.bv_len >> (block->s2b_shift + 9);
Kent Overstreet79886132013-11-23 17:19:00 -0800474 if (idal_is_needed (page_address(bv.bv_page), bv.bv_len))
475 cidaw += bv.bv_len / blksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 /* Paranoia. */
478 if (count != last_rec - first_rec + 1)
479 return ERR_PTR(-EINVAL);
480 /* 1x define extent + 1x locate record + number of blocks */
481 cplength = 2 + count;
482 /* 1x define extent + 1x locate record */
483 datasize = sizeof(struct DE_fba_data) + sizeof(struct LO_fba_data) +
484 cidaw * sizeof(unsigned long);
485 /*
486 * Find out number of additional locate record ccws if the device
487 * can't do data chaining.
488 */
489 if (private->rdc_data.mode.bits.data_chain == 0) {
490 cplength += count - 1;
491 datasize += (count - 1)*sizeof(struct LO_fba_data);
492 }
493 /* Allocate the ccw request. */
Sebastian Ottc5205f22018-06-04 19:07:39 +0200494 cqr = dasd_smalloc_request(DASD_FBA_MAGIC, cplength, datasize, memdev,
495 blk_mq_rq_to_pdu(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (IS_ERR(cqr))
497 return cqr;
498 ccw = cqr->cpaddr;
499 /* First ccw is define extent. */
500 define_extent(ccw++, cqr->data, rq_data_dir(req),
Tejun Heo83096eb2009-05-07 22:24:39 +0900501 block->bp_block, blk_rq_pos(req), blk_rq_sectors(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* Build locate_record + read/write ccws. */
503 idaws = (unsigned long *) (cqr->data + sizeof(struct DE_fba_data));
504 LO_data = (struct LO_fba_data *) (idaws + cidaw);
505 /* Locate record for all blocks for smart devices. */
506 if (private->rdc_data.mode.bits.data_chain != 0) {
507 ccw[-1].flags |= CCW_FLAG_CC;
508 locate_record(ccw++, LO_data++, rq_data_dir(req), 0, count);
509 }
510 recid = first_rec;
NeilBrown5705f702007-09-25 12:35:59 +0200511 rq_for_each_segment(bv, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800512 dst = page_address(bv.bv_page) + bv.bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (dasd_page_cache) {
514 char *copy = kmem_cache_alloc(dasd_page_cache,
Christoph Lameter441e1432006-12-06 20:33:19 -0800515 GFP_DMA | __GFP_NOWARN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (copy && rq_data_dir(req) == WRITE)
Kent Overstreet79886132013-11-23 17:19:00 -0800517 memcpy(copy + bv.bv_offset, dst, bv.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (copy)
Kent Overstreet79886132013-11-23 17:19:00 -0800519 dst = copy + bv.bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Kent Overstreet79886132013-11-23 17:19:00 -0800521 for (off = 0; off < bv.bv_len; off += blksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Locate record for stupid devices. */
523 if (private->rdc_data.mode.bits.data_chain == 0) {
524 ccw[-1].flags |= CCW_FLAG_CC;
525 locate_record(ccw, LO_data++,
526 rq_data_dir(req),
527 recid - first_rec, 1);
528 ccw->flags = CCW_FLAG_CC;
529 ccw++;
530 } else {
531 if (recid > first_rec)
532 ccw[-1].flags |= CCW_FLAG_DC;
533 else
534 ccw[-1].flags |= CCW_FLAG_CC;
535 }
536 ccw->cmd_code = cmd;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100537 ccw->count = block->bp_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (idal_is_needed(dst, blksize)) {
539 ccw->cda = (__u32)(addr_t) idaws;
540 ccw->flags = CCW_FLAG_IDA;
541 idaws = idal_create_words(idaws, dst, blksize);
542 } else {
543 ccw->cda = (__u32)(addr_t) dst;
544 ccw->flags = 0;
545 }
546 ccw++;
547 dst += blksize;
548 recid++;
549 }
550 }
Holger Smolinski13de2272009-01-09 12:14:51 +0100551 if (blk_noretry_request(req) ||
552 block->base->features & DASD_FEATURE_FAILFAST)
Horst Hummel1c01b8a2006-01-06 00:19:15 -0800553 set_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100554 cqr->startdev = memdev;
555 cqr->memdev = memdev;
556 cqr->block = block;
Stefan Haberland7c8faa82010-08-09 18:13:00 +0200557 cqr->expires = memdev->default_expires * HZ; /* default 5 minutes */
Hannes Reinecke1f1ee9a2013-01-30 09:26:12 +0000558 cqr->retries = memdev->default_retries;
Heiko Carstens1aae0562013-01-30 09:49:40 +0100559 cqr->buildclk = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 cqr->status = DASD_CQR_FILLED;
561 return cqr;
562}
563
Jan Höppner28b841b2016-06-30 13:28:57 +0200564static struct dasd_ccw_req *dasd_fba_build_cp(struct dasd_device *memdev,
565 struct dasd_block *block,
566 struct request *req)
567{
568 if (req_op(req) == REQ_OP_DISCARD || req_op(req) == REQ_OP_WRITE_ZEROES)
569 return dasd_fba_build_cp_discard(memdev, block, req);
570 else
571 return dasd_fba_build_cp_regular(memdev, block, req);
572}
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574static int
575dasd_fba_free_cp(struct dasd_ccw_req *cqr, struct request *req)
576{
Sebastian Ott543691a42016-03-04 10:34:05 +0100577 struct dasd_fba_private *private = cqr->block->base->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct ccw1 *ccw;
NeilBrown5705f702007-09-25 12:35:59 +0200579 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800580 struct bio_vec bv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 char *dst, *cda;
582 unsigned int blksize, off;
NeilBrown5705f702007-09-25 12:35:59 +0200583 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (!dasd_page_cache)
586 goto out;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100587 blksize = cqr->block->bp_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ccw = cqr->cpaddr;
589 /* Skip over define extent & locate record. */
590 ccw++;
591 if (private->rdc_data.mode.bits.data_chain != 0)
592 ccw++;
NeilBrown5705f702007-09-25 12:35:59 +0200593 rq_for_each_segment(bv, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800594 dst = page_address(bv.bv_page) + bv.bv_offset;
595 for (off = 0; off < bv.bv_len; off += blksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* Skip locate record. */
597 if (private->rdc_data.mode.bits.data_chain == 0)
598 ccw++;
599 if (dst) {
600 if (ccw->flags & CCW_FLAG_IDA)
601 cda = *((char **)((addr_t) ccw->cda));
602 else
603 cda = (char *)((addr_t) ccw->cda);
604 if (dst != cda) {
605 if (rq_data_dir(req) == READ)
Kent Overstreet79886132013-11-23 17:19:00 -0800606 memcpy(dst, cda, bv.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 kmem_cache_free(dasd_page_cache,
608 (void *)((addr_t)cda & PAGE_MASK));
609 }
610 dst = NULL;
611 }
612 ccw++;
613 }
614 }
615out:
616 status = cqr->status == DASD_CQR_DONE;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100617 dasd_sfree_request(cqr, cqr->memdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return status;
619}
620
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100621static void dasd_fba_handle_terminated_request(struct dasd_ccw_req *cqr)
622{
Hannes Reineckea2ace462013-01-30 09:26:14 +0000623 if (cqr->retries < 0)
624 cqr->status = DASD_CQR_FAILED;
625 else
626 cqr->status = DASD_CQR_FILLED;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100627};
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static int
630dasd_fba_fill_info(struct dasd_device * device,
631 struct dasd_information2_t * info)
632{
Sebastian Ott543691a42016-03-04 10:34:05 +0100633 struct dasd_fba_private *private = device->private;
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 info->label_block = 1;
636 info->FBA_layout = 1;
637 info->format = DASD_FORMAT_LDL;
Sebastian Ott543691a42016-03-04 10:34:05 +0100638 info->characteristics_size = sizeof(private->rdc_data);
639 memcpy(info->characteristics, &private->rdc_data,
640 sizeof(private->rdc_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 info->confdata_size = 0;
642 return 0;
643}
644
645static void
Stefan Haberlandaeec92c2009-07-07 16:37:06 +0200646dasd_fba_dump_sense_dbf(struct dasd_device *device, struct irb *irb,
647 char *reason)
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100648{
Stefan Haberlandaeec92c2009-07-07 16:37:06 +0200649 u64 *sense;
650
651 sense = (u64 *) dasd_get_sense(irb);
652 if (sense) {
653 DBF_DEV_EVENT(DBF_EMERG, device,
654 "%s: %s %02x%02x%02x %016llx %016llx %016llx "
655 "%016llx", reason,
656 scsw_is_tm(&irb->scsw) ? "t" : "c",
657 scsw_cc(&irb->scsw), scsw_cstat(&irb->scsw),
658 scsw_dstat(&irb->scsw), sense[0], sense[1],
659 sense[2], sense[3]);
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100660 } else {
661 DBF_DEV_EVENT(DBF_EMERG, device, "%s",
662 "SORRY - NO VALID SENSE AVAILABLE\n");
663 }
664}
665
666
667static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668dasd_fba_dump_sense(struct dasd_device *device, struct dasd_ccw_req * req,
669 struct irb *irb)
670{
671 char *page;
672 struct ccw1 *act, *end, *last;
673 int len, sl, sct, count;
674
675 page = (char *) get_zeroed_page(GFP_ATOMIC);
676 if (page == NULL) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100677 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 "No memory to dump sense data");
679 return;
680 }
Sebastian Ott773bab42012-10-23 20:28:37 +0200681 len = sprintf(page, PRINTK_HEADER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 " I/O status report for device %s:\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +0200683 dev_name(&device->cdev->dev));
Sebastian Ott773bab42012-10-23 20:28:37 +0200684 len += sprintf(page + len, PRINTK_HEADER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 " in req: %p CS: 0x%02X DS: 0x%02X\n", req,
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200686 irb->scsw.cmd.cstat, irb->scsw.cmd.dstat);
Sebastian Ott773bab42012-10-23 20:28:37 +0200687 len += sprintf(page + len, PRINTK_HEADER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 " device %s: Failing CCW: %p\n",
Kay Sievers2a0217d2008-10-10 21:33:09 +0200689 dev_name(&device->cdev->dev),
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200690 (void *) (addr_t) irb->scsw.cmd.cpa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (irb->esw.esw0.erw.cons) {
692 for (sl = 0; sl < 4; sl++) {
Sebastian Ott773bab42012-10-23 20:28:37 +0200693 len += sprintf(page + len, PRINTK_HEADER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 " Sense(hex) %2d-%2d:",
695 (8 * sl), ((8 * sl) + 7));
696
697 for (sct = 0; sct < 8; sct++) {
698 len += sprintf(page + len, " %02x",
699 irb->ecw[8 * sl + sct]);
700 }
701 len += sprintf(page + len, "\n");
702 }
703 } else {
Sebastian Ott773bab42012-10-23 20:28:37 +0200704 len += sprintf(page + len, PRINTK_HEADER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 " SORRY - NO VALID SENSE AVAILABLE\n");
706 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100707 printk(KERN_ERR "%s", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* dump the Channel Program */
710 /* print first CCWs (maximum 8) */
711 act = req->cpaddr;
712 for (last = act; last->flags & (CCW_FLAG_CC | CCW_FLAG_DC); last++);
713 end = min(act + 8, last);
Sebastian Ott773bab42012-10-23 20:28:37 +0200714 len = sprintf(page, PRINTK_HEADER " Related CP in req: %p\n", req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 while (act <= end) {
Sebastian Ott773bab42012-10-23 20:28:37 +0200716 len += sprintf(page + len, PRINTK_HEADER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 " CCW %p: %08X %08X DAT:",
718 act, ((int *) act)[0], ((int *) act)[1]);
719 for (count = 0; count < 32 && count < act->count;
720 count += sizeof(int))
721 len += sprintf(page + len, " %08X",
722 ((int *) (addr_t) act->cda)
723 [(count>>2)]);
724 len += sprintf(page + len, "\n");
725 act++;
726 }
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100727 printk(KERN_ERR "%s", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729
730 /* print failing CCW area */
731 len = 0;
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200732 if (act < ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2) {
733 act = ((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa) - 2;
Sebastian Ott773bab42012-10-23 20:28:37 +0200734 len += sprintf(page + len, PRINTK_HEADER "......\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Peter Oberparleiter23d805b62008-07-14 09:58:50 +0200736 end = min((struct ccw1 *)(addr_t) irb->scsw.cmd.cpa + 2, last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 while (act <= end) {
Sebastian Ott773bab42012-10-23 20:28:37 +0200738 len += sprintf(page + len, PRINTK_HEADER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 " CCW %p: %08X %08X DAT:",
740 act, ((int *) act)[0], ((int *) act)[1]);
741 for (count = 0; count < 32 && count < act->count;
742 count += sizeof(int))
743 len += sprintf(page + len, " %08X",
744 ((int *) (addr_t) act->cda)
745 [(count>>2)]);
746 len += sprintf(page + len, "\n");
747 act++;
748 }
749
750 /* print last CCWs */
751 if (act < last - 2) {
752 act = last - 2;
Sebastian Ott773bab42012-10-23 20:28:37 +0200753 len += sprintf(page + len, PRINTK_HEADER "......\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 while (act <= last) {
Sebastian Ott773bab42012-10-23 20:28:37 +0200756 len += sprintf(page + len, PRINTK_HEADER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 " CCW %p: %08X %08X DAT:",
758 act, ((int *) act)[0], ((int *) act)[1]);
759 for (count = 0; count < 32 && count < act->count;
760 count += sizeof(int))
761 len += sprintf(page + len, " %08X",
762 ((int *) (addr_t) act->cda)
763 [(count>>2)]);
764 len += sprintf(page + len, "\n");
765 act++;
766 }
767 if (len > 0)
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100768 printk(KERN_ERR "%s", page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 free_page((unsigned long) page);
770}
771
772/*
773 * max_blocks is dependent on the amount of storage that is available
774 * in the static io buffer for each device. Currently each device has
775 * 8192 bytes (=2 pages). For 64 bit one dasd_mchunkt_t structure has
776 * 24 bytes, the struct dasd_ccw_req has 136 bytes and each block can use
777 * up to 16 bytes (8 for the ccw and 8 for the idal pointer). In
Horst Hummel138c0142006-06-29 14:58:12 +0200778 * addition we have one define extent ccw + 16 bytes of data and a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * locate record ccw for each block (stupid devices!) + 16 bytes of data.
780 * That makes:
781 * (8192 - 24 - 136 - 8 - 16) / 40 = 200.2 blocks at maximum.
782 * We want to fit two into the available memory so that we can immediately
783 * start the next request if one finishes off. That makes 100.1 blocks
784 * for one request. Give a little safety and the result is 96.
785 */
786static struct dasd_discipline dasd_fba_discipline = {
787 .owner = THIS_MODULE,
788 .name = "FBA ",
789 .ebcname = "FBA ",
790 .max_blocks = 96,
791 .check_device = dasd_fba_check_characteristics,
792 .do_analysis = dasd_fba_do_analysis,
Stefan Weinhubera4d26c62011-01-05 12:48:03 +0100793 .verify_path = dasd_generic_verify_path,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 .fill_geometry = dasd_fba_fill_geometry,
795 .start_IO = dasd_start_IO,
796 .term_IO = dasd_term_IO,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100797 .handle_terminated_request = dasd_fba_handle_terminated_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 .erp_action = dasd_fba_erp_action,
799 .erp_postaction = dasd_fba_erp_postaction,
Stefan Weinhuber5a27e602011-01-05 12:48:04 +0100800 .check_for_device_change = dasd_fba_check_for_device_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .build_cp = dasd_fba_build_cp,
802 .free_cp = dasd_fba_free_cp,
803 .dump_sense = dasd_fba_dump_sense,
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100804 .dump_sense_dbf = dasd_fba_dump_sense_dbf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 .fill_info = dasd_fba_fill_info,
806};
807
808static int __init
809dasd_fba_init(void)
810{
Sebastian Ott736e6ea2009-06-12 10:26:38 +0200811 int ret;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 ASCEBC(dasd_fba_discipline.ebcname, 4);
Sebastian Ott736e6ea2009-06-12 10:26:38 +0200814 ret = ccw_driver_register(&dasd_fba_driver);
815 if (!ret)
816 wait_for_device_probe();
817
818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821static void __exit
822dasd_fba_cleanup(void)
823{
824 ccw_driver_unregister(&dasd_fba_driver);
825}
826
827module_init(dasd_fba_init);
828module_exit(dasd_fba_cleanup);