blob: f2cf95235543c3cce8c9ad56f9f98a5894ce4e94 [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
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/enclosure.h>
27
28#include <scsi/scsi.h>
29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_dbg.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_driver.h>
33#include <scsi/scsi_host.h>
34
35struct ses_device {
Yinghai Lu691b4772008-02-13 16:25:16 -080036 unsigned char *page1;
37 unsigned char *page2;
38 unsigned char *page10;
James Bottomley9927c682008-02-03 15:48:56 -060039 short page1_len;
40 short page2_len;
41 short page10_len;
42};
43
44struct ses_component {
45 u64 addr;
46 unsigned char *desc;
47};
48
49static int ses_probe(struct device *dev)
50{
51 struct scsi_device *sdev = to_scsi_device(dev);
52 int err = -ENODEV;
53
54 if (sdev->type != TYPE_ENCLOSURE)
55 goto out;
56
57 err = 0;
58 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
59
60 out:
61 return err;
62}
63
Matthew Wilcoxc95e62c2008-06-23 09:14:31 -060064#define SES_TIMEOUT (30 * HZ)
James Bottomley9927c682008-02-03 15:48:56 -060065#define SES_RETRIES 3
66
67static int ses_recv_diag(struct scsi_device *sdev, int page_code,
68 void *buf, int bufflen)
69{
Yinghai Lu691b4772008-02-13 16:25:16 -080070 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060071 RECEIVE_DIAGNOSTIC,
72 1, /* Set PCV bit */
73 page_code,
74 bufflen >> 8,
75 bufflen & 0xff,
76 0
77 };
78
79 return scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +090080 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -060081}
82
83static int ses_send_diag(struct scsi_device *sdev, int page_code,
84 void *buf, int bufflen)
85{
86 u32 result;
87
Yinghai Lu691b4772008-02-13 16:25:16 -080088 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060089 SEND_DIAGNOSTIC,
90 0x10, /* Set PF bit */
91 0,
92 bufflen >> 8,
93 bufflen & 0xff,
94 0
95 };
96
97 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +090098 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -060099 if (result)
100 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
101 result);
102 return result;
103}
104
105static int ses_set_page2_descriptor(struct enclosure_device *edev,
106 struct enclosure_component *ecomp,
Yinghai Lu691b4772008-02-13 16:25:16 -0800107 unsigned char *desc)
James Bottomley9927c682008-02-03 15:48:56 -0600108{
109 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100110 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600111 struct ses_device *ses_dev = edev->scratch;
Yinghai Lu691b4772008-02-13 16:25:16 -0800112 unsigned char *type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11];
113 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600114
115 /* Clear everything */
116 memset(desc_ptr, 0, ses_dev->page2_len - 8);
117 for (i = 0; i < ses_dev->page1[10]; i++, type_ptr += 4) {
118 for (j = 0; j < type_ptr[1]; j++) {
119 desc_ptr += 4;
120 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
121 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
122 continue;
123 if (count++ == descriptor) {
124 memcpy(desc_ptr, desc, 4);
125 /* set select */
126 desc_ptr[0] |= 0x80;
127 /* clear reserved, just in case */
128 desc_ptr[0] &= 0xf0;
129 }
130 }
131 }
132
133 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
134}
135
Yinghai Lu691b4772008-02-13 16:25:16 -0800136static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
James Bottomley9927c682008-02-03 15:48:56 -0600137 struct enclosure_component *ecomp)
138{
139 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100140 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600141 struct ses_device *ses_dev = edev->scratch;
Yinghai Lu691b4772008-02-13 16:25:16 -0800142 unsigned char *type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11];
143 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600144
145 ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
146
147 for (i = 0; i < ses_dev->page1[10]; i++, type_ptr += 4) {
148 for (j = 0; j < type_ptr[1]; j++) {
149 desc_ptr += 4;
150 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
151 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
152 continue;
153 if (count++ == descriptor)
154 return desc_ptr;
155 }
156 }
157 return NULL;
158}
159
160static void ses_get_fault(struct enclosure_device *edev,
161 struct enclosure_component *ecomp)
162{
Yinghai Lu691b4772008-02-13 16:25:16 -0800163 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600164
165 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800166 if (desc)
167 ecomp->fault = (desc[3] & 0x60) >> 4;
James Bottomley9927c682008-02-03 15:48:56 -0600168}
169
170static int ses_set_fault(struct enclosure_device *edev,
171 struct enclosure_component *ecomp,
172 enum enclosure_component_setting val)
173{
Yinghai Lu691b4772008-02-13 16:25:16 -0800174 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600175
176 switch (val) {
177 case ENCLOSURE_SETTING_DISABLED:
178 /* zero is disabled */
179 break;
180 case ENCLOSURE_SETTING_ENABLED:
181 desc[2] = 0x02;
182 break;
183 default:
184 /* SES doesn't do the SGPIO blink settings */
185 return -EINVAL;
186 }
187
188 return ses_set_page2_descriptor(edev, ecomp, desc);
189}
190
191static void ses_get_status(struct enclosure_device *edev,
192 struct enclosure_component *ecomp)
193{
Yinghai Lu691b4772008-02-13 16:25:16 -0800194 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600195
196 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800197 if (desc)
198 ecomp->status = (desc[0] & 0x0f);
James Bottomley9927c682008-02-03 15:48:56 -0600199}
200
201static void ses_get_locate(struct enclosure_device *edev,
202 struct enclosure_component *ecomp)
203{
Yinghai Lu691b4772008-02-13 16:25:16 -0800204 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600205
206 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800207 if (desc)
208 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
James Bottomley9927c682008-02-03 15:48:56 -0600209}
210
211static int ses_set_locate(struct enclosure_device *edev,
212 struct enclosure_component *ecomp,
213 enum enclosure_component_setting val)
214{
Yinghai Lu691b4772008-02-13 16:25:16 -0800215 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600216
217 switch (val) {
218 case ENCLOSURE_SETTING_DISABLED:
219 /* zero is disabled */
220 break;
221 case ENCLOSURE_SETTING_ENABLED:
222 desc[2] = 0x02;
223 break;
224 default:
225 /* SES doesn't do the SGPIO blink settings */
226 return -EINVAL;
227 }
228 return ses_set_page2_descriptor(edev, ecomp, desc);
229}
230
231static int ses_set_active(struct enclosure_device *edev,
232 struct enclosure_component *ecomp,
233 enum enclosure_component_setting val)
234{
Yinghai Lu691b4772008-02-13 16:25:16 -0800235 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600236
237 switch (val) {
238 case ENCLOSURE_SETTING_DISABLED:
239 /* zero is disabled */
240 ecomp->active = 0;
241 break;
242 case ENCLOSURE_SETTING_ENABLED:
243 desc[2] = 0x80;
244 ecomp->active = 1;
245 break;
246 default:
247 /* SES doesn't do the SGPIO blink settings */
248 return -EINVAL;
249 }
250 return ses_set_page2_descriptor(edev, ecomp, desc);
251}
252
253static struct enclosure_component_callbacks ses_enclosure_callbacks = {
254 .get_fault = ses_get_fault,
255 .set_fault = ses_set_fault,
256 .get_status = ses_get_status,
257 .get_locate = ses_get_locate,
258 .set_locate = ses_set_locate,
259 .set_active = ses_set_active,
260};
261
262struct ses_host_edev {
263 struct Scsi_Host *shost;
264 struct enclosure_device *edev;
265};
266
267int ses_match_host(struct enclosure_device *edev, void *data)
268{
269 struct ses_host_edev *sed = data;
270 struct scsi_device *sdev;
271
Tony Jonesee959b02008-02-22 00:13:36 +0100272 if (!scsi_is_sdev_device(edev->edev.parent))
James Bottomley9927c682008-02-03 15:48:56 -0600273 return 0;
274
Tony Jonesee959b02008-02-22 00:13:36 +0100275 sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600276
277 if (sdev->host != sed->shost)
278 return 0;
279
280 sed->edev = edev;
281 return 1;
282}
283
284static void ses_process_descriptor(struct enclosure_component *ecomp,
285 unsigned char *desc)
286{
287 int eip = desc[0] & 0x10;
288 int invalid = desc[0] & 0x80;
289 enum scsi_protocol proto = desc[0] & 0x0f;
290 u64 addr = 0;
291 struct ses_component *scomp = ecomp->scratch;
292 unsigned char *d;
293
294 scomp->desc = desc;
295
296 if (invalid)
297 return;
298
299 switch (proto) {
300 case SCSI_PROTOCOL_SAS:
301 if (eip)
302 d = desc + 8;
303 else
304 d = desc + 4;
305 /* only take the phy0 addr */
306 addr = (u64)d[12] << 56 |
307 (u64)d[13] << 48 |
308 (u64)d[14] << 40 |
309 (u64)d[15] << 32 |
310 (u64)d[16] << 24 |
311 (u64)d[17] << 16 |
312 (u64)d[18] << 8 |
313 (u64)d[19];
314 break;
315 default:
316 /* FIXME: Need to add more protocols than just SAS */
317 break;
318 }
319 scomp->addr = addr;
320}
321
322struct efd {
323 u64 addr;
324 struct device *dev;
325};
326
327static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
328 void *data)
329{
330 struct efd *efd = data;
331 int i;
332 struct ses_component *scomp;
333
334 if (!edev->component[0].scratch)
335 return 0;
336
337 for (i = 0; i < edev->components; i++) {
338 scomp = edev->component[i].scratch;
339 if (scomp->addr != efd->addr)
340 continue;
341
342 enclosure_add_device(edev, i, efd->dev);
343 return 1;
344 }
345 return 0;
346}
347
James Bottomley9927c682008-02-03 15:48:56 -0600348static void ses_match_to_enclosure(struct enclosure_device *edev,
349 struct scsi_device *sdev)
350{
Matthew Wilcox40c34602008-12-31 12:11:17 -0700351 unsigned char *buf;
James Bottomley9927c682008-02-03 15:48:56 -0600352 unsigned char *desc;
Matthew Wilcox40c34602008-12-31 12:11:17 -0700353 unsigned int vpd_len;
James Bottomley9927c682008-02-03 15:48:56 -0600354 struct efd efd = {
355 .addr = 0,
356 };
James Bottomley9927c682008-02-03 15:48:56 -0600357
Matthew Wilcox40c34602008-12-31 12:11:17 -0700358 buf = scsi_get_vpd_page(sdev, 0x83);
James Bottomley9927c682008-02-03 15:48:56 -0600359 if (!buf)
360 return;
361
Matthew Wilcox40c34602008-12-31 12:11:17 -0700362 vpd_len = ((buf[2] << 8) | buf[3]) + 4;
James Bottomley671a99c2008-07-29 11:38:25 -0500363
James Bottomley9927c682008-02-03 15:48:56 -0600364 desc = buf + 4;
James Bottomley671a99c2008-07-29 11:38:25 -0500365 while (desc < buf + vpd_len) {
James Bottomley9927c682008-02-03 15:48:56 -0600366 enum scsi_protocol proto = desc[0] >> 4;
367 u8 code_set = desc[0] & 0x0f;
368 u8 piv = desc[1] & 0x80;
369 u8 assoc = (desc[1] & 0x30) >> 4;
370 u8 type = desc[1] & 0x0f;
371 u8 len = desc[3];
372
373 if (piv && code_set == 1 && assoc == 1 && code_set == 1
374 && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8)
375 efd.addr = (u64)desc[4] << 56 |
376 (u64)desc[5] << 48 |
377 (u64)desc[6] << 40 |
378 (u64)desc[7] << 32 |
379 (u64)desc[8] << 24 |
380 (u64)desc[9] << 16 |
381 (u64)desc[10] << 8 |
382 (u64)desc[11];
383
384 desc += len + 4;
385 }
386 if (!efd.addr)
387 goto free;
388
389 efd.dev = &sdev->sdev_gendev;
390
391 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
392 free:
393 kfree(buf);
394}
395
396#define INIT_ALLOC_SIZE 32
397
Tony Jonesee959b02008-02-22 00:13:36 +0100398static int ses_intf_add(struct device *cdev,
James Bottomley9927c682008-02-03 15:48:56 -0600399 struct class_interface *intf)
400{
Tony Jonesee959b02008-02-22 00:13:36 +0100401 struct scsi_device *sdev = to_scsi_device(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600402 struct scsi_device *tmp_sdev;
Yinghai Lu691b4772008-02-13 16:25:16 -0800403 unsigned char *buf = NULL, *hdr_buf, *type_ptr, *desc_ptr = NULL,
404 *addl_desc_ptr = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600405 struct ses_device *ses_dev;
406 u32 result;
Yinghai Lu691b4772008-02-13 16:25:16 -0800407 int i, j, types, len, page7_len = 0, components = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600408 int err = -ENOMEM;
409 struct enclosure_device *edev;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800410 struct ses_component *scomp = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600411
412 if (!scsi_device_enclosure(sdev)) {
413 /* not an enclosure, but might be in one */
Yinghai Lu7c46c202008-02-10 23:25:25 -0800414 edev = enclosure_find(&sdev->host->shost_gendev);
James Bottomley9927c682008-02-03 15:48:56 -0600415 if (edev) {
416 ses_match_to_enclosure(edev, sdev);
Tony Jonesee959b02008-02-22 00:13:36 +0100417 put_device(&edev->edev);
James Bottomley9927c682008-02-03 15:48:56 -0600418 }
419 return -ENODEV;
420 }
421
422 /* TYPE_ENCLOSURE prints a message in probe */
423 if (sdev->type != TYPE_ENCLOSURE)
424 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
425
426 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
427 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
428 if (!hdr_buf || !ses_dev)
429 goto err_init_free;
430
431 result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
432 if (result)
433 goto recv_failed;
434
435 if (hdr_buf[1] != 0) {
436 /* FIXME: need subenclosure support; I've just never
437 * seen a device with subenclosures and it makes the
438 * traversal routines more complex */
439 sdev_printk(KERN_ERR, sdev,
440 "FIXME driver has no support for subenclosures (%d)\n",
Yinghai Lu691b4772008-02-13 16:25:16 -0800441 hdr_buf[1]);
James Bottomley9927c682008-02-03 15:48:56 -0600442 goto err_free;
443 }
444
445 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
446 buf = kzalloc(len, GFP_KERNEL);
447 if (!buf)
448 goto err_free;
449
James Bottomley9927c682008-02-03 15:48:56 -0600450 result = ses_recv_diag(sdev, 1, buf, len);
451 if (result)
452 goto recv_failed;
453
454 types = buf[10];
James Bottomley9927c682008-02-03 15:48:56 -0600455
Yinghai Lu691b4772008-02-13 16:25:16 -0800456 type_ptr = buf + 12 + buf[11];
James Bottomley9927c682008-02-03 15:48:56 -0600457
458 for (i = 0; i < types; i++, type_ptr += 4) {
459 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
460 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
461 components += type_ptr[1];
462 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800463 ses_dev->page1 = buf;
464 ses_dev->page1_len = len;
465 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600466
467 result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
468 if (result)
469 goto recv_failed;
470
471 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
472 buf = kzalloc(len, GFP_KERNEL);
473 if (!buf)
474 goto err_free;
475
476 /* make sure getting page 2 actually works */
477 result = ses_recv_diag(sdev, 2, buf, len);
478 if (result)
479 goto recv_failed;
480 ses_dev->page2 = buf;
481 ses_dev->page2_len = len;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800482 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600483
484 /* The additional information page --- allows us
485 * to match up the devices */
486 result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
Yinghai Lu691b4772008-02-13 16:25:16 -0800487 if (!result) {
James Bottomley9927c682008-02-03 15:48:56 -0600488
Yinghai Lu691b4772008-02-13 16:25:16 -0800489 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
490 buf = kzalloc(len, GFP_KERNEL);
491 if (!buf)
492 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600493
Yinghai Lu691b4772008-02-13 16:25:16 -0800494 result = ses_recv_diag(sdev, 10, buf, len);
495 if (result)
496 goto recv_failed;
497 ses_dev->page10 = buf;
498 ses_dev->page10_len = len;
499 buf = NULL;
500 }
James Bottomley9927c682008-02-03 15:48:56 -0600501
Yinghai Lu7c46c202008-02-10 23:25:25 -0800502 scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
James Bottomley9927c682008-02-03 15:48:56 -0600503 if (!scomp)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800504 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600505
Kay Sievers71610f52008-12-03 22:41:36 +0100506 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
James Bottomley9927c682008-02-03 15:48:56 -0600507 components, &ses_enclosure_callbacks);
508 if (IS_ERR(edev)) {
509 err = PTR_ERR(edev);
510 goto err_free;
511 }
512
513 edev->scratch = ses_dev;
514 for (i = 0; i < components; i++)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800515 edev->component[i].scratch = scomp + i;
James Bottomley9927c682008-02-03 15:48:56 -0600516
517 /* Page 7 for the descriptors is optional */
James Bottomley9927c682008-02-03 15:48:56 -0600518 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
519 if (result)
520 goto simple_populate;
521
Yinghai Lu691b4772008-02-13 16:25:16 -0800522 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
James Bottomley9927c682008-02-03 15:48:56 -0600523 /* add 1 for trailing '\0' we'll use */
524 buf = kzalloc(len + 1, GFP_KERNEL);
Yinghai Lu7c46c202008-02-10 23:25:25 -0800525 if (!buf)
526 goto simple_populate;
James Bottomley9927c682008-02-03 15:48:56 -0600527 result = ses_recv_diag(sdev, 7, buf, len);
528 if (result) {
529 simple_populate:
530 kfree(buf);
531 buf = NULL;
532 desc_ptr = NULL;
533 addl_desc_ptr = NULL;
534 } else {
535 desc_ptr = buf + 8;
536 len = (desc_ptr[2] << 8) + desc_ptr[3];
537 /* skip past overall descriptor */
538 desc_ptr += len + 4;
Yinghai Lu691b4772008-02-13 16:25:16 -0800539 if (ses_dev->page10)
540 addl_desc_ptr = ses_dev->page10 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600541 }
542 type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11];
543 components = 0;
544 for (i = 0; i < types; i++, type_ptr += 4) {
545 for (j = 0; j < type_ptr[1]; j++) {
546 char *name = NULL;
547 struct enclosure_component *ecomp;
548
549 if (desc_ptr) {
Yinghai Lu691b4772008-02-13 16:25:16 -0800550 if (desc_ptr >= buf + page7_len) {
551 desc_ptr = NULL;
552 } else {
553 len = (desc_ptr[2] << 8) + desc_ptr[3];
554 desc_ptr += 4;
555 /* Add trailing zero - pushes into
556 * reserved space */
557 desc_ptr[len] = '\0';
558 name = desc_ptr;
559 }
James Bottomley9927c682008-02-03 15:48:56 -0600560 }
Yinghai Lu691b4772008-02-13 16:25:16 -0800561 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
562 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
563
564 ecomp = enclosure_component_register(edev,
James Bottomley9927c682008-02-03 15:48:56 -0600565 components++,
566 type_ptr[0],
567 name);
Yinghai Lu691b4772008-02-13 16:25:16 -0800568
569 if (!IS_ERR(ecomp) && addl_desc_ptr)
James Bottomley9927c682008-02-03 15:48:56 -0600570 ses_process_descriptor(ecomp,
571 addl_desc_ptr);
James Bottomley9927c682008-02-03 15:48:56 -0600572 }
Yinghai Lu691b4772008-02-13 16:25:16 -0800573 if (desc_ptr)
574 desc_ptr += len;
575
576 if (addl_desc_ptr)
577 addl_desc_ptr += addl_desc_ptr[1] + 2;
578
James Bottomley9927c682008-02-03 15:48:56 -0600579 }
580 }
581 kfree(buf);
582 kfree(hdr_buf);
583
584 /* see if there are any devices matching before
585 * we found the enclosure */
586 shost_for_each_device(tmp_sdev, sdev->host) {
587 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
588 continue;
589 ses_match_to_enclosure(edev, tmp_sdev);
590 }
591
592 return 0;
593
594 recv_failed:
595 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
596 result);
597 err = -ENODEV;
598 err_free:
599 kfree(buf);
Yinghai Lu7c46c202008-02-10 23:25:25 -0800600 kfree(scomp);
James Bottomley9927c682008-02-03 15:48:56 -0600601 kfree(ses_dev->page10);
602 kfree(ses_dev->page2);
603 kfree(ses_dev->page1);
604 err_init_free:
605 kfree(ses_dev);
606 kfree(hdr_buf);
607 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
608 return err;
609}
610
611static int ses_remove(struct device *dev)
612{
613 return 0;
614}
615
Tony Jonesee959b02008-02-22 00:13:36 +0100616static void ses_intf_remove(struct device *cdev,
James Bottomley9927c682008-02-03 15:48:56 -0600617 struct class_interface *intf)
618{
Tony Jonesee959b02008-02-22 00:13:36 +0100619 struct scsi_device *sdev = to_scsi_device(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600620 struct enclosure_device *edev;
621 struct ses_device *ses_dev;
622
623 if (!scsi_device_enclosure(sdev))
624 return;
625
Tony Jonesee959b02008-02-22 00:13:36 +0100626 edev = enclosure_find(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600627 if (!edev)
628 return;
629
630 ses_dev = edev->scratch;
631 edev->scratch = NULL;
632
Yinghai Lu7c46c202008-02-10 23:25:25 -0800633 kfree(ses_dev->page10);
James Bottomley9927c682008-02-03 15:48:56 -0600634 kfree(ses_dev->page1);
635 kfree(ses_dev->page2);
636 kfree(ses_dev);
637
638 kfree(edev->component[0].scratch);
639
Tony Jonesee959b02008-02-22 00:13:36 +0100640 put_device(&edev->edev);
James Bottomley9927c682008-02-03 15:48:56 -0600641 enclosure_unregister(edev);
642}
643
644static struct class_interface ses_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100645 .add_dev = ses_intf_add,
646 .remove_dev = ses_intf_remove,
James Bottomley9927c682008-02-03 15:48:56 -0600647};
648
649static struct scsi_driver ses_template = {
650 .owner = THIS_MODULE,
651 .gendrv = {
652 .name = "ses",
653 .probe = ses_probe,
654 .remove = ses_remove,
655 },
656};
657
658static int __init ses_init(void)
659{
660 int err;
661
662 err = scsi_register_interface(&ses_interface);
663 if (err)
664 return err;
665
666 err = scsi_register_driver(&ses_template.gendrv);
667 if (err)
668 goto out_unreg;
669
670 return 0;
671
672 out_unreg:
673 scsi_unregister_interface(&ses_interface);
674 return err;
675}
676
677static void __exit ses_exit(void)
678{
679 scsi_unregister_driver(&ses_template.gendrv);
680 scsi_unregister_interface(&ses_interface);
681}
682
683module_init(ses_init);
684module_exit(ses_exit);
685
686MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
687
688MODULE_AUTHOR("James Bottomley");
689MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
690MODULE_LICENSE("GPL v2");