[SCSI] libsas: fix port->dev_list locking
[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
758         spin_lock_irq(&parent->port->dev_list_lock);
759         list_del(&child->dev_list_node);
760         spin_unlock_irq(&parent->port->dev_list_lock);
761  out_free:
762         sas_port_delete(phy->port);
763  out_err:
764         phy->port = NULL;
765         kfree(child);
766         return NULL;
767 }
768
769 /* See if this phy is part of a wide port */
770 static int sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
771 {
772         struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
773         int i;
774
775         for (i = 0; i < parent->ex_dev.num_phys; i++) {
776                 struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
777
778                 if (ephy == phy)
779                         continue;
780
781                 if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
782                             SAS_ADDR_SIZE) && ephy->port) {
783                         sas_port_add_phy(ephy->port, phy->phy);
784                         phy->port = ephy->port;
785                         phy->phy_state = PHY_DEVICE_DISCOVERED;
786                         return 0;
787                 }
788         }
789
790         return -ENODEV;
791 }
792
793 static struct domain_device *sas_ex_discover_expander(
794         struct domain_device *parent, int phy_id)
795 {
796         struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
797         struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
798         struct domain_device *child = NULL;
799         struct sas_rphy *rphy;
800         struct sas_expander_device *edev;
801         struct asd_sas_port *port;
802         int res;
803
804         if (phy->routing_attr == DIRECT_ROUTING) {
805                 SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
806                             "allowed\n",
807                             SAS_ADDR(parent->sas_addr), phy_id,
808                             SAS_ADDR(phy->attached_sas_addr),
809                             phy->attached_phy_id);
810                 return NULL;
811         }
812         child = kzalloc(sizeof(*child), GFP_KERNEL);
813         if (!child)
814                 return NULL;
815
816         phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
817         /* FIXME: better error handling */
818         BUG_ON(sas_port_add(phy->port) != 0);
819
820
821         switch (phy->attached_dev_type) {
822         case EDGE_DEV:
823                 rphy = sas_expander_alloc(phy->port,
824                                           SAS_EDGE_EXPANDER_DEVICE);
825                 break;
826         case FANOUT_DEV:
827                 rphy = sas_expander_alloc(phy->port,
828                                           SAS_FANOUT_EXPANDER_DEVICE);
829                 break;
830         default:
831                 rphy = NULL;    /* shut gcc up */
832                 BUG();
833         }
834         port = parent->port;
835         child->rphy = rphy;
836         edev = rphy_to_expander_device(rphy);
837         child->dev_type = phy->attached_dev_type;
838         child->parent = parent;
839         child->port = port;
840         child->iproto = phy->attached_iproto;
841         child->tproto = phy->attached_tproto;
842         memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
843         sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
844         sas_ex_get_linkrate(parent, child, phy);
845         edev->level = parent_ex->level + 1;
846         parent->port->disc.max_level = max(parent->port->disc.max_level,
847                                            edev->level);
848         sas_init_dev(child);
849         sas_fill_in_rphy(child, rphy);
850         sas_rphy_add(rphy);
851
852         spin_lock_irq(&parent->port->dev_list_lock);
853         list_add_tail(&child->dev_list_node, &parent->port->dev_list);
854         spin_unlock_irq(&parent->port->dev_list_lock);
855
856         res = sas_discover_expander(child);
857         if (res) {
858                 spin_lock_irq(&parent->port->dev_list_lock);
859                 list_del(&child->dev_list_node);
860                 spin_unlock_irq(&parent->port->dev_list_lock);
861                 kfree(child);
862                 return NULL;
863         }
864         list_add_tail(&child->siblings, &parent->ex_dev.children);
865         return child;
866 }
867
868 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
869 {
870         struct expander_device *ex = &dev->ex_dev;
871         struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
872         struct domain_device *child = NULL;
873         int res = 0;
874
875         /* Phy state */
876         if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
877                 if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
878                         res = sas_ex_phy_discover(dev, phy_id);
879                 if (res)
880                         return res;
881         }
882
883         /* Parent and domain coherency */
884         if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
885                              SAS_ADDR(dev->port->sas_addr))) {
886                 sas_add_parent_port(dev, phy_id);
887                 return 0;
888         }
889         if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
890                             SAS_ADDR(dev->parent->sas_addr))) {
891                 sas_add_parent_port(dev, phy_id);
892                 if (ex_phy->routing_attr == TABLE_ROUTING)
893                         sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
894                 return 0;
895         }
896
897         if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
898                 sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
899
900         if (ex_phy->attached_dev_type == NO_DEVICE) {
901                 if (ex_phy->routing_attr == DIRECT_ROUTING) {
902                         memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
903                         sas_configure_routing(dev, ex_phy->attached_sas_addr);
904                 }
905                 return 0;
906         } else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
907                 return 0;
908
909         if (ex_phy->attached_dev_type != SAS_END_DEV &&
910             ex_phy->attached_dev_type != FANOUT_DEV &&
911             ex_phy->attached_dev_type != EDGE_DEV) {
912                 SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
913                             "phy 0x%x\n", ex_phy->attached_dev_type,
914                             SAS_ADDR(dev->sas_addr),
915                             phy_id);
916                 return 0;
917         }
918
919         res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
920         if (res) {
921                 SAS_DPRINTK("configure routing for dev %016llx "
922                             "reported 0x%x. Forgotten\n",
923                             SAS_ADDR(ex_phy->attached_sas_addr), res);
924                 sas_disable_routing(dev, ex_phy->attached_sas_addr);
925                 return res;
926         }
927
928         res = sas_ex_join_wide_port(dev, phy_id);
929         if (!res) {
930                 SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
931                             phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
932                 return res;
933         }
934
935         switch (ex_phy->attached_dev_type) {
936         case SAS_END_DEV:
937                 child = sas_ex_discover_end_dev(dev, phy_id);
938                 break;
939         case FANOUT_DEV:
940                 if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
941                         SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
942                                     "attached to ex %016llx phy 0x%x\n",
943                                     SAS_ADDR(ex_phy->attached_sas_addr),
944                                     ex_phy->attached_phy_id,
945                                     SAS_ADDR(dev->sas_addr),
946                                     phy_id);
947                         sas_ex_disable_phy(dev, phy_id);
948                         break;
949                 } else
950                         memcpy(dev->port->disc.fanout_sas_addr,
951                                ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
952                 /* fallthrough */
953         case EDGE_DEV:
954                 child = sas_ex_discover_expander(dev, phy_id);
955                 break;
956         default:
957                 break;
958         }
959
960         if (child) {
961                 int i;
962
963                 for (i = 0; i < ex->num_phys; i++) {
964                         if (ex->ex_phy[i].phy_state == PHY_VACANT ||
965                             ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
966                                 continue;
967                         /*
968                          * Due to races, the phy might not get added to the
969                          * wide port, so we add the phy to the wide port here.
970                          */
971                         if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
972                             SAS_ADDR(child->sas_addr)) {
973                                 ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
974                                 res = sas_ex_join_wide_port(dev, i);
975                                 if (!res)
976                                         SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
977                                                     i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));
978
979                         }
980                 }
981         }
982
983         return res;
984 }
985
986 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
987 {
988         struct expander_device *ex = &dev->ex_dev;
989         int i;
990
991         for (i = 0; i < ex->num_phys; i++) {
992                 struct ex_phy *phy = &ex->ex_phy[i];
993
994                 if (phy->phy_state == PHY_VACANT ||
995                     phy->phy_state == PHY_NOT_PRESENT)
996                         continue;
997
998                 if ((phy->attached_dev_type == EDGE_DEV ||
999                      phy->attached_dev_type == FANOUT_DEV) &&
1000                     phy->routing_attr == SUBTRACTIVE_ROUTING) {
1001
1002                         memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);
1003
1004                         return 1;
1005                 }
1006         }
1007         return 0;
1008 }
1009
1010 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1011 {
1012         struct expander_device *ex = &dev->ex_dev;
1013         struct domain_device *child;
1014         u8 sub_addr[8] = {0, };
1015
1016         list_for_each_entry(child, &ex->children, siblings) {
1017                 if (child->dev_type != EDGE_DEV &&
1018                     child->dev_type != FANOUT_DEV)
1019                         continue;
1020                 if (sub_addr[0] == 0) {
1021                         sas_find_sub_addr(child, sub_addr);
1022                         continue;
1023                 } else {
1024                         u8 s2[8];
1025
1026                         if (sas_find_sub_addr(child, s2) &&
1027                             (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1028
1029                                 SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
1030                                             "diverges from subtractive "
1031                                             "boundary %016llx\n",
1032                                             SAS_ADDR(dev->sas_addr),
1033                                             SAS_ADDR(child->sas_addr),
1034                                             SAS_ADDR(s2),
1035                                             SAS_ADDR(sub_addr));
1036
1037                                 sas_ex_disable_port(child, s2);
1038                         }
1039                 }
1040         }
1041         return 0;
1042 }
1043 /**
1044  * sas_ex_discover_devices -- discover devices attached to this expander
1045  * dev: pointer to the expander domain device
1046  * single: if you want to do a single phy, else set to -1;
1047  *
1048  * Configure this expander for use with its devices and register the
1049  * devices of this expander.
1050  */
1051 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1052 {
1053         struct expander_device *ex = &dev->ex_dev;
1054         int i = 0, end = ex->num_phys;
1055         int res = 0;
1056
1057         if (0 <= single && single < end) {
1058                 i = single;
1059                 end = i+1;
1060         }
1061
1062         for ( ; i < end; i++) {
1063                 struct ex_phy *ex_phy = &ex->ex_phy[i];
1064
1065                 if (ex_phy->phy_state == PHY_VACANT ||
1066                     ex_phy->phy_state == PHY_NOT_PRESENT ||
1067                     ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1068                         continue;
1069
1070                 switch (ex_phy->linkrate) {
1071                 case SAS_PHY_DISABLED:
1072                 case SAS_PHY_RESET_PROBLEM:
1073                 case SAS_SATA_PORT_SELECTOR:
1074                         continue;
1075                 default:
1076                         res = sas_ex_discover_dev(dev, i);
1077                         if (res)
1078                                 break;
1079                         continue;
1080                 }
1081         }
1082
1083         if (!res)
1084                 sas_check_level_subtractive_boundary(dev);
1085
1086         return res;
1087 }
1088
1089 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1090 {
1091         struct expander_device *ex = &dev->ex_dev;
1092         int i;
1093         u8  *sub_sas_addr = NULL;
1094
1095         if (dev->dev_type != EDGE_DEV)
1096                 return 0;
1097
1098         for (i = 0; i < ex->num_phys; i++) {
1099                 struct ex_phy *phy = &ex->ex_phy[i];
1100
1101                 if (phy->phy_state == PHY_VACANT ||
1102                     phy->phy_state == PHY_NOT_PRESENT)
1103                         continue;
1104
1105                 if ((phy->attached_dev_type == FANOUT_DEV ||
1106                      phy->attached_dev_type == EDGE_DEV) &&
1107                     phy->routing_attr == SUBTRACTIVE_ROUTING) {
1108
1109                         if (!sub_sas_addr)
1110                                 sub_sas_addr = &phy->attached_sas_addr[0];
1111                         else if (SAS_ADDR(sub_sas_addr) !=
1112                                  SAS_ADDR(phy->attached_sas_addr)) {
1113
1114                                 SAS_DPRINTK("ex %016llx phy 0x%x "
1115                                             "diverges(%016llx) on subtractive "
1116                                             "boundary(%016llx). Disabled\n",
1117                                             SAS_ADDR(dev->sas_addr), i,
1118                                             SAS_ADDR(phy->attached_sas_addr),
1119                                             SAS_ADDR(sub_sas_addr));
1120                                 sas_ex_disable_phy(dev, i);
1121                         }
1122                 }
1123         }
1124         return 0;
1125 }
1126
1127 static void sas_print_parent_topology_bug(struct domain_device *child,
1128                                                  struct ex_phy *parent_phy,
1129                                                  struct ex_phy *child_phy)
1130 {
1131         static const char ra_char[] = {
1132                 [DIRECT_ROUTING] = 'D',
1133                 [SUBTRACTIVE_ROUTING] = 'S',
1134                 [TABLE_ROUTING] = 'T',
1135         };
1136         static const char *ex_type[] = {
1137                 [EDGE_DEV] = "edge",
1138                 [FANOUT_DEV] = "fanout",
1139         };
1140         struct domain_device *parent = child->parent;
1141
1142         sas_printk("%s ex %016llx (T2T supp:%d) phy 0x%x <--> %s ex %016llx "
1143                    "(T2T supp:%d) phy 0x%x has %c:%c routing link!\n",
1144
1145                    ex_type[parent->dev_type],
1146                    SAS_ADDR(parent->sas_addr),
1147                    parent->ex_dev.t2t_supp,
1148                    parent_phy->phy_id,
1149
1150                    ex_type[child->dev_type],
1151                    SAS_ADDR(child->sas_addr),
1152                    child->ex_dev.t2t_supp,
1153                    child_phy->phy_id,
1154
1155                    ra_char[parent_phy->routing_attr],
1156                    ra_char[child_phy->routing_attr]);
1157 }
1158
1159 static int sas_check_eeds(struct domain_device *child,
1160                                  struct ex_phy *parent_phy,
1161                                  struct ex_phy *child_phy)
1162 {
1163         int res = 0;
1164         struct domain_device *parent = child->parent;
1165
1166         if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1167                 res = -ENODEV;
1168                 SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
1169                             "phy S:0x%x, while there is a fanout ex %016llx\n",
1170                             SAS_ADDR(parent->sas_addr),
1171                             parent_phy->phy_id,
1172                             SAS_ADDR(child->sas_addr),
1173                             child_phy->phy_id,
1174                             SAS_ADDR(parent->port->disc.fanout_sas_addr));
1175         } else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1176                 memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1177                        SAS_ADDR_SIZE);
1178                 memcpy(parent->port->disc.eeds_b, child->sas_addr,
1179                        SAS_ADDR_SIZE);
1180         } else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1181                     SAS_ADDR(parent->sas_addr)) ||
1182                    (SAS_ADDR(parent->port->disc.eeds_a) ==
1183                     SAS_ADDR(child->sas_addr)))
1184                    &&
1185                    ((SAS_ADDR(parent->port->disc.eeds_b) ==
1186                      SAS_ADDR(parent->sas_addr)) ||
1187                     (SAS_ADDR(parent->port->disc.eeds_b) ==
1188                      SAS_ADDR(child->sas_addr))))
1189                 ;
1190         else {
1191                 res = -ENODEV;
1192                 SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
1193                             "phy 0x%x link forms a third EEDS!\n",
1194                             SAS_ADDR(parent->sas_addr),
1195                             parent_phy->phy_id,
1196                             SAS_ADDR(child->sas_addr),
1197                             child_phy->phy_id);
1198         }
1199
1200         return res;
1201 }
1202
1203 /* Here we spill over 80 columns.  It is intentional.
1204  */
1205 static int sas_check_parent_topology(struct domain_device *child)
1206 {
1207         struct expander_device *child_ex = &child->ex_dev;
1208         struct expander_device *parent_ex;
1209         int i;
1210         int res = 0;
1211
1212         if (!child->parent)
1213                 return 0;
1214
1215         if (child->parent->dev_type != EDGE_DEV &&
1216             child->parent->dev_type != FANOUT_DEV)
1217                 return 0;
1218
1219         parent_ex = &child->parent->ex_dev;
1220
1221         for (i = 0; i < parent_ex->num_phys; i++) {
1222                 struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1223                 struct ex_phy *child_phy;
1224
1225                 if (parent_phy->phy_state == PHY_VACANT ||
1226                     parent_phy->phy_state == PHY_NOT_PRESENT)
1227                         continue;
1228
1229                 if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1230                         continue;
1231
1232                 child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1233
1234                 switch (child->parent->dev_type) {
1235                 case EDGE_DEV:
1236                         if (child->dev_type == FANOUT_DEV) {
1237                                 if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1238                                     child_phy->routing_attr != TABLE_ROUTING) {
1239                                         sas_print_parent_topology_bug(child, parent_phy, child_phy);
1240                                         res = -ENODEV;
1241                                 }
1242                         } else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1243                                 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1244                                         res = sas_check_eeds(child, parent_phy, child_phy);
1245                                 } else if (child_phy->routing_attr != TABLE_ROUTING) {
1246                                         sas_print_parent_topology_bug(child, parent_phy, child_phy);
1247                                         res = -ENODEV;
1248                                 }
1249                         } else if (parent_phy->routing_attr == TABLE_ROUTING) {
1250                                 if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1251                                     (child_phy->routing_attr == TABLE_ROUTING &&
1252                                      child_ex->t2t_supp && parent_ex->t2t_supp)) {
1253                                         /* All good */;
1254                                 } else {
1255                                         sas_print_parent_topology_bug(child, parent_phy, child_phy);
1256                                         res = -ENODEV;
1257                                 }
1258                         }
1259                         break;
1260                 case FANOUT_DEV:
1261                         if (parent_phy->routing_attr != TABLE_ROUTING ||
1262                             child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1263                                 sas_print_parent_topology_bug(child, parent_phy, child_phy);
1264                                 res = -ENODEV;
1265                         }
1266                         break;
1267                 default:
1268                         break;
1269                 }
1270         }
1271
1272         return res;
1273 }
1274
1275 #define RRI_REQ_SIZE  16
1276 #define RRI_RESP_SIZE 44
1277
1278 static int sas_configure_present(struct domain_device *dev, int phy_id,
1279                                  u8 *sas_addr, int *index, int *present)
1280 {
1281         int i, res = 0;
1282         struct expander_device *ex = &dev->ex_dev;
1283         struct ex_phy *phy = &ex->ex_phy[phy_id];
1284         u8 *rri_req;
1285         u8 *rri_resp;
1286
1287         *present = 0;
1288         *index = 0;
1289
1290         rri_req = alloc_smp_req(RRI_REQ_SIZE);
1291         if (!rri_req)
1292                 return -ENOMEM;
1293
1294         rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1295         if (!rri_resp) {
1296                 kfree(rri_req);
1297                 return -ENOMEM;
1298         }
1299
1300         rri_req[1] = SMP_REPORT_ROUTE_INFO;
1301         rri_req[9] = phy_id;
1302
1303         for (i = 0; i < ex->max_route_indexes ; i++) {
1304                 *(__be16 *)(rri_req+6) = cpu_to_be16(i);
1305                 res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1306                                        RRI_RESP_SIZE);
1307                 if (res)
1308                         goto out;
1309                 res = rri_resp[2];
1310                 if (res == SMP_RESP_NO_INDEX) {
1311                         SAS_DPRINTK("overflow of indexes: dev %016llx "
1312                                     "phy 0x%x index 0x%x\n",
1313                                     SAS_ADDR(dev->sas_addr), phy_id, i);
1314                         goto out;
1315                 } else if (res != SMP_RESP_FUNC_ACC) {
1316                         SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1317                                     "result 0x%x\n", __func__,
1318                                     SAS_ADDR(dev->sas_addr), phy_id, i, res);
1319                         goto out;
1320                 }
1321                 if (SAS_ADDR(sas_addr) != 0) {
1322                         if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1323                                 *index = i;
1324                                 if ((rri_resp[12] & 0x80) == 0x80)
1325                                         *present = 0;
1326                                 else
1327                                         *present = 1;
1328                                 goto out;
1329                         } else if (SAS_ADDR(rri_resp+16) == 0) {
1330                                 *index = i;
1331                                 *present = 0;
1332                                 goto out;
1333                         }
1334                 } else if (SAS_ADDR(rri_resp+16) == 0 &&
1335                            phy->last_da_index < i) {
1336                         phy->last_da_index = i;
1337                         *index = i;
1338                         *present = 0;
1339                         goto out;
1340                 }
1341         }
1342         res = -1;
1343 out:
1344         kfree(rri_req);
1345         kfree(rri_resp);
1346         return res;
1347 }
1348
1349 #define CRI_REQ_SIZE  44
1350 #define CRI_RESP_SIZE  8
1351
1352 static int sas_configure_set(struct domain_device *dev, int phy_id,
1353                              u8 *sas_addr, int index, int include)
1354 {
1355         int res;
1356         u8 *cri_req;
1357         u8 *cri_resp;
1358
1359         cri_req = alloc_smp_req(CRI_REQ_SIZE);
1360         if (!cri_req)
1361                 return -ENOMEM;
1362
1363         cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1364         if (!cri_resp) {
1365                 kfree(cri_req);
1366                 return -ENOMEM;
1367         }
1368
1369         cri_req[1] = SMP_CONF_ROUTE_INFO;
1370         *(__be16 *)(cri_req+6) = cpu_to_be16(index);
1371         cri_req[9] = phy_id;
1372         if (SAS_ADDR(sas_addr) == 0 || !include)
1373                 cri_req[12] |= 0x80;
1374         memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1375
1376         res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1377                                CRI_RESP_SIZE);
1378         if (res)
1379                 goto out;
1380         res = cri_resp[2];
1381         if (res == SMP_RESP_NO_INDEX) {
1382                 SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
1383                             "index 0x%x\n",
1384                             SAS_ADDR(dev->sas_addr), phy_id, index);
1385         }
1386 out:
1387         kfree(cri_req);
1388         kfree(cri_resp);
1389         return res;
1390 }
1391
1392 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1393                                     u8 *sas_addr, int include)
1394 {
1395         int index;
1396         int present;
1397         int res;
1398
1399         res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1400         if (res)
1401                 return res;
1402         if (include ^ present)
1403                 return sas_configure_set(dev, phy_id, sas_addr, index,include);
1404
1405         return res;
1406 }
1407
1408 /**
1409  * sas_configure_parent -- configure routing table of parent
1410  * parent: parent expander
1411  * child: child expander
1412  * sas_addr: SAS port identifier of device directly attached to child
1413  */
1414 static int sas_configure_parent(struct domain_device *parent,
1415                                 struct domain_device *child,
1416                                 u8 *sas_addr, int include)
1417 {
1418         struct expander_device *ex_parent = &parent->ex_dev;
1419         int res = 0;
1420         int i;
1421
1422         if (parent->parent) {
1423                 res = sas_configure_parent(parent->parent, parent, sas_addr,
1424                                            include);
1425                 if (res)
1426                         return res;
1427         }
1428
1429         if (ex_parent->conf_route_table == 0) {
1430                 SAS_DPRINTK("ex %016llx has self-configuring routing table\n",
1431                             SAS_ADDR(parent->sas_addr));
1432                 return 0;
1433         }
1434
1435         for (i = 0; i < ex_parent->num_phys; i++) {
1436                 struct ex_phy *phy = &ex_parent->ex_phy[i];
1437
1438                 if ((phy->routing_attr == TABLE_ROUTING) &&
1439                     (SAS_ADDR(phy->attached_sas_addr) ==
1440                      SAS_ADDR(child->sas_addr))) {
1441                         res = sas_configure_phy(parent, i, sas_addr, include);
1442                         if (res)
1443                                 return res;
1444                 }
1445         }
1446
1447         return res;
1448 }
1449
1450 /**
1451  * sas_configure_routing -- configure routing
1452  * dev: expander device
1453  * sas_addr: port identifier of device directly attached to the expander device
1454  */
1455 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1456 {
1457         if (dev->parent)
1458                 return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1459         return 0;
1460 }
1461
1462 static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr)
1463 {
1464         if (dev->parent)
1465                 return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1466         return 0;
1467 }
1468
1469 /**
1470  * sas_discover_expander -- expander discovery
1471  * @ex: pointer to expander domain device
1472  *
1473  * See comment in sas_discover_sata().
1474  */
1475 static int sas_discover_expander(struct domain_device *dev)
1476 {
1477         int res;
1478
1479         res = sas_notify_lldd_dev_found(dev);
1480         if (res)
1481                 return res;
1482
1483         res = sas_ex_general(dev);
1484         if (res)
1485                 goto out_err;
1486         res = sas_ex_manuf_info(dev);
1487         if (res)
1488                 goto out_err;
1489
1490         res = sas_expander_discover(dev);
1491         if (res) {
1492                 SAS_DPRINTK("expander %016llx discovery failed(0x%x)\n",
1493                             SAS_ADDR(dev->sas_addr), res);
1494                 goto out_err;
1495         }
1496
1497         sas_check_ex_subtractive_boundary(dev);
1498         res = sas_check_parent_topology(dev);
1499         if (res)
1500                 goto out_err;
1501         return 0;
1502 out_err:
1503         sas_notify_lldd_dev_gone(dev);
1504         return res;
1505 }
1506
1507 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1508 {
1509         int res = 0;
1510         struct domain_device *dev;
1511
1512         list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1513                 if (dev->dev_type == EDGE_DEV ||
1514                     dev->dev_type == FANOUT_DEV) {
1515                         struct sas_expander_device *ex =
1516                                 rphy_to_expander_device(dev->rphy);
1517
1518                         if (level == ex->level)
1519                                 res = sas_ex_discover_devices(dev, -1);
1520                         else if (level > 0)
1521                                 res = sas_ex_discover_devices(port->port_dev, -1);
1522
1523                 }
1524         }
1525
1526         return res;
1527 }
1528
1529 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1530 {
1531         int res;
1532         int level;
1533
1534         do {
1535                 level = port->disc.max_level;
1536                 res = sas_ex_level_discovery(port, level);
1537                 mb();
1538         } while (level < port->disc.max_level);
1539
1540         return res;
1541 }
1542
1543 int sas_discover_root_expander(struct domain_device *dev)
1544 {
1545         int res;
1546         struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1547
1548         res = sas_rphy_add(dev->rphy);
1549         if (res)
1550                 goto out_err;
1551
1552         ex->level = dev->port->disc.max_level; /* 0 */
1553         res = sas_discover_expander(dev);
1554         if (res)
1555                 goto out_err2;
1556
1557         sas_ex_bfs_disc(dev->port);
1558
1559         return res;
1560
1561 out_err2:
1562         sas_rphy_remove(dev->rphy);
1563 out_err:
1564         return res;
1565 }
1566
1567 /* ---------- Domain revalidation ---------- */
1568
1569 static int sas_get_phy_discover(struct domain_device *dev,
1570                                 int phy_id, struct smp_resp *disc_resp)
1571 {
1572         int res;
1573         u8 *disc_req;
1574
1575         disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1576         if (!disc_req)
1577                 return -ENOMEM;
1578
1579         disc_req[1] = SMP_DISCOVER;
1580         disc_req[9] = phy_id;
1581
1582         res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1583                                disc_resp, DISCOVER_RESP_SIZE);
1584         if (res)
1585                 goto out;
1586         else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1587                 res = disc_resp->result;
1588                 goto out;
1589         }
1590 out:
1591         kfree(disc_req);
1592         return res;
1593 }
1594
1595 static int sas_get_phy_change_count(struct domain_device *dev,
1596                                     int phy_id, int *pcc)
1597 {
1598         int res;
1599         struct smp_resp *disc_resp;
1600
1601         disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1602         if (!disc_resp)
1603                 return -ENOMEM;
1604
1605         res = sas_get_phy_discover(dev, phy_id, disc_resp);
1606         if (!res)
1607                 *pcc = disc_resp->disc.change_count;
1608
1609         kfree(disc_resp);
1610         return res;
1611 }
1612
1613 static int sas_get_phy_attached_sas_addr(struct domain_device *dev,
1614                                          int phy_id, u8 *attached_sas_addr)
1615 {
1616         int res;
1617         struct smp_resp *disc_resp;
1618         struct discover_resp *dr;
1619
1620         disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1621         if (!disc_resp)
1622                 return -ENOMEM;
1623         dr = &disc_resp->disc;
1624
1625         res = sas_get_phy_discover(dev, phy_id, disc_resp);
1626         if (!res) {
1627                 memcpy(attached_sas_addr,disc_resp->disc.attached_sas_addr,8);
1628                 if (dr->attached_dev_type == 0)
1629                         memset(attached_sas_addr, 0, 8);
1630         }
1631         kfree(disc_resp);
1632         return res;
1633 }
1634
1635 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1636                               int from_phy, bool update)
1637 {
1638         struct expander_device *ex = &dev->ex_dev;
1639         int res = 0;
1640         int i;
1641
1642         for (i = from_phy; i < ex->num_phys; i++) {
1643                 int phy_change_count = 0;
1644
1645                 res = sas_get_phy_change_count(dev, i, &phy_change_count);
1646                 if (res)
1647                         goto out;
1648                 else if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1649                         if (update)
1650                                 ex->ex_phy[i].phy_change_count =
1651                                         phy_change_count;
1652                         *phy_id = i;
1653                         return 0;
1654                 }
1655         }
1656 out:
1657         return res;
1658 }
1659
1660 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1661 {
1662         int res;
1663         u8  *rg_req;
1664         struct smp_resp  *rg_resp;
1665
1666         rg_req = alloc_smp_req(RG_REQ_SIZE);
1667         if (!rg_req)
1668                 return -ENOMEM;
1669
1670         rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1671         if (!rg_resp) {
1672                 kfree(rg_req);
1673                 return -ENOMEM;
1674         }
1675
1676         rg_req[1] = SMP_REPORT_GENERAL;
1677
1678         res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1679                                RG_RESP_SIZE);
1680         if (res)
1681                 goto out;
1682         if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1683                 res = rg_resp->result;
1684                 goto out;
1685         }
1686
1687         *ecc = be16_to_cpu(rg_resp->rg.change_count);
1688 out:
1689         kfree(rg_resp);
1690         kfree(rg_req);
1691         return res;
1692 }
1693 /**
1694  * sas_find_bcast_dev -  find the device issue BROADCAST(CHANGE).
1695  * @dev:domain device to be detect.
1696  * @src_dev: the device which originated BROADCAST(CHANGE).
1697  *
1698  * Add self-configuration expander suport. Suppose two expander cascading,
1699  * when the first level expander is self-configuring, hotplug the disks in
1700  * second level expander, BROADCAST(CHANGE) will not only be originated
1701  * in the second level expander, but also be originated in the first level
1702  * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1703  * expander changed count in two level expanders will all increment at least
1704  * once, but the phy which chang count has changed is the source device which
1705  * we concerned.
1706  */
1707
1708 static int sas_find_bcast_dev(struct domain_device *dev,
1709                               struct domain_device **src_dev)
1710 {
1711         struct expander_device *ex = &dev->ex_dev;
1712         int ex_change_count = -1;
1713         int phy_id = -1;
1714         int res;
1715         struct domain_device *ch;
1716
1717         res = sas_get_ex_change_count(dev, &ex_change_count);
1718         if (res)
1719                 goto out;
1720         if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1721                 /* Just detect if this expander phys phy change count changed,
1722                 * in order to determine if this expander originate BROADCAST,
1723                 * and do not update phy change count field in our structure.
1724                 */
1725                 res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1726                 if (phy_id != -1) {
1727                         *src_dev = dev;
1728                         ex->ex_change_count = ex_change_count;
1729                         SAS_DPRINTK("Expander phy change count has changed\n");
1730                         return res;
1731                 } else
1732                         SAS_DPRINTK("Expander phys DID NOT change\n");
1733         }
1734         list_for_each_entry(ch, &ex->children, siblings) {
1735                 if (ch->dev_type == EDGE_DEV || ch->dev_type == FANOUT_DEV) {
1736                         res = sas_find_bcast_dev(ch, src_dev);
1737                         if (src_dev)
1738                                 return res;
1739                 }
1740         }
1741 out:
1742         return res;
1743 }
1744
1745 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1746 {
1747         struct expander_device *ex = &dev->ex_dev;
1748         struct domain_device *child, *n;
1749
1750         list_for_each_entry_safe(child, n, &ex->children, siblings) {
1751                 child->gone = 1;
1752                 if (child->dev_type == EDGE_DEV ||
1753                     child->dev_type == FANOUT_DEV)
1754                         sas_unregister_ex_tree(port, child);
1755                 else
1756                         sas_unregister_dev(port, child);
1757         }
1758         sas_unregister_dev(port, dev);
1759 }
1760
1761 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1762                                          int phy_id, bool last)
1763 {
1764         struct expander_device *ex_dev = &parent->ex_dev;
1765         struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1766         struct domain_device *child, *n;
1767         if (last) {
1768                 list_for_each_entry_safe(child, n,
1769                         &ex_dev->children, siblings) {
1770                         if (SAS_ADDR(child->sas_addr) ==
1771                             SAS_ADDR(phy->attached_sas_addr)) {
1772                                 child->gone = 1;
1773                                 if (child->dev_type == EDGE_DEV ||
1774                                     child->dev_type == FANOUT_DEV)
1775                                         sas_unregister_ex_tree(parent->port, child);
1776                                 else
1777                                         sas_unregister_dev(parent->port, child);
1778                                 break;
1779                         }
1780                 }
1781                 parent->gone = 1;
1782                 sas_disable_routing(parent, phy->attached_sas_addr);
1783         }
1784         memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1785         sas_port_delete_phy(phy->port, phy->phy);
1786         if (phy->port->num_phys == 0)
1787                 sas_port_delete(phy->port);
1788         phy->port = NULL;
1789 }
1790
1791 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1792                                           const int level)
1793 {
1794         struct expander_device *ex_root = &root->ex_dev;
1795         struct domain_device *child;
1796         int res = 0;
1797
1798         list_for_each_entry(child, &ex_root->children, siblings) {
1799                 if (child->dev_type == EDGE_DEV ||
1800                     child->dev_type == FANOUT_DEV) {
1801                         struct sas_expander_device *ex =
1802                                 rphy_to_expander_device(child->rphy);
1803
1804                         if (level > ex->level)
1805                                 res = sas_discover_bfs_by_root_level(child,
1806                                                                      level);
1807                         else if (level == ex->level)
1808                                 res = sas_ex_discover_devices(child, -1);
1809                 }
1810         }
1811         return res;
1812 }
1813
1814 static int sas_discover_bfs_by_root(struct domain_device *dev)
1815 {
1816         int res;
1817         struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1818         int level = ex->level+1;
1819
1820         res = sas_ex_discover_devices(dev, -1);
1821         if (res)
1822                 goto out;
1823         do {
1824                 res = sas_discover_bfs_by_root_level(dev, level);
1825                 mb();
1826                 level += 1;
1827         } while (level <= dev->port->disc.max_level);
1828 out:
1829         return res;
1830 }
1831
1832 static int sas_discover_new(struct domain_device *dev, int phy_id)
1833 {
1834         struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1835         struct domain_device *child;
1836         bool found = false;
1837         int res, i;
1838
1839         SAS_DPRINTK("ex %016llx phy%d new device attached\n",
1840                     SAS_ADDR(dev->sas_addr), phy_id);
1841         res = sas_ex_phy_discover(dev, phy_id);
1842         if (res)
1843                 goto out;
1844         /* to support the wide port inserted */
1845         for (i = 0; i < dev->ex_dev.num_phys; i++) {
1846                 struct ex_phy *ex_phy_temp = &dev->ex_dev.ex_phy[i];
1847                 if (i == phy_id)
1848                         continue;
1849                 if (SAS_ADDR(ex_phy_temp->attached_sas_addr) ==
1850                     SAS_ADDR(ex_phy->attached_sas_addr)) {
1851                         found = true;
1852                         break;
1853                 }
1854         }
1855         if (found) {
1856                 sas_ex_join_wide_port(dev, phy_id);
1857                 return 0;
1858         }
1859         res = sas_ex_discover_devices(dev, phy_id);
1860         if (!res)
1861                 goto out;
1862         list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1863                 if (SAS_ADDR(child->sas_addr) ==
1864                     SAS_ADDR(ex_phy->attached_sas_addr)) {
1865                         if (child->dev_type == EDGE_DEV ||
1866                             child->dev_type == FANOUT_DEV)
1867                                 res = sas_discover_bfs_by_root(child);
1868                         break;
1869                 }
1870         }
1871 out:
1872         return res;
1873 }
1874
1875 static int sas_rediscover_dev(struct domain_device *dev, int phy_id, bool last)
1876 {
1877         struct expander_device *ex = &dev->ex_dev;
1878         struct ex_phy *phy = &ex->ex_phy[phy_id];
1879         u8 attached_sas_addr[8];
1880         int res;
1881
1882         res = sas_get_phy_attached_sas_addr(dev, phy_id, attached_sas_addr);
1883         switch (res) {
1884         case SMP_RESP_NO_PHY:
1885                 phy->phy_state = PHY_NOT_PRESENT;
1886                 sas_unregister_devs_sas_addr(dev, phy_id, last);
1887                 goto out; break;
1888         case SMP_RESP_PHY_VACANT:
1889                 phy->phy_state = PHY_VACANT;
1890                 sas_unregister_devs_sas_addr(dev, phy_id, last);
1891                 goto out; break;
1892         case SMP_RESP_FUNC_ACC:
1893                 break;
1894         }
1895
1896         if (SAS_ADDR(attached_sas_addr) == 0) {
1897                 phy->phy_state = PHY_EMPTY;
1898                 sas_unregister_devs_sas_addr(dev, phy_id, last);
1899         } else if (SAS_ADDR(attached_sas_addr) ==
1900                    SAS_ADDR(phy->attached_sas_addr)) {
1901                 SAS_DPRINTK("ex %016llx phy 0x%x broadcast flutter\n",
1902                             SAS_ADDR(dev->sas_addr), phy_id);
1903                 sas_ex_phy_discover(dev, phy_id);
1904         } else
1905                 res = sas_discover_new(dev, phy_id);
1906 out:
1907         return res;
1908 }
1909
1910 /**
1911  * sas_rediscover - revalidate the domain.
1912  * @dev:domain device to be detect.
1913  * @phy_id: the phy id will be detected.
1914  *
1915  * NOTE: this process _must_ quit (return) as soon as any connection
1916  * errors are encountered.  Connection recovery is done elsewhere.
1917  * Discover process only interrogates devices in order to discover the
1918  * domain.For plugging out, we un-register the device only when it is
1919  * the last phy in the port, for other phys in this port, we just delete it
1920  * from the port.For inserting, we do discovery when it is the
1921  * first phy,for other phys in this port, we add it to the port to
1922  * forming the wide-port.
1923  */
1924 static int sas_rediscover(struct domain_device *dev, const int phy_id)
1925 {
1926         struct expander_device *ex = &dev->ex_dev;
1927         struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
1928         int res = 0;
1929         int i;
1930         bool last = true;       /* is this the last phy of the port */
1931
1932         SAS_DPRINTK("ex %016llx phy%d originated BROADCAST(CHANGE)\n",
1933                     SAS_ADDR(dev->sas_addr), phy_id);
1934
1935         if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
1936                 for (i = 0; i < ex->num_phys; i++) {
1937                         struct ex_phy *phy = &ex->ex_phy[i];
1938
1939                         if (i == phy_id)
1940                                 continue;
1941                         if (SAS_ADDR(phy->attached_sas_addr) ==
1942                             SAS_ADDR(changed_phy->attached_sas_addr)) {
1943                                 SAS_DPRINTK("phy%d part of wide port with "
1944                                             "phy%d\n", phy_id, i);
1945                                 last = false;
1946                                 break;
1947                         }
1948                 }
1949                 res = sas_rediscover_dev(dev, phy_id, last);
1950         } else
1951                 res = sas_discover_new(dev, phy_id);
1952         return res;
1953 }
1954
1955 /**
1956  * sas_revalidate_domain -- revalidate the domain
1957  * @port: port to the domain of interest
1958  *
1959  * NOTE: this process _must_ quit (return) as soon as any connection
1960  * errors are encountered.  Connection recovery is done elsewhere.
1961  * Discover process only interrogates devices in order to discover the
1962  * domain.
1963  */
1964 int sas_ex_revalidate_domain(struct domain_device *port_dev)
1965 {
1966         int res;
1967         struct domain_device *dev = NULL;
1968
1969         res = sas_find_bcast_dev(port_dev, &dev);
1970         if (res)
1971                 goto out;
1972         if (dev) {
1973                 struct expander_device *ex = &dev->ex_dev;
1974                 int i = 0, phy_id;
1975
1976                 do {
1977                         phy_id = -1;
1978                         res = sas_find_bcast_phy(dev, &phy_id, i, true);
1979                         if (phy_id == -1)
1980                                 break;
1981                         res = sas_rediscover(dev, phy_id);
1982                         i = phy_id + 1;
1983                 } while (i < ex->num_phys);
1984         }
1985 out:
1986         return res;
1987 }
1988
1989 int sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
1990                     struct request *req)
1991 {
1992         struct domain_device *dev;
1993         int ret, type;
1994         struct request *rsp = req->next_rq;
1995
1996         if (!rsp) {
1997                 printk("%s: space for a smp response is missing\n",
1998                        __func__);
1999                 return -EINVAL;
2000         }
2001
2002         /* no rphy means no smp target support (ie aic94xx host) */
2003         if (!rphy)
2004                 return sas_smp_host_handler(shost, req, rsp);
2005
2006         type = rphy->identify.device_type;
2007
2008         if (type != SAS_EDGE_EXPANDER_DEVICE &&
2009             type != SAS_FANOUT_EXPANDER_DEVICE) {
2010                 printk("%s: can we send a smp request to a device?\n",
2011                        __func__);
2012                 return -EINVAL;
2013         }
2014
2015         dev = sas_find_dev_by_rphy(rphy);
2016         if (!dev) {
2017                 printk("%s: fail to find a domain_device?\n", __func__);
2018                 return -EINVAL;
2019         }
2020
2021         /* do we need to support multiple segments? */
2022         if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
2023                 printk("%s: multiple segments req %u %u, rsp %u %u\n",
2024                        __func__, req->bio->bi_vcnt, blk_rq_bytes(req),
2025                        rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
2026                 return -EINVAL;
2027         }
2028
2029         ret = smp_execute_task(dev, bio_data(req->bio), blk_rq_bytes(req),
2030                                bio_data(rsp->bio), blk_rq_bytes(rsp));
2031         if (ret > 0) {
2032                 /* positive number is the untransferred residual */
2033                 rsp->resid_len = ret;
2034                 req->resid_len = 0;
2035                 ret = 0;
2036         } else if (ret == 0) {
2037                 rsp->resid_len = 0;
2038                 req->resid_len = 0;
2039         }
2040
2041         return ret;
2042 }