nfit.c (6ea24cf79e055f0a62a64baa8587e2254a493c7b) nfit.c (ee8520fe8cd4cd2658ca555781eefeb4914c4ef9)
1/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but

--- 316 unchanged lines hidden (view full) ---

325 * driver.
326 */
327 clear_err->status = 0;
328 clear_err->cleared = clear_err->length;
329 *cmd_rc = 0;
330 return 0;
331}
332
1/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but

--- 316 unchanged lines hidden (view full) ---

325 * driver.
326 */
327 clear_err->status = 0;
328 clear_err->cleared = clear_err->length;
329 *cmd_rc = 0;
330 return 0;
331}
332
333static int nfit_test_cmd_smart(struct nd_cmd_smart *smart, unsigned int buf_len)
334{
335 static const struct nd_smart_payload smart_data = {
336 .flags = ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID
337 | ND_SMART_SPARES_VALID | ND_SMART_ALARM_VALID
338 | ND_SMART_USED_VALID | ND_SMART_SHUTDOWN_VALID,
339 .health = ND_SMART_NON_CRITICAL_HEALTH,
340 .temperature = 23 * 16,
341 .spares = 75,
342 .alarm_flags = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
343 .life_used = 5,
344 .shutdown_state = 0,
345 .vendor_size = 0,
346 };
347
348 if (buf_len < sizeof(*smart))
349 return -EINVAL;
350 memcpy(smart->data, &smart_data, sizeof(smart_data));
351 return 0;
352}
353
354static int nfit_test_cmd_smart_threshold(struct nd_cmd_smart_threshold *smart_t,
355 unsigned int buf_len)
356{
357 static const struct nd_smart_threshold_payload smart_t_data = {
358 .alarm_control = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
359 .temperature = 40 * 16,
360 .spares = 5,
361 };
362
363 if (buf_len < sizeof(*smart_t))
364 return -EINVAL;
365 memcpy(smart_t->data, &smart_t_data, sizeof(smart_t_data));
366 return 0;
367}
368
333static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
334 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
335 unsigned int buf_len, int *cmd_rc)
336{
337 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
338 struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
369static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
370 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
371 unsigned int buf_len, int *cmd_rc)
372{
373 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
374 struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
375 unsigned int func = cmd;
339 int i, rc = 0, __cmd_rc;
340
341 if (!cmd_rc)
342 cmd_rc = &__cmd_rc;
343 *cmd_rc = 0;
344
345 if (nvdimm) {
346 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
376 int i, rc = 0, __cmd_rc;
377
378 if (!cmd_rc)
379 cmd_rc = &__cmd_rc;
380 *cmd_rc = 0;
381
382 if (nvdimm) {
383 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
384 unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
347
385
348 if (!nfit_mem || !test_bit(cmd, &nfit_mem->dsm_mask))
386 if (!nfit_mem)
349 return -ENOTTY;
350
387 return -ENOTTY;
388
389 if (cmd == ND_CMD_CALL) {
390 struct nd_cmd_pkg *call_pkg = buf;
391
392 buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
393 buf = (void *) call_pkg->nd_payload;
394 func = call_pkg->nd_command;
395 if (call_pkg->nd_family != nfit_mem->family)
396 return -ENOTTY;
397 }
398
399 if (!test_bit(cmd, &cmd_mask)
400 || !test_bit(func, &nfit_mem->dsm_mask))
401 return -ENOTTY;
402
351 /* lookup label space for the given dimm */
352 for (i = 0; i < ARRAY_SIZE(handle); i++)
353 if (__to_nfit_memdev(nfit_mem)->device_handle ==
354 handle[i])
355 break;
356 if (i >= ARRAY_SIZE(handle))
357 return -ENXIO;
358
403 /* lookup label space for the given dimm */
404 for (i = 0; i < ARRAY_SIZE(handle); i++)
405 if (__to_nfit_memdev(nfit_mem)->device_handle ==
406 handle[i])
407 break;
408 if (i >= ARRAY_SIZE(handle))
409 return -ENXIO;
410
359 switch (cmd) {
411 switch (func) {
360 case ND_CMD_GET_CONFIG_SIZE:
361 rc = nfit_test_cmd_get_config_size(buf, buf_len);
362 break;
363 case ND_CMD_GET_CONFIG_DATA:
364 rc = nfit_test_cmd_get_config_data(buf, buf_len,
365 t->label[i]);
366 break;
367 case ND_CMD_SET_CONFIG_DATA:
368 rc = nfit_test_cmd_set_config_data(buf, buf_len,
369 t->label[i]);
370 break;
412 case ND_CMD_GET_CONFIG_SIZE:
413 rc = nfit_test_cmd_get_config_size(buf, buf_len);
414 break;
415 case ND_CMD_GET_CONFIG_DATA:
416 rc = nfit_test_cmd_get_config_data(buf, buf_len,
417 t->label[i]);
418 break;
419 case ND_CMD_SET_CONFIG_DATA:
420 rc = nfit_test_cmd_set_config_data(buf, buf_len,
421 t->label[i]);
422 break;
423 case ND_CMD_SMART:
424 rc = nfit_test_cmd_smart(buf, buf_len);
425 break;
426 case ND_CMD_SMART_THRESHOLD:
427 rc = nfit_test_cmd_smart_threshold(buf, buf_len);
428 break;
371 default:
372 return -ENOTTY;
373 }
374 } else {
375 struct ars_state *ars_state = &t->ars_state;
376
429 default:
430 return -ENOTTY;
431 }
432 } else {
433 struct ars_state *ars_state = &t->ars_state;
434
377 if (!nd_desc || !test_bit(cmd, &nd_desc->dsm_mask))
435 if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
378 return -ENOTTY;
379
436 return -ENOTTY;
437
380 switch (cmd) {
438 switch (func) {
381 case ND_CMD_ARS_CAP:
382 rc = nfit_test_cmd_ars_cap(buf, buf_len);
383 break;
384 case ND_CMD_ARS_START:
385 rc = nfit_test_cmd_ars_start(ars_state, buf, buf_len,
386 cmd_rc);
387 break;
388 case ND_CMD_ARS_STATUS:

--- 18 unchanged lines hidden (view full) ---

407{
408 struct nfit_test_resource *nfit_res = data;
409 struct resource *res = nfit_res->res;
410
411 spin_lock(&nfit_test_lock);
412 list_del(&nfit_res->list);
413 spin_unlock(&nfit_test_lock);
414
439 case ND_CMD_ARS_CAP:
440 rc = nfit_test_cmd_ars_cap(buf, buf_len);
441 break;
442 case ND_CMD_ARS_START:
443 rc = nfit_test_cmd_ars_start(ars_state, buf, buf_len,
444 cmd_rc);
445 break;
446 case ND_CMD_ARS_STATUS:

--- 18 unchanged lines hidden (view full) ---

465{
466 struct nfit_test_resource *nfit_res = data;
467 struct resource *res = nfit_res->res;
468
469 spin_lock(&nfit_test_lock);
470 list_del(&nfit_res->list);
471 spin_unlock(&nfit_test_lock);
472
415 if (is_vmalloc_addr(nfit_res->buf))
416 vfree(nfit_res->buf);
417 else
418 dma_free_coherent(nfit_res->dev, resource_size(res),
419 nfit_res->buf, res->start);
473 vfree(nfit_res->buf);
420 kfree(res);
421 kfree(nfit_res);
422}
423
424static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
425 void *buf)
426{
427 struct device *dev = &t->pdev.dev;

--- 16 unchanged lines hidden (view full) ---

444 res->end = *dma + size - 1;
445 res->name = "NFIT";
446 spin_lock(&nfit_test_lock);
447 list_add(&nfit_res->list, &t->resources);
448 spin_unlock(&nfit_test_lock);
449
450 return nfit_res->buf;
451 err:
474 kfree(res);
475 kfree(nfit_res);
476}
477
478static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
479 void *buf)
480{
481 struct device *dev = &t->pdev.dev;

--- 16 unchanged lines hidden (view full) ---

498 res->end = *dma + size - 1;
499 res->name = "NFIT";
500 spin_lock(&nfit_test_lock);
501 list_add(&nfit_res->list, &t->resources);
502 spin_unlock(&nfit_test_lock);
503
504 return nfit_res->buf;
505 err:
452 if (buf && !is_vmalloc_addr(buf))
453 dma_free_coherent(dev, size, buf, *dma);
454 else if (buf)
506 if (buf)
455 vfree(buf);
456 kfree(res);
457 kfree(nfit_res);
458 return NULL;
459}
460
461static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
462{
463 void *buf = vmalloc(size);
464
465 *dma = (unsigned long) buf;
466 return __test_alloc(t, size, dma, buf);
467}
468
507 vfree(buf);
508 kfree(res);
509 kfree(nfit_res);
510 return NULL;
511}
512
513static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
514{
515 void *buf = vmalloc(size);
516
517 *dma = (unsigned long) buf;
518 return __test_alloc(t, size, dma, buf);
519}
520
469static void *test_alloc_coherent(struct nfit_test *t, size_t size,
470 dma_addr_t *dma)
471{
472 struct device *dev = &t->pdev.dev;
473 void *buf = dma_alloc_coherent(dev, size, dma, GFP_KERNEL);
474
475 return __test_alloc(t, size, dma, buf);
476}
477
478static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
479{
480 int i;
481
482 for (i = 0; i < ARRAY_SIZE(instances); i++) {
483 struct nfit_test_resource *n, *nfit_res = NULL;
484 struct nfit_test *t = instances[i];
485

--- 43 unchanged lines hidden (view full) ---

529 + sizeof(struct acpi_nfit_flush_address) * NUM_DCR;
530 int i;
531
532 t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
533 if (!t->nfit_buf)
534 return -ENOMEM;
535 t->nfit_size = nfit_size;
536
521static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
522{
523 int i;
524
525 for (i = 0; i < ARRAY_SIZE(instances); i++) {
526 struct nfit_test_resource *n, *nfit_res = NULL;
527 struct nfit_test *t = instances[i];
528

--- 43 unchanged lines hidden (view full) ---

572 + sizeof(struct acpi_nfit_flush_address) * NUM_DCR;
573 int i;
574
575 t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
576 if (!t->nfit_buf)
577 return -ENOMEM;
578 t->nfit_size = nfit_size;
579
537 t->spa_set[0] = test_alloc_coherent(t, SPA0_SIZE, &t->spa_set_dma[0]);
580 t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
538 if (!t->spa_set[0])
539 return -ENOMEM;
540
581 if (!t->spa_set[0])
582 return -ENOMEM;
583
541 t->spa_set[1] = test_alloc_coherent(t, SPA1_SIZE, &t->spa_set_dma[1]);
584 t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
542 if (!t->spa_set[1])
543 return -ENOMEM;
544
585 if (!t->spa_set[1])
586 return -ENOMEM;
587
545 t->spa_set[2] = test_alloc_coherent(t, SPA0_SIZE, &t->spa_set_dma[2]);
588 t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
546 if (!t->spa_set[2])
547 return -ENOMEM;
548
549 for (i = 0; i < NUM_DCR; i++) {
550 t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
551 if (!t->dimm[i])
552 return -ENOMEM;
553

--- 22 unchanged lines hidden (view full) ---

576 + sizeof(struct acpi_nfit_memory_map)
577 + offsetof(struct acpi_nfit_control_region, window_size);
578
579 t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
580 if (!t->nfit_buf)
581 return -ENOMEM;
582 t->nfit_size = nfit_size;
583
589 if (!t->spa_set[2])
590 return -ENOMEM;
591
592 for (i = 0; i < NUM_DCR; i++) {
593 t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
594 if (!t->dimm[i])
595 return -ENOMEM;
596

--- 22 unchanged lines hidden (view full) ---

619 + sizeof(struct acpi_nfit_memory_map)
620 + offsetof(struct acpi_nfit_control_region, window_size);
621
622 t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
623 if (!t->nfit_buf)
624 return -ENOMEM;
625 t->nfit_size = nfit_size;
626
584 t->spa_set[0] = test_alloc_coherent(t, SPA2_SIZE, &t->spa_set_dma[0]);
627 t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
585 if (!t->spa_set[0])
586 return -ENOMEM;
587
588 return ars_state_init(&t->pdev.dev, &t->ars_state);
589}
590
591static void nfit_test0_setup(struct nfit_test *t)
592{

--- 653 unchanged lines hidden (view full) ---

1246 flush->device_handle = handle[4];
1247 flush->hint_count = 1;
1248 flush->hint_address[0] = t->flush_dma[4];
1249 }
1250
1251 post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA0_SIZE);
1252
1253 acpi_desc = &t->acpi_desc;
628 if (!t->spa_set[0])
629 return -ENOMEM;
630
631 return ars_state_init(&t->pdev.dev, &t->ars_state);
632}
633
634static void nfit_test0_setup(struct nfit_test *t)
635{

--- 653 unchanged lines hidden (view full) ---

1289 flush->device_handle = handle[4];
1290 flush->hint_count = 1;
1291 flush->hint_address[0] = t->flush_dma[4];
1292 }
1293
1294 post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA0_SIZE);
1295
1296 acpi_desc = &t->acpi_desc;
1254 set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_dsm_force_en);
1255 set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_dsm_force_en);
1256 set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_dsm_force_en);
1257 set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_dsm_force_en);
1258 set_bit(ND_CMD_ARS_START, &acpi_desc->bus_dsm_force_en);
1259 set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_dsm_force_en);
1260 set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_dsm_force_en);
1297 set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
1298 set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
1299 set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
1300 set_bit(ND_CMD_SMART, &acpi_desc->dimm_cmd_force_en);
1301 set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1302 set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1303 set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1304 set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
1305 set_bit(ND_CMD_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
1261}
1262
1263static void nfit_test1_setup(struct nfit_test *t)
1264{
1265 size_t offset;
1266 void *nfit_buf = t->nfit_buf;
1267 struct acpi_nfit_memory_map *memdev;
1268 struct acpi_nfit_control_region *dcr;

--- 41 unchanged lines hidden (view full) ---

1310 dcr->revision_id = 1;
1311 dcr->serial_number = ~0;
1312 dcr->code = NFIT_FIC_BYTE;
1313 dcr->windows = 0;
1314
1315 post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA2_SIZE);
1316
1317 acpi_desc = &t->acpi_desc;
1306}
1307
1308static void nfit_test1_setup(struct nfit_test *t)
1309{
1310 size_t offset;
1311 void *nfit_buf = t->nfit_buf;
1312 struct acpi_nfit_memory_map *memdev;
1313 struct acpi_nfit_control_region *dcr;

--- 41 unchanged lines hidden (view full) ---

1355 dcr->revision_id = 1;
1356 dcr->serial_number = ~0;
1357 dcr->code = NFIT_FIC_BYTE;
1358 dcr->windows = 0;
1359
1360 post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA2_SIZE);
1361
1362 acpi_desc = &t->acpi_desc;
1318 set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_dsm_force_en);
1319 set_bit(ND_CMD_ARS_START, &acpi_desc->bus_dsm_force_en);
1320 set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_dsm_force_en);
1321 set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_dsm_force_en);
1363 set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1364 set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1365 set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1366 set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
1322}
1323
1324static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
1325 void *iobuf, u64 len, int rw)
1326{
1327 struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
1328 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1329 struct nd_region *nd_region = &ndbr->nd_region;

--- 128 unchanged lines hidden (view full) ---

1458 .probe = nfit_test_probe,
1459 .remove = nfit_test_remove,
1460 .driver = {
1461 .name = KBUILD_MODNAME,
1462 },
1463 .id_table = nfit_test_id,
1464};
1465
1367}
1368
1369static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
1370 void *iobuf, u64 len, int rw)
1371{
1372 struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
1373 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1374 struct nd_region *nd_region = &ndbr->nd_region;

--- 128 unchanged lines hidden (view full) ---

1503 .probe = nfit_test_probe,
1504 .remove = nfit_test_remove,
1505 .driver = {
1506 .name = KBUILD_MODNAME,
1507 },
1508 .id_table = nfit_test_id,
1509};
1510
1466#ifdef CONFIG_CMA_SIZE_MBYTES
1467#define CMA_SIZE_MBYTES CONFIG_CMA_SIZE_MBYTES
1468#else
1469#define CMA_SIZE_MBYTES 0
1470#endif
1471
1472static __init int nfit_test_init(void)
1473{
1474 int rc, i;
1475
1476 nfit_test_setup(nfit_test_lookup);
1477
1478 for (i = 0; i < NUM_NFITS; i++) {
1479 struct nfit_test *nfit_test;
1480 struct platform_device *pdev;
1511static __init int nfit_test_init(void)
1512{
1513 int rc, i;
1514
1515 nfit_test_setup(nfit_test_lookup);
1516
1517 for (i = 0; i < NUM_NFITS; i++) {
1518 struct nfit_test *nfit_test;
1519 struct platform_device *pdev;
1481 static int once;
1482
1483 nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
1484 if (!nfit_test) {
1485 rc = -ENOMEM;
1486 goto err_register;
1487 }
1488 INIT_LIST_HEAD(&nfit_test->resources);
1489 switch (i) {

--- 22 unchanged lines hidden (view full) ---

1512 goto err_register;
1513 }
1514
1515 rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1516 if (rc)
1517 goto err_register;
1518
1519 instances[i] = nfit_test;
1520
1521 nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
1522 if (!nfit_test) {
1523 rc = -ENOMEM;
1524 goto err_register;
1525 }
1526 INIT_LIST_HEAD(&nfit_test->resources);
1527 switch (i) {

--- 22 unchanged lines hidden (view full) ---

1550 goto err_register;
1551 }
1552
1553 rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1554 if (rc)
1555 goto err_register;
1556
1557 instances[i] = nfit_test;
1520
1521 if (!once++) {
1522 dma_addr_t dma;
1523 void *buf;
1524
1525 buf = dma_alloc_coherent(&pdev->dev, SZ_128M, &dma,
1526 GFP_KERNEL);
1527 if (!buf) {
1528 rc = -ENOMEM;
1529 dev_warn(&pdev->dev, "need 128M of free cma\n");
1530 goto err_register;
1531 }
1532 dma_free_coherent(&pdev->dev, SZ_128M, buf, dma);
1533 }
1534 }
1535
1536 rc = platform_driver_register(&nfit_test_driver);
1537 if (rc)
1538 goto err_register;
1539 return 0;
1540
1541 err_register:

--- 21 unchanged lines hidden ---
1558 }
1559
1560 rc = platform_driver_register(&nfit_test_driver);
1561 if (rc)
1562 goto err_register;
1563 return 0;
1564
1565 err_register:

--- 21 unchanged lines hidden ---