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 | |
| 267 | int ses_match_host(struct enclosure_device *edev, void *data) |
| 268 | { |
| 269 | struct ses_host_edev *sed = data; |
| 270 | struct scsi_device *sdev; |
| 271 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 272 | if (!scsi_is_sdev_device(edev->edev.parent)) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 273 | return 0; |
| 274 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 275 | sdev = to_scsi_device(edev->edev.parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 276 | |
| 277 | if (sdev->host != sed->shost) |
| 278 | return 0; |
| 279 | |
| 280 | sed->edev = edev; |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | static 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 | |
| 322 | struct efd { |
| 323 | u64 addr; |
| 324 | struct device *dev; |
| 325 | }; |
| 326 | |
| 327 | static 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 Bottomley | 671a99c | 2008-07-29 11:38:25 -0500 | [diff] [blame] | 348 | #define VPD_INQUIRY_SIZE 36 |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 349 | |
| 350 | static void ses_match_to_enclosure(struct enclosure_device *edev, |
| 351 | struct scsi_device *sdev) |
| 352 | { |
| 353 | unsigned char *buf = kmalloc(VPD_INQUIRY_SIZE, GFP_KERNEL); |
| 354 | unsigned char *desc; |
James Bottomley | 671a99c | 2008-07-29 11:38:25 -0500 | [diff] [blame] | 355 | u16 vpd_len; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 356 | struct efd efd = { |
| 357 | .addr = 0, |
| 358 | }; |
| 359 | unsigned char cmd[] = { |
| 360 | INQUIRY, |
| 361 | 1, |
| 362 | 0x83, |
| 363 | VPD_INQUIRY_SIZE >> 8, |
| 364 | VPD_INQUIRY_SIZE & 0xff, |
| 365 | 0 |
| 366 | }; |
| 367 | |
| 368 | if (!buf) |
| 369 | return; |
| 370 | |
| 371 | if (scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 372 | VPD_INQUIRY_SIZE, NULL, SES_TIMEOUT, SES_RETRIES, |
| 373 | NULL)) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 374 | goto free; |
| 375 | |
James Bottomley | 671a99c | 2008-07-29 11:38:25 -0500 | [diff] [blame] | 376 | vpd_len = (buf[2] << 8) + buf[3]; |
| 377 | kfree(buf); |
| 378 | buf = kmalloc(vpd_len, GFP_KERNEL); |
| 379 | if (!buf) |
| 380 | return; |
| 381 | cmd[3] = vpd_len >> 8; |
| 382 | cmd[4] = vpd_len & 0xff; |
| 383 | if (scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 384 | vpd_len, NULL, SES_TIMEOUT, SES_RETRIES, NULL)) |
James Bottomley | 671a99c | 2008-07-29 11:38:25 -0500 | [diff] [blame] | 385 | goto free; |
| 386 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 387 | desc = buf + 4; |
James Bottomley | 671a99c | 2008-07-29 11:38:25 -0500 | [diff] [blame] | 388 | while (desc < buf + vpd_len) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 389 | enum scsi_protocol proto = desc[0] >> 4; |
| 390 | u8 code_set = desc[0] & 0x0f; |
| 391 | u8 piv = desc[1] & 0x80; |
| 392 | u8 assoc = (desc[1] & 0x30) >> 4; |
| 393 | u8 type = desc[1] & 0x0f; |
| 394 | u8 len = desc[3]; |
| 395 | |
| 396 | if (piv && code_set == 1 && assoc == 1 && code_set == 1 |
| 397 | && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8) |
| 398 | efd.addr = (u64)desc[4] << 56 | |
| 399 | (u64)desc[5] << 48 | |
| 400 | (u64)desc[6] << 40 | |
| 401 | (u64)desc[7] << 32 | |
| 402 | (u64)desc[8] << 24 | |
| 403 | (u64)desc[9] << 16 | |
| 404 | (u64)desc[10] << 8 | |
| 405 | (u64)desc[11]; |
| 406 | |
| 407 | desc += len + 4; |
| 408 | } |
| 409 | if (!efd.addr) |
| 410 | goto free; |
| 411 | |
| 412 | efd.dev = &sdev->sdev_gendev; |
| 413 | |
| 414 | enclosure_for_each_device(ses_enclosure_find_by_addr, &efd); |
| 415 | free: |
| 416 | kfree(buf); |
| 417 | } |
| 418 | |
| 419 | #define INIT_ALLOC_SIZE 32 |
| 420 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 421 | static int ses_intf_add(struct device *cdev, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 422 | struct class_interface *intf) |
| 423 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 424 | struct scsi_device *sdev = to_scsi_device(cdev->parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 425 | struct scsi_device *tmp_sdev; |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 426 | unsigned char *buf = NULL, *hdr_buf, *type_ptr, *desc_ptr = NULL, |
| 427 | *addl_desc_ptr = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 428 | struct ses_device *ses_dev; |
| 429 | u32 result; |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 430 | int i, j, types, len, page7_len = 0, components = 0; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 431 | int err = -ENOMEM; |
| 432 | struct enclosure_device *edev; |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 433 | struct ses_component *scomp = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 434 | |
| 435 | if (!scsi_device_enclosure(sdev)) { |
| 436 | /* not an enclosure, but might be in one */ |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 437 | edev = enclosure_find(&sdev->host->shost_gendev); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 438 | if (edev) { |
| 439 | ses_match_to_enclosure(edev, sdev); |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 440 | put_device(&edev->edev); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 441 | } |
| 442 | return -ENODEV; |
| 443 | } |
| 444 | |
| 445 | /* TYPE_ENCLOSURE prints a message in probe */ |
| 446 | if (sdev->type != TYPE_ENCLOSURE) |
| 447 | sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n"); |
| 448 | |
| 449 | ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL); |
| 450 | hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL); |
| 451 | if (!hdr_buf || !ses_dev) |
| 452 | goto err_init_free; |
| 453 | |
| 454 | result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE); |
| 455 | if (result) |
| 456 | goto recv_failed; |
| 457 | |
| 458 | if (hdr_buf[1] != 0) { |
| 459 | /* FIXME: need subenclosure support; I've just never |
| 460 | * seen a device with subenclosures and it makes the |
| 461 | * traversal routines more complex */ |
| 462 | sdev_printk(KERN_ERR, sdev, |
| 463 | "FIXME driver has no support for subenclosures (%d)\n", |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 464 | hdr_buf[1]); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 465 | goto err_free; |
| 466 | } |
| 467 | |
| 468 | len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 469 | buf = kzalloc(len, GFP_KERNEL); |
| 470 | if (!buf) |
| 471 | goto err_free; |
| 472 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 473 | result = ses_recv_diag(sdev, 1, buf, len); |
| 474 | if (result) |
| 475 | goto recv_failed; |
| 476 | |
| 477 | types = buf[10]; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 478 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 479 | type_ptr = buf + 12 + buf[11]; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 480 | |
| 481 | for (i = 0; i < types; i++, type_ptr += 4) { |
| 482 | if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || |
| 483 | type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) |
| 484 | components += type_ptr[1]; |
| 485 | } |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 486 | ses_dev->page1 = buf; |
| 487 | ses_dev->page1_len = len; |
| 488 | buf = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 489 | |
| 490 | result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE); |
| 491 | if (result) |
| 492 | goto recv_failed; |
| 493 | |
| 494 | len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 495 | buf = kzalloc(len, GFP_KERNEL); |
| 496 | if (!buf) |
| 497 | goto err_free; |
| 498 | |
| 499 | /* make sure getting page 2 actually works */ |
| 500 | result = ses_recv_diag(sdev, 2, buf, len); |
| 501 | if (result) |
| 502 | goto recv_failed; |
| 503 | ses_dev->page2 = buf; |
| 504 | ses_dev->page2_len = len; |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 505 | buf = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 506 | |
| 507 | /* The additional information page --- allows us |
| 508 | * to match up the devices */ |
| 509 | result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 510 | if (!result) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 511 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 512 | len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 513 | buf = kzalloc(len, GFP_KERNEL); |
| 514 | if (!buf) |
| 515 | goto err_free; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 516 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 517 | result = ses_recv_diag(sdev, 10, buf, len); |
| 518 | if (result) |
| 519 | goto recv_failed; |
| 520 | ses_dev->page10 = buf; |
| 521 | ses_dev->page10_len = len; |
| 522 | buf = NULL; |
| 523 | } |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 524 | |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 525 | scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 526 | if (!scomp) |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 527 | goto err_free; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 528 | |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 529 | edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev), |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 530 | components, &ses_enclosure_callbacks); |
| 531 | if (IS_ERR(edev)) { |
| 532 | err = PTR_ERR(edev); |
| 533 | goto err_free; |
| 534 | } |
| 535 | |
| 536 | edev->scratch = ses_dev; |
| 537 | for (i = 0; i < components; i++) |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 538 | edev->component[i].scratch = scomp + i; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 539 | |
| 540 | /* Page 7 for the descriptors is optional */ |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 541 | result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE); |
| 542 | if (result) |
| 543 | goto simple_populate; |
| 544 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 545 | page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 546 | /* add 1 for trailing '\0' we'll use */ |
| 547 | buf = kzalloc(len + 1, GFP_KERNEL); |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 548 | if (!buf) |
| 549 | goto simple_populate; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 550 | result = ses_recv_diag(sdev, 7, buf, len); |
| 551 | if (result) { |
| 552 | simple_populate: |
| 553 | kfree(buf); |
| 554 | buf = NULL; |
| 555 | desc_ptr = NULL; |
| 556 | addl_desc_ptr = NULL; |
| 557 | } else { |
| 558 | desc_ptr = buf + 8; |
| 559 | len = (desc_ptr[2] << 8) + desc_ptr[3]; |
| 560 | /* skip past overall descriptor */ |
| 561 | desc_ptr += len + 4; |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 562 | if (ses_dev->page10) |
| 563 | addl_desc_ptr = ses_dev->page10 + 8; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 564 | } |
| 565 | type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11]; |
| 566 | components = 0; |
| 567 | for (i = 0; i < types; i++, type_ptr += 4) { |
| 568 | for (j = 0; j < type_ptr[1]; j++) { |
| 569 | char *name = NULL; |
| 570 | struct enclosure_component *ecomp; |
| 571 | |
| 572 | if (desc_ptr) { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 573 | if (desc_ptr >= buf + page7_len) { |
| 574 | desc_ptr = NULL; |
| 575 | } else { |
| 576 | len = (desc_ptr[2] << 8) + desc_ptr[3]; |
| 577 | desc_ptr += 4; |
| 578 | /* Add trailing zero - pushes into |
| 579 | * reserved space */ |
| 580 | desc_ptr[len] = '\0'; |
| 581 | name = desc_ptr; |
| 582 | } |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 583 | } |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 584 | if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || |
| 585 | type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) { |
| 586 | |
| 587 | ecomp = enclosure_component_register(edev, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 588 | components++, |
| 589 | type_ptr[0], |
| 590 | name); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 591 | |
| 592 | if (!IS_ERR(ecomp) && addl_desc_ptr) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 593 | ses_process_descriptor(ecomp, |
| 594 | addl_desc_ptr); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 595 | } |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 596 | if (desc_ptr) |
| 597 | desc_ptr += len; |
| 598 | |
| 599 | if (addl_desc_ptr) |
| 600 | addl_desc_ptr += addl_desc_ptr[1] + 2; |
| 601 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | kfree(buf); |
| 605 | kfree(hdr_buf); |
| 606 | |
| 607 | /* see if there are any devices matching before |
| 608 | * we found the enclosure */ |
| 609 | shost_for_each_device(tmp_sdev, sdev->host) { |
| 610 | if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev)) |
| 611 | continue; |
| 612 | ses_match_to_enclosure(edev, tmp_sdev); |
| 613 | } |
| 614 | |
| 615 | return 0; |
| 616 | |
| 617 | recv_failed: |
| 618 | sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n", |
| 619 | result); |
| 620 | err = -ENODEV; |
| 621 | err_free: |
| 622 | kfree(buf); |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 623 | kfree(scomp); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 624 | kfree(ses_dev->page10); |
| 625 | kfree(ses_dev->page2); |
| 626 | kfree(ses_dev->page1); |
| 627 | err_init_free: |
| 628 | kfree(ses_dev); |
| 629 | kfree(hdr_buf); |
| 630 | sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err); |
| 631 | return err; |
| 632 | } |
| 633 | |
| 634 | static int ses_remove(struct device *dev) |
| 635 | { |
| 636 | return 0; |
| 637 | } |
| 638 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 639 | static void ses_intf_remove(struct device *cdev, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 640 | struct class_interface *intf) |
| 641 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 642 | struct scsi_device *sdev = to_scsi_device(cdev->parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 643 | struct enclosure_device *edev; |
| 644 | struct ses_device *ses_dev; |
| 645 | |
| 646 | if (!scsi_device_enclosure(sdev)) |
| 647 | return; |
| 648 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 649 | edev = enclosure_find(cdev->parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 650 | if (!edev) |
| 651 | return; |
| 652 | |
| 653 | ses_dev = edev->scratch; |
| 654 | edev->scratch = NULL; |
| 655 | |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 656 | kfree(ses_dev->page10); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 657 | kfree(ses_dev->page1); |
| 658 | kfree(ses_dev->page2); |
| 659 | kfree(ses_dev); |
| 660 | |
| 661 | kfree(edev->component[0].scratch); |
| 662 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 663 | put_device(&edev->edev); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 664 | enclosure_unregister(edev); |
| 665 | } |
| 666 | |
| 667 | static struct class_interface ses_interface = { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 668 | .add_dev = ses_intf_add, |
| 669 | .remove_dev = ses_intf_remove, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 670 | }; |
| 671 | |
| 672 | static struct scsi_driver ses_template = { |
| 673 | .owner = THIS_MODULE, |
| 674 | .gendrv = { |
| 675 | .name = "ses", |
| 676 | .probe = ses_probe, |
| 677 | .remove = ses_remove, |
| 678 | }, |
| 679 | }; |
| 680 | |
| 681 | static int __init ses_init(void) |
| 682 | { |
| 683 | int err; |
| 684 | |
| 685 | err = scsi_register_interface(&ses_interface); |
| 686 | if (err) |
| 687 | return err; |
| 688 | |
| 689 | err = scsi_register_driver(&ses_template.gendrv); |
| 690 | if (err) |
| 691 | goto out_unreg; |
| 692 | |
| 693 | return 0; |
| 694 | |
| 695 | out_unreg: |
| 696 | scsi_unregister_interface(&ses_interface); |
| 697 | return err; |
| 698 | } |
| 699 | |
| 700 | static void __exit ses_exit(void) |
| 701 | { |
| 702 | scsi_unregister_driver(&ses_template.gendrv); |
| 703 | scsi_unregister_interface(&ses_interface); |
| 704 | } |
| 705 | |
| 706 | module_init(ses_init); |
| 707 | module_exit(ses_exit); |
| 708 | |
| 709 | MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE); |
| 710 | |
| 711 | MODULE_AUTHOR("James Bottomley"); |
| 712 | MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver"); |
| 713 | MODULE_LICENSE("GPL v2"); |