xref: /dragonfly/sys/dev/disk/iscsi/initiator/isc_cam.c (revision 3d33658b)
1 /*-
2  * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/iscsi/initiator/isc_cam.c,v 1.3 2008/11/25 07:17:11 scottl Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/callout.h>
32 #include <sys/lock.h>
33 #include <sys/conf.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/uio.h>
38 #include <sys/sysctl.h>
39 #include <sys/eventhandler.h>
40 #include <sys/globaldata.h>
41 #include <sys/mutex2.h>
42 
43 #include <bus/cam/cam.h>
44 #include <bus/cam/cam_ccb.h>
45 #include <bus/cam/cam_sim.h>
46 #include <bus/cam/cam_xpt.h>
47 #include <bus/cam/cam_xpt_sim.h>
48 #include <bus/cam/cam_xpt_periph.h>
49 #include <bus/cam/cam_periph.h>
50 
51 #include <dev/disk/iscsi/initiator/iscsi.h>
52 #include <dev/disk/iscsi/initiator/iscsivar.h>
53 
54 // XXX: untested/incomplete
55 void
56 ic_freeze(isc_session_t *sp)
57 {
58      debug_called(8);
59 #if 0
60      sdebug(2, "freezing path=%p", sp->cam_path == NULL? 0: sp->cam_path);
61      if((sp->cam_path != NULL) && !(sp->flags & ISC_FROZEN)) {
62 	  xpt_freeze_devq(sp->cam_path, 1);
63      }
64 #endif
65      sp->flags |= ISC_FROZEN;
66 }
67 
68 // XXX: untested/incomplete
69 void
70 ic_release(isc_session_t *sp)
71 {
72      debug_called(8);
73 #if 0
74      sdebug(2, "release path=%p", sp->cam_path == NULL? 0: sp->cam_path);
75      if((sp->cam_path != NULL) && (sp->flags & ISC_FROZEN)) {
76 	  xpt_release_devq(sp->cam_path, 1, TRUE);
77      }
78 #endif
79      sp->flags &= ~ISC_FROZEN;
80 }
81 
82 void
83 ic_lost_target(isc_session_t *sp, int target)
84 {
85      struct isc_softc   *isp = sp->isc;
86 
87      debug_called(8);
88      sdebug(2, "target=%d", target);
89      if(sp->cam_path != NULL) {
90 	  lockmgr(&isp->cam_lock, LK_EXCLUSIVE);
91 	  xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
92 	  xpt_free_path(sp->cam_path);
93 	  lockmgr(&isp->cam_lock, LK_RELEASE);
94 	  sp->cam_path = 0; // XXX
95      }
96 }
97 
98 static void
99 _scan_callback(struct cam_periph *periph, union ccb *ccb)
100 {
101      isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0;
102 
103      debug_called(8);
104 
105      xpt_free_ccb(&ccb->ccb_h);
106      if (sp->flags & ISC_FFPWAIT) {
107 	  sp->flags &= ~ISC_FFPWAIT;
108 	  wakeup(sp);
109      }
110 }
111 
112 static void
113 _scan_target(isc_session_t *sp, int target)
114 {
115      union ccb *ccb;
116 
117      debug_called(8);
118      sdebug(2, "target=%d", target);
119 
120      ccb = xpt_alloc_ccb();
121 
122      CAM_LOCK(sp->isc);
123      xpt_setup_ccb(&ccb->ccb_h, sp->cam_path, 5/*priority (low)*/);
124      ccb->ccb_h.func_code	= XPT_SCAN_BUS;
125      ccb->ccb_h.cbfcnp		= _scan_callback;
126      ccb->crcn.flags		= CAM_FLAG_NONE;
127      ccb->ccb_h.spriv_ptr0	= sp;
128 
129      xpt_action(ccb);
130      CAM_UNLOCK(sp->isc);
131 }
132 
133 int
134 ic_fullfeature(struct cdev *dev)
135 {
136      struct isc_softc 	*isp = dev->si_drv1;
137      isc_session_t	*sp = (isc_session_t *)dev->si_drv2;
138 
139      debug_called(8);
140      sdebug(3, "dev=%d sc=%p", minor(dev), isp);
141 
142      sp->flags &= ~ISC_FFPHASE;
143      sp->flags |= ISC_FFPWAIT;
144 
145      CAM_LOCK(isp);
146      if(xpt_create_path(&sp->cam_path, xpt_periph, cam_sim_path(sp->isc->cam_sim),
147 			sp->sid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
148 	  xdebug("can't create cam path");
149 	  CAM_UNLOCK(isp);
150 	  return ENODEV; // XXX
151      }
152      CAM_UNLOCK(isp);
153 
154      _scan_target(sp, sp->sid);
155 
156      while(sp->flags & ISC_FFPWAIT)
157 	  tsleep(sp, 0, "ffp", 5*hz); // timeout should be configurable
158      if(sp->target_nluns > 0) {
159 	  sp->flags |= ISC_FFPHASE;
160 	  return 0;
161      }
162 
163      return ENODEV;
164 }
165 
166 static void
167 _inq(struct cam_sim *sim, union ccb *ccb, int maxluns)
168 {
169      struct ccb_pathinq *cpi = &ccb->cpi;
170 
171      debug_called(4);
172 
173      cpi->version_num = 1; /* XXX??? */
174      cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32;
175      cpi->target_sprt = 0;
176      cpi->hba_misc = 0;
177      cpi->hba_eng_cnt = 0;
178      cpi->max_target = ISCSI_MAX_TARGETS - 1;
179      cpi->initiator_id = ISCSI_MAX_TARGETS;
180      cpi->max_lun = maxluns;
181      cpi->bus_id = cam_sim_bus(sim);
182      cpi->base_transfer_speed = 3300;
183      strncpy(cpi->sim_vid, "DragonFly", SIM_IDLEN);
184      strncpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
185      strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
186      cpi->unit_number = cam_sim_unit(sim);
187      cpi->ccb_h.status = CAM_REQ_CMP;
188 }
189 
190 static __inline int
191 _scsi_encap(struct cam_sim *sim, union ccb *ccb)
192 {
193      int		ret;
194      struct isc_softc	*isp = (struct isc_softc *)cam_sim_softc(sim);
195 
196      lockmgr(&isp->cam_lock, LK_EXCLUSIVE);
197      ret = scsi_encap(sim, ccb);
198      lockmgr(&isp->cam_lock, LK_RELEASE);
199      return ret;
200 }
201 
202 static void
203 ic_action(struct cam_sim *sim, union ccb *ccb)
204 {
205      struct ccb_hdr	*ccb_h = &ccb->ccb_h;
206      struct isc_softc	*isp = (struct isc_softc *)cam_sim_softc(sim);
207      isc_session_t	*sp;
208 
209      debug_called(8);
210 
211      if((ccb_h->target_id != CAM_TARGET_WILDCARD) && (ccb_h->target_id < MAX_SESSIONS))
212 	  sp = isp->sessions[ccb_h->target_id];
213      else
214 	  sp = NULL;
215 
216      ccb_h->spriv_ptr0 = sp;
217 
218      debug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%d retry_count=%d timeout=%d",
219 	   ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status,
220 	   ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
221 	   ccb->ccb_h.retry_count, ccb_h->timeout);
222      /*
223       | first quick check
224       */
225      switch(ccb_h->func_code) {
226      default:
227 	  // XXX: maybe check something else?
228 	  break;
229 
230      case XPT_SCSI_IO:
231      case XPT_RESET_DEV:
232      case XPT_GET_TRAN_SETTINGS:
233      case XPT_SET_TRAN_SETTINGS:
234      case XPT_CALC_GEOMETRY:
235 	  if(sp == NULL) {
236 	       ccb->ccb_h.status = CAM_DEV_NOT_THERE;
237 	       xpt_done(ccb);
238 	       return;
239 	  }
240 	  break;
241 
242      case XPT_PATH_INQ:
243      case XPT_NOOP:
244 	  if(sp == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
245 	       ccb->ccb_h.status = CAM_DEV_NOT_THERE;
246 	       xpt_done(ccb);
247 	       debug(4, "status = CAM_DEV_NOT_THERE");
248 	       return;
249 	  }
250      }
251 
252      switch(ccb_h->func_code) {
253 
254      case XPT_PATH_INQ:
255 	  _inq(sim, ccb, (sp? sp->opt.maxluns: ISCSI_MAX_LUNS) - 1);
256 	  break;
257 
258      case XPT_RESET_BUS: // (can just be a stub that does nothing and completes)
259      {
260 	  struct ccb_pathinq *cpi = &ccb->cpi;
261 
262 	  debug(3, "XPT_RESET_BUS");
263 	  cpi->ccb_h.status = CAM_REQ_CMP;
264 	  break;
265      }
266 
267      case XPT_SCSI_IO:
268      {
269 	  struct ccb_scsiio* csio = &ccb->csio;
270 
271 	  debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]);
272 	  if(sp == NULL) {
273 	       ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS;
274 	       debug(4, "xpt_done.status=%d", ccb_h->status);
275 	       break;
276 	  }
277 	  if(ccb_h->target_lun == CAM_LUN_WILDCARD) {
278 	       debug(3, "target=%d: bad lun (-1)", ccb_h->target_id);
279 	       ccb_h->status = CAM_LUN_INVALID;
280 	       break;
281 	  }
282 	  if(_scsi_encap(sim, ccb) != 0)
283 	       return;
284 	  break;
285      }
286 
287      case XPT_CALC_GEOMETRY:
288      {
289 	  struct	ccb_calc_geometry *ccg;
290 
291 	  ccg = &ccb->ccg;
292 	  debug(6, "XPT_CALC_GEOMETRY vsize=%jd bsize=%d", ccg->volume_size, ccg->block_size);
293 	  if(ccg->block_size == 0 ||
294 	     (ccg->volume_size < ccg->block_size)) {
295 	       // print error message  ...
296 	       /* XXX: what error is appropiate? */
297 	       break;
298 	  } else
299 	       cam_calc_geometry(ccg, /*extended*/1);
300 	  break;
301      }
302 
303      case XPT_GET_TRAN_SETTINGS:
304      default:
305 	  ccb_h->status = CAM_REQ_INVALID;
306 	  break;
307      }
308      xpt_done(ccb);
309      return;
310 }
311 
312 static void
313 ic_poll(struct cam_sim *sim)
314 {
315      debug_called(8);
316 
317 }
318 
319 int
320 ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp)
321 {
322      int	i;
323 
324      debug_called(8);
325 
326      if(sp && sp->isc->cam_sim) {
327 	  cp->path_id = cam_sim_path(sp->isc->cam_sim);
328 	  cp->target_id = sp->sid;
329 	  cp->target_nluns = sp->target_nluns; // XXX: -1?
330 	  for(i = 0; i < cp->target_nluns; i++)
331 	       cp->target_lun[i] = sp->target_lun[i];
332 	  return 0;
333      }
334      return ENXIO;
335 }
336 
337 void
338 ic_destroy(struct isc_softc *isp)
339 {
340      debug_called(8);
341 
342      CAM_LOCK(isp); // can't harm :-)
343 
344      xpt_async(AC_LOST_DEVICE, isp->cam_path, NULL);
345      xpt_free_path(isp->cam_path);
346      xpt_bus_deregister(cam_sim_path(isp->cam_sim));
347      cam_sim_free(isp->cam_sim);
348 
349      CAM_UNLOCK(isp);
350 }
351 
352 int
353 ic_init(struct isc_softc *isp)
354 {
355      struct cam_sim	*sim;
356      struct cam_devq	*devq;
357      struct cam_path	*path;
358 
359      if((devq = cam_simq_alloc(256)) == NULL)
360 	  return ENOMEM;
361 
362      lockinit(&isp->cam_lock, "isc-cam", 0, LK_CANRECURSE);
363      sim = cam_sim_alloc(ic_action, ic_poll,
364 			 "iscsi", isp, 0, /* unit */
365 			 &sim_mplock,
366 			 1,		/* max_dev_transactions */
367 			 100,		/* max_tagged_dev_transactions */
368 			 devq);
369      cam_simq_release(devq);
370      if(sim == NULL) {
371 	  lockuninit(&isp->cam_lock);
372 	  return ENXIO;
373      }
374      CAM_LOCK(isp);
375      if(xpt_bus_register(sim,
376 			 0/*bus_number*/) != CAM_SUCCESS)
377 	  goto bad;
378 
379      if(xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
380 			CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
381 	  xpt_bus_deregister(cam_sim_path(sim));
382 	  goto bad;
383      }
384 
385      CAM_UNLOCK(isp);
386 
387      isp->cam_sim = sim;
388      isp->cam_path = path;
389 
390      debug(2, "cam subsystem initialized"); // XXX: add dev ...
391      debug(4, "sim=%p path=%p", sim, path);
392      return 0;
393 
394  bad:
395      cam_sim_free(sim);
396      CAM_UNLOCK(isp);
397      return ENXIO;
398 }
399