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