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