2 * Copyright (c) 1998,1999,2000
3 * Traakan, Inc., Los Altos, CA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include "ndmagents.h"
41 #ifndef NDMOS_OPTION_NO_CONTROL_AGENT
45 ndmca_op_robot_remedy (struct ndm_session *sess)
47 struct ndm_job_param * job = &sess->control_acb.job;
53 rc = ndmca_connect_robot_agent (sess);
54 if (rc) return rc; /* already tattled */
56 rc = ndmca_robot_prep_target (sess);
57 if (rc) return rc; /* already tattled */
59 rc = ndmca_robot_check_ready (sess);
60 if (rc) { /* already tattled */
61 ndmalogf (sess, 0, 0, "Robot is not ready, trying to remedy");
62 rc = ndmca_robot_remedy_ready (sess);
64 ndmalogf (sess, 0, 0, "Robot remedy failed");
73 ndmca_op_robot_startup (struct ndm_session *sess, int verify_media_flag)
75 struct ndm_job_param * job = &sess->control_acb.job;
81 rc = ndmca_connect_robot_agent (sess);
82 if (rc) return rc; /* already tattled */
84 rc = ndmca_robot_prep_target (sess);
85 if (rc) return rc; /* already tattled */
87 rc = ndmca_robot_check_ready (sess);
88 if (rc) { /* already tattled */
89 if (!job->auto_remedy) {
90 ndmalogf (sess, 0, 0, "Robot is not ready, failing");
93 ndmalogf (sess, 0, 0, "Robot is not ready, trying to remedy");
94 rc = ndmca_robot_remedy_ready (sess);
96 ndmalogf (sess, 0, 0, "Robot remedy failed");
101 if (verify_media_flag) {
102 rc = ndmca_media_verify (sess);
103 if (rc) return rc; /* already tattled */
110 * ndmca_op_rewind_tape() and ndmca_op_eject_tape() really
111 * belong somewhere else. Here because they are close
112 * to the other "tape handling" operations.
116 ndmca_op_rewind_tape (struct ndm_session *sess)
118 return ndmca_op_mtio (sess, NDMP9_MTIO_REW);
122 ndmca_op_eject_tape (struct ndm_session *sess)
124 return ndmca_op_mtio (sess, NDMP9_MTIO_OFF);
129 ndmca_op_mtio (struct ndm_session *sess, ndmp9_tape_mtio_op mtio_op)
131 struct ndm_control_agent *ca = &sess->control_acb;
134 ca->tape_mode = NDMP9_TAPE_READ_MODE;
137 rc = ndmca_connect_tape_agent (sess);
139 ndmconn_destruct (sess->plumb.tape);
140 return rc; /* already tattled */
143 rc = ndmca_media_open_tape (sess);
144 if (rc) return rc; /* already tattled */
146 if (mtio_op == NDMP9_MTIO_OFF) {
147 /* best-effort rewind */
148 ndmca_media_mtio_tape (sess, NDMP9_MTIO_REW, 1, 0);
151 rc = ndmca_media_mtio_tape (sess, mtio_op, 1, 0);
153 /* best-effort close */
154 ndmca_media_close_tape (sess);
155 return rc; /* already tattled */
158 rc = ndmca_media_close_tape (sess);
159 if (rc) return rc; /* already tattled */
165 ndmca_op_move_tape (struct ndm_session *sess)
167 struct ndm_control_agent *ca = &sess->control_acb;
168 struct ndm_job_param * job = &ca->job;
169 int src_addr = job->from_addr;
170 int dst_addr = job->to_addr;
174 if (!job->to_addr_given || !job->from_addr_given) {
175 ndmalogf (sess, 0, 0, "Missing to/from addr");
179 rc = ndmca_robot_startup (sess);
180 if (rc) return rc; /* already tattled -- NOT */
182 rc = ndmca_robot_obtain_info (sess);
183 if (rc) return rc; /* already tattled -- NOT */
185 rc = ndmca_robot_move (sess, src_addr, dst_addr);
186 if (rc) return rc; /* already tattled */
192 ndmca_op_import_tape (struct ndm_session *sess)
194 struct ndm_control_agent *ca = &sess->control_acb;
195 struct ndm_job_param * job = &ca->job;
196 struct smc_ctrl_block * smc = &ca->smc_cb;
198 int dst_addr = job->to_addr;
202 if (!job->to_addr_given) {
203 ndmalogf (sess, 0, 0, "Missing to-addr");
207 rc = ndmca_robot_startup (sess);
208 if (rc) return rc; /* already tattled -- NOT */
210 rc = ndmca_robot_obtain_info (sess);
211 if (rc) return rc; /* already tattled -- NOT */
213 if (smc->elem_aa.iee_count < 1) {
214 ndmalogf (sess, 0, 0, "robot has no import/export; try move");
217 src_addr = smc->elem_aa.iee_addr;
219 rc = ndmca_robot_move (sess, src_addr, dst_addr);
220 if (rc) return rc; /* already tattled */
226 ndmca_op_export_tape (struct ndm_session *sess)
228 struct ndm_control_agent *ca = &sess->control_acb;
229 struct ndm_job_param * job = &ca->job;
230 struct smc_ctrl_block * smc = &ca->smc_cb;
231 int src_addr = job->from_addr;
236 if (!job->from_addr_given) {
237 ndmalogf (sess, 0, 0, "Missing from-addr");
241 rc = ndmca_robot_startup (sess);
242 if (rc) return rc; /* already tattled -- NOT */
244 rc = ndmca_robot_obtain_info (sess);
245 if (rc) return rc; /* already tattled -- NOT */
247 if (smc->elem_aa.iee_count < 1) {
248 ndmalogf (sess, 0, 0, "robot has no import/export; try move");
251 dst_addr = smc->elem_aa.iee_addr;
253 rc = ndmca_robot_move (sess, src_addr, dst_addr);
254 if (rc) return rc; /* already tattled */
260 ndmca_op_load_tape (struct ndm_session *sess)
262 struct ndm_control_agent *ca = &sess->control_acb;
263 struct ndm_job_param * job = &ca->job;
264 struct smc_ctrl_block * smc = &ca->smc_cb;
265 int src_addr = job->from_addr;
270 if (!job->from_addr_given) {
271 ndmalogf (sess, 0, 0, "Missing from-addr");
275 rc = ndmca_robot_startup (sess);
276 if (rc) return rc; /* already tattled -- NOT */
278 rc = ndmca_robot_obtain_info (sess);
279 if (rc) return rc; /* already tattled -- NOT */
281 if (job->drive_addr_given) {
282 dst_addr = job->drive_addr;
283 } else if (smc->elem_aa.dte_count > 0) {
284 dst_addr = smc->elem_aa.dte_addr;
286 ndmalogf (sess, 0, 0, "robot has no tape drives? try move");
291 * Calculation for dst_addr repeated in ndmca_robot_load().
292 * We just did it to be sure it would succeed
295 rc = ndmca_robot_load (sess, src_addr);
296 if (rc) return rc; /* already tattled */
302 ndmca_op_unload_tape (struct ndm_session *sess)
304 struct ndm_control_agent *ca = &sess->control_acb;
305 struct ndm_job_param * job = &ca->job;
306 struct smc_ctrl_block * smc = &ca->smc_cb;
307 int src_addr = job->from_addr;
312 rc = ndmca_robot_startup (sess);
313 if (rc) return rc; /* already tattled -- NOT */
315 rc = ndmca_robot_obtain_info (sess);
316 if (rc) return rc; /* already tattled -- NOT */
318 if (job->drive_addr_given) {
319 src_addr = job->drive_addr;
320 } else if (smc->elem_aa.dte_count > 0) {
321 src_addr = smc->elem_aa.dte_addr;
323 ndmalogf (sess, 0, 0, "robot has no tape drives? try move");
328 * Calculation for src_addr repeated in ndmca_robot_unload().
329 * We just did it to be sure it would succeed
332 if (job->tape_device) {
334 rc = ndmca_op_mtio (sess, job->use_eject
335 ? NDMP9_MTIO_OFF : NDMP9_MTIO_REW);
338 if (job->to_addr_given) {
339 dst_addr = job->to_addr;
341 struct smc_element_descriptor * edp;
342 struct smc_element_descriptor * edp2;
346 * Try to automatically determine where to
347 * put the tape, if there is one in the drive.
348 * This is pretty much a rip-off of remedy_robot().
349 * The difference is here we believe the user
350 * that something should happen. Otherwise,
351 * the user would have used REMEDY_ROBOT.
354 edp = ndmca_robot_find_element (sess, src_addr);
356 ndmalogf (sess, 0, 1,
357 "no such slot @%d, trying unload anyway",
359 dst_addr = 0; /* g'luck! */
364 ndmalogf (sess, 0, 1,
365 "drive @%d empty, trying unload anyway",
367 dst_addr = 0; /* g'luck! */
371 sprintf (prefix, "drive @%d full", edp->element_address);
374 ndmalogf (sess, 0, 1,
375 "%s, no SValid info, you must specify to-addr",
380 dst_addr = edp->src_se_addr;
382 sprintf (NDMOS_API_STREND(prefix), ", src @%d",
385 edp2 = ndmca_robot_find_element (sess, dst_addr);
387 ndmalogf (sess, 0, 1,
388 "%s, no such addr, trying unload anyway",
393 if (edp2->element_type_code != SMC_ELEM_TYPE_SE) {
394 ndmalogf (sess, 0, 1,
395 "%s, not slot, trying unload anyway", prefix);
400 ndmalogf (sess, 0, 1,
401 "%s, slot Full, trying unload anyway", prefix);
408 rc = ndmca_robot_unload (sess, dst_addr);
409 if (rc) return rc; /* already tattled */
415 ndmca_op_init_elem_status (struct ndm_session *sess)
420 rc = ndmca_robot_startup (sess);
421 if (rc) return rc; /* already tattled -- NOT */
424 rc = ndmca_robot_obtain_info (sess);
426 rc = ndmca_robot_init_elem_status (sess);
427 if (rc) return rc; /* already tattled */
429 rc = ndmca_robot_query (sess);
430 if (rc) return rc; /* already tattled -- WAY WAY tattled */
435 #endif /* !NDMOS_OPTION_NO_CONTROL_AGENT */