isci: replace isci_remote_device completion with event queue
[deliverable/linux.git] / drivers / scsi / isci / remote_device.c
1 /*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * BSD LICENSE
25 *
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 *
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
38 * distribution.
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56 #include "isci.h"
57 #include "scic_io_request.h"
58 #include "scic_remote_device.h"
59 #include "scic_phy.h"
60 #include "scic_port.h"
61 #include "port.h"
62 #include "remote_device.h"
63 #include "request.h"
64 #include "task.h"
65
66
67
68 /**
69 * isci_remote_device_deconstruct() - This function frees an isci_remote_device.
70 * @isci_host: This parameter specifies the isci host object.
71 * @isci_device: This parameter specifies the remote device to be freed.
72 *
73 */
74 static void isci_remote_device_deconstruct(
75 struct isci_host *isci_host,
76 struct isci_remote_device *isci_device)
77 {
78 dev_dbg(&isci_host->pdev->dev,
79 "%s: isci_device = %p\n", __func__, isci_device);
80
81 /* There should not be any outstanding io's. All paths to
82 * here should go through isci_remote_device_nuke_requests.
83 * If we hit this condition, we will need a way to complete
84 * io requests in process */
85 while (!list_empty(&isci_device->reqs_in_process)) {
86
87 dev_err(&isci_host->pdev->dev,
88 "%s: ** request list not empty! **\n", __func__);
89 BUG();
90 }
91
92 /* Remove all related references to this device and free
93 * the cache object.
94 */
95 scic_remote_device_destruct(to_sci_dev(isci_device));
96 isci_device->domain_dev->lldd_dev = NULL;
97 list_del(&isci_device->node);
98
99 clear_bit(IDEV_STOP_PENDING, &isci_device->flags);
100 clear_bit(IDEV_START_PENDING, &isci_device->flags);
101 wake_up(&isci_host->eventq);
102 complete(isci_device->cmp);
103 kmem_cache_free(isci_kmem_cache, isci_device);
104 }
105
106
107 /**
108 * isci_remote_device_construct() - This function calls the scic remote device
109 * construct and start functions, it waits on the remote device start
110 * completion.
111 * @port: This parameter specifies the isci port with the remote device.
112 * @isci_device: This parameter specifies the isci remote device
113 *
114 * status from the scic calls, the caller to this function should clean up
115 * resources as appropriate.
116 */
117 static enum sci_status isci_remote_device_construct(
118 struct isci_port *port,
119 struct isci_remote_device *isci_device)
120 {
121 enum sci_status status = SCI_SUCCESS;
122
123 /* let the core do it's common constuction. */
124 scic_remote_device_construct(port->sci_port_handle,
125 to_sci_dev(isci_device));
126
127 /* let the core do it's device specific constuction. */
128 if (isci_device->domain_dev->parent &&
129 (isci_device->domain_dev->parent->dev_type == EDGE_DEV)) {
130 int i;
131
132 /* struct smp_response_discover discover_response; */
133 struct discover_resp discover_response;
134 struct domain_device *parent =
135 isci_device->domain_dev->parent;
136
137 struct expander_device *parent_ex = &parent->ex_dev;
138
139 for (i = 0; i < parent_ex->num_phys; i++) {
140
141 struct ex_phy *phy = &parent_ex->ex_phy[i];
142
143 if ((phy->phy_state == PHY_VACANT) ||
144 (phy->phy_state == PHY_NOT_PRESENT))
145 continue;
146
147 if (SAS_ADDR(phy->attached_sas_addr)
148 == SAS_ADDR(isci_device->domain_dev->sas_addr)) {
149
150 discover_response.attached_dev_type
151 = phy->attached_dev_type;
152 discover_response.linkrate
153 = phy->linkrate;
154 discover_response.attached_sata_host
155 = phy->attached_sata_host;
156 discover_response.attached_sata_dev
157 = phy->attached_sata_dev;
158 discover_response.attached_sata_ps
159 = phy->attached_sata_ps;
160 discover_response.iproto
161 = phy->attached_iproto >> 1;
162 discover_response.tproto
163 = phy->attached_tproto >> 1;
164 memcpy(
165 discover_response.attached_sas_addr,
166 phy->attached_sas_addr,
167 SAS_ADDR_SIZE
168 );
169 discover_response.attached_phy_id
170 = phy->attached_phy_id;
171 discover_response.change_count
172 = phy->phy_change_count;
173 discover_response.routing_attr
174 = phy->routing_attr;
175 discover_response.hmin_linkrate
176 = phy->phy->minimum_linkrate_hw;
177 discover_response.hmax_linkrate
178 = phy->phy->maximum_linkrate_hw;
179 discover_response.pmin_linkrate
180 = phy->phy->minimum_linkrate;
181 discover_response.pmax_linkrate
182 = phy->phy->maximum_linkrate;
183 }
184 }
185
186
187 dev_dbg(&port->isci_host->pdev->dev,
188 "%s: parent->dev_type = EDGE_DEV\n",
189 __func__);
190
191 status = scic_remote_device_ea_construct(to_sci_dev(isci_device),
192 (struct smp_response_discover *)&discover_response);
193
194 } else
195 status = scic_remote_device_da_construct(to_sci_dev(isci_device));
196
197
198 if (status != SCI_SUCCESS) {
199 dev_dbg(&port->isci_host->pdev->dev,
200 "%s: scic_remote_device_da_construct failed - "
201 "isci_device = %p\n",
202 __func__,
203 isci_device);
204
205 return status;
206 }
207
208 sci_object_set_association(to_sci_dev(isci_device), isci_device);
209
210 BUG_ON(port->isci_host == NULL);
211
212 /* start the device. */
213 status = scic_remote_device_start(to_sci_dev(isci_device),
214 ISCI_REMOTE_DEVICE_START_TIMEOUT);
215
216 if (status != SCI_SUCCESS) {
217 dev_warn(&port->isci_host->pdev->dev,
218 "%s: scic_remote_device_start failed\n",
219 __func__);
220 return status;
221 }
222
223 return status;
224 }
225
226
227 /**
228 * isci_remote_device_nuke_requests() - This function terminates all requests
229 * for a given remote device.
230 * @isci_device: This parameter specifies the remote device
231 *
232 */
233 void isci_remote_device_nuke_requests(
234 struct isci_remote_device *isci_device)
235 {
236 DECLARE_COMPLETION_ONSTACK(aborted_task_completion);
237 struct isci_host *isci_host;
238
239 isci_host = isci_device->isci_port->isci_host;
240
241 dev_dbg(&isci_host->pdev->dev,
242 "%s: isci_device = %p\n", __func__, isci_device);
243
244 /* Cleanup all requests pending for this device. */
245 isci_terminate_pending_requests(isci_host, isci_device, terminating);
246
247 dev_dbg(&isci_host->pdev->dev,
248 "%s: isci_device = %p, done\n", __func__, isci_device);
249 }
250
251
252
253 /**
254 * This function builds the isci_remote_device when a libsas dev_found message
255 * is received.
256 * @isci_host: This parameter specifies the isci host object.
257 * @port: This parameter specifies the isci_port conected to this device.
258 *
259 * pointer to new isci_remote_device.
260 */
261 static struct isci_remote_device *
262 isci_remote_device_alloc(struct isci_host *isci_host, struct isci_port *port)
263 {
264 struct isci_remote_device *isci_device;
265
266 isci_device = kmem_cache_zalloc(isci_kmem_cache, GFP_KERNEL);
267
268 if (!isci_device) {
269 dev_warn(&isci_host->pdev->dev, "%s: failed\n", __func__);
270 return NULL;
271 }
272
273 INIT_LIST_HEAD(&isci_device->reqs_in_process);
274 INIT_LIST_HEAD(&isci_device->node);
275
276 spin_lock_init(&isci_device->state_lock);
277 isci_remote_device_change_state(isci_device, isci_freed);
278
279 return isci_device;
280
281 }
282
283 /**
284 * isci_remote_device_ready() - This function is called by the scic when the
285 * remote device is ready. We mark the isci device as ready and signal the
286 * waiting proccess.
287 * @idev: This parameter specifies the remote device
288 *
289 */
290 void isci_remote_device_ready(struct isci_remote_device *idev)
291 {
292 struct isci_host *ihost = idev->isci_port->isci_host;
293 unsigned long flags;
294
295 dev_dbg(&ihost->pdev->dev,
296 "%s: isci_device = %p\n", __func__, idev);
297
298 spin_lock_irqsave(&idev->isci_port->remote_device_lock, flags);
299 isci_remote_device_change_state(idev, isci_ready_for_io);
300 if (test_and_clear_bit(IDEV_START_PENDING, &idev->flags))
301 wake_up(&ihost->eventq);
302 spin_unlock_irqrestore(&idev->isci_port->remote_device_lock, flags);
303 }
304
305 /**
306 * isci_remote_device_not_ready() - This function is called by the scic when
307 * the remote device is not ready. We mark the isci device as ready (not
308 * "ready_for_io") and signal the waiting proccess.
309 * @isci_host: This parameter specifies the isci host object.
310 * @isci_device: This parameter specifies the remote device
311 *
312 */
313 void isci_remote_device_not_ready(
314 struct isci_remote_device *isci_device,
315 u32 reason_code)
316 {
317 dev_dbg(&isci_device->isci_port->isci_host->pdev->dev,
318 "%s: isci_device = %p\n", __func__, isci_device);
319
320 if (reason_code == SCIC_REMOTE_DEVICE_NOT_READY_STOP_REQUESTED)
321 isci_remote_device_change_state(isci_device, isci_stopping);
322 else
323 /* device ready is actually a "not ready for io" state. */
324 isci_remote_device_change_state(isci_device, isci_ready);
325 }
326
327 /**
328 * isci_remote_device_stop_complete() - This function is called by the scic
329 * when the remote device stop has completed. We mark the isci device as not
330 * ready and remove the isci remote device.
331 * @isci_host: This parameter specifies the isci host object.
332 * @isci_device: This parameter specifies the remote device.
333 * @status: This parameter specifies status of the completion.
334 *
335 */
336 void isci_remote_device_stop_complete(
337 struct isci_host *isci_host,
338 struct isci_remote_device *isci_device,
339 enum sci_status status)
340 {
341 dev_dbg(&isci_host->pdev->dev,
342 "%s: complete isci_device = %p, status = 0x%x\n",
343 __func__,
344 isci_device,
345 status);
346
347 isci_remote_device_change_state(isci_device, isci_stopped);
348
349 /* after stop, we can tear down resources. */
350 isci_remote_device_deconstruct(isci_host, isci_device);
351
352 }
353
354 /**
355 * isci_remote_device_start_complete() - This function is called by the scic
356 * when the remote device start has completed
357 * @isci_host: This parameter specifies the isci host object.
358 * @isci_device: This parameter specifies the remote device.
359 * @status: This parameter specifies status of the completion.
360 *
361 */
362 void isci_remote_device_start_complete(
363 struct isci_host *isci_host,
364 struct isci_remote_device *isci_device,
365 enum sci_status status)
366 {
367
368
369 }
370
371
372 /**
373 * isci_remote_device_stop() - This function is called internally to stop the
374 * remote device.
375 * @isci_host: This parameter specifies the isci host object.
376 * @isci_device: This parameter specifies the remote device.
377 *
378 * The status of the scic request to stop.
379 */
380 enum sci_status isci_remote_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
381 {
382 enum sci_status status;
383 unsigned long flags;
384 DECLARE_COMPLETION_ONSTACK(completion);
385
386 dev_dbg(&ihost->pdev->dev,
387 "%s: isci_device = %p\n", __func__, idev);
388
389 isci_remote_device_change_state(idev, isci_stopping);
390 set_bit(IDEV_STOP_PENDING, &idev->flags);
391 idev->cmp = &completion;
392
393 spin_lock_irqsave(&ihost->scic_lock, flags);
394
395 status = scic_remote_device_stop(to_sci_dev(idev), 50);
396
397 spin_unlock_irqrestore(&ihost->scic_lock, flags);
398
399 /* Wait for the stop complete callback. */
400 if (status == SCI_SUCCESS)
401 wait_for_device_stop(ihost, idev);
402
403 dev_dbg(&ihost->pdev->dev,
404 "%s: idev = %p - after completion wait\n",
405 __func__, idev);
406
407 return status;
408 }
409
410 /**
411 * isci_remote_device_gone() - This function is called by libsas when a domain
412 * device is removed.
413 * @domain_device: This parameter specifies the libsas domain device.
414 *
415 */
416 void isci_remote_device_gone(struct domain_device *dev)
417 {
418 struct isci_host *ihost = dev->port->ha->lldd_ha;
419 struct isci_remote_device *idev = dev->lldd_dev;
420
421 dev_dbg(&ihost->pdev->dev,
422 "%s: domain_device = %p, isci_device = %p, isci_port = %p\n",
423 __func__, dev, idev, idev->isci_port);
424
425 isci_remote_device_stop(ihost, idev);
426 }
427
428
429 /**
430 * isci_remote_device_found() - This function is called by libsas when a remote
431 * device is discovered. A remote device object is created and started. the
432 * function then sleeps until the sci core device started message is
433 * received.
434 * @domain_device: This parameter specifies the libsas domain device.
435 *
436 * status, zero indicates success.
437 */
438 int isci_remote_device_found(struct domain_device *domain_dev)
439 {
440 unsigned long flags;
441 struct isci_host *isci_host;
442 struct isci_port *isci_port;
443 struct isci_phy *isci_phy;
444 struct asd_sas_port *sas_port;
445 struct asd_sas_phy *sas_phy;
446 struct isci_remote_device *isci_device;
447 enum sci_status status;
448
449 isci_host = isci_host_from_sas_ha(domain_dev->port->ha);
450
451 dev_dbg(&isci_host->pdev->dev,
452 "%s: domain_device = %p\n", __func__, domain_dev);
453
454 wait_for_start(isci_host);
455
456 sas_port = domain_dev->port;
457 sas_phy = list_first_entry(&sas_port->phy_list, struct asd_sas_phy,
458 port_phy_el);
459 isci_phy = to_isci_phy(sas_phy);
460 isci_port = isci_phy->isci_port;
461
462 /* we are being called for a device on this port,
463 * so it has to come up eventually
464 */
465 wait_for_completion(&isci_port->start_complete);
466
467 if ((isci_stopping == isci_port_get_state(isci_port)) ||
468 (isci_stopped == isci_port_get_state(isci_port)))
469 return -ENODEV;
470
471 isci_device = isci_remote_device_alloc(isci_host, isci_port);
472
473 INIT_LIST_HEAD(&isci_device->node);
474 domain_dev->lldd_dev = isci_device;
475 isci_device->domain_dev = domain_dev;
476 isci_device->isci_port = isci_port;
477 isci_remote_device_change_state(isci_device, isci_starting);
478
479
480 spin_lock_irqsave(&isci_port->remote_device_lock, flags);
481 list_add_tail(&isci_device->node, &isci_port->remote_dev_list);
482
483 set_bit(IDEV_START_PENDING, &isci_device->flags);
484 status = isci_remote_device_construct(isci_port, isci_device);
485 spin_unlock_irqrestore(&isci_port->remote_device_lock, flags);
486
487 dev_dbg(&isci_host->pdev->dev,
488 "%s: isci_device = %p\n",
489 __func__, isci_device);
490
491 if (status != SCI_SUCCESS) {
492
493 spin_lock_irqsave(&isci_port->remote_device_lock, flags);
494 isci_remote_device_deconstruct(
495 isci_host,
496 isci_device
497 );
498 spin_unlock_irqrestore(&isci_port->remote_device_lock, flags);
499 return -ENODEV;
500 }
501
502 /* wait for the device ready callback. */
503 wait_for_device_start(isci_host, isci_device);
504
505 return 0;
506 }
507 /**
508 * isci_device_is_reset_pending() - This function will check if there is any
509 * pending reset condition on the device.
510 * @request: This parameter is the isci_device object.
511 *
512 * true if there is a reset pending for the device.
513 */
514 bool isci_device_is_reset_pending(
515 struct isci_host *isci_host,
516 struct isci_remote_device *isci_device)
517 {
518 struct isci_request *isci_request;
519 struct isci_request *tmp_req;
520 bool reset_is_pending = false;
521 unsigned long flags;
522
523 dev_dbg(&isci_host->pdev->dev,
524 "%s: isci_device = %p\n", __func__, isci_device);
525
526 spin_lock_irqsave(&isci_host->scic_lock, flags);
527
528 /* Check for reset on all pending requests. */
529 list_for_each_entry_safe(isci_request, tmp_req,
530 &isci_device->reqs_in_process, dev_node) {
531 dev_dbg(&isci_host->pdev->dev,
532 "%s: isci_device = %p request = %p\n",
533 __func__, isci_device, isci_request);
534
535 if (isci_request->ttype == io_task) {
536
537 unsigned long flags;
538 struct sas_task *task = isci_request_access_task(
539 isci_request);
540
541 spin_lock_irqsave(&task->task_state_lock, flags);
542 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
543 reset_is_pending = true;
544 spin_unlock_irqrestore(&task->task_state_lock, flags);
545 }
546 }
547
548 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
549
550 dev_dbg(&isci_host->pdev->dev,
551 "%s: isci_device = %p reset_is_pending = %d\n",
552 __func__, isci_device, reset_is_pending);
553
554 return reset_is_pending;
555 }
556
557 /**
558 * isci_device_clear_reset_pending() - This function will clear if any pending
559 * reset condition flags on the device.
560 * @request: This parameter is the isci_device object.
561 *
562 * true if there is a reset pending for the device.
563 */
564 void isci_device_clear_reset_pending(struct isci_remote_device *isci_device)
565 {
566 struct isci_request *isci_request;
567 struct isci_request *tmp_req;
568 struct isci_host *isci_host = NULL;
569 unsigned long flags = 0;
570
571 /* FIXME more port gone confusion, and this time it makes the
572 * locking "fun"
573 */
574 if (isci_device->isci_port != NULL)
575 isci_host = isci_device->isci_port->isci_host;
576
577 /*
578 * FIXME when the isci_host gets sorted out
579 * use dev_dbg()
580 */
581 pr_debug("%s: isci_device=%p, isci_host=%p\n",
582 __func__, isci_device, isci_host);
583
584 if (isci_host != NULL)
585 spin_lock_irqsave(&isci_host->scic_lock, flags);
586 else
587 pr_err("%s: isci_device %p; isci_host == NULL!\n",
588 __func__, isci_device);
589
590 /* Clear reset pending on all pending requests. */
591 list_for_each_entry_safe(isci_request, tmp_req,
592 &isci_device->reqs_in_process, dev_node) {
593 /*
594 * FIXME when the conditional spinlock is gone
595 * change to dev_dbg()
596 */
597 pr_debug("%s: isci_device = %p request = %p\n",
598 __func__, isci_device, isci_request);
599
600 if (isci_request->ttype == io_task) {
601
602 unsigned long flags2;
603 struct sas_task *task = isci_request_access_task(
604 isci_request);
605
606 spin_lock_irqsave(&task->task_state_lock, flags2);
607 task->task_state_flags &= ~SAS_TASK_NEED_DEV_RESET;
608 spin_unlock_irqrestore(&task->task_state_lock, flags2);
609 }
610 }
611
612 if (isci_host != NULL)
613 spin_unlock_irqrestore(&isci_host->scic_lock, flags);
614 }
615
616 /**
617 * isci_remote_device_change_state() - This function gets the status of the
618 * remote_device object.
619 * @isci_device: This parameter points to the isci_remote_device object
620 *
621 * status of the object as a isci_status enum.
622 */
623 void isci_remote_device_change_state(
624 struct isci_remote_device *isci_device,
625 enum isci_status status)
626 {
627 unsigned long flags;
628
629 spin_lock_irqsave(&isci_device->state_lock, flags);
630 isci_device->status = status;
631 spin_unlock_irqrestore(&isci_device->state_lock, flags);
632 }
This page took 0.084918 seconds and 6 git commands to generate.