blob: bece386f1d4bd72418cbc58f9ae92603f505cefa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/message/fusion/mptctl.c
Moore, Eric Dean b6fe4dd2005-04-22 18:01:34 -04003 * mpt Ioctl driver.
Prakash, Sathyaf36789e2007-08-14 16:22:54 +05304 * For use with LSI PCI chip/adapters
5 * running LSI Fusion MPT (Message Passing Technology) firmware.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Prakash, Sathyacddc0ab2008-05-21 00:56:41 +05307 * Copyright (c) 1999-2008 LSI Corporation
Eric Moore16d20102007-06-13 16:31:07 -06008 * (mailto:DL-MPTFusionLinux@lsi.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
12/*
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; version 2 of the License.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 NO WARRANTY
23 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
24 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
25 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
26 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
27 solely responsible for determining the appropriateness of using and
28 distributing the Program and assumes all risks associated with its
29 exercise of rights under this Agreement, including but not limited to
30 the risks and costs of program errors, damage to or loss of data,
31 programs or equipment, and unavailability or interruption of operations.
32
33 DISCLAIMER OF LIABILITY
34 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
35 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
38 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
39 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
40 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
41
42 You should have received a copy of the GNU General Public License
43 along with this program; if not, write to the Free Software
44 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45*/
46/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/kernel.h>
49#include <linux/module.h>
50#include <linux/errno.h>
51#include <linux/init.h>
52#include <linux/slab.h>
53#include <linux/types.h>
54#include <linux/pci.h>
55#include <linux/delay.h> /* for mdelay */
56#include <linux/miscdevice.h>
57#include <linux/smp_lock.h>
58#include <linux/compat.h>
59
60#include <asm/io.h>
61#include <asm/uaccess.h>
62
63#include <scsi/scsi.h>
64#include <scsi/scsi_cmnd.h>
65#include <scsi/scsi_device.h>
66#include <scsi/scsi_host.h>
67#include <scsi/scsi_tcq.h>
68
Prakash, Sathyacddc0ab2008-05-21 00:56:41 +053069#define COPYRIGHT "Copyright (c) 1999-2008 LSI Corporation"
Prakash, Sathyaf36789e2007-08-14 16:22:54 +053070#define MODULEAUTHOR "LSI Corporation"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include "mptbase.h"
72#include "mptctl.h"
73
74/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
75#define my_NAME "Fusion MPT misc device (ioctl) driver"
76#define my_VERSION MPT_LINUX_VERSION_COMMON
77#define MYNAM "mptctl"
78
79MODULE_AUTHOR(MODULEAUTHOR);
80MODULE_DESCRIPTION(my_NAME);
81MODULE_LICENSE("GPL");
Eric Moore9f4203b2007-01-04 20:47:47 -070082MODULE_VERSION(my_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
85
Prakash, Sathyaf606f572007-08-14 16:12:53 +053086static u8 mptctl_id = MPT_MAX_PROTOCOL_DRIVERS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static DECLARE_WAIT_QUEUE_HEAD ( mptctl_wait );
89
90/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
91
92struct buflist {
93 u8 *kptr;
94 int len;
95};
96
97/*
98 * Function prototypes. Called from OS entry point mptctl_ioctl.
99 * arg contents specific to function.
100 */
101static int mptctl_fw_download(unsigned long arg);
Moore, Eric Dean d485eb82005-05-11 17:37:26 -0600102static int mptctl_getiocinfo(unsigned long arg, unsigned int cmd);
103static int mptctl_gettargetinfo(unsigned long arg);
104static int mptctl_readtest(unsigned long arg);
105static int mptctl_mpt_command(unsigned long arg);
106static int mptctl_eventquery(unsigned long arg);
107static int mptctl_eventenable(unsigned long arg);
108static int mptctl_eventreport(unsigned long arg);
109static int mptctl_replace_fw(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static int mptctl_do_reset(unsigned long arg);
112static int mptctl_hp_hostinfo(unsigned long arg, unsigned int cmd);
113static int mptctl_hp_targetinfo(unsigned long arg);
114
115static int mptctl_probe(struct pci_dev *, const struct pci_device_id *);
116static void mptctl_remove(struct pci_dev *);
117
118#ifdef CONFIG_COMPAT
119static long compat_mpctl_ioctl(struct file *f, unsigned cmd, unsigned long arg);
120#endif
121/*
122 * Private function calls.
123 */
Moore, Eric Dean d485eb82005-05-11 17:37:26 -0600124static int mptctl_do_mpt_command(struct mpt_ioctl_command karg, void __user *mfPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int mptctl_do_fw_download(int ioc, char __user *ufwbuf, size_t fwlen);
Moore, Eric Dean d485eb82005-05-11 17:37:26 -0600126static MptSge_t *kbuf_alloc_2_sgl(int bytes, u32 dir, int sge_offset, int *frags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct buflist **blp, dma_addr_t *sglbuf_dma, MPT_ADAPTER *ioc);
Moore, Eric Dean d485eb82005-05-11 17:37:26 -0600128static void kfree_sgl(MptSge_t *sgl, dma_addr_t sgl_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct buflist *buflist, MPT_ADAPTER *ioc);
130static void mptctl_timeout_expired (MPT_IOCTL *ioctl);
131static int mptctl_bus_reset(MPT_IOCTL *ioctl);
132static int mptctl_set_tm_flags(MPT_SCSI_HOST *hd);
133static void mptctl_free_tm_flags(MPT_ADAPTER *ioc);
134
135/*
136 * Reset Handler cleanup function
137 */
138static int mptctl_ioc_reset(MPT_ADAPTER *ioc, int reset_phase);
139
Moore, Ericea5a7a82006-02-02 17:20:01 -0700140/*
141 * Event Handler function
142 */
143static int mptctl_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply);
Moore, Ericc972c702006-03-14 09:14:06 -0700144static struct fasync_struct *async_queue=NULL;
Moore, Ericea5a7a82006-02-02 17:20:01 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
147/*
148 * Scatter gather list (SGL) sizes and limits...
149 */
150//#define MAX_SCSI_FRAGS 9
151#define MAX_FRAGS_SPILL1 9
152#define MAX_FRAGS_SPILL2 15
153#define FRAGS_PER_BUCKET (MAX_FRAGS_SPILL2 + 1)
154
155//#define MAX_CHAIN_FRAGS 64
156//#define MAX_CHAIN_FRAGS (15+15+15+16)
157#define MAX_CHAIN_FRAGS (4 * MAX_FRAGS_SPILL2 + 1)
158
159// Define max sg LIST bytes ( == (#frags + #chains) * 8 bytes each)
160// Works out to: 592d bytes! (9+1)*8 + 4*(15+1)*8
161// ^----------------- 80 + 512
162#define MAX_SGL_BYTES ((MAX_FRAGS_SPILL1 + 1 + (4 * FRAGS_PER_BUCKET)) * 8)
163
164/* linux only seems to ever give 128kB MAX contiguous (GFP_USER) mem bytes */
165#define MAX_KMALLOC_SZ (128*1024)
166
167#define MPT_IOCTL_DEFAULT_TIMEOUT 10 /* Default timeout value (seconds) */
168
169/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
170/**
171 * mptctl_syscall_down - Down the MPT adapter syscall semaphore.
172 * @ioc: Pointer to MPT adapter
173 * @nonblock: boolean, non-zero if O_NONBLOCK is set
174 *
175 * All of the ioctl commands can potentially sleep, which is illegal
176 * with a spinlock held, thus we perform mutual exclusion here.
177 *
178 * Returns negative errno on error, or zero for success.
179 */
180static inline int
181mptctl_syscall_down(MPT_ADAPTER *ioc, int nonblock)
182{
183 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (nonblock) {
Christoph Hellwigeeb846c2006-01-13 18:27:11 +0100186 if (!mutex_trylock(&ioc->ioctl->ioctl_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 rc = -EAGAIN;
188 } else {
Christoph Hellwigeeb846c2006-01-13 18:27:11 +0100189 if (mutex_lock_interruptible(&ioc->ioctl->ioctl_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 rc = -ERESTARTSYS;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return rc;
193}
194
195/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
196/*
197 * This is the callback for any message we have posted. The message itself
198 * will be returned to the message pool when we return from the IRQ
199 *
200 * This runs in irq context so be short and sweet.
201 */
202static int
203mptctl_reply(MPT_ADAPTER *ioc, MPT_FRAME_HDR *req, MPT_FRAME_HDR *reply)
204{
205 char *sense_data;
206 int sz, req_index;
207 u16 iocStatus;
208 u8 cmd;
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (req)
211 cmd = req->u.hdr.Function;
212 else
213 return 1;
Prakash, Sathya09120a82007-07-24 15:49:05 +0530214 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\tcompleting mpi function (0x%02X), req=%p, "
215 "reply=%p\n", ioc->name, req->u.hdr.Function, req, reply));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (ioc->ioctl) {
218
219 if (reply==NULL) {
220
Prakash, Sathya09120a82007-07-24 15:49:05 +0530221 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_reply() NULL Reply "
222 "Function=%x!\n", ioc->name, cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 ioc->ioctl->status |= MPT_IOCTL_STATUS_COMMAND_GOOD;
225 ioc->ioctl->reset &= ~MPTCTL_RESET_OK;
226
227 /* We are done, issue wake up
228 */
229 ioc->ioctl->wait_done = 1;
230 wake_up (&mptctl_wait);
231 return 1;
232
233 }
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Copy the reply frame (which much exist
236 * for non-SCSI I/O) to the IOC structure.
237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 memcpy(ioc->ioctl->ReplyFrame, reply,
239 min(ioc->reply_sz, 4*reply->u.reply.MsgLength));
240 ioc->ioctl->status |= MPT_IOCTL_STATUS_RF_VALID;
241
242 /* Set the command status to GOOD if IOC Status is GOOD
243 * OR if SCSI I/O cmd and data underrun or recovered error.
244 */
Christoph Hellwig637fa992005-08-18 16:25:44 +0200245 iocStatus = le16_to_cpu(reply->u.reply.IOCStatus) & MPI_IOCSTATUS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (iocStatus == MPI_IOCSTATUS_SUCCESS)
247 ioc->ioctl->status |= MPT_IOCTL_STATUS_COMMAND_GOOD;
248
Prakash, Sathya09120a82007-07-24 15:49:05 +0530249 if (iocStatus || reply->u.reply.IOCLogInfo)
250 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\tiocstatus (0x%04X), "
251 "loginfo (0x%08X)\n", ioc->name,
252 iocStatus,
253 le32_to_cpu(reply->u.reply.IOCLogInfo)));
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if ((cmd == MPI_FUNCTION_SCSI_IO_REQUEST) ||
256 (cmd == MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
Prakash, Sathya09120a82007-07-24 15:49:05 +0530257
258 if (reply->u.sreply.SCSIStatus || reply->u.sreply.SCSIState)
259 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT
260 "\tscsi_status (0x%02x), scsi_state (0x%02x), "
261 "tag = (0x%04x), transfer_count (0x%08x)\n", ioc->name,
262 reply->u.sreply.SCSIStatus,
263 reply->u.sreply.SCSIState,
264 le16_to_cpu(reply->u.sreply.TaskTag),
265 le32_to_cpu(reply->u.sreply.TransferCount)));
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 ioc->ioctl->reset &= ~MPTCTL_RESET_OK;
268
269 if ((iocStatus == MPI_IOCSTATUS_SCSI_DATA_UNDERRUN) ||
270 (iocStatus == MPI_IOCSTATUS_SCSI_RECOVERED_ERROR)) {
271 ioc->ioctl->status |= MPT_IOCTL_STATUS_COMMAND_GOOD;
272 }
273 }
274
275 /* Copy the sense data - if present
276 */
277 if ((cmd == MPI_FUNCTION_SCSI_IO_REQUEST) &&
278 (reply->u.sreply.SCSIState &
279 MPI_SCSI_STATE_AUTOSENSE_VALID)){
280 sz = req->u.scsireq.SenseBufferLength;
281 req_index =
282 le16_to_cpu(req->u.frame.hwhdr.msgctxu.fld.req_idx);
283 sense_data =
284 ((u8 *)ioc->sense_buf_pool +
285 (req_index * MPT_SENSE_BUFFER_ALLOC));
286 memcpy(ioc->ioctl->sense, sense_data, sz);
287 ioc->ioctl->status |= MPT_IOCTL_STATUS_SENSE_VALID;
288 }
289
290 if (cmd == MPI_FUNCTION_SCSI_TASK_MGMT)
291 mptctl_free_tm_flags(ioc);
292
293 /* We are done, issue wake up
294 */
295 ioc->ioctl->wait_done = 1;
296 wake_up (&mptctl_wait);
297 }
298 return 1;
299}
300
301/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
302/* mptctl_timeout_expired
303 *
304 * Expecting an interrupt, however timed out.
305 *
306 */
307static void mptctl_timeout_expired (MPT_IOCTL *ioctl)
308{
309 int rc = 1;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (ioctl == NULL)
312 return;
Julia Lawallc31558e2008-12-26 08:27:48 +0100313 dctlprintk(ioctl->ioc,
314 printk(MYIOC_s_DEBUG_FMT ": Timeout Expired! Host %d\n",
315 ioctl->ioc->name, ioctl->ioc->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 ioctl->wait_done = 0;
318 if (ioctl->reset & MPTCTL_RESET_OK)
319 rc = mptctl_bus_reset(ioctl);
320
321 if (rc) {
322 /* Issue a reset for this device.
323 * The IOC is not responding.
324 */
Prakash, Sathya09120a82007-07-24 15:49:05 +0530325 dctlprintk(ioctl->ioc, printk(MYIOC_s_DEBUG_FMT "Calling HardReset! \n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 ioctl->ioc->name));
Eric Moorecd2c6192007-01-29 09:47:47 -0700327 mpt_HardResetHandler(ioctl->ioc, CAN_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 return;
330
331}
332
333/* mptctl_bus_reset
334 *
335 * Bus reset code.
336 *
337 */
338static int mptctl_bus_reset(MPT_IOCTL *ioctl)
339{
340 MPT_FRAME_HDR *mf;
341 SCSITaskMgmt_t *pScsiTm;
342 MPT_SCSI_HOST *hd;
343 int ii;
Prakash, Sathya7a195f42007-08-14 16:08:40 +0530344 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346
347 ioctl->reset &= ~MPTCTL_RESET_OK;
348
349 if (ioctl->ioc->sh == NULL)
350 return -EPERM;
351
Eric Mooree7eae9f2007-09-29 10:15:59 -0600352 hd = shost_priv(ioctl->ioc->sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (hd == NULL)
354 return -EPERM;
355
356 /* Single threading ....
357 */
358 if (mptctl_set_tm_flags(hd) != 0)
359 return -EPERM;
360
361 /* Send request
362 */
363 if ((mf = mpt_get_msg_frame(mptctl_id, ioctl->ioc)) == NULL) {
Prakash, Sathya09120a82007-07-24 15:49:05 +0530364 dtmprintk(ioctl->ioc, printk(MYIOC_s_DEBUG_FMT "IssueTaskMgmt, no msg frames!!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ioctl->ioc->name));
366
367 mptctl_free_tm_flags(ioctl->ioc);
368 return -ENOMEM;
369 }
370
Prakash, Sathya09120a82007-07-24 15:49:05 +0530371 dtmprintk(ioctl->ioc, printk(MYIOC_s_DEBUG_FMT "IssueTaskMgmt request @ %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ioctl->ioc->name, mf));
373
374 pScsiTm = (SCSITaskMgmt_t *) mf;
Eric Moore793955f2007-01-29 09:42:20 -0700375 pScsiTm->TargetID = ioctl->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 pScsiTm->Bus = hd->port; /* 0 */
377 pScsiTm->ChainOffset = 0;
378 pScsiTm->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
379 pScsiTm->Reserved = 0;
380 pScsiTm->TaskType = MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS;
381 pScsiTm->Reserved1 = 0;
382 pScsiTm->MsgFlags = MPI_SCSITASKMGMT_MSGFLAGS_LIPRESET_RESET_OPTION;
383
384 for (ii= 0; ii < 8; ii++)
385 pScsiTm->LUN[ii] = 0;
386
387 for (ii=0; ii < 7; ii++)
388 pScsiTm->Reserved2[ii] = 0;
389
390 pScsiTm->TaskMsgContext = 0;
Prakash, Sathya09120a82007-07-24 15:49:05 +0530391 dtmprintk(ioctl->ioc, printk(MYIOC_s_DEBUG_FMT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 "mptctl_bus_reset: issued.\n", ioctl->ioc->name));
393
Prakash, Sathya09120a82007-07-24 15:49:05 +0530394 DBG_DUMP_TM_REQUEST_FRAME(ioctl->ioc, (u32 *)mf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 ioctl->wait_done=0;
Prakash, Sathya7a195f42007-08-14 16:08:40 +0530397
398 if ((ioctl->ioc->facts.IOCCapabilities & MPI_IOCFACTS_CAPABILITY_HIGH_PRI_Q) &&
399 (ioctl->ioc->facts.MsgVersion >= MPI_VERSION_01_05))
400 mpt_put_msg_frame_hi_pri(mptctl_id, ioctl->ioc, mf);
401 else {
402 retval = mpt_send_handshake_request(mptctl_id, ioctl->ioc,
403 sizeof(SCSITaskMgmt_t), (u32*)pScsiTm, CAN_SLEEP);
404 if (retval != 0) {
405 dfailprintk(ioctl->ioc, printk(MYIOC_s_ERR_FMT "_send_handshake FAILED!"
406 " (hd %p, ioc %p, mf %p) \n", hd->ioc->name, hd,
407 hd->ioc, mf));
408 goto mptctl_bus_reset_done;
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
412 /* Now wait for the command to complete */
Moore, Eric86a7dca2006-02-02 17:19:37 -0700413 ii = wait_event_timeout(mptctl_wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 ioctl->wait_done == 1,
415 HZ*5 /* 5 second timeout */);
416
417 if(ii <=0 && (ioctl->wait_done != 1 )) {
Moore, Eric86a7dca2006-02-02 17:19:37 -0700418 mpt_free_msg_frame(hd->ioc, mf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 ioctl->wait_done = 0;
420 retval = -1; /* return failure */
421 }
422
423mptctl_bus_reset_done:
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 mptctl_free_tm_flags(ioctl->ioc);
426 return retval;
427}
428
429static int
430mptctl_set_tm_flags(MPT_SCSI_HOST *hd) {
431 unsigned long flags;
432
433 spin_lock_irqsave(&hd->ioc->FreeQlock, flags);
434
435 if (hd->tmState == TM_STATE_NONE) {
436 hd->tmState = TM_STATE_IN_PROGRESS;
437 hd->tmPending = 1;
438 spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
439 } else {
440 spin_unlock_irqrestore(&hd->ioc->FreeQlock, flags);
441 return -EBUSY;
442 }
443
444 return 0;
445}
446
447static void
448mptctl_free_tm_flags(MPT_ADAPTER *ioc)
449{
450 MPT_SCSI_HOST * hd;
451 unsigned long flags;
452
Eric Mooree7eae9f2007-09-29 10:15:59 -0600453 hd = shost_priv(ioc->sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (hd == NULL)
455 return;
456
457 spin_lock_irqsave(&ioc->FreeQlock, flags);
458
459 hd->tmState = TM_STATE_NONE;
460 hd->tmPending = 0;
461 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
462
463 return;
464}
465
466/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
467/* mptctl_ioc_reset
468 *
469 * Clean-up functionality. Used only if there has been a
470 * reload of the FW due.
471 *
472 */
473static int
474mptctl_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
475{
476 MPT_IOCTL *ioctl = ioc->ioctl;
Eric Moore29dd3602007-09-14 18:46:51 -0600477 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "IOC %s_reset routed to IOCTL driver!\n", ioc->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
479 reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
480
481 if(ioctl == NULL)
482 return 1;
483
484 switch(reset_phase) {
485 case MPT_IOC_SETUP_RESET:
486 ioctl->status |= MPT_IOCTL_STATUS_DID_IOCRESET;
487 break;
488 case MPT_IOC_POST_RESET:
489 ioctl->status &= ~MPT_IOCTL_STATUS_DID_IOCRESET;
490 break;
491 case MPT_IOC_PRE_RESET:
492 default:
493 break;
494 }
495
496 return 1;
497}
498
499/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Moore, Ericea5a7a82006-02-02 17:20:01 -0700500/* ASYNC Event Notification Support */
501static int
502mptctl_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
503{
504 u8 event;
505
506 event = le32_to_cpu(pEvReply->Event) & 0xFF;
507
Prakash, Sathya09120a82007-07-24 15:49:05 +0530508 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s() called\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700509 ioc->name, __func__));
Moore, Ericea5a7a82006-02-02 17:20:01 -0700510 if(async_queue == NULL)
511 return 1;
512
513 /* Raise SIGIO for persistent events.
514 * TODO - this define is not in MPI spec yet,
515 * but they plan to set it to 0x21
516 */
517 if (event == 0x21 ) {
518 ioc->aen_event_read_flag=1;
Prakash, Sathya09120a82007-07-24 15:49:05 +0530519 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "Raised SIGIO to application\n",
520 ioc->name));
521 devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT
522 "Raised SIGIO to application\n", ioc->name));
Moore, Ericea5a7a82006-02-02 17:20:01 -0700523 kill_fasync(&async_queue, SIGIO, POLL_IN);
524 return 1;
525 }
526
527 /* This flag is set after SIGIO was raised, and
528 * remains set until the application has read
529 * the event log via ioctl=MPTEVENTREPORT
530 */
531 if(ioc->aen_event_read_flag)
532 return 1;
533
534 /* Signal only for the events that are
535 * requested for by the application
536 */
537 if (ioc->events && (ioc->eventTypes & ( 1 << event))) {
538 ioc->aen_event_read_flag=1;
Prakash, Sathya09120a82007-07-24 15:49:05 +0530539 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT
540 "Raised SIGIO to application\n", ioc->name));
541 devtverboseprintk(ioc, printk(MYIOC_s_DEBUG_FMT
542 "Raised SIGIO to application\n", ioc->name));
Moore, Ericea5a7a82006-02-02 17:20:01 -0700543 kill_fasync(&async_queue, SIGIO, POLL_IN);
544 }
545 return 1;
546}
547
548static int
549mptctl_fasync(int fd, struct file *filep, int mode)
550{
551 MPT_ADAPTER *ioc;
Jonathan Corbetb7e3e1f2008-06-19 15:41:11 -0600552 int ret;
Moore, Ericea5a7a82006-02-02 17:20:01 -0700553
Jonathan Corbetb7e3e1f2008-06-19 15:41:11 -0600554 lock_kernel();
Moore, Ericea5a7a82006-02-02 17:20:01 -0700555 list_for_each_entry(ioc, &ioc_list, list)
556 ioc->aen_event_read_flag=0;
557
Jonathan Corbetb7e3e1f2008-06-19 15:41:11 -0600558 ret = fasync_helper(fd, filep, mode, &async_queue);
559 unlock_kernel();
560 return ret;
Moore, Ericea5a7a82006-02-02 17:20:01 -0700561}
562
Moore, Ericea5a7a82006-02-02 17:20:01 -0700563/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
565 * MPT ioctl handler
566 * cmd - specify the particular IOCTL command to be issued
567 * arg - data specific to the command. Must not be null.
568 */
569static long
570__mptctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
571{
572 mpt_ioctl_header __user *uhdr = (void __user *) arg;
573 mpt_ioctl_header khdr;
574 int iocnum;
575 unsigned iocnumX;
576 int nonblock = (file->f_flags & O_NONBLOCK);
577 int ret;
578 MPT_ADAPTER *iocp = NULL;
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (copy_from_user(&khdr, uhdr, sizeof(khdr))) {
Eric Moore29dd3602007-09-14 18:46:51 -0600581 printk(KERN_ERR MYNAM "%s::mptctl_ioctl() @%d - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 "Unable to copy mpt_ioctl_header data @ %p\n",
583 __FILE__, __LINE__, uhdr);
584 return -EFAULT;
585 }
586 ret = -ENXIO; /* (-6) No such device or address */
587
588 /* Verify intended MPT adapter - set iocnum and the adapter
589 * pointer (iocp)
590 */
591 iocnumX = khdr.iocnum & 0xFF;
592 if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
593 (iocp == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -0600594 printk(KERN_DEBUG MYNAM "%s::mptctl_ioctl() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +0530595 __FILE__, __LINE__, iocnumX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return -ENODEV;
597 }
598
599 if (!iocp->active) {
Eric Moore29dd3602007-09-14 18:46:51 -0600600 printk(KERN_DEBUG MYNAM "%s::mptctl_ioctl() @%d - Controller disabled.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 __FILE__, __LINE__);
602 return -EFAULT;
603 }
604
605 /* Handle those commands that are just returning
606 * information stored in the driver.
607 * These commands should never time out and are unaffected
608 * by TM and FW reloads.
609 */
610 if ((cmd & ~IOCSIZE_MASK) == (MPTIOCINFO & ~IOCSIZE_MASK)) {
611 return mptctl_getiocinfo(arg, _IOC_SIZE(cmd));
612 } else if (cmd == MPTTARGETINFO) {
613 return mptctl_gettargetinfo(arg);
614 } else if (cmd == MPTTEST) {
615 return mptctl_readtest(arg);
616 } else if (cmd == MPTEVENTQUERY) {
617 return mptctl_eventquery(arg);
618 } else if (cmd == MPTEVENTENABLE) {
619 return mptctl_eventenable(arg);
620 } else if (cmd == MPTEVENTREPORT) {
621 return mptctl_eventreport(arg);
622 } else if (cmd == MPTFWREPLACE) {
623 return mptctl_replace_fw(arg);
624 }
625
626 /* All of these commands require an interrupt or
627 * are unknown/illegal.
628 */
629 if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
630 return ret;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (cmd == MPTFWDOWNLOAD)
633 ret = mptctl_fw_download(arg);
634 else if (cmd == MPTCOMMAND)
635 ret = mptctl_mpt_command(arg);
636 else if (cmd == MPTHARDRESET)
637 ret = mptctl_do_reset(arg);
638 else if ((cmd & ~IOCSIZE_MASK) == (HP_GETHOSTINFO & ~IOCSIZE_MASK))
639 ret = mptctl_hp_hostinfo(arg, _IOC_SIZE(cmd));
640 else if (cmd == HP_GETTARGETINFO)
641 ret = mptctl_hp_targetinfo(arg);
642 else
643 ret = -EINVAL;
644
Christoph Hellwigeeb846c2006-01-13 18:27:11 +0100645 mutex_unlock(&iocp->ioctl->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 return ret;
648}
649
650static long
651mptctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
652{
653 long ret;
654 lock_kernel();
655 ret = __mptctl_ioctl(file, cmd, arg);
656 unlock_kernel();
657 return ret;
658}
659
660static int mptctl_do_reset(unsigned long arg)
661{
662 struct mpt_ioctl_diag_reset __user *urinfo = (void __user *) arg;
663 struct mpt_ioctl_diag_reset krinfo;
664 MPT_ADAPTER *iocp;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (copy_from_user(&krinfo, urinfo, sizeof(struct mpt_ioctl_diag_reset))) {
Eric Moore29dd3602007-09-14 18:46:51 -0600667 printk(KERN_ERR MYNAM "%s@%d::mptctl_do_reset - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 "Unable to copy mpt_ioctl_diag_reset struct @ %p\n",
669 __FILE__, __LINE__, urinfo);
670 return -EFAULT;
671 }
672
673 if (mpt_verify_adapter(krinfo.hdr.iocnum, &iocp) < 0) {
Eric Moore29dd3602007-09-14 18:46:51 -0600674 printk(KERN_DEBUG MYNAM "%s@%d::mptctl_do_reset - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +0530675 __FILE__, __LINE__, krinfo.hdr.iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -ENODEV; /* (-6) No such device or address */
677 }
678
Prakash, Sathya09120a82007-07-24 15:49:05 +0530679 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "mptctl_do_reset called.\n",
680 iocp->name));
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (mpt_HardResetHandler(iocp, CAN_SLEEP) != 0) {
Eric Moore29dd3602007-09-14 18:46:51 -0600683 printk (MYIOC_s_ERR_FMT "%s@%d::mptctl_do_reset - reset failed.\n",
684 iocp->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -1;
686 }
687
688 return 0;
689}
690
691/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
692/*
693 * MPT FW download function. Cast the arg into the mpt_fw_xfer structure.
694 * This structure contains: iocnum, firmware length (bytes),
695 * pointer to user space memory where the fw image is stored.
696 *
697 * Outputs: None.
698 * Return: 0 if successful
699 * -EFAULT if data unavailable
700 * -ENXIO if no such device
701 * -EAGAIN if resource problem
702 * -ENOMEM if no memory for SGE
703 * -EMLINK if too many chain buffers required
704 * -EBADRQC if adapter does not support FW download
705 * -EBUSY if adapter is busy
706 * -ENOMSG if FW upload returned bad status
707 */
708static int
709mptctl_fw_download(unsigned long arg)
710{
711 struct mpt_fw_xfer __user *ufwdl = (void __user *) arg;
712 struct mpt_fw_xfer kfwdl;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (copy_from_user(&kfwdl, ufwdl, sizeof(struct mpt_fw_xfer))) {
Eric Moore29dd3602007-09-14 18:46:51 -0600715 printk(KERN_ERR MYNAM "%s@%d::_ioctl_fwdl - "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 "Unable to copy mpt_fw_xfer struct @ %p\n",
717 __FILE__, __LINE__, ufwdl);
718 return -EFAULT;
719 }
720
721 return mptctl_do_fw_download(kfwdl.iocnum, kfwdl.bufp, kfwdl.fwlen);
722}
723
724/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
725/*
726 * FW Download engine.
727 * Outputs: None.
728 * Return: 0 if successful
729 * -EFAULT if data unavailable
730 * -ENXIO if no such device
731 * -EAGAIN if resource problem
732 * -ENOMEM if no memory for SGE
733 * -EMLINK if too many chain buffers required
734 * -EBADRQC if adapter does not support FW download
735 * -EBUSY if adapter is busy
736 * -ENOMSG if FW upload returned bad status
737 */
738static int
739mptctl_do_fw_download(int ioc, char __user *ufwbuf, size_t fwlen)
740{
741 FWDownload_t *dlmsg;
742 MPT_FRAME_HDR *mf;
743 MPT_ADAPTER *iocp;
744 FWDownloadTCSGE_t *ptsge;
745 MptSge_t *sgl, *sgIn;
746 char *sgOut;
747 struct buflist *buflist;
748 struct buflist *bl;
749 dma_addr_t sgl_dma;
750 int ret;
751 int numfrags = 0;
752 int maxfrags;
753 int n = 0;
754 u32 sgdir;
755 u32 nib;
756 int fw_bytes_copied = 0;
757 int i;
758 int sge_offset = 0;
759 u16 iocstat;
760 pFWDownloadReply_t ReplyMsg = NULL;
761
Moore, Eric946cbf02006-02-02 17:19:50 -0700762 if (mpt_verify_adapter(ioc, &iocp) < 0) {
Eric Moore29dd3602007-09-14 18:46:51 -0600763 printk(KERN_DEBUG MYNAM "ioctl_fwdl - ioc%d not found!\n",
764 ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return -ENODEV; /* (-6) No such device or address */
Moore, Eric946cbf02006-02-02 17:19:50 -0700766 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Moore, Eric946cbf02006-02-02 17:19:50 -0700768 /* Valid device. Get a message frame and construct the FW download message.
769 */
770 if ((mf = mpt_get_msg_frame(mptctl_id, iocp)) == NULL)
771 return -EAGAIN;
772 }
Prakash, Sathya09120a82007-07-24 15:49:05 +0530773
774 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT
775 "mptctl_do_fwdl called. mptctl_id = %xh.\n", iocp->name, mptctl_id));
776 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "DbG: kfwdl.bufp = %p\n",
777 iocp->name, ufwbuf));
778 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "DbG: kfwdl.fwlen = %d\n",
779 iocp->name, (int)fwlen));
780 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "DbG: kfwdl.ioc = %04xh\n",
781 iocp->name, ioc));
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 dlmsg = (FWDownload_t*) mf;
784 ptsge = (FWDownloadTCSGE_t *) &dlmsg->SGL;
785 sgOut = (char *) (ptsge + 1);
786
787 /*
788 * Construct f/w download request
789 */
790 dlmsg->ImageType = MPI_FW_DOWNLOAD_ITYPE_FW;
791 dlmsg->Reserved = 0;
792 dlmsg->ChainOffset = 0;
793 dlmsg->Function = MPI_FUNCTION_FW_DOWNLOAD;
794 dlmsg->Reserved1[0] = dlmsg->Reserved1[1] = dlmsg->Reserved1[2] = 0;
Moore, Eric946cbf02006-02-02 17:19:50 -0700795 if (iocp->facts.MsgVersion >= MPI_VERSION_01_05)
796 dlmsg->MsgFlags = MPI_FW_DOWNLOAD_MSGFLGS_LAST_SEGMENT;
797 else
798 dlmsg->MsgFlags = 0;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 /* Set up the Transaction SGE.
802 */
803 ptsge->Reserved = 0;
804 ptsge->ContextSize = 0;
805 ptsge->DetailsLength = 12;
806 ptsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
807 ptsge->Reserved_0100_Checksum = 0;
808 ptsge->ImageOffset = 0;
809 ptsge->ImageSize = cpu_to_le32(fwlen);
810
811 /* Add the SGL
812 */
813
814 /*
815 * Need to kmalloc area(s) for holding firmware image bytes.
816 * But we need to do it piece meal, using a proper
817 * scatter gather list (with 128kB MAX hunks).
818 *
819 * A practical limit here might be # of sg hunks that fit into
820 * a single IOC request frame; 12 or 8 (see below), so:
821 * For FC9xx: 12 x 128kB == 1.5 mB (max)
822 * For C1030: 8 x 128kB == 1 mB (max)
823 * We could support chaining, but things get ugly(ier:)
824 *
825 * Set the sge_offset to the start of the sgl (bytes).
826 */
827 sgdir = 0x04000000; /* IOC will READ from sys mem */
828 sge_offset = sizeof(MPIHeader_t) + sizeof(FWDownloadTCSGE_t);
829 if ((sgl = kbuf_alloc_2_sgl(fwlen, sgdir, sge_offset,
830 &numfrags, &buflist, &sgl_dma, iocp)) == NULL)
831 return -ENOMEM;
832
833 /*
834 * We should only need SGL with 2 simple_32bit entries (up to 256 kB)
835 * for FC9xx f/w image, but calculate max number of sge hunks
836 * we can fit into a request frame, and limit ourselves to that.
837 * (currently no chain support)
838 * maxfrags = (Request Size - FWdownload Size ) / Size of 32 bit SGE
839 * Request maxfrags
840 * 128 12
841 * 96 8
842 * 64 4
843 */
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +0530844 maxfrags = (iocp->req_sz - sizeof(MPIHeader_t) -
845 sizeof(FWDownloadTCSGE_t))
846 / iocp->SGE_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (numfrags > maxfrags) {
848 ret = -EMLINK;
849 goto fwdl_out;
850 }
851
Prakash, Sathya09120a82007-07-24 15:49:05 +0530852 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "DbG: sgl buffer = %p, sgfrags = %d\n",
853 iocp->name, sgl, numfrags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /*
856 * Parse SG list, copying sgl itself,
857 * plus f/w image hunks from user space as we go...
858 */
859 ret = -EFAULT;
860 sgIn = sgl;
861 bl = buflist;
862 for (i=0; i < numfrags; i++) {
863
864 /* Get the SGE type: 0 - TCSGE, 3 - Chain, 1 - Simple SGE
865 * Skip everything but Simple. If simple, copy from
866 * user space into kernel space.
867 * Note: we should not have anything but Simple as
868 * Chain SGE are illegal.
869 */
870 nib = (sgIn->FlagsLength & 0x30000000) >> 28;
871 if (nib == 0 || nib == 3) {
872 ;
873 } else if (sgIn->Address) {
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +0530874 iocp->add_sge(sgOut, sgIn->FlagsLength, sgIn->Address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 n++;
876 if (copy_from_user(bl->kptr, ufwbuf+fw_bytes_copied, bl->len)) {
Eric Moore29dd3602007-09-14 18:46:51 -0600877 printk(MYIOC_s_ERR_FMT "%s@%d::_ioctl_fwdl - "
878 "Unable to copy f/w buffer hunk#%d @ %p\n",
879 iocp->name, __FILE__, __LINE__, n, ufwbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 goto fwdl_out;
881 }
882 fw_bytes_copied += bl->len;
883 }
884 sgIn++;
885 bl++;
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +0530886 sgOut += iocp->SGE_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Prakash, Sathya09120a82007-07-24 15:49:05 +0530889 DBG_DUMP_FW_DOWNLOAD(iocp, (u32 *)mf, numfrags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /*
892 * Finally, perform firmware download.
893 */
Moore, Eric946cbf02006-02-02 17:19:50 -0700894 ReplyMsg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 mpt_put_msg_frame(mptctl_id, iocp, mf);
896
897 /* Now wait for the command to complete */
Moore, Eric86a7dca2006-02-02 17:19:37 -0700898 ret = wait_event_timeout(mptctl_wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 iocp->ioctl->wait_done == 1,
900 HZ*60);
901
902 if(ret <=0 && (iocp->ioctl->wait_done != 1 )) {
903 /* Now we need to reset the board */
904 mptctl_timeout_expired(iocp->ioctl);
905 ret = -ENODATA;
906 goto fwdl_out;
907 }
908
909 if (sgl)
910 kfree_sgl(sgl, sgl_dma, buflist, iocp);
911
912 ReplyMsg = (pFWDownloadReply_t)iocp->ioctl->ReplyFrame;
913 iocstat = le16_to_cpu(ReplyMsg->IOCStatus) & MPI_IOCSTATUS_MASK;
914 if (iocstat == MPI_IOCSTATUS_SUCCESS) {
Eric Moore29dd3602007-09-14 18:46:51 -0600915 printk(MYIOC_s_INFO_FMT "F/W update successfull!\n", iocp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
917 } else if (iocstat == MPI_IOCSTATUS_INVALID_FUNCTION) {
Eric Moore29dd3602007-09-14 18:46:51 -0600918 printk(MYIOC_s_WARN_FMT "Hmmm... F/W download not supported!?!\n",
919 iocp->name);
920 printk(MYIOC_s_WARN_FMT "(time to go bang on somebodies door)\n",
921 iocp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return -EBADRQC;
923 } else if (iocstat == MPI_IOCSTATUS_BUSY) {
Eric Moore29dd3602007-09-14 18:46:51 -0600924 printk(MYIOC_s_WARN_FMT "IOC_BUSY!\n", iocp->name);
925 printk(MYIOC_s_WARN_FMT "(try again later?)\n", iocp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -EBUSY;
927 } else {
Eric Moore29dd3602007-09-14 18:46:51 -0600928 printk(MYIOC_s_WARN_FMT "ioctl_fwdl() returned [bad] status = %04xh\n",
929 iocp->name, iocstat);
930 printk(MYIOC_s_WARN_FMT "(bad VooDoo)\n", iocp->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -ENOMSG;
932 }
933 return 0;
934
935fwdl_out:
936 kfree_sgl(sgl, sgl_dma, buflist, iocp);
937 return ret;
938}
939
940/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
941/*
942 * SGE Allocation routine
943 *
944 * Inputs: bytes - number of bytes to be transferred
945 * sgdir - data direction
946 * sge_offset - offset (in bytes) from the start of the request
947 * frame to the first SGE
948 * ioc - pointer to the mptadapter
949 * Outputs: frags - number of scatter gather elements
950 * blp - point to the buflist pointer
951 * sglbuf_dma - pointer to the (dma) sgl
952 * Returns: Null if failes
953 * pointer to the (virtual) sgl if successful.
954 */
955static MptSge_t *
956kbuf_alloc_2_sgl(int bytes, u32 sgdir, int sge_offset, int *frags,
957 struct buflist **blp, dma_addr_t *sglbuf_dma, MPT_ADAPTER *ioc)
958{
959 MptSge_t *sglbuf = NULL; /* pointer to array of SGE */
960 /* and chain buffers */
961 struct buflist *buflist = NULL; /* kernel routine */
962 MptSge_t *sgl;
963 int numfrags = 0;
964 int fragcnt = 0;
965 int alloc_sz = min(bytes,MAX_KMALLOC_SZ); // avoid kernel warning msg!
966 int bytes_allocd = 0;
967 int this_alloc;
968 dma_addr_t pa; // phys addr
969 int i, buflist_ent;
970 int sg_spill = MAX_FRAGS_SPILL1;
971 int dir;
972 /* initialization */
973 *frags = 0;
974 *blp = NULL;
975
976 /* Allocate and initialize an array of kernel
977 * structures for the SG elements.
978 */
979 i = MAX_SGL_BYTES / 8;
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -0700980 buflist = kzalloc(i, GFP_USER);
981 if (!buflist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 buflist_ent = 0;
984
985 /* Allocate a single block of memory to store the sg elements and
986 * the chain buffers. The calling routine is responsible for
987 * copying the data in this array into the correct place in the
988 * request and chain buffers.
989 */
990 sglbuf = pci_alloc_consistent(ioc->pcidev, MAX_SGL_BYTES, sglbuf_dma);
991 if (sglbuf == NULL)
992 goto free_and_fail;
993
994 if (sgdir & 0x04000000)
995 dir = PCI_DMA_TODEVICE;
996 else
997 dir = PCI_DMA_FROMDEVICE;
998
999 /* At start:
1000 * sgl = sglbuf = point to beginning of sg buffer
1001 * buflist_ent = 0 = first kernel structure
1002 * sg_spill = number of SGE that can be written before the first
1003 * chain element.
1004 *
1005 */
1006 sgl = sglbuf;
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05301007 sg_spill = ((ioc->req_sz - sge_offset)/ioc->SGE_size) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 while (bytes_allocd < bytes) {
1009 this_alloc = min(alloc_sz, bytes-bytes_allocd);
1010 buflist[buflist_ent].len = this_alloc;
1011 buflist[buflist_ent].kptr = pci_alloc_consistent(ioc->pcidev,
1012 this_alloc,
1013 &pa);
1014 if (buflist[buflist_ent].kptr == NULL) {
1015 alloc_sz = alloc_sz / 2;
1016 if (alloc_sz == 0) {
Eric Moore29dd3602007-09-14 18:46:51 -06001017 printk(MYIOC_s_WARN_FMT "-SG: No can do - "
1018 "not enough memory! :-(\n", ioc->name);
1019 printk(MYIOC_s_WARN_FMT "-SG: (freeing %d frags)\n",
1020 ioc->name, numfrags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto free_and_fail;
1022 }
1023 continue;
1024 } else {
1025 dma_addr_t dma_addr;
1026
1027 bytes_allocd += this_alloc;
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05301028 sgl->FlagsLength = (0x10000000|sgdir|this_alloc);
1029 dma_addr = pci_map_single(ioc->pcidev,
1030 buflist[buflist_ent].kptr, this_alloc, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 sgl->Address = dma_addr;
1032
1033 fragcnt++;
1034 numfrags++;
1035 sgl++;
1036 buflist_ent++;
1037 }
1038
1039 if (bytes_allocd >= bytes)
1040 break;
1041
1042 /* Need to chain? */
1043 if (fragcnt == sg_spill) {
Eric Moore29dd3602007-09-14 18:46:51 -06001044 printk(MYIOC_s_WARN_FMT
1045 "-SG: No can do - " "Chain required! :-(\n", ioc->name);
1046 printk(MYIOC_s_WARN_FMT "(freeing %d frags)\n", ioc->name, numfrags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto free_and_fail;
1048 }
1049
1050 /* overflow check... */
1051 if (numfrags*8 > MAX_SGL_BYTES){
1052 /* GRRRRR... */
Eric Moore29dd3602007-09-14 18:46:51 -06001053 printk(MYIOC_s_WARN_FMT "-SG: No can do - "
1054 "too many SG frags! :-(\n", ioc->name);
1055 printk(MYIOC_s_WARN_FMT "-SG: (freeing %d frags)\n",
1056 ioc->name, numfrags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 goto free_and_fail;
1058 }
1059 }
1060
1061 /* Last sge fixup: set LE+eol+eob bits */
1062 sgl[-1].FlagsLength |= 0xC1000000;
1063
1064 *frags = numfrags;
1065 *blp = buflist;
1066
Prakash, Sathya09120a82007-07-24 15:49:05 +05301067 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "-SG: kbuf_alloc_2_sgl() - "
1068 "%d SG frags generated!\n", ioc->name, numfrags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Prakash, Sathya09120a82007-07-24 15:49:05 +05301070 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "-SG: kbuf_alloc_2_sgl() - "
1071 "last (big) alloc_sz=%d\n", ioc->name, alloc_sz));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 return sglbuf;
1074
1075free_and_fail:
1076 if (sglbuf != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 for (i = 0; i < numfrags; i++) {
1078 dma_addr_t dma_addr;
1079 u8 *kptr;
1080 int len;
1081
1082 if ((sglbuf[i].FlagsLength >> 24) == 0x30)
1083 continue;
1084
1085 dma_addr = sglbuf[i].Address;
1086 kptr = buflist[i].kptr;
1087 len = buflist[i].len;
1088
1089 pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1090 }
1091 pci_free_consistent(ioc->pcidev, MAX_SGL_BYTES, sglbuf, *sglbuf_dma);
1092 }
1093 kfree(buflist);
1094 return NULL;
1095}
1096
1097/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1098/*
1099 * Routine to free the SGL elements.
1100 */
1101static void
1102kfree_sgl(MptSge_t *sgl, dma_addr_t sgl_dma, struct buflist *buflist, MPT_ADAPTER *ioc)
1103{
1104 MptSge_t *sg = sgl;
1105 struct buflist *bl = buflist;
1106 u32 nib;
1107 int dir;
1108 int n = 0;
1109
1110 if (sg->FlagsLength & 0x04000000)
1111 dir = PCI_DMA_TODEVICE;
1112 else
1113 dir = PCI_DMA_FROMDEVICE;
1114
1115 nib = (sg->FlagsLength & 0xF0000000) >> 28;
1116 while (! (nib & 0x4)) { /* eob */
1117 /* skip ignore/chain. */
1118 if (nib == 0 || nib == 3) {
1119 ;
1120 } else if (sg->Address) {
1121 dma_addr_t dma_addr;
1122 void *kptr;
1123 int len;
1124
1125 dma_addr = sg->Address;
1126 kptr = bl->kptr;
1127 len = bl->len;
1128 pci_unmap_single(ioc->pcidev, dma_addr, len, dir);
1129 pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1130 n++;
1131 }
1132 sg++;
1133 bl++;
1134 nib = (le32_to_cpu(sg->FlagsLength) & 0xF0000000) >> 28;
1135 }
1136
1137 /* we're at eob! */
1138 if (sg->Address) {
1139 dma_addr_t dma_addr;
1140 void *kptr;
1141 int len;
1142
1143 dma_addr = sg->Address;
1144 kptr = bl->kptr;
1145 len = bl->len;
1146 pci_unmap_single(ioc->pcidev, dma_addr, len, dir);
1147 pci_free_consistent(ioc->pcidev, len, kptr, dma_addr);
1148 n++;
1149 }
1150
1151 pci_free_consistent(ioc->pcidev, MAX_SGL_BYTES, sgl, sgl_dma);
1152 kfree(buflist);
Prakash, Sathya09120a82007-07-24 15:49:05 +05301153 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "-SG: Free'd 1 SGL buf + %d kbufs!\n",
1154 ioc->name, n));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
1157/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1158/*
1159 * mptctl_getiocinfo - Query the host adapter for IOC information.
1160 * @arg: User space argument
1161 *
1162 * Outputs: None.
1163 * Return: 0 if successful
1164 * -EFAULT if data unavailable
1165 * -ENODEV if no such device/adapter
1166 */
1167static int
1168mptctl_getiocinfo (unsigned long arg, unsigned int data_size)
1169{
1170 struct mpt_ioctl_iocinfo __user *uarg = (void __user *) arg;
1171 struct mpt_ioctl_iocinfo *karg;
1172 MPT_ADAPTER *ioc;
1173 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int iocnum;
Moore, Eric Dean b6fe4dd2005-04-22 18:01:34 -04001175 unsigned int port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int cim_rev;
1177 u8 revision;
Eric Moore793955f2007-01-29 09:42:20 -07001178 struct scsi_device *sdev;
Eric Moorea69de502007-09-14 18:48:19 -06001179 VirtDevice *vdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* Add of PCI INFO results in unaligned access for
1182 * IA64 and Sparc. Reset long to int. Return no PCI
1183 * data for obsolete format.
1184 */
1185 if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev0))
1186 cim_rev = 0;
1187 else if (data_size == sizeof(struct mpt_ioctl_iocinfo_rev1))
1188 cim_rev = 1;
1189 else if (data_size == sizeof(struct mpt_ioctl_iocinfo))
1190 cim_rev = 2;
1191 else if (data_size == (sizeof(struct mpt_ioctl_iocinfo_rev0)+12))
1192 cim_rev = 0; /* obsolete */
1193 else
1194 return -EFAULT;
1195
1196 karg = kmalloc(data_size, GFP_KERNEL);
1197 if (karg == NULL) {
Eric Moore29dd3602007-09-14 18:46:51 -06001198 printk(KERN_ERR MYNAM "%s::mpt_ioctl_iocinfo() @%d - no memory available!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 __FILE__, __LINE__);
1200 return -ENOMEM;
1201 }
1202
1203 if (copy_from_user(karg, uarg, data_size)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001204 printk(KERN_ERR MYNAM "%s@%d::mptctl_getiocinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 "Unable to read in mpt_ioctl_iocinfo struct @ %p\n",
1206 __FILE__, __LINE__, uarg);
1207 kfree(karg);
1208 return -EFAULT;
1209 }
1210
1211 if (((iocnum = mpt_verify_adapter(karg->hdr.iocnum, &ioc)) < 0) ||
1212 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001213 printk(KERN_DEBUG MYNAM "%s::mptctl_getiocinfo() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301214 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 kfree(karg);
1216 return -ENODEV;
1217 }
1218
Moore, Eric Dean b6fe4dd2005-04-22 18:01:34 -04001219 /* Verify the data transfer size is correct. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (karg->hdr.maxDataSize != data_size) {
Eric Moore29dd3602007-09-14 18:46:51 -06001221 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_getiocinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 "Structure size mismatch. Command not completed.\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001223 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 kfree(karg);
1225 return -EFAULT;
1226 }
1227
Prakash, Sathya09120a82007-07-24 15:49:05 +05301228 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_getiocinfo called.\n",
1229 ioc->name));
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* Fill in the data and return the structure to the calling
1232 * program
1233 */
Moore, Eric9cc1cfb2006-02-02 17:19:33 -07001234 if (ioc->bus_type == SAS)
1235 karg->adapterType = MPT_IOCTL_INTERFACE_SAS;
1236 else if (ioc->bus_type == FC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 karg->adapterType = MPT_IOCTL_INTERFACE_FC;
1238 else
1239 karg->adapterType = MPT_IOCTL_INTERFACE_SCSI;
1240
Moore, Eric Dean b6fe4dd2005-04-22 18:01:34 -04001241 if (karg->hdr.port > 1)
1242 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 port = karg->hdr.port;
1244
1245 karg->port = port;
1246 pdev = (struct pci_dev *) ioc->pcidev;
1247
1248 karg->pciId = pdev->device;
1249 pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
1250 karg->hwRev = revision;
1251 karg->subSystemDevice = pdev->subsystem_device;
1252 karg->subSystemVendor = pdev->subsystem_vendor;
1253
1254 if (cim_rev == 1) {
1255 /* Get the PCI bus, device, and function numbers for the IOC
1256 */
1257 karg->pciInfo.u.bits.busNumber = pdev->bus->number;
1258 karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
1259 karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
1260 } else if (cim_rev == 2) {
Moore, Eric86a7dca2006-02-02 17:19:37 -07001261 /* Get the PCI bus, device, function and segment ID numbers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 for the IOC */
1263 karg->pciInfo.u.bits.busNumber = pdev->bus->number;
1264 karg->pciInfo.u.bits.deviceNumber = PCI_SLOT( pdev->devfn );
1265 karg->pciInfo.u.bits.functionNumber = PCI_FUNC( pdev->devfn );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 karg->pciInfo.segmentID = pci_domain_nr(pdev->bus);
1267 }
1268
1269 /* Get number of devices
1270 */
Eric Moore793955f2007-01-29 09:42:20 -07001271 karg->numDevices = 0;
1272 if (ioc->sh) {
1273 shost_for_each_device(sdev, ioc->sh) {
Eric Moorea69de502007-09-14 18:48:19 -06001274 vdevice = sdev->hostdata;
1275 if (vdevice->vtarget->tflags &
Eric Moore793955f2007-01-29 09:42:20 -07001276 MPT_TARGET_FLAGS_RAID_COMPONENT)
1277 continue;
1278 karg->numDevices++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /* Set the BIOS and FW Version
1283 */
1284 karg->FWVersion = ioc->facts.FWVersion.Word;
1285 karg->BIOSVersion = ioc->biosVersion;
1286
1287 /* Set the Version Strings.
1288 */
1289 strncpy (karg->driverVersion, MPT_LINUX_PACKAGE_NAME, MPT_IOCTL_VERSION_LENGTH);
1290 karg->driverVersion[MPT_IOCTL_VERSION_LENGTH-1]='\0';
1291
1292 karg->busChangeEvent = 0;
1293 karg->hostId = ioc->pfacts[port].PortSCSIID;
1294 karg->rsvd[0] = karg->rsvd[1] = 0;
1295
1296 /* Copy the data from kernel memory to user memory
1297 */
1298 if (copy_to_user((char __user *)arg, karg, data_size)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001299 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_getiocinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 "Unable to write out mpt_ioctl_iocinfo struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001301 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 kfree(karg);
1303 return -EFAULT;
1304 }
1305
1306 kfree(karg);
1307 return 0;
1308}
1309
1310/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1311/*
1312 * mptctl_gettargetinfo - Query the host adapter for target information.
1313 * @arg: User space argument
1314 *
1315 * Outputs: None.
1316 * Return: 0 if successful
1317 * -EFAULT if data unavailable
1318 * -ENODEV if no such device/adapter
1319 */
1320static int
1321mptctl_gettargetinfo (unsigned long arg)
1322{
1323 struct mpt_ioctl_targetinfo __user *uarg = (void __user *) arg;
1324 struct mpt_ioctl_targetinfo karg;
1325 MPT_ADAPTER *ioc;
Eric Moorea69de502007-09-14 18:48:19 -06001326 VirtDevice *vdevice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 char *pmem;
1328 int *pdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int iocnum;
1330 int numDevices = 0;
Eric Moore793955f2007-01-29 09:42:20 -07001331 int lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int maxWordsLeft;
1333 int numBytes;
Eric Moore793955f2007-01-29 09:42:20 -07001334 u8 port;
1335 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_targetinfo))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001338 printk(KERN_ERR MYNAM "%s@%d::mptctl_gettargetinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 "Unable to read in mpt_ioctl_targetinfo struct @ %p\n",
1340 __FILE__, __LINE__, uarg);
1341 return -EFAULT;
1342 }
1343
1344 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1345 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001346 printk(KERN_DEBUG MYNAM "%s::mptctl_gettargetinfo() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301347 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -ENODEV;
1349 }
1350
Prakash, Sathya09120a82007-07-24 15:49:05 +05301351 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_gettargetinfo called.\n",
1352 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* Get the port number and set the maximum number of bytes
1354 * in the returned structure.
1355 * Ignore the port setting.
1356 */
1357 numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
1358 maxWordsLeft = numBytes/sizeof(int);
1359 port = karg.hdr.port;
1360
1361 if (maxWordsLeft <= 0) {
Eric Moore29dd3602007-09-14 18:46:51 -06001362 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_gettargetinfo() - no memory available!\n",
1363 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return -ENOMEM;
1365 }
1366
1367 /* Fill in the data and return the structure to the calling
1368 * program
1369 */
1370
1371 /* struct mpt_ioctl_targetinfo does not contain sufficient space
1372 * for the target structures so when the IOCTL is called, there is
1373 * not sufficient stack space for the structure. Allocate memory,
1374 * populate the memory, copy back to the user, then free memory.
1375 * targetInfo format:
1376 * bits 31-24: reserved
1377 * 23-16: LUN
1378 * 15- 8: Bus Number
1379 * 7- 0: Target ID
1380 */
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07001381 pmem = kzalloc(numBytes, GFP_KERNEL);
1382 if (!pmem) {
Eric Moore29dd3602007-09-14 18:46:51 -06001383 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_gettargetinfo() - no memory available!\n",
1384 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -ENOMEM;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 pdata = (int *) pmem;
1388
1389 /* Get number of devices
1390 */
Eric Moore793955f2007-01-29 09:42:20 -07001391 if (ioc->sh){
1392 shost_for_each_device(sdev, ioc->sh) {
1393 if (!maxWordsLeft)
1394 continue;
Eric Moorea69de502007-09-14 18:48:19 -06001395 vdevice = sdev->hostdata;
1396 if (vdevice->vtarget->tflags &
Eric Moore793955f2007-01-29 09:42:20 -07001397 MPT_TARGET_FLAGS_RAID_COMPONENT)
1398 continue;
Eric Moorea69de502007-09-14 18:48:19 -06001399 lun = (vdevice->vtarget->raidVolume) ? 0x80 : vdevice->lun;
1400 *pdata = (((u8)lun << 16) + (vdevice->vtarget->channel << 8) +
1401 (vdevice->vtarget->id ));
Eric Moore793955f2007-01-29 09:42:20 -07001402 pdata++;
1403 numDevices++;
1404 --maxWordsLeft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 karg.numDevices = numDevices;
1408
1409 /* Copy part of the data from kernel memory to user memory
1410 */
1411 if (copy_to_user((char __user *)arg, &karg,
1412 sizeof(struct mpt_ioctl_targetinfo))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001413 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_gettargetinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001415 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 kfree(pmem);
1417 return -EFAULT;
1418 }
1419
1420 /* Copy the remaining data from kernel memory to user memory
1421 */
1422 if (copy_to_user(uarg->targetInfo, pmem, numBytes)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001423 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_gettargetinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001425 ioc->name, __FILE__, __LINE__, pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 kfree(pmem);
1427 return -EFAULT;
1428 }
1429
1430 kfree(pmem);
1431
1432 return 0;
1433}
1434
1435/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1436/* MPT IOCTL Test function.
1437 *
1438 * Outputs: None.
1439 * Return: 0 if successful
1440 * -EFAULT if data unavailable
1441 * -ENODEV if no such device/adapter
1442 */
1443static int
1444mptctl_readtest (unsigned long arg)
1445{
1446 struct mpt_ioctl_test __user *uarg = (void __user *) arg;
1447 struct mpt_ioctl_test karg;
1448 MPT_ADAPTER *ioc;
1449 int iocnum;
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_test))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001452 printk(KERN_ERR MYNAM "%s@%d::mptctl_readtest - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 "Unable to read in mpt_ioctl_test struct @ %p\n",
1454 __FILE__, __LINE__, uarg);
1455 return -EFAULT;
1456 }
1457
1458 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1459 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001460 printk(KERN_DEBUG MYNAM "%s::mptctl_readtest() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301461 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return -ENODEV;
1463 }
1464
Prakash, Sathya09120a82007-07-24 15:49:05 +05301465 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_readtest called.\n",
1466 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /* Fill in the data and return the structure to the calling
1468 * program
1469 */
1470
1471#ifdef MFCNT
1472 karg.chip_type = ioc->mfcnt;
1473#else
1474 karg.chip_type = ioc->pcidev->device;
1475#endif
1476 strncpy (karg.name, ioc->name, MPT_MAX_NAME);
1477 karg.name[MPT_MAX_NAME-1]='\0';
1478 strncpy (karg.product, ioc->prod_name, MPT_PRODUCT_LENGTH);
1479 karg.product[MPT_PRODUCT_LENGTH-1]='\0';
1480
1481 /* Copy the data from kernel memory to user memory
1482 */
1483 if (copy_to_user((char __user *)arg, &karg, sizeof(struct mpt_ioctl_test))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001484 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_readtest - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 "Unable to write out mpt_ioctl_test struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001486 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return -EFAULT;
1488 }
1489
1490 return 0;
1491}
1492
1493/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1494/*
1495 * mptctl_eventquery - Query the host adapter for the event types
1496 * that are being logged.
1497 * @arg: User space argument
1498 *
1499 * Outputs: None.
1500 * Return: 0 if successful
1501 * -EFAULT if data unavailable
1502 * -ENODEV if no such device/adapter
1503 */
1504static int
1505mptctl_eventquery (unsigned long arg)
1506{
1507 struct mpt_ioctl_eventquery __user *uarg = (void __user *) arg;
1508 struct mpt_ioctl_eventquery karg;
1509 MPT_ADAPTER *ioc;
1510 int iocnum;
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventquery))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001513 printk(KERN_ERR MYNAM "%s@%d::mptctl_eventquery - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 "Unable to read in mpt_ioctl_eventquery struct @ %p\n",
1515 __FILE__, __LINE__, uarg);
1516 return -EFAULT;
1517 }
1518
1519 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1520 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001521 printk(KERN_DEBUG MYNAM "%s::mptctl_eventquery() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301522 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -ENODEV;
1524 }
1525
Prakash, Sathya09120a82007-07-24 15:49:05 +05301526 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_eventquery called.\n",
1527 ioc->name));
Moore, Eric5b5ef4f2006-02-02 17:19:40 -07001528 karg.eventEntries = MPTCTL_EVENT_LOG_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 karg.eventTypes = ioc->eventTypes;
1530
1531 /* Copy the data from kernel memory to user memory
1532 */
1533 if (copy_to_user((char __user *)arg, &karg, sizeof(struct mpt_ioctl_eventquery))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001534 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_eventquery - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 "Unable to write out mpt_ioctl_eventquery struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001536 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return -EFAULT;
1538 }
1539 return 0;
1540}
1541
1542/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1543static int
1544mptctl_eventenable (unsigned long arg)
1545{
1546 struct mpt_ioctl_eventenable __user *uarg = (void __user *) arg;
1547 struct mpt_ioctl_eventenable karg;
1548 MPT_ADAPTER *ioc;
1549 int iocnum;
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventenable))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001552 printk(KERN_ERR MYNAM "%s@%d::mptctl_eventenable - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 "Unable to read in mpt_ioctl_eventenable struct @ %p\n",
1554 __FILE__, __LINE__, uarg);
1555 return -EFAULT;
1556 }
1557
1558 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1559 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001560 printk(KERN_DEBUG MYNAM "%s::mptctl_eventenable() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301561 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return -ENODEV;
1563 }
1564
Prakash, Sathya09120a82007-07-24 15:49:05 +05301565 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_eventenable called.\n",
1566 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (ioc->events == NULL) {
1568 /* Have not yet allocated memory - do so now.
1569 */
1570 int sz = MPTCTL_EVENT_LOG_SIZE * sizeof(MPT_IOCTL_EVENTS);
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07001571 ioc->events = kzalloc(sz, GFP_KERNEL);
1572 if (!ioc->events) {
Eric Moore29dd3602007-09-14 18:46:51 -06001573 printk(MYIOC_s_ERR_FMT
1574 ": ERROR - Insufficient memory to add adapter!\n",
1575 ioc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return -ENOMEM;
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ioc->alloc_total += sz;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 ioc->eventContext = 0;
1581 }
1582
1583 /* Update the IOC event logging flag.
1584 */
1585 ioc->eventTypes = karg.eventTypes;
1586
1587 return 0;
1588}
1589
1590/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1591static int
1592mptctl_eventreport (unsigned long arg)
1593{
1594 struct mpt_ioctl_eventreport __user *uarg = (void __user *) arg;
1595 struct mpt_ioctl_eventreport karg;
1596 MPT_ADAPTER *ioc;
1597 int iocnum;
1598 int numBytes, maxEvents, max;
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_eventreport))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001601 printk(KERN_ERR MYNAM "%s@%d::mptctl_eventreport - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 "Unable to read in mpt_ioctl_eventreport struct @ %p\n",
1603 __FILE__, __LINE__, uarg);
1604 return -EFAULT;
1605 }
1606
1607 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1608 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001609 printk(KERN_DEBUG MYNAM "%s::mptctl_eventreport() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301610 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return -ENODEV;
1612 }
Prakash, Sathya09120a82007-07-24 15:49:05 +05301613 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_eventreport called.\n",
1614 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 numBytes = karg.hdr.maxDataSize - sizeof(mpt_ioctl_header);
1617 maxEvents = numBytes/sizeof(MPT_IOCTL_EVENTS);
1618
1619
Moore, Eric5b5ef4f2006-02-02 17:19:40 -07001620 max = MPTCTL_EVENT_LOG_SIZE < maxEvents ? MPTCTL_EVENT_LOG_SIZE : maxEvents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 /* If fewer than 1 event is requested, there must have
1623 * been some type of error.
1624 */
1625 if ((max < 1) || !ioc->events)
1626 return -ENODATA;
1627
Moore, Ericea5a7a82006-02-02 17:20:01 -07001628 /* reset this flag so SIGIO can restart */
1629 ioc->aen_event_read_flag=0;
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /* Copy the data from kernel memory to user memory
1632 */
1633 numBytes = max * sizeof(MPT_IOCTL_EVENTS);
1634 if (copy_to_user(uarg->eventData, ioc->events, numBytes)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001635 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_eventreport - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 "Unable to write out mpt_ioctl_eventreport struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001637 ioc->name, __FILE__, __LINE__, ioc->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return -EFAULT;
1639 }
1640
1641 return 0;
1642}
1643
1644/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1645static int
1646mptctl_replace_fw (unsigned long arg)
1647{
1648 struct mpt_ioctl_replace_fw __user *uarg = (void __user *) arg;
1649 struct mpt_ioctl_replace_fw karg;
1650 MPT_ADAPTER *ioc;
1651 int iocnum;
1652 int newFwSize;
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_replace_fw))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001655 printk(KERN_ERR MYNAM "%s@%d::mptctl_replace_fw - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 "Unable to read in mpt_ioctl_replace_fw struct @ %p\n",
1657 __FILE__, __LINE__, uarg);
1658 return -EFAULT;
1659 }
1660
1661 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1662 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001663 printk(KERN_DEBUG MYNAM "%s::mptctl_replace_fw() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301664 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return -ENODEV;
1666 }
1667
Prakash, Sathya09120a82007-07-24 15:49:05 +05301668 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_replace_fw called.\n",
1669 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /* If caching FW, Free the old FW image
1671 */
1672 if (ioc->cached_fw == NULL)
1673 return 0;
1674
1675 mpt_free_fw_memory(ioc);
1676
1677 /* Allocate memory for the new FW image
1678 */
1679 newFwSize = karg.newImageSize;
1680
1681 if (newFwSize & 0x01)
1682 newFwSize += 1;
1683 if (newFwSize & 0x02)
1684 newFwSize += 2;
1685
1686 mpt_alloc_fw_memory(ioc, newFwSize);
1687 if (ioc->cached_fw == NULL)
1688 return -ENOMEM;
1689
1690 /* Copy the data from user memory to kernel space
1691 */
1692 if (copy_from_user(ioc->cached_fw, uarg->newImage, newFwSize)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001693 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_replace_fw - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 "Unable to read in mpt_ioctl_replace_fw image "
Eric Moore29dd3602007-09-14 18:46:51 -06001695 "@ %p\n", ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 mpt_free_fw_memory(ioc);
1697 return -EFAULT;
1698 }
1699
1700 /* Update IOCFactsReply
1701 */
1702 ioc->facts.FWImageSize = newFwSize;
1703 return 0;
1704}
1705
1706/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1707/* MPT IOCTL MPTCOMMAND function.
1708 * Cast the arg into the mpt_ioctl_mpt_command structure.
1709 *
1710 * Outputs: None.
1711 * Return: 0 if successful
Joe Perchesfc1323b2008-02-03 17:21:01 +02001712 * -EBUSY if previous command timeout and IOC reset is not complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 * -EFAULT if data unavailable
1714 * -ENODEV if no such device/adapter
1715 * -ETIME if timer expires
1716 * -ENOMEM if memory allocation error
1717 */
1718static int
1719mptctl_mpt_command (unsigned long arg)
1720{
1721 struct mpt_ioctl_command __user *uarg = (void __user *) arg;
1722 struct mpt_ioctl_command karg;
1723 MPT_ADAPTER *ioc;
1724 int iocnum;
1725 int rc;
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 if (copy_from_user(&karg, uarg, sizeof(struct mpt_ioctl_command))) {
Eric Moore29dd3602007-09-14 18:46:51 -06001729 printk(KERN_ERR MYNAM "%s@%d::mptctl_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 "Unable to read in mpt_ioctl_command struct @ %p\n",
1731 __FILE__, __LINE__, uarg);
1732 return -EFAULT;
1733 }
1734
1735 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1736 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001737 printk(KERN_DEBUG MYNAM "%s::mptctl_mpt_command() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301738 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return -ENODEV;
1740 }
1741
1742 rc = mptctl_do_mpt_command (karg, &uarg->MF);
1743
1744 return rc;
1745}
1746
1747/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1748/* Worker routine for the IOCTL MPTCOMMAND and MPTCOMMAND32 (sparc) commands.
1749 *
1750 * Outputs: None.
1751 * Return: 0 if successful
Joe Perchesfc1323b2008-02-03 17:21:01 +02001752 * -EBUSY if previous command timeout and IOC reset is not complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 * -EFAULT if data unavailable
1754 * -ENODEV if no such device/adapter
1755 * -ETIME if timer expires
1756 * -ENOMEM if memory allocation error
1757 * -EPERM if SCSI I/O and target is untagged
1758 */
1759static int
1760mptctl_do_mpt_command (struct mpt_ioctl_command karg, void __user *mfPtr)
1761{
1762 MPT_ADAPTER *ioc;
1763 MPT_FRAME_HDR *mf = NULL;
1764 MPIHeader_t *hdr;
1765 char *psge;
1766 struct buflist bufIn; /* data In buffer */
1767 struct buflist bufOut; /* data Out buffer */
1768 dma_addr_t dma_addr_in;
1769 dma_addr_t dma_addr_out;
1770 int sgSize = 0; /* Num SG elements */
1771 int iocnum, flagsLength;
1772 int sz, rc = 0;
1773 int msgContext;
1774 u16 req_idx;
1775 ulong timeout;
Eric Moore793955f2007-01-29 09:42:20 -07001776 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Eric Mooreab371282007-09-29 10:22:54 -06001778 /* bufIn and bufOut are used for user to kernel space transfers
1779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 bufIn.kptr = bufOut.kptr = NULL;
Eric Mooreab371282007-09-29 10:22:54 -06001781 bufIn.len = bufOut.len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
1784 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001785 printk(KERN_DEBUG MYNAM "%s::mptctl_do_mpt_command() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05301786 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return -ENODEV;
1788 }
1789 if (!ioc->ioctl) {
Eric Moore29dd3602007-09-14 18:46:51 -06001790 printk(KERN_ERR MYNAM "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 "No memory available during driver init.\n",
1792 __FILE__, __LINE__);
1793 return -ENOMEM;
1794 } else if (ioc->ioctl->status & MPT_IOCTL_STATUS_DID_IOCRESET) {
Eric Moore29dd3602007-09-14 18:46:51 -06001795 printk(KERN_ERR MYNAM "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 "Busy with IOC Reset \n", __FILE__, __LINE__);
1797 return -EBUSY;
1798 }
1799
1800 /* Verify that the final request frame will not be too large.
1801 */
1802 sz = karg.dataSgeOffset * 4;
1803 if (karg.dataInSize > 0)
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05301804 sz += ioc->SGE_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (karg.dataOutSize > 0)
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05301806 sz += ioc->SGE_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 if (sz > ioc->req_sz) {
Eric Moore29dd3602007-09-14 18:46:51 -06001809 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 "Request frame too large (%d) maximum (%d)\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001811 ioc->name, __FILE__, __LINE__, sz, ioc->req_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return -EFAULT;
1813 }
1814
1815 /* Get a free request frame and save the message context.
1816 */
1817 if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL)
1818 return -EAGAIN;
1819
1820 hdr = (MPIHeader_t *) mf;
1821 msgContext = le32_to_cpu(hdr->MsgContext);
1822 req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
1823
1824 /* Copy the request frame
1825 * Reset the saved message context.
1826 * Request frame in user space
1827 */
1828 if (copy_from_user(mf, mfPtr, karg.dataSgeOffset * 4)) {
Eric Moore29dd3602007-09-14 18:46:51 -06001829 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 "Unable to read MF from mpt_ioctl_command struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06001831 ioc->name, __FILE__, __LINE__, mfPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 rc = -EFAULT;
1833 goto done_free_mem;
1834 }
1835 hdr->MsgContext = cpu_to_le32(msgContext);
1836
1837
1838 /* Verify that this request is allowed.
1839 */
Prakash, Sathya09120a82007-07-24 15:49:05 +05301840 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "sending mpi function (0x%02X), req=%p\n",
1841 ioc->name, hdr->Function, mf));
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 switch (hdr->Function) {
1844 case MPI_FUNCTION_IOC_FACTS:
1845 case MPI_FUNCTION_PORT_FACTS:
1846 karg.dataOutSize = karg.dataInSize = 0;
1847 break;
1848
1849 case MPI_FUNCTION_CONFIG:
Prakash, Sathya09120a82007-07-24 15:49:05 +05301850 {
1851 Config_t *config_frame;
1852 config_frame = (Config_t *)mf;
1853 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "\ttype=0x%02x ext_type=0x%02x "
1854 "number=0x%02x action=0x%02x\n", ioc->name,
1855 config_frame->Header.PageType,
1856 config_frame->ExtPageType,
1857 config_frame->Header.PageNumber,
1858 config_frame->Action));
1859 break;
1860 }
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 case MPI_FUNCTION_FC_COMMON_TRANSPORT_SEND:
1863 case MPI_FUNCTION_FC_EX_LINK_SRVC_SEND:
1864 case MPI_FUNCTION_FW_UPLOAD:
1865 case MPI_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
1866 case MPI_FUNCTION_FW_DOWNLOAD:
1867 case MPI_FUNCTION_FC_PRIMITIVE_SEND:
Moore, Eric096f7a22006-02-02 17:19:30 -07001868 case MPI_FUNCTION_TOOLBOX:
1869 case MPI_FUNCTION_SAS_IO_UNIT_CONTROL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 break;
1871
1872 case MPI_FUNCTION_SCSI_IO_REQUEST:
1873 if (ioc->sh) {
1874 SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 int qtag = MPI_SCSIIO_CONTROL_UNTAGGED;
1876 int scsidir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 int dataSize;
Eric Moore793955f2007-01-29 09:42:20 -07001878 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Eric Moore793955f2007-01-29 09:42:20 -07001880 id = (ioc->devices_per_bus == 0) ? 256 : ioc->devices_per_bus;
1881 if (pScsiReq->TargetID > id) {
Eric Moore29dd3602007-09-14 18:46:51 -06001882 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 "Target ID out of bounds. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06001884 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 rc = -ENODEV;
1886 goto done_free_mem;
1887 }
1888
Eric Moore793955f2007-01-29 09:42:20 -07001889 if (pScsiReq->Bus >= ioc->number_of_buses) {
Eric Moore29dd3602007-09-14 18:46:51 -06001890 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Eric Moore793955f2007-01-29 09:42:20 -07001891 "Target Bus out of bounds. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06001892 ioc->name, __FILE__, __LINE__);
Eric Moore793955f2007-01-29 09:42:20 -07001893 rc = -ENODEV;
1894 goto done_free_mem;
1895 }
1896
Moore, Eric5f07e242006-02-02 17:19:44 -07001897 pScsiReq->MsgFlags &= ~MPI_SCSIIO_MSGFLGS_SENSE_WIDTH;
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05301898 pScsiReq->MsgFlags |= mpt_msg_flags(ioc);
Moore, Eric5f07e242006-02-02 17:19:44 -07001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /* verify that app has not requested
1902 * more sense data than driver
1903 * can provide, if so, reset this parameter
1904 * set the sense buffer pointer low address
1905 * update the control field to specify Q type
1906 */
1907 if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
1908 pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
1909 else
1910 pScsiReq->SenseBufferLength = karg.maxSenseBytes;
1911
1912 pScsiReq->SenseBufferLowAddr =
1913 cpu_to_le32(ioc->sense_buf_low_dma
1914 + (req_idx * MPT_SENSE_BUFFER_ALLOC));
1915
Eric Moore793955f2007-01-29 09:42:20 -07001916 shost_for_each_device(sdev, ioc->sh) {
1917 struct scsi_target *starget = scsi_target(sdev);
1918 VirtTarget *vtarget = starget->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Eric Moore793955f2007-01-29 09:42:20 -07001920 if ((pScsiReq->TargetID == vtarget->id) &&
1921 (pScsiReq->Bus == vtarget->channel) &&
1922 (vtarget->tflags & MPT_TARGET_FLAGS_Q_YES))
1923 qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
1924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 /* Have the IOCTL driver set the direction based
1927 * on the dataOutSize (ordering issue with Sparc).
1928 */
1929 if (karg.dataOutSize > 0) {
1930 scsidir = MPI_SCSIIO_CONTROL_WRITE;
1931 dataSize = karg.dataOutSize;
1932 } else {
1933 scsidir = MPI_SCSIIO_CONTROL_READ;
1934 dataSize = karg.dataInSize;
1935 }
1936
1937 pScsiReq->Control = cpu_to_le32(scsidir | qtag);
1938 pScsiReq->DataLength = cpu_to_le32(dataSize);
1939
1940 ioc->ioctl->reset = MPTCTL_RESET_OK;
Eric Moore793955f2007-01-29 09:42:20 -07001941 ioc->ioctl->id = pScsiReq->TargetID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 } else {
Eric Moore29dd3602007-09-14 18:46:51 -06001944 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 "SCSI driver is not loaded. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06001946 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 rc = -EFAULT;
1948 goto done_free_mem;
1949 }
1950 break;
1951
Moore, Eric096f7a22006-02-02 17:19:30 -07001952 case MPI_FUNCTION_SMP_PASSTHROUGH:
1953 /* Check mf->PassthruFlags to determine if
1954 * transfer is ImmediateMode or not.
1955 * Immediate mode returns data in the ReplyFrame.
1956 * Else, we are sending request and response data
1957 * in two SGLs at the end of the mf.
1958 */
1959 break;
1960
1961 case MPI_FUNCTION_SATA_PASSTHROUGH:
1962 if (!ioc->sh) {
Eric Moore29dd3602007-09-14 18:46:51 -06001963 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Moore, Eric096f7a22006-02-02 17:19:30 -07001964 "SCSI driver is not loaded. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06001965 ioc->name, __FILE__, __LINE__);
Moore, Eric096f7a22006-02-02 17:19:30 -07001966 rc = -EFAULT;
1967 goto done_free_mem;
1968 }
1969 break;
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 case MPI_FUNCTION_RAID_ACTION:
1972 /* Just add a SGE
1973 */
1974 break;
1975
1976 case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
1977 if (ioc->sh) {
1978 SCSIIORequest_t *pScsiReq = (SCSIIORequest_t *) mf;
1979 int qtag = MPI_SCSIIO_CONTROL_SIMPLEQ;
1980 int scsidir = MPI_SCSIIO_CONTROL_READ;
1981 int dataSize;
1982
Moore, Eric5f07e242006-02-02 17:19:44 -07001983 pScsiReq->MsgFlags &= ~MPI_SCSIIO_MSGFLGS_SENSE_WIDTH;
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05301984 pScsiReq->MsgFlags |= mpt_msg_flags(ioc);
Moore, Eric5f07e242006-02-02 17:19:44 -07001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 /* verify that app has not requested
1988 * more sense data than driver
1989 * can provide, if so, reset this parameter
1990 * set the sense buffer pointer low address
1991 * update the control field to specify Q type
1992 */
1993 if (karg.maxSenseBytes > MPT_SENSE_BUFFER_SIZE)
1994 pScsiReq->SenseBufferLength = MPT_SENSE_BUFFER_SIZE;
1995 else
1996 pScsiReq->SenseBufferLength = karg.maxSenseBytes;
1997
1998 pScsiReq->SenseBufferLowAddr =
1999 cpu_to_le32(ioc->sense_buf_low_dma
2000 + (req_idx * MPT_SENSE_BUFFER_ALLOC));
2001
2002 /* All commands to physical devices are tagged
2003 */
2004
2005 /* Have the IOCTL driver set the direction based
2006 * on the dataOutSize (ordering issue with Sparc).
2007 */
2008 if (karg.dataOutSize > 0) {
2009 scsidir = MPI_SCSIIO_CONTROL_WRITE;
2010 dataSize = karg.dataOutSize;
2011 } else {
2012 scsidir = MPI_SCSIIO_CONTROL_READ;
2013 dataSize = karg.dataInSize;
2014 }
2015
2016 pScsiReq->Control = cpu_to_le32(scsidir | qtag);
2017 pScsiReq->DataLength = cpu_to_le32(dataSize);
2018
2019 ioc->ioctl->reset = MPTCTL_RESET_OK;
Eric Moore793955f2007-01-29 09:42:20 -07002020 ioc->ioctl->id = pScsiReq->TargetID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 } else {
Eric Moore29dd3602007-09-14 18:46:51 -06002022 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 "SCSI driver is not loaded. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06002024 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 rc = -EFAULT;
2026 goto done_free_mem;
2027 }
2028 break;
2029
2030 case MPI_FUNCTION_SCSI_TASK_MGMT:
2031 {
2032 MPT_SCSI_HOST *hd = NULL;
Eric Mooree7eae9f2007-09-29 10:15:59 -06002033 if ((ioc->sh == NULL) || ((hd = shost_priv(ioc->sh)) == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002034 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 "SCSI driver not loaded or SCSI host not found. \n",
Eric Moore29dd3602007-09-14 18:46:51 -06002036 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 rc = -EFAULT;
2038 goto done_free_mem;
2039 } else if (mptctl_set_tm_flags(hd) != 0) {
2040 rc = -EPERM;
2041 goto done_free_mem;
2042 }
2043 }
2044 break;
2045
2046 case MPI_FUNCTION_IOC_INIT:
2047 {
2048 IOCInit_t *pInit = (IOCInit_t *) mf;
2049 u32 high_addr, sense_high;
2050
2051 /* Verify that all entries in the IOC INIT match
2052 * existing setup (and in LE format).
2053 */
2054 if (sizeof(dma_addr_t) == sizeof(u64)) {
2055 high_addr = cpu_to_le32((u32)((u64)ioc->req_frames_dma >> 32));
2056 sense_high= cpu_to_le32((u32)((u64)ioc->sense_buf_pool_dma >> 32));
2057 } else {
2058 high_addr = 0;
2059 sense_high= 0;
2060 }
2061
2062 if ((pInit->Flags != 0) || (pInit->MaxDevices != ioc->facts.MaxDevices) ||
2063 (pInit->MaxBuses != ioc->facts.MaxBuses) ||
2064 (pInit->ReplyFrameSize != cpu_to_le16(ioc->reply_sz)) ||
2065 (pInit->HostMfaHighAddr != high_addr) ||
2066 (pInit->SenseBufferHighAddr != sense_high)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002067 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 "IOC_INIT issued with 1 or more incorrect parameters. Rejected.\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002069 ioc->name, __FILE__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 rc = -EFAULT;
2071 goto done_free_mem;
2072 }
2073 }
2074 break;
2075 default:
2076 /*
2077 * MPI_FUNCTION_PORT_ENABLE
2078 * MPI_FUNCTION_TARGET_CMD_BUFFER_POST
2079 * MPI_FUNCTION_TARGET_ASSIST
2080 * MPI_FUNCTION_TARGET_STATUS_SEND
2081 * MPI_FUNCTION_TARGET_MODE_ABORT
2082 * MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET
2083 * MPI_FUNCTION_IO_UNIT_RESET
2084 * MPI_FUNCTION_HANDSHAKE
2085 * MPI_FUNCTION_REPLY_FRAME_REMOVAL
2086 * MPI_FUNCTION_EVENT_NOTIFICATION
2087 * (driver handles event notification)
2088 * MPI_FUNCTION_EVENT_ACK
2089 */
2090
2091 /* What to do with these??? CHECK ME!!!
2092 MPI_FUNCTION_FC_LINK_SRVC_BUF_POST
2093 MPI_FUNCTION_FC_LINK_SRVC_RSP
2094 MPI_FUNCTION_FC_ABORT
2095 MPI_FUNCTION_LAN_SEND
2096 MPI_FUNCTION_LAN_RECEIVE
2097 MPI_FUNCTION_LAN_RESET
2098 */
2099
Eric Moore29dd3602007-09-14 18:46:51 -06002100 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 "Illegal request (function 0x%x) \n",
Eric Moore29dd3602007-09-14 18:46:51 -06002102 ioc->name, __FILE__, __LINE__, hdr->Function);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 rc = -EFAULT;
2104 goto done_free_mem;
2105 }
2106
2107 /* Add the SGL ( at most one data in SGE and one data out SGE )
2108 * In the case of two SGE's - the data out (write) will always
2109 * preceede the data in (read) SGE. psgList is used to free the
2110 * allocated memory.
2111 */
2112 psge = (char *) (((int *) mf) + karg.dataSgeOffset);
2113 flagsLength = 0;
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (karg.dataOutSize > 0)
2116 sgSize ++;
2117
2118 if (karg.dataInSize > 0)
2119 sgSize ++;
2120
2121 if (sgSize > 0) {
2122
2123 /* Set up the dataOut memory allocation */
2124 if (karg.dataOutSize > 0) {
2125 if (karg.dataInSize > 0) {
2126 flagsLength = ( MPI_SGE_FLAGS_SIMPLE_ELEMENT |
2127 MPI_SGE_FLAGS_END_OF_BUFFER |
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05302128 MPI_SGE_FLAGS_DIRECTION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 << MPI_SGE_FLAGS_SHIFT;
2130 } else {
2131 flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE;
2132 }
2133 flagsLength |= karg.dataOutSize;
2134 bufOut.len = karg.dataOutSize;
2135 bufOut.kptr = pci_alloc_consistent(
2136 ioc->pcidev, bufOut.len, &dma_addr_out);
2137
2138 if (bufOut.kptr == NULL) {
2139 rc = -ENOMEM;
2140 goto done_free_mem;
2141 } else {
2142 /* Set up this SGE.
2143 * Copy to MF and to sglbuf
2144 */
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05302145 ioc->add_sge(psge, flagsLength, dma_addr_out);
2146 psge += ioc->SGE_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 /* Copy user data to kernel space.
2149 */
2150 if (copy_from_user(bufOut.kptr,
2151 karg.dataOutBufPtr,
2152 bufOut.len)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002153 printk(MYIOC_s_ERR_FMT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 "%s@%d::mptctl_do_mpt_command - Unable "
2155 "to read user data "
2156 "struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002157 ioc->name, __FILE__, __LINE__,karg.dataOutBufPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 rc = -EFAULT;
2159 goto done_free_mem;
2160 }
2161 }
2162 }
2163
2164 if (karg.dataInSize > 0) {
2165 flagsLength = MPT_SGE_FLAGS_SSIMPLE_READ;
2166 flagsLength |= karg.dataInSize;
2167
2168 bufIn.len = karg.dataInSize;
2169 bufIn.kptr = pci_alloc_consistent(ioc->pcidev,
2170 bufIn.len, &dma_addr_in);
2171
2172 if (bufIn.kptr == NULL) {
2173 rc = -ENOMEM;
2174 goto done_free_mem;
2175 } else {
2176 /* Set up this SGE
2177 * Copy to MF and to sglbuf
2178 */
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05302179 ioc->add_sge(psge, flagsLength, dma_addr_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
2181 }
2182 } else {
2183 /* Add a NULL SGE
2184 */
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05302185 ioc->add_sge(psge, flagsLength, (dma_addr_t) -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187
2188 ioc->ioctl->wait_done = 0;
2189 if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT) {
2190
Prakash, Sathya09120a82007-07-24 15:49:05 +05302191 DBG_DUMP_TM_REQUEST_FRAME(ioc, (u32 *)mf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Prakash, Sathya7a195f42007-08-14 16:08:40 +05302193 if ((ioc->facts.IOCCapabilities & MPI_IOCFACTS_CAPABILITY_HIGH_PRI_Q) &&
2194 (ioc->facts.MsgVersion >= MPI_VERSION_01_05))
2195 mpt_put_msg_frame_hi_pri(mptctl_id, ioc, mf);
2196 else {
2197 rc =mpt_send_handshake_request(mptctl_id, ioc,
2198 sizeof(SCSITaskMgmt_t), (u32*)mf, CAN_SLEEP);
2199 if (rc != 0) {
2200 dfailprintk(ioc, printk(MYIOC_s_ERR_FMT
2201 "_send_handshake FAILED! (ioc %p, mf %p)\n",
2202 ioc->name, ioc, mf));
2203 mptctl_free_tm_flags(ioc);
2204 rc = -ENODATA;
2205 goto done_free_mem;
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208
2209 } else
2210 mpt_put_msg_frame(mptctl_id, ioc, mf);
2211
2212 /* Now wait for the command to complete */
2213 timeout = (karg.timeout > 0) ? karg.timeout : MPT_IOCTL_DEFAULT_TIMEOUT;
Moore, Eric86a7dca2006-02-02 17:19:37 -07002214 timeout = wait_event_timeout(mptctl_wait,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 ioc->ioctl->wait_done == 1,
2216 HZ*timeout);
2217
2218 if(timeout <=0 && (ioc->ioctl->wait_done != 1 )) {
2219 /* Now we need to reset the board */
2220
2221 if (hdr->Function == MPI_FUNCTION_SCSI_TASK_MGMT)
2222 mptctl_free_tm_flags(ioc);
2223
2224 mptctl_timeout_expired(ioc->ioctl);
2225 rc = -ENODATA;
2226 goto done_free_mem;
2227 }
2228
2229 mf = NULL;
2230
2231 /* If a valid reply frame, copy to the user.
2232 * Offset 2: reply length in U32's
2233 */
2234 if (ioc->ioctl->status & MPT_IOCTL_STATUS_RF_VALID) {
2235 if (karg.maxReplyBytes < ioc->reply_sz) {
2236 sz = min(karg.maxReplyBytes, 4*ioc->ioctl->ReplyFrame[2]);
2237 } else {
2238 sz = min(ioc->reply_sz, 4*ioc->ioctl->ReplyFrame[2]);
2239 }
2240
2241 if (sz > 0) {
2242 if (copy_to_user(karg.replyFrameBufPtr,
2243 &ioc->ioctl->ReplyFrame, sz)){
Eric Moore29dd3602007-09-14 18:46:51 -06002244 printk(MYIOC_s_ERR_FMT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 "%s@%d::mptctl_do_mpt_command - "
2246 "Unable to write out reply frame %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002247 ioc->name, __FILE__, __LINE__, karg.replyFrameBufPtr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 rc = -ENODATA;
2249 goto done_free_mem;
2250 }
2251 }
2252 }
2253
2254 /* If valid sense data, copy to user.
2255 */
2256 if (ioc->ioctl->status & MPT_IOCTL_STATUS_SENSE_VALID) {
2257 sz = min(karg.maxSenseBytes, MPT_SENSE_BUFFER_SIZE);
2258 if (sz > 0) {
2259 if (copy_to_user(karg.senseDataPtr, ioc->ioctl->sense, sz)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002260 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 "Unable to write sense data to user %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002262 ioc->name, __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 karg.senseDataPtr);
2264 rc = -ENODATA;
2265 goto done_free_mem;
2266 }
2267 }
2268 }
2269
2270 /* If the overall status is _GOOD and data in, copy data
2271 * to user.
2272 */
2273 if ((ioc->ioctl->status & MPT_IOCTL_STATUS_COMMAND_GOOD) &&
2274 (karg.dataInSize > 0) && (bufIn.kptr)) {
2275
2276 if (copy_to_user(karg.dataInBufPtr,
2277 bufIn.kptr, karg.dataInSize)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002278 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_do_mpt_command - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 "Unable to write data to user %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002280 ioc->name, __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 karg.dataInBufPtr);
2282 rc = -ENODATA;
2283 }
2284 }
2285
2286done_free_mem:
2287
2288 ioc->ioctl->status &= ~(MPT_IOCTL_STATUS_COMMAND_GOOD |
2289 MPT_IOCTL_STATUS_SENSE_VALID |
2290 MPT_IOCTL_STATUS_RF_VALID );
2291
2292 /* Free the allocated memory.
2293 */
2294 if (bufOut.kptr != NULL) {
2295 pci_free_consistent(ioc->pcidev,
2296 bufOut.len, (void *) bufOut.kptr, dma_addr_out);
2297 }
2298
2299 if (bufIn.kptr != NULL) {
2300 pci_free_consistent(ioc->pcidev,
2301 bufIn.len, (void *) bufIn.kptr, dma_addr_in);
2302 }
2303
2304 /* mf is null if command issued successfully
2305 * otherwise, failure occured after mf acquired.
2306 */
2307 if (mf)
2308 mpt_free_msg_frame(ioc, mf);
2309
2310 return rc;
2311}
2312
2313/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Eric Mooreba856d32006-07-11 17:34:01 -06002314/* Prototype Routine for the HOST INFO command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 *
2316 * Outputs: None.
2317 * Return: 0 if successful
2318 * -EFAULT if data unavailable
Joe Perchesfc1323b2008-02-03 17:21:01 +02002319 * -EBUSY if previous command timeout and IOC reset is not complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 * -ENODEV if no such device/adapter
2321 * -ETIME if timer expires
2322 * -ENOMEM if memory allocation error
2323 */
2324static int
2325mptctl_hp_hostinfo(unsigned long arg, unsigned int data_size)
2326{
2327 hp_host_info_t __user *uarg = (void __user *) arg;
2328 MPT_ADAPTER *ioc;
2329 struct pci_dev *pdev;
Moore, Eric592f9c22006-02-02 17:19:47 -07002330 char *pbuf=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 dma_addr_t buf_dma;
2332 hp_host_info_t karg;
2333 CONFIGPARMS cfg;
2334 ConfigPageHeader_t hdr;
2335 int iocnum;
2336 int rc, cim_rev;
Moore, Eric592f9c22006-02-02 17:19:47 -07002337 ToolboxIstwiReadWriteRequest_t *IstwiRWRequest;
2338 MPT_FRAME_HDR *mf = NULL;
2339 MPIHeader_t *mpi_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 /* Reset long to int. Should affect IA64 and SPARC only
2342 */
2343 if (data_size == sizeof(hp_host_info_t))
2344 cim_rev = 1;
2345 else if (data_size == sizeof(hp_host_info_rev0_t))
2346 cim_rev = 0; /* obsolete */
2347 else
2348 return -EFAULT;
2349
2350 if (copy_from_user(&karg, uarg, sizeof(hp_host_info_t))) {
Eric Moore29dd3602007-09-14 18:46:51 -06002351 printk(KERN_ERR MYNAM "%s@%d::mptctl_hp_host_info - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 "Unable to read in hp_host_info struct @ %p\n",
2353 __FILE__, __LINE__, uarg);
2354 return -EFAULT;
2355 }
2356
2357 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
2358 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002359 printk(KERN_DEBUG MYNAM "%s::mptctl_hp_hostinfo() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05302360 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return -ENODEV;
2362 }
Prakash, Sathya09120a82007-07-24 15:49:05 +05302363 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT ": mptctl_hp_hostinfo called.\n",
2364 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 /* Fill in the data and return the structure to the calling
2367 * program
2368 */
2369 pdev = (struct pci_dev *) ioc->pcidev;
2370
2371 karg.vendor = pdev->vendor;
2372 karg.device = pdev->device;
2373 karg.subsystem_id = pdev->subsystem_device;
2374 karg.subsystem_vendor = pdev->subsystem_vendor;
2375 karg.devfn = pdev->devfn;
2376 karg.bus = pdev->bus->number;
2377
2378 /* Save the SCSI host no. if
2379 * SCSI driver loaded
2380 */
2381 if (ioc->sh != NULL)
2382 karg.host_no = ioc->sh->host_no;
2383 else
2384 karg.host_no = -1;
2385
2386 /* Reformat the fw_version into a string
2387 */
2388 karg.fw_version[0] = ioc->facts.FWVersion.Struct.Major >= 10 ?
2389 ((ioc->facts.FWVersion.Struct.Major / 10) + '0') : '0';
2390 karg.fw_version[1] = (ioc->facts.FWVersion.Struct.Major % 10 ) + '0';
2391 karg.fw_version[2] = '.';
2392 karg.fw_version[3] = ioc->facts.FWVersion.Struct.Minor >= 10 ?
2393 ((ioc->facts.FWVersion.Struct.Minor / 10) + '0') : '0';
2394 karg.fw_version[4] = (ioc->facts.FWVersion.Struct.Minor % 10 ) + '0';
2395 karg.fw_version[5] = '.';
2396 karg.fw_version[6] = ioc->facts.FWVersion.Struct.Unit >= 10 ?
2397 ((ioc->facts.FWVersion.Struct.Unit / 10) + '0') : '0';
2398 karg.fw_version[7] = (ioc->facts.FWVersion.Struct.Unit % 10 ) + '0';
2399 karg.fw_version[8] = '.';
2400 karg.fw_version[9] = ioc->facts.FWVersion.Struct.Dev >= 10 ?
2401 ((ioc->facts.FWVersion.Struct.Dev / 10) + '0') : '0';
2402 karg.fw_version[10] = (ioc->facts.FWVersion.Struct.Dev % 10 ) + '0';
2403 karg.fw_version[11] = '\0';
2404
2405 /* Issue a config request to get the device serial number
2406 */
2407 hdr.PageVersion = 0;
2408 hdr.PageLength = 0;
2409 hdr.PageNumber = 0;
2410 hdr.PageType = MPI_CONFIG_PAGETYPE_MANUFACTURING;
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002411 cfg.cfghdr.hdr = &hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 cfg.physAddr = -1;
2413 cfg.pageAddr = 0;
2414 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2415 cfg.dir = 0; /* read */
2416 cfg.timeout = 10;
2417
2418 strncpy(karg.serial_number, " ", 24);
2419 if (mpt_config(ioc, &cfg) == 0) {
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002420 if (cfg.cfghdr.hdr->PageLength > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 /* Issue the second config page request */
2422 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2423
2424 pbuf = pci_alloc_consistent(ioc->pcidev, hdr.PageLength * 4, &buf_dma);
2425 if (pbuf) {
2426 cfg.physAddr = buf_dma;
2427 if (mpt_config(ioc, &cfg) == 0) {
2428 ManufacturingPage0_t *pdata = (ManufacturingPage0_t *) pbuf;
2429 if (strlen(pdata->BoardTracerNumber) > 1) {
2430 strncpy(karg.serial_number, pdata->BoardTracerNumber, 24);
2431 karg.serial_number[24-1]='\0';
2432 }
2433 }
2434 pci_free_consistent(ioc->pcidev, hdr.PageLength * 4, pbuf, buf_dma);
2435 pbuf = NULL;
2436 }
2437 }
2438 }
2439 rc = mpt_GetIocState(ioc, 1);
2440 switch (rc) {
2441 case MPI_IOC_STATE_OPERATIONAL:
2442 karg.ioc_status = HP_STATUS_OK;
2443 break;
2444
2445 case MPI_IOC_STATE_FAULT:
2446 karg.ioc_status = HP_STATUS_FAILED;
2447 break;
2448
2449 case MPI_IOC_STATE_RESET:
2450 case MPI_IOC_STATE_READY:
2451 default:
2452 karg.ioc_status = HP_STATUS_OTHER;
2453 break;
2454 }
2455
2456 karg.base_io_addr = pci_resource_start(pdev, 0);
2457
Moore, Eric9cc1cfb2006-02-02 17:19:33 -07002458 if ((ioc->bus_type == SAS) || (ioc->bus_type == FC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 karg.bus_phys_width = HP_BUS_WIDTH_UNK;
2460 else
2461 karg.bus_phys_width = HP_BUS_WIDTH_16;
2462
2463 karg.hard_resets = 0;
2464 karg.soft_resets = 0;
2465 karg.timeouts = 0;
2466 if (ioc->sh != NULL) {
Eric Mooree7eae9f2007-09-29 10:15:59 -06002467 MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 if (hd && (cim_rev == 1)) {
2470 karg.hard_resets = hd->hard_resets;
2471 karg.soft_resets = hd->soft_resets;
2472 karg.timeouts = hd->timeouts;
2473 }
2474 }
2475
Moore, Eric592f9c22006-02-02 17:19:47 -07002476 /*
2477 * Gather ISTWI(Industry Standard Two Wire Interface) Data
2478 */
2479 if ((mf = mpt_get_msg_frame(mptctl_id, ioc)) == NULL) {
Prakash, Sathya09120a82007-07-24 15:49:05 +05302480 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT "%s, no msg frames!!\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002481 ioc->name,__func__));
Moore, Eric592f9c22006-02-02 17:19:47 -07002482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484
Moore, Eric592f9c22006-02-02 17:19:47 -07002485 IstwiRWRequest = (ToolboxIstwiReadWriteRequest_t *)mf;
2486 mpi_hdr = (MPIHeader_t *) mf;
2487 memset(IstwiRWRequest,0,sizeof(ToolboxIstwiReadWriteRequest_t));
2488 IstwiRWRequest->Function = MPI_FUNCTION_TOOLBOX;
2489 IstwiRWRequest->Tool = MPI_TOOLBOX_ISTWI_READ_WRITE_TOOL;
2490 IstwiRWRequest->MsgContext = mpi_hdr->MsgContext;
2491 IstwiRWRequest->Flags = MPI_TB_ISTWI_FLAGS_READ;
2492 IstwiRWRequest->NumAddressBytes = 0x01;
2493 IstwiRWRequest->DataLength = cpu_to_le16(0x04);
2494 if (pdev->devfn & 1)
2495 IstwiRWRequest->DeviceAddr = 0xB2;
2496 else
2497 IstwiRWRequest->DeviceAddr = 0xB0;
2498
2499 pbuf = pci_alloc_consistent(ioc->pcidev, 4, &buf_dma);
2500 if (!pbuf)
2501 goto out;
Kashyap, Desai14d0f0b2009-05-29 16:37:04 +05302502 ioc->add_sge((char *)&IstwiRWRequest->SGL,
Moore, Eric592f9c22006-02-02 17:19:47 -07002503 (MPT_SGE_FLAGS_SSIMPLE_READ|4), buf_dma);
2504
2505 ioc->ioctl->wait_done = 0;
2506 mpt_put_msg_frame(mptctl_id, ioc, mf);
2507
2508 rc = wait_event_timeout(mptctl_wait,
2509 ioc->ioctl->wait_done == 1,
2510 HZ*MPT_IOCTL_DEFAULT_TIMEOUT /* 10 sec */);
2511
2512 if(rc <=0 && (ioc->ioctl->wait_done != 1 )) {
Prakash, Sathya09120a82007-07-24 15:49:05 +05302513 /*
Moore, Eric592f9c22006-02-02 17:19:47 -07002514 * Now we need to reset the board
2515 */
2516 mpt_free_msg_frame(ioc, mf);
2517 mptctl_timeout_expired(ioc->ioctl);
2518 goto out;
2519 }
2520
Prakash, Sathya09120a82007-07-24 15:49:05 +05302521 /*
Moore, Eric592f9c22006-02-02 17:19:47 -07002522 *ISTWI Data Definition
2523 * pbuf[0] = FW_VERSION = 0x4
2524 * pbuf[1] = Bay Count = 6 or 4 or 2, depending on
2525 * the config, you should be seeing one out of these three values
2526 * pbuf[2] = Drive Installed Map = bit pattern depend on which
2527 * bays have drives in them
2528 * pbuf[3] = Checksum (0x100 = (byte0 + byte2 + byte3)
2529 */
2530 if (ioc->ioctl->status & MPT_IOCTL_STATUS_RF_VALID)
2531 karg.rsvd = *(u32 *)pbuf;
2532
2533 out:
2534 if (pbuf)
2535 pci_free_consistent(ioc->pcidev, 4, pbuf, buf_dma);
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /* Copy the data from kernel memory to user memory
2538 */
2539 if (copy_to_user((char __user *)arg, &karg, sizeof(hp_host_info_t))) {
Eric Moore29dd3602007-09-14 18:46:51 -06002540 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_hpgethostinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 "Unable to write out hp_host_info @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002542 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return -EFAULT;
2544 }
2545
2546 return 0;
2547
2548}
2549
2550/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
Eric Mooreba856d32006-07-11 17:34:01 -06002551/* Prototype Routine for the TARGET INFO command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 *
2553 * Outputs: None.
2554 * Return: 0 if successful
2555 * -EFAULT if data unavailable
Joe Perchesfc1323b2008-02-03 17:21:01 +02002556 * -EBUSY if previous command timeout and IOC reset is not complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 * -ENODEV if no such device/adapter
2558 * -ETIME if timer expires
2559 * -ENOMEM if memory allocation error
2560 */
2561static int
2562mptctl_hp_targetinfo(unsigned long arg)
2563{
2564 hp_target_info_t __user *uarg = (void __user *) arg;
2565 SCSIDevicePage0_t *pg0_alloc;
2566 SCSIDevicePage3_t *pg3_alloc;
2567 MPT_ADAPTER *ioc;
2568 MPT_SCSI_HOST *hd = NULL;
2569 hp_target_info_t karg;
2570 int iocnum;
2571 int data_sz;
2572 dma_addr_t page_dma;
2573 CONFIGPARMS cfg;
2574 ConfigPageHeader_t hdr;
2575 int tmp, np, rc = 0;
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (copy_from_user(&karg, uarg, sizeof(hp_target_info_t))) {
Eric Moore29dd3602007-09-14 18:46:51 -06002578 printk(KERN_ERR MYNAM "%s@%d::mptctl_hp_targetinfo - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 "Unable to read in hp_host_targetinfo struct @ %p\n",
2580 __FILE__, __LINE__, uarg);
2581 return -EFAULT;
2582 }
2583
2584 if (((iocnum = mpt_verify_adapter(karg.hdr.iocnum, &ioc)) < 0) ||
2585 (ioc == NULL)) {
Eric Moore29dd3602007-09-14 18:46:51 -06002586 printk(KERN_DEBUG MYNAM "%s::mptctl_hp_targetinfo() @%d - ioc%d not found!\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05302587 __FILE__, __LINE__, iocnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return -ENODEV;
2589 }
Eric Moore29dd3602007-09-14 18:46:51 -06002590 dctlprintk(ioc, printk(MYIOC_s_DEBUG_FMT "mptctl_hp_targetinfo called.\n",
Prakash, Sathya09120a82007-07-24 15:49:05 +05302591 ioc->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 /* There is nothing to do for FCP parts.
2594 */
Moore, Eric9cc1cfb2006-02-02 17:19:33 -07002595 if ((ioc->bus_type == SAS) || (ioc->bus_type == FC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 return 0;
2597
2598 if ((ioc->spi_data.sdp0length == 0) || (ioc->sh == NULL))
2599 return 0;
2600
2601 if (ioc->sh->host_no != karg.hdr.host)
2602 return -ENODEV;
2603
2604 /* Get the data transfer speeds
2605 */
2606 data_sz = ioc->spi_data.sdp0length * 4;
2607 pg0_alloc = (SCSIDevicePage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page_dma);
2608 if (pg0_alloc) {
2609 hdr.PageVersion = ioc->spi_data.sdp0version;
2610 hdr.PageLength = data_sz;
2611 hdr.PageNumber = 0;
2612 hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
2613
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002614 cfg.cfghdr.hdr = &hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
2616 cfg.dir = 0;
2617 cfg.timeout = 0;
2618 cfg.physAddr = page_dma;
2619
2620 cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
2621
2622 if ((rc = mpt_config(ioc, &cfg)) == 0) {
2623 np = le32_to_cpu(pg0_alloc->NegotiatedParameters);
2624 karg.negotiated_width = np & MPI_SCSIDEVPAGE0_NP_WIDE ?
2625 HP_BUS_WIDTH_16 : HP_BUS_WIDTH_8;
2626
2627 if (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) {
2628 tmp = (np & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> 8;
2629 if (tmp < 0x09)
2630 karg.negotiated_speed = HP_DEV_SPEED_ULTRA320;
2631 else if (tmp <= 0x09)
2632 karg.negotiated_speed = HP_DEV_SPEED_ULTRA160;
2633 else if (tmp <= 0x0A)
2634 karg.negotiated_speed = HP_DEV_SPEED_ULTRA2;
2635 else if (tmp <= 0x0C)
2636 karg.negotiated_speed = HP_DEV_SPEED_ULTRA;
2637 else if (tmp <= 0x25)
2638 karg.negotiated_speed = HP_DEV_SPEED_FAST;
2639 else
2640 karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
2641 } else
2642 karg.negotiated_speed = HP_DEV_SPEED_ASYNC;
2643 }
2644
2645 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg0_alloc, page_dma);
2646 }
2647
2648 /* Set defaults
2649 */
2650 karg.message_rejects = -1;
2651 karg.phase_errors = -1;
2652 karg.parity_errors = -1;
2653 karg.select_timeouts = -1;
2654
2655 /* Get the target error parameters
2656 */
2657 hdr.PageVersion = 0;
2658 hdr.PageLength = 0;
2659 hdr.PageNumber = 3;
2660 hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
2661
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002662 cfg.cfghdr.hdr = &hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
2664 cfg.dir = 0;
2665 cfg.timeout = 0;
2666 cfg.physAddr = -1;
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002667 if ((mpt_config(ioc, &cfg) == 0) && (cfg.cfghdr.hdr->PageLength > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 /* Issue the second config page request */
2669 cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
Christoph Hellwig69218ee2005-08-18 16:26:15 +02002670 data_sz = (int) cfg.cfghdr.hdr->PageLength * 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 pg3_alloc = (SCSIDevicePage3_t *) pci_alloc_consistent(
2672 ioc->pcidev, data_sz, &page_dma);
2673 if (pg3_alloc) {
2674 cfg.physAddr = page_dma;
2675 cfg.pageAddr = (karg.hdr.channel << 8) | karg.hdr.id;
2676 if ((rc = mpt_config(ioc, &cfg)) == 0) {
2677 karg.message_rejects = (u32) le16_to_cpu(pg3_alloc->MsgRejectCount);
2678 karg.phase_errors = (u32) le16_to_cpu(pg3_alloc->PhaseErrorCount);
2679 karg.parity_errors = (u32) le16_to_cpu(pg3_alloc->ParityErrorCount);
2680 }
2681 pci_free_consistent(ioc->pcidev, data_sz, (u8 *) pg3_alloc, page_dma);
2682 }
2683 }
Eric Mooree7eae9f2007-09-29 10:15:59 -06002684 hd = shost_priv(ioc->sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (hd != NULL)
2686 karg.select_timeouts = hd->sel_timeout[karg.hdr.id];
2687
2688 /* Copy the data from kernel memory to user memory
2689 */
2690 if (copy_to_user((char __user *)arg, &karg, sizeof(hp_target_info_t))) {
Eric Moore29dd3602007-09-14 18:46:51 -06002691 printk(MYIOC_s_ERR_FMT "%s@%d::mptctl_hp_target_info - "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 "Unable to write out mpt_ioctl_targetinfo struct @ %p\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002693 ioc->name, __FILE__, __LINE__, uarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return -EFAULT;
2695 }
2696
2697 return 0;
2698}
2699
2700/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2701
Arjan van de Venfa027c22007-02-12 00:55:33 -08002702static const struct file_operations mptctl_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 .owner = THIS_MODULE,
2704 .llseek = no_llseek,
Moore, Ericea5a7a82006-02-02 17:20:01 -07002705 .fasync = mptctl_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 .unlocked_ioctl = mptctl_ioctl,
2707#ifdef CONFIG_COMPAT
2708 .compat_ioctl = compat_mpctl_ioctl,
2709#endif
2710};
2711
2712static struct miscdevice mptctl_miscdev = {
2713 MPT_MINOR,
2714 MYNAM,
2715 &mptctl_fops
2716};
2717
2718/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2719
2720#ifdef CONFIG_COMPAT
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722static int
2723compat_mptfwxfer_ioctl(struct file *filp, unsigned int cmd,
2724 unsigned long arg)
2725{
2726 struct mpt_fw_xfer32 kfw32;
2727 struct mpt_fw_xfer kfw;
2728 MPT_ADAPTER *iocp = NULL;
2729 int iocnum, iocnumX;
2730 int nonblock = (filp->f_flags & O_NONBLOCK);
2731 int ret;
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 if (copy_from_user(&kfw32, (char __user *)arg, sizeof(kfw32)))
2735 return -EFAULT;
2736
2737 /* Verify intended MPT adapter */
2738 iocnumX = kfw32.iocnum & 0xFF;
2739 if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
2740 (iocp == NULL)) {
Prakash, Sathya09120a82007-07-24 15:49:05 +05302741 printk(KERN_DEBUG MYNAM "::compat_mptfwxfer_ioctl @%d - ioc%d not found!\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002742 __LINE__, iocnumX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 return -ENODEV;
2744 }
2745
2746 if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
2747 return ret;
2748
Prakash, Sathya09120a82007-07-24 15:49:05 +05302749 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "compat_mptfwxfer_ioctl() called\n",
2750 iocp->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 kfw.iocnum = iocnum;
2752 kfw.fwlen = kfw32.fwlen;
2753 kfw.bufp = compat_ptr(kfw32.bufp);
2754
2755 ret = mptctl_do_fw_download(kfw.iocnum, kfw.bufp, kfw.fwlen);
2756
Christoph Hellwigeeb846c2006-01-13 18:27:11 +01002757 mutex_unlock(&iocp->ioctl->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 return ret;
2760}
2761
2762static int
2763compat_mpt_command(struct file *filp, unsigned int cmd,
2764 unsigned long arg)
2765{
2766 struct mpt_ioctl_command32 karg32;
2767 struct mpt_ioctl_command32 __user *uarg = (struct mpt_ioctl_command32 __user *) arg;
2768 struct mpt_ioctl_command karg;
2769 MPT_ADAPTER *iocp = NULL;
2770 int iocnum, iocnumX;
2771 int nonblock = (filp->f_flags & O_NONBLOCK);
2772 int ret;
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32)))
2775 return -EFAULT;
2776
2777 /* Verify intended MPT adapter */
2778 iocnumX = karg32.hdr.iocnum & 0xFF;
2779 if (((iocnum = mpt_verify_adapter(iocnumX, &iocp)) < 0) ||
2780 (iocp == NULL)) {
Prakash, Sathya09120a82007-07-24 15:49:05 +05302781 printk(KERN_DEBUG MYNAM "::compat_mpt_command @%d - ioc%d not found!\n",
Eric Moore29dd3602007-09-14 18:46:51 -06002782 __LINE__, iocnumX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return -ENODEV;
2784 }
2785
2786 if ((ret = mptctl_syscall_down(iocp, nonblock)) != 0)
2787 return ret;
2788
Prakash, Sathya09120a82007-07-24 15:49:05 +05302789 dctlprintk(iocp, printk(MYIOC_s_DEBUG_FMT "compat_mpt_command() called\n",
2790 iocp->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 /* Copy data to karg */
2792 karg.hdr.iocnum = karg32.hdr.iocnum;
2793 karg.hdr.port = karg32.hdr.port;
2794 karg.timeout = karg32.timeout;
2795 karg.maxReplyBytes = karg32.maxReplyBytes;
2796
2797 karg.dataInSize = karg32.dataInSize;
2798 karg.dataOutSize = karg32.dataOutSize;
2799 karg.maxSenseBytes = karg32.maxSenseBytes;
2800 karg.dataSgeOffset = karg32.dataSgeOffset;
2801
2802 karg.replyFrameBufPtr = (char __user *)(unsigned long)karg32.replyFrameBufPtr;
2803 karg.dataInBufPtr = (char __user *)(unsigned long)karg32.dataInBufPtr;
2804 karg.dataOutBufPtr = (char __user *)(unsigned long)karg32.dataOutBufPtr;
2805 karg.senseDataPtr = (char __user *)(unsigned long)karg32.senseDataPtr;
2806
2807 /* Pass new structure to do_mpt_command
2808 */
2809 ret = mptctl_do_mpt_command (karg, &uarg->MF);
2810
Christoph Hellwigeeb846c2006-01-13 18:27:11 +01002811 mutex_unlock(&iocp->ioctl->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813 return ret;
2814}
2815
2816static long compat_mpctl_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
2817{
2818 long ret;
2819 lock_kernel();
2820 switch (cmd) {
2821 case MPTIOCINFO:
2822 case MPTIOCINFO1:
2823 case MPTIOCINFO2:
2824 case MPTTARGETINFO:
2825 case MPTEVENTQUERY:
2826 case MPTEVENTENABLE:
2827 case MPTEVENTREPORT:
2828 case MPTHARDRESET:
2829 case HP_GETHOSTINFO:
2830 case HP_GETTARGETINFO:
2831 case MPTTEST:
2832 ret = __mptctl_ioctl(f, cmd, arg);
2833 break;
2834 case MPTCOMMAND32:
2835 ret = compat_mpt_command(f, cmd, arg);
2836 break;
2837 case MPTFWDOWNLOAD32:
2838 ret = compat_mptfwxfer_ioctl(f, cmd, arg);
2839 break;
2840 default:
2841 ret = -ENOIOCTLCMD;
2842 break;
2843 }
2844 unlock_kernel();
2845 return ret;
2846}
2847
2848#endif
2849
2850
2851/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2852/*
2853 * mptctl_probe - Installs ioctl devices per bus.
2854 * @pdev: Pointer to pci_dev structure
2855 *
2856 * Returns 0 for success, non-zero for failure.
2857 *
2858 */
2859
2860static int
2861mptctl_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2862{
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07002863 MPT_IOCTL *mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2865
2866 /*
2867 * Allocate and inite a MPT_IOCTL structure
2868 */
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07002869 mem = kzalloc(sizeof(MPT_IOCTL), GFP_KERNEL);
2870 if (!mem) {
2871 mptctl_remove(pdev);
2872 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
2874
Mariusz Kozlowskid7383a22007-08-10 14:50:50 -07002875 ioc->ioctl = mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 ioc->ioctl->ioc = ioc;
Christoph Hellwigeeb846c2006-01-13 18:27:11 +01002877 mutex_init(&ioc->ioctl->ioctl_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880
2881/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2882/*
2883 * mptctl_remove - Removed ioctl devices
2884 * @pdev: Pointer to pci_dev structure
2885 *
2886 *
2887 */
2888static void
2889mptctl_remove(struct pci_dev *pdev)
2890{
2891 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
2892
2893 kfree ( ioc->ioctl );
2894}
2895
2896static struct mpt_pci_driver mptctl_driver = {
2897 .probe = mptctl_probe,
2898 .remove = mptctl_remove,
2899};
2900
2901/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2902static int __init mptctl_init(void)
2903{
2904 int err;
2905 int where = 1;
2906
2907 show_mptmod_ver(my_NAME, my_VERSION);
2908
Prakash, Sathya09120a82007-07-24 15:49:05 +05302909 mpt_device_driver_register(&mptctl_driver, MPTCTL_DRIVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 /* Register this device */
2912 err = misc_register(&mptctl_miscdev);
2913 if (err < 0) {
2914 printk(KERN_ERR MYNAM ": Can't register misc device [minor=%d].\n", MPT_MINOR);
2915 goto out_fail;
2916 }
2917 printk(KERN_INFO MYNAM ": Registered with Fusion MPT base driver\n");
2918 printk(KERN_INFO MYNAM ": /dev/%s @ (major,minor=%d,%d)\n",
2919 mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
2920
2921 /*
2922 * Install our handler
2923 */
2924 ++where;
Prakash, Sathyaf606f572007-08-14 16:12:53 +05302925 mptctl_id = mpt_register(mptctl_reply, MPTCTL_DRIVER);
2926 if (!mptctl_id || mptctl_id >= MPT_MAX_PROTOCOL_DRIVERS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 printk(KERN_ERR MYNAM ": ERROR: Failed to register with Fusion MPT base driver\n");
2928 misc_deregister(&mptctl_miscdev);
2929 err = -EBUSY;
2930 goto out_fail;
2931 }
2932
Prakash, Sathya09120a82007-07-24 15:49:05 +05302933 mpt_reset_register(mptctl_id, mptctl_ioc_reset);
2934 mpt_event_register(mptctl_id, mptctl_event_process);
Moore, Ericea5a7a82006-02-02 17:20:01 -07002935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return 0;
2937
2938out_fail:
2939
2940 mpt_device_driver_deregister(MPTCTL_DRIVER);
2941
2942 return err;
2943}
2944
2945/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2946static void mptctl_exit(void)
2947{
2948 misc_deregister(&mptctl_miscdev);
2949 printk(KERN_INFO MYNAM ": Deregistered /dev/%s @ (major,minor=%d,%d)\n",
2950 mptctl_miscdev.name, MISC_MAJOR, mptctl_miscdev.minor);
2951
2952 /* De-register reset handler from base module */
2953 mpt_reset_deregister(mptctl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955 /* De-register callback handler from base module */
2956 mpt_deregister(mptctl_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 mpt_device_driver_deregister(MPTCTL_DRIVER);
2959
2960}
2961
2962/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
2963
2964module_init(mptctl_init);
2965module_exit(mptctl_exit);