| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1 | /* | 
|  | 2 | * Serial Attached SCSI (SAS) Expander discovery and configuration | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2005 Adaptec, Inc.  All rights reserved. | 
|  | 5 | * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> | 
|  | 6 | * | 
|  | 7 | * This file is licensed under GPLv2. | 
|  | 8 | * | 
|  | 9 | * This program is free software; you can redistribute it and/or | 
|  | 10 | * modify it under the terms of the GNU General Public License as | 
|  | 11 | * published by the Free Software Foundation; either version 2 of the | 
|  | 12 | * License, or (at your option) any later version. | 
|  | 13 | * | 
|  | 14 | * This program is distributed in the hope that it will be useful, but | 
|  | 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 17 | * General Public License for more details. | 
|  | 18 | * | 
|  | 19 | * You should have received a copy of the GNU General Public License | 
|  | 20 | * along with this program; if not, write to the Free Software | 
|  | 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | 
|  | 22 | * | 
|  | 23 | */ | 
|  | 24 |  | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 25 | #include <linux/scatterlist.h> | 
|  | 26 |  | 
|  | 27 | #include "sas_internal.h" | 
|  | 28 |  | 
|  | 29 | #include <scsi/scsi_transport.h> | 
|  | 30 | #include <scsi/scsi_transport_sas.h> | 
|  | 31 | #include "../scsi_sas_internal.h" | 
|  | 32 |  | 
|  | 33 | static int sas_discover_expander(struct domain_device *dev); | 
|  | 34 | static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr); | 
|  | 35 | static int sas_configure_phy(struct domain_device *dev, int phy_id, | 
|  | 36 | u8 *sas_addr, int include); | 
|  | 37 | static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr); | 
|  | 38 |  | 
|  | 39 | #if 0 | 
|  | 40 | /* FIXME: smp needs to migrate into the sas class */ | 
| Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 41 | static ssize_t smp_portal_read(struct kobject *, struct bin_attribute *, | 
|  | 42 | char *, loff_t, size_t); | 
|  | 43 | static ssize_t smp_portal_write(struct kobject *, struct bin_attribute *, | 
|  | 44 | char *, loff_t, size_t); | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 45 | #endif | 
|  | 46 |  | 
|  | 47 | /* ---------- SMP task management ---------- */ | 
|  | 48 |  | 
|  | 49 | static void smp_task_timedout(unsigned long _task) | 
|  | 50 | { | 
|  | 51 | struct sas_task *task = (void *) _task; | 
|  | 52 | unsigned long flags; | 
|  | 53 |  | 
|  | 54 | spin_lock_irqsave(&task->task_state_lock, flags); | 
|  | 55 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) | 
|  | 56 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; | 
|  | 57 | spin_unlock_irqrestore(&task->task_state_lock, flags); | 
|  | 58 |  | 
|  | 59 | complete(&task->completion); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | static void smp_task_done(struct sas_task *task) | 
|  | 63 | { | 
|  | 64 | if (!del_timer(&task->timer)) | 
|  | 65 | return; | 
|  | 66 | complete(&task->completion); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | /* Give it some long enough timeout. In seconds. */ | 
|  | 70 | #define SMP_TIMEOUT 10 | 
|  | 71 |  | 
|  | 72 | static int smp_execute_task(struct domain_device *dev, void *req, int req_size, | 
|  | 73 | void *resp, int resp_size) | 
|  | 74 | { | 
| malahal@us.ibm.com | 42961ee | 2006-10-04 17:34:03 -0700 | [diff] [blame] | 75 | int res, retry; | 
|  | 76 | struct sas_task *task = NULL; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 77 | struct sas_internal *i = | 
|  | 78 | to_sas_internal(dev->port->ha->core.shost->transportt); | 
|  | 79 |  | 
| malahal@us.ibm.com | 42961ee | 2006-10-04 17:34:03 -0700 | [diff] [blame] | 80 | for (retry = 0; retry < 3; retry++) { | 
|  | 81 | task = sas_alloc_task(GFP_KERNEL); | 
|  | 82 | if (!task) | 
|  | 83 | return -ENOMEM; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 84 |  | 
| malahal@us.ibm.com | 42961ee | 2006-10-04 17:34:03 -0700 | [diff] [blame] | 85 | task->dev = dev; | 
|  | 86 | task->task_proto = dev->tproto; | 
|  | 87 | sg_init_one(&task->smp_task.smp_req, req, req_size); | 
|  | 88 | sg_init_one(&task->smp_task.smp_resp, resp, resp_size); | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 89 |  | 
| malahal@us.ibm.com | 42961ee | 2006-10-04 17:34:03 -0700 | [diff] [blame] | 90 | task->task_done = smp_task_done; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 91 |  | 
| malahal@us.ibm.com | 42961ee | 2006-10-04 17:34:03 -0700 | [diff] [blame] | 92 | task->timer.data = (unsigned long) task; | 
|  | 93 | task->timer.function = smp_task_timedout; | 
|  | 94 | task->timer.expires = jiffies + SMP_TIMEOUT*HZ; | 
|  | 95 | add_timer(&task->timer); | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 96 |  | 
| malahal@us.ibm.com | 42961ee | 2006-10-04 17:34:03 -0700 | [diff] [blame] | 97 | res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL); | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 98 |  | 
| malahal@us.ibm.com | 42961ee | 2006-10-04 17:34:03 -0700 | [diff] [blame] | 99 | if (res) { | 
|  | 100 | del_timer(&task->timer); | 
|  | 101 | SAS_DPRINTK("executing SMP task failed:%d\n", res); | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 102 | goto ex_err; | 
|  | 103 | } | 
| malahal@us.ibm.com | 42961ee | 2006-10-04 17:34:03 -0700 | [diff] [blame] | 104 |  | 
|  | 105 | wait_for_completion(&task->completion); | 
|  | 106 | res = -ETASK; | 
|  | 107 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { | 
|  | 108 | SAS_DPRINTK("smp task timed out or aborted\n"); | 
|  | 109 | i->dft->lldd_abort_task(task); | 
|  | 110 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { | 
|  | 111 | SAS_DPRINTK("SMP task aborted and not done\n"); | 
|  | 112 | goto ex_err; | 
|  | 113 | } | 
|  | 114 | } | 
|  | 115 | if (task->task_status.resp == SAS_TASK_COMPLETE && | 
|  | 116 | task->task_status.stat == SAM_GOOD) { | 
|  | 117 | res = 0; | 
|  | 118 | break; | 
|  | 119 | } else { | 
|  | 120 | SAS_DPRINTK("%s: task to dev %016llx response: 0x%x " | 
|  | 121 | "status 0x%x\n", __FUNCTION__, | 
|  | 122 | SAS_ADDR(dev->sas_addr), | 
|  | 123 | task->task_status.resp, | 
|  | 124 | task->task_status.stat); | 
|  | 125 | sas_free_task(task); | 
|  | 126 | task = NULL; | 
|  | 127 | } | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 128 | } | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 129 | ex_err: | 
| malahal@us.ibm.com | 42961ee | 2006-10-04 17:34:03 -0700 | [diff] [blame] | 130 | BUG_ON(retry == 3 && task != NULL); | 
|  | 131 | if (task != NULL) { | 
|  | 132 | sas_free_task(task); | 
|  | 133 | } | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 134 | return res; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | /* ---------- Allocations ---------- */ | 
|  | 138 |  | 
|  | 139 | static inline void *alloc_smp_req(int size) | 
|  | 140 | { | 
|  | 141 | u8 *p = kzalloc(size, GFP_KERNEL); | 
|  | 142 | if (p) | 
|  | 143 | p[0] = SMP_REQUEST; | 
|  | 144 | return p; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | static inline void *alloc_smp_resp(int size) | 
|  | 148 | { | 
|  | 149 | return kzalloc(size, GFP_KERNEL); | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | /* ---------- Expander configuration ---------- */ | 
|  | 153 |  | 
|  | 154 | static void sas_set_ex_phy(struct domain_device *dev, int phy_id, | 
|  | 155 | void *disc_resp) | 
|  | 156 | { | 
|  | 157 | struct expander_device *ex = &dev->ex_dev; | 
|  | 158 | struct ex_phy *phy = &ex->ex_phy[phy_id]; | 
|  | 159 | struct smp_resp *resp = disc_resp; | 
|  | 160 | struct discover_resp *dr = &resp->disc; | 
|  | 161 | struct sas_rphy *rphy = dev->rphy; | 
|  | 162 | int rediscover = (phy->phy != NULL); | 
|  | 163 |  | 
|  | 164 | if (!rediscover) { | 
|  | 165 | phy->phy = sas_phy_alloc(&rphy->dev, phy_id); | 
|  | 166 |  | 
|  | 167 | /* FIXME: error_handling */ | 
|  | 168 | BUG_ON(!phy->phy); | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | switch (resp->result) { | 
|  | 172 | case SMP_RESP_PHY_VACANT: | 
|  | 173 | phy->phy_state = PHY_VACANT; | 
|  | 174 | return; | 
|  | 175 | default: | 
|  | 176 | phy->phy_state = PHY_NOT_PRESENT; | 
|  | 177 | return; | 
|  | 178 | case SMP_RESP_FUNC_ACC: | 
|  | 179 | phy->phy_state = PHY_EMPTY; /* do not know yet */ | 
|  | 180 | break; | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | phy->phy_id = phy_id; | 
|  | 184 | phy->attached_dev_type = dr->attached_dev_type; | 
|  | 185 | phy->linkrate = dr->linkrate; | 
|  | 186 | phy->attached_sata_host = dr->attached_sata_host; | 
|  | 187 | phy->attached_sata_dev  = dr->attached_sata_dev; | 
|  | 188 | phy->attached_sata_ps   = dr->attached_sata_ps; | 
|  | 189 | phy->attached_iproto = dr->iproto << 1; | 
|  | 190 | phy->attached_tproto = dr->tproto << 1; | 
|  | 191 | memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE); | 
|  | 192 | phy->attached_phy_id = dr->attached_phy_id; | 
|  | 193 | phy->phy_change_count = dr->change_count; | 
|  | 194 | phy->routing_attr = dr->routing_attr; | 
|  | 195 | phy->virtual = dr->virtual; | 
|  | 196 | phy->last_da_index = -1; | 
|  | 197 |  | 
|  | 198 | phy->phy->identify.initiator_port_protocols = phy->attached_iproto; | 
|  | 199 | phy->phy->identify.target_port_protocols = phy->attached_tproto; | 
|  | 200 | phy->phy->identify.phy_identifier = phy_id; | 
| James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 201 | phy->phy->minimum_linkrate_hw = dr->hmin_linkrate; | 
|  | 202 | phy->phy->maximum_linkrate_hw = dr->hmax_linkrate; | 
|  | 203 | phy->phy->minimum_linkrate = dr->pmin_linkrate; | 
|  | 204 | phy->phy->maximum_linkrate = dr->pmax_linkrate; | 
| James Bottomley | 88edf74 | 2006-09-06 17:36:13 -0500 | [diff] [blame] | 205 | phy->phy->negotiated_linkrate = phy->linkrate; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 206 |  | 
|  | 207 | if (!rediscover) | 
|  | 208 | sas_phy_add(phy->phy); | 
|  | 209 |  | 
|  | 210 | SAS_DPRINTK("ex %016llx phy%02d:%c attached: %016llx\n", | 
|  | 211 | SAS_ADDR(dev->sas_addr), phy->phy_id, | 
|  | 212 | phy->routing_attr == TABLE_ROUTING ? 'T' : | 
|  | 213 | phy->routing_attr == DIRECT_ROUTING ? 'D' : | 
|  | 214 | phy->routing_attr == SUBTRACTIVE_ROUTING ? 'S' : '?', | 
|  | 215 | SAS_ADDR(phy->attached_sas_addr)); | 
|  | 216 |  | 
|  | 217 | return; | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | #define DISCOVER_REQ_SIZE  16 | 
|  | 221 | #define DISCOVER_RESP_SIZE 56 | 
|  | 222 |  | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 223 | static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req, | 
|  | 224 | u8 *disc_resp, int single) | 
|  | 225 | { | 
|  | 226 | int i, res; | 
|  | 227 |  | 
|  | 228 | disc_req[9] = single; | 
|  | 229 | for (i = 1 ; i < 3; i++) { | 
|  | 230 | struct discover_resp *dr; | 
|  | 231 |  | 
|  | 232 | res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE, | 
|  | 233 | disc_resp, DISCOVER_RESP_SIZE); | 
|  | 234 | if (res) | 
|  | 235 | return res; | 
|  | 236 | /* This is detecting a failure to transmit inital | 
|  | 237 | * dev to host FIS as described in section G.5 of | 
|  | 238 | * sas-2 r 04b */ | 
|  | 239 | dr = &((struct smp_resp *)disc_resp)->disc; | 
|  | 240 | if (!(dr->attached_dev_type == 0 && | 
|  | 241 | dr->attached_sata_dev)) | 
|  | 242 | break; | 
|  | 243 | /* In order to generate the dev to host FIS, we | 
|  | 244 | * send a link reset to the expander port */ | 
| James Bottomley | 38e2f03 | 2006-09-07 15:52:09 -0500 | [diff] [blame] | 245 | sas_smp_phy_control(dev, single, PHY_FUNC_LINK_RESET, NULL); | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 246 | /* Wait for the reset to trigger the negotiation */ | 
|  | 247 | msleep(500); | 
|  | 248 | } | 
|  | 249 | sas_set_ex_phy(dev, single, disc_resp); | 
|  | 250 | return 0; | 
|  | 251 | } | 
|  | 252 |  | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 253 | static int sas_ex_phy_discover(struct domain_device *dev, int single) | 
|  | 254 | { | 
|  | 255 | struct expander_device *ex = &dev->ex_dev; | 
|  | 256 | int  res = 0; | 
|  | 257 | u8   *disc_req; | 
|  | 258 | u8   *disc_resp; | 
|  | 259 |  | 
|  | 260 | disc_req = alloc_smp_req(DISCOVER_REQ_SIZE); | 
|  | 261 | if (!disc_req) | 
|  | 262 | return -ENOMEM; | 
|  | 263 |  | 
|  | 264 | disc_resp = alloc_smp_req(DISCOVER_RESP_SIZE); | 
|  | 265 | if (!disc_resp) { | 
|  | 266 | kfree(disc_req); | 
|  | 267 | return -ENOMEM; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | disc_req[1] = SMP_DISCOVER; | 
|  | 271 |  | 
|  | 272 | if (0 <= single && single < ex->num_phys) { | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 273 | res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single); | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 274 | } else { | 
|  | 275 | int i; | 
|  | 276 |  | 
|  | 277 | for (i = 0; i < ex->num_phys; i++) { | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 278 | res = sas_ex_phy_discover_helper(dev, disc_req, | 
|  | 279 | disc_resp, i); | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 280 | if (res) | 
|  | 281 | goto out_err; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 282 | } | 
|  | 283 | } | 
|  | 284 | out_err: | 
|  | 285 | kfree(disc_resp); | 
|  | 286 | kfree(disc_req); | 
|  | 287 | return res; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | static int sas_expander_discover(struct domain_device *dev) | 
|  | 291 | { | 
|  | 292 | struct expander_device *ex = &dev->ex_dev; | 
|  | 293 | int res = -ENOMEM; | 
|  | 294 |  | 
|  | 295 | ex->ex_phy = kzalloc(sizeof(*ex->ex_phy)*ex->num_phys, GFP_KERNEL); | 
|  | 296 | if (!ex->ex_phy) | 
|  | 297 | return -ENOMEM; | 
|  | 298 |  | 
|  | 299 | res = sas_ex_phy_discover(dev, -1); | 
|  | 300 | if (res) | 
|  | 301 | goto out_err; | 
|  | 302 |  | 
|  | 303 | return 0; | 
|  | 304 | out_err: | 
|  | 305 | kfree(ex->ex_phy); | 
|  | 306 | ex->ex_phy = NULL; | 
|  | 307 | return res; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | #define MAX_EXPANDER_PHYS 128 | 
|  | 311 |  | 
|  | 312 | static void ex_assign_report_general(struct domain_device *dev, | 
|  | 313 | struct smp_resp *resp) | 
|  | 314 | { | 
|  | 315 | struct report_general_resp *rg = &resp->rg; | 
|  | 316 |  | 
|  | 317 | dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count); | 
|  | 318 | dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes); | 
|  | 319 | dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS); | 
|  | 320 | dev->ex_dev.conf_route_table = rg->conf_route_table; | 
|  | 321 | dev->ex_dev.configuring = rg->configuring; | 
|  | 322 | memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8); | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | #define RG_REQ_SIZE   8 | 
|  | 326 | #define RG_RESP_SIZE 32 | 
|  | 327 |  | 
|  | 328 | static int sas_ex_general(struct domain_device *dev) | 
|  | 329 | { | 
|  | 330 | u8 *rg_req; | 
|  | 331 | struct smp_resp *rg_resp; | 
|  | 332 | int res; | 
|  | 333 | int i; | 
|  | 334 |  | 
|  | 335 | rg_req = alloc_smp_req(RG_REQ_SIZE); | 
|  | 336 | if (!rg_req) | 
|  | 337 | return -ENOMEM; | 
|  | 338 |  | 
|  | 339 | rg_resp = alloc_smp_resp(RG_RESP_SIZE); | 
|  | 340 | if (!rg_resp) { | 
|  | 341 | kfree(rg_req); | 
|  | 342 | return -ENOMEM; | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | rg_req[1] = SMP_REPORT_GENERAL; | 
|  | 346 |  | 
|  | 347 | for (i = 0; i < 5; i++) { | 
|  | 348 | res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp, | 
|  | 349 | RG_RESP_SIZE); | 
|  | 350 |  | 
|  | 351 | if (res) { | 
|  | 352 | SAS_DPRINTK("RG to ex %016llx failed:0x%x\n", | 
|  | 353 | SAS_ADDR(dev->sas_addr), res); | 
|  | 354 | goto out; | 
|  | 355 | } else if (rg_resp->result != SMP_RESP_FUNC_ACC) { | 
|  | 356 | SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n", | 
|  | 357 | SAS_ADDR(dev->sas_addr), rg_resp->result); | 
|  | 358 | res = rg_resp->result; | 
|  | 359 | goto out; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | ex_assign_report_general(dev, rg_resp); | 
|  | 363 |  | 
|  | 364 | if (dev->ex_dev.configuring) { | 
|  | 365 | SAS_DPRINTK("RG: ex %llx self-configuring...\n", | 
|  | 366 | SAS_ADDR(dev->sas_addr)); | 
|  | 367 | schedule_timeout_interruptible(5*HZ); | 
|  | 368 | } else | 
|  | 369 | break; | 
|  | 370 | } | 
|  | 371 | out: | 
|  | 372 | kfree(rg_req); | 
|  | 373 | kfree(rg_resp); | 
|  | 374 | return res; | 
|  | 375 | } | 
|  | 376 |  | 
|  | 377 | static void ex_assign_manuf_info(struct domain_device *dev, void | 
|  | 378 | *_mi_resp) | 
|  | 379 | { | 
|  | 380 | u8 *mi_resp = _mi_resp; | 
|  | 381 | struct sas_rphy *rphy = dev->rphy; | 
|  | 382 | struct sas_expander_device *edev = rphy_to_expander_device(rphy); | 
|  | 383 |  | 
|  | 384 | memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN); | 
|  | 385 | memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN); | 
|  | 386 | memcpy(edev->product_rev, mi_resp + 36, | 
|  | 387 | SAS_EXPANDER_PRODUCT_REV_LEN); | 
|  | 388 |  | 
|  | 389 | if (mi_resp[8] & 1) { | 
|  | 390 | memcpy(edev->component_vendor_id, mi_resp + 40, | 
|  | 391 | SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN); | 
|  | 392 | edev->component_id = mi_resp[48] << 8 | mi_resp[49]; | 
|  | 393 | edev->component_revision_id = mi_resp[50]; | 
|  | 394 | } | 
|  | 395 | } | 
|  | 396 |  | 
|  | 397 | #define MI_REQ_SIZE   8 | 
|  | 398 | #define MI_RESP_SIZE 64 | 
|  | 399 |  | 
|  | 400 | static int sas_ex_manuf_info(struct domain_device *dev) | 
|  | 401 | { | 
|  | 402 | u8 *mi_req; | 
|  | 403 | u8 *mi_resp; | 
|  | 404 | int res; | 
|  | 405 |  | 
|  | 406 | mi_req = alloc_smp_req(MI_REQ_SIZE); | 
|  | 407 | if (!mi_req) | 
|  | 408 | return -ENOMEM; | 
|  | 409 |  | 
|  | 410 | mi_resp = alloc_smp_resp(MI_RESP_SIZE); | 
|  | 411 | if (!mi_resp) { | 
|  | 412 | kfree(mi_req); | 
|  | 413 | return -ENOMEM; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | mi_req[1] = SMP_REPORT_MANUF_INFO; | 
|  | 417 |  | 
|  | 418 | res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE); | 
|  | 419 | if (res) { | 
|  | 420 | SAS_DPRINTK("MI: ex %016llx failed:0x%x\n", | 
|  | 421 | SAS_ADDR(dev->sas_addr), res); | 
|  | 422 | goto out; | 
|  | 423 | } else if (mi_resp[2] != SMP_RESP_FUNC_ACC) { | 
|  | 424 | SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n", | 
|  | 425 | SAS_ADDR(dev->sas_addr), mi_resp[2]); | 
|  | 426 | goto out; | 
|  | 427 | } | 
|  | 428 |  | 
|  | 429 | ex_assign_manuf_info(dev, mi_resp); | 
|  | 430 | out: | 
|  | 431 | kfree(mi_req); | 
|  | 432 | kfree(mi_resp); | 
|  | 433 | return res; | 
|  | 434 | } | 
|  | 435 |  | 
|  | 436 | #define PC_REQ_SIZE  44 | 
|  | 437 | #define PC_RESP_SIZE 8 | 
|  | 438 |  | 
|  | 439 | int sas_smp_phy_control(struct domain_device *dev, int phy_id, | 
| James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 440 | enum phy_func phy_func, | 
|  | 441 | struct sas_phy_linkrates *rates) | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 442 | { | 
|  | 443 | u8 *pc_req; | 
|  | 444 | u8 *pc_resp; | 
|  | 445 | int res; | 
|  | 446 |  | 
|  | 447 | pc_req = alloc_smp_req(PC_REQ_SIZE); | 
|  | 448 | if (!pc_req) | 
|  | 449 | return -ENOMEM; | 
|  | 450 |  | 
|  | 451 | pc_resp = alloc_smp_resp(PC_RESP_SIZE); | 
|  | 452 | if (!pc_resp) { | 
|  | 453 | kfree(pc_req); | 
|  | 454 | return -ENOMEM; | 
|  | 455 | } | 
|  | 456 |  | 
|  | 457 | pc_req[1] = SMP_PHY_CONTROL; | 
|  | 458 | pc_req[9] = phy_id; | 
|  | 459 | pc_req[10]= phy_func; | 
| James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 460 | if (rates) { | 
|  | 461 | pc_req[32] = rates->minimum_linkrate << 4; | 
|  | 462 | pc_req[33] = rates->maximum_linkrate << 4; | 
|  | 463 | } | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 464 |  | 
|  | 465 | res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE); | 
|  | 466 |  | 
|  | 467 | kfree(pc_resp); | 
|  | 468 | kfree(pc_req); | 
|  | 469 | return res; | 
|  | 470 | } | 
|  | 471 |  | 
|  | 472 | static void sas_ex_disable_phy(struct domain_device *dev, int phy_id) | 
|  | 473 | { | 
|  | 474 | struct expander_device *ex = &dev->ex_dev; | 
|  | 475 | struct ex_phy *phy = &ex->ex_phy[phy_id]; | 
|  | 476 |  | 
| James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 477 | sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL); | 
| James Bottomley | 88edf74 | 2006-09-06 17:36:13 -0500 | [diff] [blame] | 478 | phy->linkrate = SAS_PHY_DISABLED; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 479 | } | 
|  | 480 |  | 
|  | 481 | static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr) | 
|  | 482 | { | 
|  | 483 | struct expander_device *ex = &dev->ex_dev; | 
|  | 484 | int i; | 
|  | 485 |  | 
|  | 486 | for (i = 0; i < ex->num_phys; i++) { | 
|  | 487 | struct ex_phy *phy = &ex->ex_phy[i]; | 
|  | 488 |  | 
|  | 489 | if (phy->phy_state == PHY_VACANT || | 
|  | 490 | phy->phy_state == PHY_NOT_PRESENT) | 
|  | 491 | continue; | 
|  | 492 |  | 
|  | 493 | if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr)) | 
|  | 494 | sas_ex_disable_phy(dev, i); | 
|  | 495 | } | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | static int sas_dev_present_in_domain(struct asd_sas_port *port, | 
|  | 499 | u8 *sas_addr) | 
|  | 500 | { | 
|  | 501 | struct domain_device *dev; | 
|  | 502 |  | 
|  | 503 | if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr)) | 
|  | 504 | return 1; | 
|  | 505 | list_for_each_entry(dev, &port->dev_list, dev_list_node) { | 
|  | 506 | if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr)) | 
|  | 507 | return 1; | 
|  | 508 | } | 
|  | 509 | return 0; | 
|  | 510 | } | 
|  | 511 |  | 
|  | 512 | #define RPEL_REQ_SIZE	16 | 
|  | 513 | #define RPEL_RESP_SIZE	32 | 
|  | 514 | int sas_smp_get_phy_events(struct sas_phy *phy) | 
|  | 515 | { | 
|  | 516 | int res; | 
|  | 517 | struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); | 
|  | 518 | struct domain_device *dev = sas_find_dev_by_rphy(rphy); | 
|  | 519 | u8 *req = alloc_smp_req(RPEL_REQ_SIZE); | 
|  | 520 | u8 *resp = kzalloc(RPEL_RESP_SIZE, GFP_KERNEL); | 
|  | 521 |  | 
|  | 522 | if (!resp) | 
|  | 523 | return -ENOMEM; | 
|  | 524 |  | 
|  | 525 | req[1] = SMP_REPORT_PHY_ERR_LOG; | 
|  | 526 | req[9] = phy->number; | 
|  | 527 |  | 
|  | 528 | res = smp_execute_task(dev, req, RPEL_REQ_SIZE, | 
|  | 529 | resp, RPEL_RESP_SIZE); | 
|  | 530 |  | 
|  | 531 | if (!res) | 
|  | 532 | goto out; | 
|  | 533 |  | 
|  | 534 | phy->invalid_dword_count = scsi_to_u32(&resp[12]); | 
|  | 535 | phy->running_disparity_error_count = scsi_to_u32(&resp[16]); | 
|  | 536 | phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]); | 
|  | 537 | phy->phy_reset_problem_count = scsi_to_u32(&resp[24]); | 
|  | 538 |  | 
|  | 539 | out: | 
|  | 540 | kfree(resp); | 
|  | 541 | return res; | 
|  | 542 |  | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | #define RPS_REQ_SIZE  16 | 
|  | 546 | #define RPS_RESP_SIZE 60 | 
|  | 547 |  | 
|  | 548 | static int sas_get_report_phy_sata(struct domain_device *dev, | 
|  | 549 | int phy_id, | 
|  | 550 | struct smp_resp *rps_resp) | 
|  | 551 | { | 
|  | 552 | int res; | 
|  | 553 | u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE); | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 554 | u8 *resp = (u8 *)rps_resp; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 555 |  | 
|  | 556 | if (!rps_req) | 
|  | 557 | return -ENOMEM; | 
|  | 558 |  | 
|  | 559 | rps_req[1] = SMP_REPORT_PHY_SATA; | 
|  | 560 | rps_req[9] = phy_id; | 
|  | 561 |  | 
|  | 562 | res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE, | 
|  | 563 | rps_resp, RPS_RESP_SIZE); | 
|  | 564 |  | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 565 | /* 0x34 is the FIS type for the D2H fis.  There's a potential | 
|  | 566 | * standards cockup here.  sas-2 explicitly specifies the FIS | 
|  | 567 | * should be encoded so that FIS type is in resp[24]. | 
|  | 568 | * However, some expanders endian reverse this.  Undo the | 
|  | 569 | * reversal here */ | 
|  | 570 | if (!res && resp[27] == 0x34 && resp[24] != 0x34) { | 
|  | 571 | int i; | 
|  | 572 |  | 
|  | 573 | for (i = 0; i < 5; i++) { | 
|  | 574 | int j = 24 + (i*4); | 
|  | 575 | u8 a, b; | 
|  | 576 | a = resp[j + 0]; | 
|  | 577 | b = resp[j + 1]; | 
|  | 578 | resp[j + 0] = resp[j + 3]; | 
|  | 579 | resp[j + 1] = resp[j + 2]; | 
|  | 580 | resp[j + 2] = b; | 
|  | 581 | resp[j + 3] = a; | 
|  | 582 | } | 
|  | 583 | } | 
|  | 584 |  | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 585 | kfree(rps_req); | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 586 | return res; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 587 | } | 
|  | 588 |  | 
|  | 589 | static void sas_ex_get_linkrate(struct domain_device *parent, | 
|  | 590 | struct domain_device *child, | 
|  | 591 | struct ex_phy *parent_phy) | 
|  | 592 | { | 
|  | 593 | struct expander_device *parent_ex = &parent->ex_dev; | 
|  | 594 | struct sas_port *port; | 
|  | 595 | int i; | 
|  | 596 |  | 
|  | 597 | child->pathways = 0; | 
|  | 598 |  | 
|  | 599 | port = parent_phy->port; | 
|  | 600 |  | 
|  | 601 | for (i = 0; i < parent_ex->num_phys; i++) { | 
|  | 602 | struct ex_phy *phy = &parent_ex->ex_phy[i]; | 
|  | 603 |  | 
|  | 604 | if (phy->phy_state == PHY_VACANT || | 
|  | 605 | phy->phy_state == PHY_NOT_PRESENT) | 
|  | 606 | continue; | 
|  | 607 |  | 
|  | 608 | if (SAS_ADDR(phy->attached_sas_addr) == | 
|  | 609 | SAS_ADDR(child->sas_addr)) { | 
|  | 610 |  | 
|  | 611 | child->min_linkrate = min(parent->min_linkrate, | 
|  | 612 | phy->linkrate); | 
|  | 613 | child->max_linkrate = max(parent->max_linkrate, | 
|  | 614 | phy->linkrate); | 
|  | 615 | child->pathways++; | 
|  | 616 | sas_port_add_phy(port, phy->phy); | 
|  | 617 | } | 
|  | 618 | } | 
|  | 619 | child->linkrate = min(parent_phy->linkrate, child->max_linkrate); | 
|  | 620 | child->pathways = min(child->pathways, parent->pathways); | 
|  | 621 | } | 
|  | 622 |  | 
|  | 623 | static struct domain_device *sas_ex_discover_end_dev( | 
|  | 624 | struct domain_device *parent, int phy_id) | 
|  | 625 | { | 
|  | 626 | struct expander_device *parent_ex = &parent->ex_dev; | 
|  | 627 | struct ex_phy *phy = &parent_ex->ex_phy[phy_id]; | 
|  | 628 | struct domain_device *child = NULL; | 
|  | 629 | struct sas_rphy *rphy; | 
|  | 630 | int res; | 
|  | 631 |  | 
|  | 632 | if (phy->attached_sata_host || phy->attached_sata_ps) | 
|  | 633 | return NULL; | 
|  | 634 |  | 
|  | 635 | child = kzalloc(sizeof(*child), GFP_KERNEL); | 
|  | 636 | if (!child) | 
|  | 637 | return NULL; | 
|  | 638 |  | 
|  | 639 | child->parent = parent; | 
|  | 640 | child->port   = parent->port; | 
|  | 641 | child->iproto = phy->attached_iproto; | 
|  | 642 | memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE); | 
|  | 643 | sas_hash_addr(child->hashed_sas_addr, child->sas_addr); | 
| James Bottomley | 024879e | 2006-11-15 18:03:07 -0600 | [diff] [blame] | 644 | if (!phy->port) { | 
|  | 645 | phy->port = sas_port_alloc(&parent->rphy->dev, phy_id); | 
|  | 646 | if (unlikely(!phy->port)) | 
|  | 647 | goto out_err; | 
|  | 648 | if (unlikely(sas_port_add(phy->port) != 0)) { | 
|  | 649 | sas_port_free(phy->port); | 
|  | 650 | goto out_err; | 
|  | 651 | } | 
|  | 652 | } | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 653 | sas_ex_get_linkrate(parent, child, phy); | 
|  | 654 |  | 
|  | 655 | if ((phy->attached_tproto & SAS_PROTO_STP) || phy->attached_sata_dev) { | 
|  | 656 | child->dev_type = SATA_DEV; | 
|  | 657 | if (phy->attached_tproto & SAS_PROTO_STP) | 
|  | 658 | child->tproto = phy->attached_tproto; | 
|  | 659 | if (phy->attached_sata_dev) | 
|  | 660 | child->tproto |= SATA_DEV; | 
|  | 661 | res = sas_get_report_phy_sata(parent, phy_id, | 
|  | 662 | &child->sata_dev.rps_resp); | 
|  | 663 | if (res) { | 
|  | 664 | SAS_DPRINTK("report phy sata to %016llx:0x%x returned " | 
|  | 665 | "0x%x\n", SAS_ADDR(parent->sas_addr), | 
|  | 666 | phy_id, res); | 
| James Bottomley | 024879e | 2006-11-15 18:03:07 -0600 | [diff] [blame] | 667 | goto out_free; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 668 | } | 
|  | 669 | memcpy(child->frame_rcvd, &child->sata_dev.rps_resp.rps.fis, | 
|  | 670 | sizeof(struct dev_to_host_fis)); | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 671 |  | 
|  | 672 | rphy = sas_end_device_alloc(phy->port); | 
| James Bottomley | 528fd55 | 2006-10-16 10:57:05 -0500 | [diff] [blame] | 673 | if (unlikely(!rphy)) | 
|  | 674 | goto out_free; | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 675 |  | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 676 | sas_init_dev(child); | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 677 |  | 
|  | 678 | child->rphy = rphy; | 
|  | 679 |  | 
|  | 680 | spin_lock(&parent->port->dev_list_lock); | 
|  | 681 | list_add_tail(&child->dev_list_node, &parent->port->dev_list); | 
|  | 682 | spin_unlock(&parent->port->dev_list_lock); | 
|  | 683 |  | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 684 | res = sas_discover_sata(child); | 
|  | 685 | if (res) { | 
|  | 686 | SAS_DPRINTK("sas_discover_sata() for device %16llx at " | 
|  | 687 | "%016llx:0x%x returned 0x%x\n", | 
|  | 688 | SAS_ADDR(child->sas_addr), | 
|  | 689 | SAS_ADDR(parent->sas_addr), phy_id, res); | 
| James Bottomley | 1acce19 | 2006-08-22 12:39:19 -0500 | [diff] [blame] | 690 | goto out_list_del; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 691 | } | 
|  | 692 | } else if (phy->attached_tproto & SAS_PROTO_SSP) { | 
|  | 693 | child->dev_type = SAS_END_DEV; | 
|  | 694 | rphy = sas_end_device_alloc(phy->port); | 
|  | 695 | /* FIXME: error handling */ | 
| James Bottomley | 024879e | 2006-11-15 18:03:07 -0600 | [diff] [blame] | 696 | if (unlikely(!rphy)) | 
|  | 697 | goto out_free; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 698 | child->tproto = phy->attached_tproto; | 
|  | 699 | sas_init_dev(child); | 
|  | 700 |  | 
|  | 701 | child->rphy = rphy; | 
|  | 702 | sas_fill_in_rphy(child, rphy); | 
|  | 703 |  | 
|  | 704 | spin_lock(&parent->port->dev_list_lock); | 
|  | 705 | list_add_tail(&child->dev_list_node, &parent->port->dev_list); | 
|  | 706 | spin_unlock(&parent->port->dev_list_lock); | 
|  | 707 |  | 
|  | 708 | res = sas_discover_end_dev(child); | 
|  | 709 | if (res) { | 
|  | 710 | SAS_DPRINTK("sas_discover_end_dev() for device %16llx " | 
|  | 711 | "at %016llx:0x%x returned 0x%x\n", | 
|  | 712 | SAS_ADDR(child->sas_addr), | 
|  | 713 | SAS_ADDR(parent->sas_addr), phy_id, res); | 
| James Bottomley | 024879e | 2006-11-15 18:03:07 -0600 | [diff] [blame] | 714 | goto out_list_del; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 715 | } | 
|  | 716 | } else { | 
|  | 717 | SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n", | 
|  | 718 | phy->attached_tproto, SAS_ADDR(parent->sas_addr), | 
|  | 719 | phy_id); | 
|  | 720 | } | 
|  | 721 |  | 
|  | 722 | list_add_tail(&child->siblings, &parent_ex->children); | 
|  | 723 | return child; | 
| James Bottomley | 024879e | 2006-11-15 18:03:07 -0600 | [diff] [blame] | 724 |  | 
|  | 725 | out_list_del: | 
| Darrick J. Wong | 6f63caa | 2007-01-26 14:08:43 -0800 | [diff] [blame] | 726 | sas_rphy_free(child->rphy); | 
|  | 727 | child->rphy = NULL; | 
| James Bottomley | 024879e | 2006-11-15 18:03:07 -0600 | [diff] [blame] | 728 | list_del(&child->dev_list_node); | 
| James Bottomley | 024879e | 2006-11-15 18:03:07 -0600 | [diff] [blame] | 729 | out_free: | 
|  | 730 | sas_port_delete(phy->port); | 
|  | 731 | out_err: | 
|  | 732 | phy->port = NULL; | 
|  | 733 | kfree(child); | 
|  | 734 | return NULL; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 735 | } | 
|  | 736 |  | 
| Darrick J. Wong | 423f7cf | 2007-01-30 12:07:27 -0800 | [diff] [blame] | 737 | /* See if this phy is part of a wide port */ | 
|  | 738 | static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id) | 
|  | 739 | { | 
|  | 740 | struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id]; | 
|  | 741 | int i; | 
|  | 742 |  | 
|  | 743 | for (i = 0; i < parent->ex_dev.num_phys; i++) { | 
|  | 744 | struct ex_phy *ephy = &parent->ex_dev.ex_phy[i]; | 
|  | 745 |  | 
|  | 746 | if (ephy == phy) | 
|  | 747 | continue; | 
|  | 748 |  | 
|  | 749 | if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr, | 
|  | 750 | SAS_ADDR_SIZE) && ephy->port) { | 
|  | 751 | sas_port_add_phy(ephy->port, phy->phy); | 
|  | 752 | phy->phy_state = PHY_DEVICE_DISCOVERED; | 
|  | 753 | return 0; | 
|  | 754 | } | 
|  | 755 | } | 
|  | 756 |  | 
|  | 757 | return -ENODEV; | 
|  | 758 | } | 
|  | 759 |  | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 760 | static struct domain_device *sas_ex_discover_expander( | 
|  | 761 | struct domain_device *parent, int phy_id) | 
|  | 762 | { | 
|  | 763 | struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy); | 
|  | 764 | struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id]; | 
|  | 765 | struct domain_device *child = NULL; | 
|  | 766 | struct sas_rphy *rphy; | 
|  | 767 | struct sas_expander_device *edev; | 
|  | 768 | struct asd_sas_port *port; | 
|  | 769 | int res; | 
|  | 770 |  | 
|  | 771 | if (phy->routing_attr == DIRECT_ROUTING) { | 
|  | 772 | SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not " | 
|  | 773 | "allowed\n", | 
|  | 774 | SAS_ADDR(parent->sas_addr), phy_id, | 
|  | 775 | SAS_ADDR(phy->attached_sas_addr), | 
|  | 776 | phy->attached_phy_id); | 
|  | 777 | return NULL; | 
|  | 778 | } | 
|  | 779 | child = kzalloc(sizeof(*child), GFP_KERNEL); | 
|  | 780 | if (!child) | 
|  | 781 | return NULL; | 
|  | 782 |  | 
|  | 783 | phy->port = sas_port_alloc(&parent->rphy->dev, phy_id); | 
|  | 784 | /* FIXME: better error handling */ | 
|  | 785 | BUG_ON(sas_port_add(phy->port) != 0); | 
|  | 786 |  | 
|  | 787 |  | 
|  | 788 | switch (phy->attached_dev_type) { | 
|  | 789 | case EDGE_DEV: | 
|  | 790 | rphy = sas_expander_alloc(phy->port, | 
|  | 791 | SAS_EDGE_EXPANDER_DEVICE); | 
|  | 792 | break; | 
|  | 793 | case FANOUT_DEV: | 
|  | 794 | rphy = sas_expander_alloc(phy->port, | 
|  | 795 | SAS_FANOUT_EXPANDER_DEVICE); | 
|  | 796 | break; | 
|  | 797 | default: | 
|  | 798 | rphy = NULL;	/* shut gcc up */ | 
|  | 799 | BUG(); | 
|  | 800 | } | 
|  | 801 | port = parent->port; | 
|  | 802 | child->rphy = rphy; | 
|  | 803 | edev = rphy_to_expander_device(rphy); | 
|  | 804 | child->dev_type = phy->attached_dev_type; | 
|  | 805 | child->parent = parent; | 
|  | 806 | child->port = port; | 
|  | 807 | child->iproto = phy->attached_iproto; | 
|  | 808 | child->tproto = phy->attached_tproto; | 
|  | 809 | memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE); | 
|  | 810 | sas_hash_addr(child->hashed_sas_addr, child->sas_addr); | 
|  | 811 | sas_ex_get_linkrate(parent, child, phy); | 
|  | 812 | edev->level = parent_ex->level + 1; | 
|  | 813 | parent->port->disc.max_level = max(parent->port->disc.max_level, | 
|  | 814 | edev->level); | 
|  | 815 | sas_init_dev(child); | 
|  | 816 | sas_fill_in_rphy(child, rphy); | 
|  | 817 | sas_rphy_add(rphy); | 
|  | 818 |  | 
|  | 819 | spin_lock(&parent->port->dev_list_lock); | 
|  | 820 | list_add_tail(&child->dev_list_node, &parent->port->dev_list); | 
|  | 821 | spin_unlock(&parent->port->dev_list_lock); | 
|  | 822 |  | 
|  | 823 | res = sas_discover_expander(child); | 
|  | 824 | if (res) { | 
|  | 825 | kfree(child); | 
|  | 826 | return NULL; | 
|  | 827 | } | 
|  | 828 | list_add_tail(&child->siblings, &parent->ex_dev.children); | 
|  | 829 | return child; | 
|  | 830 | } | 
|  | 831 |  | 
|  | 832 | static int sas_ex_discover_dev(struct domain_device *dev, int phy_id) | 
|  | 833 | { | 
|  | 834 | struct expander_device *ex = &dev->ex_dev; | 
|  | 835 | struct ex_phy *ex_phy = &ex->ex_phy[phy_id]; | 
|  | 836 | struct domain_device *child = NULL; | 
|  | 837 | int res = 0; | 
|  | 838 |  | 
|  | 839 | /* Phy state */ | 
| James Bottomley | 88edf74 | 2006-09-06 17:36:13 -0500 | [diff] [blame] | 840 | if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) { | 
| James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 841 | if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL)) | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 842 | res = sas_ex_phy_discover(dev, phy_id); | 
|  | 843 | if (res) | 
|  | 844 | return res; | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | /* Parent and domain coherency */ | 
|  | 848 | if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) == | 
|  | 849 | SAS_ADDR(dev->port->sas_addr))) { | 
|  | 850 | sas_add_parent_port(dev, phy_id); | 
|  | 851 | return 0; | 
|  | 852 | } | 
|  | 853 | if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) == | 
|  | 854 | SAS_ADDR(dev->parent->sas_addr))) { | 
|  | 855 | sas_add_parent_port(dev, phy_id); | 
|  | 856 | if (ex_phy->routing_attr == TABLE_ROUTING) | 
|  | 857 | sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1); | 
|  | 858 | return 0; | 
|  | 859 | } | 
|  | 860 |  | 
|  | 861 | if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr)) | 
|  | 862 | sas_ex_disable_port(dev, ex_phy->attached_sas_addr); | 
|  | 863 |  | 
|  | 864 | if (ex_phy->attached_dev_type == NO_DEVICE) { | 
|  | 865 | if (ex_phy->routing_attr == DIRECT_ROUTING) { | 
|  | 866 | memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE); | 
|  | 867 | sas_configure_routing(dev, ex_phy->attached_sas_addr); | 
|  | 868 | } | 
|  | 869 | return 0; | 
| James Bottomley | 88edf74 | 2006-09-06 17:36:13 -0500 | [diff] [blame] | 870 | } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN) | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 871 | return 0; | 
|  | 872 |  | 
|  | 873 | if (ex_phy->attached_dev_type != SAS_END_DEV && | 
|  | 874 | ex_phy->attached_dev_type != FANOUT_DEV && | 
|  | 875 | ex_phy->attached_dev_type != EDGE_DEV) { | 
|  | 876 | SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx " | 
|  | 877 | "phy 0x%x\n", ex_phy->attached_dev_type, | 
|  | 878 | SAS_ADDR(dev->sas_addr), | 
|  | 879 | phy_id); | 
|  | 880 | return 0; | 
|  | 881 | } | 
|  | 882 |  | 
|  | 883 | res = sas_configure_routing(dev, ex_phy->attached_sas_addr); | 
|  | 884 | if (res) { | 
|  | 885 | SAS_DPRINTK("configure routing for dev %016llx " | 
|  | 886 | "reported 0x%x. Forgotten\n", | 
|  | 887 | SAS_ADDR(ex_phy->attached_sas_addr), res); | 
|  | 888 | sas_disable_routing(dev, ex_phy->attached_sas_addr); | 
|  | 889 | return res; | 
|  | 890 | } | 
|  | 891 |  | 
| Darrick J. Wong | 423f7cf | 2007-01-30 12:07:27 -0800 | [diff] [blame] | 892 | res = sas_ex_join_wide_port(dev, phy_id); | 
|  | 893 | if (!res) { | 
|  | 894 | SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n", | 
|  | 895 | phy_id, SAS_ADDR(ex_phy->attached_sas_addr)); | 
|  | 896 | return res; | 
|  | 897 | } | 
|  | 898 |  | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 899 | switch (ex_phy->attached_dev_type) { | 
|  | 900 | case SAS_END_DEV: | 
|  | 901 | child = sas_ex_discover_end_dev(dev, phy_id); | 
|  | 902 | break; | 
|  | 903 | case FANOUT_DEV: | 
|  | 904 | if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) { | 
|  | 905 | SAS_DPRINTK("second fanout expander %016llx phy 0x%x " | 
|  | 906 | "attached to ex %016llx phy 0x%x\n", | 
|  | 907 | SAS_ADDR(ex_phy->attached_sas_addr), | 
|  | 908 | ex_phy->attached_phy_id, | 
|  | 909 | SAS_ADDR(dev->sas_addr), | 
|  | 910 | phy_id); | 
|  | 911 | sas_ex_disable_phy(dev, phy_id); | 
|  | 912 | break; | 
|  | 913 | } else | 
|  | 914 | memcpy(dev->port->disc.fanout_sas_addr, | 
|  | 915 | ex_phy->attached_sas_addr, SAS_ADDR_SIZE); | 
|  | 916 | /* fallthrough */ | 
|  | 917 | case EDGE_DEV: | 
|  | 918 | child = sas_ex_discover_expander(dev, phy_id); | 
|  | 919 | break; | 
|  | 920 | default: | 
|  | 921 | break; | 
|  | 922 | } | 
|  | 923 |  | 
|  | 924 | if (child) { | 
|  | 925 | int i; | 
|  | 926 |  | 
|  | 927 | for (i = 0; i < ex->num_phys; i++) { | 
|  | 928 | if (ex->ex_phy[i].phy_state == PHY_VACANT || | 
|  | 929 | ex->ex_phy[i].phy_state == PHY_NOT_PRESENT) | 
|  | 930 | continue; | 
|  | 931 |  | 
|  | 932 | if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) == | 
|  | 933 | SAS_ADDR(child->sas_addr)) | 
|  | 934 | ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED; | 
|  | 935 | } | 
|  | 936 | } | 
|  | 937 |  | 
|  | 938 | return res; | 
|  | 939 | } | 
|  | 940 |  | 
|  | 941 | static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr) | 
|  | 942 | { | 
|  | 943 | struct expander_device *ex = &dev->ex_dev; | 
|  | 944 | int i; | 
|  | 945 |  | 
|  | 946 | for (i = 0; i < ex->num_phys; i++) { | 
|  | 947 | struct ex_phy *phy = &ex->ex_phy[i]; | 
|  | 948 |  | 
|  | 949 | if (phy->phy_state == PHY_VACANT || | 
|  | 950 | phy->phy_state == PHY_NOT_PRESENT) | 
|  | 951 | continue; | 
|  | 952 |  | 
|  | 953 | if ((phy->attached_dev_type == EDGE_DEV || | 
|  | 954 | phy->attached_dev_type == FANOUT_DEV) && | 
|  | 955 | phy->routing_attr == SUBTRACTIVE_ROUTING) { | 
|  | 956 |  | 
|  | 957 | memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE); | 
|  | 958 |  | 
|  | 959 | return 1; | 
|  | 960 | } | 
|  | 961 | } | 
|  | 962 | return 0; | 
|  | 963 | } | 
|  | 964 |  | 
|  | 965 | static int sas_check_level_subtractive_boundary(struct domain_device *dev) | 
|  | 966 | { | 
|  | 967 | struct expander_device *ex = &dev->ex_dev; | 
|  | 968 | struct domain_device *child; | 
|  | 969 | u8 sub_addr[8] = {0, }; | 
|  | 970 |  | 
|  | 971 | list_for_each_entry(child, &ex->children, siblings) { | 
|  | 972 | if (child->dev_type != EDGE_DEV && | 
|  | 973 | child->dev_type != FANOUT_DEV) | 
|  | 974 | continue; | 
|  | 975 | if (sub_addr[0] == 0) { | 
|  | 976 | sas_find_sub_addr(child, sub_addr); | 
|  | 977 | continue; | 
|  | 978 | } else { | 
|  | 979 | u8 s2[8]; | 
|  | 980 |  | 
|  | 981 | if (sas_find_sub_addr(child, s2) && | 
|  | 982 | (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) { | 
|  | 983 |  | 
|  | 984 | SAS_DPRINTK("ex %016llx->%016llx-?->%016llx " | 
|  | 985 | "diverges from subtractive " | 
|  | 986 | "boundary %016llx\n", | 
|  | 987 | SAS_ADDR(dev->sas_addr), | 
|  | 988 | SAS_ADDR(child->sas_addr), | 
|  | 989 | SAS_ADDR(s2), | 
|  | 990 | SAS_ADDR(sub_addr)); | 
|  | 991 |  | 
|  | 992 | sas_ex_disable_port(child, s2); | 
|  | 993 | } | 
|  | 994 | } | 
|  | 995 | } | 
|  | 996 | return 0; | 
|  | 997 | } | 
|  | 998 | /** | 
|  | 999 | * sas_ex_discover_devices -- discover devices attached to this expander | 
|  | 1000 | * dev: pointer to the expander domain device | 
|  | 1001 | * single: if you want to do a single phy, else set to -1; | 
|  | 1002 | * | 
|  | 1003 | * Configure this expander for use with its devices and register the | 
|  | 1004 | * devices of this expander. | 
|  | 1005 | */ | 
|  | 1006 | static int sas_ex_discover_devices(struct domain_device *dev, int single) | 
|  | 1007 | { | 
|  | 1008 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1009 | int i = 0, end = ex->num_phys; | 
|  | 1010 | int res = 0; | 
|  | 1011 |  | 
|  | 1012 | if (0 <= single && single < end) { | 
|  | 1013 | i = single; | 
|  | 1014 | end = i+1; | 
|  | 1015 | } | 
|  | 1016 |  | 
|  | 1017 | for ( ; i < end; i++) { | 
|  | 1018 | struct ex_phy *ex_phy = &ex->ex_phy[i]; | 
|  | 1019 |  | 
|  | 1020 | if (ex_phy->phy_state == PHY_VACANT || | 
|  | 1021 | ex_phy->phy_state == PHY_NOT_PRESENT || | 
|  | 1022 | ex_phy->phy_state == PHY_DEVICE_DISCOVERED) | 
|  | 1023 | continue; | 
|  | 1024 |  | 
|  | 1025 | switch (ex_phy->linkrate) { | 
| James Bottomley | 88edf74 | 2006-09-06 17:36:13 -0500 | [diff] [blame] | 1026 | case SAS_PHY_DISABLED: | 
|  | 1027 | case SAS_PHY_RESET_PROBLEM: | 
|  | 1028 | case SAS_SATA_PORT_SELECTOR: | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1029 | continue; | 
|  | 1030 | default: | 
|  | 1031 | res = sas_ex_discover_dev(dev, i); | 
|  | 1032 | if (res) | 
|  | 1033 | break; | 
|  | 1034 | continue; | 
|  | 1035 | } | 
|  | 1036 | } | 
|  | 1037 |  | 
|  | 1038 | if (!res) | 
|  | 1039 | sas_check_level_subtractive_boundary(dev); | 
|  | 1040 |  | 
|  | 1041 | return res; | 
|  | 1042 | } | 
|  | 1043 |  | 
|  | 1044 | static int sas_check_ex_subtractive_boundary(struct domain_device *dev) | 
|  | 1045 | { | 
|  | 1046 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1047 | int i; | 
|  | 1048 | u8  *sub_sas_addr = NULL; | 
|  | 1049 |  | 
|  | 1050 | if (dev->dev_type != EDGE_DEV) | 
|  | 1051 | return 0; | 
|  | 1052 |  | 
|  | 1053 | for (i = 0; i < ex->num_phys; i++) { | 
|  | 1054 | struct ex_phy *phy = &ex->ex_phy[i]; | 
|  | 1055 |  | 
|  | 1056 | if (phy->phy_state == PHY_VACANT || | 
|  | 1057 | phy->phy_state == PHY_NOT_PRESENT) | 
|  | 1058 | continue; | 
|  | 1059 |  | 
|  | 1060 | if ((phy->attached_dev_type == FANOUT_DEV || | 
|  | 1061 | phy->attached_dev_type == EDGE_DEV) && | 
|  | 1062 | phy->routing_attr == SUBTRACTIVE_ROUTING) { | 
|  | 1063 |  | 
|  | 1064 | if (!sub_sas_addr) | 
|  | 1065 | sub_sas_addr = &phy->attached_sas_addr[0]; | 
|  | 1066 | else if (SAS_ADDR(sub_sas_addr) != | 
|  | 1067 | SAS_ADDR(phy->attached_sas_addr)) { | 
|  | 1068 |  | 
|  | 1069 | SAS_DPRINTK("ex %016llx phy 0x%x " | 
|  | 1070 | "diverges(%016llx) on subtractive " | 
|  | 1071 | "boundary(%016llx). Disabled\n", | 
|  | 1072 | SAS_ADDR(dev->sas_addr), i, | 
|  | 1073 | SAS_ADDR(phy->attached_sas_addr), | 
|  | 1074 | SAS_ADDR(sub_sas_addr)); | 
|  | 1075 | sas_ex_disable_phy(dev, i); | 
|  | 1076 | } | 
|  | 1077 | } | 
|  | 1078 | } | 
|  | 1079 | return 0; | 
|  | 1080 | } | 
|  | 1081 |  | 
|  | 1082 | static void sas_print_parent_topology_bug(struct domain_device *child, | 
|  | 1083 | struct ex_phy *parent_phy, | 
|  | 1084 | struct ex_phy *child_phy) | 
|  | 1085 | { | 
|  | 1086 | static const char ra_char[] = { | 
|  | 1087 | [DIRECT_ROUTING] = 'D', | 
|  | 1088 | [SUBTRACTIVE_ROUTING] = 'S', | 
|  | 1089 | [TABLE_ROUTING] = 'T', | 
|  | 1090 | }; | 
|  | 1091 | static const char *ex_type[] = { | 
|  | 1092 | [EDGE_DEV] = "edge", | 
|  | 1093 | [FANOUT_DEV] = "fanout", | 
|  | 1094 | }; | 
|  | 1095 | struct domain_device *parent = child->parent; | 
|  | 1096 |  | 
|  | 1097 | sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx phy 0x%x " | 
|  | 1098 | "has %c:%c routing link!\n", | 
|  | 1099 |  | 
|  | 1100 | ex_type[parent->dev_type], | 
|  | 1101 | SAS_ADDR(parent->sas_addr), | 
|  | 1102 | parent_phy->phy_id, | 
|  | 1103 |  | 
|  | 1104 | ex_type[child->dev_type], | 
|  | 1105 | SAS_ADDR(child->sas_addr), | 
|  | 1106 | child_phy->phy_id, | 
|  | 1107 |  | 
|  | 1108 | ra_char[parent_phy->routing_attr], | 
|  | 1109 | ra_char[child_phy->routing_attr]); | 
|  | 1110 | } | 
|  | 1111 |  | 
|  | 1112 | static int sas_check_eeds(struct domain_device *child, | 
|  | 1113 | struct ex_phy *parent_phy, | 
|  | 1114 | struct ex_phy *child_phy) | 
|  | 1115 | { | 
|  | 1116 | int res = 0; | 
|  | 1117 | struct domain_device *parent = child->parent; | 
|  | 1118 |  | 
|  | 1119 | if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) { | 
|  | 1120 | res = -ENODEV; | 
|  | 1121 | SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx " | 
|  | 1122 | "phy S:0x%x, while there is a fanout ex %016llx\n", | 
|  | 1123 | SAS_ADDR(parent->sas_addr), | 
|  | 1124 | parent_phy->phy_id, | 
|  | 1125 | SAS_ADDR(child->sas_addr), | 
|  | 1126 | child_phy->phy_id, | 
|  | 1127 | SAS_ADDR(parent->port->disc.fanout_sas_addr)); | 
|  | 1128 | } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) { | 
|  | 1129 | memcpy(parent->port->disc.eeds_a, parent->sas_addr, | 
|  | 1130 | SAS_ADDR_SIZE); | 
|  | 1131 | memcpy(parent->port->disc.eeds_b, child->sas_addr, | 
|  | 1132 | SAS_ADDR_SIZE); | 
|  | 1133 | } else if (((SAS_ADDR(parent->port->disc.eeds_a) == | 
|  | 1134 | SAS_ADDR(parent->sas_addr)) || | 
|  | 1135 | (SAS_ADDR(parent->port->disc.eeds_a) == | 
|  | 1136 | SAS_ADDR(child->sas_addr))) | 
|  | 1137 | && | 
|  | 1138 | ((SAS_ADDR(parent->port->disc.eeds_b) == | 
|  | 1139 | SAS_ADDR(parent->sas_addr)) || | 
|  | 1140 | (SAS_ADDR(parent->port->disc.eeds_b) == | 
|  | 1141 | SAS_ADDR(child->sas_addr)))) | 
|  | 1142 | ; | 
|  | 1143 | else { | 
|  | 1144 | res = -ENODEV; | 
|  | 1145 | SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx " | 
|  | 1146 | "phy 0x%x link forms a third EEDS!\n", | 
|  | 1147 | SAS_ADDR(parent->sas_addr), | 
|  | 1148 | parent_phy->phy_id, | 
|  | 1149 | SAS_ADDR(child->sas_addr), | 
|  | 1150 | child_phy->phy_id); | 
|  | 1151 | } | 
|  | 1152 |  | 
|  | 1153 | return res; | 
|  | 1154 | } | 
|  | 1155 |  | 
|  | 1156 | /* Here we spill over 80 columns.  It is intentional. | 
|  | 1157 | */ | 
|  | 1158 | static int sas_check_parent_topology(struct domain_device *child) | 
|  | 1159 | { | 
|  | 1160 | struct expander_device *child_ex = &child->ex_dev; | 
|  | 1161 | struct expander_device *parent_ex; | 
|  | 1162 | int i; | 
|  | 1163 | int res = 0; | 
|  | 1164 |  | 
|  | 1165 | if (!child->parent) | 
|  | 1166 | return 0; | 
|  | 1167 |  | 
|  | 1168 | if (child->parent->dev_type != EDGE_DEV && | 
|  | 1169 | child->parent->dev_type != FANOUT_DEV) | 
|  | 1170 | return 0; | 
|  | 1171 |  | 
|  | 1172 | parent_ex = &child->parent->ex_dev; | 
|  | 1173 |  | 
|  | 1174 | for (i = 0; i < parent_ex->num_phys; i++) { | 
|  | 1175 | struct ex_phy *parent_phy = &parent_ex->ex_phy[i]; | 
|  | 1176 | struct ex_phy *child_phy; | 
|  | 1177 |  | 
|  | 1178 | if (parent_phy->phy_state == PHY_VACANT || | 
|  | 1179 | parent_phy->phy_state == PHY_NOT_PRESENT) | 
|  | 1180 | continue; | 
|  | 1181 |  | 
|  | 1182 | if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr)) | 
|  | 1183 | continue; | 
|  | 1184 |  | 
|  | 1185 | child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id]; | 
|  | 1186 |  | 
|  | 1187 | switch (child->parent->dev_type) { | 
|  | 1188 | case EDGE_DEV: | 
|  | 1189 | if (child->dev_type == FANOUT_DEV) { | 
|  | 1190 | if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING || | 
|  | 1191 | child_phy->routing_attr != TABLE_ROUTING) { | 
|  | 1192 | sas_print_parent_topology_bug(child, parent_phy, child_phy); | 
|  | 1193 | res = -ENODEV; | 
|  | 1194 | } | 
|  | 1195 | } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) { | 
|  | 1196 | if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) { | 
|  | 1197 | res = sas_check_eeds(child, parent_phy, child_phy); | 
|  | 1198 | } else if (child_phy->routing_attr != TABLE_ROUTING) { | 
|  | 1199 | sas_print_parent_topology_bug(child, parent_phy, child_phy); | 
|  | 1200 | res = -ENODEV; | 
|  | 1201 | } | 
|  | 1202 | } else if (parent_phy->routing_attr == TABLE_ROUTING && | 
|  | 1203 | child_phy->routing_attr != SUBTRACTIVE_ROUTING) { | 
|  | 1204 | sas_print_parent_topology_bug(child, parent_phy, child_phy); | 
|  | 1205 | res = -ENODEV; | 
|  | 1206 | } | 
|  | 1207 | break; | 
|  | 1208 | case FANOUT_DEV: | 
|  | 1209 | if (parent_phy->routing_attr != TABLE_ROUTING || | 
|  | 1210 | child_phy->routing_attr != SUBTRACTIVE_ROUTING) { | 
|  | 1211 | sas_print_parent_topology_bug(child, parent_phy, child_phy); | 
|  | 1212 | res = -ENODEV; | 
|  | 1213 | } | 
|  | 1214 | break; | 
|  | 1215 | default: | 
|  | 1216 | break; | 
|  | 1217 | } | 
|  | 1218 | } | 
|  | 1219 |  | 
|  | 1220 | return res; | 
|  | 1221 | } | 
|  | 1222 |  | 
|  | 1223 | #define RRI_REQ_SIZE  16 | 
|  | 1224 | #define RRI_RESP_SIZE 44 | 
|  | 1225 |  | 
|  | 1226 | static int sas_configure_present(struct domain_device *dev, int phy_id, | 
|  | 1227 | u8 *sas_addr, int *index, int *present) | 
|  | 1228 | { | 
|  | 1229 | int i, res = 0; | 
|  | 1230 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1231 | struct ex_phy *phy = &ex->ex_phy[phy_id]; | 
|  | 1232 | u8 *rri_req; | 
|  | 1233 | u8 *rri_resp; | 
|  | 1234 |  | 
|  | 1235 | *present = 0; | 
|  | 1236 | *index = 0; | 
|  | 1237 |  | 
|  | 1238 | rri_req = alloc_smp_req(RRI_REQ_SIZE); | 
|  | 1239 | if (!rri_req) | 
|  | 1240 | return -ENOMEM; | 
|  | 1241 |  | 
|  | 1242 | rri_resp = alloc_smp_resp(RRI_RESP_SIZE); | 
|  | 1243 | if (!rri_resp) { | 
|  | 1244 | kfree(rri_req); | 
|  | 1245 | return -ENOMEM; | 
|  | 1246 | } | 
|  | 1247 |  | 
|  | 1248 | rri_req[1] = SMP_REPORT_ROUTE_INFO; | 
|  | 1249 | rri_req[9] = phy_id; | 
|  | 1250 |  | 
|  | 1251 | for (i = 0; i < ex->max_route_indexes ; i++) { | 
|  | 1252 | *(__be16 *)(rri_req+6) = cpu_to_be16(i); | 
|  | 1253 | res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp, | 
|  | 1254 | RRI_RESP_SIZE); | 
|  | 1255 | if (res) | 
|  | 1256 | goto out; | 
|  | 1257 | res = rri_resp[2]; | 
|  | 1258 | if (res == SMP_RESP_NO_INDEX) { | 
|  | 1259 | SAS_DPRINTK("overflow of indexes: dev %016llx " | 
|  | 1260 | "phy 0x%x index 0x%x\n", | 
|  | 1261 | SAS_ADDR(dev->sas_addr), phy_id, i); | 
|  | 1262 | goto out; | 
|  | 1263 | } else if (res != SMP_RESP_FUNC_ACC) { | 
|  | 1264 | SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x " | 
|  | 1265 | "result 0x%x\n", __FUNCTION__, | 
|  | 1266 | SAS_ADDR(dev->sas_addr), phy_id, i, res); | 
|  | 1267 | goto out; | 
|  | 1268 | } | 
|  | 1269 | if (SAS_ADDR(sas_addr) != 0) { | 
|  | 1270 | if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) { | 
|  | 1271 | *index = i; | 
|  | 1272 | if ((rri_resp[12] & 0x80) == 0x80) | 
|  | 1273 | *present = 0; | 
|  | 1274 | else | 
|  | 1275 | *present = 1; | 
|  | 1276 | goto out; | 
|  | 1277 | } else if (SAS_ADDR(rri_resp+16) == 0) { | 
|  | 1278 | *index = i; | 
|  | 1279 | *present = 0; | 
|  | 1280 | goto out; | 
|  | 1281 | } | 
|  | 1282 | } else if (SAS_ADDR(rri_resp+16) == 0 && | 
|  | 1283 | phy->last_da_index < i) { | 
|  | 1284 | phy->last_da_index = i; | 
|  | 1285 | *index = i; | 
|  | 1286 | *present = 0; | 
|  | 1287 | goto out; | 
|  | 1288 | } | 
|  | 1289 | } | 
|  | 1290 | res = -1; | 
|  | 1291 | out: | 
|  | 1292 | kfree(rri_req); | 
|  | 1293 | kfree(rri_resp); | 
|  | 1294 | return res; | 
|  | 1295 | } | 
|  | 1296 |  | 
|  | 1297 | #define CRI_REQ_SIZE  44 | 
|  | 1298 | #define CRI_RESP_SIZE  8 | 
|  | 1299 |  | 
|  | 1300 | static int sas_configure_set(struct domain_device *dev, int phy_id, | 
|  | 1301 | u8 *sas_addr, int index, int include) | 
|  | 1302 | { | 
|  | 1303 | int res; | 
|  | 1304 | u8 *cri_req; | 
|  | 1305 | u8 *cri_resp; | 
|  | 1306 |  | 
|  | 1307 | cri_req = alloc_smp_req(CRI_REQ_SIZE); | 
|  | 1308 | if (!cri_req) | 
|  | 1309 | return -ENOMEM; | 
|  | 1310 |  | 
|  | 1311 | cri_resp = alloc_smp_resp(CRI_RESP_SIZE); | 
|  | 1312 | if (!cri_resp) { | 
|  | 1313 | kfree(cri_req); | 
|  | 1314 | return -ENOMEM; | 
|  | 1315 | } | 
|  | 1316 |  | 
|  | 1317 | cri_req[1] = SMP_CONF_ROUTE_INFO; | 
|  | 1318 | *(__be16 *)(cri_req+6) = cpu_to_be16(index); | 
|  | 1319 | cri_req[9] = phy_id; | 
|  | 1320 | if (SAS_ADDR(sas_addr) == 0 || !include) | 
|  | 1321 | cri_req[12] |= 0x80; | 
|  | 1322 | memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE); | 
|  | 1323 |  | 
|  | 1324 | res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp, | 
|  | 1325 | CRI_RESP_SIZE); | 
|  | 1326 | if (res) | 
|  | 1327 | goto out; | 
|  | 1328 | res = cri_resp[2]; | 
|  | 1329 | if (res == SMP_RESP_NO_INDEX) { | 
|  | 1330 | SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x " | 
|  | 1331 | "index 0x%x\n", | 
|  | 1332 | SAS_ADDR(dev->sas_addr), phy_id, index); | 
|  | 1333 | } | 
|  | 1334 | out: | 
|  | 1335 | kfree(cri_req); | 
|  | 1336 | kfree(cri_resp); | 
|  | 1337 | return res; | 
|  | 1338 | } | 
|  | 1339 |  | 
|  | 1340 | static int sas_configure_phy(struct domain_device *dev, int phy_id, | 
|  | 1341 | u8 *sas_addr, int include) | 
|  | 1342 | { | 
|  | 1343 | int index; | 
|  | 1344 | int present; | 
|  | 1345 | int res; | 
|  | 1346 |  | 
|  | 1347 | res = sas_configure_present(dev, phy_id, sas_addr, &index, &present); | 
|  | 1348 | if (res) | 
|  | 1349 | return res; | 
|  | 1350 | if (include ^ present) | 
|  | 1351 | return sas_configure_set(dev, phy_id, sas_addr, index,include); | 
|  | 1352 |  | 
|  | 1353 | return res; | 
|  | 1354 | } | 
|  | 1355 |  | 
|  | 1356 | /** | 
|  | 1357 | * sas_configure_parent -- configure routing table of parent | 
|  | 1358 | * parent: parent expander | 
|  | 1359 | * child: child expander | 
|  | 1360 | * sas_addr: SAS port identifier of device directly attached to child | 
|  | 1361 | */ | 
|  | 1362 | static int sas_configure_parent(struct domain_device *parent, | 
|  | 1363 | struct domain_device *child, | 
|  | 1364 | u8 *sas_addr, int include) | 
|  | 1365 | { | 
|  | 1366 | struct expander_device *ex_parent = &parent->ex_dev; | 
|  | 1367 | int res = 0; | 
|  | 1368 | int i; | 
|  | 1369 |  | 
|  | 1370 | if (parent->parent) { | 
|  | 1371 | res = sas_configure_parent(parent->parent, parent, sas_addr, | 
|  | 1372 | include); | 
|  | 1373 | if (res) | 
|  | 1374 | return res; | 
|  | 1375 | } | 
|  | 1376 |  | 
|  | 1377 | if (ex_parent->conf_route_table == 0) { | 
|  | 1378 | SAS_DPRINTK("ex %016llx has self-configuring routing table\n", | 
|  | 1379 | SAS_ADDR(parent->sas_addr)); | 
|  | 1380 | return 0; | 
|  | 1381 | } | 
|  | 1382 |  | 
|  | 1383 | for (i = 0; i < ex_parent->num_phys; i++) { | 
|  | 1384 | struct ex_phy *phy = &ex_parent->ex_phy[i]; | 
|  | 1385 |  | 
|  | 1386 | if ((phy->routing_attr == TABLE_ROUTING) && | 
|  | 1387 | (SAS_ADDR(phy->attached_sas_addr) == | 
|  | 1388 | SAS_ADDR(child->sas_addr))) { | 
|  | 1389 | res = sas_configure_phy(parent, i, sas_addr, include); | 
|  | 1390 | if (res) | 
|  | 1391 | return res; | 
|  | 1392 | } | 
|  | 1393 | } | 
|  | 1394 |  | 
|  | 1395 | return res; | 
|  | 1396 | } | 
|  | 1397 |  | 
|  | 1398 | /** | 
|  | 1399 | * sas_configure_routing -- configure routing | 
|  | 1400 | * dev: expander device | 
|  | 1401 | * sas_addr: port identifier of device directly attached to the expander device | 
|  | 1402 | */ | 
|  | 1403 | static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr) | 
|  | 1404 | { | 
|  | 1405 | if (dev->parent) | 
|  | 1406 | return sas_configure_parent(dev->parent, dev, sas_addr, 1); | 
|  | 1407 | return 0; | 
|  | 1408 | } | 
|  | 1409 |  | 
|  | 1410 | static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr) | 
|  | 1411 | { | 
|  | 1412 | if (dev->parent) | 
|  | 1413 | return sas_configure_parent(dev->parent, dev, sas_addr, 0); | 
|  | 1414 | return 0; | 
|  | 1415 | } | 
|  | 1416 |  | 
|  | 1417 | #if 0 | 
|  | 1418 | #define SMP_BIN_ATTR_NAME "smp_portal" | 
|  | 1419 |  | 
|  | 1420 | static void sas_ex_smp_hook(struct domain_device *dev) | 
|  | 1421 | { | 
|  | 1422 | struct expander_device *ex_dev = &dev->ex_dev; | 
|  | 1423 | struct bin_attribute *bin_attr = &ex_dev->smp_bin_attr; | 
|  | 1424 |  | 
|  | 1425 | memset(bin_attr, 0, sizeof(*bin_attr)); | 
|  | 1426 |  | 
|  | 1427 | bin_attr->attr.name = SMP_BIN_ATTR_NAME; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1428 | bin_attr->attr.mode = 0600; | 
|  | 1429 |  | 
|  | 1430 | bin_attr->size = 0; | 
|  | 1431 | bin_attr->private = NULL; | 
|  | 1432 | bin_attr->read = smp_portal_read; | 
|  | 1433 | bin_attr->write= smp_portal_write; | 
|  | 1434 | bin_attr->mmap = NULL; | 
|  | 1435 |  | 
|  | 1436 | ex_dev->smp_portal_pid = -1; | 
|  | 1437 | init_MUTEX(&ex_dev->smp_sema); | 
|  | 1438 | } | 
|  | 1439 | #endif | 
|  | 1440 |  | 
|  | 1441 | /** | 
|  | 1442 | * sas_discover_expander -- expander discovery | 
|  | 1443 | * @ex: pointer to expander domain device | 
|  | 1444 | * | 
|  | 1445 | * See comment in sas_discover_sata(). | 
|  | 1446 | */ | 
|  | 1447 | static int sas_discover_expander(struct domain_device *dev) | 
|  | 1448 | { | 
|  | 1449 | int res; | 
|  | 1450 |  | 
|  | 1451 | res = sas_notify_lldd_dev_found(dev); | 
|  | 1452 | if (res) | 
|  | 1453 | return res; | 
|  | 1454 |  | 
|  | 1455 | res = sas_ex_general(dev); | 
|  | 1456 | if (res) | 
|  | 1457 | goto out_err; | 
|  | 1458 | res = sas_ex_manuf_info(dev); | 
|  | 1459 | if (res) | 
|  | 1460 | goto out_err; | 
|  | 1461 |  | 
|  | 1462 | res = sas_expander_discover(dev); | 
|  | 1463 | if (res) { | 
|  | 1464 | SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n", | 
|  | 1465 | SAS_ADDR(dev->sas_addr), res); | 
|  | 1466 | goto out_err; | 
|  | 1467 | } | 
|  | 1468 |  | 
|  | 1469 | sas_check_ex_subtractive_boundary(dev); | 
|  | 1470 | res = sas_check_parent_topology(dev); | 
|  | 1471 | if (res) | 
|  | 1472 | goto out_err; | 
|  | 1473 | return 0; | 
|  | 1474 | out_err: | 
|  | 1475 | sas_notify_lldd_dev_gone(dev); | 
|  | 1476 | return res; | 
|  | 1477 | } | 
|  | 1478 |  | 
|  | 1479 | static int sas_ex_level_discovery(struct asd_sas_port *port, const int level) | 
|  | 1480 | { | 
|  | 1481 | int res = 0; | 
|  | 1482 | struct domain_device *dev; | 
|  | 1483 |  | 
|  | 1484 | list_for_each_entry(dev, &port->dev_list, dev_list_node) { | 
|  | 1485 | if (dev->dev_type == EDGE_DEV || | 
|  | 1486 | dev->dev_type == FANOUT_DEV) { | 
|  | 1487 | struct sas_expander_device *ex = | 
|  | 1488 | rphy_to_expander_device(dev->rphy); | 
|  | 1489 |  | 
|  | 1490 | if (level == ex->level) | 
|  | 1491 | res = sas_ex_discover_devices(dev, -1); | 
|  | 1492 | else if (level > 0) | 
|  | 1493 | res = sas_ex_discover_devices(port->port_dev, -1); | 
|  | 1494 |  | 
|  | 1495 | } | 
|  | 1496 | } | 
|  | 1497 |  | 
|  | 1498 | return res; | 
|  | 1499 | } | 
|  | 1500 |  | 
|  | 1501 | static int sas_ex_bfs_disc(struct asd_sas_port *port) | 
|  | 1502 | { | 
|  | 1503 | int res; | 
|  | 1504 | int level; | 
|  | 1505 |  | 
|  | 1506 | do { | 
|  | 1507 | level = port->disc.max_level; | 
|  | 1508 | res = sas_ex_level_discovery(port, level); | 
|  | 1509 | mb(); | 
|  | 1510 | } while (level < port->disc.max_level); | 
|  | 1511 |  | 
|  | 1512 | return res; | 
|  | 1513 | } | 
|  | 1514 |  | 
|  | 1515 | int sas_discover_root_expander(struct domain_device *dev) | 
|  | 1516 | { | 
|  | 1517 | int res; | 
|  | 1518 | struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy); | 
|  | 1519 |  | 
| Darrick J. Wong | bf45120 | 2007-01-11 14:14:52 -0800 | [diff] [blame] | 1520 | res = sas_rphy_add(dev->rphy); | 
|  | 1521 | if (res) | 
|  | 1522 | goto out_err; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1523 |  | 
|  | 1524 | ex->level = dev->port->disc.max_level; /* 0 */ | 
|  | 1525 | res = sas_discover_expander(dev); | 
| Darrick J. Wong | bf45120 | 2007-01-11 14:14:52 -0800 | [diff] [blame] | 1526 | if (res) | 
|  | 1527 | goto out_err2; | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1528 |  | 
| Darrick J. Wong | bf45120 | 2007-01-11 14:14:52 -0800 | [diff] [blame] | 1529 | sas_ex_bfs_disc(dev->port); | 
|  | 1530 |  | 
|  | 1531 | return res; | 
|  | 1532 |  | 
|  | 1533 | out_err2: | 
| Darrick J. Wong | 6f63caa | 2007-01-26 14:08:43 -0800 | [diff] [blame] | 1534 | sas_rphy_remove(dev->rphy); | 
| Darrick J. Wong | bf45120 | 2007-01-11 14:14:52 -0800 | [diff] [blame] | 1535 | out_err: | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1536 | return res; | 
|  | 1537 | } | 
|  | 1538 |  | 
|  | 1539 | /* ---------- Domain revalidation ---------- */ | 
|  | 1540 |  | 
|  | 1541 | static int sas_get_phy_discover(struct domain_device *dev, | 
|  | 1542 | int phy_id, struct smp_resp *disc_resp) | 
|  | 1543 | { | 
|  | 1544 | int res; | 
|  | 1545 | u8 *disc_req; | 
|  | 1546 |  | 
|  | 1547 | disc_req = alloc_smp_req(DISCOVER_REQ_SIZE); | 
|  | 1548 | if (!disc_req) | 
|  | 1549 | return -ENOMEM; | 
|  | 1550 |  | 
|  | 1551 | disc_req[1] = SMP_DISCOVER; | 
|  | 1552 | disc_req[9] = phy_id; | 
|  | 1553 |  | 
|  | 1554 | res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE, | 
|  | 1555 | disc_resp, DISCOVER_RESP_SIZE); | 
|  | 1556 | if (res) | 
|  | 1557 | goto out; | 
|  | 1558 | else if (disc_resp->result != SMP_RESP_FUNC_ACC) { | 
|  | 1559 | res = disc_resp->result; | 
|  | 1560 | goto out; | 
|  | 1561 | } | 
|  | 1562 | out: | 
|  | 1563 | kfree(disc_req); | 
|  | 1564 | return res; | 
|  | 1565 | } | 
|  | 1566 |  | 
|  | 1567 | static int sas_get_phy_change_count(struct domain_device *dev, | 
|  | 1568 | int phy_id, int *pcc) | 
|  | 1569 | { | 
|  | 1570 | int res; | 
|  | 1571 | struct smp_resp *disc_resp; | 
|  | 1572 |  | 
|  | 1573 | disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE); | 
|  | 1574 | if (!disc_resp) | 
|  | 1575 | return -ENOMEM; | 
|  | 1576 |  | 
|  | 1577 | res = sas_get_phy_discover(dev, phy_id, disc_resp); | 
|  | 1578 | if (!res) | 
|  | 1579 | *pcc = disc_resp->disc.change_count; | 
|  | 1580 |  | 
|  | 1581 | kfree(disc_resp); | 
|  | 1582 | return res; | 
|  | 1583 | } | 
|  | 1584 |  | 
|  | 1585 | static int sas_get_phy_attached_sas_addr(struct domain_device *dev, | 
|  | 1586 | int phy_id, u8 *attached_sas_addr) | 
|  | 1587 | { | 
|  | 1588 | int res; | 
|  | 1589 | struct smp_resp *disc_resp; | 
|  | 1590 | struct discover_resp *dr; | 
|  | 1591 |  | 
|  | 1592 | disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE); | 
|  | 1593 | if (!disc_resp) | 
|  | 1594 | return -ENOMEM; | 
|  | 1595 | dr = &disc_resp->disc; | 
|  | 1596 |  | 
|  | 1597 | res = sas_get_phy_discover(dev, phy_id, disc_resp); | 
|  | 1598 | if (!res) { | 
|  | 1599 | memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8); | 
|  | 1600 | if (dr->attached_dev_type == 0) | 
|  | 1601 | memset(attached_sas_addr, 0, 8); | 
|  | 1602 | } | 
|  | 1603 | kfree(disc_resp); | 
|  | 1604 | return res; | 
|  | 1605 | } | 
|  | 1606 |  | 
|  | 1607 | static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id, | 
|  | 1608 | int from_phy) | 
|  | 1609 | { | 
|  | 1610 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1611 | int res = 0; | 
|  | 1612 | int i; | 
|  | 1613 |  | 
|  | 1614 | for (i = from_phy; i < ex->num_phys; i++) { | 
|  | 1615 | int phy_change_count = 0; | 
|  | 1616 |  | 
|  | 1617 | res = sas_get_phy_change_count(dev, i, &phy_change_count); | 
|  | 1618 | if (res) | 
|  | 1619 | goto out; | 
|  | 1620 | else if (phy_change_count != ex->ex_phy[i].phy_change_count) { | 
|  | 1621 | ex->ex_phy[i].phy_change_count = phy_change_count; | 
|  | 1622 | *phy_id = i; | 
|  | 1623 | return 0; | 
|  | 1624 | } | 
|  | 1625 | } | 
|  | 1626 | out: | 
|  | 1627 | return res; | 
|  | 1628 | } | 
|  | 1629 |  | 
|  | 1630 | static int sas_get_ex_change_count(struct domain_device *dev, int *ecc) | 
|  | 1631 | { | 
|  | 1632 | int res; | 
|  | 1633 | u8  *rg_req; | 
|  | 1634 | struct smp_resp  *rg_resp; | 
|  | 1635 |  | 
|  | 1636 | rg_req = alloc_smp_req(RG_REQ_SIZE); | 
|  | 1637 | if (!rg_req) | 
|  | 1638 | return -ENOMEM; | 
|  | 1639 |  | 
|  | 1640 | rg_resp = alloc_smp_resp(RG_RESP_SIZE); | 
|  | 1641 | if (!rg_resp) { | 
|  | 1642 | kfree(rg_req); | 
|  | 1643 | return -ENOMEM; | 
|  | 1644 | } | 
|  | 1645 |  | 
|  | 1646 | rg_req[1] = SMP_REPORT_GENERAL; | 
|  | 1647 |  | 
|  | 1648 | res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp, | 
|  | 1649 | RG_RESP_SIZE); | 
|  | 1650 | if (res) | 
|  | 1651 | goto out; | 
|  | 1652 | if (rg_resp->result != SMP_RESP_FUNC_ACC) { | 
|  | 1653 | res = rg_resp->result; | 
|  | 1654 | goto out; | 
|  | 1655 | } | 
|  | 1656 |  | 
|  | 1657 | *ecc = be16_to_cpu(rg_resp->rg.change_count); | 
|  | 1658 | out: | 
|  | 1659 | kfree(rg_resp); | 
|  | 1660 | kfree(rg_req); | 
|  | 1661 | return res; | 
|  | 1662 | } | 
|  | 1663 |  | 
|  | 1664 | static int sas_find_bcast_dev(struct domain_device *dev, | 
|  | 1665 | struct domain_device **src_dev) | 
|  | 1666 | { | 
|  | 1667 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1668 | int ex_change_count = -1; | 
|  | 1669 | int res; | 
|  | 1670 |  | 
|  | 1671 | res = sas_get_ex_change_count(dev, &ex_change_count); | 
|  | 1672 | if (res) | 
|  | 1673 | goto out; | 
|  | 1674 | if (ex_change_count != -1 && | 
|  | 1675 | ex_change_count != ex->ex_change_count) { | 
|  | 1676 | *src_dev = dev; | 
|  | 1677 | ex->ex_change_count = ex_change_count; | 
|  | 1678 | } else { | 
|  | 1679 | struct domain_device *ch; | 
|  | 1680 |  | 
|  | 1681 | list_for_each_entry(ch, &ex->children, siblings) { | 
|  | 1682 | if (ch->dev_type == EDGE_DEV || | 
|  | 1683 | ch->dev_type == FANOUT_DEV) { | 
|  | 1684 | res = sas_find_bcast_dev(ch, src_dev); | 
|  | 1685 | if (src_dev) | 
|  | 1686 | return res; | 
|  | 1687 | } | 
|  | 1688 | } | 
|  | 1689 | } | 
|  | 1690 | out: | 
|  | 1691 | return res; | 
|  | 1692 | } | 
|  | 1693 |  | 
|  | 1694 | static void sas_unregister_ex_tree(struct domain_device *dev) | 
|  | 1695 | { | 
|  | 1696 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1697 | struct domain_device *child, *n; | 
|  | 1698 |  | 
|  | 1699 | list_for_each_entry_safe(child, n, &ex->children, siblings) { | 
|  | 1700 | if (child->dev_type == EDGE_DEV || | 
|  | 1701 | child->dev_type == FANOUT_DEV) | 
|  | 1702 | sas_unregister_ex_tree(child); | 
|  | 1703 | else | 
|  | 1704 | sas_unregister_dev(child); | 
|  | 1705 | } | 
|  | 1706 | sas_unregister_dev(dev); | 
|  | 1707 | } | 
|  | 1708 |  | 
|  | 1709 | static void sas_unregister_devs_sas_addr(struct domain_device *parent, | 
|  | 1710 | int phy_id) | 
|  | 1711 | { | 
|  | 1712 | struct expander_device *ex_dev = &parent->ex_dev; | 
|  | 1713 | struct ex_phy *phy = &ex_dev->ex_phy[phy_id]; | 
|  | 1714 | struct domain_device *child, *n; | 
|  | 1715 |  | 
|  | 1716 | list_for_each_entry_safe(child, n, &ex_dev->children, siblings) { | 
|  | 1717 | if (SAS_ADDR(child->sas_addr) == | 
|  | 1718 | SAS_ADDR(phy->attached_sas_addr)) { | 
|  | 1719 | if (child->dev_type == EDGE_DEV || | 
|  | 1720 | child->dev_type == FANOUT_DEV) | 
|  | 1721 | sas_unregister_ex_tree(child); | 
|  | 1722 | else | 
|  | 1723 | sas_unregister_dev(child); | 
|  | 1724 | break; | 
|  | 1725 | } | 
|  | 1726 | } | 
|  | 1727 | sas_disable_routing(parent, phy->attached_sas_addr); | 
|  | 1728 | memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE); | 
|  | 1729 | sas_port_delete_phy(phy->port, phy->phy); | 
|  | 1730 | if (phy->port->num_phys == 0) | 
|  | 1731 | sas_port_delete(phy->port); | 
|  | 1732 | phy->port = NULL; | 
|  | 1733 | } | 
|  | 1734 |  | 
|  | 1735 | static int sas_discover_bfs_by_root_level(struct domain_device *root, | 
|  | 1736 | const int level) | 
|  | 1737 | { | 
|  | 1738 | struct expander_device *ex_root = &root->ex_dev; | 
|  | 1739 | struct domain_device *child; | 
|  | 1740 | int res = 0; | 
|  | 1741 |  | 
|  | 1742 | list_for_each_entry(child, &ex_root->children, siblings) { | 
|  | 1743 | if (child->dev_type == EDGE_DEV || | 
|  | 1744 | child->dev_type == FANOUT_DEV) { | 
|  | 1745 | struct sas_expander_device *ex = | 
|  | 1746 | rphy_to_expander_device(child->rphy); | 
|  | 1747 |  | 
|  | 1748 | if (level > ex->level) | 
|  | 1749 | res = sas_discover_bfs_by_root_level(child, | 
|  | 1750 | level); | 
|  | 1751 | else if (level == ex->level) | 
|  | 1752 | res = sas_ex_discover_devices(child, -1); | 
|  | 1753 | } | 
|  | 1754 | } | 
|  | 1755 | return res; | 
|  | 1756 | } | 
|  | 1757 |  | 
|  | 1758 | static int sas_discover_bfs_by_root(struct domain_device *dev) | 
|  | 1759 | { | 
|  | 1760 | int res; | 
|  | 1761 | struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy); | 
|  | 1762 | int level = ex->level+1; | 
|  | 1763 |  | 
|  | 1764 | res = sas_ex_discover_devices(dev, -1); | 
|  | 1765 | if (res) | 
|  | 1766 | goto out; | 
|  | 1767 | do { | 
|  | 1768 | res = sas_discover_bfs_by_root_level(dev, level); | 
|  | 1769 | mb(); | 
|  | 1770 | level += 1; | 
|  | 1771 | } while (level <= dev->port->disc.max_level); | 
|  | 1772 | out: | 
|  | 1773 | return res; | 
|  | 1774 | } | 
|  | 1775 |  | 
|  | 1776 | static int sas_discover_new(struct domain_device *dev, int phy_id) | 
|  | 1777 | { | 
|  | 1778 | struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id]; | 
|  | 1779 | struct domain_device *child; | 
|  | 1780 | int res; | 
|  | 1781 |  | 
|  | 1782 | SAS_DPRINTK("ex %016llx phy%d new device attached\n", | 
|  | 1783 | SAS_ADDR(dev->sas_addr), phy_id); | 
|  | 1784 | res = sas_ex_phy_discover(dev, phy_id); | 
|  | 1785 | if (res) | 
|  | 1786 | goto out; | 
|  | 1787 | res = sas_ex_discover_devices(dev, phy_id); | 
|  | 1788 | if (res) | 
|  | 1789 | goto out; | 
|  | 1790 | list_for_each_entry(child, &dev->ex_dev.children, siblings) { | 
|  | 1791 | if (SAS_ADDR(child->sas_addr) == | 
|  | 1792 | SAS_ADDR(ex_phy->attached_sas_addr)) { | 
|  | 1793 | if (child->dev_type == EDGE_DEV || | 
|  | 1794 | child->dev_type == FANOUT_DEV) | 
|  | 1795 | res = sas_discover_bfs_by_root(child); | 
|  | 1796 | break; | 
|  | 1797 | } | 
|  | 1798 | } | 
|  | 1799 | out: | 
|  | 1800 | return res; | 
|  | 1801 | } | 
|  | 1802 |  | 
|  | 1803 | static int sas_rediscover_dev(struct domain_device *dev, int phy_id) | 
|  | 1804 | { | 
|  | 1805 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1806 | struct ex_phy *phy = &ex->ex_phy[phy_id]; | 
|  | 1807 | u8 attached_sas_addr[8]; | 
|  | 1808 | int res; | 
|  | 1809 |  | 
|  | 1810 | res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr); | 
|  | 1811 | switch (res) { | 
|  | 1812 | case SMP_RESP_NO_PHY: | 
|  | 1813 | phy->phy_state = PHY_NOT_PRESENT; | 
|  | 1814 | sas_unregister_devs_sas_addr(dev, phy_id); | 
|  | 1815 | goto out; break; | 
|  | 1816 | case SMP_RESP_PHY_VACANT: | 
|  | 1817 | phy->phy_state = PHY_VACANT; | 
|  | 1818 | sas_unregister_devs_sas_addr(dev, phy_id); | 
|  | 1819 | goto out; break; | 
|  | 1820 | case SMP_RESP_FUNC_ACC: | 
|  | 1821 | break; | 
|  | 1822 | } | 
|  | 1823 |  | 
|  | 1824 | if (SAS_ADDR(attached_sas_addr) == 0) { | 
|  | 1825 | phy->phy_state = PHY_EMPTY; | 
|  | 1826 | sas_unregister_devs_sas_addr(dev, phy_id); | 
|  | 1827 | } else if (SAS_ADDR(attached_sas_addr) == | 
|  | 1828 | SAS_ADDR(phy->attached_sas_addr)) { | 
|  | 1829 | SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n", | 
|  | 1830 | SAS_ADDR(dev->sas_addr), phy_id); | 
| James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 1831 | sas_ex_phy_discover(dev, phy_id); | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1832 | } else | 
|  | 1833 | res = sas_discover_new(dev, phy_id); | 
|  | 1834 | out: | 
|  | 1835 | return res; | 
|  | 1836 | } | 
|  | 1837 |  | 
|  | 1838 | static int sas_rediscover(struct domain_device *dev, const int phy_id) | 
|  | 1839 | { | 
|  | 1840 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1841 | struct ex_phy *changed_phy = &ex->ex_phy[phy_id]; | 
|  | 1842 | int res = 0; | 
|  | 1843 | int i; | 
|  | 1844 |  | 
|  | 1845 | SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n", | 
|  | 1846 | SAS_ADDR(dev->sas_addr), phy_id); | 
|  | 1847 |  | 
|  | 1848 | if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) { | 
|  | 1849 | for (i = 0; i < ex->num_phys; i++) { | 
|  | 1850 | struct ex_phy *phy = &ex->ex_phy[i]; | 
|  | 1851 |  | 
|  | 1852 | if (i == phy_id) | 
|  | 1853 | continue; | 
|  | 1854 | if (SAS_ADDR(phy->attached_sas_addr) == | 
|  | 1855 | SAS_ADDR(changed_phy->attached_sas_addr)) { | 
|  | 1856 | SAS_DPRINTK("phy%d part of wide port with " | 
|  | 1857 | "phy%d\n", phy_id, i); | 
|  | 1858 | goto out; | 
|  | 1859 | } | 
|  | 1860 | } | 
|  | 1861 | res = sas_rediscover_dev(dev, phy_id); | 
|  | 1862 | } else | 
|  | 1863 | res = sas_discover_new(dev, phy_id); | 
|  | 1864 | out: | 
|  | 1865 | return res; | 
|  | 1866 | } | 
|  | 1867 |  | 
|  | 1868 | /** | 
|  | 1869 | * sas_revalidate_domain -- revalidate the domain | 
|  | 1870 | * @port: port to the domain of interest | 
|  | 1871 | * | 
|  | 1872 | * NOTE: this process _must_ quit (return) as soon as any connection | 
|  | 1873 | * errors are encountered.  Connection recovery is done elsewhere. | 
|  | 1874 | * Discover process only interrogates devices in order to discover the | 
|  | 1875 | * domain. | 
|  | 1876 | */ | 
|  | 1877 | int sas_ex_revalidate_domain(struct domain_device *port_dev) | 
|  | 1878 | { | 
|  | 1879 | int res; | 
|  | 1880 | struct domain_device *dev = NULL; | 
|  | 1881 |  | 
|  | 1882 | res = sas_find_bcast_dev(port_dev, &dev); | 
|  | 1883 | if (res) | 
|  | 1884 | goto out; | 
|  | 1885 | if (dev) { | 
|  | 1886 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1887 | int i = 0, phy_id; | 
|  | 1888 |  | 
|  | 1889 | do { | 
|  | 1890 | phy_id = -1; | 
|  | 1891 | res = sas_find_bcast_phy(dev, &phy_id, i); | 
|  | 1892 | if (phy_id == -1) | 
|  | 1893 | break; | 
|  | 1894 | res = sas_rediscover(dev, phy_id); | 
|  | 1895 | i = phy_id + 1; | 
|  | 1896 | } while (i < ex->num_phys); | 
|  | 1897 | } | 
|  | 1898 | out: | 
|  | 1899 | return res; | 
|  | 1900 | } | 
|  | 1901 |  | 
|  | 1902 | #if 0 | 
|  | 1903 | /* ---------- SMP portal ---------- */ | 
|  | 1904 |  | 
| Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 1905 | static ssize_t smp_portal_write(struct kobject *kobj, | 
|  | 1906 | struct bin_attribute *bin_attr, | 
|  | 1907 | char *buf, loff_t offs, size_t size) | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1908 | { | 
|  | 1909 | struct domain_device *dev = to_dom_device(kobj); | 
|  | 1910 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1911 |  | 
|  | 1912 | if (offs != 0) | 
|  | 1913 | return -EFBIG; | 
|  | 1914 | else if (size == 0) | 
|  | 1915 | return 0; | 
|  | 1916 |  | 
|  | 1917 | down_interruptible(&ex->smp_sema); | 
|  | 1918 | if (ex->smp_req) | 
|  | 1919 | kfree(ex->smp_req); | 
|  | 1920 | ex->smp_req = kzalloc(size, GFP_USER); | 
|  | 1921 | if (!ex->smp_req) { | 
|  | 1922 | up(&ex->smp_sema); | 
|  | 1923 | return -ENOMEM; | 
|  | 1924 | } | 
|  | 1925 | memcpy(ex->smp_req, buf, size); | 
|  | 1926 | ex->smp_req_size = size; | 
|  | 1927 | ex->smp_portal_pid = current->pid; | 
|  | 1928 | up(&ex->smp_sema); | 
|  | 1929 |  | 
|  | 1930 | return size; | 
|  | 1931 | } | 
|  | 1932 |  | 
| Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 1933 | static ssize_t smp_portal_read(struct kobject *kobj, | 
|  | 1934 | struct bin_attribute *bin_attr, | 
|  | 1935 | char *buf, loff_t offs, size_t size) | 
| James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 1936 | { | 
|  | 1937 | struct domain_device *dev = to_dom_device(kobj); | 
|  | 1938 | struct expander_device *ex = &dev->ex_dev; | 
|  | 1939 | u8 *smp_resp; | 
|  | 1940 | int res = -EINVAL; | 
|  | 1941 |  | 
|  | 1942 | /* XXX: sysfs gives us an offset of 0x10 or 0x8 while in fact | 
|  | 1943 | *  it should be 0. | 
|  | 1944 | */ | 
|  | 1945 |  | 
|  | 1946 | down_interruptible(&ex->smp_sema); | 
|  | 1947 | if (!ex->smp_req || ex->smp_portal_pid != current->pid) | 
|  | 1948 | goto out; | 
|  | 1949 |  | 
|  | 1950 | res = 0; | 
|  | 1951 | if (size == 0) | 
|  | 1952 | goto out; | 
|  | 1953 |  | 
|  | 1954 | res = -ENOMEM; | 
|  | 1955 | smp_resp = alloc_smp_resp(size); | 
|  | 1956 | if (!smp_resp) | 
|  | 1957 | goto out; | 
|  | 1958 | res = smp_execute_task(dev, ex->smp_req, ex->smp_req_size, | 
|  | 1959 | smp_resp, size); | 
|  | 1960 | if (!res) { | 
|  | 1961 | memcpy(buf, smp_resp, size); | 
|  | 1962 | res = size; | 
|  | 1963 | } | 
|  | 1964 |  | 
|  | 1965 | kfree(smp_resp); | 
|  | 1966 | out: | 
|  | 1967 | kfree(ex->smp_req); | 
|  | 1968 | ex->smp_req = NULL; | 
|  | 1969 | ex->smp_req_size = 0; | 
|  | 1970 | ex->smp_portal_pid = -1; | 
|  | 1971 | up(&ex->smp_sema); | 
|  | 1972 | return res; | 
|  | 1973 | } | 
|  | 1974 | #endif |