ch.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SCSI Media Changer device driver for Linux 2.6
  4. *
  5. * (c) 1996-2003 Gerd Knorr <kraxel@bytesex.org>
  6. *
  7. */
  8. #define VERSION "0.25"
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/fs.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/major.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/completion.h>
  20. #include <linux/compat.h>
  21. #include <linux/chio.h> /* here are all the ioctls */
  22. #include <linux/mutex.h>
  23. #include <linux/idr.h>
  24. #include <linux/slab.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_driver.h>
  28. #include <scsi/scsi_ioctl.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_dbg.h>
  33. #define CH_DT_MAX 16
  34. #define CH_TYPES 8
  35. #define CH_MAX_DEVS 128
  36. MODULE_DESCRIPTION("device driver for scsi media changer devices");
  37. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org>");
  38. MODULE_LICENSE("GPL");
  39. MODULE_ALIAS_CHARDEV_MAJOR(SCSI_CHANGER_MAJOR);
  40. MODULE_ALIAS_SCSI_DEVICE(TYPE_MEDIUM_CHANGER);
  41. static int init = 1;
  42. module_param(init, int, 0444);
  43. MODULE_PARM_DESC(init, \
  44. "initialize element status on driver load (default: on)");
  45. static int timeout_move = 300;
  46. module_param(timeout_move, int, 0644);
  47. MODULE_PARM_DESC(timeout_move,"timeout for move commands "
  48. "(default: 300 seconds)");
  49. static int timeout_init = 3600;
  50. module_param(timeout_init, int, 0644);
  51. MODULE_PARM_DESC(timeout_init,"timeout for INITIALIZE ELEMENT STATUS "
  52. "(default: 3600 seconds)");
  53. static int verbose = 1;
  54. module_param(verbose, int, 0644);
  55. MODULE_PARM_DESC(verbose,"be verbose (default: on)");
  56. static int debug;
  57. module_param(debug, int, 0644);
  58. MODULE_PARM_DESC(debug,"enable/disable debug messages, also prints more "
  59. "detailed sense codes on scsi errors (default: off)");
  60. static int dt_id[CH_DT_MAX] = { [ 0 ... (CH_DT_MAX-1) ] = -1 };
  61. static int dt_lun[CH_DT_MAX];
  62. module_param_array(dt_id, int, NULL, 0444);
  63. module_param_array(dt_lun, int, NULL, 0444);
  64. /* tell the driver about vendor-specific slots */
  65. static int vendor_firsts[CH_TYPES-4];
  66. static int vendor_counts[CH_TYPES-4];
  67. module_param_array(vendor_firsts, int, NULL, 0444);
  68. module_param_array(vendor_counts, int, NULL, 0444);
  69. static const char * vendor_labels[CH_TYPES-4] = {
  70. "v0", "v1", "v2", "v3"
  71. };
  72. // module_param_string_array(vendor_labels, NULL, 0444);
  73. #define ch_printk(prefix, ch, fmt, a...) \
  74. sdev_prefix_printk(prefix, (ch)->device, (ch)->name, fmt, ##a)
  75. #define DPRINTK(fmt, arg...) \
  76. do { \
  77. if (debug) \
  78. ch_printk(KERN_DEBUG, ch, fmt, ##arg); \
  79. } while (0)
  80. #define VPRINTK(level, fmt, arg...) \
  81. do { \
  82. if (verbose) \
  83. ch_printk(level, ch, fmt, ##arg); \
  84. } while (0)
  85. /* ------------------------------------------------------------------- */
  86. #define MAX_RETRIES 1
  87. static const struct class ch_sysfs_class = {
  88. .name = "scsi_changer",
  89. };
  90. typedef struct {
  91. struct kref ref;
  92. struct list_head list;
  93. int minor;
  94. char name[8];
  95. struct scsi_device *device;
  96. struct scsi_device **dt; /* ptrs to data transfer elements */
  97. u_int firsts[CH_TYPES];
  98. u_int counts[CH_TYPES];
  99. u_int voltags;
  100. struct mutex lock;
  101. } scsi_changer;
  102. static DEFINE_IDR(ch_index_idr);
  103. static DEFINE_SPINLOCK(ch_index_lock);
  104. static const struct {
  105. unsigned char sense;
  106. unsigned char asc;
  107. unsigned char ascq;
  108. int errno;
  109. } ch_err[] = {
  110. /* Just filled in what looks right. Hav'nt checked any standard paper for
  111. these errno assignments, so they may be wrong... */
  112. {
  113. .sense = ILLEGAL_REQUEST,
  114. .asc = 0x21,
  115. .ascq = 0x01,
  116. .errno = EBADSLT, /* Invalid element address */
  117. },{
  118. .sense = ILLEGAL_REQUEST,
  119. .asc = 0x28,
  120. .ascq = 0x01,
  121. .errno = EBADE, /* Import or export element accessed */
  122. },{
  123. .sense = ILLEGAL_REQUEST,
  124. .asc = 0x3B,
  125. .ascq = 0x0D,
  126. .errno = EXFULL, /* Medium destination element full */
  127. },{
  128. .sense = ILLEGAL_REQUEST,
  129. .asc = 0x3B,
  130. .ascq = 0x0E,
  131. .errno = EBADE, /* Medium source element empty */
  132. },{
  133. .sense = ILLEGAL_REQUEST,
  134. .asc = 0x20,
  135. .ascq = 0x00,
  136. .errno = EBADRQC, /* Invalid command operation code */
  137. },{
  138. /* end of list */
  139. }
  140. };
  141. /* ------------------------------------------------------------------- */
  142. static int ch_find_errno(struct scsi_sense_hdr *sshdr)
  143. {
  144. int i,errno = 0;
  145. /* Check to see if additional sense information is available */
  146. if (scsi_sense_valid(sshdr) &&
  147. sshdr->asc != 0) {
  148. for (i = 0; ch_err[i].errno != 0; i++) {
  149. if (ch_err[i].sense == sshdr->sense_key &&
  150. ch_err[i].asc == sshdr->asc &&
  151. ch_err[i].ascq == sshdr->ascq) {
  152. errno = -ch_err[i].errno;
  153. break;
  154. }
  155. }
  156. }
  157. if (errno == 0)
  158. errno = -EIO;
  159. return errno;
  160. }
  161. static int
  162. ch_do_scsi(scsi_changer *ch, unsigned char *cmd, int cmd_len,
  163. void *buffer, unsigned int buflength, enum req_op op)
  164. {
  165. int errno = 0, timeout, result;
  166. struct scsi_sense_hdr sshdr;
  167. struct scsi_failure failure_defs[] = {
  168. {
  169. .sense = UNIT_ATTENTION,
  170. .asc = SCMD_FAILURE_ASC_ANY,
  171. .ascq = SCMD_FAILURE_ASCQ_ANY,
  172. .allowed = 3,
  173. .result = SAM_STAT_CHECK_CONDITION,
  174. },
  175. {}
  176. };
  177. struct scsi_failures failures = {
  178. .failure_definitions = failure_defs,
  179. };
  180. const struct scsi_exec_args exec_args = {
  181. .sshdr = &sshdr,
  182. .failures = &failures,
  183. };
  184. timeout = (cmd[0] == INITIALIZE_ELEMENT_STATUS)
  185. ? timeout_init : timeout_move;
  186. result = scsi_execute_cmd(ch->device, cmd, op, buffer, buflength,
  187. timeout * HZ, MAX_RETRIES, &exec_args);
  188. if (result < 0)
  189. return result;
  190. if (scsi_sense_valid(&sshdr)) {
  191. if (debug)
  192. scsi_print_sense_hdr(ch->device, ch->name, &sshdr);
  193. errno = ch_find_errno(&sshdr);
  194. }
  195. return errno;
  196. }
  197. /* ------------------------------------------------------------------------ */
  198. static int
  199. ch_elem_to_typecode(scsi_changer *ch, u_int elem)
  200. {
  201. int i;
  202. for (i = 0; i < CH_TYPES; i++) {
  203. if (elem >= ch->firsts[i] &&
  204. elem < ch->firsts[i] +
  205. ch->counts[i])
  206. return i+1;
  207. }
  208. return 0;
  209. }
  210. static int
  211. ch_read_element_status(scsi_changer *ch, u_int elem, char *data)
  212. {
  213. u_char cmd[12];
  214. u_char *buffer;
  215. int result;
  216. buffer = kmalloc(512, GFP_KERNEL);
  217. if(!buffer)
  218. return -ENOMEM;
  219. retry:
  220. memset(cmd,0,sizeof(cmd));
  221. cmd[0] = READ_ELEMENT_STATUS;
  222. cmd[1] = ((ch->device->lun & 0x7) << 5) |
  223. (ch->voltags ? 0x10 : 0) |
  224. ch_elem_to_typecode(ch,elem);
  225. cmd[2] = (elem >> 8) & 0xff;
  226. cmd[3] = elem & 0xff;
  227. cmd[5] = 1;
  228. cmd[9] = 255;
  229. if (0 == (result = ch_do_scsi(ch, cmd, 12,
  230. buffer, 256, REQ_OP_DRV_IN))) {
  231. if (((buffer[16] << 8) | buffer[17]) != elem) {
  232. DPRINTK("asked for element 0x%02x, got 0x%02x\n",
  233. elem,(buffer[16] << 8) | buffer[17]);
  234. kfree(buffer);
  235. return -EIO;
  236. }
  237. memcpy(data,buffer+16,16);
  238. } else {
  239. if (ch->voltags) {
  240. ch->voltags = 0;
  241. VPRINTK(KERN_INFO, "device has no volume tag support\n");
  242. goto retry;
  243. }
  244. DPRINTK("READ ELEMENT STATUS for element 0x%x failed\n",elem);
  245. }
  246. kfree(buffer);
  247. return result;
  248. }
  249. static int
  250. ch_init_elem(scsi_changer *ch)
  251. {
  252. int err;
  253. u_char cmd[6];
  254. VPRINTK(KERN_INFO, "INITIALIZE ELEMENT STATUS, may take some time ...\n");
  255. memset(cmd,0,sizeof(cmd));
  256. cmd[0] = INITIALIZE_ELEMENT_STATUS;
  257. cmd[1] = (ch->device->lun & 0x7) << 5;
  258. err = ch_do_scsi(ch, cmd, 6, NULL, 0, REQ_OP_DRV_IN);
  259. VPRINTK(KERN_INFO, "... finished\n");
  260. return err;
  261. }
  262. static int
  263. ch_readconfig(scsi_changer *ch)
  264. {
  265. u_char cmd[10], data[16];
  266. u_char *buffer;
  267. int result,id,lun,i;
  268. u_int elem;
  269. buffer = kzalloc(512, GFP_KERNEL);
  270. if (!buffer)
  271. return -ENOMEM;
  272. memset(cmd,0,sizeof(cmd));
  273. cmd[0] = MODE_SENSE;
  274. cmd[1] = (ch->device->lun & 0x7) << 5;
  275. cmd[2] = 0x1d;
  276. cmd[4] = 255;
  277. result = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);
  278. if (0 != result) {
  279. cmd[1] |= (1<<3);
  280. result = ch_do_scsi(ch, cmd, 10, buffer, 255, REQ_OP_DRV_IN);
  281. }
  282. if (0 == result) {
  283. ch->firsts[CHET_MT] =
  284. (buffer[buffer[3]+ 6] << 8) | buffer[buffer[3]+ 7];
  285. ch->counts[CHET_MT] =
  286. (buffer[buffer[3]+ 8] << 8) | buffer[buffer[3]+ 9];
  287. ch->firsts[CHET_ST] =
  288. (buffer[buffer[3]+10] << 8) | buffer[buffer[3]+11];
  289. ch->counts[CHET_ST] =
  290. (buffer[buffer[3]+12] << 8) | buffer[buffer[3]+13];
  291. ch->firsts[CHET_IE] =
  292. (buffer[buffer[3]+14] << 8) | buffer[buffer[3]+15];
  293. ch->counts[CHET_IE] =
  294. (buffer[buffer[3]+16] << 8) | buffer[buffer[3]+17];
  295. ch->firsts[CHET_DT] =
  296. (buffer[buffer[3]+18] << 8) | buffer[buffer[3]+19];
  297. ch->counts[CHET_DT] =
  298. (buffer[buffer[3]+20] << 8) | buffer[buffer[3]+21];
  299. VPRINTK(KERN_INFO, "type #1 (mt): 0x%x+%d [medium transport]\n",
  300. ch->firsts[CHET_MT],
  301. ch->counts[CHET_MT]);
  302. VPRINTK(KERN_INFO, "type #2 (st): 0x%x+%d [storage]\n",
  303. ch->firsts[CHET_ST],
  304. ch->counts[CHET_ST]);
  305. VPRINTK(KERN_INFO, "type #3 (ie): 0x%x+%d [import/export]\n",
  306. ch->firsts[CHET_IE],
  307. ch->counts[CHET_IE]);
  308. VPRINTK(KERN_INFO, "type #4 (dt): 0x%x+%d [data transfer]\n",
  309. ch->firsts[CHET_DT],
  310. ch->counts[CHET_DT]);
  311. } else {
  312. VPRINTK(KERN_INFO, "reading element address assignment page failed!\n");
  313. }
  314. /* vendor specific element types */
  315. for (i = 0; i < 4; i++) {
  316. if (0 == vendor_counts[i])
  317. continue;
  318. if (NULL == vendor_labels[i])
  319. continue;
  320. ch->firsts[CHET_V1+i] = vendor_firsts[i];
  321. ch->counts[CHET_V1+i] = vendor_counts[i];
  322. VPRINTK(KERN_INFO, "type #%d (v%d): 0x%x+%d [%s, vendor specific]\n",
  323. i+5,i+1,vendor_firsts[i],vendor_counts[i],
  324. vendor_labels[i]);
  325. }
  326. /* look up the devices of the data transfer elements */
  327. ch->dt = kcalloc(ch->counts[CHET_DT], sizeof(*ch->dt),
  328. GFP_KERNEL);
  329. if (!ch->dt) {
  330. kfree(buffer);
  331. return -ENOMEM;
  332. }
  333. for (elem = 0; elem < ch->counts[CHET_DT]; elem++) {
  334. id = -1;
  335. lun = 0;
  336. if (elem < CH_DT_MAX && -1 != dt_id[elem]) {
  337. id = dt_id[elem];
  338. lun = dt_lun[elem];
  339. VPRINTK(KERN_INFO, "dt 0x%x: [insmod option] ",
  340. elem+ch->firsts[CHET_DT]);
  341. } else if (0 != ch_read_element_status
  342. (ch,elem+ch->firsts[CHET_DT],data)) {
  343. VPRINTK(KERN_INFO, "dt 0x%x: READ ELEMENT STATUS failed\n",
  344. elem+ch->firsts[CHET_DT]);
  345. } else {
  346. VPRINTK(KERN_INFO, "dt 0x%x: ",elem+ch->firsts[CHET_DT]);
  347. if (data[6] & 0x80) {
  348. VPRINTK(KERN_CONT, "not this SCSI bus\n");
  349. ch->dt[elem] = NULL;
  350. } else if (0 == (data[6] & 0x30)) {
  351. VPRINTK(KERN_CONT, "ID/LUN unknown\n");
  352. ch->dt[elem] = NULL;
  353. } else {
  354. id = ch->device->id;
  355. lun = 0;
  356. if (data[6] & 0x20) id = data[7];
  357. if (data[6] & 0x10) lun = data[6] & 7;
  358. }
  359. }
  360. if (-1 != id) {
  361. VPRINTK(KERN_CONT, "ID %i, LUN %i, ",id,lun);
  362. ch->dt[elem] =
  363. scsi_device_lookup(ch->device->host,
  364. ch->device->channel,
  365. id,lun);
  366. if (!ch->dt[elem]) {
  367. /* should not happen */
  368. VPRINTK(KERN_CONT, "Huh? device not found!\n");
  369. } else {
  370. VPRINTK(KERN_CONT, "name: %8.8s %16.16s %4.4s\n",
  371. ch->dt[elem]->vendor,
  372. ch->dt[elem]->model,
  373. ch->dt[elem]->rev);
  374. }
  375. }
  376. }
  377. ch->voltags = 1;
  378. kfree(buffer);
  379. return 0;
  380. }
  381. /* ------------------------------------------------------------------------ */
  382. static int
  383. ch_position(scsi_changer *ch, u_int trans, u_int elem, int rotate)
  384. {
  385. u_char cmd[10];
  386. DPRINTK("position: 0x%x\n",elem);
  387. if (0 == trans)
  388. trans = ch->firsts[CHET_MT];
  389. memset(cmd,0,sizeof(cmd));
  390. cmd[0] = POSITION_TO_ELEMENT;
  391. cmd[1] = (ch->device->lun & 0x7) << 5;
  392. cmd[2] = (trans >> 8) & 0xff;
  393. cmd[3] = trans & 0xff;
  394. cmd[4] = (elem >> 8) & 0xff;
  395. cmd[5] = elem & 0xff;
  396. cmd[8] = rotate ? 1 : 0;
  397. return ch_do_scsi(ch, cmd, 10, NULL, 0, REQ_OP_DRV_IN);
  398. }
  399. static int
  400. ch_move(scsi_changer *ch, u_int trans, u_int src, u_int dest, int rotate)
  401. {
  402. u_char cmd[12];
  403. DPRINTK("move: 0x%x => 0x%x\n",src,dest);
  404. if (0 == trans)
  405. trans = ch->firsts[CHET_MT];
  406. memset(cmd,0,sizeof(cmd));
  407. cmd[0] = MOVE_MEDIUM;
  408. cmd[1] = (ch->device->lun & 0x7) << 5;
  409. cmd[2] = (trans >> 8) & 0xff;
  410. cmd[3] = trans & 0xff;
  411. cmd[4] = (src >> 8) & 0xff;
  412. cmd[5] = src & 0xff;
  413. cmd[6] = (dest >> 8) & 0xff;
  414. cmd[7] = dest & 0xff;
  415. cmd[10] = rotate ? 1 : 0;
  416. return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);
  417. }
  418. static int
  419. ch_exchange(scsi_changer *ch, u_int trans, u_int src,
  420. u_int dest1, u_int dest2, int rotate1, int rotate2)
  421. {
  422. u_char cmd[12];
  423. DPRINTK("exchange: 0x%x => 0x%x => 0x%x\n",
  424. src,dest1,dest2);
  425. if (0 == trans)
  426. trans = ch->firsts[CHET_MT];
  427. memset(cmd,0,sizeof(cmd));
  428. cmd[0] = EXCHANGE_MEDIUM;
  429. cmd[1] = (ch->device->lun & 0x7) << 5;
  430. cmd[2] = (trans >> 8) & 0xff;
  431. cmd[3] = trans & 0xff;
  432. cmd[4] = (src >> 8) & 0xff;
  433. cmd[5] = src & 0xff;
  434. cmd[6] = (dest1 >> 8) & 0xff;
  435. cmd[7] = dest1 & 0xff;
  436. cmd[8] = (dest2 >> 8) & 0xff;
  437. cmd[9] = dest2 & 0xff;
  438. cmd[10] = (rotate1 ? 1 : 0) | (rotate2 ? 2 : 0);
  439. return ch_do_scsi(ch, cmd, 12, NULL, 0, REQ_OP_DRV_IN);
  440. }
  441. static void
  442. ch_check_voltag(char *tag)
  443. {
  444. int i;
  445. for (i = 0; i < 32; i++) {
  446. /* restrict to ascii */
  447. if (tag[i] >= 0x7f || tag[i] < 0x20)
  448. tag[i] = ' ';
  449. /* don't allow search wildcards */
  450. if (tag[i] == '?' ||
  451. tag[i] == '*')
  452. tag[i] = ' ';
  453. }
  454. }
  455. static int
  456. ch_set_voltag(scsi_changer *ch, u_int elem,
  457. int alternate, int clear, u_char *tag)
  458. {
  459. u_char cmd[12];
  460. u_char *buffer;
  461. int result;
  462. buffer = kzalloc(512, GFP_KERNEL);
  463. if (!buffer)
  464. return -ENOMEM;
  465. DPRINTK("%s %s voltag: 0x%x => \"%s\"\n",
  466. clear ? "clear" : "set",
  467. alternate ? "alternate" : "primary",
  468. elem, tag);
  469. memset(cmd,0,sizeof(cmd));
  470. cmd[0] = SEND_VOLUME_TAG;
  471. cmd[1] = ((ch->device->lun & 0x7) << 5) |
  472. ch_elem_to_typecode(ch,elem);
  473. cmd[2] = (elem >> 8) & 0xff;
  474. cmd[3] = elem & 0xff;
  475. cmd[5] = clear
  476. ? (alternate ? 0x0d : 0x0c)
  477. : (alternate ? 0x0b : 0x0a);
  478. cmd[9] = 255;
  479. memcpy(buffer,tag,32);
  480. ch_check_voltag(buffer);
  481. result = ch_do_scsi(ch, cmd, 12, buffer, 256, REQ_OP_DRV_OUT);
  482. kfree(buffer);
  483. return result;
  484. }
  485. static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
  486. {
  487. int retval = 0;
  488. u_char data[16];
  489. unsigned int i;
  490. mutex_lock(&ch->lock);
  491. for (i = 0; i < ch->counts[type]; i++) {
  492. if (0 != ch_read_element_status
  493. (ch, ch->firsts[type]+i,data)) {
  494. retval = -EIO;
  495. break;
  496. }
  497. put_user(data[2], dest+i);
  498. if (data[2] & CESTATUS_EXCEPT)
  499. VPRINTK(KERN_INFO, "element 0x%x: asc=0x%x, ascq=0x%x\n",
  500. ch->firsts[type]+i,
  501. (int)data[4],(int)data[5]);
  502. retval = ch_read_element_status
  503. (ch, ch->firsts[type]+i,data);
  504. if (0 != retval)
  505. break;
  506. }
  507. mutex_unlock(&ch->lock);
  508. return retval;
  509. }
  510. /* ------------------------------------------------------------------------ */
  511. static void ch_destroy(struct kref *ref)
  512. {
  513. scsi_changer *ch = container_of(ref, scsi_changer, ref);
  514. ch->device = NULL;
  515. kfree(ch->dt);
  516. kfree(ch);
  517. }
  518. static int
  519. ch_release(struct inode *inode, struct file *file)
  520. {
  521. scsi_changer *ch = file->private_data;
  522. scsi_device_put(ch->device);
  523. file->private_data = NULL;
  524. kref_put(&ch->ref, ch_destroy);
  525. return 0;
  526. }
  527. static int
  528. ch_open(struct inode *inode, struct file *file)
  529. {
  530. scsi_changer *ch;
  531. int minor = iminor(inode);
  532. spin_lock(&ch_index_lock);
  533. ch = idr_find(&ch_index_idr, minor);
  534. if (ch == NULL || !kref_get_unless_zero(&ch->ref)) {
  535. spin_unlock(&ch_index_lock);
  536. return -ENXIO;
  537. }
  538. spin_unlock(&ch_index_lock);
  539. if (scsi_device_get(ch->device)) {
  540. kref_put(&ch->ref, ch_destroy);
  541. return -ENXIO;
  542. }
  543. /* Synchronize with ch_probe() */
  544. mutex_lock(&ch->lock);
  545. file->private_data = ch;
  546. mutex_unlock(&ch->lock);
  547. return 0;
  548. }
  549. static int
  550. ch_checkrange(scsi_changer *ch, unsigned int type, unsigned int unit)
  551. {
  552. if (type >= CH_TYPES || unit >= ch->counts[type])
  553. return -1;
  554. return 0;
  555. }
  556. struct changer_element_status32 {
  557. int ces_type;
  558. compat_uptr_t ces_data;
  559. };
  560. #define CHIOGSTATUS32 _IOW('c', 8, struct changer_element_status32)
  561. static long ch_ioctl(struct file *file,
  562. unsigned int cmd, unsigned long arg)
  563. {
  564. scsi_changer *ch = file->private_data;
  565. int retval;
  566. void __user *argp = (void __user *)arg;
  567. retval = scsi_ioctl_block_when_processing_errors(ch->device, cmd,
  568. file->f_flags & O_NDELAY);
  569. if (retval)
  570. return retval;
  571. switch (cmd) {
  572. case CHIOGPARAMS:
  573. {
  574. struct changer_params params;
  575. params.cp_curpicker = 0;
  576. params.cp_npickers = ch->counts[CHET_MT];
  577. params.cp_nslots = ch->counts[CHET_ST];
  578. params.cp_nportals = ch->counts[CHET_IE];
  579. params.cp_ndrives = ch->counts[CHET_DT];
  580. if (copy_to_user(argp, &params, sizeof(params)))
  581. return -EFAULT;
  582. return 0;
  583. }
  584. case CHIOGVPARAMS:
  585. {
  586. struct changer_vendor_params vparams;
  587. memset(&vparams,0,sizeof(vparams));
  588. if (ch->counts[CHET_V1]) {
  589. vparams.cvp_n1 = ch->counts[CHET_V1];
  590. strscpy(vparams.cvp_label1, vendor_labels[0],
  591. sizeof(vparams.cvp_label1));
  592. }
  593. if (ch->counts[CHET_V2]) {
  594. vparams.cvp_n2 = ch->counts[CHET_V2];
  595. strscpy(vparams.cvp_label2, vendor_labels[1],
  596. sizeof(vparams.cvp_label2));
  597. }
  598. if (ch->counts[CHET_V3]) {
  599. vparams.cvp_n3 = ch->counts[CHET_V3];
  600. strscpy(vparams.cvp_label3, vendor_labels[2],
  601. sizeof(vparams.cvp_label3));
  602. }
  603. if (ch->counts[CHET_V4]) {
  604. vparams.cvp_n4 = ch->counts[CHET_V4];
  605. strscpy(vparams.cvp_label4, vendor_labels[3],
  606. sizeof(vparams.cvp_label4));
  607. }
  608. if (copy_to_user(argp, &vparams, sizeof(vparams)))
  609. return -EFAULT;
  610. return 0;
  611. }
  612. case CHIOPOSITION:
  613. {
  614. struct changer_position pos;
  615. if (copy_from_user(&pos, argp, sizeof (pos)))
  616. return -EFAULT;
  617. if (0 != ch_checkrange(ch, pos.cp_type, pos.cp_unit)) {
  618. DPRINTK("CHIOPOSITION: invalid parameter\n");
  619. return -EBADSLT;
  620. }
  621. mutex_lock(&ch->lock);
  622. retval = ch_position(ch,0,
  623. ch->firsts[pos.cp_type] + pos.cp_unit,
  624. pos.cp_flags & CP_INVERT);
  625. mutex_unlock(&ch->lock);
  626. return retval;
  627. }
  628. case CHIOMOVE:
  629. {
  630. struct changer_move mv;
  631. if (copy_from_user(&mv, argp, sizeof (mv)))
  632. return -EFAULT;
  633. if (0 != ch_checkrange(ch, mv.cm_fromtype, mv.cm_fromunit) ||
  634. 0 != ch_checkrange(ch, mv.cm_totype, mv.cm_tounit )) {
  635. DPRINTK("CHIOMOVE: invalid parameter\n");
  636. return -EBADSLT;
  637. }
  638. mutex_lock(&ch->lock);
  639. retval = ch_move(ch,0,
  640. ch->firsts[mv.cm_fromtype] + mv.cm_fromunit,
  641. ch->firsts[mv.cm_totype] + mv.cm_tounit,
  642. mv.cm_flags & CM_INVERT);
  643. mutex_unlock(&ch->lock);
  644. return retval;
  645. }
  646. case CHIOEXCHANGE:
  647. {
  648. struct changer_exchange mv;
  649. if (copy_from_user(&mv, argp, sizeof (mv)))
  650. return -EFAULT;
  651. if (0 != ch_checkrange(ch, mv.ce_srctype, mv.ce_srcunit ) ||
  652. 0 != ch_checkrange(ch, mv.ce_fdsttype, mv.ce_fdstunit) ||
  653. 0 != ch_checkrange(ch, mv.ce_sdsttype, mv.ce_sdstunit)) {
  654. DPRINTK("CHIOEXCHANGE: invalid parameter\n");
  655. return -EBADSLT;
  656. }
  657. mutex_lock(&ch->lock);
  658. retval = ch_exchange
  659. (ch,0,
  660. ch->firsts[mv.ce_srctype] + mv.ce_srcunit,
  661. ch->firsts[mv.ce_fdsttype] + mv.ce_fdstunit,
  662. ch->firsts[mv.ce_sdsttype] + mv.ce_sdstunit,
  663. mv.ce_flags & CE_INVERT1, mv.ce_flags & CE_INVERT2);
  664. mutex_unlock(&ch->lock);
  665. return retval;
  666. }
  667. case CHIOGSTATUS:
  668. {
  669. struct changer_element_status ces;
  670. if (copy_from_user(&ces, argp, sizeof (ces)))
  671. return -EFAULT;
  672. if (ces.ces_type < 0 || ces.ces_type >= CH_TYPES)
  673. return -EINVAL;
  674. return ch_gstatus(ch, ces.ces_type, ces.ces_data);
  675. }
  676. #ifdef CONFIG_COMPAT
  677. case CHIOGSTATUS32:
  678. {
  679. struct changer_element_status32 ces32;
  680. if (copy_from_user(&ces32, argp, sizeof(ces32)))
  681. return -EFAULT;
  682. if (ces32.ces_type < 0 || ces32.ces_type >= CH_TYPES)
  683. return -EINVAL;
  684. return ch_gstatus(ch, ces32.ces_type,
  685. compat_ptr(ces32.ces_data));
  686. }
  687. #endif
  688. case CHIOGELEM:
  689. {
  690. struct changer_get_element cge;
  691. u_char ch_cmd[12];
  692. u_char *buffer;
  693. unsigned int elem;
  694. int result,i;
  695. if (copy_from_user(&cge, argp, sizeof (cge)))
  696. return -EFAULT;
  697. if (0 != ch_checkrange(ch, cge.cge_type, cge.cge_unit))
  698. return -EINVAL;
  699. elem = ch->firsts[cge.cge_type] + cge.cge_unit;
  700. buffer = kmalloc(512, GFP_KERNEL);
  701. if (!buffer)
  702. return -ENOMEM;
  703. mutex_lock(&ch->lock);
  704. voltag_retry:
  705. memset(ch_cmd, 0, sizeof(ch_cmd));
  706. ch_cmd[0] = READ_ELEMENT_STATUS;
  707. ch_cmd[1] = ((ch->device->lun & 0x7) << 5) |
  708. (ch->voltags ? 0x10 : 0) |
  709. ch_elem_to_typecode(ch,elem);
  710. ch_cmd[2] = (elem >> 8) & 0xff;
  711. ch_cmd[3] = elem & 0xff;
  712. ch_cmd[5] = 1;
  713. ch_cmd[9] = 255;
  714. result = ch_do_scsi(ch, ch_cmd, 12, buffer, 256, REQ_OP_DRV_IN);
  715. if (!result) {
  716. cge.cge_status = buffer[18];
  717. cge.cge_flags = 0;
  718. if (buffer[18] & CESTATUS_EXCEPT) {
  719. cge.cge_errno = EIO;
  720. }
  721. if (buffer[25] & 0x80) {
  722. cge.cge_flags |= CGE_SRC;
  723. if (buffer[25] & 0x40)
  724. cge.cge_flags |= CGE_INVERT;
  725. elem = (buffer[26]<<8) | buffer[27];
  726. for (i = 0; i < 4; i++) {
  727. if (elem >= ch->firsts[i] &&
  728. elem < ch->firsts[i] + ch->counts[i]) {
  729. cge.cge_srctype = i;
  730. cge.cge_srcunit = elem-ch->firsts[i];
  731. }
  732. }
  733. }
  734. if ((buffer[22] & 0x30) == 0x30) {
  735. cge.cge_flags |= CGE_IDLUN;
  736. cge.cge_id = buffer[23];
  737. cge.cge_lun = buffer[22] & 7;
  738. }
  739. if (buffer[9] & 0x80) {
  740. cge.cge_flags |= CGE_PVOLTAG;
  741. memcpy(cge.cge_pvoltag,buffer+28,36);
  742. }
  743. if (buffer[9] & 0x40) {
  744. cge.cge_flags |= CGE_AVOLTAG;
  745. memcpy(cge.cge_avoltag,buffer+64,36);
  746. }
  747. } else if (ch->voltags) {
  748. ch->voltags = 0;
  749. VPRINTK(KERN_INFO, "device has no volume tag support\n");
  750. goto voltag_retry;
  751. }
  752. kfree(buffer);
  753. mutex_unlock(&ch->lock);
  754. if (copy_to_user(argp, &cge, sizeof (cge)))
  755. return -EFAULT;
  756. return result;
  757. }
  758. case CHIOINITELEM:
  759. {
  760. mutex_lock(&ch->lock);
  761. retval = ch_init_elem(ch);
  762. mutex_unlock(&ch->lock);
  763. return retval;
  764. }
  765. case CHIOSVOLTAG:
  766. {
  767. struct changer_set_voltag csv;
  768. int elem;
  769. if (copy_from_user(&csv, argp, sizeof(csv)))
  770. return -EFAULT;
  771. if (0 != ch_checkrange(ch, csv.csv_type, csv.csv_unit)) {
  772. DPRINTK("CHIOSVOLTAG: invalid parameter\n");
  773. return -EBADSLT;
  774. }
  775. elem = ch->firsts[csv.csv_type] + csv.csv_unit;
  776. mutex_lock(&ch->lock);
  777. retval = ch_set_voltag(ch, elem,
  778. csv.csv_flags & CSV_AVOLTAG,
  779. csv.csv_flags & CSV_CLEARTAG,
  780. csv.csv_voltag);
  781. mutex_unlock(&ch->lock);
  782. return retval;
  783. }
  784. default:
  785. return scsi_ioctl(ch->device, file->f_mode & FMODE_WRITE, cmd,
  786. argp);
  787. }
  788. }
  789. /* ------------------------------------------------------------------------ */
  790. static int ch_probe(struct device *dev)
  791. {
  792. struct scsi_device *sd = to_scsi_device(dev);
  793. struct device *class_dev;
  794. int ret;
  795. scsi_changer *ch;
  796. if (sd->type != TYPE_MEDIUM_CHANGER)
  797. return -ENODEV;
  798. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  799. if (NULL == ch)
  800. return -ENOMEM;
  801. idr_preload(GFP_KERNEL);
  802. spin_lock(&ch_index_lock);
  803. ret = idr_alloc(&ch_index_idr, ch, 0, CH_MAX_DEVS + 1, GFP_NOWAIT);
  804. spin_unlock(&ch_index_lock);
  805. idr_preload_end();
  806. if (ret < 0) {
  807. if (ret == -ENOSPC)
  808. ret = -ENODEV;
  809. goto free_ch;
  810. }
  811. ch->minor = ret;
  812. sprintf(ch->name,"ch%d",ch->minor);
  813. ret = scsi_device_get(sd);
  814. if (ret) {
  815. sdev_printk(KERN_WARNING, sd, "ch%d: failed to get device\n",
  816. ch->minor);
  817. goto remove_idr;
  818. }
  819. mutex_init(&ch->lock);
  820. kref_init(&ch->ref);
  821. ch->device = sd;
  822. class_dev = device_create(&ch_sysfs_class, dev,
  823. MKDEV(SCSI_CHANGER_MAJOR, ch->minor), ch,
  824. "s%s", ch->name);
  825. if (IS_ERR(class_dev)) {
  826. sdev_printk(KERN_WARNING, sd, "ch%d: device_create failed\n",
  827. ch->minor);
  828. ret = PTR_ERR(class_dev);
  829. goto put_device;
  830. }
  831. mutex_lock(&ch->lock);
  832. ret = ch_readconfig(ch);
  833. if (ret) {
  834. mutex_unlock(&ch->lock);
  835. goto destroy_dev;
  836. }
  837. if (init)
  838. ch_init_elem(ch);
  839. mutex_unlock(&ch->lock);
  840. dev_set_drvdata(dev, ch);
  841. sdev_printk(KERN_INFO, sd, "Attached scsi changer %s\n", ch->name);
  842. return 0;
  843. destroy_dev:
  844. device_destroy(&ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor));
  845. put_device:
  846. scsi_device_put(sd);
  847. remove_idr:
  848. idr_remove(&ch_index_idr, ch->minor);
  849. free_ch:
  850. kfree(ch);
  851. return ret;
  852. }
  853. static int ch_remove(struct device *dev)
  854. {
  855. scsi_changer *ch = dev_get_drvdata(dev);
  856. spin_lock(&ch_index_lock);
  857. idr_remove(&ch_index_idr, ch->minor);
  858. dev_set_drvdata(dev, NULL);
  859. spin_unlock(&ch_index_lock);
  860. device_destroy(&ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR, ch->minor));
  861. scsi_device_put(ch->device);
  862. kref_put(&ch->ref, ch_destroy);
  863. return 0;
  864. }
  865. static struct scsi_driver ch_template = {
  866. .gendrv = {
  867. .name = "ch",
  868. .owner = THIS_MODULE,
  869. .probe = ch_probe,
  870. .remove = ch_remove,
  871. },
  872. };
  873. static const struct file_operations changer_fops = {
  874. .owner = THIS_MODULE,
  875. .open = ch_open,
  876. .release = ch_release,
  877. .unlocked_ioctl = ch_ioctl,
  878. .compat_ioctl = compat_ptr_ioctl,
  879. .llseek = noop_llseek,
  880. };
  881. static int __init init_ch_module(void)
  882. {
  883. int rc;
  884. printk(KERN_INFO "SCSI Media Changer driver v" VERSION " \n");
  885. rc = class_register(&ch_sysfs_class);
  886. if (rc)
  887. return rc;
  888. rc = register_chrdev(SCSI_CHANGER_MAJOR,"ch",&changer_fops);
  889. if (rc < 0) {
  890. printk("Unable to get major %d for SCSI-Changer\n",
  891. SCSI_CHANGER_MAJOR);
  892. goto fail1;
  893. }
  894. rc = scsi_register_driver(&ch_template.gendrv);
  895. if (rc < 0)
  896. goto fail2;
  897. return 0;
  898. fail2:
  899. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  900. fail1:
  901. class_unregister(&ch_sysfs_class);
  902. return rc;
  903. }
  904. static void __exit exit_ch_module(void)
  905. {
  906. scsi_unregister_driver(&ch_template.gendrv);
  907. unregister_chrdev(SCSI_CHANGER_MAJOR, "ch");
  908. class_unregister(&ch_sysfs_class);
  909. idr_destroy(&ch_index_idr);
  910. }
  911. module_init(init_ch_module);
  912. module_exit(exit_ch_module);