1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  CCW device SENSE ID I/O handling.
4  *
5  *    Copyright IBM Corp. 2002, 2009
6  *    Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
7  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>
8  *		 Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/types.h>
14 #include <linux/errno.h>
15 #include <asm/machine.h>
16 #include <asm/ccwdev.h>
17 #include <asm/setup.h>
18 #include <asm/cio.h>
19 #include <asm/diag.h>
20 
21 #include "cio.h"
22 #include "cio_debug.h"
23 #include "device.h"
24 #include "io_sch.h"
25 
26 #define SENSE_ID_RETRIES	256
27 #define SENSE_ID_TIMEOUT	(10 * HZ)
28 #define SENSE_ID_MIN_LEN	4
29 #define SENSE_ID_BASIC_LEN	7
30 
31 /**
32  * diag210_to_senseid - convert diag 0x210 data to sense id information
33  * @senseid: sense id
34  * @diag: diag 0x210 data
35  *
36  * Return 0 on success, non-zero otherwise.
37  */
diag210_to_senseid(struct senseid * senseid,struct diag210 * diag)38 static int diag210_to_senseid(struct senseid *senseid, struct diag210 *diag)
39 {
40 	static struct {
41 		int class, type, cu_type;
42 	} vm_devices[] = {
43 		{ 0x08, 0x01, 0x3480 },
44 		{ 0x08, 0x02, 0x3430 },
45 		{ 0x08, 0x10, 0x3420 },
46 		{ 0x08, 0x42, 0x3424 },
47 		{ 0x08, 0x44, 0x9348 },
48 		{ 0x08, 0x81, 0x3490 },
49 		{ 0x08, 0x82, 0x3422 },
50 		{ 0x10, 0x41, 0x1403 },
51 		{ 0x10, 0x42, 0x3211 },
52 		{ 0x10, 0x43, 0x3203 },
53 		{ 0x10, 0x45, 0x3800 },
54 		{ 0x10, 0x47, 0x3262 },
55 		{ 0x10, 0x48, 0x3820 },
56 		{ 0x10, 0x49, 0x3800 },
57 		{ 0x10, 0x4a, 0x4245 },
58 		{ 0x10, 0x4b, 0x4248 },
59 		{ 0x10, 0x4d, 0x3800 },
60 		{ 0x10, 0x4e, 0x3820 },
61 		{ 0x10, 0x4f, 0x3820 },
62 		{ 0x10, 0x82, 0x2540 },
63 		{ 0x10, 0x84, 0x3525 },
64 		{ 0x20, 0x81, 0x2501 },
65 		{ 0x20, 0x82, 0x2540 },
66 		{ 0x20, 0x84, 0x3505 },
67 		{ 0x40, 0x01, 0x3278 },
68 		{ 0x40, 0x04, 0x3277 },
69 		{ 0x40, 0x80, 0x2250 },
70 		{ 0x40, 0xc0, 0x5080 },
71 		{ 0x80, 0x00, 0x3215 },
72 	};
73 	int i;
74 
75 	/* Special case for osa devices. */
76 	if (diag->vrdcvcla == 0x02 && diag->vrdcvtyp == 0x20) {
77 		senseid->cu_type = 0x3088;
78 		senseid->cu_model = 0x60;
79 		senseid->reserved = 0xff;
80 		return 0;
81 	}
82 	for (i = 0; i < ARRAY_SIZE(vm_devices); i++) {
83 		if (diag->vrdcvcla == vm_devices[i].class &&
84 		    diag->vrdcvtyp == vm_devices[i].type) {
85 			senseid->cu_type = vm_devices[i].cu_type;
86 			senseid->reserved = 0xff;
87 			return 0;
88 		}
89 	}
90 
91 	return -ENODEV;
92 }
93 
94 /**
95  * diag210_get_dev_info - retrieve device information via diag 0x210
96  * @cdev: ccw device
97  *
98  * Returns zero on success, non-zero otherwise.
99  */
diag210_get_dev_info(struct ccw_device * cdev)100 static int diag210_get_dev_info(struct ccw_device *cdev)
101 {
102 	struct ccw_dev_id *dev_id = &cdev->private->dev_id;
103 	struct senseid *senseid = &cdev->private->dma_area->senseid;
104 	struct diag210 diag_data;
105 	int rc;
106 
107 	if (dev_id->ssid != 0)
108 		return -ENODEV;
109 	memset(&diag_data, 0, sizeof(diag_data));
110 	diag_data.vrdcdvno	= dev_id->devno;
111 	diag_data.vrdclen	= sizeof(diag_data);
112 	rc = diag210(&diag_data);
113 	CIO_TRACE_EVENT(4, "diag210");
114 	CIO_HEX_EVENT(4, &rc, sizeof(rc));
115 	CIO_HEX_EVENT(4, &diag_data, sizeof(diag_data));
116 	if (rc != 0 && rc != 2)
117 		goto err_failed;
118 	if (diag210_to_senseid(senseid, &diag_data))
119 		goto err_unknown;
120 	return 0;
121 
122 err_unknown:
123 	CIO_MSG_EVENT(0, "snsid: device 0.%x.%04x: unknown diag210 data\n",
124 		      dev_id->ssid, dev_id->devno);
125 	return -ENODEV;
126 err_failed:
127 	CIO_MSG_EVENT(0, "snsid: device 0.%x.%04x: diag210 failed (rc=%d)\n",
128 		      dev_id->ssid, dev_id->devno, rc);
129 	return -ENODEV;
130 }
131 
132 /*
133  * Initialize SENSE ID data.
134  */
snsid_init(struct ccw_device * cdev)135 static void snsid_init(struct ccw_device *cdev)
136 {
137 	cdev->private->flags.esid = 0;
138 
139 	memset(&cdev->private->dma_area->senseid, 0,
140 	       sizeof(cdev->private->dma_area->senseid));
141 	cdev->private->dma_area->senseid.cu_type = 0xffff;
142 }
143 
144 /*
145  * Check for complete SENSE ID data.
146  */
snsid_check(struct ccw_device * cdev,void * data)147 static int snsid_check(struct ccw_device *cdev, void *data)
148 {
149 	struct cmd_scsw *scsw = &cdev->private->dma_area->irb.scsw.cmd;
150 	int len = sizeof(struct senseid) - scsw->count;
151 
152 	/* Check for incomplete SENSE ID data. */
153 	if (len < SENSE_ID_MIN_LEN)
154 		goto out_restart;
155 	if (cdev->private->dma_area->senseid.cu_type == 0xffff)
156 		goto out_restart;
157 	/* Check for incompatible SENSE ID data. */
158 	if (cdev->private->dma_area->senseid.reserved != 0xff)
159 		return -EOPNOTSUPP;
160 	/* Check for extended-identification information. */
161 	if (len > SENSE_ID_BASIC_LEN)
162 		cdev->private->flags.esid = 1;
163 	return 0;
164 
165 out_restart:
166 	snsid_init(cdev);
167 	return -EAGAIN;
168 }
169 
170 /*
171  * Process SENSE ID request result.
172  */
snsid_callback(struct ccw_device * cdev,void * data,int rc)173 static void snsid_callback(struct ccw_device *cdev, void *data, int rc)
174 {
175 	struct ccw_dev_id *id = &cdev->private->dev_id;
176 	struct senseid *senseid = &cdev->private->dma_area->senseid;
177 	int vm = 0;
178 
179 	if (rc && machine_is_vm()) {
180 		/* Try diag 0x210 fallback on z/VM. */
181 		snsid_init(cdev);
182 		if (diag210_get_dev_info(cdev) == 0) {
183 			rc = 0;
184 			vm = 1;
185 		}
186 	}
187 	CIO_MSG_EVENT(2, "snsid: device 0.%x.%04x: rc=%d %04x/%02x "
188 		      "%04x/%02x%s\n", id->ssid, id->devno, rc,
189 		      senseid->cu_type, senseid->cu_model, senseid->dev_type,
190 		      senseid->dev_model, vm ? " (diag210)" : "");
191 	ccw_device_sense_id_done(cdev, rc);
192 }
193 
194 /**
195  * ccw_device_sense_id_start - perform SENSE ID
196  * @cdev: ccw device
197  *
198  * Execute a SENSE ID channel program on @cdev to update its sense id
199  * information. When finished, call ccw_device_sense_id_done with a
200  * return code specifying the result.
201  */
ccw_device_sense_id_start(struct ccw_device * cdev)202 void ccw_device_sense_id_start(struct ccw_device *cdev)
203 {
204 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
205 	struct ccw_request *req = &cdev->private->req;
206 	struct ccw1 *cp = cdev->private->dma_area->iccws;
207 
208 	CIO_TRACE_EVENT(4, "snsid");
209 	CIO_HEX_EVENT(4, &cdev->private->dev_id, sizeof(cdev->private->dev_id));
210 	/* Data setup. */
211 	snsid_init(cdev);
212 	/* Channel program setup. */
213 	cp->cmd_code	= CCW_CMD_SENSE_ID;
214 	cp->cda		= virt_to_dma32(&cdev->private->dma_area->senseid);
215 	cp->count	= sizeof(struct senseid);
216 	cp->flags	= CCW_FLAG_SLI;
217 	/* Request setup. */
218 	memset(req, 0, sizeof(*req));
219 	req->cp		= cp;
220 	req->timeout	= SENSE_ID_TIMEOUT;
221 	req->maxretries	= SENSE_ID_RETRIES;
222 	req->lpm	= sch->schib.pmcw.pam & sch->opm;
223 	req->check	= snsid_check;
224 	req->callback	= snsid_callback;
225 	ccw_request_start(cdev);
226 }
227