1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
4 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
5 */
6 #include <linux/module.h>
7 #include <linux/mempool.h>
8 #include <linux/string.h>
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/pci.h>
13 #include <linux/skbuff.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/spinlock.h>
17 #include <linux/workqueue.h>
18 #include <linux/if_ether.h>
19 #include <scsi/fc/fc_fip.h>
20 #include <scsi/scsi_host.h>
21 #include <scsi/scsi_transport.h>
22 #include <scsi/scsi_transport_fc.h>
23 #include <scsi/scsi_tcq.h>
24 #include <scsi/fc_frame.h>
25
26 #include "vnic_dev.h"
27 #include "vnic_intr.h"
28 #include "vnic_stats.h"
29 #include "fnic_io.h"
30 #include "fnic.h"
31 #include "fnic_fdls.h"
32 #include "fdls_fc.h"
33
34 #define PCI_DEVICE_ID_CISCO_FNIC 0x0045
35
36 /* Timer to poll notification area for events. Used for MSI interrupts */
37 #define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ)
38
39 static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
40 static struct kmem_cache *fnic_io_req_cache;
41 static struct kmem_cache *fdls_frame_cache;
42 static struct kmem_cache *fdls_frame_elem_cache;
43 static struct kmem_cache *fdls_frame_recv_cache;
44 static LIST_HEAD(fnic_list);
45 static DEFINE_IDA(fnic_ida);
46
47 struct work_struct reset_fnic_work;
48 LIST_HEAD(reset_fnic_list);
49 DEFINE_SPINLOCK(reset_fnic_list_lock);
50 DEFINE_SPINLOCK(fnic_list_lock);
51
52 /* Supported devices by fnic module */
53 static const struct pci_device_id fnic_id_table[] = {
54 { PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) },
55 { 0, }
56 };
57
58 MODULE_DESCRIPTION(DRV_DESCRIPTION);
59 MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
60 "Joseph R. Eykholt <jeykholt@cisco.com>");
61 MODULE_LICENSE("GPL v2");
62 MODULE_VERSION(DRV_VERSION);
63 MODULE_DEVICE_TABLE(pci, fnic_id_table);
64
65 unsigned int fnic_log_level;
66 module_param(fnic_log_level, int, S_IRUGO|S_IWUSR);
67 MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels");
68
69 unsigned int fnic_fdmi_support = 1;
70 module_param(fnic_fdmi_support, int, 0644);
71 MODULE_PARM_DESC(fnic_fdmi_support, "FDMI support");
72
73 static unsigned int fnic_tgt_id_binding = 1;
74 module_param(fnic_tgt_id_binding, uint, 0644);
75 MODULE_PARM_DESC(fnic_tgt_id_binding,
76 "Target ID binding (0 for none. 1 for binding by WWPN (default))");
77
78 unsigned int io_completions = FNIC_DFLT_IO_COMPLETIONS;
79 module_param(io_completions, int, S_IRUGO|S_IWUSR);
80 MODULE_PARM_DESC(io_completions, "Max CQ entries to process at a time");
81
82 unsigned int fnic_trace_max_pages = 16;
83 module_param(fnic_trace_max_pages, uint, S_IRUGO|S_IWUSR);
84 MODULE_PARM_DESC(fnic_trace_max_pages, "Total allocated memory pages "
85 "for fnic trace buffer");
86
87 unsigned int fnic_fc_trace_max_pages = 64;
88 module_param(fnic_fc_trace_max_pages, uint, S_IRUGO|S_IWUSR);
89 MODULE_PARM_DESC(fnic_fc_trace_max_pages,
90 "Total allocated memory pages for fc trace buffer");
91
92 unsigned int nvme_dev_loss_tmo = 30;
93 module_param_named(nvme_dev_loss_tmo, nvme_dev_loss_tmo, uint, 0644);
94 MODULE_PARM_DESC(nvme_dev_loss_tmo, "configurable NVME dev loss timeout");
95
96 unsigned int nvme_max_ios_to_process = 16;
97 module_param(nvme_max_ios_to_process, uint, 0644);
98 MODULE_PARM_DESC(nvme_max_ios_to_process,
99 "Maximum number of NVME IOs to process per work queue");
100
101 static unsigned int fnic_max_qdepth = FNIC_DFLT_QUEUE_DEPTH;
102 module_param(fnic_max_qdepth, uint, S_IRUGO|S_IWUSR);
103 MODULE_PARM_DESC(fnic_max_qdepth, "Queue depth to report for each LUN");
104
105 unsigned int pc_rscn_handling_feature_flag = PC_RSCN_HANDLING_FEATURE_ON;
106 module_param(pc_rscn_handling_feature_flag, uint, 0644);
107 MODULE_PARM_DESC(pc_rscn_handling_feature_flag,
108 "PCRSCN handling (0 for none. 1 to handle PCRSCN (default))");
109
110 struct workqueue_struct *reset_fnic_work_queue;
111 struct workqueue_struct *fnic_fip_queue;
112 struct workqueue_struct *fnic_cmpl_queue;
113
fnic_sdev_init(struct scsi_device * sdev)114 static int fnic_sdev_init(struct scsi_device *sdev)
115 {
116 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
117
118 if (!rport || fc_remote_port_chkready(rport))
119 return -ENXIO;
120
121 scsi_change_queue_depth(sdev, fnic_max_qdepth);
122 return 0;
123 }
124
125 static const struct scsi_host_template fnic_host_template = {
126 .module = THIS_MODULE,
127 .name = DRV_NAME,
128 .queuecommand = fnic_queuecommand,
129 .eh_timed_out = fc_eh_timed_out,
130 .eh_abort_handler = fnic_abort_cmd,
131 .eh_device_reset_handler = fnic_device_reset,
132 .eh_host_reset_handler = fnic_eh_host_reset_handler,
133 .sdev_init = fnic_sdev_init,
134 .change_queue_depth = scsi_change_queue_depth,
135 .this_id = -1,
136 .cmd_per_lun = 3,
137 .can_queue = FNIC_DFLT_IO_REQ,
138 .sg_tablesize = FNIC_MAX_SG_DESC_CNT,
139 .max_sectors = 0xffff,
140 .shost_groups = fnic_host_groups,
141 .track_queue_depth = 1,
142 .cmd_size = sizeof(struct fnic_cmd_priv),
143 .map_queues = fnic_mq_map_queues_cpus,
144 };
145
146 static void
fnic_set_rport_dev_loss_tmo(struct fc_rport * rport,u32 timeout)147 fnic_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
148 {
149 if (timeout)
150 rport->dev_loss_tmo = timeout;
151 else
152 rport->dev_loss_tmo = 1;
153 }
154
155 static void fnic_get_host_speed(struct Scsi_Host *shost);
156 static struct scsi_transport_template *fnic_fc_transport;
157 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *);
158 static void fnic_reset_host_stats(struct Scsi_Host *);
159
160 static struct fc_function_template fnic_fc_functions = {
161
162 .show_host_node_name = 1,
163 .show_host_port_name = 1,
164 .show_host_supported_classes = 1,
165 .show_host_supported_fc4s = 1,
166 .show_host_active_fc4s = 1,
167 .show_host_maxframe_size = 1,
168 .show_host_port_id = 1,
169 .show_host_supported_speeds = 1,
170 .get_host_speed = fnic_get_host_speed,
171 .show_host_speed = 1,
172 .show_host_port_type = 1,
173 .get_host_port_state = fnic_get_host_port_state,
174 .show_host_port_state = 1,
175 .show_host_symbolic_name = 1,
176 .show_rport_maxframe_size = 1,
177 .show_rport_supported_classes = 1,
178 .show_host_fabric_name = 1,
179 .show_starget_node_name = 1,
180 .show_starget_port_name = 1,
181 .show_starget_port_id = 1,
182 .show_rport_dev_loss_tmo = 1,
183 .set_rport_dev_loss_tmo = fnic_set_rport_dev_loss_tmo,
184 .issue_fc_host_lip = fnic_issue_fc_host_lip,
185 .get_fc_host_stats = fnic_get_stats,
186 .reset_fc_host_stats = fnic_reset_host_stats,
187 .dd_fcrport_size = sizeof(struct rport_dd_data_s),
188 .terminate_rport_io = fnic_terminate_rport_io,
189 .bsg_request = NULL,
190 };
191
fnic_get_host_speed(struct Scsi_Host * shost)192 static void fnic_get_host_speed(struct Scsi_Host *shost)
193 {
194 struct fnic *fnic = *((struct fnic **) shost_priv(shost));
195 u32 port_speed = vnic_dev_port_speed(fnic->vdev);
196 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
197
198 FNIC_MAIN_DBG(KERN_INFO, fnic,
199 "port_speed: %d Mbps", port_speed);
200 atomic64_set(&fnic_stats->misc_stats.port_speed_in_mbps, port_speed);
201
202 /* Add in other values as they get defined in fw */
203 switch (port_speed) {
204 case DCEM_PORTSPEED_1G:
205 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
206 break;
207 case DCEM_PORTSPEED_2G:
208 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
209 break;
210 case DCEM_PORTSPEED_4G:
211 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
212 break;
213 case DCEM_PORTSPEED_8G:
214 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
215 break;
216 case DCEM_PORTSPEED_10G:
217 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
218 break;
219 case DCEM_PORTSPEED_16G:
220 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
221 break;
222 case DCEM_PORTSPEED_20G:
223 fc_host_speed(shost) = FC_PORTSPEED_20GBIT;
224 break;
225 case DCEM_PORTSPEED_25G:
226 fc_host_speed(shost) = FC_PORTSPEED_25GBIT;
227 break;
228 case DCEM_PORTSPEED_32G:
229 fc_host_speed(shost) = FC_PORTSPEED_32GBIT;
230 break;
231 case DCEM_PORTSPEED_40G:
232 case DCEM_PORTSPEED_4x10G:
233 fc_host_speed(shost) = FC_PORTSPEED_40GBIT;
234 break;
235 case DCEM_PORTSPEED_50G:
236 fc_host_speed(shost) = FC_PORTSPEED_50GBIT;
237 break;
238 case DCEM_PORTSPEED_64G:
239 fc_host_speed(shost) = FC_PORTSPEED_64GBIT;
240 break;
241 case DCEM_PORTSPEED_100G:
242 fc_host_speed(shost) = FC_PORTSPEED_100GBIT;
243 break;
244 case DCEM_PORTSPEED_128G:
245 fc_host_speed(shost) = FC_PORTSPEED_128GBIT;
246 break;
247 default:
248 FNIC_MAIN_DBG(KERN_INFO, fnic,
249 "Unknown FC speed: %d Mbps", port_speed);
250 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
251 break;
252 }
253 }
254
255 /* Placeholder function */
fnic_get_stats(struct Scsi_Host * host)256 static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
257 {
258 int ret;
259 struct fnic *fnic = *((struct fnic **) shost_priv(host));
260 struct fc_host_statistics *stats = &fnic->fnic_stats.host_stats;
261 struct vnic_stats *vs;
262 unsigned long flags;
263
264 if (time_before
265 (jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
266 return stats;
267 fnic->stats_time = jiffies;
268
269 spin_lock_irqsave(&fnic->fnic_lock, flags);
270 ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
271 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
272
273 if (ret) {
274 FNIC_MAIN_DBG(KERN_DEBUG, fnic,
275 "fnic: Get vnic stats failed: 0x%x", ret);
276 return stats;
277 }
278 vs = fnic->stats;
279 stats->tx_frames = vs->tx.tx_unicast_frames_ok;
280 stats->tx_words = vs->tx.tx_unicast_bytes_ok / 4;
281 stats->rx_frames = vs->rx.rx_unicast_frames_ok;
282 stats->rx_words = vs->rx.rx_unicast_bytes_ok / 4;
283 stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
284 stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
285 stats->invalid_crc_count = vs->rx.rx_crc_errors;
286 stats->seconds_since_last_reset =
287 (jiffies - fnic->stats_reset_time) / HZ;
288 stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
289 stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
290 return stats;
291 }
292
293 /*
294 * fnic_dump_fchost_stats
295 * note : dumps fc_statistics into system logs
296 */
fnic_dump_fchost_stats(struct Scsi_Host * host,struct fc_host_statistics * stats)297 void fnic_dump_fchost_stats(struct Scsi_Host *host,
298 struct fc_host_statistics *stats)
299 {
300 struct fnic *fnic = *((struct fnic **) shost_priv(host));
301
302 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
303 "fnic: seconds since last reset = %llu\n",
304 stats->seconds_since_last_reset);
305 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
306 "fnic: tx frames = %llu\n",
307 stats->tx_frames);
308 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
309 "fnic: tx words = %llu\n",
310 stats->tx_words);
311 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
312 "fnic: rx frames = %llu\n",
313 stats->rx_frames);
314 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
315 "fnic: rx words = %llu\n",
316 stats->rx_words);
317 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
318 "fnic: lip count = %llu\n",
319 stats->lip_count);
320 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
321 "fnic: nos count = %llu\n",
322 stats->nos_count);
323 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
324 "fnic: error frames = %llu\n",
325 stats->error_frames);
326 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
327 "fnic: dumped frames = %llu\n",
328 stats->dumped_frames);
329 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
330 "fnic: link failure count = %llu\n",
331 stats->link_failure_count);
332 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
333 "fnic: loss of sync count = %llu\n",
334 stats->loss_of_sync_count);
335 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
336 "fnic: loss of signal count = %llu\n",
337 stats->loss_of_signal_count);
338 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
339 "fnic: prim seq protocol err count = %llu\n",
340 stats->prim_seq_protocol_err_count);
341 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
342 "fnic: invalid tx word count= %llu\n",
343 stats->invalid_tx_word_count);
344 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
345 "fnic: invalid crc count = %llu\n",
346 stats->invalid_crc_count);
347 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
348 "fnic: fcp input requests = %llu\n",
349 stats->fcp_input_requests);
350 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
351 "fnic: fcp output requests = %llu\n",
352 stats->fcp_output_requests);
353 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
354 "fnic: fcp control requests = %llu\n",
355 stats->fcp_control_requests);
356 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
357 "fnic: fcp input megabytes = %llu\n",
358 stats->fcp_input_megabytes);
359 FNIC_MAIN_NOTE(KERN_NOTICE, fnic,
360 "fnic: fcp output megabytes = %llu\n",
361 stats->fcp_output_megabytes);
362 return;
363 }
364
365 /*
366 * fnic_reset_host_stats : clears host stats
367 * note : called when reset_statistics set under sysfs dir
368 */
fnic_reset_host_stats(struct Scsi_Host * host)369 static void fnic_reset_host_stats(struct Scsi_Host *host)
370 {
371 int ret;
372 struct fnic *fnic = *((struct fnic **) shost_priv(host));
373 struct fc_host_statistics *stats;
374 unsigned long flags;
375
376 /* dump current stats, before clearing them */
377 stats = fnic_get_stats(host);
378 fnic_dump_fchost_stats(host, stats);
379
380 spin_lock_irqsave(&fnic->fnic_lock, flags);
381 ret = vnic_dev_stats_clear(fnic->vdev);
382 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
383
384 if (ret) {
385 FNIC_MAIN_DBG(KERN_DEBUG, fnic,
386 "fnic: Reset vnic stats failed"
387 " 0x%x", ret);
388 return;
389 }
390 fnic->stats_reset_time = jiffies;
391 memset(stats, 0, sizeof(*stats));
392
393 return;
394 }
395
fnic_log_q_error(struct fnic * fnic)396 void fnic_log_q_error(struct fnic *fnic)
397 {
398 unsigned int i;
399 u32 error_status;
400
401 for (i = 0; i < fnic->raw_wq_count; i++) {
402 error_status = ioread32(&fnic->wq[i].ctrl->error_status);
403 if (error_status)
404 dev_err(&fnic->pdev->dev, "WQ[%d] error_status %d\n", i, error_status);
405 }
406
407 for (i = 0; i < fnic->rq_count; i++) {
408 error_status = ioread32(&fnic->rq[i].ctrl->error_status);
409 if (error_status)
410 dev_err(&fnic->pdev->dev, "RQ[%d] error_status %d\n", i, error_status);
411 }
412
413 for (i = 0; i < fnic->wq_copy_count; i++) {
414 error_status = ioread32(&fnic->hw_copy_wq[i].ctrl->error_status);
415 if (error_status)
416 dev_err(&fnic->pdev->dev, "CWQ[%d] error_status %d\n", i, error_status);
417 }
418 }
419
fnic_handle_link_event(struct fnic * fnic)420 void fnic_handle_link_event(struct fnic *fnic)
421 {
422 unsigned long flags;
423
424 spin_lock_irqsave(&fnic->fnic_lock, flags);
425 if (fnic->stop_rx_link_events) {
426 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
427 return;
428 }
429 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
430
431 queue_work(fnic_event_queue, &fnic->link_work);
432
433 }
434
fnic_notify_set(struct fnic * fnic)435 static int fnic_notify_set(struct fnic *fnic)
436 {
437 int err;
438
439 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
440 case VNIC_DEV_INTR_MODE_INTX:
441 err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
442 break;
443 case VNIC_DEV_INTR_MODE_MSI:
444 err = vnic_dev_notify_set(fnic->vdev, -1);
445 break;
446 case VNIC_DEV_INTR_MODE_MSIX:
447 err = vnic_dev_notify_set(fnic->vdev, fnic->wq_copy_count + fnic->copy_wq_base);
448 break;
449 default:
450 dev_err(&fnic->pdev->dev, "Interrupt mode should be set up"
451 " before devcmd notify set %d\n",
452 vnic_dev_get_intr_mode(fnic->vdev));
453 err = -1;
454 break;
455 }
456
457 return err;
458 }
459
fnic_notify_timer(struct timer_list * t)460 static void fnic_notify_timer(struct timer_list *t)
461 {
462 struct fnic *fnic = timer_container_of(fnic, t, notify_timer);
463
464 fnic_handle_link_event(fnic);
465 mod_timer(&fnic->notify_timer,
466 round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
467 }
468
fnic_notify_timer_start(struct fnic * fnic)469 static void fnic_notify_timer_start(struct fnic *fnic)
470 {
471 switch (vnic_dev_get_intr_mode(fnic->vdev)) {
472 case VNIC_DEV_INTR_MODE_MSI:
473 /*
474 * Schedule first timeout immediately. The driver is
475 * initiatialized and ready to look for link up notification
476 */
477 mod_timer(&fnic->notify_timer, jiffies);
478 break;
479 default:
480 /* Using intr for notification for INTx/MSI-X */
481 break;
482 }
483 }
484
fnic_dev_wait(struct vnic_dev * vdev,int (* start)(struct vnic_dev *,int),int (* finished)(struct vnic_dev *,int *),int arg)485 static int fnic_dev_wait(struct vnic_dev *vdev,
486 int (*start)(struct vnic_dev *, int),
487 int (*finished)(struct vnic_dev *, int *),
488 int arg)
489 {
490 unsigned long time;
491 int done;
492 int err;
493 int count;
494
495 count = 0;
496
497 err = start(vdev, arg);
498 if (err)
499 return err;
500
501 /* Wait for func to complete.
502 * Sometime schedule_timeout_uninterruptible take long time
503 * to wake up so we do not retry as we are only waiting for
504 * 2 seconds in while loop. By adding count, we make sure
505 * we try atleast three times before returning -ETIMEDOUT
506 */
507 time = jiffies + (HZ * 2);
508 do {
509 err = finished(vdev, &done);
510 count++;
511 if (err)
512 return err;
513 if (done)
514 return 0;
515 schedule_timeout_uninterruptible(HZ / 10);
516 } while (time_after(time, jiffies) || (count < 3));
517
518 return -ETIMEDOUT;
519 }
520
fnic_cleanup(struct fnic * fnic)521 static int fnic_cleanup(struct fnic *fnic)
522 {
523 unsigned int i;
524 int err;
525 int raw_wq_rq_counts;
526
527 vnic_dev_disable(fnic->vdev);
528 for (i = 0; i < fnic->intr_count; i++)
529 vnic_intr_mask(&fnic->intr[i]);
530
531 for (i = 0; i < fnic->rq_count; i++) {
532 err = vnic_rq_disable(&fnic->rq[i]);
533 if (err)
534 return err;
535 }
536 for (i = 0; i < fnic->raw_wq_count; i++) {
537 err = vnic_wq_disable(&fnic->wq[i]);
538 if (err)
539 return err;
540 }
541 for (i = 0; i < fnic->wq_copy_count; i++) {
542 err = vnic_wq_copy_disable(&fnic->hw_copy_wq[i]);
543 if (err)
544 return err;
545 raw_wq_rq_counts = fnic->raw_wq_count + fnic->rq_count;
546 fnic_wq_copy_cmpl_handler(fnic, -1, i + raw_wq_rq_counts);
547 }
548
549 /* Clean up completed IOs and FCS frames */
550 fnic_wq_cmpl_handler(fnic, -1);
551 fnic_rq_cmpl_handler(fnic, -1);
552
553 /* Clean up the IOs and FCS frames that have not completed */
554 for (i = 0; i < fnic->raw_wq_count; i++)
555 vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
556 for (i = 0; i < fnic->rq_count; i++)
557 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
558 for (i = 0; i < fnic->wq_copy_count; i++)
559 vnic_wq_copy_clean(&fnic->hw_copy_wq[i],
560 fnic_wq_copy_cleanup_handler);
561
562 if (IS_FNIC_NVME_INITIATOR(fnic))
563 nvfnic_flush_nvme_io_list(fnic);
564
565 for (i = 0; i < fnic->cq_count; i++)
566 vnic_cq_clean(&fnic->cq[i]);
567 for (i = 0; i < fnic->intr_count; i++)
568 vnic_intr_clean(&fnic->intr[i]);
569
570 mempool_destroy(fnic->io_req_pool);
571 mempool_destroy(fnic->frame_pool);
572 mempool_destroy(fnic->frame_elem_pool);
573 mempool_destroy(fnic->frame_recv_pool);
574 for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
575 mempool_destroy(fnic->io_sgl_pool[i]);
576
577 return 0;
578 }
579
fnic_iounmap(struct fnic * fnic)580 static void fnic_iounmap(struct fnic *fnic)
581 {
582 if (fnic->bar0.vaddr)
583 iounmap(fnic->bar0.vaddr);
584 }
585
fnic_set_vlan(struct fnic * fnic,u16 vlan_id)586 static void fnic_set_vlan(struct fnic *fnic, u16 vlan_id)
587 {
588 vnic_dev_set_default_vlan(fnic->vdev, vlan_id);
589 }
590
fnic_scsi_init(struct fnic * fnic)591 static void fnic_scsi_init(struct fnic *fnic)
592 {
593 struct Scsi_Host *host = fnic->host;
594
595 snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
596 host->host_no);
597
598 host->transportt = fnic_fc_transport;
599 }
600
fnic_free_ioreq_tables_mq(struct fnic * fnic)601 static void fnic_free_ioreq_tables_mq(struct fnic *fnic)
602 {
603 int hwq;
604
605 for (hwq = 0; hwq < fnic->wq_copy_count; hwq++)
606 kfree(fnic->sw_copy_wq[hwq].io_req_table);
607 }
608
fnic_scsi_drv_init(struct fnic * fnic)609 static int fnic_scsi_drv_init(struct fnic *fnic)
610 {
611 struct Scsi_Host *host = fnic->host;
612 int err;
613 struct pci_dev *pdev = fnic->pdev;
614 struct fnic_iport_s *iport = &fnic->iport;
615 int hwq;
616
617 /* Configure maximum outstanding IO reqs*/
618 if (fnic->config.io_throttle_count != FNIC_UCSM_DFLT_THROTTLE_CNT_BLD)
619 host->can_queue = min_t(u32, FNIC_MAX_IO_REQ,
620 max_t(u32, FNIC_MIN_IO_REQ,
621 fnic->config.io_throttle_count));
622
623 fnic->fnic_max_tag_id = host->can_queue;
624 host->max_lun = fnic->config.luns_per_tgt;
625 host->max_id = FNIC_MAX_FCP_TARGET;
626 host->max_cmd_len = FNIC_FCOE_MAX_CMD_LEN;
627
628 host->nr_hw_queues = fnic->wq_copy_count;
629
630 dev_info(&fnic->pdev->dev, "fnic: can_queue: %d max_lun: %llu",
631 host->can_queue, host->max_lun);
632
633 dev_info(&fnic->pdev->dev, "fnic: max_id: %d max_cmd_len: %d nr_hw_queues: %d",
634 host->max_id, host->max_cmd_len, host->nr_hw_queues);
635
636 for (hwq = 0; hwq < fnic->wq_copy_count; hwq++) {
637 fnic->sw_copy_wq[hwq].ioreq_table_size = fnic->fnic_max_tag_id;
638 fnic->sw_copy_wq[hwq].io_req_table =
639 kzalloc((fnic->sw_copy_wq[hwq].ioreq_table_size + 1) *
640 sizeof(struct fnic_io_req *), GFP_KERNEL);
641
642 if (!fnic->sw_copy_wq[hwq].io_req_table) {
643 fnic_free_ioreq_tables_mq(fnic);
644 return -ENOMEM;
645 }
646 }
647
648 dev_info(&fnic->pdev->dev, "fnic copy wqs: %d, Q0 ioreq table size: %d\n",
649 fnic->wq_copy_count, fnic->sw_copy_wq[0].ioreq_table_size);
650
651 fnic_scsi_init(fnic);
652
653 err = scsi_add_host(fnic->host, &pdev->dev);
654 if (err) {
655 dev_err(&fnic->pdev->dev, "fnic: scsi add host failed: aborting\n");
656 return err;
657 }
658 fc_host_maxframe_size(fnic->host) = iport->max_payload_size;
659 fc_host_dev_loss_tmo(fnic->host) =
660 fnic->config.port_down_timeout / 1000;
661 sprintf(fc_host_symbolic_name(fnic->host),
662 DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
663 fc_host_port_type(fnic->host) = FC_PORTTYPE_NPORT;
664 fc_host_node_name(fnic->host) = iport->wwnn;
665 fc_host_port_name(fnic->host) = iport->wwpn;
666 fc_host_supported_classes(fnic->host) = FC_COS_CLASS3;
667 memset(fc_host_supported_fc4s(fnic->host), 0,
668 sizeof(fc_host_supported_fc4s(fnic->host)));
669 fc_host_supported_fc4s(fnic->host)[2] = 1;
670 fc_host_supported_fc4s(fnic->host)[7] = 1;
671 fc_host_supported_speeds(fnic->host) = 0;
672 fc_host_supported_speeds(fnic->host) |= FC_PORTSPEED_8GBIT;
673
674 dev_info(&fnic->pdev->dev, "shost_data: 0x%p\n", fnic->host->shost_data);
675 if (fnic->host->shost_data != NULL) {
676 if (fnic_tgt_id_binding == 0) {
677 dev_info(&fnic->pdev->dev, "Setting target binding to NONE\n");
678 fc_host_tgtid_bind_type(fnic->host) = FC_TGTID_BIND_NONE;
679 } else {
680 dev_info(&fnic->pdev->dev, "Setting target binding to WWPN\n");
681 fc_host_tgtid_bind_type(fnic->host) = FC_TGTID_BIND_BY_WWPN;
682 }
683 }
684
685 fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
686 if (!fnic->io_req_pool) {
687 scsi_remove_host(fnic->host);
688 return -ENOMEM;
689 }
690
691 return 0;
692 }
693
fnic_nvme_drv_init(struct fnic * fnic)694 static int fnic_nvme_drv_init(struct fnic *fnic)
695 {
696 int ret;
697 int hwq;
698
699 fnic->fnic_max_tag_id = NVFNIC_FCPIO_TAG_POOL_SZ;
700
701 for (hwq = 0; hwq < fnic->wq_copy_count; hwq++) {
702 fnic->sw_copy_wq[hwq].ioreq_table_size = fnic->fnic_max_tag_id;
703 fnic->sw_copy_wq[hwq].io_req_table =
704 kzalloc((fnic->sw_copy_wq[hwq].ioreq_table_size + 1) *
705 sizeof(struct fnic_io_req *), GFP_KERNEL);
706
707 if (!fnic->sw_copy_wq[hwq].io_req_table) {
708 fnic_free_ioreq_tables_mq(fnic);
709 return -ENOMEM;
710 }
711 }
712
713 dev_info(&fnic->pdev->dev, "fnic copy wqs: %d, Q0 ioreq table size: %d\n",
714 fnic->wq_copy_count, fnic->sw_copy_wq[0].ioreq_table_size);
715
716 if (sbitmap_init_node(&fnic->nvfnic_tag_map, NVFNIC_FCPIO_TAG_POOL_SZ,
717 -1, GFP_KERNEL, NUMA_NO_NODE, false, true)) {
718 dev_err(&fnic->pdev->dev,
719 "Unable to allocate tag pool\n");
720 ret = -ENOMEM;
721 goto out_free_ioreq_tables;
722 }
723
724 fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
725 if (!fnic->io_req_pool) {
726 ret = -ENOMEM;
727 goto out_free_sbitmap;
728 }
729
730 init_llist_head(&fnic->nvme_io_event_llist);
731 INIT_WORK(&fnic->nvme_io_cmpl_work, nvfnic_nvme_iodone_work);
732
733 ret = nvfnic_add_lport(fnic);
734 if (ret)
735 goto out_free_req_pool;
736
737 return 0;
738 out_free_req_pool:
739 mempool_destroy(fnic->io_req_pool);
740 out_free_sbitmap:
741 sbitmap_free(&fnic->nvfnic_tag_map);
742 out_free_ioreq_tables:
743 fnic_free_ioreq_tables_mq(fnic);
744 return ret;
745 }
746
fnic_mq_init_queue_map(struct fnic * fnic,struct blk_mq_queue_map * qmap)747 static void fnic_mq_init_queue_map(struct fnic *fnic,
748 struct blk_mq_queue_map *qmap)
749 {
750 unsigned int cpu;
751
752 for_each_possible_cpu(cpu)
753 qmap->mq_map[cpu] = 0;
754 }
755
fnic_mq_map_queues_cpus(struct Scsi_Host * host)756 void fnic_mq_map_queues_cpus(struct Scsi_Host *host)
757 {
758 const struct cpumask *mask;
759 unsigned int queue, cpu;
760 struct fnic *fnic = *((struct fnic **) shost_priv(host));
761 struct pci_dev *l_pdev = fnic->pdev;
762 int intr_mode = fnic->config.intr_mode;
763 struct blk_mq_queue_map *qmap = &host->tag_set.map[HCTX_TYPE_DEFAULT];
764
765 if (intr_mode == VNIC_DEV_INTR_MODE_MSI || intr_mode == VNIC_DEV_INTR_MODE_INTX) {
766 FNIC_MAIN_DBG(KERN_ERR, fnic,
767 "intr_mode is not msix\n");
768 return;
769 }
770
771 FNIC_MAIN_DBG(KERN_INFO, fnic,
772 "qmap->nr_queues: %d\n", qmap->nr_queues);
773
774 if (l_pdev == NULL) {
775 FNIC_MAIN_DBG(KERN_ERR, fnic,
776 "l_pdev is null\n");
777 return;
778 }
779
780 fnic_mq_init_queue_map(fnic, qmap);
781
782 /*
783 * Setup CPU to Queue mapping for all managed MSI-X IRQs.
784 * Q0 is driver critical and non-managed, hence start from Q1.
785 */
786 for (queue = 1; queue < qmap->nr_queues; queue++) {
787 int irq_num = pci_irq_vector(fnic->pdev,
788 queue + FNIC_PCI_OFFSET);
789
790 if (irq_num < 0)
791 continue;
792
793 mask = pci_irq_get_affinity(fnic->pdev,
794 queue + FNIC_PCI_OFFSET);
795 if (!mask) {
796 shost_printk(KERN_ERR, host,
797 "failed to get irq_affinity map for queue:%d\n", irq_num);
798 continue;
799 }
800 FNIC_MAIN_DBG(KERN_INFO, fnic,
801 "got irq_affinity map for %d:\n", irq_num);
802 for_each_cpu(cpu, mask) {
803 qmap->mq_map[cpu] = qmap->queue_offset + queue;
804 FNIC_MAIN_DBG(KERN_INFO, fnic,
805 "[Q%d] cpu:%d <=> irq:%d\n",
806 queue, cpu, irq_num);
807 }
808 }
809 }
810
fnic_probe(struct pci_dev * pdev,const struct pci_device_id * ent)811 static int fnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
812 {
813 struct Scsi_Host *host = NULL;
814 struct fnic *fnic;
815 mempool_t *pool;
816 struct fnic_iport_s *iport;
817 int err = 0;
818 int fnic_id = 0;
819 int i;
820 unsigned long flags;
821 char *desc, *subsys_desc;
822 int len;
823
824 /*
825 * Allocate fnic
826 */
827 fnic = kzalloc_obj(struct fnic);
828 if (!fnic) {
829 err = -ENOMEM;
830 goto err_out_fnic_alloc;
831 }
832
833 iport = &fnic->iport;
834
835 fnic_id = ida_alloc(&fnic_ida, GFP_KERNEL);
836 if (fnic_id < 0) {
837 dev_err(&pdev->dev, "Unable to alloc fnic ID\n");
838 err = fnic_id;
839 goto err_out_ida_alloc;
840 }
841
842 fnic->pdev = pdev;
843 fnic->fnic_num = fnic_id;
844
845 /* Find model name from PCIe subsys ID */
846 if (fnic_get_desc_by_devid(pdev, &desc, &subsys_desc) == 0) {
847 dev_info(&fnic->pdev->dev, "Model: %s\n", subsys_desc);
848
849 /* Update FDMI model */
850 fnic->subsys_desc_len = strlen(subsys_desc);
851 len = ARRAY_SIZE(fnic->subsys_desc);
852 if (fnic->subsys_desc_len > len)
853 fnic->subsys_desc_len = len;
854 memcpy(fnic->subsys_desc, subsys_desc, fnic->subsys_desc_len);
855 dev_info(&fnic->pdev->dev, "FDMI Model: %s\n", fnic->subsys_desc);
856 } else {
857 fnic->subsys_desc_len = 0;
858 dev_info(&fnic->pdev->dev, "Model: %s subsys_id: 0x%04x\n", "Unknown",
859 pdev->subsystem_device);
860 }
861
862 err = pci_enable_device(pdev);
863 if (err) {
864 dev_err(&fnic->pdev->dev, "Cannot enable PCI device, aborting.\n");
865 goto err_out_pci_enable_device;
866 }
867
868 err = pci_request_regions(pdev, DRV_NAME);
869 if (err) {
870 dev_err(&fnic->pdev->dev, "Cannot enable PCI resources, aborting\n");
871 goto err_out_pci_request_regions;
872 }
873
874 pci_set_master(pdev);
875
876 /* Query PCI controller on system for DMA addressing
877 * limitation for the device. Try 47-bit first, and
878 * fail to 32-bit. Cisco VIC supports 47 bits only.
879 */
880 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(47));
881 if (err) {
882 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
883 if (err) {
884 dev_err(&fnic->pdev->dev, "No usable DMA configuration "
885 "aborting\n");
886 goto err_out_set_dma_mask;
887 }
888 }
889
890 /* Map vNIC resources from BAR0 */
891 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
892 dev_err(&fnic->pdev->dev, "BAR0 not memory-map'able, aborting.\n");
893 err = -ENODEV;
894 goto err_out_map_bar;
895 }
896
897 fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
898 fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
899 fnic->bar0.len = pci_resource_len(pdev, 0);
900
901 if (!fnic->bar0.vaddr) {
902 dev_err(&fnic->pdev->dev, "Cannot memory-map BAR0 res hdr, "
903 "aborting.\n");
904 err = -ENODEV;
905 goto err_out_fnic_map_bar;
906 }
907
908 fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
909 if (!fnic->vdev) {
910 dev_err(&fnic->pdev->dev, "vNIC registration failed, "
911 "aborting.\n");
912 err = -ENODEV;
913 goto err_out_dev_register;
914 }
915
916 err = vnic_dev_cmd_init(fnic->vdev);
917 if (err) {
918 dev_err(&fnic->pdev->dev, "vnic_dev_cmd_init() returns %d, aborting\n",
919 err);
920 goto err_out_dev_cmd_init;
921 }
922
923 err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
924 vnic_dev_open_done, CMD_OPENF_RQ_ENABLE_THEN_POST);
925 if (err) {
926 dev_err(&fnic->pdev->dev, "vNIC dev open failed, aborting.\n");
927 goto err_out_dev_open;
928 }
929
930 err = vnic_dev_init(fnic->vdev, 0);
931 if (err) {
932 dev_err(&fnic->pdev->dev, "vNIC dev init failed, aborting.\n");
933 goto err_out_dev_init;
934 }
935
936 err = vnic_dev_mac_addr(fnic->vdev, iport->hwmac);
937 if (err) {
938 dev_err(&fnic->pdev->dev, "vNIC get MAC addr failed\n");
939 goto err_out_dev_mac_addr;
940 }
941 /* set data_src for point-to-point mode and to keep it non-zero */
942 memcpy(fnic->data_src_addr, iport->hwmac, ETH_ALEN);
943
944 /* Get vNIC configuration */
945 err = fnic_get_vnic_config(fnic);
946 if (err) {
947 dev_err(&fnic->pdev->dev, "Get vNIC configuration failed, "
948 "aborting.\n");
949 goto err_out_fnic_get_config;
950 }
951
952 switch (fnic->config.flags & FNIC_ROLE_CONFIG_MASK) {
953 case VFCF_FC_INITIATOR:
954 {
955 host =
956 scsi_host_alloc(&fnic_host_template,
957 sizeof(struct fnic *));
958 if (!host) {
959 dev_err(&fnic->pdev->dev, "Unable to allocate scsi host\n");
960 err = -ENOMEM;
961 goto err_out_scsi_host_alloc;
962 }
963 *((struct fnic **) shost_priv(host)) = fnic;
964
965 fnic->host = host;
966 fnic->role = FNIC_ROLE_FCP_INITIATOR;
967 dev_info(&fnic->pdev->dev, "fnic: %d is scsi initiator\n",
968 fnic->fnic_num);
969 }
970 break;
971 case VFCF_FC_TARGET:
972 dev_info(&fnic->pdev->dev,
973 "fnic: %d is scsi target\n",
974 fnic->fnic_num);
975 err = -EOPNOTSUPP;
976 goto err_out_fnic_role;
977 case VFCF_FC_NVME_INITIATOR:
978 fnic_nvmef_debugfs_init(fnic);
979 fnic->role = FNIC_ROLE_NVME_INITIATOR;
980 dev_info(&fnic->pdev->dev, "fnic: %d is NVME initiator\n",
981 fnic->fnic_num);
982 break;
983 case VFCF_FC_NVME_TARGET:
984 dev_info(&fnic->pdev->dev,
985 "fnic: %d is NVME target\n",
986 fnic->fnic_num);
987 err = -EOPNOTSUPP;
988 goto err_out_fnic_role;
989 default:
990 dev_info(&fnic->pdev->dev,
991 "fnic: %d has no role defined (0x%x)\n",
992 fnic->fnic_num, fnic->config.flags & FNIC_ROLE_CONFIG_MASK);
993 err = -EINVAL;
994 goto err_out_fnic_role;
995 }
996
997 /* Setup PCI resources */
998 pci_set_drvdata(pdev, fnic);
999
1000 fnic_get_res_counts(fnic);
1001
1002 err = fnic_set_intr_mode(fnic);
1003 if (err) {
1004 dev_err(&fnic->pdev->dev, "Failed to set intr mode, "
1005 "aborting.\n");
1006 goto err_out_fnic_set_intr_mode;
1007 }
1008
1009 err = fnic_alloc_vnic_resources(fnic);
1010 if (err) {
1011 dev_err(&fnic->pdev->dev, "Failed to alloc vNIC resources, "
1012 "aborting.\n");
1013 goto err_out_fnic_alloc_vnic_res;
1014 }
1015 dev_info(&fnic->pdev->dev, "fnic copy wqs: %d, Q0 ioreq table size: %d\n",
1016 fnic->wq_copy_count, fnic->sw_copy_wq[0].ioreq_table_size);
1017
1018 /* initialize all fnic locks */
1019 spin_lock_init(&fnic->fnic_lock);
1020
1021 for (i = 0; i < FNIC_WQ_MAX; i++)
1022 spin_lock_init(&fnic->wq_lock[i]);
1023
1024 for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
1025 spin_lock_init(&fnic->wq_copy_lock[i]);
1026 fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
1027 fnic->fw_ack_recd[i] = 0;
1028 fnic->fw_ack_index[i] = -1;
1029 }
1030
1031 pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
1032 if (!pool) {
1033 err = -ENOMEM;
1034 goto err_out_free_resources;
1035 }
1036 fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
1037
1038 pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
1039 if (!pool) {
1040 err = -ENOMEM;
1041 goto err_out_free_dflt_pool;
1042 }
1043 fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
1044
1045 pool = mempool_create_slab_pool(FDLS_MIN_FRAMES, fdls_frame_cache);
1046 if (!pool) {
1047 err = -ENOMEM;
1048 goto err_out_fdls_frame_pool;
1049 }
1050 fnic->frame_pool = pool;
1051
1052 pool = mempool_create_slab_pool(FDLS_MIN_FRAME_ELEM,
1053 fdls_frame_elem_cache);
1054 if (!pool) {
1055 err = -ENOMEM;
1056 goto err_out_fdls_frame_elem_pool;
1057 }
1058 fnic->frame_elem_pool = pool;
1059
1060 pool = mempool_create_slab_pool(FDLS_MIN_FRAMES,
1061 fdls_frame_recv_cache);
1062 if (!pool) {
1063 err = -ENOMEM;
1064 goto err_out_fdls_frame_recv_pool;
1065 }
1066 fnic->frame_recv_pool = pool;
1067
1068 /* setup vlan config, hw inserts vlan header */
1069 fnic->vlan_hw_insert = 1;
1070 fnic->vlan_id = 0;
1071
1072 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
1073 dev_info(&fnic->pdev->dev, "firmware supports FIP\n");
1074 /* enable directed and multicast */
1075 vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
1076 vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
1077 vnic_dev_add_addr(fnic->vdev, iport->hwmac);
1078 spin_lock_init(&fnic->vlans_lock);
1079 INIT_WORK(&fnic->fip_frame_work, fnic_handle_fip_frame);
1080 INIT_LIST_HEAD(&fnic->fip_frame_queue);
1081 INIT_LIST_HEAD(&fnic->vlan_list);
1082 timer_setup(&fnic->retry_fip_timer, fnic_handle_fip_timer, 0);
1083 timer_setup(&fnic->fcs_ka_timer, fnic_handle_fcs_ka_timer, 0);
1084 timer_setup(&fnic->enode_ka_timer, fnic_handle_enode_ka_timer, 0);
1085 timer_setup(&fnic->vn_ka_timer, fnic_handle_vn_ka_timer, 0);
1086 fnic->set_vlan = fnic_set_vlan;
1087 } else {
1088 dev_info(&fnic->pdev->dev, "firmware uses non-FIP mode\n");
1089 }
1090 fnic->state = FNIC_IN_FC_MODE;
1091
1092 atomic_set(&fnic->in_flight, 0);
1093 fnic->state_flags = FNIC_FLAGS_NONE;
1094
1095 /* Enable hardware stripping of vlan header on ingress */
1096 fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
1097
1098 /* Setup notification buffer area */
1099 err = fnic_notify_set(fnic);
1100 if (err) {
1101 dev_err(&fnic->pdev->dev, "Failed to alloc notify buffer, aborting.\n");
1102 goto err_out_fnic_notify_set;
1103 }
1104
1105 /* Setup notify timer when using MSI interrupts */
1106 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
1107 timer_setup(&fnic->notify_timer, fnic_notify_timer, 0);
1108
1109 /* allocate RQ buffers and post them to RQ*/
1110 for (i = 0; i < fnic->rq_count; i++) {
1111 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
1112 if (err) {
1113 dev_err(&fnic->pdev->dev, "fnic_alloc_rq_frame can't alloc "
1114 "frame\n");
1115 goto err_out_alloc_rq_buf;
1116 }
1117 }
1118
1119 init_completion(&fnic->reset_completion_wait);
1120
1121 /* Start local port initialization */
1122 iport->max_flogi_retries = fnic->config.flogi_retries;
1123 iport->max_plogi_retries = fnic->config.plogi_retries;
1124 iport->plogi_timeout = fnic->config.plogi_timeout;
1125 if (IS_FNIC_FCP_INITIATOR(fnic)) {
1126 iport->service_params = (FNIC_FCP_SP_INITIATOR |
1127 FNIC_FCP_SP_RD_XRDY_DIS | FNIC_FCP_SP_CONF_CMPL);
1128 if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
1129 iport->service_params |= FNIC_FCP_SP_RETRY;
1130 } else if (IS_FNIC_NVME_INITIATOR(fnic)) {
1131 iport->service_params = (FNIC_NVME_SP_INITIATOR);
1132 if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
1133 iport->service_params |= FNIC_NVME_SP_SLER;
1134 }
1135
1136 iport->boot_time = jiffies;
1137 iport->e_d_tov = fnic->config.ed_tov;
1138 iport->r_a_tov = fnic->config.ra_tov;
1139 iport->link_supported_speeds = FNIC_PORTSPEED_10GBIT;
1140 iport->wwpn = fnic->config.port_wwn;
1141 iport->wwnn = fnic->config.node_wwn;
1142
1143 iport->max_payload_size = fnic->config.maxdatafieldsize;
1144
1145 if ((iport->max_payload_size < FNIC_MIN_DATA_FIELD_SIZE) ||
1146 (iport->max_payload_size > FNIC_FC_MAX_PAYLOAD_LEN) ||
1147 ((iport->max_payload_size % 4) != 0)) {
1148 iport->max_payload_size = FNIC_FC_MAX_PAYLOAD_LEN;
1149 }
1150
1151 iport->flags |= FNIC_FIRST_LINK_UP;
1152
1153 timer_setup(&(iport->fabric.retry_timer), fdls_fabric_timer_callback,
1154 0);
1155
1156 fnic->stats_reset_time = jiffies;
1157
1158 INIT_WORK(&fnic->link_work, fnic_handle_link);
1159 INIT_WORK(&fnic->frame_work, fnic_handle_frame);
1160 INIT_WORK(&fnic->tport_work, fnic_tport_event_handler);
1161 INIT_WORK(&fnic->flush_work, fnic_flush_tx);
1162
1163 INIT_LIST_HEAD(&fnic->frame_queue);
1164 INIT_LIST_HEAD(&fnic->tx_queue);
1165 INIT_LIST_HEAD(&fnic->tport_event_list);
1166 init_llist_head(&fnic->nvme_io_event_llist);
1167
1168 INIT_DELAYED_WORK(&iport->oxid_pool.schedule_oxid_free_retry,
1169 fdls_schedule_oxid_free_retry_work);
1170
1171 /* Initialize the oxid reclaim list and work struct */
1172 INIT_LIST_HEAD(&iport->oxid_pool.oxid_reclaim_list);
1173 INIT_DELAYED_WORK(&iport->oxid_pool.oxid_reclaim_work, fdls_reclaim_oxid_handler);
1174
1175 /* Enable all queues */
1176 for (i = 0; i < fnic->raw_wq_count; i++)
1177 vnic_wq_enable(&fnic->wq[i]);
1178 for (i = 0; i < fnic->rq_count; i++) {
1179 if (!ioread32(&fnic->rq[i].ctrl->enable))
1180 vnic_rq_enable(&fnic->rq[i]);
1181 }
1182 for (i = 0; i < fnic->wq_copy_count; i++)
1183 vnic_wq_copy_enable(&fnic->hw_copy_wq[i]);
1184
1185 vnic_dev_enable(fnic->vdev);
1186
1187 err = fnic_request_intr(fnic);
1188 if (err) {
1189 dev_err(&fnic->pdev->dev, "Unable to request irq.\n");
1190 goto err_out_fnic_request_intr;
1191 }
1192
1193 fnic_notify_timer_start(fnic);
1194
1195 fnic_fdls_init(fnic, (fnic->config.flags & VFCF_FIP_CAPABLE));
1196
1197 if (IS_FNIC_FCP_INITIATOR(fnic)) {
1198 err = fnic_scsi_drv_init(fnic);
1199 if (err)
1200 goto err_out_scsi_drv_init;
1201 } else if (IS_FNIC_NVME_INITIATOR(fnic)) {
1202 err = fnic_nvme_drv_init(fnic);
1203 if (err)
1204 goto err_out_nvme_drv_init;
1205 }
1206
1207 err = fnic_stats_debugfs_init(fnic);
1208 if (err) {
1209 dev_err(&fnic->pdev->dev, "Failed to initialize debugfs for stats\n");
1210 goto err_out_free_stats_debugfs;
1211 }
1212
1213 for (i = 0; i < fnic->intr_count; i++)
1214 vnic_intr_unmask(&fnic->intr[i]);
1215
1216 spin_lock_irqsave(&fnic_list_lock, flags);
1217 list_add_tail(&fnic->list, &fnic_list);
1218 spin_unlock_irqrestore(&fnic_list_lock, flags);
1219
1220 return 0;
1221
1222 err_out_free_stats_debugfs:
1223 fnic_stats_debugfs_remove(fnic);
1224 fnic_free_ioreq_tables_mq(fnic);
1225 if (IS_FNIC_FCP_INITIATOR(fnic))
1226 scsi_remove_host(fnic->host);
1227 err_out_nvme_drv_init:
1228 err_out_scsi_drv_init:
1229 fnic_free_intr(fnic);
1230 err_out_fnic_request_intr:
1231 err_out_alloc_rq_buf:
1232 for (i = 0; i < fnic->rq_count; i++) {
1233 if (ioread32(&fnic->rq[i].ctrl->enable))
1234 vnic_rq_disable(&fnic->rq[i]);
1235 vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
1236 }
1237 vnic_dev_notify_unset(fnic->vdev);
1238 err_out_fnic_notify_set:
1239 mempool_destroy(fnic->frame_recv_pool);
1240 err_out_fdls_frame_recv_pool:
1241 mempool_destroy(fnic->frame_elem_pool);
1242 err_out_fdls_frame_elem_pool:
1243 mempool_destroy(fnic->frame_pool);
1244 err_out_fdls_frame_pool:
1245 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
1246 err_out_free_dflt_pool:
1247 mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
1248 err_out_free_resources:
1249 fnic_free_vnic_resources(fnic);
1250 err_out_fnic_alloc_vnic_res:
1251 fnic_clear_intr_mode(fnic);
1252 err_out_fnic_set_intr_mode:
1253 if (IS_FNIC_NVME_INITIATOR(fnic))
1254 fnic_nvmef_debugfs_remove(fnic);
1255 if (IS_FNIC_FCP_INITIATOR(fnic))
1256 scsi_host_put(fnic->host);
1257 err_out_fnic_role:
1258 err_out_scsi_host_alloc:
1259 err_out_fnic_get_config:
1260 err_out_dev_mac_addr:
1261 err_out_dev_init:
1262 vnic_dev_close(fnic->vdev);
1263 err_out_dev_open:
1264 err_out_dev_cmd_init:
1265 vnic_dev_unregister(fnic->vdev);
1266 err_out_dev_register:
1267 fnic_iounmap(fnic);
1268 err_out_fnic_map_bar:
1269 err_out_map_bar:
1270 err_out_set_dma_mask:
1271 pci_release_regions(pdev);
1272 err_out_pci_request_regions:
1273 pci_disable_device(pdev);
1274 err_out_pci_enable_device:
1275 ida_free(&fnic_ida, fnic->fnic_num);
1276 err_out_ida_alloc:
1277 kfree(fnic);
1278 err_out_fnic_alloc:
1279 return err;
1280 }
1281
fnic_remove(struct pci_dev * pdev)1282 static void fnic_remove(struct pci_dev *pdev)
1283 {
1284 struct fnic *fnic = pci_get_drvdata(pdev);
1285 unsigned long flags;
1286
1287 /*
1288 * Sometimes when probe() fails and do not exit with an error code,
1289 * remove() gets called with 'drvdata' not set. Avoid a crash by
1290 * adding a defensive check.
1291 */
1292 if (!fnic)
1293 return;
1294
1295 spin_lock_irqsave(&fnic->fnic_lock, flags);
1296 fnic->stop_rx_link_events = 1;
1297 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1298
1299 /*
1300 * Flush the fnic event queue. After this call, there should
1301 * be no event queued for this fnic device in the workqueue
1302 */
1303 flush_workqueue(fnic_event_queue);
1304
1305 if (IS_FNIC_FCP_INITIATOR(fnic))
1306 fnic_scsi_unload(fnic);
1307 else if (IS_FNIC_NVME_INITIATOR(fnic))
1308 nvfnic_nvme_unload(fnic);
1309
1310 if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
1311 timer_delete_sync(&fnic->notify_timer);
1312
1313 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
1314 timer_delete_sync(&fnic->retry_fip_timer);
1315 timer_delete_sync(&fnic->fcs_ka_timer);
1316 timer_delete_sync(&fnic->enode_ka_timer);
1317 timer_delete_sync(&fnic->vn_ka_timer);
1318
1319 fnic_fcoe_reset_vlans(fnic);
1320 }
1321
1322 if ((fnic_fdmi_support == 1) && (fnic->iport.fabric.fdmi_pending > 0))
1323 timer_delete_sync(&fnic->iport.fabric.fdmi_timer);
1324
1325 fnic_nvmef_debugfs_remove(fnic);
1326 fnic_stats_debugfs_remove(fnic);
1327
1328 /*
1329 * This stops the fnic device, masks all interrupts. Completed
1330 * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
1331 * cleaned up
1332 */
1333 fnic_cleanup(fnic);
1334
1335 if (IS_FNIC_NVME_INITIATOR(fnic)) {
1336 sbitmap_free(&fnic->nvfnic_tag_map);
1337 fnic_free_ioreq_tables_mq(fnic);
1338 }
1339
1340 spin_lock_irqsave(&fnic_list_lock, flags);
1341 list_del(&fnic->list);
1342 spin_unlock_irqrestore(&fnic_list_lock, flags);
1343
1344 fnic_free_rxq(fnic);
1345 fnic_free_txq(fnic);
1346
1347 vnic_dev_notify_unset(fnic->vdev);
1348 fnic_free_intr(fnic);
1349 fnic_free_vnic_resources(fnic);
1350 fnic_clear_intr_mode(fnic);
1351 vnic_dev_close(fnic->vdev);
1352 vnic_dev_unregister(fnic->vdev);
1353 fnic_iounmap(fnic);
1354 pci_release_regions(pdev);
1355 pci_disable_device(pdev);
1356 pci_set_drvdata(pdev, NULL);
1357 ida_free(&fnic_ida, fnic->fnic_num);
1358 if (IS_FNIC_FCP_INITIATOR(fnic)) {
1359 fnic_scsi_unload_cleanup(fnic);
1360 scsi_host_put(fnic->host);
1361 }
1362 kfree(fnic);
1363 }
1364
1365 static struct pci_driver fnic_driver = {
1366 .name = DRV_NAME,
1367 .id_table = fnic_id_table,
1368 .probe = fnic_probe,
1369 .remove = fnic_remove,
1370 };
1371
fnic_init_module(void)1372 static int __init fnic_init_module(void)
1373 {
1374 size_t len;
1375 int err = 0;
1376
1377 printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
1378
1379 /* Create debugfs entries for fnic */
1380 err = fnic_debugfs_init();
1381 if (err < 0) {
1382 printk(KERN_ERR PFX "Failed to create fnic directory "
1383 "for tracing and stats logging\n");
1384 fnic_debugfs_terminate();
1385 }
1386
1387 /* Allocate memory for trace buffer */
1388 err = fnic_trace_buf_init();
1389 if (err < 0) {
1390 printk(KERN_ERR PFX
1391 "Trace buffer initialization Failed. "
1392 "Fnic Tracing utility is disabled\n");
1393 fnic_trace_free();
1394 }
1395
1396 /* Allocate memory for fc trace buffer */
1397 err = fnic_fc_trace_init();
1398 if (err < 0) {
1399 printk(KERN_ERR PFX "FC trace buffer initialization Failed "
1400 "FC frame tracing utility is disabled\n");
1401 fnic_fc_trace_free();
1402 }
1403
1404 /* Create a cache for allocation of default size sgls */
1405 len = sizeof(struct fnic_dflt_sgl_list);
1406 fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
1407 ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
1408 SLAB_HWCACHE_ALIGN,
1409 NULL);
1410 if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
1411 printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
1412 err = -ENOMEM;
1413 goto err_create_fnic_sgl_slab_dflt;
1414 }
1415
1416 /* Create a cache for allocation of max size sgls*/
1417 len = sizeof(struct fnic_sgl_list);
1418 fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
1419 ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
1420 SLAB_HWCACHE_ALIGN,
1421 NULL);
1422 if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
1423 printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
1424 err = -ENOMEM;
1425 goto err_create_fnic_sgl_slab_max;
1426 }
1427
1428 /* Create a cache of io_req structs for use via mempool */
1429 fnic_io_req_cache = kmem_cache_create("fnic_io_req",
1430 sizeof(struct fnic_io_req),
1431 0, SLAB_HWCACHE_ALIGN, NULL);
1432 if (!fnic_io_req_cache) {
1433 printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
1434 err = -ENOMEM;
1435 goto err_create_fnic_ioreq_slab;
1436 }
1437
1438 fdls_frame_cache = kmem_cache_create("fdls_frames",
1439 FNIC_FCOE_FRAME_MAXSZ,
1440 0, SLAB_HWCACHE_ALIGN, NULL);
1441 if (!fdls_frame_cache) {
1442 pr_err("fnic fdls frame cache create failed\n");
1443 err = -ENOMEM;
1444 goto err_create_fdls_frame_cache;
1445 }
1446
1447 fdls_frame_elem_cache = kmem_cache_create("fdls_frame_elem",
1448 sizeof(struct fnic_frame_list),
1449 0, SLAB_HWCACHE_ALIGN, NULL);
1450 if (!fdls_frame_elem_cache) {
1451 pr_err("fnic fdls frame elem cache create failed\n");
1452 err = -ENOMEM;
1453 goto err_create_fdls_frame_cache_elem;
1454 }
1455
1456 fdls_frame_recv_cache = kmem_cache_create("fdls_frame_recv",
1457 FNIC_FRAME_HT_ROOM,
1458 0, SLAB_HWCACHE_ALIGN, NULL);
1459 if (!fdls_frame_recv_cache) {
1460 pr_err("fnic fdls frame recv cach create failed\n");
1461 err = -ENOMEM;
1462 goto err_create_fdls_frame_recv_cache;
1463 }
1464
1465 fnic_event_queue =
1466 alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, "fnic_event_wq");
1467 if (!fnic_event_queue) {
1468 printk(KERN_ERR PFX "fnic work queue create failed\n");
1469 err = -ENOMEM;
1470 goto err_create_fnic_workq;
1471 }
1472
1473 fnic_fip_queue =
1474 alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, "fnic_fip_q");
1475 if (!fnic_fip_queue) {
1476 printk(KERN_ERR PFX "fnic FIP work queue create failed\n");
1477 err = -ENOMEM;
1478 goto err_create_fip_workq;
1479 }
1480
1481 fnic_cmpl_queue =
1482 alloc_workqueue("fnic_cmpl_wq",
1483 WQ_PERCPU | WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1484 if (!fnic_cmpl_queue) {
1485 pr_err("fnic completion work queue create failed\n");
1486 err = -ENOMEM;
1487 goto err_create_cmpl_workq;
1488 }
1489
1490 if (pc_rscn_handling_feature_flag == PC_RSCN_HANDLING_FEATURE_ON) {
1491 reset_fnic_work_queue =
1492 create_singlethread_workqueue("reset_fnic_work_queue");
1493 if (!reset_fnic_work_queue) {
1494 pr_err("reset fnic work queue create failed\n");
1495 err = -ENOMEM;
1496 goto err_create_reset_fnic_workq;
1497 }
1498 spin_lock_init(&reset_fnic_list_lock);
1499 INIT_LIST_HEAD(&reset_fnic_list);
1500 INIT_WORK(&reset_fnic_work, fnic_reset_work_handler);
1501 }
1502
1503 fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
1504 if (!fnic_fc_transport) {
1505 printk(KERN_ERR PFX "fc_attach_transport error\n");
1506 err = -ENOMEM;
1507 goto err_fc_transport;
1508 }
1509
1510 /* register the driver with PCI system */
1511 err = pci_register_driver(&fnic_driver);
1512 if (err < 0) {
1513 printk(KERN_ERR PFX "pci register error\n");
1514 goto err_pci_register;
1515 }
1516 return err;
1517
1518 err_pci_register:
1519 fc_release_transport(fnic_fc_transport);
1520 err_fc_transport:
1521 destroy_workqueue(fnic_cmpl_queue);
1522 err_create_cmpl_workq:
1523 destroy_workqueue(fnic_fip_queue);
1524 err_create_fip_workq:
1525 if (pc_rscn_handling_feature_flag == PC_RSCN_HANDLING_FEATURE_ON)
1526 destroy_workqueue(reset_fnic_work_queue);
1527 err_create_reset_fnic_workq:
1528 kmem_cache_destroy(fdls_frame_recv_cache);
1529 err_create_fdls_frame_recv_cache:
1530 destroy_workqueue(fnic_event_queue);
1531 err_create_fnic_workq:
1532 kmem_cache_destroy(fdls_frame_elem_cache);
1533 err_create_fdls_frame_cache_elem:
1534 kmem_cache_destroy(fdls_frame_cache);
1535 err_create_fdls_frame_cache:
1536 kmem_cache_destroy(fnic_io_req_cache);
1537 err_create_fnic_ioreq_slab:
1538 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
1539 err_create_fnic_sgl_slab_max:
1540 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
1541 err_create_fnic_sgl_slab_dflt:
1542 fnic_trace_free();
1543 fnic_fc_trace_free();
1544 fnic_debugfs_terminate();
1545 return err;
1546 }
1547
fnic_cleanup_module(void)1548 static void __exit fnic_cleanup_module(void)
1549 {
1550 pci_unregister_driver(&fnic_driver);
1551 destroy_workqueue(fnic_cmpl_queue);
1552 destroy_workqueue(fnic_event_queue);
1553
1554 if (pc_rscn_handling_feature_flag == PC_RSCN_HANDLING_FEATURE_ON)
1555 destroy_workqueue(reset_fnic_work_queue);
1556
1557 if (fnic_fip_queue)
1558 destroy_workqueue(fnic_fip_queue);
1559
1560 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
1561 kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
1562 kmem_cache_destroy(fnic_io_req_cache);
1563 kmem_cache_destroy(fdls_frame_cache);
1564 fc_release_transport(fnic_fc_transport);
1565 fnic_trace_free();
1566 fnic_fc_trace_free();
1567 fnic_debugfs_terminate();
1568 ida_destroy(&fnic_ida);
1569 }
1570
1571 module_init(fnic_init_module);
1572 module_exit(fnic_cleanup_module);
1573