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