]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/libsas/sas_discover.c
[SCSI] libsas: kill unused smp_portal code
[linux-2.6-omap-h63xx.git] / drivers / scsi / libsas / sas_discover.c
1 /*
2  * Serial Attached SCSI (SAS) Discover process
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/pci.h>
26 #include <linux/scatterlist.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_eh.h>
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 /* ---------- Basic task processing for discovery purposes ---------- */
36
37 void sas_init_dev(struct domain_device *dev)
38 {
39         INIT_LIST_HEAD(&dev->siblings);
40         INIT_LIST_HEAD(&dev->dev_list_node);
41         switch (dev->dev_type) {
42         case SAS_END_DEV:
43                 break;
44         case EDGE_DEV:
45         case FANOUT_DEV:
46                 INIT_LIST_HEAD(&dev->ex_dev.children);
47                 break;
48         case SATA_DEV:
49         case SATA_PM:
50         case SATA_PM_PORT:
51                 INIT_LIST_HEAD(&dev->sata_dev.children);
52                 break;
53         default:
54                 break;
55         }
56 }
57
58 static void sas_task_timedout(unsigned long _task)
59 {
60         struct sas_task *task = (void *) _task;
61         unsigned long flags;
62
63         spin_lock_irqsave(&task->task_state_lock, flags);
64         if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
65                 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
66         spin_unlock_irqrestore(&task->task_state_lock, flags);
67
68         complete(&task->completion);
69 }
70
71 static void sas_disc_task_done(struct sas_task *task)
72 {
73         if (!del_timer(&task->timer))
74                 return;
75         complete(&task->completion);
76 }
77
78 #define SAS_DEV_TIMEOUT 10
79
80 /**
81  * sas_execute_task -- Basic task processing for discovery
82  * @task: the task to be executed
83  * @buffer: pointer to buffer to do I/O
84  * @size: size of @buffer
85  * @pci_dma_dir: PCI_DMA_...
86  */
87 static int sas_execute_task(struct sas_task *task, void *buffer, int size,
88                             int pci_dma_dir)
89 {
90         int res = 0;
91         struct scatterlist *scatter = NULL;
92         struct task_status_struct *ts = &task->task_status;
93         int num_scatter = 0;
94         int retries = 0;
95         struct sas_internal *i =
96                 to_sas_internal(task->dev->port->ha->core.shost->transportt);
97
98         if (pci_dma_dir != PCI_DMA_NONE) {
99                 scatter = kzalloc(sizeof(*scatter), GFP_KERNEL);
100                 if (!scatter)
101                         goto out;
102
103                 sg_init_one(scatter, buffer, size);
104                 num_scatter = 1;
105         }
106
107         task->task_proto = task->dev->tproto;
108         task->scatter = scatter;
109         task->num_scatter = num_scatter;
110         task->total_xfer_len = size;
111         task->data_dir = pci_dma_dir;
112         task->task_done = sas_disc_task_done;
113         if (pci_dma_dir != PCI_DMA_NONE &&
114             sas_protocol_ata(task->task_proto)) {
115                 task->num_scatter = pci_map_sg(task->dev->port->ha->pcidev,
116                                                task->scatter,
117                                                task->num_scatter,
118                                                task->data_dir);
119         }
120
121         for (retries = 0; retries < 5; retries++) {
122                 task->task_state_flags = SAS_TASK_STATE_PENDING;
123                 init_completion(&task->completion);
124
125                 task->timer.data = (unsigned long) task;
126                 task->timer.function = sas_task_timedout;
127                 task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ;
128                 add_timer(&task->timer);
129
130                 res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL);
131                 if (res) {
132                         del_timer(&task->timer);
133                         SAS_DPRINTK("executing SAS discovery task failed:%d\n",
134                                     res);
135                         goto ex_err;
136                 }
137                 wait_for_completion(&task->completion);
138                 res = -ETASK;
139                 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
140                         int res2;
141                         SAS_DPRINTK("task aborted, flags:0x%x\n",
142                                     task->task_state_flags);
143                         res2 = i->dft->lldd_abort_task(task);
144                         SAS_DPRINTK("came back from abort task\n");
145                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
146                                 if (res2 == TMF_RESP_FUNC_COMPLETE)
147                                         continue; /* Retry the task */
148                                 else
149                                         goto ex_err;
150                         }
151                 }
152                 if (task->task_status.stat == SAM_BUSY ||
153                            task->task_status.stat == SAM_TASK_SET_FULL ||
154                            task->task_status.stat == SAS_QUEUE_FULL) {
155                         SAS_DPRINTK("task: q busy, sleeping...\n");
156                         schedule_timeout_interruptible(HZ);
157                 } else if (task->task_status.stat == SAM_CHECK_COND) {
158                         struct scsi_sense_hdr shdr;
159
160                         if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size,
161                                                   &shdr)) {
162                                 SAS_DPRINTK("couldn't normalize sense\n");
163                                 continue;
164                         }
165                         if ((shdr.sense_key == 6 && shdr.asc == 0x29) ||
166                             (shdr.sense_key == 2 && shdr.asc == 4 &&
167                              shdr.ascq == 1)) {
168                                 SAS_DPRINTK("device %016llx LUN: %016llx "
169                                             "powering up or not ready yet, "
170                                             "sleeping...\n",
171                                             SAS_ADDR(task->dev->sas_addr),
172                                             SAS_ADDR(task->ssp_task.LUN));
173
174                                 schedule_timeout_interruptible(5*HZ);
175                         } else if (shdr.sense_key == 1) {
176                                 res = 0;
177                                 break;
178                         } else if (shdr.sense_key == 5) {
179                                 break;
180                         } else {
181                                 SAS_DPRINTK("dev %016llx LUN: %016llx "
182                                             "sense key:0x%x ASC:0x%x ASCQ:0x%x"
183                                             "\n",
184                                             SAS_ADDR(task->dev->sas_addr),
185                                             SAS_ADDR(task->ssp_task.LUN),
186                                             shdr.sense_key,
187                                             shdr.asc, shdr.ascq);
188                         }
189                 } else if (task->task_status.resp != SAS_TASK_COMPLETE ||
190                            task->task_status.stat != SAM_GOOD) {
191                         SAS_DPRINTK("task finished with resp:0x%x, "
192                                     "stat:0x%x\n",
193                                     task->task_status.resp,
194                                     task->task_status.stat);
195                         goto ex_err;
196                 } else {
197                         res = 0;
198                         break;
199                 }
200         }
201 ex_err:
202         if (pci_dma_dir != PCI_DMA_NONE) {
203                 if (sas_protocol_ata(task->task_proto))
204                         pci_unmap_sg(task->dev->port->ha->pcidev,
205                                      task->scatter, task->num_scatter,
206                                      task->data_dir);
207                 kfree(scatter);
208         }
209 out:
210         return res;
211 }
212
213 /* ---------- Domain device discovery ---------- */
214
215 /**
216  * sas_get_port_device -- Discover devices which caused port creation
217  * @port: pointer to struct sas_port of interest
218  *
219  * Devices directly attached to a HA port, have no parent.  This is
220  * how we know they are (domain) "root" devices.  All other devices
221  * do, and should have their "parent" pointer set appropriately as
222  * soon as a child device is discovered.
223  */
224 static int sas_get_port_device(struct asd_sas_port *port)
225 {
226         unsigned long flags;
227         struct asd_sas_phy *phy;
228         struct sas_rphy *rphy;
229         struct domain_device *dev;
230
231         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
232         if (!dev)
233                 return -ENOMEM;
234
235         spin_lock_irqsave(&port->phy_list_lock, flags);
236         if (list_empty(&port->phy_list)) {
237                 spin_unlock_irqrestore(&port->phy_list_lock, flags);
238                 kfree(dev);
239                 return -ENODEV;
240         }
241         phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
242         spin_lock(&phy->frame_rcvd_lock);
243         memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
244                                              (size_t)phy->frame_rcvd_size));
245         spin_unlock(&phy->frame_rcvd_lock);
246         spin_unlock_irqrestore(&port->phy_list_lock, flags);
247
248         if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
249                 struct dev_to_host_fis *fis =
250                         (struct dev_to_host_fis *) dev->frame_rcvd;
251                 if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
252                     fis->byte_count_low==0x69 && fis->byte_count_high == 0x96
253                     && (fis->device & ~0x10) == 0)
254                         dev->dev_type = SATA_PM;
255                 else
256                         dev->dev_type = SATA_DEV;
257                 dev->tproto = SATA_PROTO;
258         } else {
259                 struct sas_identify_frame *id =
260                         (struct sas_identify_frame *) dev->frame_rcvd;
261                 dev->dev_type = id->dev_type;
262                 dev->iproto = id->initiator_bits;
263                 dev->tproto = id->target_bits;
264         }
265
266         sas_init_dev(dev);
267
268         switch (dev->dev_type) {
269         case SAS_END_DEV:
270         case SATA_DEV:
271                 rphy = sas_end_device_alloc(port->port);
272                 break;
273         case EDGE_DEV:
274                 rphy = sas_expander_alloc(port->port,
275                                           SAS_EDGE_EXPANDER_DEVICE);
276                 break;
277         case FANOUT_DEV:
278                 rphy = sas_expander_alloc(port->port,
279                                           SAS_FANOUT_EXPANDER_DEVICE);
280                 break;
281         default:
282                 printk("ERROR: Unidentified device type %d\n", dev->dev_type);
283                 rphy = NULL;
284                 break;
285         }
286
287         if (!rphy) {
288                 kfree(dev);
289                 return -ENODEV;
290         }
291         rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
292         memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
293         sas_fill_in_rphy(dev, rphy);
294         sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
295         port->port_dev = dev;
296         dev->port = port;
297         dev->linkrate = port->linkrate;
298         dev->min_linkrate = port->linkrate;
299         dev->max_linkrate = port->linkrate;
300         dev->pathways = port->num_phys;
301         memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
302         memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
303         memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
304         port->disc.max_level = 0;
305
306         dev->rphy = rphy;
307         spin_lock_irq(&port->dev_list_lock);
308         list_add_tail(&dev->dev_list_node, &port->dev_list);
309         spin_unlock_irq(&port->dev_list_lock);
310
311         return 0;
312 }
313
314 /* ---------- Discover and Revalidate ---------- */
315
316 /* ---------- SATA ---------- */
317
318 static void sas_get_ata_command_set(struct domain_device *dev)
319 {
320         struct dev_to_host_fis *fis =
321                 (struct dev_to_host_fis *) dev->frame_rcvd;
322
323         if ((fis->sector_count == 1 && /* ATA */
324              fis->lbal         == 1 &&
325              fis->lbam         == 0 &&
326              fis->lbah         == 0 &&
327              fis->device       == 0)
328             ||
329             (fis->sector_count == 0 && /* CE-ATA (mATA) */
330              fis->lbal         == 0 &&
331              fis->lbam         == 0xCE &&
332              fis->lbah         == 0xAA &&
333              (fis->device & ~0x10) == 0))
334
335                 dev->sata_dev.command_set = ATA_COMMAND_SET;
336
337         else if ((fis->interrupt_reason == 1 && /* ATAPI */
338                   fis->lbal             == 1 &&
339                   fis->byte_count_low   == 0x14 &&
340                   fis->byte_count_high  == 0xEB &&
341                   (fis->device & ~0x10) == 0))
342
343                 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
344
345         else if ((fis->sector_count == 1 && /* SEMB */
346                   fis->lbal         == 1 &&
347                   fis->lbam         == 0x3C &&
348                   fis->lbah         == 0xC3 &&
349                   fis->device       == 0)
350                 ||
351                  (fis->interrupt_reason == 1 && /* SATA PM */
352                   fis->lbal             == 1 &&
353                   fis->byte_count_low   == 0x69 &&
354                   fis->byte_count_high  == 0x96 &&
355                   (fis->device & ~0x10) == 0))
356
357                 /* Treat it as a superset? */
358                 dev->sata_dev.command_set = ATAPI_COMMAND_SET;
359 }
360
361 /**
362  * sas_issue_ata_cmd -- Basic SATA command processing for discovery
363  * @dev: the device to send the command to
364  * @command: the command register
365  * @features: the features register
366  * @buffer: pointer to buffer to do I/O
367  * @size: size of @buffer
368  * @pci_dma_dir: PCI_DMA_...
369  */
370 static int sas_issue_ata_cmd(struct domain_device *dev, u8 command,
371                              u8 features, void *buffer, int size,
372                              int pci_dma_dir)
373 {
374         int res = 0;
375         struct sas_task *task;
376         struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *)
377                 &dev->frame_rcvd[0];
378
379         res = -ENOMEM;
380         task = sas_alloc_task(GFP_KERNEL);
381         if (!task)
382                 goto out;
383
384         task->dev = dev;
385
386         task->ata_task.fis.fis_type = 0x27;
387         task->ata_task.fis.command = command;
388         task->ata_task.fis.features = features;
389         task->ata_task.fis.device = d2h_fis->device;
390         task->ata_task.retry_count = 1;
391
392         res = sas_execute_task(task, buffer, size, pci_dma_dir);
393
394         sas_free_task(task);
395 out:
396         return res;
397 }
398
399 static void sas_sata_propagate_sas_addr(struct domain_device *dev)
400 {
401         unsigned long flags;
402         struct asd_sas_port *port = dev->port;
403         struct asd_sas_phy  *phy;
404
405         BUG_ON(dev->parent);
406
407         memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
408         spin_lock_irqsave(&port->phy_list_lock, flags);
409         list_for_each_entry(phy, &port->phy_list, port_phy_el)
410                 memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE);
411         spin_unlock_irqrestore(&port->phy_list_lock, flags);
412 }
413
414 #define ATA_IDENTIFY_DEV         0xEC
415 #define ATA_IDENTIFY_PACKET_DEV  0xA1
416 #define ATA_SET_FEATURES         0xEF
417 #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07
418
419 /**
420  * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV)
421  * @dev: STP/SATA device of interest (ATA/ATAPI)
422  *
423  * The LLDD has already been notified of this device, so that we can
424  * send FISes to it.  Here we try to get IDENTIFY DEVICE or IDENTIFY
425  * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its
426  * performance for this device.
427  */
428 static int sas_discover_sata_dev(struct domain_device *dev)
429 {
430         int     res;
431         __le16  *identify_x;
432         u8      command;
433
434         identify_x = kzalloc(512, GFP_KERNEL);
435         if (!identify_x)
436                 return -ENOMEM;
437
438         if (dev->sata_dev.command_set == ATA_COMMAND_SET) {
439                 dev->sata_dev.identify_device = identify_x;
440                 command = ATA_IDENTIFY_DEV;
441         } else {
442                 dev->sata_dev.identify_packet_device = identify_x;
443                 command = ATA_IDENTIFY_PACKET_DEV;
444         }
445
446         res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
447                                 PCI_DMA_FROMDEVICE);
448         if (res)
449                 goto out_err;
450
451         /* lives on the media? */
452         if (le16_to_cpu(identify_x[0]) & 4) {
453                 /* incomplete response */
454                 SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to "
455                             "dev %llx\n", SAS_ADDR(dev->sas_addr));
456                 if (!le16_to_cpu(identify_x[83] & (1<<6)))
457                         goto cont1;
458                 res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES,
459                                         ATA_FEATURE_PUP_STBY_SPIN_UP,
460                                         NULL, 0, PCI_DMA_NONE);
461                 if (res)
462                         goto cont1;
463
464                 schedule_timeout_interruptible(5*HZ); /* More time? */
465                 res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512,
466                                         PCI_DMA_FROMDEVICE);
467                 if (res)
468                         goto out_err;
469         }
470 cont1:
471         /* Get WWN */
472         if (dev->port->oob_mode != SATA_OOB_MODE) {
473                 memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr,
474                        SAS_ADDR_SIZE);
475         } else if (dev->sata_dev.command_set == ATA_COMMAND_SET &&
476                    (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000)
477                    == 0x5000) {
478                 int i;
479
480                 for (i = 0; i < 4; i++) {
481                         dev->sas_addr[2*i] =
482              (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8;
483                         dev->sas_addr[2*i+1] =
484               le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF;
485                 }
486         }
487         sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
488         if (!dev->parent)
489                 sas_sata_propagate_sas_addr(dev);
490
491         /* XXX Hint: register this SATA device with SATL.
492            When this returns, dev->sata_dev->lu is alive and
493            present.
494         sas_satl_register_dev(dev);
495         */
496
497         sas_fill_in_rphy(dev, dev->rphy);
498
499         return 0;
500 out_err:
501         dev->sata_dev.identify_packet_device = NULL;
502         dev->sata_dev.identify_device = NULL;
503         kfree(identify_x);
504         return res;
505 }
506
507 static int sas_discover_sata_pm(struct domain_device *dev)
508 {
509         return -ENODEV;
510 }
511
512 int sas_notify_lldd_dev_found(struct domain_device *dev)
513 {
514         int res = 0;
515         struct sas_ha_struct *sas_ha = dev->port->ha;
516         struct Scsi_Host *shost = sas_ha->core.shost;
517         struct sas_internal *i = to_sas_internal(shost->transportt);
518
519         if (i->dft->lldd_dev_found) {
520                 res = i->dft->lldd_dev_found(dev);
521                 if (res) {
522                         printk("sas: driver on pcidev %s cannot handle "
523                                "device %llx, error:%d\n",
524                                pci_name(sas_ha->pcidev),
525                                SAS_ADDR(dev->sas_addr), res);
526                 }
527         }
528         return res;
529 }
530
531
532 void sas_notify_lldd_dev_gone(struct domain_device *dev)
533 {
534         struct sas_ha_struct *sas_ha = dev->port->ha;
535         struct Scsi_Host *shost = sas_ha->core.shost;
536         struct sas_internal *i = to_sas_internal(shost->transportt);
537
538         if (i->dft->lldd_dev_gone)
539                 i->dft->lldd_dev_gone(dev);
540 }
541
542 /* ---------- Common/dispatchers ---------- */
543
544 /**
545  * sas_discover_sata -- discover an STP/SATA domain device
546  * @dev: pointer to struct domain_device of interest
547  *
548  * First we notify the LLDD of this device, so we can send frames to
549  * it.  Then depending on the type of device we call the appropriate
550  * discover functions.  Once device discover is done, we notify the
551  * LLDD so that it can fine-tune its parameters for the device, by
552  * removing it and then adding it.  That is, the second time around,
553  * the driver would have certain fields, that it is looking at, set.
554  * Finally we initialize the kobj so that the device can be added to
555  * the system at registration time.  Devices directly attached to a HA
556  * port, have no parents.  All other devices do, and should have their
557  * "parent" pointer set appropriately before calling this function.
558  */
559 int sas_discover_sata(struct domain_device *dev)
560 {
561         int res;
562
563         sas_get_ata_command_set(dev);
564
565         res = sas_notify_lldd_dev_found(dev);
566         if (res)
567                 return res;
568
569         switch (dev->dev_type) {
570         case SATA_DEV:
571                 res = sas_discover_sata_dev(dev);
572                 break;
573         case SATA_PM:
574                 res = sas_discover_sata_pm(dev);
575                 break;
576         default:
577                 break;
578         }
579         sas_notify_lldd_dev_gone(dev);
580         if (!res) {
581                 sas_notify_lldd_dev_found(dev);
582                 res = sas_rphy_add(dev->rphy);
583         }
584
585         return res;
586 }
587
588 /**
589  * sas_discover_end_dev -- discover an end device (SSP, etc)
590  * @end: pointer to domain device of interest
591  *
592  * See comment in sas_discover_sata().
593  */
594 int sas_discover_end_dev(struct domain_device *dev)
595 {
596         int res;
597
598         res = sas_notify_lldd_dev_found(dev);
599         if (res)
600                 goto out_err2;
601
602         res = sas_rphy_add(dev->rphy);
603         if (res)
604                 goto out_err;
605
606         return 0;
607
608 out_err:
609         sas_notify_lldd_dev_gone(dev);
610 out_err2:
611         return res;
612 }
613
614 /* ---------- Device registration and unregistration ---------- */
615
616 static inline void sas_unregister_common_dev(struct domain_device *dev)
617 {
618         sas_notify_lldd_dev_gone(dev);
619         if (!dev->parent)
620                 dev->port->port_dev = NULL;
621         else
622                 list_del_init(&dev->siblings);
623         list_del_init(&dev->dev_list_node);
624 }
625
626 void sas_unregister_dev(struct domain_device *dev)
627 {
628         if (dev->rphy) {
629                 sas_remove_children(&dev->rphy->dev);
630                 sas_rphy_delete(dev->rphy);
631                 dev->rphy = NULL;
632         }
633         if (dev->dev_type == EDGE_DEV || dev->dev_type == FANOUT_DEV) {
634                 /* remove the phys and ports, everything else should be gone */
635                 kfree(dev->ex_dev.ex_phy);
636                 dev->ex_dev.ex_phy = NULL;
637         }
638         sas_unregister_common_dev(dev);
639 }
640
641 void sas_unregister_domain_devices(struct asd_sas_port *port)
642 {
643         struct domain_device *dev, *n;
644
645         list_for_each_entry_safe_reverse(dev,n,&port->dev_list,dev_list_node)
646                 sas_unregister_dev(dev);
647
648         port->port->rphy = NULL;
649
650 }
651
652 /* ---------- Discovery and Revalidation ---------- */
653
654 /**
655  * sas_discover_domain -- discover the domain
656  * @port: port to the domain of interest
657  *
658  * NOTE: this process _must_ quit (return) as soon as any connection
659  * errors are encountered.  Connection recovery is done elsewhere.
660  * Discover process only interrogates devices in order to discover the
661  * domain.
662  */
663 static void sas_discover_domain(struct work_struct *work)
664 {
665         struct domain_device *dev;
666         int error = 0;
667         struct sas_discovery_event *ev =
668                 container_of(work, struct sas_discovery_event, work);
669         struct asd_sas_port *port = ev->port;
670
671         sas_begin_event(DISCE_DISCOVER_DOMAIN, &port->disc.disc_event_lock,
672                         &port->disc.pending);
673
674         if (port->port_dev)
675                 return;
676
677         error = sas_get_port_device(port);
678         if (error)
679                 return;
680         dev = port->port_dev;
681
682         SAS_DPRINTK("DOING DISCOVERY on port %d, pid:%d\n", port->id,
683                     current->pid);
684
685         switch (dev->dev_type) {
686         case SAS_END_DEV:
687                 error = sas_discover_end_dev(dev);
688                 break;
689         case EDGE_DEV:
690         case FANOUT_DEV:
691                 error = sas_discover_root_expander(dev);
692                 break;
693         case SATA_DEV:
694         case SATA_PM:
695                 error = sas_discover_sata(dev);
696                 break;
697         default:
698                 SAS_DPRINTK("unhandled device %d\n", dev->dev_type);
699                 break;
700         }
701
702         if (error) {
703                 sas_rphy_free(dev->rphy);
704                 dev->rphy = NULL;
705
706                 spin_lock_irq(&port->dev_list_lock);
707                 list_del_init(&dev->dev_list_node);
708                 spin_unlock_irq(&port->dev_list_lock);
709
710                 kfree(dev); /* not kobject_register-ed yet */
711                 port->port_dev = NULL;
712         }
713
714         SAS_DPRINTK("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
715                     current->pid, error);
716 }
717
718 static void sas_revalidate_domain(struct work_struct *work)
719 {
720         int res = 0;
721         struct sas_discovery_event *ev =
722                 container_of(work, struct sas_discovery_event, work);
723         struct asd_sas_port *port = ev->port;
724
725         sas_begin_event(DISCE_REVALIDATE_DOMAIN, &port->disc.disc_event_lock,
726                         &port->disc.pending);
727
728         SAS_DPRINTK("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
729                     current->pid);
730         if (port->port_dev)
731                 res = sas_ex_revalidate_domain(port->port_dev);
732
733         SAS_DPRINTK("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
734                     port->id, current->pid, res);
735 }
736
737 /* ---------- Events ---------- */
738
739 int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
740 {
741         struct sas_discovery *disc;
742
743         if (!port)
744                 return 0;
745         disc = &port->disc;
746
747         BUG_ON(ev >= DISC_NUM_EVENTS);
748
749         sas_queue_event(ev, &disc->disc_event_lock, &disc->pending,
750                         &disc->disc_work[ev].work, port->ha);
751
752         return 0;
753 }
754
755 /**
756  * sas_init_disc -- initialize the discovery struct in the port
757  * @port: pointer to struct port
758  *
759  * Called when the ports are being initialized.
760  */
761 void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
762 {
763         int i;
764
765         static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
766                 [DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
767                 [DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
768         };
769
770         spin_lock_init(&disc->disc_event_lock);
771         disc->pending = 0;
772         for (i = 0; i < DISC_NUM_EVENTS; i++) {
773                 INIT_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
774                 disc->disc_work[i].port = port;
775         }
776 }