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