Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 1 | /* |
| 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 Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 13 | #include <linux/mempool.h> |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 14 | #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 | |
| 22 | debug_info_t *scm_debug; |
| 23 | static int scm_major; |
Sebastian Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 24 | static mempool_t *aidaw_pool; |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 25 | static DEFINE_SPINLOCK(list_lock); |
| 26 | static LIST_HEAD(inactive_requests); |
| 27 | static unsigned int nr_requests = 64; |
| 28 | static atomic_t nr_devices = ATOMIC_INIT(0); |
| 29 | module_param(nr_requests, uint, S_IRUGO); |
| 30 | MODULE_PARM_DESC(nr_requests, "Number of parallel requests."); |
| 31 | |
| 32 | MODULE_DESCRIPTION("Block driver for s390 storage class memory."); |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | MODULE_ALIAS("scm:scmdev*"); |
| 35 | |
| 36 | static 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 Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 41 | __scm_free_rq_cluster(scmrq); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 42 | kfree(aobrq); |
| 43 | } |
| 44 | |
| 45 | static 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 Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 57 | |
| 58 | mempool_destroy(aidaw_pool); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static 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 Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 71 | scmrq->aob = (void *) get_zeroed_page(GFP_DMA); |
Sebastian Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 72 | if (!scmrq->aob) { |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 73 | __scm_free_rq(scmrq); |
| 74 | return -ENOMEM; |
| 75 | } |
Sebastian Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 76 | |
| 77 | if (__scm_alloc_rq_cluster(scmrq)) { |
| 78 | __scm_free_rq(scmrq); |
| 79 | return -ENOMEM; |
| 80 | } |
| 81 | |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 82 | 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 | |
| 90 | static int scm_alloc_rqs(unsigned int nrqs) |
| 91 | { |
| 92 | int ret = 0; |
| 93 | |
Sebastian Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 94 | aidaw_pool = mempool_create_page_pool(max(nrqs/8, 1U), 0); |
| 95 | if (!aidaw_pool) |
| 96 | return -ENOMEM; |
| 97 | |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 98 | while (nrqs-- && !ret) |
| 99 | ret = __scm_alloc_rq(); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static 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); |
| 113 | out: |
| 114 | spin_unlock(&list_lock); |
| 115 | return scmrq; |
| 116 | } |
| 117 | |
| 118 | static void scm_request_done(struct scm_request *scmrq) |
| 119 | { |
Sebastian Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 120 | struct msb *msb = &scmrq->aob->msb[0]; |
| 121 | u64 aidaw = msb->data_addr; |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 122 | unsigned long flags; |
| 123 | |
Sebastian Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 124 | if ((msb->flags & MSB_FLAG_IDA) && aidaw) |
| 125 | mempool_free(virt_to_page(aidaw), aidaw_pool); |
| 126 | |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 127 | spin_lock_irqsave(&list_lock, flags); |
| 128 | list_add(&scmrq->list, &inactive_requests); |
| 129 | spin_unlock_irqrestore(&list_lock, flags); |
| 130 | } |
| 131 | |
Sebastian Ott | 4fa3c01 | 2013-02-28 12:07:48 +0100 | [diff] [blame] | 132 | static 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 Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 137 | struct 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 | |
| 144 | static int scm_request_prepare(struct scm_request *scmrq) |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 145 | { |
| 146 | struct scm_blk_dev *bdev = scmrq->bdev; |
| 147 | struct scm_device *scmdev = bdev->gendisk->private_data; |
Sebastian Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 148 | struct aidaw *aidaw = scm_aidaw_alloc(); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 149 | struct msb *msb = &scmrq->aob->msb[0]; |
| 150 | struct req_iterator iter; |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 151 | struct bio_vec bv; |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 152 | |
Sebastian Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 153 | if (!aidaw) |
| 154 | return -ENOMEM; |
| 155 | |
| 156 | memset(aidaw, 0, PAGE_SIZE); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 157 | 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 Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 167 | WARN_ON(bv.bv_offset); |
| 168 | msb->blk_count += bv.bv_len >> 12; |
| 169 | aidaw->data_addr = (u64) page_address(bv.bv_page); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 170 | aidaw++; |
| 171 | } |
Sebastian Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 172 | |
| 173 | return 0; |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static 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 Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 184 | 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 Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 191 | scm_request_cluster_init(scmrq); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static 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 Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 203 | void scm_request_requeue(struct scm_request *scmrq) |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 204 | { |
| 205 | struct scm_blk_dev *bdev = scmrq->bdev; |
| 206 | |
Sebastian Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 207 | scm_release_cluster(scmrq); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 208 | blk_requeue_request(bdev->rq, scmrq->request); |
Sebastian Ott | 8360cb5 | 2013-02-28 12:07:27 +0100 | [diff] [blame] | 209 | atomic_dec(&bdev->queued_reqs); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 210 | scm_request_done(scmrq); |
| 211 | scm_ensure_queue_restart(bdev); |
| 212 | } |
| 213 | |
Sebastian Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 214 | void scm_request_finish(struct scm_request *scmrq) |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 215 | { |
Sebastian Ott | 8360cb5 | 2013-02-28 12:07:27 +0100 | [diff] [blame] | 216 | struct scm_blk_dev *bdev = scmrq->bdev; |
| 217 | |
Sebastian Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 218 | scm_release_cluster(scmrq); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 219 | blk_end_request_all(scmrq->request, scmrq->error); |
Sebastian Ott | 8360cb5 | 2013-02-28 12:07:27 +0100 | [diff] [blame] | 220 | atomic_dec(&bdev->queued_reqs); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 221 | scm_request_done(scmrq); |
| 222 | } |
| 223 | |
| 224 | static 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 Maier | de9587a | 2013-11-05 12:59:46 +0100 | [diff] [blame] | 233 | 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 Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 237 | continue; |
Steffen Maier | de9587a | 2013-11-05 12:59:46 +0100 | [diff] [blame] | 238 | } |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 239 | |
Sebastian Ott | 4fa3c01 | 2013-02-28 12:07:48 +0100 | [diff] [blame] | 240 | if (!scm_permit_request(bdev, req)) { |
| 241 | scm_ensure_queue_restart(bdev); |
| 242 | return; |
| 243 | } |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 244 | 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 Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 251 | 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 Ott | 8360cb5 | 2013-02-28 12:07:27 +0100 | [diff] [blame] | 257 | atomic_inc(&bdev->queued_reqs); |
Sebastian Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 258 | blk_start_request(req); |
| 259 | scm_initiate_cluster_request(scmrq); |
| 260 | return; |
| 261 | } |
Sebastian Ott | 9d4df77 | 2014-12-05 16:32:13 +0100 | [diff] [blame^] | 262 | |
| 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 Ott | 8360cb5 | 2013-02-28 12:07:27 +0100 | [diff] [blame] | 271 | atomic_inc(&bdev->queued_reqs); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 272 | blk_start_request(req); |
| 273 | |
Sebastian Ott | 605c369 | 2013-11-14 10:44:56 +0100 | [diff] [blame] | 274 | ret = eadm_start_aob(scmrq->aob); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 275 | if (ret) { |
| 276 | SCM_LOG(5, "no subchannel"); |
| 277 | scm_request_requeue(scmrq); |
| 278 | return; |
| 279 | } |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | static 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 | |
| 300 | void 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 Ott | 4fa3c01 | 2013-02-28 12:07:48 +0100 | [diff] [blame] | 315 | static 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 Ott | 3bff603 | 2013-03-18 16:01:30 +0100 | [diff] [blame] | 328 | pr_info("%lx: Write access to the SCM increment is suspended\n", |
Sebastian Ott | 4fa3c01 | 2013-02-28 12:07:48 +0100 | [diff] [blame] | 329 | (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 | |
| 337 | restart: |
Sebastian Ott | 605c369 | 2013-11-14 10:44:56 +0100 | [diff] [blame] | 338 | if (!eadm_start_aob(scmrq->aob)) |
Sebastian Ott | 4fa3c01 | 2013-02-28 12:07:48 +0100 | [diff] [blame] | 339 | return; |
| 340 | |
| 341 | requeue: |
| 342 | spin_lock_irqsave(&bdev->rq_lock, flags); |
| 343 | scm_request_requeue(scmrq); |
| 344 | spin_unlock_irqrestore(&bdev->rq_lock, flags); |
| 345 | } |
| 346 | |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 347 | static 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 Ott | 4fa3c01 | 2013-02-28 12:07:48 +0100 | [diff] [blame] | 360 | scm_blk_handle_error(scmrq); |
| 361 | |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 362 | /* Request restarted or requeued, handle next. */ |
| 363 | spin_lock_irqsave(&bdev->lock, flags); |
| 364 | continue; |
| 365 | } |
Sebastian Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 366 | |
| 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 Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 373 | scm_request_finish(scmrq); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 374 | 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 Ott | 605c369 | 2013-11-14 10:44:56 +0100 | [diff] [blame] | 381 | static const struct block_device_operations scm_blk_devops = { |
| 382 | .owner = THIS_MODULE, |
| 383 | }; |
| 384 | |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 385 | int 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 Ott | 4fa3c01 | 2013-02-28 12:07:48 +0100 | [diff] [blame] | 399 | bdev->state = SCM_OPER; |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 400 | 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 Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 420 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, rq); |
Sebastian Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 421 | scm_blk_dev_cluster_setup(bdev); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 422 | |
| 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 | |
| 450 | out_queue: |
| 451 | blk_cleanup_queue(rq); |
| 452 | out: |
| 453 | atomic_dec(&nr_devices); |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | void 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 Ott | 4fa3c01 | 2013-02-28 12:07:48 +0100 | [diff] [blame] | 465 | void 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 Ott | 3bff603 | 2013-03-18 16:01:30 +0100 | [diff] [blame] | 471 | pr_info("%lx: Write access to the SCM increment is restored\n", |
Sebastian Ott | 4fa3c01 | 2013-02-28 12:07:48 +0100 | [diff] [blame] | 472 | (unsigned long) bdev->scmdev->address); |
| 473 | bdev->state = SCM_OPER; |
| 474 | spin_unlock_irqrestore(&bdev->lock, flags); |
| 475 | } |
| 476 | |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 477 | static int __init scm_blk_init(void) |
| 478 | { |
Sebastian Ott | 0d804b2 | 2012-08-28 16:51:19 +0200 | [diff] [blame] | 479 | int ret = -EINVAL; |
| 480 | |
| 481 | if (!scm_cluster_size_valid()) |
| 482 | goto out; |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 483 | |
| 484 | ret = register_blkdev(0, "scm"); |
| 485 | if (ret < 0) |
| 486 | goto out; |
| 487 | |
| 488 | scm_major = ret; |
Wei Yongjun | 94f9852 | 2013-03-20 13:40:54 +0100 | [diff] [blame] | 489 | ret = scm_alloc_rqs(nr_requests); |
| 490 | if (ret) |
Sebastian Ott | fff60fa | 2013-04-25 13:03:18 +0200 | [diff] [blame] | 491 | goto out_free; |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 492 | |
| 493 | scm_debug = debug_register("scm_log", 16, 1, 16); |
Wei Yongjun | 94f9852 | 2013-03-20 13:40:54 +0100 | [diff] [blame] | 494 | if (!scm_debug) { |
| 495 | ret = -ENOMEM; |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 496 | goto out_free; |
Wei Yongjun | 94f9852 | 2013-03-20 13:40:54 +0100 | [diff] [blame] | 497 | } |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 498 | |
| 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 | |
| 508 | out_dbf: |
| 509 | debug_unregister(scm_debug); |
| 510 | out_free: |
| 511 | scm_free_rqs(); |
Sebastian Ott | f30664e | 2012-08-28 16:50:38 +0200 | [diff] [blame] | 512 | unregister_blkdev(scm_major, "scm"); |
| 513 | out: |
| 514 | return ret; |
| 515 | } |
| 516 | module_init(scm_blk_init); |
| 517 | |
| 518 | static 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 | } |
| 525 | module_exit(scm_blk_cleanup); |