blob: eba183c428cf3c3fc78e55c356cdd20e615d48d1 [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>
28
29#include <scsi/scsi.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_dbg.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_driver.h>
34#include <scsi/scsi_host.h>
35
36struct ses_device {
Yinghai Lu691b4772008-02-13 16:25:16 -080037 unsigned char *page1;
James Bottomley8c3adc72011-03-18 11:24:23 -040038 unsigned char *page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -080039 unsigned char *page2;
40 unsigned char *page10;
James Bottomley9927c682008-02-03 15:48:56 -060041 short page1_len;
James Bottomley8c3adc72011-03-18 11:24:23 -040042 short page1_num_types;
James Bottomley9927c682008-02-03 15:48:56 -060043 short page2_len;
44 short page10_len;
45};
46
47struct ses_component {
48 u64 addr;
49 unsigned char *desc;
50};
51
52static int ses_probe(struct device *dev)
53{
54 struct scsi_device *sdev = to_scsi_device(dev);
55 int err = -ENODEV;
56
57 if (sdev->type != TYPE_ENCLOSURE)
58 goto out;
59
60 err = 0;
61 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
62
63 out:
64 return err;
65}
66
Matthew Wilcoxc95e62c2008-06-23 09:14:31 -060067#define SES_TIMEOUT (30 * HZ)
James Bottomley9927c682008-02-03 15:48:56 -060068#define SES_RETRIES 3
69
70static int ses_recv_diag(struct scsi_device *sdev, int page_code,
71 void *buf, int bufflen)
72{
Yinghai Lu691b4772008-02-13 16:25:16 -080073 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060074 RECEIVE_DIAGNOSTIC,
75 1, /* Set PCV bit */
76 page_code,
77 bufflen >> 8,
78 bufflen & 0xff,
79 0
80 };
81
82 return scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +090083 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -060084}
85
86static int ses_send_diag(struct scsi_device *sdev, int page_code,
87 void *buf, int bufflen)
88{
89 u32 result;
90
Yinghai Lu691b4772008-02-13 16:25:16 -080091 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060092 SEND_DIAGNOSTIC,
93 0x10, /* Set PF bit */
94 0,
95 bufflen >> 8,
96 bufflen & 0xff,
97 0
98 };
99
100 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900101 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600102 if (result)
103 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
104 result);
105 return result;
106}
107
108static int ses_set_page2_descriptor(struct enclosure_device *edev,
109 struct enclosure_component *ecomp,
Yinghai Lu691b4772008-02-13 16:25:16 -0800110 unsigned char *desc)
James Bottomley9927c682008-02-03 15:48:56 -0600111{
112 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100113 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600114 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400115 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800116 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600117
118 /* Clear everything */
119 memset(desc_ptr, 0, ses_dev->page2_len - 8);
James Bottomley8c3adc72011-03-18 11:24:23 -0400120 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600121 for (j = 0; j < type_ptr[1]; j++) {
122 desc_ptr += 4;
123 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
124 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
125 continue;
126 if (count++ == descriptor) {
127 memcpy(desc_ptr, desc, 4);
128 /* set select */
129 desc_ptr[0] |= 0x80;
130 /* clear reserved, just in case */
131 desc_ptr[0] &= 0xf0;
132 }
133 }
134 }
135
136 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
137}
138
Yinghai Lu691b4772008-02-13 16:25:16 -0800139static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
James Bottomley9927c682008-02-03 15:48:56 -0600140 struct enclosure_component *ecomp)
141{
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 ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
149
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 return desc_ptr;
158 }
159 }
160 return NULL;
161}
162
Douglas Gilbert2a350ca2011-06-09 00:27:07 -0400163/* For device slot and array device slot elements, byte 3 bit 6
164 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
165 * code stands these bits are shifted 4 positions right so in
166 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
James Bottomley9927c682008-02-03 15:48:56 -0600167static void ses_get_fault(struct enclosure_device *edev,
168 struct enclosure_component *ecomp)
169{
Yinghai Lu691b4772008-02-13 16:25:16 -0800170 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600171
172 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800173 if (desc)
174 ecomp->fault = (desc[3] & 0x60) >> 4;
James Bottomley9927c682008-02-03 15:48:56 -0600175}
176
177static int ses_set_fault(struct enclosure_device *edev,
178 struct enclosure_component *ecomp,
179 enum enclosure_component_setting val)
180{
Yinghai Lu691b4772008-02-13 16:25:16 -0800181 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600182
183 switch (val) {
184 case ENCLOSURE_SETTING_DISABLED:
185 /* zero is disabled */
186 break;
187 case ENCLOSURE_SETTING_ENABLED:
Douglas Gilbert2a350ca2011-06-09 00:27:07 -0400188 desc[3] = 0x20;
James Bottomley9927c682008-02-03 15:48:56 -0600189 break;
190 default:
191 /* SES doesn't do the SGPIO blink settings */
192 return -EINVAL;
193 }
194
195 return ses_set_page2_descriptor(edev, ecomp, desc);
196}
197
198static void ses_get_status(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
203 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800204 if (desc)
205 ecomp->status = (desc[0] & 0x0f);
James Bottomley9927c682008-02-03 15:48:56 -0600206}
207
208static void ses_get_locate(struct enclosure_device *edev,
209 struct enclosure_component *ecomp)
210{
Yinghai Lu691b4772008-02-13 16:25:16 -0800211 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600212
213 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800214 if (desc)
215 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
James Bottomley9927c682008-02-03 15:48:56 -0600216}
217
218static int ses_set_locate(struct enclosure_device *edev,
219 struct enclosure_component *ecomp,
220 enum enclosure_component_setting val)
221{
Yinghai Lu691b4772008-02-13 16:25:16 -0800222 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600223
224 switch (val) {
225 case ENCLOSURE_SETTING_DISABLED:
226 /* zero is disabled */
227 break;
228 case ENCLOSURE_SETTING_ENABLED:
229 desc[2] = 0x02;
230 break;
231 default:
232 /* SES doesn't do the SGPIO blink settings */
233 return -EINVAL;
234 }
235 return ses_set_page2_descriptor(edev, ecomp, desc);
236}
237
238static int ses_set_active(struct enclosure_device *edev,
239 struct enclosure_component *ecomp,
240 enum enclosure_component_setting val)
241{
Yinghai Lu691b4772008-02-13 16:25:16 -0800242 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600243
244 switch (val) {
245 case ENCLOSURE_SETTING_DISABLED:
246 /* zero is disabled */
247 ecomp->active = 0;
248 break;
249 case ENCLOSURE_SETTING_ENABLED:
250 desc[2] = 0x80;
251 ecomp->active = 1;
252 break;
253 default:
254 /* SES doesn't do the SGPIO blink settings */
255 return -EINVAL;
256 }
257 return ses_set_page2_descriptor(edev, ecomp, desc);
258}
259
260static struct enclosure_component_callbacks ses_enclosure_callbacks = {
261 .get_fault = ses_get_fault,
262 .set_fault = ses_set_fault,
263 .get_status = ses_get_status,
264 .get_locate = ses_get_locate,
265 .set_locate = ses_set_locate,
266 .set_active = ses_set_active,
267};
268
269struct ses_host_edev {
270 struct Scsi_Host *shost;
271 struct enclosure_device *edev;
272};
273
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800274#if 0
James Bottomley9927c682008-02-03 15:48:56 -0600275int ses_match_host(struct enclosure_device *edev, void *data)
276{
277 struct ses_host_edev *sed = data;
278 struct scsi_device *sdev;
279
Tony Jonesee959b02008-02-22 00:13:36 +0100280 if (!scsi_is_sdev_device(edev->edev.parent))
James Bottomley9927c682008-02-03 15:48:56 -0600281 return 0;
282
Tony Jonesee959b02008-02-22 00:13:36 +0100283 sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600284
285 if (sdev->host != sed->shost)
286 return 0;
287
288 sed->edev = edev;
289 return 1;
290}
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800291#endif /* 0 */
James Bottomley9927c682008-02-03 15:48:56 -0600292
293static void ses_process_descriptor(struct enclosure_component *ecomp,
294 unsigned char *desc)
295{
296 int eip = desc[0] & 0x10;
297 int invalid = desc[0] & 0x80;
298 enum scsi_protocol proto = desc[0] & 0x0f;
299 u64 addr = 0;
300 struct ses_component *scomp = ecomp->scratch;
301 unsigned char *d;
302
303 scomp->desc = desc;
304
305 if (invalid)
306 return;
307
308 switch (proto) {
309 case SCSI_PROTOCOL_SAS:
310 if (eip)
311 d = desc + 8;
312 else
313 d = desc + 4;
314 /* only take the phy0 addr */
315 addr = (u64)d[12] << 56 |
316 (u64)d[13] << 48 |
317 (u64)d[14] << 40 |
318 (u64)d[15] << 32 |
319 (u64)d[16] << 24 |
320 (u64)d[17] << 16 |
321 (u64)d[18] << 8 |
322 (u64)d[19];
323 break;
324 default:
325 /* FIXME: Need to add more protocols than just SAS */
326 break;
327 }
328 scomp->addr = addr;
329}
330
331struct efd {
332 u64 addr;
333 struct device *dev;
334};
335
336static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
337 void *data)
338{
339 struct efd *efd = data;
340 int i;
341 struct ses_component *scomp;
342
343 if (!edev->component[0].scratch)
344 return 0;
345
346 for (i = 0; i < edev->components; i++) {
347 scomp = edev->component[i].scratch;
348 if (scomp->addr != efd->addr)
349 continue;
350
351 enclosure_add_device(edev, i, efd->dev);
352 return 1;
353 }
354 return 0;
355}
356
James Bottomley21fab1d2009-08-01 00:43:59 +0000357#define INIT_ALLOC_SIZE 32
358
359static void ses_enclosure_data_process(struct enclosure_device *edev,
360 struct scsi_device *sdev,
361 int create)
362{
363 u32 result;
364 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
365 int i, j, page7_len, len, components;
366 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400367 int types = ses_dev->page1_num_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000368 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
369
370 if (!hdr_buf)
371 goto simple_populate;
372
373 /* re-read page 10 */
374 if (ses_dev->page10)
375 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
376 /* Page 7 for the descriptors is optional */
377 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
378 if (result)
379 goto simple_populate;
380
381 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
382 /* add 1 for trailing '\0' we'll use */
383 buf = kzalloc(len + 1, GFP_KERNEL);
384 if (!buf)
385 goto simple_populate;
386 result = ses_recv_diag(sdev, 7, buf, len);
387 if (result) {
388 simple_populate:
389 kfree(buf);
390 buf = NULL;
391 desc_ptr = NULL;
392 len = 0;
393 page7_len = 0;
394 } else {
395 desc_ptr = buf + 8;
396 len = (desc_ptr[2] << 8) + desc_ptr[3];
397 /* skip past overall descriptor */
398 desc_ptr += len + 4;
James Bottomley21fab1d2009-08-01 00:43:59 +0000399 }
John Hughes877a5592009-11-04 19:01:22 +0100400 if (ses_dev->page10)
401 addl_desc_ptr = ses_dev->page10 + 8;
James Bottomley8c3adc72011-03-18 11:24:23 -0400402 type_ptr = ses_dev->page1_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000403 components = 0;
404 for (i = 0; i < types; i++, type_ptr += 4) {
405 for (j = 0; j < type_ptr[1]; j++) {
406 char *name = NULL;
407 struct enclosure_component *ecomp;
408
409 if (desc_ptr) {
410 if (desc_ptr >= buf + page7_len) {
411 desc_ptr = NULL;
412 } else {
413 len = (desc_ptr[2] << 8) + desc_ptr[3];
414 desc_ptr += 4;
415 /* Add trailing zero - pushes into
416 * reserved space */
417 desc_ptr[len] = '\0';
418 name = desc_ptr;
419 }
420 }
421 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
422 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
423
424 if (create)
425 ecomp = enclosure_component_register(edev,
426 components++,
427 type_ptr[0],
428 name);
429 else
430 ecomp = &edev->component[components++];
431
432 if (!IS_ERR(ecomp) && addl_desc_ptr)
433 ses_process_descriptor(ecomp,
434 addl_desc_ptr);
435 }
436 if (desc_ptr)
437 desc_ptr += len;
438
439 if (addl_desc_ptr)
440 addl_desc_ptr += addl_desc_ptr[1] + 2;
441
442 }
443 }
444 kfree(buf);
445 kfree(hdr_buf);
446}
447
James Bottomley9927c682008-02-03 15:48:56 -0600448static void ses_match_to_enclosure(struct enclosure_device *edev,
449 struct scsi_device *sdev)
450{
Matthew Wilcox40c34602008-12-31 12:11:17 -0700451 unsigned char *buf;
James Bottomley9927c682008-02-03 15:48:56 -0600452 unsigned char *desc;
Matthew Wilcox40c34602008-12-31 12:11:17 -0700453 unsigned int vpd_len;
James Bottomley9927c682008-02-03 15:48:56 -0600454 struct efd efd = {
455 .addr = 0,
456 };
James Bottomley9927c682008-02-03 15:48:56 -0600457
James Bottomleye3deec02009-11-03 12:33:07 -0600458 buf = kmalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
459 if (!buf || scsi_get_vpd_page(sdev, 0x83, buf, INIT_ALLOC_SIZE))
460 goto free;
James Bottomley9927c682008-02-03 15:48:56 -0600461
James Bottomley21fab1d2009-08-01 00:43:59 +0000462 ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0);
463
Matthew Wilcox40c34602008-12-31 12:11:17 -0700464 vpd_len = ((buf[2] << 8) | buf[3]) + 4;
James Bottomleye3deec02009-11-03 12:33:07 -0600465 kfree(buf);
466 buf = kmalloc(vpd_len, GFP_KERNEL);
467 if (!buf ||scsi_get_vpd_page(sdev, 0x83, buf, vpd_len))
468 goto free;
James Bottomley671a99c2008-07-29 11:38:25 -0500469
James Bottomley9927c682008-02-03 15:48:56 -0600470 desc = buf + 4;
James Bottomley671a99c2008-07-29 11:38:25 -0500471 while (desc < buf + vpd_len) {
James Bottomley9927c682008-02-03 15:48:56 -0600472 enum scsi_protocol proto = desc[0] >> 4;
473 u8 code_set = desc[0] & 0x0f;
474 u8 piv = desc[1] & 0x80;
475 u8 assoc = (desc[1] & 0x30) >> 4;
476 u8 type = desc[1] & 0x0f;
477 u8 len = desc[3];
478
Roel Kluinb3f1f9a2009-02-17 16:59:24 +0100479 if (piv && code_set == 1 && assoc == 1
James Bottomley9927c682008-02-03 15:48:56 -0600480 && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8)
481 efd.addr = (u64)desc[4] << 56 |
482 (u64)desc[5] << 48 |
483 (u64)desc[6] << 40 |
484 (u64)desc[7] << 32 |
485 (u64)desc[8] << 24 |
486 (u64)desc[9] << 16 |
487 (u64)desc[10] << 8 |
488 (u64)desc[11];
489
490 desc += len + 4;
491 }
492 if (!efd.addr)
493 goto free;
494
495 efd.dev = &sdev->sdev_gendev;
496
497 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
498 free:
499 kfree(buf);
500}
501
Tony Jonesee959b02008-02-22 00:13:36 +0100502static int ses_intf_add(struct device *cdev,
James Bottomley9927c682008-02-03 15:48:56 -0600503 struct class_interface *intf)
504{
Tony Jonesee959b02008-02-22 00:13:36 +0100505 struct scsi_device *sdev = to_scsi_device(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600506 struct scsi_device *tmp_sdev;
James Bottomley21fab1d2009-08-01 00:43:59 +0000507 unsigned char *buf = NULL, *hdr_buf, *type_ptr;
James Bottomley9927c682008-02-03 15:48:56 -0600508 struct ses_device *ses_dev;
509 u32 result;
James Bottomley21fab1d2009-08-01 00:43:59 +0000510 int i, types, len, components = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600511 int err = -ENOMEM;
James Bottomley8c3adc72011-03-18 11:24:23 -0400512 int num_enclosures;
James Bottomley9927c682008-02-03 15:48:56 -0600513 struct enclosure_device *edev;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800514 struct ses_component *scomp = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600515
516 if (!scsi_device_enclosure(sdev)) {
517 /* not an enclosure, but might be in one */
James Bottomley163f52b2009-08-01 00:39:36 +0000518 struct enclosure_device *prev = NULL;
519
520 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
James Bottomley9927c682008-02-03 15:48:56 -0600521 ses_match_to_enclosure(edev, sdev);
James Bottomley163f52b2009-08-01 00:39:36 +0000522 prev = edev;
James Bottomley9927c682008-02-03 15:48:56 -0600523 }
524 return -ENODEV;
525 }
526
527 /* TYPE_ENCLOSURE prints a message in probe */
528 if (sdev->type != TYPE_ENCLOSURE)
529 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
530
531 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
532 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
533 if (!hdr_buf || !ses_dev)
534 goto err_init_free;
535
536 result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
537 if (result)
538 goto recv_failed;
539
James Bottomley9927c682008-02-03 15:48:56 -0600540 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
541 buf = kzalloc(len, GFP_KERNEL);
542 if (!buf)
543 goto err_free;
544
James Bottomley9927c682008-02-03 15:48:56 -0600545 result = ses_recv_diag(sdev, 1, buf, len);
546 if (result)
547 goto recv_failed;
548
James Bottomley8c3adc72011-03-18 11:24:23 -0400549 types = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600550
James Bottomley8c3adc72011-03-18 11:24:23 -0400551 /* we always have one main enclosure and the rest are referred
552 * to as secondary subenclosures */
553 num_enclosures = buf[1] + 1;
James Bottomley9927c682008-02-03 15:48:56 -0600554
James Bottomley8c3adc72011-03-18 11:24:23 -0400555 /* begin at the enclosure descriptor */
556 type_ptr = buf + 8;
557 /* skip all the enclosure descriptors */
558 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
559 types += type_ptr[2];
560 type_ptr += type_ptr[3] + 4;
561 }
562
563 ses_dev->page1_types = type_ptr;
564 ses_dev->page1_num_types = types;
565
566 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600567 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
568 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
569 components += type_ptr[1];
570 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800571 ses_dev->page1 = buf;
572 ses_dev->page1_len = len;
573 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600574
575 result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
576 if (result)
577 goto recv_failed;
578
579 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
580 buf = kzalloc(len, GFP_KERNEL);
581 if (!buf)
582 goto err_free;
583
584 /* make sure getting page 2 actually works */
585 result = ses_recv_diag(sdev, 2, buf, len);
586 if (result)
587 goto recv_failed;
588 ses_dev->page2 = buf;
589 ses_dev->page2_len = len;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800590 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600591
592 /* The additional information page --- allows us
593 * to match up the devices */
594 result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
Yinghai Lu691b4772008-02-13 16:25:16 -0800595 if (!result) {
James Bottomley9927c682008-02-03 15:48:56 -0600596
Yinghai Lu691b4772008-02-13 16:25:16 -0800597 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
598 buf = kzalloc(len, GFP_KERNEL);
599 if (!buf)
600 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600601
Yinghai Lu691b4772008-02-13 16:25:16 -0800602 result = ses_recv_diag(sdev, 10, buf, len);
603 if (result)
604 goto recv_failed;
605 ses_dev->page10 = buf;
606 ses_dev->page10_len = len;
607 buf = NULL;
608 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800609 scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
James Bottomley9927c682008-02-03 15:48:56 -0600610 if (!scomp)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800611 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600612
Kay Sievers71610f52008-12-03 22:41:36 +0100613 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
James Bottomley9927c682008-02-03 15:48:56 -0600614 components, &ses_enclosure_callbacks);
615 if (IS_ERR(edev)) {
616 err = PTR_ERR(edev);
617 goto err_free;
618 }
619
Julia Lawall9b3a6542010-03-10 15:20:42 -0800620 kfree(hdr_buf);
621
James Bottomley9927c682008-02-03 15:48:56 -0600622 edev->scratch = ses_dev;
623 for (i = 0; i < components; i++)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800624 edev->component[i].scratch = scomp + i;
James Bottomley9927c682008-02-03 15:48:56 -0600625
James Bottomley21fab1d2009-08-01 00:43:59 +0000626 ses_enclosure_data_process(edev, sdev, 1);
James Bottomley9927c682008-02-03 15:48:56 -0600627
628 /* see if there are any devices matching before
629 * we found the enclosure */
630 shost_for_each_device(tmp_sdev, sdev->host) {
631 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
632 continue;
633 ses_match_to_enclosure(edev, tmp_sdev);
634 }
635
636 return 0;
637
638 recv_failed:
639 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
640 result);
641 err = -ENODEV;
642 err_free:
643 kfree(buf);
Yinghai Lu7c46c202008-02-10 23:25:25 -0800644 kfree(scomp);
James Bottomley9927c682008-02-03 15:48:56 -0600645 kfree(ses_dev->page10);
646 kfree(ses_dev->page2);
647 kfree(ses_dev->page1);
648 err_init_free:
649 kfree(ses_dev);
650 kfree(hdr_buf);
651 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
652 return err;
653}
654
655static int ses_remove(struct device *dev)
656{
657 return 0;
658}
659
James Bottomley43d8eb92009-08-01 00:41:22 +0000660static void ses_intf_remove_component(struct scsi_device *sdev)
James Bottomley9927c682008-02-03 15:48:56 -0600661{
James Bottomley43d8eb92009-08-01 00:41:22 +0000662 struct enclosure_device *edev, *prev = NULL;
663
664 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
665 prev = edev;
666 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
667 break;
668 }
669 if (edev)
670 put_device(&edev->edev);
671}
672
673static void ses_intf_remove_enclosure(struct scsi_device *sdev)
674{
James Bottomley9927c682008-02-03 15:48:56 -0600675 struct enclosure_device *edev;
676 struct ses_device *ses_dev;
677
James Bottomley163f52b2009-08-01 00:39:36 +0000678 /* exact match to this enclosure */
James Bottomley43d8eb92009-08-01 00:41:22 +0000679 edev = enclosure_find(&sdev->sdev_gendev, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600680 if (!edev)
681 return;
682
683 ses_dev = edev->scratch;
684 edev->scratch = NULL;
685
Yinghai Lu7c46c202008-02-10 23:25:25 -0800686 kfree(ses_dev->page10);
James Bottomley9927c682008-02-03 15:48:56 -0600687 kfree(ses_dev->page1);
688 kfree(ses_dev->page2);
689 kfree(ses_dev);
690
691 kfree(edev->component[0].scratch);
692
Tony Jonesee959b02008-02-22 00:13:36 +0100693 put_device(&edev->edev);
James Bottomley9927c682008-02-03 15:48:56 -0600694 enclosure_unregister(edev);
695}
696
James Bottomley43d8eb92009-08-01 00:41:22 +0000697static void ses_intf_remove(struct device *cdev,
698 struct class_interface *intf)
699{
700 struct scsi_device *sdev = to_scsi_device(cdev->parent);
701
702 if (!scsi_device_enclosure(sdev))
703 ses_intf_remove_component(sdev);
704 else
705 ses_intf_remove_enclosure(sdev);
706}
707
James Bottomley9927c682008-02-03 15:48:56 -0600708static struct class_interface ses_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100709 .add_dev = ses_intf_add,
710 .remove_dev = ses_intf_remove,
James Bottomley9927c682008-02-03 15:48:56 -0600711};
712
713static struct scsi_driver ses_template = {
714 .owner = THIS_MODULE,
715 .gendrv = {
716 .name = "ses",
717 .probe = ses_probe,
718 .remove = ses_remove,
719 },
720};
721
722static int __init ses_init(void)
723{
724 int err;
725
726 err = scsi_register_interface(&ses_interface);
727 if (err)
728 return err;
729
730 err = scsi_register_driver(&ses_template.gendrv);
731 if (err)
732 goto out_unreg;
733
734 return 0;
735
736 out_unreg:
737 scsi_unregister_interface(&ses_interface);
738 return err;
739}
740
741static void __exit ses_exit(void)
742{
743 scsi_unregister_driver(&ses_template.gendrv);
744 scsi_unregister_interface(&ses_interface);
745}
746
747module_init(ses_init);
748module_exit(ses_exit);
749
750MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
751
752MODULE_AUTHOR("James Bottomley");
753MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
754MODULE_LICENSE("GPL v2");