]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/mpt2sas/mpt2sas_scsih.c
tree-wide: fix assorted typos all over the place
[net-next-2.6.git] / drivers / scsi / mpt2sas / mpt2sas_scsih.c
CommitLineData
635374e7
EM
1/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
19d3ebe3 5 * Copyright (C) 2007-2009 LSI Corporation
635374e7
EM
6 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
44#include <linux/version.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/interrupt.h>
55
56#include "mpt2sas_base.h"
57
58MODULE_AUTHOR(MPT2SAS_AUTHOR);
59MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60MODULE_LICENSE("GPL");
61MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
62
63#define RAID_CHANNEL 1
64
65/* forward proto's */
66static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67 struct _sas_node *sas_expander);
68static void _firmware_event_work(struct work_struct *work);
69
70/* global parameters */
ba33fadf 71LIST_HEAD(mpt2sas_ioc_list);
635374e7
EM
72
73/* local parameters */
635374e7
EM
74static u8 scsi_io_cb_idx = -1;
75static u8 tm_cb_idx = -1;
76static u8 ctl_cb_idx = -1;
77static u8 base_cb_idx = -1;
78static u8 transport_cb_idx = -1;
79static u8 config_cb_idx = -1;
80static int mpt_ids;
81
77e63ed4
KD
82static u8 tm_tr_cb_idx = -1 ;
83static u8 tm_sas_control_cb_idx = -1;
84
635374e7 85/* command line options */
ba33fadf 86static u32 logging_level;
635374e7
EM
87MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
88 "(default=0)");
89
90/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
91#define MPT2SAS_MAX_LUN (16895)
92static int max_lun = MPT2SAS_MAX_LUN;
93module_param(max_lun, int, 0);
94MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
95
96/**
97 * struct sense_info - common structure for obtaining sense keys
98 * @skey: sense key
99 * @asc: additional sense code
100 * @ascq: additional sense code qualifier
101 */
102struct sense_info {
103 u8 skey;
104 u8 asc;
105 u8 ascq;
106};
107
108
635374e7
EM
109/**
110 * struct fw_event_work - firmware event struct
111 * @list: link list framework
112 * @work: work object (ioc->fault_reset_work_q)
113 * @ioc: per adapter object
114 * @VF_ID: virtual function id
7b936b02 115 * @VP_ID: virtual port id
635374e7
EM
116 * @host_reset_handling: handling events during host reset
117 * @ignore: flag meaning this event has been marked to ignore
118 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
119 * @event_data: reply event data payload follows
120 *
121 * This object stored on ioc->fw_event_list.
122 */
123struct fw_event_work {
124 struct list_head list;
6f92a7a0 125 struct work_struct work;
635374e7
EM
126 struct MPT2SAS_ADAPTER *ioc;
127 u8 VF_ID;
7b936b02 128 u8 VP_ID;
635374e7
EM
129 u8 host_reset_handling;
130 u8 ignore;
131 u16 event;
132 void *event_data;
133};
134
135/**
136 * struct _scsi_io_transfer - scsi io transfer
137 * @handle: sas device handle (assigned by firmware)
138 * @is_raid: flag set for hidden raid components
139 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
140 * @data_length: data transfer length
141 * @data_dma: dma pointer to data
142 * @sense: sense data
143 * @lun: lun number
144 * @cdb_length: cdb length
145 * @cdb: cdb contents
635374e7 146 * @timeout: timeout for this command
7b936b02
KD
147 * @VF_ID: virtual function id
148 * @VP_ID: virtual port id
149 * @valid_reply: flag set for reply message
635374e7
EM
150 * @sense_length: sense length
151 * @ioc_status: ioc status
152 * @scsi_state: scsi state
153 * @scsi_status: scsi staus
154 * @log_info: log information
155 * @transfer_length: data length transfer when there is a reply message
156 *
157 * Used for sending internal scsi commands to devices within this module.
158 * Refer to _scsi_send_scsi_io().
159 */
160struct _scsi_io_transfer {
161 u16 handle;
162 u8 is_raid;
163 enum dma_data_direction dir;
164 u32 data_length;
165 dma_addr_t data_dma;
166 u8 sense[SCSI_SENSE_BUFFERSIZE];
167 u32 lun;
168 u8 cdb_length;
169 u8 cdb[32];
170 u8 timeout;
7b936b02
KD
171 u8 VF_ID;
172 u8 VP_ID;
635374e7
EM
173 u8 valid_reply;
174 /* the following bits are only valid when 'valid_reply = 1' */
175 u32 sense_length;
176 u16 ioc_status;
177 u8 scsi_state;
178 u8 scsi_status;
179 u32 log_info;
180 u32 transfer_length;
181};
182
183/*
184 * The pci device ids are defined in mpi/mpi2_cnfg.h.
185 */
186static struct pci_device_id scsih_pci_table[] = {
187 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
188 PCI_ANY_ID, PCI_ANY_ID },
189 /* Falcon ~ 2008*/
190 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
191 PCI_ANY_ID, PCI_ANY_ID },
192 /* Liberator ~ 2108 */
193 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
194 PCI_ANY_ID, PCI_ANY_ID },
195 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
196 PCI_ANY_ID, PCI_ANY_ID },
197 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
198 PCI_ANY_ID, PCI_ANY_ID },
199 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
200 PCI_ANY_ID, PCI_ANY_ID },
201 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
202 PCI_ANY_ID, PCI_ANY_ID },
203 {0} /* Terminating entry */
204};
205MODULE_DEVICE_TABLE(pci, scsih_pci_table);
206
207/**
d5d135b3 208 * _scsih_set_debug_level - global setting of ioc->logging_level.
635374e7
EM
209 *
210 * Note: The logging levels are defined in mpt2sas_debug.h.
211 */
212static int
d5d135b3 213_scsih_set_debug_level(const char *val, struct kernel_param *kp)
635374e7
EM
214{
215 int ret = param_set_int(val, kp);
216 struct MPT2SAS_ADAPTER *ioc;
217
218 if (ret)
219 return ret;
220
221 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
ba33fadf 222 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
635374e7
EM
223 ioc->logging_level = logging_level;
224 return 0;
225}
d5d135b3 226module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
635374e7
EM
227 &logging_level, 0644);
228
229/**
230 * _scsih_srch_boot_sas_address - search based on sas_address
231 * @sas_address: sas address
232 * @boot_device: boot device object from bios page 2
233 *
234 * Returns 1 when there's a match, 0 means no match.
235 */
236static inline int
237_scsih_srch_boot_sas_address(u64 sas_address,
238 Mpi2BootDeviceSasWwid_t *boot_device)
239{
240 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
241}
242
243/**
244 * _scsih_srch_boot_device_name - search based on device name
245 * @device_name: device name specified in INDENTIFY fram
246 * @boot_device: boot device object from bios page 2
247 *
248 * Returns 1 when there's a match, 0 means no match.
249 */
250static inline int
251_scsih_srch_boot_device_name(u64 device_name,
252 Mpi2BootDeviceDeviceName_t *boot_device)
253{
254 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
255}
256
257/**
258 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
259 * @enclosure_logical_id: enclosure logical id
260 * @slot_number: slot number
261 * @boot_device: boot device object from bios page 2
262 *
263 * Returns 1 when there's a match, 0 means no match.
264 */
265static inline int
266_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
267 Mpi2BootDeviceEnclosureSlot_t *boot_device)
268{
269 return (enclosure_logical_id == le64_to_cpu(boot_device->
270 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
271 SlotNumber)) ? 1 : 0;
272}
273
274/**
275 * _scsih_is_boot_device - search for matching boot device.
276 * @sas_address: sas address
277 * @device_name: device name specified in INDENTIFY fram
278 * @enclosure_logical_id: enclosure logical id
279 * @slot_number: slot number
280 * @form: specifies boot device form
281 * @boot_device: boot device object from bios page 2
282 *
283 * Returns 1 when there's a match, 0 means no match.
284 */
285static int
286_scsih_is_boot_device(u64 sas_address, u64 device_name,
287 u64 enclosure_logical_id, u16 slot, u8 form,
288 Mpi2BiosPage2BootDevice_t *boot_device)
289{
290 int rc = 0;
291
292 switch (form) {
293 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
294 if (!sas_address)
295 break;
296 rc = _scsih_srch_boot_sas_address(
297 sas_address, &boot_device->SasWwid);
298 break;
299 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
300 if (!enclosure_logical_id)
301 break;
302 rc = _scsih_srch_boot_encl_slot(
303 enclosure_logical_id,
304 slot, &boot_device->EnclosureSlot);
305 break;
306 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
307 if (!device_name)
308 break;
309 rc = _scsih_srch_boot_device_name(
310 device_name, &boot_device->DeviceName);
311 break;
312 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
313 break;
314 }
315
316 return rc;
317}
318
319/**
320 * _scsih_determine_boot_device - determine boot device.
321 * @ioc: per adapter object
322 * @device: either sas_device or raid_device object
323 * @is_raid: [flag] 1 = raid object, 0 = sas object
324 *
325 * Determines whether this device should be first reported device to
326 * to scsi-ml or sas transport, this purpose is for persistant boot device.
327 * There are primary, alternate, and current entries in bios page 2. The order
328 * priority is primary, alternate, then current. This routine saves
329 * the corresponding device object and is_raid flag in the ioc object.
330 * The saved data to be used later in _scsih_probe_boot_devices().
331 */
332static void
333_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
334 void *device, u8 is_raid)
335{
336 struct _sas_device *sas_device;
337 struct _raid_device *raid_device;
338 u64 sas_address;
339 u64 device_name;
340 u64 enclosure_logical_id;
341 u16 slot;
342
343 /* only process this function when driver loads */
344 if (!ioc->wait_for_port_enable_to_complete)
345 return;
346
347 if (!is_raid) {
348 sas_device = device;
349 sas_address = sas_device->sas_address;
350 device_name = sas_device->device_name;
351 enclosure_logical_id = sas_device->enclosure_logical_id;
352 slot = sas_device->slot;
353 } else {
354 raid_device = device;
355 sas_address = raid_device->wwid;
356 device_name = 0;
357 enclosure_logical_id = 0;
358 slot = 0;
359 }
360
361 if (!ioc->req_boot_device.device) {
362 if (_scsih_is_boot_device(sas_address, device_name,
363 enclosure_logical_id, slot,
364 (ioc->bios_pg2.ReqBootDeviceForm &
365 MPI2_BIOSPAGE2_FORM_MASK),
366 &ioc->bios_pg2.RequestedBootDevice)) {
367 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
368 "%s: req_boot_device(0x%016llx)\n",
369 ioc->name, __func__,
370 (unsigned long long)sas_address));
371 ioc->req_boot_device.device = device;
372 ioc->req_boot_device.is_raid = is_raid;
373 }
374 }
375
376 if (!ioc->req_alt_boot_device.device) {
377 if (_scsih_is_boot_device(sas_address, device_name,
378 enclosure_logical_id, slot,
379 (ioc->bios_pg2.ReqAltBootDeviceForm &
380 MPI2_BIOSPAGE2_FORM_MASK),
381 &ioc->bios_pg2.RequestedAltBootDevice)) {
382 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
383 "%s: req_alt_boot_device(0x%016llx)\n",
384 ioc->name, __func__,
385 (unsigned long long)sas_address));
386 ioc->req_alt_boot_device.device = device;
387 ioc->req_alt_boot_device.is_raid = is_raid;
388 }
389 }
390
391 if (!ioc->current_boot_device.device) {
392 if (_scsih_is_boot_device(sas_address, device_name,
393 enclosure_logical_id, slot,
394 (ioc->bios_pg2.CurrentBootDeviceForm &
395 MPI2_BIOSPAGE2_FORM_MASK),
396 &ioc->bios_pg2.CurrentBootDevice)) {
397 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
398 "%s: current_boot_device(0x%016llx)\n",
399 ioc->name, __func__,
400 (unsigned long long)sas_address));
401 ioc->current_boot_device.device = device;
402 ioc->current_boot_device.is_raid = is_raid;
403 }
404 }
405}
406
407/**
408 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
409 * @ioc: per adapter object
410 * @sas_address: sas address
411 * Context: Calling function should acquire ioc->sas_device_lock
412 *
413 * This searches for sas_device based on sas_address, then return sas_device
414 * object.
415 */
416struct _sas_device *
417mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
418 u64 sas_address)
419{
420 struct _sas_device *sas_device, *r;
421
422 r = NULL;
423 /* check the sas_device_init_list */
424 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
425 list) {
426 if (sas_device->sas_address != sas_address)
427 continue;
428 r = sas_device;
429 goto out;
430 }
431
432 /* then check the sas_device_list */
433 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
434 if (sas_device->sas_address != sas_address)
435 continue;
436 r = sas_device;
437 goto out;
438 }
439 out:
440 return r;
441}
442
443/**
444 * _scsih_sas_device_find_by_handle - sas device search
445 * @ioc: per adapter object
446 * @handle: sas device handle (assigned by firmware)
447 * Context: Calling function should acquire ioc->sas_device_lock
448 *
449 * This searches for sas_device based on sas_address, then return sas_device
450 * object.
451 */
452static struct _sas_device *
453_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
454{
455 struct _sas_device *sas_device, *r;
456
457 r = NULL;
458 if (ioc->wait_for_port_enable_to_complete) {
459 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
460 list) {
461 if (sas_device->handle != handle)
462 continue;
463 r = sas_device;
464 goto out;
465 }
466 } else {
467 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
468 if (sas_device->handle != handle)
469 continue;
470 r = sas_device;
471 goto out;
472 }
473 }
474
475 out:
476 return r;
477}
478
479/**
480 * _scsih_sas_device_remove - remove sas_device from list.
481 * @ioc: per adapter object
482 * @sas_device: the sas_device object
483 * Context: This function will acquire ioc->sas_device_lock.
484 *
485 * Removing object and freeing associated memory from the ioc->sas_device_list.
486 */
487static void
488_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
489 struct _sas_device *sas_device)
490{
491 unsigned long flags;
492
493 spin_lock_irqsave(&ioc->sas_device_lock, flags);
494 list_del(&sas_device->list);
495 memset(sas_device, 0, sizeof(struct _sas_device));
496 kfree(sas_device);
497 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
498}
499
500/**
501 * _scsih_sas_device_add - insert sas_device to the list.
502 * @ioc: per adapter object
503 * @sas_device: the sas_device object
504 * Context: This function will acquire ioc->sas_device_lock.
505 *
506 * Adding new object to the ioc->sas_device_list.
507 */
508static void
509_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
510 struct _sas_device *sas_device)
511{
512 unsigned long flags;
513 u16 handle, parent_handle;
514 u64 sas_address;
515
516 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
517 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
518 sas_device->handle, (unsigned long long)sas_device->sas_address));
519
520 spin_lock_irqsave(&ioc->sas_device_lock, flags);
521 list_add_tail(&sas_device->list, &ioc->sas_device_list);
522 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
523
524 handle = sas_device->handle;
525 parent_handle = sas_device->parent_handle;
526 sas_address = sas_device->sas_address;
8901cbb4 527 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle))
635374e7 528 _scsih_sas_device_remove(ioc, sas_device);
635374e7
EM
529}
530
531/**
532 * _scsih_sas_device_init_add - insert sas_device to the list.
533 * @ioc: per adapter object
534 * @sas_device: the sas_device object
535 * Context: This function will acquire ioc->sas_device_lock.
536 *
537 * Adding new object at driver load time to the ioc->sas_device_init_list.
538 */
539static void
540_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
541 struct _sas_device *sas_device)
542{
543 unsigned long flags;
544
545 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
546 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
547 sas_device->handle, (unsigned long long)sas_device->sas_address));
548
549 spin_lock_irqsave(&ioc->sas_device_lock, flags);
550 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
551 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
552 _scsih_determine_boot_device(ioc, sas_device, 0);
553}
554
555/**
556 * mpt2sas_scsih_expander_find_by_handle - expander device search
557 * @ioc: per adapter object
558 * @handle: expander handle (assigned by firmware)
559 * Context: Calling function should acquire ioc->sas_device_lock
560 *
561 * This searches for expander device based on handle, then returns the
562 * sas_node object.
563 */
564struct _sas_node *
565mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
566{
567 struct _sas_node *sas_expander, *r;
568
569 r = NULL;
570 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
571 if (sas_expander->handle != handle)
572 continue;
573 r = sas_expander;
574 goto out;
575 }
576 out:
577 return r;
578}
579
580/**
581 * _scsih_raid_device_find_by_id - raid device search
582 * @ioc: per adapter object
583 * @id: sas device target id
584 * @channel: sas device channel
585 * Context: Calling function should acquire ioc->raid_device_lock
586 *
587 * This searches for raid_device based on target id, then return raid_device
588 * object.
589 */
590static struct _raid_device *
591_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
592{
593 struct _raid_device *raid_device, *r;
594
595 r = NULL;
596 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
597 if (raid_device->id == id && raid_device->channel == channel) {
598 r = raid_device;
599 goto out;
600 }
601 }
602
603 out:
604 return r;
605}
606
607/**
608 * _scsih_raid_device_find_by_handle - raid device search
609 * @ioc: per adapter object
610 * @handle: sas device handle (assigned by firmware)
611 * Context: Calling function should acquire ioc->raid_device_lock
612 *
613 * This searches for raid_device based on handle, then return raid_device
614 * object.
615 */
616static struct _raid_device *
617_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
618{
619 struct _raid_device *raid_device, *r;
620
621 r = NULL;
622 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
623 if (raid_device->handle != handle)
624 continue;
625 r = raid_device;
626 goto out;
627 }
628
629 out:
630 return r;
631}
632
633/**
634 * _scsih_raid_device_find_by_wwid - raid device search
635 * @ioc: per adapter object
636 * @handle: sas device handle (assigned by firmware)
637 * Context: Calling function should acquire ioc->raid_device_lock
638 *
639 * This searches for raid_device based on wwid, then return raid_device
640 * object.
641 */
642static struct _raid_device *
643_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
644{
645 struct _raid_device *raid_device, *r;
646
647 r = NULL;
648 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
649 if (raid_device->wwid != wwid)
650 continue;
651 r = raid_device;
652 goto out;
653 }
654
655 out:
656 return r;
657}
658
659/**
660 * _scsih_raid_device_add - add raid_device object
661 * @ioc: per adapter object
662 * @raid_device: raid_device object
663 *
664 * This is added to the raid_device_list link list.
665 */
666static void
667_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
668 struct _raid_device *raid_device)
669{
670 unsigned long flags;
671
672 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
673 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
674 raid_device->handle, (unsigned long long)raid_device->wwid));
675
676 spin_lock_irqsave(&ioc->raid_device_lock, flags);
677 list_add_tail(&raid_device->list, &ioc->raid_device_list);
678 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
679}
680
681/**
682 * _scsih_raid_device_remove - delete raid_device object
683 * @ioc: per adapter object
684 * @raid_device: raid_device object
685 *
686 * This is removed from the raid_device_list link list.
687 */
688static void
689_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
690 struct _raid_device *raid_device)
691{
692 unsigned long flags;
693
694 spin_lock_irqsave(&ioc->raid_device_lock, flags);
695 list_del(&raid_device->list);
696 memset(raid_device, 0, sizeof(struct _raid_device));
697 kfree(raid_device);
698 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
699}
700
701/**
702 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
703 * @ioc: per adapter object
704 * @sas_address: sas address
705 * Context: Calling function should acquire ioc->sas_node_lock.
706 *
707 * This searches for expander device based on sas_address, then returns the
708 * sas_node object.
709 */
710struct _sas_node *
711mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
712 u64 sas_address)
713{
714 struct _sas_node *sas_expander, *r;
715
716 r = NULL;
717 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
718 if (sas_expander->sas_address != sas_address)
719 continue;
720 r = sas_expander;
721 goto out;
722 }
723 out:
724 return r;
725}
726
727/**
728 * _scsih_expander_node_add - insert expander device to the list.
729 * @ioc: per adapter object
730 * @sas_expander: the sas_device object
731 * Context: This function will acquire ioc->sas_node_lock.
732 *
733 * Adding new object to the ioc->sas_expander_list.
734 *
735 * Return nothing.
736 */
737static void
738_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
739 struct _sas_node *sas_expander)
740{
741 unsigned long flags;
742
743 spin_lock_irqsave(&ioc->sas_node_lock, flags);
744 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
745 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
746}
747
748/**
749 * _scsih_is_end_device - determines if device is an end device
750 * @device_info: bitfield providing information about the device.
751 * Context: none
752 *
753 * Returns 1 if end device.
754 */
755static int
756_scsih_is_end_device(u32 device_info)
757{
758 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
759 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
760 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
761 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
762 return 1;
763 else
764 return 0;
765}
766
767/**
595bb0bd 768 * mptscsih_get_scsi_lookup - returns scmd entry
635374e7
EM
769 * @ioc: per adapter object
770 * @smid: system request message index
635374e7
EM
771 *
772 * Returns the smid stored scmd pointer.
773 */
774static struct scsi_cmnd *
775_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
776{
595bb0bd 777 return ioc->scsi_lookup[smid - 1].scmd;
635374e7
EM
778}
779
780/**
781 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
782 * @ioc: per adapter object
783 * @smid: system request message index
784 * @scmd: pointer to scsi command object
785 * Context: This function will acquire ioc->scsi_lookup_lock.
786 *
787 * This will search for a scmd pointer in the scsi_lookup array,
788 * returning the revelent smid. A returned value of zero means invalid.
789 */
790static u16
791_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
792 *scmd)
793{
794 u16 smid;
795 unsigned long flags;
796 int i;
797
798 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
799 smid = 0;
595bb0bd 800 for (i = 0; i < ioc->scsiio_depth; i++) {
635374e7 801 if (ioc->scsi_lookup[i].scmd == scmd) {
595bb0bd 802 smid = ioc->scsi_lookup[i].smid;
635374e7
EM
803 goto out;
804 }
805 }
806 out:
807 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
808 return smid;
809}
810
811/**
812 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
813 * @ioc: per adapter object
814 * @id: target id
815 * @channel: channel
816 * Context: This function will acquire ioc->scsi_lookup_lock.
817 *
818 * This will search for a matching channel:id in the scsi_lookup array,
819 * returning 1 if found.
820 */
821static u8
822_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
823 int channel)
824{
825 u8 found;
826 unsigned long flags;
827 int i;
828
829 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
830 found = 0;
595bb0bd 831 for (i = 0 ; i < ioc->scsiio_depth; i++) {
635374e7
EM
832 if (ioc->scsi_lookup[i].scmd &&
833 (ioc->scsi_lookup[i].scmd->device->id == id &&
834 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
835 found = 1;
836 goto out;
837 }
838 }
839 out:
840 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
841 return found;
842}
843
993e0da7
EM
844/**
845 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
846 * @ioc: per adapter object
847 * @id: target id
848 * @lun: lun number
849 * @channel: channel
850 * Context: This function will acquire ioc->scsi_lookup_lock.
851 *
852 * This will search for a matching channel:id:lun in the scsi_lookup array,
853 * returning 1 if found.
854 */
855static u8
856_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
857 unsigned int lun, int channel)
858{
859 u8 found;
860 unsigned long flags;
861 int i;
862
863 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
864 found = 0;
595bb0bd 865 for (i = 0 ; i < ioc->scsiio_depth; i++) {
993e0da7
EM
866 if (ioc->scsi_lookup[i].scmd &&
867 (ioc->scsi_lookup[i].scmd->device->id == id &&
868 ioc->scsi_lookup[i].scmd->device->channel == channel &&
869 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
870 found = 1;
871 goto out;
872 }
873 }
874 out:
875 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
876 return found;
877}
878
635374e7
EM
879/**
880 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
881 * @ioc: per adapter object
882 * @smid: system request message index
883 *
884 * Returns phys pointer to chain buffer.
885 */
886static dma_addr_t
887_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
888{
889 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
890 ioc->chains_needed_per_io));
891}
892
893/**
894 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
895 * @ioc: per adapter object
896 * @smid: system request message index
897 *
898 * Returns virt pointer to chain buffer.
899 */
900static void *
901_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
902{
903 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
904 ioc->chains_needed_per_io)));
905}
906
907/**
908 * _scsih_build_scatter_gather - main sg creation routine
909 * @ioc: per adapter object
910 * @scmd: scsi command
911 * @smid: system request message index
912 * Context: none.
913 *
914 * The main routine that builds scatter gather table from a given
915 * scsi request sent via the .queuecommand main handler.
916 *
917 * Returns 0 success, anything else error
918 */
919static int
920_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
921 struct scsi_cmnd *scmd, u16 smid)
922{
923 Mpi2SCSIIORequest_t *mpi_request;
924 dma_addr_t chain_dma;
925 struct scatterlist *sg_scmd;
926 void *sg_local, *chain;
927 u32 chain_offset;
928 u32 chain_length;
929 u32 chain_flags;
930 u32 sges_left;
931 u32 sges_in_segment;
932 u32 sgl_flags;
933 u32 sgl_flags_last_element;
934 u32 sgl_flags_end_buffer;
935
936 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
937
938 /* init scatter gather flags */
939 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
940 if (scmd->sc_data_direction == DMA_TO_DEVICE)
941 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
942 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
943 << MPI2_SGE_FLAGS_SHIFT;
944 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
945 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
946 << MPI2_SGE_FLAGS_SHIFT;
947 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
948
949 sg_scmd = scsi_sglist(scmd);
950 sges_left = scsi_dma_map(scmd);
951 if (!sges_left) {
952 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
953 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
954 return -ENOMEM;
955 }
956
957 sg_local = &mpi_request->SGL;
958 sges_in_segment = ioc->max_sges_in_main_message;
959 if (sges_left <= sges_in_segment)
960 goto fill_in_last_segment;
961
962 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
963 (sges_in_segment * ioc->sge_size))/4;
964
965 /* fill in main message segment when there is a chain following */
966 while (sges_in_segment) {
967 if (sges_in_segment == 1)
968 ioc->base_add_sg_single(sg_local,
969 sgl_flags_last_element | sg_dma_len(sg_scmd),
970 sg_dma_address(sg_scmd));
971 else
972 ioc->base_add_sg_single(sg_local, sgl_flags |
973 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
974 sg_scmd = sg_next(sg_scmd);
975 sg_local += ioc->sge_size;
976 sges_left--;
977 sges_in_segment--;
978 }
979
980 /* initializing the chain flags and pointers */
981 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
982 chain = _scsih_get_chain_buffer(ioc, smid);
983 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
984 do {
985 sges_in_segment = (sges_left <=
986 ioc->max_sges_in_chain_message) ? sges_left :
987 ioc->max_sges_in_chain_message;
988 chain_offset = (sges_left == sges_in_segment) ?
989 0 : (sges_in_segment * ioc->sge_size)/4;
990 chain_length = sges_in_segment * ioc->sge_size;
991 if (chain_offset) {
992 chain_offset = chain_offset <<
993 MPI2_SGE_CHAIN_OFFSET_SHIFT;
994 chain_length += ioc->sge_size;
995 }
996 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
997 chain_length, chain_dma);
998 sg_local = chain;
999 if (!chain_offset)
1000 goto fill_in_last_segment;
1001
1002 /* fill in chain segments */
1003 while (sges_in_segment) {
1004 if (sges_in_segment == 1)
1005 ioc->base_add_sg_single(sg_local,
1006 sgl_flags_last_element |
1007 sg_dma_len(sg_scmd),
1008 sg_dma_address(sg_scmd));
1009 else
1010 ioc->base_add_sg_single(sg_local, sgl_flags |
1011 sg_dma_len(sg_scmd),
1012 sg_dma_address(sg_scmd));
1013 sg_scmd = sg_next(sg_scmd);
1014 sg_local += ioc->sge_size;
1015 sges_left--;
1016 sges_in_segment--;
1017 }
1018
1019 chain_dma += ioc->request_sz;
1020 chain += ioc->request_sz;
1021 } while (1);
1022
1023
1024 fill_in_last_segment:
1025
1026 /* fill the last segment */
1027 while (sges_left) {
1028 if (sges_left == 1)
1029 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1030 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1031 else
1032 ioc->base_add_sg_single(sg_local, sgl_flags |
1033 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1034 sg_scmd = sg_next(sg_scmd);
1035 sg_local += ioc->sge_size;
1036 sges_left--;
1037 }
1038
1039 return 0;
1040}
1041
1042/**
d5d135b3 1043 * _scsih_change_queue_depth - setting device queue depth
635374e7
EM
1044 * @sdev: scsi device struct
1045 * @qdepth: requested queue depth
1046 *
1047 * Returns queue depth.
1048 */
1049static int
d5d135b3 1050_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
635374e7
EM
1051{
1052 struct Scsi_Host *shost = sdev->host;
1053 int max_depth;
1054 int tag_type;
1055
1056 max_depth = shost->can_queue;
1057 if (!sdev->tagged_supported)
1058 max_depth = 1;
1059 if (qdepth > max_depth)
1060 qdepth = max_depth;
1061 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1062 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1063
1064 if (sdev->inquiry_len > 7)
1065 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1066 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1067 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1068 sdev->ordered_tags, sdev->scsi_level,
1069 (sdev->inquiry[7] & 2) >> 1);
1070
1071 return sdev->queue_depth;
1072}
1073
1074/**
595bb0bd 1075 * _scsih_change_queue_type - changing device queue tag type
635374e7
EM
1076 * @sdev: scsi device struct
1077 * @tag_type: requested tag type
1078 *
1079 * Returns queue tag type.
1080 */
1081static int
d5d135b3 1082_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
635374e7
EM
1083{
1084 if (sdev->tagged_supported) {
1085 scsi_set_tag_type(sdev, tag_type);
1086 if (tag_type)
1087 scsi_activate_tcq(sdev, sdev->queue_depth);
1088 else
1089 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1090 } else
1091 tag_type = 0;
1092
1093 return tag_type;
1094}
1095
1096/**
d5d135b3 1097 * _scsih_target_alloc - target add routine
635374e7
EM
1098 * @starget: scsi target struct
1099 *
1100 * Returns 0 if ok. Any other return is assumed to be an error and
1101 * the device is ignored.
1102 */
1103static int
d5d135b3 1104_scsih_target_alloc(struct scsi_target *starget)
635374e7
EM
1105{
1106 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1107 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1108 struct MPT2SAS_TARGET *sas_target_priv_data;
1109 struct _sas_device *sas_device;
1110 struct _raid_device *raid_device;
1111 unsigned long flags;
1112 struct sas_rphy *rphy;
1113
1114 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1115 if (!sas_target_priv_data)
1116 return -ENOMEM;
1117
1118 starget->hostdata = sas_target_priv_data;
1119 sas_target_priv_data->starget = starget;
1120 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1121
1122 /* RAID volumes */
1123 if (starget->channel == RAID_CHANNEL) {
1124 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1125 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1126 starget->channel);
1127 if (raid_device) {
1128 sas_target_priv_data->handle = raid_device->handle;
1129 sas_target_priv_data->sas_address = raid_device->wwid;
1130 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1131 raid_device->starget = starget;
1132 }
1133 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1134 return 0;
1135 }
1136
1137 /* sas/sata devices */
1138 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1139 rphy = dev_to_rphy(starget->dev.parent);
1140 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1141 rphy->identify.sas_address);
1142
1143 if (sas_device) {
1144 sas_target_priv_data->handle = sas_device->handle;
1145 sas_target_priv_data->sas_address = sas_device->sas_address;
1146 sas_device->starget = starget;
1147 sas_device->id = starget->id;
1148 sas_device->channel = starget->channel;
1149 if (sas_device->hidden_raid_component)
1150 sas_target_priv_data->flags |=
1151 MPT_TARGET_FLAGS_RAID_COMPONENT;
1152 }
1153 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1154
1155 return 0;
1156}
1157
1158/**
d5d135b3 1159 * _scsih_target_destroy - target destroy routine
635374e7
EM
1160 * @starget: scsi target struct
1161 *
1162 * Returns nothing.
1163 */
1164static void
d5d135b3 1165_scsih_target_destroy(struct scsi_target *starget)
635374e7
EM
1166{
1167 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1168 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1169 struct MPT2SAS_TARGET *sas_target_priv_data;
1170 struct _sas_device *sas_device;
1171 struct _raid_device *raid_device;
1172 unsigned long flags;
1173 struct sas_rphy *rphy;
1174
1175 sas_target_priv_data = starget->hostdata;
1176 if (!sas_target_priv_data)
1177 return;
1178
1179 if (starget->channel == RAID_CHANNEL) {
1180 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1181 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1182 starget->channel);
1183 if (raid_device) {
1184 raid_device->starget = NULL;
1185 raid_device->sdev = NULL;
1186 }
1187 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1188 goto out;
1189 }
1190
1191 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1192 rphy = dev_to_rphy(starget->dev.parent);
1193 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1194 rphy->identify.sas_address);
8901cbb4
EM
1195 if (sas_device && (sas_device->starget == starget) &&
1196 (sas_device->id == starget->id) &&
1197 (sas_device->channel == starget->channel))
635374e7
EM
1198 sas_device->starget = NULL;
1199
1200 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1201
1202 out:
1203 kfree(sas_target_priv_data);
1204 starget->hostdata = NULL;
1205}
1206
1207/**
d5d135b3 1208 * _scsih_slave_alloc - device add routine
635374e7
EM
1209 * @sdev: scsi device struct
1210 *
1211 * Returns 0 if ok. Any other return is assumed to be an error and
1212 * the device is ignored.
1213 */
1214static int
d5d135b3 1215_scsih_slave_alloc(struct scsi_device *sdev)
635374e7
EM
1216{
1217 struct Scsi_Host *shost;
1218 struct MPT2SAS_ADAPTER *ioc;
1219 struct MPT2SAS_TARGET *sas_target_priv_data;
1220 struct MPT2SAS_DEVICE *sas_device_priv_data;
1221 struct scsi_target *starget;
1222 struct _raid_device *raid_device;
1223 struct _sas_device *sas_device;
1224 unsigned long flags;
1225
1226 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1227 if (!sas_device_priv_data)
1228 return -ENOMEM;
1229
1230 sas_device_priv_data->lun = sdev->lun;
1231 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1232
1233 starget = scsi_target(sdev);
1234 sas_target_priv_data = starget->hostdata;
1235 sas_target_priv_data->num_luns++;
1236 sas_device_priv_data->sas_target = sas_target_priv_data;
1237 sdev->hostdata = sas_device_priv_data;
1238 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1239 sdev->no_uld_attach = 1;
1240
1241 shost = dev_to_shost(&starget->dev);
1242 ioc = shost_priv(shost);
1243 if (starget->channel == RAID_CHANNEL) {
1244 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1245 raid_device = _scsih_raid_device_find_by_id(ioc,
1246 starget->id, starget->channel);
1247 if (raid_device)
1248 raid_device->sdev = sdev; /* raid is single lun */
1249 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1250 } else {
1251 /* set TLR bit for SSP devices */
1252 if (!(ioc->facts.IOCCapabilities &
1253 MPI2_IOCFACTS_CAPABILITY_TLR))
1254 goto out;
1255 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1256 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1257 sas_device_priv_data->sas_target->sas_address);
1258 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1259 if (sas_device && sas_device->device_info &
1260 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1261 sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1262 }
1263
1264 out:
1265 return 0;
1266}
1267
1268/**
d5d135b3 1269 * _scsih_slave_destroy - device destroy routine
635374e7
EM
1270 * @sdev: scsi device struct
1271 *
1272 * Returns nothing.
1273 */
1274static void
d5d135b3 1275_scsih_slave_destroy(struct scsi_device *sdev)
635374e7
EM
1276{
1277 struct MPT2SAS_TARGET *sas_target_priv_data;
1278 struct scsi_target *starget;
1279
1280 if (!sdev->hostdata)
1281 return;
1282
1283 starget = scsi_target(sdev);
1284 sas_target_priv_data = starget->hostdata;
1285 sas_target_priv_data->num_luns--;
1286 kfree(sdev->hostdata);
1287 sdev->hostdata = NULL;
1288}
1289
1290/**
d5d135b3 1291 * _scsih_display_sata_capabilities - sata capabilities
635374e7
EM
1292 * @ioc: per adapter object
1293 * @sas_device: the sas_device object
1294 * @sdev: scsi device struct
1295 */
1296static void
d5d135b3 1297_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
635374e7
EM
1298 struct _sas_device *sas_device, struct scsi_device *sdev)
1299{
1300 Mpi2ConfigReply_t mpi_reply;
1301 Mpi2SasDevicePage0_t sas_device_pg0;
1302 u32 ioc_status;
1303 u16 flags;
1304 u32 device_info;
1305
1306 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1307 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1308 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1309 ioc->name, __FILE__, __LINE__, __func__);
1310 return;
1311 }
1312
1313 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1314 MPI2_IOCSTATUS_MASK;
1315 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1316 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1317 ioc->name, __FILE__, __LINE__, __func__);
1318 return;
1319 }
1320
1321 flags = le16_to_cpu(sas_device_pg0.Flags);
1322 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1323
1324 sdev_printk(KERN_INFO, sdev,
1325 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1326 "sw_preserve(%s)\n",
1327 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1328 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1329 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1330 "n",
1331 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1332 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1333 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1334}
1335
1336/**
1337 * _scsih_get_volume_capabilities - volume capabilities
1338 * @ioc: per adapter object
1339 * @sas_device: the raid_device object
1340 */
1341static void
1342_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1343 struct _raid_device *raid_device)
1344{
1345 Mpi2RaidVolPage0_t *vol_pg0;
1346 Mpi2RaidPhysDiskPage0_t pd_pg0;
1347 Mpi2SasDevicePage0_t sas_device_pg0;
1348 Mpi2ConfigReply_t mpi_reply;
1349 u16 sz;
1350 u8 num_pds;
1351
1352 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1353 &num_pds)) || !num_pds) {
1354 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1355 ioc->name, __FILE__, __LINE__, __func__);
1356 return;
1357 }
1358
1359 raid_device->num_pds = num_pds;
1360 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1361 sizeof(Mpi2RaidVol0PhysDisk_t));
1362 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1363 if (!vol_pg0) {
1364 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1365 ioc->name, __FILE__, __LINE__, __func__);
1366 return;
1367 }
1368
1369 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1370 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1371 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1372 ioc->name, __FILE__, __LINE__, __func__);
1373 kfree(vol_pg0);
1374 return;
1375 }
1376
1377 raid_device->volume_type = vol_pg0->VolumeType;
1378
1379 /* figure out what the underlying devices are by
1380 * obtaining the device_info bits for the 1st device
1381 */
1382 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1383 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1384 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1385 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1386 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1387 le16_to_cpu(pd_pg0.DevHandle)))) {
1388 raid_device->device_info =
1389 le32_to_cpu(sas_device_pg0.DeviceInfo);
1390 }
1391 }
1392
1393 kfree(vol_pg0);
1394}
1395
1396/**
d5d135b3 1397 * _scsih_slave_configure - device configure routine.
635374e7
EM
1398 * @sdev: scsi device struct
1399 *
1400 * Returns 0 if ok. Any other return is assumed to be an error and
1401 * the device is ignored.
1402 */
1403static int
d5d135b3 1404_scsih_slave_configure(struct scsi_device *sdev)
635374e7
EM
1405{
1406 struct Scsi_Host *shost = sdev->host;
1407 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1408 struct MPT2SAS_DEVICE *sas_device_priv_data;
1409 struct MPT2SAS_TARGET *sas_target_priv_data;
1410 struct _sas_device *sas_device;
1411 struct _raid_device *raid_device;
1412 unsigned long flags;
1413 int qdepth;
1414 u8 ssp_target = 0;
1415 char *ds = "";
1416 char *r_level = "";
1417
1418 qdepth = 1;
1419 sas_device_priv_data = sdev->hostdata;
1420 sas_device_priv_data->configured_lun = 1;
1421 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1422 sas_target_priv_data = sas_device_priv_data->sas_target;
1423
1424 /* raid volume handling */
1425 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1426
1427 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1428 raid_device = _scsih_raid_device_find_by_handle(ioc,
1429 sas_target_priv_data->handle);
1430 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1431 if (!raid_device) {
1432 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1433 ioc->name, __FILE__, __LINE__, __func__);
1434 return 0;
1435 }
1436
1437 _scsih_get_volume_capabilities(ioc, raid_device);
1438
1439 /* RAID Queue Depth Support
1440 * IS volume = underlying qdepth of drive type, either
1441 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1442 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1443 */
1444 if (raid_device->device_info &
1445 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1446 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1447 ds = "SSP";
1448 } else {
1449 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1450 if (raid_device->device_info &
1451 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1452 ds = "SATA";
1453 else
1454 ds = "STP";
1455 }
1456
1457 switch (raid_device->volume_type) {
1458 case MPI2_RAID_VOL_TYPE_RAID0:
1459 r_level = "RAID0";
1460 break;
1461 case MPI2_RAID_VOL_TYPE_RAID1E:
1462 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
ed79f128
KD
1463 if (ioc->manu_pg10.OEMIdentifier &&
1464 (ioc->manu_pg10.GenericFlags0 &
1465 MFG10_GF0_R10_DISPLAY) &&
1466 !(raid_device->num_pds % 2))
1467 r_level = "RAID10";
1468 else
1469 r_level = "RAID1E";
635374e7
EM
1470 break;
1471 case MPI2_RAID_VOL_TYPE_RAID1:
1472 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1473 r_level = "RAID1";
1474 break;
1475 case MPI2_RAID_VOL_TYPE_RAID10:
1476 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1477 r_level = "RAID10";
1478 break;
1479 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1480 default:
1481 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1482 r_level = "RAIDX";
1483 break;
1484 }
1485
1486 sdev_printk(KERN_INFO, sdev, "%s: "
1487 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1488 r_level, raid_device->handle,
1489 (unsigned long long)raid_device->wwid,
1490 raid_device->num_pds, ds);
d5d135b3 1491 _scsih_change_queue_depth(sdev, qdepth);
635374e7
EM
1492 return 0;
1493 }
1494
1495 /* non-raid handling */
1496 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1497 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1498 sas_device_priv_data->sas_target->sas_address);
1499 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1500 if (sas_device) {
1501 if (sas_target_priv_data->flags &
1502 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1503 mpt2sas_config_get_volume_handle(ioc,
1504 sas_device->handle, &sas_device->volume_handle);
1505 mpt2sas_config_get_volume_wwid(ioc,
1506 sas_device->volume_handle,
1507 &sas_device->volume_wwid);
1508 }
1509 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1510 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1511 ssp_target = 1;
1512 ds = "SSP";
1513 } else {
1514 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1515 if (sas_device->device_info &
1516 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1517 ds = "STP";
1518 else if (sas_device->device_info &
1519 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1520 ds = "SATA";
1521 }
1522
1523 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1524 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1525 ds, sas_device->handle,
1526 (unsigned long long)sas_device->sas_address,
1527 (unsigned long long)sas_device->device_name);
1528 sdev_printk(KERN_INFO, sdev, "%s: "
1529 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1530 (unsigned long long) sas_device->enclosure_logical_id,
1531 sas_device->slot);
1532
1533 if (!ssp_target)
d5d135b3 1534 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
635374e7
EM
1535 }
1536
d5d135b3 1537 _scsih_change_queue_depth(sdev, qdepth);
635374e7
EM
1538
1539 if (ssp_target)
1540 sas_read_port_mode_page(sdev);
1541 return 0;
1542}
1543
1544/**
d5d135b3 1545 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
635374e7
EM
1546 * @sdev: scsi device struct
1547 * @bdev: pointer to block device context
1548 * @capacity: device size (in 512 byte sectors)
1549 * @params: three element array to place output:
1550 * params[0] number of heads (max 255)
1551 * params[1] number of sectors (max 63)
1552 * params[2] number of cylinders
1553 *
1554 * Return nothing.
1555 */
1556static int
d5d135b3 1557_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
635374e7
EM
1558 sector_t capacity, int params[])
1559{
1560 int heads;
1561 int sectors;
1562 sector_t cylinders;
1563 ulong dummy;
1564
1565 heads = 64;
1566 sectors = 32;
1567
1568 dummy = heads * sectors;
1569 cylinders = capacity;
1570 sector_div(cylinders, dummy);
1571
1572 /*
1573 * Handle extended translation size for logical drives
1574 * > 1Gb
1575 */
1576 if ((ulong)capacity >= 0x200000) {
1577 heads = 255;
1578 sectors = 63;
1579 dummy = heads * sectors;
1580 cylinders = capacity;
1581 sector_div(cylinders, dummy);
1582 }
1583
1584 /* return result */
1585 params[0] = heads;
1586 params[1] = sectors;
1587 params[2] = cylinders;
1588
1589 return 0;
1590}
1591
1592/**
1593 * _scsih_response_code - translation of device response code
1594 * @ioc: per adapter object
1595 * @response_code: response code returned by the device
1596 *
1597 * Return nothing.
1598 */
1599static void
1600_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1601{
1602 char *desc;
1603
1604 switch (response_code) {
1605 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1606 desc = "task management request completed";
1607 break;
1608 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1609 desc = "invalid frame";
1610 break;
1611 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1612 desc = "task management request not supported";
1613 break;
1614 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1615 desc = "task management request failed";
1616 break;
1617 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1618 desc = "task management request succeeded";
1619 break;
1620 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1621 desc = "invalid lun";
1622 break;
1623 case 0xA:
1624 desc = "overlapped tag attempted";
1625 break;
1626 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1627 desc = "task queued, however not sent to target";
1628 break;
1629 default:
1630 desc = "unknown";
1631 break;
1632 }
1633 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1634 ioc->name, response_code, desc);
1635}
1636
1637/**
d5d135b3 1638 * _scsih_tm_done - tm completion routine
635374e7
EM
1639 * @ioc: per adapter object
1640 * @smid: system request message index
7b936b02 1641 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
1642 * @reply: reply message frame(lower 32bit addr)
1643 * Context: none.
1644 *
1645 * The callback handler when using scsih_issue_tm.
1646 *
77e63ed4
KD
1647 * Return 1 meaning mf should be freed from _base_interrupt
1648 * 0 means the mf is freed from this function.
635374e7 1649 */
77e63ed4 1650static u8
7b936b02 1651_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
635374e7
EM
1652{
1653 MPI2DefaultReply_t *mpi_reply;
1654
1655 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
77e63ed4 1656 return 1;
635374e7 1657 if (ioc->tm_cmds.smid != smid)
77e63ed4 1658 return 1;
635374e7
EM
1659 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1660 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1661 if (mpi_reply) {
1662 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1663 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1664 }
1665 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1666 complete(&ioc->tm_cmds.done);
77e63ed4 1667 return 1;
635374e7
EM
1668}
1669
1670/**
1671 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1672 * @ioc: per adapter object
1673 * @handle: device handle
1674 *
1675 * During taskmangement request, we need to freeze the device queue.
1676 */
1677void
1678mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1679{
1680 struct MPT2SAS_DEVICE *sas_device_priv_data;
1681 struct scsi_device *sdev;
1682 u8 skip = 0;
1683
1684 shost_for_each_device(sdev, ioc->shost) {
1685 if (skip)
1686 continue;
1687 sas_device_priv_data = sdev->hostdata;
1688 if (!sas_device_priv_data)
1689 continue;
1690 if (sas_device_priv_data->sas_target->handle == handle) {
1691 sas_device_priv_data->sas_target->tm_busy = 1;
1692 skip = 1;
1693 ioc->ignore_loginfos = 1;
1694 }
1695 }
1696}
1697
1698/**
1699 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1700 * @ioc: per adapter object
1701 * @handle: device handle
1702 *
1703 * During taskmangement request, we need to freeze the device queue.
1704 */
1705void
1706mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1707{
1708 struct MPT2SAS_DEVICE *sas_device_priv_data;
1709 struct scsi_device *sdev;
1710 u8 skip = 0;
1711
1712 shost_for_each_device(sdev, ioc->shost) {
1713 if (skip)
1714 continue;
1715 sas_device_priv_data = sdev->hostdata;
1716 if (!sas_device_priv_data)
1717 continue;
1718 if (sas_device_priv_data->sas_target->handle == handle) {
1719 sas_device_priv_data->sas_target->tm_busy = 0;
1720 skip = 1;
1721 ioc->ignore_loginfos = 0;
1722 }
1723 }
1724}
1725
1726/**
1727 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1728 * @ioc: per adapter struct
1729 * @device_handle: device handle
1730 * @lun: lun number
1731 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1732 * @smid_task: smid assigned to the task
1733 * @timeout: timeout in seconds
1734 * Context: The calling function needs to acquire the tm_cmds.mutex
1735 *
1736 * A generic API for sending task management requests to firmware.
1737 *
1738 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1739 * this API.
1740 *
1741 * The callback index is set inside `ioc->tm_cb_idx`.
1742 *
1743 * Return nothing.
1744 */
1745void
1746mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1747 u8 type, u16 smid_task, ulong timeout)
1748{
1749 Mpi2SCSITaskManagementRequest_t *mpi_request;
1750 Mpi2SCSITaskManagementReply_t *mpi_reply;
1751 u16 smid = 0;
1752 u32 ioc_state;
1753 unsigned long timeleft;
635374e7 1754
155dd4c7
KD
1755 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1756 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1757 __func__, ioc->name);
1758 return;
1759 }
1760
1761 if (ioc->shost_recovery) {
635374e7
EM
1762 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1763 __func__, ioc->name);
1764 return;
1765 }
635374e7
EM
1766
1767 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1768 if (ioc_state & MPI2_DOORBELL_USED) {
1769 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1770 "active!\n", ioc->name));
1771 goto issue_host_reset;
1772 }
1773
1774 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1775 mpt2sas_base_fault_info(ioc, ioc_state &
1776 MPI2_DOORBELL_DATA_MASK);
1777 goto issue_host_reset;
1778 }
1779
595bb0bd 1780 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
635374e7
EM
1781 if (!smid) {
1782 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1783 ioc->name, __func__);
1784 return;
1785 }
1786
1787 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
595bb0bd
KD
1788 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
1789 smid_task));
635374e7
EM
1790 ioc->tm_cmds.status = MPT2_CMD_PENDING;
1791 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1792 ioc->tm_cmds.smid = smid;
1793 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1794 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1795 mpi_request->DevHandle = cpu_to_le16(handle);
1796 mpi_request->TaskType = type;
1797 mpi_request->TaskMID = cpu_to_le16(smid_task);
7b936b02
KD
1798 mpi_request->VP_ID = 0; /* TODO */
1799 mpi_request->VF_ID = 0;
635374e7
EM
1800 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1801 mpt2sas_scsih_set_tm_flag(ioc, handle);
5b768581 1802 init_completion(&ioc->tm_cmds.done);
7b936b02 1803 mpt2sas_base_put_smid_hi_priority(ioc, smid);
635374e7
EM
1804 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1805 mpt2sas_scsih_clear_tm_flag(ioc, handle);
1806 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1807 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1808 ioc->name, __func__);
1809 _debug_dump_mf(mpi_request,
1810 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1811 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1812 goto issue_host_reset;
1813 }
1814
1815 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1816 mpi_reply = ioc->tm_cmds.reply;
1817 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1818 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1819 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1820 le32_to_cpu(mpi_reply->IOCLogInfo),
1821 le32_to_cpu(mpi_reply->TerminationCount)));
1822 if (ioc->logging_level & MPT_DEBUG_TM)
1823 _scsih_response_code(ioc, mpi_reply->ResponseCode);
1824 }
1825 return;
1826 issue_host_reset:
1827 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1828}
1829
1830/**
d5d135b3 1831 * _scsih_abort - eh threads main abort routine
635374e7
EM
1832 * @sdev: scsi device struct
1833 *
1834 * Returns SUCCESS if command aborted else FAILED
1835 */
1836static int
d5d135b3 1837_scsih_abort(struct scsi_cmnd *scmd)
635374e7
EM
1838{
1839 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1840 struct MPT2SAS_DEVICE *sas_device_priv_data;
1841 u16 smid;
1842 u16 handle;
1843 int r;
1844 struct scsi_cmnd *scmd_lookup;
1845
1846 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1847 ioc->name, scmd);
1848 scsi_print_command(scmd);
1849
1850 sas_device_priv_data = scmd->device->hostdata;
1851 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1852 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1853 ioc->name, scmd);
1854 scmd->result = DID_NO_CONNECT << 16;
1855 scmd->scsi_done(scmd);
1856 r = SUCCESS;
1857 goto out;
1858 }
1859
1860 /* search for the command */
1861 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1862 if (!smid) {
1863 scmd->result = DID_RESET << 16;
1864 r = SUCCESS;
1865 goto out;
1866 }
1867
1868 /* for hidden raid components and volumes this is not supported */
1869 if (sas_device_priv_data->sas_target->flags &
1870 MPT_TARGET_FLAGS_RAID_COMPONENT ||
1871 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1872 scmd->result = DID_RESET << 16;
1873 r = FAILED;
1874 goto out;
1875 }
1876
1877 mutex_lock(&ioc->tm_cmds.mutex);
1878 handle = sas_device_priv_data->sas_target->handle;
1879 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1880 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1881
1882 /* sanity check - see whether command actually completed */
1883 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1884 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1885 r = FAILED;
1886 else
1887 r = SUCCESS;
1888 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1889 mutex_unlock(&ioc->tm_cmds.mutex);
1890
1891 out:
1892 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1893 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1894 return r;
1895}
1896
635374e7 1897/**
d5d135b3 1898 * _scsih_dev_reset - eh threads main device reset routine
635374e7
EM
1899 * @sdev: scsi device struct
1900 *
1901 * Returns SUCCESS if command aborted else FAILED
1902 */
1903static int
d5d135b3 1904_scsih_dev_reset(struct scsi_cmnd *scmd)
635374e7
EM
1905{
1906 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1907 struct MPT2SAS_DEVICE *sas_device_priv_data;
1908 struct _sas_device *sas_device;
1909 unsigned long flags;
1910 u16 handle;
1911 int r;
1912
993e0da7 1913 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
635374e7
EM
1914 ioc->name, scmd);
1915 scsi_print_command(scmd);
1916
1917 sas_device_priv_data = scmd->device->hostdata;
1918 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1919 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1920 ioc->name, scmd);
1921 scmd->result = DID_NO_CONNECT << 16;
1922 scmd->scsi_done(scmd);
1923 r = SUCCESS;
1924 goto out;
1925 }
1926
1927 /* for hidden raid components obtain the volume_handle */
1928 handle = 0;
1929 if (sas_device_priv_data->sas_target->flags &
1930 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1931 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1932 sas_device = _scsih_sas_device_find_by_handle(ioc,
1933 sas_device_priv_data->sas_target->handle);
1934 if (sas_device)
1935 handle = sas_device->volume_handle;
1936 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1937 } else
1938 handle = sas_device_priv_data->sas_target->handle;
1939
1940 if (!handle) {
1941 scmd->result = DID_RESET << 16;
1942 r = FAILED;
1943 goto out;
1944 }
1945
993e0da7
EM
1946 mutex_lock(&ioc->tm_cmds.mutex);
1947 mpt2sas_scsih_issue_tm(ioc, handle, 0,
1948 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
1949 30);
1950
1951 /*
1952 * sanity check see whether all commands to this device been
1953 * completed
1954 */
1955 if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
1956 scmd->device->lun, scmd->device->channel))
1957 r = FAILED;
1958 else
1959 r = SUCCESS;
1960 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1961 mutex_unlock(&ioc->tm_cmds.mutex);
1962
1963 out:
1964 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
1965 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1966 return r;
1967}
1968
1969/**
d5d135b3 1970 * _scsih_target_reset - eh threads main target reset routine
993e0da7
EM
1971 * @sdev: scsi device struct
1972 *
1973 * Returns SUCCESS if command aborted else FAILED
1974 */
1975static int
d5d135b3 1976_scsih_target_reset(struct scsi_cmnd *scmd)
993e0da7
EM
1977{
1978 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1979 struct MPT2SAS_DEVICE *sas_device_priv_data;
1980 struct _sas_device *sas_device;
1981 unsigned long flags;
1982 u16 handle;
1983 int r;
1984
1985 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
1986 ioc->name, scmd);
1987 scsi_print_command(scmd);
1988
1989 sas_device_priv_data = scmd->device->hostdata;
1990 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1991 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
1992 ioc->name, scmd);
1993 scmd->result = DID_NO_CONNECT << 16;
1994 scmd->scsi_done(scmd);
1995 r = SUCCESS;
1996 goto out;
1997 }
1998
1999 /* for hidden raid components obtain the volume_handle */
2000 handle = 0;
2001 if (sas_device_priv_data->sas_target->flags &
2002 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2003 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2004 sas_device = _scsih_sas_device_find_by_handle(ioc,
2005 sas_device_priv_data->sas_target->handle);
2006 if (sas_device)
2007 handle = sas_device->volume_handle;
2008 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2009 } else
2010 handle = sas_device_priv_data->sas_target->handle;
2011
2012 if (!handle) {
2013 scmd->result = DID_RESET << 16;
2014 r = FAILED;
2015 goto out;
2016 }
2017
635374e7
EM
2018 mutex_lock(&ioc->tm_cmds.mutex);
2019 mpt2sas_scsih_issue_tm(ioc, handle, 0,
2020 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2021
2022 /*
2023 * sanity check see whether all commands to this target been
2024 * completed
2025 */
2026 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2027 scmd->device->channel))
2028 r = FAILED;
2029 else
2030 r = SUCCESS;
2031 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2032 mutex_unlock(&ioc->tm_cmds.mutex);
2033
2034 out:
2035 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2036 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2037 return r;
2038}
2039
2040/**
595bb0bd 2041 * _scsih_host_reset - eh threads main host reset routine
635374e7
EM
2042 * @sdev: scsi device struct
2043 *
2044 * Returns SUCCESS if command aborted else FAILED
2045 */
2046static int
d5d135b3 2047_scsih_host_reset(struct scsi_cmnd *scmd)
635374e7
EM
2048{
2049 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2050 int r, retval;
2051
2052 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2053 ioc->name, scmd);
2054 scsi_print_command(scmd);
2055
2056 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2057 FORCE_BIG_HAMMER);
2058 r = (retval < 0) ? FAILED : SUCCESS;
2059 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2060 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2061
2062 return r;
2063}
2064
2065/**
2066 * _scsih_fw_event_add - insert and queue up fw_event
2067 * @ioc: per adapter object
2068 * @fw_event: object describing the event
2069 * Context: This function will acquire ioc->fw_event_lock.
2070 *
2071 * This adds the firmware event object into link list, then queues it up to
2072 * be processed from user context.
2073 *
2074 * Return nothing.
2075 */
2076static void
2077_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2078{
2079 unsigned long flags;
2080
2081 if (ioc->firmware_event_thread == NULL)
2082 return;
2083
2084 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2085 list_add_tail(&fw_event->list, &ioc->fw_event_list);
6f92a7a0
EM
2086 INIT_WORK(&fw_event->work, _firmware_event_work);
2087 queue_work(ioc->firmware_event_thread, &fw_event->work);
635374e7
EM
2088 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2089}
2090
2091/**
2092 * _scsih_fw_event_free - delete fw_event
2093 * @ioc: per adapter object
2094 * @fw_event: object describing the event
2095 * Context: This function will acquire ioc->fw_event_lock.
2096 *
2097 * This removes firmware event object from link list, frees associated memory.
2098 *
2099 * Return nothing.
2100 */
2101static void
2102_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2103 *fw_event)
2104{
2105 unsigned long flags;
2106
2107 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2108 list_del(&fw_event->list);
2109 kfree(fw_event->event_data);
2110 kfree(fw_event);
2111 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2112}
2113
2114/**
2115 * _scsih_fw_event_add - requeue an event
2116 * @ioc: per adapter object
2117 * @fw_event: object describing the event
2118 * Context: This function will acquire ioc->fw_event_lock.
2119 *
2120 * Return nothing.
2121 */
2122static void
2123_scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2124 *fw_event, unsigned long delay)
2125{
2126 unsigned long flags;
2127 if (ioc->firmware_event_thread == NULL)
2128 return;
2129
2130 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6f92a7a0 2131 queue_work(ioc->firmware_event_thread, &fw_event->work);
635374e7
EM
2132 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2133}
2134
2135/**
2136 * _scsih_fw_event_off - turn flag off preventing event handling
2137 * @ioc: per adapter object
2138 *
2139 * Used to prevent handling of firmware events during adapter reset
2140 * driver unload.
2141 *
2142 * Return nothing.
2143 */
2144static void
2145_scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2146{
2147 unsigned long flags;
2148
2149 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2150 ioc->fw_events_off = 1;
2151 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2152
2153}
2154
2155/**
2156 * _scsih_fw_event_on - turn flag on allowing firmware event handling
2157 * @ioc: per adapter object
2158 *
2159 * Returns nothing.
2160 */
2161static void
2162_scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2163{
2164 unsigned long flags;
2165
2166 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2167 ioc->fw_events_off = 0;
2168 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2169}
2170
2171/**
2172 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2173 * @ioc: per adapter object
2174 * @handle: device handle
2175 *
2176 * During device pull we need to appropiately set the sdev state.
2177 */
2178static void
2179_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2180{
2181 struct MPT2SAS_DEVICE *sas_device_priv_data;
2182 struct scsi_device *sdev;
2183
2184 shost_for_each_device(sdev, ioc->shost) {
2185 sas_device_priv_data = sdev->hostdata;
2186 if (!sas_device_priv_data)
2187 continue;
2188 if (!sas_device_priv_data->block)
2189 continue;
2190 if (sas_device_priv_data->sas_target->handle == handle) {
2191 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2192 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2193 "handle(0x%04x)\n", ioc->name, handle));
2194 sas_device_priv_data->block = 0;
34a03bef 2195 scsi_internal_device_unblock(sdev);
635374e7
EM
2196 }
2197 }
2198}
2199
2200/**
2201 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2202 * @ioc: per adapter object
2203 * @handle: device handle
2204 *
2205 * During device pull we need to appropiately set the sdev state.
2206 */
2207static void
2208_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2209{
2210 struct MPT2SAS_DEVICE *sas_device_priv_data;
2211 struct scsi_device *sdev;
2212
2213 shost_for_each_device(sdev, ioc->shost) {
2214 sas_device_priv_data = sdev->hostdata;
2215 if (!sas_device_priv_data)
2216 continue;
2217 if (sas_device_priv_data->block)
2218 continue;
2219 if (sas_device_priv_data->sas_target->handle == handle) {
2220 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2221 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2222 "handle(0x%04x)\n", ioc->name, handle));
2223 sas_device_priv_data->block = 1;
34a03bef 2224 scsi_internal_device_block(sdev);
635374e7
EM
2225 }
2226 }
2227}
2228
2229/**
2230 * _scsih_block_io_to_children_attached_to_ex
2231 * @ioc: per adapter object
2232 * @sas_expander: the sas_device object
2233 *
2234 * This routine set sdev state to SDEV_BLOCK for all devices
2235 * attached to this expander. This function called when expander is
2236 * pulled.
2237 */
2238static void
2239_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2240 struct _sas_node *sas_expander)
2241{
2242 struct _sas_port *mpt2sas_port;
2243 struct _sas_device *sas_device;
2244 struct _sas_node *expander_sibling;
2245 unsigned long flags;
2246
2247 if (!sas_expander)
2248 return;
2249
2250 list_for_each_entry(mpt2sas_port,
2251 &sas_expander->sas_port_list, port_list) {
2252 if (mpt2sas_port->remote_identify.device_type ==
2253 SAS_END_DEVICE) {
2254 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2255 sas_device =
2256 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2257 mpt2sas_port->remote_identify.sas_address);
2258 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2259 if (!sas_device)
2260 continue;
2261 _scsih_block_io_device(ioc, sas_device->handle);
2262 }
2263 }
2264
2265 list_for_each_entry(mpt2sas_port,
2266 &sas_expander->sas_port_list, port_list) {
2267
2268 if (mpt2sas_port->remote_identify.device_type ==
2269 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2270 mpt2sas_port->remote_identify.device_type ==
2271 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2272
2273 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2274 expander_sibling =
2275 mpt2sas_scsih_expander_find_by_sas_address(
2276 ioc, mpt2sas_port->remote_identify.sas_address);
2277 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2278 _scsih_block_io_to_children_attached_to_ex(ioc,
2279 expander_sibling);
2280 }
2281 }
2282}
2283
2284/**
2285 * _scsih_block_io_to_children_attached_directly
2286 * @ioc: per adapter object
2287 * @event_data: topology change event data
2288 *
2289 * This routine set sdev state to SDEV_BLOCK for all devices
2290 * direct attached during device pull.
2291 */
2292static void
2293_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2294 Mpi2EventDataSasTopologyChangeList_t *event_data)
2295{
2296 int i;
2297 u16 handle;
2298 u16 reason_code;
2299 u8 phy_number;
34a03bef 2300 u8 link_rate;
635374e7
EM
2301
2302 for (i = 0; i < event_data->NumEntries; i++) {
2303 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2304 if (!handle)
2305 continue;
2306 phy_number = event_data->StartPhyNum + i;
2307 reason_code = event_data->PHY[i].PhyStatus &
2308 MPI2_EVENT_SAS_TOPO_RC_MASK;
2309 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2310 _scsih_block_io_device(ioc, handle);
34a03bef
KD
2311 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
2312 link_rate = event_data->PHY[i].LinkRate >> 4;
2313 if (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5)
2314 _scsih_ublock_io_device(ioc, handle);
2315 }
635374e7
EM
2316 }
2317}
2318
77e63ed4
KD
2319/**
2320 * _scsih_tm_tr_send - send task management request
2321 * @ioc: per adapter object
2322 * @handle: device handle
2323 * Context: interrupt time.
2324 *
2325 * This code is to initiate the device removal handshake protocal
2326 * with controller firmware. This function will issue target reset
2327 * using high priority request queue. It will send a sas iounit
2328 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2329 *
2330 * This is designed to send muliple task management request at the same
2331 * time to the fifo. If the fifo is full, we will append the request,
2332 * and process it in a future completion.
2333 */
2334static void
2335_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2336{
2337 Mpi2SCSITaskManagementRequest_t *mpi_request;
2338 struct MPT2SAS_TARGET *sas_target_priv_data;
2339 u16 smid;
2340 struct _sas_device *sas_device;
2341 unsigned long flags;
2342 struct _tr_list *delayed_tr;
2343
2344 if (ioc->shost_recovery) {
2345 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2346 __func__, ioc->name);
2347 return;
2348 }
2349
2350 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2351 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2352 if (!sas_device) {
2353 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2354 printk(MPT2SAS_ERR_FMT "%s: failed finding sas_device\n",
2355 ioc->name, __func__);
2356 return;
2357 }
2358 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2359
2360 /* skip is hidden raid component */
2361 if (sas_device->hidden_raid_component)
2362 return;
2363
2364 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2365 if (!smid) {
2366 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2367 if (!delayed_tr)
2368 return;
2369 INIT_LIST_HEAD(&delayed_tr->list);
2370 delayed_tr->handle = handle;
2371 delayed_tr->state = MPT2SAS_REQ_SAS_CNTRL;
2372 list_add_tail(&delayed_tr->list,
2373 &ioc->delayed_tr_list);
2374 if (sas_device->starget)
2375 dewtprintk(ioc, starget_printk(KERN_INFO,
2376 sas_device->starget, "DELAYED:tr:handle(0x%04x), "
2377 "(open)\n", sas_device->handle));
2378 return;
2379 }
2380
2381 if (sas_device->starget && sas_device->starget->hostdata) {
2382 sas_target_priv_data = sas_device->starget->hostdata;
2383 sas_target_priv_data->tm_busy = 1;
2384 dewtprintk(ioc, starget_printk(KERN_INFO, sas_device->starget,
2385 "tr:handle(0x%04x), (open)\n", sas_device->handle));
2386 }
2387
2388 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2389 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2390 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2391 mpi_request->DevHandle = cpu_to_le16(handle);
2392 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2393 sas_device->state |= MPTSAS_STATE_TR_SEND;
2394 sas_device->state |= MPT2SAS_REQ_SAS_CNTRL;
2395 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2396}
2397
2398
2399
2400/**
2401 * _scsih_sas_control_complete - completion routine
2402 * @ioc: per adapter object
2403 * @smid: system request message index
2404 * @msix_index: MSIX table index supplied by the OS
2405 * @reply: reply message frame(lower 32bit addr)
2406 * Context: interrupt time.
2407 *
2408 * This is the sas iounit controll completion routine.
2409 * This code is part of the code to initiate the device removal
2410 * handshake protocal with controller firmware.
2411 *
2412 * Return 1 meaning mf should be freed from _base_interrupt
2413 * 0 means the mf is freed from this function.
2414 */
2415static u8
2416_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2417 u8 msix_index, u32 reply)
2418{
2419 unsigned long flags;
2420 u16 handle;
2421 struct _sas_device *sas_device;
2422 Mpi2SasIoUnitControlReply_t *mpi_reply =
2423 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2424
2425 handle = le16_to_cpu(mpi_reply->DevHandle);
2426
2427 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2428 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2429 if (!sas_device) {
2430 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2431 printk(MPT2SAS_ERR_FMT "%s: failed finding sas_device\n",
2432 ioc->name, __func__);
2433 return 1;
2434 }
2435 sas_device->state |= MPTSAS_STATE_CNTRL_COMPLETE;
2436 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2437
2438 if (sas_device->starget)
2439 dewtprintk(ioc, starget_printk(KERN_INFO, sas_device->starget,
2440 "sc_complete:handle(0x%04x), "
2441 "ioc_status(0x%04x), loginfo(0x%08x)\n",
2442 handle, le16_to_cpu(mpi_reply->IOCStatus),
2443 le32_to_cpu(mpi_reply->IOCLogInfo)));
2444 return 1;
2445}
2446
2447/**
2448 * _scsih_tm_tr_complete -
2449 * @ioc: per adapter object
2450 * @smid: system request message index
2451 * @msix_index: MSIX table index supplied by the OS
2452 * @reply: reply message frame(lower 32bit addr)
2453 * Context: interrupt time.
2454 *
2455 * This is the target reset completion routine.
2456 * This code is part of the code to initiate the device removal
2457 * handshake protocal with controller firmware.
2458 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2459 *
2460 * Return 1 meaning mf should be freed from _base_interrupt
2461 * 0 means the mf is freed from this function.
2462 */
2463static u8
2464_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2465 u32 reply)
2466{
2467 unsigned long flags;
2468 u16 handle;
2469 struct _sas_device *sas_device;
2470 Mpi2SCSITaskManagementReply_t *mpi_reply =
2471 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2472 Mpi2SasIoUnitControlRequest_t *mpi_request;
2473 u16 smid_sas_ctrl;
2474 struct MPT2SAS_TARGET *sas_target_priv_data;
2475 struct _tr_list *delayed_tr;
2476 u8 rc;
2477
2478 handle = le16_to_cpu(mpi_reply->DevHandle);
2479 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2480 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2481 if (!sas_device) {
2482 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2483 printk(MPT2SAS_ERR_FMT "%s: failed finding sas_device\n",
2484 ioc->name, __func__);
2485 return 1;
2486 }
2487 sas_device->state |= MPTSAS_STATE_TR_COMPLETE;
2488 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2489
2490 if (sas_device->starget)
2491 dewtprintk(ioc, starget_printk(KERN_INFO, sas_device->starget,
2492 "tr_complete:handle(0x%04x), (%s) ioc_status(0x%04x), "
2493 "loginfo(0x%08x), completed(%d)\n",
2494 sas_device->handle, (sas_device->state &
2495 MPT2SAS_REQ_SAS_CNTRL) ? "open" : "active",
2496 le16_to_cpu(mpi_reply->IOCStatus),
2497 le32_to_cpu(mpi_reply->IOCLogInfo),
2498 le32_to_cpu(mpi_reply->TerminationCount)));
2499
2500 if (sas_device->starget && sas_device->starget->hostdata) {
2501 sas_target_priv_data = sas_device->starget->hostdata;
2502 sas_target_priv_data->tm_busy = 0;
2503 }
2504
2505 if (!list_empty(&ioc->delayed_tr_list)) {
2506 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2507 struct _tr_list, list);
2508 mpt2sas_base_free_smid(ioc, smid);
2509 if (delayed_tr->state & MPT2SAS_REQ_SAS_CNTRL)
2510 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2511 list_del(&delayed_tr->list);
2512 kfree(delayed_tr);
2513 rc = 0; /* tells base_interrupt not to free mf */
2514 } else
2515 rc = 1;
2516
2517
2518 if (!(sas_device->state & MPT2SAS_REQ_SAS_CNTRL))
2519 return rc;
2520
2521 if (ioc->shost_recovery) {
2522 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2523 __func__, ioc->name);
2524 return rc;
2525 }
2526
2527 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2528 if (!smid_sas_ctrl) {
2529 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2530 ioc->name, __func__);
2531 return rc;
2532 }
2533
2534 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2535 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2536 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2537 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
2538 mpi_request->DevHandle = mpi_reply->DevHandle;
2539 sas_device->state |= MPTSAS_STATE_CNTRL_SEND;
2540 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
2541 return rc;
2542}
2543
635374e7
EM
2544/**
2545 * _scsih_check_topo_delete_events - sanity check on topo events
2546 * @ioc: per adapter object
2547 * @event_data: the event data payload
2548 *
2549 * This routine added to better handle cable breaker.
2550 *
2551 * This handles the case where driver recieves multiple expander
2552 * add and delete events in a single shot. When there is a delete event
2553 * the routine will void any pending add events waiting in the event queue.
2554 *
2555 * Return nothing.
2556 */
2557static void
2558_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2559 Mpi2EventDataSasTopologyChangeList_t *event_data)
2560{
2561 struct fw_event_work *fw_event;
2562 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2563 u16 expander_handle;
2564 struct _sas_node *sas_expander;
2565 unsigned long flags;
77e63ed4
KD
2566 int i, reason_code;
2567 u16 handle;
2568
2569 for (i = 0 ; i < event_data->NumEntries; i++) {
2570 if (event_data->PHY[i].PhyStatus &
2571 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2572 continue;
2573 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2574 if (!handle)
2575 continue;
2576 reason_code = event_data->PHY[i].PhyStatus &
2577 MPI2_EVENT_SAS_TOPO_RC_MASK;
2578 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2579 _scsih_tm_tr_send(ioc, handle);
2580 }
635374e7
EM
2581
2582 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2583 if (expander_handle < ioc->sas_hba.num_phys) {
2584 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2585 return;
2586 }
2587
2588 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2589 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2590 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2591 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2592 expander_handle);
2593 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2594 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2595 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2596 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2597
2598 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2599 return;
2600
2601 /* mark ignore flag for pending events */
2602 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2603 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2604 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2605 fw_event->ignore)
2606 continue;
2607 local_event_data = fw_event->event_data;
2608 if (local_event_data->ExpStatus ==
2609 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2610 local_event_data->ExpStatus ==
2611 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2612 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2613 expander_handle) {
2614 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2615 "setting ignoring flag\n", ioc->name));
2616 fw_event->ignore = 1;
2617 }
2618 }
2619 }
2620 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2621}
2622
635374e7
EM
2623/**
2624 * _scsih_flush_running_cmds - completing outstanding commands.
2625 * @ioc: per adapter object
2626 *
2627 * The flushing out of all pending scmd commands following host reset,
2628 * where all IO is dropped to the floor.
2629 *
2630 * Return nothing.
2631 */
2632static void
2633_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2634{
2635 struct scsi_cmnd *scmd;
2636 u16 smid;
2637 u16 count = 0;
2638
595bb0bd
KD
2639 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
2640 scmd = _scsih_scsi_lookup_get(ioc, smid);
635374e7
EM
2641 if (!scmd)
2642 continue;
2643 count++;
2644 mpt2sas_base_free_smid(ioc, smid);
2645 scsi_dma_unmap(scmd);
2646 scmd->result = DID_RESET << 16;
2647 scmd->scsi_done(scmd);
2648 }
2649 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2650 ioc->name, count));
2651}
2652
3c621b3e
EM
2653/**
2654 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2655 * @scmd: pointer to scsi command object
2656 * @mpi_request: pointer to the SCSI_IO reqest message frame
2657 *
2658 * Supporting protection 1 and 3.
2659 *
2660 * Returns nothing
2661 */
2662static void
2663_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2664{
2665 u16 eedp_flags;
2666 unsigned char prot_op = scsi_get_prot_op(scmd);
2667 unsigned char prot_type = scsi_get_prot_type(scmd);
2668
2669 if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2670 prot_type == SCSI_PROT_DIF_TYPE2 ||
2671 prot_op == SCSI_PROT_NORMAL)
2672 return;
2673
2674 if (prot_op == SCSI_PROT_READ_STRIP)
2675 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2676 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2677 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2678 else
2679 return;
2680
2681 mpi_request->EEDPBlockSize = scmd->device->sector_size;
2682
2683 switch (prot_type) {
2684 case SCSI_PROT_DIF_TYPE1:
2685
2686 /*
2687 * enable ref/guard checking
2688 * auto increment ref tag
2689 */
2690 mpi_request->EEDPFlags = eedp_flags |
2691 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2692 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2693 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2694 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2695 cpu_to_be32(scsi_get_lba(scmd));
2696
2697 break;
2698
2699 case SCSI_PROT_DIF_TYPE3:
2700
2701 /*
2702 * enable guard checking
2703 */
2704 mpi_request->EEDPFlags = eedp_flags |
2705 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2706
2707 break;
2708 }
2709}
2710
2711/**
2712 * _scsih_eedp_error_handling - return sense code for EEDP errors
2713 * @scmd: pointer to scsi command object
2714 * @ioc_status: ioc status
2715 *
2716 * Returns nothing
2717 */
2718static void
2719_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2720{
2721 u8 ascq;
2722 u8 sk;
2723 u8 host_byte;
2724
2725 switch (ioc_status) {
2726 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2727 ascq = 0x01;
2728 break;
2729 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2730 ascq = 0x02;
2731 break;
2732 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2733 ascq = 0x03;
2734 break;
2735 default:
2736 ascq = 0x00;
2737 break;
2738 }
2739
2740 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2741 sk = ILLEGAL_REQUEST;
2742 host_byte = DID_ABORT;
2743 } else {
2744 sk = ABORTED_COMMAND;
2745 host_byte = DID_OK;
2746 }
2747
2748 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2749 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2750 SAM_STAT_CHECK_CONDITION;
2751}
2752
635374e7 2753/**
d5d135b3 2754 * _scsih_qcmd - main scsi request entry point
635374e7
EM
2755 * @scmd: pointer to scsi command object
2756 * @done: function pointer to be invoked on completion
2757 *
2758 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2759 *
2760 * Returns 0 on success. If there's a failure, return either:
2761 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2762 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2763 */
2764static int
d5d135b3 2765_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
635374e7
EM
2766{
2767 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2768 struct MPT2SAS_DEVICE *sas_device_priv_data;
2769 struct MPT2SAS_TARGET *sas_target_priv_data;
2770 Mpi2SCSIIORequest_t *mpi_request;
2771 u32 mpi_control;
2772 u16 smid;
635374e7
EM
2773
2774 scmd->scsi_done = done;
2775 sas_device_priv_data = scmd->device->hostdata;
2776 if (!sas_device_priv_data) {
2777 scmd->result = DID_NO_CONNECT << 16;
2778 scmd->scsi_done(scmd);
2779 return 0;
2780 }
2781
2782 sas_target_priv_data = sas_device_priv_data->sas_target;
2783 if (!sas_target_priv_data || sas_target_priv_data->handle ==
2784 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2785 scmd->result = DID_NO_CONNECT << 16;
2786 scmd->scsi_done(scmd);
2787 return 0;
2788 }
2789
2790 /* see if we are busy with task managment stuff */
155dd4c7
KD
2791 if (sas_target_priv_data->tm_busy)
2792 return SCSI_MLQUEUE_DEVICE_BUSY;
2793 else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
635374e7 2794 return SCSI_MLQUEUE_HOST_BUSY;
635374e7
EM
2795
2796 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2797 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2798 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2799 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2800 else
2801 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2802
2803 /* set tags */
2804 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2805 if (scmd->device->tagged_supported) {
2806 if (scmd->device->ordered_tags)
2807 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2808 else
2809 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2810 } else
2811/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2812/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2813 */
2814 mpi_control |= (0x500);
2815
2816 } else
2817 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2818
2819 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2820 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2821
595bb0bd 2822 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
635374e7
EM
2823 if (!smid) {
2824 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2825 ioc->name, __func__);
2826 goto out;
2827 }
2828 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2829 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3c621b3e 2830 _scsih_setup_eedp(scmd, mpi_request);
635374e7
EM
2831 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2832 if (sas_device_priv_data->sas_target->flags &
2833 MPT_TARGET_FLAGS_RAID_COMPONENT)
2834 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2835 else
2836 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2837 mpi_request->DevHandle =
2838 cpu_to_le16(sas_device_priv_data->sas_target->handle);
2839 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2840 mpi_request->Control = cpu_to_le32(mpi_control);
2841 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2842 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2843 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2844 mpi_request->SenseBufferLowAddress =
2845 (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2846 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2847 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2848 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
7b936b02
KD
2849 mpi_request->VF_ID = 0; /* TODO */
2850 mpi_request->VP_ID = 0;
635374e7
EM
2851 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2852 mpi_request->LUN);
2853 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2854
2855 if (!mpi_request->DataLength) {
2856 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2857 } else {
2858 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2859 mpt2sas_base_free_smid(ioc, smid);
2860 goto out;
2861 }
2862 }
2863
7b936b02 2864 mpt2sas_base_put_smid_scsi_io(ioc, smid,
635374e7
EM
2865 sas_device_priv_data->sas_target->handle);
2866 return 0;
2867
2868 out:
2869 return SCSI_MLQUEUE_HOST_BUSY;
2870}
2871
2872/**
2873 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2874 * @sense_buffer: sense data returned by target
2875 * @data: normalized skey/asc/ascq
2876 *
2877 * Return nothing.
2878 */
2879static void
2880_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2881{
2882 if ((sense_buffer[0] & 0x7F) >= 0x72) {
2883 /* descriptor format */
2884 data->skey = sense_buffer[1] & 0x0F;
2885 data->asc = sense_buffer[2];
2886 data->ascq = sense_buffer[3];
2887 } else {
2888 /* fixed format */
2889 data->skey = sense_buffer[2] & 0x0F;
2890 data->asc = sense_buffer[12];
2891 data->ascq = sense_buffer[13];
2892 }
2893}
2894
2895#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2896/**
af901ca1 2897 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
635374e7
EM
2898 * @ioc: per adapter object
2899 * @scmd: pointer to scsi command object
2900 * @mpi_reply: reply mf payload returned from firmware
2901 *
2902 * scsi_status - SCSI Status code returned from target device
2903 * scsi_state - state info associated with SCSI_IO determined by ioc
2904 * ioc_status - ioc supplied status info
2905 *
2906 * Return nothing.
2907 */
2908static void
2909_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2910 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
2911{
2912 u32 response_info;
2913 u8 *response_bytes;
2914 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
2915 MPI2_IOCSTATUS_MASK;
2916 u8 scsi_state = mpi_reply->SCSIState;
2917 u8 scsi_status = mpi_reply->SCSIStatus;
2918 char *desc_ioc_state = NULL;
2919 char *desc_scsi_status = NULL;
2920 char *desc_scsi_state = ioc->tmp_string;
be9e8cd7
KD
2921 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
2922
2923 if (log_info == 0x31170000)
2924 return;
635374e7
EM
2925
2926 switch (ioc_status) {
2927 case MPI2_IOCSTATUS_SUCCESS:
2928 desc_ioc_state = "success";
2929 break;
2930 case MPI2_IOCSTATUS_INVALID_FUNCTION:
2931 desc_ioc_state = "invalid function";
2932 break;
2933 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2934 desc_ioc_state = "scsi recovered error";
2935 break;
2936 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2937 desc_ioc_state = "scsi invalid dev handle";
2938 break;
2939 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2940 desc_ioc_state = "scsi device not there";
2941 break;
2942 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2943 desc_ioc_state = "scsi data overrun";
2944 break;
2945 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2946 desc_ioc_state = "scsi data underrun";
2947 break;
2948 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2949 desc_ioc_state = "scsi io data error";
2950 break;
2951 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2952 desc_ioc_state = "scsi protocol error";
2953 break;
2954 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2955 desc_ioc_state = "scsi task terminated";
2956 break;
2957 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2958 desc_ioc_state = "scsi residual mismatch";
2959 break;
2960 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2961 desc_ioc_state = "scsi task mgmt failed";
2962 break;
2963 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2964 desc_ioc_state = "scsi ioc terminated";
2965 break;
2966 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2967 desc_ioc_state = "scsi ext terminated";
2968 break;
3c621b3e
EM
2969 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2970 desc_ioc_state = "eedp guard error";
2971 break;
2972 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2973 desc_ioc_state = "eedp ref tag error";
2974 break;
2975 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2976 desc_ioc_state = "eedp app tag error";
2977 break;
635374e7
EM
2978 default:
2979 desc_ioc_state = "unknown";
2980 break;
2981 }
2982
2983 switch (scsi_status) {
2984 case MPI2_SCSI_STATUS_GOOD:
2985 desc_scsi_status = "good";
2986 break;
2987 case MPI2_SCSI_STATUS_CHECK_CONDITION:
2988 desc_scsi_status = "check condition";
2989 break;
2990 case MPI2_SCSI_STATUS_CONDITION_MET:
2991 desc_scsi_status = "condition met";
2992 break;
2993 case MPI2_SCSI_STATUS_BUSY:
2994 desc_scsi_status = "busy";
2995 break;
2996 case MPI2_SCSI_STATUS_INTERMEDIATE:
2997 desc_scsi_status = "intermediate";
2998 break;
2999 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3000 desc_scsi_status = "intermediate condmet";
3001 break;
3002 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3003 desc_scsi_status = "reservation conflict";
3004 break;
3005 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3006 desc_scsi_status = "command terminated";
3007 break;
3008 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3009 desc_scsi_status = "task set full";
3010 break;
3011 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3012 desc_scsi_status = "aca active";
3013 break;
3014 case MPI2_SCSI_STATUS_TASK_ABORTED:
3015 desc_scsi_status = "task aborted";
3016 break;
3017 default:
3018 desc_scsi_status = "unknown";
3019 break;
3020 }
3021
3022 desc_scsi_state[0] = '\0';
3023 if (!scsi_state)
3024 desc_scsi_state = " ";
3025 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3026 strcat(desc_scsi_state, "response info ");
3027 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3028 strcat(desc_scsi_state, "state terminated ");
3029 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3030 strcat(desc_scsi_state, "no status ");
3031 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3032 strcat(desc_scsi_state, "autosense failed ");
3033 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3034 strcat(desc_scsi_state, "autosense valid ");
3035
3036 scsi_print_command(scmd);
3037 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
3038 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
3039 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
3040 ioc_status, smid);
3041 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3042 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3043 scsi_get_resid(scmd));
3044 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3045 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3046 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3047 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3048 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3049 scsi_status, desc_scsi_state, scsi_state);
3050
3051 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3052 struct sense_info data;
3053 _scsih_normalize_sense(scmd->sense_buffer, &data);
3054 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
3055 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
3056 data.asc, data.ascq);
3057 }
3058
3059 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3060 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3061 response_bytes = (u8 *)&response_info;
3062 _scsih_response_code(ioc, response_bytes[3]);
3063 }
3064}
3065#endif
3066
3067/**
3068 * _scsih_smart_predicted_fault - illuminate Fault LED
3069 * @ioc: per adapter object
3070 * @handle: device handle
3071 *
3072 * Return nothing.
3073 */
3074static void
3075_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3076{
3077 Mpi2SepReply_t mpi_reply;
3078 Mpi2SepRequest_t mpi_request;
3079 struct scsi_target *starget;
3080 struct MPT2SAS_TARGET *sas_target_priv_data;
3081 Mpi2EventNotificationReply_t *event_reply;
3082 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3083 struct _sas_device *sas_device;
3084 ssize_t sz;
3085 unsigned long flags;
3086
3087 /* only handle non-raid devices */
3088 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3089 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3090 if (!sas_device) {
3091 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3092 return;
3093 }
3094 starget = sas_device->starget;
3095 sas_target_priv_data = starget->hostdata;
3096
3097 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3098 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3099 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3100 return;
3101 }
3102 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3103 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3104
3105 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3106 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3107 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3108 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3109 mpi_request.SlotStatus =
3110 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
3111 mpi_request.DevHandle = cpu_to_le16(handle);
3112 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3113 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3114 &mpi_request)) != 0) {
3115 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3116 ioc->name, __FILE__, __LINE__, __func__);
3117 return;
3118 }
3119
3120 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3121 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3122 "enclosure_processor: ioc_status (0x%04x), "
3123 "loginfo(0x%08x)\n", ioc->name,
3124 le16_to_cpu(mpi_reply.IOCStatus),
3125 le32_to_cpu(mpi_reply.IOCLogInfo)));
3126 return;
3127 }
3128 }
3129
3130 /* insert into event log */
3131 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3132 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3133 event_reply = kzalloc(sz, GFP_KERNEL);
3134 if (!event_reply) {
3135 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3136 ioc->name, __FILE__, __LINE__, __func__);
3137 return;
3138 }
3139
3140 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3141 event_reply->Event =
3142 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3143 event_reply->MsgLength = sz/4;
3144 event_reply->EventDataLength =
3145 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3146 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3147 event_reply->EventData;
3148 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3149 event_data->ASC = 0x5D;
3150 event_data->DevHandle = cpu_to_le16(handle);
3151 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3152 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3153 kfree(event_reply);
3154}
3155
3156/**
d5d135b3 3157 * _scsih_io_done - scsi request callback
635374e7
EM
3158 * @ioc: per adapter object
3159 * @smid: system request message index
7b936b02 3160 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
3161 * @reply: reply message frame(lower 32bit addr)
3162 *
77e63ed4 3163 * Callback handler when using _scsih_qcmd.
635374e7 3164 *
77e63ed4
KD
3165 * Return 1 meaning mf should be freed from _base_interrupt
3166 * 0 means the mf is freed from this function.
635374e7 3167 */
77e63ed4 3168static u8
7b936b02 3169_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
635374e7
EM
3170{
3171 Mpi2SCSIIORequest_t *mpi_request;
3172 Mpi2SCSIIOReply_t *mpi_reply;
3173 struct scsi_cmnd *scmd;
3174 u16 ioc_status;
3175 u32 xfer_cnt;
3176 u8 scsi_state;
3177 u8 scsi_status;
3178 u32 log_info;
3179 struct MPT2SAS_DEVICE *sas_device_priv_data;
3180 u32 response_code;
3181
3182 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
595bb0bd 3183 scmd = _scsih_scsi_lookup_get(ioc, smid);
635374e7 3184 if (scmd == NULL)
77e63ed4 3185 return 1;
635374e7
EM
3186
3187 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3188
3189 if (mpi_reply == NULL) {
3190 scmd->result = DID_OK << 16;
3191 goto out;
3192 }
3193
3194 sas_device_priv_data = scmd->device->hostdata;
3195 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3196 sas_device_priv_data->sas_target->deleted) {
3197 scmd->result = DID_NO_CONNECT << 16;
3198 goto out;
3199 }
3200
3201 /* turning off TLR */
3202 if (!sas_device_priv_data->tlr_snoop_check) {
3203 sas_device_priv_data->tlr_snoop_check++;
3204 if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
3205 response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
3206 >> 24);
3207 if (response_code ==
3208 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
3209 sas_device_priv_data->flags &=
3210 ~MPT_DEVICE_TLR_ON;
3211 }
3212 }
3213
3214 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3215 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3216 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3217 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3218 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3219 else
3220 log_info = 0;
3221 ioc_status &= MPI2_IOCSTATUS_MASK;
3222 scsi_state = mpi_reply->SCSIState;
3223 scsi_status = mpi_reply->SCSIStatus;
3224
3225 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3226 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3227 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3228 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3229 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3230 }
3231
3232 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3233 struct sense_info data;
3234 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3235 smid);
0d04df9b 3236 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
635374e7 3237 le32_to_cpu(mpi_reply->SenseCount));
0d04df9b 3238 memcpy(scmd->sense_buffer, sense_data, sz);
635374e7
EM
3239 _scsih_normalize_sense(scmd->sense_buffer, &data);
3240 /* failure prediction threshold exceeded */
3241 if (data.asc == 0x5D)
3242 _scsih_smart_predicted_fault(ioc,
3243 le16_to_cpu(mpi_reply->DevHandle));
3244 }
3245
3246 switch (ioc_status) {
3247 case MPI2_IOCSTATUS_BUSY:
3248 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3249 scmd->result = SAM_STAT_BUSY;
3250 break;
3251
3252 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3253 scmd->result = DID_NO_CONNECT << 16;
3254 break;
3255
3256 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3257 if (sas_device_priv_data->block) {
3258 scmd->result = (DID_BUS_BUSY << 16);
3259 break;
3260 }
3261
3262 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3263 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3264 scmd->result = DID_RESET << 16;
3265 break;
3266
3267 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3268 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3269 scmd->result = DID_SOFT_ERROR << 16;
3270 else
3271 scmd->result = (DID_OK << 16) | scsi_status;
3272 break;
3273
3274 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3275 scmd->result = (DID_OK << 16) | scsi_status;
3276
3277 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3278 break;
3279
3280 if (xfer_cnt < scmd->underflow) {
3281 if (scsi_status == SAM_STAT_BUSY)
3282 scmd->result = SAM_STAT_BUSY;
3283 else
3284 scmd->result = DID_SOFT_ERROR << 16;
3285 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3286 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3287 scmd->result = DID_SOFT_ERROR << 16;
3288 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3289 scmd->result = DID_RESET << 16;
3290 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3291 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3292 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3293 scmd->result = (DRIVER_SENSE << 24) |
3294 SAM_STAT_CHECK_CONDITION;
3295 scmd->sense_buffer[0] = 0x70;
3296 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3297 scmd->sense_buffer[12] = 0x20;
3298 scmd->sense_buffer[13] = 0;
3299 }
3300 break;
3301
3302 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3303 scsi_set_resid(scmd, 0);
3304 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3305 case MPI2_IOCSTATUS_SUCCESS:
3306 scmd->result = (DID_OK << 16) | scsi_status;
3307 if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3308 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3309 scmd->result = DID_SOFT_ERROR << 16;
3310 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3311 scmd->result = DID_RESET << 16;
3312 break;
3313
3c621b3e
EM
3314 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3315 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3316 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3317 _scsih_eedp_error_handling(scmd, ioc_status);
3318 break;
635374e7
EM
3319 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3320 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3321 case MPI2_IOCSTATUS_INVALID_SGL:
3322 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3323 case MPI2_IOCSTATUS_INVALID_FIELD:
3324 case MPI2_IOCSTATUS_INVALID_STATE:
3325 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3326 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3327 default:
3328 scmd->result = DID_SOFT_ERROR << 16;
3329 break;
3330
3331 }
3332
3333#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3334 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3335 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3336#endif
3337
3338 out:
3339 scsi_dma_unmap(scmd);
3340 scmd->scsi_done(scmd);
77e63ed4 3341 return 1;
635374e7
EM
3342}
3343
635374e7
EM
3344/**
3345 * _scsih_sas_host_refresh - refreshing sas host object contents
3346 * @ioc: per adapter object
3347 * @update: update link information
3348 * Context: user
3349 *
3350 * During port enable, fw will send topology events for every device. Its
3351 * possible that the handles may change from the previous setting, so this
3352 * code keeping handles updating if changed.
3353 *
3354 * Return nothing.
3355 */
3356static void
3357_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
3358{
3359 u16 sz;
3360 u16 ioc_status;
3361 int i;
3362 Mpi2ConfigReply_t mpi_reply;
3363 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3364
3365 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3366 "updating handles for sas_host(0x%016llx)\n",
3367 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3368
3369 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3370 * sizeof(Mpi2SasIOUnit0PhyData_t));
3371 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3372 if (!sas_iounit_pg0) {
3373 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3374 ioc->name, __FILE__, __LINE__, __func__);
3375 return;
3376 }
3377 if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3378 sas_iounit_pg0, sz))) {
3379 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3380 MPI2_IOCSTATUS_MASK;
3381 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3382 goto out;
3383 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3384 ioc->sas_hba.phy[i].handle =
3385 le16_to_cpu(sas_iounit_pg0->PhyData[i].
3386 ControllerDevHandle);
3387 if (update)
cc0f5207
KD
3388 mpt2sas_transport_update_links(
3389 ioc,
635374e7
EM
3390 ioc->sas_hba.phy[i].handle,
3391 le16_to_cpu(sas_iounit_pg0->PhyData[i].
3392 AttachedDevHandle), i,
3393 sas_iounit_pg0->PhyData[i].
3394 NegotiatedLinkRate >> 4);
3395 }
3396 }
3397
3398 out:
3399 kfree(sas_iounit_pg0);
3400}
3401
3402/**
3403 * _scsih_sas_host_add - create sas host object
3404 * @ioc: per adapter object
3405 *
3406 * Creating host side data object, stored in ioc->sas_hba
3407 *
3408 * Return nothing.
3409 */
3410static void
3411_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3412{
3413 int i;
3414 Mpi2ConfigReply_t mpi_reply;
3415 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3416 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3417 Mpi2SasPhyPage0_t phy_pg0;
3418 Mpi2SasDevicePage0_t sas_device_pg0;
3419 Mpi2SasEnclosurePage0_t enclosure_pg0;
3420 u16 ioc_status;
3421 u16 sz;
3422 u16 device_missing_delay;
3423
3424 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3425 if (!ioc->sas_hba.num_phys) {
3426 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3427 ioc->name, __FILE__, __LINE__, __func__);
3428 return;
3429 }
3430
3431 /* sas_iounit page 0 */
3432 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3433 sizeof(Mpi2SasIOUnit0PhyData_t));
3434 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3435 if (!sas_iounit_pg0) {
3436 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3437 ioc->name, __FILE__, __LINE__, __func__);
3438 return;
3439 }
3440 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3441 sas_iounit_pg0, sz))) {
3442 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3443 ioc->name, __FILE__, __LINE__, __func__);
3444 goto out;
3445 }
3446 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3447 MPI2_IOCSTATUS_MASK;
3448 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3449 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3450 ioc->name, __FILE__, __LINE__, __func__);
3451 goto out;
3452 }
3453
3454 /* sas_iounit page 1 */
3455 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3456 sizeof(Mpi2SasIOUnit1PhyData_t));
3457 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3458 if (!sas_iounit_pg1) {
3459 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3460 ioc->name, __FILE__, __LINE__, __func__);
3461 goto out;
3462 }
3463 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3464 sas_iounit_pg1, sz))) {
3465 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3466 ioc->name, __FILE__, __LINE__, __func__);
3467 goto out;
3468 }
3469 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3470 MPI2_IOCSTATUS_MASK;
3471 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3472 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3473 ioc->name, __FILE__, __LINE__, __func__);
3474 goto out;
3475 }
3476
3477 ioc->io_missing_delay =
3478 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3479 device_missing_delay =
3480 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3481 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3482 ioc->device_missing_delay = (device_missing_delay &
3483 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3484 else
3485 ioc->device_missing_delay = device_missing_delay &
3486 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3487
3488 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3489 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3490 sizeof(struct _sas_phy), GFP_KERNEL);
3491 if (!ioc->sas_hba.phy) {
3492 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3493 ioc->name, __FILE__, __LINE__, __func__);
3494 goto out;
3495 }
3496 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3497 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3498 i))) {
3499 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3500 ioc->name, __FILE__, __LINE__, __func__);
3501 goto out;
3502 }
3503 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3504 MPI2_IOCSTATUS_MASK;
3505 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3506 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3507 ioc->name, __FILE__, __LINE__, __func__);
3508 goto out;
3509 }
3510 ioc->sas_hba.phy[i].handle =
3511 le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
3512 ioc->sas_hba.phy[i].phy_id = i;
3513 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3514 phy_pg0, ioc->sas_hba.parent_dev);
3515 }
3516 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3517 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
3518 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3519 ioc->name, __FILE__, __LINE__, __func__);
3520 goto out;
3521 }
3522 ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
3523 ioc->sas_hba.enclosure_handle =
3524 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3525 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3526 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3527 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3528 (unsigned long long) ioc->sas_hba.sas_address,
3529 ioc->sas_hba.num_phys) ;
3530
3531 if (ioc->sas_hba.enclosure_handle) {
3532 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3533 &enclosure_pg0,
3534 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3535 ioc->sas_hba.enclosure_handle))) {
3536 ioc->sas_hba.enclosure_logical_id =
3537 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3538 }
3539 }
3540
3541 out:
3542 kfree(sas_iounit_pg1);
3543 kfree(sas_iounit_pg0);
3544}
3545
3546/**
3547 * _scsih_expander_add - creating expander object
3548 * @ioc: per adapter object
3549 * @handle: expander handle
3550 *
3551 * Creating expander object, stored in ioc->sas_expander_list.
3552 *
3553 * Return 0 for success, else error.
3554 */
3555static int
3556_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3557{
3558 struct _sas_node *sas_expander;
3559 Mpi2ConfigReply_t mpi_reply;
3560 Mpi2ExpanderPage0_t expander_pg0;
3561 Mpi2ExpanderPage1_t expander_pg1;
3562 Mpi2SasEnclosurePage0_t enclosure_pg0;
3563 u32 ioc_status;
3564 u16 parent_handle;
3565 __le64 sas_address;
3566 int i;
3567 unsigned long flags;
20f5895d 3568 struct _sas_port *mpt2sas_port = NULL;
635374e7
EM
3569 int rc = 0;
3570
3571 if (!handle)
3572 return -1;
3573
155dd4c7
KD
3574 if (ioc->shost_recovery)
3575 return -1;
3576
635374e7
EM
3577 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3578 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3579 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3580 ioc->name, __FILE__, __LINE__, __func__);
3581 return -1;
3582 }
3583
3584 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3585 MPI2_IOCSTATUS_MASK;
3586 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3587 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3588 ioc->name, __FILE__, __LINE__, __func__);
3589 return -1;
3590 }
3591
3592 /* handle out of order topology events */
3593 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
3594 if (parent_handle >= ioc->sas_hba.num_phys) {
3595 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3596 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3597 parent_handle);
3598 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3599 if (!sas_expander) {
3600 rc = _scsih_expander_add(ioc, parent_handle);
3601 if (rc != 0)
3602 return rc;
3603 }
3604 }
3605
635374e7 3606 spin_lock_irqsave(&ioc->sas_node_lock, flags);
595bb0bd 3607 sas_address = le64_to_cpu(expander_pg0.SASAddress);
635374e7
EM
3608 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3609 sas_address);
3610 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3611
3612 if (sas_expander)
3613 return 0;
3614
3615 sas_expander = kzalloc(sizeof(struct _sas_node),
3616 GFP_KERNEL);
3617 if (!sas_expander) {
3618 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3619 ioc->name, __FILE__, __LINE__, __func__);
3620 return -1;
3621 }
3622
3623 sas_expander->handle = handle;
3624 sas_expander->num_phys = expander_pg0.NumPhys;
3625 sas_expander->parent_handle = parent_handle;
3626 sas_expander->enclosure_handle =
3627 le16_to_cpu(expander_pg0.EnclosureHandle);
3628 sas_expander->sas_address = sas_address;
3629
3630 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3631 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
3632 handle, sas_expander->parent_handle, (unsigned long long)
3633 sas_expander->sas_address, sas_expander->num_phys);
3634
3635 if (!sas_expander->num_phys)
3636 goto out_fail;
3637 sas_expander->phy = kcalloc(sas_expander->num_phys,
3638 sizeof(struct _sas_phy), GFP_KERNEL);
3639 if (!sas_expander->phy) {
3640 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3641 ioc->name, __FILE__, __LINE__, __func__);
3642 rc = -1;
3643 goto out_fail;
3644 }
3645
3646 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3647 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
3648 sas_expander->parent_handle);
3649 if (!mpt2sas_port) {
3650 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3651 ioc->name, __FILE__, __LINE__, __func__);
3652 rc = -1;
3653 goto out_fail;
3654 }
3655 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3656
3657 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3658 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3659 &expander_pg1, i, handle))) {
3660 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3661 ioc->name, __FILE__, __LINE__, __func__);
20f5895d
KD
3662 rc = -1;
3663 goto out_fail;
635374e7
EM
3664 }
3665 sas_expander->phy[i].handle = handle;
3666 sas_expander->phy[i].phy_id = i;
20f5895d
KD
3667
3668 if ((mpt2sas_transport_add_expander_phy(ioc,
3669 &sas_expander->phy[i], expander_pg1,
3670 sas_expander->parent_dev))) {
3671 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3672 ioc->name, __FILE__, __LINE__, __func__);
3673 rc = -1;
3674 goto out_fail;
3675 }
635374e7
EM
3676 }
3677
3678 if (sas_expander->enclosure_handle) {
3679 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3680 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3681 sas_expander->enclosure_handle))) {
3682 sas_expander->enclosure_logical_id =
3683 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3684 }
3685 }
3686
3687 _scsih_expander_node_add(ioc, sas_expander);
3688 return 0;
3689
3690 out_fail:
3691
20f5895d
KD
3692 if (mpt2sas_port)
3693 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
3694 sas_expander->parent_handle);
635374e7
EM
3695 kfree(sas_expander);
3696 return rc;
3697}
3698
3699/**
3700 * _scsih_expander_remove - removing expander object
3701 * @ioc: per adapter object
3702 * @handle: expander handle
3703 *
3704 * Return nothing.
3705 */
3706static void
3707_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3708{
3709 struct _sas_node *sas_expander;
3710 unsigned long flags;
3711
155dd4c7
KD
3712 if (ioc->shost_recovery)
3713 return;
3714
635374e7
EM
3715 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3716 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
3717 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3718 _scsih_expander_node_remove(ioc, sas_expander);
3719}
3720
3721/**
3722 * _scsih_add_device - creating sas device object
3723 * @ioc: per adapter object
3724 * @handle: sas device handle
3725 * @phy_num: phy number end device attached to
3726 * @is_pd: is this hidden raid component
3727 *
3728 * Creating end device object, stored in ioc->sas_device_list.
3729 *
3730 * Returns 0 for success, non-zero for failure.
3731 */
3732static int
3733_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3734{
3735 Mpi2ConfigReply_t mpi_reply;
3736 Mpi2SasDevicePage0_t sas_device_pg0;
3737 Mpi2SasEnclosurePage0_t enclosure_pg0;
3738 struct _sas_device *sas_device;
3739 u32 ioc_status;
3740 __le64 sas_address;
3741 u32 device_info;
3742 unsigned long flags;
3743
3744 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3745 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3746 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3747 ioc->name, __FILE__, __LINE__, __func__);
3748 return -1;
3749 }
3750
3751 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3752 MPI2_IOCSTATUS_MASK;
3753 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3754 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3755 ioc->name, __FILE__, __LINE__, __func__);
3756 return -1;
3757 }
3758
3759 /* check if device is present */
3760 if (!(le16_to_cpu(sas_device_pg0.Flags) &
3761 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3762 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3763 ioc->name, __FILE__, __LINE__, __func__);
3764 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3765 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3766 return -1;
3767 }
3768
3769 /* check if there were any issus with discovery */
3770 if (sas_device_pg0.AccessStatus ==
3771 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3772 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3773 ioc->name, __FILE__, __LINE__, __func__);
3774 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3775 ioc->name, sas_device_pg0.AccessStatus);
3776 return -1;
3777 }
3778
3779 /* check if this is end device */
3780 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3781 if (!(_scsih_is_end_device(device_info))) {
3782 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3783 ioc->name, __FILE__, __LINE__, __func__);
3784 return -1;
3785 }
3786
3787 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3788
3789 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3790 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3791 sas_address);
3792 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3793
3794 if (sas_device) {
3795 _scsih_ublock_io_device(ioc, handle);
3796 return 0;
3797 }
3798
3799 sas_device = kzalloc(sizeof(struct _sas_device),
3800 GFP_KERNEL);
3801 if (!sas_device) {
3802 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3803 ioc->name, __FILE__, __LINE__, __func__);
3804 return -1;
3805 }
3806
3807 sas_device->handle = handle;
3808 sas_device->parent_handle =
3809 le16_to_cpu(sas_device_pg0.ParentDevHandle);
3810 sas_device->enclosure_handle =
3811 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3812 sas_device->slot =
3813 le16_to_cpu(sas_device_pg0.Slot);
3814 sas_device->device_info = device_info;
3815 sas_device->sas_address = sas_address;
3816 sas_device->hidden_raid_component = is_pd;
3817
3818 /* get enclosure_logical_id */
15052c9e
KD
3819 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
3820 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3821 sas_device->enclosure_handle)))
635374e7
EM
3822 sas_device->enclosure_logical_id =
3823 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
635374e7
EM
3824
3825 /* get device name */
3826 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3827
3828 if (ioc->wait_for_port_enable_to_complete)
3829 _scsih_sas_device_init_add(ioc, sas_device);
3830 else
3831 _scsih_sas_device_add(ioc, sas_device);
3832
3833 return 0;
3834}
3835
3836/**
3837 * _scsih_remove_device - removing sas device object
3838 * @ioc: per adapter object
3839 * @handle: sas device handle
3840 *
3841 * Return nothing.
3842 */
3843static void
3844_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3845{
3846 struct MPT2SAS_TARGET *sas_target_priv_data;
3847 struct _sas_device *sas_device;
3848 unsigned long flags;
3849 Mpi2SasIoUnitControlReply_t mpi_reply;
3850 Mpi2SasIoUnitControlRequest_t mpi_request;
3851 u16 device_handle;
3852
3853 /* lookup sas_device */
3854 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3855 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3856 if (!sas_device) {
3857 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3858 return;
3859 }
3860
3861 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
3862 "(0x%04x)\n", ioc->name, __func__, handle));
3863
3864 if (sas_device->starget && sas_device->starget->hostdata) {
3865 sas_target_priv_data = sas_device->starget->hostdata;
3866 sas_target_priv_data->deleted = 1;
3867 }
3868 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3869
3870 if (ioc->remove_host)
3871 goto out;
3872
77e63ed4
KD
3873 if ((sas_device->state & MPTSAS_STATE_TR_COMPLETE)) {
3874 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
3875 "target_reset handle(0x%04x)\n", ioc->name, handle));
3876 goto skip_tr;
3877 }
3878
635374e7
EM
3879 /* Target Reset to flush out all the outstanding IO */
3880 device_handle = (sas_device->hidden_raid_component) ?
3881 sas_device->volume_handle : handle;
3882 if (device_handle) {
3883 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3884 "handle(0x%04x)\n", ioc->name, device_handle));
3885 mutex_lock(&ioc->tm_cmds.mutex);
3886 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3887 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3888 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3889 mutex_unlock(&ioc->tm_cmds.mutex);
3890 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3891 "done: handle(0x%04x)\n", ioc->name, device_handle));
155dd4c7
KD
3892 if (ioc->shost_recovery)
3893 goto out;
635374e7 3894 }
77e63ed4
KD
3895 skip_tr:
3896
3897 if ((sas_device->state & MPTSAS_STATE_CNTRL_COMPLETE)) {
3898 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "\tskip "
3899 "sas_cntrl handle(0x%04x)\n", ioc->name, handle));
3900 goto out;
3901 }
635374e7
EM
3902
3903 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
3904 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
3905 "(0x%04x)\n", ioc->name, handle));
3906 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3907 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3908 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3909 mpi_request.DevHandle = handle;
7b936b02
KD
3910 mpi_request.VF_ID = 0; /* TODO */
3911 mpi_request.VP_ID = 0;
635374e7
EM
3912 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
3913 &mpi_request)) != 0) {
3914 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3915 ioc->name, __FILE__, __LINE__, __func__);
3916 }
3917
3918 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
3919 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
3920 le16_to_cpu(mpi_reply.IOCStatus),
3921 le32_to_cpu(mpi_reply.IOCLogInfo)));
3922
3923 out:
34a03bef
KD
3924
3925 _scsih_ublock_io_device(ioc, handle);
3926
635374e7
EM
3927 mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
3928 sas_device->parent_handle);
3929
3930 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
3931 "(0x%016llx)\n", ioc->name, sas_device->handle,
3932 (unsigned long long) sas_device->sas_address);
3933 _scsih_sas_device_remove(ioc, sas_device);
3934
3935 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
3936 "(0x%04x)\n", ioc->name, __func__, handle));
3937}
3938
3939#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3940/**
3941 * _scsih_sas_topology_change_event_debug - debug for topology event
3942 * @ioc: per adapter object
3943 * @event_data: event data payload
3944 * Context: user.
3945 */
3946static void
3947_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3948 Mpi2EventDataSasTopologyChangeList_t *event_data)
3949{
3950 int i;
3951 u16 handle;
3952 u16 reason_code;
3953 u8 phy_number;
3954 char *status_str = NULL;
3955 char link_rate[25];
3956
3957 switch (event_data->ExpStatus) {
3958 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
3959 status_str = "add";
3960 break;
3961 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
3962 status_str = "remove";
3963 break;
3964 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
3965 status_str = "responding";
3966 break;
3967 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
3968 status_str = "remove delay";
3969 break;
3970 default:
3971 status_str = "unknown status";
3972 break;
3973 }
3974 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
3975 ioc->name, status_str);
3976 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
3977 "start_phy(%02d), count(%d)\n",
3978 le16_to_cpu(event_data->ExpanderDevHandle),
3979 le16_to_cpu(event_data->EnclosureHandle),
3980 event_data->StartPhyNum, event_data->NumEntries);
3981 for (i = 0; i < event_data->NumEntries; i++) {
3982 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3983 if (!handle)
3984 continue;
3985 phy_number = event_data->StartPhyNum + i;
3986 reason_code = event_data->PHY[i].PhyStatus &
3987 MPI2_EVENT_SAS_TOPO_RC_MASK;
3988 switch (reason_code) {
3989 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3990 snprintf(link_rate, 25, ": add, link(0x%02x)",
3991 (event_data->PHY[i].LinkRate >> 4));
3992 status_str = link_rate;
3993 break;
3994 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3995 status_str = ": remove";
3996 break;
3997 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
3998 status_str = ": remove_delay";
3999 break;
4000 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4001 snprintf(link_rate, 25, ": link(0x%02x)",
4002 (event_data->PHY[i].LinkRate >> 4));
4003 status_str = link_rate;
4004 break;
4005 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
4006 status_str = ": responding";
4007 break;
4008 default:
4009 status_str = ": unknown";
4010 break;
4011 }
4012 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
4013 phy_number, handle, status_str);
4014 }
4015}
4016#endif
4017
4018/**
4019 * _scsih_sas_topology_change_event - handle topology changes
4020 * @ioc: per adapter object
7b936b02 4021 * @fw_event: The fw_event_work object
635374e7
EM
4022 * Context: user.
4023 *
4024 */
4025static void
7b936b02 4026_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
635374e7
EM
4027 struct fw_event_work *fw_event)
4028{
4029 int i;
4030 u16 parent_handle, handle;
4031 u16 reason_code;
4032 u8 phy_number;
4033 struct _sas_node *sas_expander;
4034 unsigned long flags;
4035 u8 link_rate_;
7b936b02 4036 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
635374e7
EM
4037
4038#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4039 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4040 _scsih_sas_topology_change_event_debug(ioc, event_data);
4041#endif
4042
4043 if (!ioc->sas_hba.num_phys)
4044 _scsih_sas_host_add(ioc);
4045 else
4046 _scsih_sas_host_refresh(ioc, 0);
4047
4048 if (fw_event->ignore) {
4049 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
4050 "event\n", ioc->name));
4051 return;
4052 }
4053
4054 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4055
4056 /* handle expander add */
4057 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4058 if (_scsih_expander_add(ioc, parent_handle) != 0)
4059 return;
4060
4061 /* handle siblings events */
4062 for (i = 0; i < event_data->NumEntries; i++) {
4063 if (fw_event->ignore) {
4064 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
4065 "expander event\n", ioc->name));
4066 return;
4067 }
155dd4c7
KD
4068 if (ioc->shost_recovery)
4069 return;
308609c6
KD
4070 phy_number = event_data->StartPhyNum + i;
4071 reason_code = event_data->PHY[i].PhyStatus &
4072 MPI2_EVENT_SAS_TOPO_RC_MASK;
4073 if ((event_data->PHY[i].PhyStatus &
4074 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4075 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
635374e7
EM
4076 continue;
4077 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4078 if (!handle)
4079 continue;
635374e7
EM
4080 link_rate_ = event_data->PHY[i].LinkRate >> 4;
4081 switch (reason_code) {
4082 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
4083 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
4084 if (!parent_handle) {
4085 if (phy_number < ioc->sas_hba.num_phys)
cc0f5207
KD
4086 mpt2sas_transport_update_links(
4087 ioc,
4088 ioc->sas_hba.phy[phy_number].handle,
4089 handle, phy_number, link_rate_);
635374e7
EM
4090 } else {
4091 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4092 sas_expander =
4093 mpt2sas_scsih_expander_find_by_handle(ioc,
4094 parent_handle);
4095 spin_unlock_irqrestore(&ioc->sas_node_lock,
4096 flags);
4097 if (sas_expander) {
4098 if (phy_number < sas_expander->num_phys)
cc0f5207
KD
4099 mpt2sas_transport_update_links(
4100 ioc,
4101 sas_expander->
4102 phy[phy_number].handle,
4103 handle, phy_number,
4104 link_rate_);
635374e7 4105 }
635374e7
EM
4106 }
4107 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
4108 if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
4109 break;
4110 _scsih_add_device(ioc, handle, phy_number, 0);
4111 }
4112 break;
4113 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
4114 _scsih_remove_device(ioc, handle);
4115 break;
4116 }
4117 }
4118
4119 /* handle expander removal */
4120 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
4121 _scsih_expander_remove(ioc, parent_handle);
4122
4123}
4124
4125#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4126/**
4127 * _scsih_sas_device_status_change_event_debug - debug for device event
4128 * @event_data: event data payload
4129 * Context: user.
4130 *
4131 * Return nothing.
4132 */
4133static void
4134_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4135 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4136{
4137 char *reason_str = NULL;
4138
4139 switch (event_data->ReasonCode) {
4140 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4141 reason_str = "smart data";
4142 break;
4143 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4144 reason_str = "unsupported device discovered";
4145 break;
4146 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4147 reason_str = "internal device reset";
4148 break;
4149 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4150 reason_str = "internal task abort";
4151 break;
4152 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4153 reason_str = "internal task abort set";
4154 break;
4155 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4156 reason_str = "internal clear task set";
4157 break;
4158 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4159 reason_str = "internal query task";
4160 break;
4161 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4162 reason_str = "sata init failure";
4163 break;
4164 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4165 reason_str = "internal device reset complete";
4166 break;
4167 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4168 reason_str = "internal task abort complete";
4169 break;
4170 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4171 reason_str = "internal async notification";
4172 break;
4173 default:
4174 reason_str = "unknown reason";
4175 break;
4176 }
4177 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
4178 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4179 reason_str, le16_to_cpu(event_data->DevHandle),
4180 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4181 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
4182 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
4183 event_data->ASC, event_data->ASCQ);
4184 printk(KERN_INFO "\n");
4185}
4186#endif
4187
4188/**
4189 * _scsih_sas_device_status_change_event - handle device status change
4190 * @ioc: per adapter object
7b936b02 4191 * @fw_event: The fw_event_work object
635374e7
EM
4192 * Context: user.
4193 *
4194 * Return nothing.
4195 */
4196static void
7b936b02
KD
4197_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4198 struct fw_event_work *fw_event)
635374e7
EM
4199{
4200#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4201 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
7b936b02
KD
4202 _scsih_sas_device_status_change_event_debug(ioc,
4203 fw_event->event_data);
635374e7
EM
4204#endif
4205}
4206
4207#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4208/**
4209 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4210 * @ioc: per adapter object
4211 * @event_data: event data payload
4212 * Context: user.
4213 *
4214 * Return nothing.
4215 */
4216static void
4217_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4218 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4219{
4220 char *reason_str = NULL;
4221
4222 switch (event_data->ReasonCode) {
4223 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4224 reason_str = "enclosure add";
4225 break;
4226 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4227 reason_str = "enclosure remove";
4228 break;
4229 default:
4230 reason_str = "unknown reason";
4231 break;
4232 }
4233
4234 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4235 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4236 " number slots(%d)\n", ioc->name, reason_str,
4237 le16_to_cpu(event_data->EnclosureHandle),
4238 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4239 le16_to_cpu(event_data->StartSlot));
4240}
4241#endif
4242
4243/**
4244 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4245 * @ioc: per adapter object
7b936b02 4246 * @fw_event: The fw_event_work object
635374e7
EM
4247 * Context: user.
4248 *
4249 * Return nothing.
4250 */
4251static void
4252_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
7b936b02 4253 struct fw_event_work *fw_event)
635374e7
EM
4254{
4255#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4256 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4257 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
7b936b02 4258 fw_event->event_data);
635374e7
EM
4259#endif
4260}
4261
4262/**
4263 * _scsih_sas_broadcast_primative_event - handle broadcast events
4264 * @ioc: per adapter object
7b936b02 4265 * @fw_event: The fw_event_work object
635374e7
EM
4266 * Context: user.
4267 *
4268 * Return nothing.
4269 */
4270static void
7b936b02
KD
4271_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
4272 struct fw_event_work *fw_event)
635374e7
EM
4273{
4274 struct scsi_cmnd *scmd;
4275 u16 smid, handle;
4276 u32 lun;
4277 struct MPT2SAS_DEVICE *sas_device_priv_data;
4278 u32 termination_count;
4279 u32 query_count;
4280 Mpi2SCSITaskManagementReply_t *mpi_reply;
7b936b02
KD
4281#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4282 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
4283#endif
635374e7
EM
4284 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4285 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4286 event_data->PortWidth));
635374e7
EM
4287 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4288 __func__));
4289
4290 mutex_lock(&ioc->tm_cmds.mutex);
4291 termination_count = 0;
4292 query_count = 0;
4293 mpi_reply = ioc->tm_cmds.reply;
595bb0bd 4294 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
635374e7
EM
4295 scmd = _scsih_scsi_lookup_get(ioc, smid);
4296 if (!scmd)
4297 continue;
4298 sas_device_priv_data = scmd->device->hostdata;
4299 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4300 continue;
4301 /* skip hidden raid components */
4302 if (sas_device_priv_data->sas_target->flags &
4303 MPT_TARGET_FLAGS_RAID_COMPONENT)
4304 continue;
4305 /* skip volumes */
4306 if (sas_device_priv_data->sas_target->flags &
4307 MPT_TARGET_FLAGS_VOLUME)
4308 continue;
4309
4310 handle = sas_device_priv_data->sas_target->handle;
4311 lun = sas_device_priv_data->lun;
4312 query_count++;
4313
4314 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4315 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
8901cbb4 4316 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
635374e7
EM
4317
4318 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
4319 (mpi_reply->ResponseCode ==
4320 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4321 mpi_reply->ResponseCode ==
4322 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4323 continue;
4324
4325 mpt2sas_scsih_issue_tm(ioc, handle, lun,
8901cbb4
EM
4326 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4327 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
635374e7
EM
4328 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4329 }
635374e7
EM
4330 ioc->broadcast_aen_busy = 0;
4331 mutex_unlock(&ioc->tm_cmds.mutex);
4332
4333 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4334 "%s - exit, query_count = %d termination_count = %d\n",
4335 ioc->name, __func__, query_count, termination_count));
4336}
4337
4338/**
4339 * _scsih_sas_discovery_event - handle discovery events
4340 * @ioc: per adapter object
7b936b02 4341 * @fw_event: The fw_event_work object
635374e7
EM
4342 * Context: user.
4343 *
4344 * Return nothing.
4345 */
4346static void
7b936b02
KD
4347_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
4348 struct fw_event_work *fw_event)
635374e7 4349{
7b936b02
KD
4350 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
4351
635374e7
EM
4352#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4353 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4354 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4355 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4356 "start" : "stop");
4357 if (event_data->DiscoveryStatus)
595bb0bd
KD
4358 printk("discovery_status(0x%08x)",
4359 le32_to_cpu(event_data->DiscoveryStatus));
635374e7
EM
4360 printk("\n");
4361 }
4362#endif
4363
4364 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4365 !ioc->sas_hba.num_phys)
4366 _scsih_sas_host_add(ioc);
4367}
4368
4369/**
4370 * _scsih_reprobe_lun - reprobing lun
4371 * @sdev: scsi device struct
4372 * @no_uld_attach: sdev->no_uld_attach flag setting
4373 *
4374 **/
4375static void
4376_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4377{
4378 int rc;
4379
4380 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4381 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4382 sdev->no_uld_attach ? "hidding" : "exposing");
4383 rc = scsi_device_reprobe(sdev);
4384}
4385
4386/**
4387 * _scsih_reprobe_target - reprobing target
4388 * @starget: scsi target struct
4389 * @no_uld_attach: sdev->no_uld_attach flag setting
4390 *
4391 * Note: no_uld_attach flag determines whether the disk device is attached
4392 * to block layer. A value of `1` means to not attach.
4393 **/
4394static void
4395_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4396{
4397 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4398
4399 if (no_uld_attach)
4400 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4401 else
4402 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4403
4404 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4405 _scsih_reprobe_lun);
4406}
4407/**
4408 * _scsih_sas_volume_add - add new volume
4409 * @ioc: per adapter object
4410 * @element: IR config element data
4411 * Context: user.
4412 *
4413 * Return nothing.
4414 */
4415static void
4416_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4417 Mpi2EventIrConfigElement_t *element)
4418{
4419 struct _raid_device *raid_device;
4420 unsigned long flags;
4421 u64 wwid;
4422 u16 handle = le16_to_cpu(element->VolDevHandle);
4423 int rc;
4424
635374e7
EM
4425 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4426 if (!wwid) {
4427 printk(MPT2SAS_ERR_FMT
4428 "failure at %s:%d/%s()!\n", ioc->name,
4429 __FILE__, __LINE__, __func__);
4430 return;
4431 }
4432
4433 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4434 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4435 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4436
4437 if (raid_device)
4438 return;
4439
4440 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4441 if (!raid_device) {
4442 printk(MPT2SAS_ERR_FMT
4443 "failure at %s:%d/%s()!\n", ioc->name,
4444 __FILE__, __LINE__, __func__);
4445 return;
4446 }
4447
4448 raid_device->id = ioc->sas_id++;
4449 raid_device->channel = RAID_CHANNEL;
4450 raid_device->handle = handle;
4451 raid_device->wwid = wwid;
4452 _scsih_raid_device_add(ioc, raid_device);
4453 if (!ioc->wait_for_port_enable_to_complete) {
4454 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4455 raid_device->id, 0);
4456 if (rc)
4457 _scsih_raid_device_remove(ioc, raid_device);
4458 } else
4459 _scsih_determine_boot_device(ioc, raid_device, 1);
4460}
4461
4462/**
4463 * _scsih_sas_volume_delete - delete volume
4464 * @ioc: per adapter object
4465 * @element: IR config element data
4466 * Context: user.
4467 *
4468 * Return nothing.
4469 */
4470static void
4471_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4472 Mpi2EventIrConfigElement_t *element)
4473{
4474 struct _raid_device *raid_device;
4475 u16 handle = le16_to_cpu(element->VolDevHandle);
4476 unsigned long flags;
4477 struct MPT2SAS_TARGET *sas_target_priv_data;
4478
635374e7
EM
4479 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4480 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4481 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4482 if (!raid_device)
4483 return;
4484 if (raid_device->starget) {
4485 sas_target_priv_data = raid_device->starget->hostdata;
4486 sas_target_priv_data->deleted = 1;
4487 scsi_remove_target(&raid_device->starget->dev);
4488 }
4489 _scsih_raid_device_remove(ioc, raid_device);
4490}
4491
4492/**
4493 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4494 * @ioc: per adapter object
4495 * @element: IR config element data
4496 * Context: user.
4497 *
4498 * Return nothing.
4499 */
4500static void
4501_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4502 Mpi2EventIrConfigElement_t *element)
4503{
4504 struct _sas_device *sas_device;
4505 unsigned long flags;
4506 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4507
4508 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4509 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4510 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4511 if (!sas_device)
4512 return;
4513
4514 /* exposing raid component */
4515 sas_device->volume_handle = 0;
4516 sas_device->volume_wwid = 0;
4517 sas_device->hidden_raid_component = 0;
4518 _scsih_reprobe_target(sas_device->starget, 0);
4519}
4520
4521/**
4522 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4523 * @ioc: per adapter object
4524 * @element: IR config element data
4525 * Context: user.
4526 *
4527 * Return nothing.
4528 */
4529static void
4530_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4531 Mpi2EventIrConfigElement_t *element)
4532{
4533 struct _sas_device *sas_device;
4534 unsigned long flags;
4535 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4536
4537 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4538 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4539 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4540 if (!sas_device)
4541 return;
4542
4543 /* hiding raid component */
4544 mpt2sas_config_get_volume_handle(ioc, handle,
4545 &sas_device->volume_handle);
4546 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4547 &sas_device->volume_wwid);
4548 sas_device->hidden_raid_component = 1;
4549 _scsih_reprobe_target(sas_device->starget, 1);
4550}
4551
4552/**
4553 * _scsih_sas_pd_delete - delete pd component
4554 * @ioc: per adapter object
4555 * @element: IR config element data
4556 * Context: user.
4557 *
4558 * Return nothing.
4559 */
4560static void
4561_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4562 Mpi2EventIrConfigElement_t *element)
4563{
4564 struct _sas_device *sas_device;
4565 unsigned long flags;
4566 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4567
4568 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4569 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4570 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4571 if (!sas_device)
4572 return;
4573 _scsih_remove_device(ioc, handle);
4574}
4575
4576/**
4577 * _scsih_sas_pd_add - remove pd component
4578 * @ioc: per adapter object
4579 * @element: IR config element data
4580 * Context: user.
4581 *
4582 * Return nothing.
4583 */
4584static void
4585_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4586 Mpi2EventIrConfigElement_t *element)
4587{
4588 struct _sas_device *sas_device;
4589 unsigned long flags;
4590 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
62727a7b
KD
4591 Mpi2ConfigReply_t mpi_reply;
4592 Mpi2SasDevicePage0_t sas_device_pg0;
4593 u32 ioc_status;
635374e7
EM
4594
4595 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4596 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4597 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
62727a7b 4598 if (sas_device) {
635374e7 4599 sas_device->hidden_raid_component = 1;
62727a7b
KD
4600 return;
4601 }
4602
4603 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4604 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4605 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4606 ioc->name, __FILE__, __LINE__, __func__);
4607 return;
4608 }
4609
4610 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4611 MPI2_IOCSTATUS_MASK;
4612 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4613 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4614 ioc->name, __FILE__, __LINE__, __func__);
4615 return;
4616 }
4617
cc0f5207 4618 mpt2sas_transport_update_links(ioc,
62727a7b
KD
4619 le16_to_cpu(sas_device_pg0.ParentDevHandle),
4620 handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4621
4622 _scsih_add_device(ioc, handle, 0, 1);
635374e7
EM
4623}
4624
4625#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4626/**
4627 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4628 * @ioc: per adapter object
4629 * @event_data: event data payload
4630 * Context: user.
4631 *
4632 * Return nothing.
4633 */
4634static void
4635_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4636 Mpi2EventDataIrConfigChangeList_t *event_data)
4637{
4638 Mpi2EventIrConfigElement_t *element;
4639 u8 element_type;
4640 int i;
4641 char *reason_str = NULL, *element_str = NULL;
4642
4643 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4644
4645 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4646 ioc->name, (le32_to_cpu(event_data->Flags) &
4647 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4648 "foreign" : "native", event_data->NumElements);
4649 for (i = 0; i < event_data->NumElements; i++, element++) {
4650 switch (element->ReasonCode) {
4651 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4652 reason_str = "add";
4653 break;
4654 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4655 reason_str = "remove";
4656 break;
4657 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4658 reason_str = "no change";
4659 break;
4660 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4661 reason_str = "hide";
4662 break;
4663 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4664 reason_str = "unhide";
4665 break;
4666 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4667 reason_str = "volume_created";
4668 break;
4669 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4670 reason_str = "volume_deleted";
4671 break;
4672 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4673 reason_str = "pd_created";
4674 break;
4675 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4676 reason_str = "pd_deleted";
4677 break;
4678 default:
4679 reason_str = "unknown reason";
4680 break;
4681 }
4682 element_type = le16_to_cpu(element->ElementFlags) &
4683 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4684 switch (element_type) {
4685 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4686 element_str = "volume";
4687 break;
4688 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4689 element_str = "phys disk";
4690 break;
4691 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4692 element_str = "hot spare";
4693 break;
4694 default:
4695 element_str = "unknown element";
4696 break;
4697 }
4698 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4699 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4700 reason_str, le16_to_cpu(element->VolDevHandle),
4701 le16_to_cpu(element->PhysDiskDevHandle),
4702 element->PhysDiskNum);
4703 }
4704}
4705#endif
4706
4707/**
4708 * _scsih_sas_ir_config_change_event - handle ir configuration change events
4709 * @ioc: per adapter object
7b936b02 4710 * @fw_event: The fw_event_work object
635374e7
EM
4711 * Context: user.
4712 *
4713 * Return nothing.
4714 */
4715static void
7b936b02
KD
4716_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
4717 struct fw_event_work *fw_event)
635374e7
EM
4718{
4719 Mpi2EventIrConfigElement_t *element;
4720 int i;
62727a7b 4721 u8 foreign_config;
7b936b02 4722 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
635374e7
EM
4723
4724#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4725 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4726 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4727
4728#endif
62727a7b
KD
4729 foreign_config = (le32_to_cpu(event_data->Flags) &
4730 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
635374e7
EM
4731
4732 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4733 for (i = 0; i < event_data->NumElements; i++, element++) {
4734
4735 switch (element->ReasonCode) {
4736 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4737 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
62727a7b
KD
4738 if (!foreign_config)
4739 _scsih_sas_volume_add(ioc, element);
635374e7
EM
4740 break;
4741 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4742 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
62727a7b
KD
4743 if (!foreign_config)
4744 _scsih_sas_volume_delete(ioc, element);
635374e7
EM
4745 break;
4746 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4747 _scsih_sas_pd_hide(ioc, element);
4748 break;
4749 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4750 _scsih_sas_pd_expose(ioc, element);
4751 break;
4752 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4753 _scsih_sas_pd_add(ioc, element);
4754 break;
4755 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4756 _scsih_sas_pd_delete(ioc, element);
4757 break;
4758 }
4759 }
4760}
4761
4762/**
4763 * _scsih_sas_ir_volume_event - IR volume event
4764 * @ioc: per adapter object
7b936b02 4765 * @fw_event: The fw_event_work object
635374e7
EM
4766 * Context: user.
4767 *
4768 * Return nothing.
4769 */
4770static void
7b936b02
KD
4771_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
4772 struct fw_event_work *fw_event)
635374e7
EM
4773{
4774 u64 wwid;
4775 unsigned long flags;
4776 struct _raid_device *raid_device;
4777 u16 handle;
4778 u32 state;
4779 int rc;
4780 struct MPT2SAS_TARGET *sas_target_priv_data;
7b936b02 4781 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
635374e7
EM
4782
4783 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4784 return;
4785
4786 handle = le16_to_cpu(event_data->VolDevHandle);
4787 state = le32_to_cpu(event_data->NewValue);
4788 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4789 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4790 le32_to_cpu(event_data->PreviousValue), state));
4791
4792 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4793 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4794 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4795
4796 switch (state) {
4797 case MPI2_RAID_VOL_STATE_MISSING:
4798 case MPI2_RAID_VOL_STATE_FAILED:
4799 if (!raid_device)
4800 break;
4801 if (raid_device->starget) {
4802 sas_target_priv_data = raid_device->starget->hostdata;
4803 sas_target_priv_data->deleted = 1;
4804 scsi_remove_target(&raid_device->starget->dev);
4805 }
4806 _scsih_raid_device_remove(ioc, raid_device);
4807 break;
4808
4809 case MPI2_RAID_VOL_STATE_ONLINE:
4810 case MPI2_RAID_VOL_STATE_DEGRADED:
4811 case MPI2_RAID_VOL_STATE_OPTIMAL:
4812 if (raid_device)
4813 break;
4814
4815 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4816 if (!wwid) {
4817 printk(MPT2SAS_ERR_FMT
4818 "failure at %s:%d/%s()!\n", ioc->name,
4819 __FILE__, __LINE__, __func__);
4820 break;
4821 }
4822
4823 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4824 if (!raid_device) {
4825 printk(MPT2SAS_ERR_FMT
4826 "failure at %s:%d/%s()!\n", ioc->name,
4827 __FILE__, __LINE__, __func__);
4828 break;
4829 }
4830
4831 raid_device->id = ioc->sas_id++;
4832 raid_device->channel = RAID_CHANNEL;
4833 raid_device->handle = handle;
4834 raid_device->wwid = wwid;
4835 _scsih_raid_device_add(ioc, raid_device);
4836 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4837 raid_device->id, 0);
4838 if (rc)
4839 _scsih_raid_device_remove(ioc, raid_device);
4840 break;
4841
4842 case MPI2_RAID_VOL_STATE_INITIALIZING:
4843 default:
4844 break;
4845 }
4846}
4847
4848/**
4849 * _scsih_sas_ir_physical_disk_event - PD event
4850 * @ioc: per adapter object
7b936b02 4851 * @fw_event: The fw_event_work object
635374e7
EM
4852 * Context: user.
4853 *
4854 * Return nothing.
4855 */
4856static void
7b936b02
KD
4857_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
4858 struct fw_event_work *fw_event)
635374e7
EM
4859{
4860 u16 handle;
4861 u32 state;
4862 struct _sas_device *sas_device;
4863 unsigned long flags;
62727a7b
KD
4864 Mpi2ConfigReply_t mpi_reply;
4865 Mpi2SasDevicePage0_t sas_device_pg0;
4866 u32 ioc_status;
7b936b02 4867 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
635374e7
EM
4868
4869 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4870 return;
4871
4872 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4873 state = le32_to_cpu(event_data->NewValue);
4874
4875 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4876 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4877 le32_to_cpu(event_data->PreviousValue), state));
4878
4879 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4880 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4881 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4882
4883 switch (state) {
635374e7
EM
4884 case MPI2_RAID_PD_STATE_ONLINE:
4885 case MPI2_RAID_PD_STATE_DEGRADED:
4886 case MPI2_RAID_PD_STATE_REBUILDING:
4887 case MPI2_RAID_PD_STATE_OPTIMAL:
62727a7b 4888 if (sas_device) {
635374e7 4889 sas_device->hidden_raid_component = 1;
62727a7b
KD
4890 return;
4891 }
4892
4893 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4894 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
4895 handle))) {
4896 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4897 ioc->name, __FILE__, __LINE__, __func__);
4898 return;
4899 }
4900
4901 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4902 MPI2_IOCSTATUS_MASK;
4903 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4904 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4905 ioc->name, __FILE__, __LINE__, __func__);
4906 return;
4907 }
4908
cc0f5207 4909 mpt2sas_transport_update_links(ioc,
62727a7b
KD
4910 le16_to_cpu(sas_device_pg0.ParentDevHandle),
4911 handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4912
4913 _scsih_add_device(ioc, handle, 0, 1);
4914
635374e7
EM
4915 break;
4916
62727a7b 4917 case MPI2_RAID_PD_STATE_OFFLINE:
635374e7
EM
4918 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
4919 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
4920 case MPI2_RAID_PD_STATE_HOT_SPARE:
4921 default:
4922 break;
4923 }
4924}
4925
4926#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4927/**
4928 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
4929 * @ioc: per adapter object
4930 * @event_data: event data payload
4931 * Context: user.
4932 *
4933 * Return nothing.
4934 */
4935static void
4936_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
4937 Mpi2EventDataIrOperationStatus_t *event_data)
4938{
4939 char *reason_str = NULL;
4940
4941 switch (event_data->RAIDOperation) {
4942 case MPI2_EVENT_IR_RAIDOP_RESYNC:
4943 reason_str = "resync";
4944 break;
4945 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
4946 reason_str = "online capacity expansion";
4947 break;
4948 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
4949 reason_str = "consistency check";
4950 break;
4951 default:
4952 reason_str = "unknown reason";
4953 break;
4954 }
4955
4956 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
4957 "\thandle(0x%04x), percent complete(%d)\n",
4958 ioc->name, reason_str,
4959 le16_to_cpu(event_data->VolDevHandle),
4960 event_data->PercentComplete);
4961}
4962#endif
4963
4964/**
4965 * _scsih_sas_ir_operation_status_event - handle RAID operation events
4966 * @ioc: per adapter object
7b936b02 4967 * @fw_event: The fw_event_work object
635374e7
EM
4968 * Context: user.
4969 *
4970 * Return nothing.
4971 */
4972static void
7b936b02
KD
4973_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
4974 struct fw_event_work *fw_event)
635374e7
EM
4975{
4976#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4977 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
7b936b02
KD
4978 _scsih_sas_ir_operation_status_event_debug(ioc,
4979 fw_event->event_data);
635374e7
EM
4980#endif
4981}
4982
4983/**
4984 * _scsih_task_set_full - handle task set full
4985 * @ioc: per adapter object
7b936b02 4986 * @fw_event: The fw_event_work object
635374e7
EM
4987 * Context: user.
4988 *
4989 * Throttle back qdepth.
4990 */
4991static void
7b936b02
KD
4992_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
4993 *fw_event)
635374e7
EM
4994{
4995 unsigned long flags;
4996 struct _sas_device *sas_device;
4997 static struct _raid_device *raid_device;
4998 struct scsi_device *sdev;
4999 int depth;
5000 u16 current_depth;
5001 u16 handle;
5002 int id, channel;
5003 u64 sas_address;
7b936b02 5004 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
635374e7
EM
5005
5006 current_depth = le16_to_cpu(event_data->CurrentDepth);
5007 handle = le16_to_cpu(event_data->DevHandle);
5008 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5009 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5010 if (!sas_device) {
5011 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5012 return;
5013 }
5014 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5015 id = sas_device->id;
5016 channel = sas_device->channel;
5017 sas_address = sas_device->sas_address;
5018
5019 /* if hidden raid component, then change to volume characteristics */
5020 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
5021 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5022 raid_device = _scsih_raid_device_find_by_handle(
5023 ioc, sas_device->volume_handle);
5024 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5025 if (raid_device) {
5026 id = raid_device->id;
5027 channel = raid_device->channel;
5028 handle = raid_device->handle;
5029 sas_address = raid_device->wwid;
5030 }
5031 }
5032
5033 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
5034 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
5035 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5036 handle, (unsigned long long)sas_address, current_depth);
5037
5038 shost_for_each_device(sdev, ioc->shost) {
5039 if (sdev->id == id && sdev->channel == channel) {
5040 if (current_depth > sdev->queue_depth) {
5041 if (ioc->logging_level &
5042 MPT_DEBUG_TASK_SET_FULL)
5043 sdev_printk(KERN_INFO, sdev, "strange "
5044 "observation, the queue depth is"
5045 " (%d) meanwhile fw queue depth "
5046 "is (%d)\n", sdev->queue_depth,
5047 current_depth);
5048 continue;
5049 }
5050 depth = scsi_track_queue_full(sdev,
5051 current_depth - 1);
5052 if (depth > 0)
5053 sdev_printk(KERN_INFO, sdev, "Queue depth "
5054 "reduced to (%d)\n", depth);
5055 else if (depth < 0)
5056 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5057 "Queueing is being disabled\n");
5058 else if (depth == 0)
5059 if (ioc->logging_level &
5060 MPT_DEBUG_TASK_SET_FULL)
5061 sdev_printk(KERN_INFO, sdev,
5062 "Queue depth not changed yet\n");
5063 }
5064 }
5065}
5066
5067/**
5068 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5069 * @ioc: per adapter object
5070 * @sas_address: sas address
5071 * @slot: enclosure slot id
5072 * @handle: device handle
5073 *
5074 * After host reset, find out whether devices are still responding.
5075 * Used in _scsi_remove_unresponsive_sas_devices.
5076 *
5077 * Return nothing.
5078 */
5079static void
5080_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5081 u16 slot, u16 handle)
5082{
5083 struct MPT2SAS_TARGET *sas_target_priv_data;
5084 struct scsi_target *starget;
5085 struct _sas_device *sas_device;
5086 unsigned long flags;
5087
5088 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5089 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5090 if (sas_device->sas_address == sas_address &&
5091 sas_device->slot == slot && sas_device->starget) {
5092 sas_device->responding = 1;
77e63ed4
KD
5093 sas_device->state = 0;
5094 starget = sas_device->starget;
5095 sas_target_priv_data = starget->hostdata;
5096 sas_target_priv_data->tm_busy = 0;
635374e7
EM
5097 starget_printk(KERN_INFO, sas_device->starget,
5098 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5099 "logical id(0x%016llx), slot(%d)\n", handle,
5100 (unsigned long long)sas_device->sas_address,
5101 (unsigned long long)
5102 sas_device->enclosure_logical_id,
5103 sas_device->slot);
5104 if (sas_device->handle == handle)
5105 goto out;
5106 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5107 sas_device->handle);
5108 sas_device->handle = handle;
635374e7
EM
5109 sas_target_priv_data->handle = handle;
5110 goto out;
5111 }
5112 }
5113 out:
5114 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5115}
5116
5117/**
5118 * _scsih_search_responding_sas_devices -
5119 * @ioc: per adapter object
5120 *
5121 * After host reset, find out whether devices are still responding.
5122 * If not remove.
5123 *
5124 * Return nothing.
5125 */
5126static void
5127_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5128{
5129 Mpi2SasDevicePage0_t sas_device_pg0;
5130 Mpi2ConfigReply_t mpi_reply;
5131 u16 ioc_status;
5132 __le64 sas_address;
5133 u16 handle;
5134 u32 device_info;
5135 u16 slot;
5136
5137 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5138
5139 if (list_empty(&ioc->sas_device_list))
5140 return;
5141
5142 handle = 0xFFFF;
5143 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5144 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5145 handle))) {
5146 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5147 MPI2_IOCSTATUS_MASK;
5148 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5149 break;
5150 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5151 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5152 if (!(_scsih_is_end_device(device_info)))
5153 continue;
5154 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5155 slot = le16_to_cpu(sas_device_pg0.Slot);
5156 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5157 handle);
5158 }
5159}
5160
5161/**
5162 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5163 * @ioc: per adapter object
5164 * @wwid: world wide identifier for raid volume
5165 * @handle: device handle
5166 *
5167 * After host reset, find out whether devices are still responding.
5168 * Used in _scsi_remove_unresponsive_raid_devices.
5169 *
5170 * Return nothing.
5171 */
5172static void
5173_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5174 u16 handle)
5175{
5176 struct MPT2SAS_TARGET *sas_target_priv_data;
5177 struct scsi_target *starget;
5178 struct _raid_device *raid_device;
5179 unsigned long flags;
5180
5181 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5182 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5183 if (raid_device->wwid == wwid && raid_device->starget) {
5184 raid_device->responding = 1;
5185 starget_printk(KERN_INFO, raid_device->starget,
5186 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5187 (unsigned long long)raid_device->wwid);
5188 if (raid_device->handle == handle)
5189 goto out;
5190 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5191 raid_device->handle);
5192 raid_device->handle = handle;
5193 starget = raid_device->starget;
5194 sas_target_priv_data = starget->hostdata;
5195 sas_target_priv_data->handle = handle;
5196 goto out;
5197 }
5198 }
5199 out:
5200 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5201}
5202
5203/**
5204 * _scsih_search_responding_raid_devices -
5205 * @ioc: per adapter object
5206 *
5207 * After host reset, find out whether devices are still responding.
5208 * If not remove.
5209 *
5210 * Return nothing.
5211 */
5212static void
5213_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
5214{
5215 Mpi2RaidVolPage1_t volume_pg1;
5216 Mpi2ConfigReply_t mpi_reply;
5217 u16 ioc_status;
5218 u16 handle;
5219
5220 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5221
5222 if (list_empty(&ioc->raid_device_list))
5223 return;
5224
5225 handle = 0xFFFF;
5226 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5227 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5228 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5229 MPI2_IOCSTATUS_MASK;
5230 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5231 break;
5232 handle = le16_to_cpu(volume_pg1.DevHandle);
5233 _scsih_mark_responding_raid_device(ioc,
5234 le64_to_cpu(volume_pg1.WWID), handle);
5235 }
5236}
5237
5238/**
5239 * _scsih_mark_responding_expander - mark a expander as responding
5240 * @ioc: per adapter object
5241 * @sas_address: sas address
5242 * @handle:
5243 *
5244 * After host reset, find out whether devices are still responding.
5245 * Used in _scsi_remove_unresponsive_expanders.
5246 *
5247 * Return nothing.
5248 */
5249static void
5250_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5251 u16 handle)
5252{
5253 struct _sas_node *sas_expander;
5254 unsigned long flags;
5255
5256 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5257 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
5258 if (sas_expander->sas_address == sas_address) {
5259 sas_expander->responding = 1;
5260 if (sas_expander->handle != handle) {
5261 printk(KERN_INFO "old handle(0x%04x)\n",
5262 sas_expander->handle);
5263 sas_expander->handle = handle;
5264 }
5265 goto out;
5266 }
5267 }
5268 out:
5269 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5270}
5271
5272/**
5273 * _scsih_search_responding_expanders -
5274 * @ioc: per adapter object
5275 *
5276 * After host reset, find out whether devices are still responding.
5277 * If not remove.
5278 *
5279 * Return nothing.
5280 */
5281static void
5282_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5283{
5284 Mpi2ExpanderPage0_t expander_pg0;
5285 Mpi2ConfigReply_t mpi_reply;
5286 u16 ioc_status;
5287 __le64 sas_address;
5288 u16 handle;
5289
5290 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5291
5292 if (list_empty(&ioc->sas_expander_list))
5293 return;
5294
5295 handle = 0xFFFF;
5296 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5297 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5298
5299 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5300 MPI2_IOCSTATUS_MASK;
5301 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5302 break;
5303
5304 handle = le16_to_cpu(expander_pg0.DevHandle);
5305 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5306 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5307 "sas_addr(0x%016llx)\n", handle,
5308 (unsigned long long)sas_address);
5309 _scsih_mark_responding_expander(ioc, sas_address, handle);
5310 }
5311
5312}
5313
5314/**
5315 * _scsih_remove_unresponding_devices - removing unresponding devices
5316 * @ioc: per adapter object
5317 *
5318 * Return nothing.
5319 */
5320static void
5321_scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5322{
5323 struct _sas_device *sas_device, *sas_device_next;
cd4e12e8 5324 struct _sas_node *sas_expander;
635374e7 5325 struct _raid_device *raid_device, *raid_device_next;
635374e7 5326
635374e7
EM
5327
5328 list_for_each_entry_safe(sas_device, sas_device_next,
5329 &ioc->sas_device_list, list) {
5330 if (sas_device->responding) {
5331 sas_device->responding = 0;
5332 continue;
5333 }
5334 if (sas_device->starget)
5335 starget_printk(KERN_INFO, sas_device->starget,
5336 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5337 "enclosure logical id(0x%016llx), slot(%d)\n",
5338 sas_device->handle,
5339 (unsigned long long)sas_device->sas_address,
5340 (unsigned long long)
5341 sas_device->enclosure_logical_id,
5342 sas_device->slot);
5343 _scsih_remove_device(ioc, sas_device->handle);
5344 }
5345
5346 list_for_each_entry_safe(raid_device, raid_device_next,
5347 &ioc->raid_device_list, list) {
5348 if (raid_device->responding) {
5349 raid_device->responding = 0;
5350 continue;
5351 }
5352 if (raid_device->starget) {
5353 starget_printk(KERN_INFO, raid_device->starget,
5354 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5355 raid_device->handle,
5356 (unsigned long long)raid_device->wwid);
5357 scsi_remove_target(&raid_device->starget->dev);
5358 }
5359 _scsih_raid_device_remove(ioc, raid_device);
5360 }
5361
cd4e12e8
KD
5362 retry_expander_search:
5363 sas_expander = NULL;
5364 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
635374e7
EM
5365 if (sas_expander->responding) {
5366 sas_expander->responding = 0;
5367 continue;
5368 }
635374e7 5369 _scsih_expander_remove(ioc, sas_expander->handle);
cd4e12e8
KD
5370 goto retry_expander_search;
5371 }
5372}
5373
5374/**
5375 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5376 * @ioc: per adapter object
5377 * @reset_phase: phase
5378 *
5379 * The handler for doing any required cleanup or initialization.
5380 *
5381 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5382 * MPT2_IOC_DONE_RESET
5383 *
5384 * Return nothing.
5385 */
5386void
5387mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5388{
5389 switch (reset_phase) {
5390 case MPT2_IOC_PRE_RESET:
5391 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5392 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
5393 _scsih_fw_event_off(ioc);
5394 break;
5395 case MPT2_IOC_AFTER_RESET:
5396 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5397 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
5398 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5399 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5400 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5401 complete(&ioc->tm_cmds.done);
5402 }
5403 _scsih_fw_event_on(ioc);
5404 _scsih_flush_running_cmds(ioc);
5405 break;
5406 case MPT2_IOC_DONE_RESET:
5407 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5408 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
5409 _scsih_sas_host_refresh(ioc, 0);
5410 _scsih_search_responding_sas_devices(ioc);
5411 _scsih_search_responding_raid_devices(ioc);
5412 _scsih_search_responding_expanders(ioc);
5413 break;
5414 case MPT2_IOC_RUNNING:
5415 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5416 "MPT2_IOC_RUNNING\n", ioc->name, __func__));
5417 _scsih_remove_unresponding_devices(ioc);
5418 break;
635374e7
EM
5419 }
5420}
5421
5422/**
5423 * _firmware_event_work - delayed task for processing firmware events
5424 * @ioc: per adapter object
5425 * @work: equal to the fw_event_work object
5426 * Context: user.
5427 *
5428 * Return nothing.
5429 */
5430static void
5431_firmware_event_work(struct work_struct *work)
5432{
5433 struct fw_event_work *fw_event = container_of(work,
6f92a7a0 5434 struct fw_event_work, work);
635374e7
EM
5435 unsigned long flags;
5436 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5437
635374e7
EM
5438 /* the queue is being flushed so ignore this event */
5439 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5440 if (ioc->fw_events_off || ioc->remove_host) {
5441 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5442 _scsih_fw_event_free(ioc, fw_event);
5443 return;
5444 }
5445 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5446
635374e7 5447 if (ioc->shost_recovery) {
635374e7
EM
5448 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5449 return;
5450 }
635374e7
EM
5451
5452 switch (fw_event->event) {
5453 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7b936b02 5454 _scsih_sas_topology_change_event(ioc, fw_event);
635374e7
EM
5455 break;
5456 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7b936b02
KD
5457 _scsih_sas_device_status_change_event(ioc,
5458 fw_event);
635374e7
EM
5459 break;
5460 case MPI2_EVENT_SAS_DISCOVERY:
7b936b02
KD
5461 _scsih_sas_discovery_event(ioc,
5462 fw_event);
635374e7
EM
5463 break;
5464 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7b936b02
KD
5465 _scsih_sas_broadcast_primative_event(ioc,
5466 fw_event);
635374e7
EM
5467 break;
5468 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5469 _scsih_sas_enclosure_dev_status_change_event(ioc,
7b936b02 5470 fw_event);
635374e7
EM
5471 break;
5472 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7b936b02 5473 _scsih_sas_ir_config_change_event(ioc, fw_event);
635374e7
EM
5474 break;
5475 case MPI2_EVENT_IR_VOLUME:
7b936b02 5476 _scsih_sas_ir_volume_event(ioc, fw_event);
635374e7
EM
5477 break;
5478 case MPI2_EVENT_IR_PHYSICAL_DISK:
7b936b02 5479 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
635374e7
EM
5480 break;
5481 case MPI2_EVENT_IR_OPERATION_STATUS:
7b936b02 5482 _scsih_sas_ir_operation_status_event(ioc, fw_event);
635374e7
EM
5483 break;
5484 case MPI2_EVENT_TASK_SET_FULL:
7b936b02 5485 _scsih_task_set_full(ioc, fw_event);
635374e7
EM
5486 break;
5487 }
5488 _scsih_fw_event_free(ioc, fw_event);
5489}
5490
5491/**
5492 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5493 * @ioc: per adapter object
7b936b02 5494 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
5495 * @reply: reply message frame(lower 32bit addr)
5496 * Context: interrupt.
5497 *
5498 * This function merely adds a new work task into ioc->firmware_event_thread.
5499 * The tasks are worked from _firmware_event_work in user context.
5500 *
77e63ed4
KD
5501 * Return 1 meaning mf should be freed from _base_interrupt
5502 * 0 means the mf is freed from this function.
635374e7 5503 */
77e63ed4 5504u8
7b936b02
KD
5505mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
5506 u32 reply)
635374e7
EM
5507{
5508 struct fw_event_work *fw_event;
5509 Mpi2EventNotificationReply_t *mpi_reply;
5510 unsigned long flags;
5511 u16 event;
5512
5513 /* events turned off due to host reset or driver unloading */
5514 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5515 if (ioc->fw_events_off || ioc->remove_host) {
5516 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
77e63ed4 5517 return 1;
635374e7
EM
5518 }
5519 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5520
77e63ed4 5521 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
635374e7
EM
5522 event = le16_to_cpu(mpi_reply->Event);
5523
5524 switch (event) {
5525 /* handle these */
5526 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5527 {
5528 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5529 (Mpi2EventDataSasBroadcastPrimitive_t *)
5530 mpi_reply->EventData;
5531
5532 if (baen_data->Primitive !=
5533 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5534 ioc->broadcast_aen_busy)
77e63ed4 5535 return 1;
635374e7
EM
5536 ioc->broadcast_aen_busy = 1;
5537 break;
5538 }
5539
5540 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5541 _scsih_check_topo_delete_events(ioc,
5542 (Mpi2EventDataSasTopologyChangeList_t *)
5543 mpi_reply->EventData);
5544 break;
5545
5546 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5547 case MPI2_EVENT_IR_OPERATION_STATUS:
5548 case MPI2_EVENT_SAS_DISCOVERY:
5549 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5550 case MPI2_EVENT_IR_VOLUME:
5551 case MPI2_EVENT_IR_PHYSICAL_DISK:
5552 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5553 case MPI2_EVENT_TASK_SET_FULL:
5554 break;
5555
5556 default: /* ignore the rest */
77e63ed4 5557 return 1;
635374e7
EM
5558 }
5559
5560 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5561 if (!fw_event) {
5562 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5563 ioc->name, __FILE__, __LINE__, __func__);
77e63ed4 5564 return 1;
635374e7
EM
5565 }
5566 fw_event->event_data =
5567 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5568 if (!fw_event->event_data) {
5569 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5570 ioc->name, __FILE__, __LINE__, __func__);
5571 kfree(fw_event);
77e63ed4 5572 return 1;
635374e7
EM
5573 }
5574
5575 memcpy(fw_event->event_data, mpi_reply->EventData,
5576 mpi_reply->EventDataLength*4);
5577 fw_event->ioc = ioc;
7b936b02
KD
5578 fw_event->VF_ID = mpi_reply->VF_ID;
5579 fw_event->VP_ID = mpi_reply->VP_ID;
635374e7
EM
5580 fw_event->event = event;
5581 _scsih_fw_event_add(ioc, fw_event);
77e63ed4 5582 return 1;
635374e7
EM
5583}
5584
5585/* shost template */
5586static struct scsi_host_template scsih_driver_template = {
5587 .module = THIS_MODULE,
5588 .name = "Fusion MPT SAS Host",
5589 .proc_name = MPT2SAS_DRIVER_NAME,
d5d135b3
EM
5590 .queuecommand = _scsih_qcmd,
5591 .target_alloc = _scsih_target_alloc,
5592 .slave_alloc = _scsih_slave_alloc,
5593 .slave_configure = _scsih_slave_configure,
5594 .target_destroy = _scsih_target_destroy,
5595 .slave_destroy = _scsih_slave_destroy,
5596 .change_queue_depth = _scsih_change_queue_depth,
5597 .change_queue_type = _scsih_change_queue_type,
5598 .eh_abort_handler = _scsih_abort,
5599 .eh_device_reset_handler = _scsih_dev_reset,
5600 .eh_target_reset_handler = _scsih_target_reset,
5601 .eh_host_reset_handler = _scsih_host_reset,
5602 .bios_param = _scsih_bios_param,
635374e7
EM
5603 .can_queue = 1,
5604 .this_id = -1,
5605 .sg_tablesize = MPT2SAS_SG_DEPTH,
5606 .max_sectors = 8192,
5607 .cmd_per_lun = 7,
5608 .use_clustering = ENABLE_CLUSTERING,
5609 .shost_attrs = mpt2sas_host_attrs,
5610 .sdev_attrs = mpt2sas_dev_attrs,
5611};
5612
5613/**
5614 * _scsih_expander_node_remove - removing expander device from list.
5615 * @ioc: per adapter object
5616 * @sas_expander: the sas_device object
5617 * Context: Calling function should acquire ioc->sas_node_lock.
5618 *
5619 * Removing object and freeing associated memory from the
5620 * ioc->sas_expander_list.
5621 *
5622 * Return nothing.
5623 */
5624static void
5625_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5626 struct _sas_node *sas_expander)
5627{
5628 struct _sas_port *mpt2sas_port;
5629 struct _sas_device *sas_device;
5630 struct _sas_node *expander_sibling;
5631 unsigned long flags;
5632
5633 if (!sas_expander)
5634 return;
5635
5636 /* remove sibling ports attached to this expander */
5637 retry_device_search:
5638 list_for_each_entry(mpt2sas_port,
5639 &sas_expander->sas_port_list, port_list) {
5640 if (mpt2sas_port->remote_identify.device_type ==
5641 SAS_END_DEVICE) {
5642 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5643 sas_device =
5644 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5645 mpt2sas_port->remote_identify.sas_address);
5646 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5647 if (!sas_device)
5648 continue;
5649 _scsih_remove_device(ioc, sas_device->handle);
155dd4c7
KD
5650 if (ioc->shost_recovery)
5651 return;
635374e7
EM
5652 goto retry_device_search;
5653 }
5654 }
5655
5656 retry_expander_search:
5657 list_for_each_entry(mpt2sas_port,
5658 &sas_expander->sas_port_list, port_list) {
5659
5660 if (mpt2sas_port->remote_identify.device_type ==
5661 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5662 mpt2sas_port->remote_identify.device_type ==
5663 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5664
5665 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5666 expander_sibling =
5667 mpt2sas_scsih_expander_find_by_sas_address(
5668 ioc, mpt2sas_port->remote_identify.sas_address);
5669 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5670 if (!expander_sibling)
5671 continue;
5672 _scsih_expander_remove(ioc, expander_sibling->handle);
155dd4c7
KD
5673 if (ioc->shost_recovery)
5674 return;
635374e7
EM
5675 goto retry_expander_search;
5676 }
5677 }
5678
5679 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
5680 sas_expander->parent_handle);
5681
5682 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5683 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5684 sas_expander->handle, (unsigned long long)
5685 sas_expander->sas_address);
5686
5687 list_del(&sas_expander->list);
5688 kfree(sas_expander->phy);
5689 kfree(sas_expander);
5690}
5691
5692/**
d5d135b3 5693 * _scsih_remove - detach and remove add host
635374e7
EM
5694 * @pdev: PCI device struct
5695 *
5696 * Return nothing.
5697 */
5698static void __devexit
d5d135b3 5699_scsih_remove(struct pci_dev *pdev)
635374e7
EM
5700{
5701 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5702 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5703 struct _sas_port *mpt2sas_port;
5704 struct _sas_device *sas_device;
5705 struct _sas_node *expander_sibling;
5706 struct workqueue_struct *wq;
5707 unsigned long flags;
5708
5709 ioc->remove_host = 1;
5710 _scsih_fw_event_off(ioc);
5711
5712 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5713 wq = ioc->firmware_event_thread;
5714 ioc->firmware_event_thread = NULL;
5715 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5716 if (wq)
5717 destroy_workqueue(wq);
5718
5719 /* free ports attached to the sas_host */
5720 retry_again:
5721 list_for_each_entry(mpt2sas_port,
5722 &ioc->sas_hba.sas_port_list, port_list) {
5723 if (mpt2sas_port->remote_identify.device_type ==
5724 SAS_END_DEVICE) {
5725 sas_device =
5726 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5727 mpt2sas_port->remote_identify.sas_address);
5728 if (sas_device) {
5729 _scsih_remove_device(ioc, sas_device->handle);
5730 goto retry_again;
5731 }
5732 } else {
5733 expander_sibling =
5734 mpt2sas_scsih_expander_find_by_sas_address(ioc,
5735 mpt2sas_port->remote_identify.sas_address);
5736 if (expander_sibling) {
5737 _scsih_expander_remove(ioc,
5738 expander_sibling->handle);
5739 goto retry_again;
5740 }
5741 }
5742 }
5743
5744 /* free phys attached to the sas_host */
5745 if (ioc->sas_hba.num_phys) {
5746 kfree(ioc->sas_hba.phy);
5747 ioc->sas_hba.phy = NULL;
5748 ioc->sas_hba.num_phys = 0;
5749 }
5750
5751 sas_remove_host(shost);
5752 mpt2sas_base_detach(ioc);
5753 list_del(&ioc->list);
5754 scsi_remove_host(shost);
5755 scsi_host_put(shost);
5756}
5757
5758/**
5759 * _scsih_probe_boot_devices - reports 1st device
5760 * @ioc: per adapter object
5761 *
5762 * If specified in bios page 2, this routine reports the 1st
5763 * device scsi-ml or sas transport for persistent boot device
5764 * purposes. Please refer to function _scsih_determine_boot_device()
5765 */
5766static void
5767_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5768{
5769 u8 is_raid;
5770 void *device;
5771 struct _sas_device *sas_device;
5772 struct _raid_device *raid_device;
5773 u16 handle, parent_handle;
5774 u64 sas_address;
5775 unsigned long flags;
5776 int rc;
5777
5778 device = NULL;
5779 if (ioc->req_boot_device.device) {
5780 device = ioc->req_boot_device.device;
5781 is_raid = ioc->req_boot_device.is_raid;
5782 } else if (ioc->req_alt_boot_device.device) {
5783 device = ioc->req_alt_boot_device.device;
5784 is_raid = ioc->req_alt_boot_device.is_raid;
5785 } else if (ioc->current_boot_device.device) {
5786 device = ioc->current_boot_device.device;
5787 is_raid = ioc->current_boot_device.is_raid;
5788 }
5789
5790 if (!device)
5791 return;
5792
5793 if (is_raid) {
5794 raid_device = device;
5795 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5796 raid_device->id, 0);
5797 if (rc)
5798 _scsih_raid_device_remove(ioc, raid_device);
5799 } else {
5800 sas_device = device;
5801 handle = sas_device->handle;
5802 parent_handle = sas_device->parent_handle;
5803 sas_address = sas_device->sas_address;
5804 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5805 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5806 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5807 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5808 sas_device->parent_handle)) {
5809 _scsih_sas_device_remove(ioc, sas_device);
5810 } else if (!sas_device->starget) {
5811 mpt2sas_transport_port_remove(ioc, sas_address,
5812 parent_handle);
5813 _scsih_sas_device_remove(ioc, sas_device);
5814 }
5815 }
5816}
5817
5818/**
5819 * _scsih_probe_raid - reporting raid volumes to scsi-ml
5820 * @ioc: per adapter object
5821 *
5822 * Called during initial loading of the driver.
5823 */
5824static void
5825_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5826{
5827 struct _raid_device *raid_device, *raid_next;
5828 int rc;
5829
5830 list_for_each_entry_safe(raid_device, raid_next,
5831 &ioc->raid_device_list, list) {
5832 if (raid_device->starget)
5833 continue;
5834 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5835 raid_device->id, 0);
5836 if (rc)
5837 _scsih_raid_device_remove(ioc, raid_device);
5838 }
5839}
5840
5841/**
77e63ed4 5842 * _scsih_probe_sas - reporting sas devices to sas transport
635374e7
EM
5843 * @ioc: per adapter object
5844 *
5845 * Called during initial loading of the driver.
5846 */
5847static void
5848_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5849{
5850 struct _sas_device *sas_device, *next;
5851 unsigned long flags;
5852 u16 handle, parent_handle;
5853 u64 sas_address;
5854
5855 /* SAS Device List */
5856 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5857 list) {
5858 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5859 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5860 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5861
5862 handle = sas_device->handle;
5863 parent_handle = sas_device->parent_handle;
5864 sas_address = sas_device->sas_address;
5865 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
5866 _scsih_sas_device_remove(ioc, sas_device);
5867 } else if (!sas_device->starget) {
5868 mpt2sas_transport_port_remove(ioc, sas_address,
5869 parent_handle);
5870 _scsih_sas_device_remove(ioc, sas_device);
5871 }
5872 }
5873}
5874
5875/**
5876 * _scsih_probe_devices - probing for devices
5877 * @ioc: per adapter object
5878 *
5879 * Called during initial loading of the driver.
5880 */
5881static void
5882_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
5883{
5884 u16 volume_mapping_flags =
5885 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
5886 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
5887
5888 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
5889 return; /* return when IOC doesn't support initiator mode */
5890
5891 _scsih_probe_boot_devices(ioc);
5892
5893 if (ioc->ir_firmware) {
5894 if ((volume_mapping_flags &
5895 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
5896 _scsih_probe_sas(ioc);
5897 _scsih_probe_raid(ioc);
5898 } else {
5899 _scsih_probe_raid(ioc);
5900 _scsih_probe_sas(ioc);
5901 }
5902 } else
5903 _scsih_probe_sas(ioc);
5904}
5905
5906/**
d5d135b3 5907 * _scsih_probe - attach and add scsi host
635374e7
EM
5908 * @pdev: PCI device struct
5909 * @id: pci device id
5910 *
5911 * Returns 0 success, anything else error.
5912 */
5913static int
d5d135b3 5914_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
635374e7
EM
5915{
5916 struct MPT2SAS_ADAPTER *ioc;
5917 struct Scsi_Host *shost;
5918
5919 shost = scsi_host_alloc(&scsih_driver_template,
5920 sizeof(struct MPT2SAS_ADAPTER));
5921 if (!shost)
5922 return -ENODEV;
5923
5924 /* init local params */
5925 ioc = shost_priv(shost);
5926 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
5927 INIT_LIST_HEAD(&ioc->list);
ba33fadf 5928 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
635374e7
EM
5929 ioc->shost = shost;
5930 ioc->id = mpt_ids++;
5931 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
5932 ioc->pdev = pdev;
5933 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
5934 ioc->tm_cb_idx = tm_cb_idx;
5935 ioc->ctl_cb_idx = ctl_cb_idx;
5936 ioc->base_cb_idx = base_cb_idx;
5937 ioc->transport_cb_idx = transport_cb_idx;
5938 ioc->config_cb_idx = config_cb_idx;
77e63ed4
KD
5939 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
5940 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
635374e7
EM
5941 ioc->logging_level = logging_level;
5942 /* misc semaphores and spin locks */
5943 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
5944 spin_lock_init(&ioc->scsi_lookup_lock);
5945 spin_lock_init(&ioc->sas_device_lock);
5946 spin_lock_init(&ioc->sas_node_lock);
5947 spin_lock_init(&ioc->fw_event_lock);
5948 spin_lock_init(&ioc->raid_device_lock);
5949
5950 INIT_LIST_HEAD(&ioc->sas_device_list);
5951 INIT_LIST_HEAD(&ioc->sas_device_init_list);
5952 INIT_LIST_HEAD(&ioc->sas_expander_list);
5953 INIT_LIST_HEAD(&ioc->fw_event_list);
5954 INIT_LIST_HEAD(&ioc->raid_device_list);
5955 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
77e63ed4 5956 INIT_LIST_HEAD(&ioc->delayed_tr_list);
635374e7
EM
5957
5958 /* init shost parameters */
5959 shost->max_cmd_len = 16;
5960 shost->max_lun = max_lun;
5961 shost->transportt = mpt2sas_transport_template;
5962 shost->unique_id = ioc->id;
5963
5964 if ((scsi_add_host(shost, &pdev->dev))) {
5965 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5966 ioc->name, __FILE__, __LINE__, __func__);
5967 list_del(&ioc->list);
5968 goto out_add_shost_fail;
5969 }
5970
3c621b3e
EM
5971 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
5972 | SHOST_DIF_TYPE3_PROTECTION);
77e63ed4 5973 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
3c621b3e 5974
635374e7
EM
5975 /* event thread */
5976 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
5977 "fw_event%d", ioc->id);
5978 ioc->firmware_event_thread = create_singlethread_workqueue(
5979 ioc->firmware_event_name);
5980 if (!ioc->firmware_event_thread) {
5981 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5982 ioc->name, __FILE__, __LINE__, __func__);
5983 goto out_thread_fail;
5984 }
5985
5986 ioc->wait_for_port_enable_to_complete = 1;
5987 if ((mpt2sas_base_attach(ioc))) {
5988 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5989 ioc->name, __FILE__, __LINE__, __func__);
5990 goto out_attach_fail;
5991 }
5992
5993 ioc->wait_for_port_enable_to_complete = 0;
5994 _scsih_probe_devices(ioc);
5995 return 0;
5996
5997 out_attach_fail:
5998 destroy_workqueue(ioc->firmware_event_thread);
5999 out_thread_fail:
6000 list_del(&ioc->list);
6001 scsi_remove_host(shost);
6002 out_add_shost_fail:
6003 return -ENODEV;
6004}
6005
6006#ifdef CONFIG_PM
6007/**
d5d135b3 6008 * _scsih_suspend - power management suspend main entry point
635374e7
EM
6009 * @pdev: PCI device struct
6010 * @state: PM state change to (usually PCI_D3)
6011 *
6012 * Returns 0 success, anything else error.
6013 */
6014static int
d5d135b3 6015_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
635374e7
EM
6016{
6017 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6018 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6019 u32 device_state;
6020
e4750c98 6021 mpt2sas_base_stop_watchdog(ioc);
635374e7
EM
6022 flush_scheduled_work();
6023 scsi_block_requests(shost);
6024 device_state = pci_choose_state(pdev, state);
6025 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6026 "operating state [D%d]\n", ioc->name, pdev,
6027 pci_name(pdev), device_state);
6028
6029 mpt2sas_base_free_resources(ioc);
6030 pci_save_state(pdev);
6031 pci_disable_device(pdev);
6032 pci_set_power_state(pdev, device_state);
6033 return 0;
6034}
6035
6036/**
d5d135b3 6037 * _scsih_resume - power management resume main entry point
635374e7
EM
6038 * @pdev: PCI device struct
6039 *
6040 * Returns 0 success, anything else error.
6041 */
6042static int
d5d135b3 6043_scsih_resume(struct pci_dev *pdev)
635374e7
EM
6044{
6045 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6046 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6047 u32 device_state = pdev->current_state;
6048 int r;
6049
6050 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6051 "operating state [D%d]\n", ioc->name, pdev,
6052 pci_name(pdev), device_state);
6053
6054 pci_set_power_state(pdev, PCI_D0);
6055 pci_enable_wake(pdev, PCI_D0, 0);
6056 pci_restore_state(pdev);
6057 ioc->pdev = pdev;
6058 r = mpt2sas_base_map_resources(ioc);
6059 if (r)
6060 return r;
6061
6062 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6063 scsi_unblock_requests(shost);
e4750c98 6064 mpt2sas_base_start_watchdog(ioc);
635374e7
EM
6065 return 0;
6066}
6067#endif /* CONFIG_PM */
6068
6069
6070static struct pci_driver scsih_driver = {
6071 .name = MPT2SAS_DRIVER_NAME,
6072 .id_table = scsih_pci_table,
d5d135b3
EM
6073 .probe = _scsih_probe,
6074 .remove = __devexit_p(_scsih_remove),
635374e7 6075#ifdef CONFIG_PM
d5d135b3
EM
6076 .suspend = _scsih_suspend,
6077 .resume = _scsih_resume,
635374e7
EM
6078#endif
6079};
6080
6081
6082/**
d5d135b3 6083 * _scsih_init - main entry point for this driver.
635374e7
EM
6084 *
6085 * Returns 0 success, anything else error.
6086 */
6087static int __init
d5d135b3 6088_scsih_init(void)
635374e7
EM
6089{
6090 int error;
6091
6092 mpt_ids = 0;
6093 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
6094 MPT2SAS_DRIVER_VERSION);
6095
6096 mpt2sas_transport_template =
6097 sas_attach_transport(&mpt2sas_transport_functions);
6098 if (!mpt2sas_transport_template)
6099 return -ENODEV;
6100
6101 mpt2sas_base_initialize_callback_handler();
6102
6103 /* queuecommand callback hander */
d5d135b3 6104 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
635374e7
EM
6105
6106 /* task managment callback handler */
d5d135b3 6107 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
635374e7
EM
6108
6109 /* base internal commands callback handler */
6110 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
6111
6112 /* transport internal commands callback handler */
6113 transport_cb_idx = mpt2sas_base_register_callback_handler(
6114 mpt2sas_transport_done);
6115
6116 /* configuration page API internal commands callback handler */
6117 config_cb_idx = mpt2sas_base_register_callback_handler(
6118 mpt2sas_config_done);
6119
6120 /* ctl module callback handler */
6121 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
6122
77e63ed4
KD
6123 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
6124 _scsih_tm_tr_complete);
6125 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
6126 _scsih_sas_control_complete);
6127
635374e7
EM
6128 mpt2sas_ctl_init();
6129
6130 error = pci_register_driver(&scsih_driver);
6131 if (error)
6132 sas_release_transport(mpt2sas_transport_template);
6133
6134 return error;
6135}
6136
6137/**
d5d135b3 6138 * _scsih_exit - exit point for this driver (when it is a module).
635374e7
EM
6139 *
6140 * Returns 0 success, anything else error.
6141 */
6142static void __exit
d5d135b3 6143_scsih_exit(void)
635374e7
EM
6144{
6145 printk(KERN_INFO "mpt2sas version %s unloading\n",
6146 MPT2SAS_DRIVER_VERSION);
6147
6148 pci_unregister_driver(&scsih_driver);
6149
6150 sas_release_transport(mpt2sas_transport_template);
6151 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
6152 mpt2sas_base_release_callback_handler(tm_cb_idx);
6153 mpt2sas_base_release_callback_handler(base_cb_idx);
6154 mpt2sas_base_release_callback_handler(transport_cb_idx);
6155 mpt2sas_base_release_callback_handler(config_cb_idx);
6156 mpt2sas_base_release_callback_handler(ctl_cb_idx);
6157
77e63ed4
KD
6158 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
6159 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
6160
635374e7
EM
6161 mpt2sas_ctl_exit();
6162}
6163
d5d135b3
EM
6164module_init(_scsih_init);
6165module_exit(_scsih_exit);