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