blob: c2afba2a5414df7115ed16a289b33fde9002487a [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;
James Bottomley9927c682008-02-03 15:48:56 -060090
James Bottomley3417c1b2015-12-08 09:00:31 -080091 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +090092 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
Brian King424f7272017-08-01 13:45:36 -050093 if (unlikely(ret))
James Bottomley3417c1b2015-12-08 09:00:31 -080094 return ret;
95
96 recv_page_code = ((unsigned char *)buf)[0];
97
98 if (likely(recv_page_code == page_code))
99 return ret;
100
101 /* successful diagnostic but wrong page code. This happens to some
102 * USB devices, just print a message and pretend there was an error */
103
104 sdev_printk(KERN_ERR, sdev,
105 "Wrong diagnostic page; asked for %d got %u\n",
106 page_code, recv_page_code);
107
108 return -EINVAL;
James Bottomley9927c682008-02-03 15:48:56 -0600109}
110
111static int ses_send_diag(struct scsi_device *sdev, int page_code,
112 void *buf, int bufflen)
113{
114 u32 result;
115
Yinghai Lu691b4772008-02-13 16:25:16 -0800116 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -0600117 SEND_DIAGNOSTIC,
118 0x10, /* Set PF bit */
119 0,
120 bufflen >> 8,
121 bufflen & 0xff,
122 0
123 };
124
125 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900126 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600127 if (result)
128 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
129 result);
130 return result;
131}
132
133static int ses_set_page2_descriptor(struct enclosure_device *edev,
134 struct enclosure_component *ecomp,
Yinghai Lu691b4772008-02-13 16:25:16 -0800135 unsigned char *desc)
James Bottomley9927c682008-02-03 15:48:56 -0600136{
137 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100138 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600139 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400140 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800141 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600142
143 /* Clear everything */
144 memset(desc_ptr, 0, ses_dev->page2_len - 8);
James Bottomley8c3adc72011-03-18 11:24:23 -0400145 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600146 for (j = 0; j < type_ptr[1]; j++) {
147 desc_ptr += 4;
148 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
149 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
150 continue;
151 if (count++ == descriptor) {
152 memcpy(desc_ptr, desc, 4);
153 /* set select */
154 desc_ptr[0] |= 0x80;
155 /* clear reserved, just in case */
156 desc_ptr[0] &= 0xf0;
157 }
158 }
159 }
160
161 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
162}
163
Yinghai Lu691b4772008-02-13 16:25:16 -0800164static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
James Bottomley9927c682008-02-03 15:48:56 -0600165 struct enclosure_component *ecomp)
166{
167 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100168 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600169 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400170 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800171 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600172
Hannes Reineckeacf8ab92017-08-15 10:21:41 +0200173 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0)
174 return NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600175
James Bottomley8c3adc72011-03-18 11:24:23 -0400176 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600177 for (j = 0; j < type_ptr[1]; j++) {
178 desc_ptr += 4;
179 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
180 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
181 continue;
182 if (count++ == descriptor)
183 return desc_ptr;
184 }
185 }
186 return NULL;
187}
188
Douglas Gilbert2a350ca2011-06-09 00:27:07 -0400189/* For device slot and array device slot elements, byte 3 bit 6
190 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
191 * code stands these bits are shifted 4 positions right so in
192 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
James Bottomley9927c682008-02-03 15:48:56 -0600193static void ses_get_fault(struct enclosure_device *edev,
194 struct enclosure_component *ecomp)
195{
Yinghai Lu691b4772008-02-13 16:25:16 -0800196 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600197
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200198 if (!ses_page2_supported(edev)) {
199 ecomp->fault = 0;
200 return;
201 }
James Bottomley9927c682008-02-03 15:48:56 -0600202 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800203 if (desc)
204 ecomp->fault = (desc[3] & 0x60) >> 4;
James Bottomley9927c682008-02-03 15:48:56 -0600205}
206
207static int ses_set_fault(struct enclosure_device *edev,
208 struct enclosure_component *ecomp,
209 enum enclosure_component_setting val)
210{
Song Liu08024882014-12-30 14:46:18 -0800211 unsigned char desc[4];
212 unsigned char *desc_ptr;
213
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200214 if (!ses_page2_supported(edev))
215 return -EINVAL;
216
Song Liu08024882014-12-30 14:46:18 -0800217 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
218
219 if (!desc_ptr)
220 return -EIO;
221
222 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600223
224 switch (val) {
225 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800226 desc[3] &= 0xdf;
James Bottomley9927c682008-02-03 15:48:56 -0600227 break;
228 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800229 desc[3] |= 0x20;
James Bottomley9927c682008-02-03 15:48:56 -0600230 break;
231 default:
232 /* SES doesn't do the SGPIO blink settings */
233 return -EINVAL;
234 }
235
236 return ses_set_page2_descriptor(edev, ecomp, desc);
237}
238
239static void ses_get_status(struct enclosure_device *edev,
240 struct enclosure_component *ecomp)
241{
Yinghai Lu691b4772008-02-13 16:25:16 -0800242 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600243
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200244 if (!ses_page2_supported(edev)) {
245 ecomp->status = 0;
246 return;
247 }
James Bottomley9927c682008-02-03 15:48:56 -0600248 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800249 if (desc)
250 ecomp->status = (desc[0] & 0x0f);
James Bottomley9927c682008-02-03 15:48:56 -0600251}
252
253static void ses_get_locate(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->locate = 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->locate = (desc[2] & 0x02) ? 1 : 0;
James Bottomley9927c682008-02-03 15:48:56 -0600265}
266
267static int ses_set_locate(struct enclosure_device *edev,
268 struct enclosure_component *ecomp,
269 enum enclosure_component_setting val)
270{
Song Liu08024882014-12-30 14:46:18 -0800271 unsigned char desc[4];
272 unsigned char *desc_ptr;
273
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200274 if (!ses_page2_supported(edev))
275 return -EINVAL;
276
Song Liu08024882014-12-30 14:46:18 -0800277 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
278
279 if (!desc_ptr)
280 return -EIO;
281
282 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600283
284 switch (val) {
285 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800286 desc[2] &= 0xfd;
James Bottomley9927c682008-02-03 15:48:56 -0600287 break;
288 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800289 desc[2] |= 0x02;
James Bottomley9927c682008-02-03 15:48:56 -0600290 break;
291 default:
292 /* SES doesn't do the SGPIO blink settings */
293 return -EINVAL;
294 }
295 return ses_set_page2_descriptor(edev, ecomp, desc);
296}
297
298static int ses_set_active(struct enclosure_device *edev,
299 struct enclosure_component *ecomp,
300 enum enclosure_component_setting val)
301{
Song Liu08024882014-12-30 14:46:18 -0800302 unsigned char desc[4];
303 unsigned char *desc_ptr;
304
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200305 if (!ses_page2_supported(edev))
306 return -EINVAL;
307
Song Liu08024882014-12-30 14:46:18 -0800308 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
309
310 if (!desc_ptr)
311 return -EIO;
312
313 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600314
315 switch (val) {
316 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800317 desc[2] &= 0x7f;
James Bottomley9927c682008-02-03 15:48:56 -0600318 ecomp->active = 0;
319 break;
320 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800321 desc[2] |= 0x80;
James Bottomley9927c682008-02-03 15:48:56 -0600322 ecomp->active = 1;
323 break;
324 default:
325 /* SES doesn't do the SGPIO blink settings */
326 return -EINVAL;
327 }
328 return ses_set_page2_descriptor(edev, ecomp, desc);
329}
330
Dan Williams967f7ba2014-12-30 14:46:16 -0800331static int ses_show_id(struct enclosure_device *edev, char *buf)
332{
333 struct ses_device *ses_dev = edev->scratch;
334 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
335
336 return sprintf(buf, "%#llx\n", id);
337}
338
Song Liu08024882014-12-30 14:46:18 -0800339static void ses_get_power_status(struct enclosure_device *edev,
340 struct enclosure_component *ecomp)
341{
342 unsigned char *desc;
343
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200344 if (!ses_page2_supported(edev)) {
345 ecomp->power_status = 0;
346 return;
347 }
348
Song Liu08024882014-12-30 14:46:18 -0800349 desc = ses_get_page2_descriptor(edev, ecomp);
350 if (desc)
351 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
352}
353
354static int ses_set_power_status(struct enclosure_device *edev,
355 struct enclosure_component *ecomp,
356 int val)
357{
358 unsigned char desc[4];
359 unsigned char *desc_ptr;
360
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200361 if (!ses_page2_supported(edev))
362 return -EINVAL;
363
Song Liu08024882014-12-30 14:46:18 -0800364 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
365
366 if (!desc_ptr)
367 return -EIO;
368
369 init_device_slot_control(desc, ecomp, desc_ptr);
370
371 switch (val) {
372 /* power = 1 is device_off = 0 and vice versa */
373 case 0:
374 desc[3] |= 0x10;
375 break;
376 case 1:
377 desc[3] &= 0xef;
378 break;
379 default:
380 return -EINVAL;
381 }
382 ecomp->power_status = val;
383 return ses_set_page2_descriptor(edev, ecomp, desc);
384}
385
James Bottomley9927c682008-02-03 15:48:56 -0600386static struct enclosure_component_callbacks ses_enclosure_callbacks = {
387 .get_fault = ses_get_fault,
388 .set_fault = ses_set_fault,
389 .get_status = ses_get_status,
390 .get_locate = ses_get_locate,
391 .set_locate = ses_set_locate,
Song Liu08024882014-12-30 14:46:18 -0800392 .get_power_status = ses_get_power_status,
393 .set_power_status = ses_set_power_status,
James Bottomley9927c682008-02-03 15:48:56 -0600394 .set_active = ses_set_active,
Dan Williams967f7ba2014-12-30 14:46:16 -0800395 .show_id = ses_show_id,
James Bottomley9927c682008-02-03 15:48:56 -0600396};
397
398struct ses_host_edev {
399 struct Scsi_Host *shost;
400 struct enclosure_device *edev;
401};
402
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800403#if 0
James Bottomley9927c682008-02-03 15:48:56 -0600404int ses_match_host(struct enclosure_device *edev, void *data)
405{
406 struct ses_host_edev *sed = data;
407 struct scsi_device *sdev;
408
Tony Jonesee959b02008-02-22 00:13:36 +0100409 if (!scsi_is_sdev_device(edev->edev.parent))
James Bottomley9927c682008-02-03 15:48:56 -0600410 return 0;
411
Tony Jonesee959b02008-02-22 00:13:36 +0100412 sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600413
414 if (sdev->host != sed->shost)
415 return 0;
416
417 sed->edev = edev;
418 return 1;
419}
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800420#endif /* 0 */
James Bottomley9927c682008-02-03 15:48:56 -0600421
422static void ses_process_descriptor(struct enclosure_component *ecomp,
423 unsigned char *desc)
424{
425 int eip = desc[0] & 0x10;
426 int invalid = desc[0] & 0x80;
427 enum scsi_protocol proto = desc[0] & 0x0f;
428 u64 addr = 0;
Dan Williams921ce7f2014-12-30 14:46:17 -0800429 int slot = -1;
James Bottomley9927c682008-02-03 15:48:56 -0600430 struct ses_component *scomp = ecomp->scratch;
431 unsigned char *d;
432
James Bottomley9927c682008-02-03 15:48:56 -0600433 if (invalid)
434 return;
435
436 switch (proto) {
Dan Williams921ce7f2014-12-30 14:46:17 -0800437 case SCSI_PROTOCOL_FCP:
438 if (eip) {
439 d = desc + 4;
440 slot = d[3];
441 }
442 break;
James Bottomley9927c682008-02-03 15:48:56 -0600443 case SCSI_PROTOCOL_SAS:
Dan Williams921ce7f2014-12-30 14:46:17 -0800444 if (eip) {
445 d = desc + 4;
446 slot = d[3];
James Bottomley9927c682008-02-03 15:48:56 -0600447 d = desc + 8;
Dan Williams921ce7f2014-12-30 14:46:17 -0800448 } else
James Bottomley9927c682008-02-03 15:48:56 -0600449 d = desc + 4;
450 /* only take the phy0 addr */
451 addr = (u64)d[12] << 56 |
452 (u64)d[13] << 48 |
453 (u64)d[14] << 40 |
454 (u64)d[15] << 32 |
455 (u64)d[16] << 24 |
456 (u64)d[17] << 16 |
457 (u64)d[18] << 8 |
458 (u64)d[19];
459 break;
460 default:
461 /* FIXME: Need to add more protocols than just SAS */
462 break;
463 }
Dan Williams921ce7f2014-12-30 14:46:17 -0800464 ecomp->slot = slot;
James Bottomley9927c682008-02-03 15:48:56 -0600465 scomp->addr = addr;
466}
467
468struct efd {
469 u64 addr;
470 struct device *dev;
471};
472
473static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
474 void *data)
475{
476 struct efd *efd = data;
477 int i;
478 struct ses_component *scomp;
479
480 if (!edev->component[0].scratch)
481 return 0;
482
483 for (i = 0; i < edev->components; i++) {
484 scomp = edev->component[i].scratch;
485 if (scomp->addr != efd->addr)
486 continue;
487
Dan Williams15a0fbb2014-12-30 14:46:15 -0800488 if (enclosure_add_device(edev, i, efd->dev) == 0)
489 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
James Bottomley9927c682008-02-03 15:48:56 -0600490 return 1;
491 }
492 return 0;
493}
494
James Bottomley21fab1d2009-08-01 00:43:59 +0000495#define INIT_ALLOC_SIZE 32
496
497static void ses_enclosure_data_process(struct enclosure_device *edev,
498 struct scsi_device *sdev,
499 int create)
500{
501 u32 result;
502 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
503 int i, j, page7_len, len, components;
504 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400505 int types = ses_dev->page1_num_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000506 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
507
508 if (!hdr_buf)
509 goto simple_populate;
510
511 /* re-read page 10 */
512 if (ses_dev->page10)
513 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
514 /* Page 7 for the descriptors is optional */
515 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
516 if (result)
517 goto simple_populate;
518
519 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
520 /* add 1 for trailing '\0' we'll use */
521 buf = kzalloc(len + 1, GFP_KERNEL);
522 if (!buf)
523 goto simple_populate;
524 result = ses_recv_diag(sdev, 7, buf, len);
525 if (result) {
526 simple_populate:
527 kfree(buf);
528 buf = NULL;
529 desc_ptr = NULL;
530 len = 0;
531 page7_len = 0;
532 } else {
533 desc_ptr = buf + 8;
534 len = (desc_ptr[2] << 8) + desc_ptr[3];
535 /* skip past overall descriptor */
536 desc_ptr += len + 4;
James Bottomley21fab1d2009-08-01 00:43:59 +0000537 }
John Hughes877a5592009-11-04 19:01:22 +0100538 if (ses_dev->page10)
539 addl_desc_ptr = ses_dev->page10 + 8;
James Bottomley8c3adc72011-03-18 11:24:23 -0400540 type_ptr = ses_dev->page1_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000541 components = 0;
542 for (i = 0; i < types; i++, type_ptr += 4) {
543 for (j = 0; j < type_ptr[1]; j++) {
544 char *name = NULL;
545 struct enclosure_component *ecomp;
546
547 if (desc_ptr) {
548 if (desc_ptr >= buf + page7_len) {
549 desc_ptr = NULL;
550 } else {
551 len = (desc_ptr[2] << 8) + desc_ptr[3];
552 desc_ptr += 4;
553 /* Add trailing zero - pushes into
554 * reserved space */
555 desc_ptr[len] = '\0';
556 name = desc_ptr;
557 }
558 }
559 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
560 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
561
562 if (create)
Dan Williamsed09dcc2014-12-30 14:46:14 -0800563 ecomp = enclosure_component_alloc(
564 edev,
565 components++,
566 type_ptr[0],
567 name);
James Bottomley21fab1d2009-08-01 00:43:59 +0000568 else
569 ecomp = &edev->component[components++];
570
Dan Williamsed09dcc2014-12-30 14:46:14 -0800571 if (!IS_ERR(ecomp)) {
572 if (addl_desc_ptr)
573 ses_process_descriptor(
574 ecomp,
575 addl_desc_ptr);
576 if (create)
577 enclosure_component_register(
578 ecomp);
579 }
James Bottomley21fab1d2009-08-01 00:43:59 +0000580 }
581 if (desc_ptr)
582 desc_ptr += len;
583
James Bottomley5e103352015-12-11 09:16:38 -0800584 if (addl_desc_ptr &&
585 /* only find additional descriptions for specific devices */
586 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
587 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
588 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
589 /* these elements are optional */
590 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
591 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
592 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
James Bottomley21fab1d2009-08-01 00:43:59 +0000593 addl_desc_ptr += addl_desc_ptr[1] + 2;
594
595 }
596 }
597 kfree(buf);
598 kfree(hdr_buf);
599}
600
James Bottomley9927c682008-02-03 15:48:56 -0600601static void ses_match_to_enclosure(struct enclosure_device *edev,
Li Dongyang9c0a5002017-11-14 10:48:04 +1100602 struct scsi_device *sdev,
603 int refresh)
James Bottomley9927c682008-02-03 15:48:56 -0600604{
Li Dongyang9c0a5002017-11-14 10:48:04 +1100605 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600606 struct efd efd = {
607 .addr = 0,
608 };
James Bottomley9927c682008-02-03 15:48:56 -0600609
Li Dongyang9c0a5002017-11-14 10:48:04 +1100610 if (refresh)
611 ses_enclosure_data_process(edev, edev_sdev, 0);
James Bottomley21fab1d2009-08-01 00:43:59 +0000612
Ewan D. Milne9373eba2017-01-09 16:33:36 -0500613 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
James Bottomley3f8d6f22015-12-09 12:56:07 -0800614 efd.addr = sas_get_address(sdev);
James Bottomley671a99c2008-07-29 11:38:25 -0500615
Hannes Reineckec38c0072014-03-15 09:51:51 +0100616 if (efd.addr) {
617 efd.dev = &sdev->sdev_gendev;
James Bottomley9927c682008-02-03 15:48:56 -0600618
Hannes Reineckec38c0072014-03-15 09:51:51 +0100619 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
620 }
James Bottomley9927c682008-02-03 15:48:56 -0600621}
622
Tony Jonesee959b02008-02-22 00:13:36 +0100623static int ses_intf_add(struct device *cdev,
James Bottomley9927c682008-02-03 15:48:56 -0600624 struct class_interface *intf)
625{
Tony Jonesee959b02008-02-22 00:13:36 +0100626 struct scsi_device *sdev = to_scsi_device(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600627 struct scsi_device *tmp_sdev;
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200628 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
James Bottomley9927c682008-02-03 15:48:56 -0600629 struct ses_device *ses_dev;
630 u32 result;
James Bottomley21fab1d2009-08-01 00:43:59 +0000631 int i, types, len, components = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600632 int err = -ENOMEM;
James Bottomley8c3adc72011-03-18 11:24:23 -0400633 int num_enclosures;
James Bottomley9927c682008-02-03 15:48:56 -0600634 struct enclosure_device *edev;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800635 struct ses_component *scomp = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600636
637 if (!scsi_device_enclosure(sdev)) {
638 /* not an enclosure, but might be in one */
James Bottomley163f52b2009-08-01 00:39:36 +0000639 struct enclosure_device *prev = NULL;
640
641 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
Li Dongyang9c0a5002017-11-14 10:48:04 +1100642 ses_match_to_enclosure(edev, sdev, 1);
James Bottomley163f52b2009-08-01 00:39:36 +0000643 prev = edev;
James Bottomley9927c682008-02-03 15:48:56 -0600644 }
645 return -ENODEV;
646 }
647
648 /* TYPE_ENCLOSURE prints a message in probe */
649 if (sdev->type != TYPE_ENCLOSURE)
650 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
651
652 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
653 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
654 if (!hdr_buf || !ses_dev)
655 goto err_init_free;
656
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200657 page = 1;
658 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
James Bottomley9927c682008-02-03 15:48:56 -0600659 if (result)
660 goto recv_failed;
661
James Bottomley9927c682008-02-03 15:48:56 -0600662 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
663 buf = kzalloc(len, GFP_KERNEL);
664 if (!buf)
665 goto err_free;
666
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200667 result = ses_recv_diag(sdev, page, buf, len);
James Bottomley9927c682008-02-03 15:48:56 -0600668 if (result)
669 goto recv_failed;
670
James Bottomley8c3adc72011-03-18 11:24:23 -0400671 types = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600672
James Bottomley8c3adc72011-03-18 11:24:23 -0400673 /* we always have one main enclosure and the rest are referred
674 * to as secondary subenclosures */
675 num_enclosures = buf[1] + 1;
James Bottomley9927c682008-02-03 15:48:56 -0600676
James Bottomley8c3adc72011-03-18 11:24:23 -0400677 /* begin at the enclosure descriptor */
678 type_ptr = buf + 8;
679 /* skip all the enclosure descriptors */
680 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
681 types += type_ptr[2];
682 type_ptr += type_ptr[3] + 4;
683 }
684
685 ses_dev->page1_types = type_ptr;
686 ses_dev->page1_num_types = types;
687
688 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600689 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
690 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
691 components += type_ptr[1];
692 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800693 ses_dev->page1 = buf;
694 ses_dev->page1_len = len;
695 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600696
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200697 page = 2;
698 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
James Bottomley9927c682008-02-03 15:48:56 -0600699 if (result)
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200700 goto page2_not_supported;
James Bottomley9927c682008-02-03 15:48:56 -0600701
702 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
703 buf = kzalloc(len, GFP_KERNEL);
704 if (!buf)
705 goto err_free;
706
707 /* make sure getting page 2 actually works */
708 result = ses_recv_diag(sdev, 2, buf, len);
709 if (result)
710 goto recv_failed;
711 ses_dev->page2 = buf;
712 ses_dev->page2_len = len;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800713 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600714
715 /* The additional information page --- allows us
716 * to match up the devices */
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200717 page = 10;
718 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
Yinghai Lu691b4772008-02-13 16:25:16 -0800719 if (!result) {
James Bottomley9927c682008-02-03 15:48:56 -0600720
Yinghai Lu691b4772008-02-13 16:25:16 -0800721 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
722 buf = kzalloc(len, GFP_KERNEL);
723 if (!buf)
724 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600725
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200726 result = ses_recv_diag(sdev, page, buf, len);
Yinghai Lu691b4772008-02-13 16:25:16 -0800727 if (result)
728 goto recv_failed;
729 ses_dev->page10 = buf;
730 ses_dev->page10_len = len;
731 buf = NULL;
732 }
Hannes Reineckedc56ce12017-08-15 10:21:43 +0200733page2_not_supported:
Kees Cook6396bb22018-06-12 14:03:40 -0700734 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
James Bottomley9927c682008-02-03 15:48:56 -0600735 if (!scomp)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800736 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600737
Kay Sievers71610f52008-12-03 22:41:36 +0100738 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
James Bottomley9927c682008-02-03 15:48:56 -0600739 components, &ses_enclosure_callbacks);
740 if (IS_ERR(edev)) {
741 err = PTR_ERR(edev);
742 goto err_free;
743 }
744
Julia Lawall9b3a6542010-03-10 15:20:42 -0800745 kfree(hdr_buf);
746
James Bottomley9927c682008-02-03 15:48:56 -0600747 edev->scratch = ses_dev;
748 for (i = 0; i < components; i++)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800749 edev->component[i].scratch = scomp + i;
James Bottomley9927c682008-02-03 15:48:56 -0600750
James Bottomley21fab1d2009-08-01 00:43:59 +0000751 ses_enclosure_data_process(edev, sdev, 1);
James Bottomley9927c682008-02-03 15:48:56 -0600752
753 /* see if there are any devices matching before
754 * we found the enclosure */
755 shost_for_each_device(tmp_sdev, sdev->host) {
756 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
757 continue;
Li Dongyang9c0a5002017-11-14 10:48:04 +1100758 ses_match_to_enclosure(edev, tmp_sdev, 0);
James Bottomley9927c682008-02-03 15:48:56 -0600759 }
760
761 return 0;
762
763 recv_failed:
764 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
Hannes Reinecke81b59d72017-08-15 10:21:42 +0200765 page);
James Bottomley9927c682008-02-03 15:48:56 -0600766 err = -ENODEV;
767 err_free:
768 kfree(buf);
Yinghai Lu7c46c202008-02-10 23:25:25 -0800769 kfree(scomp);
James Bottomley9927c682008-02-03 15:48:56 -0600770 kfree(ses_dev->page10);
771 kfree(ses_dev->page2);
772 kfree(ses_dev->page1);
773 err_init_free:
774 kfree(ses_dev);
775 kfree(hdr_buf);
776 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
777 return err;
778}
779
780static int ses_remove(struct device *dev)
781{
782 return 0;
783}
784
James Bottomley43d8eb92009-08-01 00:41:22 +0000785static void ses_intf_remove_component(struct scsi_device *sdev)
James Bottomley9927c682008-02-03 15:48:56 -0600786{
James Bottomley43d8eb92009-08-01 00:41:22 +0000787 struct enclosure_device *edev, *prev = NULL;
788
789 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
790 prev = edev;
791 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
792 break;
793 }
794 if (edev)
795 put_device(&edev->edev);
796}
797
798static void ses_intf_remove_enclosure(struct scsi_device *sdev)
799{
James Bottomley9927c682008-02-03 15:48:56 -0600800 struct enclosure_device *edev;
801 struct ses_device *ses_dev;
802
James Bottomley163f52b2009-08-01 00:39:36 +0000803 /* exact match to this enclosure */
James Bottomley43d8eb92009-08-01 00:41:22 +0000804 edev = enclosure_find(&sdev->sdev_gendev, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600805 if (!edev)
806 return;
807
808 ses_dev = edev->scratch;
809 edev->scratch = NULL;
810
Yinghai Lu7c46c202008-02-10 23:25:25 -0800811 kfree(ses_dev->page10);
James Bottomley9927c682008-02-03 15:48:56 -0600812 kfree(ses_dev->page1);
813 kfree(ses_dev->page2);
814 kfree(ses_dev);
815
816 kfree(edev->component[0].scratch);
817
Tony Jonesee959b02008-02-22 00:13:36 +0100818 put_device(&edev->edev);
Calvin Owensa5a039b2017-08-24 15:13:52 +0200819 enclosure_unregister(edev);
James Bottomley9927c682008-02-03 15:48:56 -0600820}
821
James Bottomley43d8eb92009-08-01 00:41:22 +0000822static void ses_intf_remove(struct device *cdev,
823 struct class_interface *intf)
824{
825 struct scsi_device *sdev = to_scsi_device(cdev->parent);
826
827 if (!scsi_device_enclosure(sdev))
828 ses_intf_remove_component(sdev);
829 else
830 ses_intf_remove_enclosure(sdev);
831}
832
James Bottomley9927c682008-02-03 15:48:56 -0600833static struct class_interface ses_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100834 .add_dev = ses_intf_add,
835 .remove_dev = ses_intf_remove,
James Bottomley9927c682008-02-03 15:48:56 -0600836};
837
838static struct scsi_driver ses_template = {
James Bottomley9927c682008-02-03 15:48:56 -0600839 .gendrv = {
840 .name = "ses",
Christoph Hellwig3af6b352014-11-12 18:34:51 +0100841 .owner = THIS_MODULE,
James Bottomley9927c682008-02-03 15:48:56 -0600842 .probe = ses_probe,
843 .remove = ses_remove,
844 },
845};
846
847static int __init ses_init(void)
848{
849 int err;
850
851 err = scsi_register_interface(&ses_interface);
852 if (err)
853 return err;
854
855 err = scsi_register_driver(&ses_template.gendrv);
856 if (err)
857 goto out_unreg;
858
859 return 0;
860
861 out_unreg:
862 scsi_unregister_interface(&ses_interface);
863 return err;
864}
865
866static void __exit ses_exit(void)
867{
868 scsi_unregister_driver(&ses_template.gendrv);
869 scsi_unregister_interface(&ses_interface);
870}
871
872module_init(ses_init);
873module_exit(ses_exit);
874
875MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
876
877MODULE_AUTHOR("James Bottomley");
878MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
879MODULE_LICENSE("GPL v2");