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