what is the earliest source of the practice of gilgulei sheleg (rolling in snow)?
also, what is the purpose of this and why specifically snow instead of cold water or whatever .
This is one of the many self-flagellative practices of the Hassidei Ashkenaz. The Rokeah, for example, writes (Hilkhot Teshuva: 11) that sitting in ice or snow is an appropriate form of penance for sexual relations with a married woman:
הבא על אשת איש שהוא במיתה יסבול צער קשה כמיתה ישב בקרח או בשלג בכל יום שעה אחת בכל יום פעם אחת או פעמיים
It doesnt seem that one needs to do that act of self-affliction in particular; it is just a way of causing great suffering to oneself, "similar to death". Accordingly, in the summer, he suggests (there) other forms of self-flagellation, such as sitting among bees or other insects.
He further generally recommends all types of self-flagellation and suffering.
This is similar to the writings of his mentor R. Yehuda HaHassid who in Sefer Hassidim (ed. Margolis: 176) suggests torturing oneself by sitting in ice. (Although perhaps this refers to sitting in a frozen river (cf. 177) in which the pain comes from the cold water, rather than the ice.) He similarly approvingly cites a story (528) about a pious person sitting with his feet in freezing water until his feet became frozen together. Here too it doesn't sound like there is significance to torturing yourself with ice in particular; winter just affords someone with great snow and ice torture opportunities. During the summer other forms of self-affliction are possible (cf. 167).
Importantly, however, the Sefer Hassidim adds (there; in parenthesis in ed. Margolis) that water is particularly appropriate for use in afflicting oneself, as there is a Midrash that Adam afflicted himself for 130 years with water as a form of penance for eating from the ets hadaat. (Perhaps this would apply to snow or ice as well):
ולמה במים אמרו במדרש ק"ל שנים היה אדם הראשון יושב במים עד חוטמו להתכפר על שחטא בעץ הדעת שנגזר גזירה על כל הדורות
A partial answer re. the second half of the question*:
The purpose of this form of bodily mortification is to sustain commensurate repentance (a concept developed by the early pietists known as "teshuvat ha-mishkal", wherein the offender inflicts pain commensurate to the punishment biblically prescribed for a given sin).
In a responsum re. a person who committed homosexual activity, R. Yosef Hayyim of Baghdad (Rav Pe'alim 2:44, s.v. 'ועל שאלה הג) prescribes, according to Lurianic tradition, rolling in snow. He adds that if snow cannot be obtained, immersion in freezing waters is sufficient. Again, because the original aim is accomplished.
*Re. the first half; @mevaqesh's source is indeed, AFAIK, either the earliest or of the earliest discussing the practice. My only reservation is based on recollection of J. Elbaum in his book 'תשובת הלב וקבלת יסורים' where IIRC he published a manuscript of Rokeach's predecessor, R. Judah Ha-Hassid who likewise made mention of rolling in snow (including smearing one's self with honey and sitting among bees, even to the extent of suicide in atonement for personal sins).