ch.c 25 KB

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