blob: 2a651b2063a2332ac6281b30ac90594d4895a627 [file] [log] [blame]
James Bottomley9927c682008-02-03 15:48:56 -06001/*
2 * SCSI Enclosure Services
3 *
4 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
5 *
6**-----------------------------------------------------------------------------
7**
8** This program is free software; you can redistribute it and/or
9** modify it under the terms of the GNU General Public License
10** version 2 as published by the Free Software Foundation.
11**
12** This program is distributed in the hope that it will be useful,
13** but WITHOUT ANY WARRANTY; without even the implied warranty of
14** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15** GNU General Public License for more details.
16**
17** You should have received a copy of the GNU General Public License
18** along with this program; if not, write to the Free Software
19** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20**
21**-----------------------------------------------------------------------------
22*/
23
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
James Bottomley9927c682008-02-03 15:48:56 -060025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/enclosure.h>
Hannes Reineckec38c0072014-03-15 09:51:51 +010028#include <asm/unaligned.h>
James Bottomley9927c682008-02-03 15:48:56 -060029
30#include <scsi/scsi.h>
31#include <scsi/scsi_cmnd.h>
32#include <scsi/scsi_dbg.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_driver.h>
35#include <scsi/scsi_host.h>
36
James Bottomley3f8d6f22015-12-09 12:56:07 -080037#include <scsi/scsi_transport_sas.h>
38
James Bottomley9927c682008-02-03 15:48:56 -060039struct ses_device {
Yinghai Lu691b4772008-02-13 16:25:16 -080040 unsigned char *page1;
James Bottomley8c3adc72011-03-18 11:24:23 -040041 unsigned char *page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -080042 unsigned char *page2;
43 unsigned char *page10;
James Bottomley9927c682008-02-03 15:48:56 -060044 short page1_len;
James Bottomley8c3adc72011-03-18 11:24:23 -040045 short page1_num_types;
James Bottomley9927c682008-02-03 15:48:56 -060046 short page2_len;
47 short page10_len;
48};
49
50struct ses_component {
51 u64 addr;
James Bottomley9927c682008-02-03 15:48:56 -060052};
53
54static int ses_probe(struct device *dev)
55{
56 struct scsi_device *sdev = to_scsi_device(dev);
57 int err = -ENODEV;
58
59 if (sdev->type != TYPE_ENCLOSURE)
60 goto out;
61
62 err = 0;
63 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
64
65 out:
66 return err;
67}
68
Matthew Wilcoxc95e62c2008-06-23 09:14:31 -060069#define SES_TIMEOUT (30 * HZ)
James Bottomley9927c682008-02-03 15:48:56 -060070#define SES_RETRIES 3
71
Song Liu08024882014-12-30 14:46:18 -080072static void init_device_slot_control(unsigned char *dest_desc,
73 struct enclosure_component *ecomp,
74 unsigned char *status)
75{
76 memcpy(dest_desc, status, 4);
77 dest_desc[0] = 0;
78 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
79 if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
80 dest_desc[1] = 0;
81 dest_desc[2] &= 0xde;
82 dest_desc[3] &= 0x3c;
83}
84
85
James Bottomley9927c682008-02-03 15:48:56 -060086static int ses_recv_diag(struct scsi_device *sdev, int page_code,
87 void *buf, int bufflen)
88{
James Bottomley3417c1b2015-12-08 09:00:31 -080089 int ret;
Yinghai Lu691b4772008-02-13 16:25:16 -080090 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060091 RECEIVE_DIAGNOSTIC,
92 1, /* Set PCV bit */
93 page_code,
94 bufflen >> 8,
95 bufflen & 0xff,
96 0
97 };
James Bottomley3417c1b2015-12-08 09:00:31 -080098 unsigned char recv_page_code;
James Bottomley9927c682008-02-03 15:48:56 -060099
James Bottomley3417c1b2015-12-08 09:00:31 -0800100 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900101 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley3417c1b2015-12-08 09:00:31 -0800102 if (unlikely(!ret))
103 return ret;
104
105 recv_page_code = ((unsigned char *)buf)[0];
106
107 if (likely(recv_page_code == page_code))
108 return ret;
109
110 /* successful diagnostic but wrong page code. This happens to some
111 * USB devices, just print a message and pretend there was an error */
112
113 sdev_printk(KERN_ERR, sdev,
114 "Wrong diagnostic page; asked for %d got %u\n",
115 page_code, recv_page_code);
116
117 return -EINVAL;
James Bottomley9927c682008-02-03 15:48:56 -0600118}
119
120static int ses_send_diag(struct scsi_device *sdev, int page_code,
121 void *buf, int bufflen)
122{
123 u32 result;
124
Yinghai Lu691b4772008-02-13 16:25:16 -0800125 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -0600126 SEND_DIAGNOSTIC,
127 0x10, /* Set PF bit */
128 0,
129 bufflen >> 8,
130 bufflen & 0xff,
131 0
132 };
133
134 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900135 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600136 if (result)
137 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
138 result);
139 return result;
140}
141
142static int ses_set_page2_descriptor(struct enclosure_device *edev,
143 struct enclosure_component *ecomp,
Yinghai Lu691b4772008-02-13 16:25:16 -0800144 unsigned char *desc)
James Bottomley9927c682008-02-03 15:48:56 -0600145{
146 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100147 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600148 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400149 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800150 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600151
152 /* Clear everything */
153 memset(desc_ptr, 0, ses_dev->page2_len - 8);
James Bottomley8c3adc72011-03-18 11:24:23 -0400154 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600155 for (j = 0; j < type_ptr[1]; j++) {
156 desc_ptr += 4;
157 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
158 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
159 continue;
160 if (count++ == descriptor) {
161 memcpy(desc_ptr, desc, 4);
162 /* set select */
163 desc_ptr[0] |= 0x80;
164 /* clear reserved, just in case */
165 desc_ptr[0] &= 0xf0;
166 }
167 }
168 }
169
170 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
171}
172
Yinghai Lu691b4772008-02-13 16:25:16 -0800173static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
James Bottomley9927c682008-02-03 15:48:56 -0600174 struct enclosure_component *ecomp)
175{
176 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100177 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600178 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400179 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800180 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600181
Hannes Reineckeacf8ab92017-08-15 10:21:41 +0200182 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0)
183 return NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600184
James Bottomley8c3adc72011-03-18 11:24:23 -0400185 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600186 for (j = 0; j < type_ptr[1]; j++) {
187 desc_ptr += 4;
188 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
189 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
190 continue;
191 if (count++ == descriptor)
192 return desc_ptr;
193 }
194 }
195 return NULL;
196}
197
Douglas Gilbert2a350ca2011-06-09 00:27:07 -0400198/* For device slot and array device slot elements, byte 3 bit 6
199 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
200 * code stands these bits are shifted 4 positions right so in
201 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
James Bottomley9927c682008-02-03 15:48:56 -0600202static void ses_get_fault(struct enclosure_device *edev,
203 struct enclosure_component *ecomp)
204{
Yinghai Lu691b4772008-02-13 16:25:16 -0800205 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600206
207 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
219 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
220
221 if (!desc_ptr)
222 return -EIO;
223
224 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600225
226 switch (val) {
227 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800228 desc[3] &= 0xdf;
James Bottomley9927c682008-02-03 15:48:56 -0600229 break;
230 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800231 desc[3] |= 0x20;
James Bottomley9927c682008-02-03 15:48:56 -0600232 break;
233 default:
234 /* SES doesn't do the SGPIO blink settings */
235 return -EINVAL;
236 }
237
238 return ses_set_page2_descriptor(edev, ecomp, desc);
239}
240
241static void ses_get_status(struct enclosure_device *edev,
242 struct enclosure_component *ecomp)
243{
Yinghai Lu691b4772008-02-13 16:25:16 -0800244 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600245
246 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800247 if (desc)
248 ecomp->status = (desc[0] & 0x0f);
James Bottomley9927c682008-02-03 15:48:56 -0600249}
250
251static void ses_get_locate(struct enclosure_device *edev,
252 struct enclosure_component *ecomp)
253{
Yinghai Lu691b4772008-02-13 16:25:16 -0800254 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600255
256 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800257 if (desc)
258 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
James Bottomley9927c682008-02-03 15:48:56 -0600259}
260
261static int ses_set_locate(struct enclosure_device *edev,
262 struct enclosure_component *ecomp,
263 enum enclosure_component_setting val)
264{
Song Liu08024882014-12-30 14:46:18 -0800265 unsigned char desc[4];
266 unsigned char *desc_ptr;
267
268 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
269
270 if (!desc_ptr)
271 return -EIO;
272
273 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600274
275 switch (val) {
276 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800277 desc[2] &= 0xfd;
James Bottomley9927c682008-02-03 15:48:56 -0600278 break;
279 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800280 desc[2] |= 0x02;
James Bottomley9927c682008-02-03 15:48:56 -0600281 break;
282 default:
283 /* SES doesn't do the SGPIO blink settings */
284 return -EINVAL;
285 }
286 return ses_set_page2_descriptor(edev, ecomp, desc);
287}
288
289static int ses_set_active(struct enclosure_device *edev,
290 struct enclosure_component *ecomp,
291 enum enclosure_component_setting val)
292{
Song Liu08024882014-12-30 14:46:18 -0800293 unsigned char desc[4];
294 unsigned char *desc_ptr;
295
296 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
297
298 if (!desc_ptr)
299 return -EIO;
300
301 init_device_slot_control(desc, ecomp, desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600302
303 switch (val) {
304 case ENCLOSURE_SETTING_DISABLED:
Song Liu08024882014-12-30 14:46:18 -0800305 desc[2] &= 0x7f;
James Bottomley9927c682008-02-03 15:48:56 -0600306 ecomp->active = 0;
307 break;
308 case ENCLOSURE_SETTING_ENABLED:
Song Liu08024882014-12-30 14:46:18 -0800309 desc[2] |= 0x80;
James Bottomley9927c682008-02-03 15:48:56 -0600310 ecomp->active = 1;
311 break;
312 default:
313 /* SES doesn't do the SGPIO blink settings */
314 return -EINVAL;
315 }
316 return ses_set_page2_descriptor(edev, ecomp, desc);
317}
318
Dan Williams967f7ba2014-12-30 14:46:16 -0800319static int ses_show_id(struct enclosure_device *edev, char *buf)
320{
321 struct ses_device *ses_dev = edev->scratch;
322 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
323
324 return sprintf(buf, "%#llx\n", id);
325}
326
Song Liu08024882014-12-30 14:46:18 -0800327static void ses_get_power_status(struct enclosure_device *edev,
328 struct enclosure_component *ecomp)
329{
330 unsigned char *desc;
331
332 desc = ses_get_page2_descriptor(edev, ecomp);
333 if (desc)
334 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
335}
336
337static int ses_set_power_status(struct enclosure_device *edev,
338 struct enclosure_component *ecomp,
339 int val)
340{
341 unsigned char desc[4];
342 unsigned char *desc_ptr;
343
344 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
345
346 if (!desc_ptr)
347 return -EIO;
348
349 init_device_slot_control(desc, ecomp, desc_ptr);
350
351 switch (val) {
352 /* power = 1 is device_off = 0 and vice versa */
353 case 0:
354 desc[3] |= 0x10;
355 break;
356 case 1:
357 desc[3] &= 0xef;
358 break;
359 default:
360 return -EINVAL;
361 }
362 ecomp->power_status = val;
363 return ses_set_page2_descriptor(edev, ecomp, desc);
364}
365
James Bottomley9927c682008-02-03 15:48:56 -0600366static struct enclosure_component_callbacks ses_enclosure_callbacks = {
367 .get_fault = ses_get_fault,
368 .set_fault = ses_set_fault,
369 .get_status = ses_get_status,
370 .get_locate = ses_get_locate,
371 .set_locate = ses_set_locate,
Song Liu08024882014-12-30 14:46:18 -0800372 .get_power_status = ses_get_power_status,
373 .set_power_status = ses_set_power_status,
James Bottomley9927c682008-02-03 15:48:56 -0600374 .set_active = ses_set_active,
Dan Williams967f7ba2014-12-30 14:46:16 -0800375 .show_id = ses_show_id,
James Bottomley9927c682008-02-03 15:48:56 -0600376};
377
378struct ses_host_edev {
379 struct Scsi_Host *shost;
380 struct enclosure_device *edev;
381};
382
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800383#if 0
James Bottomley9927c682008-02-03 15:48:56 -0600384int ses_match_host(struct enclosure_device *edev, void *data)
385{
386 struct ses_host_edev *sed = data;
387 struct scsi_device *sdev;
388
Tony Jonesee959b02008-02-22 00:13:36 +0100389 if (!scsi_is_sdev_device(edev->edev.parent))
James Bottomley9927c682008-02-03 15:48:56 -0600390 return 0;
391
Tony Jonesee959b02008-02-22 00:13:36 +0100392 sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600393
394 if (sdev->host != sed->shost)
395 return 0;
396
397 sed->edev = edev;
398 return 1;
399}
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800400#endif /* 0 */
James Bottomley9927c682008-02-03 15:48:56 -0600401
402static void ses_process_descriptor(struct enclosure_component *ecomp,
403 unsigned char *desc)
404{
405 int eip = desc[0] & 0x10;
406 int invalid = desc[0] & 0x80;
407 enum scsi_protocol proto = desc[0] & 0x0f;
408 u64 addr = 0;
Dan Williams921ce7f2014-12-30 14:46:17 -0800409 int slot = -1;
James Bottomley9927c682008-02-03 15:48:56 -0600410 struct ses_component *scomp = ecomp->scratch;
411 unsigned char *d;
412
James Bottomley9927c682008-02-03 15:48:56 -0600413 if (invalid)
414 return;
415
416 switch (proto) {
Dan Williams921ce7f2014-12-30 14:46:17 -0800417 case SCSI_PROTOCOL_FCP:
418 if (eip) {
419 d = desc + 4;
420 slot = d[3];
421 }
422 break;
James Bottomley9927c682008-02-03 15:48:56 -0600423 case SCSI_PROTOCOL_SAS:
Dan Williams921ce7f2014-12-30 14:46:17 -0800424 if (eip) {
425 d = desc + 4;
426 slot = d[3];
James Bottomley9927c682008-02-03 15:48:56 -0600427 d = desc + 8;
Dan Williams921ce7f2014-12-30 14:46:17 -0800428 } else
James Bottomley9927c682008-02-03 15:48:56 -0600429 d = desc + 4;
430 /* only take the phy0 addr */
431 addr = (u64)d[12] << 56 |
432 (u64)d[13] << 48 |
433 (u64)d[14] << 40 |
434 (u64)d[15] << 32 |
435 (u64)d[16] << 24 |
436 (u64)d[17] << 16 |
437 (u64)d[18] << 8 |
438 (u64)d[19];
439 break;
440 default:
441 /* FIXME: Need to add more protocols than just SAS */
442 break;
443 }
Dan Williams921ce7f2014-12-30 14:46:17 -0800444 ecomp->slot = slot;
James Bottomley9927c682008-02-03 15:48:56 -0600445 scomp->addr = addr;
446}
447
448struct efd {
449 u64 addr;
450 struct device *dev;
451};
452
453static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
454 void *data)
455{
456 struct efd *efd = data;
457 int i;
458 struct ses_component *scomp;
459
460 if (!edev->component[0].scratch)
461 return 0;
462
463 for (i = 0; i < edev->components; i++) {
464 scomp = edev->component[i].scratch;
465 if (scomp->addr != efd->addr)
466 continue;
467
Dan Williams15a0fbb2014-12-30 14:46:15 -0800468 if (enclosure_add_device(edev, i, efd->dev) == 0)
469 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
James Bottomley9927c682008-02-03 15:48:56 -0600470 return 1;
471 }
472 return 0;
473}
474
James Bottomley21fab1d2009-08-01 00:43:59 +0000475#define INIT_ALLOC_SIZE 32
476
477static void ses_enclosure_data_process(struct enclosure_device *edev,
478 struct scsi_device *sdev,
479 int create)
480{
481 u32 result;
482 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
483 int i, j, page7_len, len, components;
484 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400485 int types = ses_dev->page1_num_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000486 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
487
488 if (!hdr_buf)
489 goto simple_populate;
490
491 /* re-read page 10 */
492 if (ses_dev->page10)
493 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
494 /* Page 7 for the descriptors is optional */
495 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
496 if (result)
497 goto simple_populate;
498
499 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
500 /* add 1 for trailing '\0' we'll use */
501 buf = kzalloc(len + 1, GFP_KERNEL);
502 if (!buf)
503 goto simple_populate;
504 result = ses_recv_diag(sdev, 7, buf, len);
505 if (result) {
506 simple_populate:
507 kfree(buf);
508 buf = NULL;
509 desc_ptr = NULL;
510 len = 0;
511 page7_len = 0;
512 } else {
513 desc_ptr = buf + 8;
514 len = (desc_ptr[2] << 8) + desc_ptr[3];
515 /* skip past overall descriptor */
516 desc_ptr += len + 4;
James Bottomley21fab1d2009-08-01 00:43:59 +0000517 }
John Hughes877a5592009-11-04 19:01:22 +0100518 if (ses_dev->page10)
519 addl_desc_ptr = ses_dev->page10 + 8;
James Bottomley8c3adc72011-03-18 11:24:23 -0400520 type_ptr = ses_dev->page1_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000521 components = 0;
522 for (i = 0; i < types; i++, type_ptr += 4) {
523 for (j = 0; j < type_ptr[1]; j++) {
524 char *name = NULL;
525 struct enclosure_component *ecomp;
526
527 if (desc_ptr) {
528 if (desc_ptr >= buf + page7_len) {
529 desc_ptr = NULL;
530 } else {
531 len = (desc_ptr[2] << 8) + desc_ptr[3];
532 desc_ptr += 4;
533 /* Add trailing zero - pushes into
534 * reserved space */
535 desc_ptr[len] = '\0';
536 name = desc_ptr;
537 }
538 }
539 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
540 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
541
542 if (create)
Dan Williamsed09dcc2014-12-30 14:46:14 -0800543 ecomp = enclosure_component_alloc(
544 edev,
545 components++,
546 type_ptr[0],
547 name);
James Bottomley21fab1d2009-08-01 00:43:59 +0000548 else
549 ecomp = &edev->component[components++];
550
Dan Williamsed09dcc2014-12-30 14:46:14 -0800551 if (!IS_ERR(ecomp)) {
552 if (addl_desc_ptr)
553 ses_process_descriptor(
554 ecomp,
555 addl_desc_ptr);
556 if (create)
557 enclosure_component_register(
558 ecomp);
559 }
James Bottomley21fab1d2009-08-01 00:43:59 +0000560 }
561 if (desc_ptr)
562 desc_ptr += len;
563
James Bottomley5e103352015-12-11 09:16:38 -0800564 if (addl_desc_ptr &&
565 /* only find additional descriptions for specific devices */
566 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
567 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
568 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
569 /* these elements are optional */
570 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
571 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
572 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
James Bottomley21fab1d2009-08-01 00:43:59 +0000573 addl_desc_ptr += addl_desc_ptr[1] + 2;
574
575 }
576 }
577 kfree(buf);
578 kfree(hdr_buf);
579}
580
James Bottomley9927c682008-02-03 15:48:56 -0600581static void ses_match_to_enclosure(struct enclosure_device *edev,
582 struct scsi_device *sdev)
583{
James Bottomley9927c682008-02-03 15:48:56 -0600584 struct efd efd = {
585 .addr = 0,
586 };
James Bottomley9927c682008-02-03 15:48:56 -0600587
James Bottomley21fab1d2009-08-01 00:43:59 +0000588 ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0);
589
Ewan D. Milne9373eba2017-01-09 16:33:36 -0500590 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
James Bottomley3f8d6f22015-12-09 12:56:07 -0800591 efd.addr = sas_get_address(sdev);
James Bottomley671a99c2008-07-29 11:38:25 -0500592
Hannes Reineckec38c0072014-03-15 09:51:51 +0100593 if (efd.addr) {
594 efd.dev = &sdev->sdev_gendev;
James Bottomley9927c682008-02-03 15:48:56 -0600595
Hannes Reineckec38c0072014-03-15 09:51:51 +0100596 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
597 }
James Bottomley9927c682008-02-03 15:48:56 -0600598}
599
Tony Jonesee959b02008-02-22 00:13:36 +0100600static int ses_intf_add(struct device *cdev,
James Bottomley9927c682008-02-03 15:48:56 -0600601 struct class_interface *intf)
602{
Tony Jonesee959b02008-02-22 00:13:36 +0100603 struct scsi_device *sdev = to_scsi_device(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600604 struct scsi_device *tmp_sdev;
James Bottomley21fab1d2009-08-01 00:43:59 +0000605 unsigned char *buf = NULL, *hdr_buf, *type_ptr;
James Bottomley9927c682008-02-03 15:48:56 -0600606 struct ses_device *ses_dev;
607 u32 result;
James Bottomley21fab1d2009-08-01 00:43:59 +0000608 int i, types, len, components = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600609 int err = -ENOMEM;
James Bottomley8c3adc72011-03-18 11:24:23 -0400610 int num_enclosures;
James Bottomley9927c682008-02-03 15:48:56 -0600611 struct enclosure_device *edev;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800612 struct ses_component *scomp = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600613
614 if (!scsi_device_enclosure(sdev)) {
615 /* not an enclosure, but might be in one */
James Bottomley163f52b2009-08-01 00:39:36 +0000616 struct enclosure_device *prev = NULL;
617
618 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
James Bottomley9927c682008-02-03 15:48:56 -0600619 ses_match_to_enclosure(edev, sdev);
James Bottomley163f52b2009-08-01 00:39:36 +0000620 prev = edev;
James Bottomley9927c682008-02-03 15:48:56 -0600621 }
622 return -ENODEV;
623 }
624
625 /* TYPE_ENCLOSURE prints a message in probe */
626 if (sdev->type != TYPE_ENCLOSURE)
627 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
628
629 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
630 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
631 if (!hdr_buf || !ses_dev)
632 goto err_init_free;
633
634 result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
635 if (result)
636 goto recv_failed;
637
James Bottomley9927c682008-02-03 15:48:56 -0600638 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
639 buf = kzalloc(len, GFP_KERNEL);
640 if (!buf)
641 goto err_free;
642
James Bottomley9927c682008-02-03 15:48:56 -0600643 result = ses_recv_diag(sdev, 1, buf, len);
644 if (result)
645 goto recv_failed;
646
James Bottomley8c3adc72011-03-18 11:24:23 -0400647 types = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600648
James Bottomley8c3adc72011-03-18 11:24:23 -0400649 /* we always have one main enclosure and the rest are referred
650 * to as secondary subenclosures */
651 num_enclosures = buf[1] + 1;
James Bottomley9927c682008-02-03 15:48:56 -0600652
James Bottomley8c3adc72011-03-18 11:24:23 -0400653 /* begin at the enclosure descriptor */
654 type_ptr = buf + 8;
655 /* skip all the enclosure descriptors */
656 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
657 types += type_ptr[2];
658 type_ptr += type_ptr[3] + 4;
659 }
660
661 ses_dev->page1_types = type_ptr;
662 ses_dev->page1_num_types = types;
663
664 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600665 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
666 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
667 components += type_ptr[1];
668 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800669 ses_dev->page1 = buf;
670 ses_dev->page1_len = len;
671 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600672
673 result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
674 if (result)
675 goto recv_failed;
676
677 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
678 buf = kzalloc(len, GFP_KERNEL);
679 if (!buf)
680 goto err_free;
681
682 /* make sure getting page 2 actually works */
683 result = ses_recv_diag(sdev, 2, buf, len);
684 if (result)
685 goto recv_failed;
686 ses_dev->page2 = buf;
687 ses_dev->page2_len = len;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800688 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600689
690 /* The additional information page --- allows us
691 * to match up the devices */
692 result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
Yinghai Lu691b4772008-02-13 16:25:16 -0800693 if (!result) {
James Bottomley9927c682008-02-03 15:48:56 -0600694
Yinghai Lu691b4772008-02-13 16:25:16 -0800695 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
696 buf = kzalloc(len, GFP_KERNEL);
697 if (!buf)
698 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600699
Yinghai Lu691b4772008-02-13 16:25:16 -0800700 result = ses_recv_diag(sdev, 10, buf, len);
701 if (result)
702 goto recv_failed;
703 ses_dev->page10 = buf;
704 ses_dev->page10_len = len;
705 buf = NULL;
706 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800707 scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
James Bottomley9927c682008-02-03 15:48:56 -0600708 if (!scomp)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800709 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600710
Kay Sievers71610f52008-12-03 22:41:36 +0100711 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
James Bottomley9927c682008-02-03 15:48:56 -0600712 components, &ses_enclosure_callbacks);
713 if (IS_ERR(edev)) {
714 err = PTR_ERR(edev);
715 goto err_free;
716 }
717
Julia Lawall9b3a6542010-03-10 15:20:42 -0800718 kfree(hdr_buf);
719
James Bottomley9927c682008-02-03 15:48:56 -0600720 edev->scratch = ses_dev;
721 for (i = 0; i < components; i++)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800722 edev->component[i].scratch = scomp + i;
James Bottomley9927c682008-02-03 15:48:56 -0600723
James Bottomley21fab1d2009-08-01 00:43:59 +0000724 ses_enclosure_data_process(edev, sdev, 1);
James Bottomley9927c682008-02-03 15:48:56 -0600725
726 /* see if there are any devices matching before
727 * we found the enclosure */
728 shost_for_each_device(tmp_sdev, sdev->host) {
729 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
730 continue;
731 ses_match_to_enclosure(edev, tmp_sdev);
732 }
733
734 return 0;
735
736 recv_failed:
737 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
738 result);
739 err = -ENODEV;
740 err_free:
741 kfree(buf);
Yinghai Lu7c46c202008-02-10 23:25:25 -0800742 kfree(scomp);
James Bottomley9927c682008-02-03 15:48:56 -0600743 kfree(ses_dev->page10);
744 kfree(ses_dev->page2);
745 kfree(ses_dev->page1);
746 err_init_free:
747 kfree(ses_dev);
748 kfree(hdr_buf);
749 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
750 return err;
751}
752
753static int ses_remove(struct device *dev)
754{
755 return 0;
756}
757
James Bottomley43d8eb92009-08-01 00:41:22 +0000758static void ses_intf_remove_component(struct scsi_device *sdev)
James Bottomley9927c682008-02-03 15:48:56 -0600759{
James Bottomley43d8eb92009-08-01 00:41:22 +0000760 struct enclosure_device *edev, *prev = NULL;
761
762 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
763 prev = edev;
764 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
765 break;
766 }
767 if (edev)
768 put_device(&edev->edev);
769}
770
771static void ses_intf_remove_enclosure(struct scsi_device *sdev)
772{
James Bottomley9927c682008-02-03 15:48:56 -0600773 struct enclosure_device *edev;
774 struct ses_device *ses_dev;
775
James Bottomley163f52b2009-08-01 00:39:36 +0000776 /* exact match to this enclosure */
James Bottomley43d8eb92009-08-01 00:41:22 +0000777 edev = enclosure_find(&sdev->sdev_gendev, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600778 if (!edev)
779 return;
780
Calvin Owense120dcb2016-05-13 13:28:21 -0700781 enclosure_unregister(edev);
782
James Bottomley9927c682008-02-03 15:48:56 -0600783 ses_dev = edev->scratch;
784 edev->scratch = NULL;
785
Yinghai Lu7c46c202008-02-10 23:25:25 -0800786 kfree(ses_dev->page10);
James Bottomley9927c682008-02-03 15:48:56 -0600787 kfree(ses_dev->page1);
788 kfree(ses_dev->page2);
789 kfree(ses_dev);
790
791 kfree(edev->component[0].scratch);
792
Tony Jonesee959b02008-02-22 00:13:36 +0100793 put_device(&edev->edev);
James Bottomley9927c682008-02-03 15:48:56 -0600794}
795
James Bottomley43d8eb92009-08-01 00:41:22 +0000796static void ses_intf_remove(struct device *cdev,
797 struct class_interface *intf)
798{
799 struct scsi_device *sdev = to_scsi_device(cdev->parent);
800
801 if (!scsi_device_enclosure(sdev))
802 ses_intf_remove_component(sdev);
803 else
804 ses_intf_remove_enclosure(sdev);
805}
806
James Bottomley9927c682008-02-03 15:48:56 -0600807static struct class_interface ses_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100808 .add_dev = ses_intf_add,
809 .remove_dev = ses_intf_remove,
James Bottomley9927c682008-02-03 15:48:56 -0600810};
811
812static struct scsi_driver ses_template = {
James Bottomley9927c682008-02-03 15:48:56 -0600813 .gendrv = {
814 .name = "ses",
Christoph Hellwig3af6b352014-11-12 18:34:51 +0100815 .owner = THIS_MODULE,
James Bottomley9927c682008-02-03 15:48:56 -0600816 .probe = ses_probe,
817 .remove = ses_remove,
818 },
819};
820
821static int __init ses_init(void)
822{
823 int err;
824
825 err = scsi_register_interface(&ses_interface);
826 if (err)
827 return err;
828
829 err = scsi_register_driver(&ses_template.gendrv);
830 if (err)
831 goto out_unreg;
832
833 return 0;
834
835 out_unreg:
836 scsi_unregister_interface(&ses_interface);
837 return err;
838}
839
840static void __exit ses_exit(void)
841{
842 scsi_unregister_driver(&ses_template.gendrv);
843 scsi_unregister_interface(&ses_interface);
844}
845
846module_init(ses_init);
847module_exit(ses_exit);
848
849MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
850
851MODULE_AUTHOR("James Bottomley");
852MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
853MODULE_LICENSE("GPL v2");