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