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