2 * Block driver for s390 storage class memory.
4 * Copyright IBM Corp. 2012
5 * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com>
8 #define KMSG_COMPONENT "scm_block"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/interrupt.h>
12 #include <linux/spinlock.h>
13 #include <linux/module.h>
14 #include <linux/blkdev.h>
15 #include <linux/genhd.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
21 debug_info_t
*scm_debug
;
23 static DEFINE_SPINLOCK(list_lock
);
24 static LIST_HEAD(inactive_requests
);
25 static unsigned int nr_requests
= 64;
26 static atomic_t nr_devices
= ATOMIC_INIT(0);
27 module_param(nr_requests
, uint
, S_IRUGO
);
28 MODULE_PARM_DESC(nr_requests
, "Number of parallel requests.");
30 MODULE_DESCRIPTION("Block driver for s390 storage class memory.");
31 MODULE_LICENSE("GPL");
32 MODULE_ALIAS("scm:scmdev*");
34 static void __scm_free_rq(struct scm_request
*scmrq
)
36 struct aob_rq_header
*aobrq
= to_aobrq(scmrq
);
38 free_page((unsigned long) scmrq
->aob
);
39 free_page((unsigned long) scmrq
->aidaw
);
40 __scm_free_rq_cluster(scmrq
);
44 static void scm_free_rqs(void)
46 struct list_head
*iter
, *safe
;
47 struct scm_request
*scmrq
;
49 spin_lock_irq(&list_lock
);
50 list_for_each_safe(iter
, safe
, &inactive_requests
) {
51 scmrq
= list_entry(iter
, struct scm_request
, list
);
52 list_del(&scmrq
->list
);
55 spin_unlock_irq(&list_lock
);
58 static int __scm_alloc_rq(void)
60 struct aob_rq_header
*aobrq
;
61 struct scm_request
*scmrq
;
63 aobrq
= kzalloc(sizeof(*aobrq
) + sizeof(*scmrq
), GFP_KERNEL
);
67 scmrq
= (void *) aobrq
->data
;
68 scmrq
->aidaw
= (void *) get_zeroed_page(GFP_DMA
);
69 scmrq
->aob
= (void *) get_zeroed_page(GFP_DMA
);
70 if (!scmrq
->aob
|| !scmrq
->aidaw
) {
75 if (__scm_alloc_rq_cluster(scmrq
)) {
80 INIT_LIST_HEAD(&scmrq
->list
);
81 spin_lock_irq(&list_lock
);
82 list_add(&scmrq
->list
, &inactive_requests
);
83 spin_unlock_irq(&list_lock
);
88 static int scm_alloc_rqs(unsigned int nrqs
)
92 while (nrqs
-- && !ret
)
93 ret
= __scm_alloc_rq();
98 static struct scm_request
*scm_request_fetch(void)
100 struct scm_request
*scmrq
= NULL
;
102 spin_lock(&list_lock
);
103 if (list_empty(&inactive_requests
))
105 scmrq
= list_first_entry(&inactive_requests
, struct scm_request
, list
);
106 list_del(&scmrq
->list
);
108 spin_unlock(&list_lock
);
112 static void scm_request_done(struct scm_request
*scmrq
)
116 spin_lock_irqsave(&list_lock
, flags
);
117 list_add(&scmrq
->list
, &inactive_requests
);
118 spin_unlock_irqrestore(&list_lock
, flags
);
121 static int scm_open(struct block_device
*blkdev
, fmode_t mode
)
123 return scm_get_ref();
126 static void scm_release(struct gendisk
*gendisk
, fmode_t mode
)
131 static const struct block_device_operations scm_blk_devops
= {
132 .owner
= THIS_MODULE
,
134 .release
= scm_release
,
137 static bool scm_permit_request(struct scm_blk_dev
*bdev
, struct request
*req
)
139 return rq_data_dir(req
) != WRITE
|| bdev
->state
!= SCM_WR_PROHIBIT
;
142 static void scm_request_prepare(struct scm_request
*scmrq
)
144 struct scm_blk_dev
*bdev
= scmrq
->bdev
;
145 struct scm_device
*scmdev
= bdev
->gendisk
->private_data
;
146 struct aidaw
*aidaw
= scmrq
->aidaw
;
147 struct msb
*msb
= &scmrq
->aob
->msb
[0];
148 struct req_iterator iter
;
152 scmrq
->aob
->request
.msb_count
= 1;
153 msb
->scm_addr
= scmdev
->address
+
154 ((u64
) blk_rq_pos(scmrq
->request
) << 9);
155 msb
->oc
= (rq_data_dir(scmrq
->request
) == READ
) ?
156 MSB_OC_READ
: MSB_OC_WRITE
;
157 msb
->flags
|= MSB_FLAG_IDA
;
158 msb
->data_addr
= (u64
) aidaw
;
160 rq_for_each_segment(bv
, scmrq
->request
, iter
) {
161 WARN_ON(bv
->bv_offset
);
162 msb
->blk_count
+= bv
->bv_len
>> 12;
163 aidaw
->data_addr
= (u64
) page_address(bv
->bv_page
);
168 static inline void scm_request_init(struct scm_blk_dev
*bdev
,
169 struct scm_request
*scmrq
,
172 struct aob_rq_header
*aobrq
= to_aobrq(scmrq
);
173 struct aob
*aob
= scmrq
->aob
;
175 memset(aob
, 0, sizeof(*aob
));
176 memset(scmrq
->aidaw
, 0, PAGE_SIZE
);
177 aobrq
->scmdev
= bdev
->scmdev
;
178 aob
->request
.cmd_code
= ARQB_CMD_MOVE
;
179 aob
->request
.data
= (u64
) aobrq
;
180 scmrq
->request
= req
;
184 scm_request_cluster_init(scmrq
);
187 static void scm_ensure_queue_restart(struct scm_blk_dev
*bdev
)
189 if (atomic_read(&bdev
->queued_reqs
)) {
190 /* Queue restart is triggered by the next interrupt. */
193 blk_delay_queue(bdev
->rq
, SCM_QUEUE_DELAY
);
196 void scm_request_requeue(struct scm_request
*scmrq
)
198 struct scm_blk_dev
*bdev
= scmrq
->bdev
;
200 scm_release_cluster(scmrq
);
201 blk_requeue_request(bdev
->rq
, scmrq
->request
);
202 atomic_dec(&bdev
->queued_reqs
);
203 scm_request_done(scmrq
);
204 scm_ensure_queue_restart(bdev
);
207 void scm_request_finish(struct scm_request
*scmrq
)
209 struct scm_blk_dev
*bdev
= scmrq
->bdev
;
211 scm_release_cluster(scmrq
);
212 blk_end_request_all(scmrq
->request
, scmrq
->error
);
213 atomic_dec(&bdev
->queued_reqs
);
214 scm_request_done(scmrq
);
217 static void scm_blk_request(struct request_queue
*rq
)
219 struct scm_device
*scmdev
= rq
->queuedata
;
220 struct scm_blk_dev
*bdev
= dev_get_drvdata(&scmdev
->dev
);
221 struct scm_request
*scmrq
;
225 while ((req
= blk_peek_request(rq
))) {
226 if (req
->cmd_type
!= REQ_TYPE_FS
)
229 if (!scm_permit_request(bdev
, req
)) {
230 scm_ensure_queue_restart(bdev
);
233 scmrq
= scm_request_fetch();
235 SCM_LOG(5, "no request");
236 scm_ensure_queue_restart(bdev
);
239 scm_request_init(bdev
, scmrq
, req
);
240 if (!scm_reserve_cluster(scmrq
)) {
241 SCM_LOG(5, "cluster busy");
242 scm_request_done(scmrq
);
245 if (scm_need_cluster_request(scmrq
)) {
246 atomic_inc(&bdev
->queued_reqs
);
247 blk_start_request(req
);
248 scm_initiate_cluster_request(scmrq
);
251 scm_request_prepare(scmrq
);
252 atomic_inc(&bdev
->queued_reqs
);
253 blk_start_request(req
);
255 ret
= scm_start_aob(scmrq
->aob
);
257 SCM_LOG(5, "no subchannel");
258 scm_request_requeue(scmrq
);
264 static void __scmrq_log_error(struct scm_request
*scmrq
)
266 struct aob
*aob
= scmrq
->aob
;
268 if (scmrq
->error
== -ETIMEDOUT
)
269 SCM_LOG(1, "Request timeout");
271 SCM_LOG(1, "Request error");
272 SCM_LOG_HEX(1, &aob
->response
, sizeof(aob
->response
));
275 SCM_LOG(1, "Retry request");
277 pr_err("An I/O operation to SCM failed with rc=%d\n",
281 void scm_blk_irq(struct scm_device
*scmdev
, void *data
, int error
)
283 struct scm_request
*scmrq
= data
;
284 struct scm_blk_dev
*bdev
= scmrq
->bdev
;
286 scmrq
->error
= error
;
288 __scmrq_log_error(scmrq
);
290 spin_lock(&bdev
->lock
);
291 list_add_tail(&scmrq
->list
, &bdev
->finished_requests
);
292 spin_unlock(&bdev
->lock
);
293 tasklet_hi_schedule(&bdev
->tasklet
);
296 static void scm_blk_handle_error(struct scm_request
*scmrq
)
298 struct scm_blk_dev
*bdev
= scmrq
->bdev
;
301 if (scmrq
->error
!= -EIO
)
304 /* For -EIO the response block is valid. */
305 switch (scmrq
->aob
->response
.eqc
) {
306 case EQC_WR_PROHIBIT
:
307 spin_lock_irqsave(&bdev
->lock
, flags
);
308 if (bdev
->state
!= SCM_WR_PROHIBIT
)
309 pr_info("%lx: Write access to the SCM increment is suspended\n",
310 (unsigned long) bdev
->scmdev
->address
);
311 bdev
->state
= SCM_WR_PROHIBIT
;
312 spin_unlock_irqrestore(&bdev
->lock
, flags
);
319 if (!scm_start_aob(scmrq
->aob
))
323 spin_lock_irqsave(&bdev
->rq_lock
, flags
);
324 scm_request_requeue(scmrq
);
325 spin_unlock_irqrestore(&bdev
->rq_lock
, flags
);
328 static void scm_blk_tasklet(struct scm_blk_dev
*bdev
)
330 struct scm_request
*scmrq
;
333 spin_lock_irqsave(&bdev
->lock
, flags
);
334 while (!list_empty(&bdev
->finished_requests
)) {
335 scmrq
= list_first_entry(&bdev
->finished_requests
,
336 struct scm_request
, list
);
337 list_del(&scmrq
->list
);
338 spin_unlock_irqrestore(&bdev
->lock
, flags
);
340 if (scmrq
->error
&& scmrq
->retries
-- > 0) {
341 scm_blk_handle_error(scmrq
);
343 /* Request restarted or requeued, handle next. */
344 spin_lock_irqsave(&bdev
->lock
, flags
);
348 if (scm_test_cluster_request(scmrq
)) {
349 scm_cluster_request_irq(scmrq
);
350 spin_lock_irqsave(&bdev
->lock
, flags
);
354 scm_request_finish(scmrq
);
355 spin_lock_irqsave(&bdev
->lock
, flags
);
357 spin_unlock_irqrestore(&bdev
->lock
, flags
);
358 /* Look out for more requests. */
359 blk_run_queue(bdev
->rq
);
362 int scm_blk_dev_setup(struct scm_blk_dev
*bdev
, struct scm_device
*scmdev
)
364 struct request_queue
*rq
;
365 int len
, ret
= -ENOMEM
;
366 unsigned int devindex
, nr_max_blk
;
368 devindex
= atomic_inc_return(&nr_devices
) - 1;
369 /* scma..scmz + scmaa..scmzz */
370 if (devindex
> 701) {
375 bdev
->scmdev
= scmdev
;
376 bdev
->state
= SCM_OPER
;
377 spin_lock_init(&bdev
->rq_lock
);
378 spin_lock_init(&bdev
->lock
);
379 INIT_LIST_HEAD(&bdev
->finished_requests
);
380 atomic_set(&bdev
->queued_reqs
, 0);
381 tasklet_init(&bdev
->tasklet
,
382 (void (*)(unsigned long)) scm_blk_tasklet
,
383 (unsigned long) bdev
);
385 rq
= blk_init_queue(scm_blk_request
, &bdev
->rq_lock
);
390 nr_max_blk
= min(scmdev
->nr_max_block
,
391 (unsigned int) (PAGE_SIZE
/ sizeof(struct aidaw
)));
393 blk_queue_logical_block_size(rq
, 1 << 12);
394 blk_queue_max_hw_sectors(rq
, nr_max_blk
<< 3); /* 8 * 512 = blk_size */
395 blk_queue_max_segments(rq
, nr_max_blk
);
396 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, rq
);
397 scm_blk_dev_cluster_setup(bdev
);
399 bdev
->gendisk
= alloc_disk(SCM_NR_PARTS
);
403 rq
->queuedata
= scmdev
;
404 bdev
->gendisk
->driverfs_dev
= &scmdev
->dev
;
405 bdev
->gendisk
->private_data
= scmdev
;
406 bdev
->gendisk
->fops
= &scm_blk_devops
;
407 bdev
->gendisk
->queue
= rq
;
408 bdev
->gendisk
->major
= scm_major
;
409 bdev
->gendisk
->first_minor
= devindex
* SCM_NR_PARTS
;
411 len
= snprintf(bdev
->gendisk
->disk_name
, DISK_NAME_LEN
, "scm");
413 len
+= snprintf(bdev
->gendisk
->disk_name
+ len
,
414 DISK_NAME_LEN
- len
, "%c",
415 'a' + (devindex
/ 26) - 1);
416 devindex
= devindex
% 26;
418 snprintf(bdev
->gendisk
->disk_name
+ len
, DISK_NAME_LEN
- len
, "%c",
421 /* 512 byte sectors */
422 set_capacity(bdev
->gendisk
, scmdev
->size
>> 9);
423 add_disk(bdev
->gendisk
);
427 blk_cleanup_queue(rq
);
429 atomic_dec(&nr_devices
);
433 void scm_blk_dev_cleanup(struct scm_blk_dev
*bdev
)
435 tasklet_kill(&bdev
->tasklet
);
436 del_gendisk(bdev
->gendisk
);
437 blk_cleanup_queue(bdev
->gendisk
->queue
);
438 put_disk(bdev
->gendisk
);
441 void scm_blk_set_available(struct scm_blk_dev
*bdev
)
445 spin_lock_irqsave(&bdev
->lock
, flags
);
446 if (bdev
->state
== SCM_WR_PROHIBIT
)
447 pr_info("%lx: Write access to the SCM increment is restored\n",
448 (unsigned long) bdev
->scmdev
->address
);
449 bdev
->state
= SCM_OPER
;
450 spin_unlock_irqrestore(&bdev
->lock
, flags
);
453 static int __init
scm_blk_init(void)
457 if (!scm_cluster_size_valid())
460 ret
= register_blkdev(0, "scm");
465 ret
= scm_alloc_rqs(nr_requests
);
469 scm_debug
= debug_register("scm_log", 16, 1, 16);
475 debug_register_view(scm_debug
, &debug_hex_ascii_view
);
476 debug_set_level(scm_debug
, 2);
478 ret
= scm_drv_init();
485 debug_unregister(scm_debug
);
488 unregister_blkdev(scm_major
, "scm");
492 module_init(scm_blk_init
);
494 static void __exit
scm_blk_cleanup(void)
497 debug_unregister(scm_debug
);
499 unregister_blkdev(scm_major
, "scm");
501 module_exit(scm_blk_cleanup
);