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