blob: 80bfece1a2de74bdcf908a5a381ab5291521741a [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
37struct ses_device {
Yinghai Lu691b4772008-02-13 16:25:16 -080038 unsigned char *page1;
James Bottomley8c3adc72011-03-18 11:24:23 -040039 unsigned char *page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -080040 unsigned char *page2;
41 unsigned char *page10;
James Bottomley9927c682008-02-03 15:48:56 -060042 short page1_len;
James Bottomley8c3adc72011-03-18 11:24:23 -040043 short page1_num_types;
James Bottomley9927c682008-02-03 15:48:56 -060044 short page2_len;
45 short page10_len;
46};
47
48struct ses_component {
49 u64 addr;
50 unsigned char *desc;
51};
52
53static int ses_probe(struct device *dev)
54{
55 struct scsi_device *sdev = to_scsi_device(dev);
56 int err = -ENODEV;
57
58 if (sdev->type != TYPE_ENCLOSURE)
59 goto out;
60
61 err = 0;
62 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
63
64 out:
65 return err;
66}
67
Matthew Wilcoxc95e62c2008-06-23 09:14:31 -060068#define SES_TIMEOUT (30 * HZ)
James Bottomley9927c682008-02-03 15:48:56 -060069#define SES_RETRIES 3
70
71static int ses_recv_diag(struct scsi_device *sdev, int page_code,
72 void *buf, int bufflen)
73{
Yinghai Lu691b4772008-02-13 16:25:16 -080074 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060075 RECEIVE_DIAGNOSTIC,
76 1, /* Set PCV bit */
77 page_code,
78 bufflen >> 8,
79 bufflen & 0xff,
80 0
81 };
82
83 return scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +090084 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -060085}
86
87static int ses_send_diag(struct scsi_device *sdev, int page_code,
88 void *buf, int bufflen)
89{
90 u32 result;
91
Yinghai Lu691b4772008-02-13 16:25:16 -080092 unsigned char cmd[] = {
James Bottomley9927c682008-02-03 15:48:56 -060093 SEND_DIAGNOSTIC,
94 0x10, /* Set PF bit */
95 0,
96 bufflen >> 8,
97 bufflen & 0xff,
98 0
99 };
100
101 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900102 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600103 if (result)
104 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
105 result);
106 return result;
107}
108
109static int ses_set_page2_descriptor(struct enclosure_device *edev,
110 struct enclosure_component *ecomp,
Yinghai Lu691b4772008-02-13 16:25:16 -0800111 unsigned char *desc)
James Bottomley9927c682008-02-03 15:48:56 -0600112{
113 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100114 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600115 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400116 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800117 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600118
119 /* Clear everything */
120 memset(desc_ptr, 0, ses_dev->page2_len - 8);
James Bottomley8c3adc72011-03-18 11:24:23 -0400121 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600122 for (j = 0; j < type_ptr[1]; j++) {
123 desc_ptr += 4;
124 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
125 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
126 continue;
127 if (count++ == descriptor) {
128 memcpy(desc_ptr, desc, 4);
129 /* set select */
130 desc_ptr[0] |= 0x80;
131 /* clear reserved, just in case */
132 desc_ptr[0] &= 0xf0;
133 }
134 }
135 }
136
137 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
138}
139
Yinghai Lu691b4772008-02-13 16:25:16 -0800140static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
James Bottomley9927c682008-02-03 15:48:56 -0600141 struct enclosure_component *ecomp)
142{
143 int i, j, count = 0, descriptor = ecomp->number;
Tony Jonesee959b02008-02-22 00:13:36 +0100144 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600145 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400146 unsigned char *type_ptr = ses_dev->page1_types;
Yinghai Lu691b4772008-02-13 16:25:16 -0800147 unsigned char *desc_ptr = ses_dev->page2 + 8;
James Bottomley9927c682008-02-03 15:48:56 -0600148
149 ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
150
James Bottomley8c3adc72011-03-18 11:24:23 -0400151 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600152 for (j = 0; j < type_ptr[1]; j++) {
153 desc_ptr += 4;
154 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
155 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
156 continue;
157 if (count++ == descriptor)
158 return desc_ptr;
159 }
160 }
161 return NULL;
162}
163
Douglas Gilbert2a350ca2011-06-09 00:27:07 -0400164/* For device slot and array device slot elements, byte 3 bit 6
165 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
166 * code stands these bits are shifted 4 positions right so in
167 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
James Bottomley9927c682008-02-03 15:48:56 -0600168static void ses_get_fault(struct enclosure_device *edev,
169 struct enclosure_component *ecomp)
170{
Yinghai Lu691b4772008-02-13 16:25:16 -0800171 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600172
173 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800174 if (desc)
175 ecomp->fault = (desc[3] & 0x60) >> 4;
James Bottomley9927c682008-02-03 15:48:56 -0600176}
177
178static int ses_set_fault(struct enclosure_device *edev,
179 struct enclosure_component *ecomp,
180 enum enclosure_component_setting val)
181{
Yinghai Lu691b4772008-02-13 16:25:16 -0800182 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600183
184 switch (val) {
185 case ENCLOSURE_SETTING_DISABLED:
186 /* zero is disabled */
187 break;
188 case ENCLOSURE_SETTING_ENABLED:
Douglas Gilbert2a350ca2011-06-09 00:27:07 -0400189 desc[3] = 0x20;
James Bottomley9927c682008-02-03 15:48:56 -0600190 break;
191 default:
192 /* SES doesn't do the SGPIO blink settings */
193 return -EINVAL;
194 }
195
196 return ses_set_page2_descriptor(edev, ecomp, desc);
197}
198
199static void ses_get_status(struct enclosure_device *edev,
200 struct enclosure_component *ecomp)
201{
Yinghai Lu691b4772008-02-13 16:25:16 -0800202 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600203
204 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800205 if (desc)
206 ecomp->status = (desc[0] & 0x0f);
James Bottomley9927c682008-02-03 15:48:56 -0600207}
208
209static void ses_get_locate(struct enclosure_device *edev,
210 struct enclosure_component *ecomp)
211{
Yinghai Lu691b4772008-02-13 16:25:16 -0800212 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600213
214 desc = ses_get_page2_descriptor(edev, ecomp);
Yinghai Lu691b4772008-02-13 16:25:16 -0800215 if (desc)
216 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
James Bottomley9927c682008-02-03 15:48:56 -0600217}
218
219static int ses_set_locate(struct enclosure_device *edev,
220 struct enclosure_component *ecomp,
221 enum enclosure_component_setting val)
222{
Yinghai Lu691b4772008-02-13 16:25:16 -0800223 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600224
225 switch (val) {
226 case ENCLOSURE_SETTING_DISABLED:
227 /* zero is disabled */
228 break;
229 case ENCLOSURE_SETTING_ENABLED:
230 desc[2] = 0x02;
231 break;
232 default:
233 /* SES doesn't do the SGPIO blink settings */
234 return -EINVAL;
235 }
236 return ses_set_page2_descriptor(edev, ecomp, desc);
237}
238
239static int ses_set_active(struct enclosure_device *edev,
240 struct enclosure_component *ecomp,
241 enum enclosure_component_setting val)
242{
Yinghai Lu691b4772008-02-13 16:25:16 -0800243 unsigned char desc[4] = {0 };
James Bottomley9927c682008-02-03 15:48:56 -0600244
245 switch (val) {
246 case ENCLOSURE_SETTING_DISABLED:
247 /* zero is disabled */
248 ecomp->active = 0;
249 break;
250 case ENCLOSURE_SETTING_ENABLED:
251 desc[2] = 0x80;
252 ecomp->active = 1;
253 break;
254 default:
255 /* SES doesn't do the SGPIO blink settings */
256 return -EINVAL;
257 }
258 return ses_set_page2_descriptor(edev, ecomp, desc);
259}
260
261static struct enclosure_component_callbacks ses_enclosure_callbacks = {
262 .get_fault = ses_get_fault,
263 .set_fault = ses_set_fault,
264 .get_status = ses_get_status,
265 .get_locate = ses_get_locate,
266 .set_locate = ses_set_locate,
267 .set_active = ses_set_active,
268};
269
270struct ses_host_edev {
271 struct Scsi_Host *shost;
272 struct enclosure_device *edev;
273};
274
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800275#if 0
James Bottomley9927c682008-02-03 15:48:56 -0600276int ses_match_host(struct enclosure_device *edev, void *data)
277{
278 struct ses_host_edev *sed = data;
279 struct scsi_device *sdev;
280
Tony Jonesee959b02008-02-22 00:13:36 +0100281 if (!scsi_is_sdev_device(edev->edev.parent))
James Bottomley9927c682008-02-03 15:48:56 -0600282 return 0;
283
Tony Jonesee959b02008-02-22 00:13:36 +0100284 sdev = to_scsi_device(edev->edev.parent);
James Bottomley9927c682008-02-03 15:48:56 -0600285
286 if (sdev->host != sed->shost)
287 return 0;
288
289 sed->edev = edev;
290 return 1;
291}
Adrian Bunke0aae1a2009-03-04 12:06:05 -0800292#endif /* 0 */
James Bottomley9927c682008-02-03 15:48:56 -0600293
294static void ses_process_descriptor(struct enclosure_component *ecomp,
295 unsigned char *desc)
296{
297 int eip = desc[0] & 0x10;
298 int invalid = desc[0] & 0x80;
299 enum scsi_protocol proto = desc[0] & 0x0f;
300 u64 addr = 0;
301 struct ses_component *scomp = ecomp->scratch;
302 unsigned char *d;
303
304 scomp->desc = desc;
305
306 if (invalid)
307 return;
308
309 switch (proto) {
310 case SCSI_PROTOCOL_SAS:
311 if (eip)
312 d = desc + 8;
313 else
314 d = desc + 4;
315 /* only take the phy0 addr */
316 addr = (u64)d[12] << 56 |
317 (u64)d[13] << 48 |
318 (u64)d[14] << 40 |
319 (u64)d[15] << 32 |
320 (u64)d[16] << 24 |
321 (u64)d[17] << 16 |
322 (u64)d[18] << 8 |
323 (u64)d[19];
324 break;
325 default:
326 /* FIXME: Need to add more protocols than just SAS */
327 break;
328 }
329 scomp->addr = addr;
330}
331
332struct efd {
333 u64 addr;
334 struct device *dev;
335};
336
337static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
338 void *data)
339{
340 struct efd *efd = data;
341 int i;
342 struct ses_component *scomp;
343
344 if (!edev->component[0].scratch)
345 return 0;
346
347 for (i = 0; i < edev->components; i++) {
348 scomp = edev->component[i].scratch;
349 if (scomp->addr != efd->addr)
350 continue;
351
352 enclosure_add_device(edev, i, efd->dev);
353 return 1;
354 }
355 return 0;
356}
357
James Bottomley21fab1d2009-08-01 00:43:59 +0000358#define INIT_ALLOC_SIZE 32
359
360static void ses_enclosure_data_process(struct enclosure_device *edev,
361 struct scsi_device *sdev,
362 int create)
363{
364 u32 result;
365 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
366 int i, j, page7_len, len, components;
367 struct ses_device *ses_dev = edev->scratch;
James Bottomley8c3adc72011-03-18 11:24:23 -0400368 int types = ses_dev->page1_num_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000369 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
370
371 if (!hdr_buf)
372 goto simple_populate;
373
374 /* re-read page 10 */
375 if (ses_dev->page10)
376 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
377 /* Page 7 for the descriptors is optional */
378 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
379 if (result)
380 goto simple_populate;
381
382 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
383 /* add 1 for trailing '\0' we'll use */
384 buf = kzalloc(len + 1, GFP_KERNEL);
385 if (!buf)
386 goto simple_populate;
387 result = ses_recv_diag(sdev, 7, buf, len);
388 if (result) {
389 simple_populate:
390 kfree(buf);
391 buf = NULL;
392 desc_ptr = NULL;
393 len = 0;
394 page7_len = 0;
395 } else {
396 desc_ptr = buf + 8;
397 len = (desc_ptr[2] << 8) + desc_ptr[3];
398 /* skip past overall descriptor */
399 desc_ptr += len + 4;
James Bottomley21fab1d2009-08-01 00:43:59 +0000400 }
John Hughes877a5592009-11-04 19:01:22 +0100401 if (ses_dev->page10)
402 addl_desc_ptr = ses_dev->page10 + 8;
James Bottomley8c3adc72011-03-18 11:24:23 -0400403 type_ptr = ses_dev->page1_types;
James Bottomley21fab1d2009-08-01 00:43:59 +0000404 components = 0;
405 for (i = 0; i < types; i++, type_ptr += 4) {
406 for (j = 0; j < type_ptr[1]; j++) {
407 char *name = NULL;
408 struct enclosure_component *ecomp;
409
410 if (desc_ptr) {
411 if (desc_ptr >= buf + page7_len) {
412 desc_ptr = NULL;
413 } else {
414 len = (desc_ptr[2] << 8) + desc_ptr[3];
415 desc_ptr += 4;
416 /* Add trailing zero - pushes into
417 * reserved space */
418 desc_ptr[len] = '\0';
419 name = desc_ptr;
420 }
421 }
422 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
423 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
424
425 if (create)
426 ecomp = enclosure_component_register(edev,
427 components++,
428 type_ptr[0],
429 name);
430 else
431 ecomp = &edev->component[components++];
432
433 if (!IS_ERR(ecomp) && addl_desc_ptr)
434 ses_process_descriptor(ecomp,
435 addl_desc_ptr);
436 }
437 if (desc_ptr)
438 desc_ptr += len;
439
440 if (addl_desc_ptr)
441 addl_desc_ptr += addl_desc_ptr[1] + 2;
442
443 }
444 }
445 kfree(buf);
446 kfree(hdr_buf);
447}
448
James Bottomley9927c682008-02-03 15:48:56 -0600449static void ses_match_to_enclosure(struct enclosure_device *edev,
450 struct scsi_device *sdev)
451{
James Bottomley9927c682008-02-03 15:48:56 -0600452 unsigned char *desc;
James Bottomley9927c682008-02-03 15:48:56 -0600453 struct efd efd = {
454 .addr = 0,
455 };
James Bottomley9927c682008-02-03 15:48:56 -0600456
James Bottomley21fab1d2009-08-01 00:43:59 +0000457 ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0);
458
Hannes Reineckec38c0072014-03-15 09:51:51 +0100459 if (!sdev->vpd_pg83_len)
460 return;
James Bottomley671a99c2008-07-29 11:38:25 -0500461
Hannes Reineckec38c0072014-03-15 09:51:51 +0100462 desc = sdev->vpd_pg83 + 4;
463 while (desc < sdev->vpd_pg83 + sdev->vpd_pg83_len) {
James Bottomley9927c682008-02-03 15:48:56 -0600464 enum scsi_protocol proto = desc[0] >> 4;
465 u8 code_set = desc[0] & 0x0f;
466 u8 piv = desc[1] & 0x80;
467 u8 assoc = (desc[1] & 0x30) >> 4;
468 u8 type = desc[1] & 0x0f;
469 u8 len = desc[3];
470
Roel Kluinb3f1f9a2009-02-17 16:59:24 +0100471 if (piv && code_set == 1 && assoc == 1
James Bottomley9927c682008-02-03 15:48:56 -0600472 && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8)
Hannes Reineckec38c0072014-03-15 09:51:51 +0100473 efd.addr = get_unaligned_be64(&desc[4]);
James Bottomley9927c682008-02-03 15:48:56 -0600474
475 desc += len + 4;
476 }
Hannes Reineckec38c0072014-03-15 09:51:51 +0100477 if (efd.addr) {
478 efd.dev = &sdev->sdev_gendev;
James Bottomley9927c682008-02-03 15:48:56 -0600479
Hannes Reineckec38c0072014-03-15 09:51:51 +0100480 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
481 }
James Bottomley9927c682008-02-03 15:48:56 -0600482}
483
Tony Jonesee959b02008-02-22 00:13:36 +0100484static int ses_intf_add(struct device *cdev,
James Bottomley9927c682008-02-03 15:48:56 -0600485 struct class_interface *intf)
486{
Tony Jonesee959b02008-02-22 00:13:36 +0100487 struct scsi_device *sdev = to_scsi_device(cdev->parent);
James Bottomley9927c682008-02-03 15:48:56 -0600488 struct scsi_device *tmp_sdev;
James Bottomley21fab1d2009-08-01 00:43:59 +0000489 unsigned char *buf = NULL, *hdr_buf, *type_ptr;
James Bottomley9927c682008-02-03 15:48:56 -0600490 struct ses_device *ses_dev;
491 u32 result;
James Bottomley21fab1d2009-08-01 00:43:59 +0000492 int i, types, len, components = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600493 int err = -ENOMEM;
James Bottomley8c3adc72011-03-18 11:24:23 -0400494 int num_enclosures;
James Bottomley9927c682008-02-03 15:48:56 -0600495 struct enclosure_device *edev;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800496 struct ses_component *scomp = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600497
498 if (!scsi_device_enclosure(sdev)) {
499 /* not an enclosure, but might be in one */
James Bottomley163f52b2009-08-01 00:39:36 +0000500 struct enclosure_device *prev = NULL;
501
502 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
James Bottomley9927c682008-02-03 15:48:56 -0600503 ses_match_to_enclosure(edev, sdev);
James Bottomley163f52b2009-08-01 00:39:36 +0000504 prev = edev;
James Bottomley9927c682008-02-03 15:48:56 -0600505 }
506 return -ENODEV;
507 }
508
509 /* TYPE_ENCLOSURE prints a message in probe */
510 if (sdev->type != TYPE_ENCLOSURE)
511 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
512
513 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
514 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
515 if (!hdr_buf || !ses_dev)
516 goto err_init_free;
517
518 result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
519 if (result)
520 goto recv_failed;
521
James Bottomley9927c682008-02-03 15:48:56 -0600522 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
523 buf = kzalloc(len, GFP_KERNEL);
524 if (!buf)
525 goto err_free;
526
James Bottomley9927c682008-02-03 15:48:56 -0600527 result = ses_recv_diag(sdev, 1, buf, len);
528 if (result)
529 goto recv_failed;
530
James Bottomley8c3adc72011-03-18 11:24:23 -0400531 types = 0;
James Bottomley9927c682008-02-03 15:48:56 -0600532
James Bottomley8c3adc72011-03-18 11:24:23 -0400533 /* we always have one main enclosure and the rest are referred
534 * to as secondary subenclosures */
535 num_enclosures = buf[1] + 1;
James Bottomley9927c682008-02-03 15:48:56 -0600536
James Bottomley8c3adc72011-03-18 11:24:23 -0400537 /* begin at the enclosure descriptor */
538 type_ptr = buf + 8;
539 /* skip all the enclosure descriptors */
540 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
541 types += type_ptr[2];
542 type_ptr += type_ptr[3] + 4;
543 }
544
545 ses_dev->page1_types = type_ptr;
546 ses_dev->page1_num_types = types;
547
548 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
James Bottomley9927c682008-02-03 15:48:56 -0600549 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
550 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
551 components += type_ptr[1];
552 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800553 ses_dev->page1 = buf;
554 ses_dev->page1_len = len;
555 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600556
557 result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
558 if (result)
559 goto recv_failed;
560
561 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
562 buf = kzalloc(len, GFP_KERNEL);
563 if (!buf)
564 goto err_free;
565
566 /* make sure getting page 2 actually works */
567 result = ses_recv_diag(sdev, 2, buf, len);
568 if (result)
569 goto recv_failed;
570 ses_dev->page2 = buf;
571 ses_dev->page2_len = len;
Yinghai Lu7c46c202008-02-10 23:25:25 -0800572 buf = NULL;
James Bottomley9927c682008-02-03 15:48:56 -0600573
574 /* The additional information page --- allows us
575 * to match up the devices */
576 result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
Yinghai Lu691b4772008-02-13 16:25:16 -0800577 if (!result) {
James Bottomley9927c682008-02-03 15:48:56 -0600578
Yinghai Lu691b4772008-02-13 16:25:16 -0800579 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
580 buf = kzalloc(len, GFP_KERNEL);
581 if (!buf)
582 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600583
Yinghai Lu691b4772008-02-13 16:25:16 -0800584 result = ses_recv_diag(sdev, 10, buf, len);
585 if (result)
586 goto recv_failed;
587 ses_dev->page10 = buf;
588 ses_dev->page10_len = len;
589 buf = NULL;
590 }
Yinghai Lu7c46c202008-02-10 23:25:25 -0800591 scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
James Bottomley9927c682008-02-03 15:48:56 -0600592 if (!scomp)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800593 goto err_free;
James Bottomley9927c682008-02-03 15:48:56 -0600594
Kay Sievers71610f52008-12-03 22:41:36 +0100595 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
James Bottomley9927c682008-02-03 15:48:56 -0600596 components, &ses_enclosure_callbacks);
597 if (IS_ERR(edev)) {
598 err = PTR_ERR(edev);
599 goto err_free;
600 }
601
Julia Lawall9b3a6542010-03-10 15:20:42 -0800602 kfree(hdr_buf);
603
James Bottomley9927c682008-02-03 15:48:56 -0600604 edev->scratch = ses_dev;
605 for (i = 0; i < components; i++)
Yinghai Lu7c46c202008-02-10 23:25:25 -0800606 edev->component[i].scratch = scomp + i;
James Bottomley9927c682008-02-03 15:48:56 -0600607
James Bottomley21fab1d2009-08-01 00:43:59 +0000608 ses_enclosure_data_process(edev, sdev, 1);
James Bottomley9927c682008-02-03 15:48:56 -0600609
610 /* see if there are any devices matching before
611 * we found the enclosure */
612 shost_for_each_device(tmp_sdev, sdev->host) {
613 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
614 continue;
615 ses_match_to_enclosure(edev, tmp_sdev);
616 }
617
618 return 0;
619
620 recv_failed:
621 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
622 result);
623 err = -ENODEV;
624 err_free:
625 kfree(buf);
Yinghai Lu7c46c202008-02-10 23:25:25 -0800626 kfree(scomp);
James Bottomley9927c682008-02-03 15:48:56 -0600627 kfree(ses_dev->page10);
628 kfree(ses_dev->page2);
629 kfree(ses_dev->page1);
630 err_init_free:
631 kfree(ses_dev);
632 kfree(hdr_buf);
633 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
634 return err;
635}
636
637static int ses_remove(struct device *dev)
638{
639 return 0;
640}
641
James Bottomley43d8eb92009-08-01 00:41:22 +0000642static void ses_intf_remove_component(struct scsi_device *sdev)
James Bottomley9927c682008-02-03 15:48:56 -0600643{
James Bottomley43d8eb92009-08-01 00:41:22 +0000644 struct enclosure_device *edev, *prev = NULL;
645
646 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
647 prev = edev;
648 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
649 break;
650 }
651 if (edev)
652 put_device(&edev->edev);
653}
654
655static void ses_intf_remove_enclosure(struct scsi_device *sdev)
656{
James Bottomley9927c682008-02-03 15:48:56 -0600657 struct enclosure_device *edev;
658 struct ses_device *ses_dev;
659
James Bottomley163f52b2009-08-01 00:39:36 +0000660 /* exact match to this enclosure */
James Bottomley43d8eb92009-08-01 00:41:22 +0000661 edev = enclosure_find(&sdev->sdev_gendev, NULL);
James Bottomley9927c682008-02-03 15:48:56 -0600662 if (!edev)
663 return;
664
665 ses_dev = edev->scratch;
666 edev->scratch = NULL;
667
Yinghai Lu7c46c202008-02-10 23:25:25 -0800668 kfree(ses_dev->page10);
James Bottomley9927c682008-02-03 15:48:56 -0600669 kfree(ses_dev->page1);
670 kfree(ses_dev->page2);
671 kfree(ses_dev);
672
673 kfree(edev->component[0].scratch);
674
Tony Jonesee959b02008-02-22 00:13:36 +0100675 put_device(&edev->edev);
James Bottomley9927c682008-02-03 15:48:56 -0600676 enclosure_unregister(edev);
677}
678
James Bottomley43d8eb92009-08-01 00:41:22 +0000679static void ses_intf_remove(struct device *cdev,
680 struct class_interface *intf)
681{
682 struct scsi_device *sdev = to_scsi_device(cdev->parent);
683
684 if (!scsi_device_enclosure(sdev))
685 ses_intf_remove_component(sdev);
686 else
687 ses_intf_remove_enclosure(sdev);
688}
689
James Bottomley9927c682008-02-03 15:48:56 -0600690static struct class_interface ses_interface = {
Tony Jonesee959b02008-02-22 00:13:36 +0100691 .add_dev = ses_intf_add,
692 .remove_dev = ses_intf_remove,
James Bottomley9927c682008-02-03 15:48:56 -0600693};
694
695static struct scsi_driver ses_template = {
696 .owner = THIS_MODULE,
697 .gendrv = {
698 .name = "ses",
699 .probe = ses_probe,
700 .remove = ses_remove,
701 },
702};
703
704static int __init ses_init(void)
705{
706 int err;
707
708 err = scsi_register_interface(&ses_interface);
709 if (err)
710 return err;
711
712 err = scsi_register_driver(&ses_template.gendrv);
713 if (err)
714 goto out_unreg;
715
716 return 0;
717
718 out_unreg:
719 scsi_unregister_interface(&ses_interface);
720 return err;
721}
722
723static void __exit ses_exit(void)
724{
725 scsi_unregister_driver(&ses_template.gendrv);
726 scsi_unregister_interface(&ses_interface);
727}
728
729module_init(ses_init);
730module_exit(ses_exit);
731
732MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
733
734MODULE_AUTHOR("James Bottomley");
735MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
736MODULE_LICENSE("GPL v2");