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