blob: cd27cb92ac6dda23af02d7b7dad2c6d8ad1f2447 [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{
120 unsigned long flags;
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100121 struct msb *msb;
122 u64 aidaw;
123 int i;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200124
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100125 for (i = 0; i < SCM_RQ_PER_IO && scmrq->request[i]; i++) {
126 msb = &scmrq->aob->msb[i];
127 aidaw = msb->data_addr;
128
129 if ((msb->flags & MSB_FLAG_IDA) && aidaw &&
130 IS_ALIGNED(aidaw, PAGE_SIZE))
131 mempool_free(virt_to_page(aidaw), aidaw_pool);
132 }
Sebastian Ott9d4df772014-12-05 16:32:13 +0100133
Sebastian Ottf30664e2012-08-28 16:50:38 +0200134 spin_lock_irqsave(&list_lock, flags);
135 list_add(&scmrq->list, &inactive_requests);
136 spin_unlock_irqrestore(&list_lock, flags);
137}
138
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100139static bool scm_permit_request(struct scm_blk_dev *bdev, struct request *req)
140{
141 return rq_data_dir(req) != WRITE || bdev->state != SCM_WR_PROHIBIT;
142}
143
Sebastian Ottde88d0d2014-12-05 16:41:47 +0100144static inline struct aidaw *scm_aidaw_alloc(void)
Sebastian Ott9d4df772014-12-05 16:32:13 +0100145{
146 struct page *page = mempool_alloc(aidaw_pool, GFP_ATOMIC);
147
148 return page ? page_address(page) : NULL;
149}
150
Sebastian Ottde88d0d2014-12-05 16:41:47 +0100151static inline unsigned long scm_aidaw_bytes(struct aidaw *aidaw)
152{
153 unsigned long _aidaw = (unsigned long) aidaw;
154 unsigned long bytes = ALIGN(_aidaw, PAGE_SIZE) - _aidaw;
155
156 return (bytes / sizeof(*aidaw)) * PAGE_SIZE;
157}
158
159struct aidaw *scm_aidaw_fetch(struct scm_request *scmrq, unsigned int bytes)
160{
161 struct aidaw *aidaw;
162
163 if (scm_aidaw_bytes(scmrq->next_aidaw) >= bytes)
164 return scmrq->next_aidaw;
165
166 aidaw = scm_aidaw_alloc();
167 if (aidaw)
168 memset(aidaw, 0, PAGE_SIZE);
169 return aidaw;
170}
171
Sebastian Ott9d4df772014-12-05 16:32:13 +0100172static int scm_request_prepare(struct scm_request *scmrq)
Sebastian Ottf30664e2012-08-28 16:50:38 +0200173{
174 struct scm_blk_dev *bdev = scmrq->bdev;
175 struct scm_device *scmdev = bdev->gendisk->private_data;
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100176 int pos = scmrq->aob->request.msb_count;
177 struct msb *msb = &scmrq->aob->msb[pos];
178 struct request *req = scmrq->request[pos];
Sebastian Ottf30664e2012-08-28 16:50:38 +0200179 struct req_iterator iter;
Sebastian Ottde88d0d2014-12-05 16:41:47 +0100180 struct aidaw *aidaw;
Kent Overstreet79886132013-11-23 17:19:00 -0800181 struct bio_vec bv;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200182
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100183 aidaw = scm_aidaw_fetch(scmrq, blk_rq_bytes(req));
Sebastian Ott9d4df772014-12-05 16:32:13 +0100184 if (!aidaw)
185 return -ENOMEM;
186
Sebastian Ottf30664e2012-08-28 16:50:38 +0200187 msb->bs = MSB_BS_4K;
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100188 scmrq->aob->request.msb_count++;
189 msb->scm_addr = scmdev->address + ((u64) blk_rq_pos(req) << 9);
190 msb->oc = (rq_data_dir(req) == READ) ? MSB_OC_READ : MSB_OC_WRITE;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200191 msb->flags |= MSB_FLAG_IDA;
192 msb->data_addr = (u64) aidaw;
193
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100194 rq_for_each_segment(bv, req, iter) {
Kent Overstreet79886132013-11-23 17:19:00 -0800195 WARN_ON(bv.bv_offset);
196 msb->blk_count += bv.bv_len >> 12;
197 aidaw->data_addr = (u64) page_address(bv.bv_page);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200198 aidaw++;
199 }
Sebastian Ott9d4df772014-12-05 16:32:13 +0100200
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100201 scmrq->next_aidaw = aidaw;
Sebastian Ott9d4df772014-12-05 16:32:13 +0100202 return 0;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200203}
204
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100205static inline void scm_request_set(struct scm_request *scmrq,
206 struct request *req)
207{
208 scmrq->request[scmrq->aob->request.msb_count] = req;
209}
210
Sebastian Ottf30664e2012-08-28 16:50:38 +0200211static inline void scm_request_init(struct scm_blk_dev *bdev,
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100212 struct scm_request *scmrq)
Sebastian Ottf30664e2012-08-28 16:50:38 +0200213{
214 struct aob_rq_header *aobrq = to_aobrq(scmrq);
215 struct aob *aob = scmrq->aob;
216
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100217 memset(scmrq->request, 0, sizeof(scmrq->request));
Sebastian Ottf30664e2012-08-28 16:50:38 +0200218 memset(aob, 0, sizeof(*aob));
Sebastian Ottf30664e2012-08-28 16:50:38 +0200219 aobrq->scmdev = bdev->scmdev;
220 aob->request.cmd_code = ARQB_CMD_MOVE;
221 aob->request.data = (u64) aobrq;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200222 scmrq->bdev = bdev;
223 scmrq->retries = 4;
224 scmrq->error = 0;
Sebastian Ottde88d0d2014-12-05 16:41:47 +0100225 /* We don't use all msbs - place aidaws at the end of the aob page. */
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100226 scmrq->next_aidaw = (void *) &aob->msb[SCM_RQ_PER_IO];
Sebastian Ott0d804b22012-08-28 16:51:19 +0200227 scm_request_cluster_init(scmrq);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200228}
229
230static void scm_ensure_queue_restart(struct scm_blk_dev *bdev)
231{
232 if (atomic_read(&bdev->queued_reqs)) {
233 /* Queue restart is triggered by the next interrupt. */
234 return;
235 }
236 blk_delay_queue(bdev->rq, SCM_QUEUE_DELAY);
237}
238
Sebastian Ott0d804b22012-08-28 16:51:19 +0200239void scm_request_requeue(struct scm_request *scmrq)
Sebastian Ottf30664e2012-08-28 16:50:38 +0200240{
241 struct scm_blk_dev *bdev = scmrq->bdev;
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100242 int i;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200243
Sebastian Ott0d804b22012-08-28 16:51:19 +0200244 scm_release_cluster(scmrq);
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100245 for (i = 0; i < SCM_RQ_PER_IO && scmrq->request[i]; i++)
246 blk_requeue_request(bdev->rq, scmrq->request[i]);
247
Sebastian Ott8360cb52013-02-28 12:07:27 +0100248 atomic_dec(&bdev->queued_reqs);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200249 scm_request_done(scmrq);
250 scm_ensure_queue_restart(bdev);
251}
252
Sebastian Ott0d804b22012-08-28 16:51:19 +0200253void scm_request_finish(struct scm_request *scmrq)
Sebastian Ottf30664e2012-08-28 16:50:38 +0200254{
Sebastian Ott8360cb52013-02-28 12:07:27 +0100255 struct scm_blk_dev *bdev = scmrq->bdev;
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100256 int i;
Sebastian Ott8360cb52013-02-28 12:07:27 +0100257
Sebastian Ott0d804b22012-08-28 16:51:19 +0200258 scm_release_cluster(scmrq);
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100259 for (i = 0; i < SCM_RQ_PER_IO && scmrq->request[i]; i++)
260 blk_end_request_all(scmrq->request[i], scmrq->error);
261
Sebastian Ott8360cb52013-02-28 12:07:27 +0100262 atomic_dec(&bdev->queued_reqs);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200263 scm_request_done(scmrq);
264}
265
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100266static int scm_request_start(struct scm_request *scmrq)
267{
268 struct scm_blk_dev *bdev = scmrq->bdev;
269 int ret;
270
271 atomic_inc(&bdev->queued_reqs);
272 if (!scmrq->aob->request.msb_count) {
273 scm_request_requeue(scmrq);
274 return -EINVAL;
275 }
276
277 ret = eadm_start_aob(scmrq->aob);
278 if (ret) {
279 SCM_LOG(5, "no subchannel");
280 scm_request_requeue(scmrq);
281 }
282 return ret;
283}
284
Sebastian Ottf30664e2012-08-28 16:50:38 +0200285static void scm_blk_request(struct request_queue *rq)
286{
287 struct scm_device *scmdev = rq->queuedata;
288 struct scm_blk_dev *bdev = dev_get_drvdata(&scmdev->dev);
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100289 struct scm_request *scmrq = NULL;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200290 struct request *req;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200291
292 while ((req = blk_peek_request(rq))) {
Steffen Maierde9587a2013-11-05 12:59:46 +0100293 if (req->cmd_type != REQ_TYPE_FS) {
294 blk_start_request(req);
295 blk_dump_rq_flags(req, KMSG_COMPONENT " bad request");
296 blk_end_request_all(req, -EIO);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200297 continue;
Steffen Maierde9587a2013-11-05 12:59:46 +0100298 }
Sebastian Ottf30664e2012-08-28 16:50:38 +0200299
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100300 if (!scm_permit_request(bdev, req))
301 goto out;
302
Sebastian Ottf30664e2012-08-28 16:50:38 +0200303 if (!scmrq) {
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100304 scmrq = scm_request_fetch();
305 if (!scmrq) {
306 SCM_LOG(5, "no request");
307 goto out;
308 }
309 scm_request_init(bdev, scmrq);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200310 }
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100311 scm_request_set(scmrq, req);
312
Sebastian Ott0d804b22012-08-28 16:51:19 +0200313 if (!scm_reserve_cluster(scmrq)) {
314 SCM_LOG(5, "cluster busy");
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100315 scm_request_set(scmrq, NULL);
316 if (scmrq->aob->request.msb_count)
317 goto out;
318
Sebastian Ott0d804b22012-08-28 16:51:19 +0200319 scm_request_done(scmrq);
320 return;
321 }
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100322
Sebastian Ott0d804b22012-08-28 16:51:19 +0200323 if (scm_need_cluster_request(scmrq)) {
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100324 if (scmrq->aob->request.msb_count) {
325 /* Start cluster requests separately. */
326 scm_request_set(scmrq, NULL);
327 if (scm_request_start(scmrq))
328 return;
329 } else {
330 atomic_inc(&bdev->queued_reqs);
331 blk_start_request(req);
332 scm_initiate_cluster_request(scmrq);
333 }
334 scmrq = NULL;
335 continue;
Sebastian Ott0d804b22012-08-28 16:51:19 +0200336 }
Sebastian Ott9d4df772014-12-05 16:32:13 +0100337
338 if (scm_request_prepare(scmrq)) {
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100339 SCM_LOG(5, "aidaw alloc failed");
340 scm_request_set(scmrq, NULL);
341 goto out;
Sebastian Ott9d4df772014-12-05 16:32:13 +0100342 }
Sebastian Ottf30664e2012-08-28 16:50:38 +0200343 blk_start_request(req);
344
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100345 if (scmrq->aob->request.msb_count < SCM_RQ_PER_IO)
346 continue;
347
348 if (scm_request_start(scmrq))
Sebastian Ottf30664e2012-08-28 16:50:38 +0200349 return;
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100350
351 scmrq = NULL;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200352 }
Sebastian Ottbbc610a2014-12-05 16:43:58 +0100353out:
354 if (scmrq)
355 scm_request_start(scmrq);
356 else
357 scm_ensure_queue_restart(bdev);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200358}
359
360static void __scmrq_log_error(struct scm_request *scmrq)
361{
362 struct aob *aob = scmrq->aob;
363
364 if (scmrq->error == -ETIMEDOUT)
365 SCM_LOG(1, "Request timeout");
366 else {
367 SCM_LOG(1, "Request error");
368 SCM_LOG_HEX(1, &aob->response, sizeof(aob->response));
369 }
370 if (scmrq->retries)
371 SCM_LOG(1, "Retry request");
372 else
373 pr_err("An I/O operation to SCM failed with rc=%d\n",
374 scmrq->error);
375}
376
377void scm_blk_irq(struct scm_device *scmdev, void *data, int error)
378{
379 struct scm_request *scmrq = data;
380 struct scm_blk_dev *bdev = scmrq->bdev;
381
382 scmrq->error = error;
383 if (error)
384 __scmrq_log_error(scmrq);
385
386 spin_lock(&bdev->lock);
387 list_add_tail(&scmrq->list, &bdev->finished_requests);
388 spin_unlock(&bdev->lock);
389 tasklet_hi_schedule(&bdev->tasklet);
390}
391
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100392static void scm_blk_handle_error(struct scm_request *scmrq)
393{
394 struct scm_blk_dev *bdev = scmrq->bdev;
395 unsigned long flags;
396
397 if (scmrq->error != -EIO)
398 goto restart;
399
400 /* For -EIO the response block is valid. */
401 switch (scmrq->aob->response.eqc) {
402 case EQC_WR_PROHIBIT:
403 spin_lock_irqsave(&bdev->lock, flags);
404 if (bdev->state != SCM_WR_PROHIBIT)
Sebastian Ott3bff6032013-03-18 16:01:30 +0100405 pr_info("%lx: Write access to the SCM increment is suspended\n",
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100406 (unsigned long) bdev->scmdev->address);
407 bdev->state = SCM_WR_PROHIBIT;
408 spin_unlock_irqrestore(&bdev->lock, flags);
409 goto requeue;
410 default:
411 break;
412 }
413
414restart:
Sebastian Ott605c3692013-11-14 10:44:56 +0100415 if (!eadm_start_aob(scmrq->aob))
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100416 return;
417
418requeue:
419 spin_lock_irqsave(&bdev->rq_lock, flags);
420 scm_request_requeue(scmrq);
421 spin_unlock_irqrestore(&bdev->rq_lock, flags);
422}
423
Sebastian Ottf30664e2012-08-28 16:50:38 +0200424static void scm_blk_tasklet(struct scm_blk_dev *bdev)
425{
426 struct scm_request *scmrq;
427 unsigned long flags;
428
429 spin_lock_irqsave(&bdev->lock, flags);
430 while (!list_empty(&bdev->finished_requests)) {
431 scmrq = list_first_entry(&bdev->finished_requests,
432 struct scm_request, list);
433 list_del(&scmrq->list);
434 spin_unlock_irqrestore(&bdev->lock, flags);
435
436 if (scmrq->error && scmrq->retries-- > 0) {
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100437 scm_blk_handle_error(scmrq);
438
Sebastian Ottf30664e2012-08-28 16:50:38 +0200439 /* Request restarted or requeued, handle next. */
440 spin_lock_irqsave(&bdev->lock, flags);
441 continue;
442 }
Sebastian Ott0d804b22012-08-28 16:51:19 +0200443
444 if (scm_test_cluster_request(scmrq)) {
445 scm_cluster_request_irq(scmrq);
446 spin_lock_irqsave(&bdev->lock, flags);
447 continue;
448 }
449
Sebastian Ottf30664e2012-08-28 16:50:38 +0200450 scm_request_finish(scmrq);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200451 spin_lock_irqsave(&bdev->lock, flags);
452 }
453 spin_unlock_irqrestore(&bdev->lock, flags);
454 /* Look out for more requests. */
455 blk_run_queue(bdev->rq);
456}
457
Sebastian Ott605c3692013-11-14 10:44:56 +0100458static const struct block_device_operations scm_blk_devops = {
459 .owner = THIS_MODULE,
460};
461
Sebastian Ottf30664e2012-08-28 16:50:38 +0200462int scm_blk_dev_setup(struct scm_blk_dev *bdev, struct scm_device *scmdev)
463{
464 struct request_queue *rq;
465 int len, ret = -ENOMEM;
466 unsigned int devindex, nr_max_blk;
467
468 devindex = atomic_inc_return(&nr_devices) - 1;
469 /* scma..scmz + scmaa..scmzz */
470 if (devindex > 701) {
471 ret = -ENODEV;
472 goto out;
473 }
474
475 bdev->scmdev = scmdev;
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100476 bdev->state = SCM_OPER;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200477 spin_lock_init(&bdev->rq_lock);
478 spin_lock_init(&bdev->lock);
479 INIT_LIST_HEAD(&bdev->finished_requests);
480 atomic_set(&bdev->queued_reqs, 0);
481 tasklet_init(&bdev->tasklet,
482 (void (*)(unsigned long)) scm_blk_tasklet,
483 (unsigned long) bdev);
484
485 rq = blk_init_queue(scm_blk_request, &bdev->rq_lock);
486 if (!rq)
487 goto out;
488
489 bdev->rq = rq;
490 nr_max_blk = min(scmdev->nr_max_block,
491 (unsigned int) (PAGE_SIZE / sizeof(struct aidaw)));
492
493 blk_queue_logical_block_size(rq, 1 << 12);
494 blk_queue_max_hw_sectors(rq, nr_max_blk << 3); /* 8 * 512 = blk_size */
495 blk_queue_max_segments(rq, nr_max_blk);
496 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, rq);
Mike Snitzerb277da02014-10-04 10:55:32 -0600497 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, rq);
Sebastian Ott0d804b22012-08-28 16:51:19 +0200498 scm_blk_dev_cluster_setup(bdev);
Sebastian Ottf30664e2012-08-28 16:50:38 +0200499
500 bdev->gendisk = alloc_disk(SCM_NR_PARTS);
501 if (!bdev->gendisk)
502 goto out_queue;
503
504 rq->queuedata = scmdev;
505 bdev->gendisk->driverfs_dev = &scmdev->dev;
506 bdev->gendisk->private_data = scmdev;
507 bdev->gendisk->fops = &scm_blk_devops;
508 bdev->gendisk->queue = rq;
509 bdev->gendisk->major = scm_major;
510 bdev->gendisk->first_minor = devindex * SCM_NR_PARTS;
511
512 len = snprintf(bdev->gendisk->disk_name, DISK_NAME_LEN, "scm");
513 if (devindex > 25) {
514 len += snprintf(bdev->gendisk->disk_name + len,
515 DISK_NAME_LEN - len, "%c",
516 'a' + (devindex / 26) - 1);
517 devindex = devindex % 26;
518 }
519 snprintf(bdev->gendisk->disk_name + len, DISK_NAME_LEN - len, "%c",
520 'a' + devindex);
521
522 /* 512 byte sectors */
523 set_capacity(bdev->gendisk, scmdev->size >> 9);
524 add_disk(bdev->gendisk);
525 return 0;
526
527out_queue:
528 blk_cleanup_queue(rq);
529out:
530 atomic_dec(&nr_devices);
531 return ret;
532}
533
534void scm_blk_dev_cleanup(struct scm_blk_dev *bdev)
535{
536 tasklet_kill(&bdev->tasklet);
537 del_gendisk(bdev->gendisk);
538 blk_cleanup_queue(bdev->gendisk->queue);
539 put_disk(bdev->gendisk);
540}
541
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100542void scm_blk_set_available(struct scm_blk_dev *bdev)
543{
544 unsigned long flags;
545
546 spin_lock_irqsave(&bdev->lock, flags);
547 if (bdev->state == SCM_WR_PROHIBIT)
Sebastian Ott3bff6032013-03-18 16:01:30 +0100548 pr_info("%lx: Write access to the SCM increment is restored\n",
Sebastian Ott4fa3c012013-02-28 12:07:48 +0100549 (unsigned long) bdev->scmdev->address);
550 bdev->state = SCM_OPER;
551 spin_unlock_irqrestore(&bdev->lock, flags);
552}
553
Sebastian Ottf30664e2012-08-28 16:50:38 +0200554static int __init scm_blk_init(void)
555{
Sebastian Ott0d804b22012-08-28 16:51:19 +0200556 int ret = -EINVAL;
557
558 if (!scm_cluster_size_valid())
559 goto out;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200560
561 ret = register_blkdev(0, "scm");
562 if (ret < 0)
563 goto out;
564
565 scm_major = ret;
Wei Yongjun94f98522013-03-20 13:40:54 +0100566 ret = scm_alloc_rqs(nr_requests);
567 if (ret)
Sebastian Ottfff60fa2013-04-25 13:03:18 +0200568 goto out_free;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200569
570 scm_debug = debug_register("scm_log", 16, 1, 16);
Wei Yongjun94f98522013-03-20 13:40:54 +0100571 if (!scm_debug) {
572 ret = -ENOMEM;
Sebastian Ottf30664e2012-08-28 16:50:38 +0200573 goto out_free;
Wei Yongjun94f98522013-03-20 13:40:54 +0100574 }
Sebastian Ottf30664e2012-08-28 16:50:38 +0200575
576 debug_register_view(scm_debug, &debug_hex_ascii_view);
577 debug_set_level(scm_debug, 2);
578
579 ret = scm_drv_init();
580 if (ret)
581 goto out_dbf;
582
583 return ret;
584
585out_dbf:
586 debug_unregister(scm_debug);
587out_free:
588 scm_free_rqs();
Sebastian Ottf30664e2012-08-28 16:50:38 +0200589 unregister_blkdev(scm_major, "scm");
590out:
591 return ret;
592}
593module_init(scm_blk_init);
594
595static void __exit scm_blk_cleanup(void)
596{
597 scm_drv_cleanup();
598 debug_unregister(scm_debug);
599 scm_free_rqs();
600 unregister_blkdev(scm_major, "scm");
601}
602module_exit(scm_blk_cleanup);