OED
The Oxford English Dictionary has this:
As n. phr. in the slang phr. to give (one) what for = to inflict severe pain or chastisement. Also, to show (someone) what for: to make him take notice; to show who is in charge.
Their first quotation is 1873 (for "give you what for") but I some antedatings.
"Give you what for"
Here's a pair of 1868 antedatings. First, Springdale Abbey (page 159, 1868) by Joseph Parker:

Spite of your melancholy wheezing, to which the hydropathic adder turns a deaf ear, he catches you a sharp rap on the back, and with a knowing wink at one of his co-bucketters, he says he will give you " what for."
Second, Secrets of the Turf; or, how I won the Derby (page 125, 1868) by Samuel Bracebridge Hemying:

I'm the master of the situation, and if you don't take your hook instanter, I reckon I'll give you what for with this bit of wood.”
"Show you what for"
I found some earlier antedatings for this version, the oldest in Wild Western Scenes: A Narrative of Adventures in the Western Wilderness (page 137, 1858) by John Beauchamp Jones:

"What did you do that for ?" asked Sneak, rising up and brushing the snow from his head, and face, his fall having broken the icy surface.
" You rascal, you ! I'll show you what for !" cried Joe, endeavouring to get at him again.
This shows it used as an angry response to someone asking "What did you do that for?".