blob: e83163c66884508278ba28a400a2e0d6f79e36a4 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 *
4 * Linux MegaRAID device driver
5 *
6 * Copyright (c) 2003-2004 LSI Logic Corporation.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * FILE : megaraid_mm.c
Ju, Seokmann0b4972d2006-07-25 08:45:06 -06009 * Version : v2.20.2.7 (Jul 16 2006)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Common management module
12 */
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040013#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Arnd Bergmannc45d15d2010-06-02 14:28:52 +020015#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "megaraid_mm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18
19// Entry points for char node driver
Arnd Bergmannc45d15d2010-06-02 14:28:52 +020020static DEFINE_MUTEX(mraid_mm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021static int mraid_mm_open(struct inode *, struct file *);
Arnd Bergmannf4927c42010-04-27 00:24:01 +020022static long mraid_mm_unlocked_ioctl(struct file *, uint, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24
25// routines to convert to and from the old the format
26static int mimd_to_kioc(mimd_t __user *, mraid_mmadp_t *, uioc_t *);
27static int kioc_to_mimd(uioc_t *, mimd_t __user *);
28
29
30// Helper functions
31static int handle_drvrcmd(void __user *, uint8_t, int *);
32static int lld_ioctl(mraid_mmadp_t *, uioc_t *);
33static void ioctl_done(uioc_t *);
Kees Cookc251a7b2017-10-22 15:30:04 -070034static void lld_timedout(struct timer_list *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static void hinfo_to_cinfo(mraid_hba_info_t *, mcontroller_t *);
36static mraid_mmadp_t *mraid_mm_get_adapter(mimd_t __user *, int *);
37static uioc_t *mraid_mm_alloc_kioc(mraid_mmadp_t *);
38static void mraid_mm_dealloc_kioc(mraid_mmadp_t *, uioc_t *);
39static int mraid_mm_attach_buf(mraid_mmadp_t *, uioc_t *, int);
40static int mraid_mm_setup_dma_pools(mraid_mmadp_t *);
41static void mraid_mm_free_adp_resources(mraid_mmadp_t *);
42static void mraid_mm_teardown_dma_pools(mraid_mmadp_t *);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044MODULE_AUTHOR("LSI Logic Corporation");
45MODULE_DESCRIPTION("LSI Logic Management Module");
46MODULE_LICENSE("GPL");
47MODULE_VERSION(LSI_COMMON_MOD_VERSION);
48
49static int dbglevel = CL_ANN;
50module_param_named(dlevel, dbglevel, int, 0);
51MODULE_PARM_DESC(dlevel, "Debug level (default=0)");
52
53EXPORT_SYMBOL(mraid_mm_register_adp);
54EXPORT_SYMBOL(mraid_mm_unregister_adp);
55EXPORT_SYMBOL(mraid_mm_adapter_app_handle);
56
David Milburn2a4aa2c2007-04-19 12:10:24 -050057static uint32_t drvr_ver = 0x02200207;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static int adapters_count_g;
60static struct list_head adapters_list_g;
61
62static wait_queue_head_t wait_q;
63
Arjan van de Ven00977a52007-02-12 00:55:34 -080064static const struct file_operations lsi_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .open = mraid_mm_open,
Arnd Bergmannf4927c42010-04-27 00:24:01 +020066 .unlocked_ioctl = mraid_mm_unlocked_ioctl,
Arnd Bergmann01b8bca82018-09-11 17:28:08 +020067 .compat_ioctl = compat_ptr_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +020069 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Thomas Horsten90a95af2008-02-04 23:53:18 -080072static struct miscdevice megaraid_mm_dev = {
73 .minor = MISC_DYNAMIC_MINOR,
74 .name = "megadev0",
75 .fops = &lsi_fops,
76};
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/**
79 * mraid_mm_open - open routine for char node interface
Randy Dunlapa69b74d2007-01-05 22:41:48 -080080 * @inode : unused
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * @filep : unused
82 *
Randy Dunlapa69b74d2007-01-05 22:41:48 -080083 * Allow ioctl operations by apps only if they have superuser privilege.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
85static int
86mraid_mm_open(struct inode *inode, struct file *filep)
87{
88 /*
89 * Only allow superuser to access private ioctl interface
90 */
91 if (!capable(CAP_SYS_ADMIN)) return (-EACCES);
92
93 return 0;
94}
95
96/**
97 * mraid_mm_ioctl - module entry-point for ioctls
98 * @inode : inode (ignored)
99 * @filep : file operations pointer (ignored)
100 * @cmd : ioctl command
101 * @arg : user ioctl packet
102 */
103static int
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200104mraid_mm_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 uioc_t *kioc;
107 char signature[EXT_IOCTL_SIGN_SZ] = {0};
108 int rval;
109 mraid_mmadp_t *adp;
110 uint8_t old_ioctl;
111 int drvrcmd_rval;
112 void __user *argp = (void __user *)arg;
113
114 /*
115 * Make sure only USCSICMD are issued through this interface.
116 * MIMD application would still fire different command.
117 */
118
119 if ((_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD)) {
120 return (-EINVAL);
121 }
122
123 /*
124 * Look for signature to see if this is the new or old ioctl format.
125 */
126 if (copy_from_user(signature, argp, EXT_IOCTL_SIGN_SZ)) {
127 con_log(CL_ANN, (KERN_WARNING
128 "megaraid cmm: copy from usr addr failed\n"));
129 return (-EFAULT);
130 }
131
132 if (memcmp(signature, EXT_IOCTL_SIGN, EXT_IOCTL_SIGN_SZ) == 0)
133 old_ioctl = 0;
134 else
135 old_ioctl = 1;
136
137 /*
138 * At present, we don't support the new ioctl packet
139 */
140 if (!old_ioctl )
141 return (-EINVAL);
142
143 /*
144 * If it is a driver ioctl (as opposed to fw ioctls), then we can
145 * handle the command locally. rval > 0 means it is not a drvr cmd
146 */
147 rval = handle_drvrcmd(argp, old_ioctl, &drvrcmd_rval);
148
149 if (rval < 0)
150 return rval;
151 else if (rval == 0)
152 return drvrcmd_rval;
153
154 rval = 0;
155 if ((adp = mraid_mm_get_adapter(argp, &rval)) == NULL) {
156 return rval;
157 }
158
159 /*
160 * Check if adapter can accept ioctl. We may have marked it offline
161 * if any previous kioc had timedout on this controller.
162 */
163 if (!adp->quiescent) {
164 con_log(CL_ANN, (KERN_WARNING
165 "megaraid cmm: controller cannot accept cmds due to "
166 "earlier errors\n" ));
167 return -EFAULT;
168 }
169
170 /*
171 * The following call will block till a kioc is available
Nicholas Krause7296f622016-01-05 14:32:54 -0500172 * or return NULL if the list head is empty for the pointer
173 * of type mraid_mmapt passed to mraid_mm_alloc_kioc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
175 kioc = mraid_mm_alloc_kioc(adp);
Nicholas Krause7296f622016-01-05 14:32:54 -0500176 if (!kioc)
177 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /*
180 * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
181 */
182 if ((rval = mimd_to_kioc(argp, adp, kioc))) {
183 mraid_mm_dealloc_kioc(adp, kioc);
184 return rval;
185 }
186
187 kioc->done = ioctl_done;
188
189 /*
190 * Issue the IOCTL to the low level driver. After the IOCTL completes
191 * release the kioc if and only if it was _not_ timedout. If it was
192 * timedout, that means that resources are still with low level driver.
193 */
194 if ((rval = lld_ioctl(adp, kioc))) {
195
196 if (!kioc->timedout)
197 mraid_mm_dealloc_kioc(adp, kioc);
198
199 return rval;
200 }
201
202 /*
203 * Convert the kioc back to user space
204 */
205 rval = kioc_to_mimd(kioc, argp);
206
207 /*
208 * Return the kioc to free pool
209 */
210 mraid_mm_dealloc_kioc(adp, kioc);
211
212 return rval;
213}
214
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200215static long
216mraid_mm_unlocked_ioctl(struct file *filep, unsigned int cmd,
217 unsigned long arg)
218{
219 int err;
220
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200221 mutex_lock(&mraid_mm_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200222 err = mraid_mm_ioctl(filep, cmd, arg);
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200223 mutex_unlock(&mraid_mm_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200224
225 return err;
226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228/**
229 * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
230 * @umimd : User space mimd_t ioctl packet
Randy Dunlapa69b74d2007-01-05 22:41:48 -0800231 * @rval : returned success/error status
232 *
233 * The function return value is a pointer to the located @adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235static mraid_mmadp_t *
236mraid_mm_get_adapter(mimd_t __user *umimd, int *rval)
237{
238 mraid_mmadp_t *adapter;
239 mimd_t mimd;
240 uint32_t adapno;
241 int iterator;
242
243
244 if (copy_from_user(&mimd, umimd, sizeof(mimd_t))) {
245 *rval = -EFAULT;
246 return NULL;
247 }
248
249 adapno = GETADAP(mimd.ui.fcs.adapno);
250
251 if (adapno >= adapters_count_g) {
252 *rval = -ENODEV;
253 return NULL;
254 }
255
256 adapter = NULL;
257 iterator = 0;
258
259 list_for_each_entry(adapter, &adapters_list_g, list) {
260 if (iterator++ == adapno) break;
261 }
262
263 if (!adapter) {
264 *rval = -ENODEV;
265 return NULL;
266 }
267
268 return adapter;
269}
270
Randy Dunlapa69b74d2007-01-05 22:41:48 -0800271/**
272 * handle_drvrcmd - Checks if the opcode is a driver cmd and if it is, handles it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * @arg : packet sent by the user app
274 * @old_ioctl : mimd if 1; uioc otherwise
Randy Dunlapa69b74d2007-01-05 22:41:48 -0800275 * @rval : pointer for command's returned value (not function status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
277static int
278handle_drvrcmd(void __user *arg, uint8_t old_ioctl, int *rval)
279{
280 mimd_t __user *umimd;
281 mimd_t kmimd;
282 uint8_t opcode;
283 uint8_t subopcode;
284
285 if (old_ioctl)
286 goto old_packet;
287 else
288 goto new_packet;
289
290new_packet:
291 return (-ENOTSUPP);
292
293old_packet:
294 *rval = 0;
295 umimd = arg;
296
297 if (copy_from_user(&kmimd, umimd, sizeof(mimd_t)))
298 return (-EFAULT);
299
300 opcode = kmimd.ui.fcs.opcode;
301 subopcode = kmimd.ui.fcs.subopcode;
302
303 /*
304 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
305 * GET_NUMADP, then we can handle. Otherwise we should return 1 to
306 * indicate that we cannot handle this.
307 */
308 if (opcode != 0x82)
309 return 1;
310
311 switch (subopcode) {
312
313 case MEGAIOC_QDRVRVER:
314
315 if (copy_to_user(kmimd.data, &drvr_ver, sizeof(uint32_t)))
316 return (-EFAULT);
317
318 return 0;
319
320 case MEGAIOC_QNADAP:
321
322 *rval = adapters_count_g;
323
324 if (copy_to_user(kmimd.data, &adapters_count_g,
325 sizeof(uint32_t)))
326 return (-EFAULT);
327
328 return 0;
329
330 default:
331 /* cannot handle */
332 return 1;
333 }
334
335 return 0;
336}
337
338
339/**
340 * mimd_to_kioc - Converter from old to new ioctl format
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * @umimd : user space old MIMD IOCTL
Randy Dunlapa69b74d2007-01-05 22:41:48 -0800342 * @adp : adapter softstate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * @kioc : kernel space new format IOCTL
344 *
345 * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
346 * new packet is in kernel space so that driver can perform operations on it
347 * freely.
348 */
349
350static int
351mimd_to_kioc(mimd_t __user *umimd, mraid_mmadp_t *adp, uioc_t *kioc)
352{
353 mbox64_t *mbox64;
354 mbox_t *mbox;
355 mraid_passthru_t *pthru32;
356 uint32_t adapno;
357 uint8_t opcode;
358 uint8_t subopcode;
359 mimd_t mimd;
360
361 if (copy_from_user(&mimd, umimd, sizeof(mimd_t)))
362 return (-EFAULT);
363
364 /*
365 * Applications are not allowed to send extd pthru
366 */
367 if ((mimd.mbox[0] == MBOXCMD_PASSTHRU64) ||
368 (mimd.mbox[0] == MBOXCMD_EXTPTHRU))
369 return (-EINVAL);
370
371 opcode = mimd.ui.fcs.opcode;
372 subopcode = mimd.ui.fcs.subopcode;
373 adapno = GETADAP(mimd.ui.fcs.adapno);
374
375 if (adapno >= adapters_count_g)
376 return (-ENODEV);
377
378 kioc->adapno = adapno;
379 kioc->mb_type = MBOX_LEGACY;
380 kioc->app_type = APPTYPE_MIMD;
381
382 switch (opcode) {
383
384 case 0x82:
385
386 if (subopcode == MEGAIOC_QADAPINFO) {
387
388 kioc->opcode = GET_ADAP_INFO;
389 kioc->data_dir = UIOC_RD;
390 kioc->xferlen = sizeof(mraid_hba_info_t);
391
392 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
393 return (-ENOMEM);
394 }
395 else {
396 con_log(CL_ANN, (KERN_WARNING
397 "megaraid cmm: Invalid subop\n"));
398 return (-EINVAL);
399 }
400
401 break;
402
403 case 0x81:
404
405 kioc->opcode = MBOX_CMD;
406 kioc->xferlen = mimd.ui.fcs.length;
407 kioc->user_data_len = kioc->xferlen;
408 kioc->user_data = mimd.ui.fcs.buffer;
409
410 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
411 return (-ENOMEM);
412
413 if (mimd.outlen) kioc->data_dir = UIOC_RD;
414 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
415
416 break;
417
418 case 0x80:
419
420 kioc->opcode = MBOX_CMD;
421 kioc->xferlen = (mimd.outlen > mimd.inlen) ?
422 mimd.outlen : mimd.inlen;
423 kioc->user_data_len = kioc->xferlen;
424 kioc->user_data = mimd.data;
425
426 if (mraid_mm_attach_buf(adp, kioc, kioc->xferlen))
427 return (-ENOMEM);
428
429 if (mimd.outlen) kioc->data_dir = UIOC_RD;
430 if (mimd.inlen) kioc->data_dir |= UIOC_WR;
431
432 break;
433
434 default:
435 return (-EINVAL);
436 }
437
438 /*
439 * If driver command, nothing else to do
440 */
441 if (opcode == 0x82)
442 return 0;
443
444 /*
445 * This is a mailbox cmd; copy the mailbox from mimd
446 */
447 mbox64 = (mbox64_t *)((unsigned long)kioc->cmdbuf);
448 mbox = &mbox64->mbox32;
449 memcpy(mbox, mimd.mbox, 14);
450
451 if (mbox->cmd != MBOXCMD_PASSTHRU) { // regular DCMD
452
453 mbox->xferaddr = (uint32_t)kioc->buf_paddr;
454
455 if (kioc->data_dir & UIOC_WR) {
456 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
457 kioc->xferlen)) {
458 return (-EFAULT);
459 }
460 }
461
462 return 0;
463 }
464
465 /*
466 * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
467 * Just like in above case, the beginning for memblk is treated as
468 * a mailbox. The passthru will begin at next 1K boundary. And the
469 * data will start 1K after that.
470 */
471 pthru32 = kioc->pthru32;
472 kioc->user_pthru = &umimd->pthru;
473 mbox->xferaddr = (uint32_t)kioc->pthru32_h;
474
475 if (copy_from_user(pthru32, kioc->user_pthru,
476 sizeof(mraid_passthru_t))) {
477 return (-EFAULT);
478 }
479
480 pthru32->dataxferaddr = kioc->buf_paddr;
481 if (kioc->data_dir & UIOC_WR) {
Dan Carpenter3de22602013-10-30 20:13:51 +0300482 if (pthru32->dataxferlen > kioc->xferlen)
483 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (copy_from_user(kioc->buf_vaddr, kioc->user_data,
485 pthru32->dataxferlen)) {
486 return (-EFAULT);
487 }
488 }
489
490 return 0;
491}
492
493/**
494 * mraid_mm_attch_buf - Attach a free dma buffer for required size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * @adp : Adapter softstate
496 * @kioc : kioc that the buffer needs to be attached to
497 * @xferlen : required length for buffer
498 *
499 * First we search for a pool with smallest buffer that is >= @xferlen. If
500 * that pool has no free buffer, we will try for the next bigger size. If none
501 * is available, we will try to allocate the smallest buffer that is >=
502 * @xferlen and attach it the pool.
503 */
504static int
505mraid_mm_attach_buf(mraid_mmadp_t *adp, uioc_t *kioc, int xferlen)
506{
507 mm_dmapool_t *pool;
508 int right_pool = -1;
509 unsigned long flags;
510 int i;
511
512 kioc->pool_index = -1;
513 kioc->buf_vaddr = NULL;
514 kioc->buf_paddr = 0;
515 kioc->free_buf = 0;
516
517 /*
518 * We need xferlen amount of memory. See if we can get it from our
519 * dma pools. If we don't get exact size, we will try bigger buffer
520 */
521
522 for (i = 0; i < MAX_DMA_POOLS; i++) {
523
524 pool = &adp->dma_pool_list[i];
525
526 if (xferlen > pool->buf_size)
527 continue;
528
529 if (right_pool == -1)
530 right_pool = i;
531
532 spin_lock_irqsave(&pool->lock, flags);
533
534 if (!pool->in_use) {
535
536 pool->in_use = 1;
537 kioc->pool_index = i;
538 kioc->buf_vaddr = pool->vaddr;
539 kioc->buf_paddr = pool->paddr;
540
541 spin_unlock_irqrestore(&pool->lock, flags);
542 return 0;
543 }
544 else {
545 spin_unlock_irqrestore(&pool->lock, flags);
546 continue;
547 }
548 }
549
550 /*
551 * If xferlen doesn't match any of our pools, return error
552 */
553 if (right_pool == -1)
554 return -EINVAL;
555
556 /*
557 * We did not get any buffer from the preallocated pool. Let us try
558 * to allocate one new buffer. NOTE: This is a blocking call.
559 */
560 pool = &adp->dma_pool_list[right_pool];
561
562 spin_lock_irqsave(&pool->lock, flags);
563
564 kioc->pool_index = right_pool;
565 kioc->free_buf = 1;
Romain Perierfc69d862017-07-06 10:13:06 +0200566 kioc->buf_vaddr = dma_pool_alloc(pool->handle, GFP_ATOMIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 &kioc->buf_paddr);
568 spin_unlock_irqrestore(&pool->lock, flags);
569
570 if (!kioc->buf_vaddr)
571 return -ENOMEM;
572
573 return 0;
574}
575
576/**
577 * mraid_mm_alloc_kioc - Returns a uioc_t from free list
578 * @adp : Adapter softstate for this module
579 *
580 * The kioc_semaphore is initialized with number of kioc nodes in the
581 * free kioc pool. If the kioc pool is empty, this function blocks till
582 * a kioc becomes free.
583 */
584static uioc_t *
585mraid_mm_alloc_kioc(mraid_mmadp_t *adp)
586{
587 uioc_t *kioc;
588 struct list_head* head;
589 unsigned long flags;
590
591 down(&adp->kioc_semaphore);
592
593 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
594
595 head = &adp->kioc_pool;
596
597 if (list_empty(head)) {
598 up(&adp->kioc_semaphore);
599 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
600
601 con_log(CL_ANN, ("megaraid cmm: kioc list empty!\n"));
602 return NULL;
603 }
604
605 kioc = list_entry(head->next, uioc_t, list);
606 list_del_init(&kioc->list);
607
608 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
609
610 memset((caddr_t)(unsigned long)kioc->cmdbuf, 0, sizeof(mbox64_t));
611 memset((caddr_t) kioc->pthru32, 0, sizeof(mraid_passthru_t));
612
613 kioc->buf_vaddr = NULL;
614 kioc->buf_paddr = 0;
615 kioc->pool_index =-1;
616 kioc->free_buf = 0;
617 kioc->user_data = NULL;
618 kioc->user_data_len = 0;
619 kioc->user_pthru = NULL;
620 kioc->timedout = 0;
621
622 return kioc;
623}
624
625/**
626 * mraid_mm_dealloc_kioc - Return kioc to free pool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * @adp : Adapter softstate
628 * @kioc : uioc_t node to be returned to free pool
629 */
630static void
631mraid_mm_dealloc_kioc(mraid_mmadp_t *adp, uioc_t *kioc)
632{
633 mm_dmapool_t *pool;
634 unsigned long flags;
635
636 if (kioc->pool_index != -1) {
637 pool = &adp->dma_pool_list[kioc->pool_index];
638
639 /* This routine may be called in non-isr context also */
640 spin_lock_irqsave(&pool->lock, flags);
641
642 /*
643 * While attaching the dma buffer, if we didn't get the
644 * required buffer from the pool, we would have allocated
645 * it at the run time and set the free_buf flag. We must
646 * free that buffer. Otherwise, just mark that the buffer is
647 * not in use
648 */
649 if (kioc->free_buf == 1)
Romain Perierfc69d862017-07-06 10:13:06 +0200650 dma_pool_free(pool->handle, kioc->buf_vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 kioc->buf_paddr);
652 else
653 pool->in_use = 0;
654
655 spin_unlock_irqrestore(&pool->lock, flags);
656 }
657
658 /* Return the kioc to the free pool */
659 spin_lock_irqsave(&adp->kioc_pool_lock, flags);
660 list_add(&kioc->list, &adp->kioc_pool);
661 spin_unlock_irqrestore(&adp->kioc_pool_lock, flags);
662
663 /* increment the free kioc count */
664 up(&adp->kioc_semaphore);
665
666 return;
667}
668
669/**
670 * lld_ioctl - Routine to issue ioctl to low level drvr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * @adp : The adapter handle
672 * @kioc : The ioctl packet with kernel addresses
673 */
674static int
675lld_ioctl(mraid_mmadp_t *adp, uioc_t *kioc)
676{
677 int rval;
Kees Cookc251a7b2017-10-22 15:30:04 -0700678 struct uioc_timeout timeout = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 kioc->status = -ENODATA;
681 rval = adp->issue_uioc(adp->drvr_data, kioc, IOCTL_ISSUE);
682
683 if (rval) return rval;
684
685 /*
686 * Start the timer
687 */
688 if (adp->timeout > 0) {
Kees Cookc251a7b2017-10-22 15:30:04 -0700689 timeout.uioc = kioc;
690 timer_setup_on_stack(&timeout.timer, lld_timedout, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Kees Cookc251a7b2017-10-22 15:30:04 -0700692 timeout.timer.expires = jiffies + adp->timeout * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Kees Cookc251a7b2017-10-22 15:30:04 -0700694 add_timer(&timeout.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 /*
698 * Wait till the low level driver completes the ioctl. After this
699 * call, the ioctl either completed successfully or timedout.
700 */
701 wait_event(wait_q, (kioc->status != -ENODATA));
Kees Cookc251a7b2017-10-22 15:30:04 -0700702 if (timeout.timer.function) {
703 del_timer_sync(&timeout.timer);
704 destroy_timer_on_stack(&timeout.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
707 /*
708 * If the command had timedout, we mark the controller offline
709 * before returning
710 */
711 if (kioc->timedout) {
712 adp->quiescent = 0;
713 }
714
715 return kioc->status;
716}
717
718
719/**
720 * ioctl_done - callback from the low level driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * @kioc : completed ioctl packet
722 */
723static void
724ioctl_done(uioc_t *kioc)
725{
726 uint32_t adapno;
727 int iterator;
728 mraid_mmadp_t* adapter;
729
730 /*
731 * When the kioc returns from driver, make sure it still doesn't
732 * have ENODATA in status. Otherwise, driver will hang on wait_event
733 * forever
734 */
735 if (kioc->status == -ENODATA) {
736 con_log(CL_ANN, (KERN_WARNING
737 "megaraid cmm: lld didn't change status!\n"));
738
739 kioc->status = -EINVAL;
740 }
741
742 /*
743 * Check if this kioc was timedout before. If so, nobody is waiting
744 * on this kioc. We don't have to wake up anybody. Instead, we just
745 * have to free the kioc
746 */
747 if (kioc->timedout) {
748 iterator = 0;
749 adapter = NULL;
750 adapno = kioc->adapno;
751
752 con_log(CL_ANN, ( KERN_WARNING "megaraid cmm: completed "
753 "ioctl that was timedout before\n"));
754
755 list_for_each_entry(adapter, &adapters_list_g, list) {
756 if (iterator++ == adapno) break;
757 }
758
759 kioc->timedout = 0;
760
761 if (adapter) {
762 mraid_mm_dealloc_kioc( adapter, kioc );
763 }
764 }
765 else {
766 wake_up(&wait_q);
767 }
768}
769
770
Randy Dunlapa69b74d2007-01-05 22:41:48 -0800771/**
772 * lld_timedout - callback from the expired timer
Kees Cookc251a7b2017-10-22 15:30:04 -0700773 * @t : timer that timed out
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
775static void
Kees Cookc251a7b2017-10-22 15:30:04 -0700776lld_timedout(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Kees Cookc251a7b2017-10-22 15:30:04 -0700778 struct uioc_timeout *timeout = from_timer(timeout, t, timer);
779 uioc_t *kioc = timeout->uioc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 kioc->status = -ETIME;
782 kioc->timedout = 1;
783
784 con_log(CL_ANN, (KERN_WARNING "megaraid cmm: ioctl timed out\n"));
785
786 wake_up(&wait_q);
787}
788
789
790/**
Randy Dunlapa69b74d2007-01-05 22:41:48 -0800791 * kioc_to_mimd - Converter from new back to old format
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 * @kioc : Kernel space IOCTL packet (successfully issued)
793 * @mimd : User space MIMD packet
794 */
795static int
796kioc_to_mimd(uioc_t *kioc, mimd_t __user *mimd)
797{
798 mimd_t kmimd;
799 uint8_t opcode;
800 uint8_t subopcode;
801
802 mbox64_t *mbox64;
803 mraid_passthru_t __user *upthru32;
804 mraid_passthru_t *kpthru32;
805 mcontroller_t cinfo;
806 mraid_hba_info_t *hinfo;
807
808
809 if (copy_from_user(&kmimd, mimd, sizeof(mimd_t)))
810 return (-EFAULT);
811
812 opcode = kmimd.ui.fcs.opcode;
813 subopcode = kmimd.ui.fcs.subopcode;
814
815 if (opcode == 0x82) {
816 switch (subopcode) {
817
818 case MEGAIOC_QADAPINFO:
819
820 hinfo = (mraid_hba_info_t *)(unsigned long)
821 kioc->buf_vaddr;
822
823 hinfo_to_cinfo(hinfo, &cinfo);
824
825 if (copy_to_user(kmimd.data, &cinfo, sizeof(cinfo)))
826 return (-EFAULT);
827
828 return 0;
829
830 default:
831 return (-EINVAL);
832 }
833
834 return 0;
835 }
836
837 mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
838
839 if (kioc->user_pthru) {
840
841 upthru32 = kioc->user_pthru;
842 kpthru32 = kioc->pthru32;
843
844 if (copy_to_user(&upthru32->scsistatus,
845 &kpthru32->scsistatus,
846 sizeof(uint8_t))) {
847 return (-EFAULT);
848 }
849 }
850
851 if (kioc->user_data) {
852 if (copy_to_user(kioc->user_data, kioc->buf_vaddr,
853 kioc->user_data_len)) {
854 return (-EFAULT);
855 }
856 }
857
858 if (copy_to_user(&mimd->mbox[17],
859 &mbox64->mbox32.status, sizeof(uint8_t))) {
860 return (-EFAULT);
861 }
862
863 return 0;
864}
865
866
867/**
868 * hinfo_to_cinfo - Convert new format hba info into old format
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * @hinfo : New format, more comprehensive adapter info
870 * @cinfo : Old format adapter info to support mimd_t apps
871 */
872static void
873hinfo_to_cinfo(mraid_hba_info_t *hinfo, mcontroller_t *cinfo)
874{
875 if (!hinfo || !cinfo)
876 return;
877
878 cinfo->base = hinfo->baseport;
879 cinfo->irq = hinfo->irq;
880 cinfo->numldrv = hinfo->num_ldrv;
881 cinfo->pcibus = hinfo->pci_bus;
882 cinfo->pcidev = hinfo->pci_slot;
883 cinfo->pcifun = PCI_FUNC(hinfo->pci_dev_fn);
884 cinfo->pciid = hinfo->pci_device_id;
885 cinfo->pcivendor = hinfo->pci_vendor_id;
886 cinfo->pcislot = hinfo->pci_slot;
887 cinfo->uid = hinfo->unique_id;
888}
889
890
Randy Dunlapa69b74d2007-01-05 22:41:48 -0800891/**
892 * mraid_mm_register_adp - Registration routine for low level drivers
Matthias Schid3948ff82013-06-26 16:25:53 +0200893 * @lld_adp : Adapter object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
895int
896mraid_mm_register_adp(mraid_mmadp_t *lld_adp)
897{
898 mraid_mmadp_t *adapter;
899 mbox64_t *mbox_list;
900 uioc_t *kioc;
901 uint32_t rval;
902 int i;
903
904
905 if (lld_adp->drvr_type != DRVRTYPE_MBOX)
906 return (-EINVAL);
907
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700908 adapter = kzalloc(sizeof(mraid_mmadp_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Adrian Bunk52364672006-03-10 23:24:55 +0100910 if (!adapter)
911 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 adapter->unique_id = lld_adp->unique_id;
915 adapter->drvr_type = lld_adp->drvr_type;
916 adapter->drvr_data = lld_adp->drvr_data;
917 adapter->pdev = lld_adp->pdev;
918 adapter->issue_uioc = lld_adp->issue_uioc;
919 adapter->timeout = lld_adp->timeout;
920 adapter->max_kioc = lld_adp->max_kioc;
921 adapter->quiescent = 1;
922
923 /*
924 * Allocate single blocks of memory for all required kiocs,
925 * mailboxes and passthru structures.
926 */
Kees Cook6da2ec52018-06-12 13:55:00 -0700927 adapter->kioc_list = kmalloc_array(lld_adp->max_kioc,
928 sizeof(uioc_t),
929 GFP_KERNEL);
930 adapter->mbox_list = kmalloc_array(lld_adp->max_kioc,
931 sizeof(mbox64_t),
932 GFP_KERNEL);
Romain Perierfc69d862017-07-06 10:13:06 +0200933 adapter->pthru_dma_pool = dma_pool_create("megaraid mm pthru pool",
934 &adapter->pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 sizeof(mraid_passthru_t),
936 16, 0);
937
938 if (!adapter->kioc_list || !adapter->mbox_list ||
939 !adapter->pthru_dma_pool) {
940
941 con_log(CL_ANN, (KERN_WARNING
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700942 "megaraid cmm: out of memory, %s %d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 __LINE__));
944
945 rval = (-ENOMEM);
946
947 goto memalloc_error;
948 }
949
950 /*
951 * Slice kioc_list and make a kioc_pool with the individiual kiocs
952 */
953 INIT_LIST_HEAD(&adapter->kioc_pool);
954 spin_lock_init(&adapter->kioc_pool_lock);
955 sema_init(&adapter->kioc_semaphore, lld_adp->max_kioc);
956
957 mbox_list = (mbox64_t *)adapter->mbox_list;
958
959 for (i = 0; i < lld_adp->max_kioc; i++) {
960
961 kioc = adapter->kioc_list + i;
962 kioc->cmdbuf = (uint64_t)(unsigned long)(mbox_list + i);
Romain Perierfc69d862017-07-06 10:13:06 +0200963 kioc->pthru32 = dma_pool_alloc(adapter->pthru_dma_pool,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 GFP_KERNEL, &kioc->pthru32_h);
965
966 if (!kioc->pthru32) {
967
968 con_log(CL_ANN, (KERN_WARNING
969 "megaraid cmm: out of memory, %s %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700970 __func__, __LINE__));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 rval = (-ENOMEM);
973
974 goto pthru_dma_pool_error;
975 }
976
977 list_add_tail(&kioc->list, &adapter->kioc_pool);
978 }
979
980 // Setup the dma pools for data buffers
981 if ((rval = mraid_mm_setup_dma_pools(adapter)) != 0) {
982 goto dma_pool_error;
983 }
984
985 list_add_tail(&adapter->list, &adapters_list_g);
986
987 adapters_count_g++;
988
989 return 0;
990
991dma_pool_error:
992 /* Do nothing */
993
994pthru_dma_pool_error:
995
996 for (i = 0; i < lld_adp->max_kioc; i++) {
997 kioc = adapter->kioc_list + i;
998 if (kioc->pthru32) {
Romain Perierfc69d862017-07-06 10:13:06 +0200999 dma_pool_free(adapter->pthru_dma_pool, kioc->pthru32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 kioc->pthru32_h);
1001 }
1002 }
1003
1004memalloc_error:
1005
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001006 kfree(adapter->kioc_list);
1007 kfree(adapter->mbox_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Thomas Meyer19c05072018-12-02 21:52:11 +01001009 dma_pool_destroy(adapter->pthru_dma_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Jesper Juhlc9475cb2005-11-07 01:01:26 -08001011 kfree(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 return rval;
1014}
1015
1016
1017/**
1018 * mraid_mm_adapter_app_handle - return the application handle for this adapter
Randy Dunlapa69b74d2007-01-05 22:41:48 -08001019 * @unique_id : adapter unique identifier
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 *
Randy Dunlapa69b74d2007-01-05 22:41:48 -08001021 * For the given driver data, locate the adapter in our global list and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * return the corresponding handle, which is also used by applications to
1023 * uniquely identify an adapter.
1024 *
Randy Dunlapa69b74d2007-01-05 22:41:48 -08001025 * Return adapter handle if found in the list.
1026 * Return 0 if adapter could not be located, should never happen though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 */
1028uint32_t
1029mraid_mm_adapter_app_handle(uint32_t unique_id)
1030{
1031 mraid_mmadp_t *adapter;
1032 mraid_mmadp_t *tmp;
1033 int index = 0;
1034
1035 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1036
1037 if (adapter->unique_id == unique_id) {
1038
1039 return MKADAP(index);
1040 }
1041
1042 index++;
1043 }
1044
1045 return 0;
1046}
1047
1048
1049/**
1050 * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 * @adp : Adapter softstate
1052 *
1053 * We maintain a pool of dma buffers per each adapter. Each pool has one
1054 * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
1055 * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
1056 * dont' want to waste too much memory by allocating more buffers per each
1057 * pool.
1058 */
1059static int
1060mraid_mm_setup_dma_pools(mraid_mmadp_t *adp)
1061{
1062 mm_dmapool_t *pool;
1063 int bufsize;
1064 int i;
1065
1066 /*
1067 * Create MAX_DMA_POOLS number of pools
1068 */
1069 bufsize = MRAID_MM_INIT_BUFF_SIZE;
1070
1071 for (i = 0; i < MAX_DMA_POOLS; i++){
1072
1073 pool = &adp->dma_pool_list[i];
1074
1075 pool->buf_size = bufsize;
1076 spin_lock_init(&pool->lock);
1077
Romain Perierfc69d862017-07-06 10:13:06 +02001078 pool->handle = dma_pool_create("megaraid mm data buffer",
1079 &adp->pdev->dev, bufsize,
1080 16, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (!pool->handle) {
1083 goto dma_pool_setup_error;
1084 }
1085
Romain Perierfc69d862017-07-06 10:13:06 +02001086 pool->vaddr = dma_pool_alloc(pool->handle, GFP_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 &pool->paddr);
1088
1089 if (!pool->vaddr)
1090 goto dma_pool_setup_error;
1091
1092 bufsize = bufsize * 2;
1093 }
1094
1095 return 0;
1096
1097dma_pool_setup_error:
1098
1099 mraid_mm_teardown_dma_pools(adp);
1100 return (-ENOMEM);
1101}
1102
1103
Randy Dunlapa69b74d2007-01-05 22:41:48 -08001104/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 * mraid_mm_unregister_adp - Unregister routine for low level drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 * @unique_id : UID of the adpater
Randy Dunlapa69b74d2007-01-05 22:41:48 -08001107 *
1108 * Assumes no outstanding ioctls to llds.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 */
1110int
1111mraid_mm_unregister_adp(uint32_t unique_id)
1112{
1113 mraid_mmadp_t *adapter;
1114 mraid_mmadp_t *tmp;
1115
1116 list_for_each_entry_safe(adapter, tmp, &adapters_list_g, list) {
1117
1118
1119 if (adapter->unique_id == unique_id) {
1120
1121 adapters_count_g--;
1122
1123 list_del_init(&adapter->list);
1124
1125 mraid_mm_free_adp_resources(adapter);
1126
1127 kfree(adapter);
1128
1129 con_log(CL_ANN, (
1130 "megaraid cmm: Unregistered one adapter:%#x\n",
1131 unique_id));
1132
1133 return 0;
1134 }
1135 }
1136
1137 return (-ENODEV);
1138}
1139
1140/**
1141 * mraid_mm_free_adp_resources - Free adapter softstate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * @adp : Adapter softstate
1143 */
1144static void
1145mraid_mm_free_adp_resources(mraid_mmadp_t *adp)
1146{
1147 uioc_t *kioc;
1148 int i;
1149
1150 mraid_mm_teardown_dma_pools(adp);
1151
1152 for (i = 0; i < adp->max_kioc; i++) {
1153
1154 kioc = adp->kioc_list + i;
1155
Romain Perierfc69d862017-07-06 10:13:06 +02001156 dma_pool_free(adp->pthru_dma_pool, kioc->pthru32,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 kioc->pthru32_h);
1158 }
1159
1160 kfree(adp->kioc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 kfree(adp->mbox_list);
1162
Romain Perierfc69d862017-07-06 10:13:06 +02001163 dma_pool_destroy(adp->pthru_dma_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165
1166 return;
1167}
1168
1169
1170/**
1171 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * @adp : Adapter softstate
1173 */
1174static void
1175mraid_mm_teardown_dma_pools(mraid_mmadp_t *adp)
1176{
1177 int i;
1178 mm_dmapool_t *pool;
1179
1180 for (i = 0; i < MAX_DMA_POOLS; i++) {
1181
1182 pool = &adp->dma_pool_list[i];
1183
1184 if (pool->handle) {
1185
1186 if (pool->vaddr)
Romain Perierfc69d862017-07-06 10:13:06 +02001187 dma_pool_free(pool->handle, pool->vaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 pool->paddr);
1189
Romain Perierfc69d862017-07-06 10:13:06 +02001190 dma_pool_destroy(pool->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 pool->handle = NULL;
1192 }
1193 }
1194
1195 return;
1196}
1197
1198/**
Randy Dunlapa69b74d2007-01-05 22:41:48 -08001199 * mraid_mm_init - Module entry point
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
1201static int __init
1202mraid_mm_init(void)
1203{
Thomas Horsten90a95af2008-02-04 23:53:18 -08001204 int err;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 // Announce the driver version
1207 con_log(CL_ANN, (KERN_INFO "megaraid cmm: %s %s\n",
1208 LSI_COMMON_MOD_VERSION, LSI_COMMON_MOD_EXT_VERSION));
1209
Thomas Horsten90a95af2008-02-04 23:53:18 -08001210 err = misc_register(&megaraid_mm_dev);
1211 if (err < 0) {
1212 con_log(CL_ANN, ("megaraid cmm: cannot register misc device\n"));
1213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 init_waitqueue_head(&wait_q);
1217
1218 INIT_LIST_HEAD(&adapters_list_g);
1219
1220 return 0;
1221}
1222
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224/**
Randy Dunlapa69b74d2007-01-05 22:41:48 -08001225 * mraid_mm_exit - Module exit point
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
1227static void __exit
1228mraid_mm_exit(void)
1229{
1230 con_log(CL_DLEVEL1 , ("exiting common mod\n"));
1231
Thomas Horsten90a95af2008-02-04 23:53:18 -08001232 misc_deregister(&megaraid_mm_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235module_init(mraid_mm_init);
1236module_exit(mraid_mm_exit);
1237
1238/* vi: set ts=8 sw=8 tw=78: */