blob: 7f5a6a86f820fbae8baee63e7de722b480c96462 [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;
38 unsigned char *page2;
39 unsigned char *page10;
James Bottomley9927c682008-02-03 15:48:56 -060040 short page1_len;
41 short page2_len;
42 short page10_len;
43};
44
45struct ses_component {
46 u64 addr;
47 unsigned char *desc;
48};
49
50static int ses_probe(struct device *dev)
51{
52 struct scsi_device *sdev = to_scsi_device(dev);
53 int err = -ENODEV;
54
55 if (sdev->type != TYPE_ENCLOSURE)
56 goto out;
57
58 err = 0;
59 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
60
61 out:
62 return err;
63}
64
Matthew Wilcoxc95e62c2008-06-23 09:14:31 -060065#define SES_TIMEOUT (30 * HZ)
James Bottomley9927c682008-02-03 15:48:56 -060066#define SES_RETRIES 3
67
68static int ses_recv_diag(struct scsi_device *sdev, int page_code,
69 void *buf, int bufflen)
70{
Yinghai Lu691b4772008-02-13 16:25:16 -080071 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060072 RECEIVE_DIAGNOSTIC,
73 1, /* Set PCV bit */
74 page_code,
75 bufflen >> 8,
76 bufflen & 0xff,
77 0
78 };
79
80 return scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +090081 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -060082}
83
84static int ses_send_diag(struct scsi_device *sdev, int page_code,
85 void *buf, int bufflen)
86{
87 u32 result;
88
Yinghai Lu691b4772008-02-13 16:25:16 -080089 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060090 SEND_DIAGNOSTIC,
91 0x10, /* Set PF bit */
92 0,
93 bufflen >> 8,
94 bufflen & 0xff,
95 0
96 };
97
98 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +090099 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600100 if (result)
101 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
102 result);
103 return result;
104}
105
106static int ses_set_page2_descriptor(struct enclosure_device *edev,
107 struct enclosure_component *ecomp,
Yinghai Lu691b4772008-02-13 16:25:16 -0800108 unsigned char *desc)
James Bottomley9927c682008-02-03 15:48:56 -0600109{
110 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100111 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600112 struct ses_device *ses_dev = edev->scratch;
Yinghai Lu691b4772008-02-13 16:25:16 -0800113 unsigned char *type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11];
114 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600115
116 /* Clear everything */
117 memset(desc_ptr, 0, ses_dev->page2_len - 8);
118 for (i = 0; i < ses_dev->page1[10]; i++, type_ptr += 4) {
119 for (j = 0; j < type_ptr[1]; j++) {
120 desc_ptr += 4;
121 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
122 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
123 continue;
124 if (count++ == descriptor) {
125 memcpy(desc_ptr, desc, 4);
126 /* set select */
127 desc_ptr[0] |= 0x80;
128 /* clear reserved, just in case */
129 desc_ptr[0] &= 0xf0;
130 }
131 }
132 }
133
134 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
135}
136
Yinghai Lu691b4772008-02-13 16:25:16 -0800137static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
James Bottomley9927c682008-02-03 15:48:56 -0600138 struct enclosure_component *ecomp)
139{
140 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100141 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600142 struct ses_device *ses_dev = edev->scratch;
Yinghai Lu691b4772008-02-13 16:25:16 -0800143 unsigned char *type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11];
144 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600145
146 ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
147
148 for (i = 0; i < ses_dev->page1[10]; i++, type_ptr += 4) {
149 for (j = 0; j < type_ptr[1]; j++) {
150 desc_ptr += 4;
151 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
152 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
153 continue;
154 if (count++ == descriptor)
155 return desc_ptr;
156 }
157 }
158 return NULL;
159}
160
161static void ses_get_fault(struct enclosure_device *edev,
162 struct enclosure_component *ecomp)
163{
Yinghai Lu691b4772008-02-13 16:25:16 -0800164 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600165
166 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800167 if (desc)
168 ecomp->fault = (desc[3] & 0x60) >> 4;
James Bottomley9927c682008-02-03 15:48:56 -0600169}
170
171static int ses_set_fault(struct enclosure_device *edev,
172 struct enclosure_component *ecomp,
173 enum enclosure_component_setting val)
174{
Yinghai Lu691b4772008-02-13 16:25:16 -0800175 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600176
177 switch (val) {
178 case ENCLOSURE_SETTING_DISABLED:
179 /* zero is disabled */
180 break;
181 case ENCLOSURE_SETTING_ENABLED:
182 desc[2] = 0x02;
183 break;
184 default:
185 /* SES doesn't do the SGPIO blink settings */
186 return -EINVAL;
187 }
188
189 return ses_set_page2_descriptor(edev, ecomp, desc);
190}
191
192static void ses_get_status(struct enclosure_device *edev,
193 struct enclosure_component *ecomp)
194{
Yinghai Lu691b4772008-02-13 16:25:16 -0800195 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600196
197 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800198 if (desc)
199 ecomp->status = (desc[0] & 0x0f);
James Bottomley9927c682008-02-03 15:48:56 -0600200}
201
202static void ses_get_locate(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->locate = (desc[2] & 0x02) ? 1 : 0;
James Bottomley9927c682008-02-03 15:48:56 -0600210}
211
212static int ses_set_locate(struct enclosure_device *edev,
213 struct enclosure_component *ecomp,
214 enum enclosure_component_setting val)
215{
Yinghai Lu691b4772008-02-13 16:25:16 -0800216 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600217
218 switch (val) {
219 case ENCLOSURE_SETTING_DISABLED:
220 /* zero is disabled */
221 break;
222 case ENCLOSURE_SETTING_ENABLED:
223 desc[2] = 0x02;
224 break;
225 default:
226 /* SES doesn't do the SGPIO blink settings */
227 return -EINVAL;
228 }
229 return ses_set_page2_descriptor(edev, ecomp, desc);
230}
231
232static int ses_set_active(struct enclosure_device *edev,
233 struct enclosure_component *ecomp,
234 enum enclosure_component_setting val)
235{
Yinghai Lu691b4772008-02-13 16:25:16 -0800236 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600237
238 switch (val) {
239 case ENCLOSURE_SETTING_DISABLED:
240 /* zero is disabled */
241 ecomp->active = 0;
242 break;
243 case ENCLOSURE_SETTING_ENABLED:
244 desc[2] = 0x80;
245 ecomp->active = 1;
246 break;
247 default:
248 /* SES doesn't do the SGPIO blink settings */
249 return -EINVAL;
250 }
251 return ses_set_page2_descriptor(edev, ecomp, desc);
252}
253
254static struct enclosure_component_callbacks ses_enclosure_callbacks = {
255 .get_fault = ses_get_fault,
256 .set_fault = ses_set_fault,
257 .get_status = ses_get_status,
258 .get_locate = ses_get_locate,
259 .set_locate = ses_set_locate,
260 .set_active = ses_set_active,
261};
262
263struct ses_host_edev {
264 struct Scsi_Host *shost;
265 struct enclosure_device *edev;
266};
267
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800268#if 0
James Bottomley9927c682008-02-03 15:48:56 -0600269int ses_match_host(struct enclosure_device *edev, void *data)
270{
271 struct ses_host_edev *sed = data;
272 struct scsi_device *sdev;
273
Tony Jonesee959b02008-02-22 00:13:36 +0100274 if (!scsi_is_sdev_device(edev->edev.parent))
James Bottomley9927c682008-02-03 15:48:56 -0600275 return 0;
276
Tony Jonesee959b02008-02-22 00:13:36 +0100277 sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600278
279 if (sdev->host != sed->shost)
280 return 0;
281
282 sed->edev = edev;
283 return 1;
284}
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800285#endif /* 0 */
James Bottomley9927c682008-02-03 15:48:56 -0600286
287static void ses_process_descriptor(struct enclosure_component *ecomp,
288 unsigned char *desc)
289{
290 int eip = desc[0] & 0x10;
291 int invalid = desc[0] & 0x80;
292 enum scsi_protocol proto = desc[0] & 0x0f;
293 u64 addr = 0;
294 struct ses_component *scomp = ecomp->scratch;
295 unsigned char *d;
296
297 scomp->desc = desc;
298
299 if (invalid)
300 return;
301
302 switch (proto) {
303 case SCSI_PROTOCOL_SAS:
304 if (eip)
305 d = desc + 8;
306 else
307 d = desc + 4;
308 /* only take the phy0 addr */
309 addr = (u64)d[12] << 56 |
310 (u64)d[13] << 48 |
311 (u64)d[14] << 40 |
312 (u64)d[15] << 32 |
313 (u64)d[16] << 24 |
314 (u64)d[17] << 16 |
315 (u64)d[18] << 8 |
316 (u64)d[19];
317 break;
318 default:
319 /* FIXME: Need to add more protocols than just SAS */
320 break;
321 }
322 scomp->addr = addr;
323}
324
325struct efd {
326 u64 addr;
327 struct device *dev;
328};
329
330static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
331 void *data)
332{
333 struct efd *efd = data;
334 int i;
335 struct ses_component *scomp;
336
337 if (!edev->component[0].scratch)
338 return 0;
339
340 for (i = 0; i < edev->components; i++) {
341 scomp = edev->component[i].scratch;
342 if (scomp->addr != efd->addr)
343 continue;
344
345 enclosure_add_device(edev, i, efd->dev);
346 return 1;
347 }
348 return 0;
349}
350
James Bottomley21fab1d2009-08-01 00:43:59 +0000351#define INIT_ALLOC_SIZE 32
352
353static void ses_enclosure_data_process(struct enclosure_device *edev,
354 struct scsi_device *sdev,
355 int create)
356{
357 u32 result;
358 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
359 int i, j, page7_len, len, components;
360 struct ses_device *ses_dev = edev->scratch;
361 int types = ses_dev->page1[10];
362 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
363
364 if (!hdr_buf)
365 goto simple_populate;
366
367 /* re-read page 10 */
368 if (ses_dev->page10)
369 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
370 /* Page 7 for the descriptors is optional */
371 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
372 if (result)
373 goto simple_populate;
374
375 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
376 /* add 1 for trailing '\0' we'll use */
377 buf = kzalloc(len + 1, GFP_KERNEL);
378 if (!buf)
379 goto simple_populate;
380 result = ses_recv_diag(sdev, 7, buf, len);
381 if (result) {
382 simple_populate:
383 kfree(buf);
384 buf = NULL;
385 desc_ptr = NULL;
386 len = 0;
387 page7_len = 0;
388 } else {
389 desc_ptr = buf + 8;
390 len = (desc_ptr[2] << 8) + desc_ptr[3];
391 /* skip past overall descriptor */
392 desc_ptr += len + 4;
393 if (ses_dev->page10)
394 addl_desc_ptr = ses_dev->page10 + 8;
395 }
396 type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11];
397 components = 0;
398 for (i = 0; i < types; i++, type_ptr += 4) {
399 for (j = 0; j < type_ptr[1]; j++) {
400 char *name = NULL;
401 struct enclosure_component *ecomp;
402
403 if (desc_ptr) {
404 if (desc_ptr >= buf + page7_len) {
405 desc_ptr = NULL;
406 } else {
407 len = (desc_ptr[2] << 8) + desc_ptr[3];
408 desc_ptr += 4;
409 /* Add trailing zero - pushes into
410 * reserved space */
411 desc_ptr[len] = '\0';
412 name = desc_ptr;
413 }
414 }
415 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
416 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
417
418 if (create)
419 ecomp = enclosure_component_register(edev,
420 components++,
421 type_ptr[0],
422 name);
423 else
424 ecomp = &edev->component[components++];
425
426 if (!IS_ERR(ecomp) && addl_desc_ptr)
427 ses_process_descriptor(ecomp,
428 addl_desc_ptr);
429 }
430 if (desc_ptr)
431 desc_ptr += len;
432
433 if (addl_desc_ptr)
434 addl_desc_ptr += addl_desc_ptr[1] + 2;
435
436 }
437 }
438 kfree(buf);
439 kfree(hdr_buf);
440}
441
James Bottomley9927c682008-02-03 15:48:56 -0600442static void ses_match_to_enclosure(struct enclosure_device *edev,
443 struct scsi_device *sdev)
444{
Matthew Wilcox40c34602008-12-31 12:11:17 -0700445 unsigned char *buf;
James Bottomley9927c682008-02-03 15:48:56 -0600446 unsigned char *desc;
Matthew Wilcox40c34602008-12-31 12:11:17 -0700447 unsigned int vpd_len;
James Bottomley9927c682008-02-03 15:48:56 -0600448 struct efd efd = {
449 .addr = 0,
450 };
James Bottomley9927c682008-02-03 15:48:56 -0600451
James Bottomleye3deec02009-11-03 12:33:07 -0600452 buf = kmalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
453 if (!buf || scsi_get_vpd_page(sdev, 0x83, buf, INIT_ALLOC_SIZE))
454 goto free;
James Bottomley9927c682008-02-03 15:48:56 -0600455
James Bottomley21fab1d2009-08-01 00:43:59 +0000456 ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0);
457
Matthew Wilcox40c34602008-12-31 12:11:17 -0700458 vpd_len = ((buf[2] << 8) | buf[3]) + 4;
James Bottomleye3deec02009-11-03 12:33:07 -0600459 kfree(buf);
460 buf = kmalloc(vpd_len, GFP_KERNEL);
461 if (!buf ||scsi_get_vpd_page(sdev, 0x83, buf, vpd_len))
462 goto free;
James Bottomley671a99c2008-07-29 11:38:25 -0500463
James Bottomley9927c682008-02-03 15:48:56 -0600464 desc = buf + 4;
James Bottomley671a99c2008-07-29 11:38:25 -0500465 while (desc < buf + vpd_len) {
James Bottomley9927c682008-02-03 15:48:56 -0600466 enum scsi_protocol proto = desc[0] >> 4;
467 u8 code_set = desc[0] & 0x0f;
468 u8 piv = desc[1] & 0x80;
469 u8 assoc = (desc[1] & 0x30) >> 4;
470 u8 type = desc[1] & 0x0f;
471 u8 len = desc[3];
472
Roel Kluinb3f1f9a2009-02-17 16:59:24 +0100473 if (piv && code_set == 1 && assoc == 1
James Bottomley9927c682008-02-03 15:48:56 -0600474 && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8)
475 efd.addr = (u64)desc[4] << 56 |
476 (u64)desc[5] << 48 |
477 (u64)desc[6] << 40 |
478 (u64)desc[7] << 32 |
479 (u64)desc[8] << 24 |
480 (u64)desc[9] << 16 |
481 (u64)desc[10] << 8 |
482 (u64)desc[11];
483
484 desc += len + 4;
485 }
486 if (!efd.addr)
487 goto free;
488
489 efd.dev = &sdev->sdev_gendev;
490
491 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
492 free:
493 kfree(buf);
494}
495
Tony Jonesee959b02008-02-22 00:13:36 +0100496static int ses_intf_add(struct device *cdev,
James Bottomley9927c682008-02-03 15:48:56 -0600497 struct class_interface *intf)
498{
Tony Jonesee959b02008-02-22 00:13:36 +0100499 struct scsi_device *sdev = to_scsi_device(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600500 struct scsi_device *tmp_sdev;
James Bottomley21fab1d2009-08-01 00:43:59 +0000501 unsigned char *buf = NULL, *hdr_buf, *type_ptr;
James Bottomley9927c682008-02-03 15:48:56 -0600502 struct ses_device *ses_dev;
503 u32 result;
James Bottomley21fab1d2009-08-01 00:43:59 +0000504 int i, types, len, components = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600505 int err = -ENOMEM;
506 struct enclosure_device *edev;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800507 struct ses_component *scomp = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600508
509 if (!scsi_device_enclosure(sdev)) {
510 /* not an enclosure, but might be in one */
James Bottomley163f52b2009-08-01 00:39:36 +0000511 struct enclosure_device *prev = NULL;
512
513 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
James Bottomley9927c682008-02-03 15:48:56 -0600514 ses_match_to_enclosure(edev, sdev);
James Bottomley163f52b2009-08-01 00:39:36 +0000515 prev = edev;
James Bottomley9927c682008-02-03 15:48:56 -0600516 }
517 return -ENODEV;
518 }
519
520 /* TYPE_ENCLOSURE prints a message in probe */
521 if (sdev->type != TYPE_ENCLOSURE)
522 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
523
524 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
525 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
526 if (!hdr_buf || !ses_dev)
527 goto err_init_free;
528
529 result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
530 if (result)
531 goto recv_failed;
532
533 if (hdr_buf[1] != 0) {
534 /* FIXME: need subenclosure support; I've just never
535 * seen a device with subenclosures and it makes the
536 * traversal routines more complex */
537 sdev_printk(KERN_ERR, sdev,
538 "FIXME driver has no support for subenclosures (%d)\n",
Yinghai Lu691b4772008-02-13 16:25:16 -0800539 hdr_buf[1]);
James Bottomley9927c682008-02-03 15:48:56 -0600540 goto err_free;
541 }
542
543 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
544 buf = kzalloc(len, GFP_KERNEL);
545 if (!buf)
546 goto err_free;
547
James Bottomley9927c682008-02-03 15:48:56 -0600548 result = ses_recv_diag(sdev, 1, buf, len);
549 if (result)
550 goto recv_failed;
551
552 types = buf[10];
James Bottomley9927c682008-02-03 15:48:56 -0600553
Yinghai Lu691b4772008-02-13 16:25:16 -0800554 type_ptr = buf + 12 + buf[11];
James Bottomley9927c682008-02-03 15:48:56 -0600555
556 for (i = 0; i < types; i++, type_ptr += 4) {
557 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
558 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
559 components += type_ptr[1];
560 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800561 ses_dev->page1 = buf;
562 ses_dev->page1_len = len;
563 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600564
565 result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
566 if (result)
567 goto recv_failed;
568
569 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
570 buf = kzalloc(len, GFP_KERNEL);
571 if (!buf)
572 goto err_free;
573
574 /* make sure getting page 2 actually works */
575 result = ses_recv_diag(sdev, 2, buf, len);
576 if (result)
577 goto recv_failed;
578 ses_dev->page2 = buf;
579 ses_dev->page2_len = len;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800580 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600581
582 /* The additional information page --- allows us
583 * to match up the devices */
584 result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
Yinghai Lu691b4772008-02-13 16:25:16 -0800585 if (!result) {
James Bottomley9927c682008-02-03 15:48:56 -0600586
Yinghai Lu691b4772008-02-13 16:25:16 -0800587 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
588 buf = kzalloc(len, GFP_KERNEL);
589 if (!buf)
590 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600591
Yinghai Lu691b4772008-02-13 16:25:16 -0800592 result = ses_recv_diag(sdev, 10, buf, len);
593 if (result)
594 goto recv_failed;
595 ses_dev->page10 = buf;
596 ses_dev->page10_len = len;
597 buf = NULL;
598 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800599 scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
James Bottomley9927c682008-02-03 15:48:56 -0600600 if (!scomp)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800601 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600602
Kay Sievers71610f52008-12-03 22:41:36 +0100603 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
James Bottomley9927c682008-02-03 15:48:56 -0600604 components, &ses_enclosure_callbacks);
605 if (IS_ERR(edev)) {
606 err = PTR_ERR(edev);
607 goto err_free;
608 }
609
Julia Lawall9b3a6542010-03-10 15:20:42 -0800610 kfree(hdr_buf);
611
James Bottomley9927c682008-02-03 15:48:56 -0600612 edev->scratch = ses_dev;
613 for (i = 0; i < components; i++)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800614 edev->component[i].scratch = scomp + i;
James Bottomley9927c682008-02-03 15:48:56 -0600615
James Bottomley21fab1d2009-08-01 00:43:59 +0000616 ses_enclosure_data_process(edev, sdev, 1);
James Bottomley9927c682008-02-03 15:48:56 -0600617
618 /* see if there are any devices matching before
619 * we found the enclosure */
620 shost_for_each_device(tmp_sdev, sdev->host) {
621 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
622 continue;
623 ses_match_to_enclosure(edev, tmp_sdev);
624 }
625
626 return 0;
627
628 recv_failed:
629 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
630 result);
631 err = -ENODEV;
632 err_free:
633 kfree(buf);
Yinghai Lu7c46c202008-02-10 23:25:25 -0800634 kfree(scomp);
James Bottomley9927c682008-02-03 15:48:56 -0600635 kfree(ses_dev->page10);
636 kfree(ses_dev->page2);
637 kfree(ses_dev->page1);
638 err_init_free:
639 kfree(ses_dev);
640 kfree(hdr_buf);
641 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
642 return err;
643}
644
645static int ses_remove(struct device *dev)
646{
647 return 0;
648}
649
James Bottomley43d8eb92009-08-01 00:41:22 +0000650static void ses_intf_remove_component(struct scsi_device *sdev)
James Bottomley9927c682008-02-03 15:48:56 -0600651{
James Bottomley43d8eb92009-08-01 00:41:22 +0000652 struct enclosure_device *edev, *prev = NULL;
653
654 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
655 prev = edev;
656 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
657 break;
658 }
659 if (edev)
660 put_device(&edev->edev);
661}
662
663static void ses_intf_remove_enclosure(struct scsi_device *sdev)
664{
James Bottomley9927c682008-02-03 15:48:56 -0600665 struct enclosure_device *edev;
666 struct ses_device *ses_dev;
667
James Bottomley163f52b2009-08-01 00:39:36 +0000668 /* exact match to this enclosure */
James Bottomley43d8eb92009-08-01 00:41:22 +0000669 edev = enclosure_find(&sdev->sdev_gendev, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600670 if (!edev)
671 return;
672
673 ses_dev = edev->scratch;
674 edev->scratch = NULL;
675
Yinghai Lu7c46c202008-02-10 23:25:25 -0800676 kfree(ses_dev->page10);
James Bottomley9927c682008-02-03 15:48:56 -0600677 kfree(ses_dev->page1);
678 kfree(ses_dev->page2);
679 kfree(ses_dev);
680
681 kfree(edev->component[0].scratch);
682
Tony Jonesee959b02008-02-22 00:13:36 +0100683 put_device(&edev->edev);
James Bottomley9927c682008-02-03 15:48:56 -0600684 enclosure_unregister(edev);
685}
686
James Bottomley43d8eb92009-08-01 00:41:22 +0000687static void ses_intf_remove(struct device *cdev,
688 struct class_interface *intf)
689{
690 struct scsi_device *sdev = to_scsi_device(cdev->parent);
691
692 if (!scsi_device_enclosure(sdev))
693 ses_intf_remove_component(sdev);
694 else
695 ses_intf_remove_enclosure(sdev);
696}
697
James Bottomley9927c682008-02-03 15:48:56 -0600698static struct class_interface ses_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100699 .add_dev = ses_intf_add,
700 .remove_dev = ses_intf_remove,
James Bottomley9927c682008-02-03 15:48:56 -0600701};
702
703static struct scsi_driver ses_template = {
704 .owner = THIS_MODULE,
705 .gendrv = {
706 .name = "ses",
707 .probe = ses_probe,
708 .remove = ses_remove,
709 },
710};
711
712static int __init ses_init(void)
713{
714 int err;
715
716 err = scsi_register_interface(&ses_interface);
717 if (err)
718 return err;
719
720 err = scsi_register_driver(&ses_template.gendrv);
721 if (err)
722 goto out_unreg;
723
724 return 0;
725
726 out_unreg:
727 scsi_unregister_interface(&ses_interface);
728 return err;
729}
730
731static void __exit ses_exit(void)
732{
733 scsi_unregister_driver(&ses_template.gendrv);
734 scsi_unregister_interface(&ses_interface);
735}
736
737module_init(ses_init);
738module_exit(ses_exit);
739
740MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
741
742MODULE_AUTHOR("James Bottomley");
743MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
744MODULE_LICENSE("GPL v2");