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