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