blob: c0d5d5e1f9c8c869e8e29b74dad0c1fdfe2b29d4 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
5 * Copyright (C) 2007-2008 LSI Corporation
6 * (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
44#include <linux/version.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/interrupt.h>
55
56#include "mpt2sas_base.h"
57
58MODULE_AUTHOR(MPT2SAS_AUTHOR);
59MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60MODULE_LICENSE("GPL");
61MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
62
63#define RAID_CHANNEL 1
64
65/* forward proto's */
66static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67 struct _sas_node *sas_expander);
68static void _firmware_event_work(struct work_struct *work);
69
70/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060071LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060072
73/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060074static u8 scsi_io_cb_idx = -1;
75static u8 tm_cb_idx = -1;
76static u8 ctl_cb_idx = -1;
77static u8 base_cb_idx = -1;
78static u8 transport_cb_idx = -1;
79static u8 config_cb_idx = -1;
80static int mpt_ids;
81
82/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060083static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060084MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
85 "(default=0)");
86
87/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
88#define MPT2SAS_MAX_LUN (16895)
89static int max_lun = MPT2SAS_MAX_LUN;
90module_param(max_lun, int, 0);
91MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
92
93/**
94 * struct sense_info - common structure for obtaining sense keys
95 * @skey: sense key
96 * @asc: additional sense code
97 * @ascq: additional sense code qualifier
98 */
99struct sense_info {
100 u8 skey;
101 u8 asc;
102 u8 ascq;
103};
104
105
Eric Moore635374e2009-03-09 01:21:12 -0600106/**
107 * struct fw_event_work - firmware event struct
108 * @list: link list framework
109 * @work: work object (ioc->fault_reset_work_q)
110 * @ioc: per adapter object
111 * @VF_ID: virtual function id
112 * @host_reset_handling: handling events during host reset
113 * @ignore: flag meaning this event has been marked to ignore
114 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
115 * @event_data: reply event data payload follows
116 *
117 * This object stored on ioc->fw_event_list.
118 */
119struct fw_event_work {
120 struct list_head list;
Eric Moore6f92a7a2009-04-21 15:43:33 -0600121 struct work_struct work;
Eric Moore635374e2009-03-09 01:21:12 -0600122 struct MPT2SAS_ADAPTER *ioc;
123 u8 VF_ID;
124 u8 host_reset_handling;
125 u8 ignore;
126 u16 event;
127 void *event_data;
128};
129
130/**
131 * struct _scsi_io_transfer - scsi io transfer
132 * @handle: sas device handle (assigned by firmware)
133 * @is_raid: flag set for hidden raid components
134 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
135 * @data_length: data transfer length
136 * @data_dma: dma pointer to data
137 * @sense: sense data
138 * @lun: lun number
139 * @cdb_length: cdb length
140 * @cdb: cdb contents
141 * @valid_reply: flag set for reply message
142 * @timeout: timeout for this command
143 * @sense_length: sense length
144 * @ioc_status: ioc status
145 * @scsi_state: scsi state
146 * @scsi_status: scsi staus
147 * @log_info: log information
148 * @transfer_length: data length transfer when there is a reply message
149 *
150 * Used for sending internal scsi commands to devices within this module.
151 * Refer to _scsi_send_scsi_io().
152 */
153struct _scsi_io_transfer {
154 u16 handle;
155 u8 is_raid;
156 enum dma_data_direction dir;
157 u32 data_length;
158 dma_addr_t data_dma;
159 u8 sense[SCSI_SENSE_BUFFERSIZE];
160 u32 lun;
161 u8 cdb_length;
162 u8 cdb[32];
163 u8 timeout;
164 u8 valid_reply;
165 /* the following bits are only valid when 'valid_reply = 1' */
166 u32 sense_length;
167 u16 ioc_status;
168 u8 scsi_state;
169 u8 scsi_status;
170 u32 log_info;
171 u32 transfer_length;
172};
173
174/*
175 * The pci device ids are defined in mpi/mpi2_cnfg.h.
176 */
177static struct pci_device_id scsih_pci_table[] = {
178 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
179 PCI_ANY_ID, PCI_ANY_ID },
180 /* Falcon ~ 2008*/
181 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
182 PCI_ANY_ID, PCI_ANY_ID },
183 /* Liberator ~ 2108 */
184 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
185 PCI_ANY_ID, PCI_ANY_ID },
186 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
187 PCI_ANY_ID, PCI_ANY_ID },
188 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
189 PCI_ANY_ID, PCI_ANY_ID },
190 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
191 PCI_ANY_ID, PCI_ANY_ID },
192 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
193 PCI_ANY_ID, PCI_ANY_ID },
194 {0} /* Terminating entry */
195};
196MODULE_DEVICE_TABLE(pci, scsih_pci_table);
197
198/**
Eric Moored5d135b2009-05-18 13:02:08 -0600199 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600200 *
201 * Note: The logging levels are defined in mpt2sas_debug.h.
202 */
203static int
Eric Moored5d135b2009-05-18 13:02:08 -0600204_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600205{
206 int ret = param_set_int(val, kp);
207 struct MPT2SAS_ADAPTER *ioc;
208
209 if (ret)
210 return ret;
211
212 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600213 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600214 ioc->logging_level = logging_level;
215 return 0;
216}
Eric Moored5d135b2009-05-18 13:02:08 -0600217module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600218 &logging_level, 0644);
219
220/**
221 * _scsih_srch_boot_sas_address - search based on sas_address
222 * @sas_address: sas address
223 * @boot_device: boot device object from bios page 2
224 *
225 * Returns 1 when there's a match, 0 means no match.
226 */
227static inline int
228_scsih_srch_boot_sas_address(u64 sas_address,
229 Mpi2BootDeviceSasWwid_t *boot_device)
230{
231 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
232}
233
234/**
235 * _scsih_srch_boot_device_name - search based on device name
236 * @device_name: device name specified in INDENTIFY fram
237 * @boot_device: boot device object from bios page 2
238 *
239 * Returns 1 when there's a match, 0 means no match.
240 */
241static inline int
242_scsih_srch_boot_device_name(u64 device_name,
243 Mpi2BootDeviceDeviceName_t *boot_device)
244{
245 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
246}
247
248/**
249 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
250 * @enclosure_logical_id: enclosure logical id
251 * @slot_number: slot number
252 * @boot_device: boot device object from bios page 2
253 *
254 * Returns 1 when there's a match, 0 means no match.
255 */
256static inline int
257_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
258 Mpi2BootDeviceEnclosureSlot_t *boot_device)
259{
260 return (enclosure_logical_id == le64_to_cpu(boot_device->
261 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
262 SlotNumber)) ? 1 : 0;
263}
264
265/**
266 * _scsih_is_boot_device - search for matching boot device.
267 * @sas_address: sas address
268 * @device_name: device name specified in INDENTIFY fram
269 * @enclosure_logical_id: enclosure logical id
270 * @slot_number: slot number
271 * @form: specifies boot device form
272 * @boot_device: boot device object from bios page 2
273 *
274 * Returns 1 when there's a match, 0 means no match.
275 */
276static int
277_scsih_is_boot_device(u64 sas_address, u64 device_name,
278 u64 enclosure_logical_id, u16 slot, u8 form,
279 Mpi2BiosPage2BootDevice_t *boot_device)
280{
281 int rc = 0;
282
283 switch (form) {
284 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
285 if (!sas_address)
286 break;
287 rc = _scsih_srch_boot_sas_address(
288 sas_address, &boot_device->SasWwid);
289 break;
290 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
291 if (!enclosure_logical_id)
292 break;
293 rc = _scsih_srch_boot_encl_slot(
294 enclosure_logical_id,
295 slot, &boot_device->EnclosureSlot);
296 break;
297 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
298 if (!device_name)
299 break;
300 rc = _scsih_srch_boot_device_name(
301 device_name, &boot_device->DeviceName);
302 break;
303 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
304 break;
305 }
306
307 return rc;
308}
309
310/**
311 * _scsih_determine_boot_device - determine boot device.
312 * @ioc: per adapter object
313 * @device: either sas_device or raid_device object
314 * @is_raid: [flag] 1 = raid object, 0 = sas object
315 *
316 * Determines whether this device should be first reported device to
317 * to scsi-ml or sas transport, this purpose is for persistant boot device.
318 * There are primary, alternate, and current entries in bios page 2. The order
319 * priority is primary, alternate, then current. This routine saves
320 * the corresponding device object and is_raid flag in the ioc object.
321 * The saved data to be used later in _scsih_probe_boot_devices().
322 */
323static void
324_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
325 void *device, u8 is_raid)
326{
327 struct _sas_device *sas_device;
328 struct _raid_device *raid_device;
329 u64 sas_address;
330 u64 device_name;
331 u64 enclosure_logical_id;
332 u16 slot;
333
334 /* only process this function when driver loads */
335 if (!ioc->wait_for_port_enable_to_complete)
336 return;
337
338 if (!is_raid) {
339 sas_device = device;
340 sas_address = sas_device->sas_address;
341 device_name = sas_device->device_name;
342 enclosure_logical_id = sas_device->enclosure_logical_id;
343 slot = sas_device->slot;
344 } else {
345 raid_device = device;
346 sas_address = raid_device->wwid;
347 device_name = 0;
348 enclosure_logical_id = 0;
349 slot = 0;
350 }
351
352 if (!ioc->req_boot_device.device) {
353 if (_scsih_is_boot_device(sas_address, device_name,
354 enclosure_logical_id, slot,
355 (ioc->bios_pg2.ReqBootDeviceForm &
356 MPI2_BIOSPAGE2_FORM_MASK),
357 &ioc->bios_pg2.RequestedBootDevice)) {
358 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
359 "%s: req_boot_device(0x%016llx)\n",
360 ioc->name, __func__,
361 (unsigned long long)sas_address));
362 ioc->req_boot_device.device = device;
363 ioc->req_boot_device.is_raid = is_raid;
364 }
365 }
366
367 if (!ioc->req_alt_boot_device.device) {
368 if (_scsih_is_boot_device(sas_address, device_name,
369 enclosure_logical_id, slot,
370 (ioc->bios_pg2.ReqAltBootDeviceForm &
371 MPI2_BIOSPAGE2_FORM_MASK),
372 &ioc->bios_pg2.RequestedAltBootDevice)) {
373 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
374 "%s: req_alt_boot_device(0x%016llx)\n",
375 ioc->name, __func__,
376 (unsigned long long)sas_address));
377 ioc->req_alt_boot_device.device = device;
378 ioc->req_alt_boot_device.is_raid = is_raid;
379 }
380 }
381
382 if (!ioc->current_boot_device.device) {
383 if (_scsih_is_boot_device(sas_address, device_name,
384 enclosure_logical_id, slot,
385 (ioc->bios_pg2.CurrentBootDeviceForm &
386 MPI2_BIOSPAGE2_FORM_MASK),
387 &ioc->bios_pg2.CurrentBootDevice)) {
388 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
389 "%s: current_boot_device(0x%016llx)\n",
390 ioc->name, __func__,
391 (unsigned long long)sas_address));
392 ioc->current_boot_device.device = device;
393 ioc->current_boot_device.is_raid = is_raid;
394 }
395 }
396}
397
398/**
399 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
400 * @ioc: per adapter object
401 * @sas_address: sas address
402 * Context: Calling function should acquire ioc->sas_device_lock
403 *
404 * This searches for sas_device based on sas_address, then return sas_device
405 * object.
406 */
407struct _sas_device *
408mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
409 u64 sas_address)
410{
411 struct _sas_device *sas_device, *r;
412
413 r = NULL;
414 /* check the sas_device_init_list */
415 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
416 list) {
417 if (sas_device->sas_address != sas_address)
418 continue;
419 r = sas_device;
420 goto out;
421 }
422
423 /* then check the sas_device_list */
424 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
425 if (sas_device->sas_address != sas_address)
426 continue;
427 r = sas_device;
428 goto out;
429 }
430 out:
431 return r;
432}
433
434/**
435 * _scsih_sas_device_find_by_handle - sas device search
436 * @ioc: per adapter object
437 * @handle: sas device handle (assigned by firmware)
438 * Context: Calling function should acquire ioc->sas_device_lock
439 *
440 * This searches for sas_device based on sas_address, then return sas_device
441 * object.
442 */
443static struct _sas_device *
444_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
445{
446 struct _sas_device *sas_device, *r;
447
448 r = NULL;
449 if (ioc->wait_for_port_enable_to_complete) {
450 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
451 list) {
452 if (sas_device->handle != handle)
453 continue;
454 r = sas_device;
455 goto out;
456 }
457 } else {
458 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
459 if (sas_device->handle != handle)
460 continue;
461 r = sas_device;
462 goto out;
463 }
464 }
465
466 out:
467 return r;
468}
469
470/**
471 * _scsih_sas_device_remove - remove sas_device from list.
472 * @ioc: per adapter object
473 * @sas_device: the sas_device object
474 * Context: This function will acquire ioc->sas_device_lock.
475 *
476 * Removing object and freeing associated memory from the ioc->sas_device_list.
477 */
478static void
479_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
480 struct _sas_device *sas_device)
481{
482 unsigned long flags;
483
484 spin_lock_irqsave(&ioc->sas_device_lock, flags);
485 list_del(&sas_device->list);
486 memset(sas_device, 0, sizeof(struct _sas_device));
487 kfree(sas_device);
488 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
489}
490
491/**
492 * _scsih_sas_device_add - insert sas_device to the list.
493 * @ioc: per adapter object
494 * @sas_device: the sas_device object
495 * Context: This function will acquire ioc->sas_device_lock.
496 *
497 * Adding new object to the ioc->sas_device_list.
498 */
499static void
500_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
501 struct _sas_device *sas_device)
502{
503 unsigned long flags;
504 u16 handle, parent_handle;
505 u64 sas_address;
506
507 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
508 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
509 sas_device->handle, (unsigned long long)sas_device->sas_address));
510
511 spin_lock_irqsave(&ioc->sas_device_lock, flags);
512 list_add_tail(&sas_device->list, &ioc->sas_device_list);
513 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
514
515 handle = sas_device->handle;
516 parent_handle = sas_device->parent_handle;
517 sas_address = sas_device->sas_address;
Eric Moore8901cbb2009-04-21 15:41:32 -0600518 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle))
Eric Moore635374e2009-03-09 01:21:12 -0600519 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600520}
521
522/**
523 * _scsih_sas_device_init_add - insert sas_device to the list.
524 * @ioc: per adapter object
525 * @sas_device: the sas_device object
526 * Context: This function will acquire ioc->sas_device_lock.
527 *
528 * Adding new object at driver load time to the ioc->sas_device_init_list.
529 */
530static void
531_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
532 struct _sas_device *sas_device)
533{
534 unsigned long flags;
535
536 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
537 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
538 sas_device->handle, (unsigned long long)sas_device->sas_address));
539
540 spin_lock_irqsave(&ioc->sas_device_lock, flags);
541 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
542 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
543 _scsih_determine_boot_device(ioc, sas_device, 0);
544}
545
546/**
547 * mpt2sas_scsih_expander_find_by_handle - expander device search
548 * @ioc: per adapter object
549 * @handle: expander handle (assigned by firmware)
550 * Context: Calling function should acquire ioc->sas_device_lock
551 *
552 * This searches for expander device based on handle, then returns the
553 * sas_node object.
554 */
555struct _sas_node *
556mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
557{
558 struct _sas_node *sas_expander, *r;
559
560 r = NULL;
561 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
562 if (sas_expander->handle != handle)
563 continue;
564 r = sas_expander;
565 goto out;
566 }
567 out:
568 return r;
569}
570
571/**
572 * _scsih_raid_device_find_by_id - raid device search
573 * @ioc: per adapter object
574 * @id: sas device target id
575 * @channel: sas device channel
576 * Context: Calling function should acquire ioc->raid_device_lock
577 *
578 * This searches for raid_device based on target id, then return raid_device
579 * object.
580 */
581static struct _raid_device *
582_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
583{
584 struct _raid_device *raid_device, *r;
585
586 r = NULL;
587 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
588 if (raid_device->id == id && raid_device->channel == channel) {
589 r = raid_device;
590 goto out;
591 }
592 }
593
594 out:
595 return r;
596}
597
598/**
599 * _scsih_raid_device_find_by_handle - raid device search
600 * @ioc: per adapter object
601 * @handle: sas device handle (assigned by firmware)
602 * Context: Calling function should acquire ioc->raid_device_lock
603 *
604 * This searches for raid_device based on handle, then return raid_device
605 * object.
606 */
607static struct _raid_device *
608_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
609{
610 struct _raid_device *raid_device, *r;
611
612 r = NULL;
613 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
614 if (raid_device->handle != handle)
615 continue;
616 r = raid_device;
617 goto out;
618 }
619
620 out:
621 return r;
622}
623
624/**
625 * _scsih_raid_device_find_by_wwid - raid device search
626 * @ioc: per adapter object
627 * @handle: sas device handle (assigned by firmware)
628 * Context: Calling function should acquire ioc->raid_device_lock
629 *
630 * This searches for raid_device based on wwid, then return raid_device
631 * object.
632 */
633static struct _raid_device *
634_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
635{
636 struct _raid_device *raid_device, *r;
637
638 r = NULL;
639 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
640 if (raid_device->wwid != wwid)
641 continue;
642 r = raid_device;
643 goto out;
644 }
645
646 out:
647 return r;
648}
649
650/**
651 * _scsih_raid_device_add - add raid_device object
652 * @ioc: per adapter object
653 * @raid_device: raid_device object
654 *
655 * This is added to the raid_device_list link list.
656 */
657static void
658_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
659 struct _raid_device *raid_device)
660{
661 unsigned long flags;
662
663 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
664 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
665 raid_device->handle, (unsigned long long)raid_device->wwid));
666
667 spin_lock_irqsave(&ioc->raid_device_lock, flags);
668 list_add_tail(&raid_device->list, &ioc->raid_device_list);
669 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
670}
671
672/**
673 * _scsih_raid_device_remove - delete raid_device object
674 * @ioc: per adapter object
675 * @raid_device: raid_device object
676 *
677 * This is removed from the raid_device_list link list.
678 */
679static void
680_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
681 struct _raid_device *raid_device)
682{
683 unsigned long flags;
684
685 spin_lock_irqsave(&ioc->raid_device_lock, flags);
686 list_del(&raid_device->list);
687 memset(raid_device, 0, sizeof(struct _raid_device));
688 kfree(raid_device);
689 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
690}
691
692/**
693 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
694 * @ioc: per adapter object
695 * @sas_address: sas address
696 * Context: Calling function should acquire ioc->sas_node_lock.
697 *
698 * This searches for expander device based on sas_address, then returns the
699 * sas_node object.
700 */
701struct _sas_node *
702mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
703 u64 sas_address)
704{
705 struct _sas_node *sas_expander, *r;
706
707 r = NULL;
708 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
709 if (sas_expander->sas_address != sas_address)
710 continue;
711 r = sas_expander;
712 goto out;
713 }
714 out:
715 return r;
716}
717
718/**
719 * _scsih_expander_node_add - insert expander device to the list.
720 * @ioc: per adapter object
721 * @sas_expander: the sas_device object
722 * Context: This function will acquire ioc->sas_node_lock.
723 *
724 * Adding new object to the ioc->sas_expander_list.
725 *
726 * Return nothing.
727 */
728static void
729_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
730 struct _sas_node *sas_expander)
731{
732 unsigned long flags;
733
734 spin_lock_irqsave(&ioc->sas_node_lock, flags);
735 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
736 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
737}
738
739/**
740 * _scsih_is_end_device - determines if device is an end device
741 * @device_info: bitfield providing information about the device.
742 * Context: none
743 *
744 * Returns 1 if end device.
745 */
746static int
747_scsih_is_end_device(u32 device_info)
748{
749 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
750 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
751 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
752 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
753 return 1;
754 else
755 return 0;
756}
757
758/**
759 * _scsih_scsi_lookup_get - returns scmd entry
760 * @ioc: per adapter object
761 * @smid: system request message index
762 * Context: This function will acquire ioc->scsi_lookup_lock.
763 *
764 * Returns the smid stored scmd pointer.
765 */
766static struct scsi_cmnd *
767_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
768{
769 unsigned long flags;
770 struct scsi_cmnd *scmd;
771
772 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
773 scmd = ioc->scsi_lookup[smid - 1].scmd;
774 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
775 return scmd;
776}
777
778/**
779 * mptscsih_getclear_scsi_lookup - returns scmd entry
780 * @ioc: per adapter object
781 * @smid: system request message index
782 * Context: This function will acquire ioc->scsi_lookup_lock.
783 *
784 * Returns the smid stored scmd pointer, as well as clearing the scmd pointer.
785 */
786static struct scsi_cmnd *
787_scsih_scsi_lookup_getclear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
788{
789 unsigned long flags;
790 struct scsi_cmnd *scmd;
791
792 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
793 scmd = ioc->scsi_lookup[smid - 1].scmd;
794 ioc->scsi_lookup[smid - 1].scmd = NULL;
795 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
796 return scmd;
797}
798
799/**
800 * _scsih_scsi_lookup_set - updates scmd entry in lookup
801 * @ioc: per adapter object
802 * @smid: system request message index
803 * @scmd: pointer to scsi command object
804 * Context: This function will acquire ioc->scsi_lookup_lock.
805 *
806 * This will save scmd pointer in the scsi_lookup array.
807 *
808 * Return nothing.
809 */
810static void
811_scsih_scsi_lookup_set(struct MPT2SAS_ADAPTER *ioc, u16 smid,
812 struct scsi_cmnd *scmd)
813{
814 unsigned long flags;
815
816 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
817 ioc->scsi_lookup[smid - 1].scmd = scmd;
818 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
819}
820
821/**
822 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
823 * @ioc: per adapter object
824 * @smid: system request message index
825 * @scmd: pointer to scsi command object
826 * Context: This function will acquire ioc->scsi_lookup_lock.
827 *
828 * This will search for a scmd pointer in the scsi_lookup array,
829 * returning the revelent smid. A returned value of zero means invalid.
830 */
831static u16
832_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
833 *scmd)
834{
835 u16 smid;
836 unsigned long flags;
837 int i;
838
839 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
840 smid = 0;
841 for (i = 0; i < ioc->request_depth; i++) {
842 if (ioc->scsi_lookup[i].scmd == scmd) {
843 smid = i + 1;
844 goto out;
845 }
846 }
847 out:
848 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
849 return smid;
850}
851
852/**
853 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
854 * @ioc: per adapter object
855 * @id: target id
856 * @channel: channel
857 * Context: This function will acquire ioc->scsi_lookup_lock.
858 *
859 * This will search for a matching channel:id in the scsi_lookup array,
860 * returning 1 if found.
861 */
862static u8
863_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
864 int channel)
865{
866 u8 found;
867 unsigned long flags;
868 int i;
869
870 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
871 found = 0;
872 for (i = 0 ; i < ioc->request_depth; i++) {
873 if (ioc->scsi_lookup[i].scmd &&
874 (ioc->scsi_lookup[i].scmd->device->id == id &&
875 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
876 found = 1;
877 goto out;
878 }
879 }
880 out:
881 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
882 return found;
883}
884
885/**
Eric Moore993e0da2009-05-18 13:00:45 -0600886 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
887 * @ioc: per adapter object
888 * @id: target id
889 * @lun: lun number
890 * @channel: channel
891 * Context: This function will acquire ioc->scsi_lookup_lock.
892 *
893 * This will search for a matching channel:id:lun in the scsi_lookup array,
894 * returning 1 if found.
895 */
896static u8
897_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
898 unsigned int lun, int channel)
899{
900 u8 found;
901 unsigned long flags;
902 int i;
903
904 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
905 found = 0;
906 for (i = 0 ; i < ioc->request_depth; i++) {
907 if (ioc->scsi_lookup[i].scmd &&
908 (ioc->scsi_lookup[i].scmd->device->id == id &&
909 ioc->scsi_lookup[i].scmd->device->channel == channel &&
910 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
911 found = 1;
912 goto out;
913 }
914 }
915 out:
916 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
917 return found;
918}
919
920/**
Eric Moore635374e2009-03-09 01:21:12 -0600921 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
922 * @ioc: per adapter object
923 * @smid: system request message index
924 *
925 * Returns phys pointer to chain buffer.
926 */
927static dma_addr_t
928_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
929{
930 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
931 ioc->chains_needed_per_io));
932}
933
934/**
935 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
936 * @ioc: per adapter object
937 * @smid: system request message index
938 *
939 * Returns virt pointer to chain buffer.
940 */
941static void *
942_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
943{
944 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
945 ioc->chains_needed_per_io)));
946}
947
948/**
949 * _scsih_build_scatter_gather - main sg creation routine
950 * @ioc: per adapter object
951 * @scmd: scsi command
952 * @smid: system request message index
953 * Context: none.
954 *
955 * The main routine that builds scatter gather table from a given
956 * scsi request sent via the .queuecommand main handler.
957 *
958 * Returns 0 success, anything else error
959 */
960static int
961_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
962 struct scsi_cmnd *scmd, u16 smid)
963{
964 Mpi2SCSIIORequest_t *mpi_request;
965 dma_addr_t chain_dma;
966 struct scatterlist *sg_scmd;
967 void *sg_local, *chain;
968 u32 chain_offset;
969 u32 chain_length;
970 u32 chain_flags;
971 u32 sges_left;
972 u32 sges_in_segment;
973 u32 sgl_flags;
974 u32 sgl_flags_last_element;
975 u32 sgl_flags_end_buffer;
976
977 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
978
979 /* init scatter gather flags */
980 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
981 if (scmd->sc_data_direction == DMA_TO_DEVICE)
982 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
983 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
984 << MPI2_SGE_FLAGS_SHIFT;
985 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
986 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
987 << MPI2_SGE_FLAGS_SHIFT;
988 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
989
990 sg_scmd = scsi_sglist(scmd);
991 sges_left = scsi_dma_map(scmd);
992 if (!sges_left) {
993 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
994 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
995 return -ENOMEM;
996 }
997
998 sg_local = &mpi_request->SGL;
999 sges_in_segment = ioc->max_sges_in_main_message;
1000 if (sges_left <= sges_in_segment)
1001 goto fill_in_last_segment;
1002
1003 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1004 (sges_in_segment * ioc->sge_size))/4;
1005
1006 /* fill in main message segment when there is a chain following */
1007 while (sges_in_segment) {
1008 if (sges_in_segment == 1)
1009 ioc->base_add_sg_single(sg_local,
1010 sgl_flags_last_element | sg_dma_len(sg_scmd),
1011 sg_dma_address(sg_scmd));
1012 else
1013 ioc->base_add_sg_single(sg_local, sgl_flags |
1014 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1015 sg_scmd = sg_next(sg_scmd);
1016 sg_local += ioc->sge_size;
1017 sges_left--;
1018 sges_in_segment--;
1019 }
1020
1021 /* initializing the chain flags and pointers */
1022 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1023 chain = _scsih_get_chain_buffer(ioc, smid);
1024 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1025 do {
1026 sges_in_segment = (sges_left <=
1027 ioc->max_sges_in_chain_message) ? sges_left :
1028 ioc->max_sges_in_chain_message;
1029 chain_offset = (sges_left == sges_in_segment) ?
1030 0 : (sges_in_segment * ioc->sge_size)/4;
1031 chain_length = sges_in_segment * ioc->sge_size;
1032 if (chain_offset) {
1033 chain_offset = chain_offset <<
1034 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1035 chain_length += ioc->sge_size;
1036 }
1037 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1038 chain_length, chain_dma);
1039 sg_local = chain;
1040 if (!chain_offset)
1041 goto fill_in_last_segment;
1042
1043 /* fill in chain segments */
1044 while (sges_in_segment) {
1045 if (sges_in_segment == 1)
1046 ioc->base_add_sg_single(sg_local,
1047 sgl_flags_last_element |
1048 sg_dma_len(sg_scmd),
1049 sg_dma_address(sg_scmd));
1050 else
1051 ioc->base_add_sg_single(sg_local, sgl_flags |
1052 sg_dma_len(sg_scmd),
1053 sg_dma_address(sg_scmd));
1054 sg_scmd = sg_next(sg_scmd);
1055 sg_local += ioc->sge_size;
1056 sges_left--;
1057 sges_in_segment--;
1058 }
1059
1060 chain_dma += ioc->request_sz;
1061 chain += ioc->request_sz;
1062 } while (1);
1063
1064
1065 fill_in_last_segment:
1066
1067 /* fill the last segment */
1068 while (sges_left) {
1069 if (sges_left == 1)
1070 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1071 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1072 else
1073 ioc->base_add_sg_single(sg_local, sgl_flags |
1074 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1075 sg_scmd = sg_next(sg_scmd);
1076 sg_local += ioc->sge_size;
1077 sges_left--;
1078 }
1079
1080 return 0;
1081}
1082
1083/**
Eric Moored5d135b2009-05-18 13:02:08 -06001084 * _scsih_change_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001085 * @sdev: scsi device struct
1086 * @qdepth: requested queue depth
1087 *
1088 * Returns queue depth.
1089 */
1090static int
Eric Moored5d135b2009-05-18 13:02:08 -06001091_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001092{
1093 struct Scsi_Host *shost = sdev->host;
1094 int max_depth;
1095 int tag_type;
1096
1097 max_depth = shost->can_queue;
1098 if (!sdev->tagged_supported)
1099 max_depth = 1;
1100 if (qdepth > max_depth)
1101 qdepth = max_depth;
1102 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1103 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1104
1105 if (sdev->inquiry_len > 7)
1106 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1107 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1108 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1109 sdev->ordered_tags, sdev->scsi_level,
1110 (sdev->inquiry[7] & 2) >> 1);
1111
1112 return sdev->queue_depth;
1113}
1114
1115/**
Eric Moored5d135b2009-05-18 13:02:08 -06001116 * _scsih_change_queue_depth - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001117 * @sdev: scsi device struct
1118 * @tag_type: requested tag type
1119 *
1120 * Returns queue tag type.
1121 */
1122static int
Eric Moored5d135b2009-05-18 13:02:08 -06001123_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001124{
1125 if (sdev->tagged_supported) {
1126 scsi_set_tag_type(sdev, tag_type);
1127 if (tag_type)
1128 scsi_activate_tcq(sdev, sdev->queue_depth);
1129 else
1130 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1131 } else
1132 tag_type = 0;
1133
1134 return tag_type;
1135}
1136
1137/**
Eric Moored5d135b2009-05-18 13:02:08 -06001138 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001139 * @starget: scsi target struct
1140 *
1141 * Returns 0 if ok. Any other return is assumed to be an error and
1142 * the device is ignored.
1143 */
1144static int
Eric Moored5d135b2009-05-18 13:02:08 -06001145_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001146{
1147 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1148 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1149 struct MPT2SAS_TARGET *sas_target_priv_data;
1150 struct _sas_device *sas_device;
1151 struct _raid_device *raid_device;
1152 unsigned long flags;
1153 struct sas_rphy *rphy;
1154
1155 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1156 if (!sas_target_priv_data)
1157 return -ENOMEM;
1158
1159 starget->hostdata = sas_target_priv_data;
1160 sas_target_priv_data->starget = starget;
1161 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1162
1163 /* RAID volumes */
1164 if (starget->channel == RAID_CHANNEL) {
1165 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1166 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1167 starget->channel);
1168 if (raid_device) {
1169 sas_target_priv_data->handle = raid_device->handle;
1170 sas_target_priv_data->sas_address = raid_device->wwid;
1171 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1172 raid_device->starget = starget;
1173 }
1174 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1175 return 0;
1176 }
1177
1178 /* sas/sata devices */
1179 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1180 rphy = dev_to_rphy(starget->dev.parent);
1181 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1182 rphy->identify.sas_address);
1183
1184 if (sas_device) {
1185 sas_target_priv_data->handle = sas_device->handle;
1186 sas_target_priv_data->sas_address = sas_device->sas_address;
1187 sas_device->starget = starget;
1188 sas_device->id = starget->id;
1189 sas_device->channel = starget->channel;
1190 if (sas_device->hidden_raid_component)
1191 sas_target_priv_data->flags |=
1192 MPT_TARGET_FLAGS_RAID_COMPONENT;
1193 }
1194 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1195
1196 return 0;
1197}
1198
1199/**
Eric Moored5d135b2009-05-18 13:02:08 -06001200 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001201 * @starget: scsi target struct
1202 *
1203 * Returns nothing.
1204 */
1205static void
Eric Moored5d135b2009-05-18 13:02:08 -06001206_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001207{
1208 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1209 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1210 struct MPT2SAS_TARGET *sas_target_priv_data;
1211 struct _sas_device *sas_device;
1212 struct _raid_device *raid_device;
1213 unsigned long flags;
1214 struct sas_rphy *rphy;
1215
1216 sas_target_priv_data = starget->hostdata;
1217 if (!sas_target_priv_data)
1218 return;
1219
1220 if (starget->channel == RAID_CHANNEL) {
1221 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1222 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1223 starget->channel);
1224 if (raid_device) {
1225 raid_device->starget = NULL;
1226 raid_device->sdev = NULL;
1227 }
1228 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1229 goto out;
1230 }
1231
1232 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1233 rphy = dev_to_rphy(starget->dev.parent);
1234 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1235 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001236 if (sas_device && (sas_device->starget == starget) &&
1237 (sas_device->id == starget->id) &&
1238 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001239 sas_device->starget = NULL;
1240
1241 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1242
1243 out:
1244 kfree(sas_target_priv_data);
1245 starget->hostdata = NULL;
1246}
1247
1248/**
Eric Moored5d135b2009-05-18 13:02:08 -06001249 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001250 * @sdev: scsi device struct
1251 *
1252 * Returns 0 if ok. Any other return is assumed to be an error and
1253 * the device is ignored.
1254 */
1255static int
Eric Moored5d135b2009-05-18 13:02:08 -06001256_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001257{
1258 struct Scsi_Host *shost;
1259 struct MPT2SAS_ADAPTER *ioc;
1260 struct MPT2SAS_TARGET *sas_target_priv_data;
1261 struct MPT2SAS_DEVICE *sas_device_priv_data;
1262 struct scsi_target *starget;
1263 struct _raid_device *raid_device;
1264 struct _sas_device *sas_device;
1265 unsigned long flags;
1266
1267 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1268 if (!sas_device_priv_data)
1269 return -ENOMEM;
1270
1271 sas_device_priv_data->lun = sdev->lun;
1272 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1273
1274 starget = scsi_target(sdev);
1275 sas_target_priv_data = starget->hostdata;
1276 sas_target_priv_data->num_luns++;
1277 sas_device_priv_data->sas_target = sas_target_priv_data;
1278 sdev->hostdata = sas_device_priv_data;
1279 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1280 sdev->no_uld_attach = 1;
1281
1282 shost = dev_to_shost(&starget->dev);
1283 ioc = shost_priv(shost);
1284 if (starget->channel == RAID_CHANNEL) {
1285 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1286 raid_device = _scsih_raid_device_find_by_id(ioc,
1287 starget->id, starget->channel);
1288 if (raid_device)
1289 raid_device->sdev = sdev; /* raid is single lun */
1290 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1291 } else {
1292 /* set TLR bit for SSP devices */
1293 if (!(ioc->facts.IOCCapabilities &
1294 MPI2_IOCFACTS_CAPABILITY_TLR))
1295 goto out;
1296 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1297 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1298 sas_device_priv_data->sas_target->sas_address);
1299 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1300 if (sas_device && sas_device->device_info &
1301 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1302 sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1303 }
1304
1305 out:
1306 return 0;
1307}
1308
1309/**
Eric Moored5d135b2009-05-18 13:02:08 -06001310 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001311 * @sdev: scsi device struct
1312 *
1313 * Returns nothing.
1314 */
1315static void
Eric Moored5d135b2009-05-18 13:02:08 -06001316_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001317{
1318 struct MPT2SAS_TARGET *sas_target_priv_data;
1319 struct scsi_target *starget;
1320
1321 if (!sdev->hostdata)
1322 return;
1323
1324 starget = scsi_target(sdev);
1325 sas_target_priv_data = starget->hostdata;
1326 sas_target_priv_data->num_luns--;
1327 kfree(sdev->hostdata);
1328 sdev->hostdata = NULL;
1329}
1330
1331/**
Eric Moored5d135b2009-05-18 13:02:08 -06001332 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001333 * @ioc: per adapter object
1334 * @sas_device: the sas_device object
1335 * @sdev: scsi device struct
1336 */
1337static void
Eric Moored5d135b2009-05-18 13:02:08 -06001338_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001339 struct _sas_device *sas_device, struct scsi_device *sdev)
1340{
1341 Mpi2ConfigReply_t mpi_reply;
1342 Mpi2SasDevicePage0_t sas_device_pg0;
1343 u32 ioc_status;
1344 u16 flags;
1345 u32 device_info;
1346
1347 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1348 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1349 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1350 ioc->name, __FILE__, __LINE__, __func__);
1351 return;
1352 }
1353
1354 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1355 MPI2_IOCSTATUS_MASK;
1356 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1357 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1358 ioc->name, __FILE__, __LINE__, __func__);
1359 return;
1360 }
1361
1362 flags = le16_to_cpu(sas_device_pg0.Flags);
1363 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1364
1365 sdev_printk(KERN_INFO, sdev,
1366 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1367 "sw_preserve(%s)\n",
1368 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1369 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1370 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1371 "n",
1372 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1373 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1374 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1375}
1376
1377/**
1378 * _scsih_get_volume_capabilities - volume capabilities
1379 * @ioc: per adapter object
1380 * @sas_device: the raid_device object
1381 */
1382static void
1383_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1384 struct _raid_device *raid_device)
1385{
1386 Mpi2RaidVolPage0_t *vol_pg0;
1387 Mpi2RaidPhysDiskPage0_t pd_pg0;
1388 Mpi2SasDevicePage0_t sas_device_pg0;
1389 Mpi2ConfigReply_t mpi_reply;
1390 u16 sz;
1391 u8 num_pds;
1392
1393 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1394 &num_pds)) || !num_pds) {
1395 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1396 ioc->name, __FILE__, __LINE__, __func__);
1397 return;
1398 }
1399
1400 raid_device->num_pds = num_pds;
1401 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1402 sizeof(Mpi2RaidVol0PhysDisk_t));
1403 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1404 if (!vol_pg0) {
1405 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1406 ioc->name, __FILE__, __LINE__, __func__);
1407 return;
1408 }
1409
1410 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1411 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1412 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1413 ioc->name, __FILE__, __LINE__, __func__);
1414 kfree(vol_pg0);
1415 return;
1416 }
1417
1418 raid_device->volume_type = vol_pg0->VolumeType;
1419
1420 /* figure out what the underlying devices are by
1421 * obtaining the device_info bits for the 1st device
1422 */
1423 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1424 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1425 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1426 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1427 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1428 le16_to_cpu(pd_pg0.DevHandle)))) {
1429 raid_device->device_info =
1430 le32_to_cpu(sas_device_pg0.DeviceInfo);
1431 }
1432 }
1433
1434 kfree(vol_pg0);
1435}
1436
1437/**
Eric Moored5d135b2009-05-18 13:02:08 -06001438 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001439 * @sdev: scsi device struct
1440 *
1441 * Returns 0 if ok. Any other return is assumed to be an error and
1442 * the device is ignored.
1443 */
1444static int
Eric Moored5d135b2009-05-18 13:02:08 -06001445_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001446{
1447 struct Scsi_Host *shost = sdev->host;
1448 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1449 struct MPT2SAS_DEVICE *sas_device_priv_data;
1450 struct MPT2SAS_TARGET *sas_target_priv_data;
1451 struct _sas_device *sas_device;
1452 struct _raid_device *raid_device;
1453 unsigned long flags;
1454 int qdepth;
1455 u8 ssp_target = 0;
1456 char *ds = "";
1457 char *r_level = "";
1458
1459 qdepth = 1;
1460 sas_device_priv_data = sdev->hostdata;
1461 sas_device_priv_data->configured_lun = 1;
1462 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1463 sas_target_priv_data = sas_device_priv_data->sas_target;
1464
1465 /* raid volume handling */
1466 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1467
1468 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1469 raid_device = _scsih_raid_device_find_by_handle(ioc,
1470 sas_target_priv_data->handle);
1471 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1472 if (!raid_device) {
1473 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1474 ioc->name, __FILE__, __LINE__, __func__);
1475 return 0;
1476 }
1477
1478 _scsih_get_volume_capabilities(ioc, raid_device);
1479
1480 /* RAID Queue Depth Support
1481 * IS volume = underlying qdepth of drive type, either
1482 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1483 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1484 */
1485 if (raid_device->device_info &
1486 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1487 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1488 ds = "SSP";
1489 } else {
1490 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1491 if (raid_device->device_info &
1492 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1493 ds = "SATA";
1494 else
1495 ds = "STP";
1496 }
1497
1498 switch (raid_device->volume_type) {
1499 case MPI2_RAID_VOL_TYPE_RAID0:
1500 r_level = "RAID0";
1501 break;
1502 case MPI2_RAID_VOL_TYPE_RAID1E:
1503 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1504 r_level = "RAID1E";
1505 break;
1506 case MPI2_RAID_VOL_TYPE_RAID1:
1507 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1508 r_level = "RAID1";
1509 break;
1510 case MPI2_RAID_VOL_TYPE_RAID10:
1511 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1512 r_level = "RAID10";
1513 break;
1514 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1515 default:
1516 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1517 r_level = "RAIDX";
1518 break;
1519 }
1520
1521 sdev_printk(KERN_INFO, sdev, "%s: "
1522 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1523 r_level, raid_device->handle,
1524 (unsigned long long)raid_device->wwid,
1525 raid_device->num_pds, ds);
Eric Moored5d135b2009-05-18 13:02:08 -06001526 _scsih_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06001527 return 0;
1528 }
1529
1530 /* non-raid handling */
1531 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1532 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1533 sas_device_priv_data->sas_target->sas_address);
1534 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1535 if (sas_device) {
1536 if (sas_target_priv_data->flags &
1537 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1538 mpt2sas_config_get_volume_handle(ioc,
1539 sas_device->handle, &sas_device->volume_handle);
1540 mpt2sas_config_get_volume_wwid(ioc,
1541 sas_device->volume_handle,
1542 &sas_device->volume_wwid);
1543 }
1544 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1545 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1546 ssp_target = 1;
1547 ds = "SSP";
1548 } else {
1549 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1550 if (sas_device->device_info &
1551 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1552 ds = "STP";
1553 else if (sas_device->device_info &
1554 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1555 ds = "SATA";
1556 }
1557
1558 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1559 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1560 ds, sas_device->handle,
1561 (unsigned long long)sas_device->sas_address,
1562 (unsigned long long)sas_device->device_name);
1563 sdev_printk(KERN_INFO, sdev, "%s: "
1564 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1565 (unsigned long long) sas_device->enclosure_logical_id,
1566 sas_device->slot);
1567
1568 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001569 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001570 }
1571
Eric Moored5d135b2009-05-18 13:02:08 -06001572 _scsih_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06001573
1574 if (ssp_target)
1575 sas_read_port_mode_page(sdev);
1576 return 0;
1577}
1578
1579/**
Eric Moored5d135b2009-05-18 13:02:08 -06001580 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001581 * @sdev: scsi device struct
1582 * @bdev: pointer to block device context
1583 * @capacity: device size (in 512 byte sectors)
1584 * @params: three element array to place output:
1585 * params[0] number of heads (max 255)
1586 * params[1] number of sectors (max 63)
1587 * params[2] number of cylinders
1588 *
1589 * Return nothing.
1590 */
1591static int
Eric Moored5d135b2009-05-18 13:02:08 -06001592_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001593 sector_t capacity, int params[])
1594{
1595 int heads;
1596 int sectors;
1597 sector_t cylinders;
1598 ulong dummy;
1599
1600 heads = 64;
1601 sectors = 32;
1602
1603 dummy = heads * sectors;
1604 cylinders = capacity;
1605 sector_div(cylinders, dummy);
1606
1607 /*
1608 * Handle extended translation size for logical drives
1609 * > 1Gb
1610 */
1611 if ((ulong)capacity >= 0x200000) {
1612 heads = 255;
1613 sectors = 63;
1614 dummy = heads * sectors;
1615 cylinders = capacity;
1616 sector_div(cylinders, dummy);
1617 }
1618
1619 /* return result */
1620 params[0] = heads;
1621 params[1] = sectors;
1622 params[2] = cylinders;
1623
1624 return 0;
1625}
1626
1627/**
1628 * _scsih_response_code - translation of device response code
1629 * @ioc: per adapter object
1630 * @response_code: response code returned by the device
1631 *
1632 * Return nothing.
1633 */
1634static void
1635_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1636{
1637 char *desc;
1638
1639 switch (response_code) {
1640 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1641 desc = "task management request completed";
1642 break;
1643 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1644 desc = "invalid frame";
1645 break;
1646 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1647 desc = "task management request not supported";
1648 break;
1649 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1650 desc = "task management request failed";
1651 break;
1652 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1653 desc = "task management request succeeded";
1654 break;
1655 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1656 desc = "invalid lun";
1657 break;
1658 case 0xA:
1659 desc = "overlapped tag attempted";
1660 break;
1661 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1662 desc = "task queued, however not sent to target";
1663 break;
1664 default:
1665 desc = "unknown";
1666 break;
1667 }
1668 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1669 ioc->name, response_code, desc);
1670}
1671
1672/**
Eric Moored5d135b2009-05-18 13:02:08 -06001673 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001674 * @ioc: per adapter object
1675 * @smid: system request message index
1676 * @VF_ID: virtual function id
1677 * @reply: reply message frame(lower 32bit addr)
1678 * Context: none.
1679 *
1680 * The callback handler when using scsih_issue_tm.
1681 *
1682 * Return nothing.
1683 */
1684static void
Eric Moored5d135b2009-05-18 13:02:08 -06001685_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001686{
1687 MPI2DefaultReply_t *mpi_reply;
1688
1689 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1690 return;
1691 if (ioc->tm_cmds.smid != smid)
1692 return;
1693 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1694 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1695 if (mpi_reply) {
1696 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1697 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1698 }
1699 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1700 complete(&ioc->tm_cmds.done);
1701}
1702
1703/**
1704 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1705 * @ioc: per adapter object
1706 * @handle: device handle
1707 *
1708 * During taskmangement request, we need to freeze the device queue.
1709 */
1710void
1711mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1712{
1713 struct MPT2SAS_DEVICE *sas_device_priv_data;
1714 struct scsi_device *sdev;
1715 u8 skip = 0;
1716
1717 shost_for_each_device(sdev, ioc->shost) {
1718 if (skip)
1719 continue;
1720 sas_device_priv_data = sdev->hostdata;
1721 if (!sas_device_priv_data)
1722 continue;
1723 if (sas_device_priv_data->sas_target->handle == handle) {
1724 sas_device_priv_data->sas_target->tm_busy = 1;
1725 skip = 1;
1726 ioc->ignore_loginfos = 1;
1727 }
1728 }
1729}
1730
1731/**
1732 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1733 * @ioc: per adapter object
1734 * @handle: device handle
1735 *
1736 * During taskmangement request, we need to freeze the device queue.
1737 */
1738void
1739mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1740{
1741 struct MPT2SAS_DEVICE *sas_device_priv_data;
1742 struct scsi_device *sdev;
1743 u8 skip = 0;
1744
1745 shost_for_each_device(sdev, ioc->shost) {
1746 if (skip)
1747 continue;
1748 sas_device_priv_data = sdev->hostdata;
1749 if (!sas_device_priv_data)
1750 continue;
1751 if (sas_device_priv_data->sas_target->handle == handle) {
1752 sas_device_priv_data->sas_target->tm_busy = 0;
1753 skip = 1;
1754 ioc->ignore_loginfos = 0;
1755 }
1756 }
1757}
1758
1759/**
1760 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1761 * @ioc: per adapter struct
1762 * @device_handle: device handle
1763 * @lun: lun number
1764 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1765 * @smid_task: smid assigned to the task
1766 * @timeout: timeout in seconds
1767 * Context: The calling function needs to acquire the tm_cmds.mutex
1768 *
1769 * A generic API for sending task management requests to firmware.
1770 *
1771 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1772 * this API.
1773 *
1774 * The callback index is set inside `ioc->tm_cb_idx`.
1775 *
1776 * Return nothing.
1777 */
1778void
1779mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1780 u8 type, u16 smid_task, ulong timeout)
1781{
1782 Mpi2SCSITaskManagementRequest_t *mpi_request;
1783 Mpi2SCSITaskManagementReply_t *mpi_reply;
1784 u16 smid = 0;
1785 u32 ioc_state;
1786 unsigned long timeleft;
1787 u8 VF_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001788
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301789 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1790 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1791 __func__, ioc->name);
1792 return;
1793 }
1794
1795 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06001796 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1797 __func__, ioc->name);
1798 return;
1799 }
Eric Moore635374e2009-03-09 01:21:12 -06001800
1801 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1802 if (ioc_state & MPI2_DOORBELL_USED) {
1803 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1804 "active!\n", ioc->name));
1805 goto issue_host_reset;
1806 }
1807
1808 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1809 mpt2sas_base_fault_info(ioc, ioc_state &
1810 MPI2_DOORBELL_DATA_MASK);
1811 goto issue_host_reset;
1812 }
1813
1814 smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
1815 if (!smid) {
1816 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1817 ioc->name, __func__);
1818 return;
1819 }
1820
1821 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
1822 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
1823 ioc->tm_cmds.status = MPT2_CMD_PENDING;
1824 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1825 ioc->tm_cmds.smid = smid;
1826 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1827 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1828 mpi_request->DevHandle = cpu_to_le16(handle);
1829 mpi_request->TaskType = type;
1830 mpi_request->TaskMID = cpu_to_le16(smid_task);
1831 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1832 mpt2sas_scsih_set_tm_flag(ioc, handle);
1833 mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
1834 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1835 mpt2sas_scsih_clear_tm_flag(ioc, handle);
1836 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1837 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1838 ioc->name, __func__);
1839 _debug_dump_mf(mpi_request,
1840 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1841 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1842 goto issue_host_reset;
1843 }
1844
1845 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1846 mpi_reply = ioc->tm_cmds.reply;
1847 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1848 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1849 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1850 le32_to_cpu(mpi_reply->IOCLogInfo),
1851 le32_to_cpu(mpi_reply->TerminationCount)));
1852 if (ioc->logging_level & MPT_DEBUG_TM)
1853 _scsih_response_code(ioc, mpi_reply->ResponseCode);
1854 }
1855 return;
1856 issue_host_reset:
1857 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1858}
1859
1860/**
Eric Moored5d135b2009-05-18 13:02:08 -06001861 * _scsih_abort - eh threads main abort routine
Eric Moore635374e2009-03-09 01:21:12 -06001862 * @sdev: scsi device struct
1863 *
1864 * Returns SUCCESS if command aborted else FAILED
1865 */
1866static int
Eric Moored5d135b2009-05-18 13:02:08 -06001867_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001868{
1869 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1870 struct MPT2SAS_DEVICE *sas_device_priv_data;
1871 u16 smid;
1872 u16 handle;
1873 int r;
1874 struct scsi_cmnd *scmd_lookup;
1875
1876 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1877 ioc->name, scmd);
1878 scsi_print_command(scmd);
1879
1880 sas_device_priv_data = scmd->device->hostdata;
1881 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1882 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1883 ioc->name, scmd);
1884 scmd->result = DID_NO_CONNECT << 16;
1885 scmd->scsi_done(scmd);
1886 r = SUCCESS;
1887 goto out;
1888 }
1889
1890 /* search for the command */
1891 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1892 if (!smid) {
1893 scmd->result = DID_RESET << 16;
1894 r = SUCCESS;
1895 goto out;
1896 }
1897
1898 /* for hidden raid components and volumes this is not supported */
1899 if (sas_device_priv_data->sas_target->flags &
1900 MPT_TARGET_FLAGS_RAID_COMPONENT ||
1901 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1902 scmd->result = DID_RESET << 16;
1903 r = FAILED;
1904 goto out;
1905 }
1906
1907 mutex_lock(&ioc->tm_cmds.mutex);
1908 handle = sas_device_priv_data->sas_target->handle;
1909 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1910 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1911
1912 /* sanity check - see whether command actually completed */
1913 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1914 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1915 r = FAILED;
1916 else
1917 r = SUCCESS;
1918 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1919 mutex_unlock(&ioc->tm_cmds.mutex);
1920
1921 out:
1922 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1923 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1924 return r;
1925}
1926
Eric Moore635374e2009-03-09 01:21:12 -06001927/**
Eric Moored5d135b2009-05-18 13:02:08 -06001928 * _scsih_dev_reset - eh threads main device reset routine
Eric Moore635374e2009-03-09 01:21:12 -06001929 * @sdev: scsi device struct
1930 *
1931 * Returns SUCCESS if command aborted else FAILED
1932 */
1933static int
Eric Moored5d135b2009-05-18 13:02:08 -06001934_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001935{
1936 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1937 struct MPT2SAS_DEVICE *sas_device_priv_data;
1938 struct _sas_device *sas_device;
1939 unsigned long flags;
1940 u16 handle;
1941 int r;
1942
Eric Moore993e0da2009-05-18 13:00:45 -06001943 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
Eric Moore635374e2009-03-09 01:21:12 -06001944 ioc->name, scmd);
1945 scsi_print_command(scmd);
1946
1947 sas_device_priv_data = scmd->device->hostdata;
1948 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1949 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1950 ioc->name, scmd);
1951 scmd->result = DID_NO_CONNECT << 16;
1952 scmd->scsi_done(scmd);
1953 r = SUCCESS;
1954 goto out;
1955 }
1956
1957 /* for hidden raid components obtain the volume_handle */
1958 handle = 0;
1959 if (sas_device_priv_data->sas_target->flags &
1960 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1961 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1962 sas_device = _scsih_sas_device_find_by_handle(ioc,
1963 sas_device_priv_data->sas_target->handle);
1964 if (sas_device)
1965 handle = sas_device->volume_handle;
1966 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1967 } else
1968 handle = sas_device_priv_data->sas_target->handle;
1969
1970 if (!handle) {
1971 scmd->result = DID_RESET << 16;
1972 r = FAILED;
1973 goto out;
1974 }
1975
1976 mutex_lock(&ioc->tm_cmds.mutex);
1977 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore993e0da2009-05-18 13:00:45 -06001978 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, scmd->device->lun,
1979 30);
1980
1981 /*
1982 * sanity check see whether all commands to this device been
1983 * completed
1984 */
1985 if (_scsih_scsi_lookup_find_by_lun(ioc, scmd->device->id,
1986 scmd->device->lun, scmd->device->channel))
1987 r = FAILED;
1988 else
1989 r = SUCCESS;
1990 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1991 mutex_unlock(&ioc->tm_cmds.mutex);
1992
1993 out:
1994 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
1995 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1996 return r;
1997}
1998
1999/**
Eric Moored5d135b2009-05-18 13:02:08 -06002000 * _scsih_target_reset - eh threads main target reset routine
Eric Moore993e0da2009-05-18 13:00:45 -06002001 * @sdev: scsi device struct
2002 *
2003 * Returns SUCCESS if command aborted else FAILED
2004 */
2005static int
Eric Moored5d135b2009-05-18 13:02:08 -06002006_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002007{
2008 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2009 struct MPT2SAS_DEVICE *sas_device_priv_data;
2010 struct _sas_device *sas_device;
2011 unsigned long flags;
2012 u16 handle;
2013 int r;
2014
2015 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2016 ioc->name, scmd);
2017 scsi_print_command(scmd);
2018
2019 sas_device_priv_data = scmd->device->hostdata;
2020 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2021 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2022 ioc->name, scmd);
2023 scmd->result = DID_NO_CONNECT << 16;
2024 scmd->scsi_done(scmd);
2025 r = SUCCESS;
2026 goto out;
2027 }
2028
2029 /* for hidden raid components obtain the volume_handle */
2030 handle = 0;
2031 if (sas_device_priv_data->sas_target->flags &
2032 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2033 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2034 sas_device = _scsih_sas_device_find_by_handle(ioc,
2035 sas_device_priv_data->sas_target->handle);
2036 if (sas_device)
2037 handle = sas_device->volume_handle;
2038 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2039 } else
2040 handle = sas_device_priv_data->sas_target->handle;
2041
2042 if (!handle) {
2043 scmd->result = DID_RESET << 16;
2044 r = FAILED;
2045 goto out;
2046 }
2047
2048 mutex_lock(&ioc->tm_cmds.mutex);
2049 mpt2sas_scsih_issue_tm(ioc, handle, 0,
Eric Moore635374e2009-03-09 01:21:12 -06002050 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
2051
2052 /*
2053 * sanity check see whether all commands to this target been
2054 * completed
2055 */
2056 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
2057 scmd->device->channel))
2058 r = FAILED;
2059 else
2060 r = SUCCESS;
2061 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2062 mutex_unlock(&ioc->tm_cmds.mutex);
2063
2064 out:
2065 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2066 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2067 return r;
2068}
2069
2070/**
Eric Moored5d135b2009-05-18 13:02:08 -06002071 * _scsih_abort - eh threads main host reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002072 * @sdev: scsi device struct
2073 *
2074 * Returns SUCCESS if command aborted else FAILED
2075 */
2076static int
Eric Moored5d135b2009-05-18 13:02:08 -06002077_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002078{
2079 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2080 int r, retval;
2081
2082 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2083 ioc->name, scmd);
2084 scsi_print_command(scmd);
2085
2086 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2087 FORCE_BIG_HAMMER);
2088 r = (retval < 0) ? FAILED : SUCCESS;
2089 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2090 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2091
2092 return r;
2093}
2094
2095/**
2096 * _scsih_fw_event_add - insert and queue up fw_event
2097 * @ioc: per adapter object
2098 * @fw_event: object describing the event
2099 * Context: This function will acquire ioc->fw_event_lock.
2100 *
2101 * This adds the firmware event object into link list, then queues it up to
2102 * be processed from user context.
2103 *
2104 * Return nothing.
2105 */
2106static void
2107_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2108{
2109 unsigned long flags;
2110
2111 if (ioc->firmware_event_thread == NULL)
2112 return;
2113
2114 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2115 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Eric Moore6f92a7a2009-04-21 15:43:33 -06002116 INIT_WORK(&fw_event->work, _firmware_event_work);
2117 queue_work(ioc->firmware_event_thread, &fw_event->work);
Eric Moore635374e2009-03-09 01:21:12 -06002118 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2119}
2120
2121/**
2122 * _scsih_fw_event_free - delete fw_event
2123 * @ioc: per adapter object
2124 * @fw_event: object describing the event
2125 * Context: This function will acquire ioc->fw_event_lock.
2126 *
2127 * This removes firmware event object from link list, frees associated memory.
2128 *
2129 * Return nothing.
2130 */
2131static void
2132_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2133 *fw_event)
2134{
2135 unsigned long flags;
2136
2137 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2138 list_del(&fw_event->list);
2139 kfree(fw_event->event_data);
2140 kfree(fw_event);
2141 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2142}
2143
2144/**
2145 * _scsih_fw_event_add - requeue an event
2146 * @ioc: per adapter object
2147 * @fw_event: object describing the event
2148 * Context: This function will acquire ioc->fw_event_lock.
2149 *
2150 * Return nothing.
2151 */
2152static void
2153_scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2154 *fw_event, unsigned long delay)
2155{
2156 unsigned long flags;
2157 if (ioc->firmware_event_thread == NULL)
2158 return;
2159
2160 spin_lock_irqsave(&ioc->fw_event_lock, flags);
Eric Moore6f92a7a2009-04-21 15:43:33 -06002161 queue_work(ioc->firmware_event_thread, &fw_event->work);
Eric Moore635374e2009-03-09 01:21:12 -06002162 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2163}
2164
2165/**
2166 * _scsih_fw_event_off - turn flag off preventing event handling
2167 * @ioc: per adapter object
2168 *
2169 * Used to prevent handling of firmware events during adapter reset
2170 * driver unload.
2171 *
2172 * Return nothing.
2173 */
2174static void
2175_scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2176{
2177 unsigned long flags;
2178
2179 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2180 ioc->fw_events_off = 1;
2181 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2182
2183}
2184
2185/**
2186 * _scsih_fw_event_on - turn flag on allowing firmware event handling
2187 * @ioc: per adapter object
2188 *
2189 * Returns nothing.
2190 */
2191static void
2192_scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2193{
2194 unsigned long flags;
2195
2196 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2197 ioc->fw_events_off = 0;
2198 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2199}
2200
2201/**
2202 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2203 * @ioc: per adapter object
2204 * @handle: device handle
2205 *
2206 * During device pull we need to appropiately set the sdev state.
2207 */
2208static void
2209_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2210{
2211 struct MPT2SAS_DEVICE *sas_device_priv_data;
2212 struct scsi_device *sdev;
2213
2214 shost_for_each_device(sdev, ioc->shost) {
2215 sas_device_priv_data = sdev->hostdata;
2216 if (!sas_device_priv_data)
2217 continue;
2218 if (!sas_device_priv_data->block)
2219 continue;
2220 if (sas_device_priv_data->sas_target->handle == handle) {
2221 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2222 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2223 "handle(0x%04x)\n", ioc->name, handle));
2224 sas_device_priv_data->block = 0;
2225 scsi_device_set_state(sdev, SDEV_RUNNING);
2226 }
2227 }
2228}
2229
2230/**
2231 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2232 * @ioc: per adapter object
2233 * @handle: device handle
2234 *
2235 * During device pull we need to appropiately set the sdev state.
2236 */
2237static void
2238_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2239{
2240 struct MPT2SAS_DEVICE *sas_device_priv_data;
2241 struct scsi_device *sdev;
2242
2243 shost_for_each_device(sdev, ioc->shost) {
2244 sas_device_priv_data = sdev->hostdata;
2245 if (!sas_device_priv_data)
2246 continue;
2247 if (sas_device_priv_data->block)
2248 continue;
2249 if (sas_device_priv_data->sas_target->handle == handle) {
2250 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2251 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2252 "handle(0x%04x)\n", ioc->name, handle));
2253 sas_device_priv_data->block = 1;
2254 scsi_device_set_state(sdev, SDEV_BLOCK);
2255 }
2256 }
2257}
2258
2259/**
2260 * _scsih_block_io_to_children_attached_to_ex
2261 * @ioc: per adapter object
2262 * @sas_expander: the sas_device object
2263 *
2264 * This routine set sdev state to SDEV_BLOCK for all devices
2265 * attached to this expander. This function called when expander is
2266 * pulled.
2267 */
2268static void
2269_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2270 struct _sas_node *sas_expander)
2271{
2272 struct _sas_port *mpt2sas_port;
2273 struct _sas_device *sas_device;
2274 struct _sas_node *expander_sibling;
2275 unsigned long flags;
2276
2277 if (!sas_expander)
2278 return;
2279
2280 list_for_each_entry(mpt2sas_port,
2281 &sas_expander->sas_port_list, port_list) {
2282 if (mpt2sas_port->remote_identify.device_type ==
2283 SAS_END_DEVICE) {
2284 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2285 sas_device =
2286 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2287 mpt2sas_port->remote_identify.sas_address);
2288 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2289 if (!sas_device)
2290 continue;
2291 _scsih_block_io_device(ioc, sas_device->handle);
2292 }
2293 }
2294
2295 list_for_each_entry(mpt2sas_port,
2296 &sas_expander->sas_port_list, port_list) {
2297
2298 if (mpt2sas_port->remote_identify.device_type ==
2299 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2300 mpt2sas_port->remote_identify.device_type ==
2301 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2302
2303 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2304 expander_sibling =
2305 mpt2sas_scsih_expander_find_by_sas_address(
2306 ioc, mpt2sas_port->remote_identify.sas_address);
2307 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2308 _scsih_block_io_to_children_attached_to_ex(ioc,
2309 expander_sibling);
2310 }
2311 }
2312}
2313
2314/**
2315 * _scsih_block_io_to_children_attached_directly
2316 * @ioc: per adapter object
2317 * @event_data: topology change event data
2318 *
2319 * This routine set sdev state to SDEV_BLOCK for all devices
2320 * direct attached during device pull.
2321 */
2322static void
2323_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2324 Mpi2EventDataSasTopologyChangeList_t *event_data)
2325{
2326 int i;
2327 u16 handle;
2328 u16 reason_code;
2329 u8 phy_number;
2330
2331 for (i = 0; i < event_data->NumEntries; i++) {
2332 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2333 if (!handle)
2334 continue;
2335 phy_number = event_data->StartPhyNum + i;
2336 reason_code = event_data->PHY[i].PhyStatus &
2337 MPI2_EVENT_SAS_TOPO_RC_MASK;
2338 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2339 _scsih_block_io_device(ioc, handle);
2340 }
2341}
2342
2343/**
2344 * _scsih_check_topo_delete_events - sanity check on topo events
2345 * @ioc: per adapter object
2346 * @event_data: the event data payload
2347 *
2348 * This routine added to better handle cable breaker.
2349 *
2350 * This handles the case where driver recieves multiple expander
2351 * add and delete events in a single shot. When there is a delete event
2352 * the routine will void any pending add events waiting in the event queue.
2353 *
2354 * Return nothing.
2355 */
2356static void
2357_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2358 Mpi2EventDataSasTopologyChangeList_t *event_data)
2359{
2360 struct fw_event_work *fw_event;
2361 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2362 u16 expander_handle;
2363 struct _sas_node *sas_expander;
2364 unsigned long flags;
2365
2366 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2367 if (expander_handle < ioc->sas_hba.num_phys) {
2368 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2369 return;
2370 }
2371
2372 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2373 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2374 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2375 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2376 expander_handle);
2377 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2378 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2379 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2380 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2381
2382 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2383 return;
2384
2385 /* mark ignore flag for pending events */
2386 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2387 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2388 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2389 fw_event->ignore)
2390 continue;
2391 local_event_data = fw_event->event_data;
2392 if (local_event_data->ExpStatus ==
2393 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2394 local_event_data->ExpStatus ==
2395 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2396 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2397 expander_handle) {
2398 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2399 "setting ignoring flag\n", ioc->name));
2400 fw_event->ignore = 1;
2401 }
2402 }
2403 }
2404 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2405}
2406
2407/**
Eric Moore635374e2009-03-09 01:21:12 -06002408 * _scsih_flush_running_cmds - completing outstanding commands.
2409 * @ioc: per adapter object
2410 *
2411 * The flushing out of all pending scmd commands following host reset,
2412 * where all IO is dropped to the floor.
2413 *
2414 * Return nothing.
2415 */
2416static void
2417_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2418{
2419 struct scsi_cmnd *scmd;
2420 u16 smid;
2421 u16 count = 0;
2422
2423 for (smid = 1; smid <= ioc->request_depth; smid++) {
2424 scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2425 if (!scmd)
2426 continue;
2427 count++;
2428 mpt2sas_base_free_smid(ioc, smid);
2429 scsi_dma_unmap(scmd);
2430 scmd->result = DID_RESET << 16;
2431 scmd->scsi_done(scmd);
2432 }
2433 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2434 ioc->name, count));
2435}
2436
2437/**
Eric Moore3c621b32009-05-18 12:59:41 -06002438 * _scsih_setup_eedp - setup MPI request for EEDP transfer
2439 * @scmd: pointer to scsi command object
2440 * @mpi_request: pointer to the SCSI_IO reqest message frame
2441 *
2442 * Supporting protection 1 and 3.
2443 *
2444 * Returns nothing
2445 */
2446static void
2447_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
2448{
2449 u16 eedp_flags;
2450 unsigned char prot_op = scsi_get_prot_op(scmd);
2451 unsigned char prot_type = scsi_get_prot_type(scmd);
2452
2453 if (prot_type == SCSI_PROT_DIF_TYPE0 ||
2454 prot_type == SCSI_PROT_DIF_TYPE2 ||
2455 prot_op == SCSI_PROT_NORMAL)
2456 return;
2457
2458 if (prot_op == SCSI_PROT_READ_STRIP)
2459 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
2460 else if (prot_op == SCSI_PROT_WRITE_INSERT)
2461 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
2462 else
2463 return;
2464
2465 mpi_request->EEDPBlockSize = scmd->device->sector_size;
2466
2467 switch (prot_type) {
2468 case SCSI_PROT_DIF_TYPE1:
2469
2470 /*
2471 * enable ref/guard checking
2472 * auto increment ref tag
2473 */
2474 mpi_request->EEDPFlags = eedp_flags |
2475 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2476 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2477 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2478 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
2479 cpu_to_be32(scsi_get_lba(scmd));
2480
2481 break;
2482
2483 case SCSI_PROT_DIF_TYPE3:
2484
2485 /*
2486 * enable guard checking
2487 */
2488 mpi_request->EEDPFlags = eedp_flags |
2489 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
2490
2491 break;
2492 }
2493}
2494
2495/**
2496 * _scsih_eedp_error_handling - return sense code for EEDP errors
2497 * @scmd: pointer to scsi command object
2498 * @ioc_status: ioc status
2499 *
2500 * Returns nothing
2501 */
2502static void
2503_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
2504{
2505 u8 ascq;
2506 u8 sk;
2507 u8 host_byte;
2508
2509 switch (ioc_status) {
2510 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2511 ascq = 0x01;
2512 break;
2513 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2514 ascq = 0x02;
2515 break;
2516 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2517 ascq = 0x03;
2518 break;
2519 default:
2520 ascq = 0x00;
2521 break;
2522 }
2523
2524 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2525 sk = ILLEGAL_REQUEST;
2526 host_byte = DID_ABORT;
2527 } else {
2528 sk = ABORTED_COMMAND;
2529 host_byte = DID_OK;
2530 }
2531
2532 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
2533 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
2534 SAM_STAT_CHECK_CONDITION;
2535}
2536
2537/**
Eric Moored5d135b2009-05-18 13:02:08 -06002538 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06002539 * @scmd: pointer to scsi command object
2540 * @done: function pointer to be invoked on completion
2541 *
2542 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2543 *
2544 * Returns 0 on success. If there's a failure, return either:
2545 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2546 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2547 */
2548static int
Eric Moored5d135b2009-05-18 13:02:08 -06002549_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06002550{
2551 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2552 struct MPT2SAS_DEVICE *sas_device_priv_data;
2553 struct MPT2SAS_TARGET *sas_target_priv_data;
2554 Mpi2SCSIIORequest_t *mpi_request;
2555 u32 mpi_control;
2556 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06002557
2558 scmd->scsi_done = done;
2559 sas_device_priv_data = scmd->device->hostdata;
2560 if (!sas_device_priv_data) {
2561 scmd->result = DID_NO_CONNECT << 16;
2562 scmd->scsi_done(scmd);
2563 return 0;
2564 }
2565
2566 sas_target_priv_data = sas_device_priv_data->sas_target;
2567 if (!sas_target_priv_data || sas_target_priv_data->handle ==
2568 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2569 scmd->result = DID_NO_CONNECT << 16;
2570 scmd->scsi_done(scmd);
2571 return 0;
2572 }
2573
2574 /* see if we are busy with task managment stuff */
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302575 if (sas_target_priv_data->tm_busy)
2576 return SCSI_MLQUEUE_DEVICE_BUSY;
2577 else if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06002578 return SCSI_MLQUEUE_HOST_BUSY;
Eric Moore635374e2009-03-09 01:21:12 -06002579
2580 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2581 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2582 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2583 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2584 else
2585 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2586
2587 /* set tags */
2588 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2589 if (scmd->device->tagged_supported) {
2590 if (scmd->device->ordered_tags)
2591 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2592 else
2593 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2594 } else
2595/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2596/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2597 */
2598 mpi_control |= (0x500);
2599
2600 } else
2601 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2602
2603 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2604 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2605
2606 smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
2607 if (!smid) {
2608 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2609 ioc->name, __func__);
2610 goto out;
2611 }
2612 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2613 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06002614 _scsih_setup_eedp(scmd, mpi_request);
Eric Moore635374e2009-03-09 01:21:12 -06002615 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2616 if (sas_device_priv_data->sas_target->flags &
2617 MPT_TARGET_FLAGS_RAID_COMPONENT)
2618 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2619 else
2620 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2621 mpi_request->DevHandle =
2622 cpu_to_le16(sas_device_priv_data->sas_target->handle);
2623 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2624 mpi_request->Control = cpu_to_le32(mpi_control);
2625 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2626 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2627 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2628 mpi_request->SenseBufferLowAddress =
2629 (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2630 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2631 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2632 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
2633
2634 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2635 mpi_request->LUN);
2636 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2637
2638 if (!mpi_request->DataLength) {
2639 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2640 } else {
2641 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2642 mpt2sas_base_free_smid(ioc, smid);
2643 goto out;
2644 }
2645 }
2646
2647 _scsih_scsi_lookup_set(ioc, smid, scmd);
2648 mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
2649 sas_device_priv_data->sas_target->handle);
2650 return 0;
2651
2652 out:
2653 return SCSI_MLQUEUE_HOST_BUSY;
2654}
2655
2656/**
2657 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2658 * @sense_buffer: sense data returned by target
2659 * @data: normalized skey/asc/ascq
2660 *
2661 * Return nothing.
2662 */
2663static void
2664_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2665{
2666 if ((sense_buffer[0] & 0x7F) >= 0x72) {
2667 /* descriptor format */
2668 data->skey = sense_buffer[1] & 0x0F;
2669 data->asc = sense_buffer[2];
2670 data->ascq = sense_buffer[3];
2671 } else {
2672 /* fixed format */
2673 data->skey = sense_buffer[2] & 0x0F;
2674 data->asc = sense_buffer[12];
2675 data->ascq = sense_buffer[13];
2676 }
2677}
2678
2679#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2680/**
2681 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2682 * @ioc: per adapter object
2683 * @scmd: pointer to scsi command object
2684 * @mpi_reply: reply mf payload returned from firmware
2685 *
2686 * scsi_status - SCSI Status code returned from target device
2687 * scsi_state - state info associated with SCSI_IO determined by ioc
2688 * ioc_status - ioc supplied status info
2689 *
2690 * Return nothing.
2691 */
2692static void
2693_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2694 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
2695{
2696 u32 response_info;
2697 u8 *response_bytes;
2698 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
2699 MPI2_IOCSTATUS_MASK;
2700 u8 scsi_state = mpi_reply->SCSIState;
2701 u8 scsi_status = mpi_reply->SCSIStatus;
2702 char *desc_ioc_state = NULL;
2703 char *desc_scsi_status = NULL;
2704 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05302705 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
2706
2707 if (log_info == 0x31170000)
2708 return;
Eric Moore635374e2009-03-09 01:21:12 -06002709
2710 switch (ioc_status) {
2711 case MPI2_IOCSTATUS_SUCCESS:
2712 desc_ioc_state = "success";
2713 break;
2714 case MPI2_IOCSTATUS_INVALID_FUNCTION:
2715 desc_ioc_state = "invalid function";
2716 break;
2717 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2718 desc_ioc_state = "scsi recovered error";
2719 break;
2720 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2721 desc_ioc_state = "scsi invalid dev handle";
2722 break;
2723 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2724 desc_ioc_state = "scsi device not there";
2725 break;
2726 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2727 desc_ioc_state = "scsi data overrun";
2728 break;
2729 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2730 desc_ioc_state = "scsi data underrun";
2731 break;
2732 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2733 desc_ioc_state = "scsi io data error";
2734 break;
2735 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2736 desc_ioc_state = "scsi protocol error";
2737 break;
2738 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2739 desc_ioc_state = "scsi task terminated";
2740 break;
2741 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2742 desc_ioc_state = "scsi residual mismatch";
2743 break;
2744 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2745 desc_ioc_state = "scsi task mgmt failed";
2746 break;
2747 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2748 desc_ioc_state = "scsi ioc terminated";
2749 break;
2750 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2751 desc_ioc_state = "scsi ext terminated";
2752 break;
Eric Moore3c621b32009-05-18 12:59:41 -06002753 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
2754 desc_ioc_state = "eedp guard error";
2755 break;
2756 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
2757 desc_ioc_state = "eedp ref tag error";
2758 break;
2759 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
2760 desc_ioc_state = "eedp app tag error";
2761 break;
Eric Moore635374e2009-03-09 01:21:12 -06002762 default:
2763 desc_ioc_state = "unknown";
2764 break;
2765 }
2766
2767 switch (scsi_status) {
2768 case MPI2_SCSI_STATUS_GOOD:
2769 desc_scsi_status = "good";
2770 break;
2771 case MPI2_SCSI_STATUS_CHECK_CONDITION:
2772 desc_scsi_status = "check condition";
2773 break;
2774 case MPI2_SCSI_STATUS_CONDITION_MET:
2775 desc_scsi_status = "condition met";
2776 break;
2777 case MPI2_SCSI_STATUS_BUSY:
2778 desc_scsi_status = "busy";
2779 break;
2780 case MPI2_SCSI_STATUS_INTERMEDIATE:
2781 desc_scsi_status = "intermediate";
2782 break;
2783 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
2784 desc_scsi_status = "intermediate condmet";
2785 break;
2786 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
2787 desc_scsi_status = "reservation conflict";
2788 break;
2789 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
2790 desc_scsi_status = "command terminated";
2791 break;
2792 case MPI2_SCSI_STATUS_TASK_SET_FULL:
2793 desc_scsi_status = "task set full";
2794 break;
2795 case MPI2_SCSI_STATUS_ACA_ACTIVE:
2796 desc_scsi_status = "aca active";
2797 break;
2798 case MPI2_SCSI_STATUS_TASK_ABORTED:
2799 desc_scsi_status = "task aborted";
2800 break;
2801 default:
2802 desc_scsi_status = "unknown";
2803 break;
2804 }
2805
2806 desc_scsi_state[0] = '\0';
2807 if (!scsi_state)
2808 desc_scsi_state = " ";
2809 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
2810 strcat(desc_scsi_state, "response info ");
2811 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2812 strcat(desc_scsi_state, "state terminated ");
2813 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
2814 strcat(desc_scsi_state, "no status ");
2815 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
2816 strcat(desc_scsi_state, "autosense failed ");
2817 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
2818 strcat(desc_scsi_state, "autosense valid ");
2819
2820 scsi_print_command(scmd);
2821 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
2822 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
2823 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
2824 ioc_status, smid);
2825 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
2826 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
2827 scsi_get_resid(scmd));
2828 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
2829 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
2830 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
2831 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
2832 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
2833 scsi_status, desc_scsi_state, scsi_state);
2834
2835 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2836 struct sense_info data;
2837 _scsih_normalize_sense(scmd->sense_buffer, &data);
2838 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
2839 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
2840 data.asc, data.ascq);
2841 }
2842
2843 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2844 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
2845 response_bytes = (u8 *)&response_info;
2846 _scsih_response_code(ioc, response_bytes[3]);
2847 }
2848}
2849#endif
2850
2851/**
2852 * _scsih_smart_predicted_fault - illuminate Fault LED
2853 * @ioc: per adapter object
2854 * @handle: device handle
2855 *
2856 * Return nothing.
2857 */
2858static void
2859_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2860{
2861 Mpi2SepReply_t mpi_reply;
2862 Mpi2SepRequest_t mpi_request;
2863 struct scsi_target *starget;
2864 struct MPT2SAS_TARGET *sas_target_priv_data;
2865 Mpi2EventNotificationReply_t *event_reply;
2866 Mpi2EventDataSasDeviceStatusChange_t *event_data;
2867 struct _sas_device *sas_device;
2868 ssize_t sz;
2869 unsigned long flags;
2870
2871 /* only handle non-raid devices */
2872 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2873 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2874 if (!sas_device) {
2875 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2876 return;
2877 }
2878 starget = sas_device->starget;
2879 sas_target_priv_data = starget->hostdata;
2880
2881 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
2882 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
2883 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2884 return;
2885 }
2886 starget_printk(KERN_WARNING, starget, "predicted fault\n");
2887 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2888
2889 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
2890 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
2891 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
2892 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
2893 mpi_request.SlotStatus =
2894 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
2895 mpi_request.DevHandle = cpu_to_le16(handle);
2896 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
2897 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
2898 &mpi_request)) != 0) {
2899 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2900 ioc->name, __FILE__, __LINE__, __func__);
2901 return;
2902 }
2903
2904 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
2905 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2906 "enclosure_processor: ioc_status (0x%04x), "
2907 "loginfo(0x%08x)\n", ioc->name,
2908 le16_to_cpu(mpi_reply.IOCStatus),
2909 le32_to_cpu(mpi_reply.IOCLogInfo)));
2910 return;
2911 }
2912 }
2913
2914 /* insert into event log */
2915 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
2916 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
2917 event_reply = kzalloc(sz, GFP_KERNEL);
2918 if (!event_reply) {
2919 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2920 ioc->name, __FILE__, __LINE__, __func__);
2921 return;
2922 }
2923
2924 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2925 event_reply->Event =
2926 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
2927 event_reply->MsgLength = sz/4;
2928 event_reply->EventDataLength =
2929 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
2930 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
2931 event_reply->EventData;
2932 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
2933 event_data->ASC = 0x5D;
2934 event_data->DevHandle = cpu_to_le16(handle);
2935 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
2936 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
2937 kfree(event_reply);
2938}
2939
2940/**
Eric Moored5d135b2009-05-18 13:02:08 -06002941 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06002942 * @ioc: per adapter object
2943 * @smid: system request message index
2944 * @VF_ID: virtual function id
2945 * @reply: reply message frame(lower 32bit addr)
2946 *
2947 * Callback handler when using scsih_qcmd.
2948 *
2949 * Return nothing.
2950 */
2951static void
Eric Moored5d135b2009-05-18 13:02:08 -06002952_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002953{
2954 Mpi2SCSIIORequest_t *mpi_request;
2955 Mpi2SCSIIOReply_t *mpi_reply;
2956 struct scsi_cmnd *scmd;
2957 u16 ioc_status;
2958 u32 xfer_cnt;
2959 u8 scsi_state;
2960 u8 scsi_status;
2961 u32 log_info;
2962 struct MPT2SAS_DEVICE *sas_device_priv_data;
2963 u32 response_code;
2964
2965 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2966 scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2967 if (scmd == NULL)
2968 return;
2969
2970 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2971
2972 if (mpi_reply == NULL) {
2973 scmd->result = DID_OK << 16;
2974 goto out;
2975 }
2976
2977 sas_device_priv_data = scmd->device->hostdata;
2978 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
2979 sas_device_priv_data->sas_target->deleted) {
2980 scmd->result = DID_NO_CONNECT << 16;
2981 goto out;
2982 }
2983
2984 /* turning off TLR */
2985 if (!sas_device_priv_data->tlr_snoop_check) {
2986 sas_device_priv_data->tlr_snoop_check++;
2987 if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
2988 response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
2989 >> 24);
2990 if (response_code ==
2991 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
2992 sas_device_priv_data->flags &=
2993 ~MPT_DEVICE_TLR_ON;
2994 }
2995 }
2996
2997 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
2998 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
2999 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3000 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3001 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3002 else
3003 log_info = 0;
3004 ioc_status &= MPI2_IOCSTATUS_MASK;
3005 scsi_state = mpi_reply->SCSIState;
3006 scsi_status = mpi_reply->SCSIStatus;
3007
3008 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3009 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3010 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3011 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3012 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3013 }
3014
3015 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3016 struct sense_info data;
3017 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3018 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003019 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003020 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003021 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003022 _scsih_normalize_sense(scmd->sense_buffer, &data);
3023 /* failure prediction threshold exceeded */
3024 if (data.asc == 0x5D)
3025 _scsih_smart_predicted_fault(ioc,
3026 le16_to_cpu(mpi_reply->DevHandle));
3027 }
3028
3029 switch (ioc_status) {
3030 case MPI2_IOCSTATUS_BUSY:
3031 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3032 scmd->result = SAM_STAT_BUSY;
3033 break;
3034
3035 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3036 scmd->result = DID_NO_CONNECT << 16;
3037 break;
3038
3039 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3040 if (sas_device_priv_data->block) {
3041 scmd->result = (DID_BUS_BUSY << 16);
3042 break;
3043 }
3044
3045 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3046 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3047 scmd->result = DID_RESET << 16;
3048 break;
3049
3050 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3051 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3052 scmd->result = DID_SOFT_ERROR << 16;
3053 else
3054 scmd->result = (DID_OK << 16) | scsi_status;
3055 break;
3056
3057 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3058 scmd->result = (DID_OK << 16) | scsi_status;
3059
3060 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3061 break;
3062
3063 if (xfer_cnt < scmd->underflow) {
3064 if (scsi_status == SAM_STAT_BUSY)
3065 scmd->result = SAM_STAT_BUSY;
3066 else
3067 scmd->result = DID_SOFT_ERROR << 16;
3068 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3069 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3070 scmd->result = DID_SOFT_ERROR << 16;
3071 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3072 scmd->result = DID_RESET << 16;
3073 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3074 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3075 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3076 scmd->result = (DRIVER_SENSE << 24) |
3077 SAM_STAT_CHECK_CONDITION;
3078 scmd->sense_buffer[0] = 0x70;
3079 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3080 scmd->sense_buffer[12] = 0x20;
3081 scmd->sense_buffer[13] = 0;
3082 }
3083 break;
3084
3085 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3086 scsi_set_resid(scmd, 0);
3087 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3088 case MPI2_IOCSTATUS_SUCCESS:
3089 scmd->result = (DID_OK << 16) | scsi_status;
3090 if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3091 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3092 scmd->result = DID_SOFT_ERROR << 16;
3093 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3094 scmd->result = DID_RESET << 16;
3095 break;
3096
Eric Moore3c621b32009-05-18 12:59:41 -06003097 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3098 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3099 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3100 _scsih_eedp_error_handling(scmd, ioc_status);
3101 break;
Eric Moore635374e2009-03-09 01:21:12 -06003102 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3103 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3104 case MPI2_IOCSTATUS_INVALID_SGL:
3105 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3106 case MPI2_IOCSTATUS_INVALID_FIELD:
3107 case MPI2_IOCSTATUS_INVALID_STATE:
3108 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3109 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3110 default:
3111 scmd->result = DID_SOFT_ERROR << 16;
3112 break;
3113
3114 }
3115
3116#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3117 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3118 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3119#endif
3120
3121 out:
3122 scsi_dma_unmap(scmd);
3123 scmd->scsi_done(scmd);
3124}
3125
3126/**
Eric Moore635374e2009-03-09 01:21:12 -06003127 * _scsih_sas_host_refresh - refreshing sas host object contents
3128 * @ioc: per adapter object
3129 * @update: update link information
3130 * Context: user
3131 *
3132 * During port enable, fw will send topology events for every device. Its
3133 * possible that the handles may change from the previous setting, so this
3134 * code keeping handles updating if changed.
3135 *
3136 * Return nothing.
3137 */
3138static void
3139_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
3140{
3141 u16 sz;
3142 u16 ioc_status;
3143 int i;
3144 Mpi2ConfigReply_t mpi_reply;
3145 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3146
3147 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3148 "updating handles for sas_host(0x%016llx)\n",
3149 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3150
3151 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3152 * sizeof(Mpi2SasIOUnit0PhyData_t));
3153 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3154 if (!sas_iounit_pg0) {
3155 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3156 ioc->name, __FILE__, __LINE__, __func__);
3157 return;
3158 }
3159 if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3160 sas_iounit_pg0, sz))) {
3161 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3162 MPI2_IOCSTATUS_MASK;
3163 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3164 goto out;
3165 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3166 ioc->sas_hba.phy[i].handle =
3167 le16_to_cpu(sas_iounit_pg0->PhyData[i].
3168 ControllerDevHandle);
3169 if (update)
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05303170 mpt2sas_transport_update_links(
3171 ioc,
Eric Moore635374e2009-03-09 01:21:12 -06003172 ioc->sas_hba.phy[i].handle,
3173 le16_to_cpu(sas_iounit_pg0->PhyData[i].
3174 AttachedDevHandle), i,
3175 sas_iounit_pg0->PhyData[i].
3176 NegotiatedLinkRate >> 4);
3177 }
3178 }
3179
3180 out:
3181 kfree(sas_iounit_pg0);
3182}
3183
3184/**
3185 * _scsih_sas_host_add - create sas host object
3186 * @ioc: per adapter object
3187 *
3188 * Creating host side data object, stored in ioc->sas_hba
3189 *
3190 * Return nothing.
3191 */
3192static void
3193_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3194{
3195 int i;
3196 Mpi2ConfigReply_t mpi_reply;
3197 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3198 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3199 Mpi2SasPhyPage0_t phy_pg0;
3200 Mpi2SasDevicePage0_t sas_device_pg0;
3201 Mpi2SasEnclosurePage0_t enclosure_pg0;
3202 u16 ioc_status;
3203 u16 sz;
3204 u16 device_missing_delay;
3205
3206 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3207 if (!ioc->sas_hba.num_phys) {
3208 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3209 ioc->name, __FILE__, __LINE__, __func__);
3210 return;
3211 }
3212
3213 /* sas_iounit page 0 */
3214 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3215 sizeof(Mpi2SasIOUnit0PhyData_t));
3216 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3217 if (!sas_iounit_pg0) {
3218 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3219 ioc->name, __FILE__, __LINE__, __func__);
3220 return;
3221 }
3222 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3223 sas_iounit_pg0, sz))) {
3224 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3225 ioc->name, __FILE__, __LINE__, __func__);
3226 goto out;
3227 }
3228 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3229 MPI2_IOCSTATUS_MASK;
3230 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3231 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3232 ioc->name, __FILE__, __LINE__, __func__);
3233 goto out;
3234 }
3235
3236 /* sas_iounit page 1 */
3237 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3238 sizeof(Mpi2SasIOUnit1PhyData_t));
3239 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3240 if (!sas_iounit_pg1) {
3241 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3242 ioc->name, __FILE__, __LINE__, __func__);
3243 goto out;
3244 }
3245 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3246 sas_iounit_pg1, sz))) {
3247 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3248 ioc->name, __FILE__, __LINE__, __func__);
3249 goto out;
3250 }
3251 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3252 MPI2_IOCSTATUS_MASK;
3253 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3254 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3255 ioc->name, __FILE__, __LINE__, __func__);
3256 goto out;
3257 }
3258
3259 ioc->io_missing_delay =
3260 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3261 device_missing_delay =
3262 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3263 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3264 ioc->device_missing_delay = (device_missing_delay &
3265 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3266 else
3267 ioc->device_missing_delay = device_missing_delay &
3268 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3269
3270 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3271 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3272 sizeof(struct _sas_phy), GFP_KERNEL);
3273 if (!ioc->sas_hba.phy) {
3274 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3275 ioc->name, __FILE__, __LINE__, __func__);
3276 goto out;
3277 }
3278 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3279 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3280 i))) {
3281 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3282 ioc->name, __FILE__, __LINE__, __func__);
3283 goto out;
3284 }
3285 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3286 MPI2_IOCSTATUS_MASK;
3287 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3288 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3289 ioc->name, __FILE__, __LINE__, __func__);
3290 goto out;
3291 }
3292 ioc->sas_hba.phy[i].handle =
3293 le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
3294 ioc->sas_hba.phy[i].phy_id = i;
3295 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3296 phy_pg0, ioc->sas_hba.parent_dev);
3297 }
3298 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3299 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
3300 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3301 ioc->name, __FILE__, __LINE__, __func__);
3302 goto out;
3303 }
3304 ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
3305 ioc->sas_hba.enclosure_handle =
3306 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3307 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3308 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3309 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3310 (unsigned long long) ioc->sas_hba.sas_address,
3311 ioc->sas_hba.num_phys) ;
3312
3313 if (ioc->sas_hba.enclosure_handle) {
3314 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3315 &enclosure_pg0,
3316 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3317 ioc->sas_hba.enclosure_handle))) {
3318 ioc->sas_hba.enclosure_logical_id =
3319 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3320 }
3321 }
3322
3323 out:
3324 kfree(sas_iounit_pg1);
3325 kfree(sas_iounit_pg0);
3326}
3327
3328/**
3329 * _scsih_expander_add - creating expander object
3330 * @ioc: per adapter object
3331 * @handle: expander handle
3332 *
3333 * Creating expander object, stored in ioc->sas_expander_list.
3334 *
3335 * Return 0 for success, else error.
3336 */
3337static int
3338_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3339{
3340 struct _sas_node *sas_expander;
3341 Mpi2ConfigReply_t mpi_reply;
3342 Mpi2ExpanderPage0_t expander_pg0;
3343 Mpi2ExpanderPage1_t expander_pg1;
3344 Mpi2SasEnclosurePage0_t enclosure_pg0;
3345 u32 ioc_status;
3346 u16 parent_handle;
3347 __le64 sas_address;
3348 int i;
3349 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303350 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003351 int rc = 0;
3352
3353 if (!handle)
3354 return -1;
3355
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303356 if (ioc->shost_recovery)
3357 return -1;
3358
Eric Moore635374e2009-03-09 01:21:12 -06003359 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3360 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3361 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3362 ioc->name, __FILE__, __LINE__, __func__);
3363 return -1;
3364 }
3365
3366 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3367 MPI2_IOCSTATUS_MASK;
3368 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3369 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3370 ioc->name, __FILE__, __LINE__, __func__);
3371 return -1;
3372 }
3373
3374 /* handle out of order topology events */
3375 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
3376 if (parent_handle >= ioc->sas_hba.num_phys) {
3377 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3378 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3379 parent_handle);
3380 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3381 if (!sas_expander) {
3382 rc = _scsih_expander_add(ioc, parent_handle);
3383 if (rc != 0)
3384 return rc;
3385 }
3386 }
3387
3388 sas_address = le64_to_cpu(expander_pg0.SASAddress);
3389
3390 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3391 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3392 sas_address);
3393 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3394
3395 if (sas_expander)
3396 return 0;
3397
3398 sas_expander = kzalloc(sizeof(struct _sas_node),
3399 GFP_KERNEL);
3400 if (!sas_expander) {
3401 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3402 ioc->name, __FILE__, __LINE__, __func__);
3403 return -1;
3404 }
3405
3406 sas_expander->handle = handle;
3407 sas_expander->num_phys = expander_pg0.NumPhys;
3408 sas_expander->parent_handle = parent_handle;
3409 sas_expander->enclosure_handle =
3410 le16_to_cpu(expander_pg0.EnclosureHandle);
3411 sas_expander->sas_address = sas_address;
3412
3413 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3414 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
3415 handle, sas_expander->parent_handle, (unsigned long long)
3416 sas_expander->sas_address, sas_expander->num_phys);
3417
3418 if (!sas_expander->num_phys)
3419 goto out_fail;
3420 sas_expander->phy = kcalloc(sas_expander->num_phys,
3421 sizeof(struct _sas_phy), GFP_KERNEL);
3422 if (!sas_expander->phy) {
3423 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3424 ioc->name, __FILE__, __LINE__, __func__);
3425 rc = -1;
3426 goto out_fail;
3427 }
3428
3429 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3430 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
3431 sas_expander->parent_handle);
3432 if (!mpt2sas_port) {
3433 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3434 ioc->name, __FILE__, __LINE__, __func__);
3435 rc = -1;
3436 goto out_fail;
3437 }
3438 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3439
3440 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3441 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3442 &expander_pg1, i, handle))) {
3443 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3444 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05303445 rc = -1;
3446 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06003447 }
3448 sas_expander->phy[i].handle = handle;
3449 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05303450
3451 if ((mpt2sas_transport_add_expander_phy(ioc,
3452 &sas_expander->phy[i], expander_pg1,
3453 sas_expander->parent_dev))) {
3454 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3455 ioc->name, __FILE__, __LINE__, __func__);
3456 rc = -1;
3457 goto out_fail;
3458 }
Eric Moore635374e2009-03-09 01:21:12 -06003459 }
3460
3461 if (sas_expander->enclosure_handle) {
3462 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3463 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3464 sas_expander->enclosure_handle))) {
3465 sas_expander->enclosure_logical_id =
3466 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3467 }
3468 }
3469
3470 _scsih_expander_node_add(ioc, sas_expander);
3471 return 0;
3472
3473 out_fail:
3474
Kashyap, Desai20f58952009-08-07 19:34:26 +05303475 if (mpt2sas_port)
3476 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
3477 sas_expander->parent_handle);
Eric Moore635374e2009-03-09 01:21:12 -06003478 kfree(sas_expander);
3479 return rc;
3480}
3481
3482/**
3483 * _scsih_expander_remove - removing expander object
3484 * @ioc: per adapter object
3485 * @handle: expander handle
3486 *
3487 * Return nothing.
3488 */
3489static void
3490_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3491{
3492 struct _sas_node *sas_expander;
3493 unsigned long flags;
3494
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303495 if (ioc->shost_recovery)
3496 return;
3497
Eric Moore635374e2009-03-09 01:21:12 -06003498 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3499 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
3500 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3501 _scsih_expander_node_remove(ioc, sas_expander);
3502}
3503
3504/**
3505 * _scsih_add_device - creating sas device object
3506 * @ioc: per adapter object
3507 * @handle: sas device handle
3508 * @phy_num: phy number end device attached to
3509 * @is_pd: is this hidden raid component
3510 *
3511 * Creating end device object, stored in ioc->sas_device_list.
3512 *
3513 * Returns 0 for success, non-zero for failure.
3514 */
3515static int
3516_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3517{
3518 Mpi2ConfigReply_t mpi_reply;
3519 Mpi2SasDevicePage0_t sas_device_pg0;
3520 Mpi2SasEnclosurePage0_t enclosure_pg0;
3521 struct _sas_device *sas_device;
3522 u32 ioc_status;
3523 __le64 sas_address;
3524 u32 device_info;
3525 unsigned long flags;
3526
3527 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3528 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3529 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3530 ioc->name, __FILE__, __LINE__, __func__);
3531 return -1;
3532 }
3533
3534 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3535 MPI2_IOCSTATUS_MASK;
3536 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3537 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3538 ioc->name, __FILE__, __LINE__, __func__);
3539 return -1;
3540 }
3541
3542 /* check if device is present */
3543 if (!(le16_to_cpu(sas_device_pg0.Flags) &
3544 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3545 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3546 ioc->name, __FILE__, __LINE__, __func__);
3547 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3548 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3549 return -1;
3550 }
3551
3552 /* check if there were any issus with discovery */
3553 if (sas_device_pg0.AccessStatus ==
3554 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3555 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3556 ioc->name, __FILE__, __LINE__, __func__);
3557 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3558 ioc->name, sas_device_pg0.AccessStatus);
3559 return -1;
3560 }
3561
3562 /* check if this is end device */
3563 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3564 if (!(_scsih_is_end_device(device_info))) {
3565 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3566 ioc->name, __FILE__, __LINE__, __func__);
3567 return -1;
3568 }
3569
3570 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3571
3572 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3573 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3574 sas_address);
3575 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3576
3577 if (sas_device) {
3578 _scsih_ublock_io_device(ioc, handle);
3579 return 0;
3580 }
3581
3582 sas_device = kzalloc(sizeof(struct _sas_device),
3583 GFP_KERNEL);
3584 if (!sas_device) {
3585 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3586 ioc->name, __FILE__, __LINE__, __func__);
3587 return -1;
3588 }
3589
3590 sas_device->handle = handle;
3591 sas_device->parent_handle =
3592 le16_to_cpu(sas_device_pg0.ParentDevHandle);
3593 sas_device->enclosure_handle =
3594 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3595 sas_device->slot =
3596 le16_to_cpu(sas_device_pg0.Slot);
3597 sas_device->device_info = device_info;
3598 sas_device->sas_address = sas_address;
3599 sas_device->hidden_raid_component = is_pd;
3600
3601 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05303602 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
3603 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3604 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06003605 sas_device->enclosure_logical_id =
3606 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06003607
3608 /* get device name */
3609 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3610
3611 if (ioc->wait_for_port_enable_to_complete)
3612 _scsih_sas_device_init_add(ioc, sas_device);
3613 else
3614 _scsih_sas_device_add(ioc, sas_device);
3615
3616 return 0;
3617}
3618
3619/**
3620 * _scsih_remove_device - removing sas device object
3621 * @ioc: per adapter object
3622 * @handle: sas device handle
3623 *
3624 * Return nothing.
3625 */
3626static void
3627_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3628{
3629 struct MPT2SAS_TARGET *sas_target_priv_data;
3630 struct _sas_device *sas_device;
3631 unsigned long flags;
3632 Mpi2SasIoUnitControlReply_t mpi_reply;
3633 Mpi2SasIoUnitControlRequest_t mpi_request;
3634 u16 device_handle;
3635
3636 /* lookup sas_device */
3637 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3638 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3639 if (!sas_device) {
3640 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3641 return;
3642 }
3643
3644 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
3645 "(0x%04x)\n", ioc->name, __func__, handle));
3646
3647 if (sas_device->starget && sas_device->starget->hostdata) {
3648 sas_target_priv_data = sas_device->starget->hostdata;
3649 sas_target_priv_data->deleted = 1;
3650 }
3651 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3652
3653 if (ioc->remove_host)
3654 goto out;
3655
3656 /* Target Reset to flush out all the outstanding IO */
3657 device_handle = (sas_device->hidden_raid_component) ?
3658 sas_device->volume_handle : handle;
3659 if (device_handle) {
3660 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3661 "handle(0x%04x)\n", ioc->name, device_handle));
3662 mutex_lock(&ioc->tm_cmds.mutex);
3663 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3664 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3665 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3666 mutex_unlock(&ioc->tm_cmds.mutex);
3667 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3668 "done: handle(0x%04x)\n", ioc->name, device_handle));
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303669 if (ioc->shost_recovery)
3670 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003671 }
3672
3673 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
3674 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
3675 "(0x%04x)\n", ioc->name, handle));
3676 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3677 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3678 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3679 mpi_request.DevHandle = handle;
3680 mpi_request.VF_ID = 0;
3681 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
3682 &mpi_request)) != 0) {
3683 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3684 ioc->name, __FILE__, __LINE__, __func__);
3685 }
3686
3687 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
3688 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
3689 le16_to_cpu(mpi_reply.IOCStatus),
3690 le32_to_cpu(mpi_reply.IOCLogInfo)));
3691
3692 out:
3693 mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
3694 sas_device->parent_handle);
3695
3696 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
3697 "(0x%016llx)\n", ioc->name, sas_device->handle,
3698 (unsigned long long) sas_device->sas_address);
3699 _scsih_sas_device_remove(ioc, sas_device);
3700
3701 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
3702 "(0x%04x)\n", ioc->name, __func__, handle));
3703}
3704
3705#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3706/**
3707 * _scsih_sas_topology_change_event_debug - debug for topology event
3708 * @ioc: per adapter object
3709 * @event_data: event data payload
3710 * Context: user.
3711 */
3712static void
3713_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3714 Mpi2EventDataSasTopologyChangeList_t *event_data)
3715{
3716 int i;
3717 u16 handle;
3718 u16 reason_code;
3719 u8 phy_number;
3720 char *status_str = NULL;
3721 char link_rate[25];
3722
3723 switch (event_data->ExpStatus) {
3724 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
3725 status_str = "add";
3726 break;
3727 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
3728 status_str = "remove";
3729 break;
3730 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
3731 status_str = "responding";
3732 break;
3733 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
3734 status_str = "remove delay";
3735 break;
3736 default:
3737 status_str = "unknown status";
3738 break;
3739 }
3740 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
3741 ioc->name, status_str);
3742 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
3743 "start_phy(%02d), count(%d)\n",
3744 le16_to_cpu(event_data->ExpanderDevHandle),
3745 le16_to_cpu(event_data->EnclosureHandle),
3746 event_data->StartPhyNum, event_data->NumEntries);
3747 for (i = 0; i < event_data->NumEntries; i++) {
3748 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3749 if (!handle)
3750 continue;
3751 phy_number = event_data->StartPhyNum + i;
3752 reason_code = event_data->PHY[i].PhyStatus &
3753 MPI2_EVENT_SAS_TOPO_RC_MASK;
3754 switch (reason_code) {
3755 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3756 snprintf(link_rate, 25, ": add, link(0x%02x)",
3757 (event_data->PHY[i].LinkRate >> 4));
3758 status_str = link_rate;
3759 break;
3760 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3761 status_str = ": remove";
3762 break;
3763 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
3764 status_str = ": remove_delay";
3765 break;
3766 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3767 snprintf(link_rate, 25, ": link(0x%02x)",
3768 (event_data->PHY[i].LinkRate >> 4));
3769 status_str = link_rate;
3770 break;
3771 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
3772 status_str = ": responding";
3773 break;
3774 default:
3775 status_str = ": unknown";
3776 break;
3777 }
3778 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
3779 phy_number, handle, status_str);
3780 }
3781}
3782#endif
3783
3784/**
3785 * _scsih_sas_topology_change_event - handle topology changes
3786 * @ioc: per adapter object
3787 * @VF_ID:
3788 * @event_data: event data payload
3789 * fw_event:
3790 * Context: user.
3791 *
3792 */
3793static void
3794_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3795 Mpi2EventDataSasTopologyChangeList_t *event_data,
3796 struct fw_event_work *fw_event)
3797{
3798 int i;
3799 u16 parent_handle, handle;
3800 u16 reason_code;
3801 u8 phy_number;
3802 struct _sas_node *sas_expander;
3803 unsigned long flags;
3804 u8 link_rate_;
3805
3806#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3807 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3808 _scsih_sas_topology_change_event_debug(ioc, event_data);
3809#endif
3810
3811 if (!ioc->sas_hba.num_phys)
3812 _scsih_sas_host_add(ioc);
3813 else
3814 _scsih_sas_host_refresh(ioc, 0);
3815
3816 if (fw_event->ignore) {
3817 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
3818 "event\n", ioc->name));
3819 return;
3820 }
3821
3822 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3823
3824 /* handle expander add */
3825 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
3826 if (_scsih_expander_add(ioc, parent_handle) != 0)
3827 return;
3828
3829 /* handle siblings events */
3830 for (i = 0; i < event_data->NumEntries; i++) {
3831 if (fw_event->ignore) {
3832 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
3833 "expander event\n", ioc->name));
3834 return;
3835 }
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303836 if (ioc->shost_recovery)
3837 return;
Eric Moore635374e2009-03-09 01:21:12 -06003838 if (event_data->PHY[i].PhyStatus &
3839 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
3840 continue;
3841 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3842 if (!handle)
3843 continue;
3844 phy_number = event_data->StartPhyNum + i;
3845 reason_code = event_data->PHY[i].PhyStatus &
3846 MPI2_EVENT_SAS_TOPO_RC_MASK;
3847 link_rate_ = event_data->PHY[i].LinkRate >> 4;
3848 switch (reason_code) {
3849 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3850 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3851 if (!parent_handle) {
3852 if (phy_number < ioc->sas_hba.num_phys)
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05303853 mpt2sas_transport_update_links(
3854 ioc,
3855 ioc->sas_hba.phy[phy_number].handle,
3856 handle, phy_number, link_rate_);
Eric Moore635374e2009-03-09 01:21:12 -06003857 } else {
3858 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3859 sas_expander =
3860 mpt2sas_scsih_expander_find_by_handle(ioc,
3861 parent_handle);
3862 spin_unlock_irqrestore(&ioc->sas_node_lock,
3863 flags);
3864 if (sas_expander) {
3865 if (phy_number < sas_expander->num_phys)
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05303866 mpt2sas_transport_update_links(
3867 ioc,
3868 sas_expander->
3869 phy[phy_number].handle,
3870 handle, phy_number,
3871 link_rate_);
Eric Moore635374e2009-03-09 01:21:12 -06003872 }
3873 }
3874 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
3875 if (link_rate_ >= MPI2_SAS_NEG_LINK_RATE_1_5)
3876 _scsih_ublock_io_device(ioc, handle);
3877 }
3878 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
3879 if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
3880 break;
3881 _scsih_add_device(ioc, handle, phy_number, 0);
3882 }
3883 break;
3884 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3885 _scsih_remove_device(ioc, handle);
3886 break;
3887 }
3888 }
3889
3890 /* handle expander removal */
3891 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3892 _scsih_expander_remove(ioc, parent_handle);
3893
3894}
3895
3896#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3897/**
3898 * _scsih_sas_device_status_change_event_debug - debug for device event
3899 * @event_data: event data payload
3900 * Context: user.
3901 *
3902 * Return nothing.
3903 */
3904static void
3905_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3906 Mpi2EventDataSasDeviceStatusChange_t *event_data)
3907{
3908 char *reason_str = NULL;
3909
3910 switch (event_data->ReasonCode) {
3911 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
3912 reason_str = "smart data";
3913 break;
3914 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
3915 reason_str = "unsupported device discovered";
3916 break;
3917 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
3918 reason_str = "internal device reset";
3919 break;
3920 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
3921 reason_str = "internal task abort";
3922 break;
3923 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
3924 reason_str = "internal task abort set";
3925 break;
3926 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
3927 reason_str = "internal clear task set";
3928 break;
3929 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
3930 reason_str = "internal query task";
3931 break;
3932 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
3933 reason_str = "sata init failure";
3934 break;
3935 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
3936 reason_str = "internal device reset complete";
3937 break;
3938 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
3939 reason_str = "internal task abort complete";
3940 break;
3941 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
3942 reason_str = "internal async notification";
3943 break;
3944 default:
3945 reason_str = "unknown reason";
3946 break;
3947 }
3948 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
3949 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
3950 reason_str, le16_to_cpu(event_data->DevHandle),
3951 (unsigned long long)le64_to_cpu(event_data->SASAddress));
3952 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
3953 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
3954 event_data->ASC, event_data->ASCQ);
3955 printk(KERN_INFO "\n");
3956}
3957#endif
3958
3959/**
3960 * _scsih_sas_device_status_change_event - handle device status change
3961 * @ioc: per adapter object
3962 * @VF_ID:
3963 * @event_data: event data payload
3964 * Context: user.
3965 *
3966 * Return nothing.
3967 */
3968static void
3969_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3970 Mpi2EventDataSasDeviceStatusChange_t *event_data)
3971{
3972#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3973 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3974 _scsih_sas_device_status_change_event_debug(ioc, event_data);
3975#endif
3976}
3977
3978#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3979/**
3980 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
3981 * @ioc: per adapter object
3982 * @event_data: event data payload
3983 * Context: user.
3984 *
3985 * Return nothing.
3986 */
3987static void
3988_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3989 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
3990{
3991 char *reason_str = NULL;
3992
3993 switch (event_data->ReasonCode) {
3994 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
3995 reason_str = "enclosure add";
3996 break;
3997 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
3998 reason_str = "enclosure remove";
3999 break;
4000 default:
4001 reason_str = "unknown reason";
4002 break;
4003 }
4004
4005 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
4006 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4007 " number slots(%d)\n", ioc->name, reason_str,
4008 le16_to_cpu(event_data->EnclosureHandle),
4009 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4010 le16_to_cpu(event_data->StartSlot));
4011}
4012#endif
4013
4014/**
4015 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4016 * @ioc: per adapter object
4017 * @VF_ID:
4018 * @event_data: event data payload
4019 * Context: user.
4020 *
4021 * Return nothing.
4022 */
4023static void
4024_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4025 u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4026{
4027#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4028 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4029 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
4030 event_data);
4031#endif
4032}
4033
4034/**
4035 * _scsih_sas_broadcast_primative_event - handle broadcast events
4036 * @ioc: per adapter object
4037 * @event_data: event data payload
4038 * Context: user.
4039 *
4040 * Return nothing.
4041 */
4042static void
4043_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4044 Mpi2EventDataSasBroadcastPrimitive_t *event_data)
4045{
4046 struct scsi_cmnd *scmd;
4047 u16 smid, handle;
4048 u32 lun;
4049 struct MPT2SAS_DEVICE *sas_device_priv_data;
4050 u32 termination_count;
4051 u32 query_count;
4052 Mpi2SCSITaskManagementReply_t *mpi_reply;
4053
4054 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
4055 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4056 event_data->PortWidth));
4057
4058 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
4059 __func__));
4060
4061 mutex_lock(&ioc->tm_cmds.mutex);
4062 termination_count = 0;
4063 query_count = 0;
4064 mpi_reply = ioc->tm_cmds.reply;
4065 for (smid = 1; smid <= ioc->request_depth; smid++) {
4066 scmd = _scsih_scsi_lookup_get(ioc, smid);
4067 if (!scmd)
4068 continue;
4069 sas_device_priv_data = scmd->device->hostdata;
4070 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4071 continue;
4072 /* skip hidden raid components */
4073 if (sas_device_priv_data->sas_target->flags &
4074 MPT_TARGET_FLAGS_RAID_COMPONENT)
4075 continue;
4076 /* skip volumes */
4077 if (sas_device_priv_data->sas_target->flags &
4078 MPT_TARGET_FLAGS_VOLUME)
4079 continue;
4080
4081 handle = sas_device_priv_data->sas_target->handle;
4082 lun = sas_device_priv_data->lun;
4083 query_count++;
4084
4085 mpt2sas_scsih_issue_tm(ioc, handle, lun,
4086 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
Eric Moore8901cbb2009-04-21 15:41:32 -06004087 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06004088
4089 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
4090 (mpi_reply->ResponseCode ==
4091 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
4092 mpi_reply->ResponseCode ==
4093 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
4094 continue;
4095
4096 mpt2sas_scsih_issue_tm(ioc, handle, lun,
Eric Moore8901cbb2009-04-21 15:41:32 -06004097 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30);
4098 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06004099 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
4100 }
Eric Moore635374e2009-03-09 01:21:12 -06004101 ioc->broadcast_aen_busy = 0;
4102 mutex_unlock(&ioc->tm_cmds.mutex);
4103
4104 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
4105 "%s - exit, query_count = %d termination_count = %d\n",
4106 ioc->name, __func__, query_count, termination_count));
4107}
4108
4109/**
4110 * _scsih_sas_discovery_event - handle discovery events
4111 * @ioc: per adapter object
4112 * @event_data: event data payload
4113 * Context: user.
4114 *
4115 * Return nothing.
4116 */
4117static void
4118_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4119 Mpi2EventDataSasDiscovery_t *event_data)
4120{
4121#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4122 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
4123 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
4124 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
4125 "start" : "stop");
4126 if (event_data->DiscoveryStatus)
4127 printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
4128 ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
4129 printk("\n");
4130 }
4131#endif
4132
4133 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
4134 !ioc->sas_hba.num_phys)
4135 _scsih_sas_host_add(ioc);
4136}
4137
4138/**
4139 * _scsih_reprobe_lun - reprobing lun
4140 * @sdev: scsi device struct
4141 * @no_uld_attach: sdev->no_uld_attach flag setting
4142 *
4143 **/
4144static void
4145_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
4146{
4147 int rc;
4148
4149 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
4150 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
4151 sdev->no_uld_attach ? "hidding" : "exposing");
4152 rc = scsi_device_reprobe(sdev);
4153}
4154
4155/**
4156 * _scsih_reprobe_target - reprobing target
4157 * @starget: scsi target struct
4158 * @no_uld_attach: sdev->no_uld_attach flag setting
4159 *
4160 * Note: no_uld_attach flag determines whether the disk device is attached
4161 * to block layer. A value of `1` means to not attach.
4162 **/
4163static void
4164_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4165{
4166 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4167
4168 if (no_uld_attach)
4169 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4170 else
4171 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4172
4173 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4174 _scsih_reprobe_lun);
4175}
4176/**
4177 * _scsih_sas_volume_add - add new volume
4178 * @ioc: per adapter object
4179 * @element: IR config element data
4180 * Context: user.
4181 *
4182 * Return nothing.
4183 */
4184static void
4185_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4186 Mpi2EventIrConfigElement_t *element)
4187{
4188 struct _raid_device *raid_device;
4189 unsigned long flags;
4190 u64 wwid;
4191 u16 handle = le16_to_cpu(element->VolDevHandle);
4192 int rc;
4193
Eric Moore635374e2009-03-09 01:21:12 -06004194 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4195 if (!wwid) {
4196 printk(MPT2SAS_ERR_FMT
4197 "failure at %s:%d/%s()!\n", ioc->name,
4198 __FILE__, __LINE__, __func__);
4199 return;
4200 }
4201
4202 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4203 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4204 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4205
4206 if (raid_device)
4207 return;
4208
4209 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4210 if (!raid_device) {
4211 printk(MPT2SAS_ERR_FMT
4212 "failure at %s:%d/%s()!\n", ioc->name,
4213 __FILE__, __LINE__, __func__);
4214 return;
4215 }
4216
4217 raid_device->id = ioc->sas_id++;
4218 raid_device->channel = RAID_CHANNEL;
4219 raid_device->handle = handle;
4220 raid_device->wwid = wwid;
4221 _scsih_raid_device_add(ioc, raid_device);
4222 if (!ioc->wait_for_port_enable_to_complete) {
4223 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4224 raid_device->id, 0);
4225 if (rc)
4226 _scsih_raid_device_remove(ioc, raid_device);
4227 } else
4228 _scsih_determine_boot_device(ioc, raid_device, 1);
4229}
4230
4231/**
4232 * _scsih_sas_volume_delete - delete volume
4233 * @ioc: per adapter object
4234 * @element: IR config element data
4235 * Context: user.
4236 *
4237 * Return nothing.
4238 */
4239static void
4240_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4241 Mpi2EventIrConfigElement_t *element)
4242{
4243 struct _raid_device *raid_device;
4244 u16 handle = le16_to_cpu(element->VolDevHandle);
4245 unsigned long flags;
4246 struct MPT2SAS_TARGET *sas_target_priv_data;
4247
Eric Moore635374e2009-03-09 01:21:12 -06004248 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4249 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4250 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4251 if (!raid_device)
4252 return;
4253 if (raid_device->starget) {
4254 sas_target_priv_data = raid_device->starget->hostdata;
4255 sas_target_priv_data->deleted = 1;
4256 scsi_remove_target(&raid_device->starget->dev);
4257 }
4258 _scsih_raid_device_remove(ioc, raid_device);
4259}
4260
4261/**
4262 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4263 * @ioc: per adapter object
4264 * @element: IR config element data
4265 * Context: user.
4266 *
4267 * Return nothing.
4268 */
4269static void
4270_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4271 Mpi2EventIrConfigElement_t *element)
4272{
4273 struct _sas_device *sas_device;
4274 unsigned long flags;
4275 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4276
4277 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4278 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4279 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4280 if (!sas_device)
4281 return;
4282
4283 /* exposing raid component */
4284 sas_device->volume_handle = 0;
4285 sas_device->volume_wwid = 0;
4286 sas_device->hidden_raid_component = 0;
4287 _scsih_reprobe_target(sas_device->starget, 0);
4288}
4289
4290/**
4291 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4292 * @ioc: per adapter object
4293 * @element: IR config element data
4294 * Context: user.
4295 *
4296 * Return nothing.
4297 */
4298static void
4299_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4300 Mpi2EventIrConfigElement_t *element)
4301{
4302 struct _sas_device *sas_device;
4303 unsigned long flags;
4304 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4305
4306 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4307 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4308 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4309 if (!sas_device)
4310 return;
4311
4312 /* hiding raid component */
4313 mpt2sas_config_get_volume_handle(ioc, handle,
4314 &sas_device->volume_handle);
4315 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4316 &sas_device->volume_wwid);
4317 sas_device->hidden_raid_component = 1;
4318 _scsih_reprobe_target(sas_device->starget, 1);
4319}
4320
4321/**
4322 * _scsih_sas_pd_delete - delete pd component
4323 * @ioc: per adapter object
4324 * @element: IR config element data
4325 * Context: user.
4326 *
4327 * Return nothing.
4328 */
4329static void
4330_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4331 Mpi2EventIrConfigElement_t *element)
4332{
4333 struct _sas_device *sas_device;
4334 unsigned long flags;
4335 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4336
4337 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4338 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4339 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4340 if (!sas_device)
4341 return;
4342 _scsih_remove_device(ioc, handle);
4343}
4344
4345/**
4346 * _scsih_sas_pd_add - remove pd component
4347 * @ioc: per adapter object
4348 * @element: IR config element data
4349 * Context: user.
4350 *
4351 * Return nothing.
4352 */
4353static void
4354_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4355 Mpi2EventIrConfigElement_t *element)
4356{
4357 struct _sas_device *sas_device;
4358 unsigned long flags;
4359 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304360 Mpi2ConfigReply_t mpi_reply;
4361 Mpi2SasDevicePage0_t sas_device_pg0;
4362 u32 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06004363
4364 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4365 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4366 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai62727a72009-08-07 19:35:18 +05304367 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06004368 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304369 return;
4370 }
4371
4372 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4373 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4374 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4375 ioc->name, __FILE__, __LINE__, __func__);
4376 return;
4377 }
4378
4379 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4380 MPI2_IOCSTATUS_MASK;
4381 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4382 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4383 ioc->name, __FILE__, __LINE__, __func__);
4384 return;
4385 }
4386
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05304387 mpt2sas_transport_update_links(ioc,
Kashyap, Desai62727a72009-08-07 19:35:18 +05304388 le16_to_cpu(sas_device_pg0.ParentDevHandle),
4389 handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4390
4391 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06004392}
4393
4394#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4395/**
4396 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4397 * @ioc: per adapter object
4398 * @event_data: event data payload
4399 * Context: user.
4400 *
4401 * Return nothing.
4402 */
4403static void
4404_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4405 Mpi2EventDataIrConfigChangeList_t *event_data)
4406{
4407 Mpi2EventIrConfigElement_t *element;
4408 u8 element_type;
4409 int i;
4410 char *reason_str = NULL, *element_str = NULL;
4411
4412 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4413
4414 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4415 ioc->name, (le32_to_cpu(event_data->Flags) &
4416 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4417 "foreign" : "native", event_data->NumElements);
4418 for (i = 0; i < event_data->NumElements; i++, element++) {
4419 switch (element->ReasonCode) {
4420 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4421 reason_str = "add";
4422 break;
4423 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4424 reason_str = "remove";
4425 break;
4426 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4427 reason_str = "no change";
4428 break;
4429 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4430 reason_str = "hide";
4431 break;
4432 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4433 reason_str = "unhide";
4434 break;
4435 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4436 reason_str = "volume_created";
4437 break;
4438 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4439 reason_str = "volume_deleted";
4440 break;
4441 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4442 reason_str = "pd_created";
4443 break;
4444 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4445 reason_str = "pd_deleted";
4446 break;
4447 default:
4448 reason_str = "unknown reason";
4449 break;
4450 }
4451 element_type = le16_to_cpu(element->ElementFlags) &
4452 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4453 switch (element_type) {
4454 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4455 element_str = "volume";
4456 break;
4457 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4458 element_str = "phys disk";
4459 break;
4460 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4461 element_str = "hot spare";
4462 break;
4463 default:
4464 element_str = "unknown element";
4465 break;
4466 }
4467 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4468 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4469 reason_str, le16_to_cpu(element->VolDevHandle),
4470 le16_to_cpu(element->PhysDiskDevHandle),
4471 element->PhysDiskNum);
4472 }
4473}
4474#endif
4475
4476/**
4477 * _scsih_sas_ir_config_change_event - handle ir configuration change events
4478 * @ioc: per adapter object
4479 * @VF_ID:
4480 * @event_data: event data payload
4481 * Context: user.
4482 *
4483 * Return nothing.
4484 */
4485static void
4486_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4487 Mpi2EventDataIrConfigChangeList_t *event_data)
4488{
4489 Mpi2EventIrConfigElement_t *element;
4490 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304491 u8 foreign_config;
Eric Moore635374e2009-03-09 01:21:12 -06004492
4493#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4494 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4495 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4496
4497#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05304498 foreign_config = (le32_to_cpu(event_data->Flags) &
4499 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06004500
4501 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4502 for (i = 0; i < event_data->NumElements; i++, element++) {
4503
4504 switch (element->ReasonCode) {
4505 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4506 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304507 if (!foreign_config)
4508 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06004509 break;
4510 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4511 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304512 if (!foreign_config)
4513 _scsih_sas_volume_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06004514 break;
4515 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4516 _scsih_sas_pd_hide(ioc, element);
4517 break;
4518 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4519 _scsih_sas_pd_expose(ioc, element);
4520 break;
4521 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4522 _scsih_sas_pd_add(ioc, element);
4523 break;
4524 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4525 _scsih_sas_pd_delete(ioc, element);
4526 break;
4527 }
4528 }
4529}
4530
4531/**
4532 * _scsih_sas_ir_volume_event - IR volume event
4533 * @ioc: per adapter object
4534 * @event_data: event data payload
4535 * Context: user.
4536 *
4537 * Return nothing.
4538 */
4539static void
4540_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4541 Mpi2EventDataIrVolume_t *event_data)
4542{
4543 u64 wwid;
4544 unsigned long flags;
4545 struct _raid_device *raid_device;
4546 u16 handle;
4547 u32 state;
4548 int rc;
4549 struct MPT2SAS_TARGET *sas_target_priv_data;
4550
4551 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4552 return;
4553
4554 handle = le16_to_cpu(event_data->VolDevHandle);
4555 state = le32_to_cpu(event_data->NewValue);
4556 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4557 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4558 le32_to_cpu(event_data->PreviousValue), state));
4559
4560 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4561 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4562 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4563
4564 switch (state) {
4565 case MPI2_RAID_VOL_STATE_MISSING:
4566 case MPI2_RAID_VOL_STATE_FAILED:
4567 if (!raid_device)
4568 break;
4569 if (raid_device->starget) {
4570 sas_target_priv_data = raid_device->starget->hostdata;
4571 sas_target_priv_data->deleted = 1;
4572 scsi_remove_target(&raid_device->starget->dev);
4573 }
4574 _scsih_raid_device_remove(ioc, raid_device);
4575 break;
4576
4577 case MPI2_RAID_VOL_STATE_ONLINE:
4578 case MPI2_RAID_VOL_STATE_DEGRADED:
4579 case MPI2_RAID_VOL_STATE_OPTIMAL:
4580 if (raid_device)
4581 break;
4582
4583 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4584 if (!wwid) {
4585 printk(MPT2SAS_ERR_FMT
4586 "failure at %s:%d/%s()!\n", ioc->name,
4587 __FILE__, __LINE__, __func__);
4588 break;
4589 }
4590
4591 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4592 if (!raid_device) {
4593 printk(MPT2SAS_ERR_FMT
4594 "failure at %s:%d/%s()!\n", ioc->name,
4595 __FILE__, __LINE__, __func__);
4596 break;
4597 }
4598
4599 raid_device->id = ioc->sas_id++;
4600 raid_device->channel = RAID_CHANNEL;
4601 raid_device->handle = handle;
4602 raid_device->wwid = wwid;
4603 _scsih_raid_device_add(ioc, raid_device);
4604 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4605 raid_device->id, 0);
4606 if (rc)
4607 _scsih_raid_device_remove(ioc, raid_device);
4608 break;
4609
4610 case MPI2_RAID_VOL_STATE_INITIALIZING:
4611 default:
4612 break;
4613 }
4614}
4615
4616/**
4617 * _scsih_sas_ir_physical_disk_event - PD event
4618 * @ioc: per adapter object
4619 * @event_data: event data payload
4620 * Context: user.
4621 *
4622 * Return nothing.
4623 */
4624static void
4625_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4626 Mpi2EventDataIrPhysicalDisk_t *event_data)
4627{
4628 u16 handle;
4629 u32 state;
4630 struct _sas_device *sas_device;
4631 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304632 Mpi2ConfigReply_t mpi_reply;
4633 Mpi2SasDevicePage0_t sas_device_pg0;
4634 u32 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06004635
4636 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4637 return;
4638
4639 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4640 state = le32_to_cpu(event_data->NewValue);
4641
4642 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4643 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4644 le32_to_cpu(event_data->PreviousValue), state));
4645
4646 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4647 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4648 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4649
4650 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06004651 case MPI2_RAID_PD_STATE_ONLINE:
4652 case MPI2_RAID_PD_STATE_DEGRADED:
4653 case MPI2_RAID_PD_STATE_REBUILDING:
4654 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desai62727a72009-08-07 19:35:18 +05304655 if (sas_device) {
Eric Moore635374e2009-03-09 01:21:12 -06004656 sas_device->hidden_raid_component = 1;
Kashyap, Desai62727a72009-08-07 19:35:18 +05304657 return;
4658 }
4659
4660 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4661 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
4662 handle))) {
4663 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4664 ioc->name, __FILE__, __LINE__, __func__);
4665 return;
4666 }
4667
4668 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4669 MPI2_IOCSTATUS_MASK;
4670 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4671 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4672 ioc->name, __FILE__, __LINE__, __func__);
4673 return;
4674 }
4675
Kashyap, Desaicc0f5202009-08-20 13:22:39 +05304676 mpt2sas_transport_update_links(ioc,
Kashyap, Desai62727a72009-08-07 19:35:18 +05304677 le16_to_cpu(sas_device_pg0.ParentDevHandle),
4678 handle, sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
4679
4680 _scsih_add_device(ioc, handle, 0, 1);
4681
Eric Moore635374e2009-03-09 01:21:12 -06004682 break;
4683
Kashyap, Desai62727a72009-08-07 19:35:18 +05304684 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06004685 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
4686 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
4687 case MPI2_RAID_PD_STATE_HOT_SPARE:
4688 default:
4689 break;
4690 }
4691}
4692
4693#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4694/**
4695 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
4696 * @ioc: per adapter object
4697 * @event_data: event data payload
4698 * Context: user.
4699 *
4700 * Return nothing.
4701 */
4702static void
4703_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
4704 Mpi2EventDataIrOperationStatus_t *event_data)
4705{
4706 char *reason_str = NULL;
4707
4708 switch (event_data->RAIDOperation) {
4709 case MPI2_EVENT_IR_RAIDOP_RESYNC:
4710 reason_str = "resync";
4711 break;
4712 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
4713 reason_str = "online capacity expansion";
4714 break;
4715 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
4716 reason_str = "consistency check";
4717 break;
4718 default:
4719 reason_str = "unknown reason";
4720 break;
4721 }
4722
4723 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
4724 "\thandle(0x%04x), percent complete(%d)\n",
4725 ioc->name, reason_str,
4726 le16_to_cpu(event_data->VolDevHandle),
4727 event_data->PercentComplete);
4728}
4729#endif
4730
4731/**
4732 * _scsih_sas_ir_operation_status_event - handle RAID operation events
4733 * @ioc: per adapter object
4734 * @VF_ID:
4735 * @event_data: event data payload
4736 * Context: user.
4737 *
4738 * Return nothing.
4739 */
4740static void
4741_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4742 Mpi2EventDataIrOperationStatus_t *event_data)
4743{
4744#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4745 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4746 _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
4747#endif
4748}
4749
4750/**
4751 * _scsih_task_set_full - handle task set full
4752 * @ioc: per adapter object
4753 * @event_data: event data payload
4754 * Context: user.
4755 *
4756 * Throttle back qdepth.
4757 */
4758static void
4759_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4760 Mpi2EventDataTaskSetFull_t *event_data)
4761{
4762 unsigned long flags;
4763 struct _sas_device *sas_device;
4764 static struct _raid_device *raid_device;
4765 struct scsi_device *sdev;
4766 int depth;
4767 u16 current_depth;
4768 u16 handle;
4769 int id, channel;
4770 u64 sas_address;
4771
4772 current_depth = le16_to_cpu(event_data->CurrentDepth);
4773 handle = le16_to_cpu(event_data->DevHandle);
4774 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4775 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4776 if (!sas_device) {
4777 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4778 return;
4779 }
4780 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4781 id = sas_device->id;
4782 channel = sas_device->channel;
4783 sas_address = sas_device->sas_address;
4784
4785 /* if hidden raid component, then change to volume characteristics */
4786 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
4787 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4788 raid_device = _scsih_raid_device_find_by_handle(
4789 ioc, sas_device->volume_handle);
4790 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4791 if (raid_device) {
4792 id = raid_device->id;
4793 channel = raid_device->channel;
4794 handle = raid_device->handle;
4795 sas_address = raid_device->wwid;
4796 }
4797 }
4798
4799 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
4800 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
4801 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
4802 handle, (unsigned long long)sas_address, current_depth);
4803
4804 shost_for_each_device(sdev, ioc->shost) {
4805 if (sdev->id == id && sdev->channel == channel) {
4806 if (current_depth > sdev->queue_depth) {
4807 if (ioc->logging_level &
4808 MPT_DEBUG_TASK_SET_FULL)
4809 sdev_printk(KERN_INFO, sdev, "strange "
4810 "observation, the queue depth is"
4811 " (%d) meanwhile fw queue depth "
4812 "is (%d)\n", sdev->queue_depth,
4813 current_depth);
4814 continue;
4815 }
4816 depth = scsi_track_queue_full(sdev,
4817 current_depth - 1);
4818 if (depth > 0)
4819 sdev_printk(KERN_INFO, sdev, "Queue depth "
4820 "reduced to (%d)\n", depth);
4821 else if (depth < 0)
4822 sdev_printk(KERN_INFO, sdev, "Tagged Command "
4823 "Queueing is being disabled\n");
4824 else if (depth == 0)
4825 if (ioc->logging_level &
4826 MPT_DEBUG_TASK_SET_FULL)
4827 sdev_printk(KERN_INFO, sdev,
4828 "Queue depth not changed yet\n");
4829 }
4830 }
4831}
4832
4833/**
4834 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
4835 * @ioc: per adapter object
4836 * @sas_address: sas address
4837 * @slot: enclosure slot id
4838 * @handle: device handle
4839 *
4840 * After host reset, find out whether devices are still responding.
4841 * Used in _scsi_remove_unresponsive_sas_devices.
4842 *
4843 * Return nothing.
4844 */
4845static void
4846_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4847 u16 slot, u16 handle)
4848{
4849 struct MPT2SAS_TARGET *sas_target_priv_data;
4850 struct scsi_target *starget;
4851 struct _sas_device *sas_device;
4852 unsigned long flags;
4853
4854 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4855 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
4856 if (sas_device->sas_address == sas_address &&
4857 sas_device->slot == slot && sas_device->starget) {
4858 sas_device->responding = 1;
4859 starget_printk(KERN_INFO, sas_device->starget,
4860 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
4861 "logical id(0x%016llx), slot(%d)\n", handle,
4862 (unsigned long long)sas_device->sas_address,
4863 (unsigned long long)
4864 sas_device->enclosure_logical_id,
4865 sas_device->slot);
4866 if (sas_device->handle == handle)
4867 goto out;
4868 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4869 sas_device->handle);
4870 sas_device->handle = handle;
4871 starget = sas_device->starget;
4872 sas_target_priv_data = starget->hostdata;
4873 sas_target_priv_data->handle = handle;
4874 goto out;
4875 }
4876 }
4877 out:
4878 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4879}
4880
4881/**
4882 * _scsih_search_responding_sas_devices -
4883 * @ioc: per adapter object
4884 *
4885 * After host reset, find out whether devices are still responding.
4886 * If not remove.
4887 *
4888 * Return nothing.
4889 */
4890static void
4891_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
4892{
4893 Mpi2SasDevicePage0_t sas_device_pg0;
4894 Mpi2ConfigReply_t mpi_reply;
4895 u16 ioc_status;
4896 __le64 sas_address;
4897 u16 handle;
4898 u32 device_info;
4899 u16 slot;
4900
4901 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4902
4903 if (list_empty(&ioc->sas_device_list))
4904 return;
4905
4906 handle = 0xFFFF;
4907 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4908 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
4909 handle))) {
4910 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4911 MPI2_IOCSTATUS_MASK;
4912 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4913 break;
4914 handle = le16_to_cpu(sas_device_pg0.DevHandle);
4915 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4916 if (!(_scsih_is_end_device(device_info)))
4917 continue;
4918 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4919 slot = le16_to_cpu(sas_device_pg0.Slot);
4920 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
4921 handle);
4922 }
4923}
4924
4925/**
4926 * _scsih_mark_responding_raid_device - mark a raid_device as responding
4927 * @ioc: per adapter object
4928 * @wwid: world wide identifier for raid volume
4929 * @handle: device handle
4930 *
4931 * After host reset, find out whether devices are still responding.
4932 * Used in _scsi_remove_unresponsive_raid_devices.
4933 *
4934 * Return nothing.
4935 */
4936static void
4937_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
4938 u16 handle)
4939{
4940 struct MPT2SAS_TARGET *sas_target_priv_data;
4941 struct scsi_target *starget;
4942 struct _raid_device *raid_device;
4943 unsigned long flags;
4944
4945 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4946 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
4947 if (raid_device->wwid == wwid && raid_device->starget) {
4948 raid_device->responding = 1;
4949 starget_printk(KERN_INFO, raid_device->starget,
4950 "handle(0x%04x), wwid(0x%016llx)\n", handle,
4951 (unsigned long long)raid_device->wwid);
4952 if (raid_device->handle == handle)
4953 goto out;
4954 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4955 raid_device->handle);
4956 raid_device->handle = handle;
4957 starget = raid_device->starget;
4958 sas_target_priv_data = starget->hostdata;
4959 sas_target_priv_data->handle = handle;
4960 goto out;
4961 }
4962 }
4963 out:
4964 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4965}
4966
4967/**
4968 * _scsih_search_responding_raid_devices -
4969 * @ioc: per adapter object
4970 *
4971 * After host reset, find out whether devices are still responding.
4972 * If not remove.
4973 *
4974 * Return nothing.
4975 */
4976static void
4977_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
4978{
4979 Mpi2RaidVolPage1_t volume_pg1;
4980 Mpi2ConfigReply_t mpi_reply;
4981 u16 ioc_status;
4982 u16 handle;
4983
4984 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4985
4986 if (list_empty(&ioc->raid_device_list))
4987 return;
4988
4989 handle = 0xFFFF;
4990 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
4991 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
4992 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4993 MPI2_IOCSTATUS_MASK;
4994 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4995 break;
4996 handle = le16_to_cpu(volume_pg1.DevHandle);
4997 _scsih_mark_responding_raid_device(ioc,
4998 le64_to_cpu(volume_pg1.WWID), handle);
4999 }
5000}
5001
5002/**
5003 * _scsih_mark_responding_expander - mark a expander as responding
5004 * @ioc: per adapter object
5005 * @sas_address: sas address
5006 * @handle:
5007 *
5008 * After host reset, find out whether devices are still responding.
5009 * Used in _scsi_remove_unresponsive_expanders.
5010 *
5011 * Return nothing.
5012 */
5013static void
5014_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5015 u16 handle)
5016{
5017 struct _sas_node *sas_expander;
5018 unsigned long flags;
5019
5020 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5021 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
5022 if (sas_expander->sas_address == sas_address) {
5023 sas_expander->responding = 1;
5024 if (sas_expander->handle != handle) {
5025 printk(KERN_INFO "old handle(0x%04x)\n",
5026 sas_expander->handle);
5027 sas_expander->handle = handle;
5028 }
5029 goto out;
5030 }
5031 }
5032 out:
5033 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5034}
5035
5036/**
5037 * _scsih_search_responding_expanders -
5038 * @ioc: per adapter object
5039 *
5040 * After host reset, find out whether devices are still responding.
5041 * If not remove.
5042 *
5043 * Return nothing.
5044 */
5045static void
5046_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
5047{
5048 Mpi2ExpanderPage0_t expander_pg0;
5049 Mpi2ConfigReply_t mpi_reply;
5050 u16 ioc_status;
5051 __le64 sas_address;
5052 u16 handle;
5053
5054 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5055
5056 if (list_empty(&ioc->sas_expander_list))
5057 return;
5058
5059 handle = 0xFFFF;
5060 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5061 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
5062
5063 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5064 MPI2_IOCSTATUS_MASK;
5065 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5066 break;
5067
5068 handle = le16_to_cpu(expander_pg0.DevHandle);
5069 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5070 printk(KERN_INFO "\texpander present: handle(0x%04x), "
5071 "sas_addr(0x%016llx)\n", handle,
5072 (unsigned long long)sas_address);
5073 _scsih_mark_responding_expander(ioc, sas_address, handle);
5074 }
5075
5076}
5077
5078/**
5079 * _scsih_remove_unresponding_devices - removing unresponding devices
5080 * @ioc: per adapter object
5081 *
5082 * Return nothing.
5083 */
5084static void
5085_scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
5086{
5087 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305088 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06005089 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06005090
Eric Moore635374e2009-03-09 01:21:12 -06005091
5092 list_for_each_entry_safe(sas_device, sas_device_next,
5093 &ioc->sas_device_list, list) {
5094 if (sas_device->responding) {
5095 sas_device->responding = 0;
5096 continue;
5097 }
5098 if (sas_device->starget)
5099 starget_printk(KERN_INFO, sas_device->starget,
5100 "removing: handle(0x%04x), sas_addr(0x%016llx), "
5101 "enclosure logical id(0x%016llx), slot(%d)\n",
5102 sas_device->handle,
5103 (unsigned long long)sas_device->sas_address,
5104 (unsigned long long)
5105 sas_device->enclosure_logical_id,
5106 sas_device->slot);
5107 _scsih_remove_device(ioc, sas_device->handle);
5108 }
5109
5110 list_for_each_entry_safe(raid_device, raid_device_next,
5111 &ioc->raid_device_list, list) {
5112 if (raid_device->responding) {
5113 raid_device->responding = 0;
5114 continue;
5115 }
5116 if (raid_device->starget) {
5117 starget_printk(KERN_INFO, raid_device->starget,
5118 "removing: handle(0x%04x), wwid(0x%016llx)\n",
5119 raid_device->handle,
5120 (unsigned long long)raid_device->wwid);
5121 scsi_remove_target(&raid_device->starget->dev);
5122 }
5123 _scsih_raid_device_remove(ioc, raid_device);
5124 }
5125
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305126 retry_expander_search:
5127 sas_expander = NULL;
5128 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06005129 if (sas_expander->responding) {
5130 sas_expander->responding = 0;
5131 continue;
5132 }
Eric Moore635374e2009-03-09 01:21:12 -06005133 _scsih_expander_remove(ioc, sas_expander->handle);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05305134 goto retry_expander_search;
5135 }
5136}
5137
5138/**
5139 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
5140 * @ioc: per adapter object
5141 * @reset_phase: phase
5142 *
5143 * The handler for doing any required cleanup or initialization.
5144 *
5145 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
5146 * MPT2_IOC_DONE_RESET
5147 *
5148 * Return nothing.
5149 */
5150void
5151mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
5152{
5153 switch (reset_phase) {
5154 case MPT2_IOC_PRE_RESET:
5155 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5156 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
5157 _scsih_fw_event_off(ioc);
5158 break;
5159 case MPT2_IOC_AFTER_RESET:
5160 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5161 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
5162 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
5163 ioc->tm_cmds.status |= MPT2_CMD_RESET;
5164 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
5165 complete(&ioc->tm_cmds.done);
5166 }
5167 _scsih_fw_event_on(ioc);
5168 _scsih_flush_running_cmds(ioc);
5169 break;
5170 case MPT2_IOC_DONE_RESET:
5171 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5172 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
5173 _scsih_sas_host_refresh(ioc, 0);
5174 _scsih_search_responding_sas_devices(ioc);
5175 _scsih_search_responding_raid_devices(ioc);
5176 _scsih_search_responding_expanders(ioc);
5177 break;
5178 case MPT2_IOC_RUNNING:
5179 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
5180 "MPT2_IOC_RUNNING\n", ioc->name, __func__));
5181 _scsih_remove_unresponding_devices(ioc);
5182 break;
Eric Moore635374e2009-03-09 01:21:12 -06005183 }
5184}
5185
5186/**
5187 * _firmware_event_work - delayed task for processing firmware events
5188 * @ioc: per adapter object
5189 * @work: equal to the fw_event_work object
5190 * Context: user.
5191 *
5192 * Return nothing.
5193 */
5194static void
5195_firmware_event_work(struct work_struct *work)
5196{
5197 struct fw_event_work *fw_event = container_of(work,
Eric Moore6f92a7a2009-04-21 15:43:33 -06005198 struct fw_event_work, work);
Eric Moore635374e2009-03-09 01:21:12 -06005199 unsigned long flags;
5200 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
5201
Eric Moore635374e2009-03-09 01:21:12 -06005202 /* the queue is being flushed so ignore this event */
5203 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5204 if (ioc->fw_events_off || ioc->remove_host) {
5205 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5206 _scsih_fw_event_free(ioc, fw_event);
5207 return;
5208 }
5209 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5210
Eric Moore635374e2009-03-09 01:21:12 -06005211 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06005212 _scsih_fw_event_requeue(ioc, fw_event, 1000);
5213 return;
5214 }
Eric Moore635374e2009-03-09 01:21:12 -06005215
5216 switch (fw_event->event) {
5217 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5218 _scsih_sas_topology_change_event(ioc, fw_event->VF_ID,
5219 fw_event->event_data, fw_event);
5220 break;
5221 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5222 _scsih_sas_device_status_change_event(ioc, fw_event->VF_ID,
5223 fw_event->event_data);
5224 break;
5225 case MPI2_EVENT_SAS_DISCOVERY:
5226 _scsih_sas_discovery_event(ioc, fw_event->VF_ID,
5227 fw_event->event_data);
5228 break;
5229 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5230 _scsih_sas_broadcast_primative_event(ioc, fw_event->VF_ID,
5231 fw_event->event_data);
5232 break;
5233 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5234 _scsih_sas_enclosure_dev_status_change_event(ioc,
5235 fw_event->VF_ID, fw_event->event_data);
5236 break;
5237 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5238 _scsih_sas_ir_config_change_event(ioc, fw_event->VF_ID,
5239 fw_event->event_data);
5240 break;
5241 case MPI2_EVENT_IR_VOLUME:
5242 _scsih_sas_ir_volume_event(ioc, fw_event->VF_ID,
5243 fw_event->event_data);
5244 break;
5245 case MPI2_EVENT_IR_PHYSICAL_DISK:
5246 _scsih_sas_ir_physical_disk_event(ioc, fw_event->VF_ID,
5247 fw_event->event_data);
5248 break;
5249 case MPI2_EVENT_IR_OPERATION_STATUS:
5250 _scsih_sas_ir_operation_status_event(ioc, fw_event->VF_ID,
5251 fw_event->event_data);
5252 break;
5253 case MPI2_EVENT_TASK_SET_FULL:
5254 _scsih_task_set_full(ioc, fw_event->VF_ID,
5255 fw_event->event_data);
5256 break;
5257 }
5258 _scsih_fw_event_free(ioc, fw_event);
5259}
5260
5261/**
5262 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5263 * @ioc: per adapter object
5264 * @VF_ID: virtual function id
5265 * @reply: reply message frame(lower 32bit addr)
5266 * Context: interrupt.
5267 *
5268 * This function merely adds a new work task into ioc->firmware_event_thread.
5269 * The tasks are worked from _firmware_event_work in user context.
5270 *
5271 * Return nothing.
5272 */
5273void
5274mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
5275{
5276 struct fw_event_work *fw_event;
5277 Mpi2EventNotificationReply_t *mpi_reply;
5278 unsigned long flags;
5279 u16 event;
5280
5281 /* events turned off due to host reset or driver unloading */
5282 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5283 if (ioc->fw_events_off || ioc->remove_host) {
5284 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5285 return;
5286 }
5287 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5288
5289 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5290 event = le16_to_cpu(mpi_reply->Event);
5291
5292 switch (event) {
5293 /* handle these */
5294 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5295 {
5296 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5297 (Mpi2EventDataSasBroadcastPrimitive_t *)
5298 mpi_reply->EventData;
5299
5300 if (baen_data->Primitive !=
5301 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5302 ioc->broadcast_aen_busy)
5303 return;
5304 ioc->broadcast_aen_busy = 1;
5305 break;
5306 }
5307
5308 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5309 _scsih_check_topo_delete_events(ioc,
5310 (Mpi2EventDataSasTopologyChangeList_t *)
5311 mpi_reply->EventData);
5312 break;
5313
5314 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5315 case MPI2_EVENT_IR_OPERATION_STATUS:
5316 case MPI2_EVENT_SAS_DISCOVERY:
5317 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5318 case MPI2_EVENT_IR_VOLUME:
5319 case MPI2_EVENT_IR_PHYSICAL_DISK:
5320 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5321 case MPI2_EVENT_TASK_SET_FULL:
5322 break;
5323
5324 default: /* ignore the rest */
5325 return;
5326 }
5327
5328 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5329 if (!fw_event) {
5330 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5331 ioc->name, __FILE__, __LINE__, __func__);
5332 return;
5333 }
5334 fw_event->event_data =
5335 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5336 if (!fw_event->event_data) {
5337 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5338 ioc->name, __FILE__, __LINE__, __func__);
5339 kfree(fw_event);
5340 return;
5341 }
5342
5343 memcpy(fw_event->event_data, mpi_reply->EventData,
5344 mpi_reply->EventDataLength*4);
5345 fw_event->ioc = ioc;
5346 fw_event->VF_ID = VF_ID;
5347 fw_event->event = event;
5348 _scsih_fw_event_add(ioc, fw_event);
5349}
5350
5351/* shost template */
5352static struct scsi_host_template scsih_driver_template = {
5353 .module = THIS_MODULE,
5354 .name = "Fusion MPT SAS Host",
5355 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06005356 .queuecommand = _scsih_qcmd,
5357 .target_alloc = _scsih_target_alloc,
5358 .slave_alloc = _scsih_slave_alloc,
5359 .slave_configure = _scsih_slave_configure,
5360 .target_destroy = _scsih_target_destroy,
5361 .slave_destroy = _scsih_slave_destroy,
5362 .change_queue_depth = _scsih_change_queue_depth,
5363 .change_queue_type = _scsih_change_queue_type,
5364 .eh_abort_handler = _scsih_abort,
5365 .eh_device_reset_handler = _scsih_dev_reset,
5366 .eh_target_reset_handler = _scsih_target_reset,
5367 .eh_host_reset_handler = _scsih_host_reset,
5368 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06005369 .can_queue = 1,
5370 .this_id = -1,
5371 .sg_tablesize = MPT2SAS_SG_DEPTH,
5372 .max_sectors = 8192,
5373 .cmd_per_lun = 7,
5374 .use_clustering = ENABLE_CLUSTERING,
5375 .shost_attrs = mpt2sas_host_attrs,
5376 .sdev_attrs = mpt2sas_dev_attrs,
5377};
5378
5379/**
5380 * _scsih_expander_node_remove - removing expander device from list.
5381 * @ioc: per adapter object
5382 * @sas_expander: the sas_device object
5383 * Context: Calling function should acquire ioc->sas_node_lock.
5384 *
5385 * Removing object and freeing associated memory from the
5386 * ioc->sas_expander_list.
5387 *
5388 * Return nothing.
5389 */
5390static void
5391_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5392 struct _sas_node *sas_expander)
5393{
5394 struct _sas_port *mpt2sas_port;
5395 struct _sas_device *sas_device;
5396 struct _sas_node *expander_sibling;
5397 unsigned long flags;
5398
5399 if (!sas_expander)
5400 return;
5401
5402 /* remove sibling ports attached to this expander */
5403 retry_device_search:
5404 list_for_each_entry(mpt2sas_port,
5405 &sas_expander->sas_port_list, port_list) {
5406 if (mpt2sas_port->remote_identify.device_type ==
5407 SAS_END_DEVICE) {
5408 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5409 sas_device =
5410 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5411 mpt2sas_port->remote_identify.sas_address);
5412 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5413 if (!sas_device)
5414 continue;
5415 _scsih_remove_device(ioc, sas_device->handle);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305416 if (ioc->shost_recovery)
5417 return;
Eric Moore635374e2009-03-09 01:21:12 -06005418 goto retry_device_search;
5419 }
5420 }
5421
5422 retry_expander_search:
5423 list_for_each_entry(mpt2sas_port,
5424 &sas_expander->sas_port_list, port_list) {
5425
5426 if (mpt2sas_port->remote_identify.device_type ==
5427 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5428 mpt2sas_port->remote_identify.device_type ==
5429 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5430
5431 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5432 expander_sibling =
5433 mpt2sas_scsih_expander_find_by_sas_address(
5434 ioc, mpt2sas_port->remote_identify.sas_address);
5435 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5436 if (!expander_sibling)
5437 continue;
5438 _scsih_expander_remove(ioc, expander_sibling->handle);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305439 if (ioc->shost_recovery)
5440 return;
Eric Moore635374e2009-03-09 01:21:12 -06005441 goto retry_expander_search;
5442 }
5443 }
5444
5445 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
5446 sas_expander->parent_handle);
5447
5448 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5449 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5450 sas_expander->handle, (unsigned long long)
5451 sas_expander->sas_address);
5452
5453 list_del(&sas_expander->list);
5454 kfree(sas_expander->phy);
5455 kfree(sas_expander);
5456}
5457
5458/**
Eric Moored5d135b2009-05-18 13:02:08 -06005459 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06005460 * @pdev: PCI device struct
5461 *
5462 * Return nothing.
5463 */
5464static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06005465_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06005466{
5467 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5468 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5469 struct _sas_port *mpt2sas_port;
5470 struct _sas_device *sas_device;
5471 struct _sas_node *expander_sibling;
5472 struct workqueue_struct *wq;
5473 unsigned long flags;
5474
5475 ioc->remove_host = 1;
5476 _scsih_fw_event_off(ioc);
5477
5478 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5479 wq = ioc->firmware_event_thread;
5480 ioc->firmware_event_thread = NULL;
5481 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5482 if (wq)
5483 destroy_workqueue(wq);
5484
5485 /* free ports attached to the sas_host */
5486 retry_again:
5487 list_for_each_entry(mpt2sas_port,
5488 &ioc->sas_hba.sas_port_list, port_list) {
5489 if (mpt2sas_port->remote_identify.device_type ==
5490 SAS_END_DEVICE) {
5491 sas_device =
5492 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5493 mpt2sas_port->remote_identify.sas_address);
5494 if (sas_device) {
5495 _scsih_remove_device(ioc, sas_device->handle);
5496 goto retry_again;
5497 }
5498 } else {
5499 expander_sibling =
5500 mpt2sas_scsih_expander_find_by_sas_address(ioc,
5501 mpt2sas_port->remote_identify.sas_address);
5502 if (expander_sibling) {
5503 _scsih_expander_remove(ioc,
5504 expander_sibling->handle);
5505 goto retry_again;
5506 }
5507 }
5508 }
5509
5510 /* free phys attached to the sas_host */
5511 if (ioc->sas_hba.num_phys) {
5512 kfree(ioc->sas_hba.phy);
5513 ioc->sas_hba.phy = NULL;
5514 ioc->sas_hba.num_phys = 0;
5515 }
5516
5517 sas_remove_host(shost);
5518 mpt2sas_base_detach(ioc);
5519 list_del(&ioc->list);
5520 scsi_remove_host(shost);
5521 scsi_host_put(shost);
5522}
5523
5524/**
5525 * _scsih_probe_boot_devices - reports 1st device
5526 * @ioc: per adapter object
5527 *
5528 * If specified in bios page 2, this routine reports the 1st
5529 * device scsi-ml or sas transport for persistent boot device
5530 * purposes. Please refer to function _scsih_determine_boot_device()
5531 */
5532static void
5533_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5534{
5535 u8 is_raid;
5536 void *device;
5537 struct _sas_device *sas_device;
5538 struct _raid_device *raid_device;
5539 u16 handle, parent_handle;
5540 u64 sas_address;
5541 unsigned long flags;
5542 int rc;
5543
5544 device = NULL;
5545 if (ioc->req_boot_device.device) {
5546 device = ioc->req_boot_device.device;
5547 is_raid = ioc->req_boot_device.is_raid;
5548 } else if (ioc->req_alt_boot_device.device) {
5549 device = ioc->req_alt_boot_device.device;
5550 is_raid = ioc->req_alt_boot_device.is_raid;
5551 } else if (ioc->current_boot_device.device) {
5552 device = ioc->current_boot_device.device;
5553 is_raid = ioc->current_boot_device.is_raid;
5554 }
5555
5556 if (!device)
5557 return;
5558
5559 if (is_raid) {
5560 raid_device = device;
5561 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5562 raid_device->id, 0);
5563 if (rc)
5564 _scsih_raid_device_remove(ioc, raid_device);
5565 } else {
5566 sas_device = device;
5567 handle = sas_device->handle;
5568 parent_handle = sas_device->parent_handle;
5569 sas_address = sas_device->sas_address;
5570 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5571 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5572 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5573 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5574 sas_device->parent_handle)) {
5575 _scsih_sas_device_remove(ioc, sas_device);
5576 } else if (!sas_device->starget) {
5577 mpt2sas_transport_port_remove(ioc, sas_address,
5578 parent_handle);
5579 _scsih_sas_device_remove(ioc, sas_device);
5580 }
5581 }
5582}
5583
5584/**
5585 * _scsih_probe_raid - reporting raid volumes to scsi-ml
5586 * @ioc: per adapter object
5587 *
5588 * Called during initial loading of the driver.
5589 */
5590static void
5591_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5592{
5593 struct _raid_device *raid_device, *raid_next;
5594 int rc;
5595
5596 list_for_each_entry_safe(raid_device, raid_next,
5597 &ioc->raid_device_list, list) {
5598 if (raid_device->starget)
5599 continue;
5600 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5601 raid_device->id, 0);
5602 if (rc)
5603 _scsih_raid_device_remove(ioc, raid_device);
5604 }
5605}
5606
5607/**
5608 * _scsih_probe_sas - reporting raid volumes to sas transport
5609 * @ioc: per adapter object
5610 *
5611 * Called during initial loading of the driver.
5612 */
5613static void
5614_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5615{
5616 struct _sas_device *sas_device, *next;
5617 unsigned long flags;
5618 u16 handle, parent_handle;
5619 u64 sas_address;
5620
5621 /* SAS Device List */
5622 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5623 list) {
5624 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5625 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5626 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5627
5628 handle = sas_device->handle;
5629 parent_handle = sas_device->parent_handle;
5630 sas_address = sas_device->sas_address;
5631 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
5632 _scsih_sas_device_remove(ioc, sas_device);
5633 } else if (!sas_device->starget) {
5634 mpt2sas_transport_port_remove(ioc, sas_address,
5635 parent_handle);
5636 _scsih_sas_device_remove(ioc, sas_device);
5637 }
5638 }
5639}
5640
5641/**
5642 * _scsih_probe_devices - probing for devices
5643 * @ioc: per adapter object
5644 *
5645 * Called during initial loading of the driver.
5646 */
5647static void
5648_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
5649{
5650 u16 volume_mapping_flags =
5651 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
5652 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
5653
5654 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
5655 return; /* return when IOC doesn't support initiator mode */
5656
5657 _scsih_probe_boot_devices(ioc);
5658
5659 if (ioc->ir_firmware) {
5660 if ((volume_mapping_flags &
5661 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
5662 _scsih_probe_sas(ioc);
5663 _scsih_probe_raid(ioc);
5664 } else {
5665 _scsih_probe_raid(ioc);
5666 _scsih_probe_sas(ioc);
5667 }
5668 } else
5669 _scsih_probe_sas(ioc);
5670}
5671
5672/**
Eric Moored5d135b2009-05-18 13:02:08 -06005673 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06005674 * @pdev: PCI device struct
5675 * @id: pci device id
5676 *
5677 * Returns 0 success, anything else error.
5678 */
5679static int
Eric Moored5d135b2009-05-18 13:02:08 -06005680_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06005681{
5682 struct MPT2SAS_ADAPTER *ioc;
5683 struct Scsi_Host *shost;
5684
5685 shost = scsi_host_alloc(&scsih_driver_template,
5686 sizeof(struct MPT2SAS_ADAPTER));
5687 if (!shost)
5688 return -ENODEV;
5689
5690 /* init local params */
5691 ioc = shost_priv(shost);
5692 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
5693 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06005694 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06005695 ioc->shost = shost;
5696 ioc->id = mpt_ids++;
5697 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
5698 ioc->pdev = pdev;
5699 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
5700 ioc->tm_cb_idx = tm_cb_idx;
5701 ioc->ctl_cb_idx = ctl_cb_idx;
5702 ioc->base_cb_idx = base_cb_idx;
5703 ioc->transport_cb_idx = transport_cb_idx;
5704 ioc->config_cb_idx = config_cb_idx;
5705 ioc->logging_level = logging_level;
5706 /* misc semaphores and spin locks */
5707 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
5708 spin_lock_init(&ioc->scsi_lookup_lock);
5709 spin_lock_init(&ioc->sas_device_lock);
5710 spin_lock_init(&ioc->sas_node_lock);
5711 spin_lock_init(&ioc->fw_event_lock);
5712 spin_lock_init(&ioc->raid_device_lock);
5713
5714 INIT_LIST_HEAD(&ioc->sas_device_list);
5715 INIT_LIST_HEAD(&ioc->sas_device_init_list);
5716 INIT_LIST_HEAD(&ioc->sas_expander_list);
5717 INIT_LIST_HEAD(&ioc->fw_event_list);
5718 INIT_LIST_HEAD(&ioc->raid_device_list);
5719 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
5720
5721 /* init shost parameters */
5722 shost->max_cmd_len = 16;
5723 shost->max_lun = max_lun;
5724 shost->transportt = mpt2sas_transport_template;
5725 shost->unique_id = ioc->id;
5726
5727 if ((scsi_add_host(shost, &pdev->dev))) {
5728 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5729 ioc->name, __FILE__, __LINE__, __func__);
5730 list_del(&ioc->list);
5731 goto out_add_shost_fail;
5732 }
5733
Eric Moore3c621b32009-05-18 12:59:41 -06005734 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
5735 | SHOST_DIF_TYPE3_PROTECTION);
5736
Eric Moore635374e2009-03-09 01:21:12 -06005737 /* event thread */
5738 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
5739 "fw_event%d", ioc->id);
5740 ioc->firmware_event_thread = create_singlethread_workqueue(
5741 ioc->firmware_event_name);
5742 if (!ioc->firmware_event_thread) {
5743 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5744 ioc->name, __FILE__, __LINE__, __func__);
5745 goto out_thread_fail;
5746 }
5747
5748 ioc->wait_for_port_enable_to_complete = 1;
5749 if ((mpt2sas_base_attach(ioc))) {
5750 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5751 ioc->name, __FILE__, __LINE__, __func__);
5752 goto out_attach_fail;
5753 }
5754
5755 ioc->wait_for_port_enable_to_complete = 0;
5756 _scsih_probe_devices(ioc);
5757 return 0;
5758
5759 out_attach_fail:
5760 destroy_workqueue(ioc->firmware_event_thread);
5761 out_thread_fail:
5762 list_del(&ioc->list);
5763 scsi_remove_host(shost);
5764 out_add_shost_fail:
5765 return -ENODEV;
5766}
5767
5768#ifdef CONFIG_PM
5769/**
Eric Moored5d135b2009-05-18 13:02:08 -06005770 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06005771 * @pdev: PCI device struct
5772 * @state: PM state change to (usually PCI_D3)
5773 *
5774 * Returns 0 success, anything else error.
5775 */
5776static int
Eric Moored5d135b2009-05-18 13:02:08 -06005777_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06005778{
5779 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5780 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5781 u32 device_state;
5782
Kashyap, Desaie4750c92009-08-07 19:37:59 +05305783 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005784 flush_scheduled_work();
5785 scsi_block_requests(shost);
5786 device_state = pci_choose_state(pdev, state);
5787 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
5788 "operating state [D%d]\n", ioc->name, pdev,
5789 pci_name(pdev), device_state);
5790
5791 mpt2sas_base_free_resources(ioc);
5792 pci_save_state(pdev);
5793 pci_disable_device(pdev);
5794 pci_set_power_state(pdev, device_state);
5795 return 0;
5796}
5797
5798/**
Eric Moored5d135b2009-05-18 13:02:08 -06005799 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06005800 * @pdev: PCI device struct
5801 *
5802 * Returns 0 success, anything else error.
5803 */
5804static int
Eric Moored5d135b2009-05-18 13:02:08 -06005805_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06005806{
5807 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5808 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5809 u32 device_state = pdev->current_state;
5810 int r;
5811
5812 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
5813 "operating state [D%d]\n", ioc->name, pdev,
5814 pci_name(pdev), device_state);
5815
5816 pci_set_power_state(pdev, PCI_D0);
5817 pci_enable_wake(pdev, PCI_D0, 0);
5818 pci_restore_state(pdev);
5819 ioc->pdev = pdev;
5820 r = mpt2sas_base_map_resources(ioc);
5821 if (r)
5822 return r;
5823
5824 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
5825 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05305826 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005827 return 0;
5828}
5829#endif /* CONFIG_PM */
5830
5831
5832static struct pci_driver scsih_driver = {
5833 .name = MPT2SAS_DRIVER_NAME,
5834 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06005835 .probe = _scsih_probe,
5836 .remove = __devexit_p(_scsih_remove),
Eric Moore635374e2009-03-09 01:21:12 -06005837#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06005838 .suspend = _scsih_suspend,
5839 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06005840#endif
5841};
5842
5843
5844/**
Eric Moored5d135b2009-05-18 13:02:08 -06005845 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06005846 *
5847 * Returns 0 success, anything else error.
5848 */
5849static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06005850_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06005851{
5852 int error;
5853
5854 mpt_ids = 0;
5855 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
5856 MPT2SAS_DRIVER_VERSION);
5857
5858 mpt2sas_transport_template =
5859 sas_attach_transport(&mpt2sas_transport_functions);
5860 if (!mpt2sas_transport_template)
5861 return -ENODEV;
5862
5863 mpt2sas_base_initialize_callback_handler();
5864
5865 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06005866 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06005867
5868 /* task managment callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06005869 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06005870
5871 /* base internal commands callback handler */
5872 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
5873
5874 /* transport internal commands callback handler */
5875 transport_cb_idx = mpt2sas_base_register_callback_handler(
5876 mpt2sas_transport_done);
5877
5878 /* configuration page API internal commands callback handler */
5879 config_cb_idx = mpt2sas_base_register_callback_handler(
5880 mpt2sas_config_done);
5881
5882 /* ctl module callback handler */
5883 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
5884
5885 mpt2sas_ctl_init();
5886
5887 error = pci_register_driver(&scsih_driver);
5888 if (error)
5889 sas_release_transport(mpt2sas_transport_template);
5890
5891 return error;
5892}
5893
5894/**
Eric Moored5d135b2009-05-18 13:02:08 -06005895 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06005896 *
5897 * Returns 0 success, anything else error.
5898 */
5899static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06005900_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06005901{
5902 printk(KERN_INFO "mpt2sas version %s unloading\n",
5903 MPT2SAS_DRIVER_VERSION);
5904
5905 pci_unregister_driver(&scsih_driver);
5906
5907 sas_release_transport(mpt2sas_transport_template);
5908 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
5909 mpt2sas_base_release_callback_handler(tm_cb_idx);
5910 mpt2sas_base_release_callback_handler(base_cb_idx);
5911 mpt2sas_base_release_callback_handler(transport_cb_idx);
5912 mpt2sas_base_release_callback_handler(config_cb_idx);
5913 mpt2sas_base_release_callback_handler(ctl_cb_idx);
5914
5915 mpt2sas_ctl_exit();
5916}
5917
Eric Moored5d135b2009-05-18 13:02:08 -06005918module_init(_scsih_init);
5919module_exit(_scsih_exit);