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