Energy levels of magnesium, Mg I through Mg XII