isc_cam.c revision 315813
1/*-
2 * Copyright (c) 2005-2010 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 */
27/*
28 | $Id: isc_cam.c 998 2009-12-20 10:32:45Z danny $
29 */
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: stable/10/sys/dev/iscsi_initiator/isc_cam.c 315813 2017-03-23 06:41:13Z mav $");
32
33#include "opt_iscsi_initiator.h"
34
35#include <sys/param.h>
36#include <sys/kernel.h>
37#include <sys/callout.h>
38#if __FreeBSD_version >= 700000
39#include <sys/lock.h>
40#include <sys/mutex.h>
41#endif
42#include <sys/conf.h>
43#include <sys/systm.h>
44#include <sys/malloc.h>
45#include <sys/mbuf.h>
46#include <sys/uio.h>
47#include <sys/sysctl.h>
48#include <sys/sx.h>
49
50#include <cam/cam.h>
51#include <cam/cam_ccb.h>
52#include <cam/cam_sim.h>
53#include <cam/cam_xpt_sim.h>
54#include <cam/cam_periph.h>
55
56#include <dev/iscsi_initiator/iscsi.h>
57#include <dev/iscsi_initiator/iscsivar.h>
58
59static void
60_inq(struct cam_sim *sim, union ccb *ccb)
61{
62     struct ccb_pathinq *cpi = &ccb->cpi;
63     isc_session_t *sp = cam_sim_softc(sim);
64
65     debug_called(8);
66     debug(3, "sid=%d target=%d lun=%d", sp->sid, ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
67
68     cpi->version_num = 1; /* XXX??? */
69     cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32;
70     cpi->target_sprt = 0;
71     cpi->hba_misc = 0;
72     cpi->hba_eng_cnt = 0;
73     cpi->max_target = 0; //ISCSI_MAX_TARGETS - 1;
74     cpi->initiator_id = ISCSI_MAX_TARGETS;
75     cpi->max_lun = sp->opt.maxluns - 1;
76     cpi->bus_id = cam_sim_bus(sim);
77     cpi->base_transfer_speed = 3300; // 40000; // XXX:
78     strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
79     strlcpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
80     strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
81     cpi->unit_number = cam_sim_unit(sim);
82     cpi->ccb_h.status = CAM_REQ_CMP;
83#if defined(KNOB_VALID_ADDRESS)
84     cpi->transport = XPORT_ISCSI;
85     cpi->transport_version = 0;
86#endif
87}
88
89static __inline int
90_scsi_encap(struct cam_sim *sim, union ccb *ccb)
91{
92     int		ret;
93
94#if __FreeBSD_version < 700000
95     ret = scsi_encap(sim, ccb);
96#else
97     isc_session_t	*sp = cam_sim_softc(sim);
98
99     mtx_unlock(&sp->cam_mtx);
100     ret = scsi_encap(sim, ccb);
101     mtx_lock(&sp->cam_mtx);
102#endif
103     return ret;
104}
105
106void
107ic_lost_target(isc_session_t *sp, int target)
108{
109     debug_called(8);
110     sdebug(2, "lost target=%d", target);
111
112     if(sp->cam_path != NULL) {
113	  mtx_lock(&sp->cam_mtx);
114	  xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
115	  xpt_free_path(sp->cam_path);
116	  mtx_unlock(&sp->cam_mtx);
117	  sp->cam_path = 0; // XXX
118     }
119}
120
121static void
122scan_callback(struct cam_periph *periph, union ccb *ccb)
123{
124     isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0;
125
126     debug_called(8);
127
128     xpt_free_ccb(ccb);
129
130     if(sp->flags & ISC_SCANWAIT) {
131	  sp->flags &= ~ISC_SCANWAIT;
132	  wakeup(sp);
133     }
134}
135
136static int
137ic_scan(isc_session_t *sp)
138{
139     union ccb	*ccb;
140
141     debug_called(8);
142     sdebug(2, "scanning sid=%d", sp->sid);
143
144     sp->flags &= ~ISC_CAMDEVS;
145     sp->flags |= ISC_SCANWAIT;
146
147     ccb = xpt_alloc_ccb();
148     ccb->ccb_h.path		= sp->cam_path;
149     ccb->ccb_h.cbfcnp		= scan_callback;
150     ccb->ccb_h.spriv_ptr0	= sp;
151
152     xpt_rescan(ccb);
153
154     while(sp->flags & ISC_SCANWAIT)
155	  tsleep(sp, PRIBIO, "ffp", 5*hz); // the timeout time should
156					    // be configurable
157     sdebug(2, "# of luns=%d", sp->target_nluns);
158
159     if(sp->target_nluns > 0) {
160	  sp->flags |= ISC_CAMDEVS;
161	  return 0;
162     }
163
164     return ENODEV;
165}
166
167static void
168ic_action(struct cam_sim *sim, union ccb *ccb)
169{
170     isc_session_t	*sp = cam_sim_softc(sim);
171     struct ccb_hdr	*ccb_h = &ccb->ccb_h;
172
173     debug_called(8);
174
175     ccb_h->spriv_ptr0 = sp;
176     sdebug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%d retry_count=%d timeout=%d",
177	   ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status,
178	   ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
179	   ccb->ccb_h.retry_count, ccb_h->timeout);
180     if(sp == NULL) {
181	  xdebug("sp == NULL! cannot happen");
182	  return;
183     }
184     switch(ccb_h->func_code) {
185     case XPT_PATH_INQ:
186	  _inq(sim, ccb);
187	  break;
188
189     case XPT_RESET_BUS: // (can just be a stub that does nothing and completes)
190     {
191	  struct ccb_pathinq *cpi = &ccb->cpi;
192
193	  debug(3, "XPT_RESET_BUS");
194	  cpi->ccb_h.status = CAM_REQ_CMP;
195	  break;
196     }
197
198     case XPT_SCSI_IO:
199     {
200	  struct ccb_scsiio* csio = &ccb->csio;
201
202	  debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]);
203	  if(sp == NULL) {
204	       ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS;
205	       debug(4, "xpt_done.status=%d", ccb_h->status);
206	       break;
207	  }
208	  if(ccb_h->target_lun == CAM_LUN_WILDCARD) {
209	       debug(3, "target=%d: bad lun (-1)", ccb_h->target_id);
210	       ccb_h->status = CAM_LUN_INVALID;
211	       break;
212	  }
213	  if(_scsi_encap(sim, ccb) != 0)
214	       return;
215	  break;
216     }
217
218     case XPT_CALC_GEOMETRY:
219     {
220	  struct	ccb_calc_geometry *ccg;
221
222	  ccg = &ccb->ccg;
223	  debug(4, "sid=%d target=%d lun=%d XPT_CALC_GEOMETRY vsize=%jd bsize=%d",
224		sp->sid, ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
225		ccg->volume_size, ccg->block_size);
226	  if(ccg->block_size == 0 ||
227	     (ccg->volume_size < ccg->block_size)) {
228	       // print error message  ...
229	       /* XXX: what error is appropiate? */
230	       break;
231	  }
232	  else {
233	       int	lun, *off, boff;
234
235	       lun = ccb->ccb_h.target_lun;
236	       if(lun > ISCSI_MAX_LUNS) {
237		    // XXX:
238		    xdebug("lun %d > ISCSI_MAX_LUNS!\n", lun);
239		    lun %= ISCSI_MAX_LUNS;
240	       }
241	       off = &sp->target_lun[lun / (sizeof(int)*8)];
242	       boff = BIT(lun % (sizeof(int)*8));
243	       debug(4, "sp->target_nluns=%d *off=%x boff=%x",
244		     sp->target_nluns, *off, boff);
245
246	       if((*off & boff) == 0) {
247		    sp->target_nluns++;
248		    *off |= boff;
249	       }
250	       cam_calc_geometry(ccg, /*extended*/1);
251	  }
252	  break;
253     }
254
255     case XPT_GET_TRAN_SETTINGS:
256     default:
257	  ccb_h->status = CAM_REQ_INVALID;
258	  break;
259     }
260#if __FreeBSD_version < 700000
261     XPT_DONE(sp, ccb);
262#else
263     xpt_done(ccb);
264#endif
265     return;
266}
267
268static void
269ic_poll(struct cam_sim *sim)
270{
271     debug_called(4);
272
273}
274
275int
276ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp)
277{
278     debug_called(8);
279
280     if(sp && sp->cam_sim) {
281	  cp->path_id = cam_sim_path(sp->cam_sim);
282	  cp->target_id = 0;
283	  cp->target_nluns = ISCSI_MAX_LUNS; // XXX: -1?
284	  return 0;
285     }
286     return ENXIO;
287}
288
289void
290ic_destroy(isc_session_t *sp )
291{
292     debug_called(8);
293
294     if(sp->cam_path != NULL) {
295	  sdebug(2, "name=%s unit=%d",
296		 cam_sim_name(sp->cam_sim), cam_sim_unit(sp->cam_sim));
297	  CAM_LOCK(sp);
298#if 0
299	  xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
300#else
301	  xpt_async(XPT_RESET_BUS, sp->cam_path, NULL);
302#endif
303	  xpt_free_path(sp->cam_path);
304	  xpt_bus_deregister(cam_sim_path(sp->cam_sim));
305	  cam_sim_free(sp->cam_sim, TRUE /*free_devq*/);
306
307	  CAM_UNLOCK(sp);
308	  sdebug(2, "done");
309     }
310}
311
312int
313ic_init(isc_session_t *sp)
314{
315     struct cam_sim	*sim;
316     struct cam_devq	*devq;
317
318     debug_called(8);
319
320     if((devq = cam_simq_alloc(256)) == NULL)
321	  return ENOMEM;
322
323#if __FreeBSD_version >= 700000
324     mtx_init(&sp->cam_mtx, "isc-cam", NULL, MTX_DEF);
325#else
326     isp->cam_mtx = Giant;
327#endif
328     sim = cam_sim_alloc(ic_action,
329			 ic_poll,
330			 "iscsi",
331			 sp,
332			 sp->sid,	// unit
333#if __FreeBSD_version >= 700000
334			 &sp->cam_mtx,
335#endif
336			 1,		// max_dev_transactions
337			 0,		// max_tagged_dev_transactions
338			 devq);
339     if(sim == NULL) {
340	  cam_simq_free(devq);
341#if __FreeBSD_version >= 700000
342	  mtx_destroy(&sp->cam_mtx);
343#endif
344	  return ENXIO;
345     }
346
347     CAM_LOCK(sp);
348     if(xpt_bus_register(sim,
349#if __FreeBSD_version >= 700000
350			 NULL,
351#endif
352			 0/*bus_number*/) != CAM_SUCCESS) {
353
354	  cam_sim_free(sim, /*free_devq*/TRUE);
355	  CAM_UNLOCK(sp);
356#if __FreeBSD_version >= 700000
357	  mtx_destroy(&sp->cam_mtx);
358#endif
359	  return ENXIO;
360     }
361     sp->cam_sim = sim;
362     if(xpt_create_path(&sp->cam_path, NULL, cam_sim_path(sp->cam_sim),
363	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
364	  xpt_bus_deregister(cam_sim_path(sp->cam_sim));
365	  cam_sim_free(sim, /*free_devq*/TRUE);
366	  CAM_UNLOCK(sp);
367#if __FreeBSD_version >= 700000
368	  mtx_destroy(&sp->cam_mtx);
369#endif
370	  return ENXIO;
371     }
372     CAM_UNLOCK(sp);
373
374     sdebug(1, "cam subsystem initialized");
375
376     ic_scan(sp);
377
378     return 0;
379}
380