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