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