1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2014 Cisco Systems, Inc. All rights reserved.
4 #include <linux/errno.h>
5 #include <linux/mempool.h>
7 #include <scsi/scsi_tcq.h>
14 /* snic target types */
15 static const char * const snic_tgt_type_str[] = {
16 [SNIC_TGT_DAS] = "DAS",
17 [SNIC_TGT_SAN] = "SAN",
20 static inline const char *
21 snic_tgt_type_to_str(int typ)
23 return ((typ > SNIC_TGT_NONE && typ <= SNIC_TGT_SAN) ?
24 snic_tgt_type_str[typ] : "Unknown");
27 static const char * const snic_tgt_state_str[] = {
28 [SNIC_TGT_STAT_INIT] = "INIT",
29 [SNIC_TGT_STAT_ONLINE] = "ONLINE",
30 [SNIC_TGT_STAT_OFFLINE] = "OFFLINE",
31 [SNIC_TGT_STAT_DEL] = "DELETION IN PROGRESS",
35 snic_tgt_state_to_str(int state)
37 return ((state >= SNIC_TGT_STAT_INIT && state <= SNIC_TGT_STAT_DEL) ?
38 snic_tgt_state_str[state] : "UNKNOWN");
42 * Initiate report_tgt req desc
45 snic_report_tgt_init(struct snic_host_req *req, u32 hid, u8 *buf, u32 len,
46 dma_addr_t rsp_buf_pa, ulong ctx)
48 struct snic_sg_desc *sgd = NULL;
51 snic_io_hdr_enc(&req->hdr, SNIC_REQ_REPORT_TGTS, 0, SCSI_NO_TAG, hid,
54 req->u.rpt_tgts.sg_cnt = cpu_to_le16(1);
55 sgd = req_to_sgl(req);
56 sgd[0].addr = cpu_to_le64(rsp_buf_pa);
57 sgd[0].len = cpu_to_le32(len);
59 req->u.rpt_tgts.sg_addr = cpu_to_le64((ulong)sgd);
63 * snic_queue_report_tgt_req: Queues report target request.
66 snic_queue_report_tgt_req(struct snic *snic)
68 struct snic_req_info *rqi = NULL;
69 u32 ntgts, buf_len = 0;
74 rqi = snic_req_init(snic, 1);
80 if (snic->fwinfo.max_tgts)
81 ntgts = min_t(u32, snic->fwinfo.max_tgts, snic->shost->max_id);
83 ntgts = snic->shost->max_id;
85 /* Allocate Response Buffer */
86 SNIC_BUG_ON(ntgts == 0);
87 buf_len = ntgts * sizeof(struct snic_tgt_id) + SNIC_SG_DESC_ALIGN;
89 buf = kzalloc(buf_len, GFP_KERNEL);
91 snic_req_free(snic, rqi);
92 SNIC_HOST_ERR(snic->shost, "Resp Buf Alloc Failed.\n");
98 SNIC_BUG_ON((((unsigned long)buf) % SNIC_SG_DESC_ALIGN) != 0);
100 pa = dma_map_single(&snic->pdev->dev, buf, buf_len, DMA_FROM_DEVICE);
101 if (dma_mapping_error(&snic->pdev->dev, pa)) {
102 SNIC_HOST_ERR(snic->shost,
103 "Rpt-tgt rspbuf %p: PCI DMA Mapping Failed\n",
106 snic_req_free(snic, rqi);
113 SNIC_BUG_ON(pa == 0);
114 rqi->sge_va = (ulong) buf;
116 snic_report_tgt_init(rqi->req,
123 snic_handle_untagged_req(snic, rqi);
125 ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);
127 dma_unmap_single(&snic->pdev->dev, pa, buf_len,
131 snic_release_untagged_req(snic, rqi);
132 SNIC_HOST_ERR(snic->shost, "Queuing Report Tgts Failed.\n");
137 SNIC_DISC_DBG(snic->shost, "Report Targets Issued.\n");
142 SNIC_HOST_ERR(snic->shost,
143 "Queuing Report Targets Failed, err = %d\n",
146 } /* end of snic_queue_report_tgt_req */
150 snic_scsi_scan_tgt(struct work_struct *work)
152 struct snic_tgt *tgt = container_of(work, struct snic_tgt, scan_work);
153 struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
156 SNIC_HOST_INFO(shost, "Scanning Target id 0x%x\n", tgt->id);
157 scsi_scan_target(&tgt->dev,
163 spin_lock_irqsave(shost->host_lock, flags);
164 tgt->flags &= ~SNIC_TGT_SCAN_PENDING;
165 spin_unlock_irqrestore(shost->host_lock, flags);
166 } /* end of snic_scsi_scan_tgt */
171 static struct snic_tgt *
172 snic_tgt_lookup(struct snic *snic, struct snic_tgt_id *tgtid)
174 struct list_head *cur, *nxt;
175 struct snic_tgt *tgt = NULL;
177 list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {
178 tgt = list_entry(cur, struct snic_tgt, list);
179 if (tgt->id == le32_to_cpu(tgtid->tgt_id))
185 } /* end of snic_tgt_lookup */
188 * snic_tgt_dev_release : Called on dropping last ref for snic_tgt object
191 snic_tgt_dev_release(struct device *dev)
193 struct snic_tgt *tgt = dev_to_tgt(dev);
195 SNIC_HOST_INFO(snic_tgt_to_shost(tgt),
196 "Target Device ID %d (%s) Permanently Deleted.\n",
200 SNIC_BUG_ON(!list_empty(&tgt->list));
205 * snic_tgt_del : work function to delete snic_tgt
208 snic_tgt_del(struct work_struct *work)
210 struct snic_tgt *tgt = container_of(work, struct snic_tgt, del_work);
211 struct Scsi_Host *shost = snic_tgt_to_shost(tgt);
213 if (tgt->flags & SNIC_TGT_SCAN_PENDING)
214 scsi_flush_work(shost);
216 /* Block IOs on child devices, stops new IOs */
217 scsi_target_block(&tgt->dev);
220 snic_tgt_scsi_abort_io(tgt);
222 /* Unblock IOs now, to flush if there are any. */
223 scsi_target_unblock(&tgt->dev, SDEV_TRANSPORT_OFFLINE);
225 /* Delete SCSI Target and sdevs */
226 scsi_remove_target(&tgt->dev); /* ?? */
227 device_del(&tgt->dev);
228 put_device(&tgt->dev);
229 } /* end of snic_tgt_del */
231 /* snic_tgt_create: checks for existence of snic_tgt, if it doesn't
234 static struct snic_tgt *
235 snic_tgt_create(struct snic *snic, struct snic_tgt_id *tgtid)
237 struct snic_tgt *tgt = NULL;
241 tgt = snic_tgt_lookup(snic, tgtid);
243 /* update the information if required */
247 tgt = kzalloc(sizeof(*tgt), GFP_KERNEL);
249 SNIC_HOST_ERR(snic->shost, "Failure to allocate snic_tgt.\n");
255 INIT_LIST_HEAD(&tgt->list);
256 tgt->id = le32_to_cpu(tgtid->tgt_id);
259 SNIC_BUG_ON(le16_to_cpu(tgtid->tgt_type) > SNIC_TGT_SAN);
260 tgt->tdata.typ = le16_to_cpu(tgtid->tgt_type);
263 * Plugging into SML Device Tree
265 tgt->tdata.disc_id = 0;
266 tgt->state = SNIC_TGT_STAT_INIT;
267 device_initialize(&tgt->dev);
268 tgt->dev.parent = get_device(&snic->shost->shost_gendev);
269 tgt->dev.release = snic_tgt_dev_release;
270 INIT_WORK(&tgt->scan_work, snic_scsi_scan_tgt);
271 INIT_WORK(&tgt->del_work, snic_tgt_del);
272 switch (tgt->tdata.typ) {
274 dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",
275 snic->shost->host_no, tgt->channel, tgt->id);
279 dev_set_name(&tgt->dev, "snic_san_tgt:%d:%d-%d",
280 snic->shost->host_no, tgt->channel, tgt->id);
284 SNIC_HOST_INFO(snic->shost, "Target type Unknown Detected.\n");
285 dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",
286 snic->shost->host_no, tgt->channel, tgt->id);
290 spin_lock_irqsave(snic->shost->host_lock, flags);
291 list_add_tail(&tgt->list, &snic->disc.tgt_list);
292 tgt->scsi_tgt_id = snic->disc.nxt_tgt_id++;
293 tgt->state = SNIC_TGT_STAT_ONLINE;
294 spin_unlock_irqrestore(snic->shost->host_lock, flags);
296 SNIC_HOST_INFO(snic->shost,
297 "Tgt %d, type = %s detected. Adding..\n",
298 tgt->id, snic_tgt_type_to_str(tgt->tdata.typ));
300 ret = device_add(&tgt->dev);
302 SNIC_HOST_ERR(snic->shost,
303 "Snic Tgt: device_add, with err = %d\n",
306 put_device(&snic->shost->shost_gendev);
313 SNIC_HOST_INFO(snic->shost, "Scanning %s.\n", dev_name(&tgt->dev));
315 scsi_queue_work(snic->shost, &tgt->scan_work);
318 } /* end of snic_tgt_create */
320 /* Handler for discovery */
322 snic_handle_tgt_disc(struct work_struct *work)
324 struct snic *snic = container_of(work, struct snic, tgt_work);
325 struct snic_tgt_id *tgtid = NULL;
326 struct snic_tgt *tgt = NULL;
330 spin_lock_irqsave(&snic->snic_lock, flags);
331 if (snic->in_remove) {
332 spin_unlock_irqrestore(&snic->snic_lock, flags);
333 kfree(snic->disc.rtgt_info);
337 spin_unlock_irqrestore(&snic->snic_lock, flags);
339 mutex_lock(&snic->disc.mutex);
340 /* Discover triggered during disc in progress */
341 if (snic->disc.req_cnt) {
342 snic->disc.state = SNIC_DISC_DONE;
343 snic->disc.req_cnt = 0;
344 mutex_unlock(&snic->disc.mutex);
345 kfree(snic->disc.rtgt_info);
346 snic->disc.rtgt_info = NULL;
348 SNIC_HOST_INFO(snic->shost, "tgt_disc: Discovery restart.\n");
349 /* Start Discovery Again */
350 snic_disc_start(snic);
355 tgtid = (struct snic_tgt_id *)snic->disc.rtgt_info;
357 SNIC_BUG_ON(snic->disc.rtgt_cnt == 0 || tgtid == NULL);
359 for (i = 0; i < snic->disc.rtgt_cnt; i++) {
360 tgt = snic_tgt_create(snic, &tgtid[i]);
362 int buf_sz = snic->disc.rtgt_cnt * sizeof(*tgtid);
364 SNIC_HOST_ERR(snic->shost, "Failed to create tgt.\n");
365 snic_hex_dump("rpt_tgt_rsp", (char *)tgtid, buf_sz);
370 snic->disc.rtgt_info = NULL;
371 snic->disc.state = SNIC_DISC_DONE;
372 mutex_unlock(&snic->disc.mutex);
374 SNIC_HOST_INFO(snic->shost, "Discovery Completed.\n");
377 } /* end of snic_handle_tgt_disc */
381 snic_report_tgt_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
385 u32 cmnd_id, hid, tgt_cnt = 0;
387 struct snic_req_info *rqi = NULL;
388 struct snic_tgt_id *tgtid;
391 snic_io_hdr_dec(&fwreq->hdr, &typ, &cmpl_stat, &cmnd_id, &hid, &ctx);
392 rqi = (struct snic_req_info *) ctx;
393 tgtid = (struct snic_tgt_id *) rqi->sge_va;
395 tgt_cnt = le32_to_cpu(fwreq->u.rpt_tgts_cmpl.tgt_cnt);
397 SNIC_HOST_ERR(snic->shost, "No Targets Found on this host.\n");
403 /* printing list of targets here */
404 SNIC_HOST_INFO(snic->shost, "Target Count = %d\n", tgt_cnt);
406 SNIC_BUG_ON(tgt_cnt > snic->fwinfo.max_tgts);
408 for (i = 0; i < tgt_cnt; i++)
409 SNIC_HOST_INFO(snic->shost,
411 le32_to_cpu(tgtid[i].tgt_id));
414 * Queue work for further processing,
415 * Response Buffer Memory is freed after creating targets
417 snic->disc.rtgt_cnt = tgt_cnt;
418 snic->disc.rtgt_info = (u8 *) tgtid;
419 queue_work(snic_glob->event_q, &snic->tgt_work);
423 /* Unmap Response Buffer */
424 snic_pci_unmap_rsp_buf(snic, rqi);
429 snic_release_untagged_req(snic, rqi);
432 } /* end of snic_report_tgt_cmpl_handler */
434 /* Discovery init fn */
436 snic_disc_init(struct snic_disc *disc)
438 INIT_LIST_HEAD(&disc->tgt_list);
439 mutex_init(&disc->mutex);
441 disc->nxt_tgt_id = 0;
442 disc->state = SNIC_DISC_INIT;
445 disc->rtgt_info = NULL;
447 } /* end of snic_disc_init */
449 /* Discovery, uninit fn */
451 snic_disc_term(struct snic *snic)
453 struct snic_disc *disc = &snic->disc;
455 mutex_lock(&disc->mutex);
458 SNIC_SCSI_DBG(snic->shost, "Terminating Discovery.\n");
460 mutex_unlock(&disc->mutex);
464 * snic_disc_start: Discovery Start ...
467 snic_disc_start(struct snic *snic)
469 struct snic_disc *disc = &snic->disc;
473 SNIC_SCSI_DBG(snic->shost, "Discovery Start.\n");
475 spin_lock_irqsave(&snic->snic_lock, flags);
476 if (snic->in_remove) {
477 spin_unlock_irqrestore(&snic->snic_lock, flags);
478 SNIC_ERR("snic driver removal in progress ...\n");
483 spin_unlock_irqrestore(&snic->snic_lock, flags);
485 mutex_lock(&disc->mutex);
486 if (disc->state == SNIC_DISC_PENDING) {
488 mutex_unlock(&disc->mutex);
492 disc->state = SNIC_DISC_PENDING;
493 mutex_unlock(&disc->mutex);
495 ret = snic_queue_report_tgt_req(snic);
497 SNIC_HOST_INFO(snic->shost, "Discovery Failed, err=%d.\n", ret);
500 } /* end of snic_disc_start */
506 snic_handle_disc(struct work_struct *work)
508 struct snic *snic = container_of(work, struct snic, disc_work);
511 SNIC_HOST_INFO(snic->shost, "disc_work: Discovery\n");
513 ret = snic_disc_start(snic);
518 SNIC_HOST_ERR(snic->shost,
519 "disc_work: Discovery Failed w/ err = %d\n",
521 } /* end of snic_disc_work */
524 * snic_tgt_del_all : cleanup all snic targets
525 * Called on unbinding the interface
528 snic_tgt_del_all(struct snic *snic)
530 struct snic_tgt *tgt = NULL;
531 struct list_head *cur, *nxt;
534 scsi_flush_work(snic->shost);
536 mutex_lock(&snic->disc.mutex);
537 spin_lock_irqsave(snic->shost->host_lock, flags);
539 list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {
540 tgt = list_entry(cur, struct snic_tgt, list);
541 tgt->state = SNIC_TGT_STAT_DEL;
542 list_del_init(&tgt->list);
543 SNIC_HOST_INFO(snic->shost, "Tgt %d q'ing for del\n", tgt->id);
544 queue_work(snic_glob->event_q, &tgt->del_work);
547 spin_unlock_irqrestore(snic->shost->host_lock, flags);
548 mutex_unlock(&snic->disc.mutex);
550 flush_workqueue(snic_glob->event_q);
551 } /* end of snic_tgt_del_all */