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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/enclosure.h> |
Hannes Reinecke | c38c007 | 2014-03-15 09:51:51 +0100 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 29 | |
| 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 | |
| 37 | struct ses_device { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 38 | unsigned char *page1; |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 39 | unsigned char *page1_types; |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 40 | unsigned char *page2; |
| 41 | unsigned char *page10; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 42 | short page1_len; |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 43 | short page1_num_types; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 44 | short page2_len; |
| 45 | short page10_len; |
| 46 | }; |
| 47 | |
| 48 | struct ses_component { |
| 49 | u64 addr; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static int ses_probe(struct device *dev) |
| 53 | { |
| 54 | struct scsi_device *sdev = to_scsi_device(dev); |
| 55 | int err = -ENODEV; |
| 56 | |
| 57 | if (sdev->type != TYPE_ENCLOSURE) |
| 58 | goto out; |
| 59 | |
| 60 | err = 0; |
| 61 | sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n"); |
| 62 | |
| 63 | out: |
| 64 | return err; |
| 65 | } |
| 66 | |
Matthew Wilcox | c95e62c | 2008-06-23 09:14:31 -0600 | [diff] [blame] | 67 | #define SES_TIMEOUT (30 * HZ) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 68 | #define SES_RETRIES 3 |
| 69 | |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 70 | static void init_device_slot_control(unsigned char *dest_desc, |
| 71 | struct enclosure_component *ecomp, |
| 72 | unsigned char *status) |
| 73 | { |
| 74 | memcpy(dest_desc, status, 4); |
| 75 | dest_desc[0] = 0; |
| 76 | /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */ |
| 77 | if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE) |
| 78 | dest_desc[1] = 0; |
| 79 | dest_desc[2] &= 0xde; |
| 80 | dest_desc[3] &= 0x3c; |
| 81 | } |
| 82 | |
| 83 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 84 | static int ses_recv_diag(struct scsi_device *sdev, int page_code, |
| 85 | void *buf, int bufflen) |
| 86 | { |
James Bottomley | 3417c1b | 2015-12-08 09:00:31 -0800 | [diff] [blame^] | 87 | int ret; |
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 | RECEIVE_DIAGNOSTIC, |
| 90 | 1, /* Set PCV bit */ |
| 91 | page_code, |
| 92 | bufflen >> 8, |
| 93 | bufflen & 0xff, |
| 94 | 0 |
| 95 | }; |
James Bottomley | 3417c1b | 2015-12-08 09:00:31 -0800 | [diff] [blame^] | 96 | unsigned char recv_page_code; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 97 | |
James Bottomley | 3417c1b | 2015-12-08 09:00:31 -0800 | [diff] [blame^] | 98 | ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 99 | NULL, SES_TIMEOUT, SES_RETRIES, NULL); |
James Bottomley | 3417c1b | 2015-12-08 09:00:31 -0800 | [diff] [blame^] | 100 | if (unlikely(!ret)) |
| 101 | return ret; |
| 102 | |
| 103 | recv_page_code = ((unsigned char *)buf)[0]; |
| 104 | |
| 105 | if (likely(recv_page_code == page_code)) |
| 106 | return ret; |
| 107 | |
| 108 | /* successful diagnostic but wrong page code. This happens to some |
| 109 | * USB devices, just print a message and pretend there was an error */ |
| 110 | |
| 111 | sdev_printk(KERN_ERR, sdev, |
| 112 | "Wrong diagnostic page; asked for %d got %u\n", |
| 113 | page_code, recv_page_code); |
| 114 | |
| 115 | return -EINVAL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static int ses_send_diag(struct scsi_device *sdev, int page_code, |
| 119 | void *buf, int bufflen) |
| 120 | { |
| 121 | u32 result; |
| 122 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 123 | unsigned char cmd[] = { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 124 | SEND_DIAGNOSTIC, |
| 125 | 0x10, /* Set PF bit */ |
| 126 | 0, |
| 127 | bufflen >> 8, |
| 128 | bufflen & 0xff, |
| 129 | 0 |
| 130 | }; |
| 131 | |
| 132 | result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen, |
FUJITA Tomonori | f4f4e47 | 2008-12-04 14:24:39 +0900 | [diff] [blame] | 133 | NULL, SES_TIMEOUT, SES_RETRIES, NULL); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 134 | if (result) |
| 135 | sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n", |
| 136 | result); |
| 137 | return result; |
| 138 | } |
| 139 | |
| 140 | static int ses_set_page2_descriptor(struct enclosure_device *edev, |
| 141 | struct enclosure_component *ecomp, |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 142 | unsigned char *desc) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 143 | { |
| 144 | int i, j, count = 0, descriptor = ecomp->number; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 145 | struct scsi_device *sdev = to_scsi_device(edev->edev.parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 146 | struct ses_device *ses_dev = edev->scratch; |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 147 | unsigned char *type_ptr = ses_dev->page1_types; |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 148 | unsigned char *desc_ptr = ses_dev->page2 + 8; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 149 | |
| 150 | /* Clear everything */ |
| 151 | memset(desc_ptr, 0, ses_dev->page2_len - 8); |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 152 | for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 153 | for (j = 0; j < type_ptr[1]; j++) { |
| 154 | desc_ptr += 4; |
| 155 | if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE && |
| 156 | type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE) |
| 157 | continue; |
| 158 | if (count++ == descriptor) { |
| 159 | memcpy(desc_ptr, desc, 4); |
| 160 | /* set select */ |
| 161 | desc_ptr[0] |= 0x80; |
| 162 | /* clear reserved, just in case */ |
| 163 | desc_ptr[0] &= 0xf0; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len); |
| 169 | } |
| 170 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 171 | static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 172 | struct enclosure_component *ecomp) |
| 173 | { |
| 174 | int i, j, count = 0, descriptor = ecomp->number; |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 175 | struct scsi_device *sdev = to_scsi_device(edev->edev.parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 176 | struct ses_device *ses_dev = edev->scratch; |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 177 | unsigned char *type_ptr = ses_dev->page1_types; |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 178 | unsigned char *desc_ptr = ses_dev->page2 + 8; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 179 | |
| 180 | ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len); |
| 181 | |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 182 | for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 183 | for (j = 0; j < type_ptr[1]; j++) { |
| 184 | desc_ptr += 4; |
| 185 | if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE && |
| 186 | type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE) |
| 187 | continue; |
| 188 | if (count++ == descriptor) |
| 189 | return desc_ptr; |
| 190 | } |
| 191 | } |
| 192 | return NULL; |
| 193 | } |
| 194 | |
Douglas Gilbert | 2a350ca | 2011-06-09 00:27:07 -0400 | [diff] [blame] | 195 | /* For device slot and array device slot elements, byte 3 bit 6 |
| 196 | * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this |
| 197 | * code stands these bits are shifted 4 positions right so in |
| 198 | * sysfs they will appear as bits 2 and 1 respectively. Strange. */ |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 199 | static void ses_get_fault(struct enclosure_device *edev, |
| 200 | struct enclosure_component *ecomp) |
| 201 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 202 | unsigned char *desc; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 203 | |
| 204 | desc = ses_get_page2_descriptor(edev, ecomp); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 205 | if (desc) |
| 206 | ecomp->fault = (desc[3] & 0x60) >> 4; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static int ses_set_fault(struct enclosure_device *edev, |
| 210 | struct enclosure_component *ecomp, |
| 211 | enum enclosure_component_setting val) |
| 212 | { |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 213 | unsigned char desc[4]; |
| 214 | unsigned char *desc_ptr; |
| 215 | |
| 216 | desc_ptr = ses_get_page2_descriptor(edev, ecomp); |
| 217 | |
| 218 | if (!desc_ptr) |
| 219 | return -EIO; |
| 220 | |
| 221 | init_device_slot_control(desc, ecomp, desc_ptr); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 222 | |
| 223 | switch (val) { |
| 224 | case ENCLOSURE_SETTING_DISABLED: |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 225 | desc[3] &= 0xdf; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 226 | break; |
| 227 | case ENCLOSURE_SETTING_ENABLED: |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 228 | desc[3] |= 0x20; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 229 | break; |
| 230 | default: |
| 231 | /* SES doesn't do the SGPIO blink settings */ |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | return ses_set_page2_descriptor(edev, ecomp, desc); |
| 236 | } |
| 237 | |
| 238 | static void ses_get_status(struct enclosure_device *edev, |
| 239 | struct enclosure_component *ecomp) |
| 240 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 241 | unsigned char *desc; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 242 | |
| 243 | desc = ses_get_page2_descriptor(edev, ecomp); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 244 | if (desc) |
| 245 | ecomp->status = (desc[0] & 0x0f); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static void ses_get_locate(struct enclosure_device *edev, |
| 249 | struct enclosure_component *ecomp) |
| 250 | { |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 251 | unsigned char *desc; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 252 | |
| 253 | desc = ses_get_page2_descriptor(edev, ecomp); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 254 | if (desc) |
| 255 | ecomp->locate = (desc[2] & 0x02) ? 1 : 0; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int ses_set_locate(struct enclosure_device *edev, |
| 259 | struct enclosure_component *ecomp, |
| 260 | enum enclosure_component_setting val) |
| 261 | { |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 262 | unsigned char desc[4]; |
| 263 | unsigned char *desc_ptr; |
| 264 | |
| 265 | desc_ptr = ses_get_page2_descriptor(edev, ecomp); |
| 266 | |
| 267 | if (!desc_ptr) |
| 268 | return -EIO; |
| 269 | |
| 270 | init_device_slot_control(desc, ecomp, desc_ptr); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 271 | |
| 272 | switch (val) { |
| 273 | case ENCLOSURE_SETTING_DISABLED: |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 274 | desc[2] &= 0xfd; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 275 | break; |
| 276 | case ENCLOSURE_SETTING_ENABLED: |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 277 | desc[2] |= 0x02; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 278 | break; |
| 279 | default: |
| 280 | /* SES doesn't do the SGPIO blink settings */ |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | return ses_set_page2_descriptor(edev, ecomp, desc); |
| 284 | } |
| 285 | |
| 286 | static int ses_set_active(struct enclosure_device *edev, |
| 287 | struct enclosure_component *ecomp, |
| 288 | enum enclosure_component_setting val) |
| 289 | { |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 290 | unsigned char desc[4]; |
| 291 | unsigned char *desc_ptr; |
| 292 | |
| 293 | desc_ptr = ses_get_page2_descriptor(edev, ecomp); |
| 294 | |
| 295 | if (!desc_ptr) |
| 296 | return -EIO; |
| 297 | |
| 298 | init_device_slot_control(desc, ecomp, desc_ptr); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 299 | |
| 300 | switch (val) { |
| 301 | case ENCLOSURE_SETTING_DISABLED: |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 302 | desc[2] &= 0x7f; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 303 | ecomp->active = 0; |
| 304 | break; |
| 305 | case ENCLOSURE_SETTING_ENABLED: |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 306 | desc[2] |= 0x80; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 307 | ecomp->active = 1; |
| 308 | break; |
| 309 | default: |
| 310 | /* SES doesn't do the SGPIO blink settings */ |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | return ses_set_page2_descriptor(edev, ecomp, desc); |
| 314 | } |
| 315 | |
Dan Williams | 967f7ba | 2014-12-30 14:46:16 -0800 | [diff] [blame] | 316 | static int ses_show_id(struct enclosure_device *edev, char *buf) |
| 317 | { |
| 318 | struct ses_device *ses_dev = edev->scratch; |
| 319 | unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4); |
| 320 | |
| 321 | return sprintf(buf, "%#llx\n", id); |
| 322 | } |
| 323 | |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 324 | static void ses_get_power_status(struct enclosure_device *edev, |
| 325 | struct enclosure_component *ecomp) |
| 326 | { |
| 327 | unsigned char *desc; |
| 328 | |
| 329 | desc = ses_get_page2_descriptor(edev, ecomp); |
| 330 | if (desc) |
| 331 | ecomp->power_status = (desc[3] & 0x10) ? 0 : 1; |
| 332 | } |
| 333 | |
| 334 | static int ses_set_power_status(struct enclosure_device *edev, |
| 335 | struct enclosure_component *ecomp, |
| 336 | int val) |
| 337 | { |
| 338 | unsigned char desc[4]; |
| 339 | unsigned char *desc_ptr; |
| 340 | |
| 341 | desc_ptr = ses_get_page2_descriptor(edev, ecomp); |
| 342 | |
| 343 | if (!desc_ptr) |
| 344 | return -EIO; |
| 345 | |
| 346 | init_device_slot_control(desc, ecomp, desc_ptr); |
| 347 | |
| 348 | switch (val) { |
| 349 | /* power = 1 is device_off = 0 and vice versa */ |
| 350 | case 0: |
| 351 | desc[3] |= 0x10; |
| 352 | break; |
| 353 | case 1: |
| 354 | desc[3] &= 0xef; |
| 355 | break; |
| 356 | default: |
| 357 | return -EINVAL; |
| 358 | } |
| 359 | ecomp->power_status = val; |
| 360 | return ses_set_page2_descriptor(edev, ecomp, desc); |
| 361 | } |
| 362 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 363 | static struct enclosure_component_callbacks ses_enclosure_callbacks = { |
| 364 | .get_fault = ses_get_fault, |
| 365 | .set_fault = ses_set_fault, |
| 366 | .get_status = ses_get_status, |
| 367 | .get_locate = ses_get_locate, |
| 368 | .set_locate = ses_set_locate, |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 369 | .get_power_status = ses_get_power_status, |
| 370 | .set_power_status = ses_set_power_status, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 371 | .set_active = ses_set_active, |
Dan Williams | 967f7ba | 2014-12-30 14:46:16 -0800 | [diff] [blame] | 372 | .show_id = ses_show_id, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | struct ses_host_edev { |
| 376 | struct Scsi_Host *shost; |
| 377 | struct enclosure_device *edev; |
| 378 | }; |
| 379 | |
Adrian Bunk | e0aae1a | 2009-03-04 12:06:05 -0800 | [diff] [blame] | 380 | #if 0 |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 381 | int ses_match_host(struct enclosure_device *edev, void *data) |
| 382 | { |
| 383 | struct ses_host_edev *sed = data; |
| 384 | struct scsi_device *sdev; |
| 385 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 386 | if (!scsi_is_sdev_device(edev->edev.parent)) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 387 | return 0; |
| 388 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 389 | sdev = to_scsi_device(edev->edev.parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 390 | |
| 391 | if (sdev->host != sed->shost) |
| 392 | return 0; |
| 393 | |
| 394 | sed->edev = edev; |
| 395 | return 1; |
| 396 | } |
Adrian Bunk | e0aae1a | 2009-03-04 12:06:05 -0800 | [diff] [blame] | 397 | #endif /* 0 */ |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 398 | |
| 399 | static void ses_process_descriptor(struct enclosure_component *ecomp, |
| 400 | unsigned char *desc) |
| 401 | { |
| 402 | int eip = desc[0] & 0x10; |
| 403 | int invalid = desc[0] & 0x80; |
| 404 | enum scsi_protocol proto = desc[0] & 0x0f; |
| 405 | u64 addr = 0; |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame] | 406 | int slot = -1; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 407 | struct ses_component *scomp = ecomp->scratch; |
| 408 | unsigned char *d; |
| 409 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 410 | if (invalid) |
| 411 | return; |
| 412 | |
| 413 | switch (proto) { |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame] | 414 | case SCSI_PROTOCOL_FCP: |
| 415 | if (eip) { |
| 416 | d = desc + 4; |
| 417 | slot = d[3]; |
| 418 | } |
| 419 | break; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 420 | case SCSI_PROTOCOL_SAS: |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame] | 421 | if (eip) { |
| 422 | d = desc + 4; |
| 423 | slot = d[3]; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 424 | d = desc + 8; |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame] | 425 | } else |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 426 | d = desc + 4; |
| 427 | /* only take the phy0 addr */ |
| 428 | addr = (u64)d[12] << 56 | |
| 429 | (u64)d[13] << 48 | |
| 430 | (u64)d[14] << 40 | |
| 431 | (u64)d[15] << 32 | |
| 432 | (u64)d[16] << 24 | |
| 433 | (u64)d[17] << 16 | |
| 434 | (u64)d[18] << 8 | |
| 435 | (u64)d[19]; |
| 436 | break; |
| 437 | default: |
| 438 | /* FIXME: Need to add more protocols than just SAS */ |
| 439 | break; |
| 440 | } |
Dan Williams | 921ce7f | 2014-12-30 14:46:17 -0800 | [diff] [blame] | 441 | ecomp->slot = slot; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 442 | scomp->addr = addr; |
| 443 | } |
| 444 | |
| 445 | struct efd { |
| 446 | u64 addr; |
| 447 | struct device *dev; |
| 448 | }; |
| 449 | |
| 450 | static int ses_enclosure_find_by_addr(struct enclosure_device *edev, |
| 451 | void *data) |
| 452 | { |
| 453 | struct efd *efd = data; |
| 454 | int i; |
| 455 | struct ses_component *scomp; |
| 456 | |
| 457 | if (!edev->component[0].scratch) |
| 458 | return 0; |
| 459 | |
| 460 | for (i = 0; i < edev->components; i++) { |
| 461 | scomp = edev->component[i].scratch; |
| 462 | if (scomp->addr != efd->addr) |
| 463 | continue; |
| 464 | |
Dan Williams | 15a0fbb | 2014-12-30 14:46:15 -0800 | [diff] [blame] | 465 | if (enclosure_add_device(edev, i, efd->dev) == 0) |
| 466 | kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 467 | return 1; |
| 468 | } |
| 469 | return 0; |
| 470 | } |
| 471 | |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 472 | #define INIT_ALLOC_SIZE 32 |
| 473 | |
| 474 | static void ses_enclosure_data_process(struct enclosure_device *edev, |
| 475 | struct scsi_device *sdev, |
| 476 | int create) |
| 477 | { |
| 478 | u32 result; |
| 479 | unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL; |
| 480 | int i, j, page7_len, len, components; |
| 481 | struct ses_device *ses_dev = edev->scratch; |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 482 | int types = ses_dev->page1_num_types; |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 483 | unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL); |
| 484 | |
| 485 | if (!hdr_buf) |
| 486 | goto simple_populate; |
| 487 | |
| 488 | /* re-read page 10 */ |
| 489 | if (ses_dev->page10) |
| 490 | ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len); |
| 491 | /* Page 7 for the descriptors is optional */ |
| 492 | result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE); |
| 493 | if (result) |
| 494 | goto simple_populate; |
| 495 | |
| 496 | page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 497 | /* add 1 for trailing '\0' we'll use */ |
| 498 | buf = kzalloc(len + 1, GFP_KERNEL); |
| 499 | if (!buf) |
| 500 | goto simple_populate; |
| 501 | result = ses_recv_diag(sdev, 7, buf, len); |
| 502 | if (result) { |
| 503 | simple_populate: |
| 504 | kfree(buf); |
| 505 | buf = NULL; |
| 506 | desc_ptr = NULL; |
| 507 | len = 0; |
| 508 | page7_len = 0; |
| 509 | } else { |
| 510 | desc_ptr = buf + 8; |
| 511 | len = (desc_ptr[2] << 8) + desc_ptr[3]; |
| 512 | /* skip past overall descriptor */ |
| 513 | desc_ptr += len + 4; |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 514 | } |
John Hughes | 877a559 | 2009-11-04 19:01:22 +0100 | [diff] [blame] | 515 | if (ses_dev->page10) |
| 516 | addl_desc_ptr = ses_dev->page10 + 8; |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 517 | type_ptr = ses_dev->page1_types; |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 518 | components = 0; |
| 519 | for (i = 0; i < types; i++, type_ptr += 4) { |
| 520 | for (j = 0; j < type_ptr[1]; j++) { |
| 521 | char *name = NULL; |
| 522 | struct enclosure_component *ecomp; |
| 523 | |
| 524 | if (desc_ptr) { |
| 525 | if (desc_ptr >= buf + page7_len) { |
| 526 | desc_ptr = NULL; |
| 527 | } else { |
| 528 | len = (desc_ptr[2] << 8) + desc_ptr[3]; |
| 529 | desc_ptr += 4; |
| 530 | /* Add trailing zero - pushes into |
| 531 | * reserved space */ |
| 532 | desc_ptr[len] = '\0'; |
| 533 | name = desc_ptr; |
| 534 | } |
| 535 | } |
| 536 | if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || |
| 537 | type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) { |
| 538 | |
| 539 | if (create) |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 540 | ecomp = enclosure_component_alloc( |
| 541 | edev, |
| 542 | components++, |
| 543 | type_ptr[0], |
| 544 | name); |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 545 | else |
| 546 | ecomp = &edev->component[components++]; |
| 547 | |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 548 | if (!IS_ERR(ecomp)) { |
Song Liu | 0802488 | 2014-12-30 14:46:18 -0800 | [diff] [blame] | 549 | ses_get_power_status(edev, ecomp); |
Dan Williams | ed09dcc | 2014-12-30 14:46:14 -0800 | [diff] [blame] | 550 | if (addl_desc_ptr) |
| 551 | ses_process_descriptor( |
| 552 | ecomp, |
| 553 | addl_desc_ptr); |
| 554 | if (create) |
| 555 | enclosure_component_register( |
| 556 | ecomp); |
| 557 | } |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 558 | } |
| 559 | if (desc_ptr) |
| 560 | desc_ptr += len; |
| 561 | |
| 562 | if (addl_desc_ptr) |
| 563 | addl_desc_ptr += addl_desc_ptr[1] + 2; |
| 564 | |
| 565 | } |
| 566 | } |
| 567 | kfree(buf); |
| 568 | kfree(hdr_buf); |
| 569 | } |
| 570 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 571 | static void ses_match_to_enclosure(struct enclosure_device *edev, |
| 572 | struct scsi_device *sdev) |
| 573 | { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 574 | unsigned char *desc; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 575 | struct efd efd = { |
| 576 | .addr = 0, |
| 577 | }; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 578 | |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 579 | ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0); |
| 580 | |
Hannes Reinecke | c38c007 | 2014-03-15 09:51:51 +0100 | [diff] [blame] | 581 | if (!sdev->vpd_pg83_len) |
| 582 | return; |
James Bottomley | 671a99c | 2008-07-29 11:38:25 -0500 | [diff] [blame] | 583 | |
Hannes Reinecke | c38c007 | 2014-03-15 09:51:51 +0100 | [diff] [blame] | 584 | desc = sdev->vpd_pg83 + 4; |
| 585 | while (desc < sdev->vpd_pg83 + sdev->vpd_pg83_len) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 586 | enum scsi_protocol proto = desc[0] >> 4; |
| 587 | u8 code_set = desc[0] & 0x0f; |
| 588 | u8 piv = desc[1] & 0x80; |
| 589 | u8 assoc = (desc[1] & 0x30) >> 4; |
| 590 | u8 type = desc[1] & 0x0f; |
| 591 | u8 len = desc[3]; |
| 592 | |
Roel Kluin | b3f1f9a | 2009-02-17 16:59:24 +0100 | [diff] [blame] | 593 | if (piv && code_set == 1 && assoc == 1 |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 594 | && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8) |
Hannes Reinecke | c38c007 | 2014-03-15 09:51:51 +0100 | [diff] [blame] | 595 | efd.addr = get_unaligned_be64(&desc[4]); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 596 | |
| 597 | desc += len + 4; |
| 598 | } |
Hannes Reinecke | c38c007 | 2014-03-15 09:51:51 +0100 | [diff] [blame] | 599 | if (efd.addr) { |
| 600 | efd.dev = &sdev->sdev_gendev; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 601 | |
Hannes Reinecke | c38c007 | 2014-03-15 09:51:51 +0100 | [diff] [blame] | 602 | enclosure_for_each_device(ses_enclosure_find_by_addr, &efd); |
| 603 | } |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 604 | } |
| 605 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 606 | static int ses_intf_add(struct device *cdev, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 607 | struct class_interface *intf) |
| 608 | { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 609 | struct scsi_device *sdev = to_scsi_device(cdev->parent); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 610 | struct scsi_device *tmp_sdev; |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 611 | unsigned char *buf = NULL, *hdr_buf, *type_ptr; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 612 | struct ses_device *ses_dev; |
| 613 | u32 result; |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 614 | int i, types, len, components = 0; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 615 | int err = -ENOMEM; |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 616 | int num_enclosures; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 617 | struct enclosure_device *edev; |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 618 | struct ses_component *scomp = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 619 | |
| 620 | if (!scsi_device_enclosure(sdev)) { |
| 621 | /* not an enclosure, but might be in one */ |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 622 | struct enclosure_device *prev = NULL; |
| 623 | |
| 624 | while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 625 | ses_match_to_enclosure(edev, sdev); |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 626 | prev = edev; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 627 | } |
| 628 | return -ENODEV; |
| 629 | } |
| 630 | |
| 631 | /* TYPE_ENCLOSURE prints a message in probe */ |
| 632 | if (sdev->type != TYPE_ENCLOSURE) |
| 633 | sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n"); |
| 634 | |
| 635 | ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL); |
| 636 | hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL); |
| 637 | if (!hdr_buf || !ses_dev) |
| 638 | goto err_init_free; |
| 639 | |
| 640 | result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE); |
| 641 | if (result) |
| 642 | goto recv_failed; |
| 643 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 644 | len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 645 | buf = kzalloc(len, GFP_KERNEL); |
| 646 | if (!buf) |
| 647 | goto err_free; |
| 648 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 649 | result = ses_recv_diag(sdev, 1, buf, len); |
| 650 | if (result) |
| 651 | goto recv_failed; |
| 652 | |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 653 | types = 0; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 654 | |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 655 | /* we always have one main enclosure and the rest are referred |
| 656 | * to as secondary subenclosures */ |
| 657 | num_enclosures = buf[1] + 1; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 658 | |
James Bottomley | 8c3adc7 | 2011-03-18 11:24:23 -0400 | [diff] [blame] | 659 | /* begin at the enclosure descriptor */ |
| 660 | type_ptr = buf + 8; |
| 661 | /* skip all the enclosure descriptors */ |
| 662 | for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) { |
| 663 | types += type_ptr[2]; |
| 664 | type_ptr += type_ptr[3] + 4; |
| 665 | } |
| 666 | |
| 667 | ses_dev->page1_types = type_ptr; |
| 668 | ses_dev->page1_num_types = types; |
| 669 | |
| 670 | for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 671 | if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || |
| 672 | type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) |
| 673 | components += type_ptr[1]; |
| 674 | } |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 675 | ses_dev->page1 = buf; |
| 676 | ses_dev->page1_len = len; |
| 677 | buf = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 678 | |
| 679 | result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE); |
| 680 | if (result) |
| 681 | goto recv_failed; |
| 682 | |
| 683 | len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 684 | buf = kzalloc(len, GFP_KERNEL); |
| 685 | if (!buf) |
| 686 | goto err_free; |
| 687 | |
| 688 | /* make sure getting page 2 actually works */ |
| 689 | result = ses_recv_diag(sdev, 2, buf, len); |
| 690 | if (result) |
| 691 | goto recv_failed; |
| 692 | ses_dev->page2 = buf; |
| 693 | ses_dev->page2_len = len; |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 694 | buf = NULL; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 695 | |
| 696 | /* The additional information page --- allows us |
| 697 | * to match up the devices */ |
| 698 | result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE); |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 699 | if (!result) { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 700 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 701 | len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; |
| 702 | buf = kzalloc(len, GFP_KERNEL); |
| 703 | if (!buf) |
| 704 | goto err_free; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 705 | |
Yinghai Lu | 691b477 | 2008-02-13 16:25:16 -0800 | [diff] [blame] | 706 | result = ses_recv_diag(sdev, 10, buf, len); |
| 707 | if (result) |
| 708 | goto recv_failed; |
| 709 | ses_dev->page10 = buf; |
| 710 | ses_dev->page10_len = len; |
| 711 | buf = NULL; |
| 712 | } |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 713 | scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 714 | if (!scomp) |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 715 | goto err_free; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 716 | |
Kay Sievers | 71610f5 | 2008-12-03 22:41:36 +0100 | [diff] [blame] | 717 | edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev), |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 718 | components, &ses_enclosure_callbacks); |
| 719 | if (IS_ERR(edev)) { |
| 720 | err = PTR_ERR(edev); |
| 721 | goto err_free; |
| 722 | } |
| 723 | |
Julia Lawall | 9b3a654 | 2010-03-10 15:20:42 -0800 | [diff] [blame] | 724 | kfree(hdr_buf); |
| 725 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 726 | edev->scratch = ses_dev; |
| 727 | for (i = 0; i < components; i++) |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 728 | edev->component[i].scratch = scomp + i; |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 729 | |
James Bottomley | 21fab1d | 2009-08-01 00:43:59 +0000 | [diff] [blame] | 730 | ses_enclosure_data_process(edev, sdev, 1); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 731 | |
| 732 | /* see if there are any devices matching before |
| 733 | * we found the enclosure */ |
| 734 | shost_for_each_device(tmp_sdev, sdev->host) { |
| 735 | if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev)) |
| 736 | continue; |
| 737 | ses_match_to_enclosure(edev, tmp_sdev); |
| 738 | } |
| 739 | |
| 740 | return 0; |
| 741 | |
| 742 | recv_failed: |
| 743 | sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n", |
| 744 | result); |
| 745 | err = -ENODEV; |
| 746 | err_free: |
| 747 | kfree(buf); |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 748 | kfree(scomp); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 749 | kfree(ses_dev->page10); |
| 750 | kfree(ses_dev->page2); |
| 751 | kfree(ses_dev->page1); |
| 752 | err_init_free: |
| 753 | kfree(ses_dev); |
| 754 | kfree(hdr_buf); |
| 755 | sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err); |
| 756 | return err; |
| 757 | } |
| 758 | |
| 759 | static int ses_remove(struct device *dev) |
| 760 | { |
| 761 | return 0; |
| 762 | } |
| 763 | |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 764 | static void ses_intf_remove_component(struct scsi_device *sdev) |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 765 | { |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 766 | struct enclosure_device *edev, *prev = NULL; |
| 767 | |
| 768 | while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) { |
| 769 | prev = edev; |
| 770 | if (!enclosure_remove_device(edev, &sdev->sdev_gendev)) |
| 771 | break; |
| 772 | } |
| 773 | if (edev) |
| 774 | put_device(&edev->edev); |
| 775 | } |
| 776 | |
| 777 | static void ses_intf_remove_enclosure(struct scsi_device *sdev) |
| 778 | { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 779 | struct enclosure_device *edev; |
| 780 | struct ses_device *ses_dev; |
| 781 | |
James Bottomley | 163f52b | 2009-08-01 00:39:36 +0000 | [diff] [blame] | 782 | /* exact match to this enclosure */ |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 783 | edev = enclosure_find(&sdev->sdev_gendev, NULL); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 784 | if (!edev) |
| 785 | return; |
| 786 | |
| 787 | ses_dev = edev->scratch; |
| 788 | edev->scratch = NULL; |
| 789 | |
Yinghai Lu | 7c46c20 | 2008-02-10 23:25:25 -0800 | [diff] [blame] | 790 | kfree(ses_dev->page10); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 791 | kfree(ses_dev->page1); |
| 792 | kfree(ses_dev->page2); |
| 793 | kfree(ses_dev); |
| 794 | |
| 795 | kfree(edev->component[0].scratch); |
| 796 | |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 797 | put_device(&edev->edev); |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 798 | enclosure_unregister(edev); |
| 799 | } |
| 800 | |
James Bottomley | 43d8eb9 | 2009-08-01 00:41:22 +0000 | [diff] [blame] | 801 | static void ses_intf_remove(struct device *cdev, |
| 802 | struct class_interface *intf) |
| 803 | { |
| 804 | struct scsi_device *sdev = to_scsi_device(cdev->parent); |
| 805 | |
| 806 | if (!scsi_device_enclosure(sdev)) |
| 807 | ses_intf_remove_component(sdev); |
| 808 | else |
| 809 | ses_intf_remove_enclosure(sdev); |
| 810 | } |
| 811 | |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 812 | static struct class_interface ses_interface = { |
Tony Jones | ee959b0 | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 813 | .add_dev = ses_intf_add, |
| 814 | .remove_dev = ses_intf_remove, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 815 | }; |
| 816 | |
| 817 | static struct scsi_driver ses_template = { |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 818 | .gendrv = { |
| 819 | .name = "ses", |
Christoph Hellwig | 3af6b35 | 2014-11-12 18:34:51 +0100 | [diff] [blame] | 820 | .owner = THIS_MODULE, |
James Bottomley | 9927c68 | 2008-02-03 15:48:56 -0600 | [diff] [blame] | 821 | .probe = ses_probe, |
| 822 | .remove = ses_remove, |
| 823 | }, |
| 824 | }; |
| 825 | |
| 826 | static int __init ses_init(void) |
| 827 | { |
| 828 | int err; |
| 829 | |
| 830 | err = scsi_register_interface(&ses_interface); |
| 831 | if (err) |
| 832 | return err; |
| 833 | |
| 834 | err = scsi_register_driver(&ses_template.gendrv); |
| 835 | if (err) |
| 836 | goto out_unreg; |
| 837 | |
| 838 | return 0; |
| 839 | |
| 840 | out_unreg: |
| 841 | scsi_unregister_interface(&ses_interface); |
| 842 | return err; |
| 843 | } |
| 844 | |
| 845 | static void __exit ses_exit(void) |
| 846 | { |
| 847 | scsi_unregister_driver(&ses_template.gendrv); |
| 848 | scsi_unregister_interface(&ses_interface); |
| 849 | } |
| 850 | |
| 851 | module_init(ses_init); |
| 852 | module_exit(ses_exit); |
| 853 | |
| 854 | MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE); |
| 855 | |
| 856 | MODULE_AUTHOR("James Bottomley"); |
| 857 | MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver"); |
| 858 | MODULE_LICENSE("GPL v2"); |