scsi.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * scsi.c Copyright (C) 1992 Drew Eckhardt
  4. * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
  5. * Copyright (C) 2002, 2003 Christoph Hellwig
  6. *
  7. * generic mid-level SCSI driver
  8. * Initial versions: Drew Eckhardt
  9. * Subsequent revisions: Eric Youngdale
  10. *
  11. * <drew@colorado.edu>
  12. *
  13. * Bug correction thanks go to :
  14. * Rik Faith <faith@cs.unc.edu>
  15. * Tommy Thorn <tthorn>
  16. * Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
  17. *
  18. * Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to
  19. * add scatter-gather, multiple outstanding request, and other
  20. * enhancements.
  21. *
  22. * Native multichannel, wide scsi, /proc/scsi and hot plugging
  23. * support added by Michael Neuffer <mike@i-connect.net>
  24. *
  25. * Added request_module("scsi_hostadapter") for kerneld:
  26. * (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modprobe.conf)
  27. * Bjorn Ekwall <bj0rn@blox.se>
  28. * (changed to kmod)
  29. *
  30. * Major improvements to the timeout, abort, and reset processing,
  31. * as well as performance modifications for large queue depths by
  32. * Leonard N. Zubkoff <lnz@dandelion.com>
  33. *
  34. * Converted cli() code to spinlocks, Ingo Molnar
  35. *
  36. * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
  37. *
  38. * out_of_space hacks, D. Gilbert (dpg) 990608
  39. */
  40. #include <linux/module.h>
  41. #include <linux/moduleparam.h>
  42. #include <linux/kernel.h>
  43. #include <linux/timer.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/delay.h>
  48. #include <linux/init.h>
  49. #include <linux/completion.h>
  50. #include <linux/unistd.h>
  51. #include <linux/spinlock.h>
  52. #include <linux/kmod.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/notifier.h>
  55. #include <linux/cpu.h>
  56. #include <linux/mutex.h>
  57. #include <linux/unaligned.h>
  58. #include <scsi/scsi.h>
  59. #include <scsi/scsi_cmnd.h>
  60. #include <scsi/scsi_dbg.h>
  61. #include <scsi/scsi_device.h>
  62. #include <scsi/scsi_driver.h>
  63. #include <scsi/scsi_eh.h>
  64. #include <scsi/scsi_host.h>
  65. #include <scsi/scsi_tcq.h>
  66. #include "scsi_priv.h"
  67. #include "scsi_logging.h"
  68. #define CREATE_TRACE_POINTS
  69. #include <trace/events/scsi.h>
  70. /*
  71. * Definitions and constants.
  72. */
  73. /*
  74. * Note - the initial logging level can be set here to log events at boot time.
  75. * After the system is up, you may enable logging via the /proc interface.
  76. */
  77. unsigned int scsi_logging_level;
  78. #if defined(CONFIG_SCSI_LOGGING)
  79. EXPORT_SYMBOL(scsi_logging_level);
  80. #endif
  81. #ifdef CONFIG_SCSI_LOGGING
  82. void scsi_log_send(struct scsi_cmnd *cmd)
  83. {
  84. unsigned int level;
  85. /*
  86. * If ML QUEUE log level is greater than or equal to:
  87. *
  88. * 1: nothing (match completion)
  89. *
  90. * 2: log opcode + command of all commands + cmd address
  91. *
  92. * 3: same as 2
  93. *
  94. * 4: same as 3
  95. */
  96. if (unlikely(scsi_logging_level)) {
  97. level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
  98. SCSI_LOG_MLQUEUE_BITS);
  99. if (level > 1) {
  100. scmd_printk(KERN_INFO, cmd,
  101. "Send: scmd 0x%p\n", cmd);
  102. scsi_print_command(cmd);
  103. }
  104. }
  105. }
  106. void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
  107. {
  108. unsigned int level;
  109. /*
  110. * If ML COMPLETE log level is greater than or equal to:
  111. *
  112. * 1: log disposition, result, opcode + command, and conditionally
  113. * sense data for failures or non SUCCESS dispositions.
  114. *
  115. * 2: same as 1 but for all command completions.
  116. *
  117. * 3: same as 2
  118. *
  119. * 4: same as 3 plus dump extra junk
  120. */
  121. if (unlikely(scsi_logging_level)) {
  122. level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
  123. SCSI_LOG_MLCOMPLETE_BITS);
  124. if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
  125. (level > 1)) {
  126. scsi_print_result(cmd, "Done", disposition);
  127. scsi_print_command(cmd);
  128. if (scsi_status_is_check_condition(cmd->result))
  129. scsi_print_sense(cmd);
  130. if (level > 3)
  131. scmd_printk(KERN_INFO, cmd,
  132. "scsi host busy %d failed %d\n",
  133. scsi_host_busy(cmd->device->host),
  134. cmd->device->host->host_failed);
  135. }
  136. }
  137. }
  138. #endif
  139. /**
  140. * scsi_finish_command - cleanup and pass command back to upper layer
  141. * @cmd: the command
  142. *
  143. * Description: Pass command off to upper layer for finishing of I/O
  144. * request, waking processes that are waiting on results,
  145. * etc.
  146. */
  147. void scsi_finish_command(struct scsi_cmnd *cmd)
  148. {
  149. struct scsi_device *sdev = cmd->device;
  150. struct scsi_target *starget = scsi_target(sdev);
  151. struct Scsi_Host *shost = sdev->host;
  152. struct scsi_driver *drv;
  153. unsigned int good_bytes;
  154. scsi_device_unbusy(sdev, cmd);
  155. /*
  156. * Clear the flags that say that the device/target/host is no longer
  157. * capable of accepting new commands.
  158. */
  159. if (atomic_read(&shost->host_blocked))
  160. atomic_set(&shost->host_blocked, 0);
  161. if (atomic_read(&starget->target_blocked))
  162. atomic_set(&starget->target_blocked, 0);
  163. if (atomic_read(&sdev->device_blocked))
  164. atomic_set(&sdev->device_blocked, 0);
  165. SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
  166. "Notifying upper driver of completion "
  167. "(result %x)\n", cmd->result));
  168. good_bytes = scsi_bufflen(cmd);
  169. if (!blk_rq_is_passthrough(scsi_cmd_to_rq(cmd))) {
  170. int old_good_bytes = good_bytes;
  171. drv = scsi_cmd_to_driver(cmd);
  172. if (drv->done)
  173. good_bytes = drv->done(cmd);
  174. /*
  175. * USB may not give sense identifying bad sector and
  176. * simply return a residue instead, so subtract off the
  177. * residue if drv->done() error processing indicates no
  178. * change to the completion length.
  179. */
  180. if (good_bytes == old_good_bytes)
  181. good_bytes -= scsi_get_resid(cmd);
  182. }
  183. scsi_io_completion(cmd, good_bytes);
  184. }
  185. /*
  186. * 4096 is big enough for saturating fast SCSI LUNs.
  187. */
  188. int scsi_device_max_queue_depth(struct scsi_device *sdev)
  189. {
  190. return min_t(int, sdev->host->can_queue, 4096);
  191. }
  192. /**
  193. * scsi_change_queue_depth - change a device's queue depth
  194. * @sdev: SCSI Device in question
  195. * @depth: number of commands allowed to be queued to the driver
  196. *
  197. * Sets the device queue depth and returns the new value.
  198. */
  199. int scsi_change_queue_depth(struct scsi_device *sdev, int depth)
  200. {
  201. if (!sdev->budget_map.map)
  202. return -EINVAL;
  203. depth = min_t(int, depth, scsi_device_max_queue_depth(sdev));
  204. if (depth > 0) {
  205. sdev->queue_depth = depth;
  206. wmb();
  207. }
  208. if (sdev->request_queue)
  209. blk_set_queue_depth(sdev->request_queue, depth);
  210. sbitmap_resize(&sdev->budget_map, sdev->queue_depth);
  211. return sdev->queue_depth;
  212. }
  213. EXPORT_SYMBOL(scsi_change_queue_depth);
  214. /**
  215. * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth
  216. * @sdev: SCSI Device in question
  217. * @depth: Current number of outstanding SCSI commands on this device,
  218. * not counting the one returned as QUEUE_FULL.
  219. *
  220. * Description: This function will track successive QUEUE_FULL events on a
  221. * specific SCSI device to determine if and when there is a
  222. * need to adjust the queue depth on the device.
  223. *
  224. * Returns:
  225. * * 0 - No change needed
  226. * * >0 - Adjust queue depth to this new depth,
  227. * * -1 - Drop back to untagged operation using host->cmd_per_lun as the
  228. * untagged command depth
  229. *
  230. * Lock Status: None held on entry
  231. *
  232. * Notes: Low level drivers may call this at any time and we will do
  233. * "The Right Thing." We are interrupt context safe.
  234. */
  235. int scsi_track_queue_full(struct scsi_device *sdev, int depth)
  236. {
  237. if (!sdev->budget_map.map)
  238. return 0;
  239. /*
  240. * Don't let QUEUE_FULLs on the same
  241. * jiffies count, they could all be from
  242. * same event.
  243. */
  244. if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4))
  245. return 0;
  246. sdev->last_queue_full_time = jiffies;
  247. if (sdev->last_queue_full_depth != depth) {
  248. sdev->last_queue_full_count = 1;
  249. sdev->last_queue_full_depth = depth;
  250. } else {
  251. sdev->last_queue_full_count++;
  252. }
  253. if (sdev->last_queue_full_count <= 10)
  254. return 0;
  255. return scsi_change_queue_depth(sdev, depth);
  256. }
  257. EXPORT_SYMBOL(scsi_track_queue_full);
  258. /**
  259. * scsi_vpd_inquiry - Request a device provide us with a VPD page
  260. * @sdev: The device to ask
  261. * @buffer: Where to put the result
  262. * @page: Which Vital Product Data to return
  263. * @len: The length of the buffer
  264. *
  265. * This is an internal helper function. You probably want to use
  266. * scsi_get_vpd_page instead.
  267. *
  268. * Returns size of the vpd page on success or a negative error number.
  269. */
  270. static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer,
  271. u8 page, unsigned len)
  272. {
  273. int result;
  274. unsigned char cmd[16];
  275. if (len < 4)
  276. return -EINVAL;
  277. cmd[0] = INQUIRY;
  278. cmd[1] = 1; /* EVPD */
  279. cmd[2] = page;
  280. cmd[3] = len >> 8;
  281. cmd[4] = len & 0xff;
  282. cmd[5] = 0; /* Control byte */
  283. /*
  284. * I'm not convinced we need to try quite this hard to get VPD, but
  285. * all the existing users tried this hard.
  286. */
  287. result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, buffer, len,
  288. 30 * HZ, 3, NULL);
  289. if (result)
  290. return -EIO;
  291. /*
  292. * Sanity check that we got the page back that we asked for and that
  293. * the page size is not 0.
  294. */
  295. if (buffer[1] != page)
  296. return -EIO;
  297. result = get_unaligned_be16(&buffer[2]);
  298. if (!result)
  299. return -EIO;
  300. return result + 4;
  301. }
  302. enum scsi_vpd_parameters {
  303. SCSI_VPD_HEADER_SIZE = 4,
  304. SCSI_VPD_LIST_SIZE = 36,
  305. };
  306. static int scsi_get_vpd_size(struct scsi_device *sdev, u8 page)
  307. {
  308. unsigned char vpd[SCSI_VPD_LIST_SIZE] __aligned(4);
  309. int result;
  310. if (sdev->no_vpd_size)
  311. return SCSI_DEFAULT_VPD_LEN;
  312. /*
  313. * Fetch the supported pages VPD and validate that the requested page
  314. * number is present.
  315. */
  316. if (page != 0) {
  317. result = scsi_vpd_inquiry(sdev, vpd, 0, sizeof(vpd));
  318. if (result < SCSI_VPD_HEADER_SIZE)
  319. return 0;
  320. if (result > sizeof(vpd)) {
  321. dev_warn_once(&sdev->sdev_gendev,
  322. "%s: long VPD page 0 length: %d bytes\n",
  323. __func__, result);
  324. result = sizeof(vpd);
  325. }
  326. result -= SCSI_VPD_HEADER_SIZE;
  327. if (!memchr(&vpd[SCSI_VPD_HEADER_SIZE], page, result))
  328. return 0;
  329. }
  330. /*
  331. * Fetch the VPD page header to find out how big the page
  332. * is. This is done to prevent problems on legacy devices
  333. * which can not handle allocation lengths as large as
  334. * potentially requested by the caller.
  335. */
  336. result = scsi_vpd_inquiry(sdev, vpd, page, SCSI_VPD_HEADER_SIZE);
  337. if (result < 0)
  338. return 0;
  339. if (result < SCSI_VPD_HEADER_SIZE) {
  340. dev_warn_once(&sdev->sdev_gendev,
  341. "%s: short VPD page 0x%02x length: %d bytes\n",
  342. __func__, page, result);
  343. return 0;
  344. }
  345. return result;
  346. }
  347. /**
  348. * scsi_get_vpd_page - Get Vital Product Data from a SCSI device
  349. * @sdev: The device to ask
  350. * @page: Which Vital Product Data to return
  351. * @buf: where to store the VPD
  352. * @buf_len: number of bytes in the VPD buffer area
  353. *
  354. * SCSI devices may optionally supply Vital Product Data. Each 'page'
  355. * of VPD is defined in the appropriate SCSI document (eg SPC, SBC).
  356. * If the device supports this VPD page, this routine fills @buf
  357. * with the data from that page and return 0. If the VPD page is not
  358. * supported or its content cannot be retrieved, -EINVAL is returned.
  359. */
  360. int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf,
  361. int buf_len)
  362. {
  363. int result, vpd_len;
  364. if (!scsi_device_supports_vpd(sdev))
  365. return -EINVAL;
  366. vpd_len = scsi_get_vpd_size(sdev, page);
  367. if (vpd_len <= 0)
  368. return -EINVAL;
  369. vpd_len = min(vpd_len, buf_len);
  370. /*
  371. * Fetch the actual page. Since the appropriate size was reported
  372. * by the device it is now safe to ask for something bigger.
  373. */
  374. memset(buf, 0, buf_len);
  375. result = scsi_vpd_inquiry(sdev, buf, page, vpd_len);
  376. if (result < 0)
  377. return -EINVAL;
  378. else if (result > vpd_len)
  379. dev_warn_once(&sdev->sdev_gendev,
  380. "%s: VPD page 0x%02x result %d > %d bytes\n",
  381. __func__, page, result, vpd_len);
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(scsi_get_vpd_page);
  385. /**
  386. * scsi_get_vpd_buf - Get Vital Product Data from a SCSI device
  387. * @sdev: The device to ask
  388. * @page: Which Vital Product Data to return
  389. *
  390. * Returns %NULL upon failure.
  391. */
  392. static struct scsi_vpd *scsi_get_vpd_buf(struct scsi_device *sdev, u8 page)
  393. {
  394. struct scsi_vpd *vpd_buf;
  395. int vpd_len, result;
  396. vpd_len = scsi_get_vpd_size(sdev, page);
  397. if (vpd_len <= 0)
  398. return NULL;
  399. retry_pg:
  400. /*
  401. * Fetch the actual page. Since the appropriate size was reported
  402. * by the device it is now safe to ask for something bigger.
  403. */
  404. vpd_buf = kmalloc(sizeof(*vpd_buf) + vpd_len, GFP_KERNEL);
  405. if (!vpd_buf)
  406. return NULL;
  407. result = scsi_vpd_inquiry(sdev, vpd_buf->data, page, vpd_len);
  408. if (result < 0) {
  409. kfree(vpd_buf);
  410. return NULL;
  411. }
  412. if (result > vpd_len) {
  413. dev_warn_once(&sdev->sdev_gendev,
  414. "%s: VPD page 0x%02x result %d > %d bytes\n",
  415. __func__, page, result, vpd_len);
  416. vpd_len = result;
  417. kfree(vpd_buf);
  418. goto retry_pg;
  419. }
  420. vpd_buf->len = result;
  421. return vpd_buf;
  422. }
  423. static void scsi_update_vpd_page(struct scsi_device *sdev, u8 page,
  424. struct scsi_vpd __rcu **sdev_vpd_buf)
  425. {
  426. struct scsi_vpd *vpd_buf;
  427. vpd_buf = scsi_get_vpd_buf(sdev, page);
  428. if (!vpd_buf)
  429. return;
  430. mutex_lock(&sdev->inquiry_mutex);
  431. vpd_buf = rcu_replace_pointer(*sdev_vpd_buf, vpd_buf,
  432. lockdep_is_held(&sdev->inquiry_mutex));
  433. mutex_unlock(&sdev->inquiry_mutex);
  434. if (vpd_buf)
  435. kfree_rcu(vpd_buf, rcu);
  436. }
  437. /**
  438. * scsi_attach_vpd - Attach Vital Product Data to a SCSI device structure
  439. * @sdev: The device to ask
  440. *
  441. * Attach the 'Device Identification' VPD page (0x83) and the
  442. * 'Unit Serial Number' VPD page (0x80) to a SCSI device
  443. * structure. This information can be used to identify the device
  444. * uniquely.
  445. */
  446. void scsi_attach_vpd(struct scsi_device *sdev)
  447. {
  448. int i;
  449. struct scsi_vpd *vpd_buf;
  450. if (!scsi_device_supports_vpd(sdev))
  451. return;
  452. /* Ask for all the pages supported by this device */
  453. vpd_buf = scsi_get_vpd_buf(sdev, 0);
  454. if (!vpd_buf)
  455. return;
  456. for (i = 4; i < vpd_buf->len; i++) {
  457. switch (vpd_buf->data[i]) {
  458. case 0x0:
  459. scsi_update_vpd_page(sdev, 0x0, &sdev->vpd_pg0);
  460. break;
  461. case 0x80:
  462. scsi_update_vpd_page(sdev, 0x80, &sdev->vpd_pg80);
  463. break;
  464. case 0x83:
  465. scsi_update_vpd_page(sdev, 0x83, &sdev->vpd_pg83);
  466. break;
  467. case 0x89:
  468. scsi_update_vpd_page(sdev, 0x89, &sdev->vpd_pg89);
  469. break;
  470. case 0xb0:
  471. scsi_update_vpd_page(sdev, 0xb0, &sdev->vpd_pgb0);
  472. break;
  473. case 0xb1:
  474. scsi_update_vpd_page(sdev, 0xb1, &sdev->vpd_pgb1);
  475. break;
  476. case 0xb2:
  477. scsi_update_vpd_page(sdev, 0xb2, &sdev->vpd_pgb2);
  478. break;
  479. case 0xb7:
  480. scsi_update_vpd_page(sdev, 0xb7, &sdev->vpd_pgb7);
  481. break;
  482. default:
  483. break;
  484. }
  485. }
  486. kfree(vpd_buf);
  487. }
  488. /**
  489. * scsi_report_opcode - Find out if a given command is supported
  490. * @sdev: scsi device to query
  491. * @buffer: scratch buffer (must be at least 20 bytes long)
  492. * @len: length of buffer
  493. * @opcode: opcode for the command to look up
  494. * @sa: service action for the command to look up
  495. *
  496. * Uses the REPORT SUPPORTED OPERATION CODES to check support for the
  497. * command identified with @opcode and @sa. If the command does not
  498. * have a service action, @sa must be 0. Returns -EINVAL if RSOC fails,
  499. * 0 if the command is not supported and 1 if the device claims to
  500. * support the command.
  501. */
  502. int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
  503. unsigned int len, unsigned char opcode,
  504. unsigned short sa)
  505. {
  506. unsigned char cmd[16];
  507. struct scsi_sense_hdr sshdr;
  508. int result, request_len;
  509. const struct scsi_exec_args exec_args = {
  510. .sshdr = &sshdr,
  511. };
  512. if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3)
  513. return -EINVAL;
  514. /* RSOC header + size of command we are asking about */
  515. request_len = 4 + COMMAND_SIZE(opcode);
  516. if (request_len > len) {
  517. dev_warn_once(&sdev->sdev_gendev,
  518. "%s: len %u bytes, opcode 0x%02x needs %u\n",
  519. __func__, len, opcode, request_len);
  520. return -EINVAL;
  521. }
  522. memset(cmd, 0, 16);
  523. cmd[0] = MAINTENANCE_IN;
  524. cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES;
  525. if (!sa) {
  526. cmd[2] = 1; /* One command format */
  527. cmd[3] = opcode;
  528. } else {
  529. cmd[2] = 3; /* One command format with service action */
  530. cmd[3] = opcode;
  531. put_unaligned_be16(sa, &cmd[4]);
  532. }
  533. put_unaligned_be32(request_len, &cmd[6]);
  534. memset(buffer, 0, len);
  535. result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, buffer,
  536. request_len, 30 * HZ, 3, &exec_args);
  537. if (result < 0)
  538. return result;
  539. if (result && scsi_sense_valid(&sshdr) &&
  540. sshdr.sense_key == ILLEGAL_REQUEST &&
  541. (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00)
  542. return -EINVAL;
  543. if ((buffer[1] & 3) == 3) /* Command supported */
  544. return 1;
  545. return 0;
  546. }
  547. EXPORT_SYMBOL(scsi_report_opcode);
  548. #define SCSI_CDL_CHECK_BUF_LEN 64
  549. static bool scsi_cdl_check_cmd(struct scsi_device *sdev, u8 opcode, u16 sa,
  550. unsigned char *buf)
  551. {
  552. int ret;
  553. u8 cdlp;
  554. /* Check operation code */
  555. ret = scsi_report_opcode(sdev, buf, SCSI_CDL_CHECK_BUF_LEN, opcode, sa);
  556. if (ret <= 0)
  557. return false;
  558. if ((buf[1] & 0x03) != 0x03)
  559. return false;
  560. /*
  561. * See SPC-6, One_command parameter data format for
  562. * REPORT SUPPORTED OPERATION CODES. We have the following cases
  563. * depending on rwcdlp (buf[0] & 0x01) value:
  564. * - rwcdlp == 0: then cdlp indicates support for the A mode page when
  565. * it is equal to 1 and for the B mode page when it is
  566. * equal to 2.
  567. * - rwcdlp == 1: then cdlp indicates support for the T2A mode page
  568. * when it is equal to 1 and for the T2B mode page when
  569. * it is equal to 2.
  570. * Overall, to detect support for command duration limits, we only need
  571. * to check that cdlp is 1 or 2.
  572. */
  573. cdlp = (buf[1] & 0x18) >> 3;
  574. return cdlp == 0x01 || cdlp == 0x02;
  575. }
  576. /**
  577. * scsi_cdl_check - Check if a SCSI device supports Command Duration Limits
  578. * @sdev: The device to check
  579. */
  580. void scsi_cdl_check(struct scsi_device *sdev)
  581. {
  582. bool cdl_supported;
  583. unsigned char *buf;
  584. /*
  585. * Support for CDL was defined in SPC-5. Ignore devices reporting an
  586. * lower SPC version. This also avoids problems with old drives choking
  587. * on MAINTENANCE_IN / MI_REPORT_SUPPORTED_OPERATION_CODES with a
  588. * service action specified, as done in scsi_cdl_check_cmd().
  589. */
  590. if (sdev->scsi_level < SCSI_SPC_5) {
  591. sdev->cdl_supported = 0;
  592. return;
  593. }
  594. buf = kmalloc(SCSI_CDL_CHECK_BUF_LEN, GFP_KERNEL);
  595. if (!buf) {
  596. sdev->cdl_supported = 0;
  597. return;
  598. }
  599. /* Check support for READ_16, WRITE_16, READ_32 and WRITE_32 commands */
  600. cdl_supported =
  601. scsi_cdl_check_cmd(sdev, READ_16, 0, buf) ||
  602. scsi_cdl_check_cmd(sdev, WRITE_16, 0, buf) ||
  603. scsi_cdl_check_cmd(sdev, VARIABLE_LENGTH_CMD, READ_32, buf) ||
  604. scsi_cdl_check_cmd(sdev, VARIABLE_LENGTH_CMD, WRITE_32, buf);
  605. if (cdl_supported) {
  606. /*
  607. * We have CDL support: force the use of READ16/WRITE16.
  608. * READ32 and WRITE32 will be used for devices that support
  609. * the T10_PI_TYPE2_PROTECTION protection type.
  610. */
  611. sdev->use_16_for_rw = 1;
  612. sdev->use_10_for_rw = 0;
  613. sdev->cdl_supported = 1;
  614. /*
  615. * If the device supports CDL, make sure that the current drive
  616. * feature status is consistent with the user controlled
  617. * cdl_enable state.
  618. */
  619. scsi_cdl_enable(sdev, sdev->cdl_enable);
  620. } else {
  621. sdev->cdl_supported = 0;
  622. }
  623. kfree(buf);
  624. }
  625. /**
  626. * scsi_cdl_enable - Enable or disable a SCSI device supports for Command
  627. * Duration Limits
  628. * @sdev: The target device
  629. * @enable: the target state
  630. */
  631. int scsi_cdl_enable(struct scsi_device *sdev, bool enable)
  632. {
  633. char buf[64];
  634. int ret;
  635. if (!sdev->cdl_supported)
  636. return -EOPNOTSUPP;
  637. /*
  638. * For ATA devices, CDL needs to be enabled with a SET FEATURES command.
  639. */
  640. if (sdev->is_ata) {
  641. struct scsi_mode_data data;
  642. struct scsi_sense_hdr sshdr;
  643. char *buf_data;
  644. int len;
  645. ret = scsi_mode_sense(sdev, 0x08, 0x0a, 0xf2, buf, sizeof(buf),
  646. 5 * HZ, 3, &data, NULL);
  647. if (ret)
  648. return -EINVAL;
  649. /* Enable or disable CDL using the ATA feature page */
  650. len = min_t(size_t, sizeof(buf),
  651. data.length - data.header_length -
  652. data.block_descriptor_length);
  653. buf_data = buf + data.header_length +
  654. data.block_descriptor_length;
  655. /*
  656. * If we want to enable CDL and CDL is already enabled on the
  657. * device, do nothing. This avoids needlessly resetting the CDL
  658. * statistics on the device as that is implied by the CDL enable
  659. * action. Similar to this, there is no need to do anything if
  660. * we want to disable CDL and CDL is already disabled.
  661. */
  662. if (enable) {
  663. if ((buf_data[4] & 0x03) == 0x02)
  664. goto out;
  665. buf_data[4] &= ~0x03;
  666. buf_data[4] |= 0x02;
  667. } else {
  668. if ((buf_data[4] & 0x03) == 0x00)
  669. goto out;
  670. buf_data[4] &= ~0x03;
  671. }
  672. ret = scsi_mode_select(sdev, 1, 0, buf_data, len, 5 * HZ, 3,
  673. &data, &sshdr);
  674. if (ret) {
  675. if (ret > 0 && scsi_sense_valid(&sshdr))
  676. scsi_print_sense_hdr(sdev,
  677. dev_name(&sdev->sdev_gendev), &sshdr);
  678. return ret;
  679. }
  680. }
  681. out:
  682. sdev->cdl_enable = enable;
  683. return 0;
  684. }
  685. /**
  686. * scsi_device_get - get an additional reference to a scsi_device
  687. * @sdev: device to get a reference to
  688. *
  689. * Description: Gets a reference to the scsi_device and increments the use count
  690. * of the underlying LLDD module. You must hold host_lock of the
  691. * parent Scsi_Host or already have a reference when calling this.
  692. *
  693. * This will fail if a device is deleted or cancelled, or when the LLD module
  694. * is in the process of being unloaded.
  695. */
  696. int scsi_device_get(struct scsi_device *sdev)
  697. {
  698. if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
  699. goto fail;
  700. if (!try_module_get(sdev->host->hostt->module))
  701. goto fail;
  702. if (!get_device(&sdev->sdev_gendev))
  703. goto fail_put_module;
  704. return 0;
  705. fail_put_module:
  706. module_put(sdev->host->hostt->module);
  707. fail:
  708. return -ENXIO;
  709. }
  710. EXPORT_SYMBOL(scsi_device_get);
  711. /**
  712. * scsi_device_put - release a reference to a scsi_device
  713. * @sdev: device to release a reference on.
  714. *
  715. * Description: Release a reference to the scsi_device and decrements the use
  716. * count of the underlying LLDD module. The device is freed once the last
  717. * user vanishes.
  718. */
  719. void scsi_device_put(struct scsi_device *sdev)
  720. {
  721. struct module *mod = sdev->host->hostt->module;
  722. put_device(&sdev->sdev_gendev);
  723. module_put(mod);
  724. }
  725. EXPORT_SYMBOL(scsi_device_put);
  726. /* helper for shost_for_each_device, see that for documentation */
  727. struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
  728. struct scsi_device *prev)
  729. {
  730. struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
  731. struct scsi_device *next = NULL;
  732. unsigned long flags;
  733. spin_lock_irqsave(shost->host_lock, flags);
  734. while (list->next != &shost->__devices) {
  735. next = list_entry(list->next, struct scsi_device, siblings);
  736. /*
  737. * Skip pseudo devices and also devices we can't get a
  738. * reference to.
  739. */
  740. if (!scsi_device_is_pseudo_dev(next) && !scsi_device_get(next))
  741. break;
  742. next = NULL;
  743. list = list->next;
  744. }
  745. spin_unlock_irqrestore(shost->host_lock, flags);
  746. if (prev)
  747. scsi_device_put(prev);
  748. return next;
  749. }
  750. EXPORT_SYMBOL(__scsi_iterate_devices);
  751. /**
  752. * starget_for_each_device - helper to walk all devices of a target
  753. * @starget: target whose devices we want to iterate over.
  754. * @data: Opaque passed to each function call.
  755. * @fn: Function to call on each device
  756. *
  757. * This traverses over each device of @starget. The devices have
  758. * a reference that must be released by scsi_host_put when breaking
  759. * out of the loop.
  760. */
  761. void starget_for_each_device(struct scsi_target *starget, void *data,
  762. void (*fn)(struct scsi_device *, void *))
  763. {
  764. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  765. struct scsi_device *sdev;
  766. shost_for_each_device(sdev, shost) {
  767. if ((sdev->channel == starget->channel) &&
  768. (sdev->id == starget->id))
  769. fn(sdev, data);
  770. }
  771. }
  772. EXPORT_SYMBOL(starget_for_each_device);
  773. /**
  774. * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
  775. * @starget: target whose devices we want to iterate over.
  776. * @data: parameter for callback @fn()
  777. * @fn: callback function that is invoked for each device
  778. *
  779. * This traverses over each device of @starget. It does _not_
  780. * take a reference on the scsi_device, so the whole loop must be
  781. * protected by shost->host_lock.
  782. *
  783. * Note: The only reason why drivers would want to use this is because
  784. * they need to access the device list in irq context. Otherwise you
  785. * really want to use starget_for_each_device instead.
  786. **/
  787. void __starget_for_each_device(struct scsi_target *starget, void *data,
  788. void (*fn)(struct scsi_device *, void *))
  789. {
  790. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  791. struct scsi_device *sdev;
  792. __shost_for_each_device(sdev, shost) {
  793. if ((sdev->channel == starget->channel) &&
  794. (sdev->id == starget->id))
  795. fn(sdev, data);
  796. }
  797. }
  798. EXPORT_SYMBOL(__starget_for_each_device);
  799. /**
  800. * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
  801. * @starget: SCSI target pointer
  802. * @lun: SCSI Logical Unit Number
  803. *
  804. * Description: Looks up the scsi_device with the specified @lun for a given
  805. * @starget. The returned scsi_device does not have an additional
  806. * reference. You must hold the host's host_lock over this call and
  807. * any access to the returned scsi_device. A scsi_device in state
  808. * SDEV_DEL is skipped.
  809. *
  810. * Note: The only reason why drivers should use this is because
  811. * they need to access the device list in irq context. Otherwise you
  812. * really want to use scsi_device_lookup_by_target instead.
  813. **/
  814. struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
  815. u64 lun)
  816. {
  817. struct scsi_device *sdev;
  818. list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
  819. if (sdev->sdev_state == SDEV_DEL)
  820. continue;
  821. if (sdev->lun ==lun)
  822. return sdev;
  823. }
  824. return NULL;
  825. }
  826. EXPORT_SYMBOL(__scsi_device_lookup_by_target);
  827. /**
  828. * scsi_device_lookup_by_target - find a device given the target
  829. * @starget: SCSI target pointer
  830. * @lun: SCSI Logical Unit Number
  831. *
  832. * Description: Looks up the scsi_device with the specified @lun for a given
  833. * @starget. The returned scsi_device has an additional reference that
  834. * needs to be released with scsi_device_put once you're done with it.
  835. **/
  836. struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
  837. u64 lun)
  838. {
  839. struct scsi_device *sdev;
  840. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  841. unsigned long flags;
  842. spin_lock_irqsave(shost->host_lock, flags);
  843. sdev = __scsi_device_lookup_by_target(starget, lun);
  844. if (sdev && scsi_device_get(sdev))
  845. sdev = NULL;
  846. spin_unlock_irqrestore(shost->host_lock, flags);
  847. return sdev;
  848. }
  849. EXPORT_SYMBOL(scsi_device_lookup_by_target);
  850. /**
  851. * __scsi_device_lookup - find a device given the host (UNLOCKED)
  852. * @shost: SCSI host pointer
  853. * @channel: SCSI channel (zero if only one channel)
  854. * @id: SCSI target number (physical unit number)
  855. * @lun: SCSI Logical Unit Number
  856. *
  857. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  858. * for a given host. The returned scsi_device does not have an additional
  859. * reference. You must hold the host's host_lock over this call and any access
  860. * to the returned scsi_device.
  861. *
  862. * Note: The only reason why drivers would want to use this is because
  863. * they need to access the device list in irq context. Otherwise you
  864. * really want to use scsi_device_lookup instead.
  865. **/
  866. struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
  867. uint channel, uint id, u64 lun)
  868. {
  869. struct scsi_device *sdev;
  870. list_for_each_entry(sdev, &shost->__devices, siblings) {
  871. if (sdev->sdev_state == SDEV_DEL)
  872. continue;
  873. if (sdev->channel == channel && sdev->id == id &&
  874. sdev->lun ==lun)
  875. return sdev;
  876. }
  877. return NULL;
  878. }
  879. EXPORT_SYMBOL(__scsi_device_lookup);
  880. /**
  881. * scsi_device_lookup - find a device given the host
  882. * @shost: SCSI host pointer
  883. * @channel: SCSI channel (zero if only one channel)
  884. * @id: SCSI target number (physical unit number)
  885. * @lun: SCSI Logical Unit Number
  886. *
  887. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  888. * for a given host. The returned scsi_device has an additional reference that
  889. * needs to be released with scsi_device_put once you're done with it.
  890. **/
  891. struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
  892. uint channel, uint id, u64 lun)
  893. {
  894. struct scsi_device *sdev;
  895. unsigned long flags;
  896. spin_lock_irqsave(shost->host_lock, flags);
  897. sdev = __scsi_device_lookup(shost, channel, id, lun);
  898. if (sdev && scsi_device_get(sdev))
  899. sdev = NULL;
  900. spin_unlock_irqrestore(shost->host_lock, flags);
  901. return sdev;
  902. }
  903. EXPORT_SYMBOL(scsi_device_lookup);
  904. MODULE_DESCRIPTION("SCSI core");
  905. MODULE_LICENSE("GPL");
  906. module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
  907. MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
  908. static int __init init_scsi(void)
  909. {
  910. int error;
  911. error = scsi_init_procfs();
  912. if (error)
  913. goto cleanup_queue;
  914. error = scsi_init_devinfo();
  915. if (error)
  916. goto cleanup_procfs;
  917. error = scsi_init_hosts();
  918. if (error)
  919. goto cleanup_devlist;
  920. error = scsi_init_sysctl();
  921. if (error)
  922. goto cleanup_hosts;
  923. error = scsi_sysfs_register();
  924. if (error)
  925. goto cleanup_sysctl;
  926. scsi_netlink_init();
  927. printk(KERN_NOTICE "SCSI subsystem initialized\n");
  928. return 0;
  929. cleanup_sysctl:
  930. scsi_exit_sysctl();
  931. cleanup_hosts:
  932. scsi_exit_hosts();
  933. cleanup_devlist:
  934. scsi_exit_devinfo();
  935. cleanup_procfs:
  936. scsi_exit_procfs();
  937. cleanup_queue:
  938. scsi_exit_queue();
  939. printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
  940. -error);
  941. return error;
  942. }
  943. static void __exit exit_scsi(void)
  944. {
  945. scsi_netlink_exit();
  946. scsi_sysfs_unregister();
  947. scsi_exit_sysctl();
  948. scsi_exit_hosts();
  949. scsi_exit_devinfo();
  950. scsi_exit_procfs();
  951. scsi_exit_queue();
  952. }
  953. subsys_initcall(init_scsi);
  954. module_exit(exit_scsi);