Feast of the Elfs by John C. Wright
Second book in the Green Knight's Squire trilogy. Serious spoilers ahead.
Starting with the way it starts with Gil in jail.
The redcap he dealt with at the end of the last book tries to get his cap back by freeing him, but Gil won't take it; instead he gets advice on how to reach the halls of the elfs. Then, he's taken out to talk with a man who sits in a dark room (you may have met him in another book, not by Wright) and is offered a job involving policework and swearing allegiance to King Arthur. The man suggests that he must train for knighthood with elfs, enemy though they are.
And so at liberty, he meets up with Ruff -- who can smell he's a policeman now -- and they set out. He learns a secret of Ruff's. And his quest to train as a knight goes on, involving spiteful squires, birds who help, the rule at the feast about how a certain dish can only be served after a marvel, rescuing a damsel in distress, not obeying an important command, the king's champion, the title Green Knight, and much more.