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