blob: 5b2abadea0941f29fdf06e3829eb80496b53b20d [file] [log] [blame]
Sebastian Ottf30664e2012-08-28 16:50:38 +02001/*
2 * Block driver for s390 storage class memory.
3 *
4 * Copyright IBM Corp. 2012
5 * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com>
6 */
7
8#define KMSG_COMPONENT "scm_block"
9#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10
11#include <linux/interrupt.h>
12#include <linux/spinlock.h>
Sebastian Ott9d4df772014-12-05 16:32:13 +010013#include <linux/mempool.h>
Sebastian Ottf30664e2012-08-28 16:50:38 +020014#include <linux/module.h>
15#include <linux/blkdev.h>
16#include <linux/genhd.h>
17#include <linux/slab.h>
18#include <linux/list.h>
19#include <asm/eadm.h>
20#include "scm_blk.h"
21
22debug_info_t *scm_debug;
23static int scm_major;
Sebastian Ott9d4df772014-12-05 16:32:13 +010024static mempool_t *aidaw_pool;
Sebastian Ottf30664e2012-08-28 16:50:38 +020025static DEFINE_SPINLOCK(list_lock);
26static LIST_HEAD(inactive_requests);
27static unsigned int nr_requests = 64;
28static atomic_t nr_devices = ATOMIC_INIT(0);
29module_param(nr_requests, uint, S_IRUGO);
30MODULE_PARM_DESC(nr_requests, "Number of parallel requests.");
31
32MODULE_DESCRIPTION("Block driver for s390 storage class memory.");
33MODULE_LICENSE("GPL");
34MODULE_ALIAS("scm:scmdev*");
35
36static void __scm_free_rq(struct scm_request *scmrq)
37{
38 struct aob_rq_header *aobrq = to_aobrq(scmrq);
39
40 free_page((unsigned long) scmrq->aob);
Sebastian Ott0d804b22012-08-28 16:51:19 +020041 __scm_free_rq_cluster(scmrq);
Sebastian Ottf30664e2012-08-28 16:50:38 +020042 kfree(aobrq);
43}
44
45static void scm_free_rqs(void)
46{
47 struct list_head *iter, *safe;
48 struct scm_request *scmrq;
49
50 spin_lock_irq(&list_lock);
51 list_for_each_safe(iter, safe, &inactive_requests) {
52 scmrq = list_entry(iter, struct scm_request, list);
53 list_del(&scmrq->list);
54 __scm_free_rq(scmrq);
55 }
56 spin_unlock_irq(&list_lock);
Sebastian Ott9d4df772014-12-05 16:32:13 +010057
58 mempool_destroy(aidaw_pool);
Sebastian Ottf30664e2012-08-28 16:50:38 +020059}
60
61static int __scm_alloc_rq(void)
62{
63 struct aob_rq_header *aobrq;
64 struct scm_request *scmrq;
65
66 aobrq = kzalloc(sizeof(*aobrq) + sizeof(*scmrq), GFP_KERNEL);
67 if (!aobrq)
68 return -ENOMEM;
69
70 scmrq = (void *) aobrq->data;
Sebastian Ottf30664e2012-08-28 16:50:38 +020071 scmrq->aob = (void *) get_zeroed_page(GFP_DMA);
Sebastian Ott9d4df772014-12-05 16:32:13 +010072 if (!scmrq->aob) {
Sebastian Ottf30664e2012-08-28 16:50:38 +020073 __scm_free_rq(scmrq);
74 return -ENOMEM;
75 }
Sebastian Ott0d804b22012-08-28 16:51:19 +020076
77 if (__scm_alloc_rq_cluster(scmrq)) {
78 __scm_free_rq(scmrq);
79 return -ENOMEM;
80 }
81
Sebastian Ottf30664e2012-08-28 16:50:38 +020082 INIT_LIST_HEAD(&scmrq->list);
83 spin_lock_irq(&list_lock);
84 list_add(&scmrq->list, &inactive_requests);
85 spin_unlock_irq(&list_lock);
86
87 return 0;
88}
89
90static int scm_alloc_rqs(unsigned int nrqs)
91{
92 int ret = 0;
93
Sebastian Ott9d4df772014-12-05 16:32:13 +010094 aidaw_pool = mempool_create_page_pool(max(nrqs/8, 1U), 0);
95 if (!aidaw_pool)
96 return -ENOMEM;
97
Sebastian Ottf30664e2012-08-28 16:50:38 +020098 while (nrqs-- && !ret)
99 ret = __scm_alloc_rq();
100
101 return ret;
102}
103
104static struct scm_request *scm_request_fetch(void)
105{
106 struct scm_request *scmrq = NULL;
107
108 spin_lock(&list_lock);
109 if (list_empty(&inactive_requests))
110 goto out;
111 scmrq = list_first_entry(&inactive_requests, struct scm_request, list);
112 list_del(&scmrq->list);
113out:
114 spin_unlock(&list_lock);
115 return scmrq;
116}
117
118static void scm_request_done(struct scm_request *scmrq)
119{
Sebastian Ott9d4df772014-12-05 16:32:13 +0100120 struct msb *msb = &scmrq->aob->msb[0];
121 u64 aidaw = msb->data_addr;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200122 unsigned long flags;
123
Sebastian Ott9d4df772014-12-05 16:32:13 +0100124 if ((msb->flags & MSB_FLAG_IDA) && aidaw)
125 mempool_free(virt_to_page(aidaw), aidaw_pool);
126
Sebastian Ottf30664e2012-08-28 16:50:38 +0200127 spin_lock_irqsave(&list_lock, flags);
128 list_add(&scmrq->list, &inactive_requests);
129 spin_unlock_irqrestore(&list_lock, flags);
130}
131
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100132static bool scm_permit_request(struct scm_blk_dev *bdev, struct request *req)
133{
134 return rq_data_dir(req) != WRITE || bdev->state != SCM_WR_PROHIBIT;
135}
136
Sebastian Ott9d4df772014-12-05 16:32:13 +0100137struct aidaw *scm_aidaw_alloc(void)
138{
139 struct page *page = mempool_alloc(aidaw_pool, GFP_ATOMIC);
140
141 return page ? page_address(page) : NULL;
142}
143
144static int scm_request_prepare(struct scm_request *scmrq)
Sebastian Ottf30664e2012-08-28 16:50:38 +0200145{
146 struct scm_blk_dev *bdev = scmrq->bdev;
147 struct scm_device *scmdev = bdev->gendisk->private_data;
Sebastian Ott9d4df772014-12-05 16:32:13 +0100148 struct aidaw *aidaw = scm_aidaw_alloc();
Sebastian Ottf30664e2012-08-28 16:50:38 +0200149 struct msb *msb = &scmrq->aob->msb[0];
150 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -0800151 struct bio_vec bv;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200152
Sebastian Ott9d4df772014-12-05 16:32:13 +0100153 if (!aidaw)
154 return -ENOMEM;
155
156 memset(aidaw, 0, PAGE_SIZE);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200157 msb->bs = MSB_BS_4K;
158 scmrq->aob->request.msb_count = 1;
159 msb->scm_addr = scmdev->address +
160 ((u64) blk_rq_pos(scmrq->request) << 9);
161 msb->oc = (rq_data_dir(scmrq->request) == READ) ?
162 MSB_OC_READ : MSB_OC_WRITE;
163 msb->flags |= MSB_FLAG_IDA;
164 msb->data_addr = (u64) aidaw;
165
166 rq_for_each_segment(bv, scmrq->request, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800167 WARN_ON(bv.bv_offset);
168 msb->blk_count += bv.bv_len >> 12;
169 aidaw->data_addr = (u64) page_address(bv.bv_page);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200170 aidaw++;
171 }
Sebastian Ott9d4df772014-12-05 16:32:13 +0100172
173 return 0;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200174}
175
176static inline void scm_request_init(struct scm_blk_dev *bdev,
177 struct scm_request *scmrq,
178 struct request *req)
179{
180 struct aob_rq_header *aobrq = to_aobrq(scmrq);
181 struct aob *aob = scmrq->aob;
182
183 memset(aob, 0, sizeof(*aob));
Sebastian Ottf30664e2012-08-28 16:50:38 +0200184 aobrq->scmdev = bdev->scmdev;
185 aob->request.cmd_code = ARQB_CMD_MOVE;
186 aob->request.data = (u64) aobrq;
187 scmrq->request = req;
188 scmrq->bdev = bdev;
189 scmrq->retries = 4;
190 scmrq->error = 0;
Sebastian Ott0d804b22012-08-28 16:51:19 +0200191 scm_request_cluster_init(scmrq);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200192}
193
194static void scm_ensure_queue_restart(struct scm_blk_dev *bdev)
195{
196 if (atomic_read(&bdev->queued_reqs)) {
197 /* Queue restart is triggered by the next interrupt. */
198 return;
199 }
200 blk_delay_queue(bdev->rq, SCM_QUEUE_DELAY);
201}
202
Sebastian Ott0d804b22012-08-28 16:51:19 +0200203void scm_request_requeue(struct scm_request *scmrq)
Sebastian Ottf30664e2012-08-28 16:50:38 +0200204{
205 struct scm_blk_dev *bdev = scmrq->bdev;
206
Sebastian Ott0d804b22012-08-28 16:51:19 +0200207 scm_release_cluster(scmrq);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200208 blk_requeue_request(bdev->rq, scmrq->request);
Sebastian Ott8360cb52013-02-28 12:07:27 +0100209 atomic_dec(&bdev->queued_reqs);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200210 scm_request_done(scmrq);
211 scm_ensure_queue_restart(bdev);
212}
213
Sebastian Ott0d804b22012-08-28 16:51:19 +0200214void scm_request_finish(struct scm_request *scmrq)
Sebastian Ottf30664e2012-08-28 16:50:38 +0200215{
Sebastian Ott8360cb52013-02-28 12:07:27 +0100216 struct scm_blk_dev *bdev = scmrq->bdev;
217
Sebastian Ott0d804b22012-08-28 16:51:19 +0200218 scm_release_cluster(scmrq);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200219 blk_end_request_all(scmrq->request, scmrq->error);
Sebastian Ott8360cb52013-02-28 12:07:27 +0100220 atomic_dec(&bdev->queued_reqs);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200221 scm_request_done(scmrq);
222}
223
224static void scm_blk_request(struct request_queue *rq)
225{
226 struct scm_device *scmdev = rq->queuedata;
227 struct scm_blk_dev *bdev = dev_get_drvdata(&scmdev->dev);
228 struct scm_request *scmrq;
229 struct request *req;
230 int ret;
231
232 while ((req = blk_peek_request(rq))) {
Steffen Maierde9587a2013-11-05 12:59:46 +0100233 if (req->cmd_type != REQ_TYPE_FS) {
234 blk_start_request(req);
235 blk_dump_rq_flags(req, KMSG_COMPONENT " bad request");
236 blk_end_request_all(req, -EIO);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200237 continue;
Steffen Maierde9587a2013-11-05 12:59:46 +0100238 }
Sebastian Ottf30664e2012-08-28 16:50:38 +0200239
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100240 if (!scm_permit_request(bdev, req)) {
241 scm_ensure_queue_restart(bdev);
242 return;
243 }
Sebastian Ottf30664e2012-08-28 16:50:38 +0200244 scmrq = scm_request_fetch();
245 if (!scmrq) {
246 SCM_LOG(5, "no request");
247 scm_ensure_queue_restart(bdev);
248 return;
249 }
250 scm_request_init(bdev, scmrq, req);
Sebastian Ott0d804b22012-08-28 16:51:19 +0200251 if (!scm_reserve_cluster(scmrq)) {
252 SCM_LOG(5, "cluster busy");
253 scm_request_done(scmrq);
254 return;
255 }
256 if (scm_need_cluster_request(scmrq)) {
Sebastian Ott8360cb52013-02-28 12:07:27 +0100257 atomic_inc(&bdev->queued_reqs);
Sebastian Ott0d804b22012-08-28 16:51:19 +0200258 blk_start_request(req);
259 scm_initiate_cluster_request(scmrq);
260 return;
261 }
Sebastian Ott9d4df772014-12-05 16:32:13 +0100262
263 if (scm_request_prepare(scmrq)) {
264 SCM_LOG(5, "no aidaw");
265 scm_release_cluster(scmrq);
266 scm_request_done(scmrq);
267 scm_ensure_queue_restart(bdev);
268 return;
269 }
270
Sebastian Ott8360cb52013-02-28 12:07:27 +0100271 atomic_inc(&bdev->queued_reqs);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200272 blk_start_request(req);
273
Sebastian Ott605c3692013-11-14 10:44:56 +0100274 ret = eadm_start_aob(scmrq->aob);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200275 if (ret) {
276 SCM_LOG(5, "no subchannel");
277 scm_request_requeue(scmrq);
278 return;
279 }
Sebastian Ottf30664e2012-08-28 16:50:38 +0200280 }
281}
282
283static void __scmrq_log_error(struct scm_request *scmrq)
284{
285 struct aob *aob = scmrq->aob;
286
287 if (scmrq->error == -ETIMEDOUT)
288 SCM_LOG(1, "Request timeout");
289 else {
290 SCM_LOG(1, "Request error");
291 SCM_LOG_HEX(1, &aob->response, sizeof(aob->response));
292 }
293 if (scmrq->retries)
294 SCM_LOG(1, "Retry request");
295 else
296 pr_err("An I/O operation to SCM failed with rc=%d\n",
297 scmrq->error);
298}
299
300void scm_blk_irq(struct scm_device *scmdev, void *data, int error)
301{
302 struct scm_request *scmrq = data;
303 struct scm_blk_dev *bdev = scmrq->bdev;
304
305 scmrq->error = error;
306 if (error)
307 __scmrq_log_error(scmrq);
308
309 spin_lock(&bdev->lock);
310 list_add_tail(&scmrq->list, &bdev->finished_requests);
311 spin_unlock(&bdev->lock);
312 tasklet_hi_schedule(&bdev->tasklet);
313}
314
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100315static void scm_blk_handle_error(struct scm_request *scmrq)
316{
317 struct scm_blk_dev *bdev = scmrq->bdev;
318 unsigned long flags;
319
320 if (scmrq->error != -EIO)
321 goto restart;
322
323 /* For -EIO the response block is valid. */
324 switch (scmrq->aob->response.eqc) {
325 case EQC_WR_PROHIBIT:
326 spin_lock_irqsave(&bdev->lock, flags);
327 if (bdev->state != SCM_WR_PROHIBIT)
Sebastian Ott3bff6032013-03-18 16:01:30 +0100328 pr_info("%lx: Write access to the SCM increment is suspended\n",
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100329 (unsigned long) bdev->scmdev->address);
330 bdev->state = SCM_WR_PROHIBIT;
331 spin_unlock_irqrestore(&bdev->lock, flags);
332 goto requeue;
333 default:
334 break;
335 }
336
337restart:
Sebastian Ott605c3692013-11-14 10:44:56 +0100338 if (!eadm_start_aob(scmrq->aob))
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100339 return;
340
341requeue:
342 spin_lock_irqsave(&bdev->rq_lock, flags);
343 scm_request_requeue(scmrq);
344 spin_unlock_irqrestore(&bdev->rq_lock, flags);
345}
346
Sebastian Ottf30664e2012-08-28 16:50:38 +0200347static void scm_blk_tasklet(struct scm_blk_dev *bdev)
348{
349 struct scm_request *scmrq;
350 unsigned long flags;
351
352 spin_lock_irqsave(&bdev->lock, flags);
353 while (!list_empty(&bdev->finished_requests)) {
354 scmrq = list_first_entry(&bdev->finished_requests,
355 struct scm_request, list);
356 list_del(&scmrq->list);
357 spin_unlock_irqrestore(&bdev->lock, flags);
358
359 if (scmrq->error && scmrq->retries-- > 0) {
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100360 scm_blk_handle_error(scmrq);
361
Sebastian Ottf30664e2012-08-28 16:50:38 +0200362 /* Request restarted or requeued, handle next. */
363 spin_lock_irqsave(&bdev->lock, flags);
364 continue;
365 }
Sebastian Ott0d804b22012-08-28 16:51:19 +0200366
367 if (scm_test_cluster_request(scmrq)) {
368 scm_cluster_request_irq(scmrq);
369 spin_lock_irqsave(&bdev->lock, flags);
370 continue;
371 }
372
Sebastian Ottf30664e2012-08-28 16:50:38 +0200373 scm_request_finish(scmrq);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200374 spin_lock_irqsave(&bdev->lock, flags);
375 }
376 spin_unlock_irqrestore(&bdev->lock, flags);
377 /* Look out for more requests. */
378 blk_run_queue(bdev->rq);
379}
380
Sebastian Ott605c3692013-11-14 10:44:56 +0100381static const struct block_device_operations scm_blk_devops = {
382 .owner = THIS_MODULE,
383};
384
Sebastian Ottf30664e2012-08-28 16:50:38 +0200385int scm_blk_dev_setup(struct scm_blk_dev *bdev, struct scm_device *scmdev)
386{
387 struct request_queue *rq;
388 int len, ret = -ENOMEM;
389 unsigned int devindex, nr_max_blk;
390
391 devindex = atomic_inc_return(&nr_devices) - 1;
392 /* scma..scmz + scmaa..scmzz */
393 if (devindex > 701) {
394 ret = -ENODEV;
395 goto out;
396 }
397
398 bdev->scmdev = scmdev;
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100399 bdev->state = SCM_OPER;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200400 spin_lock_init(&bdev->rq_lock);
401 spin_lock_init(&bdev->lock);
402 INIT_LIST_HEAD(&bdev->finished_requests);
403 atomic_set(&bdev->queued_reqs, 0);
404 tasklet_init(&bdev->tasklet,
405 (void (*)(unsigned long)) scm_blk_tasklet,
406 (unsigned long) bdev);
407
408 rq = blk_init_queue(scm_blk_request, &bdev->rq_lock);
409 if (!rq)
410 goto out;
411
412 bdev->rq = rq;
413 nr_max_blk = min(scmdev->nr_max_block,
414 (unsigned int) (PAGE_SIZE / sizeof(struct aidaw)));
415
416 blk_queue_logical_block_size(rq, 1 << 12);
417 blk_queue_max_hw_sectors(rq, nr_max_blk << 3); /* 8 * 512 = blk_size */
418 blk_queue_max_segments(rq, nr_max_blk);
419 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, rq);
Mike Snitzerb277da02014-10-04 10:55:32 -0600420 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, rq);
Sebastian Ott0d804b22012-08-28 16:51:19 +0200421 scm_blk_dev_cluster_setup(bdev);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200422
423 bdev->gendisk = alloc_disk(SCM_NR_PARTS);
424 if (!bdev->gendisk)
425 goto out_queue;
426
427 rq->queuedata = scmdev;
428 bdev->gendisk->driverfs_dev = &scmdev->dev;
429 bdev->gendisk->private_data = scmdev;
430 bdev->gendisk->fops = &scm_blk_devops;
431 bdev->gendisk->queue = rq;
432 bdev->gendisk->major = scm_major;
433 bdev->gendisk->first_minor = devindex * SCM_NR_PARTS;
434
435 len = snprintf(bdev->gendisk->disk_name, DISK_NAME_LEN, "scm");
436 if (devindex > 25) {
437 len += snprintf(bdev->gendisk->disk_name + len,
438 DISK_NAME_LEN - len, "%c",
439 'a' + (devindex / 26) - 1);
440 devindex = devindex % 26;
441 }
442 snprintf(bdev->gendisk->disk_name + len, DISK_NAME_LEN - len, "%c",
443 'a' + devindex);
444
445 /* 512 byte sectors */
446 set_capacity(bdev->gendisk, scmdev->size >> 9);
447 add_disk(bdev->gendisk);
448 return 0;
449
450out_queue:
451 blk_cleanup_queue(rq);
452out:
453 atomic_dec(&nr_devices);
454 return ret;
455}
456
457void scm_blk_dev_cleanup(struct scm_blk_dev *bdev)
458{
459 tasklet_kill(&bdev->tasklet);
460 del_gendisk(bdev->gendisk);
461 blk_cleanup_queue(bdev->gendisk->queue);
462 put_disk(bdev->gendisk);
463}
464
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100465void scm_blk_set_available(struct scm_blk_dev *bdev)
466{
467 unsigned long flags;
468
469 spin_lock_irqsave(&bdev->lock, flags);
470 if (bdev->state == SCM_WR_PROHIBIT)
Sebastian Ott3bff6032013-03-18 16:01:30 +0100471 pr_info("%lx: Write access to the SCM increment is restored\n",
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100472 (unsigned long) bdev->scmdev->address);
473 bdev->state = SCM_OPER;
474 spin_unlock_irqrestore(&bdev->lock, flags);
475}
476
Sebastian Ottf30664e2012-08-28 16:50:38 +0200477static int __init scm_blk_init(void)
478{
Sebastian Ott0d804b22012-08-28 16:51:19 +0200479 int ret = -EINVAL;
480
481 if (!scm_cluster_size_valid())
482 goto out;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200483
484 ret = register_blkdev(0, "scm");
485 if (ret < 0)
486 goto out;
487
488 scm_major = ret;
Wei Yongjun94f98522013-03-20 13:40:54 +0100489 ret = scm_alloc_rqs(nr_requests);
490 if (ret)
Sebastian Ottfff60fa2013-04-25 13:03:18 +0200491 goto out_free;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200492
493 scm_debug = debug_register("scm_log", 16, 1, 16);
Wei Yongjun94f98522013-03-20 13:40:54 +0100494 if (!scm_debug) {
495 ret = -ENOMEM;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200496 goto out_free;
Wei Yongjun94f98522013-03-20 13:40:54 +0100497 }
Sebastian Ottf30664e2012-08-28 16:50:38 +0200498
499 debug_register_view(scm_debug, &debug_hex_ascii_view);
500 debug_set_level(scm_debug, 2);
501
502 ret = scm_drv_init();
503 if (ret)
504 goto out_dbf;
505
506 return ret;
507
508out_dbf:
509 debug_unregister(scm_debug);
510out_free:
511 scm_free_rqs();
Sebastian Ottf30664e2012-08-28 16:50:38 +0200512 unregister_blkdev(scm_major, "scm");
513out:
514 return ret;
515}
516module_init(scm_blk_init);
517
518static void __exit scm_blk_cleanup(void)
519{
520 scm_drv_cleanup();
521 debug_unregister(scm_debug);
522 scm_free_rqs();
523 unregister_blkdev(scm_major, "scm");
524}
525module_exit(scm_blk_cleanup);