Skip to content

Commit

Permalink
net/ncsi: Avoid if statements in ncsi_suspend_channel()
Browse files Browse the repository at this point in the history
There are several if/else statements in the state machine implemented
by switch/case in ncsi_suspend_channel() to avoid duplicated code. It
makes the code a bit hard to be understood.

This drops if/else statements in ncsi_suspend_channel() to improve the
code readability as Joel Stanley suggested. Also, it becomes easy to
add more states in the state machine without affecting current code.
No logical changes introduced by this.

Suggested-by: Joel Stanley <[email protected]>
Signed-off-by: Gavin Shan <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Gavin Shan authored and davem330 committed Oct 20, 2016
1 parent 5712bf9 commit 7ba5c00
Showing 1 changed file with 50 additions and 28 deletions.
78 changes: 50 additions & 28 deletions net/ncsi/ncsi-manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,42 +540,60 @@ static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
nd->state = ncsi_dev_state_suspend_select;
/* Fall through */
case ncsi_dev_state_suspend_select:
ndp->pending_req_num = 1;

nca.type = NCSI_PKT_CMD_SP;
nca.package = np->id;
nca.channel = NCSI_RESERVED_CHANNEL;
if (ndp->flags & NCSI_DEV_HWA)
nca.bytes[0] = 0;
else
nca.bytes[0] = 1;

nd->state = ncsi_dev_state_suspend_dcnt;
ret = ncsi_xmit_cmd(&nca);
if (ret)
goto error;

break;
case ncsi_dev_state_suspend_dcnt:
ndp->pending_req_num = 1;

nca.type = NCSI_PKT_CMD_DCNT;
nca.package = np->id;
nca.channel = nc->id;

nd->state = ncsi_dev_state_suspend_dc;
ret = ncsi_xmit_cmd(&nca);
if (ret)
goto error;

break;
case ncsi_dev_state_suspend_dc:
ndp->pending_req_num = 1;

nca.type = NCSI_PKT_CMD_DC;
nca.package = np->id;
nca.channel = nc->id;
nca.bytes[0] = 1;

nd->state = ncsi_dev_state_suspend_deselect;
ret = ncsi_xmit_cmd(&nca);
if (ret)
goto error;

break;
case ncsi_dev_state_suspend_deselect:
ndp->pending_req_num = 1;

np = ndp->active_package;
nc = ndp->active_channel;
nca.type = NCSI_PKT_CMD_DP;
nca.package = np->id;
if (nd->state == ncsi_dev_state_suspend_select) {
nca.type = NCSI_PKT_CMD_SP;
nca.channel = NCSI_RESERVED_CHANNEL;
if (ndp->flags & NCSI_DEV_HWA)
nca.bytes[0] = 0;
else
nca.bytes[0] = 1;
nd->state = ncsi_dev_state_suspend_dcnt;
} else if (nd->state == ncsi_dev_state_suspend_dcnt) {
nca.type = NCSI_PKT_CMD_DCNT;
nca.channel = nc->id;
nd->state = ncsi_dev_state_suspend_dc;
} else if (nd->state == ncsi_dev_state_suspend_dc) {
nca.type = NCSI_PKT_CMD_DC;
nca.channel = nc->id;
nca.bytes[0] = 1;
nd->state = ncsi_dev_state_suspend_deselect;
} else if (nd->state == ncsi_dev_state_suspend_deselect) {
nca.type = NCSI_PKT_CMD_DP;
nca.channel = NCSI_RESERVED_CHANNEL;
nd->state = ncsi_dev_state_suspend_done;
}
nca.channel = NCSI_RESERVED_CHANNEL;

nd->state = ncsi_dev_state_suspend_done;
ret = ncsi_xmit_cmd(&nca);
if (ret) {
nd->state = ncsi_dev_state_functional;
return;
}
if (ret)
goto error;

break;
case ncsi_dev_state_suspend_done:
Expand All @@ -589,6 +607,10 @@ static void ncsi_suspend_channel(struct ncsi_dev_priv *ndp)
netdev_warn(nd->dev, "Wrong NCSI state 0x%x in suspend\n",
nd->state);
}

return;
error:
nd->state = ncsi_dev_state_functional;
}

static void ncsi_configure_channel(struct ncsi_dev_priv *ndp)
Expand Down

0 comments on commit 7ba5c00

Please sign in to comment.