ch.c 25 KB

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