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