Skip to content

Commit

Permalink
[ddk][ddktl] Migrate block protocol to autogenerated version
Browse files Browse the repository at this point in the history
Tested: fvm-test, zxcrypt-test, paved vim2, paved pixelbook

Change-Id: I1f719a55e8d82372ce4250acf9acaf6a38bb55b6
  • Loading branch information
surajrmal authored and CQ bot account: [email protected] committed Nov 5, 2018
1 parent 1a28b7b commit aecb14b
Show file tree
Hide file tree
Showing 34 changed files with 538 additions and 359 deletions.
10 changes: 5 additions & 5 deletions system/dev/block/ahci/ahci.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ static int ahci_worker_thread(void* arg) {
zx_pmt_unpin(txn->pmt);
}
zxlogf(SPEW, "ahci.%d: complete txn %p\n", port->nr, txn);
block_complete(&txn->bop, ZX_OK);
block_complete(txn, ZX_OK);
mtx_lock(&port->lock);
}
port->completed &= ~(1 << slot);
Expand All @@ -544,7 +544,7 @@ static int ahci_worker_thread(void* arg) {
if ((port->flags & AHCI_PORT_FLAG_SYNC_PAUSED) && !port->running) {
port->flags &= ~AHCI_PORT_FLAG_SYNC_PAUSED;
if (port->sync) {
block_op_t* sop = &port->sync->bop;
sata_txn_t* sop = port->sync;
port->sync = NULL;
mtx_unlock(&port->lock);
block_complete(sop, ZX_OK);
Expand Down Expand Up @@ -585,7 +585,7 @@ static int ahci_worker_thread(void* arg) {
} else {
// complete immediately if nothing in flight
mtx_unlock(&port->lock);
block_complete(&txn->bop, ZX_OK);
block_complete(txn, ZX_OK);
mtx_lock(&port->lock);
}
} else {
Expand All @@ -594,7 +594,7 @@ static int ahci_worker_thread(void* arg) {
// complete the transaction with if it failed during processing
if (st != ZX_OK) {
mtx_unlock(&port->lock);
block_complete(&txn->bop, st);
block_complete(txn, st);
mtx_lock(&port->lock);
continue;
}
Expand Down Expand Up @@ -636,7 +636,7 @@ static int ahci_watchdog_thread(void* arg) {
port->running &= ~(1 << slot);
port->commands[slot] = NULL;
mtx_unlock(&port->lock);
block_complete(&txn->bop, ZX_ERR_TIMED_OUT);
block_complete(txn, ZX_ERR_TIMED_OUT);
mtx_lock(&port->lock);
}
}
Expand Down
22 changes: 12 additions & 10 deletions system/dev/block/ahci/sata.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ typedef struct sata_device {
int max_cmd; // inclusive
} sata_device_t;

static void sata_device_identify_complete(block_op_t* op, zx_status_t status) {
static void sata_device_identify_complete(void* cookie, zx_status_t status, block_op_t* op) {
sata_txn_t* txn = containerof(op, sata_txn_t, bop);
txn->status = status;
sync_completion_signal((sync_completion_t*)op->cookie);
sync_completion_signal((sync_completion_t*)cookie);
}

#define QEMU_MODEL_ID "EQUMH RADDSI K" // "QEMU HARDDISK"
Expand Down Expand Up @@ -76,12 +76,11 @@ static zx_status_t sata_device_identify(sata_device_t* dev, ahci_device_t* contr
.rw.length = 1,
.rw.offset_dev = 0,
.rw.offset_vmo = 0,
.rw.pages = NULL,
.completion_cb = sata_device_identify_complete,
.cookie = &completion,
},
.cmd = SATA_CMD_IDENTIFY_DEVICE,
.device = 0,
.completion_cb = sata_device_identify_complete,
.cookie = &completion,
};

ahci_queue(controller, dev->port, &txn);
Expand Down Expand Up @@ -232,22 +231,25 @@ static void sata_query(void* ctx, block_info_t* info_out, size_t* block_op_size_
*block_op_size_out = sizeof(sata_txn_t);
}

static void sata_queue(void* ctx, block_op_t* bop) {
static void sata_queue(void* ctx, block_op_t* bop, block_impl_queue_callback completion_cb,
void* cookie) {
sata_device_t* dev = ctx;
sata_txn_t* txn = containerof(bop, sata_txn_t, bop);
txn->completion_cb = completion_cb;
txn->cookie = cookie;

switch (BLOCK_OP(bop->command)) {
case BLOCK_OP_READ:
case BLOCK_OP_WRITE:
// complete empty transactions immediately
if (bop->rw.length == 0) {
block_complete(bop, ZX_ERR_INVALID_ARGS);
block_complete(txn, ZX_ERR_INVALID_ARGS);
return;
}
// transaction must fit within device
if ((bop->rw.offset_dev >= dev->info.block_count) ||
((dev->info.block_count - bop->rw.offset_dev) < bop->rw.length)) {
block_complete(bop, ZX_ERR_OUT_OF_RANGE);
block_complete(txn, ZX_ERR_OUT_OF_RANGE);
return;
}

Expand All @@ -260,14 +262,14 @@ static void sata_queue(void* ctx, block_op_t* bop) {
zxlogf(TRACE, "sata: queue FLUSH txn %p\n", txn);
break;
default:
block_complete(bop, ZX_ERR_NOT_SUPPORTED);
block_complete(txn, ZX_ERR_NOT_SUPPORTED);
return;
}

ahci_queue(dev->controller, dev->port, txn);
}

static block_protocol_ops_t sata_block_proto = {
static block_impl_protocol_ops_t sata_block_proto = {
.query = sata_query,
.queue = sata_queue,
};
Expand Down
6 changes: 4 additions & 2 deletions system/dev/block/ahci/sata.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ typedef struct sata_txn {

zx_status_t status;
zx_handle_t pmt;
block_impl_queue_callback completion_cb;
void* cookie;
} sata_txn_t;

typedef struct ahci_device ahci_device_t;
Expand All @@ -65,6 +67,6 @@ void ahci_set_devinfo(ahci_device_t* controller, int portnr, sata_devinfo_t* dev
// queue a txn on the controller
void ahci_queue(ahci_device_t* controller, int portnr, sata_txn_t* txn);

static inline void block_complete(block_op_t* bop, zx_status_t status) {
bop->completion_cb(bop, status);
static inline void block_complete(sata_txn_t* txn, zx_status_t status) {
txn->completion_cb(txn->cookie, status, &txn->bop);
}
28 changes: 13 additions & 15 deletions system/dev/block/block/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ typedef struct blkdev {
uint32_t threadcount;

// The block protocol of the device we are binding against.
block_protocol_t parent_protocol;
block_impl_protocol_t parent_protocol;
// The block protocol for ourselves, which redirects to the parent protocol,
// but may also collect auxiliary information like statistics.
block_protocol_t self_protocol;
block_impl_protocol_t self_protocol;

block_info_t info;
size_t block_op_size;
Expand Down Expand Up @@ -229,8 +229,8 @@ static zx_status_t blkdev_ioctl(void* ctx, uint32_t op, const void* cmd,
}
}

static void block_completion_cb(block_op_t* bop, zx_status_t status) {
blkdev_t* bdev = bop->cookie;
static void block_completion_cb(void* cookie, zx_status_t status, block_op_t* bop) {
blkdev_t* bdev = cookie;
bdev->iostatus = status;
sync_completion_signal(&bdev->iosignal);
}
Expand Down Expand Up @@ -284,12 +284,9 @@ static zx_status_t blkdev_io(blkdev_t* bdev, void* buf, size_t count,
bop->rw.vmo = bdev->iovmo;
bop->rw.offset_dev = (off + sub_txn_offset) / bsz;
bop->rw.offset_vmo = 0;
bop->rw.pages = NULL;
bop->completion_cb = block_completion_cb;
bop->cookie = bdev;

sync_completion_reset(&bdev->iosignal);
bdev->self_protocol.ops->queue(bdev->self_protocol.ctx, bop);
block_impl_queue(&bdev->self_protocol, bop, block_completion_cb, bdev);
sync_completion_wait(&bdev->iosignal, ZX_TIME_INFINITE);

if (bdev->iostatus != ZX_OK) {
Expand Down Expand Up @@ -359,13 +356,14 @@ static void blkdev_release(void* ctx) {
}
}

static void block_query(void* ctx, block_info_t* bi, size_t* bopsz) {
static void blkdev_query(void* ctx, block_info_t* bi, size_t* bopsz) {
blkdev_t* bdev = ctx;
memcpy(bi, &bdev->info, sizeof(block_info_t));
*bopsz = bdev->block_op_size;
}

static void block_queue(void* ctx, block_op_t* bop) {
static void blkdev_queue(void* ctx, block_op_t* bop, block_impl_queue_callback completion_cb,
void* cookie) {
blkdev_t* bdev = ctx;
uint64_t op = bop->command & BLOCK_OP_MASK;
bdev->stats.total_ops++;
Expand All @@ -378,7 +376,7 @@ static void block_queue(void* ctx, block_op_t* bop) {
bdev->stats.total_blocks_written += bop->rw.length;
bdev->stats.total_blocks += bop->rw.length;
}
bdev->parent_protocol.ops->queue(bdev->parent_protocol.ctx, bop);
block_impl_queue(&bdev->parent_protocol, bop, completion_cb, cookie);
}

static zx_status_t handle_stats(void* ctx, const void* cmd,
Expand Down Expand Up @@ -416,9 +414,9 @@ static zx_status_t handle_stats(void* ctx, const void* cmd,
}
}

static block_protocol_ops_t block_ops = {
.query = block_query,
.queue = block_queue,
static block_impl_protocol_ops_t block_ops = {
.query = blkdev_query,
.queue = blkdev_queue,
.get_stats = handle_stats,
};

Expand Down Expand Up @@ -449,7 +447,7 @@ static zx_status_t block_driver_bind(void* ctx, zx_device_t* dev) {

bdev->self_protocol.ctx = bdev;
bdev->self_protocol.ops = &block_ops;
bdev->parent_protocol.ops->query(bdev->parent_protocol.ctx, &bdev->info, &bdev->block_op_size);
block_impl_query(&bdev->parent_protocol, &bdev->info, &bdev->block_op_size);
// TODO(kmerrick) have this start as false and create IOCTL to toggle it
bdev->enable_stats = true;

Expand Down
16 changes: 7 additions & 9 deletions system/dev/block/block/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ void BlockComplete(BlockMsg* msg, zx_status_t status) {
extra->server->TxnEnd();
}

void BlockCompleteCb(block_op_t* bop, zx_status_t status) {
void BlockCompleteCb(void* cookie, zx_status_t status, block_op_t* bop) {
ZX_DEBUG_ASSERT(bop != nullptr);
BlockMsg msg(static_cast<block_msg_t*>(bop->cookie));
BlockMsg msg(static_cast<block_msg_t*>(cookie));
BlockComplete(&msg, status);
}

Expand All @@ -88,9 +88,6 @@ void InQueueAdd(zx_handle_t vmo, uint64_t length, uint64_t vmo_offset,
bop->rw.vmo = vmo;
bop->rw.offset_dev = dev_offset;
bop->rw.offset_vmo = vmo_offset;
bop->rw.pages = NULL;
bop->completion_cb = BlockCompleteCb;
bop->cookie = msg;
queue->push_back(msg);
}

Expand Down Expand Up @@ -262,11 +259,11 @@ void BlockServer::InQueueDrainer() {
// This may be altered in the future if block devices
// are capable of implementing hardware barriers.
msg->op.command &= ~(BLOCK_FL_BARRIER_BEFORE | BLOCK_FL_BARRIER_AFTER);
bp_->ops->queue(bp_->ctx, &msg->op);
bp_->ops->queue(bp_->ctx, &msg->op, BlockCompleteCb, &*msg);
}
}

zx_status_t BlockServer::Create(block_protocol_t* bp, fzl::fifo<block_fifo_request_t,
zx_status_t BlockServer::Create(block_impl_protocol_t* bp, fzl::fifo<block_fifo_request_t,
block_fifo_response_t>* fifo_out, BlockServer** out) {
fbl::AllocChecker ac;
BlockServer* bs = new (&ac) BlockServer(bp);
Expand Down Expand Up @@ -489,7 +486,7 @@ zx_status_t BlockServer::Serve() {
}
}

BlockServer::BlockServer(block_protocol_t* bp) :
BlockServer::BlockServer(block_impl_protocol_t* bp) :
bp_(bp), block_op_size_(0), pending_count_(0), barrier_in_progress_(false),
last_id_(VMOID_INVALID + 1) {
size_t block_op_size;
Expand All @@ -511,7 +508,8 @@ void BlockServer::ShutDown() {
}

// C declarations
zx_status_t blockserver_create(block_protocol_t* bp, zx_handle_t* fifo_out, BlockServer** out) {
zx_status_t blockserver_create(block_impl_protocol_t* bp, zx_handle_t* fifo_out,
BlockServer** out) {
fzl::fifo<block_fifo_request_t, block_fifo_response_t> fifo;
zx_status_t status = BlockServer::Create(bp, &fifo, out);
*fifo_out = fifo.release();
Expand Down
10 changes: 6 additions & 4 deletions system/dev/block/block/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class BlockMsg {
return *this;
}


~BlockMsg() {
reset();
}
Expand All @@ -144,7 +145,8 @@ class BlockMsg {
class BlockServer {
public:
// Creates a new BlockServer.
static zx_status_t Create(block_protocol_t* bp, fzl::fifo<block_fifo_request_t,
static zx_status_t Create(
block_impl_protocol_t* bp, fzl::fifo<block_fifo_request_t,
block_fifo_response_t>* fifo_out, BlockServer** out);

// Starts the BlockServer using the current thread
Expand All @@ -169,7 +171,7 @@ class BlockServer {
~BlockServer();
private:
DISALLOW_COPY_ASSIGN_AND_MOVE(BlockServer);
BlockServer(block_protocol_t* bp);
BlockServer(block_impl_protocol_t* bp);

// Helper for processing a single message read from the FIFO.
void ProcessRequest(block_fifo_request_t* request);
Expand All @@ -194,7 +196,7 @@ class BlockServer {

fzl::fifo<block_fifo_response_t, block_fifo_request_t> fifo_;
block_info_t info_;
block_protocol_t* bp_;
block_impl_protocol_t* bp_;
size_t block_op_size_;

// BARRIER_AFTER is implemented by sticking "BARRIER_BEFORE" on the
Expand All @@ -220,7 +222,7 @@ typedef struct BlockServer BlockServer;
__BEGIN_CDECLS

// Allocate a new blockserver + FIFO combo
zx_status_t blockserver_create(block_protocol_t* bp, zx_handle_t* fifo_out, BlockServer** out);
zx_status_t blockserver_create(block_impl_protocol_t* bp, zx_handle_t* fifo_out, BlockServer** out);

// Shut down the blockserver. It will stop serving requests.
void blockserver_shutdown(BlockServer* bs);
Expand Down
15 changes: 8 additions & 7 deletions system/dev/block/bootpart/bootpart.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef struct {
zx_device_t* zxdev;
zx_device_t* parent;

block_protocol_t bp;
block_impl_protocol_t bp;
zbi_partition_t part;

block_info_t info;
Expand Down Expand Up @@ -101,7 +101,8 @@ static void bootpart_query(void* ctx, block_info_t* bi, size_t* bopsz) {
*bopsz = bootpart->block_op_size;
}

static void bootpart_queue(void* ctx, block_op_t* bop) {
static void bootpart_queue(void* ctx, block_op_t* bop, block_impl_queue_callback completion_cb,
void* cookie) {
bootpart_device_t* bootpart = ctx;

switch (bop->command & BLOCK_OP_MASK) {
Expand All @@ -113,7 +114,7 @@ static void bootpart_queue(void* ctx, block_op_t* bop) {
// Ensure that the request is in-bounds
if ((bop->rw.offset_dev >= max) ||
((max - bop->rw.offset_dev) < blocks)) {
bop->completion_cb(bop, ZX_ERR_OUT_OF_RANGE);
completion_cb(cookie, ZX_ERR_OUT_OF_RANGE, bop);
return;
}

Expand All @@ -124,11 +125,11 @@ static void bootpart_queue(void* ctx, block_op_t* bop) {
case BLOCK_OP_FLUSH:
break;
default:
bop->completion_cb(bop, ZX_ERR_NOT_SUPPORTED);
completion_cb(cookie, ZX_ERR_NOT_SUPPORTED, bop);
return;
}

bootpart->bp.ops->queue(bootpart->bp.ctx, bop);
bootpart->bp.ops->queue(bootpart->bp.ctx, bop, completion_cb, cookie);
}

static void bootpart_unbind(void* ctx) {
Expand Down Expand Up @@ -156,13 +157,13 @@ static zx_protocol_device_t device_proto = {
.release = bootpart_release,
};

static block_protocol_ops_t block_ops = {
static block_impl_protocol_ops_t block_ops = {
.query = bootpart_query,
.queue = bootpart_queue,
};

static zx_status_t bootpart_bind(void* ctx, zx_device_t* parent) {
block_protocol_t bp;
block_impl_protocol_t bp;
uint8_t buffer[METADATA_PARTITION_MAP_MAX];
size_t actual;

Expand Down
Loading

0 comments on commit aecb14b

Please sign in to comment.