Subfield codes of linear codes over finite fields have recently received much attention since they can produce optimal codes, which may have applications in secret sharing, authentication codes and association schemes. In this paper, we first present a construction framework of 3-dimensional linear codes C(f.g )over F-q(m) parameterized by any two functions f, g over F-q(m) , and then study the properties of six types of C-f.g, its punctured code C-f.g*, and their corresponding subfield codes over F-q. The classification of C-f,C-g is based on special choices of f, g as trace function, norm function, almost bent function, Boolean bent function or a combination of these functions. For the first two types of C-f.g, we explicitly determine the weight distributions and dualities of C-f,C-g, C-f.g* and their subfield codes over F-q. The remaining four types of C-f.g are restricted to q = 2, and the weight distributions and dualities of the subfields code C-f,C-g(q) and C-f.g(*(q)) are completely determined. Most of the resultant linear codes (over F-q(m) or over F-q) have few weights. Some of them are optimal and some have the best-known parameters according to the tables maintained at http://www.codetables.de. In fact, 16 infinite families of optimal linear codes are produced in this paper. As a byproduct, a family of [2(4m-2), 2m+1, 2(4m-3)] quaternary Hermitian self-orthogonal codes are obtained with m >= 2. As an application, we present several infinite families of 2-designs or 3-designs with some of the codes presented in this paper.