blob: 60f01a7b728c52f418561821982ab5676f71a1b1 [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>
6 *
7**-----------------------------------------------------------------------------
8**
James Bottomley9927c682008-02-03 15:48:56 -06009**
10**-----------------------------------------------------------------------------
11*/
12
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
James Bottomley9927c682008-02-03 15:48:56 -060014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/enclosure.h>
Hannes Reineckec38c0072014-03-15 09:51:51 +010017#include <asm/unaligned.h>
James Bottomley9927c682008-02-03 15:48:56 -060018
19#include <scsi/scsi.h>
20#include <scsi/scsi_cmnd.h>
21#include <scsi/scsi_dbg.h>
22#include <scsi/scsi_device.h>
23#include <scsi/scsi_driver.h>
24#include <scsi/scsi_host.h>
25
James Bottomley3f8d6f22015-12-09 12:56:07 -080026#include <scsi/scsi_transport_sas.h>
27
James Bottomley9927c682008-02-03 15:48:56 -060028struct ses_device {
Yinghai Lu691b4772008-02-13 16:25:16 -080029 unsigned char *page1;
James Bottomley8c3adc72011-03-18 11:24:23 -040030 unsigned char *page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -080031 unsigned char *page2;
32 unsigned char *page10;
James Bottomley9927c682008-02-03 15:48:56 -060033 short page1_len;
James Bottomley8c3adc72011-03-18 11:24:23 -040034 short page1_num_types;
James Bottomley9927c682008-02-03 15:48:56 -060035 short page2_len;
36 short page10_len;
37};
38
39struct ses_component {
40 u64 addr;
James Bottomley9927c682008-02-03 15:48:56 -060041};
42
Hannes Reineckedc56ce12017-08-15 10:21:43 +020043static bool ses_page2_supported(struct enclosure_device *edev)
44{
45 struct ses_device *ses_dev = edev->scratch;
46
47 return (ses_dev->page2 != NULL);
48}
49
James Bottomley9927c682008-02-03 15:48:56 -060050static int ses_probe(struct device *dev)
51{
52 struct scsi_device *sdev = to_scsi_device(dev);
53 int err = -ENODEV;
54
55 if (sdev->type != TYPE_ENCLOSURE)
56 goto out;
57
58 err = 0;
59 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
60
61 out:
62 return err;
63}
64
Matthew Wilcoxc95e62c2008-06-23 09:14:31 -060065#define SES_TIMEOUT (30 * HZ)
James Bottomley9927c682008-02-03 15:48:56 -060066#define SES_RETRIES 3
67
Song Liu08024882014-12-30 14:46:18 -080068static void init_device_slot_control(unsigned char *dest_desc,
69 struct enclosure_component *ecomp,
70 unsigned char *status)
71{
72 memcpy(dest_desc, status, 4);
73 dest_desc[0] = 0;
74 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
75 if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
76 dest_desc[1] = 0;
77 dest_desc[2] &= 0xde;
78 dest_desc[3] &= 0x3c;
79}
80
81
James Bottomley9927c682008-02-03 15:48:56 -060082static int ses_recv_diag(struct scsi_device *sdev, int page_code,
83 void *buf, int bufflen)
84{
James Bottomley3417c1b2015-12-08 09:00:31 -080085 int ret;
Yinghai Lu691b4772008-02-13 16:25:16 -080086 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060087 RECEIVE_DIAGNOSTIC,
88 1, /* Set PCV bit */
89 page_code,
90 bufflen >> 8,
91 bufflen & 0xff,
92 0
93 };
James Bottomley3417c1b2015-12-08 09:00:31 -080094 unsigned char recv_page_code;
James Bottomley9927c682008-02-03 15:48:56 -060095
James Bottomley3417c1b2015-12-08 09:00:31 -080096 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +090097 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
Brian King424f7272017-08-01 13:45:36 -050098 if (unlikely(ret))
James Bottomley3417c1b2015-12-08 09:00:31 -080099 return ret;
100
101 recv_page_code = ((unsigned char *)buf)[0];
102
103 if (likely(recv_page_code == page_code))
104 return ret;
105
106 /* successful diagnostic but wrong page code. This happens to some
107 * USB devices, just print a message and pretend there was an error */
108
109 sdev_printk(KERN_ERR, sdev,
110 "Wrong diagnostic page; asked for %d got %u\n",
111 page_code, recv_page_code);
112
113 return -EINVAL;
James Bottomley9927c682008-02-03 15:48:56 -0600114}
115
116static int ses_send_diag(struct scsi_device *sdev, int page_code,
117 void *buf, int bufflen)
118{
119 u32 result;
120
Yinghai Lu691b4772008-02-13 16:25:16 -0800121 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -0600122 SEND_DIAGNOSTIC,
123 0x10, /* Set PF bit */
124 0,
125 bufflen >> 8,
126 bufflen & 0xff,
127 0
128 };
129
130 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900131 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600132 if (result)
133 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
134 result);
135 return result;
136}
137
138static int ses_set_page2_descriptor(struct enclosure_device *edev,
139 struct enclosure_component *ecomp,
Yinghai Lu691b4772008-02-13 16:25:16 -0800140 unsigned char *desc)
James Bottomley9927c682008-02-03 15:48:56 -0600141{
142 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100143 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600144 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400145 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800146 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600147
148 /* Clear everything */
149 memset(desc_ptr, 0, ses_dev->page2_len - 8);
James Bottomley8c3adc72011-03-18 11:24:23 -0400150 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600151 for (j = 0; j < type_ptr[1]; j++) {
152 desc_ptr += 4;
153 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
154 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
155 continue;
156 if (count++ == descriptor) {
157 memcpy(desc_ptr, desc, 4);
158 /* set select */
159 desc_ptr[0] |= 0x80;
160 /* clear reserved, just in case */
161 desc_ptr[0] &= 0xf0;
162 }
163 }
164 }
165
166 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
167}
168
Yinghai Lu691b4772008-02-13 16:25:16 -0800169static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
James Bottomley9927c682008-02-03 15:48:56 -0600170 struct enclosure_component *ecomp)
171{
172 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100173 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600174 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400175 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800176 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600177
Hannes Reineckeacf8ab92017-08-15 10:21:41 +0200178 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0)
179 return NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600180
James Bottomley8c3adc72011-03-18 11:24:23 -0400181 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600182 for (j = 0; j < type_ptr[1]; j++) {
183 desc_ptr += 4;
184 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
185 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
186 continue;
187 if (count++ == descriptor)
188 return desc_ptr;
189 }
190 }
191 return NULL;
192}
193
Douglas Gilbert2a350ca2011-06-09 00:27:07 -0400194/* For device slot and array device slot elements, byte 3 bit 6
195 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
196 * code stands these bits are shifted 4 positions right so in
197 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
James Bottomley9927c682008-02-03 15:48:56 -0600198static void ses_get_fault(struct enclosure_device *edev,
199 struct enclosure_component *ecomp)
200{
Yinghai Lu691b4772008-02-13 16:25:16 -0800201 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600202
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200203 if (!ses_page2_supported(edev)) {
204 ecomp->fault = 0;
205 return;
206 }
James Bottomley9927c682008-02-03 15:48:56 -0600207 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800208 if (desc)
209 ecomp->fault = (desc[3] & 0x60) >> 4;
James Bottomley9927c682008-02-03 15:48:56 -0600210}
211
212static int ses_set_fault(struct enclosure_device *edev,
213 struct enclosure_component *ecomp,
214 enum enclosure_component_setting val)
215{
Song Liu08024882014-12-30 14:46:18 -0800216 unsigned char desc[4];
217 unsigned char *desc_ptr;
218
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200219 if (!ses_page2_supported(edev))
220 return -EINVAL;
221
Song Liu08024882014-12-30 14:46:18 -0800222 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
223
224 if (!desc_ptr)
225 return -EIO;
226
227 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600228
229 switch (val) {
230 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800231 desc[3] &= 0xdf;
James Bottomley9927c682008-02-03 15:48:56 -0600232 break;
233 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800234 desc[3] |= 0x20;
James Bottomley9927c682008-02-03 15:48:56 -0600235 break;
236 default:
237 /* SES doesn't do the SGPIO blink settings */
238 return -EINVAL;
239 }
240
241 return ses_set_page2_descriptor(edev, ecomp, desc);
242}
243
244static void ses_get_status(struct enclosure_device *edev,
245 struct enclosure_component *ecomp)
246{
Yinghai Lu691b4772008-02-13 16:25:16 -0800247 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600248
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200249 if (!ses_page2_supported(edev)) {
250 ecomp->status = 0;
251 return;
252 }
James Bottomley9927c682008-02-03 15:48:56 -0600253 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800254 if (desc)
255 ecomp->status = (desc[0] & 0x0f);
James Bottomley9927c682008-02-03 15:48:56 -0600256}
257
258static void ses_get_locate(struct enclosure_device *edev,
259 struct enclosure_component *ecomp)
260{
Yinghai Lu691b4772008-02-13 16:25:16 -0800261 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600262
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200263 if (!ses_page2_supported(edev)) {
264 ecomp->locate = 0;
265 return;
266 }
James Bottomley9927c682008-02-03 15:48:56 -0600267 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800268 if (desc)
269 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
James Bottomley9927c682008-02-03 15:48:56 -0600270}
271
272static int ses_set_locate(struct enclosure_device *edev,
273 struct enclosure_component *ecomp,
274 enum enclosure_component_setting val)
275{
Song Liu08024882014-12-30 14:46:18 -0800276 unsigned char desc[4];
277 unsigned char *desc_ptr;
278
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200279 if (!ses_page2_supported(edev))
280 return -EINVAL;
281
Song Liu08024882014-12-30 14:46:18 -0800282 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
283
284 if (!desc_ptr)
285 return -EIO;
286
287 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600288
289 switch (val) {
290 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800291 desc[2] &= 0xfd;
James Bottomley9927c682008-02-03 15:48:56 -0600292 break;
293 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800294 desc[2] |= 0x02;
James Bottomley9927c682008-02-03 15:48:56 -0600295 break;
296 default:
297 /* SES doesn't do the SGPIO blink settings */
298 return -EINVAL;
299 }
300 return ses_set_page2_descriptor(edev, ecomp, desc);
301}
302
303static int ses_set_active(struct enclosure_device *edev,
304 struct enclosure_component *ecomp,
305 enum enclosure_component_setting val)
306{
Song Liu08024882014-12-30 14:46:18 -0800307 unsigned char desc[4];
308 unsigned char *desc_ptr;
309
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200310 if (!ses_page2_supported(edev))
311 return -EINVAL;
312
Song Liu08024882014-12-30 14:46:18 -0800313 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
314
315 if (!desc_ptr)
316 return -EIO;
317
318 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600319
320 switch (val) {
321 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800322 desc[2] &= 0x7f;
James Bottomley9927c682008-02-03 15:48:56 -0600323 ecomp->active = 0;
324 break;
325 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800326 desc[2] |= 0x80;
James Bottomley9927c682008-02-03 15:48:56 -0600327 ecomp->active = 1;
328 break;
329 default:
330 /* SES doesn't do the SGPIO blink settings */
331 return -EINVAL;
332 }
333 return ses_set_page2_descriptor(edev, ecomp, desc);
334}
335
Dan Williams967f7ba2014-12-30 14:46:16 -0800336static int ses_show_id(struct enclosure_device *edev, char *buf)
337{
338 struct ses_device *ses_dev = edev->scratch;
339 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
340
341 return sprintf(buf, "%#llx\n", id);
342}
343
Song Liu08024882014-12-30 14:46:18 -0800344static void ses_get_power_status(struct enclosure_device *edev,
345 struct enclosure_component *ecomp)
346{
347 unsigned char *desc;
348
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200349 if (!ses_page2_supported(edev)) {
350 ecomp->power_status = 0;
351 return;
352 }
353
Song Liu08024882014-12-30 14:46:18 -0800354 desc = ses_get_page2_descriptor(edev, ecomp);
355 if (desc)
356 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
357}
358
359static int ses_set_power_status(struct enclosure_device *edev,
360 struct enclosure_component *ecomp,
361 int val)
362{
363 unsigned char desc[4];
364 unsigned char *desc_ptr;
365
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200366 if (!ses_page2_supported(edev))
367 return -EINVAL;
368
Song Liu08024882014-12-30 14:46:18 -0800369 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
370
371 if (!desc_ptr)
372 return -EIO;
373
374 init_device_slot_control(desc, ecomp, desc_ptr);
375
376 switch (val) {
377 /* power = 1 is device_off = 0 and vice versa */
378 case 0:
379 desc[3] |= 0x10;
380 break;
381 case 1:
382 desc[3] &= 0xef;
383 break;
384 default:
385 return -EINVAL;
386 }
387 ecomp->power_status = val;
388 return ses_set_page2_descriptor(edev, ecomp, desc);
389}
390
James Bottomley9927c682008-02-03 15:48:56 -0600391static struct enclosure_component_callbacks ses_enclosure_callbacks = {
392 .get_fault = ses_get_fault,
393 .set_fault = ses_set_fault,
394 .get_status = ses_get_status,
395 .get_locate = ses_get_locate,
396 .set_locate = ses_set_locate,
Song Liu08024882014-12-30 14:46:18 -0800397 .get_power_status = ses_get_power_status,
398 .set_power_status = ses_set_power_status,
James Bottomley9927c682008-02-03 15:48:56 -0600399 .set_active = ses_set_active,
Dan Williams967f7ba2014-12-30 14:46:16 -0800400 .show_id = ses_show_id,
James Bottomley9927c682008-02-03 15:48:56 -0600401};
402
403struct ses_host_edev {
404 struct Scsi_Host *shost;
405 struct enclosure_device *edev;
406};
407
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800408#if 0
James Bottomley9927c682008-02-03 15:48:56 -0600409int ses_match_host(struct enclosure_device *edev, void *data)
410{
411 struct ses_host_edev *sed = data;
412 struct scsi_device *sdev;
413
Tony Jonesee959b02008-02-22 00:13:36 +0100414 if (!scsi_is_sdev_device(edev->edev.parent))
James Bottomley9927c682008-02-03 15:48:56 -0600415 return 0;
416
Tony Jonesee959b02008-02-22 00:13:36 +0100417 sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600418
419 if (sdev->host != sed->shost)
420 return 0;
421
422 sed->edev = edev;
423 return 1;
424}
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800425#endif /* 0 */
James Bottomley9927c682008-02-03 15:48:56 -0600426
427static void ses_process_descriptor(struct enclosure_component *ecomp,
428 unsigned char *desc)
429{
430 int eip = desc[0] & 0x10;
431 int invalid = desc[0] & 0x80;
432 enum scsi_protocol proto = desc[0] & 0x0f;
433 u64 addr = 0;
Dan Williams921ce7f2014-12-30 14:46:17 -0800434 int slot = -1;
James Bottomley9927c682008-02-03 15:48:56 -0600435 struct ses_component *scomp = ecomp->scratch;
436 unsigned char *d;
437
James Bottomley9927c682008-02-03 15:48:56 -0600438 if (invalid)
439 return;
440
441 switch (proto) {
Dan Williams921ce7f2014-12-30 14:46:17 -0800442 case SCSI_PROTOCOL_FCP:
443 if (eip) {
444 d = desc + 4;
445 slot = d[3];
446 }
447 break;
James Bottomley9927c682008-02-03 15:48:56 -0600448 case SCSI_PROTOCOL_SAS:
Dan Williams921ce7f2014-12-30 14:46:17 -0800449 if (eip) {
450 d = desc + 4;
451 slot = d[3];
James Bottomley9927c682008-02-03 15:48:56 -0600452 d = desc + 8;
Dan Williams921ce7f2014-12-30 14:46:17 -0800453 } else
James Bottomley9927c682008-02-03 15:48:56 -0600454 d = desc + 4;
455 /* only take the phy0 addr */
456 addr = (u64)d[12] << 56 |
457 (u64)d[13] << 48 |
458 (u64)d[14] << 40 |
459 (u64)d[15] << 32 |
460 (u64)d[16] << 24 |
461 (u64)d[17] << 16 |
462 (u64)d[18] << 8 |
463 (u64)d[19];
464 break;
465 default:
466 /* FIXME: Need to add more protocols than just SAS */
467 break;
468 }
Dan Williams921ce7f2014-12-30 14:46:17 -0800469 ecomp->slot = slot;
James Bottomley9927c682008-02-03 15:48:56 -0600470 scomp->addr = addr;
471}
472
473struct efd {
474 u64 addr;
475 struct device *dev;
476};
477
478static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
479 void *data)
480{
481 struct efd *efd = data;
482 int i;
483 struct ses_component *scomp;
484
485 if (!edev->component[0].scratch)
486 return 0;
487
488 for (i = 0; i < edev->components; i++) {
489 scomp = edev->component[i].scratch;
490 if (scomp->addr != efd->addr)
491 continue;
492
Dan Williams15a0fbb2014-12-30 14:46:15 -0800493 if (enclosure_add_device(edev, i, efd->dev) == 0)
494 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
James Bottomley9927c682008-02-03 15:48:56 -0600495 return 1;
496 }
497 return 0;
498}
499
James Bottomley21fab1d2009-08-01 00:43:59 +0000500#define INIT_ALLOC_SIZE 32
501
502static void ses_enclosure_data_process(struct enclosure_device *edev,
503 struct scsi_device *sdev,
504 int create)
505{
506 u32 result;
507 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
508 int i, j, page7_len, len, components;
509 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400510 int types = ses_dev->page1_num_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000511 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
512
513 if (!hdr_buf)
514 goto simple_populate;
515
516 /* re-read page 10 */
517 if (ses_dev->page10)
518 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
519 /* Page 7 for the descriptors is optional */
520 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
521 if (result)
522 goto simple_populate;
523
524 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
525 /* add 1 for trailing '\0' we'll use */
526 buf = kzalloc(len + 1, GFP_KERNEL);
527 if (!buf)
528 goto simple_populate;
529 result = ses_recv_diag(sdev, 7, buf, len);
530 if (result) {
531 simple_populate:
532 kfree(buf);
533 buf = NULL;
534 desc_ptr = NULL;
535 len = 0;
536 page7_len = 0;
537 } else {
538 desc_ptr = buf + 8;
539 len = (desc_ptr[2] << 8) + desc_ptr[3];
540 /* skip past overall descriptor */
541 desc_ptr += len + 4;
James Bottomley21fab1d2009-08-01 00:43:59 +0000542 }
John Hughes877a5592009-11-04 19:01:22 +0100543 if (ses_dev->page10)
544 addl_desc_ptr = ses_dev->page10 + 8;
James Bottomley8c3adc72011-03-18 11:24:23 -0400545 type_ptr = ses_dev->page1_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000546 components = 0;
547 for (i = 0; i < types; i++, type_ptr += 4) {
548 for (j = 0; j < type_ptr[1]; j++) {
549 char *name = NULL;
550 struct enclosure_component *ecomp;
551
552 if (desc_ptr) {
553 if (desc_ptr >= buf + page7_len) {
554 desc_ptr = NULL;
555 } else {
556 len = (desc_ptr[2] << 8) + desc_ptr[3];
557 desc_ptr += 4;
558 /* Add trailing zero - pushes into
559 * reserved space */
560 desc_ptr[len] = '\0';
561 name = desc_ptr;
562 }
563 }
564 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
565 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
566
567 if (create)
Dan Williamsed09dcc2014-12-30 14:46:14 -0800568 ecomp = enclosure_component_alloc(
569 edev,
570 components++,
571 type_ptr[0],
572 name);
James Bottomley21fab1d2009-08-01 00:43:59 +0000573 else
574 ecomp = &edev->component[components++];
575
Dan Williamsed09dcc2014-12-30 14:46:14 -0800576 if (!IS_ERR(ecomp)) {
577 if (addl_desc_ptr)
578 ses_process_descriptor(
579 ecomp,
580 addl_desc_ptr);
581 if (create)
582 enclosure_component_register(
583 ecomp);
584 }
James Bottomley21fab1d2009-08-01 00:43:59 +0000585 }
586 if (desc_ptr)
587 desc_ptr += len;
588
James Bottomley5e103352015-12-11 09:16:38 -0800589 if (addl_desc_ptr &&
590 /* only find additional descriptions for specific devices */
591 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
592 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
593 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
594 /* these elements are optional */
595 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
596 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
597 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
James Bottomley21fab1d2009-08-01 00:43:59 +0000598 addl_desc_ptr += addl_desc_ptr[1] + 2;
599
600 }
601 }
602 kfree(buf);
603 kfree(hdr_buf);
604}
605
James Bottomley9927c682008-02-03 15:48:56 -0600606static void ses_match_to_enclosure(struct enclosure_device *edev,
Li Dongyang9c0a5002017-11-14 10:48:04 +1100607 struct scsi_device *sdev,
608 int refresh)
James Bottomley9927c682008-02-03 15:48:56 -0600609{
Li Dongyang9c0a5002017-11-14 10:48:04 +1100610 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600611 struct efd efd = {
612 .addr = 0,
613 };
James Bottomley9927c682008-02-03 15:48:56 -0600614
Li Dongyang9c0a5002017-11-14 10:48:04 +1100615 if (refresh)
616 ses_enclosure_data_process(edev, edev_sdev, 0);
James Bottomley21fab1d2009-08-01 00:43:59 +0000617
Ewan D. Milne9373eba2017-01-09 16:33:36 -0500618 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
James Bottomley3f8d6f22015-12-09 12:56:07 -0800619 efd.addr = sas_get_address(sdev);
James Bottomley671a99c2008-07-29 11:38:25 -0500620
Hannes Reineckec38c0072014-03-15 09:51:51 +0100621 if (efd.addr) {
622 efd.dev = &sdev->sdev_gendev;
James Bottomley9927c682008-02-03 15:48:56 -0600623
Hannes Reineckec38c0072014-03-15 09:51:51 +0100624 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
625 }
James Bottomley9927c682008-02-03 15:48:56 -0600626}
627
Tony Jonesee959b02008-02-22 00:13:36 +0100628static int ses_intf_add(struct device *cdev,
James Bottomley9927c682008-02-03 15:48:56 -0600629 struct class_interface *intf)
630{
Tony Jonesee959b02008-02-22 00:13:36 +0100631 struct scsi_device *sdev = to_scsi_device(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600632 struct scsi_device *tmp_sdev;
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200633 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
James Bottomley9927c682008-02-03 15:48:56 -0600634 struct ses_device *ses_dev;
635 u32 result;
James Bottomley21fab1d2009-08-01 00:43:59 +0000636 int i, types, len, components = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600637 int err = -ENOMEM;
James Bottomley8c3adc72011-03-18 11:24:23 -0400638 int num_enclosures;
James Bottomley9927c682008-02-03 15:48:56 -0600639 struct enclosure_device *edev;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800640 struct ses_component *scomp = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600641
642 if (!scsi_device_enclosure(sdev)) {
643 /* not an enclosure, but might be in one */
James Bottomley163f52b2009-08-01 00:39:36 +0000644 struct enclosure_device *prev = NULL;
645
646 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
Li Dongyang9c0a5002017-11-14 10:48:04 +1100647 ses_match_to_enclosure(edev, sdev, 1);
James Bottomley163f52b2009-08-01 00:39:36 +0000648 prev = edev;
James Bottomley9927c682008-02-03 15:48:56 -0600649 }
650 return -ENODEV;
651 }
652
653 /* TYPE_ENCLOSURE prints a message in probe */
654 if (sdev->type != TYPE_ENCLOSURE)
655 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
656
657 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
658 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
659 if (!hdr_buf || !ses_dev)
660 goto err_init_free;
661
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200662 page = 1;
663 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
James Bottomley9927c682008-02-03 15:48:56 -0600664 if (result)
665 goto recv_failed;
666
James Bottomley9927c682008-02-03 15:48:56 -0600667 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
668 buf = kzalloc(len, GFP_KERNEL);
669 if (!buf)
670 goto err_free;
671
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200672 result = ses_recv_diag(sdev, page, buf, len);
James Bottomley9927c682008-02-03 15:48:56 -0600673 if (result)
674 goto recv_failed;
675
James Bottomley8c3adc72011-03-18 11:24:23 -0400676 types = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600677
James Bottomley8c3adc72011-03-18 11:24:23 -0400678 /* we always have one main enclosure and the rest are referred
679 * to as secondary subenclosures */
680 num_enclosures = buf[1] + 1;
James Bottomley9927c682008-02-03 15:48:56 -0600681
James Bottomley8c3adc72011-03-18 11:24:23 -0400682 /* begin at the enclosure descriptor */
683 type_ptr = buf + 8;
684 /* skip all the enclosure descriptors */
685 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
686 types += type_ptr[2];
687 type_ptr += type_ptr[3] + 4;
688 }
689
690 ses_dev->page1_types = type_ptr;
691 ses_dev->page1_num_types = types;
692
693 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600694 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
695 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
696 components += type_ptr[1];
697 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800698 ses_dev->page1 = buf;
699 ses_dev->page1_len = len;
700 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600701
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200702 page = 2;
703 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
James Bottomley9927c682008-02-03 15:48:56 -0600704 if (result)
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200705 goto page2_not_supported;
James Bottomley9927c682008-02-03 15:48:56 -0600706
707 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
708 buf = kzalloc(len, GFP_KERNEL);
709 if (!buf)
710 goto err_free;
711
712 /* make sure getting page 2 actually works */
713 result = ses_recv_diag(sdev, 2, buf, len);
714 if (result)
715 goto recv_failed;
716 ses_dev->page2 = buf;
717 ses_dev->page2_len = len;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800718 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600719
720 /* The additional information page --- allows us
721 * to match up the devices */
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200722 page = 10;
723 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
Yinghai Lu691b4772008-02-13 16:25:16 -0800724 if (!result) {
James Bottomley9927c682008-02-03 15:48:56 -0600725
Yinghai Lu691b4772008-02-13 16:25:16 -0800726 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
727 buf = kzalloc(len, GFP_KERNEL);
728 if (!buf)
729 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600730
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200731 result = ses_recv_diag(sdev, page, buf, len);
Yinghai Lu691b4772008-02-13 16:25:16 -0800732 if (result)
733 goto recv_failed;
734 ses_dev->page10 = buf;
735 ses_dev->page10_len = len;
736 buf = NULL;
737 }
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200738page2_not_supported:
Kees Cook6396bb22018-06-12 14:03:40 -0700739 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
James Bottomley9927c682008-02-03 15:48:56 -0600740 if (!scomp)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800741 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600742
Kay Sievers71610f52008-12-03 22:41:36 +0100743 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
James Bottomley9927c682008-02-03 15:48:56 -0600744 components, &ses_enclosure_callbacks);
745 if (IS_ERR(edev)) {
746 err = PTR_ERR(edev);
747 goto err_free;
748 }
749
Julia Lawall9b3a6542010-03-10 15:20:42 -0800750 kfree(hdr_buf);
751
James Bottomley9927c682008-02-03 15:48:56 -0600752 edev->scratch = ses_dev;
753 for (i = 0; i < components; i++)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800754 edev->component[i].scratch = scomp + i;
James Bottomley9927c682008-02-03 15:48:56 -0600755
James Bottomley21fab1d2009-08-01 00:43:59 +0000756 ses_enclosure_data_process(edev, sdev, 1);
James Bottomley9927c682008-02-03 15:48:56 -0600757
758 /* see if there are any devices matching before
759 * we found the enclosure */
760 shost_for_each_device(tmp_sdev, sdev->host) {
761 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
762 continue;
Li Dongyang9c0a5002017-11-14 10:48:04 +1100763 ses_match_to_enclosure(edev, tmp_sdev, 0);
James Bottomley9927c682008-02-03 15:48:56 -0600764 }
765
766 return 0;
767
768 recv_failed:
769 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200770 page);
James Bottomley9927c682008-02-03 15:48:56 -0600771 err = -ENODEV;
772 err_free:
773 kfree(buf);
Yinghai Lu7c46c202008-02-10 23:25:25 -0800774 kfree(scomp);
James Bottomley9927c682008-02-03 15:48:56 -0600775 kfree(ses_dev->page10);
776 kfree(ses_dev->page2);
777 kfree(ses_dev->page1);
778 err_init_free:
779 kfree(ses_dev);
780 kfree(hdr_buf);
781 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
782 return err;
783}
784
785static int ses_remove(struct device *dev)
786{
787 return 0;
788}
789
James Bottomley43d8eb92009-08-01 00:41:22 +0000790static void ses_intf_remove_component(struct scsi_device *sdev)
James Bottomley9927c682008-02-03 15:48:56 -0600791{
James Bottomley43d8eb92009-08-01 00:41:22 +0000792 struct enclosure_device *edev, *prev = NULL;
793
794 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
795 prev = edev;
796 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
797 break;
798 }
799 if (edev)
800 put_device(&edev->edev);
801}
802
803static void ses_intf_remove_enclosure(struct scsi_device *sdev)
804{
James Bottomley9927c682008-02-03 15:48:56 -0600805 struct enclosure_device *edev;
806 struct ses_device *ses_dev;
807
James Bottomley163f52b2009-08-01 00:39:36 +0000808 /* exact match to this enclosure */
James Bottomley43d8eb92009-08-01 00:41:22 +0000809 edev = enclosure_find(&sdev->sdev_gendev, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600810 if (!edev)
811 return;
812
813 ses_dev = edev->scratch;
814 edev->scratch = NULL;
815
Yinghai Lu7c46c202008-02-10 23:25:25 -0800816 kfree(ses_dev->page10);
James Bottomley9927c682008-02-03 15:48:56 -0600817 kfree(ses_dev->page1);
818 kfree(ses_dev->page2);
819 kfree(ses_dev);
820
821 kfree(edev->component[0].scratch);
822
Tony Jonesee959b02008-02-22 00:13:36 +0100823 put_device(&edev->edev);
Calvin Owensa5a039b2017-08-24 15:13:52 +0200824 enclosure_unregister(edev);
James Bottomley9927c682008-02-03 15:48:56 -0600825}
826
James Bottomley43d8eb92009-08-01 00:41:22 +0000827static void ses_intf_remove(struct device *cdev,
828 struct class_interface *intf)
829{
830 struct scsi_device *sdev = to_scsi_device(cdev->parent);
831
832 if (!scsi_device_enclosure(sdev))
833 ses_intf_remove_component(sdev);
834 else
835 ses_intf_remove_enclosure(sdev);
836}
837
James Bottomley9927c682008-02-03 15:48:56 -0600838static struct class_interface ses_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100839 .add_dev = ses_intf_add,
840 .remove_dev = ses_intf_remove,
James Bottomley9927c682008-02-03 15:48:56 -0600841};
842
843static struct scsi_driver ses_template = {
James Bottomley9927c682008-02-03 15:48:56 -0600844 .gendrv = {
845 .name = "ses",
Christoph Hellwig3af6b352014-11-12 18:34:51 +0100846 .owner = THIS_MODULE,
James Bottomley9927c682008-02-03 15:48:56 -0600847 .probe = ses_probe,
848 .remove = ses_remove,
849 },
850};
851
852static int __init ses_init(void)
853{
854 int err;
855
856 err = scsi_register_interface(&ses_interface);
857 if (err)
858 return err;
859
860 err = scsi_register_driver(&ses_template.gendrv);
861 if (err)
862 goto out_unreg;
863
864 return 0;
865
866 out_unreg:
867 scsi_unregister_interface(&ses_interface);
868 return err;
869}
870
871static void __exit ses_exit(void)
872{
873 scsi_unregister_driver(&ses_template.gendrv);
874 scsi_unregister_interface(&ses_interface);
875}
876
877module_init(ses_init);
878module_exit(ses_exit);
879
880MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
881
882MODULE_AUTHOR("James Bottomley");
883MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
884MODULE_LICENSE("GPL v2");