mic: vop: Fix use-after-free on remove
[linux/fpc-iii.git] / drivers / scsi / snic / snic_main.c
blob5e824fd6047a65761a587463fc82603fc8d3e354
1 /*
2 * Copyright 2014 Cisco Systems, Inc. All rights reserved.
4 * This program is free software; you may redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15 * SOFTWARE.
18 #include <linux/module.h>
19 #include <linux/mempool.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/pci.h>
25 #include <linux/skbuff.h>
26 #include <linux/interrupt.h>
27 #include <linux/spinlock.h>
28 #include <linux/workqueue.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_tcq.h>
32 #include "snic.h"
33 #include "snic_fwint.h"
35 #define PCI_DEVICE_ID_CISCO_SNIC 0x0046
37 /* Supported devices by snic module */
38 static struct pci_device_id snic_id_table[] = {
39 {PCI_DEVICE(0x1137, PCI_DEVICE_ID_CISCO_SNIC) },
40 { 0, } /* end of table */
43 unsigned int snic_log_level = 0x0;
44 module_param(snic_log_level, int, S_IRUGO|S_IWUSR);
45 MODULE_PARM_DESC(snic_log_level, "bitmask for snic logging levels");
47 #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
48 unsigned int snic_trace_max_pages = 16;
49 module_param(snic_trace_max_pages, uint, S_IRUGO|S_IWUSR);
50 MODULE_PARM_DESC(snic_trace_max_pages,
51 "Total allocated memory pages for snic trace buffer");
53 #endif
54 unsigned int snic_max_qdepth = SNIC_DFLT_QUEUE_DEPTH;
55 module_param(snic_max_qdepth, uint, S_IRUGO | S_IWUSR);
56 MODULE_PARM_DESC(snic_max_qdepth, "Queue depth to report for each LUN");
59 * snic_slave_alloc : callback function to SCSI Mid Layer, called on
60 * scsi device initialization.
62 static int
63 snic_slave_alloc(struct scsi_device *sdev)
65 struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
67 if (!tgt || snic_tgt_chkready(tgt))
68 return -ENXIO;
70 return 0;
74 * snic_slave_configure : callback function to SCSI Mid Layer, called on
75 * scsi device initialization.
77 static int
78 snic_slave_configure(struct scsi_device *sdev)
80 struct snic *snic = shost_priv(sdev->host);
81 u32 qdepth = 0, max_ios = 0;
82 int tmo = SNIC_DFLT_CMD_TIMEOUT * HZ;
84 /* Set Queue Depth */
85 max_ios = snic_max_qdepth;
86 qdepth = min_t(u32, max_ios, SNIC_MAX_QUEUE_DEPTH);
87 scsi_change_queue_depth(sdev, qdepth);
89 if (snic->fwinfo.io_tmo > 1)
90 tmo = snic->fwinfo.io_tmo * HZ;
92 /* FW requires extended timeouts */
93 blk_queue_rq_timeout(sdev->request_queue, tmo);
95 return 0;
98 static int
99 snic_change_queue_depth(struct scsi_device *sdev, int qdepth)
101 struct snic *snic = shost_priv(sdev->host);
102 int qsz = 0;
104 qsz = min_t(u32, qdepth, SNIC_MAX_QUEUE_DEPTH);
105 if (qsz < sdev->queue_depth)
106 atomic64_inc(&snic->s_stats.misc.qsz_rampdown);
107 else if (qsz > sdev->queue_depth)
108 atomic64_inc(&snic->s_stats.misc.qsz_rampup);
110 atomic64_set(&snic->s_stats.misc.last_qsz, sdev->queue_depth);
112 scsi_change_queue_depth(sdev, qsz);
114 return sdev->queue_depth;
117 static struct scsi_host_template snic_host_template = {
118 .module = THIS_MODULE,
119 .name = SNIC_DRV_NAME,
120 .queuecommand = snic_queuecommand,
121 .eh_abort_handler = snic_abort_cmd,
122 .eh_device_reset_handler = snic_device_reset,
123 .eh_host_reset_handler = snic_host_reset,
124 .slave_alloc = snic_slave_alloc,
125 .slave_configure = snic_slave_configure,
126 .change_queue_depth = snic_change_queue_depth,
127 .this_id = -1,
128 .cmd_per_lun = SNIC_DFLT_QUEUE_DEPTH,
129 .can_queue = SNIC_MAX_IO_REQ,
130 .sg_tablesize = SNIC_MAX_SG_DESC_CNT,
131 .max_sectors = 0x800,
132 .shost_attrs = snic_attrs,
133 .track_queue_depth = 1,
134 .cmd_size = sizeof(struct snic_internal_io_state),
135 .proc_name = "snic_scsi",
139 * snic_handle_link_event : Handles link events such as link up/down/error
141 void
142 snic_handle_link_event(struct snic *snic)
144 unsigned long flags;
146 spin_lock_irqsave(&snic->snic_lock, flags);
147 if (snic->stop_link_events) {
148 spin_unlock_irqrestore(&snic->snic_lock, flags);
150 return;
152 spin_unlock_irqrestore(&snic->snic_lock, flags);
154 queue_work(snic_glob->event_q, &snic->link_work);
155 } /* end of snic_handle_link_event */
158 * snic_notify_set : sets notification area
159 * This notification area is to receive events from fw
160 * Note: snic supports only MSIX interrupts, in which we can just call
161 * svnic_dev_notify_set directly
163 static int
164 snic_notify_set(struct snic *snic)
166 int ret = 0;
167 enum vnic_dev_intr_mode intr_mode;
169 intr_mode = svnic_dev_get_intr_mode(snic->vdev);
171 if (intr_mode == VNIC_DEV_INTR_MODE_MSIX) {
172 ret = svnic_dev_notify_set(snic->vdev, SNIC_MSIX_ERR_NOTIFY);
173 } else {
174 SNIC_HOST_ERR(snic->shost,
175 "Interrupt mode should be setup before devcmd notify set %d\n",
176 intr_mode);
177 ret = -1;
180 return ret;
181 } /* end of snic_notify_set */
184 * snic_dev_wait : polls vnic open status.
186 static int
187 snic_dev_wait(struct vnic_dev *vdev,
188 int (*start)(struct vnic_dev *, int),
189 int (*finished)(struct vnic_dev *, int *),
190 int arg)
192 unsigned long time;
193 int ret, done;
194 int retry_cnt = 0;
196 ret = start(vdev, arg);
197 if (ret)
198 return ret;
201 * Wait for func to complete...2 seconds max.
203 * Sometimes schedule_timeout_uninterruptible take long time
204 * to wakeup, which results skipping retry. The retry counter
205 * ensures to retry at least two times.
207 time = jiffies + (HZ * 2);
208 do {
209 ret = finished(vdev, &done);
210 if (ret)
211 return ret;
213 if (done)
214 return 0;
215 schedule_timeout_uninterruptible(HZ/10);
216 ++retry_cnt;
217 } while (time_after(time, jiffies) || (retry_cnt < 3));
219 return -ETIMEDOUT;
220 } /* end of snic_dev_wait */
223 * snic_cleanup: called by snic_remove
224 * Stops the snic device, masks all interrupts, Completed CQ entries are
225 * drained. Posted WQ/RQ/Copy-WQ entries are cleanup
227 static int
228 snic_cleanup(struct snic *snic)
230 unsigned int i;
231 int ret;
233 svnic_dev_disable(snic->vdev);
234 for (i = 0; i < snic->intr_count; i++)
235 svnic_intr_mask(&snic->intr[i]);
237 for (i = 0; i < snic->wq_count; i++) {
238 ret = svnic_wq_disable(&snic->wq[i]);
239 if (ret)
240 return ret;
243 /* Clean up completed IOs */
244 snic_fwcq_cmpl_handler(snic, -1);
246 snic_wq_cmpl_handler(snic, -1);
248 /* Clean up the IOs that have not completed */
249 for (i = 0; i < snic->wq_count; i++)
250 svnic_wq_clean(&snic->wq[i], snic_free_wq_buf);
252 for (i = 0; i < snic->cq_count; i++)
253 svnic_cq_clean(&snic->cq[i]);
255 for (i = 0; i < snic->intr_count; i++)
256 svnic_intr_clean(&snic->intr[i]);
258 /* Cleanup snic specific requests */
259 snic_free_all_untagged_reqs(snic);
261 /* Cleanup Pending SCSI commands */
262 snic_shutdown_scsi_cleanup(snic);
264 for (i = 0; i < SNIC_REQ_MAX_CACHES; i++)
265 mempool_destroy(snic->req_pool[i]);
267 return 0;
268 } /* end of snic_cleanup */
271 static void
272 snic_iounmap(struct snic *snic)
274 if (snic->bar0.vaddr)
275 iounmap(snic->bar0.vaddr);
279 * snic_vdev_open_done : polls for svnic_dev_open cmd completion.
281 static int
282 snic_vdev_open_done(struct vnic_dev *vdev, int *done)
284 struct snic *snic = svnic_dev_priv(vdev);
285 int ret;
286 int nretries = 5;
288 do {
289 ret = svnic_dev_open_done(vdev, done);
290 if (ret == 0)
291 break;
293 SNIC_HOST_INFO(snic->shost, "VNIC_DEV_OPEN Timedout.\n");
294 } while (nretries--);
296 return ret;
297 } /* end of snic_vdev_open_done */
300 * snic_add_host : registers scsi host with ML
302 static int
303 snic_add_host(struct Scsi_Host *shost, struct pci_dev *pdev)
305 int ret = 0;
307 ret = scsi_add_host(shost, &pdev->dev);
308 if (ret) {
309 SNIC_HOST_ERR(shost,
310 "snic: scsi_add_host failed. %d\n",
311 ret);
313 return ret;
316 SNIC_BUG_ON(shost->work_q != NULL);
317 snprintf(shost->work_q_name, sizeof(shost->work_q_name), "scsi_wq_%d",
318 shost->host_no);
319 shost->work_q = create_singlethread_workqueue(shost->work_q_name);
320 if (!shost->work_q) {
321 SNIC_HOST_ERR(shost, "Failed to Create ScsiHost wq.\n");
323 ret = -ENOMEM;
326 return ret;
327 } /* end of snic_add_host */
329 static void
330 snic_del_host(struct Scsi_Host *shost)
332 if (!shost->work_q)
333 return;
335 destroy_workqueue(shost->work_q);
336 shost->work_q = NULL;
337 scsi_remove_host(shost);
341 snic_get_state(struct snic *snic)
343 return atomic_read(&snic->state);
346 void
347 snic_set_state(struct snic *snic, enum snic_state state)
349 SNIC_HOST_INFO(snic->shost, "snic state change from %s to %s\n",
350 snic_state_to_str(snic_get_state(snic)),
351 snic_state_to_str(state));
353 atomic_set(&snic->state, state);
357 * snic_probe : Initialize the snic interface.
359 static int
360 snic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
362 struct Scsi_Host *shost;
363 struct snic *snic;
364 mempool_t *pool;
365 unsigned long flags;
366 u32 max_ios = 0;
367 int ret, i;
369 /* Device Information */
370 SNIC_INFO("snic device %4x:%4x:%4x:%4x: ",
371 pdev->vendor, pdev->device, pdev->subsystem_vendor,
372 pdev->subsystem_device);
374 SNIC_INFO("snic device bus %x: slot %x: fn %x\n",
375 pdev->bus->number, PCI_SLOT(pdev->devfn),
376 PCI_FUNC(pdev->devfn));
379 * Allocate SCSI Host and setup association between host, and snic
381 shost = scsi_host_alloc(&snic_host_template, sizeof(struct snic));
382 if (!shost) {
383 SNIC_ERR("Unable to alloc scsi_host\n");
384 ret = -ENOMEM;
386 goto prob_end;
388 snic = shost_priv(shost);
389 snic->shost = shost;
391 snprintf(snic->name, sizeof(snic->name) - 1, "%s%d", SNIC_DRV_NAME,
392 shost->host_no);
394 SNIC_HOST_INFO(shost,
395 "snic%d = %p shost = %p device bus %x: slot %x: fn %x\n",
396 shost->host_no, snic, shost, pdev->bus->number,
397 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
398 #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
399 /* Per snic debugfs init */
400 ret = snic_stats_debugfs_init(snic);
401 if (ret) {
402 SNIC_HOST_ERR(snic->shost,
403 "Failed to initialize debugfs stats\n");
404 snic_stats_debugfs_remove(snic);
406 #endif
408 /* Setup PCI Resources */
409 pci_set_drvdata(pdev, snic);
410 snic->pdev = pdev;
412 ret = pci_enable_device(pdev);
413 if (ret) {
414 SNIC_HOST_ERR(shost,
415 "Cannot enable PCI Resources, aborting : %d\n",
416 ret);
418 goto err_free_snic;
421 ret = pci_request_regions(pdev, SNIC_DRV_NAME);
422 if (ret) {
423 SNIC_HOST_ERR(shost,
424 "Cannot obtain PCI Resources, aborting : %d\n",
425 ret);
427 goto err_pci_disable;
430 pci_set_master(pdev);
433 * Query PCI Controller on system for DMA addressing
434 * limitation for the device. Try 43-bit first, and
435 * fail to 32-bit.
437 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(43));
438 if (ret) {
439 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
440 if (ret) {
441 SNIC_HOST_ERR(shost,
442 "No Usable DMA Configuration, aborting %d\n",
443 ret);
444 goto err_rel_regions;
448 /* Map vNIC resources from BAR0 */
449 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
450 SNIC_HOST_ERR(shost, "BAR0 not memory mappable aborting.\n");
452 ret = -ENODEV;
453 goto err_rel_regions;
456 snic->bar0.vaddr = pci_iomap(pdev, 0, 0);
457 if (!snic->bar0.vaddr) {
458 SNIC_HOST_ERR(shost,
459 "Cannot memory map BAR0 res hdr aborting.\n");
461 ret = -ENODEV;
462 goto err_rel_regions;
465 snic->bar0.bus_addr = pci_resource_start(pdev, 0);
466 snic->bar0.len = pci_resource_len(pdev, 0);
467 SNIC_BUG_ON(snic->bar0.bus_addr == 0);
469 /* Devcmd2 Resource Allocation and Initialization */
470 snic->vdev = svnic_dev_alloc_discover(NULL, snic, pdev, &snic->bar0, 1);
471 if (!snic->vdev) {
472 SNIC_HOST_ERR(shost, "vNIC Resource Discovery Failed.\n");
474 ret = -ENODEV;
475 goto err_iounmap;
478 ret = svnic_dev_cmd_init(snic->vdev, 0);
479 if (ret) {
480 SNIC_HOST_INFO(shost, "Devcmd2 Init Failed. err = %d\n", ret);
482 goto err_vnic_unreg;
485 ret = snic_dev_wait(snic->vdev, svnic_dev_open, snic_vdev_open_done, 0);
486 if (ret) {
487 SNIC_HOST_ERR(shost,
488 "vNIC dev open failed, aborting. %d\n",
489 ret);
491 goto err_vnic_unreg;
494 ret = svnic_dev_init(snic->vdev, 0);
495 if (ret) {
496 SNIC_HOST_ERR(shost,
497 "vNIC dev init failed. aborting. %d\n",
498 ret);
500 goto err_dev_close;
503 /* Get vNIC information */
504 ret = snic_get_vnic_config(snic);
505 if (ret) {
506 SNIC_HOST_ERR(shost,
507 "Get vNIC configuration failed, aborting. %d\n",
508 ret);
510 goto err_dev_close;
513 /* Configure Maximum Outstanding IO reqs */
514 max_ios = snic->config.io_throttle_count;
515 if (max_ios != SNIC_UCSM_DFLT_THROTTLE_CNT_BLD)
516 shost->can_queue = min_t(u32, SNIC_MAX_IO_REQ,
517 max_t(u32, SNIC_MIN_IO_REQ, max_ios));
519 snic->max_tag_id = shost->can_queue;
521 shost->max_lun = snic->config.luns_per_tgt;
522 shost->max_id = SNIC_MAX_TARGET;
524 shost->max_cmd_len = MAX_COMMAND_SIZE; /*defined in scsi_cmnd.h*/
526 snic_get_res_counts(snic);
529 * Assumption: Only MSIx is supported
531 ret = snic_set_intr_mode(snic);
532 if (ret) {
533 SNIC_HOST_ERR(shost,
534 "Failed to set intr mode aborting. %d\n",
535 ret);
537 goto err_dev_close;
540 ret = snic_alloc_vnic_res(snic);
541 if (ret) {
542 SNIC_HOST_ERR(shost,
543 "Failed to alloc vNIC resources aborting. %d\n",
544 ret);
546 goto err_clear_intr;
549 /* Initialize specific lists */
550 INIT_LIST_HEAD(&snic->list);
553 * spl_cmd_list for maintaining snic specific cmds
554 * such as EXCH_VER_REQ, REPORT_TARGETS etc
556 INIT_LIST_HEAD(&snic->spl_cmd_list);
557 spin_lock_init(&snic->spl_cmd_lock);
559 /* initialize all snic locks */
560 spin_lock_init(&snic->snic_lock);
562 for (i = 0; i < SNIC_WQ_MAX; i++)
563 spin_lock_init(&snic->wq_lock[i]);
565 for (i = 0; i < SNIC_IO_LOCKS; i++)
566 spin_lock_init(&snic->io_req_lock[i]);
568 pool = mempool_create_slab_pool(2,
569 snic_glob->req_cache[SNIC_REQ_CACHE_DFLT_SGL]);
570 if (!pool) {
571 SNIC_HOST_ERR(shost, "dflt sgl pool creation failed\n");
573 ret = -ENOMEM;
574 goto err_free_res;
577 snic->req_pool[SNIC_REQ_CACHE_DFLT_SGL] = pool;
579 pool = mempool_create_slab_pool(2,
580 snic_glob->req_cache[SNIC_REQ_CACHE_MAX_SGL]);
581 if (!pool) {
582 SNIC_HOST_ERR(shost, "max sgl pool creation failed\n");
584 ret = -ENOMEM;
585 goto err_free_dflt_sgl_pool;
588 snic->req_pool[SNIC_REQ_CACHE_MAX_SGL] = pool;
590 pool = mempool_create_slab_pool(2,
591 snic_glob->req_cache[SNIC_REQ_TM_CACHE]);
592 if (!pool) {
593 SNIC_HOST_ERR(shost, "snic tmreq info pool creation failed.\n");
595 ret = -ENOMEM;
596 goto err_free_max_sgl_pool;
599 snic->req_pool[SNIC_REQ_TM_CACHE] = pool;
601 /* Initialize snic state */
602 atomic_set(&snic->state, SNIC_INIT);
604 atomic_set(&snic->ios_inflight, 0);
606 /* Setup notification buffer area */
607 ret = snic_notify_set(snic);
608 if (ret) {
609 SNIC_HOST_ERR(shost,
610 "Failed to alloc notify buffer aborting. %d\n",
611 ret);
613 goto err_free_tmreq_pool;
616 spin_lock_irqsave(&snic_glob->snic_list_lock, flags);
617 list_add_tail(&snic->list, &snic_glob->snic_list);
618 spin_unlock_irqrestore(&snic_glob->snic_list_lock, flags);
620 snic_disc_init(&snic->disc);
621 INIT_WORK(&snic->tgt_work, snic_handle_tgt_disc);
622 INIT_WORK(&snic->disc_work, snic_handle_disc);
623 INIT_WORK(&snic->link_work, snic_handle_link);
625 /* Enable all queues */
626 for (i = 0; i < snic->wq_count; i++)
627 svnic_wq_enable(&snic->wq[i]);
629 ret = svnic_dev_enable_wait(snic->vdev);
630 if (ret) {
631 SNIC_HOST_ERR(shost,
632 "vNIC dev enable failed w/ error %d\n",
633 ret);
635 goto err_vdev_enable;
638 ret = snic_request_intr(snic);
639 if (ret) {
640 SNIC_HOST_ERR(shost, "Unable to request irq. %d\n", ret);
642 goto err_req_intr;
645 for (i = 0; i < snic->intr_count; i++)
646 svnic_intr_unmask(&snic->intr[i]);
648 /* Get snic params */
649 ret = snic_get_conf(snic);
650 if (ret) {
651 SNIC_HOST_ERR(shost,
652 "Failed to get snic io config from FW w err %d\n",
653 ret);
655 goto err_get_conf;
659 * Initialization done with PCI system, hardware, firmware.
660 * Add shost to SCSI
662 ret = snic_add_host(shost, pdev);
663 if (ret) {
664 SNIC_HOST_ERR(shost,
665 "Adding scsi host Failed ... exiting. %d\n",
666 ret);
668 goto err_get_conf;
671 snic_set_state(snic, SNIC_ONLINE);
673 ret = snic_disc_start(snic);
674 if (ret) {
675 SNIC_HOST_ERR(shost, "snic_probe:Discovery Failed w err = %d\n",
676 ret);
678 goto err_get_conf;
681 SNIC_HOST_INFO(shost, "SNIC Device Probe Successful.\n");
683 return 0;
685 err_get_conf:
686 snic_free_all_untagged_reqs(snic);
688 for (i = 0; i < snic->intr_count; i++)
689 svnic_intr_mask(&snic->intr[i]);
691 snic_free_intr(snic);
693 err_req_intr:
694 svnic_dev_disable(snic->vdev);
696 err_vdev_enable:
697 svnic_dev_notify_unset(snic->vdev);
699 for (i = 0; i < snic->wq_count; i++) {
700 int rc = 0;
702 rc = svnic_wq_disable(&snic->wq[i]);
703 if (rc) {
704 SNIC_HOST_ERR(shost,
705 "WQ Disable Failed w/ err = %d\n", rc);
707 break;
710 snic_del_host(snic->shost);
712 err_free_tmreq_pool:
713 mempool_destroy(snic->req_pool[SNIC_REQ_TM_CACHE]);
715 err_free_max_sgl_pool:
716 mempool_destroy(snic->req_pool[SNIC_REQ_CACHE_MAX_SGL]);
718 err_free_dflt_sgl_pool:
719 mempool_destroy(snic->req_pool[SNIC_REQ_CACHE_DFLT_SGL]);
721 err_free_res:
722 snic_free_vnic_res(snic);
724 err_clear_intr:
725 snic_clear_intr_mode(snic);
727 err_dev_close:
728 svnic_dev_close(snic->vdev);
730 err_vnic_unreg:
731 svnic_dev_unregister(snic->vdev);
733 err_iounmap:
734 snic_iounmap(snic);
736 err_rel_regions:
737 pci_release_regions(pdev);
739 err_pci_disable:
740 pci_disable_device(pdev);
742 err_free_snic:
743 #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
744 snic_stats_debugfs_remove(snic);
745 #endif
746 scsi_host_put(shost);
747 pci_set_drvdata(pdev, NULL);
749 prob_end:
750 SNIC_INFO("sNIC device : bus %d: slot %d: fn %d Registration Failed.\n",
751 pdev->bus->number, PCI_SLOT(pdev->devfn),
752 PCI_FUNC(pdev->devfn));
754 return ret;
755 } /* end of snic_probe */
759 * snic_remove : invoked on unbinding the interface to cleanup the
760 * resources allocated in snic_probe on initialization.
762 static void
763 snic_remove(struct pci_dev *pdev)
765 struct snic *snic = pci_get_drvdata(pdev);
766 unsigned long flags;
768 if (!snic) {
769 SNIC_INFO("sNIC dev: bus %d slot %d fn %d snic inst is null.\n",
770 pdev->bus->number, PCI_SLOT(pdev->devfn),
771 PCI_FUNC(pdev->devfn));
773 return;
777 * Mark state so that the workqueue thread stops forwarding
778 * received frames and link events. ISR and other threads
779 * that can queue work items will also stop creating work
780 * items on the snic workqueue
782 snic_set_state(snic, SNIC_OFFLINE);
783 spin_lock_irqsave(&snic->snic_lock, flags);
784 snic->stop_link_events = 1;
785 spin_unlock_irqrestore(&snic->snic_lock, flags);
787 flush_workqueue(snic_glob->event_q);
788 snic_disc_term(snic);
790 spin_lock_irqsave(&snic->snic_lock, flags);
791 snic->in_remove = 1;
792 spin_unlock_irqrestore(&snic->snic_lock, flags);
795 * This stops the snic device, masks all interrupts, Completed
796 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
797 * cleanup
799 snic_cleanup(snic);
801 spin_lock_irqsave(&snic_glob->snic_list_lock, flags);
802 list_del(&snic->list);
803 spin_unlock_irqrestore(&snic_glob->snic_list_lock, flags);
805 snic_tgt_del_all(snic);
806 #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
807 snic_stats_debugfs_remove(snic);
808 #endif
809 snic_del_host(snic->shost);
811 svnic_dev_notify_unset(snic->vdev);
812 snic_free_intr(snic);
813 snic_free_vnic_res(snic);
814 snic_clear_intr_mode(snic);
815 svnic_dev_close(snic->vdev);
816 svnic_dev_unregister(snic->vdev);
817 snic_iounmap(snic);
818 pci_release_regions(pdev);
819 pci_disable_device(pdev);
820 pci_set_drvdata(pdev, NULL);
822 /* this frees Scsi_Host and snic memory (continuous chunk) */
823 scsi_host_put(snic->shost);
824 } /* end of snic_remove */
827 struct snic_global *snic_glob;
830 * snic_global_data_init: Initialize SNIC Global Data
831 * Notes: All the global lists, variables should be part of global data
832 * this helps in debugging.
834 static int
835 snic_global_data_init(void)
837 int ret = 0;
838 struct kmem_cache *cachep;
839 ssize_t len = 0;
841 snic_glob = kzalloc(sizeof(*snic_glob), GFP_KERNEL);
843 if (!snic_glob) {
844 SNIC_ERR("Failed to allocate Global Context.\n");
846 ret = -ENOMEM;
847 goto gdi_end;
850 #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
851 /* Debugfs related Initialization */
852 /* Create debugfs entries for snic */
853 ret = snic_debugfs_init();
854 if (ret < 0) {
855 SNIC_ERR("Failed to create sysfs dir for tracing and stats.\n");
856 snic_debugfs_term();
857 /* continue even if it fails */
860 /* Trace related Initialization */
861 /* Allocate memory for trace buffer */
862 ret = snic_trc_init();
863 if (ret < 0) {
864 SNIC_ERR("Trace buffer init failed, SNIC tracing disabled\n");
865 snic_trc_free();
866 /* continue even if it fails */
869 #endif
870 INIT_LIST_HEAD(&snic_glob->snic_list);
871 spin_lock_init(&snic_glob->snic_list_lock);
873 /* Create a cache for allocation of snic_host_req+default size ESGLs */
874 len = sizeof(struct snic_req_info);
875 len += sizeof(struct snic_host_req) + sizeof(struct snic_dflt_sgl);
876 cachep = kmem_cache_create("snic_req_dfltsgl", len, SNIC_SG_DESC_ALIGN,
877 SLAB_HWCACHE_ALIGN, NULL);
878 if (!cachep) {
879 SNIC_ERR("Failed to create snic default sgl slab\n");
880 ret = -ENOMEM;
882 goto err_dflt_req_slab;
884 snic_glob->req_cache[SNIC_REQ_CACHE_DFLT_SGL] = cachep;
886 /* Create a cache for allocation of max size Extended SGLs */
887 len = sizeof(struct snic_req_info);
888 len += sizeof(struct snic_host_req) + sizeof(struct snic_max_sgl);
889 cachep = kmem_cache_create("snic_req_maxsgl", len, SNIC_SG_DESC_ALIGN,
890 SLAB_HWCACHE_ALIGN, NULL);
891 if (!cachep) {
892 SNIC_ERR("Failed to create snic max sgl slab\n");
893 ret = -ENOMEM;
895 goto err_max_req_slab;
897 snic_glob->req_cache[SNIC_REQ_CACHE_MAX_SGL] = cachep;
899 len = sizeof(struct snic_host_req);
900 cachep = kmem_cache_create("snic_req_maxsgl", len, SNIC_SG_DESC_ALIGN,
901 SLAB_HWCACHE_ALIGN, NULL);
902 if (!cachep) {
903 SNIC_ERR("Failed to create snic tm req slab\n");
904 ret = -ENOMEM;
906 goto err_tmreq_slab;
908 snic_glob->req_cache[SNIC_REQ_TM_CACHE] = cachep;
910 /* snic_event queue */
911 snic_glob->event_q = create_singlethread_workqueue("snic_event_wq");
912 if (!snic_glob->event_q) {
913 SNIC_ERR("snic event queue create failed\n");
914 ret = -ENOMEM;
916 goto err_eventq;
919 return ret;
921 err_eventq:
922 kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_TM_CACHE]);
924 err_tmreq_slab:
925 kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_CACHE_MAX_SGL]);
927 err_max_req_slab:
928 kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_CACHE_DFLT_SGL]);
930 err_dflt_req_slab:
931 #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
932 snic_trc_free();
933 snic_debugfs_term();
934 #endif
935 kfree(snic_glob);
936 snic_glob = NULL;
938 gdi_end:
939 return ret;
940 } /* end of snic_glob_init */
943 * snic_global_data_cleanup : Frees SNIC Global Data
945 static void
946 snic_global_data_cleanup(void)
948 SNIC_BUG_ON(snic_glob == NULL);
950 destroy_workqueue(snic_glob->event_q);
951 kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_TM_CACHE]);
952 kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_CACHE_MAX_SGL]);
953 kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_CACHE_DFLT_SGL]);
955 #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
956 /* Freeing Trace Resources */
957 snic_trc_free();
959 /* Freeing Debugfs Resources */
960 snic_debugfs_term();
961 #endif
962 kfree(snic_glob);
963 snic_glob = NULL;
964 } /* end of snic_glob_cleanup */
966 static struct pci_driver snic_driver = {
967 .name = SNIC_DRV_NAME,
968 .id_table = snic_id_table,
969 .probe = snic_probe,
970 .remove = snic_remove,
973 static int __init
974 snic_init_module(void)
976 int ret = 0;
978 #ifndef __x86_64__
979 SNIC_INFO("SNIC Driver is supported only for x86_64 platforms!\n");
980 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
981 #endif
983 SNIC_INFO("%s, ver %s\n", SNIC_DRV_DESCRIPTION, SNIC_DRV_VERSION);
985 ret = snic_global_data_init();
986 if (ret) {
987 SNIC_ERR("Failed to Initialize Global Data.\n");
989 return ret;
992 ret = pci_register_driver(&snic_driver);
993 if (ret < 0) {
994 SNIC_ERR("PCI driver register error\n");
996 goto err_pci_reg;
999 return ret;
1001 err_pci_reg:
1002 snic_global_data_cleanup();
1004 return ret;
1007 static void __exit
1008 snic_cleanup_module(void)
1010 pci_unregister_driver(&snic_driver);
1011 snic_global_data_cleanup();
1014 module_init(snic_init_module);
1015 module_exit(snic_cleanup_module);
1017 MODULE_LICENSE("GPL v2");
1018 MODULE_DESCRIPTION(SNIC_DRV_DESCRIPTION);
1019 MODULE_VERSION(SNIC_DRV_VERSION);
1020 MODULE_DEVICE_TABLE(pci, snic_id_table);
1021 MODULE_AUTHOR("Narsimhulu Musini <nmusini@cisco.com>, "
1022 "Sesidhar Baddela <sebaddel@cisco.com>");