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