ch.c 24 KB

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