Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 // Copyright 2014 Cisco Systems, Inc.  All rights reserved.
0003 
0004 #include <linux/errno.h>
0005 #include <linux/mempool.h>
0006 
0007 #include <scsi/scsi_tcq.h>
0008 
0009 #include "snic_disc.h"
0010 #include "snic.h"
0011 #include "snic_io.h"
0012 
0013 
0014 /* snic target types */
0015 static const char * const snic_tgt_type_str[] = {
0016     [SNIC_TGT_DAS] = "DAS",
0017     [SNIC_TGT_SAN] = "SAN",
0018 };
0019 
0020 static inline const char *
0021 snic_tgt_type_to_str(int typ)
0022 {
0023     return ((typ > SNIC_TGT_NONE && typ <= SNIC_TGT_SAN) ?
0024          snic_tgt_type_str[typ] : "Unknown");
0025 }
0026 
0027 static const char * const snic_tgt_state_str[] = {
0028     [SNIC_TGT_STAT_INIT]    = "INIT",
0029     [SNIC_TGT_STAT_ONLINE]  = "ONLINE",
0030     [SNIC_TGT_STAT_OFFLINE] = "OFFLINE",
0031     [SNIC_TGT_STAT_DEL] = "DELETION IN PROGRESS",
0032 };
0033 
0034 const char *
0035 snic_tgt_state_to_str(int state)
0036 {
0037     return ((state >= SNIC_TGT_STAT_INIT && state <= SNIC_TGT_STAT_DEL) ?
0038         snic_tgt_state_str[state] : "UNKNOWN");
0039 }
0040 
0041 /*
0042  * Initiate report_tgt req desc
0043  */
0044 static void
0045 snic_report_tgt_init(struct snic_host_req *req, u32 hid, u8 *buf, u32 len,
0046              dma_addr_t rsp_buf_pa, ulong ctx)
0047 {
0048     struct snic_sg_desc *sgd = NULL;
0049 
0050 
0051     snic_io_hdr_enc(&req->hdr, SNIC_REQ_REPORT_TGTS, 0, SCSI_NO_TAG, hid,
0052             1, ctx);
0053 
0054     req->u.rpt_tgts.sg_cnt = cpu_to_le16(1);
0055     sgd = req_to_sgl(req);
0056     sgd[0].addr = cpu_to_le64(rsp_buf_pa);
0057     sgd[0].len = cpu_to_le32(len);
0058     sgd[0]._resvd = 0;
0059     req->u.rpt_tgts.sg_addr = cpu_to_le64((ulong)sgd);
0060 }
0061 
0062 /*
0063  * snic_queue_report_tgt_req: Queues report target request.
0064  */
0065 static int
0066 snic_queue_report_tgt_req(struct snic *snic)
0067 {
0068     struct snic_req_info *rqi = NULL;
0069     u32 ntgts, buf_len = 0;
0070     u8 *buf = NULL;
0071     dma_addr_t pa = 0;
0072     int ret = 0;
0073 
0074     rqi = snic_req_init(snic, 1);
0075     if (!rqi) {
0076         ret = -ENOMEM;
0077         goto error;
0078     }
0079 
0080     if (snic->fwinfo.max_tgts)
0081         ntgts = min_t(u32, snic->fwinfo.max_tgts, snic->shost->max_id);
0082     else
0083         ntgts = snic->shost->max_id;
0084 
0085     /* Allocate Response Buffer */
0086     SNIC_BUG_ON(ntgts == 0);
0087     buf_len = ntgts * sizeof(struct snic_tgt_id) + SNIC_SG_DESC_ALIGN;
0088 
0089     buf = kzalloc(buf_len, GFP_KERNEL);
0090     if (!buf) {
0091         snic_req_free(snic, rqi);
0092         SNIC_HOST_ERR(snic->shost, "Resp Buf Alloc Failed.\n");
0093 
0094         ret = -ENOMEM;
0095         goto error;
0096     }
0097 
0098     SNIC_BUG_ON((((unsigned long)buf) % SNIC_SG_DESC_ALIGN) != 0);
0099 
0100     pa = dma_map_single(&snic->pdev->dev, buf, buf_len, DMA_FROM_DEVICE);
0101     if (dma_mapping_error(&snic->pdev->dev, pa)) {
0102         SNIC_HOST_ERR(snic->shost,
0103                   "Rpt-tgt rspbuf %p: PCI DMA Mapping Failed\n",
0104                   buf);
0105         kfree(buf);
0106         snic_req_free(snic, rqi);
0107         ret = -EINVAL;
0108 
0109         goto error;
0110     }
0111 
0112 
0113     SNIC_BUG_ON(pa == 0);
0114     rqi->sge_va = (ulong) buf;
0115 
0116     snic_report_tgt_init(rqi->req,
0117                  snic->config.hid,
0118                  buf,
0119                  buf_len,
0120                  pa,
0121                  (ulong)rqi);
0122 
0123     snic_handle_untagged_req(snic, rqi);
0124 
0125     ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);
0126     if (ret) {
0127         dma_unmap_single(&snic->pdev->dev, pa, buf_len,
0128                  DMA_FROM_DEVICE);
0129         kfree(buf);
0130         rqi->sge_va = 0;
0131         snic_release_untagged_req(snic, rqi);
0132         SNIC_HOST_ERR(snic->shost, "Queuing Report Tgts Failed.\n");
0133 
0134         goto error;
0135     }
0136 
0137     SNIC_DISC_DBG(snic->shost, "Report Targets Issued.\n");
0138 
0139     return ret;
0140 
0141 error:
0142     SNIC_HOST_ERR(snic->shost,
0143               "Queuing Report Targets Failed, err = %d\n",
0144               ret);
0145     return ret;
0146 } /* end of snic_queue_report_tgt_req */
0147 
0148 /* call into SML */
0149 static void
0150 snic_scsi_scan_tgt(struct work_struct *work)
0151 {
0152     struct snic_tgt *tgt = container_of(work, struct snic_tgt, scan_work);
0153     struct Scsi_Host *shost = dev_to_shost(&tgt->dev);
0154     unsigned long flags;
0155 
0156     SNIC_HOST_INFO(shost, "Scanning Target id 0x%x\n", tgt->id);
0157     scsi_scan_target(&tgt->dev,
0158              tgt->channel,
0159              tgt->scsi_tgt_id,
0160              SCAN_WILD_CARD,
0161              SCSI_SCAN_RESCAN);
0162 
0163     spin_lock_irqsave(shost->host_lock, flags);
0164     tgt->flags &= ~SNIC_TGT_SCAN_PENDING;
0165     spin_unlock_irqrestore(shost->host_lock, flags);
0166 } /* end of snic_scsi_scan_tgt */
0167 
0168 /*
0169  * snic_tgt_lookup :
0170  */
0171 static struct snic_tgt *
0172 snic_tgt_lookup(struct snic *snic, struct snic_tgt_id *tgtid)
0173 {
0174     struct list_head *cur, *nxt;
0175     struct snic_tgt *tgt = NULL;
0176 
0177     list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {
0178         tgt = list_entry(cur, struct snic_tgt, list);
0179         if (tgt->id == le32_to_cpu(tgtid->tgt_id))
0180             return tgt;
0181         tgt = NULL;
0182     }
0183 
0184     return tgt;
0185 } /* end of snic_tgt_lookup */
0186 
0187 /*
0188  * snic_tgt_dev_release : Called on dropping last ref for snic_tgt object
0189  */
0190 void
0191 snic_tgt_dev_release(struct device *dev)
0192 {
0193     struct snic_tgt *tgt = dev_to_tgt(dev);
0194 
0195     SNIC_HOST_INFO(snic_tgt_to_shost(tgt),
0196                "Target Device ID %d (%s) Permanently Deleted.\n",
0197                tgt->id,
0198                dev_name(dev));
0199 
0200     SNIC_BUG_ON(!list_empty(&tgt->list));
0201     kfree(tgt);
0202 }
0203 
0204 /*
0205  * snic_tgt_del : work function to delete snic_tgt
0206  */
0207 static void
0208 snic_tgt_del(struct work_struct *work)
0209 {
0210     struct snic_tgt *tgt = container_of(work, struct snic_tgt, del_work);
0211     struct Scsi_Host *shost = snic_tgt_to_shost(tgt);
0212 
0213     if (tgt->flags & SNIC_TGT_SCAN_PENDING)
0214         scsi_flush_work(shost);
0215 
0216     /* Block IOs on child devices, stops new IOs */
0217     scsi_target_block(&tgt->dev);
0218 
0219     /* Cleanup IOs */
0220     snic_tgt_scsi_abort_io(tgt);
0221 
0222     /* Unblock IOs now, to flush if there are any. */
0223     scsi_target_unblock(&tgt->dev, SDEV_TRANSPORT_OFFLINE);
0224 
0225     /* Delete SCSI Target and sdevs */
0226     scsi_remove_target(&tgt->dev);  /* ?? */
0227     device_del(&tgt->dev);
0228     put_device(&tgt->dev);
0229 } /* end of snic_tgt_del */
0230 
0231 /* snic_tgt_create: checks for existence of snic_tgt, if it doesn't
0232  * it creates one.
0233  */
0234 static struct snic_tgt *
0235 snic_tgt_create(struct snic *snic, struct snic_tgt_id *tgtid)
0236 {
0237     struct snic_tgt *tgt = NULL;
0238     unsigned long flags;
0239     int ret;
0240 
0241     tgt = snic_tgt_lookup(snic, tgtid);
0242     if (tgt) {
0243         /* update the information if required */
0244         return tgt;
0245     }
0246 
0247     tgt = kzalloc(sizeof(*tgt), GFP_KERNEL);
0248     if (!tgt) {
0249         SNIC_HOST_ERR(snic->shost, "Failure to allocate snic_tgt.\n");
0250         ret = -ENOMEM;
0251 
0252         return tgt;
0253     }
0254 
0255     INIT_LIST_HEAD(&tgt->list);
0256     tgt->id = le32_to_cpu(tgtid->tgt_id);
0257     tgt->channel = 0;
0258 
0259     SNIC_BUG_ON(le16_to_cpu(tgtid->tgt_type) > SNIC_TGT_SAN);
0260     tgt->tdata.typ = le16_to_cpu(tgtid->tgt_type);
0261 
0262     /*
0263      * Plugging into SML Device Tree
0264      */
0265     tgt->tdata.disc_id = 0;
0266     tgt->state = SNIC_TGT_STAT_INIT;
0267     device_initialize(&tgt->dev);
0268     tgt->dev.parent = get_device(&snic->shost->shost_gendev);
0269     tgt->dev.release = snic_tgt_dev_release;
0270     INIT_WORK(&tgt->scan_work, snic_scsi_scan_tgt);
0271     INIT_WORK(&tgt->del_work, snic_tgt_del);
0272     switch (tgt->tdata.typ) {
0273     case SNIC_TGT_DAS:
0274         dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",
0275                  snic->shost->host_no, tgt->channel, tgt->id);
0276         break;
0277 
0278     case SNIC_TGT_SAN:
0279         dev_set_name(&tgt->dev, "snic_san_tgt:%d:%d-%d",
0280                  snic->shost->host_no, tgt->channel, tgt->id);
0281         break;
0282 
0283     default:
0284         SNIC_HOST_INFO(snic->shost, "Target type Unknown Detected.\n");
0285         dev_set_name(&tgt->dev, "snic_das_tgt:%d:%d-%d",
0286                  snic->shost->host_no, tgt->channel, tgt->id);
0287         break;
0288     }
0289 
0290     spin_lock_irqsave(snic->shost->host_lock, flags);
0291     list_add_tail(&tgt->list, &snic->disc.tgt_list);
0292     tgt->scsi_tgt_id = snic->disc.nxt_tgt_id++;
0293     tgt->state = SNIC_TGT_STAT_ONLINE;
0294     spin_unlock_irqrestore(snic->shost->host_lock, flags);
0295 
0296     SNIC_HOST_INFO(snic->shost,
0297                "Tgt %d, type = %s detected. Adding..\n",
0298                tgt->id, snic_tgt_type_to_str(tgt->tdata.typ));
0299 
0300     ret = device_add(&tgt->dev);
0301     if (ret) {
0302         SNIC_HOST_ERR(snic->shost,
0303                   "Snic Tgt: device_add, with err = %d\n",
0304                   ret);
0305 
0306         put_device(&snic->shost->shost_gendev);
0307         kfree(tgt);
0308         tgt = NULL;
0309 
0310         return tgt;
0311     }
0312 
0313     SNIC_HOST_INFO(snic->shost, "Scanning %s.\n", dev_name(&tgt->dev));
0314 
0315     scsi_queue_work(snic->shost, &tgt->scan_work);
0316 
0317     return tgt;
0318 } /* end of snic_tgt_create */
0319 
0320 /* Handler for discovery */
0321 void
0322 snic_handle_tgt_disc(struct work_struct *work)
0323 {
0324     struct snic *snic = container_of(work, struct snic, tgt_work);
0325     struct snic_tgt_id *tgtid = NULL;
0326     struct snic_tgt *tgt = NULL;
0327     unsigned long flags;
0328     int i;
0329 
0330     spin_lock_irqsave(&snic->snic_lock, flags);
0331     if (snic->in_remove) {
0332         spin_unlock_irqrestore(&snic->snic_lock, flags);
0333         kfree(snic->disc.rtgt_info);
0334 
0335         return;
0336     }
0337     spin_unlock_irqrestore(&snic->snic_lock, flags);
0338 
0339     mutex_lock(&snic->disc.mutex);
0340     /* Discover triggered during disc in progress */
0341     if (snic->disc.req_cnt) {
0342         snic->disc.state = SNIC_DISC_DONE;
0343         snic->disc.req_cnt = 0;
0344         mutex_unlock(&snic->disc.mutex);
0345         kfree(snic->disc.rtgt_info);
0346         snic->disc.rtgt_info = NULL;
0347 
0348         SNIC_HOST_INFO(snic->shost, "tgt_disc: Discovery restart.\n");
0349         /* Start Discovery Again */
0350         snic_disc_start(snic);
0351 
0352         return;
0353     }
0354 
0355     tgtid = (struct snic_tgt_id *)snic->disc.rtgt_info;
0356 
0357     SNIC_BUG_ON(snic->disc.rtgt_cnt == 0 || tgtid == NULL);
0358 
0359     for (i = 0; i < snic->disc.rtgt_cnt; i++) {
0360         tgt = snic_tgt_create(snic, &tgtid[i]);
0361         if (!tgt) {
0362             int buf_sz = snic->disc.rtgt_cnt * sizeof(*tgtid);
0363 
0364             SNIC_HOST_ERR(snic->shost, "Failed to create tgt.\n");
0365             snic_hex_dump("rpt_tgt_rsp", (char *)tgtid, buf_sz);
0366             break;
0367         }
0368     }
0369 
0370     snic->disc.rtgt_info = NULL;
0371     snic->disc.state = SNIC_DISC_DONE;
0372     mutex_unlock(&snic->disc.mutex);
0373 
0374     SNIC_HOST_INFO(snic->shost, "Discovery Completed.\n");
0375 
0376     kfree(tgtid);
0377 } /* end of snic_handle_tgt_disc */
0378 
0379 
0380 int
0381 snic_report_tgt_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
0382 {
0383 
0384     u8 typ, cmpl_stat;
0385     u32 cmnd_id, hid, tgt_cnt = 0;
0386     ulong ctx;
0387     struct snic_req_info *rqi = NULL;
0388     struct snic_tgt_id *tgtid;
0389     int i, ret = 0;
0390 
0391     snic_io_hdr_dec(&fwreq->hdr, &typ, &cmpl_stat, &cmnd_id, &hid, &ctx);
0392     rqi = (struct snic_req_info *) ctx;
0393     tgtid = (struct snic_tgt_id *) rqi->sge_va;
0394 
0395     tgt_cnt = le32_to_cpu(fwreq->u.rpt_tgts_cmpl.tgt_cnt);
0396     if (tgt_cnt == 0) {
0397         SNIC_HOST_ERR(snic->shost, "No Targets Found on this host.\n");
0398         ret = 1;
0399 
0400         goto end;
0401     }
0402 
0403     /* printing list of targets here */
0404     SNIC_HOST_INFO(snic->shost, "Target Count = %d\n", tgt_cnt);
0405 
0406     SNIC_BUG_ON(tgt_cnt > snic->fwinfo.max_tgts);
0407 
0408     for (i = 0; i < tgt_cnt; i++)
0409         SNIC_HOST_INFO(snic->shost,
0410                    "Tgt id = 0x%x\n",
0411                    le32_to_cpu(tgtid[i].tgt_id));
0412 
0413     /*
0414      * Queue work for further processing,
0415      * Response Buffer Memory is freed after creating targets
0416      */
0417     snic->disc.rtgt_cnt = tgt_cnt;
0418     snic->disc.rtgt_info = (u8 *) tgtid;
0419     queue_work(snic_glob->event_q, &snic->tgt_work);
0420     ret = 0;
0421 
0422 end:
0423     /* Unmap Response Buffer */
0424     snic_pci_unmap_rsp_buf(snic, rqi);
0425     if (ret)
0426         kfree(tgtid);
0427 
0428     rqi->sge_va = 0;
0429     snic_release_untagged_req(snic, rqi);
0430 
0431     return ret;
0432 } /* end of snic_report_tgt_cmpl_handler */
0433 
0434 /* Discovery init fn */
0435 void
0436 snic_disc_init(struct snic_disc *disc)
0437 {
0438     INIT_LIST_HEAD(&disc->tgt_list);
0439     mutex_init(&disc->mutex);
0440     disc->disc_id = 0;
0441     disc->nxt_tgt_id = 0;
0442     disc->state = SNIC_DISC_INIT;
0443     disc->req_cnt = 0;
0444     disc->rtgt_cnt = 0;
0445     disc->rtgt_info = NULL;
0446     disc->cb = NULL;
0447 } /* end of snic_disc_init */
0448 
0449 /* Discovery, uninit fn */
0450 void
0451 snic_disc_term(struct snic *snic)
0452 {
0453     struct snic_disc *disc = &snic->disc;
0454 
0455     mutex_lock(&disc->mutex);
0456     if (disc->req_cnt) {
0457         disc->req_cnt = 0;
0458         SNIC_SCSI_DBG(snic->shost, "Terminating Discovery.\n");
0459     }
0460     mutex_unlock(&disc->mutex);
0461 }
0462 
0463 /*
0464  * snic_disc_start: Discovery Start ...
0465  */
0466 int
0467 snic_disc_start(struct snic *snic)
0468 {
0469     struct snic_disc *disc = &snic->disc;
0470     unsigned long flags;
0471     int ret = 0;
0472 
0473     SNIC_SCSI_DBG(snic->shost, "Discovery Start.\n");
0474 
0475     spin_lock_irqsave(&snic->snic_lock, flags);
0476     if (snic->in_remove) {
0477         spin_unlock_irqrestore(&snic->snic_lock, flags);
0478         SNIC_ERR("snic driver removal in progress ...\n");
0479         ret = 0;
0480 
0481         return ret;
0482     }
0483     spin_unlock_irqrestore(&snic->snic_lock, flags);
0484 
0485     mutex_lock(&disc->mutex);
0486     if (disc->state == SNIC_DISC_PENDING) {
0487         disc->req_cnt++;
0488         mutex_unlock(&disc->mutex);
0489 
0490         return ret;
0491     }
0492     disc->state = SNIC_DISC_PENDING;
0493     mutex_unlock(&disc->mutex);
0494 
0495     ret = snic_queue_report_tgt_req(snic);
0496     if (ret)
0497         SNIC_HOST_INFO(snic->shost, "Discovery Failed, err=%d.\n", ret);
0498 
0499     return ret;
0500 } /* end of snic_disc_start */
0501 
0502 /*
0503  * snic_disc_work :
0504  */
0505 void
0506 snic_handle_disc(struct work_struct *work)
0507 {
0508     struct snic *snic = container_of(work, struct snic, disc_work);
0509     int ret = 0;
0510 
0511     SNIC_HOST_INFO(snic->shost, "disc_work: Discovery\n");
0512 
0513     ret = snic_disc_start(snic);
0514     if (ret)
0515         goto disc_err;
0516 
0517 disc_err:
0518     SNIC_HOST_ERR(snic->shost,
0519               "disc_work: Discovery Failed w/ err = %d\n",
0520               ret);
0521 } /* end of snic_disc_work */
0522 
0523 /*
0524  * snic_tgt_del_all : cleanup all snic targets
0525  * Called on unbinding the interface
0526  */
0527 void
0528 snic_tgt_del_all(struct snic *snic)
0529 {
0530     struct snic_tgt *tgt = NULL;
0531     struct list_head *cur, *nxt;
0532     unsigned long flags;
0533 
0534     scsi_flush_work(snic->shost);
0535 
0536     mutex_lock(&snic->disc.mutex);
0537     spin_lock_irqsave(snic->shost->host_lock, flags);
0538 
0539     list_for_each_safe(cur, nxt, &snic->disc.tgt_list) {
0540         tgt = list_entry(cur, struct snic_tgt, list);
0541         tgt->state = SNIC_TGT_STAT_DEL;
0542         list_del_init(&tgt->list);
0543         SNIC_HOST_INFO(snic->shost, "Tgt %d q'ing for del\n", tgt->id);
0544         queue_work(snic_glob->event_q, &tgt->del_work);
0545         tgt = NULL;
0546     }
0547     spin_unlock_irqrestore(snic->shost->host_lock, flags);
0548     mutex_unlock(&snic->disc.mutex);
0549 
0550     flush_workqueue(snic_glob->event_q);
0551 } /* end of snic_tgt_del_all */