blob: 0a1734f34587dd4dc9e8b78468b05d37336e86b5 [file] [log] [blame]
Thomas Gleixner82c29812019-05-28 09:57:05 -07001// SPDX-License-Identifier: GPL-2.0-only
James Bottomley9927c682008-02-03 15:48:56 -06002/*
3 * SCSI Enclosure Services
4 *
5 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
Christoph Hellwig5ee7e1f2019-05-01 12:14:13 -04006 */
James Bottomley9927c682008-02-03 15:48:56 -06007
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
James Bottomley9927c682008-02-03 15:48:56 -06009#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/enclosure.h>
Hannes Reineckec38c0072014-03-15 09:51:51 +010012#include <asm/unaligned.h>
James Bottomley9927c682008-02-03 15:48:56 -060013
14#include <scsi/scsi.h>
15#include <scsi/scsi_cmnd.h>
16#include <scsi/scsi_dbg.h>
17#include <scsi/scsi_device.h>
18#include <scsi/scsi_driver.h>
19#include <scsi/scsi_host.h>
20
James Bottomley3f8d6f22015-12-09 12:56:07 -080021#include <scsi/scsi_transport_sas.h>
22
James Bottomley9927c682008-02-03 15:48:56 -060023struct ses_device {
Yinghai Lu691b4772008-02-13 16:25:16 -080024 unsigned char *page1;
James Bottomley8c3adc72011-03-18 11:24:23 -040025 unsigned char *page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -080026 unsigned char *page2;
27 unsigned char *page10;
James Bottomley9927c682008-02-03 15:48:56 -060028 short page1_len;
James Bottomley8c3adc72011-03-18 11:24:23 -040029 short page1_num_types;
James Bottomley9927c682008-02-03 15:48:56 -060030 short page2_len;
31 short page10_len;
32};
33
34struct ses_component {
35 u64 addr;
James Bottomley9927c682008-02-03 15:48:56 -060036};
37
Hannes Reineckedc56ce12017-08-15 10:21:43 +020038static bool ses_page2_supported(struct enclosure_device *edev)
39{
40 struct ses_device *ses_dev = edev->scratch;
41
42 return (ses_dev->page2 != NULL);
43}
44
James Bottomley9927c682008-02-03 15:48:56 -060045static int ses_probe(struct device *dev)
46{
47 struct scsi_device *sdev = to_scsi_device(dev);
48 int err = -ENODEV;
49
50 if (sdev->type != TYPE_ENCLOSURE)
51 goto out;
52
53 err = 0;
54 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
55
56 out:
57 return err;
58}
59
Matthew Wilcoxc95e62c2008-06-23 09:14:31 -060060#define SES_TIMEOUT (30 * HZ)
James Bottomley9927c682008-02-03 15:48:56 -060061#define SES_RETRIES 3
62
Song Liu08024882014-12-30 14:46:18 -080063static void init_device_slot_control(unsigned char *dest_desc,
64 struct enclosure_component *ecomp,
65 unsigned char *status)
66{
67 memcpy(dest_desc, status, 4);
68 dest_desc[0] = 0;
69 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
70 if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
71 dest_desc[1] = 0;
72 dest_desc[2] &= 0xde;
73 dest_desc[3] &= 0x3c;
74}
75
76
James Bottomley9927c682008-02-03 15:48:56 -060077static int ses_recv_diag(struct scsi_device *sdev, int page_code,
78 void *buf, int bufflen)
79{
James Bottomley3417c1b2015-12-08 09:00:31 -080080 int ret;
Yinghai Lu691b4772008-02-13 16:25:16 -080081 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060082 RECEIVE_DIAGNOSTIC,
83 1, /* Set PCV bit */
84 page_code,
85 bufflen >> 8,
86 bufflen & 0xff,
87 0
88 };
James Bottomley3417c1b2015-12-08 09:00:31 -080089 unsigned char recv_page_code;
Wen Xiongfbdac192021-09-16 22:24:21 -050090 unsigned int retries = SES_RETRIES;
91 struct scsi_sense_hdr sshdr;
James Bottomley9927c682008-02-03 15:48:56 -060092
Wen Xiongfbdac192021-09-16 22:24:21 -050093 do {
94 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
95 &sshdr, SES_TIMEOUT, 1, NULL);
96 } while (ret > 0 && --retries && scsi_sense_valid(&sshdr) &&
97 (sshdr.sense_key == NOT_READY ||
98 (sshdr.sense_key == UNIT_ATTENTION && sshdr.asc == 0x29)));
99
Brian King424f7272017-08-01 13:45:36 -0500100 if (unlikely(ret))
James Bottomley3417c1b2015-12-08 09:00:31 -0800101 return ret;
102
103 recv_page_code = ((unsigned char *)buf)[0];
104
105 if (likely(recv_page_code == page_code))
106 return ret;
107
108 /* successful diagnostic but wrong page code. This happens to some
109 * USB devices, just print a message and pretend there was an error */
110
111 sdev_printk(KERN_ERR, sdev,
112 "Wrong diagnostic page; asked for %d got %u\n",
113 page_code, recv_page_code);
114
115 return -EINVAL;
James Bottomley9927c682008-02-03 15:48:56 -0600116}
117
118static int ses_send_diag(struct scsi_device *sdev, int page_code,
119 void *buf, int bufflen)
120{
Jiapeng Chongdd689ed2021-09-24 17:51:53 +0800121 int result;
James Bottomley9927c682008-02-03 15:48:56 -0600122
Yinghai Lu691b4772008-02-13 16:25:16 -0800123 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -0600124 SEND_DIAGNOSTIC,
125 0x10, /* Set PF bit */
126 0,
127 bufflen >> 8,
128 bufflen & 0xff,
129 0
130 };
Wen Xiongfbdac192021-09-16 22:24:21 -0500131 struct scsi_sense_hdr sshdr;
132 unsigned int retries = SES_RETRIES;
James Bottomley9927c682008-02-03 15:48:56 -0600133
Wen Xiongfbdac192021-09-16 22:24:21 -0500134 do {
135 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
136 &sshdr, SES_TIMEOUT, 1, NULL);
137 } while (result > 0 && --retries && scsi_sense_valid(&sshdr) &&
138 (sshdr.sense_key == NOT_READY ||
139 (sshdr.sense_key == UNIT_ATTENTION && sshdr.asc == 0x29)));
140
James Bottomley9927c682008-02-03 15:48:56 -0600141 if (result)
142 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
143 result);
144 return result;
145}
146
147static int ses_set_page2_descriptor(struct enclosure_device *edev,
148 struct enclosure_component *ecomp,
Yinghai Lu691b4772008-02-13 16:25:16 -0800149 unsigned char *desc)
James Bottomley9927c682008-02-03 15:48:56 -0600150{
151 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100152 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600153 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400154 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800155 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600156
157 /* Clear everything */
158 memset(desc_ptr, 0, ses_dev->page2_len - 8);
James Bottomley8c3adc72011-03-18 11:24:23 -0400159 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600160 for (j = 0; j < type_ptr[1]; j++) {
161 desc_ptr += 4;
162 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
163 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
164 continue;
165 if (count++ == descriptor) {
166 memcpy(desc_ptr, desc, 4);
167 /* set select */
168 desc_ptr[0] |= 0x80;
169 /* clear reserved, just in case */
170 desc_ptr[0] &= 0xf0;
171 }
172 }
173 }
174
175 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
176}
177
Yinghai Lu691b4772008-02-13 16:25:16 -0800178static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
James Bottomley9927c682008-02-03 15:48:56 -0600179 struct enclosure_component *ecomp)
180{
181 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100182 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600183 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400184 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800185 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600186
Hannes Reineckeacf8ab92017-08-15 10:21:41 +0200187 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0)
188 return NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600189
James Bottomley8c3adc72011-03-18 11:24:23 -0400190 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600191 for (j = 0; j < type_ptr[1]; j++) {
192 desc_ptr += 4;
193 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
194 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
195 continue;
196 if (count++ == descriptor)
197 return desc_ptr;
198 }
199 }
200 return NULL;
201}
202
Douglas Gilbert2a350ca2011-06-09 00:27:07 -0400203/* For device slot and array device slot elements, byte 3 bit 6
204 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
205 * code stands these bits are shifted 4 positions right so in
206 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
James Bottomley9927c682008-02-03 15:48:56 -0600207static void ses_get_fault(struct enclosure_device *edev,
208 struct enclosure_component *ecomp)
209{
Yinghai Lu691b4772008-02-13 16:25:16 -0800210 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600211
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200212 if (!ses_page2_supported(edev)) {
213 ecomp->fault = 0;
214 return;
215 }
James Bottomley9927c682008-02-03 15:48:56 -0600216 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800217 if (desc)
218 ecomp->fault = (desc[3] & 0x60) >> 4;
James Bottomley9927c682008-02-03 15:48:56 -0600219}
220
221static int ses_set_fault(struct enclosure_device *edev,
222 struct enclosure_component *ecomp,
223 enum enclosure_component_setting val)
224{
Song Liu08024882014-12-30 14:46:18 -0800225 unsigned char desc[4];
226 unsigned char *desc_ptr;
227
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200228 if (!ses_page2_supported(edev))
229 return -EINVAL;
230
Song Liu08024882014-12-30 14:46:18 -0800231 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
232
233 if (!desc_ptr)
234 return -EIO;
235
236 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600237
238 switch (val) {
239 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800240 desc[3] &= 0xdf;
James Bottomley9927c682008-02-03 15:48:56 -0600241 break;
242 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800243 desc[3] |= 0x20;
James Bottomley9927c682008-02-03 15:48:56 -0600244 break;
245 default:
246 /* SES doesn't do the SGPIO blink settings */
247 return -EINVAL;
248 }
249
250 return ses_set_page2_descriptor(edev, ecomp, desc);
251}
252
253static void ses_get_status(struct enclosure_device *edev,
254 struct enclosure_component *ecomp)
255{
Yinghai Lu691b4772008-02-13 16:25:16 -0800256 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600257
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200258 if (!ses_page2_supported(edev)) {
259 ecomp->status = 0;
260 return;
261 }
James Bottomley9927c682008-02-03 15:48:56 -0600262 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800263 if (desc)
264 ecomp->status = (desc[0] & 0x0f);
James Bottomley9927c682008-02-03 15:48:56 -0600265}
266
267static void ses_get_locate(struct enclosure_device *edev,
268 struct enclosure_component *ecomp)
269{
Yinghai Lu691b4772008-02-13 16:25:16 -0800270 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600271
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200272 if (!ses_page2_supported(edev)) {
273 ecomp->locate = 0;
274 return;
275 }
James Bottomley9927c682008-02-03 15:48:56 -0600276 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800277 if (desc)
278 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
James Bottomley9927c682008-02-03 15:48:56 -0600279}
280
281static int ses_set_locate(struct enclosure_device *edev,
282 struct enclosure_component *ecomp,
283 enum enclosure_component_setting val)
284{
Song Liu08024882014-12-30 14:46:18 -0800285 unsigned char desc[4];
286 unsigned char *desc_ptr;
287
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200288 if (!ses_page2_supported(edev))
289 return -EINVAL;
290
Song Liu08024882014-12-30 14:46:18 -0800291 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
292
293 if (!desc_ptr)
294 return -EIO;
295
296 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600297
298 switch (val) {
299 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800300 desc[2] &= 0xfd;
James Bottomley9927c682008-02-03 15:48:56 -0600301 break;
302 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800303 desc[2] |= 0x02;
James Bottomley9927c682008-02-03 15:48:56 -0600304 break;
305 default:
306 /* SES doesn't do the SGPIO blink settings */
307 return -EINVAL;
308 }
309 return ses_set_page2_descriptor(edev, ecomp, desc);
310}
311
312static int ses_set_active(struct enclosure_device *edev,
313 struct enclosure_component *ecomp,
314 enum enclosure_component_setting val)
315{
Song Liu08024882014-12-30 14:46:18 -0800316 unsigned char desc[4];
317 unsigned char *desc_ptr;
318
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200319 if (!ses_page2_supported(edev))
320 return -EINVAL;
321
Song Liu08024882014-12-30 14:46:18 -0800322 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
323
324 if (!desc_ptr)
325 return -EIO;
326
327 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600328
329 switch (val) {
330 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800331 desc[2] &= 0x7f;
James Bottomley9927c682008-02-03 15:48:56 -0600332 ecomp->active = 0;
333 break;
334 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800335 desc[2] |= 0x80;
James Bottomley9927c682008-02-03 15:48:56 -0600336 ecomp->active = 1;
337 break;
338 default:
339 /* SES doesn't do the SGPIO blink settings */
340 return -EINVAL;
341 }
342 return ses_set_page2_descriptor(edev, ecomp, desc);
343}
344
Dan Williams967f7ba2014-12-30 14:46:16 -0800345static int ses_show_id(struct enclosure_device *edev, char *buf)
346{
347 struct ses_device *ses_dev = edev->scratch;
348 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
349
350 return sprintf(buf, "%#llx\n", id);
351}
352
Song Liu08024882014-12-30 14:46:18 -0800353static void ses_get_power_status(struct enclosure_device *edev,
354 struct enclosure_component *ecomp)
355{
356 unsigned char *desc;
357
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200358 if (!ses_page2_supported(edev)) {
359 ecomp->power_status = 0;
360 return;
361 }
362
Song Liu08024882014-12-30 14:46:18 -0800363 desc = ses_get_page2_descriptor(edev, ecomp);
364 if (desc)
365 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
366}
367
368static int ses_set_power_status(struct enclosure_device *edev,
369 struct enclosure_component *ecomp,
370 int val)
371{
372 unsigned char desc[4];
373 unsigned char *desc_ptr;
374
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200375 if (!ses_page2_supported(edev))
376 return -EINVAL;
377
Song Liu08024882014-12-30 14:46:18 -0800378 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
379
380 if (!desc_ptr)
381 return -EIO;
382
383 init_device_slot_control(desc, ecomp, desc_ptr);
384
385 switch (val) {
386 /* power = 1 is device_off = 0 and vice versa */
387 case 0:
388 desc[3] |= 0x10;
389 break;
390 case 1:
391 desc[3] &= 0xef;
392 break;
393 default:
394 return -EINVAL;
395 }
396 ecomp->power_status = val;
397 return ses_set_page2_descriptor(edev, ecomp, desc);
398}
399
James Bottomley9927c682008-02-03 15:48:56 -0600400static struct enclosure_component_callbacks ses_enclosure_callbacks = {
401 .get_fault = ses_get_fault,
402 .set_fault = ses_set_fault,
403 .get_status = ses_get_status,
404 .get_locate = ses_get_locate,
405 .set_locate = ses_set_locate,
Song Liu08024882014-12-30 14:46:18 -0800406 .get_power_status = ses_get_power_status,
407 .set_power_status = ses_set_power_status,
James Bottomley9927c682008-02-03 15:48:56 -0600408 .set_active = ses_set_active,
Dan Williams967f7ba2014-12-30 14:46:16 -0800409 .show_id = ses_show_id,
James Bottomley9927c682008-02-03 15:48:56 -0600410};
411
412struct ses_host_edev {
413 struct Scsi_Host *shost;
414 struct enclosure_device *edev;
415};
416
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800417#if 0
James Bottomley9927c682008-02-03 15:48:56 -0600418int ses_match_host(struct enclosure_device *edev, void *data)
419{
420 struct ses_host_edev *sed = data;
421 struct scsi_device *sdev;
422
Tony Jonesee959b02008-02-22 00:13:36 +0100423 if (!scsi_is_sdev_device(edev->edev.parent))
James Bottomley9927c682008-02-03 15:48:56 -0600424 return 0;
425
Tony Jonesee959b02008-02-22 00:13:36 +0100426 sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600427
428 if (sdev->host != sed->shost)
429 return 0;
430
431 sed->edev = edev;
432 return 1;
433}
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800434#endif /* 0 */
James Bottomley9927c682008-02-03 15:48:56 -0600435
436static void ses_process_descriptor(struct enclosure_component *ecomp,
437 unsigned char *desc)
438{
439 int eip = desc[0] & 0x10;
440 int invalid = desc[0] & 0x80;
441 enum scsi_protocol proto = desc[0] & 0x0f;
442 u64 addr = 0;
Dan Williams921ce7f2014-12-30 14:46:17 -0800443 int slot = -1;
James Bottomley9927c682008-02-03 15:48:56 -0600444 struct ses_component *scomp = ecomp->scratch;
445 unsigned char *d;
446
James Bottomley9927c682008-02-03 15:48:56 -0600447 if (invalid)
448 return;
449
450 switch (proto) {
Dan Williams921ce7f2014-12-30 14:46:17 -0800451 case SCSI_PROTOCOL_FCP:
452 if (eip) {
453 d = desc + 4;
454 slot = d[3];
455 }
456 break;
James Bottomley9927c682008-02-03 15:48:56 -0600457 case SCSI_PROTOCOL_SAS:
Dan Williams921ce7f2014-12-30 14:46:17 -0800458 if (eip) {
459 d = desc + 4;
460 slot = d[3];
James Bottomley9927c682008-02-03 15:48:56 -0600461 d = desc + 8;
Dan Williams921ce7f2014-12-30 14:46:17 -0800462 } else
James Bottomley9927c682008-02-03 15:48:56 -0600463 d = desc + 4;
464 /* only take the phy0 addr */
465 addr = (u64)d[12] << 56 |
466 (u64)d[13] << 48 |
467 (u64)d[14] << 40 |
468 (u64)d[15] << 32 |
469 (u64)d[16] << 24 |
470 (u64)d[17] << 16 |
471 (u64)d[18] << 8 |
472 (u64)d[19];
473 break;
474 default:
475 /* FIXME: Need to add more protocols than just SAS */
476 break;
477 }
Dan Williams921ce7f2014-12-30 14:46:17 -0800478 ecomp->slot = slot;
James Bottomley9927c682008-02-03 15:48:56 -0600479 scomp->addr = addr;
480}
481
482struct efd {
483 u64 addr;
484 struct device *dev;
485};
486
487static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
488 void *data)
489{
490 struct efd *efd = data;
491 int i;
492 struct ses_component *scomp;
493
494 if (!edev->component[0].scratch)
495 return 0;
496
497 for (i = 0; i < edev->components; i++) {
498 scomp = edev->component[i].scratch;
499 if (scomp->addr != efd->addr)
500 continue;
501
Dan Williams15a0fbb2014-12-30 14:46:15 -0800502 if (enclosure_add_device(edev, i, efd->dev) == 0)
503 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
James Bottomley9927c682008-02-03 15:48:56 -0600504 return 1;
505 }
506 return 0;
507}
508
James Bottomley21fab1d2009-08-01 00:43:59 +0000509#define INIT_ALLOC_SIZE 32
510
511static void ses_enclosure_data_process(struct enclosure_device *edev,
512 struct scsi_device *sdev,
513 int create)
514{
515 u32 result;
516 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
517 int i, j, page7_len, len, components;
518 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400519 int types = ses_dev->page1_num_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000520 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
521
522 if (!hdr_buf)
523 goto simple_populate;
524
525 /* re-read page 10 */
526 if (ses_dev->page10)
527 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
528 /* Page 7 for the descriptors is optional */
529 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
530 if (result)
531 goto simple_populate;
532
533 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
534 /* add 1 for trailing '\0' we'll use */
535 buf = kzalloc(len + 1, GFP_KERNEL);
536 if (!buf)
537 goto simple_populate;
538 result = ses_recv_diag(sdev, 7, buf, len);
539 if (result) {
540 simple_populate:
541 kfree(buf);
542 buf = NULL;
543 desc_ptr = NULL;
544 len = 0;
545 page7_len = 0;
546 } else {
547 desc_ptr = buf + 8;
548 len = (desc_ptr[2] << 8) + desc_ptr[3];
549 /* skip past overall descriptor */
550 desc_ptr += len + 4;
James Bottomley21fab1d2009-08-01 00:43:59 +0000551 }
John Hughes877a5592009-11-04 19:01:22 +0100552 if (ses_dev->page10)
553 addl_desc_ptr = ses_dev->page10 + 8;
James Bottomley8c3adc72011-03-18 11:24:23 -0400554 type_ptr = ses_dev->page1_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000555 components = 0;
556 for (i = 0; i < types; i++, type_ptr += 4) {
557 for (j = 0; j < type_ptr[1]; j++) {
558 char *name = NULL;
559 struct enclosure_component *ecomp;
560
561 if (desc_ptr) {
562 if (desc_ptr >= buf + page7_len) {
563 desc_ptr = NULL;
564 } else {
565 len = (desc_ptr[2] << 8) + desc_ptr[3];
566 desc_ptr += 4;
567 /* Add trailing zero - pushes into
568 * reserved space */
569 desc_ptr[len] = '\0';
570 name = desc_ptr;
571 }
572 }
573 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
574 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
575
576 if (create)
Dan Williamsed09dcc2014-12-30 14:46:14 -0800577 ecomp = enclosure_component_alloc(
578 edev,
579 components++,
580 type_ptr[0],
581 name);
James Bottomley21fab1d2009-08-01 00:43:59 +0000582 else
583 ecomp = &edev->component[components++];
584
Dan Williamsed09dcc2014-12-30 14:46:14 -0800585 if (!IS_ERR(ecomp)) {
586 if (addl_desc_ptr)
587 ses_process_descriptor(
588 ecomp,
589 addl_desc_ptr);
590 if (create)
591 enclosure_component_register(
592 ecomp);
593 }
James Bottomley21fab1d2009-08-01 00:43:59 +0000594 }
595 if (desc_ptr)
596 desc_ptr += len;
597
James Bottomley5e103352015-12-11 09:16:38 -0800598 if (addl_desc_ptr &&
599 /* only find additional descriptions for specific devices */
600 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
601 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
602 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
603 /* these elements are optional */
604 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
605 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
606 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
James Bottomley21fab1d2009-08-01 00:43:59 +0000607 addl_desc_ptr += addl_desc_ptr[1] + 2;
608
609 }
610 }
611 kfree(buf);
612 kfree(hdr_buf);
613}
614
James Bottomley9927c682008-02-03 15:48:56 -0600615static void ses_match_to_enclosure(struct enclosure_device *edev,
Li Dongyang9c0a5002017-11-14 10:48:04 +1100616 struct scsi_device *sdev,
617 int refresh)
James Bottomley9927c682008-02-03 15:48:56 -0600618{
Li Dongyang9c0a5002017-11-14 10:48:04 +1100619 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600620 struct efd efd = {
621 .addr = 0,
622 };
James Bottomley9927c682008-02-03 15:48:56 -0600623
Li Dongyang9c0a5002017-11-14 10:48:04 +1100624 if (refresh)
625 ses_enclosure_data_process(edev, edev_sdev, 0);
James Bottomley21fab1d2009-08-01 00:43:59 +0000626
Ewan D. Milne9373eba2017-01-09 16:33:36 -0500627 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
James Bottomley3f8d6f22015-12-09 12:56:07 -0800628 efd.addr = sas_get_address(sdev);
James Bottomley671a99c2008-07-29 11:38:25 -0500629
Hannes Reineckec38c0072014-03-15 09:51:51 +0100630 if (efd.addr) {
631 efd.dev = &sdev->sdev_gendev;
James Bottomley9927c682008-02-03 15:48:56 -0600632
Hannes Reineckec38c0072014-03-15 09:51:51 +0100633 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
634 }
James Bottomley9927c682008-02-03 15:48:56 -0600635}
636
Tony Jonesee959b02008-02-22 00:13:36 +0100637static int ses_intf_add(struct device *cdev,
James Bottomley9927c682008-02-03 15:48:56 -0600638 struct class_interface *intf)
639{
Tony Jonesee959b02008-02-22 00:13:36 +0100640 struct scsi_device *sdev = to_scsi_device(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600641 struct scsi_device *tmp_sdev;
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200642 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
James Bottomley9927c682008-02-03 15:48:56 -0600643 struct ses_device *ses_dev;
644 u32 result;
James Bottomley21fab1d2009-08-01 00:43:59 +0000645 int i, types, len, components = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600646 int err = -ENOMEM;
James Bottomley8c3adc72011-03-18 11:24:23 -0400647 int num_enclosures;
James Bottomley9927c682008-02-03 15:48:56 -0600648 struct enclosure_device *edev;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800649 struct ses_component *scomp = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600650
651 if (!scsi_device_enclosure(sdev)) {
652 /* not an enclosure, but might be in one */
James Bottomley163f52b2009-08-01 00:39:36 +0000653 struct enclosure_device *prev = NULL;
654
655 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
Li Dongyang9c0a5002017-11-14 10:48:04 +1100656 ses_match_to_enclosure(edev, sdev, 1);
James Bottomley163f52b2009-08-01 00:39:36 +0000657 prev = edev;
James Bottomley9927c682008-02-03 15:48:56 -0600658 }
659 return -ENODEV;
660 }
661
662 /* TYPE_ENCLOSURE prints a message in probe */
663 if (sdev->type != TYPE_ENCLOSURE)
664 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
665
666 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
667 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
668 if (!hdr_buf || !ses_dev)
669 goto err_init_free;
670
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200671 page = 1;
672 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
James Bottomley9927c682008-02-03 15:48:56 -0600673 if (result)
674 goto recv_failed;
675
James Bottomley9927c682008-02-03 15:48:56 -0600676 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
677 buf = kzalloc(len, GFP_KERNEL);
678 if (!buf)
679 goto err_free;
680
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200681 result = ses_recv_diag(sdev, page, buf, len);
James Bottomley9927c682008-02-03 15:48:56 -0600682 if (result)
683 goto recv_failed;
684
James Bottomley8c3adc72011-03-18 11:24:23 -0400685 types = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600686
James Bottomley8c3adc72011-03-18 11:24:23 -0400687 /* we always have one main enclosure and the rest are referred
688 * to as secondary subenclosures */
689 num_enclosures = buf[1] + 1;
James Bottomley9927c682008-02-03 15:48:56 -0600690
James Bottomley8c3adc72011-03-18 11:24:23 -0400691 /* begin at the enclosure descriptor */
692 type_ptr = buf + 8;
693 /* skip all the enclosure descriptors */
694 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
695 types += type_ptr[2];
696 type_ptr += type_ptr[3] + 4;
697 }
698
699 ses_dev->page1_types = type_ptr;
700 ses_dev->page1_num_types = types;
701
702 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600703 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
704 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
705 components += type_ptr[1];
706 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800707 ses_dev->page1 = buf;
708 ses_dev->page1_len = len;
709 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600710
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200711 page = 2;
712 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
James Bottomley9927c682008-02-03 15:48:56 -0600713 if (result)
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200714 goto page2_not_supported;
James Bottomley9927c682008-02-03 15:48:56 -0600715
716 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
717 buf = kzalloc(len, GFP_KERNEL);
718 if (!buf)
719 goto err_free;
720
721 /* make sure getting page 2 actually works */
722 result = ses_recv_diag(sdev, 2, buf, len);
723 if (result)
724 goto recv_failed;
725 ses_dev->page2 = buf;
726 ses_dev->page2_len = len;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800727 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600728
729 /* The additional information page --- allows us
730 * to match up the devices */
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200731 page = 10;
732 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
Yinghai Lu691b4772008-02-13 16:25:16 -0800733 if (!result) {
James Bottomley9927c682008-02-03 15:48:56 -0600734
Yinghai Lu691b4772008-02-13 16:25:16 -0800735 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
736 buf = kzalloc(len, GFP_KERNEL);
737 if (!buf)
738 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600739
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200740 result = ses_recv_diag(sdev, page, buf, len);
Yinghai Lu691b4772008-02-13 16:25:16 -0800741 if (result)
742 goto recv_failed;
743 ses_dev->page10 = buf;
744 ses_dev->page10_len = len;
745 buf = NULL;
746 }
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200747page2_not_supported:
Kees Cook6396bb22018-06-12 14:03:40 -0700748 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
James Bottomley9927c682008-02-03 15:48:56 -0600749 if (!scomp)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800750 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600751
Kay Sievers71610f52008-12-03 22:41:36 +0100752 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
James Bottomley9927c682008-02-03 15:48:56 -0600753 components, &ses_enclosure_callbacks);
754 if (IS_ERR(edev)) {
755 err = PTR_ERR(edev);
756 goto err_free;
757 }
758
Julia Lawall9b3a6542010-03-10 15:20:42 -0800759 kfree(hdr_buf);
760
James Bottomley9927c682008-02-03 15:48:56 -0600761 edev->scratch = ses_dev;
762 for (i = 0; i < components; i++)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800763 edev->component[i].scratch = scomp + i;
James Bottomley9927c682008-02-03 15:48:56 -0600764
James Bottomley21fab1d2009-08-01 00:43:59 +0000765 ses_enclosure_data_process(edev, sdev, 1);
James Bottomley9927c682008-02-03 15:48:56 -0600766
767 /* see if there are any devices matching before
768 * we found the enclosure */
769 shost_for_each_device(tmp_sdev, sdev->host) {
770 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
771 continue;
Li Dongyang9c0a5002017-11-14 10:48:04 +1100772 ses_match_to_enclosure(edev, tmp_sdev, 0);
James Bottomley9927c682008-02-03 15:48:56 -0600773 }
774
775 return 0;
776
777 recv_failed:
778 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200779 page);
James Bottomley9927c682008-02-03 15:48:56 -0600780 err = -ENODEV;
781 err_free:
782 kfree(buf);
Yinghai Lu7c46c202008-02-10 23:25:25 -0800783 kfree(scomp);
James Bottomley9927c682008-02-03 15:48:56 -0600784 kfree(ses_dev->page10);
785 kfree(ses_dev->page2);
786 kfree(ses_dev->page1);
787 err_init_free:
788 kfree(ses_dev);
789 kfree(hdr_buf);
790 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
791 return err;
792}
793
794static int ses_remove(struct device *dev)
795{
796 return 0;
797}
798
James Bottomley43d8eb92009-08-01 00:41:22 +0000799static void ses_intf_remove_component(struct scsi_device *sdev)
James Bottomley9927c682008-02-03 15:48:56 -0600800{
James Bottomley43d8eb92009-08-01 00:41:22 +0000801 struct enclosure_device *edev, *prev = NULL;
802
803 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
804 prev = edev;
805 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
806 break;
807 }
808 if (edev)
809 put_device(&edev->edev);
810}
811
812static void ses_intf_remove_enclosure(struct scsi_device *sdev)
813{
James Bottomley9927c682008-02-03 15:48:56 -0600814 struct enclosure_device *edev;
815 struct ses_device *ses_dev;
816
James Bottomley163f52b2009-08-01 00:39:36 +0000817 /* exact match to this enclosure */
James Bottomley43d8eb92009-08-01 00:41:22 +0000818 edev = enclosure_find(&sdev->sdev_gendev, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600819 if (!edev)
820 return;
821
822 ses_dev = edev->scratch;
823 edev->scratch = NULL;
824
Yinghai Lu7c46c202008-02-10 23:25:25 -0800825 kfree(ses_dev->page10);
James Bottomley9927c682008-02-03 15:48:56 -0600826 kfree(ses_dev->page1);
827 kfree(ses_dev->page2);
828 kfree(ses_dev);
829
830 kfree(edev->component[0].scratch);
831
Tony Jonesee959b02008-02-22 00:13:36 +0100832 put_device(&edev->edev);
Calvin Owensa5a039b2017-08-24 15:13:52 +0200833 enclosure_unregister(edev);
James Bottomley9927c682008-02-03 15:48:56 -0600834}
835
James Bottomley43d8eb92009-08-01 00:41:22 +0000836static void ses_intf_remove(struct device *cdev,
837 struct class_interface *intf)
838{
839 struct scsi_device *sdev = to_scsi_device(cdev->parent);
840
841 if (!scsi_device_enclosure(sdev))
842 ses_intf_remove_component(sdev);
843 else
844 ses_intf_remove_enclosure(sdev);
845}
846
James Bottomley9927c682008-02-03 15:48:56 -0600847static struct class_interface ses_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100848 .add_dev = ses_intf_add,
849 .remove_dev = ses_intf_remove,
James Bottomley9927c682008-02-03 15:48:56 -0600850};
851
852static struct scsi_driver ses_template = {
James Bottomley9927c682008-02-03 15:48:56 -0600853 .gendrv = {
854 .name = "ses",
Christoph Hellwig3af6b352014-11-12 18:34:51 +0100855 .owner = THIS_MODULE,
James Bottomley9927c682008-02-03 15:48:56 -0600856 .probe = ses_probe,
857 .remove = ses_remove,
858 },
859};
860
861static int __init ses_init(void)
862{
863 int err;
864
865 err = scsi_register_interface(&ses_interface);
866 if (err)
867 return err;
868
869 err = scsi_register_driver(&ses_template.gendrv);
870 if (err)
871 goto out_unreg;
872
873 return 0;
874
875 out_unreg:
876 scsi_unregister_interface(&ses_interface);
877 return err;
878}
879
880static void __exit ses_exit(void)
881{
882 scsi_unregister_driver(&ses_template.gendrv);
883 scsi_unregister_interface(&ses_interface);
884}
885
886module_init(ses_init);
887module_exit(ses_exit);
888
889MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
890
891MODULE_AUTHOR("James Bottomley");
892MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
893MODULE_LICENSE("GPL v2");