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