Tolstoy's description of the terrible sufferings of Ivan Illich in his great short novel "The Death of Ivan Illich" certainly had a purpose in making the reader appreciate the reality of death and the thoughts, questions, regrets, remorse. and self-assessment that approaching death could have for a human being; on the other hand, for the viewpoint character Ivan Illich himself, the suffering did not have a purpose but was just gratuitous. Here are pertinent quotes from Chapter X:
He suffered ever the same unceasing agonies and in his loneliness pondered always on the same unsoluble question: "What is this? Can it be that it is Death?" And the inner voice answered: "Yes, it is Death."
"Why these sufferings?" And the voice answered, "For no reason--they just are so." Beyond and besides this there was nothing.
So the answer to your question, "Did Ivan Illich's suffering have a purpose?" the answer, according to the author himself, is "For no reason--they just are so."