James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
| 35 | struct ses_device { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 36 | unsigned char *page1; |
| 37 | unsigned char *page2; |
| 38 | unsigned char *page10; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 39 | short page1_len; |
| 40 | short page2_len; |
| 41 | short page10_len; |
| 42 | }; |
| 43 | |
| 44 | struct ses_component { |
| 45 | u64 addr; |
| 46 | unsigned char *desc; |
| 47 | }; |
| 48 | |
| 49 | static 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 Wilcox | c95e62c | 2008-06-23 09:14:31 -0600 | [diff] [blame] | 64 | #define SES_TIMEOUT (30 * HZ) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 65 | #define SES_RETRIES 3 |
| 66 | |
| 67 | static int ses_recv_diag(struct scsi_device *sdev, int page_code, |
| 68 | void *buf, int bufflen) |
| 69 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 70 | unsigned char cmd[] = { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 71 | 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 Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 80 | NULL, SES_TIMEOUT, SES_RETRIES, NULL); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static int ses_send_diag(struct scsi_device *sdev, int page_code, |
| 84 | void *buf, int bufflen) |
| 85 | { |
| 86 | u32 result; |
| 87 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 88 | unsigned char cmd[] = { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 89 | 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 Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 98 | NULL, SES_TIMEOUT, SES_RETRIES, NULL); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 99 | if (result) |
| 100 | sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n", |
| 101 | result); |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | static int ses_set_page2_descriptor(struct enclosure_device *edev, |
| 106 | struct enclosure_component *ecomp, |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 107 | unsigned char *desc) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 108 | { |
| 109 | int i, j, count = 0, descriptor = ecomp->number; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 110 | struct scsi_device *sdev = to_scsi_device(edev->edev.parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 111 | struct ses_device *ses_dev = edev->scratch; |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 112 | unsigned char *type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11]; |
| 113 | unsigned char *desc_ptr = ses_dev->page2 + 8; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 114 | |
| 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 Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 136 | static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 137 | struct enclosure_component *ecomp) |
| 138 | { |
| 139 | int i, j, count = 0, descriptor = ecomp->number; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 140 | struct scsi_device *sdev = to_scsi_device(edev->edev.parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 141 | struct ses_device *ses_dev = edev->scratch; |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 142 | unsigned char *type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11]; |
| 143 | unsigned char *desc_ptr = ses_dev->page2 + 8; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 144 | |
| 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 | |
| 160 | static void ses_get_fault(struct enclosure_device *edev, |
| 161 | struct enclosure_component *ecomp) |
| 162 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 163 | unsigned char *desc; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 164 | |
| 165 | desc = ses_get_page2_descriptor(edev, ecomp); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 166 | if (desc) |
| 167 | ecomp->fault = (desc[3] & 0x60) >> 4; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static int ses_set_fault(struct enclosure_device *edev, |
| 171 | struct enclosure_component *ecomp, |
| 172 | enum enclosure_component_setting val) |
| 173 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 174 | unsigned char desc[4] = {0 }; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 175 | |
| 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 | |
| 191 | static void ses_get_status(struct enclosure_device *edev, |
| 192 | struct enclosure_component *ecomp) |
| 193 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 194 | unsigned char *desc; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 195 | |
| 196 | desc = ses_get_page2_descriptor(edev, ecomp); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 197 | if (desc) |
| 198 | ecomp->status = (desc[0] & 0x0f); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void ses_get_locate(struct enclosure_device *edev, |
| 202 | struct enclosure_component *ecomp) |
| 203 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 204 | unsigned char *desc; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 205 | |
| 206 | desc = ses_get_page2_descriptor(edev, ecomp); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 207 | if (desc) |
| 208 | ecomp->locate = (desc[2] & 0x02) ? 1 : 0; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static int ses_set_locate(struct enclosure_device *edev, |
| 212 | struct enclosure_component *ecomp, |
| 213 | enum enclosure_component_setting val) |
| 214 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 215 | unsigned char desc[4] = {0 }; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 216 | |
| 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 | |
| 231 | static int ses_set_active(struct enclosure_device *edev, |
| 232 | struct enclosure_component *ecomp, |
| 233 | enum enclosure_component_setting val) |
| 234 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 235 | unsigned char desc[4] = {0 }; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 236 | |
| 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 | |
| 253 | static 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 | |
| 262 | struct ses_host_edev { |
| 263 | struct Scsi_Host *shost; |
| 264 | struct enclosure_device *edev; |
| 265 | }; |
| 266 | |
Adrian Bunk | e0aae1a | 2009-03-04 12:06:05 -0800 | [diff] [blame] | 267 | #if 0 |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 268 | int ses_match_host(struct enclosure_device *edev, void *data) |
| 269 | { |
| 270 | struct ses_host_edev *sed = data; |
| 271 | struct scsi_device *sdev; |
| 272 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 273 | if (!scsi_is_sdev_device(edev->edev.parent)) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 274 | return 0; |
| 275 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 276 | sdev = to_scsi_device(edev->edev.parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 277 | |
| 278 | if (sdev->host != sed->shost) |
| 279 | return 0; |
| 280 | |
| 281 | sed->edev = edev; |
| 282 | return 1; |
| 283 | } |
Adrian Bunk | e0aae1a | 2009-03-04 12:06:05 -0800 | [diff] [blame] | 284 | #endif /* 0 */ |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 285 | |
| 286 | static void ses_process_descriptor(struct enclosure_component *ecomp, |
| 287 | unsigned char *desc) |
| 288 | { |
| 289 | int eip = desc[0] & 0x10; |
| 290 | int invalid = desc[0] & 0x80; |
| 291 | enum scsi_protocol proto = desc[0] & 0x0f; |
| 292 | u64 addr = 0; |
| 293 | struct ses_component *scomp = ecomp->scratch; |
| 294 | unsigned char *d; |
| 295 | |
| 296 | scomp->desc = desc; |
| 297 | |
| 298 | if (invalid) |
| 299 | return; |
| 300 | |
| 301 | switch (proto) { |
| 302 | case SCSI_PROTOCOL_SAS: |
| 303 | if (eip) |
| 304 | d = desc + 8; |
| 305 | else |
| 306 | d = desc + 4; |
| 307 | /* only take the phy0 addr */ |
| 308 | addr = (u64)d[12] << 56 | |
| 309 | (u64)d[13] << 48 | |
| 310 | (u64)d[14] << 40 | |
| 311 | (u64)d[15] << 32 | |
| 312 | (u64)d[16] << 24 | |
| 313 | (u64)d[17] << 16 | |
| 314 | (u64)d[18] << 8 | |
| 315 | (u64)d[19]; |
| 316 | break; |
| 317 | default: |
| 318 | /* FIXME: Need to add more protocols than just SAS */ |
| 319 | break; |
| 320 | } |
| 321 | scomp->addr = addr; |
| 322 | } |
| 323 | |
| 324 | struct efd { |
| 325 | u64 addr; |
| 326 | struct device *dev; |
| 327 | }; |
| 328 | |
| 329 | static int ses_enclosure_find_by_addr(struct enclosure_device *edev, |
| 330 | void *data) |
| 331 | { |
| 332 | struct efd *efd = data; |
| 333 | int i; |
| 334 | struct ses_component *scomp; |
| 335 | |
| 336 | if (!edev->component[0].scratch) |
| 337 | return 0; |
| 338 | |
| 339 | for (i = 0; i < edev->components; i++) { |
| 340 | scomp = edev->component[i].scratch; |
| 341 | if (scomp->addr != efd->addr) |
| 342 | continue; |
| 343 | |
| 344 | enclosure_add_device(edev, i, efd->dev); |
| 345 | return 1; |
| 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 350 | #define INIT_ALLOC_SIZE 32 |
| 351 | |
| 352 | static void ses_enclosure_data_process(struct enclosure_device *edev, |
| 353 | struct scsi_device *sdev, |
| 354 | int create) |
| 355 | { |
| 356 | u32 result; |
| 357 | unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL; |
| 358 | int i, j, page7_len, len, components; |
| 359 | struct ses_device *ses_dev = edev->scratch; |
| 360 | int types = ses_dev->page1[10]; |
| 361 | unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL); |
| 362 | |
| 363 | if (!hdr_buf) |
| 364 | goto simple_populate; |
| 365 | |
| 366 | /* re-read page 10 */ |
| 367 | if (ses_dev->page10) |
| 368 | ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len); |
| 369 | /* Page 7 for the descriptors is optional */ |
| 370 | result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE); |
| 371 | if (result) |
| 372 | goto simple_populate; |
| 373 | |
| 374 | page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 375 | /* add 1 for trailing '\0' we'll use */ |
| 376 | buf = kzalloc(len + 1, GFP_KERNEL); |
| 377 | if (!buf) |
| 378 | goto simple_populate; |
| 379 | result = ses_recv_diag(sdev, 7, buf, len); |
| 380 | if (result) { |
| 381 | simple_populate: |
| 382 | kfree(buf); |
| 383 | buf = NULL; |
| 384 | desc_ptr = NULL; |
| 385 | len = 0; |
| 386 | page7_len = 0; |
| 387 | } else { |
| 388 | desc_ptr = buf + 8; |
| 389 | len = (desc_ptr[2] << 8) + desc_ptr[3]; |
| 390 | /* skip past overall descriptor */ |
| 391 | desc_ptr += len + 4; |
| 392 | if (ses_dev->page10) |
| 393 | addl_desc_ptr = ses_dev->page10 + 8; |
| 394 | } |
| 395 | type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11]; |
| 396 | components = 0; |
| 397 | for (i = 0; i < types; i++, type_ptr += 4) { |
| 398 | for (j = 0; j < type_ptr[1]; j++) { |
| 399 | char *name = NULL; |
| 400 | struct enclosure_component *ecomp; |
| 401 | |
| 402 | if (desc_ptr) { |
| 403 | if (desc_ptr >= buf + page7_len) { |
| 404 | desc_ptr = NULL; |
| 405 | } else { |
| 406 | len = (desc_ptr[2] << 8) + desc_ptr[3]; |
| 407 | desc_ptr += 4; |
| 408 | /* Add trailing zero - pushes into |
| 409 | * reserved space */ |
| 410 | desc_ptr[len] = '\0'; |
| 411 | name = desc_ptr; |
| 412 | } |
| 413 | } |
| 414 | if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || |
| 415 | type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) { |
| 416 | |
| 417 | if (create) |
| 418 | ecomp = enclosure_component_register(edev, |
| 419 | components++, |
| 420 | type_ptr[0], |
| 421 | name); |
| 422 | else |
| 423 | ecomp = &edev->component[components++]; |
| 424 | |
| 425 | if (!IS_ERR(ecomp) && addl_desc_ptr) |
| 426 | ses_process_descriptor(ecomp, |
| 427 | addl_desc_ptr); |
| 428 | } |
| 429 | if (desc_ptr) |
| 430 | desc_ptr += len; |
| 431 | |
| 432 | if (addl_desc_ptr) |
| 433 | addl_desc_ptr += addl_desc_ptr[1] + 2; |
| 434 | |
| 435 | } |
| 436 | } |
| 437 | kfree(buf); |
| 438 | kfree(hdr_buf); |
| 439 | } |
| 440 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 441 | static void ses_match_to_enclosure(struct enclosure_device *edev, |
| 442 | struct scsi_device *sdev) |
| 443 | { |
Matthew Wilcox | 40c3460 | 2008-12-31 12:11:17 -0700 | [diff] [blame] | 444 | unsigned char *buf; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 445 | unsigned char *desc; |
Matthew Wilcox | 40c3460 | 2008-12-31 12:11:17 -0700 | [diff] [blame] | 446 | unsigned int vpd_len; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 447 | struct efd efd = { |
| 448 | .addr = 0, |
| 449 | }; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 450 | |
Matthew Wilcox | 40c3460 | 2008-12-31 12:11:17 -0700 | [diff] [blame] | 451 | buf = scsi_get_vpd_page(sdev, 0x83); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 452 | if (!buf) |
| 453 | return; |
| 454 | |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 455 | ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0); |
| 456 | |
Matthew Wilcox | 40c3460 | 2008-12-31 12:11:17 -0700 | [diff] [blame] | 457 | vpd_len = ((buf[2] << 8) | buf[3]) + 4; |
James Bottomley | 671a99c | 2008-07-29 11:38:25 -0500 | [diff] [blame] | 458 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 459 | desc = buf + 4; |
James Bottomley | 671a99c | 2008-07-29 11:38:25 -0500 | [diff] [blame] | 460 | while (desc < buf + vpd_len) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 461 | enum scsi_protocol proto = desc[0] >> 4; |
| 462 | u8 code_set = desc[0] & 0x0f; |
| 463 | u8 piv = desc[1] & 0x80; |
| 464 | u8 assoc = (desc[1] & 0x30) >> 4; |
| 465 | u8 type = desc[1] & 0x0f; |
| 466 | u8 len = desc[3]; |
| 467 | |
Roel Kluin | b3f1f9a | 2009-02-17 16:59:24 +0100 | [diff] [blame] | 468 | if (piv && code_set == 1 && assoc == 1 |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 469 | && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8) |
| 470 | efd.addr = (u64)desc[4] << 56 | |
| 471 | (u64)desc[5] << 48 | |
| 472 | (u64)desc[6] << 40 | |
| 473 | (u64)desc[7] << 32 | |
| 474 | (u64)desc[8] << 24 | |
| 475 | (u64)desc[9] << 16 | |
| 476 | (u64)desc[10] << 8 | |
| 477 | (u64)desc[11]; |
| 478 | |
| 479 | desc += len + 4; |
| 480 | } |
| 481 | if (!efd.addr) |
| 482 | goto free; |
| 483 | |
| 484 | efd.dev = &sdev->sdev_gendev; |
| 485 | |
| 486 | enclosure_for_each_device(ses_enclosure_find_by_addr, &efd); |
| 487 | free: |
| 488 | kfree(buf); |
| 489 | } |
| 490 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 491 | static int ses_intf_add(struct device *cdev, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 492 | struct class_interface *intf) |
| 493 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 494 | struct scsi_device *sdev = to_scsi_device(cdev->parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 495 | struct scsi_device *tmp_sdev; |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 496 | unsigned char *buf = NULL, *hdr_buf, *type_ptr; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 497 | struct ses_device *ses_dev; |
| 498 | u32 result; |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 499 | int i, types, len, components = 0; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 500 | int err = -ENOMEM; |
| 501 | struct enclosure_device *edev; |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 502 | struct ses_component *scomp = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 503 | |
| 504 | if (!scsi_device_enclosure(sdev)) { |
| 505 | /* not an enclosure, but might be in one */ |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 506 | struct enclosure_device *prev = NULL; |
| 507 | |
| 508 | while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 509 | ses_match_to_enclosure(edev, sdev); |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 510 | prev = edev; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 511 | } |
| 512 | return -ENODEV; |
| 513 | } |
| 514 | |
| 515 | /* TYPE_ENCLOSURE prints a message in probe */ |
| 516 | if (sdev->type != TYPE_ENCLOSURE) |
| 517 | sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n"); |
| 518 | |
| 519 | ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL); |
| 520 | hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL); |
| 521 | if (!hdr_buf || !ses_dev) |
| 522 | goto err_init_free; |
| 523 | |
| 524 | result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE); |
| 525 | if (result) |
| 526 | goto recv_failed; |
| 527 | |
| 528 | if (hdr_buf[1] != 0) { |
| 529 | /* FIXME: need subenclosure support; I've just never |
| 530 | * seen a device with subenclosures and it makes the |
| 531 | * traversal routines more complex */ |
| 532 | sdev_printk(KERN_ERR, sdev, |
| 533 | "FIXME driver has no support for subenclosures (%d)\n", |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 534 | hdr_buf[1]); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 535 | goto err_free; |
| 536 | } |
| 537 | |
| 538 | len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 539 | buf = kzalloc(len, GFP_KERNEL); |
| 540 | if (!buf) |
| 541 | goto err_free; |
| 542 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 543 | result = ses_recv_diag(sdev, 1, buf, len); |
| 544 | if (result) |
| 545 | goto recv_failed; |
| 546 | |
| 547 | types = buf[10]; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 548 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 549 | type_ptr = buf + 12 + buf[11]; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 550 | |
| 551 | for (i = 0; i < types; i++, type_ptr += 4) { |
| 552 | if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || |
| 553 | type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) |
| 554 | components += type_ptr[1]; |
| 555 | } |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 556 | ses_dev->page1 = buf; |
| 557 | ses_dev->page1_len = len; |
| 558 | buf = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 559 | |
| 560 | result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE); |
| 561 | if (result) |
| 562 | goto recv_failed; |
| 563 | |
| 564 | len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 565 | buf = kzalloc(len, GFP_KERNEL); |
| 566 | if (!buf) |
| 567 | goto err_free; |
| 568 | |
| 569 | /* make sure getting page 2 actually works */ |
| 570 | result = ses_recv_diag(sdev, 2, buf, len); |
| 571 | if (result) |
| 572 | goto recv_failed; |
| 573 | ses_dev->page2 = buf; |
| 574 | ses_dev->page2_len = len; |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 575 | buf = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 576 | |
| 577 | /* The additional information page --- allows us |
| 578 | * to match up the devices */ |
| 579 | result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 580 | if (!result) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 581 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 582 | len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 583 | buf = kzalloc(len, GFP_KERNEL); |
| 584 | if (!buf) |
| 585 | goto err_free; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 586 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 587 | result = ses_recv_diag(sdev, 10, buf, len); |
| 588 | if (result) |
| 589 | goto recv_failed; |
| 590 | ses_dev->page10 = buf; |
| 591 | ses_dev->page10_len = len; |
| 592 | buf = NULL; |
| 593 | } |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 594 | kfree(hdr_buf); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 595 | |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 596 | scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 597 | if (!scomp) |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 598 | goto err_free; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 599 | |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 600 | edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev), |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 601 | components, &ses_enclosure_callbacks); |
| 602 | if (IS_ERR(edev)) { |
| 603 | err = PTR_ERR(edev); |
| 604 | goto err_free; |
| 605 | } |
| 606 | |
| 607 | edev->scratch = ses_dev; |
| 608 | for (i = 0; i < components; i++) |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 609 | edev->component[i].scratch = scomp + i; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 610 | |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 611 | ses_enclosure_data_process(edev, sdev, 1); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 612 | |
| 613 | /* see if there are any devices matching before |
| 614 | * we found the enclosure */ |
| 615 | shost_for_each_device(tmp_sdev, sdev->host) { |
| 616 | if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev)) |
| 617 | continue; |
| 618 | ses_match_to_enclosure(edev, tmp_sdev); |
| 619 | } |
| 620 | |
| 621 | return 0; |
| 622 | |
| 623 | recv_failed: |
| 624 | sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n", |
| 625 | result); |
| 626 | err = -ENODEV; |
| 627 | err_free: |
| 628 | kfree(buf); |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 629 | kfree(scomp); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 630 | kfree(ses_dev->page10); |
| 631 | kfree(ses_dev->page2); |
| 632 | kfree(ses_dev->page1); |
| 633 | err_init_free: |
| 634 | kfree(ses_dev); |
| 635 | kfree(hdr_buf); |
| 636 | sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err); |
| 637 | return err; |
| 638 | } |
| 639 | |
| 640 | static int ses_remove(struct device *dev) |
| 641 | { |
| 642 | return 0; |
| 643 | } |
| 644 | |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 645 | static void ses_intf_remove_component(struct scsi_device *sdev) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 646 | { |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 647 | struct enclosure_device *edev, *prev = NULL; |
| 648 | |
| 649 | while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) { |
| 650 | prev = edev; |
| 651 | if (!enclosure_remove_device(edev, &sdev->sdev_gendev)) |
| 652 | break; |
| 653 | } |
| 654 | if (edev) |
| 655 | put_device(&edev->edev); |
| 656 | } |
| 657 | |
| 658 | static void ses_intf_remove_enclosure(struct scsi_device *sdev) |
| 659 | { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 660 | struct enclosure_device *edev; |
| 661 | struct ses_device *ses_dev; |
| 662 | |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 663 | /* exact match to this enclosure */ |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 664 | edev = enclosure_find(&sdev->sdev_gendev, NULL); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 665 | if (!edev) |
| 666 | return; |
| 667 | |
| 668 | ses_dev = edev->scratch; |
| 669 | edev->scratch = NULL; |
| 670 | |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 671 | kfree(ses_dev->page10); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 672 | kfree(ses_dev->page1); |
| 673 | kfree(ses_dev->page2); |
| 674 | kfree(ses_dev); |
| 675 | |
| 676 | kfree(edev->component[0].scratch); |
| 677 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 678 | put_device(&edev->edev); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 679 | enclosure_unregister(edev); |
| 680 | } |
| 681 | |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 682 | static void ses_intf_remove(struct device *cdev, |
| 683 | struct class_interface *intf) |
| 684 | { |
| 685 | struct scsi_device *sdev = to_scsi_device(cdev->parent); |
| 686 | |
| 687 | if (!scsi_device_enclosure(sdev)) |
| 688 | ses_intf_remove_component(sdev); |
| 689 | else |
| 690 | ses_intf_remove_enclosure(sdev); |
| 691 | } |
| 692 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 693 | static struct class_interface ses_interface = { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 694 | .add_dev = ses_intf_add, |
| 695 | .remove_dev = ses_intf_remove, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 696 | }; |
| 697 | |
| 698 | static struct scsi_driver ses_template = { |
| 699 | .owner = THIS_MODULE, |
| 700 | .gendrv = { |
| 701 | .name = "ses", |
| 702 | .probe = ses_probe, |
| 703 | .remove = ses_remove, |
| 704 | }, |
| 705 | }; |
| 706 | |
| 707 | static int __init ses_init(void) |
| 708 | { |
| 709 | int err; |
| 710 | |
| 711 | err = scsi_register_interface(&ses_interface); |
| 712 | if (err) |
| 713 | return err; |
| 714 | |
| 715 | err = scsi_register_driver(&ses_template.gendrv); |
| 716 | if (err) |
| 717 | goto out_unreg; |
| 718 | |
| 719 | return 0; |
| 720 | |
| 721 | out_unreg: |
| 722 | scsi_unregister_interface(&ses_interface); |
| 723 | return err; |
| 724 | } |
| 725 | |
| 726 | static void __exit ses_exit(void) |
| 727 | { |
| 728 | scsi_unregister_driver(&ses_template.gendrv); |
| 729 | scsi_unregister_interface(&ses_interface); |
| 730 | } |
| 731 | |
| 732 | module_init(ses_init); |
| 733 | module_exit(ses_exit); |
| 734 | |
| 735 | MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE); |
| 736 | |
| 737 | MODULE_AUTHOR("James Bottomley"); |
| 738 | MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver"); |
| 739 | MODULE_LICENSE("GPL v2"); |