]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/scsi/mpt3sas/mpt3sas_scsih.c
scsi: mpt3sas: Reduce memory footprint in kdump kernel
[mirror_ubuntu-focal-kernel.git] / drivers / scsi / mpt3sas / mpt3sas_scsih.c
CommitLineData
f92363d1
SR
1/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
a4ffce0d 5 * Copyright (C) 2012-2014 LSI Corporation
a03bd153
SR
6 * Copyright (C) 2013-2014 Avago Technologies
7 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
f92363d1
SR
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
29
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
43 */
44
f92363d1
SR
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>
ffdadd68 54#include <linux/pci-aspm.h>
f92363d1
SR
55#include <linux/interrupt.h>
56#include <linux/aer.h>
57#include <linux/raid_class.h>
7786ab6a 58#include <asm/unaligned.h>
f92363d1
SR
59
60#include "mpt3sas_base.h"
61
f92363d1
SR
62#define RAID_CHANNEL 1
63/* forward proto's */
64static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
65 struct _sas_node *sas_expander);
66static void _firmware_event_work(struct work_struct *work);
67
68static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
69 struct _sas_device *sas_device);
70static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
71 u8 retry_count, u8 is_pd);
72
73static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
74
f92363d1
SR
75/* global parameters */
76LIST_HEAD(mpt3sas_ioc_list);
08c4d550
SR
77/* global ioc lock for list operations */
78DEFINE_SPINLOCK(gioc_lock);
f92363d1 79
c84b06a4
SR
80MODULE_AUTHOR(MPT3SAS_AUTHOR);
81MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
82MODULE_LICENSE("GPL");
83MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
84MODULE_ALIAS("mpt2sas");
85
f92363d1
SR
86/* local parameters */
87static u8 scsi_io_cb_idx = -1;
88static u8 tm_cb_idx = -1;
89static u8 ctl_cb_idx = -1;
90static u8 base_cb_idx = -1;
91static u8 port_enable_cb_idx = -1;
92static u8 transport_cb_idx = -1;
93static u8 scsih_cb_idx = -1;
94static u8 config_cb_idx = -1;
c84b06a4
SR
95static int mpt2_ids;
96static int mpt3_ids;
f92363d1
SR
97
98static u8 tm_tr_cb_idx = -1 ;
99static u8 tm_tr_volume_cb_idx = -1 ;
100static u8 tm_sas_control_cb_idx = -1;
101
102/* command line options */
103static u32 logging_level;
104MODULE_PARM_DESC(logging_level,
105 " bits for enabling additional logging info (default=0)");
106
107
108static ushort max_sectors = 0xFFFF;
109module_param(max_sectors, ushort, 0);
110MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
111
112
113static int missing_delay[2] = {-1, -1};
114module_param_array(missing_delay, int, NULL, 0);
115MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
116
117/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
118#define MPT3SAS_MAX_LUN (16895)
1abf635d
HR
119static u64 max_lun = MPT3SAS_MAX_LUN;
120module_param(max_lun, ullong, 0);
f92363d1
SR
121MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
122
c84b06a4
SR
123static ushort hbas_to_enumerate;
124module_param(hbas_to_enumerate, ushort, 0);
125MODULE_PARM_DESC(hbas_to_enumerate,
126 " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
127 1 - enumerates only SAS 2.0 generation HBAs\n \
128 2 - enumerates only SAS 3.0 generation HBAs (default=0)");
f92363d1
SR
129
130/* diag_buffer_enable is bitwise
131 * bit 0 set = TRACE
132 * bit 1 set = SNAPSHOT
133 * bit 2 set = EXTENDED
134 *
135 * Either bit can be set, or both
136 */
137static int diag_buffer_enable = -1;
138module_param(diag_buffer_enable, int, 0);
139MODULE_PARM_DESC(diag_buffer_enable,
140 " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
141static int disable_discovery = -1;
142module_param(disable_discovery, int, 0);
143MODULE_PARM_DESC(disable_discovery, " disable discovery ");
144
145
146/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
147static int prot_mask = -1;
148module_param(prot_mask, int, 0);
149MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
150
151
152/* raid transport support */
7497392a 153struct raid_template *mpt3sas_raid_template;
c84b06a4 154struct raid_template *mpt2sas_raid_template;
f92363d1
SR
155
156
157/**
158 * struct sense_info - common structure for obtaining sense keys
159 * @skey: sense key
160 * @asc: additional sense code
161 * @ascq: additional sense code qualifier
162 */
163struct sense_info {
164 u8 skey;
165 u8 asc;
166 u8 ascq;
167};
168
169#define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
0f624c39 170#define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
f92363d1
SR
171#define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
172#define MPT3SAS_ABRT_TASK_SET (0xFFFE)
173#define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
174/**
175 * struct fw_event_work - firmware event struct
176 * @list: link list framework
177 * @work: work object (ioc->fault_reset_work_q)
f92363d1
SR
178 * @ioc: per adapter object
179 * @device_handle: device handle
180 * @VF_ID: virtual function id
181 * @VP_ID: virtual port id
182 * @ignore: flag meaning this event has been marked to ignore
b8ac0cc7
JL
183 * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
184 * @refcount: kref for this event
f92363d1
SR
185 * @event_data: reply event data payload follows
186 *
187 * This object stored on ioc->fw_event_list.
188 */
189struct fw_event_work {
190 struct list_head list;
191 struct work_struct work;
f92363d1
SR
192
193 struct MPT3SAS_ADAPTER *ioc;
194 u16 device_handle;
195 u8 VF_ID;
196 u8 VP_ID;
197 u8 ignore;
198 u16 event;
146b16c8 199 struct kref refcount;
35b62362 200 char event_data[0] __aligned(4);
f92363d1
SR
201};
202
146b16c8
SR
203static void fw_event_work_free(struct kref *r)
204{
205 kfree(container_of(r, struct fw_event_work, refcount));
206}
207
208static void fw_event_work_get(struct fw_event_work *fw_work)
209{
210 kref_get(&fw_work->refcount);
211}
212
213static void fw_event_work_put(struct fw_event_work *fw_work)
214{
215 kref_put(&fw_work->refcount, fw_event_work_free);
216}
217
218static struct fw_event_work *alloc_fw_event_work(int len)
219{
220 struct fw_event_work *fw_event;
221
222 fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
223 if (!fw_event)
224 return NULL;
225
226 kref_init(&fw_event->refcount);
227 return fw_event;
228}
229
f92363d1
SR
230/**
231 * struct _scsi_io_transfer - scsi io transfer
232 * @handle: sas device handle (assigned by firmware)
233 * @is_raid: flag set for hidden raid components
234 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
235 * @data_length: data transfer length
236 * @data_dma: dma pointer to data
237 * @sense: sense data
238 * @lun: lun number
239 * @cdb_length: cdb length
240 * @cdb: cdb contents
241 * @timeout: timeout for this command
242 * @VF_ID: virtual function id
243 * @VP_ID: virtual port id
244 * @valid_reply: flag set for reply message
245 * @sense_length: sense length
246 * @ioc_status: ioc status
247 * @scsi_state: scsi state
248 * @scsi_status: scsi staus
249 * @log_info: log information
250 * @transfer_length: data length transfer when there is a reply message
251 *
252 * Used for sending internal scsi commands to devices within this module.
253 * Refer to _scsi_send_scsi_io().
254 */
255struct _scsi_io_transfer {
256 u16 handle;
257 u8 is_raid;
258 enum dma_data_direction dir;
259 u32 data_length;
260 dma_addr_t data_dma;
261 u8 sense[SCSI_SENSE_BUFFERSIZE];
262 u32 lun;
263 u8 cdb_length;
264 u8 cdb[32];
265 u8 timeout;
266 u8 VF_ID;
267 u8 VP_ID;
268 u8 valid_reply;
269 /* the following bits are only valid when 'valid_reply = 1' */
270 u32 sense_length;
271 u16 ioc_status;
272 u8 scsi_state;
273 u8 scsi_status;
274 u32 log_info;
275 u32 transfer_length;
276};
277
f92363d1
SR
278/**
279 * _scsih_set_debug_level - global setting of ioc->logging_level.
280 *
281 * Note: The logging levels are defined in mpt3sas_debug.h.
282 */
283static int
284_scsih_set_debug_level(const char *val, struct kernel_param *kp)
285{
286 int ret = param_set_int(val, kp);
287 struct MPT3SAS_ADAPTER *ioc;
288
289 if (ret)
290 return ret;
291
292 pr_info("setting logging_level(0x%08x)\n", logging_level);
08c4d550 293 spin_lock(&gioc_lock);
f92363d1
SR
294 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
295 ioc->logging_level = logging_level;
08c4d550 296 spin_unlock(&gioc_lock);
f92363d1
SR
297 return 0;
298}
299module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
300 &logging_level, 0644);
301
302/**
303 * _scsih_srch_boot_sas_address - search based on sas_address
304 * @sas_address: sas address
305 * @boot_device: boot device object from bios page 2
306 *
307 * Returns 1 when there's a match, 0 means no match.
308 */
309static inline int
310_scsih_srch_boot_sas_address(u64 sas_address,
311 Mpi2BootDeviceSasWwid_t *boot_device)
312{
313 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
314}
315
316/**
317 * _scsih_srch_boot_device_name - search based on device name
318 * @device_name: device name specified in INDENTIFY fram
319 * @boot_device: boot device object from bios page 2
320 *
321 * Returns 1 when there's a match, 0 means no match.
322 */
323static inline int
324_scsih_srch_boot_device_name(u64 device_name,
325 Mpi2BootDeviceDeviceName_t *boot_device)
326{
327 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
328}
329
330/**
331 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
332 * @enclosure_logical_id: enclosure logical id
333 * @slot_number: slot number
334 * @boot_device: boot device object from bios page 2
335 *
336 * Returns 1 when there's a match, 0 means no match.
337 */
338static inline int
339_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
340 Mpi2BootDeviceEnclosureSlot_t *boot_device)
341{
342 return (enclosure_logical_id == le64_to_cpu(boot_device->
343 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
344 SlotNumber)) ? 1 : 0;
345}
346
347/**
348 * _scsih_is_boot_device - search for matching boot device.
349 * @sas_address: sas address
350 * @device_name: device name specified in INDENTIFY fram
351 * @enclosure_logical_id: enclosure logical id
352 * @slot_number: slot number
353 * @form: specifies boot device form
354 * @boot_device: boot device object from bios page 2
355 *
356 * Returns 1 when there's a match, 0 means no match.
357 */
358static int
359_scsih_is_boot_device(u64 sas_address, u64 device_name,
360 u64 enclosure_logical_id, u16 slot, u8 form,
361 Mpi2BiosPage2BootDevice_t *boot_device)
362{
363 int rc = 0;
364
365 switch (form) {
366 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
367 if (!sas_address)
368 break;
369 rc = _scsih_srch_boot_sas_address(
370 sas_address, &boot_device->SasWwid);
371 break;
372 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
373 if (!enclosure_logical_id)
374 break;
375 rc = _scsih_srch_boot_encl_slot(
376 enclosure_logical_id,
377 slot, &boot_device->EnclosureSlot);
378 break;
379 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
380 if (!device_name)
381 break;
382 rc = _scsih_srch_boot_device_name(
383 device_name, &boot_device->DeviceName);
384 break;
385 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
386 break;
387 }
388
389 return rc;
390}
391
392/**
393 * _scsih_get_sas_address - set the sas_address for given device handle
394 * @handle: device handle
395 * @sas_address: sas address
396 *
397 * Returns 0 success, non-zero when failure
398 */
399static int
400_scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
401 u64 *sas_address)
402{
403 Mpi2SasDevicePage0_t sas_device_pg0;
404 Mpi2ConfigReply_t mpi_reply;
405 u32 ioc_status;
406
407 *sas_address = 0;
408
409 if (handle <= ioc->sas_hba.num_phys) {
410 *sas_address = ioc->sas_hba.sas_address;
411 return 0;
412 }
413
414 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
415 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
416 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
417 __FILE__, __LINE__, __func__);
418 return -ENXIO;
419 }
420
421 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
422 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
423 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
424 return 0;
425 }
426
6c7abffc 427 /* we hit this because the given parent handle doesn't exist */
f92363d1
SR
428 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
429 return -ENXIO;
430
431 /* else error case */
432 pr_err(MPT3SAS_FMT
433 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
434 ioc->name, handle, ioc_status,
435 __FILE__, __LINE__, __func__);
436 return -EIO;
437}
438
439/**
440 * _scsih_determine_boot_device - determine boot device.
441 * @ioc: per adapter object
442 * @device: either sas_device or raid_device object
443 * @is_raid: [flag] 1 = raid object, 0 = sas object
444 *
445 * Determines whether this device should be first reported device to
446 * to scsi-ml or sas transport, this purpose is for persistent boot device.
447 * There are primary, alternate, and current entries in bios page 2. The order
448 * priority is primary, alternate, then current. This routine saves
449 * the corresponding device object and is_raid flag in the ioc object.
450 * The saved data to be used later in _scsih_probe_boot_devices().
451 */
452static void
453_scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
454 void *device, u8 is_raid)
455{
456 struct _sas_device *sas_device;
457 struct _raid_device *raid_device;
458 u64 sas_address;
459 u64 device_name;
460 u64 enclosure_logical_id;
461 u16 slot;
462
463 /* only process this function when driver loads */
464 if (!ioc->is_driver_loading)
465 return;
466
467 /* no Bios, return immediately */
468 if (!ioc->bios_pg3.BiosVersion)
469 return;
470
471 if (!is_raid) {
472 sas_device = device;
473 sas_address = sas_device->sas_address;
474 device_name = sas_device->device_name;
475 enclosure_logical_id = sas_device->enclosure_logical_id;
476 slot = sas_device->slot;
477 } else {
478 raid_device = device;
479 sas_address = raid_device->wwid;
480 device_name = 0;
481 enclosure_logical_id = 0;
482 slot = 0;
483 }
484
485 if (!ioc->req_boot_device.device) {
486 if (_scsih_is_boot_device(sas_address, device_name,
487 enclosure_logical_id, slot,
488 (ioc->bios_pg2.ReqBootDeviceForm &
489 MPI2_BIOSPAGE2_FORM_MASK),
490 &ioc->bios_pg2.RequestedBootDevice)) {
491 dinitprintk(ioc, pr_info(MPT3SAS_FMT
492 "%s: req_boot_device(0x%016llx)\n",
493 ioc->name, __func__,
494 (unsigned long long)sas_address));
495 ioc->req_boot_device.device = device;
496 ioc->req_boot_device.is_raid = is_raid;
497 }
498 }
499
500 if (!ioc->req_alt_boot_device.device) {
501 if (_scsih_is_boot_device(sas_address, device_name,
502 enclosure_logical_id, slot,
503 (ioc->bios_pg2.ReqAltBootDeviceForm &
504 MPI2_BIOSPAGE2_FORM_MASK),
505 &ioc->bios_pg2.RequestedAltBootDevice)) {
506 dinitprintk(ioc, pr_info(MPT3SAS_FMT
507 "%s: req_alt_boot_device(0x%016llx)\n",
508 ioc->name, __func__,
509 (unsigned long long)sas_address));
510 ioc->req_alt_boot_device.device = device;
511 ioc->req_alt_boot_device.is_raid = is_raid;
512 }
513 }
514
515 if (!ioc->current_boot_device.device) {
516 if (_scsih_is_boot_device(sas_address, device_name,
517 enclosure_logical_id, slot,
518 (ioc->bios_pg2.CurrentBootDeviceForm &
519 MPI2_BIOSPAGE2_FORM_MASK),
520 &ioc->bios_pg2.CurrentBootDevice)) {
521 dinitprintk(ioc, pr_info(MPT3SAS_FMT
522 "%s: current_boot_device(0x%016llx)\n",
523 ioc->name, __func__,
524 (unsigned long long)sas_address));
525 ioc->current_boot_device.device = device;
526 ioc->current_boot_device.is_raid = is_raid;
527 }
528 }
529}
530
d1cb5e49
SR
531static struct _sas_device *
532__mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
533 struct MPT3SAS_TARGET *tgt_priv)
534{
535 struct _sas_device *ret;
536
537 assert_spin_locked(&ioc->sas_device_lock);
538
539 ret = tgt_priv->sdev;
540 if (ret)
541 sas_device_get(ret);
542
543 return ret;
544}
545
546static struct _sas_device *
547mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
548 struct MPT3SAS_TARGET *tgt_priv)
549{
550 struct _sas_device *ret;
551 unsigned long flags;
552
553 spin_lock_irqsave(&ioc->sas_device_lock, flags);
554 ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
555 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
556
557 return ret;
558}
559
560
561struct _sas_device *
562__mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
563 u64 sas_address)
564{
565 struct _sas_device *sas_device;
566
567 assert_spin_locked(&ioc->sas_device_lock);
568
569 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
570 if (sas_device->sas_address == sas_address)
571 goto found_device;
572
573 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
574 if (sas_device->sas_address == sas_address)
575 goto found_device;
576
577 return NULL;
578
579found_device:
580 sas_device_get(sas_device);
581 return sas_device;
582}
583
f92363d1 584/**
d1cb5e49 585 * mpt3sas_get_sdev_by_addr - sas device search
f92363d1
SR
586 * @ioc: per adapter object
587 * @sas_address: sas address
588 * Context: Calling function should acquire ioc->sas_device_lock
589 *
590 * This searches for sas_device based on sas_address, then return sas_device
591 * object.
592 */
593struct _sas_device *
d1cb5e49 594mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
f92363d1
SR
595 u64 sas_address)
596{
597 struct _sas_device *sas_device;
d1cb5e49
SR
598 unsigned long flags;
599
600 spin_lock_irqsave(&ioc->sas_device_lock, flags);
601 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
602 sas_address);
603 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
604
605 return sas_device;
606}
607
608static struct _sas_device *
609__mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
610{
611 struct _sas_device *sas_device;
612
613 assert_spin_locked(&ioc->sas_device_lock);
f92363d1
SR
614
615 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
d1cb5e49
SR
616 if (sas_device->handle == handle)
617 goto found_device;
f92363d1
SR
618
619 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
d1cb5e49
SR
620 if (sas_device->handle == handle)
621 goto found_device;
f92363d1
SR
622
623 return NULL;
d1cb5e49
SR
624
625found_device:
626 sas_device_get(sas_device);
627 return sas_device;
f92363d1
SR
628}
629
630/**
d1cb5e49 631 * mpt3sas_get_sdev_by_handle - sas device search
f92363d1
SR
632 * @ioc: per adapter object
633 * @handle: sas device handle (assigned by firmware)
634 * Context: Calling function should acquire ioc->sas_device_lock
635 *
636 * This searches for sas_device based on sas_address, then return sas_device
637 * object.
638 */
639static struct _sas_device *
d1cb5e49 640mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
f92363d1
SR
641{
642 struct _sas_device *sas_device;
d1cb5e49 643 unsigned long flags;
f92363d1 644
d1cb5e49
SR
645 spin_lock_irqsave(&ioc->sas_device_lock, flags);
646 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
647 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
f92363d1 648
d1cb5e49 649 return sas_device;
f92363d1
SR
650}
651
652/**
653 * _scsih_sas_device_remove - remove sas_device from list.
654 * @ioc: per adapter object
655 * @sas_device: the sas_device object
656 * Context: This function will acquire ioc->sas_device_lock.
657 *
d1cb5e49 658 * If sas_device is on the list, remove it and decrement its reference count.
f92363d1
SR
659 */
660static void
661_scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
662 struct _sas_device *sas_device)
663{
664 unsigned long flags;
665
666 if (!sas_device)
667 return;
e6d45e3e
SR
668 pr_info(MPT3SAS_FMT
669 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
670 ioc->name, sas_device->handle,
671 (unsigned long long) sas_device->sas_address);
672
673 if (sas_device->enclosure_handle != 0)
674 pr_info(MPT3SAS_FMT
675 "removing enclosure logical id(0x%016llx), slot(%d)\n",
676 ioc->name, (unsigned long long)
677 sas_device->enclosure_logical_id, sas_device->slot);
678
679 if (sas_device->connector_name[0] != '\0')
680 pr_info(MPT3SAS_FMT
681 "removing enclosure level(0x%04x), connector name( %s)\n",
682 ioc->name, sas_device->enclosure_level,
683 sas_device->connector_name);
f92363d1 684
d1cb5e49
SR
685 /*
686 * The lock serializes access to the list, but we still need to verify
687 * that nobody removed the entry while we were waiting on the lock.
688 */
f92363d1 689 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
690 if (!list_empty(&sas_device->list)) {
691 list_del_init(&sas_device->list);
692 sas_device_put(sas_device);
693 }
f92363d1
SR
694 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
695}
696
697/**
698 * _scsih_device_remove_by_handle - removing device object by handle
699 * @ioc: per adapter object
700 * @handle: device handle
701 *
702 * Return nothing.
703 */
704static void
705_scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
706{
707 struct _sas_device *sas_device;
708 unsigned long flags;
709
710 if (ioc->shost_recovery)
711 return;
712
713 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
714 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
715 if (sas_device) {
716 list_del_init(&sas_device->list);
717 sas_device_put(sas_device);
718 }
f92363d1 719 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
d1cb5e49 720 if (sas_device) {
f92363d1 721 _scsih_remove_device(ioc, sas_device);
d1cb5e49
SR
722 sas_device_put(sas_device);
723 }
f92363d1
SR
724}
725
726/**
727 * mpt3sas_device_remove_by_sas_address - removing device object by sas address
728 * @ioc: per adapter object
729 * @sas_address: device sas_address
730 *
731 * Return nothing.
732 */
733void
734mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
735 u64 sas_address)
736{
737 struct _sas_device *sas_device;
738 unsigned long flags;
739
740 if (ioc->shost_recovery)
741 return;
742
743 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
744 sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
745 if (sas_device) {
746 list_del_init(&sas_device->list);
747 sas_device_put(sas_device);
748 }
f92363d1 749 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
d1cb5e49 750 if (sas_device) {
f92363d1 751 _scsih_remove_device(ioc, sas_device);
d1cb5e49
SR
752 sas_device_put(sas_device);
753 }
f92363d1
SR
754}
755
756/**
757 * _scsih_sas_device_add - insert sas_device to the list.
758 * @ioc: per adapter object
759 * @sas_device: the sas_device object
760 * Context: This function will acquire ioc->sas_device_lock.
761 *
762 * Adding new object to the ioc->sas_device_list.
763 */
764static void
765_scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
766 struct _sas_device *sas_device)
767{
768 unsigned long flags;
769
770 dewtprintk(ioc, pr_info(MPT3SAS_FMT
771 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
772 ioc->name, __func__, sas_device->handle,
773 (unsigned long long)sas_device->sas_address));
774
e6d45e3e
SR
775 if (sas_device->enclosure_handle != 0)
776 dewtprintk(ioc, pr_info(MPT3SAS_FMT
777 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
778 ioc->name, __func__, (unsigned long long)
779 sas_device->enclosure_logical_id, sas_device->slot));
780
781 if (sas_device->connector_name[0] != '\0')
782 dewtprintk(ioc, pr_info(MPT3SAS_FMT
783 "%s: enclosure level(0x%04x), connector name( %s)\n",
784 ioc->name, __func__,
785 sas_device->enclosure_level, sas_device->connector_name));
786
f92363d1 787 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 788 sas_device_get(sas_device);
f92363d1
SR
789 list_add_tail(&sas_device->list, &ioc->sas_device_list);
790 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
791
c696f7b8
SPS
792 if (ioc->hide_drives) {
793 clear_bit(sas_device->handle, ioc->pend_os_device_add);
794 return;
795 }
796
f92363d1
SR
797 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
798 sas_device->sas_address_parent)) {
799 _scsih_sas_device_remove(ioc, sas_device);
800 } else if (!sas_device->starget) {
801 /*
802 * When asyn scanning is enabled, its not possible to remove
803 * devices while scanning is turned on due to an oops in
804 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
805 */
f5edbe77 806 if (!ioc->is_driver_loading) {
f92363d1
SR
807 mpt3sas_transport_port_remove(ioc,
808 sas_device->sas_address,
809 sas_device->sas_address_parent);
f5edbe77
SR
810 _scsih_sas_device_remove(ioc, sas_device);
811 }
c696f7b8
SPS
812 } else
813 clear_bit(sas_device->handle, ioc->pend_os_device_add);
f92363d1
SR
814}
815
816/**
817 * _scsih_sas_device_init_add - insert sas_device to the list.
818 * @ioc: per adapter object
819 * @sas_device: the sas_device object
820 * Context: This function will acquire ioc->sas_device_lock.
821 *
822 * Adding new object at driver load time to the ioc->sas_device_init_list.
823 */
824static void
825_scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
826 struct _sas_device *sas_device)
827{
828 unsigned long flags;
829
830 dewtprintk(ioc, pr_info(MPT3SAS_FMT
831 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
832 __func__, sas_device->handle,
833 (unsigned long long)sas_device->sas_address));
834
e6d45e3e
SR
835 if (sas_device->enclosure_handle != 0)
836 dewtprintk(ioc, pr_info(MPT3SAS_FMT
837 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
838 ioc->name, __func__, (unsigned long long)
839 sas_device->enclosure_logical_id, sas_device->slot));
840
841 if (sas_device->connector_name[0] != '\0')
842 dewtprintk(ioc, pr_info(MPT3SAS_FMT
843 "%s: enclosure level(0x%04x), connector name( %s)\n",
844 ioc->name, __func__, sas_device->enclosure_level,
845 sas_device->connector_name));
846
f92363d1 847 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 848 sas_device_get(sas_device);
f92363d1
SR
849 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
850 _scsih_determine_boot_device(ioc, sas_device, 0);
851 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
852}
853
854/**
855 * _scsih_raid_device_find_by_id - raid device search
856 * @ioc: per adapter object
857 * @id: sas device target id
858 * @channel: sas device channel
859 * Context: Calling function should acquire ioc->raid_device_lock
860 *
861 * This searches for raid_device based on target id, then return raid_device
862 * object.
863 */
864static struct _raid_device *
865_scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
866{
867 struct _raid_device *raid_device, *r;
868
869 r = NULL;
870 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
871 if (raid_device->id == id && raid_device->channel == channel) {
872 r = raid_device;
873 goto out;
874 }
875 }
876
877 out:
878 return r;
879}
880
881/**
c84b06a4 882 * mpt3sas_raid_device_find_by_handle - raid device search
f92363d1
SR
883 * @ioc: per adapter object
884 * @handle: sas device handle (assigned by firmware)
885 * Context: Calling function should acquire ioc->raid_device_lock
886 *
887 * This searches for raid_device based on handle, then return raid_device
888 * object.
889 */
c84b06a4
SR
890struct _raid_device *
891mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
f92363d1
SR
892{
893 struct _raid_device *raid_device, *r;
894
895 r = NULL;
896 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
897 if (raid_device->handle != handle)
898 continue;
899 r = raid_device;
900 goto out;
901 }
902
903 out:
904 return r;
905}
906
907/**
908 * _scsih_raid_device_find_by_wwid - raid device search
909 * @ioc: per adapter object
910 * @handle: sas device handle (assigned by firmware)
911 * Context: Calling function should acquire ioc->raid_device_lock
912 *
913 * This searches for raid_device based on wwid, then return raid_device
914 * object.
915 */
916static struct _raid_device *
917_scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
918{
919 struct _raid_device *raid_device, *r;
920
921 r = NULL;
922 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
923 if (raid_device->wwid != wwid)
924 continue;
925 r = raid_device;
926 goto out;
927 }
928
929 out:
930 return r;
931}
932
933/**
934 * _scsih_raid_device_add - add raid_device object
935 * @ioc: per adapter object
936 * @raid_device: raid_device object
937 *
938 * This is added to the raid_device_list link list.
939 */
940static void
941_scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
942 struct _raid_device *raid_device)
943{
944 unsigned long flags;
945
946 dewtprintk(ioc, pr_info(MPT3SAS_FMT
947 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
948 raid_device->handle, (unsigned long long)raid_device->wwid));
949
950 spin_lock_irqsave(&ioc->raid_device_lock, flags);
951 list_add_tail(&raid_device->list, &ioc->raid_device_list);
952 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
953}
954
955/**
956 * _scsih_raid_device_remove - delete raid_device object
957 * @ioc: per adapter object
958 * @raid_device: raid_device object
959 *
960 */
961static void
962_scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
963 struct _raid_device *raid_device)
964{
965 unsigned long flags;
966
967 spin_lock_irqsave(&ioc->raid_device_lock, flags);
968 list_del(&raid_device->list);
969 kfree(raid_device);
970 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
971}
972
973/**
974 * mpt3sas_scsih_expander_find_by_handle - expander device search
975 * @ioc: per adapter object
976 * @handle: expander handle (assigned by firmware)
977 * Context: Calling function should acquire ioc->sas_device_lock
978 *
979 * This searches for expander device based on handle, then returns the
980 * sas_node object.
981 */
982struct _sas_node *
983mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
984{
985 struct _sas_node *sas_expander, *r;
986
987 r = NULL;
988 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
989 if (sas_expander->handle != handle)
990 continue;
991 r = sas_expander;
992 goto out;
993 }
994 out:
995 return r;
996}
997
998/**
999 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
1000 * @ioc: per adapter object
1001 * @sas_address: sas address
1002 * Context: Calling function should acquire ioc->sas_node_lock.
1003 *
1004 * This searches for expander device based on sas_address, then returns the
1005 * sas_node object.
1006 */
1007struct _sas_node *
1008mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1009 u64 sas_address)
1010{
1011 struct _sas_node *sas_expander, *r;
1012
1013 r = NULL;
1014 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1015 if (sas_expander->sas_address != sas_address)
1016 continue;
1017 r = sas_expander;
1018 goto out;
1019 }
1020 out:
1021 return r;
1022}
1023
1024/**
1025 * _scsih_expander_node_add - insert expander device to the list.
1026 * @ioc: per adapter object
1027 * @sas_expander: the sas_device object
1028 * Context: This function will acquire ioc->sas_node_lock.
1029 *
1030 * Adding new object to the ioc->sas_expander_list.
1031 *
1032 * Return nothing.
1033 */
1034static void
1035_scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1036 struct _sas_node *sas_expander)
1037{
1038 unsigned long flags;
1039
1040 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1041 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1042 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1043}
1044
1045/**
1046 * _scsih_is_end_device - determines if device is an end device
1047 * @device_info: bitfield providing information about the device.
1048 * Context: none
1049 *
1050 * Returns 1 if end device.
1051 */
1052static int
1053_scsih_is_end_device(u32 device_info)
1054{
1055 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1056 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1057 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1058 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1059 return 1;
1060 else
1061 return 0;
1062}
1063
1064/**
1065 * _scsih_scsi_lookup_get - returns scmd entry
1066 * @ioc: per adapter object
1067 * @smid: system request message index
1068 *
1069 * Returns the smid stored scmd pointer.
1070 */
1071static struct scsi_cmnd *
1072_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1073{
1074 return ioc->scsi_lookup[smid - 1].scmd;
1075}
1076
459325c4
C
1077/**
1078 * __scsih_scsi_lookup_get_clear - returns scmd entry without
1079 * holding any lock.
1080 * @ioc: per adapter object
1081 * @smid: system request message index
1082 *
1083 * Returns the smid stored scmd pointer.
1084 * Then will dereference the stored scmd pointer.
1085 */
1086static inline struct scsi_cmnd *
1087__scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc,
1088 u16 smid)
1089{
1090 struct scsi_cmnd *scmd = NULL;
1091
1092 swap(scmd, ioc->scsi_lookup[smid - 1].scmd);
1093
1094 return scmd;
1095}
1096
f92363d1
SR
1097/**
1098 * _scsih_scsi_lookup_get_clear - returns scmd entry
1099 * @ioc: per adapter object
1100 * @smid: system request message index
1101 *
1102 * Returns the smid stored scmd pointer.
1103 * Then will derefrence the stored scmd pointer.
1104 */
1105static inline struct scsi_cmnd *
1106_scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1107{
1108 unsigned long flags;
1109 struct scsi_cmnd *scmd;
1110
1111 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
459325c4 1112 scmd = __scsih_scsi_lookup_get_clear(ioc, smid);
f92363d1
SR
1113 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1114
1115 return scmd;
1116}
1117
1118/**
1119 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1120 * @ioc: per adapter object
1121 * @smid: system request message index
1122 * @scmd: pointer to scsi command object
1123 * Context: This function will acquire ioc->scsi_lookup_lock.
1124 *
1125 * This will search for a scmd pointer in the scsi_lookup array,
1126 * returning the revelent smid. A returned value of zero means invalid.
1127 */
1128static u16
1129_scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1130 *scmd)
1131{
1132 u16 smid;
1133 unsigned long flags;
1134 int i;
1135
1136 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1137 smid = 0;
1138 for (i = 0; i < ioc->scsiio_depth; i++) {
1139 if (ioc->scsi_lookup[i].scmd == scmd) {
1140 smid = ioc->scsi_lookup[i].smid;
1141 goto out;
1142 }
1143 }
1144 out:
1145 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1146 return smid;
1147}
1148
1149/**
1150 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1151 * @ioc: per adapter object
1152 * @id: target id
1153 * @channel: channel
1154 * Context: This function will acquire ioc->scsi_lookup_lock.
1155 *
1156 * This will search for a matching channel:id in the scsi_lookup array,
1157 * returning 1 if found.
1158 */
1159static u8
1160_scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1161 int channel)
1162{
1163 u8 found;
1164 unsigned long flags;
1165 int i;
1166
1167 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1168 found = 0;
1169 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1170 if (ioc->scsi_lookup[i].scmd &&
1171 (ioc->scsi_lookup[i].scmd->device->id == id &&
1172 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1173 found = 1;
1174 goto out;
1175 }
1176 }
1177 out:
1178 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1179 return found;
1180}
1181
1182/**
1183 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1184 * @ioc: per adapter object
1185 * @id: target id
1186 * @lun: lun number
1187 * @channel: channel
1188 * Context: This function will acquire ioc->scsi_lookup_lock.
1189 *
1190 * This will search for a matching channel:id:lun in the scsi_lookup array,
1191 * returning 1 if found.
1192 */
1193static u8
1194_scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1195 unsigned int lun, int channel)
1196{
1197 u8 found;
1198 unsigned long flags;
1199 int i;
1200
1201 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1202 found = 0;
1203 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1204 if (ioc->scsi_lookup[i].scmd &&
1205 (ioc->scsi_lookup[i].scmd->device->id == id &&
1206 ioc->scsi_lookup[i].scmd->device->channel == channel &&
1207 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1208 found = 1;
1209 goto out;
1210 }
1211 }
1212 out:
1213 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1214 return found;
1215}
1216
cf75d5d6 1217/**
8a7e4c24 1218 * scsih_change_queue_depth - setting device queue depth
cf75d5d6
CH
1219 * @sdev: scsi device struct
1220 * @qdepth: requested queue depth
1221 *
1222 * Returns queue depth.
1223 */
8bbb1cf6 1224static int
8a7e4c24 1225scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
f92363d1
SR
1226{
1227 struct Scsi_Host *shost = sdev->host;
1228 int max_depth;
1229 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1230 struct MPT3SAS_DEVICE *sas_device_priv_data;
1231 struct MPT3SAS_TARGET *sas_target_priv_data;
1232 struct _sas_device *sas_device;
1233 unsigned long flags;
1234
1235 max_depth = shost->can_queue;
1236
1237 /* limit max device queue for SATA to 32 */
1238 sas_device_priv_data = sdev->hostdata;
1239 if (!sas_device_priv_data)
1240 goto not_sata;
1241 sas_target_priv_data = sas_device_priv_data->sas_target;
1242 if (!sas_target_priv_data)
1243 goto not_sata;
1244 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1245 goto not_sata;
d1cb5e49 1246
f92363d1 1247 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
1248 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1249 if (sas_device) {
1250 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1251 max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1252
1253 sas_device_put(sas_device);
1254 }
f92363d1
SR
1255 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1256
1257 not_sata:
1258
1259 if (!sdev->tagged_supported)
1260 max_depth = 1;
1261 if (qdepth > max_depth)
1262 qdepth = max_depth;
cf75d5d6 1263 return scsi_change_queue_depth(sdev, qdepth);
f92363d1
SR
1264}
1265
f92363d1 1266/**
8a7e4c24 1267 * scsih_target_alloc - target add routine
f92363d1
SR
1268 * @starget: scsi target struct
1269 *
1270 * Returns 0 if ok. Any other return is assumed to be an error and
1271 * the device is ignored.
1272 */
8bbb1cf6 1273static int
8a7e4c24 1274scsih_target_alloc(struct scsi_target *starget)
f92363d1
SR
1275{
1276 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1277 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1278 struct MPT3SAS_TARGET *sas_target_priv_data;
1279 struct _sas_device *sas_device;
1280 struct _raid_device *raid_device;
1281 unsigned long flags;
1282 struct sas_rphy *rphy;
1283
62c4da44
JL
1284 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1285 GFP_KERNEL);
f92363d1
SR
1286 if (!sas_target_priv_data)
1287 return -ENOMEM;
1288
1289 starget->hostdata = sas_target_priv_data;
1290 sas_target_priv_data->starget = starget;
1291 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1292
1293 /* RAID volumes */
1294 if (starget->channel == RAID_CHANNEL) {
1295 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1296 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1297 starget->channel);
1298 if (raid_device) {
1299 sas_target_priv_data->handle = raid_device->handle;
1300 sas_target_priv_data->sas_address = raid_device->wwid;
1301 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
7786ab6a 1302 if (ioc->is_warpdrive)
6d3a56ed
SR
1303 sas_target_priv_data->raid_device = raid_device;
1304 raid_device->starget = starget;
f92363d1
SR
1305 }
1306 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1307 return 0;
1308 }
1309
1310 /* sas/sata devices */
1311 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1312 rphy = dev_to_rphy(starget->dev.parent);
d1cb5e49 1313 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
f92363d1
SR
1314 rphy->identify.sas_address);
1315
1316 if (sas_device) {
1317 sas_target_priv_data->handle = sas_device->handle;
1318 sas_target_priv_data->sas_address = sas_device->sas_address;
d1cb5e49 1319 sas_target_priv_data->sdev = sas_device;
f92363d1
SR
1320 sas_device->starget = starget;
1321 sas_device->id = starget->id;
1322 sas_device->channel = starget->channel;
1323 if (test_bit(sas_device->handle, ioc->pd_handles))
1324 sas_target_priv_data->flags |=
1325 MPT_TARGET_FLAGS_RAID_COMPONENT;
1326 if (sas_device->fast_path)
1327 sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1328 }
1329 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1330
1331 return 0;
1332}
1333
1334/**
8a7e4c24 1335 * scsih_target_destroy - target destroy routine
f92363d1
SR
1336 * @starget: scsi target struct
1337 *
1338 * Returns nothing.
1339 */
8bbb1cf6 1340static void
8a7e4c24 1341scsih_target_destroy(struct scsi_target *starget)
f92363d1
SR
1342{
1343 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1344 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1345 struct MPT3SAS_TARGET *sas_target_priv_data;
1346 struct _sas_device *sas_device;
1347 struct _raid_device *raid_device;
1348 unsigned long flags;
f92363d1
SR
1349
1350 sas_target_priv_data = starget->hostdata;
1351 if (!sas_target_priv_data)
1352 return;
1353
1354 if (starget->channel == RAID_CHANNEL) {
1355 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1356 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1357 starget->channel);
1358 if (raid_device) {
1359 raid_device->starget = NULL;
1360 raid_device->sdev = NULL;
1361 }
1362 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1363 goto out;
1364 }
1365
1366 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 1367 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
f92363d1
SR
1368 if (sas_device && (sas_device->starget == starget) &&
1369 (sas_device->id == starget->id) &&
1370 (sas_device->channel == starget->channel))
1371 sas_device->starget = NULL;
1372
d1cb5e49
SR
1373 if (sas_device) {
1374 /*
1375 * Corresponding get() is in _scsih_target_alloc()
1376 */
1377 sas_target_priv_data->sdev = NULL;
1378 sas_device_put(sas_device);
1379
1380 sas_device_put(sas_device);
1381 }
f92363d1
SR
1382 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1383
1384 out:
1385 kfree(sas_target_priv_data);
1386 starget->hostdata = NULL;
1387}
1388
1389/**
8a7e4c24 1390 * scsih_slave_alloc - device add routine
f92363d1
SR
1391 * @sdev: scsi device struct
1392 *
1393 * Returns 0 if ok. Any other return is assumed to be an error and
1394 * the device is ignored.
1395 */
8bbb1cf6 1396static int
8a7e4c24 1397scsih_slave_alloc(struct scsi_device *sdev)
f92363d1
SR
1398{
1399 struct Scsi_Host *shost;
1400 struct MPT3SAS_ADAPTER *ioc;
1401 struct MPT3SAS_TARGET *sas_target_priv_data;
1402 struct MPT3SAS_DEVICE *sas_device_priv_data;
1403 struct scsi_target *starget;
1404 struct _raid_device *raid_device;
b65cfedf 1405 struct _sas_device *sas_device;
f92363d1
SR
1406 unsigned long flags;
1407
62c4da44
JL
1408 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1409 GFP_KERNEL);
f92363d1
SR
1410 if (!sas_device_priv_data)
1411 return -ENOMEM;
1412
1413 sas_device_priv_data->lun = sdev->lun;
1414 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1415
1416 starget = scsi_target(sdev);
1417 sas_target_priv_data = starget->hostdata;
1418 sas_target_priv_data->num_luns++;
1419 sas_device_priv_data->sas_target = sas_target_priv_data;
1420 sdev->hostdata = sas_device_priv_data;
1421 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1422 sdev->no_uld_attach = 1;
1423
1424 shost = dev_to_shost(&starget->dev);
1425 ioc = shost_priv(shost);
1426 if (starget->channel == RAID_CHANNEL) {
1427 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1428 raid_device = _scsih_raid_device_find_by_id(ioc,
1429 starget->id, starget->channel);
1430 if (raid_device)
1431 raid_device->sdev = sdev; /* raid is single lun */
1432 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1433 }
1434
b65cfedf
SR
1435 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1436 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 1437 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
b65cfedf
SR
1438 sas_target_priv_data->sas_address);
1439 if (sas_device && (sas_device->starget == NULL)) {
1440 sdev_printk(KERN_INFO, sdev,
1441 "%s : sas_device->starget set to starget @ %d\n",
d1cb5e49 1442 __func__, __LINE__);
b65cfedf
SR
1443 sas_device->starget = starget;
1444 }
d1cb5e49
SR
1445
1446 if (sas_device)
1447 sas_device_put(sas_device);
1448
b65cfedf
SR
1449 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1450 }
1451
f92363d1
SR
1452 return 0;
1453}
1454
1455/**
8a7e4c24 1456 * scsih_slave_destroy - device destroy routine
f92363d1
SR
1457 * @sdev: scsi device struct
1458 *
1459 * Returns nothing.
1460 */
8bbb1cf6 1461static void
8a7e4c24 1462scsih_slave_destroy(struct scsi_device *sdev)
f92363d1
SR
1463{
1464 struct MPT3SAS_TARGET *sas_target_priv_data;
1465 struct scsi_target *starget;
1466 struct Scsi_Host *shost;
1467 struct MPT3SAS_ADAPTER *ioc;
1468 struct _sas_device *sas_device;
1469 unsigned long flags;
1470
1471 if (!sdev->hostdata)
1472 return;
1473
1474 starget = scsi_target(sdev);
1475 sas_target_priv_data = starget->hostdata;
1476 sas_target_priv_data->num_luns--;
1477
1478 shost = dev_to_shost(&starget->dev);
1479 ioc = shost_priv(shost);
1480
1481 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1482 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
1483 sas_device = __mpt3sas_get_sdev_from_target(ioc,
1484 sas_target_priv_data);
f92363d1
SR
1485 if (sas_device && !sas_target_priv_data->num_luns)
1486 sas_device->starget = NULL;
d1cb5e49
SR
1487
1488 if (sas_device)
1489 sas_device_put(sas_device);
f92363d1
SR
1490 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1491 }
1492
1493 kfree(sdev->hostdata);
1494 sdev->hostdata = NULL;
1495}
1496
1497/**
1498 * _scsih_display_sata_capabilities - sata capabilities
1499 * @ioc: per adapter object
1500 * @handle: device handle
1501 * @sdev: scsi device struct
1502 */
1503static void
1504_scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1505 u16 handle, struct scsi_device *sdev)
1506{
1507 Mpi2ConfigReply_t mpi_reply;
1508 Mpi2SasDevicePage0_t sas_device_pg0;
1509 u32 ioc_status;
1510 u16 flags;
1511 u32 device_info;
1512
1513 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1514 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1515 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1516 ioc->name, __FILE__, __LINE__, __func__);
1517 return;
1518 }
1519
1520 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1521 MPI2_IOCSTATUS_MASK;
1522 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1523 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1524 ioc->name, __FILE__, __LINE__, __func__);
1525 return;
1526 }
1527
1528 flags = le16_to_cpu(sas_device_pg0.Flags);
1529 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1530
1531 sdev_printk(KERN_INFO, sdev,
1532 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1533 "sw_preserve(%s)\n",
1534 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1535 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1536 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1537 "n",
1538 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1539 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1540 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1541}
1542
1543/*
1544 * raid transport support -
1545 * Enabled for SLES11 and newer, in older kernels the driver will panic when
6c7abffc 1546 * unloading the driver followed by a load - I believe that the subroutine
f92363d1
SR
1547 * raid_class_release() is not cleaning up properly.
1548 */
1549
1550/**
8a7e4c24 1551 * scsih_is_raid - return boolean indicating device is raid volume
f92363d1
SR
1552 * @dev the device struct object
1553 */
8bbb1cf6 1554static int
8a7e4c24 1555scsih_is_raid(struct device *dev)
f92363d1
SR
1556{
1557 struct scsi_device *sdev = to_scsi_device(dev);
7786ab6a 1558 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
f92363d1 1559
7786ab6a
SR
1560 if (ioc->is_warpdrive)
1561 return 0;
f92363d1
SR
1562 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1563}
1564
1565/**
8a7e4c24 1566 * scsih_get_resync - get raid volume resync percent complete
f92363d1
SR
1567 * @dev the device struct object
1568 */
8bbb1cf6 1569static void
8a7e4c24 1570scsih_get_resync(struct device *dev)
f92363d1
SR
1571{
1572 struct scsi_device *sdev = to_scsi_device(dev);
1573 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1574 static struct _raid_device *raid_device;
1575 unsigned long flags;
1576 Mpi2RaidVolPage0_t vol_pg0;
1577 Mpi2ConfigReply_t mpi_reply;
1578 u32 volume_status_flags;
1579 u8 percent_complete;
1580 u16 handle;
1581
1582 percent_complete = 0;
1583 handle = 0;
7786ab6a
SR
1584 if (ioc->is_warpdrive)
1585 goto out;
1586
f92363d1
SR
1587 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1588 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1589 sdev->channel);
1590 if (raid_device) {
1591 handle = raid_device->handle;
1592 percent_complete = raid_device->percent_complete;
1593 }
1594 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1595
1596 if (!handle)
1597 goto out;
1598
1599 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1600 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1601 sizeof(Mpi2RaidVolPage0_t))) {
1602 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1603 ioc->name, __FILE__, __LINE__, __func__);
1604 percent_complete = 0;
1605 goto out;
1606 }
1607
1608 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1609 if (!(volume_status_flags &
1610 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1611 percent_complete = 0;
1612
1613 out:
b130b0d5
SS
1614
1615 switch (ioc->hba_mpi_version_belonged) {
1616 case MPI2_VERSION:
c84b06a4 1617 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
b130b0d5
SS
1618 break;
1619 case MPI25_VERSION:
1620 case MPI26_VERSION:
c84b06a4 1621 raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
b130b0d5
SS
1622 break;
1623 }
f92363d1
SR
1624}
1625
1626/**
8a7e4c24 1627 * scsih_get_state - get raid volume level
f92363d1
SR
1628 * @dev the device struct object
1629 */
8bbb1cf6 1630static void
8a7e4c24 1631scsih_get_state(struct device *dev)
f92363d1
SR
1632{
1633 struct scsi_device *sdev = to_scsi_device(dev);
1634 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1635 static struct _raid_device *raid_device;
1636 unsigned long flags;
1637 Mpi2RaidVolPage0_t vol_pg0;
1638 Mpi2ConfigReply_t mpi_reply;
1639 u32 volstate;
1640 enum raid_state state = RAID_STATE_UNKNOWN;
1641 u16 handle = 0;
1642
1643 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1644 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1645 sdev->channel);
1646 if (raid_device)
1647 handle = raid_device->handle;
1648 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1649
1650 if (!raid_device)
1651 goto out;
1652
1653 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1654 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1655 sizeof(Mpi2RaidVolPage0_t))) {
1656 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1657 ioc->name, __FILE__, __LINE__, __func__);
1658 goto out;
1659 }
1660
1661 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1662 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1663 state = RAID_STATE_RESYNCING;
1664 goto out;
1665 }
1666
1667 switch (vol_pg0.VolumeState) {
1668 case MPI2_RAID_VOL_STATE_OPTIMAL:
1669 case MPI2_RAID_VOL_STATE_ONLINE:
1670 state = RAID_STATE_ACTIVE;
1671 break;
1672 case MPI2_RAID_VOL_STATE_DEGRADED:
1673 state = RAID_STATE_DEGRADED;
1674 break;
1675 case MPI2_RAID_VOL_STATE_FAILED:
1676 case MPI2_RAID_VOL_STATE_MISSING:
1677 state = RAID_STATE_OFFLINE;
1678 break;
1679 }
1680 out:
b130b0d5
SS
1681 switch (ioc->hba_mpi_version_belonged) {
1682 case MPI2_VERSION:
c84b06a4 1683 raid_set_state(mpt2sas_raid_template, dev, state);
b130b0d5
SS
1684 break;
1685 case MPI25_VERSION:
1686 case MPI26_VERSION:
c84b06a4 1687 raid_set_state(mpt3sas_raid_template, dev, state);
b130b0d5
SS
1688 break;
1689 }
f92363d1
SR
1690}
1691
1692/**
1693 * _scsih_set_level - set raid level
1694 * @sdev: scsi device struct
1695 * @volume_type: volume type
1696 */
1697static void
c84b06a4
SR
1698_scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
1699 struct scsi_device *sdev, u8 volume_type)
f92363d1
SR
1700{
1701 enum raid_level level = RAID_LEVEL_UNKNOWN;
1702
1703 switch (volume_type) {
1704 case MPI2_RAID_VOL_TYPE_RAID0:
1705 level = RAID_LEVEL_0;
1706 break;
1707 case MPI2_RAID_VOL_TYPE_RAID10:
1708 level = RAID_LEVEL_10;
1709 break;
1710 case MPI2_RAID_VOL_TYPE_RAID1E:
1711 level = RAID_LEVEL_1E;
1712 break;
1713 case MPI2_RAID_VOL_TYPE_RAID1:
1714 level = RAID_LEVEL_1;
1715 break;
1716 }
1717
b130b0d5
SS
1718 switch (ioc->hba_mpi_version_belonged) {
1719 case MPI2_VERSION:
c84b06a4 1720 raid_set_level(mpt2sas_raid_template,
b130b0d5
SS
1721 &sdev->sdev_gendev, level);
1722 break;
1723 case MPI25_VERSION:
1724 case MPI26_VERSION:
c84b06a4 1725 raid_set_level(mpt3sas_raid_template,
b130b0d5
SS
1726 &sdev->sdev_gendev, level);
1727 break;
1728 }
f92363d1
SR
1729}
1730
1731
1732/**
1733 * _scsih_get_volume_capabilities - volume capabilities
1734 * @ioc: per adapter object
1735 * @sas_device: the raid_device object
1736 *
1737 * Returns 0 for success, else 1
1738 */
1739static int
1740_scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1741 struct _raid_device *raid_device)
1742{
1743 Mpi2RaidVolPage0_t *vol_pg0;
1744 Mpi2RaidPhysDiskPage0_t pd_pg0;
1745 Mpi2SasDevicePage0_t sas_device_pg0;
1746 Mpi2ConfigReply_t mpi_reply;
1747 u16 sz;
1748 u8 num_pds;
1749
1750 if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1751 &num_pds)) || !num_pds) {
1752 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1753 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1754 __func__));
1755 return 1;
1756 }
1757
1758 raid_device->num_pds = num_pds;
1759 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1760 sizeof(Mpi2RaidVol0PhysDisk_t));
1761 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1762 if (!vol_pg0) {
1763 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1764 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1765 __func__));
1766 return 1;
1767 }
1768
1769 if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1770 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1771 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1772 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1773 __func__));
1774 kfree(vol_pg0);
1775 return 1;
1776 }
1777
1778 raid_device->volume_type = vol_pg0->VolumeType;
1779
1780 /* figure out what the underlying devices are by
1781 * obtaining the device_info bits for the 1st device
1782 */
1783 if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1784 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1785 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1786 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1787 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1788 le16_to_cpu(pd_pg0.DevHandle)))) {
1789 raid_device->device_info =
1790 le32_to_cpu(sas_device_pg0.DeviceInfo);
1791 }
1792 }
1793
1794 kfree(vol_pg0);
1795 return 0;
1796}
1797
f92363d1
SR
1798/**
1799 * _scsih_enable_tlr - setting TLR flags
1800 * @ioc: per adapter object
1801 * @sdev: scsi device struct
1802 *
1803 * Enabling Transaction Layer Retries for tape devices when
1804 * vpd page 0x90 is present
1805 *
1806 */
1807static void
1808_scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1809{
1810
1811 /* only for TAPE */
1812 if (sdev->type != TYPE_TAPE)
1813 return;
1814
1815 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1816 return;
1817
1818 sas_enable_tlr(sdev);
1819 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1820 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1821 return;
1822
1823}
1824
1825/**
8a7e4c24 1826 * scsih_slave_configure - device configure routine.
f92363d1
SR
1827 * @sdev: scsi device struct
1828 *
1829 * Returns 0 if ok. Any other return is assumed to be an error and
1830 * the device is ignored.
1831 */
8bbb1cf6 1832static int
8a7e4c24 1833scsih_slave_configure(struct scsi_device *sdev)
f92363d1
SR
1834{
1835 struct Scsi_Host *shost = sdev->host;
1836 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1837 struct MPT3SAS_DEVICE *sas_device_priv_data;
1838 struct MPT3SAS_TARGET *sas_target_priv_data;
1839 struct _sas_device *sas_device;
1840 struct _raid_device *raid_device;
1841 unsigned long flags;
1842 int qdepth;
1843 u8 ssp_target = 0;
1844 char *ds = "";
1845 char *r_level = "";
1846 u16 handle, volume_handle = 0;
1847 u64 volume_wwid = 0;
1848
1849 qdepth = 1;
1850 sas_device_priv_data = sdev->hostdata;
1851 sas_device_priv_data->configured_lun = 1;
1852 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1853 sas_target_priv_data = sas_device_priv_data->sas_target;
1854 handle = sas_target_priv_data->handle;
1855
1856 /* raid volume handling */
1857 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1858
1859 spin_lock_irqsave(&ioc->raid_device_lock, flags);
c84b06a4 1860 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
1861 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1862 if (!raid_device) {
1863 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1864 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1865 __LINE__, __func__));
1866 return 1;
1867 }
1868
1869 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1870 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1871 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1872 __LINE__, __func__));
1873 return 1;
1874 }
1875
7786ab6a
SR
1876 /*
1877 * WARPDRIVE: Initialize the required data for Direct IO
1878 */
c84b06a4 1879 mpt3sas_init_warpdrive_properties(ioc, raid_device);
f92363d1
SR
1880
1881 /* RAID Queue Depth Support
1882 * IS volume = underlying qdepth of drive type, either
1883 * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1884 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1885 */
1886 if (raid_device->device_info &
1887 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1888 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1889 ds = "SSP";
1890 } else {
1891 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1892 if (raid_device->device_info &
1893 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1894 ds = "SATA";
1895 else
1896 ds = "STP";
1897 }
1898
1899 switch (raid_device->volume_type) {
1900 case MPI2_RAID_VOL_TYPE_RAID0:
1901 r_level = "RAID0";
1902 break;
1903 case MPI2_RAID_VOL_TYPE_RAID1E:
1904 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1905 if (ioc->manu_pg10.OEMIdentifier &&
1906 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1907 MFG10_GF0_R10_DISPLAY) &&
1908 !(raid_device->num_pds % 2))
1909 r_level = "RAID10";
1910 else
1911 r_level = "RAID1E";
1912 break;
1913 case MPI2_RAID_VOL_TYPE_RAID1:
1914 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1915 r_level = "RAID1";
1916 break;
1917 case MPI2_RAID_VOL_TYPE_RAID10:
1918 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1919 r_level = "RAID10";
1920 break;
1921 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1922 default:
1923 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1924 r_level = "RAIDX";
1925 break;
1926 }
1927
7786ab6a
SR
1928 if (!ioc->hide_ir_msg)
1929 sdev_printk(KERN_INFO, sdev,
1930 "%s: handle(0x%04x), wwid(0x%016llx),"
1931 " pd_count(%d), type(%s)\n",
1932 r_level, raid_device->handle,
1933 (unsigned long long)raid_device->wwid,
1934 raid_device->num_pds, ds);
f92363d1 1935
6c197093
C
1936 if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
1937 blk_queue_max_hw_sectors(sdev->request_queue,
1938 MPT3SAS_RAID_MAX_SECTORS);
1939 sdev_printk(KERN_INFO, sdev,
1940 "Set queue's max_sector to: %u\n",
1941 MPT3SAS_RAID_MAX_SECTORS);
1942 }
1943
8a7e4c24 1944 scsih_change_queue_depth(sdev, qdepth);
f92363d1 1945
7786ab6a
SR
1946 /* raid transport support */
1947 if (!ioc->is_warpdrive)
c84b06a4 1948 _scsih_set_level(ioc, sdev, raid_device->volume_type);
f92363d1
SR
1949 return 0;
1950 }
1951
1952 /* non-raid handling */
1953 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1954 if (mpt3sas_config_get_volume_handle(ioc, handle,
1955 &volume_handle)) {
1956 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1957 "failure at %s:%d/%s()!\n", ioc->name,
1958 __FILE__, __LINE__, __func__));
1959 return 1;
1960 }
1961 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1962 volume_handle, &volume_wwid)) {
1963 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1964 "failure at %s:%d/%s()!\n", ioc->name,
1965 __FILE__, __LINE__, __func__));
1966 return 1;
1967 }
1968 }
1969
1970 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 1971 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
f92363d1
SR
1972 sas_device_priv_data->sas_target->sas_address);
1973 if (!sas_device) {
1974 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1975 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1976 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1977 __func__));
1978 return 1;
1979 }
1980
1981 sas_device->volume_handle = volume_handle;
1982 sas_device->volume_wwid = volume_wwid;
1983 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1984 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1985 ssp_target = 1;
30158dc9
SS
1986 if (sas_device->device_info &
1987 MPI2_SAS_DEVICE_INFO_SEP) {
1988 sdev_printk(KERN_WARNING, sdev,
1989 "set ignore_delay_remove for handle(0x%04x)\n",
1990 sas_device_priv_data->sas_target->handle);
1991 sas_device_priv_data->ignore_delay_remove = 1;
1992 ds = "SES";
1993 } else
1994 ds = "SSP";
f92363d1
SR
1995 } else {
1996 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1997 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1998 ds = "STP";
1999 else if (sas_device->device_info &
2000 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2001 ds = "SATA";
2002 }
2003
2004 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
2005 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2006 ds, handle, (unsigned long long)sas_device->sas_address,
2007 sas_device->phy, (unsigned long long)sas_device->device_name);
e6d45e3e
SR
2008 if (sas_device->enclosure_handle != 0)
2009 sdev_printk(KERN_INFO, sdev,
2010 "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
2011 ds, (unsigned long long)
2012 sas_device->enclosure_logical_id, sas_device->slot);
2013 if (sas_device->connector_name[0] != '\0')
2014 sdev_printk(KERN_INFO, sdev,
2015 "%s: enclosure level(0x%04x), connector name( %s)\n",
2016 ds, sas_device->enclosure_level,
2017 sas_device->connector_name);
f92363d1 2018
d1cb5e49 2019 sas_device_put(sas_device);
f92363d1
SR
2020 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2021
2022 if (!ssp_target)
2023 _scsih_display_sata_capabilities(ioc, handle, sdev);
2024
2025
8a7e4c24 2026 scsih_change_queue_depth(sdev, qdepth);
f92363d1
SR
2027
2028 if (ssp_target) {
2029 sas_read_port_mode_page(sdev);
2030 _scsih_enable_tlr(ioc, sdev);
2031 }
2032
2033 return 0;
2034}
2035
2036/**
8a7e4c24 2037 * scsih_bios_param - fetch head, sector, cylinder info for a disk
f92363d1
SR
2038 * @sdev: scsi device struct
2039 * @bdev: pointer to block device context
2040 * @capacity: device size (in 512 byte sectors)
2041 * @params: three element array to place output:
2042 * params[0] number of heads (max 255)
2043 * params[1] number of sectors (max 63)
2044 * params[2] number of cylinders
2045 *
2046 * Return nothing.
2047 */
8bbb1cf6 2048static int
8a7e4c24 2049scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
f92363d1
SR
2050 sector_t capacity, int params[])
2051{
2052 int heads;
2053 int sectors;
2054 sector_t cylinders;
2055 ulong dummy;
2056
2057 heads = 64;
2058 sectors = 32;
2059
2060 dummy = heads * sectors;
2061 cylinders = capacity;
2062 sector_div(cylinders, dummy);
2063
2064 /*
2065 * Handle extended translation size for logical drives
2066 * > 1Gb
2067 */
2068 if ((ulong)capacity >= 0x200000) {
2069 heads = 255;
2070 sectors = 63;
2071 dummy = heads * sectors;
2072 cylinders = capacity;
2073 sector_div(cylinders, dummy);
2074 }
2075
2076 /* return result */
2077 params[0] = heads;
2078 params[1] = sectors;
2079 params[2] = cylinders;
2080
2081 return 0;
2082}
2083
2084/**
2085 * _scsih_response_code - translation of device response code
2086 * @ioc: per adapter object
2087 * @response_code: response code returned by the device
2088 *
2089 * Return nothing.
2090 */
2091static void
2092_scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2093{
2094 char *desc;
2095
2096 switch (response_code) {
2097 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2098 desc = "task management request completed";
2099 break;
2100 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2101 desc = "invalid frame";
2102 break;
2103 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2104 desc = "task management request not supported";
2105 break;
2106 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2107 desc = "task management request failed";
2108 break;
2109 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2110 desc = "task management request succeeded";
2111 break;
2112 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2113 desc = "invalid lun";
2114 break;
2115 case 0xA:
2116 desc = "overlapped tag attempted";
2117 break;
2118 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2119 desc = "task queued, however not sent to target";
2120 break;
2121 default:
2122 desc = "unknown";
2123 break;
2124 }
2125 pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2126 ioc->name, response_code, desc);
2127}
2128
2129/**
2130 * _scsih_tm_done - tm completion routine
2131 * @ioc: per adapter object
2132 * @smid: system request message index
2133 * @msix_index: MSIX table index supplied by the OS
2134 * @reply: reply message frame(lower 32bit addr)
2135 * Context: none.
2136 *
2137 * The callback handler when using scsih_issue_tm.
2138 *
2139 * Return 1 meaning mf should be freed from _base_interrupt
2140 * 0 means the mf is freed from this function.
2141 */
2142static u8
2143_scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2144{
2145 MPI2DefaultReply_t *mpi_reply;
2146
2147 if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2148 return 1;
2149 if (ioc->tm_cmds.smid != smid)
2150 return 1;
f92363d1
SR
2151 ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2152 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
2153 if (mpi_reply) {
2154 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2155 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2156 }
2157 ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2158 complete(&ioc->tm_cmds.done);
2159 return 1;
2160}
2161
2162/**
2163 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2164 * @ioc: per adapter object
2165 * @handle: device handle
2166 *
2167 * During taskmangement request, we need to freeze the device queue.
2168 */
2169void
2170mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2171{
2172 struct MPT3SAS_DEVICE *sas_device_priv_data;
2173 struct scsi_device *sdev;
2174 u8 skip = 0;
2175
2176 shost_for_each_device(sdev, ioc->shost) {
2177 if (skip)
2178 continue;
2179 sas_device_priv_data = sdev->hostdata;
2180 if (!sas_device_priv_data)
2181 continue;
2182 if (sas_device_priv_data->sas_target->handle == handle) {
2183 sas_device_priv_data->sas_target->tm_busy = 1;
2184 skip = 1;
2185 ioc->ignore_loginfos = 1;
2186 }
2187 }
2188}
2189
2190/**
2191 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2192 * @ioc: per adapter object
2193 * @handle: device handle
2194 *
2195 * During taskmangement request, we need to freeze the device queue.
2196 */
2197void
2198mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2199{
2200 struct MPT3SAS_DEVICE *sas_device_priv_data;
2201 struct scsi_device *sdev;
2202 u8 skip = 0;
2203
2204 shost_for_each_device(sdev, ioc->shost) {
2205 if (skip)
2206 continue;
2207 sas_device_priv_data = sdev->hostdata;
2208 if (!sas_device_priv_data)
2209 continue;
2210 if (sas_device_priv_data->sas_target->handle == handle) {
2211 sas_device_priv_data->sas_target->tm_busy = 0;
2212 skip = 1;
2213 ioc->ignore_loginfos = 0;
2214 }
2215 }
2216}
2217
2218/**
2219 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2220 * @ioc: per adapter struct
2221 * @device_handle: device handle
2222 * @channel: the channel assigned by the OS
2223 * @id: the id assigned by the OS
2224 * @lun: lun number
2225 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2226 * @smid_task: smid assigned to the task
2227 * @timeout: timeout in seconds
f92363d1
SR
2228 * Context: user
2229 *
2230 * A generic API for sending task management requests to firmware.
2231 *
2232 * The callback index is set inside `ioc->tm_cb_idx`.
2233 *
2234 * Return SUCCESS or FAILED.
2235 */
2236int
2237mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
96902835 2238 uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
f92363d1
SR
2239{
2240 Mpi2SCSITaskManagementRequest_t *mpi_request;
2241 Mpi2SCSITaskManagementReply_t *mpi_reply;
2242 u16 smid = 0;
2243 u32 ioc_state;
f92363d1
SR
2244 struct scsiio_tracker *scsi_lookup = NULL;
2245 int rc;
03d1fb3a 2246 u16 msix_task = 0;
f92363d1 2247
96902835
CO
2248 lockdep_assert_held(&ioc->tm_cmds.mutex);
2249
f92363d1
SR
2250 if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2251 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2252 __func__, ioc->name);
96902835 2253 return FAILED;
f92363d1
SR
2254 }
2255
2256 if (ioc->shost_recovery || ioc->remove_host ||
2257 ioc->pci_error_recovery) {
2258 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2259 __func__, ioc->name);
96902835 2260 return FAILED;
f92363d1
SR
2261 }
2262
2263 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2264 if (ioc_state & MPI2_DOORBELL_USED) {
2265 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2266 "unexpected doorbell active!\n", ioc->name));
98c56ad3 2267 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
96902835 2268 return (!rc) ? SUCCESS : FAILED;
f92363d1
SR
2269 }
2270
2271 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2272 mpt3sas_base_fault_info(ioc, ioc_state &
2273 MPI2_DOORBELL_DATA_MASK);
98c56ad3 2274 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
96902835 2275 return (!rc) ? SUCCESS : FAILED;
f92363d1
SR
2276 }
2277
2278 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2279 if (!smid) {
2280 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2281 ioc->name, __func__);
96902835 2282 return FAILED;
f92363d1
SR
2283 }
2284
2285 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2286 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2287
2288 dtmprintk(ioc, pr_info(MPT3SAS_FMT
2289 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2290 ioc->name, handle, type, smid_task));
2291 ioc->tm_cmds.status = MPT3_CMD_PENDING;
2292 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2293 ioc->tm_cmds.smid = smid;
2294 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2295 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2296 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2297 mpi_request->DevHandle = cpu_to_le16(handle);
2298 mpi_request->TaskType = type;
2299 mpi_request->TaskMID = cpu_to_le16(smid_task);
2300 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2301 mpt3sas_scsih_set_tm_flag(ioc, handle);
2302 init_completion(&ioc->tm_cmds.done);
03d1fb3a
SS
2303 if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
2304 (scsi_lookup->msix_io < ioc->reply_queue_count))
2305 msix_task = scsi_lookup->msix_io;
2306 else
2307 msix_task = 0;
81c16f83 2308 ioc->put_smid_hi_priority(ioc, smid, msix_task);
8bbb1cf6 2309 wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
f92363d1
SR
2310 if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2311 pr_err(MPT3SAS_FMT "%s: timeout\n",
2312 ioc->name, __func__);
2313 _debug_dump_mf(mpi_request,
2314 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2315 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
98c56ad3
CO
2316 rc = mpt3sas_base_hard_reset_handler(ioc,
2317 FORCE_BIG_HAMMER);
f92363d1 2318 rc = (!rc) ? SUCCESS : FAILED;
96902835 2319 goto out;
f92363d1
SR
2320 }
2321 }
2322
5f0dfb7a
C
2323 /* sync IRQs in case those were busy during flush. */
2324 mpt3sas_base_sync_reply_irqs(ioc);
2325
f92363d1
SR
2326 if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2327 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2328 mpi_reply = ioc->tm_cmds.reply;
2329 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2330 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2331 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2332 le32_to_cpu(mpi_reply->IOCLogInfo),
2333 le32_to_cpu(mpi_reply->TerminationCount)));
2334 if (ioc->logging_level & MPT_DEBUG_TM) {
2335 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2336 if (mpi_reply->IOCStatus)
2337 _debug_dump_mf(mpi_request,
2338 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2339 }
2340 }
2341
2342 switch (type) {
2343 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2344 rc = SUCCESS;
2345 if (scsi_lookup->scmd == NULL)
2346 break;
2347 rc = FAILED;
2348 break;
2349
2350 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2351 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2352 rc = FAILED;
2353 else
2354 rc = SUCCESS;
2355 break;
2356 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2357 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2358 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2359 rc = FAILED;
2360 else
2361 rc = SUCCESS;
2362 break;
2363 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2364 rc = SUCCESS;
2365 break;
2366 default:
2367 rc = FAILED;
2368 break;
2369 }
2370
96902835 2371out:
f92363d1
SR
2372 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2373 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
f92363d1 2374 return rc;
96902835 2375}
f92363d1 2376
96902835
CO
2377int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
2378 uint channel, uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
2379{
2380 int ret;
2381
2382 mutex_lock(&ioc->tm_cmds.mutex);
2383 ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
2384 smid_task, timeout);
2385 mutex_unlock(&ioc->tm_cmds.mutex);
2386
2387 return ret;
f92363d1
SR
2388}
2389
2390/**
2391 * _scsih_tm_display_info - displays info about the device
2392 * @ioc: per adapter struct
2393 * @scmd: pointer to scsi command object
2394 *
2395 * Called by task management callback handlers.
2396 */
2397static void
2398_scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2399{
2400 struct scsi_target *starget = scmd->device->sdev_target;
2401 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2402 struct _sas_device *sas_device = NULL;
2403 unsigned long flags;
2404 char *device_str = NULL;
2405
2406 if (!priv_target)
2407 return;
7786ab6a
SR
2408 if (ioc->hide_ir_msg)
2409 device_str = "WarpDrive";
2410 else
2411 device_str = "volume";
f92363d1
SR
2412
2413 scsi_print_command(scmd);
2414 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2415 starget_printk(KERN_INFO, starget,
2416 "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2417 device_str, priv_target->handle,
2418 device_str, (unsigned long long)priv_target->sas_address);
2419 } else {
2420 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 2421 sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
f92363d1
SR
2422 if (sas_device) {
2423 if (priv_target->flags &
2424 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2425 starget_printk(KERN_INFO, starget,
2426 "volume handle(0x%04x), "
2427 "volume wwid(0x%016llx)\n",
2428 sas_device->volume_handle,
2429 (unsigned long long)sas_device->volume_wwid);
2430 }
2431 starget_printk(KERN_INFO, starget,
2432 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2433 sas_device->handle,
2434 (unsigned long long)sas_device->sas_address,
2435 sas_device->phy);
e6d45e3e
SR
2436 if (sas_device->enclosure_handle != 0)
2437 starget_printk(KERN_INFO, starget,
2438 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2439 (unsigned long long)
2440 sas_device->enclosure_logical_id,
2441 sas_device->slot);
869817f9 2442 if (sas_device->connector_name[0] != '\0')
e6d45e3e
SR
2443 starget_printk(KERN_INFO, starget,
2444 "enclosure level(0x%04x),connector name(%s)\n",
2445 sas_device->enclosure_level,
2446 sas_device->connector_name);
d1cb5e49
SR
2447
2448 sas_device_put(sas_device);
f92363d1
SR
2449 }
2450 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2451 }
2452}
2453
2454/**
8a7e4c24 2455 * scsih_abort - eh threads main abort routine
f92363d1
SR
2456 * @scmd: pointer to scsi command object
2457 *
2458 * Returns SUCCESS if command aborted else FAILED
2459 */
8bbb1cf6 2460static int
8a7e4c24 2461scsih_abort(struct scsi_cmnd *scmd)
f92363d1
SR
2462{
2463 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2464 struct MPT3SAS_DEVICE *sas_device_priv_data;
2465 u16 smid;
2466 u16 handle;
2467 int r;
2468
2469 sdev_printk(KERN_INFO, scmd->device,
2470 "attempting task abort! scmd(%p)\n", scmd);
2471 _scsih_tm_display_info(ioc, scmd);
2472
2473 sas_device_priv_data = scmd->device->hostdata;
2474 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2475 sdev_printk(KERN_INFO, scmd->device,
2476 "device been deleted! scmd(%p)\n", scmd);
2477 scmd->result = DID_NO_CONNECT << 16;
2478 scmd->scsi_done(scmd);
2479 r = SUCCESS;
2480 goto out;
2481 }
2482
2483 /* search for the command */
2484 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2485 if (!smid) {
2486 scmd->result = DID_RESET << 16;
2487 r = SUCCESS;
2488 goto out;
2489 }
2490
2491 /* for hidden raid components and volumes this is not supported */
2492 if (sas_device_priv_data->sas_target->flags &
2493 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2494 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2495 scmd->result = DID_RESET << 16;
2496 r = FAILED;
2497 goto out;
2498 }
2499
2500 mpt3sas_halt_firmware(ioc);
2501
2502 handle = sas_device_priv_data->sas_target->handle;
96902835 2503 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
f92363d1 2504 scmd->device->id, scmd->device->lun,
96902835 2505 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
f92363d1
SR
2506
2507 out:
2508 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2509 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2510 return r;
2511}
2512
2513/**
8a7e4c24 2514 * scsih_dev_reset - eh threads main device reset routine
f92363d1
SR
2515 * @scmd: pointer to scsi command object
2516 *
2517 * Returns SUCCESS if command aborted else FAILED
2518 */
8bbb1cf6 2519static int
8a7e4c24 2520scsih_dev_reset(struct scsi_cmnd *scmd)
f92363d1
SR
2521{
2522 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2523 struct MPT3SAS_DEVICE *sas_device_priv_data;
d1cb5e49 2524 struct _sas_device *sas_device = NULL;
f92363d1
SR
2525 u16 handle;
2526 int r;
2527
d1cb5e49
SR
2528 struct scsi_target *starget = scmd->device->sdev_target;
2529 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2530
f92363d1
SR
2531 sdev_printk(KERN_INFO, scmd->device,
2532 "attempting device reset! scmd(%p)\n", scmd);
2533 _scsih_tm_display_info(ioc, scmd);
2534
2535 sas_device_priv_data = scmd->device->hostdata;
2536 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2537 sdev_printk(KERN_INFO, scmd->device,
2538 "device been deleted! scmd(%p)\n", scmd);
2539 scmd->result = DID_NO_CONNECT << 16;
2540 scmd->scsi_done(scmd);
2541 r = SUCCESS;
2542 goto out;
2543 }
2544
2545 /* for hidden raid components obtain the volume_handle */
2546 handle = 0;
2547 if (sas_device_priv_data->sas_target->flags &
2548 MPT_TARGET_FLAGS_RAID_COMPONENT) {
d1cb5e49
SR
2549 sas_device = mpt3sas_get_sdev_from_target(ioc,
2550 target_priv_data);
f92363d1
SR
2551 if (sas_device)
2552 handle = sas_device->volume_handle;
f92363d1
SR
2553 } else
2554 handle = sas_device_priv_data->sas_target->handle;
2555
2556 if (!handle) {
2557 scmd->result = DID_RESET << 16;
2558 r = FAILED;
2559 goto out;
2560 }
2561
96902835 2562 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
f92363d1 2563 scmd->device->id, scmd->device->lun,
96902835 2564 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30);
f92363d1
SR
2565
2566 out:
2567 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2568 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
d1cb5e49
SR
2569
2570 if (sas_device)
2571 sas_device_put(sas_device);
2572
f92363d1
SR
2573 return r;
2574}
2575
2576/**
8a7e4c24 2577 * scsih_target_reset - eh threads main target reset routine
f92363d1
SR
2578 * @scmd: pointer to scsi command object
2579 *
2580 * Returns SUCCESS if command aborted else FAILED
2581 */
8bbb1cf6 2582static int
8a7e4c24 2583scsih_target_reset(struct scsi_cmnd *scmd)
f92363d1
SR
2584{
2585 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2586 struct MPT3SAS_DEVICE *sas_device_priv_data;
d1cb5e49 2587 struct _sas_device *sas_device = NULL;
f92363d1
SR
2588 u16 handle;
2589 int r;
2590 struct scsi_target *starget = scmd->device->sdev_target;
d1cb5e49 2591 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
f92363d1
SR
2592
2593 starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2594 scmd);
2595 _scsih_tm_display_info(ioc, scmd);
2596
2597 sas_device_priv_data = scmd->device->hostdata;
2598 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2599 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2600 scmd);
2601 scmd->result = DID_NO_CONNECT << 16;
2602 scmd->scsi_done(scmd);
2603 r = SUCCESS;
2604 goto out;
2605 }
2606
2607 /* for hidden raid components obtain the volume_handle */
2608 handle = 0;
2609 if (sas_device_priv_data->sas_target->flags &
2610 MPT_TARGET_FLAGS_RAID_COMPONENT) {
d1cb5e49
SR
2611 sas_device = mpt3sas_get_sdev_from_target(ioc,
2612 target_priv_data);
f92363d1
SR
2613 if (sas_device)
2614 handle = sas_device->volume_handle;
f92363d1
SR
2615 } else
2616 handle = sas_device_priv_data->sas_target->handle;
2617
2618 if (!handle) {
2619 scmd->result = DID_RESET << 16;
2620 r = FAILED;
2621 goto out;
2622 }
2623
96902835 2624 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
f92363d1 2625 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
96902835 2626 30);
f92363d1
SR
2627
2628 out:
2629 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2630 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
d1cb5e49
SR
2631
2632 if (sas_device)
2633 sas_device_put(sas_device);
2634
f92363d1
SR
2635 return r;
2636}
2637
2638
2639/**
8a7e4c24 2640 * scsih_host_reset - eh threads main host reset routine
f92363d1
SR
2641 * @scmd: pointer to scsi command object
2642 *
2643 * Returns SUCCESS if command aborted else FAILED
2644 */
8bbb1cf6 2645static int
8a7e4c24 2646scsih_host_reset(struct scsi_cmnd *scmd)
f92363d1
SR
2647{
2648 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2649 int r, retval;
2650
2651 pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2652 ioc->name, scmd);
2653 scsi_print_command(scmd);
2654
ddb588be
SR
2655 if (ioc->is_driver_loading) {
2656 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2657 ioc->name);
2658 r = FAILED;
2659 goto out;
2660 }
2661
98c56ad3 2662 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1 2663 r = (retval < 0) ? FAILED : SUCCESS;
ddb588be 2664out:
f92363d1
SR
2665 pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2666 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2667
2668 return r;
2669}
2670
2671/**
2672 * _scsih_fw_event_add - insert and queue up fw_event
2673 * @ioc: per adapter object
2674 * @fw_event: object describing the event
2675 * Context: This function will acquire ioc->fw_event_lock.
2676 *
2677 * This adds the firmware event object into link list, then queues it up to
2678 * be processed from user context.
2679 *
2680 * Return nothing.
2681 */
2682static void
2683_scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2684{
2685 unsigned long flags;
2686
2687 if (ioc->firmware_event_thread == NULL)
2688 return;
2689
2690 spin_lock_irqsave(&ioc->fw_event_lock, flags);
146b16c8 2691 fw_event_work_get(fw_event);
f92363d1
SR
2692 INIT_LIST_HEAD(&fw_event->list);
2693 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2694 INIT_WORK(&fw_event->work, _firmware_event_work);
146b16c8 2695 fw_event_work_get(fw_event);
f92363d1
SR
2696 queue_work(ioc->firmware_event_thread, &fw_event->work);
2697 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2698}
2699
2700/**
146b16c8 2701 * _scsih_fw_event_del_from_list - delete fw_event from the list
f92363d1
SR
2702 * @ioc: per adapter object
2703 * @fw_event: object describing the event
2704 * Context: This function will acquire ioc->fw_event_lock.
2705 *
146b16c8 2706 * If the fw_event is on the fw_event_list, remove it and do a put.
f92363d1
SR
2707 *
2708 * Return nothing.
2709 */
2710static void
146b16c8 2711_scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
f92363d1
SR
2712 *fw_event)
2713{
2714 unsigned long flags;
2715
2716 spin_lock_irqsave(&ioc->fw_event_lock, flags);
146b16c8
SR
2717 if (!list_empty(&fw_event->list)) {
2718 list_del_init(&fw_event->list);
2719 fw_event_work_put(fw_event);
2720 }
f92363d1
SR
2721 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2722}
2723
2724
2725 /**
2726 * mpt3sas_send_trigger_data_event - send event for processing trigger data
2727 * @ioc: per adapter object
2728 * @event_data: trigger event data
2729 *
2730 * Return nothing.
2731 */
2732void
2733mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2734 struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2735{
2736 struct fw_event_work *fw_event;
146b16c8 2737 u16 sz;
f92363d1
SR
2738
2739 if (ioc->is_driver_loading)
2740 return;
146b16c8
SR
2741 sz = sizeof(*event_data);
2742 fw_event = alloc_fw_event_work(sz);
f92363d1
SR
2743 if (!fw_event)
2744 return;
f92363d1
SR
2745 fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2746 fw_event->ioc = ioc;
2747 memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2748 _scsih_fw_event_add(ioc, fw_event);
146b16c8 2749 fw_event_work_put(fw_event);
f92363d1
SR
2750}
2751
2752/**
2753 * _scsih_error_recovery_delete_devices - remove devices not responding
2754 * @ioc: per adapter object
2755 *
2756 * Return nothing.
2757 */
2758static void
2759_scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2760{
2761 struct fw_event_work *fw_event;
2762
2763 if (ioc->is_driver_loading)
2764 return;
146b16c8 2765 fw_event = alloc_fw_event_work(0);
f92363d1
SR
2766 if (!fw_event)
2767 return;
2768 fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2769 fw_event->ioc = ioc;
2770 _scsih_fw_event_add(ioc, fw_event);
146b16c8 2771 fw_event_work_put(fw_event);
f92363d1
SR
2772}
2773
2774/**
2775 * mpt3sas_port_enable_complete - port enable completed (fake event)
2776 * @ioc: per adapter object
2777 *
2778 * Return nothing.
2779 */
2780void
2781mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2782{
2783 struct fw_event_work *fw_event;
2784
146b16c8 2785 fw_event = alloc_fw_event_work(0);
f92363d1
SR
2786 if (!fw_event)
2787 return;
2788 fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2789 fw_event->ioc = ioc;
2790 _scsih_fw_event_add(ioc, fw_event);
146b16c8
SR
2791 fw_event_work_put(fw_event);
2792}
2793
2794static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2795{
2796 unsigned long flags;
2797 struct fw_event_work *fw_event = NULL;
2798
2799 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2800 if (!list_empty(&ioc->fw_event_list)) {
2801 fw_event = list_first_entry(&ioc->fw_event_list,
2802 struct fw_event_work, list);
2803 list_del_init(&fw_event->list);
2804 }
2805 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2806
2807 return fw_event;
f92363d1
SR
2808}
2809
2810/**
2811 * _scsih_fw_event_cleanup_queue - cleanup event queue
2812 * @ioc: per adapter object
2813 *
2814 * Walk the firmware event queue, either killing timers, or waiting
2815 * for outstanding events to complete
2816 *
2817 * Return nothing.
2818 */
2819static void
2820_scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2821{
146b16c8 2822 struct fw_event_work *fw_event;
f92363d1
SR
2823
2824 if (list_empty(&ioc->fw_event_list) ||
2825 !ioc->firmware_event_thread || in_interrupt())
2826 return;
2827
146b16c8
SR
2828 while ((fw_event = dequeue_next_fw_event(ioc))) {
2829 /*
2830 * Wait on the fw_event to complete. If this returns 1, then
2831 * the event was never executed, and we need a put for the
b8ac0cc7 2832 * reference the work had on the fw_event.
146b16c8
SR
2833 *
2834 * If it did execute, we wait for it to finish, and the put will
2835 * happen from _firmware_event_work()
2836 */
b8ac0cc7 2837 if (cancel_work_sync(&fw_event->work))
146b16c8
SR
2838 fw_event_work_put(fw_event);
2839
2840 fw_event_work_put(fw_event);
f92363d1
SR
2841 }
2842}
2843
df838f92
SR
2844/**
2845 * _scsih_internal_device_block - block the sdev device
2846 * @sdev: per device object
2847 * @sas_device_priv_data : per device driver private data
2848 *
2849 * make sure device is blocked without error, if not
2850 * print an error
2851 */
2852static void
2853_scsih_internal_device_block(struct scsi_device *sdev,
2854 struct MPT3SAS_DEVICE *sas_device_priv_data)
2855{
2856 int r = 0;
2857
2858 sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2859 sas_device_priv_data->sas_target->handle);
2860 sas_device_priv_data->block = 1;
2861
551eb598 2862 r = scsi_internal_device_block_nowait(sdev);
df838f92
SR
2863 if (r == -EINVAL)
2864 sdev_printk(KERN_WARNING, sdev,
2865 "device_block failed with return(%d) for handle(0x%04x)\n",
b2fe6be7 2866 r, sas_device_priv_data->sas_target->handle);
df838f92
SR
2867}
2868
2869/**
2870 * _scsih_internal_device_unblock - unblock the sdev device
2871 * @sdev: per device object
2872 * @sas_device_priv_data : per device driver private data
2873 * make sure device is unblocked without error, if not retry
2874 * by blocking and then unblocking
2875 */
2876
2877static void
2878_scsih_internal_device_unblock(struct scsi_device *sdev,
2879 struct MPT3SAS_DEVICE *sas_device_priv_data)
2880{
2881 int r = 0;
2882
2883 sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2884 "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2885 sas_device_priv_data->block = 0;
43f7571b 2886 r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
df838f92
SR
2887 if (r == -EINVAL) {
2888 /* The device has been set to SDEV_RUNNING by SD layer during
2889 * device addition but the request queue is still stopped by
2890 * our earlier block call. We need to perform a block again
2891 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2892
2893 sdev_printk(KERN_WARNING, sdev,
2894 "device_unblock failed with return(%d) for handle(0x%04x) "
2895 "performing a block followed by an unblock\n",
b2fe6be7 2896 r, sas_device_priv_data->sas_target->handle);
df838f92 2897 sas_device_priv_data->block = 1;
551eb598 2898 r = scsi_internal_device_block_nowait(sdev);
df838f92
SR
2899 if (r)
2900 sdev_printk(KERN_WARNING, sdev, "retried device_block "
2901 "failed with return(%d) for handle(0x%04x)\n",
b2fe6be7 2902 r, sas_device_priv_data->sas_target->handle);
df838f92
SR
2903
2904 sas_device_priv_data->block = 0;
43f7571b 2905 r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
df838f92
SR
2906 if (r)
2907 sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2908 " failed with return(%d) for handle(0x%04x)\n",
b2fe6be7 2909 r, sas_device_priv_data->sas_target->handle);
df838f92
SR
2910 }
2911}
2912
f92363d1
SR
2913/**
2914 * _scsih_ublock_io_all_device - unblock every device
2915 * @ioc: per adapter object
2916 *
2917 * change the device state from block to running
2918 */
2919static void
2920_scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2921{
2922 struct MPT3SAS_DEVICE *sas_device_priv_data;
2923 struct scsi_device *sdev;
2924
2925 shost_for_each_device(sdev, ioc->shost) {
2926 sas_device_priv_data = sdev->hostdata;
2927 if (!sas_device_priv_data)
2928 continue;
2929 if (!sas_device_priv_data->block)
2930 continue;
2931
f92363d1
SR
2932 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2933 "device_running, handle(0x%04x)\n",
2934 sas_device_priv_data->sas_target->handle));
df838f92 2935 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
f92363d1
SR
2936 }
2937}
2938
2939
2940/**
2941 * _scsih_ublock_io_device - prepare device to be deleted
2942 * @ioc: per adapter object
2943 * @sas_addr: sas address
2944 *
2945 * unblock then put device in offline state
2946 */
2947static void
2948_scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2949{
2950 struct MPT3SAS_DEVICE *sas_device_priv_data;
2951 struct scsi_device *sdev;
2952
2953 shost_for_each_device(sdev, ioc->shost) {
2954 sas_device_priv_data = sdev->hostdata;
2955 if (!sas_device_priv_data)
2956 continue;
2957 if (sas_device_priv_data->sas_target->sas_address
2958 != sas_address)
2959 continue;
df838f92
SR
2960 if (sas_device_priv_data->block)
2961 _scsih_internal_device_unblock(sdev,
2962 sas_device_priv_data);
f92363d1
SR
2963 }
2964}
2965
2966/**
2967 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2968 * @ioc: per adapter object
2969 * @handle: device handle
2970 *
6c7abffc 2971 * During device pull we need to appropriately set the sdev state.
f92363d1
SR
2972 */
2973static void
2974_scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2975{
2976 struct MPT3SAS_DEVICE *sas_device_priv_data;
2977 struct scsi_device *sdev;
2978
2979 shost_for_each_device(sdev, ioc->shost) {
2980 sas_device_priv_data = sdev->hostdata;
2981 if (!sas_device_priv_data)
2982 continue;
2983 if (sas_device_priv_data->block)
2984 continue;
30158dc9
SS
2985 if (sas_device_priv_data->ignore_delay_remove) {
2986 sdev_printk(KERN_INFO, sdev,
2987 "%s skip device_block for SES handle(0x%04x)\n",
2988 __func__, sas_device_priv_data->sas_target->handle);
2989 continue;
2990 }
df838f92 2991 _scsih_internal_device_block(sdev, sas_device_priv_data);
f92363d1
SR
2992 }
2993}
2994
2995/**
2996 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2997 * @ioc: per adapter object
2998 * @handle: device handle
2999 *
6c7abffc 3000 * During device pull we need to appropriately set the sdev state.
f92363d1
SR
3001 */
3002static void
3003_scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3004{
3005 struct MPT3SAS_DEVICE *sas_device_priv_data;
3006 struct scsi_device *sdev;
e4bc7f5c
SR
3007 struct _sas_device *sas_device;
3008
d1cb5e49 3009 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
e4bc7f5c
SR
3010 if (!sas_device)
3011 return;
f92363d1
SR
3012
3013 shost_for_each_device(sdev, ioc->shost) {
3014 sas_device_priv_data = sdev->hostdata;
3015 if (!sas_device_priv_data)
3016 continue;
3017 if (sas_device_priv_data->sas_target->handle != handle)
3018 continue;
3019 if (sas_device_priv_data->block)
3020 continue;
e4bc7f5c
SR
3021 if (sas_device->pend_sas_rphy_add)
3022 continue;
30158dc9
SS
3023 if (sas_device_priv_data->ignore_delay_remove) {
3024 sdev_printk(KERN_INFO, sdev,
3025 "%s skip device_block for SES handle(0x%04x)\n",
3026 __func__, sas_device_priv_data->sas_target->handle);
3027 continue;
3028 }
df838f92 3029 _scsih_internal_device_block(sdev, sas_device_priv_data);
f92363d1 3030 }
d1cb5e49
SR
3031
3032 sas_device_put(sas_device);
f92363d1
SR
3033}
3034
3035/**
3036 * _scsih_block_io_to_children_attached_to_ex
3037 * @ioc: per adapter object
3038 * @sas_expander: the sas_device object
3039 *
3040 * This routine set sdev state to SDEV_BLOCK for all devices
3041 * attached to this expander. This function called when expander is
3042 * pulled.
3043 */
3044static void
3045_scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
3046 struct _sas_node *sas_expander)
3047{
3048 struct _sas_port *mpt3sas_port;
3049 struct _sas_device *sas_device;
3050 struct _sas_node *expander_sibling;
3051 unsigned long flags;
3052
3053 if (!sas_expander)
3054 return;
3055
3056 list_for_each_entry(mpt3sas_port,
3057 &sas_expander->sas_port_list, port_list) {
3058 if (mpt3sas_port->remote_identify.device_type ==
3059 SAS_END_DEVICE) {
3060 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
3061 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3062 mpt3sas_port->remote_identify.sas_address);
3063 if (sas_device) {
f92363d1 3064 set_bit(sas_device->handle,
d1cb5e49
SR
3065 ioc->blocking_handles);
3066 sas_device_put(sas_device);
3067 }
f92363d1
SR
3068 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3069 }
3070 }
3071
3072 list_for_each_entry(mpt3sas_port,
3073 &sas_expander->sas_port_list, port_list) {
3074
3075 if (mpt3sas_port->remote_identify.device_type ==
3076 SAS_EDGE_EXPANDER_DEVICE ||
3077 mpt3sas_port->remote_identify.device_type ==
3078 SAS_FANOUT_EXPANDER_DEVICE) {
3079 expander_sibling =
3080 mpt3sas_scsih_expander_find_by_sas_address(
3081 ioc, mpt3sas_port->remote_identify.sas_address);
3082 _scsih_block_io_to_children_attached_to_ex(ioc,
3083 expander_sibling);
3084 }
3085 }
3086}
3087
3088/**
3089 * _scsih_block_io_to_children_attached_directly
3090 * @ioc: per adapter object
3091 * @event_data: topology change event data
3092 *
3093 * This routine set sdev state to SDEV_BLOCK for all devices
3094 * direct attached during device pull.
3095 */
3096static void
3097_scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3098 Mpi2EventDataSasTopologyChangeList_t *event_data)
3099{
3100 int i;
3101 u16 handle;
3102 u16 reason_code;
f92363d1
SR
3103
3104 for (i = 0; i < event_data->NumEntries; i++) {
3105 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3106 if (!handle)
3107 continue;
f92363d1
SR
3108 reason_code = event_data->PHY[i].PhyStatus &
3109 MPI2_EVENT_SAS_TOPO_RC_MASK;
3110 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3111 _scsih_block_io_device(ioc, handle);
3112 }
3113}
3114
3115/**
3116 * _scsih_tm_tr_send - send task management request
3117 * @ioc: per adapter object
3118 * @handle: device handle
3119 * Context: interrupt time.
3120 *
3121 * This code is to initiate the device removal handshake protocol
3122 * with controller firmware. This function will issue target reset
3123 * using high priority request queue. It will send a sas iounit
3124 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3125 *
3126 * This is designed to send muliple task management request at the same
3127 * time to the fifo. If the fifo is full, we will append the request,
3128 * and process it in a future completion.
3129 */
3130static void
3131_scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3132{
3133 Mpi2SCSITaskManagementRequest_t *mpi_request;
3134 u16 smid;
d1cb5e49 3135 struct _sas_device *sas_device = NULL;
f92363d1
SR
3136 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3137 u64 sas_address = 0;
3138 unsigned long flags;
3139 struct _tr_list *delayed_tr;
3140 u32 ioc_state;
3141
3142 if (ioc->remove_host) {
3143 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3144 "%s: host has been removed: handle(0x%04x)\n",
3145 __func__, ioc->name, handle));
3146 return;
3147 } else if (ioc->pci_error_recovery) {
3148 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3149 "%s: host in pci error recovery: handle(0x%04x)\n",
3150 __func__, ioc->name,
3151 handle));
3152 return;
3153 }
3154 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3155 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3156 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3157 "%s: host is not operational: handle(0x%04x)\n",
3158 __func__, ioc->name,
3159 handle));
3160 return;
3161 }
3162
3163 /* if PD, then return */
3164 if (test_bit(handle, ioc->pd_handles))
3165 return;
3166
c696f7b8
SPS
3167 clear_bit(handle, ioc->pend_os_device_add);
3168
f92363d1 3169 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 3170 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
f92363d1
SR
3171 if (sas_device && sas_device->starget &&
3172 sas_device->starget->hostdata) {
3173 sas_target_priv_data = sas_device->starget->hostdata;
3174 sas_target_priv_data->deleted = 1;
3175 sas_address = sas_device->sas_address;
3176 }
3177 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3178
3179 if (sas_target_priv_data) {
3180 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3181 "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3182 ioc->name, handle,
3183 (unsigned long long)sas_address));
e6d45e3e
SR
3184 if (sas_device->enclosure_handle != 0)
3185 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3186 "setting delete flag:enclosure logical id(0x%016llx),"
3187 " slot(%d)\n", ioc->name, (unsigned long long)
3188 sas_device->enclosure_logical_id,
3189 sas_device->slot));
869817f9 3190 if (sas_device->connector_name[0] != '\0')
e6d45e3e
SR
3191 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3192 "setting delete flag: enclosure level(0x%04x),"
3193 " connector name( %s)\n", ioc->name,
3194 sas_device->enclosure_level,
3195 sas_device->connector_name));
f92363d1
SR
3196 _scsih_ublock_io_device(ioc, sas_address);
3197 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3198 }
3199
3200 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3201 if (!smid) {
3202 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3203 if (!delayed_tr)
d1cb5e49 3204 goto out;
f92363d1
SR
3205 INIT_LIST_HEAD(&delayed_tr->list);
3206 delayed_tr->handle = handle;
3207 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3208 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3209 "DELAYED:tr:handle(0x%04x), (open)\n",
3210 ioc->name, handle));
d1cb5e49 3211 goto out;
f92363d1
SR
3212 }
3213
3214 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3215 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3216 ioc->name, handle, smid,
3217 ioc->tm_tr_cb_idx));
3218 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3219 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3220 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3221 mpi_request->DevHandle = cpu_to_le16(handle);
3222 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
c696f7b8 3223 set_bit(handle, ioc->device_remove_in_progress);
81c16f83 3224 ioc->put_smid_hi_priority(ioc, smid, 0);
f92363d1 3225 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
d1cb5e49
SR
3226
3227out:
3228 if (sas_device)
3229 sas_device_put(sas_device);
f92363d1
SR
3230}
3231
3232/**
3233 * _scsih_tm_tr_complete -
3234 * @ioc: per adapter object
3235 * @smid: system request message index
3236 * @msix_index: MSIX table index supplied by the OS
3237 * @reply: reply message frame(lower 32bit addr)
3238 * Context: interrupt time.
3239 *
3240 * This is the target reset completion routine.
3241 * This code is part of the code to initiate the device removal
3242 * handshake protocol with controller firmware.
3243 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3244 *
3245 * Return 1 meaning mf should be freed from _base_interrupt
3246 * 0 means the mf is freed from this function.
3247 */
3248static u8
3249_scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3250 u32 reply)
3251{
3252 u16 handle;
3253 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3254 Mpi2SCSITaskManagementReply_t *mpi_reply =
3255 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3256 Mpi2SasIoUnitControlRequest_t *mpi_request;
3257 u16 smid_sas_ctrl;
3258 u32 ioc_state;
fd0331b3 3259 struct _sc_list *delayed_sc;
f92363d1
SR
3260
3261 if (ioc->remove_host) {
3262 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3263 "%s: host has been removed\n", __func__, ioc->name));
3264 return 1;
3265 } else if (ioc->pci_error_recovery) {
3266 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3267 "%s: host in pci error recovery\n", __func__,
3268 ioc->name));
3269 return 1;
3270 }
3271 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3272 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3273 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3274 "%s: host is not operational\n", __func__, ioc->name));
3275 return 1;
3276 }
3277 if (unlikely(!mpi_reply)) {
3278 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3279 ioc->name, __FILE__, __LINE__, __func__);
3280 return 1;
3281 }
3282 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3283 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3284 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3285 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3286 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3287 ioc->name, handle,
3288 le16_to_cpu(mpi_reply->DevHandle), smid));
3289 return 0;
3290 }
3291
3292 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3293 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3294 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3295 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3296 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3297 le32_to_cpu(mpi_reply->IOCLogInfo),
3298 le32_to_cpu(mpi_reply->TerminationCount)));
3299
3300 smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3301 if (!smid_sas_ctrl) {
fd0331b3
SS
3302 delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
3303 if (!delayed_sc)
3304 return _scsih_check_for_pending_tm(ioc, smid);
3305 INIT_LIST_HEAD(&delayed_sc->list);
3306 delayed_sc->handle = mpi_request_tm->DevHandle;
3307 list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
3308 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3309 "DELAYED:sc:handle(0x%04x), (open)\n",
3310 ioc->name, handle));
3311 return _scsih_check_for_pending_tm(ioc, smid);
f92363d1
SR
3312 }
3313
3314 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3315 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3316 ioc->name, handle, smid_sas_ctrl,
3317 ioc->tm_sas_control_cb_idx));
3318 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3319 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3320 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3321 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3322 mpi_request->DevHandle = mpi_request_tm->DevHandle;
81c16f83 3323 ioc->put_smid_default(ioc, smid_sas_ctrl);
f92363d1
SR
3324
3325 return _scsih_check_for_pending_tm(ioc, smid);
3326}
3327
3328
3329/**
3330 * _scsih_sas_control_complete - completion routine
3331 * @ioc: per adapter object
3332 * @smid: system request message index
3333 * @msix_index: MSIX table index supplied by the OS
3334 * @reply: reply message frame(lower 32bit addr)
3335 * Context: interrupt time.
3336 *
3337 * This is the sas iounit control completion routine.
3338 * This code is part of the code to initiate the device removal
3339 * handshake protocol with controller firmware.
3340 *
3341 * Return 1 meaning mf should be freed from _base_interrupt
3342 * 0 means the mf is freed from this function.
3343 */
3344static u8
3345_scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3346 u8 msix_index, u32 reply)
3347{
3348 Mpi2SasIoUnitControlReply_t *mpi_reply =
3349 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3350
3351 if (likely(mpi_reply)) {
3352 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3353 "sc_complete:handle(0x%04x), (open) "
3354 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3355 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3356 le16_to_cpu(mpi_reply->IOCStatus),
3357 le32_to_cpu(mpi_reply->IOCLogInfo)));
c696f7b8
SPS
3358 if (le16_to_cpu(mpi_reply->IOCStatus) ==
3359 MPI2_IOCSTATUS_SUCCESS) {
3360 clear_bit(le16_to_cpu(mpi_reply->DevHandle),
3361 ioc->device_remove_in_progress);
3362 }
f92363d1
SR
3363 } else {
3364 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3365 ioc->name, __FILE__, __LINE__, __func__);
3366 }
fd0331b3 3367 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
f92363d1
SR
3368}
3369
3370/**
3371 * _scsih_tm_tr_volume_send - send target reset request for volumes
3372 * @ioc: per adapter object
3373 * @handle: device handle
3374 * Context: interrupt time.
3375 *
3376 * This is designed to send muliple task management request at the same
3377 * time to the fifo. If the fifo is full, we will append the request,
3378 * and process it in a future completion.
3379 */
3380static void
3381_scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3382{
3383 Mpi2SCSITaskManagementRequest_t *mpi_request;
3384 u16 smid;
3385 struct _tr_list *delayed_tr;
3386
3387 if (ioc->shost_recovery || ioc->remove_host ||
3388 ioc->pci_error_recovery) {
3389 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3390 "%s: host reset in progress!\n",
3391 __func__, ioc->name));
3392 return;
3393 }
3394
3395 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3396 if (!smid) {
3397 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3398 if (!delayed_tr)
3399 return;
3400 INIT_LIST_HEAD(&delayed_tr->list);
3401 delayed_tr->handle = handle;
3402 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3403 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3404 "DELAYED:tr:handle(0x%04x), (open)\n",
3405 ioc->name, handle));
3406 return;
3407 }
3408
3409 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3410 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3411 ioc->name, handle, smid,
3412 ioc->tm_tr_volume_cb_idx));
3413 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3414 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3415 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3416 mpi_request->DevHandle = cpu_to_le16(handle);
3417 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
81c16f83 3418 ioc->put_smid_hi_priority(ioc, smid, 0);
f92363d1
SR
3419}
3420
3421/**
3422 * _scsih_tm_volume_tr_complete - target reset completion
3423 * @ioc: per adapter object
3424 * @smid: system request message index
3425 * @msix_index: MSIX table index supplied by the OS
3426 * @reply: reply message frame(lower 32bit addr)
3427 * Context: interrupt time.
3428 *
3429 * Return 1 meaning mf should be freed from _base_interrupt
3430 * 0 means the mf is freed from this function.
3431 */
3432static u8
3433_scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3434 u8 msix_index, u32 reply)
3435{
3436 u16 handle;
3437 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3438 Mpi2SCSITaskManagementReply_t *mpi_reply =
3439 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3440
3441 if (ioc->shost_recovery || ioc->remove_host ||
3442 ioc->pci_error_recovery) {
3443 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3444 "%s: host reset in progress!\n",
3445 __func__, ioc->name));
3446 return 1;
3447 }
3448 if (unlikely(!mpi_reply)) {
3449 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3450 ioc->name, __FILE__, __LINE__, __func__);
3451 return 1;
3452 }
3453
3454 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3455 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3456 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3457 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3458 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3459 ioc->name, handle,
3460 le16_to_cpu(mpi_reply->DevHandle), smid));
3461 return 0;
3462 }
3463
3464 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3465 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3466 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3467 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3468 le32_to_cpu(mpi_reply->IOCLogInfo),
3469 le32_to_cpu(mpi_reply->TerminationCount)));
3470
3471 return _scsih_check_for_pending_tm(ioc, smid);
3472}
3473
fd0331b3
SS
3474/**
3475 * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
3476 * @ioc: per adapter object
3477 * @smid: system request message index
3478 * @event: Event ID
3479 * @event_context: used to track events uniquely
3480 *
3481 * Context - processed in interrupt context.
3482 */
8bbb1cf6 3483static void
fd0331b3
SS
3484_scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 event,
3485 u32 event_context)
3486{
3487 Mpi2EventAckRequest_t *ack_request;
3488 int i = smid - ioc->internal_smid;
3489 unsigned long flags;
3490
3491 /* Without releasing the smid just update the
3492 * call back index and reuse the same smid for
3493 * processing this delayed request
3494 */
3495 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3496 ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
3497 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3498
3499 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3500 "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
3501 ioc->name, le16_to_cpu(event), smid,
3502 ioc->base_cb_idx));
3503 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
3504 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
3505 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
3506 ack_request->Event = event;
3507 ack_request->EventContext = event_context;
3508 ack_request->VF_ID = 0; /* TODO */
3509 ack_request->VP_ID = 0;
81c16f83 3510 ioc->put_smid_default(ioc, smid);
fd0331b3
SS
3511}
3512
3513/**
3514 * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
3515 * sas_io_unit_ctrl messages
3516 * @ioc: per adapter object
3517 * @smid: system request message index
3518 * @handle: device handle
3519 *
3520 * Context - processed in interrupt context.
3521 */
8bbb1cf6 3522static void
fd0331b3
SS
3523_scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
3524 u16 smid, u16 handle)
3525 {
3526 Mpi2SasIoUnitControlRequest_t *mpi_request;
3527 u32 ioc_state;
3528 int i = smid - ioc->internal_smid;
3529 unsigned long flags;
3530
3531 if (ioc->remove_host) {
3532 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3533 "%s: host has been removed\n",
3534 __func__, ioc->name));
3535 return;
3536 } else if (ioc->pci_error_recovery) {
3537 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3538 "%s: host in pci error recovery\n",
3539 __func__, ioc->name));
3540 return;
3541 }
3542 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3543 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3544 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3545 "%s: host is not operational\n",
3546 __func__, ioc->name));
3547 return;
3548 }
3549
3550 /* Without releasing the smid just update the
3551 * call back index and reuse the same smid for
3552 * processing this delayed request
3553 */
3554 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3555 ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
3556 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3557
3558 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3559 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3560 ioc->name, le16_to_cpu(handle), smid,
3561 ioc->tm_sas_control_cb_idx));
3562 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3563 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3564 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3565 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3566 mpi_request->DevHandle = handle;
81c16f83 3567 ioc->put_smid_default(ioc, smid);
fd0331b3
SS
3568}
3569
3570/**
3571 * _scsih_check_for_pending_internal_cmds - check for pending internal messages
3572 * @ioc: per adapter object
3573 * @smid: system request message index
3574 *
3575 * Context: Executed in interrupt context
3576 *
3577 * This will check delayed internal messages list, and process the
3578 * next request.
3579 *
3580 * Return 1 meaning mf should be freed from _base_interrupt
3581 * 0 means the mf is freed from this function.
3582 */
3583u8
3584mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3585{
3586 struct _sc_list *delayed_sc;
3587 struct _event_ack_list *delayed_event_ack;
3588
3589 if (!list_empty(&ioc->delayed_event_ack_list)) {
3590 delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
3591 struct _event_ack_list, list);
3592 _scsih_issue_delayed_event_ack(ioc, smid,
3593 delayed_event_ack->Event, delayed_event_ack->EventContext);
3594 list_del(&delayed_event_ack->list);
3595 kfree(delayed_event_ack);
3596 return 0;
3597 }
3598
3599 if (!list_empty(&ioc->delayed_sc_list)) {
3600 delayed_sc = list_entry(ioc->delayed_sc_list.next,
3601 struct _sc_list, list);
3602 _scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
3603 delayed_sc->handle);
3604 list_del(&delayed_sc->list);
3605 kfree(delayed_sc);
3606 return 0;
3607 }
3608 return 1;
3609}
f92363d1
SR
3610
3611/**
3612 * _scsih_check_for_pending_tm - check for pending task management
3613 * @ioc: per adapter object
3614 * @smid: system request message index
3615 *
3616 * This will check delayed target reset list, and feed the
3617 * next reqeust.
3618 *
3619 * Return 1 meaning mf should be freed from _base_interrupt
3620 * 0 means the mf is freed from this function.
3621 */
3622static u8
3623_scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3624{
3625 struct _tr_list *delayed_tr;
3626
3627 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3628 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3629 struct _tr_list, list);
3630 mpt3sas_base_free_smid(ioc, smid);
3631 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3632 list_del(&delayed_tr->list);
3633 kfree(delayed_tr);
3634 return 0;
3635 }
3636
3637 if (!list_empty(&ioc->delayed_tr_list)) {
3638 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3639 struct _tr_list, list);
3640 mpt3sas_base_free_smid(ioc, smid);
3641 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3642 list_del(&delayed_tr->list);
3643 kfree(delayed_tr);
3644 return 0;
3645 }
3646
3647 return 1;
3648}
3649
3650/**
3651 * _scsih_check_topo_delete_events - sanity check on topo events
3652 * @ioc: per adapter object
3653 * @event_data: the event data payload
3654 *
3655 * This routine added to better handle cable breaker.
3656 *
3657 * This handles the case where driver receives multiple expander
3658 * add and delete events in a single shot. When there is a delete event
3659 * the routine will void any pending add events waiting in the event queue.
3660 *
3661 * Return nothing.
3662 */
3663static void
3664_scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3665 Mpi2EventDataSasTopologyChangeList_t *event_data)
3666{
3667 struct fw_event_work *fw_event;
3668 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3669 u16 expander_handle;
3670 struct _sas_node *sas_expander;
3671 unsigned long flags;
3672 int i, reason_code;
3673 u16 handle;
3674
3675 for (i = 0 ; i < event_data->NumEntries; i++) {
3676 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3677 if (!handle)
3678 continue;
3679 reason_code = event_data->PHY[i].PhyStatus &
3680 MPI2_EVENT_SAS_TOPO_RC_MASK;
3681 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3682 _scsih_tm_tr_send(ioc, handle);
3683 }
3684
3685 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3686 if (expander_handle < ioc->sas_hba.num_phys) {
3687 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3688 return;
3689 }
3690 if (event_data->ExpStatus ==
3691 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3692 /* put expander attached devices into blocking state */
3693 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3694 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3695 expander_handle);
3696 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3697 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3698 do {
3699 handle = find_first_bit(ioc->blocking_handles,
3700 ioc->facts.MaxDevHandle);
3701 if (handle < ioc->facts.MaxDevHandle)
3702 _scsih_block_io_device(ioc, handle);
3703 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3704 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3705 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3706
3707 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3708 return;
3709
3710 /* mark ignore flag for pending events */
3711 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3712 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3713 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3714 fw_event->ignore)
3715 continue;
35b62362
JL
3716 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3717 fw_event->event_data;
f92363d1
SR
3718 if (local_event_data->ExpStatus ==
3719 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3720 local_event_data->ExpStatus ==
3721 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3722 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3723 expander_handle) {
3724 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3725 "setting ignoring flag\n", ioc->name));
3726 fw_event->ignore = 1;
3727 }
3728 }
3729 }
3730 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3731}
3732
3733/**
3734 * _scsih_set_volume_delete_flag - setting volume delete flag
3735 * @ioc: per adapter object
3736 * @handle: device handle
3737 *
3738 * This returns nothing.
3739 */
3740static void
3741_scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3742{
3743 struct _raid_device *raid_device;
3744 struct MPT3SAS_TARGET *sas_target_priv_data;
3745 unsigned long flags;
3746
3747 spin_lock_irqsave(&ioc->raid_device_lock, flags);
c84b06a4 3748 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
3749 if (raid_device && raid_device->starget &&
3750 raid_device->starget->hostdata) {
3751 sas_target_priv_data =
3752 raid_device->starget->hostdata;
3753 sas_target_priv_data->deleted = 1;
3754 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3755 "setting delete flag: handle(0x%04x), "
3756 "wwid(0x%016llx)\n", ioc->name, handle,
3757 (unsigned long long) raid_device->wwid));
3758 }
3759 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3760}
3761
3762/**
3763 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3764 * @handle: input handle
3765 * @a: handle for volume a
3766 * @b: handle for volume b
3767 *
3768 * IR firmware only supports two raid volumes. The purpose of this
3769 * routine is to set the volume handle in either a or b. When the given
3770 * input handle is non-zero, or when a and b have not been set before.
3771 */
3772static void
3773_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3774{
3775 if (!handle || handle == *a || handle == *b)
3776 return;
3777 if (!*a)
3778 *a = handle;
3779 else if (!*b)
3780 *b = handle;
3781}
3782
3783/**
3784 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3785 * @ioc: per adapter object
3786 * @event_data: the event data payload
3787 * Context: interrupt time.
3788 *
3789 * This routine will send target reset to volume, followed by target
3790 * resets to the PDs. This is called when a PD has been removed, or
3791 * volume has been deleted or removed. When the target reset is sent
3792 * to volume, the PD target resets need to be queued to start upon
3793 * completion of the volume target reset.
3794 *
3795 * Return nothing.
3796 */
3797static void
3798_scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3799 Mpi2EventDataIrConfigChangeList_t *event_data)
3800{
3801 Mpi2EventIrConfigElement_t *element;
3802 int i;
3803 u16 handle, volume_handle, a, b;
3804 struct _tr_list *delayed_tr;
3805
3806 a = 0;
3807 b = 0;
3808
7786ab6a
SR
3809 if (ioc->is_warpdrive)
3810 return;
3811
f92363d1
SR
3812 /* Volume Resets for Deleted or Removed */
3813 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3814 for (i = 0; i < event_data->NumElements; i++, element++) {
3815 if (le32_to_cpu(event_data->Flags) &
3816 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3817 continue;
3818 if (element->ReasonCode ==
3819 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3820 element->ReasonCode ==
3821 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3822 volume_handle = le16_to_cpu(element->VolDevHandle);
3823 _scsih_set_volume_delete_flag(ioc, volume_handle);
3824 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3825 }
3826 }
3827
3828 /* Volume Resets for UNHIDE events */
3829 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3830 for (i = 0; i < event_data->NumElements; i++, element++) {
3831 if (le32_to_cpu(event_data->Flags) &
3832 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3833 continue;
3834 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3835 volume_handle = le16_to_cpu(element->VolDevHandle);
3836 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3837 }
3838 }
3839
3840 if (a)
3841 _scsih_tm_tr_volume_send(ioc, a);
3842 if (b)
3843 _scsih_tm_tr_volume_send(ioc, b);
3844
3845 /* PD target resets */
3846 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3847 for (i = 0; i < event_data->NumElements; i++, element++) {
3848 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3849 continue;
3850 handle = le16_to_cpu(element->PhysDiskDevHandle);
3851 volume_handle = le16_to_cpu(element->VolDevHandle);
3852 clear_bit(handle, ioc->pd_handles);
3853 if (!volume_handle)
3854 _scsih_tm_tr_send(ioc, handle);
3855 else if (volume_handle == a || volume_handle == b) {
3856 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3857 BUG_ON(!delayed_tr);
3858 INIT_LIST_HEAD(&delayed_tr->list);
3859 delayed_tr->handle = handle;
3860 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3861 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3862 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3863 handle));
3864 } else
3865 _scsih_tm_tr_send(ioc, handle);
3866 }
3867}
3868
3869
3870/**
3871 * _scsih_check_volume_delete_events - set delete flag for volumes
3872 * @ioc: per adapter object
3873 * @event_data: the event data payload
3874 * Context: interrupt time.
3875 *
3876 * This will handle the case when the cable connected to entire volume is
3877 * pulled. We will take care of setting the deleted flag so normal IO will
3878 * not be sent.
3879 *
3880 * Return nothing.
3881 */
3882static void
3883_scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3884 Mpi2EventDataIrVolume_t *event_data)
3885{
3886 u32 state;
3887
3888 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3889 return;
3890 state = le32_to_cpu(event_data->NewValue);
3891 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3892 MPI2_RAID_VOL_STATE_FAILED)
3893 _scsih_set_volume_delete_flag(ioc,
3894 le16_to_cpu(event_data->VolDevHandle));
3895}
3896
2d8ce8c9
SR
3897/**
3898 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3899 * @ioc: per adapter object
3900 * @event_data: the temp threshold event data
3901 * Context: interrupt time.
3902 *
3903 * Return nothing.
3904 */
3905static void
3906_scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3907 Mpi2EventDataTemperature_t *event_data)
3908{
3909 if (ioc->temp_sensors_count >= event_data->SensorNum) {
3910 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3911 " exceeded for Sensor: %d !!!\n", ioc->name,
3912 ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3913 ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3914 ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3915 ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3916 event_data->SensorNum);
3917 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3918 ioc->name, event_data->CurrentTemperature);
3919 }
3920}
3921
ffb58456 3922static int _scsih_set_satl_pending(struct scsi_cmnd *scmd, bool pending)
7ff723ad 3923{
ffb58456
JB
3924 struct MPT3SAS_DEVICE *priv = scmd->device->hostdata;
3925
3926 if (scmd->cmnd[0] != ATA_12 && scmd->cmnd[0] != ATA_16)
3927 return 0;
3928
3929 if (pending)
3930 return test_and_set_bit(0, &priv->ata_command_pending);
3931
3932 clear_bit(0, &priv->ata_command_pending);
3933 return 0;
7ff723ad
SP
3934}
3935
f92363d1
SR
3936/**
3937 * _scsih_flush_running_cmds - completing outstanding commands.
3938 * @ioc: per adapter object
3939 *
3940 * The flushing out of all pending scmd commands following host reset,
3941 * where all IO is dropped to the floor.
3942 *
3943 * Return nothing.
3944 */
3945static void
3946_scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3947{
3948 struct scsi_cmnd *scmd;
3949 u16 smid;
3950 u16 count = 0;
3951
3952 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3953 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3954 if (!scmd)
3955 continue;
3956 count++;
ffb58456 3957 _scsih_set_satl_pending(scmd, false);
f92363d1
SR
3958 mpt3sas_base_free_smid(ioc, smid);
3959 scsi_dma_unmap(scmd);
3960 if (ioc->pci_error_recovery)
3961 scmd->result = DID_NO_CONNECT << 16;
3962 else
3963 scmd->result = DID_RESET << 16;
3964 scmd->scsi_done(scmd);
3965 }
3966 dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3967 ioc->name, count));
3968}
3969
3970/**
3971 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3972 * @ioc: per adapter object
3973 * @scmd: pointer to scsi command object
6c7abffc 3974 * @mpi_request: pointer to the SCSI_IO request message frame
f92363d1
SR
3975 *
3976 * Supporting protection 1 and 3.
3977 *
3978 * Returns nothing
3979 */
3980static void
3981_scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3982 Mpi2SCSIIORequest_t *mpi_request)
3983{
3984 u16 eedp_flags;
3985 unsigned char prot_op = scsi_get_prot_op(scmd);
3986 unsigned char prot_type = scsi_get_prot_type(scmd);
3987 Mpi25SCSIIORequest_t *mpi_request_3v =
3988 (Mpi25SCSIIORequest_t *)mpi_request;
3989
3990 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3991 return;
3992
3993 if (prot_op == SCSI_PROT_READ_STRIP)
3994 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3995 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3996 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3997 else
3998 return;
3999
4000 switch (prot_type) {
4001 case SCSI_PROT_DIF_TYPE1:
4002 case SCSI_PROT_DIF_TYPE2:
4003
4004 /*
4005 * enable ref/guard checking
4006 * auto increment ref tag
4007 */
4008 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
4009 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
4010 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
4011 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
648512cc 4012 cpu_to_be32(scsi_prot_ref_tag(scmd));
f92363d1
SR
4013 break;
4014
4015 case SCSI_PROT_DIF_TYPE3:
4016
4017 /*
4018 * enable guard checking
4019 */
4020 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
4021
4022 break;
4023 }
4024
4025 mpi_request_3v->EEDPBlockSize =
4026 cpu_to_le16(scmd->device->sector_size);
186a18e5
SPS
4027
4028 if (ioc->is_gen35_ioc)
4029 eedp_flags |= MPI25_SCSIIO_EEDPFLAGS_APPTAG_DISABLE_MODE;
f92363d1
SR
4030 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
4031}
4032
4033/**
4034 * _scsih_eedp_error_handling - return sense code for EEDP errors
4035 * @scmd: pointer to scsi command object
4036 * @ioc_status: ioc status
4037 *
4038 * Returns nothing
4039 */
4040static void
4041_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
4042{
4043 u8 ascq;
4044
4045 switch (ioc_status) {
4046 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4047 ascq = 0x01;
4048 break;
4049 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4050 ascq = 0x02;
4051 break;
4052 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4053 ascq = 0x03;
4054 break;
4055 default:
4056 ascq = 0x00;
4057 break;
4058 }
4059 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
4060 ascq);
4061 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
4062 SAM_STAT_CHECK_CONDITION;
4063}
4064
f92363d1 4065/**
8a7e4c24 4066 * scsih_qcmd - main scsi request entry point
f92363d1
SR
4067 * @scmd: pointer to scsi command object
4068 * @done: function pointer to be invoked on completion
4069 *
4070 * The callback index is set inside `ioc->scsi_io_cb_idx`.
4071 *
4072 * Returns 0 on success. If there's a failure, return either:
4073 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
4074 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
4075 */
8bbb1cf6 4076static int
8a7e4c24 4077scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
f92363d1 4078{
d8bfbd8d 4079 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
f92363d1
SR
4080 struct MPT3SAS_DEVICE *sas_device_priv_data;
4081 struct MPT3SAS_TARGET *sas_target_priv_data;
7786ab6a 4082 struct _raid_device *raid_device;
307d9075
AM
4083 struct request *rq = scmd->request;
4084 int class;
f92363d1
SR
4085 Mpi2SCSIIORequest_t *mpi_request;
4086 u32 mpi_control;
4087 u16 smid;
4088 u16 handle;
4089
f92363d1
SR
4090 if (ioc->logging_level & MPT_DEBUG_SCSI)
4091 scsi_print_command(scmd);
f92363d1 4092
f92363d1
SR
4093 sas_device_priv_data = scmd->device->hostdata;
4094 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
4095 scmd->result = DID_NO_CONNECT << 16;
4096 scmd->scsi_done(scmd);
4097 return 0;
4098 }
4099
4100 if (ioc->pci_error_recovery || ioc->remove_host) {
4101 scmd->result = DID_NO_CONNECT << 16;
4102 scmd->scsi_done(scmd);
4103 return 0;
4104 }
4105
ffb58456
JB
4106 /*
4107 * Bug work around for firmware SATL handling. The loop
4108 * is based on atomic operations and ensures consistency
4109 * since we're lockless at this point
4110 */
4111 do {
4112 if (test_bit(0, &sas_device_priv_data->ata_command_pending)) {
4113 scmd->result = SAM_STAT_BUSY;
4114 scmd->scsi_done(scmd);
4115 return 0;
4116 }
4117 } while (_scsih_set_satl_pending(scmd, true));
4118
f92363d1
SR
4119 sas_target_priv_data = sas_device_priv_data->sas_target;
4120
4121 /* invalid device handle */
4122 handle = sas_target_priv_data->handle;
4123 if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
4124 scmd->result = DID_NO_CONNECT << 16;
4125 scmd->scsi_done(scmd);
4126 return 0;
4127 }
4128
4129
4130 /* host recovery or link resets sent via IOCTLs */
4131 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
4132 return SCSI_MLQUEUE_HOST_BUSY;
4133
4134 /* device has been deleted */
4135 else if (sas_target_priv_data->deleted) {
4136 scmd->result = DID_NO_CONNECT << 16;
4137 scmd->scsi_done(scmd);
4138 return 0;
6c7abffc 4139 /* device busy with task management */
f92363d1
SR
4140 } else if (sas_target_priv_data->tm_busy ||
4141 sas_device_priv_data->block)
4142 return SCSI_MLQUEUE_DEVICE_BUSY;
4143
4144 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4145 mpi_control = MPI2_SCSIIO_CONTROL_READ;
4146 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4147 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
4148 else
4149 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
4150
4151 /* set tags */
609aa22f 4152 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
307d9075
AM
4153 /* NCQ Prio supported, make sure control indicated high priority */
4154 if (sas_device_priv_data->ncq_prio_enable) {
4155 class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4156 if (class == IOPRIO_CLASS_RT)
4157 mpi_control |= 1 << MPI2_SCSIIO_CONTROL_CMDPRI_SHIFT;
4158 }
7786ab6a
SR
4159 /* Make sure Device is not raid volume.
4160 * We do not expose raid functionality to upper layer for warpdrive.
4161 */
4162 if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
15de0de2 4163 && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
f92363d1
SR
4164 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4165
4166 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
4167 if (!smid) {
4168 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4169 ioc->name, __func__);
4170 goto out;
4171 }
4172 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4173 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
4174 _scsih_setup_eedp(ioc, scmd, mpi_request);
4175
4176 if (scmd->cmd_len == 32)
4177 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
4178 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4179 if (sas_device_priv_data->sas_target->flags &
4180 MPT_TARGET_FLAGS_RAID_COMPONENT)
4181 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4182 else
4183 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4184 mpi_request->DevHandle = cpu_to_le16(handle);
4185 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4186 mpi_request->Control = cpu_to_le32(mpi_control);
4187 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4188 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4189 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4190 mpi_request->SenseBufferLowAddress =
4191 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
4192 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4193 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4194 mpi_request->LUN);
4195 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4196
4197 if (mpi_request->DataLength) {
4198 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
4199 mpt3sas_base_free_smid(ioc, smid);
4200 goto out;
4201 }
4202 } else
4203 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
4204
7786ab6a
SR
4205 raid_device = sas_target_priv_data->raid_device;
4206 if (raid_device && raid_device->direct_io_enabled)
c84b06a4 4207 mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
7786ab6a 4208 smid);
7786ab6a 4209
f92363d1
SR
4210 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
4211 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
4212 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
4213 MPI25_SCSIIO_IOFLAGS_FAST_PATH);
81c16f83 4214 ioc->put_smid_fast_path(ioc, smid, handle);
f92363d1 4215 } else
81c16f83 4216 ioc->put_smid_scsi_io(ioc, smid,
7786ab6a 4217 le16_to_cpu(mpi_request->DevHandle));
f92363d1 4218 } else
81c16f83 4219 ioc->put_smid_default(ioc, smid);
f92363d1
SR
4220 return 0;
4221
4222 out:
4223 return SCSI_MLQUEUE_HOST_BUSY;
4224}
f92363d1
SR
4225
4226/**
4227 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4228 * @sense_buffer: sense data returned by target
4229 * @data: normalized skey/asc/ascq
4230 *
4231 * Return nothing.
4232 */
4233static void
4234_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4235{
4236 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4237 /* descriptor format */
4238 data->skey = sense_buffer[1] & 0x0F;
4239 data->asc = sense_buffer[2];
4240 data->ascq = sense_buffer[3];
4241 } else {
4242 /* fixed format */
4243 data->skey = sense_buffer[2] & 0x0F;
4244 data->asc = sense_buffer[12];
4245 data->ascq = sense_buffer[13];
4246 }
4247}
4248
f92363d1
SR
4249/**
4250 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4251 * @ioc: per adapter object
4252 * @scmd: pointer to scsi command object
4253 * @mpi_reply: reply mf payload returned from firmware
4254 *
4255 * scsi_status - SCSI Status code returned from target device
4256 * scsi_state - state info associated with SCSI_IO determined by ioc
4257 * ioc_status - ioc supplied status info
4258 *
4259 * Return nothing.
4260 */
4261static void
4262_scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4263 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4264{
4265 u32 response_info;
4266 u8 *response_bytes;
4267 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4268 MPI2_IOCSTATUS_MASK;
4269 u8 scsi_state = mpi_reply->SCSIState;
4270 u8 scsi_status = mpi_reply->SCSIStatus;
4271 char *desc_ioc_state = NULL;
4272 char *desc_scsi_status = NULL;
4273 char *desc_scsi_state = ioc->tmp_string;
4274 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4275 struct _sas_device *sas_device = NULL;
f92363d1
SR
4276 struct scsi_target *starget = scmd->device->sdev_target;
4277 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4278 char *device_str = NULL;
4279
4280 if (!priv_target)
4281 return;
7786ab6a
SR
4282 if (ioc->hide_ir_msg)
4283 device_str = "WarpDrive";
4284 else
4285 device_str = "volume";
f92363d1
SR
4286
4287 if (log_info == 0x31170000)
4288 return;
4289
4290 switch (ioc_status) {
4291 case MPI2_IOCSTATUS_SUCCESS:
4292 desc_ioc_state = "success";
4293 break;
4294 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4295 desc_ioc_state = "invalid function";
4296 break;
4297 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4298 desc_ioc_state = "scsi recovered error";
4299 break;
4300 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4301 desc_ioc_state = "scsi invalid dev handle";
4302 break;
4303 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4304 desc_ioc_state = "scsi device not there";
4305 break;
4306 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4307 desc_ioc_state = "scsi data overrun";
4308 break;
4309 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4310 desc_ioc_state = "scsi data underrun";
4311 break;
4312 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4313 desc_ioc_state = "scsi io data error";
4314 break;
4315 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4316 desc_ioc_state = "scsi protocol error";
4317 break;
4318 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4319 desc_ioc_state = "scsi task terminated";
4320 break;
4321 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4322 desc_ioc_state = "scsi residual mismatch";
4323 break;
4324 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4325 desc_ioc_state = "scsi task mgmt failed";
4326 break;
4327 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4328 desc_ioc_state = "scsi ioc terminated";
4329 break;
4330 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4331 desc_ioc_state = "scsi ext terminated";
4332 break;
4333 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4334 desc_ioc_state = "eedp guard error";
4335 break;
4336 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4337 desc_ioc_state = "eedp ref tag error";
4338 break;
4339 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4340 desc_ioc_state = "eedp app tag error";
4341 break;
b130b0d5
SS
4342 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4343 desc_ioc_state = "insufficient power";
4344 break;
f92363d1
SR
4345 default:
4346 desc_ioc_state = "unknown";
4347 break;
4348 }
4349
4350 switch (scsi_status) {
4351 case MPI2_SCSI_STATUS_GOOD:
4352 desc_scsi_status = "good";
4353 break;
4354 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4355 desc_scsi_status = "check condition";
4356 break;
4357 case MPI2_SCSI_STATUS_CONDITION_MET:
4358 desc_scsi_status = "condition met";
4359 break;
4360 case MPI2_SCSI_STATUS_BUSY:
4361 desc_scsi_status = "busy";
4362 break;
4363 case MPI2_SCSI_STATUS_INTERMEDIATE:
4364 desc_scsi_status = "intermediate";
4365 break;
4366 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4367 desc_scsi_status = "intermediate condmet";
4368 break;
4369 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4370 desc_scsi_status = "reservation conflict";
4371 break;
4372 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4373 desc_scsi_status = "command terminated";
4374 break;
4375 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4376 desc_scsi_status = "task set full";
4377 break;
4378 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4379 desc_scsi_status = "aca active";
4380 break;
4381 case MPI2_SCSI_STATUS_TASK_ABORTED:
4382 desc_scsi_status = "task aborted";
4383 break;
4384 default:
4385 desc_scsi_status = "unknown";
4386 break;
4387 }
4388
4389 desc_scsi_state[0] = '\0';
4390 if (!scsi_state)
4391 desc_scsi_state = " ";
4392 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4393 strcat(desc_scsi_state, "response info ");
4394 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4395 strcat(desc_scsi_state, "state terminated ");
4396 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4397 strcat(desc_scsi_state, "no status ");
4398 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4399 strcat(desc_scsi_state, "autosense failed ");
4400 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4401 strcat(desc_scsi_state, "autosense valid ");
4402
4403 scsi_print_command(scmd);
4404
4405 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4406 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4407 device_str, (unsigned long long)priv_target->sas_address);
4408 } else {
d1cb5e49 4409 sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
f92363d1
SR
4410 if (sas_device) {
4411 pr_warn(MPT3SAS_FMT
4412 "\tsas_address(0x%016llx), phy(%d)\n",
4413 ioc->name, (unsigned long long)
4414 sas_device->sas_address, sas_device->phy);
e6d45e3e
SR
4415 if (sas_device->enclosure_handle != 0)
4416 pr_warn(MPT3SAS_FMT
4417 "\tenclosure_logical_id(0x%016llx),"
4418 "slot(%d)\n", ioc->name,
4419 (unsigned long long)
4420 sas_device->enclosure_logical_id,
4421 sas_device->slot);
4422 if (sas_device->connector_name[0])
4423 pr_warn(MPT3SAS_FMT
4424 "\tenclosure level(0x%04x),"
4425 " connector name( %s)\n", ioc->name,
4426 sas_device->enclosure_level,
4427 sas_device->connector_name);
d1cb5e49
SR
4428
4429 sas_device_put(sas_device);
f92363d1 4430 }
f92363d1
SR
4431 }
4432
4433 pr_warn(MPT3SAS_FMT
4434 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4435 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4436 desc_ioc_state, ioc_status, smid);
4437 pr_warn(MPT3SAS_FMT
4438 "\trequest_len(%d), underflow(%d), resid(%d)\n",
4439 ioc->name, scsi_bufflen(scmd), scmd->underflow,
4440 scsi_get_resid(scmd));
4441 pr_warn(MPT3SAS_FMT
4442 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4443 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4444 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4445 pr_warn(MPT3SAS_FMT
4446 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4447 ioc->name, desc_scsi_status,
4448 scsi_status, desc_scsi_state, scsi_state);
4449
4450 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4451 struct sense_info data;
4452 _scsih_normalize_sense(scmd->sense_buffer, &data);
4453 pr_warn(MPT3SAS_FMT
4454 "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4455 ioc->name, data.skey,
4456 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4457 }
4458
4459 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4460 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4461 response_bytes = (u8 *)&response_info;
4462 _scsih_response_code(ioc, response_bytes[0]);
4463 }
4464}
f92363d1
SR
4465
4466/**
0f624c39 4467 * _scsih_turn_on_pfa_led - illuminate PFA LED
f92363d1
SR
4468 * @ioc: per adapter object
4469 * @handle: device handle
4470 * Context: process
4471 *
4472 * Return nothing.
4473 */
4474static void
0f624c39 4475_scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
f92363d1
SR
4476{
4477 Mpi2SepReply_t mpi_reply;
4478 Mpi2SepRequest_t mpi_request;
0f624c39
SR
4479 struct _sas_device *sas_device;
4480
d1cb5e49 4481 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
0f624c39
SR
4482 if (!sas_device)
4483 return;
f92363d1
SR
4484
4485 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4486 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4487 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4488 mpi_request.SlotStatus =
4489 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4490 mpi_request.DevHandle = cpu_to_le16(handle);
4491 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4492 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4493 &mpi_request)) != 0) {
4494 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4495 __FILE__, __LINE__, __func__);
d1cb5e49 4496 goto out;
f92363d1 4497 }
0f624c39 4498 sas_device->pfa_led_on = 1;
f92363d1
SR
4499
4500 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4501 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4502 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4503 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4504 le32_to_cpu(mpi_reply.IOCLogInfo)));
d1cb5e49 4505 goto out;
f92363d1 4506 }
d1cb5e49
SR
4507out:
4508 sas_device_put(sas_device);
f92363d1 4509}
d1cb5e49 4510
0f624c39
SR
4511/**
4512 * _scsih_turn_off_pfa_led - turn off Fault LED
4513 * @ioc: per adapter object
4514 * @sas_device: sas device whose PFA LED has to turned off
4515 * Context: process
4516 *
4517 * Return nothing.
4518 */
4519static void
4520_scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4521 struct _sas_device *sas_device)
4522{
4523 Mpi2SepReply_t mpi_reply;
4524 Mpi2SepRequest_t mpi_request;
f92363d1 4525
0f624c39
SR
4526 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4527 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4528 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4529 mpi_request.SlotStatus = 0;
4530 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4531 mpi_request.DevHandle = 0;
4532 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4533 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4534 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4535 &mpi_request)) != 0) {
4536 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4537 __FILE__, __LINE__, __func__);
4538 return;
4539 }
4540
4541 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4542 dewtprintk(ioc, printk(MPT3SAS_FMT
4543 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4544 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4545 le32_to_cpu(mpi_reply.IOCLogInfo)));
4546 return;
4547 }
4548}
d1cb5e49 4549
f92363d1 4550/**
0f624c39 4551 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
f92363d1
SR
4552 * @ioc: per adapter object
4553 * @handle: device handle
4554 * Context: interrupt.
4555 *
4556 * Return nothing.
4557 */
4558static void
0f624c39 4559_scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
f92363d1
SR
4560{
4561 struct fw_event_work *fw_event;
4562
146b16c8 4563 fw_event = alloc_fw_event_work(0);
f92363d1
SR
4564 if (!fw_event)
4565 return;
0f624c39 4566 fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
f92363d1
SR
4567 fw_event->device_handle = handle;
4568 fw_event->ioc = ioc;
4569 _scsih_fw_event_add(ioc, fw_event);
146b16c8 4570 fw_event_work_put(fw_event);
f92363d1
SR
4571}
4572
4573/**
4574 * _scsih_smart_predicted_fault - process smart errors
4575 * @ioc: per adapter object
4576 * @handle: device handle
4577 * Context: interrupt.
4578 *
4579 * Return nothing.
4580 */
4581static void
4582_scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4583{
4584 struct scsi_target *starget;
4585 struct MPT3SAS_TARGET *sas_target_priv_data;
4586 Mpi2EventNotificationReply_t *event_reply;
4587 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4588 struct _sas_device *sas_device;
4589 ssize_t sz;
4590 unsigned long flags;
4591
4592 /* only handle non-raid devices */
4593 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
4594 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4595 if (!sas_device)
4596 goto out_unlock;
4597
f92363d1
SR
4598 starget = sas_device->starget;
4599 sas_target_priv_data = starget->hostdata;
4600
4601 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
d1cb5e49
SR
4602 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4603 goto out_unlock;
4604
e6d45e3e
SR
4605 if (sas_device->enclosure_handle != 0)
4606 starget_printk(KERN_INFO, starget, "predicted fault, "
4607 "enclosure logical id(0x%016llx), slot(%d)\n",
4608 (unsigned long long)sas_device->enclosure_logical_id,
4609 sas_device->slot);
4610 if (sas_device->connector_name[0] != '\0')
4611 starget_printk(KERN_WARNING, starget, "predicted fault, "
4612 "enclosure level(0x%04x), connector name( %s)\n",
4613 sas_device->enclosure_level,
4614 sas_device->connector_name);
f92363d1
SR
4615 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4616
4617 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
0f624c39 4618 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
f92363d1
SR
4619
4620 /* insert into event log */
4621 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4622 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4623 event_reply = kzalloc(sz, GFP_KERNEL);
4624 if (!event_reply) {
4625 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4626 ioc->name, __FILE__, __LINE__, __func__);
d1cb5e49 4627 goto out;
f92363d1
SR
4628 }
4629
4630 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4631 event_reply->Event =
4632 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4633 event_reply->MsgLength = sz/4;
4634 event_reply->EventDataLength =
4635 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4636 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4637 event_reply->EventData;
4638 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4639 event_data->ASC = 0x5D;
4640 event_data->DevHandle = cpu_to_le16(handle);
4641 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4642 mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4643 kfree(event_reply);
d1cb5e49
SR
4644out:
4645 if (sas_device)
4646 sas_device_put(sas_device);
4647 return;
4648
4649out_unlock:
4650 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4651 goto out;
f92363d1
SR
4652}
4653
4654/**
4655 * _scsih_io_done - scsi request callback
4656 * @ioc: per adapter object
4657 * @smid: system request message index
4658 * @msix_index: MSIX table index supplied by the OS
4659 * @reply: reply message frame(lower 32bit addr)
4660 *
4661 * Callback handler when using _scsih_qcmd.
4662 *
4663 * Return 1 meaning mf should be freed from _base_interrupt
4664 * 0 means the mf is freed from this function.
4665 */
4666static u8
4667_scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4668{
4669 Mpi2SCSIIORequest_t *mpi_request;
4670 Mpi2SCSIIOReply_t *mpi_reply;
4671 struct scsi_cmnd *scmd;
4672 u16 ioc_status;
4673 u32 xfer_cnt;
4674 u8 scsi_state;
4675 u8 scsi_status;
4676 u32 log_info;
4677 struct MPT3SAS_DEVICE *sas_device_priv_data;
4678 u32 response_code = 0;
7786ab6a 4679 unsigned long flags;
f92363d1
SR
4680
4681 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
459325c4
C
4682
4683 if (ioc->broadcast_aen_busy || ioc->pci_error_recovery ||
4684 ioc->got_task_abort_from_ioctl)
4685 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4686 else
4687 scmd = __scsih_scsi_lookup_get_clear(ioc, smid);
4688
f92363d1
SR
4689 if (scmd == NULL)
4690 return 1;
4691
ffb58456 4692 _scsih_set_satl_pending(scmd, false);
18f6084a 4693
f92363d1
SR
4694 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4695
4696 if (mpi_reply == NULL) {
4697 scmd->result = DID_OK << 16;
4698 goto out;
4699 }
4700
4701 sas_device_priv_data = scmd->device->hostdata;
4702 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4703 sas_device_priv_data->sas_target->deleted) {
4704 scmd->result = DID_NO_CONNECT << 16;
4705 goto out;
4706 }
4707 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4708
7786ab6a
SR
4709 /*
4710 * WARPDRIVE: If direct_io is set then it is directIO,
4711 * the failed direct I/O should be redirected to volume
4712 */
c84b06a4 4713 if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
7786ab6a
SR
4714 ((ioc_status & MPI2_IOCSTATUS_MASK)
4715 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4716 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4717 ioc->scsi_lookup[smid - 1].scmd = scmd;
4718 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
c84b06a4 4719 mpt3sas_scsi_direct_io_set(ioc, smid, 0);
7786ab6a
SR
4720 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4721 mpi_request->DevHandle =
4722 cpu_to_le16(sas_device_priv_data->sas_target->handle);
81c16f83 4723 ioc->put_smid_scsi_io(ioc, smid,
7786ab6a
SR
4724 sas_device_priv_data->sas_target->handle);
4725 return 0;
4726 }
f92363d1
SR
4727 /* turning off TLR */
4728 scsi_state = mpi_reply->SCSIState;
4729 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4730 response_code =
4731 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4732 if (!sas_device_priv_data->tlr_snoop_check) {
4733 sas_device_priv_data->tlr_snoop_check++;
7786ab6a
SR
4734 if (!ioc->is_warpdrive &&
4735 !scsih_is_raid(&scmd->device->sdev_gendev) &&
c84b06a4
SR
4736 sas_is_tlr_enabled(scmd->device) &&
4737 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4738 sas_disable_tlr(scmd->device);
4739 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4740 }
f92363d1
SR
4741 }
4742
4743 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4744 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4745 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4746 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4747 else
4748 log_info = 0;
4749 ioc_status &= MPI2_IOCSTATUS_MASK;
4750 scsi_status = mpi_reply->SCSIStatus;
4751
4752 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4753 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4754 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4755 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4756 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4757 }
4758
4759 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4760 struct sense_info data;
4761 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4762 smid);
4763 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4764 le32_to_cpu(mpi_reply->SenseCount));
4765 memcpy(scmd->sense_buffer, sense_data, sz);
4766 _scsih_normalize_sense(scmd->sense_buffer, &data);
4767 /* failure prediction threshold exceeded */
4768 if (data.asc == 0x5D)
4769 _scsih_smart_predicted_fault(ioc,
4770 le16_to_cpu(mpi_reply->DevHandle));
4771 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
f92363d1 4772
0d667f72 4773 if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
e6d45e3e
SR
4774 ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4775 (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4776 (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4777 _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
e6d45e3e 4778 }
f92363d1
SR
4779 switch (ioc_status) {
4780 case MPI2_IOCSTATUS_BUSY:
4781 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4782 scmd->result = SAM_STAT_BUSY;
4783 break;
4784
4785 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4786 scmd->result = DID_NO_CONNECT << 16;
4787 break;
4788
4789 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4790 if (sas_device_priv_data->block) {
4791 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4792 goto out;
4793 }
4794 if (log_info == 0x31110630) {
4795 if (scmd->retries > 2) {
4796 scmd->result = DID_NO_CONNECT << 16;
4797 scsi_device_set_state(scmd->device,
4798 SDEV_OFFLINE);
4799 } else {
4800 scmd->result = DID_SOFT_ERROR << 16;
4801 scmd->device->expecting_cc_ua = 1;
4802 }
4803 break;
3898f08e
SR
4804 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4805 scmd->result = DID_RESET << 16;
4806 break;
f92363d1
SR
4807 }
4808 scmd->result = DID_SOFT_ERROR << 16;
4809 break;
4810 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4811 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4812 scmd->result = DID_RESET << 16;
4813 break;
4814
4815 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4816 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4817 scmd->result = DID_SOFT_ERROR << 16;
4818 else
4819 scmd->result = (DID_OK << 16) | scsi_status;
4820 break;
4821
4822 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4823 scmd->result = (DID_OK << 16) | scsi_status;
4824
4825 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4826 break;
4827
4828 if (xfer_cnt < scmd->underflow) {
4829 if (scsi_status == SAM_STAT_BUSY)
4830 scmd->result = SAM_STAT_BUSY;
4831 else
4832 scmd->result = DID_SOFT_ERROR << 16;
4833 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4834 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4835 scmd->result = DID_SOFT_ERROR << 16;
4836 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4837 scmd->result = DID_RESET << 16;
4838 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4839 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4840 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4841 scmd->result = (DRIVER_SENSE << 24) |
4842 SAM_STAT_CHECK_CONDITION;
4843 scmd->sense_buffer[0] = 0x70;
4844 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4845 scmd->sense_buffer[12] = 0x20;
4846 scmd->sense_buffer[13] = 0;
4847 }
4848 break;
4849
4850 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4851 scsi_set_resid(scmd, 0);
4852 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4853 case MPI2_IOCSTATUS_SUCCESS:
4854 scmd->result = (DID_OK << 16) | scsi_status;
4855 if (response_code ==
4856 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4857 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4858 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4859 scmd->result = DID_SOFT_ERROR << 16;
4860 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4861 scmd->result = DID_RESET << 16;
4862 break;
4863
4864 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4865 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4866 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4867 _scsih_eedp_error_handling(scmd, ioc_status);
4868 break;
4869
4870 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4871 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4872 case MPI2_IOCSTATUS_INVALID_SGL:
4873 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4874 case MPI2_IOCSTATUS_INVALID_FIELD:
4875 case MPI2_IOCSTATUS_INVALID_STATE:
4876 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4877 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
b130b0d5 4878 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
f92363d1
SR
4879 default:
4880 scmd->result = DID_SOFT_ERROR << 16;
4881 break;
4882
4883 }
4884
f92363d1
SR
4885 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4886 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
f92363d1
SR
4887
4888 out:
4889
4890 scsi_dma_unmap(scmd);
4891
4892 scmd->scsi_done(scmd);
4893 return 1;
4894}
4895
4896/**
4897 * _scsih_sas_host_refresh - refreshing sas host object contents
4898 * @ioc: per adapter object
4899 * Context: user
4900 *
4901 * During port enable, fw will send topology events for every device. Its
4902 * possible that the handles may change from the previous setting, so this
4903 * code keeping handles updating if changed.
4904 *
4905 * Return nothing.
4906 */
4907static void
4908_scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4909{
4910 u16 sz;
4911 u16 ioc_status;
4912 int i;
4913 Mpi2ConfigReply_t mpi_reply;
4914 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4915 u16 attached_handle;
4916 u8 link_rate;
4917
4918 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4919 "updating handles for sas_host(0x%016llx)\n",
4920 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4921
4922 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4923 * sizeof(Mpi2SasIOUnit0PhyData_t));
4924 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4925 if (!sas_iounit_pg0) {
4926 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4927 ioc->name, __FILE__, __LINE__, __func__);
4928 return;
4929 }
4930
4931 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4932 sas_iounit_pg0, sz)) != 0)
4933 goto out;
4934 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4935 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4936 goto out;
4937 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4938 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4939 if (i == 0)
4940 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4941 PhyData[0].ControllerDevHandle);
4942 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4943 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4944 AttachedDevHandle);
4945 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4946 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4947 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4948 attached_handle, i, link_rate);
4949 }
4950 out:
4951 kfree(sas_iounit_pg0);
4952}
4953
4954/**
4955 * _scsih_sas_host_add - create sas host object
4956 * @ioc: per adapter object
4957 *
4958 * Creating host side data object, stored in ioc->sas_hba
4959 *
4960 * Return nothing.
4961 */
4962static void
4963_scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4964{
4965 int i;
4966 Mpi2ConfigReply_t mpi_reply;
4967 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4968 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4969 Mpi2SasPhyPage0_t phy_pg0;
4970 Mpi2SasDevicePage0_t sas_device_pg0;
4971 Mpi2SasEnclosurePage0_t enclosure_pg0;
4972 u16 ioc_status;
4973 u16 sz;
4974 u8 device_missing_delay;
87aa95d4 4975 u8 num_phys;
f92363d1 4976
87aa95d4
JL
4977 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
4978 if (!num_phys) {
f92363d1
SR
4979 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4980 ioc->name, __FILE__, __LINE__, __func__);
4981 return;
4982 }
87aa95d4
JL
4983 ioc->sas_hba.phy = kcalloc(num_phys,
4984 sizeof(struct _sas_phy), GFP_KERNEL);
4985 if (!ioc->sas_hba.phy) {
4986 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4987 ioc->name, __FILE__, __LINE__, __func__);
4988 goto out;
4989 }
4990 ioc->sas_hba.num_phys = num_phys;
f92363d1
SR
4991
4992 /* sas_iounit page 0 */
4993 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4994 sizeof(Mpi2SasIOUnit0PhyData_t));
4995 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4996 if (!sas_iounit_pg0) {
4997 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4998 ioc->name, __FILE__, __LINE__, __func__);
4999 return;
5000 }
5001 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
5002 sas_iounit_pg0, sz))) {
5003 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5004 ioc->name, __FILE__, __LINE__, __func__);
5005 goto out;
5006 }
5007 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5008 MPI2_IOCSTATUS_MASK;
5009 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5010 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5011 ioc->name, __FILE__, __LINE__, __func__);
5012 goto out;
5013 }
5014
5015 /* sas_iounit page 1 */
5016 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
5017 sizeof(Mpi2SasIOUnit1PhyData_t));
5018 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
5019 if (!sas_iounit_pg1) {
5020 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5021 ioc->name, __FILE__, __LINE__, __func__);
5022 goto out;
5023 }
5024 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
5025 sas_iounit_pg1, sz))) {
5026 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5027 ioc->name, __FILE__, __LINE__, __func__);
5028 goto out;
5029 }
5030 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5031 MPI2_IOCSTATUS_MASK;
5032 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5033 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5034 ioc->name, __FILE__, __LINE__, __func__);
5035 goto out;
5036 }
5037
5038 ioc->io_missing_delay =
5039 sas_iounit_pg1->IODeviceMissingDelay;
5040 device_missing_delay =
5041 sas_iounit_pg1->ReportDeviceMissingDelay;
5042 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
5043 ioc->device_missing_delay = (device_missing_delay &
5044 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
5045 else
5046 ioc->device_missing_delay = device_missing_delay &
5047 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
5048
5049 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
f92363d1
SR
5050 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
5051 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
5052 i))) {
5053 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5054 ioc->name, __FILE__, __LINE__, __func__);
5055 goto out;
5056 }
5057 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5058 MPI2_IOCSTATUS_MASK;
5059 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5060 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5061 ioc->name, __FILE__, __LINE__, __func__);
5062 goto out;
5063 }
5064
5065 if (i == 0)
5066 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
5067 PhyData[0].ControllerDevHandle);
5068 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
5069 ioc->sas_hba.phy[i].phy_id = i;
5070 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
5071 phy_pg0, ioc->sas_hba.parent_dev);
5072 }
5073 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5074 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
5075 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5076 ioc->name, __FILE__, __LINE__, __func__);
5077 goto out;
5078 }
5079 ioc->sas_hba.enclosure_handle =
5080 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5081 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5082 pr_info(MPT3SAS_FMT
5083 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
5084 ioc->name, ioc->sas_hba.handle,
5085 (unsigned long long) ioc->sas_hba.sas_address,
5086 ioc->sas_hba.num_phys) ;
5087
5088 if (ioc->sas_hba.enclosure_handle) {
5089 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5090 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5091 ioc->sas_hba.enclosure_handle)))
5092 ioc->sas_hba.enclosure_logical_id =
5093 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5094 }
5095
5096 out:
5097 kfree(sas_iounit_pg1);
5098 kfree(sas_iounit_pg0);
5099}
5100
5101/**
5102 * _scsih_expander_add - creating expander object
5103 * @ioc: per adapter object
5104 * @handle: expander handle
5105 *
5106 * Creating expander object, stored in ioc->sas_expander_list.
5107 *
5108 * Return 0 for success, else error.
5109 */
5110static int
5111_scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5112{
5113 struct _sas_node *sas_expander;
5114 Mpi2ConfigReply_t mpi_reply;
5115 Mpi2ExpanderPage0_t expander_pg0;
5116 Mpi2ExpanderPage1_t expander_pg1;
5117 Mpi2SasEnclosurePage0_t enclosure_pg0;
5118 u32 ioc_status;
5119 u16 parent_handle;
5120 u64 sas_address, sas_address_parent = 0;
5121 int i;
5122 unsigned long flags;
5123 struct _sas_port *mpt3sas_port = NULL;
5124
5125 int rc = 0;
5126
5127 if (!handle)
5128 return -1;
5129
5130 if (ioc->shost_recovery || ioc->pci_error_recovery)
5131 return -1;
5132
5133 if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5134 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
5135 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5136 ioc->name, __FILE__, __LINE__, __func__);
5137 return -1;
5138 }
5139
5140 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5141 MPI2_IOCSTATUS_MASK;
5142 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5143 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5144 ioc->name, __FILE__, __LINE__, __func__);
5145 return -1;
5146 }
5147
5148 /* handle out of order topology events */
5149 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
5150 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
5151 != 0) {
5152 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5153 ioc->name, __FILE__, __LINE__, __func__);
5154 return -1;
5155 }
5156 if (sas_address_parent != ioc->sas_hba.sas_address) {
5157 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5158 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5159 sas_address_parent);
5160 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5161 if (!sas_expander) {
5162 rc = _scsih_expander_add(ioc, parent_handle);
5163 if (rc != 0)
5164 return rc;
5165 }
5166 }
5167
5168 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5169 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5170 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5171 sas_address);
5172 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5173
5174 if (sas_expander)
5175 return 0;
5176
5177 sas_expander = kzalloc(sizeof(struct _sas_node),
5178 GFP_KERNEL);
5179 if (!sas_expander) {
5180 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5181 ioc->name, __FILE__, __LINE__, __func__);
5182 return -1;
5183 }
5184
5185 sas_expander->handle = handle;
5186 sas_expander->num_phys = expander_pg0.NumPhys;
5187 sas_expander->sas_address_parent = sas_address_parent;
5188 sas_expander->sas_address = sas_address;
5189
5190 pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
5191 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
5192 handle, parent_handle, (unsigned long long)
5193 sas_expander->sas_address, sas_expander->num_phys);
5194
5195 if (!sas_expander->num_phys)
5196 goto out_fail;
5197 sas_expander->phy = kcalloc(sas_expander->num_phys,
5198 sizeof(struct _sas_phy), GFP_KERNEL);
5199 if (!sas_expander->phy) {
5200 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5201 ioc->name, __FILE__, __LINE__, __func__);
5202 rc = -1;
5203 goto out_fail;
5204 }
5205
5206 INIT_LIST_HEAD(&sas_expander->sas_port_list);
5207 mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
5208 sas_address_parent);
5209 if (!mpt3sas_port) {
5210 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5211 ioc->name, __FILE__, __LINE__, __func__);
5212 rc = -1;
5213 goto out_fail;
5214 }
5215 sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
5216
5217 for (i = 0 ; i < sas_expander->num_phys ; i++) {
5218 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
5219 &expander_pg1, i, handle))) {
5220 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5221 ioc->name, __FILE__, __LINE__, __func__);
5222 rc = -1;
5223 goto out_fail;
5224 }
5225 sas_expander->phy[i].handle = handle;
5226 sas_expander->phy[i].phy_id = i;
5227
5228 if ((mpt3sas_transport_add_expander_phy(ioc,
5229 &sas_expander->phy[i], expander_pg1,
5230 sas_expander->parent_dev))) {
5231 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5232 ioc->name, __FILE__, __LINE__, __func__);
5233 rc = -1;
5234 goto out_fail;
5235 }
5236 }
5237
5238 if (sas_expander->enclosure_handle) {
5239 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5240 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5241 sas_expander->enclosure_handle)))
5242 sas_expander->enclosure_logical_id =
5243 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5244 }
5245
5246 _scsih_expander_node_add(ioc, sas_expander);
5247 return 0;
5248
5249 out_fail:
5250
5251 if (mpt3sas_port)
5252 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
5253 sas_address_parent);
5254 kfree(sas_expander);
5255 return rc;
5256}
5257
5258/**
5259 * mpt3sas_expander_remove - removing expander object
5260 * @ioc: per adapter object
5261 * @sas_address: expander sas_address
5262 *
5263 * Return nothing.
5264 */
5265void
5266mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5267{
5268 struct _sas_node *sas_expander;
5269 unsigned long flags;
5270
5271 if (ioc->shost_recovery)
5272 return;
5273
5274 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5275 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5276 sas_address);
f92363d1
SR
5277 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5278 if (sas_expander)
5279 _scsih_expander_node_remove(ioc, sas_expander);
5280}
5281
5282/**
5283 * _scsih_done - internal SCSI_IO callback handler.
5284 * @ioc: per adapter object
5285 * @smid: system request message index
5286 * @msix_index: MSIX table index supplied by the OS
5287 * @reply: reply message frame(lower 32bit addr)
5288 *
5289 * Callback handler when sending internal generated SCSI_IO.
5290 * The callback index passed is `ioc->scsih_cb_idx`
5291 *
5292 * Return 1 meaning mf should be freed from _base_interrupt
5293 * 0 means the mf is freed from this function.
5294 */
5295static u8
5296_scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5297{
5298 MPI2DefaultReply_t *mpi_reply;
5299
5300 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5301 if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5302 return 1;
5303 if (ioc->scsih_cmds.smid != smid)
5304 return 1;
5305 ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5306 if (mpi_reply) {
5307 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5308 mpi_reply->MsgLength*4);
5309 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5310 }
5311 ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5312 complete(&ioc->scsih_cmds.done);
5313 return 1;
5314}
5315
5316
5317
5318
5319#define MPT3_MAX_LUNS (255)
5320
5321
5322/**
5323 * _scsih_check_access_status - check access flags
5324 * @ioc: per adapter object
5325 * @sas_address: sas address
5326 * @handle: sas device handle
5327 * @access_flags: errors returned during discovery of the device
5328 *
5329 * Return 0 for success, else failure
5330 */
5331static u8
5332_scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5333 u16 handle, u8 access_status)
5334{
5335 u8 rc = 1;
5336 char *desc = NULL;
5337
5338 switch (access_status) {
5339 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5340 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5341 rc = 0;
5342 break;
5343 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5344 desc = "sata capability failed";
5345 break;
5346 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5347 desc = "sata affiliation conflict";
5348 break;
5349 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5350 desc = "route not addressable";
5351 break;
5352 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5353 desc = "smp error not addressable";
5354 break;
5355 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5356 desc = "device blocked";
5357 break;
5358 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5359 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5360 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5361 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5362 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5363 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5364 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5365 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5366 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5367 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5368 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5369 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5370 desc = "sata initialization failed";
5371 break;
5372 default:
5373 desc = "unknown";
5374 break;
5375 }
5376
5377 if (!rc)
5378 return 0;
5379
5380 pr_err(MPT3SAS_FMT
5381 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5382 ioc->name, desc, (unsigned long long)sas_address, handle);
5383 return rc;
5384}
5385
5386/**
5387 * _scsih_check_device - checking device responsiveness
5388 * @ioc: per adapter object
5389 * @parent_sas_address: sas address of parent expander or sas host
5390 * @handle: attached device handle
5391 * @phy_numberv: phy number
5392 * @link_rate: new link rate
5393 *
5394 * Returns nothing.
5395 */
5396static void
5397_scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5398 u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5399{
5400 Mpi2ConfigReply_t mpi_reply;
5401 Mpi2SasDevicePage0_t sas_device_pg0;
5402 struct _sas_device *sas_device;
5403 u32 ioc_status;
5404 unsigned long flags;
5405 u64 sas_address;
5406 struct scsi_target *starget;
5407 struct MPT3SAS_TARGET *sas_target_priv_data;
5408 u32 device_info;
5409
5410
5411 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5412 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5413 return;
5414
5415 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5416 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5417 return;
5418
5419 /* wide port handling ~ we need only handle device once for the phy that
5420 * is matched in sas device page zero
5421 */
5422 if (phy_number != sas_device_pg0.PhyNum)
5423 return;
5424
5425 /* check if this is end device */
5426 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5427 if (!(_scsih_is_end_device(device_info)))
5428 return;
5429
5430 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5431 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
d1cb5e49 5432 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
f92363d1
SR
5433 sas_address);
5434
d1cb5e49
SR
5435 if (!sas_device)
5436 goto out_unlock;
f92363d1
SR
5437
5438 if (unlikely(sas_device->handle != handle)) {
5439 starget = sas_device->starget;
5440 sas_target_priv_data = starget->hostdata;
5441 starget_printk(KERN_INFO, starget,
5442 "handle changed from(0x%04x) to (0x%04x)!!!\n",
5443 sas_device->handle, handle);
5444 sas_target_priv_data->handle = handle;
5445 sas_device->handle = handle;
aa53bb89 5446 if (le16_to_cpu(sas_device_pg0.Flags) &
e6d45e3e
SR
5447 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5448 sas_device->enclosure_level =
aa53bb89 5449 sas_device_pg0.EnclosureLevel;
310c8e40
CO
5450 memcpy(sas_device->connector_name,
5451 sas_device_pg0.ConnectorName, 4);
5452 sas_device->connector_name[4] = '\0';
e6d45e3e
SR
5453 } else {
5454 sas_device->enclosure_level = 0;
5455 sas_device->connector_name[0] = '\0';
5456 }
f92363d1
SR
5457 }
5458
5459 /* check if device is present */
5460 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5461 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5462 pr_err(MPT3SAS_FMT
5463 "device is not present handle(0x%04x), flags!!!\n",
5464 ioc->name, handle);
d1cb5e49 5465 goto out_unlock;
f92363d1
SR
5466 }
5467
5468 /* check if there were any issues with discovery */
5469 if (_scsih_check_access_status(ioc, sas_address, handle,
d1cb5e49
SR
5470 sas_device_pg0.AccessStatus))
5471 goto out_unlock;
f92363d1
SR
5472
5473 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5474 _scsih_ublock_io_device(ioc, sas_address);
5475
d1cb5e49
SR
5476 if (sas_device)
5477 sas_device_put(sas_device);
5478 return;
5479
5480out_unlock:
5481 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5482 if (sas_device)
5483 sas_device_put(sas_device);
f92363d1
SR
5484}
5485
5486/**
5487 * _scsih_add_device - creating sas device object
5488 * @ioc: per adapter object
5489 * @handle: sas device handle
5490 * @phy_num: phy number end device attached to
5491 * @is_pd: is this hidden raid component
5492 *
5493 * Creating end device object, stored in ioc->sas_device_list.
5494 *
5495 * Returns 0 for success, non-zero for failure.
5496 */
5497static int
5498_scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5499 u8 is_pd)
5500{
5501 Mpi2ConfigReply_t mpi_reply;
5502 Mpi2SasDevicePage0_t sas_device_pg0;
5503 Mpi2SasEnclosurePage0_t enclosure_pg0;
5504 struct _sas_device *sas_device;
5505 u32 ioc_status;
5506 u64 sas_address;
5507 u32 device_info;
f92363d1
SR
5508
5509 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5510 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5511 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5512 ioc->name, __FILE__, __LINE__, __func__);
5513 return -1;
5514 }
5515
5516 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5517 MPI2_IOCSTATUS_MASK;
5518 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5519 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5520 ioc->name, __FILE__, __LINE__, __func__);
5521 return -1;
5522 }
5523
5524 /* check if this is end device */
5525 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5526 if (!(_scsih_is_end_device(device_info)))
5527 return -1;
c696f7b8 5528 set_bit(handle, ioc->pend_os_device_add);
f92363d1
SR
5529 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5530
5531 /* check if device is present */
5532 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5533 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5534 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5535 ioc->name, handle);
5536 return -1;
5537 }
5538
5539 /* check if there were any issues with discovery */
5540 if (_scsih_check_access_status(ioc, sas_address, handle,
5541 sas_device_pg0.AccessStatus))
5542 return -1;
5543
d1cb5e49
SR
5544 sas_device = mpt3sas_get_sdev_by_addr(ioc,
5545 sas_address);
5546 if (sas_device) {
c696f7b8 5547 clear_bit(handle, ioc->pend_os_device_add);
d1cb5e49 5548 sas_device_put(sas_device);
f92363d1 5549 return -1;
d1cb5e49 5550 }
f92363d1
SR
5551
5552 sas_device = kzalloc(sizeof(struct _sas_device),
5553 GFP_KERNEL);
5554 if (!sas_device) {
5555 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5556 ioc->name, __FILE__, __LINE__, __func__);
5557 return 0;
5558 }
5559
d1cb5e49 5560 kref_init(&sas_device->refcount);
f92363d1
SR
5561 sas_device->handle = handle;
5562 if (_scsih_get_sas_address(ioc,
5563 le16_to_cpu(sas_device_pg0.ParentDevHandle),
5564 &sas_device->sas_address_parent) != 0)
5565 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5566 ioc->name, __FILE__, __LINE__, __func__);
5567 sas_device->enclosure_handle =
5568 le16_to_cpu(sas_device_pg0.EnclosureHandle);
e6d45e3e
SR
5569 if (sas_device->enclosure_handle != 0)
5570 sas_device->slot =
5571 le16_to_cpu(sas_device_pg0.Slot);
f92363d1
SR
5572 sas_device->device_info = device_info;
5573 sas_device->sas_address = sas_address;
5574 sas_device->phy = sas_device_pg0.PhyNum;
5575 sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5576 MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5577
aa53bb89
SPS
5578 if (le16_to_cpu(sas_device_pg0.Flags)
5579 & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
e6d45e3e 5580 sas_device->enclosure_level =
aa53bb89 5581 sas_device_pg0.EnclosureLevel;
310c8e40
CO
5582 memcpy(sas_device->connector_name,
5583 sas_device_pg0.ConnectorName, 4);
5584 sas_device->connector_name[4] = '\0';
e6d45e3e
SR
5585 } else {
5586 sas_device->enclosure_level = 0;
5587 sas_device->connector_name[0] = '\0';
5588 }
f92363d1
SR
5589 /* get enclosure_logical_id */
5590 if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5591 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5592 sas_device->enclosure_handle)))
5593 sas_device->enclosure_logical_id =
5594 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5595
5596 /* get device name */
5597 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5598
5599 if (ioc->wait_for_discovery_to_complete)
5600 _scsih_sas_device_init_add(ioc, sas_device);
5601 else
5602 _scsih_sas_device_add(ioc, sas_device);
5603
d1cb5e49 5604 sas_device_put(sas_device);
f92363d1
SR
5605 return 0;
5606}
5607
5608/**
5609 * _scsih_remove_device - removing sas device object
5610 * @ioc: per adapter object
5611 * @sas_device_delete: the sas_device object
5612 *
5613 * Return nothing.
5614 */
5615static void
5616_scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5617 struct _sas_device *sas_device)
5618{
5619 struct MPT3SAS_TARGET *sas_target_priv_data;
5620
0f624c39
SR
5621 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5622 (sas_device->pfa_led_on)) {
5623 _scsih_turn_off_pfa_led(ioc, sas_device);
5624 sas_device->pfa_led_on = 0;
5625 }
f92363d1
SR
5626 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5627 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5628 ioc->name, __func__,
5629 sas_device->handle, (unsigned long long)
5630 sas_device->sas_address));
e6d45e3e
SR
5631 if (sas_device->enclosure_handle != 0)
5632 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5633 "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5634 ioc->name, __func__,
5635 (unsigned long long)sas_device->enclosure_logical_id,
5636 sas_device->slot));
5637 if (sas_device->connector_name[0] != '\0')
5638 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5639 "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5640 ioc->name, __func__,
5641 sas_device->enclosure_level,
5642 sas_device->connector_name));
f92363d1
SR
5643
5644 if (sas_device->starget && sas_device->starget->hostdata) {
5645 sas_target_priv_data = sas_device->starget->hostdata;
5646 sas_target_priv_data->deleted = 1;
5647 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5648 sas_target_priv_data->handle =
5649 MPT3SAS_INVALID_DEVICE_HANDLE;
5650 }
7786ab6a
SR
5651
5652 if (!ioc->hide_drives)
5653 mpt3sas_transport_port_remove(ioc,
f92363d1
SR
5654 sas_device->sas_address,
5655 sas_device->sas_address_parent);
5656
5657 pr_info(MPT3SAS_FMT
5658 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5659 ioc->name, sas_device->handle,
5660 (unsigned long long) sas_device->sas_address);
e6d45e3e
SR
5661 if (sas_device->enclosure_handle != 0)
5662 pr_info(MPT3SAS_FMT
5663 "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5664 ioc->name,
5665 (unsigned long long)sas_device->enclosure_logical_id,
5666 sas_device->slot);
5667 if (sas_device->connector_name[0] != '\0')
5668 pr_info(MPT3SAS_FMT
5669 "removing enclosure level(0x%04x), connector name( %s)\n",
5670 ioc->name, sas_device->enclosure_level,
5671 sas_device->connector_name);
f92363d1
SR
5672
5673 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5674 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5675 ioc->name, __func__,
e6d45e3e
SR
5676 sas_device->handle, (unsigned long long)
5677 sas_device->sas_address));
5678 if (sas_device->enclosure_handle != 0)
5679 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5680 "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5681 ioc->name, __func__,
5682 (unsigned long long)sas_device->enclosure_logical_id,
5683 sas_device->slot));
5684 if (sas_device->connector_name[0] != '\0')
5685 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5686 "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5687 ioc->name, __func__, sas_device->enclosure_level,
5688 sas_device->connector_name));
f92363d1
SR
5689}
5690
f92363d1
SR
5691/**
5692 * _scsih_sas_topology_change_event_debug - debug for topology event
5693 * @ioc: per adapter object
5694 * @event_data: event data payload
5695 * Context: user.
5696 */
5697static void
5698_scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5699 Mpi2EventDataSasTopologyChangeList_t *event_data)
5700{
5701 int i;
5702 u16 handle;
5703 u16 reason_code;
5704 u8 phy_number;
5705 char *status_str = NULL;
5706 u8 link_rate, prev_link_rate;
5707
5708 switch (event_data->ExpStatus) {
5709 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5710 status_str = "add";
5711 break;
5712 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5713 status_str = "remove";
5714 break;
5715 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5716 case 0:
5717 status_str = "responding";
5718 break;
5719 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5720 status_str = "remove delay";
5721 break;
5722 default:
5723 status_str = "unknown status";
5724 break;
5725 }
5726 pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5727 ioc->name, status_str);
5728 pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5729 "start_phy(%02d), count(%d)\n",
5730 le16_to_cpu(event_data->ExpanderDevHandle),
5731 le16_to_cpu(event_data->EnclosureHandle),
5732 event_data->StartPhyNum, event_data->NumEntries);
5733 for (i = 0; i < event_data->NumEntries; i++) {
5734 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5735 if (!handle)
5736 continue;
5737 phy_number = event_data->StartPhyNum + i;
5738 reason_code = event_data->PHY[i].PhyStatus &
5739 MPI2_EVENT_SAS_TOPO_RC_MASK;
5740 switch (reason_code) {
5741 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5742 status_str = "target add";
5743 break;
5744 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5745 status_str = "target remove";
5746 break;
5747 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5748 status_str = "delay target remove";
5749 break;
5750 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5751 status_str = "link rate change";
5752 break;
5753 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5754 status_str = "target responding";
5755 break;
5756 default:
5757 status_str = "unknown";
5758 break;
5759 }
5760 link_rate = event_data->PHY[i].LinkRate >> 4;
5761 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5762 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5763 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5764 handle, status_str, link_rate, prev_link_rate);
5765
5766 }
5767}
f92363d1
SR
5768
5769/**
5770 * _scsih_sas_topology_change_event - handle topology changes
5771 * @ioc: per adapter object
5772 * @fw_event: The fw_event_work object
5773 * Context: user.
5774 *
5775 */
5776static int
5777_scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5778 struct fw_event_work *fw_event)
5779{
5780 int i;
5781 u16 parent_handle, handle;
5782 u16 reason_code;
5783 u8 phy_number, max_phys;
5784 struct _sas_node *sas_expander;
5785 u64 sas_address;
5786 unsigned long flags;
5787 u8 link_rate, prev_link_rate;
35b62362
JL
5788 Mpi2EventDataSasTopologyChangeList_t *event_data =
5789 (Mpi2EventDataSasTopologyChangeList_t *)
5790 fw_event->event_data;
f92363d1 5791
f92363d1
SR
5792 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5793 _scsih_sas_topology_change_event_debug(ioc, event_data);
f92363d1
SR
5794
5795 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5796 return 0;
5797
5798 if (!ioc->sas_hba.num_phys)
5799 _scsih_sas_host_add(ioc);
5800 else
5801 _scsih_sas_host_refresh(ioc);
5802
5803 if (fw_event->ignore) {
5804 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5805 "ignoring expander event\n", ioc->name));
5806 return 0;
5807 }
5808
5809 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5810
5811 /* handle expander add */
5812 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5813 if (_scsih_expander_add(ioc, parent_handle) != 0)
5814 return 0;
5815
5816 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5817 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5818 parent_handle);
5819 if (sas_expander) {
5820 sas_address = sas_expander->sas_address;
5821 max_phys = sas_expander->num_phys;
5822 } else if (parent_handle < ioc->sas_hba.num_phys) {
5823 sas_address = ioc->sas_hba.sas_address;
5824 max_phys = ioc->sas_hba.num_phys;
5825 } else {
5826 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5827 return 0;
5828 }
5829 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5830
5831 /* handle siblings events */
5832 for (i = 0; i < event_data->NumEntries; i++) {
5833 if (fw_event->ignore) {
5834 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5835 "ignoring expander event\n", ioc->name));
5836 return 0;
5837 }
5838 if (ioc->remove_host || ioc->pci_error_recovery)
5839 return 0;
5840 phy_number = event_data->StartPhyNum + i;
5841 if (phy_number >= max_phys)
5842 continue;
5843 reason_code = event_data->PHY[i].PhyStatus &
5844 MPI2_EVENT_SAS_TOPO_RC_MASK;
5845 if ((event_data->PHY[i].PhyStatus &
5846 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5847 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5848 continue;
5849 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5850 if (!handle)
5851 continue;
5852 link_rate = event_data->PHY[i].LinkRate >> 4;
5853 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5854 switch (reason_code) {
5855 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5856
5857 if (ioc->shost_recovery)
5858 break;
5859
5860 if (link_rate == prev_link_rate)
5861 break;
5862
5863 mpt3sas_transport_update_links(ioc, sas_address,
5864 handle, phy_number, link_rate);
5865
5866 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5867 break;
5868
5869 _scsih_check_device(ioc, sas_address, handle,
5870 phy_number, link_rate);
5871
c696f7b8
SPS
5872 if (!test_bit(handle, ioc->pend_os_device_add))
5873 break;
5874
f92363d1
SR
5875
5876 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5877
5878 if (ioc->shost_recovery)
5879 break;
5880
5881 mpt3sas_transport_update_links(ioc, sas_address,
5882 handle, phy_number, link_rate);
5883
5884 _scsih_add_device(ioc, handle, phy_number, 0);
5885
5886 break;
5887 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5888
5889 _scsih_device_remove_by_handle(ioc, handle);
5890 break;
5891 }
5892 }
5893
5894 /* handle expander removal */
5895 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5896 sas_expander)
5897 mpt3sas_expander_remove(ioc, sas_address);
5898
5899 return 0;
5900}
5901
f92363d1
SR
5902/**
5903 * _scsih_sas_device_status_change_event_debug - debug for device event
5904 * @event_data: event data payload
5905 * Context: user.
5906 *
5907 * Return nothing.
5908 */
5909static void
5910_scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5911 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5912{
5913 char *reason_str = NULL;
5914
5915 switch (event_data->ReasonCode) {
5916 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5917 reason_str = "smart data";
5918 break;
5919 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5920 reason_str = "unsupported device discovered";
5921 break;
5922 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5923 reason_str = "internal device reset";
5924 break;
5925 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5926 reason_str = "internal task abort";
5927 break;
5928 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5929 reason_str = "internal task abort set";
5930 break;
5931 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5932 reason_str = "internal clear task set";
5933 break;
5934 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5935 reason_str = "internal query task";
5936 break;
5937 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5938 reason_str = "sata init failure";
5939 break;
5940 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5941 reason_str = "internal device reset complete";
5942 break;
5943 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5944 reason_str = "internal task abort complete";
5945 break;
5946 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5947 reason_str = "internal async notification";
5948 break;
5949 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5950 reason_str = "expander reduced functionality";
5951 break;
5952 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5953 reason_str = "expander reduced functionality complete";
5954 break;
5955 default:
5956 reason_str = "unknown reason";
5957 break;
5958 }
5959 pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5960 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5961 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5962 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5963 le16_to_cpu(event_data->TaskTag));
5964 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5965 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5966 event_data->ASC, event_data->ASCQ);
5967 pr_info("\n");
5968}
f92363d1
SR
5969
5970/**
5971 * _scsih_sas_device_status_change_event - handle device status change
5972 * @ioc: per adapter object
5973 * @fw_event: The fw_event_work object
5974 * Context: user.
5975 *
5976 * Return nothing.
5977 */
5978static void
5979_scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5980 struct fw_event_work *fw_event)
5981{
5982 struct MPT3SAS_TARGET *target_priv_data;
5983 struct _sas_device *sas_device;
5984 u64 sas_address;
5985 unsigned long flags;
5986 Mpi2EventDataSasDeviceStatusChange_t *event_data =
35b62362
JL
5987 (Mpi2EventDataSasDeviceStatusChange_t *)
5988 fw_event->event_data;
f92363d1 5989
f92363d1
SR
5990 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5991 _scsih_sas_device_status_change_event_debug(ioc,
5992 event_data);
f92363d1
SR
5993
5994 /* In MPI Revision K (0xC), the internal device reset complete was
5995 * implemented, so avoid setting tm_busy flag for older firmware.
5996 */
5997 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5998 return;
5999
6000 if (event_data->ReasonCode !=
6001 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
6002 event_data->ReasonCode !=
6003 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
6004 return;
6005
6006 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6007 sas_address = le64_to_cpu(event_data->SASAddress);
d1cb5e49 6008 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
f92363d1
SR
6009 sas_address);
6010
d1cb5e49
SR
6011 if (!sas_device || !sas_device->starget)
6012 goto out;
f92363d1
SR
6013
6014 target_priv_data = sas_device->starget->hostdata;
d1cb5e49
SR
6015 if (!target_priv_data)
6016 goto out;
f92363d1
SR
6017
6018 if (event_data->ReasonCode ==
6019 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
6020 target_priv_data->tm_busy = 1;
6021 else
6022 target_priv_data->tm_busy = 0;
d1cb5e49
SR
6023
6024out:
6025 if (sas_device)
6026 sas_device_put(sas_device);
6027
f92363d1 6028 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
d1cb5e49 6029
f92363d1
SR
6030}
6031
f92363d1
SR
6032/**
6033 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
6034 * event
6035 * @ioc: per adapter object
6036 * @event_data: event data payload
6037 * Context: user.
6038 *
6039 * Return nothing.
6040 */
6041static void
6042_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6043 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
6044{
6045 char *reason_str = NULL;
6046
6047 switch (event_data->ReasonCode) {
6048 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
6049 reason_str = "enclosure add";
6050 break;
6051 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
6052 reason_str = "enclosure remove";
6053 break;
6054 default:
6055 reason_str = "unknown reason";
6056 break;
6057 }
6058
6059 pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
6060 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
6061 " number slots(%d)\n", ioc->name, reason_str,
6062 le16_to_cpu(event_data->EnclosureHandle),
6063 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
6064 le16_to_cpu(event_data->StartSlot));
6065}
f92363d1
SR
6066
6067/**
6068 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
6069 * @ioc: per adapter object
6070 * @fw_event: The fw_event_work object
6071 * Context: user.
6072 *
6073 * Return nothing.
6074 */
6075static void
6076_scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
6077 struct fw_event_work *fw_event)
6078{
f92363d1
SR
6079 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6080 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
35b62362 6081 (Mpi2EventDataSasEnclDevStatusChange_t *)
f92363d1 6082 fw_event->event_data);
f92363d1
SR
6083}
6084
6085/**
6086 * _scsih_sas_broadcast_primitive_event - handle broadcast events
6087 * @ioc: per adapter object
6088 * @fw_event: The fw_event_work object
6089 * Context: user.
6090 *
6091 * Return nothing.
6092 */
6093static void
6094_scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
6095 struct fw_event_work *fw_event)
6096{
6097 struct scsi_cmnd *scmd;
6098 struct scsi_device *sdev;
6099 u16 smid, handle;
6100 u32 lun;
6101 struct MPT3SAS_DEVICE *sas_device_priv_data;
6102 u32 termination_count;
6103 u32 query_count;
6104 Mpi2SCSITaskManagementReply_t *mpi_reply;
35b62362
JL
6105 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
6106 (Mpi2EventDataSasBroadcastPrimitive_t *)
6107 fw_event->event_data;
f92363d1
SR
6108 u16 ioc_status;
6109 unsigned long flags;
6110 int r;
6111 u8 max_retries = 0;
6112 u8 task_abort_retries;
6113
6114 mutex_lock(&ioc->tm_cmds.mutex);
6115 pr_info(MPT3SAS_FMT
6116 "%s: enter: phy number(%d), width(%d)\n",
6117 ioc->name, __func__, event_data->PhyNum,
6118 event_data->PortWidth);
6119
6120 _scsih_block_io_all_device(ioc);
6121
6122 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6123 mpi_reply = ioc->tm_cmds.reply;
6124 broadcast_aen_retry:
6125
6126 /* sanity checks for retrying this loop */
6127 if (max_retries++ == 5) {
6128 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
6129 ioc->name, __func__));
6130 goto out;
6131 } else if (max_retries > 1)
6132 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
6133 ioc->name, __func__, max_retries - 1));
6134
6135 termination_count = 0;
6136 query_count = 0;
6137 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
6138 if (ioc->shost_recovery)
6139 goto out;
6140 scmd = _scsih_scsi_lookup_get(ioc, smid);
6141 if (!scmd)
6142 continue;
6143 sdev = scmd->device;
6144 sas_device_priv_data = sdev->hostdata;
6145 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
6146 continue;
6147 /* skip hidden raid components */
6148 if (sas_device_priv_data->sas_target->flags &
6149 MPT_TARGET_FLAGS_RAID_COMPONENT)
6150 continue;
6151 /* skip volumes */
6152 if (sas_device_priv_data->sas_target->flags &
6153 MPT_TARGET_FLAGS_VOLUME)
6154 continue;
6155
6156 handle = sas_device_priv_data->sas_target->handle;
6157 lun = sas_device_priv_data->lun;
6158 query_count++;
6159
6160 if (ioc->shost_recovery)
6161 goto out;
6162
6163 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6164 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
96902835 6165 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
f92363d1
SR
6166 if (r == FAILED) {
6167 sdev_printk(KERN_WARNING, sdev,
6168 "mpt3sas_scsih_issue_tm: FAILED when sending "
6169 "QUERY_TASK: scmd(%p)\n", scmd);
6170 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6171 goto broadcast_aen_retry;
6172 }
6173 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
6174 & MPI2_IOCSTATUS_MASK;
6175 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6176 sdev_printk(KERN_WARNING, sdev,
6177 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
6178 ioc_status, scmd);
6179 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6180 goto broadcast_aen_retry;
6181 }
6182
6183 /* see if IO is still owned by IOC and target */
6184 if (mpi_reply->ResponseCode ==
6185 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
6186 mpi_reply->ResponseCode ==
6187 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
6188 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6189 continue;
6190 }
6191 task_abort_retries = 0;
6192 tm_retry:
6193 if (task_abort_retries++ == 60) {
6194 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6195 "%s: ABORT_TASK: giving up\n", ioc->name,
6196 __func__));
6197 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6198 goto broadcast_aen_retry;
6199 }
6200
6201 if (ioc->shost_recovery)
6202 goto out_no_lock;
6203
6204 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
96902835
CO
6205 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid,
6206 30);
f92363d1
SR
6207 if (r == FAILED) {
6208 sdev_printk(KERN_WARNING, sdev,
6209 "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
6210 "scmd(%p)\n", scmd);
6211 goto tm_retry;
6212 }
6213
6214 if (task_abort_retries > 1)
6215 sdev_printk(KERN_WARNING, sdev,
6216 "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
6217 " scmd(%p)\n",
6218 task_abort_retries - 1, scmd);
6219
6220 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
6221 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6222 }
6223
6224 if (ioc->broadcast_aen_pending) {
6225 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6226 "%s: loop back due to pending AEN\n",
6227 ioc->name, __func__));
6228 ioc->broadcast_aen_pending = 0;
6229 goto broadcast_aen_retry;
6230 }
6231
6232 out:
6233 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6234 out_no_lock:
6235
6236 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6237 "%s - exit, query_count = %d termination_count = %d\n",
6238 ioc->name, __func__, query_count, termination_count));
6239
6240 ioc->broadcast_aen_busy = 0;
6241 if (!ioc->shost_recovery)
6242 _scsih_ublock_io_all_device(ioc);
6243 mutex_unlock(&ioc->tm_cmds.mutex);
6244}
6245
6246/**
6247 * _scsih_sas_discovery_event - handle discovery events
6248 * @ioc: per adapter object
6249 * @fw_event: The fw_event_work object
6250 * Context: user.
6251 *
6252 * Return nothing.
6253 */
6254static void
6255_scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
6256 struct fw_event_work *fw_event)
6257{
35b62362
JL
6258 Mpi2EventDataSasDiscovery_t *event_data =
6259 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
f92363d1 6260
f92363d1
SR
6261 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
6262 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
6263 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6264 "start" : "stop");
af009411
SR
6265 if (event_data->DiscoveryStatus)
6266 pr_info("discovery_status(0x%08x)",
6267 le32_to_cpu(event_data->DiscoveryStatus));
6268 pr_info("\n");
f92363d1 6269 }
f92363d1
SR
6270
6271 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6272 !ioc->sas_hba.num_phys) {
6273 if (disable_discovery > 0 && ioc->shost_recovery) {
6274 /* Wait for the reset to complete */
6275 while (ioc->shost_recovery)
6276 ssleep(1);
6277 }
6278 _scsih_sas_host_add(ioc);
6279 }
6280}
6281
6282/**
6283 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6284 * @ioc: per adapter object
6285 * @handle: device handle for physical disk
6286 * @phys_disk_num: physical disk number
6287 *
6288 * Return 0 for success, else failure.
6289 */
6290static int
6291_scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6292{
6293 Mpi2RaidActionRequest_t *mpi_request;
6294 Mpi2RaidActionReply_t *mpi_reply;
6295 u16 smid;
6296 u8 issue_reset = 0;
6297 int rc = 0;
6298 u16 ioc_status;
6299 u32 log_info;
6300
c84b06a4
SR
6301 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6302 return rc;
6303
f92363d1
SR
6304 mutex_lock(&ioc->scsih_cmds.mutex);
6305
6306 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6307 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6308 ioc->name, __func__);
6309 rc = -EAGAIN;
6310 goto out;
6311 }
6312 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6313
6314 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6315 if (!smid) {
6316 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6317 ioc->name, __func__);
6318 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6319 rc = -EAGAIN;
6320 goto out;
6321 }
6322
6323 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6324 ioc->scsih_cmds.smid = smid;
6325 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6326
6327 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6328 mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6329 mpi_request->PhysDiskNum = phys_disk_num;
6330
6331 dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6332 "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6333 handle, phys_disk_num));
6334
6335 init_completion(&ioc->scsih_cmds.done);
81c16f83 6336 ioc->put_smid_default(ioc, smid);
f92363d1
SR
6337 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6338
6339 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6340 pr_err(MPT3SAS_FMT "%s: timeout\n",
6341 ioc->name, __func__);
6342 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6343 issue_reset = 1;
6344 rc = -EFAULT;
6345 goto out;
6346 }
6347
6348 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6349
6350 mpi_reply = ioc->scsih_cmds.reply;
6351 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6352 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6353 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
6354 else
6355 log_info = 0;
6356 ioc_status &= MPI2_IOCSTATUS_MASK;
6357 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6358 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6359 "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6360 "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6361 log_info));
6362 rc = -EFAULT;
6363 } else
6364 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6365 "IR RAID_ACTION: completed successfully\n",
6366 ioc->name));
6367 }
6368
6369 out:
6370 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6371 mutex_unlock(&ioc->scsih_cmds.mutex);
6372
6373 if (issue_reset)
98c56ad3 6374 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
6375 return rc;
6376}
6377
6378/**
6379 * _scsih_reprobe_lun - reprobing lun
6380 * @sdev: scsi device struct
6381 * @no_uld_attach: sdev->no_uld_attach flag setting
6382 *
6383 **/
6384static void
6385_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6386{
f92363d1
SR
6387 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6388 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6c7abffc 6389 sdev->no_uld_attach ? "hiding" : "exposing");
8bbb1cf6 6390 WARN_ON(scsi_device_reprobe(sdev));
f92363d1
SR
6391}
6392
6393/**
6394 * _scsih_sas_volume_add - add new volume
6395 * @ioc: per adapter object
6396 * @element: IR config element data
6397 * Context: user.
6398 *
6399 * Return nothing.
6400 */
6401static void
6402_scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6403 Mpi2EventIrConfigElement_t *element)
6404{
6405 struct _raid_device *raid_device;
6406 unsigned long flags;
6407 u64 wwid;
6408 u16 handle = le16_to_cpu(element->VolDevHandle);
6409 int rc;
6410
6411 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6412 if (!wwid) {
6413 pr_err(MPT3SAS_FMT
6414 "failure at %s:%d/%s()!\n", ioc->name,
6415 __FILE__, __LINE__, __func__);
6416 return;
6417 }
6418
6419 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6420 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6421 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6422
6423 if (raid_device)
6424 return;
6425
6426 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6427 if (!raid_device) {
6428 pr_err(MPT3SAS_FMT
6429 "failure at %s:%d/%s()!\n", ioc->name,
6430 __FILE__, __LINE__, __func__);
6431 return;
6432 }
6433
6434 raid_device->id = ioc->sas_id++;
6435 raid_device->channel = RAID_CHANNEL;
6436 raid_device->handle = handle;
6437 raid_device->wwid = wwid;
6438 _scsih_raid_device_add(ioc, raid_device);
6439 if (!ioc->wait_for_discovery_to_complete) {
6440 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6441 raid_device->id, 0);
6442 if (rc)
6443 _scsih_raid_device_remove(ioc, raid_device);
6444 } else {
6445 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6446 _scsih_determine_boot_device(ioc, raid_device, 1);
6447 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6448 }
6449}
6450
6451/**
6452 * _scsih_sas_volume_delete - delete volume
6453 * @ioc: per adapter object
6454 * @handle: volume device handle
6455 * Context: user.
6456 *
6457 * Return nothing.
6458 */
6459static void
6460_scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6461{
6462 struct _raid_device *raid_device;
6463 unsigned long flags;
6464 struct MPT3SAS_TARGET *sas_target_priv_data;
6465 struct scsi_target *starget = NULL;
6466
6467 spin_lock_irqsave(&ioc->raid_device_lock, flags);
c84b06a4 6468 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
6469 if (raid_device) {
6470 if (raid_device->starget) {
6471 starget = raid_device->starget;
6472 sas_target_priv_data = starget->hostdata;
6473 sas_target_priv_data->deleted = 1;
6474 }
6475 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6476 ioc->name, raid_device->handle,
6477 (unsigned long long) raid_device->wwid);
6478 list_del(&raid_device->list);
6479 kfree(raid_device);
6480 }
6481 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6482 if (starget)
6483 scsi_remove_target(&starget->dev);
6484}
6485
6486/**
6487 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6488 * @ioc: per adapter object
6489 * @element: IR config element data
6490 * Context: user.
6491 *
6492 * Return nothing.
6493 */
6494static void
6495_scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6496 Mpi2EventIrConfigElement_t *element)
6497{
6498 struct _sas_device *sas_device;
6499 struct scsi_target *starget = NULL;
6500 struct MPT3SAS_TARGET *sas_target_priv_data;
6501 unsigned long flags;
6502 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6503
6504 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 6505 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
f92363d1
SR
6506 if (sas_device) {
6507 sas_device->volume_handle = 0;
6508 sas_device->volume_wwid = 0;
6509 clear_bit(handle, ioc->pd_handles);
6510 if (sas_device->starget && sas_device->starget->hostdata) {
6511 starget = sas_device->starget;
6512 sas_target_priv_data = starget->hostdata;
6513 sas_target_priv_data->flags &=
6514 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6515 }
6516 }
6517 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6518 if (!sas_device)
6519 return;
6520
6521 /* exposing raid component */
6522 if (starget)
6523 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
d1cb5e49
SR
6524
6525 sas_device_put(sas_device);
f92363d1
SR
6526}
6527
6528/**
6529 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6530 * @ioc: per adapter object
6531 * @element: IR config element data
6532 * Context: user.
6533 *
6534 * Return nothing.
6535 */
6536static void
6537_scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6538 Mpi2EventIrConfigElement_t *element)
6539{
6540 struct _sas_device *sas_device;
6541 struct scsi_target *starget = NULL;
6542 struct MPT3SAS_TARGET *sas_target_priv_data;
6543 unsigned long flags;
6544 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6545 u16 volume_handle = 0;
6546 u64 volume_wwid = 0;
6547
6548 mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6549 if (volume_handle)
6550 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6551 &volume_wwid);
6552
6553 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 6554 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
f92363d1
SR
6555 if (sas_device) {
6556 set_bit(handle, ioc->pd_handles);
6557 if (sas_device->starget && sas_device->starget->hostdata) {
6558 starget = sas_device->starget;
6559 sas_target_priv_data = starget->hostdata;
6560 sas_target_priv_data->flags |=
6561 MPT_TARGET_FLAGS_RAID_COMPONENT;
6562 sas_device->volume_handle = volume_handle;
6563 sas_device->volume_wwid = volume_wwid;
6564 }
6565 }
6566 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6567 if (!sas_device)
6568 return;
6569
6570 /* hiding raid component */
6571 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
c84b06a4 6572
f92363d1
SR
6573 if (starget)
6574 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
d1cb5e49
SR
6575
6576 sas_device_put(sas_device);
f92363d1
SR
6577}
6578
6579/**
6580 * _scsih_sas_pd_delete - delete pd component
6581 * @ioc: per adapter object
6582 * @element: IR config element data
6583 * Context: user.
6584 *
6585 * Return nothing.
6586 */
6587static void
6588_scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6589 Mpi2EventIrConfigElement_t *element)
6590{
6591 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6592
6593 _scsih_device_remove_by_handle(ioc, handle);
6594}
6595
6596/**
6597 * _scsih_sas_pd_add - remove pd component
6598 * @ioc: per adapter object
6599 * @element: IR config element data
6600 * Context: user.
6601 *
6602 * Return nothing.
6603 */
6604static void
6605_scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6606 Mpi2EventIrConfigElement_t *element)
6607{
6608 struct _sas_device *sas_device;
f92363d1
SR
6609 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6610 Mpi2ConfigReply_t mpi_reply;
6611 Mpi2SasDevicePage0_t sas_device_pg0;
6612 u32 ioc_status;
6613 u64 sas_address;
6614 u16 parent_handle;
6615
6616 set_bit(handle, ioc->pd_handles);
6617
d1cb5e49 6618 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
f92363d1
SR
6619 if (sas_device) {
6620 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
d1cb5e49 6621 sas_device_put(sas_device);
f92363d1
SR
6622 return;
6623 }
6624
6625 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6626 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6627 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6628 ioc->name, __FILE__, __LINE__, __func__);
6629 return;
6630 }
6631
6632 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6633 MPI2_IOCSTATUS_MASK;
6634 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6635 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6636 ioc->name, __FILE__, __LINE__, __func__);
6637 return;
6638 }
6639
6640 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6641 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6642 mpt3sas_transport_update_links(ioc, sas_address, handle,
6643 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6644
6645 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6646 _scsih_add_device(ioc, handle, 0, 1);
6647}
6648
f92363d1
SR
6649/**
6650 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6651 * @ioc: per adapter object
6652 * @event_data: event data payload
6653 * Context: user.
6654 *
6655 * Return nothing.
6656 */
6657static void
6658_scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6659 Mpi2EventDataIrConfigChangeList_t *event_data)
6660{
6661 Mpi2EventIrConfigElement_t *element;
6662 u8 element_type;
6663 int i;
6664 char *reason_str = NULL, *element_str = NULL;
6665
6666 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6667
6668 pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6669 ioc->name, (le32_to_cpu(event_data->Flags) &
6670 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6671 "foreign" : "native", event_data->NumElements);
6672 for (i = 0; i < event_data->NumElements; i++, element++) {
6673 switch (element->ReasonCode) {
6674 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6675 reason_str = "add";
6676 break;
6677 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6678 reason_str = "remove";
6679 break;
6680 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6681 reason_str = "no change";
6682 break;
6683 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6684 reason_str = "hide";
6685 break;
6686 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6687 reason_str = "unhide";
6688 break;
6689 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6690 reason_str = "volume_created";
6691 break;
6692 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6693 reason_str = "volume_deleted";
6694 break;
6695 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6696 reason_str = "pd_created";
6697 break;
6698 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6699 reason_str = "pd_deleted";
6700 break;
6701 default:
6702 reason_str = "unknown reason";
6703 break;
6704 }
6705 element_type = le16_to_cpu(element->ElementFlags) &
6706 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6707 switch (element_type) {
6708 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6709 element_str = "volume";
6710 break;
6711 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6712 element_str = "phys disk";
6713 break;
6714 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6715 element_str = "hot spare";
6716 break;
6717 default:
6718 element_str = "unknown element";
6719 break;
6720 }
6721 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6722 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6723 reason_str, le16_to_cpu(element->VolDevHandle),
6724 le16_to_cpu(element->PhysDiskDevHandle),
6725 element->PhysDiskNum);
6726 }
6727}
f92363d1
SR
6728
6729/**
6730 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6731 * @ioc: per adapter object
6732 * @fw_event: The fw_event_work object
6733 * Context: user.
6734 *
6735 * Return nothing.
6736 */
6737static void
6738_scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6739 struct fw_event_work *fw_event)
6740{
6741 Mpi2EventIrConfigElement_t *element;
6742 int i;
6743 u8 foreign_config;
35b62362
JL
6744 Mpi2EventDataIrConfigChangeList_t *event_data =
6745 (Mpi2EventDataIrConfigChangeList_t *)
6746 fw_event->event_data;
f92363d1 6747
7786ab6a
SR
6748 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6749 (!ioc->hide_ir_msg))
f92363d1
SR
6750 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6751
f92363d1
SR
6752 foreign_config = (le32_to_cpu(event_data->Flags) &
6753 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6754
6755 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
c84b06a4
SR
6756 if (ioc->shost_recovery &&
6757 ioc->hba_mpi_version_belonged != MPI2_VERSION) {
f92363d1
SR
6758 for (i = 0; i < event_data->NumElements; i++, element++) {
6759 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6760 _scsih_ir_fastpath(ioc,
6761 le16_to_cpu(element->PhysDiskDevHandle),
6762 element->PhysDiskNum);
6763 }
6764 return;
6765 }
7786ab6a 6766
f92363d1
SR
6767 for (i = 0; i < event_data->NumElements; i++, element++) {
6768
6769 switch (element->ReasonCode) {
6770 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6771 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6772 if (!foreign_config)
6773 _scsih_sas_volume_add(ioc, element);
6774 break;
6775 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6776 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6777 if (!foreign_config)
6778 _scsih_sas_volume_delete(ioc,
6779 le16_to_cpu(element->VolDevHandle));
6780 break;
6781 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
7786ab6a
SR
6782 if (!ioc->is_warpdrive)
6783 _scsih_sas_pd_hide(ioc, element);
f92363d1
SR
6784 break;
6785 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
7786ab6a
SR
6786 if (!ioc->is_warpdrive)
6787 _scsih_sas_pd_expose(ioc, element);
f92363d1
SR
6788 break;
6789 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
7786ab6a
SR
6790 if (!ioc->is_warpdrive)
6791 _scsih_sas_pd_add(ioc, element);
f92363d1
SR
6792 break;
6793 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
7786ab6a
SR
6794 if (!ioc->is_warpdrive)
6795 _scsih_sas_pd_delete(ioc, element);
f92363d1
SR
6796 break;
6797 }
6798 }
6799}
6800
6801/**
6802 * _scsih_sas_ir_volume_event - IR volume event
6803 * @ioc: per adapter object
6804 * @fw_event: The fw_event_work object
6805 * Context: user.
6806 *
6807 * Return nothing.
6808 */
6809static void
6810_scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6811 struct fw_event_work *fw_event)
6812{
6813 u64 wwid;
6814 unsigned long flags;
6815 struct _raid_device *raid_device;
6816 u16 handle;
6817 u32 state;
6818 int rc;
35b62362
JL
6819 Mpi2EventDataIrVolume_t *event_data =
6820 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
f92363d1
SR
6821
6822 if (ioc->shost_recovery)
6823 return;
6824
6825 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6826 return;
6827
6828 handle = le16_to_cpu(event_data->VolDevHandle);
6829 state = le32_to_cpu(event_data->NewValue);
7786ab6a
SR
6830 if (!ioc->hide_ir_msg)
6831 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6832 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6833 ioc->name, __func__, handle,
6834 le32_to_cpu(event_data->PreviousValue), state));
f92363d1
SR
6835 switch (state) {
6836 case MPI2_RAID_VOL_STATE_MISSING:
6837 case MPI2_RAID_VOL_STATE_FAILED:
6838 _scsih_sas_volume_delete(ioc, handle);
6839 break;
6840
6841 case MPI2_RAID_VOL_STATE_ONLINE:
6842 case MPI2_RAID_VOL_STATE_DEGRADED:
6843 case MPI2_RAID_VOL_STATE_OPTIMAL:
6844
6845 spin_lock_irqsave(&ioc->raid_device_lock, flags);
c84b06a4 6846 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
6847 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6848
6849 if (raid_device)
6850 break;
6851
6852 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6853 if (!wwid) {
6854 pr_err(MPT3SAS_FMT
6855 "failure at %s:%d/%s()!\n", ioc->name,
6856 __FILE__, __LINE__, __func__);
6857 break;
6858 }
6859
6860 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6861 if (!raid_device) {
6862 pr_err(MPT3SAS_FMT
6863 "failure at %s:%d/%s()!\n", ioc->name,
6864 __FILE__, __LINE__, __func__);
6865 break;
6866 }
6867
6868 raid_device->id = ioc->sas_id++;
6869 raid_device->channel = RAID_CHANNEL;
6870 raid_device->handle = handle;
6871 raid_device->wwid = wwid;
6872 _scsih_raid_device_add(ioc, raid_device);
6873 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6874 raid_device->id, 0);
6875 if (rc)
6876 _scsih_raid_device_remove(ioc, raid_device);
6877 break;
6878
6879 case MPI2_RAID_VOL_STATE_INITIALIZING:
6880 default:
6881 break;
6882 }
6883}
6884
6885/**
6886 * _scsih_sas_ir_physical_disk_event - PD event
6887 * @ioc: per adapter object
6888 * @fw_event: The fw_event_work object
6889 * Context: user.
6890 *
6891 * Return nothing.
6892 */
6893static void
6894_scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6895 struct fw_event_work *fw_event)
6896{
6897 u16 handle, parent_handle;
6898 u32 state;
6899 struct _sas_device *sas_device;
f92363d1
SR
6900 Mpi2ConfigReply_t mpi_reply;
6901 Mpi2SasDevicePage0_t sas_device_pg0;
6902 u32 ioc_status;
35b62362
JL
6903 Mpi2EventDataIrPhysicalDisk_t *event_data =
6904 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
f92363d1
SR
6905 u64 sas_address;
6906
6907 if (ioc->shost_recovery)
6908 return;
6909
6910 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6911 return;
6912
6913 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6914 state = le32_to_cpu(event_data->NewValue);
6915
7786ab6a
SR
6916 if (!ioc->hide_ir_msg)
6917 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6918 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6919 ioc->name, __func__, handle,
f92363d1 6920 le32_to_cpu(event_data->PreviousValue), state));
7786ab6a 6921
f92363d1
SR
6922 switch (state) {
6923 case MPI2_RAID_PD_STATE_ONLINE:
6924 case MPI2_RAID_PD_STATE_DEGRADED:
6925 case MPI2_RAID_PD_STATE_REBUILDING:
6926 case MPI2_RAID_PD_STATE_OPTIMAL:
6927 case MPI2_RAID_PD_STATE_HOT_SPARE:
6928
7786ab6a
SR
6929 if (!ioc->is_warpdrive)
6930 set_bit(handle, ioc->pd_handles);
f92363d1 6931
d1cb5e49
SR
6932 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6933 if (sas_device) {
6934 sas_device_put(sas_device);
f92363d1 6935 return;
d1cb5e49 6936 }
f92363d1
SR
6937
6938 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6939 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6940 handle))) {
6941 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6942 ioc->name, __FILE__, __LINE__, __func__);
6943 return;
6944 }
6945
6946 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6947 MPI2_IOCSTATUS_MASK;
6948 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6949 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6950 ioc->name, __FILE__, __LINE__, __func__);
6951 return;
6952 }
6953
6954 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6955 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6956 mpt3sas_transport_update_links(ioc, sas_address, handle,
6957 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6958
6959 _scsih_add_device(ioc, handle, 0, 1);
6960
6961 break;
6962
6963 case MPI2_RAID_PD_STATE_OFFLINE:
6964 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6965 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6966 default:
6967 break;
6968 }
6969}
6970
f92363d1
SR
6971/**
6972 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6973 * @ioc: per adapter object
6974 * @event_data: event data payload
6975 * Context: user.
6976 *
6977 * Return nothing.
6978 */
6979static void
6980_scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6981 Mpi2EventDataIrOperationStatus_t *event_data)
6982{
6983 char *reason_str = NULL;
6984
6985 switch (event_data->RAIDOperation) {
6986 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6987 reason_str = "resync";
6988 break;
6989 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6990 reason_str = "online capacity expansion";
6991 break;
6992 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6993 reason_str = "consistency check";
6994 break;
6995 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6996 reason_str = "background init";
6997 break;
6998 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6999 reason_str = "make data consistent";
7000 break;
7001 }
7002
7003 if (!reason_str)
7004 return;
7005
7006 pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
7007 "\thandle(0x%04x), percent complete(%d)\n",
7008 ioc->name, reason_str,
7009 le16_to_cpu(event_data->VolDevHandle),
7010 event_data->PercentComplete);
7011}
f92363d1
SR
7012
7013/**
7014 * _scsih_sas_ir_operation_status_event - handle RAID operation events
7015 * @ioc: per adapter object
7016 * @fw_event: The fw_event_work object
7017 * Context: user.
7018 *
7019 * Return nothing.
7020 */
7021static void
7022_scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
7023 struct fw_event_work *fw_event)
7024{
35b62362
JL
7025 Mpi2EventDataIrOperationStatus_t *event_data =
7026 (Mpi2EventDataIrOperationStatus_t *)
7027 fw_event->event_data;
f92363d1
SR
7028 static struct _raid_device *raid_device;
7029 unsigned long flags;
7030 u16 handle;
7031
7786ab6a
SR
7032 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
7033 (!ioc->hide_ir_msg))
f92363d1
SR
7034 _scsih_sas_ir_operation_status_event_debug(ioc,
7035 event_data);
f92363d1
SR
7036
7037 /* code added for raid transport support */
7038 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
7039
7040 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7041 handle = le16_to_cpu(event_data->VolDevHandle);
c84b06a4 7042 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
7043 if (raid_device)
7044 raid_device->percent_complete =
7045 event_data->PercentComplete;
7046 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7047 }
7048}
7049
7050/**
7051 * _scsih_prep_device_scan - initialize parameters prior to device scan
7052 * @ioc: per adapter object
7053 *
7054 * Set the deleted flag prior to device scan. If the device is found during
7055 * the scan, then we clear the deleted flag.
7056 */
7057static void
7058_scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
7059{
7060 struct MPT3SAS_DEVICE *sas_device_priv_data;
7061 struct scsi_device *sdev;
7062
7063 shost_for_each_device(sdev, ioc->shost) {
7064 sas_device_priv_data = sdev->hostdata;
7065 if (sas_device_priv_data && sas_device_priv_data->sas_target)
7066 sas_device_priv_data->sas_target->deleted = 1;
7067 }
7068}
7069
7070/**
7071 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
7072 * @ioc: per adapter object
e6d45e3e 7073 * @sas_device_pg0: SAS Device page 0
f92363d1
SR
7074 *
7075 * After host reset, find out whether devices are still responding.
7076 * Used in _scsih_remove_unresponsive_sas_devices.
7077 *
7078 * Return nothing.
7079 */
7080static void
e6d45e3e
SR
7081_scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
7082Mpi2SasDevicePage0_t *sas_device_pg0)
f92363d1
SR
7083{
7084 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7085 struct scsi_target *starget;
7086 struct _sas_device *sas_device;
7087 unsigned long flags;
7088
7089 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7090 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
e6d45e3e
SR
7091 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
7092 (sas_device->slot == sas_device_pg0->Slot)) {
f92363d1
SR
7093 sas_device->responding = 1;
7094 starget = sas_device->starget;
7095 if (starget && starget->hostdata) {
7096 sas_target_priv_data = starget->hostdata;
7097 sas_target_priv_data->tm_busy = 0;
7098 sas_target_priv_data->deleted = 0;
7099 } else
7100 sas_target_priv_data = NULL;
e6d45e3e 7101 if (starget) {
f92363d1 7102 starget_printk(KERN_INFO, starget,
e6d45e3e
SR
7103 "handle(0x%04x), sas_addr(0x%016llx)\n",
7104 sas_device_pg0->DevHandle,
f92363d1 7105 (unsigned long long)
e6d45e3e
SR
7106 sas_device->sas_address);
7107
7108 if (sas_device->enclosure_handle != 0)
7109 starget_printk(KERN_INFO, starget,
7110 "enclosure logical id(0x%016llx),"
7111 " slot(%d)\n",
7112 (unsigned long long)
7113 sas_device->enclosure_logical_id,
7114 sas_device->slot);
7115 }
7116 if (sas_device_pg0->Flags &
7117 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
7118 sas_device->enclosure_level =
aa53bb89 7119 sas_device_pg0->EnclosureLevel;
e6d45e3e
SR
7120 memcpy(&sas_device->connector_name[0],
7121 &sas_device_pg0->ConnectorName[0], 4);
7122 } else {
7123 sas_device->enclosure_level = 0;
7124 sas_device->connector_name[0] = '\0';
7125 }
7126
7127 if (sas_device->handle == sas_device_pg0->DevHandle)
f92363d1
SR
7128 goto out;
7129 pr_info("\thandle changed from(0x%04x)!!!\n",
7130 sas_device->handle);
e6d45e3e 7131 sas_device->handle = sas_device_pg0->DevHandle;
f92363d1 7132 if (sas_target_priv_data)
e6d45e3e
SR
7133 sas_target_priv_data->handle =
7134 sas_device_pg0->DevHandle;
f92363d1
SR
7135 goto out;
7136 }
7137 }
7138 out:
7139 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7140}
7141
7142/**
7143 * _scsih_search_responding_sas_devices -
7144 * @ioc: per adapter object
7145 *
7146 * After host reset, find out whether devices are still responding.
7147 * If not remove.
7148 *
7149 * Return nothing.
7150 */
7151static void
7152_scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7153{
7154 Mpi2SasDevicePage0_t sas_device_pg0;
7155 Mpi2ConfigReply_t mpi_reply;
7156 u16 ioc_status;
7157 u16 handle;
7158 u32 device_info;
7159
7160 pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
7161
7162 if (list_empty(&ioc->sas_device_list))
7163 goto out;
7164
7165 handle = 0xFFFF;
7166 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7167 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7168 handle))) {
7169 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7170 MPI2_IOCSTATUS_MASK;
14be49ac 7171 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
f92363d1 7172 break;
e6d45e3e
SR
7173 handle = sas_device_pg0.DevHandle =
7174 le16_to_cpu(sas_device_pg0.DevHandle);
f92363d1
SR
7175 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
7176 if (!(_scsih_is_end_device(device_info)))
7177 continue;
e6d45e3e
SR
7178 sas_device_pg0.SASAddress =
7179 le64_to_cpu(sas_device_pg0.SASAddress);
7180 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
aa53bb89 7181 sas_device_pg0.Flags = le16_to_cpu(sas_device_pg0.Flags);
e6d45e3e 7182 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
f92363d1
SR
7183 }
7184
7185 out:
7186 pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
7187 ioc->name);
7188}
7189
7190/**
7191 * _scsih_mark_responding_raid_device - mark a raid_device as responding
7192 * @ioc: per adapter object
7193 * @wwid: world wide identifier for raid volume
7194 * @handle: device handle
7195 *
7196 * After host reset, find out whether devices are still responding.
7197 * Used in _scsih_remove_unresponsive_raid_devices.
7198 *
7199 * Return nothing.
7200 */
7201static void
7202_scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
7203 u16 handle)
7204{
7786ab6a 7205 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
f92363d1
SR
7206 struct scsi_target *starget;
7207 struct _raid_device *raid_device;
7208 unsigned long flags;
7209
7210 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7211 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
7212 if (raid_device->wwid == wwid && raid_device->starget) {
7213 starget = raid_device->starget;
7214 if (starget && starget->hostdata) {
7215 sas_target_priv_data = starget->hostdata;
7216 sas_target_priv_data->deleted = 0;
7217 } else
7218 sas_target_priv_data = NULL;
7219 raid_device->responding = 1;
7220 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7221 starget_printk(KERN_INFO, raid_device->starget,
7222 "handle(0x%04x), wwid(0x%016llx)\n", handle,
7223 (unsigned long long)raid_device->wwid);
7786ab6a 7224
7786ab6a
SR
7225 /*
7226 * WARPDRIVE: The handles of the PDs might have changed
7227 * across the host reset so re-initialize the
7228 * required data for Direct IO
7229 */
c84b06a4 7230 mpt3sas_init_warpdrive_properties(ioc, raid_device);
f92363d1
SR
7231 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7232 if (raid_device->handle == handle) {
7233 spin_unlock_irqrestore(&ioc->raid_device_lock,
7234 flags);
7235 return;
7236 }
7237 pr_info("\thandle changed from(0x%04x)!!!\n",
7238 raid_device->handle);
7239 raid_device->handle = handle;
7240 if (sas_target_priv_data)
7241 sas_target_priv_data->handle = handle;
7242 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7243 return;
7244 }
7245 }
7246 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7247}
7248
7249/**
7250 * _scsih_search_responding_raid_devices -
7251 * @ioc: per adapter object
7252 *
7253 * After host reset, find out whether devices are still responding.
7254 * If not remove.
7255 *
7256 * Return nothing.
7257 */
7258static void
7259_scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
7260{
7261 Mpi2RaidVolPage1_t volume_pg1;
7262 Mpi2RaidVolPage0_t volume_pg0;
7263 Mpi2RaidPhysDiskPage0_t pd_pg0;
7264 Mpi2ConfigReply_t mpi_reply;
7265 u16 ioc_status;
7266 u16 handle;
7267 u8 phys_disk_num;
7268
7269 if (!ioc->ir_firmware)
7270 return;
7271
7272 pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7273 ioc->name);
7274
7275 if (list_empty(&ioc->raid_device_list))
7276 goto out;
7277
7278 handle = 0xFFFF;
7279 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7280 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7281 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7282 MPI2_IOCSTATUS_MASK;
14be49ac 7283 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
f92363d1
SR
7284 break;
7285 handle = le16_to_cpu(volume_pg1.DevHandle);
7286
7287 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7288 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7289 sizeof(Mpi2RaidVolPage0_t)))
7290 continue;
7291
7292 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7293 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7294 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7295 _scsih_mark_responding_raid_device(ioc,
7296 le64_to_cpu(volume_pg1.WWID), handle);
7297 }
7298
7299 /* refresh the pd_handles */
7786ab6a 7300 if (!ioc->is_warpdrive) {
f92363d1
SR
7301 phys_disk_num = 0xFF;
7302 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7303 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7304 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7305 phys_disk_num))) {
7306 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7307 MPI2_IOCSTATUS_MASK;
14be49ac 7308 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
f92363d1
SR
7309 break;
7310 phys_disk_num = pd_pg0.PhysDiskNum;
7311 handle = le16_to_cpu(pd_pg0.DevHandle);
7312 set_bit(handle, ioc->pd_handles);
7313 }
7786ab6a 7314 }
f92363d1
SR
7315 out:
7316 pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7317 ioc->name);
7318}
7319
7320/**
7321 * _scsih_mark_responding_expander - mark a expander as responding
7322 * @ioc: per adapter object
7323 * @sas_address: sas address
7324 * @handle:
7325 *
7326 * After host reset, find out whether devices are still responding.
7327 * Used in _scsih_remove_unresponsive_expanders.
7328 *
7329 * Return nothing.
7330 */
7331static void
7332_scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7333 u16 handle)
7334{
7335 struct _sas_node *sas_expander;
7336 unsigned long flags;
7337 int i;
7338
7339 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7340 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7341 if (sas_expander->sas_address != sas_address)
7342 continue;
7343 sas_expander->responding = 1;
7344 if (sas_expander->handle == handle)
7345 goto out;
7346 pr_info("\texpander(0x%016llx): handle changed" \
7347 " from(0x%04x) to (0x%04x)!!!\n",
7348 (unsigned long long)sas_expander->sas_address,
7349 sas_expander->handle, handle);
7350 sas_expander->handle = handle;
7351 for (i = 0 ; i < sas_expander->num_phys ; i++)
7352 sas_expander->phy[i].handle = handle;
7353 goto out;
7354 }
7355 out:
7356 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7357}
7358
7359/**
7360 * _scsih_search_responding_expanders -
7361 * @ioc: per adapter object
7362 *
7363 * After host reset, find out whether devices are still responding.
7364 * If not remove.
7365 *
7366 * Return nothing.
7367 */
7368static void
7369_scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7370{
7371 Mpi2ExpanderPage0_t expander_pg0;
7372 Mpi2ConfigReply_t mpi_reply;
7373 u16 ioc_status;
7374 u64 sas_address;
7375 u16 handle;
7376
7377 pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7378
7379 if (list_empty(&ioc->sas_expander_list))
7380 goto out;
7381
7382 handle = 0xFFFF;
7383 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7384 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7385
7386 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7387 MPI2_IOCSTATUS_MASK;
14be49ac 7388 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
f92363d1
SR
7389 break;
7390
7391 handle = le16_to_cpu(expander_pg0.DevHandle);
7392 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7393 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7394 handle,
7395 (unsigned long long)sas_address);
7396 _scsih_mark_responding_expander(ioc, sas_address, handle);
7397 }
7398
7399 out:
7400 pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7401}
7402
7403/**
7404 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7405 * @ioc: per adapter object
7406 *
7407 * Return nothing.
7408 */
7409static void
7410_scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7411{
7412 struct _sas_device *sas_device, *sas_device_next;
7413 struct _sas_node *sas_expander, *sas_expander_next;
7414 struct _raid_device *raid_device, *raid_device_next;
7415 struct list_head tmp_list;
7416 unsigned long flags;
d1cb5e49 7417 LIST_HEAD(head);
f92363d1
SR
7418
7419 pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7420 ioc->name);
7421
7422 /* removing unresponding end devices */
7423 pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7424 ioc->name);
d1cb5e49
SR
7425 /*
7426 * Iterate, pulling off devices marked as non-responding. We become the
7427 * owner for the reference the list had on any object we prune.
7428 */
7429 spin_lock_irqsave(&ioc->sas_device_lock, flags);
f92363d1
SR
7430 list_for_each_entry_safe(sas_device, sas_device_next,
7431 &ioc->sas_device_list, list) {
7432 if (!sas_device->responding)
d1cb5e49 7433 list_move_tail(&sas_device->list, &head);
f92363d1
SR
7434 else
7435 sas_device->responding = 0;
7436 }
d1cb5e49
SR
7437 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7438
7439 /*
7440 * Now, uninitialize and remove the unresponding devices we pruned.
7441 */
7442 list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7443 _scsih_remove_device(ioc, sas_device);
7444 list_del_init(&sas_device->list);
7445 sas_device_put(sas_device);
7446 }
f92363d1
SR
7447
7448 /* removing unresponding volumes */
7449 if (ioc->ir_firmware) {
7450 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7451 ioc->name);
7452 list_for_each_entry_safe(raid_device, raid_device_next,
7453 &ioc->raid_device_list, list) {
7454 if (!raid_device->responding)
7455 _scsih_sas_volume_delete(ioc,
7456 raid_device->handle);
7457 else
7458 raid_device->responding = 0;
7459 }
7460 }
7461
7462 /* removing unresponding expanders */
7463 pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7464 ioc->name);
7465 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7466 INIT_LIST_HEAD(&tmp_list);
7467 list_for_each_entry_safe(sas_expander, sas_expander_next,
7468 &ioc->sas_expander_list, list) {
7469 if (!sas_expander->responding)
7470 list_move_tail(&sas_expander->list, &tmp_list);
7471 else
7472 sas_expander->responding = 0;
7473 }
7474 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7475 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7476 list) {
f92363d1
SR
7477 _scsih_expander_node_remove(ioc, sas_expander);
7478 }
7479
7480 pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7481 ioc->name);
7482
7483 /* unblock devices */
7484 _scsih_ublock_io_all_device(ioc);
7485}
7486
7487static void
7488_scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7489 struct _sas_node *sas_expander, u16 handle)
7490{
7491 Mpi2ExpanderPage1_t expander_pg1;
7492 Mpi2ConfigReply_t mpi_reply;
7493 int i;
7494
7495 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7496 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7497 &expander_pg1, i, handle))) {
7498 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7499 ioc->name, __FILE__, __LINE__, __func__);
7500 return;
7501 }
7502
7503 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7504 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7505 expander_pg1.NegotiatedLinkRate >> 4);
7506 }
7507}
7508
7509/**
7510 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7511 * @ioc: per adapter object
7512 *
7513 * Return nothing.
7514 */
7515static void
7516_scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7517{
7518 Mpi2ExpanderPage0_t expander_pg0;
7519 Mpi2SasDevicePage0_t sas_device_pg0;
7520 Mpi2RaidVolPage1_t volume_pg1;
7521 Mpi2RaidVolPage0_t volume_pg0;
7522 Mpi2RaidPhysDiskPage0_t pd_pg0;
7523 Mpi2EventIrConfigElement_t element;
7524 Mpi2ConfigReply_t mpi_reply;
7525 u8 phys_disk_num;
7526 u16 ioc_status;
7527 u16 handle, parent_handle;
7528 u64 sas_address;
7529 struct _sas_device *sas_device;
7530 struct _sas_node *expander_device;
7531 static struct _raid_device *raid_device;
7532 u8 retry_count;
7533 unsigned long flags;
7534
7535 pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7536
7537 _scsih_sas_host_refresh(ioc);
7538
7539 pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7540
7541 /* expanders */
7542 handle = 0xFFFF;
7543 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7544 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7545 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7546 MPI2_IOCSTATUS_MASK;
f92363d1
SR
7547 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7548 pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7549 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7550 ioc->name, ioc_status,
7551 le32_to_cpu(mpi_reply.IOCLogInfo));
7552 break;
7553 }
7554 handle = le16_to_cpu(expander_pg0.DevHandle);
7555 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7556 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7557 ioc, le64_to_cpu(expander_pg0.SASAddress));
7558 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7559 if (expander_device)
7560 _scsih_refresh_expander_links(ioc, expander_device,
7561 handle);
7562 else {
7563 pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7564 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7565 handle, (unsigned long long)
7566 le64_to_cpu(expander_pg0.SASAddress));
7567 _scsih_expander_add(ioc, handle);
7568 pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7569 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7570 handle, (unsigned long long)
7571 le64_to_cpu(expander_pg0.SASAddress));
7572 }
7573 }
7574
7575 pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7576 ioc->name);
7577
7578 if (!ioc->ir_firmware)
7579 goto skip_to_sas;
7580
7581 pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7582
7583 /* phys disk */
7584 phys_disk_num = 0xFF;
7585 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7586 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7587 phys_disk_num))) {
7588 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7589 MPI2_IOCSTATUS_MASK;
f92363d1
SR
7590 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7591 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7592 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7593 ioc->name, ioc_status,
7594 le32_to_cpu(mpi_reply.IOCLogInfo));
7595 break;
7596 }
7597 phys_disk_num = pd_pg0.PhysDiskNum;
7598 handle = le16_to_cpu(pd_pg0.DevHandle);
d1cb5e49
SR
7599 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7600 if (sas_device) {
7601 sas_device_put(sas_device);
f92363d1 7602 continue;
d1cb5e49 7603 }
f92363d1
SR
7604 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7605 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7606 handle) != 0)
7607 continue;
7608 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7609 MPI2_IOCSTATUS_MASK;
7610 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7611 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7612 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7613 ioc->name, ioc_status,
7614 le32_to_cpu(mpi_reply.IOCLogInfo));
7615 break;
7616 }
7617 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7618 if (!_scsih_get_sas_address(ioc, parent_handle,
7619 &sas_address)) {
7620 pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7621 " handle (0x%04x), sas_addr(0x%016llx)\n",
7622 ioc->name, handle, (unsigned long long)
7623 le64_to_cpu(sas_device_pg0.SASAddress));
7624 mpt3sas_transport_update_links(ioc, sas_address,
7625 handle, sas_device_pg0.PhyNum,
7626 MPI2_SAS_NEG_LINK_RATE_1_5);
7627 set_bit(handle, ioc->pd_handles);
7628 retry_count = 0;
7629 /* This will retry adding the end device.
7630 * _scsih_add_device() will decide on retries and
7631 * return "1" when it should be retried
7632 */
7633 while (_scsih_add_device(ioc, handle, retry_count++,
7634 1)) {
7635 ssleep(1);
7636 }
7637 pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7638 " handle (0x%04x), sas_addr(0x%016llx)\n",
7639 ioc->name, handle, (unsigned long long)
7640 le64_to_cpu(sas_device_pg0.SASAddress));
7641 }
7642 }
7643
7644 pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7645 ioc->name);
7646
7647 pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7648
7649 /* volumes */
7650 handle = 0xFFFF;
7651 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7652 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7653 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7654 MPI2_IOCSTATUS_MASK;
f92363d1
SR
7655 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7656 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7657 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7658 ioc->name, ioc_status,
7659 le32_to_cpu(mpi_reply.IOCLogInfo));
7660 break;
7661 }
7662 handle = le16_to_cpu(volume_pg1.DevHandle);
7663 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7664 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7665 le64_to_cpu(volume_pg1.WWID));
7666 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7667 if (raid_device)
7668 continue;
7669 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7670 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7671 sizeof(Mpi2RaidVolPage0_t)))
7672 continue;
7673 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7674 MPI2_IOCSTATUS_MASK;
7675 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7676 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7677 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7678 ioc->name, ioc_status,
7679 le32_to_cpu(mpi_reply.IOCLogInfo));
7680 break;
7681 }
7682 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7683 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7684 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7685 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7686 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7687 element.VolDevHandle = volume_pg1.DevHandle;
7688 pr_info(MPT3SAS_FMT
7689 "\tBEFORE adding volume: handle (0x%04x)\n",
7690 ioc->name, volume_pg1.DevHandle);
7691 _scsih_sas_volume_add(ioc, &element);
7692 pr_info(MPT3SAS_FMT
7693 "\tAFTER adding volume: handle (0x%04x)\n",
7694 ioc->name, volume_pg1.DevHandle);
7695 }
7696 }
7697
7698 pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7699 ioc->name);
7700
7701 skip_to_sas:
7702
7703 pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7704 ioc->name);
7705
7706 /* sas devices */
7707 handle = 0xFFFF;
7708 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7709 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7710 handle))) {
7711 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7712 MPI2_IOCSTATUS_MASK;
f92363d1
SR
7713 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7714 pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7715 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7716 ioc->name, ioc_status,
7717 le32_to_cpu(mpi_reply.IOCLogInfo));
7718 break;
7719 }
7720 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7721 if (!(_scsih_is_end_device(
7722 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7723 continue;
d1cb5e49 7724 sas_device = mpt3sas_get_sdev_by_addr(ioc,
f92363d1 7725 le64_to_cpu(sas_device_pg0.SASAddress));
d1cb5e49
SR
7726 if (sas_device) {
7727 sas_device_put(sas_device);
f92363d1 7728 continue;
d1cb5e49 7729 }
f92363d1
SR
7730 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7731 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7732 pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7733 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7734 handle, (unsigned long long)
7735 le64_to_cpu(sas_device_pg0.SASAddress));
7736 mpt3sas_transport_update_links(ioc, sas_address, handle,
7737 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7738 retry_count = 0;
7739 /* This will retry adding the end device.
7740 * _scsih_add_device() will decide on retries and
7741 * return "1" when it should be retried
7742 */
7743 while (_scsih_add_device(ioc, handle, retry_count++,
7744 0)) {
7745 ssleep(1);
7746 }
7747 pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7748 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7749 handle, (unsigned long long)
7750 le64_to_cpu(sas_device_pg0.SASAddress));
7751 }
7752 }
7753 pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7754 ioc->name);
7755
7756 pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7757}
7758/**
7759 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7760 * @ioc: per adapter object
7761 * @reset_phase: phase
7762 *
7763 * The handler for doing any required cleanup or initialization.
7764 *
7765 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7766 * MPT3_IOC_DONE_RESET
7767 *
7768 * Return nothing.
7769 */
7770void
7771mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7772{
7773 switch (reset_phase) {
7774 case MPT3_IOC_PRE_RESET:
7775 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7776 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7777 break;
7778 case MPT3_IOC_AFTER_RESET:
7779 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7780 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7781 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7782 ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7783 mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7784 complete(&ioc->scsih_cmds.done);
7785 }
7786 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7787 ioc->tm_cmds.status |= MPT3_CMD_RESET;
7788 mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7789 complete(&ioc->tm_cmds.done);
7790 }
7791
c696f7b8
SPS
7792 memset(ioc->pend_os_device_add, 0, ioc->pend_os_device_add_sz);
7793 memset(ioc->device_remove_in_progress, 0,
7794 ioc->device_remove_in_progress_sz);
f92363d1
SR
7795 _scsih_fw_event_cleanup_queue(ioc);
7796 _scsih_flush_running_cmds(ioc);
7797 break;
7798 case MPT3_IOC_DONE_RESET:
7799 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7800 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7801 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7802 !ioc->sas_hba.num_phys)) {
7803 _scsih_prep_device_scan(ioc);
7804 _scsih_search_responding_sas_devices(ioc);
7805 _scsih_search_responding_raid_devices(ioc);
7806 _scsih_search_responding_expanders(ioc);
7807 _scsih_error_recovery_delete_devices(ioc);
7808 }
7809 break;
7810 }
7811}
7812
7813/**
7814 * _mpt3sas_fw_work - delayed task for processing firmware events
7815 * @ioc: per adapter object
7816 * @fw_event: The fw_event_work object
7817 * Context: user.
7818 *
7819 * Return nothing.
7820 */
7821static void
7822_mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7823{
146b16c8
SR
7824 _scsih_fw_event_del_from_list(ioc, fw_event);
7825
f92363d1 7826 /* the queue is being flushed so ignore this event */
146b16c8
SR
7827 if (ioc->remove_host || ioc->pci_error_recovery) {
7828 fw_event_work_put(fw_event);
f92363d1
SR
7829 return;
7830 }
7831
7832 switch (fw_event->event) {
7833 case MPT3SAS_PROCESS_TRIGGER_DIAG:
35b62362
JL
7834 mpt3sas_process_trigger_data(ioc,
7835 (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7836 fw_event->event_data);
f92363d1
SR
7837 break;
7838 case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
146b16c8
SR
7839 while (scsi_host_in_recovery(ioc->shost) ||
7840 ioc->shost_recovery) {
7841 /*
7842 * If we're unloading, bail. Otherwise, this can become
7843 * an infinite loop.
7844 */
7845 if (ioc->remove_host)
7846 goto out;
f92363d1 7847 ssleep(1);
146b16c8 7848 }
f92363d1
SR
7849 _scsih_remove_unresponding_sas_devices(ioc);
7850 _scsih_scan_for_devices_after_reset(ioc);
7851 break;
7852 case MPT3SAS_PORT_ENABLE_COMPLETE:
7853 ioc->start_scan = 0;
7854 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7855 mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7856 missing_delay[1]);
7857 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7858 "port enable: complete from worker thread\n",
7859 ioc->name));
7860 break;
0f624c39
SR
7861 case MPT3SAS_TURN_ON_PFA_LED:
7862 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
f92363d1
SR
7863 break;
7864 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7865 _scsih_sas_topology_change_event(ioc, fw_event);
7866 break;
7867 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7868 _scsih_sas_device_status_change_event(ioc, fw_event);
7869 break;
7870 case MPI2_EVENT_SAS_DISCOVERY:
7871 _scsih_sas_discovery_event(ioc, fw_event);
7872 break;
7873 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7874 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7875 break;
7876 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7877 _scsih_sas_enclosure_dev_status_change_event(ioc,
7878 fw_event);
7879 break;
7880 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7881 _scsih_sas_ir_config_change_event(ioc, fw_event);
7882 break;
7883 case MPI2_EVENT_IR_VOLUME:
7884 _scsih_sas_ir_volume_event(ioc, fw_event);
7885 break;
7886 case MPI2_EVENT_IR_PHYSICAL_DISK:
7887 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7888 break;
7889 case MPI2_EVENT_IR_OPERATION_STATUS:
7890 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7891 break;
7892 }
146b16c8
SR
7893out:
7894 fw_event_work_put(fw_event);
f92363d1
SR
7895}
7896
7897/**
7898 * _firmware_event_work
7899 * @ioc: per adapter object
7900 * @work: The fw_event_work object
7901 * Context: user.
7902 *
7903 * wrappers for the work thread handling firmware events
7904 *
7905 * Return nothing.
7906 */
7907
7908static void
7909_firmware_event_work(struct work_struct *work)
7910{
7911 struct fw_event_work *fw_event = container_of(work,
7912 struct fw_event_work, work);
7913
7914 _mpt3sas_fw_work(fw_event->ioc, fw_event);
7915}
7916
7917/**
7918 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7919 * @ioc: per adapter object
7920 * @msix_index: MSIX table index supplied by the OS
7921 * @reply: reply message frame(lower 32bit addr)
7922 * Context: interrupt.
7923 *
7924 * This function merely adds a new work task into ioc->firmware_event_thread.
7925 * The tasks are worked from _firmware_event_work in user context.
7926 *
7927 * Return 1 meaning mf should be freed from _base_interrupt
7928 * 0 means the mf is freed from this function.
7929 */
7930u8
7931mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7932 u32 reply)
7933{
7934 struct fw_event_work *fw_event;
7935 Mpi2EventNotificationReply_t *mpi_reply;
7936 u16 event;
7937 u16 sz;
a470a51c 7938 Mpi26EventDataActiveCableExcept_t *ActiveCableEventData;
f92363d1
SR
7939
7940 /* events turned off due to host reset or driver unloading */
7941 if (ioc->remove_host || ioc->pci_error_recovery)
7942 return 1;
7943
7944 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7945
7946 if (unlikely(!mpi_reply)) {
7947 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7948 ioc->name, __FILE__, __LINE__, __func__);
7949 return 1;
7950 }
7951
7952 event = le16_to_cpu(mpi_reply->Event);
7953
7954 if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7955 mpt3sas_trigger_event(ioc, event, 0);
7956
7957 switch (event) {
7958 /* handle these */
7959 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7960 {
7961 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7962 (Mpi2EventDataSasBroadcastPrimitive_t *)
7963 mpi_reply->EventData;
7964
7965 if (baen_data->Primitive !=
7966 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7967 return 1;
7968
7969 if (ioc->broadcast_aen_busy) {
7970 ioc->broadcast_aen_pending++;
7971 return 1;
7972 } else
7973 ioc->broadcast_aen_busy = 1;
7974 break;
7975 }
7976
7977 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7978 _scsih_check_topo_delete_events(ioc,
7979 (Mpi2EventDataSasTopologyChangeList_t *)
7980 mpi_reply->EventData);
7981 break;
7982 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7983 _scsih_check_ir_config_unhide_events(ioc,
7984 (Mpi2EventDataIrConfigChangeList_t *)
7985 mpi_reply->EventData);
7986 break;
7987 case MPI2_EVENT_IR_VOLUME:
7988 _scsih_check_volume_delete_events(ioc,
7989 (Mpi2EventDataIrVolume_t *)
7990 mpi_reply->EventData);
7991 break;
7786ab6a
SR
7992 case MPI2_EVENT_LOG_ENTRY_ADDED:
7993 {
7994 Mpi2EventDataLogEntryAdded_t *log_entry;
7995 u32 *log_code;
7996
7997 if (!ioc->is_warpdrive)
7998 break;
7999
8000 log_entry = (Mpi2EventDataLogEntryAdded_t *)
8001 mpi_reply->EventData;
8002 log_code = (u32 *)log_entry->LogData;
f92363d1 8003
7786ab6a
SR
8004 if (le16_to_cpu(log_entry->LogEntryQualifier)
8005 != MPT2_WARPDRIVE_LOGENTRY)
8006 break;
8007
8008 switch (le32_to_cpu(*log_code)) {
8009 case MPT2_WARPDRIVE_LC_SSDT:
8010 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
8011 "IO Throttling has occurred in the WarpDrive "
8012 "subsystem. Check WarpDrive documentation for "
8013 "additional details.\n", ioc->name);
8014 break;
8015 case MPT2_WARPDRIVE_LC_SSDLW:
8016 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
8017 "Program/Erase Cycles for the WarpDrive subsystem "
8018 "in degraded range. Check WarpDrive documentation "
8019 "for additional details.\n", ioc->name);
8020 break;
8021 case MPT2_WARPDRIVE_LC_SSDLF:
8022 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
8023 "There are no Program/Erase Cycles for the "
8024 "WarpDrive subsystem. The storage device will be "
8025 "in read-only mode. Check WarpDrive documentation "
8026 "for additional details.\n", ioc->name);
8027 break;
8028 case MPT2_WARPDRIVE_LC_BRMF:
8029 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
8030 "The Backup Rail Monitor has failed on the "
8031 "WarpDrive subsystem. Check WarpDrive "
8032 "documentation for additional details.\n",
8033 ioc->name);
8034 break;
8035 }
8036
8037 break;
8038 }
f92363d1
SR
8039 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
8040 case MPI2_EVENT_IR_OPERATION_STATUS:
8041 case MPI2_EVENT_SAS_DISCOVERY:
8042 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
8043 case MPI2_EVENT_IR_PHYSICAL_DISK:
8044 break;
8045
2d8ce8c9
SR
8046 case MPI2_EVENT_TEMP_THRESHOLD:
8047 _scsih_temp_threshold_events(ioc,
8048 (Mpi2EventDataTemperature_t *)
8049 mpi_reply->EventData);
8050 break;
a470a51c
C
8051 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
8052 ActiveCableEventData =
8053 (Mpi26EventDataActiveCableExcept_t *) mpi_reply->EventData;
6c44c0fe
C
8054 switch (ActiveCableEventData->ReasonCode) {
8055 case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
b99b1993
SR
8056 pr_notice(MPT3SAS_FMT
8057 "Currently an active cable with ReceptacleID %d\n",
8058 ioc->name, ActiveCableEventData->ReceptacleID);
8059 pr_notice("cannot be powered and devices connected\n");
8060 pr_notice("to this active cable will not be seen\n");
8061 pr_notice("This active cable requires %d mW of power\n",
6c44c0fe 8062 ActiveCableEventData->ActiveCablePowerRequirement);
6c44c0fe
C
8063 break;
8064
8065 case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
b99b1993
SR
8066 pr_notice(MPT3SAS_FMT
8067 "Currently a cable with ReceptacleID %d\n",
8068 ioc->name, ActiveCableEventData->ReceptacleID);
8069 pr_notice(
8070 "is not running at optimal speed(12 Gb/s rate)\n");
6c44c0fe 8071 break;
7ebd67e0 8072 }
6c44c0fe 8073
a470a51c 8074 break;
2d8ce8c9 8075
f92363d1
SR
8076 default: /* ignore the rest */
8077 return 1;
8078 }
8079
f92363d1 8080 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
146b16c8 8081 fw_event = alloc_fw_event_work(sz);
35b62362 8082 if (!fw_event) {
f92363d1
SR
8083 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8084 ioc->name, __FILE__, __LINE__, __func__);
f92363d1
SR
8085 return 1;
8086 }
8087
8088 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
8089 fw_event->ioc = ioc;
8090 fw_event->VF_ID = mpi_reply->VF_ID;
8091 fw_event->VP_ID = mpi_reply->VP_ID;
8092 fw_event->event = event;
8093 _scsih_fw_event_add(ioc, fw_event);
146b16c8 8094 fw_event_work_put(fw_event);
f92363d1
SR
8095 return 1;
8096}
8097
f92363d1
SR
8098/**
8099 * _scsih_expander_node_remove - removing expander device from list.
8100 * @ioc: per adapter object
8101 * @sas_expander: the sas_device object
f92363d1
SR
8102 *
8103 * Removing object and freeing associated memory from the
8104 * ioc->sas_expander_list.
8105 *
8106 * Return nothing.
8107 */
8108static void
8109_scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
8110 struct _sas_node *sas_expander)
8111{
8112 struct _sas_port *mpt3sas_port, *next;
bbe3def3 8113 unsigned long flags;
f92363d1
SR
8114
8115 /* remove sibling ports attached to this expander */
8116 list_for_each_entry_safe(mpt3sas_port, next,
8117 &sas_expander->sas_port_list, port_list) {
8118 if (ioc->shost_recovery)
8119 return;
8120 if (mpt3sas_port->remote_identify.device_type ==
8121 SAS_END_DEVICE)
8122 mpt3sas_device_remove_by_sas_address(ioc,
8123 mpt3sas_port->remote_identify.sas_address);
8124 else if (mpt3sas_port->remote_identify.device_type ==
8125 SAS_EDGE_EXPANDER_DEVICE ||
8126 mpt3sas_port->remote_identify.device_type ==
8127 SAS_FANOUT_EXPANDER_DEVICE)
8128 mpt3sas_expander_remove(ioc,
8129 mpt3sas_port->remote_identify.sas_address);
8130 }
8131
8132 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
8133 sas_expander->sas_address_parent);
8134
8135 pr_info(MPT3SAS_FMT
8136 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
8137 ioc->name,
8138 sas_expander->handle, (unsigned long long)
8139 sas_expander->sas_address);
8140
bbe3def3
SR
8141 spin_lock_irqsave(&ioc->sas_node_lock, flags);
8142 list_del(&sas_expander->list);
8143 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
8144
f92363d1
SR
8145 kfree(sas_expander->phy);
8146 kfree(sas_expander);
8147}
8148
8149/**
8150 * _scsih_ir_shutdown - IR shutdown notification
8151 * @ioc: per adapter object
8152 *
8153 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
8154 * the host system is shutting down.
8155 *
8156 * Return nothing.
8157 */
8158static void
8159_scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
8160{
8161 Mpi2RaidActionRequest_t *mpi_request;
8162 Mpi2RaidActionReply_t *mpi_reply;
8163 u16 smid;
8164
8165 /* is IR firmware build loaded ? */
8166 if (!ioc->ir_firmware)
8167 return;
8168
8169 /* are there any volumes ? */
8170 if (list_empty(&ioc->raid_device_list))
8171 return;
8172
8173 mutex_lock(&ioc->scsih_cmds.mutex);
8174
8175 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
8176 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
8177 ioc->name, __func__);
8178 goto out;
8179 }
8180 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
8181
8182 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
8183 if (!smid) {
8184 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
8185 ioc->name, __func__);
8186 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8187 goto out;
8188 }
8189
8190 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
8191 ioc->scsih_cmds.smid = smid;
8192 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
8193
8194 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
8195 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
8196
7786ab6a
SR
8197 if (!ioc->hide_ir_msg)
8198 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
f92363d1 8199 init_completion(&ioc->scsih_cmds.done);
81c16f83 8200 ioc->put_smid_default(ioc, smid);
f92363d1
SR
8201 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
8202
8203 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
8204 pr_err(MPT3SAS_FMT "%s: timeout\n",
8205 ioc->name, __func__);
8206 goto out;
8207 }
8208
8209 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
8210 mpi_reply = ioc->scsih_cmds.reply;
7786ab6a
SR
8211 if (!ioc->hide_ir_msg)
8212 pr_info(MPT3SAS_FMT "IR shutdown "
8213 "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
8214 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
8215 le32_to_cpu(mpi_reply->IOCLogInfo));
f92363d1
SR
8216 }
8217
8218 out:
8219 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8220 mutex_unlock(&ioc->scsih_cmds.mutex);
8221}
8222
8223/**
8a7e4c24 8224 * scsih_remove - detach and remove add host
f92363d1
SR
8225 * @pdev: PCI device struct
8226 *
8227 * Routine called when unloading the driver.
8228 * Return nothing.
8229 */
8bbb1cf6 8230static void scsih_remove(struct pci_dev *pdev)
f92363d1
SR
8231{
8232 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8233 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8234 struct _sas_port *mpt3sas_port, *next_port;
8235 struct _raid_device *raid_device, *next;
8236 struct MPT3SAS_TARGET *sas_target_priv_data;
8237 struct workqueue_struct *wq;
8238 unsigned long flags;
8239
8240 ioc->remove_host = 1;
8241 _scsih_fw_event_cleanup_queue(ioc);
8242
8243 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8244 wq = ioc->firmware_event_thread;
8245 ioc->firmware_event_thread = NULL;
8246 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8247 if (wq)
8248 destroy_workqueue(wq);
8249
8250 /* release all the volumes */
8251 _scsih_ir_shutdown(ioc);
8252 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
8253 list) {
8254 if (raid_device->starget) {
8255 sas_target_priv_data =
8256 raid_device->starget->hostdata;
8257 sas_target_priv_data->deleted = 1;
8258 scsi_remove_target(&raid_device->starget->dev);
8259 }
8260 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
8261 ioc->name, raid_device->handle,
8262 (unsigned long long) raid_device->wwid);
8263 _scsih_raid_device_remove(ioc, raid_device);
8264 }
8265
8266 /* free ports attached to the sas_host */
8267 list_for_each_entry_safe(mpt3sas_port, next_port,
8268 &ioc->sas_hba.sas_port_list, port_list) {
8269 if (mpt3sas_port->remote_identify.device_type ==
8270 SAS_END_DEVICE)
8271 mpt3sas_device_remove_by_sas_address(ioc,
8272 mpt3sas_port->remote_identify.sas_address);
8273 else if (mpt3sas_port->remote_identify.device_type ==
8274 SAS_EDGE_EXPANDER_DEVICE ||
8275 mpt3sas_port->remote_identify.device_type ==
8276 SAS_FANOUT_EXPANDER_DEVICE)
8277 mpt3sas_expander_remove(ioc,
8278 mpt3sas_port->remote_identify.sas_address);
8279 }
8280
8281 /* free phys attached to the sas_host */
8282 if (ioc->sas_hba.num_phys) {
8283 kfree(ioc->sas_hba.phy);
8284 ioc->sas_hba.phy = NULL;
8285 ioc->sas_hba.num_phys = 0;
8286 }
8287
8288 sas_remove_host(shost);
8289 mpt3sas_base_detach(ioc);
08c4d550 8290 spin_lock(&gioc_lock);
f92363d1 8291 list_del(&ioc->list);
08c4d550 8292 spin_unlock(&gioc_lock);
f92363d1
SR
8293 scsi_host_put(shost);
8294}
8295
8296/**
8a7e4c24 8297 * scsih_shutdown - routine call during system shutdown
f92363d1
SR
8298 * @pdev: PCI device struct
8299 *
8300 * Return nothing.
8301 */
8bbb1cf6 8302static void
8a7e4c24 8303scsih_shutdown(struct pci_dev *pdev)
f92363d1
SR
8304{
8305 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8306 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8307 struct workqueue_struct *wq;
8308 unsigned long flags;
8309
8310 ioc->remove_host = 1;
8311 _scsih_fw_event_cleanup_queue(ioc);
8312
8313 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8314 wq = ioc->firmware_event_thread;
8315 ioc->firmware_event_thread = NULL;
8316 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8317 if (wq)
8318 destroy_workqueue(wq);
8319
8320 _scsih_ir_shutdown(ioc);
8321 mpt3sas_base_detach(ioc);
8322}
8323
8324
8325/**
8326 * _scsih_probe_boot_devices - reports 1st device
8327 * @ioc: per adapter object
8328 *
8329 * If specified in bios page 2, this routine reports the 1st
8330 * device scsi-ml or sas transport for persistent boot device
8331 * purposes. Please refer to function _scsih_determine_boot_device()
8332 */
8333static void
8334_scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8335{
8336 u8 is_raid;
8337 void *device;
8338 struct _sas_device *sas_device;
8339 struct _raid_device *raid_device;
8340 u16 handle;
8341 u64 sas_address_parent;
8342 u64 sas_address;
8343 unsigned long flags;
8344 int rc;
8345
8346 /* no Bios, return immediately */
8347 if (!ioc->bios_pg3.BiosVersion)
8348 return;
8349
8350 device = NULL;
8351 is_raid = 0;
8352 if (ioc->req_boot_device.device) {
8353 device = ioc->req_boot_device.device;
8354 is_raid = ioc->req_boot_device.is_raid;
8355 } else if (ioc->req_alt_boot_device.device) {
8356 device = ioc->req_alt_boot_device.device;
8357 is_raid = ioc->req_alt_boot_device.is_raid;
8358 } else if (ioc->current_boot_device.device) {
8359 device = ioc->current_boot_device.device;
8360 is_raid = ioc->current_boot_device.is_raid;
8361 }
8362
8363 if (!device)
8364 return;
8365
8366 if (is_raid) {
8367 raid_device = device;
8368 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8369 raid_device->id, 0);
8370 if (rc)
8371 _scsih_raid_device_remove(ioc, raid_device);
8372 } else {
8373 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8374 sas_device = device;
8375 handle = sas_device->handle;
8376 sas_address_parent = sas_device->sas_address_parent;
8377 sas_address = sas_device->sas_address;
8378 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8379 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8380
7786ab6a
SR
8381 if (ioc->hide_drives)
8382 return;
f92363d1
SR
8383 if (!mpt3sas_transport_port_add(ioc, handle,
8384 sas_address_parent)) {
8385 _scsih_sas_device_remove(ioc, sas_device);
8386 } else if (!sas_device->starget) {
f5edbe77
SR
8387 if (!ioc->is_driver_loading) {
8388 mpt3sas_transport_port_remove(ioc,
8389 sas_address,
f92363d1 8390 sas_address_parent);
f5edbe77
SR
8391 _scsih_sas_device_remove(ioc, sas_device);
8392 }
f92363d1
SR
8393 }
8394 }
8395}
8396
8397/**
8398 * _scsih_probe_raid - reporting raid volumes to scsi-ml
8399 * @ioc: per adapter object
8400 *
8401 * Called during initial loading of the driver.
8402 */
8403static void
8404_scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8405{
8406 struct _raid_device *raid_device, *raid_next;
8407 int rc;
8408
8409 list_for_each_entry_safe(raid_device, raid_next,
8410 &ioc->raid_device_list, list) {
8411 if (raid_device->starget)
8412 continue;
8413 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8414 raid_device->id, 0);
8415 if (rc)
8416 _scsih_raid_device_remove(ioc, raid_device);
8417 }
8418}
8419
d1cb5e49
SR
8420static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8421{
8422 struct _sas_device *sas_device = NULL;
8423 unsigned long flags;
8424
8425 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8426 if (!list_empty(&ioc->sas_device_init_list)) {
8427 sas_device = list_first_entry(&ioc->sas_device_init_list,
8428 struct _sas_device, list);
8429 sas_device_get(sas_device);
8430 }
8431 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8432
8433 return sas_device;
8434}
8435
8436static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8437 struct _sas_device *sas_device)
8438{
8439 unsigned long flags;
8440
8441 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8442
8443 /*
8444 * Since we dropped the lock during the call to port_add(), we need to
8445 * be careful here that somebody else didn't move or delete this item
8446 * while we were busy with other things.
8447 *
8448 * If it was on the list, we need a put() for the reference the list
8449 * had. Either way, we need a get() for the destination list.
8450 */
8451 if (!list_empty(&sas_device->list)) {
8452 list_del_init(&sas_device->list);
8453 sas_device_put(sas_device);
8454 }
8455
8456 sas_device_get(sas_device);
8457 list_add_tail(&sas_device->list, &ioc->sas_device_list);
8458
8459 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8460}
8461
f92363d1
SR
8462/**
8463 * _scsih_probe_sas - reporting sas devices to sas transport
8464 * @ioc: per adapter object
8465 *
8466 * Called during initial loading of the driver.
8467 */
8468static void
8469_scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8470{
d1cb5e49 8471 struct _sas_device *sas_device;
f92363d1 8472
d1cb5e49
SR
8473 if (ioc->hide_drives)
8474 return;
7786ab6a 8475
d1cb5e49 8476 while ((sas_device = get_next_sas_device(ioc))) {
f92363d1
SR
8477 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8478 sas_device->sas_address_parent)) {
d1cb5e49
SR
8479 _scsih_sas_device_remove(ioc, sas_device);
8480 sas_device_put(sas_device);
f92363d1
SR
8481 continue;
8482 } else if (!sas_device->starget) {
8483 /*
8484 * When asyn scanning is enabled, its not possible to
8485 * remove devices while scanning is turned on due to an
8486 * oops in scsi_sysfs_add_sdev()->add_device()->
8487 * sysfs_addrm_start()
8488 */
f5edbe77 8489 if (!ioc->is_driver_loading) {
f92363d1
SR
8490 mpt3sas_transport_port_remove(ioc,
8491 sas_device->sas_address,
8492 sas_device->sas_address_parent);
d1cb5e49
SR
8493 _scsih_sas_device_remove(ioc, sas_device);
8494 sas_device_put(sas_device);
f5edbe77
SR
8495 continue;
8496 }
f92363d1 8497 }
d1cb5e49
SR
8498 sas_device_make_active(ioc, sas_device);
8499 sas_device_put(sas_device);
f92363d1
SR
8500 }
8501}
8502
8503/**
8504 * _scsih_probe_devices - probing for devices
8505 * @ioc: per adapter object
8506 *
8507 * Called during initial loading of the driver.
8508 */
8509static void
8510_scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8511{
8512 u16 volume_mapping_flags;
8513
8514 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8515 return; /* return when IOC doesn't support initiator mode */
8516
8517 _scsih_probe_boot_devices(ioc);
8518
8519 if (ioc->ir_firmware) {
8520 volume_mapping_flags =
8521 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8522 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8523 if (volume_mapping_flags ==
8524 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8525 _scsih_probe_raid(ioc);
8526 _scsih_probe_sas(ioc);
8527 } else {
8528 _scsih_probe_sas(ioc);
8529 _scsih_probe_raid(ioc);
8530 }
8531 } else
8532 _scsih_probe_sas(ioc);
8533}
8534
8535/**
8a7e4c24 8536 * scsih_scan_start - scsi lld callback for .scan_start
f92363d1
SR
8537 * @shost: SCSI host pointer
8538 *
8539 * The shost has the ability to discover targets on its own instead
8540 * of scanning the entire bus. In our implemention, we will kick off
8541 * firmware discovery.
8542 */
8bbb1cf6 8543static void
8a7e4c24 8544scsih_scan_start(struct Scsi_Host *shost)
f92363d1
SR
8545{
8546 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8547 int rc;
8548 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8549 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8550
8551 if (disable_discovery > 0)
8552 return;
8553
8554 ioc->start_scan = 1;
8555 rc = mpt3sas_port_enable(ioc);
8556
8557 if (rc != 0)
8558 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8559}
8560
8561/**
8a7e4c24 8562 * scsih_scan_finished - scsi lld callback for .scan_finished
f92363d1
SR
8563 * @shost: SCSI host pointer
8564 * @time: elapsed time of the scan in jiffies
8565 *
8566 * This function will be called periodicallyn until it returns 1 with the
8567 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8568 * we wait for firmware discovery to complete, then return 1.
8569 */
8bbb1cf6 8570static int
8a7e4c24 8571scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
f92363d1
SR
8572{
8573 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8574
8575 if (disable_discovery > 0) {
8576 ioc->is_driver_loading = 0;
8577 ioc->wait_for_discovery_to_complete = 0;
8578 return 1;
8579 }
8580
8581 if (time >= (300 * HZ)) {
8582 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8583 pr_info(MPT3SAS_FMT
8584 "port enable: FAILED with timeout (timeout=300s)\n",
8585 ioc->name);
8586 ioc->is_driver_loading = 0;
8587 return 1;
8588 }
8589
8590 if (ioc->start_scan)
8591 return 0;
8592
8593 if (ioc->start_scan_failed) {
8594 pr_info(MPT3SAS_FMT
8595 "port enable: FAILED with (ioc_status=0x%08x)\n",
8596 ioc->name, ioc->start_scan_failed);
8597 ioc->is_driver_loading = 0;
8598 ioc->wait_for_discovery_to_complete = 0;
8599 ioc->remove_host = 1;
8600 return 1;
8601 }
8602
8603 pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8604 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8605
8606 if (ioc->wait_for_discovery_to_complete) {
8607 ioc->wait_for_discovery_to_complete = 0;
8608 _scsih_probe_devices(ioc);
8609 }
8610 mpt3sas_base_start_watchdog(ioc);
8611 ioc->is_driver_loading = 0;
8612 return 1;
8613}
8614
c84b06a4
SR
8615/* shost template for SAS 2.0 HBA devices */
8616static struct scsi_host_template mpt2sas_driver_template = {
8617 .module = THIS_MODULE,
8618 .name = "Fusion MPT SAS Host",
8619 .proc_name = MPT2SAS_DRIVER_NAME,
8620 .queuecommand = scsih_qcmd,
8621 .target_alloc = scsih_target_alloc,
8622 .slave_alloc = scsih_slave_alloc,
8623 .slave_configure = scsih_slave_configure,
8624 .target_destroy = scsih_target_destroy,
8625 .slave_destroy = scsih_slave_destroy,
8626 .scan_finished = scsih_scan_finished,
8627 .scan_start = scsih_scan_start,
8628 .change_queue_depth = scsih_change_queue_depth,
8629 .eh_abort_handler = scsih_abort,
8630 .eh_device_reset_handler = scsih_dev_reset,
8631 .eh_target_reset_handler = scsih_target_reset,
8632 .eh_host_reset_handler = scsih_host_reset,
8633 .bios_param = scsih_bios_param,
8634 .can_queue = 1,
8635 .this_id = -1,
8636 .sg_tablesize = MPT2SAS_SG_DEPTH,
8637 .max_sectors = 32767,
8638 .cmd_per_lun = 7,
8639 .use_clustering = ENABLE_CLUSTERING,
8640 .shost_attrs = mpt3sas_host_attrs,
8641 .sdev_attrs = mpt3sas_dev_attrs,
8642 .track_queue_depth = 1,
8643};
8644
8645/* raid transport support for SAS 2.0 HBA devices */
8646static struct raid_function_template mpt2sas_raid_functions = {
8647 .cookie = &mpt2sas_driver_template,
8648 .is_raid = scsih_is_raid,
8649 .get_resync = scsih_get_resync,
8650 .get_state = scsih_get_state,
8651};
d357e84d 8652
c84b06a4
SR
8653/* shost template for SAS 3.0 HBA devices */
8654static struct scsi_host_template mpt3sas_driver_template = {
8655 .module = THIS_MODULE,
8656 .name = "Fusion MPT SAS Host",
8657 .proc_name = MPT3SAS_DRIVER_NAME,
8658 .queuecommand = scsih_qcmd,
8659 .target_alloc = scsih_target_alloc,
8660 .slave_alloc = scsih_slave_alloc,
8661 .slave_configure = scsih_slave_configure,
8662 .target_destroy = scsih_target_destroy,
8663 .slave_destroy = scsih_slave_destroy,
8664 .scan_finished = scsih_scan_finished,
8665 .scan_start = scsih_scan_start,
8666 .change_queue_depth = scsih_change_queue_depth,
8667 .eh_abort_handler = scsih_abort,
8668 .eh_device_reset_handler = scsih_dev_reset,
8669 .eh_target_reset_handler = scsih_target_reset,
8670 .eh_host_reset_handler = scsih_host_reset,
8671 .bios_param = scsih_bios_param,
8672 .can_queue = 1,
8673 .this_id = -1,
8674 .sg_tablesize = MPT3SAS_SG_DEPTH,
8675 .max_sectors = 32767,
8676 .cmd_per_lun = 7,
8677 .use_clustering = ENABLE_CLUSTERING,
8678 .shost_attrs = mpt3sas_host_attrs,
8679 .sdev_attrs = mpt3sas_dev_attrs,
8680 .track_queue_depth = 1,
8681};
8682
8683/* raid transport support for SAS 3.0 HBA devices */
8684static struct raid_function_template mpt3sas_raid_functions = {
8685 .cookie = &mpt3sas_driver_template,
8686 .is_raid = scsih_is_raid,
8687 .get_resync = scsih_get_resync,
8688 .get_state = scsih_get_state,
8689};
8690
8691/**
8692 * _scsih_determine_hba_mpi_version - determine in which MPI version class
8693 * this device belongs to.
8694 * @pdev: PCI device struct
8695 *
8696 * return MPI2_VERSION for SAS 2.0 HBA devices,
b130b0d5
SS
8697 * MPI25_VERSION for SAS 3.0 HBA devices, and
8698 * MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
c84b06a4 8699 */
8bbb1cf6 8700static u16
c84b06a4
SR
8701_scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8702{
8703
8704 switch (pdev->device) {
7786ab6a 8705 case MPI2_MFGPAGE_DEVID_SSS6200:
d357e84d
SR
8706 case MPI2_MFGPAGE_DEVID_SAS2004:
8707 case MPI2_MFGPAGE_DEVID_SAS2008:
8708 case MPI2_MFGPAGE_DEVID_SAS2108_1:
8709 case MPI2_MFGPAGE_DEVID_SAS2108_2:
8710 case MPI2_MFGPAGE_DEVID_SAS2108_3:
8711 case MPI2_MFGPAGE_DEVID_SAS2116_1:
8712 case MPI2_MFGPAGE_DEVID_SAS2116_2:
8713 case MPI2_MFGPAGE_DEVID_SAS2208_1:
8714 case MPI2_MFGPAGE_DEVID_SAS2208_2:
8715 case MPI2_MFGPAGE_DEVID_SAS2208_3:
8716 case MPI2_MFGPAGE_DEVID_SAS2208_4:
8717 case MPI2_MFGPAGE_DEVID_SAS2208_5:
8718 case MPI2_MFGPAGE_DEVID_SAS2208_6:
8719 case MPI2_MFGPAGE_DEVID_SAS2308_1:
8720 case MPI2_MFGPAGE_DEVID_SAS2308_2:
8721 case MPI2_MFGPAGE_DEVID_SAS2308_3:
c84b06a4 8722 return MPI2_VERSION;
d357e84d
SR
8723 case MPI25_MFGPAGE_DEVID_SAS3004:
8724 case MPI25_MFGPAGE_DEVID_SAS3008:
8725 case MPI25_MFGPAGE_DEVID_SAS3108_1:
8726 case MPI25_MFGPAGE_DEVID_SAS3108_2:
8727 case MPI25_MFGPAGE_DEVID_SAS3108_5:
8728 case MPI25_MFGPAGE_DEVID_SAS3108_6:
c84b06a4 8729 return MPI25_VERSION;
b130b0d5
SS
8730 case MPI26_MFGPAGE_DEVID_SAS3216:
8731 case MPI26_MFGPAGE_DEVID_SAS3224:
8732 case MPI26_MFGPAGE_DEVID_SAS3316_1:
8733 case MPI26_MFGPAGE_DEVID_SAS3316_2:
8734 case MPI26_MFGPAGE_DEVID_SAS3316_3:
8735 case MPI26_MFGPAGE_DEVID_SAS3316_4:
8736 case MPI26_MFGPAGE_DEVID_SAS3324_1:
8737 case MPI26_MFGPAGE_DEVID_SAS3324_2:
8738 case MPI26_MFGPAGE_DEVID_SAS3324_3:
8739 case MPI26_MFGPAGE_DEVID_SAS3324_4:
998f26ae
SPS
8740 case MPI26_MFGPAGE_DEVID_SAS3508:
8741 case MPI26_MFGPAGE_DEVID_SAS3508_1:
8742 case MPI26_MFGPAGE_DEVID_SAS3408:
8743 case MPI26_MFGPAGE_DEVID_SAS3516:
8744 case MPI26_MFGPAGE_DEVID_SAS3516_1:
8745 case MPI26_MFGPAGE_DEVID_SAS3416:
b130b0d5 8746 return MPI26_VERSION;
d357e84d 8747 }
c84b06a4 8748 return 0;
d357e84d
SR
8749}
8750
f92363d1 8751/**
c84b06a4 8752 * _scsih_probe - attach and add scsi host
f92363d1
SR
8753 * @pdev: PCI device struct
8754 * @id: pci device id
8755 *
8756 * Returns 0 success, anything else error.
8757 */
8bbb1cf6 8758static int
c84b06a4 8759_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
f92363d1
SR
8760{
8761 struct MPT3SAS_ADAPTER *ioc;
c84b06a4 8762 struct Scsi_Host *shost = NULL;
b65f1d4d 8763 int rv;
c84b06a4
SR
8764 u16 hba_mpi_version;
8765
8766 /* Determine in which MPI version class this pci device belongs */
8767 hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8768 if (hba_mpi_version == 0)
8769 return -ENODEV;
8770
8771 /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8772 * for other generation HBA's return with -ENODEV
8773 */
8774 if ((hbas_to_enumerate == 1) && (hba_mpi_version != MPI2_VERSION))
8775 return -ENODEV;
8776
8777 /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8778 * for other generation HBA's return with -ENODEV
8779 */
b130b0d5
SS
8780 if ((hbas_to_enumerate == 2) && (!(hba_mpi_version == MPI25_VERSION
8781 || hba_mpi_version == MPI26_VERSION)))
c84b06a4
SR
8782 return -ENODEV;
8783
8784 switch (hba_mpi_version) {
8785 case MPI2_VERSION:
ffdadd68 8786 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
8787 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
c84b06a4
SR
8788 /* Use mpt2sas driver host template for SAS 2.0 HBA's */
8789 shost = scsi_host_alloc(&mpt2sas_driver_template,
8790 sizeof(struct MPT3SAS_ADAPTER));
8791 if (!shost)
8792 return -ENODEV;
8793 ioc = shost_priv(shost);
8794 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8795 ioc->hba_mpi_version_belonged = hba_mpi_version;
8796 ioc->id = mpt2_ids++;
8797 sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8798 if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8799 ioc->is_warpdrive = 1;
8800 ioc->hide_ir_msg = 1;
8801 } else
8802 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8803 break;
8804 case MPI25_VERSION:
b130b0d5 8805 case MPI26_VERSION:
c84b06a4
SR
8806 /* Use mpt3sas driver host template for SAS 3.0 HBA's */
8807 shost = scsi_host_alloc(&mpt3sas_driver_template,
8808 sizeof(struct MPT3SAS_ADAPTER));
8809 if (!shost)
8810 return -ENODEV;
8811 ioc = shost_priv(shost);
8812 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8813 ioc->hba_mpi_version_belonged = hba_mpi_version;
8814 ioc->id = mpt3_ids++;
8815 sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
998f26ae
SPS
8816 switch (pdev->device) {
8817 case MPI26_MFGPAGE_DEVID_SAS3508:
8818 case MPI26_MFGPAGE_DEVID_SAS3508_1:
8819 case MPI26_MFGPAGE_DEVID_SAS3408:
8820 case MPI26_MFGPAGE_DEVID_SAS3516:
8821 case MPI26_MFGPAGE_DEVID_SAS3516_1:
8822 case MPI26_MFGPAGE_DEVID_SAS3416:
8823 ioc->is_gen35_ioc = 1;
8824 break;
8825 default:
8826 ioc->is_gen35_ioc = 0;
8827 }
b130b0d5
SS
8828 if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
8829 pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
0bb337c9
SPS
8830 (ioc->hba_mpi_version_belonged == MPI26_VERSION)) {
8831 ioc->combined_reply_queue = 1;
8832 if (ioc->is_gen35_ioc)
8833 ioc->combined_reply_index_count =
8834 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G35;
8835 else
8836 ioc->combined_reply_index_count =
8837 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G3;
8838 }
c84b06a4
SR
8839 break;
8840 default:
8841 return -ENODEV;
8842 }
f92363d1 8843
f92363d1 8844 INIT_LIST_HEAD(&ioc->list);
08c4d550 8845 spin_lock(&gioc_lock);
f92363d1 8846 list_add_tail(&ioc->list, &mpt3sas_ioc_list);
08c4d550 8847 spin_unlock(&gioc_lock);
f92363d1 8848 ioc->shost = shost;
f92363d1
SR
8849 ioc->pdev = pdev;
8850 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8851 ioc->tm_cb_idx = tm_cb_idx;
8852 ioc->ctl_cb_idx = ctl_cb_idx;
8853 ioc->base_cb_idx = base_cb_idx;
8854 ioc->port_enable_cb_idx = port_enable_cb_idx;
8855 ioc->transport_cb_idx = transport_cb_idx;
8856 ioc->scsih_cb_idx = scsih_cb_idx;
8857 ioc->config_cb_idx = config_cb_idx;
8858 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8859 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8860 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8861 ioc->logging_level = logging_level;
8862 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8863 /* misc semaphores and spin locks */
8864 mutex_init(&ioc->reset_in_progress_mutex);
08c4d550
SR
8865 /* initializing pci_access_mutex lock */
8866 mutex_init(&ioc->pci_access_mutex);
f92363d1
SR
8867 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8868 spin_lock_init(&ioc->scsi_lookup_lock);
8869 spin_lock_init(&ioc->sas_device_lock);
8870 spin_lock_init(&ioc->sas_node_lock);
8871 spin_lock_init(&ioc->fw_event_lock);
8872 spin_lock_init(&ioc->raid_device_lock);
8873 spin_lock_init(&ioc->diag_trigger_lock);
8874
8875 INIT_LIST_HEAD(&ioc->sas_device_list);
8876 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8877 INIT_LIST_HEAD(&ioc->sas_expander_list);
8878 INIT_LIST_HEAD(&ioc->fw_event_list);
8879 INIT_LIST_HEAD(&ioc->raid_device_list);
8880 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8881 INIT_LIST_HEAD(&ioc->delayed_tr_list);
fd0331b3
SS
8882 INIT_LIST_HEAD(&ioc->delayed_sc_list);
8883 INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
f92363d1 8884 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
cf9bd21a 8885 INIT_LIST_HEAD(&ioc->reply_queue_list);
f92363d1 8886
c84b06a4 8887 sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
d357e84d 8888
f92363d1
SR
8889 /* init shost parameters */
8890 shost->max_cmd_len = 32;
8891 shost->max_lun = max_lun;
8892 shost->transportt = mpt3sas_transport_template;
8893 shost->unique_id = ioc->id;
8894
8895 if (max_sectors != 0xFFFF) {
8896 if (max_sectors < 64) {
8897 shost->max_sectors = 64;
8898 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8899 "for max_sectors, range is 64 to 32767. Assigning "
8900 "value of 64.\n", ioc->name, max_sectors);
8901 } else if (max_sectors > 32767) {
8902 shost->max_sectors = 32767;
8903 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8904 "for max_sectors, range is 64 to 32767. Assigning "
8905 "default value of 32767.\n", ioc->name,
8906 max_sectors);
8907 } else {
8908 shost->max_sectors = max_sectors & 0xFFFE;
8909 pr_info(MPT3SAS_FMT
8910 "The max_sectors value is set to %d\n",
8911 ioc->name, shost->max_sectors);
8912 }
8913 }
8914
f92363d1
SR
8915 /* register EEDP capabilities with SCSI layer */
8916 if (prot_mask > 0)
8917 scsi_host_set_prot(shost, prot_mask);
8918 else
8919 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8920 | SHOST_DIF_TYPE2_PROTECTION
8921 | SHOST_DIF_TYPE3_PROTECTION);
8922
8923 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8924
8925 /* event thread */
8926 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
c84b06a4 8927 "fw_event_%s%d", ioc->driver_name, ioc->id);
bdff785e
SR
8928 ioc->firmware_event_thread = alloc_ordered_workqueue(
8929 ioc->firmware_event_name, WQ_MEM_RECLAIM);
f92363d1
SR
8930 if (!ioc->firmware_event_thread) {
8931 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8932 ioc->name, __FILE__, __LINE__, __func__);
b65f1d4d 8933 rv = -ENODEV;
f92363d1
SR
8934 goto out_thread_fail;
8935 }
8936
8937 ioc->is_driver_loading = 1;
8938 if ((mpt3sas_base_attach(ioc))) {
8939 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8940 ioc->name, __FILE__, __LINE__, __func__);
b65f1d4d 8941 rv = -ENODEV;
f92363d1
SR
8942 goto out_attach_fail;
8943 }
7786ab6a 8944
7786ab6a
SR
8945 if (ioc->is_warpdrive) {
8946 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8947 ioc->hide_drives = 0;
8948 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8949 ioc->hide_drives = 1;
8950 else {
c84b06a4 8951 if (mpt3sas_get_num_volumes(ioc))
7786ab6a
SR
8952 ioc->hide_drives = 1;
8953 else
8954 ioc->hide_drives = 0;
8955 }
8956 } else
8957 ioc->hide_drives = 0;
7786ab6a 8958
b65f1d4d
SR
8959 rv = scsi_add_host(shost, &pdev->dev);
8960 if (rv) {
4dc06fd8
RS
8961 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8962 ioc->name, __FILE__, __LINE__, __func__);
4dc06fd8
RS
8963 goto out_add_shost_fail;
8964 }
8965
f92363d1
SR
8966 scsi_scan_host(shost);
8967 return 0;
4dc06fd8
RS
8968out_add_shost_fail:
8969 mpt3sas_base_detach(ioc);
f92363d1
SR
8970 out_attach_fail:
8971 destroy_workqueue(ioc->firmware_event_thread);
8972 out_thread_fail:
08c4d550 8973 spin_lock(&gioc_lock);
f92363d1 8974 list_del(&ioc->list);
08c4d550 8975 spin_unlock(&gioc_lock);
f92363d1 8976 scsi_host_put(shost);
b65f1d4d 8977 return rv;
f92363d1
SR
8978}
8979
8980#ifdef CONFIG_PM
8981/**
8a7e4c24 8982 * scsih_suspend - power management suspend main entry point
f92363d1
SR
8983 * @pdev: PCI device struct
8984 * @state: PM state change to (usually PCI_D3)
8985 *
8986 * Returns 0 success, anything else error.
8987 */
8bbb1cf6 8988static int
8a7e4c24 8989scsih_suspend(struct pci_dev *pdev, pm_message_t state)
f92363d1
SR
8990{
8991 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8992 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8993 pci_power_t device_state;
8994
8995 mpt3sas_base_stop_watchdog(ioc);
8996 flush_scheduled_work();
8997 scsi_block_requests(shost);
8998 device_state = pci_choose_state(pdev, state);
8999 pr_info(MPT3SAS_FMT
9000 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
9001 ioc->name, pdev, pci_name(pdev), device_state);
9002
9003 pci_save_state(pdev);
9004 mpt3sas_base_free_resources(ioc);
9005 pci_set_power_state(pdev, device_state);
9006 return 0;
9007}
9008
9009/**
8a7e4c24 9010 * scsih_resume - power management resume main entry point
f92363d1
SR
9011 * @pdev: PCI device struct
9012 *
9013 * Returns 0 success, anything else error.
9014 */
8bbb1cf6 9015static int
8a7e4c24 9016scsih_resume(struct pci_dev *pdev)
f92363d1
SR
9017{
9018 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9019 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9020 pci_power_t device_state = pdev->current_state;
9021 int r;
9022
9023 pr_info(MPT3SAS_FMT
9024 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
9025 ioc->name, pdev, pci_name(pdev), device_state);
9026
9027 pci_set_power_state(pdev, PCI_D0);
9028 pci_enable_wake(pdev, PCI_D0, 0);
9029 pci_restore_state(pdev);
9030 ioc->pdev = pdev;
9031 r = mpt3sas_base_map_resources(ioc);
9032 if (r)
9033 return r;
9034
98c56ad3 9035 mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET);
f92363d1
SR
9036 scsi_unblock_requests(shost);
9037 mpt3sas_base_start_watchdog(ioc);
9038 return 0;
9039}
9040#endif /* CONFIG_PM */
9041
9042/**
8a7e4c24 9043 * scsih_pci_error_detected - Called when a PCI error is detected.
f92363d1
SR
9044 * @pdev: PCI device struct
9045 * @state: PCI channel state
9046 *
9047 * Description: Called when a PCI error is detected.
9048 *
9049 * Return value:
9050 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
9051 */
8bbb1cf6 9052static pci_ers_result_t
8a7e4c24 9053scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
f92363d1
SR
9054{
9055 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9056 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9057
9058 pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
9059 ioc->name, state);
9060
9061 switch (state) {
9062 case pci_channel_io_normal:
9063 return PCI_ERS_RESULT_CAN_RECOVER;
9064 case pci_channel_io_frozen:
9065 /* Fatal error, prepare for slot reset */
9066 ioc->pci_error_recovery = 1;
9067 scsi_block_requests(ioc->shost);
9068 mpt3sas_base_stop_watchdog(ioc);
9069 mpt3sas_base_free_resources(ioc);
9070 return PCI_ERS_RESULT_NEED_RESET;
9071 case pci_channel_io_perm_failure:
9072 /* Permanent error, prepare for device removal */
9073 ioc->pci_error_recovery = 1;
9074 mpt3sas_base_stop_watchdog(ioc);
9075 _scsih_flush_running_cmds(ioc);
9076 return PCI_ERS_RESULT_DISCONNECT;
9077 }
9078 return PCI_ERS_RESULT_NEED_RESET;
9079}
9080
9081/**
8a7e4c24 9082 * scsih_pci_slot_reset - Called when PCI slot has been reset.
f92363d1
SR
9083 * @pdev: PCI device struct
9084 *
9085 * Description: This routine is called by the pci error recovery
9086 * code after the PCI slot has been reset, just before we
9087 * should resume normal operations.
9088 */
8bbb1cf6 9089static pci_ers_result_t
8a7e4c24 9090scsih_pci_slot_reset(struct pci_dev *pdev)
f92363d1
SR
9091{
9092 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9093 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9094 int rc;
9095
9096 pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
9097 ioc->name);
9098
9099 ioc->pci_error_recovery = 0;
9100 ioc->pdev = pdev;
9101 pci_restore_state(pdev);
9102 rc = mpt3sas_base_map_resources(ioc);
9103 if (rc)
9104 return PCI_ERS_RESULT_DISCONNECT;
9105
98c56ad3 9106 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
9107
9108 pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
9109 (rc == 0) ? "success" : "failed");
9110
9111 if (!rc)
9112 return PCI_ERS_RESULT_RECOVERED;
9113 else
9114 return PCI_ERS_RESULT_DISCONNECT;
9115}
9116
9117/**
8a7e4c24 9118 * scsih_pci_resume() - resume normal ops after PCI reset
f92363d1
SR
9119 * @pdev: pointer to PCI device
9120 *
9121 * Called when the error recovery driver tells us that its
9122 * OK to resume normal operation. Use completion to allow
9123 * halted scsi ops to resume.
9124 */
8bbb1cf6 9125static void
8a7e4c24 9126scsih_pci_resume(struct pci_dev *pdev)
f92363d1
SR
9127{
9128 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9129 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9130
9131 pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
9132
9133 pci_cleanup_aer_uncorrect_error_status(pdev);
9134 mpt3sas_base_start_watchdog(ioc);
9135 scsi_unblock_requests(ioc->shost);
9136}
9137
9138/**
8a7e4c24 9139 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
f92363d1
SR
9140 * @pdev: pointer to PCI device
9141 */
8bbb1cf6 9142static pci_ers_result_t
8a7e4c24 9143scsih_pci_mmio_enabled(struct pci_dev *pdev)
f92363d1
SR
9144{
9145 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9146 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9147
9148 pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
9149 ioc->name);
9150
9151 /* TODO - dump whatever for debugging purposes */
9152
83c3d340
KW
9153 /* This called only if scsih_pci_error_detected returns
9154 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
9155 * works, no need to reset slot.
9156 */
9157 return PCI_ERS_RESULT_RECOVERED;
f92363d1
SR
9158}
9159
307d9075
AM
9160/**
9161 * scsih__ncq_prio_supp - Check for NCQ command priority support
9162 * @sdev: scsi device struct
9163 *
9164 * This is called when a user indicates they would like to enable
9165 * ncq command priorities. This works only on SATA devices.
9166 */
9167bool scsih_ncq_prio_supp(struct scsi_device *sdev)
9168{
9169 unsigned char *buf;
9170 bool ncq_prio_supp = false;
9171
9172 if (!scsi_device_supports_vpd(sdev))
9173 return ncq_prio_supp;
9174
9175 buf = kmalloc(SCSI_VPD_PG_LEN, GFP_KERNEL);
9176 if (!buf)
9177 return ncq_prio_supp;
9178
9179 if (!scsi_get_vpd_page(sdev, 0x89, buf, SCSI_VPD_PG_LEN))
9180 ncq_prio_supp = (buf[213] >> 4) & 1;
9181
9182 kfree(buf);
9183 return ncq_prio_supp;
9184}
c84b06a4
SR
9185/*
9186 * The pci device ids are defined in mpi/mpi2_cnfg.h.
9187 */
9188static const struct pci_device_id mpt3sas_pci_table[] = {
9189 /* Spitfire ~ 2004 */
9190 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
9191 PCI_ANY_ID, PCI_ANY_ID },
9192 /* Falcon ~ 2008 */
9193 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
9194 PCI_ANY_ID, PCI_ANY_ID },
9195 /* Liberator ~ 2108 */
9196 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
9197 PCI_ANY_ID, PCI_ANY_ID },
9198 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
9199 PCI_ANY_ID, PCI_ANY_ID },
9200 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
9201 PCI_ANY_ID, PCI_ANY_ID },
9202 /* Meteor ~ 2116 */
9203 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
9204 PCI_ANY_ID, PCI_ANY_ID },
9205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
9206 PCI_ANY_ID, PCI_ANY_ID },
9207 /* Thunderbolt ~ 2208 */
9208 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
9209 PCI_ANY_ID, PCI_ANY_ID },
9210 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
9211 PCI_ANY_ID, PCI_ANY_ID },
9212 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
9213 PCI_ANY_ID, PCI_ANY_ID },
9214 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
9215 PCI_ANY_ID, PCI_ANY_ID },
9216 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
9217 PCI_ANY_ID, PCI_ANY_ID },
9218 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
9219 PCI_ANY_ID, PCI_ANY_ID },
9220 /* Mustang ~ 2308 */
9221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
9222 PCI_ANY_ID, PCI_ANY_ID },
9223 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
9224 PCI_ANY_ID, PCI_ANY_ID },
9225 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
9226 PCI_ANY_ID, PCI_ANY_ID },
9227 /* SSS6200 */
9228 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
9229 PCI_ANY_ID, PCI_ANY_ID },
9230 /* Fury ~ 3004 and 3008 */
9231 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
9232 PCI_ANY_ID, PCI_ANY_ID },
9233 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
9234 PCI_ANY_ID, PCI_ANY_ID },
9235 /* Invader ~ 3108 */
9236 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
9237 PCI_ANY_ID, PCI_ANY_ID },
9238 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
9239 PCI_ANY_ID, PCI_ANY_ID },
9240 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
9241 PCI_ANY_ID, PCI_ANY_ID },
9242 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
9243 PCI_ANY_ID, PCI_ANY_ID },
b130b0d5
SS
9244 /* Cutlass ~ 3216 and 3224 */
9245 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
9246 PCI_ANY_ID, PCI_ANY_ID },
9247 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
9248 PCI_ANY_ID, PCI_ANY_ID },
9249 /* Intruder ~ 3316 and 3324 */
9250 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
9251 PCI_ANY_ID, PCI_ANY_ID },
9252 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
9253 PCI_ANY_ID, PCI_ANY_ID },
9254 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
9255 PCI_ANY_ID, PCI_ANY_ID },
9256 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
9257 PCI_ANY_ID, PCI_ANY_ID },
9258 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
9259 PCI_ANY_ID, PCI_ANY_ID },
9260 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
9261 PCI_ANY_ID, PCI_ANY_ID },
9262 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
9263 PCI_ANY_ID, PCI_ANY_ID },
9264 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
9265 PCI_ANY_ID, PCI_ANY_ID },
998f26ae
SPS
9266 /* Ventura, Crusader, Harpoon & Tomcat ~ 3516, 3416, 3508 & 3408*/
9267 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508,
9268 PCI_ANY_ID, PCI_ANY_ID },
9269 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508_1,
9270 PCI_ANY_ID, PCI_ANY_ID },
9271 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3408,
9272 PCI_ANY_ID, PCI_ANY_ID },
9273 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516,
9274 PCI_ANY_ID, PCI_ANY_ID },
9275 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516_1,
9276 PCI_ANY_ID, PCI_ANY_ID },
9277 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3416,
9278 PCI_ANY_ID, PCI_ANY_ID },
c84b06a4
SR
9279 {0} /* Terminating entry */
9280};
9281MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
9282
9283static struct pci_error_handlers _mpt3sas_err_handler = {
9284 .error_detected = scsih_pci_error_detected,
9285 .mmio_enabled = scsih_pci_mmio_enabled,
9286 .slot_reset = scsih_pci_slot_reset,
9287 .resume = scsih_pci_resume,
9288};
9289
9290static struct pci_driver mpt3sas_driver = {
9291 .name = MPT3SAS_DRIVER_NAME,
9292 .id_table = mpt3sas_pci_table,
9293 .probe = _scsih_probe,
9294 .remove = scsih_remove,
9295 .shutdown = scsih_shutdown,
9296 .err_handler = &_mpt3sas_err_handler,
9297#ifdef CONFIG_PM
9298 .suspend = scsih_suspend,
9299 .resume = scsih_resume,
9300#endif
9301};
9302
f92363d1 9303/**
8a7e4c24 9304 * scsih_init - main entry point for this driver.
f92363d1
SR
9305 *
9306 * Returns 0 success, anything else error.
9307 */
8bbb1cf6 9308static int
8a7e4c24 9309scsih_init(void)
f92363d1 9310{
c84b06a4
SR
9311 mpt2_ids = 0;
9312 mpt3_ids = 0;
f92363d1 9313
f92363d1
SR
9314 mpt3sas_base_initialize_callback_handler();
9315
9316 /* queuecommand callback hander */
9317 scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
9318
6c7abffc 9319 /* task management callback handler */
f92363d1
SR
9320 tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
9321
9322 /* base internal commands callback handler */
9323 base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
9324 port_enable_cb_idx = mpt3sas_base_register_callback_handler(
9325 mpt3sas_port_enable_done);
9326
9327 /* transport internal commands callback handler */
9328 transport_cb_idx = mpt3sas_base_register_callback_handler(
9329 mpt3sas_transport_done);
9330
9331 /* scsih internal commands callback handler */
9332 scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
9333
9334 /* configuration page API internal commands callback handler */
9335 config_cb_idx = mpt3sas_base_register_callback_handler(
9336 mpt3sas_config_done);
9337
9338 /* ctl module callback handler */
9339 ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
9340
9341 tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
9342 _scsih_tm_tr_complete);
9343
9344 tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
9345 _scsih_tm_volume_tr_complete);
9346
9347 tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
9348 _scsih_sas_control_complete);
9349
7497392a 9350 return 0;
f92363d1
SR
9351}
9352
9353/**
7497392a 9354 * scsih_exit - exit point for this driver (when it is a module).
f92363d1
SR
9355 *
9356 * Returns 0 success, anything else error.
9357 */
8bbb1cf6 9358static void
8a7e4c24 9359scsih_exit(void)
f92363d1 9360{
f92363d1
SR
9361
9362 mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
9363 mpt3sas_base_release_callback_handler(tm_cb_idx);
9364 mpt3sas_base_release_callback_handler(base_cb_idx);
9365 mpt3sas_base_release_callback_handler(port_enable_cb_idx);
9366 mpt3sas_base_release_callback_handler(transport_cb_idx);
9367 mpt3sas_base_release_callback_handler(scsih_cb_idx);
9368 mpt3sas_base_release_callback_handler(config_cb_idx);
9369 mpt3sas_base_release_callback_handler(ctl_cb_idx);
9370
9371 mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
9372 mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
9373 mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
9374
9375/* raid transport support */
c84b06a4
SR
9376 if (hbas_to_enumerate != 1)
9377 raid_class_release(mpt3sas_raid_template);
9378 if (hbas_to_enumerate != 2)
9379 raid_class_release(mpt2sas_raid_template);
f92363d1
SR
9380 sas_release_transport(mpt3sas_transport_template);
9381}
7786ab6a 9382
c84b06a4
SR
9383/**
9384 * _mpt3sas_init - main entry point for this driver.
9385 *
9386 * Returns 0 success, anything else error.
9387 */
9388static int __init
9389_mpt3sas_init(void)
9390{
9391 int error;
9392
9393 pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
9394 MPT3SAS_DRIVER_VERSION);
9395
9396 mpt3sas_transport_template =
9397 sas_attach_transport(&mpt3sas_transport_functions);
9398 if (!mpt3sas_transport_template)
9399 return -ENODEV;
9400
9401 /* No need attach mpt3sas raid functions template
9402 * if hbas_to_enumarate value is one.
9403 */
9404 if (hbas_to_enumerate != 1) {
9405 mpt3sas_raid_template =
9406 raid_class_attach(&mpt3sas_raid_functions);
9407 if (!mpt3sas_raid_template) {
9408 sas_release_transport(mpt3sas_transport_template);
9409 return -ENODEV;
9410 }
9411 }
9412
9413 /* No need to attach mpt2sas raid functions template
9414 * if hbas_to_enumarate value is two
9415 */
9416 if (hbas_to_enumerate != 2) {
9417 mpt2sas_raid_template =
9418 raid_class_attach(&mpt2sas_raid_functions);
9419 if (!mpt2sas_raid_template) {
9420 sas_release_transport(mpt3sas_transport_template);
9421 return -ENODEV;
9422 }
9423 }
9424
9425 error = scsih_init();
9426 if (error) {
9427 scsih_exit();
9428 return error;
9429 }
9430
9431 mpt3sas_ctl_init(hbas_to_enumerate);
9432
9433 error = pci_register_driver(&mpt3sas_driver);
9434 if (error)
9435 scsih_exit();
9436
9437 return error;
9438}
9439
9440/**
9441 * _mpt3sas_exit - exit point for this driver (when it is a module).
9442 *
9443 */
9444static void __exit
9445_mpt3sas_exit(void)
9446{
9447 pr_info("mpt3sas version %s unloading\n",
9448 MPT3SAS_DRIVER_VERSION);
9449
9450 pci_unregister_driver(&mpt3sas_driver);
9451
9452 mpt3sas_ctl_exit(hbas_to_enumerate);
9453
9454 scsih_exit();
9455}
9456
9457module_init(_mpt3sas_init);
9458module_exit(_mpt3sas_exit);