scsi.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. depth = min_t(int, depth, scsi_device_max_queue_depth(sdev));
  202. if (depth > 0) {
  203. sdev->queue_depth = depth;
  204. wmb();
  205. }
  206. if (sdev->request_queue)
  207. blk_set_queue_depth(sdev->request_queue, depth);
  208. sbitmap_resize(&sdev->budget_map, sdev->queue_depth);
  209. return sdev->queue_depth;
  210. }
  211. EXPORT_SYMBOL(scsi_change_queue_depth);
  212. /**
  213. * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth
  214. * @sdev: SCSI Device in question
  215. * @depth: Current number of outstanding SCSI commands on this device,
  216. * not counting the one returned as QUEUE_FULL.
  217. *
  218. * Description: This function will track successive QUEUE_FULL events on a
  219. * specific SCSI device to determine if and when there is a
  220. * need to adjust the queue depth on the device.
  221. *
  222. * Returns: 0 - No change needed, >0 - Adjust queue depth to this new depth,
  223. * -1 - Drop back to untagged operation using host->cmd_per_lun
  224. * as the untagged command depth
  225. *
  226. * Lock Status: None held on entry
  227. *
  228. * Notes: Low level drivers may call this at any time and we will do
  229. * "The Right Thing." We are interrupt context safe.
  230. */
  231. int scsi_track_queue_full(struct scsi_device *sdev, int depth)
  232. {
  233. /*
  234. * Don't let QUEUE_FULLs on the same
  235. * jiffies count, they could all be from
  236. * same event.
  237. */
  238. if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4))
  239. return 0;
  240. sdev->last_queue_full_time = jiffies;
  241. if (sdev->last_queue_full_depth != depth) {
  242. sdev->last_queue_full_count = 1;
  243. sdev->last_queue_full_depth = depth;
  244. } else {
  245. sdev->last_queue_full_count++;
  246. }
  247. if (sdev->last_queue_full_count <= 10)
  248. return 0;
  249. return scsi_change_queue_depth(sdev, depth);
  250. }
  251. EXPORT_SYMBOL(scsi_track_queue_full);
  252. /**
  253. * scsi_vpd_inquiry - Request a device provide us with a VPD page
  254. * @sdev: The device to ask
  255. * @buffer: Where to put the result
  256. * @page: Which Vital Product Data to return
  257. * @len: The length of the buffer
  258. *
  259. * This is an internal helper function. You probably want to use
  260. * scsi_get_vpd_page instead.
  261. *
  262. * Returns size of the vpd page on success or a negative error number.
  263. */
  264. static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer,
  265. u8 page, unsigned len)
  266. {
  267. int result;
  268. unsigned char cmd[16];
  269. if (len < 4)
  270. return -EINVAL;
  271. cmd[0] = INQUIRY;
  272. cmd[1] = 1; /* EVPD */
  273. cmd[2] = page;
  274. cmd[3] = len >> 8;
  275. cmd[4] = len & 0xff;
  276. cmd[5] = 0; /* Control byte */
  277. /*
  278. * I'm not convinced we need to try quite this hard to get VPD, but
  279. * all the existing users tried this hard.
  280. */
  281. result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, buffer, len,
  282. 30 * HZ, 3, NULL);
  283. if (result)
  284. return -EIO;
  285. /*
  286. * Sanity check that we got the page back that we asked for and that
  287. * the page size is not 0.
  288. */
  289. if (buffer[1] != page)
  290. return -EIO;
  291. result = get_unaligned_be16(&buffer[2]);
  292. if (!result)
  293. return -EIO;
  294. return result + 4;
  295. }
  296. enum scsi_vpd_parameters {
  297. SCSI_VPD_HEADER_SIZE = 4,
  298. SCSI_VPD_LIST_SIZE = 36,
  299. };
  300. static int scsi_get_vpd_size(struct scsi_device *sdev, u8 page)
  301. {
  302. unsigned char vpd[SCSI_VPD_LIST_SIZE] __aligned(4);
  303. int result;
  304. if (sdev->no_vpd_size)
  305. return SCSI_DEFAULT_VPD_LEN;
  306. /*
  307. * Fetch the supported pages VPD and validate that the requested page
  308. * number is present.
  309. */
  310. if (page != 0) {
  311. result = scsi_vpd_inquiry(sdev, vpd, 0, sizeof(vpd));
  312. if (result < SCSI_VPD_HEADER_SIZE)
  313. return 0;
  314. if (result > sizeof(vpd)) {
  315. dev_warn_once(&sdev->sdev_gendev,
  316. "%s: long VPD page 0 length: %d bytes\n",
  317. __func__, result);
  318. result = sizeof(vpd);
  319. }
  320. result -= SCSI_VPD_HEADER_SIZE;
  321. if (!memchr(&vpd[SCSI_VPD_HEADER_SIZE], page, result))
  322. return 0;
  323. }
  324. /*
  325. * Fetch the VPD page header to find out how big the page
  326. * is. This is done to prevent problems on legacy devices
  327. * which can not handle allocation lengths as large as
  328. * potentially requested by the caller.
  329. */
  330. result = scsi_vpd_inquiry(sdev, vpd, page, SCSI_VPD_HEADER_SIZE);
  331. if (result < 0)
  332. return 0;
  333. if (result < SCSI_VPD_HEADER_SIZE) {
  334. dev_warn_once(&sdev->sdev_gendev,
  335. "%s: short VPD page 0x%02x length: %d bytes\n",
  336. __func__, page, result);
  337. return 0;
  338. }
  339. return result;
  340. }
  341. /**
  342. * scsi_get_vpd_page - Get Vital Product Data from a SCSI device
  343. * @sdev: The device to ask
  344. * @page: Which Vital Product Data to return
  345. * @buf: where to store the VPD
  346. * @buf_len: number of bytes in the VPD buffer area
  347. *
  348. * SCSI devices may optionally supply Vital Product Data. Each 'page'
  349. * of VPD is defined in the appropriate SCSI document (eg SPC, SBC).
  350. * If the device supports this VPD page, this routine fills @buf
  351. * with the data from that page and return 0. If the VPD page is not
  352. * supported or its content cannot be retrieved, -EINVAL is returned.
  353. */
  354. int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf,
  355. int buf_len)
  356. {
  357. int result, vpd_len;
  358. if (!scsi_device_supports_vpd(sdev))
  359. return -EINVAL;
  360. vpd_len = scsi_get_vpd_size(sdev, page);
  361. if (vpd_len <= 0)
  362. return -EINVAL;
  363. vpd_len = min(vpd_len, buf_len);
  364. /*
  365. * Fetch the actual page. Since the appropriate size was reported
  366. * by the device it is now safe to ask for something bigger.
  367. */
  368. memset(buf, 0, buf_len);
  369. result = scsi_vpd_inquiry(sdev, buf, page, vpd_len);
  370. if (result < 0)
  371. return -EINVAL;
  372. else if (result > vpd_len)
  373. dev_warn_once(&sdev->sdev_gendev,
  374. "%s: VPD page 0x%02x result %d > %d bytes\n",
  375. __func__, page, result, vpd_len);
  376. return 0;
  377. }
  378. EXPORT_SYMBOL_GPL(scsi_get_vpd_page);
  379. /**
  380. * scsi_get_vpd_buf - Get Vital Product Data from a SCSI device
  381. * @sdev: The device to ask
  382. * @page: Which Vital Product Data to return
  383. *
  384. * Returns %NULL upon failure.
  385. */
  386. static struct scsi_vpd *scsi_get_vpd_buf(struct scsi_device *sdev, u8 page)
  387. {
  388. struct scsi_vpd *vpd_buf;
  389. int vpd_len, result;
  390. vpd_len = scsi_get_vpd_size(sdev, page);
  391. if (vpd_len <= 0)
  392. return NULL;
  393. retry_pg:
  394. /*
  395. * Fetch the actual page. Since the appropriate size was reported
  396. * by the device it is now safe to ask for something bigger.
  397. */
  398. vpd_buf = kmalloc(sizeof(*vpd_buf) + vpd_len, GFP_KERNEL);
  399. if (!vpd_buf)
  400. return NULL;
  401. result = scsi_vpd_inquiry(sdev, vpd_buf->data, page, vpd_len);
  402. if (result < 0) {
  403. kfree(vpd_buf);
  404. return NULL;
  405. }
  406. if (result > vpd_len) {
  407. dev_warn_once(&sdev->sdev_gendev,
  408. "%s: VPD page 0x%02x result %d > %d bytes\n",
  409. __func__, page, result, vpd_len);
  410. vpd_len = result;
  411. kfree(vpd_buf);
  412. goto retry_pg;
  413. }
  414. vpd_buf->len = result;
  415. return vpd_buf;
  416. }
  417. static void scsi_update_vpd_page(struct scsi_device *sdev, u8 page,
  418. struct scsi_vpd __rcu **sdev_vpd_buf)
  419. {
  420. struct scsi_vpd *vpd_buf;
  421. vpd_buf = scsi_get_vpd_buf(sdev, page);
  422. if (!vpd_buf)
  423. return;
  424. mutex_lock(&sdev->inquiry_mutex);
  425. vpd_buf = rcu_replace_pointer(*sdev_vpd_buf, vpd_buf,
  426. lockdep_is_held(&sdev->inquiry_mutex));
  427. mutex_unlock(&sdev->inquiry_mutex);
  428. if (vpd_buf)
  429. kfree_rcu(vpd_buf, rcu);
  430. }
  431. /**
  432. * scsi_attach_vpd - Attach Vital Product Data to a SCSI device structure
  433. * @sdev: The device to ask
  434. *
  435. * Attach the 'Device Identification' VPD page (0x83) and the
  436. * 'Unit Serial Number' VPD page (0x80) to a SCSI device
  437. * structure. This information can be used to identify the device
  438. * uniquely.
  439. */
  440. void scsi_attach_vpd(struct scsi_device *sdev)
  441. {
  442. int i;
  443. struct scsi_vpd *vpd_buf;
  444. if (!scsi_device_supports_vpd(sdev))
  445. return;
  446. /* Ask for all the pages supported by this device */
  447. vpd_buf = scsi_get_vpd_buf(sdev, 0);
  448. if (!vpd_buf)
  449. return;
  450. for (i = 4; i < vpd_buf->len; i++) {
  451. if (vpd_buf->data[i] == 0x0)
  452. scsi_update_vpd_page(sdev, 0x0, &sdev->vpd_pg0);
  453. if (vpd_buf->data[i] == 0x80)
  454. scsi_update_vpd_page(sdev, 0x80, &sdev->vpd_pg80);
  455. if (vpd_buf->data[i] == 0x83)
  456. scsi_update_vpd_page(sdev, 0x83, &sdev->vpd_pg83);
  457. if (vpd_buf->data[i] == 0x89)
  458. scsi_update_vpd_page(sdev, 0x89, &sdev->vpd_pg89);
  459. if (vpd_buf->data[i] == 0xb0)
  460. scsi_update_vpd_page(sdev, 0xb0, &sdev->vpd_pgb0);
  461. if (vpd_buf->data[i] == 0xb1)
  462. scsi_update_vpd_page(sdev, 0xb1, &sdev->vpd_pgb1);
  463. if (vpd_buf->data[i] == 0xb2)
  464. scsi_update_vpd_page(sdev, 0xb2, &sdev->vpd_pgb2);
  465. if (vpd_buf->data[i] == 0xb7)
  466. scsi_update_vpd_page(sdev, 0xb7, &sdev->vpd_pgb7);
  467. }
  468. kfree(vpd_buf);
  469. }
  470. /**
  471. * scsi_report_opcode - Find out if a given command is supported
  472. * @sdev: scsi device to query
  473. * @buffer: scratch buffer (must be at least 20 bytes long)
  474. * @len: length of buffer
  475. * @opcode: opcode for the command to look up
  476. * @sa: service action for the command to look up
  477. *
  478. * Uses the REPORT SUPPORTED OPERATION CODES to check support for the
  479. * command identified with @opcode and @sa. If the command does not
  480. * have a service action, @sa must be 0. Returns -EINVAL if RSOC fails,
  481. * 0 if the command is not supported and 1 if the device claims to
  482. * support the command.
  483. */
  484. int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
  485. unsigned int len, unsigned char opcode,
  486. unsigned short sa)
  487. {
  488. unsigned char cmd[16];
  489. struct scsi_sense_hdr sshdr;
  490. int result, request_len;
  491. const struct scsi_exec_args exec_args = {
  492. .sshdr = &sshdr,
  493. };
  494. if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3)
  495. return -EINVAL;
  496. /* RSOC header + size of command we are asking about */
  497. request_len = 4 + COMMAND_SIZE(opcode);
  498. if (request_len > len) {
  499. dev_warn_once(&sdev->sdev_gendev,
  500. "%s: len %u bytes, opcode 0x%02x needs %u\n",
  501. __func__, len, opcode, request_len);
  502. return -EINVAL;
  503. }
  504. memset(cmd, 0, 16);
  505. cmd[0] = MAINTENANCE_IN;
  506. cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES;
  507. if (!sa) {
  508. cmd[2] = 1; /* One command format */
  509. cmd[3] = opcode;
  510. } else {
  511. cmd[2] = 3; /* One command format with service action */
  512. cmd[3] = opcode;
  513. put_unaligned_be16(sa, &cmd[4]);
  514. }
  515. put_unaligned_be32(request_len, &cmd[6]);
  516. memset(buffer, 0, len);
  517. result = scsi_execute_cmd(sdev, cmd, REQ_OP_DRV_IN, buffer,
  518. request_len, 30 * HZ, 3, &exec_args);
  519. if (result < 0)
  520. return result;
  521. if (result && scsi_sense_valid(&sshdr) &&
  522. sshdr.sense_key == ILLEGAL_REQUEST &&
  523. (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00)
  524. return -EINVAL;
  525. if ((buffer[1] & 3) == 3) /* Command supported */
  526. return 1;
  527. return 0;
  528. }
  529. EXPORT_SYMBOL(scsi_report_opcode);
  530. #define SCSI_CDL_CHECK_BUF_LEN 64
  531. static bool scsi_cdl_check_cmd(struct scsi_device *sdev, u8 opcode, u16 sa,
  532. unsigned char *buf)
  533. {
  534. int ret;
  535. u8 cdlp;
  536. /* Check operation code */
  537. ret = scsi_report_opcode(sdev, buf, SCSI_CDL_CHECK_BUF_LEN, opcode, sa);
  538. if (ret <= 0)
  539. return false;
  540. if ((buf[1] & 0x03) != 0x03)
  541. return false;
  542. /*
  543. * See SPC-6, One_command parameter data format for
  544. * REPORT SUPPORTED OPERATION CODES. We have the following cases
  545. * depending on rwcdlp (buf[0] & 0x01) value:
  546. * - rwcdlp == 0: then cdlp indicates support for the A mode page when
  547. * it is equal to 1 and for the B mode page when it is
  548. * equal to 2.
  549. * - rwcdlp == 1: then cdlp indicates support for the T2A mode page
  550. * when it is equal to 1 and for the T2B mode page when
  551. * it is equal to 2.
  552. * Overall, to detect support for command duration limits, we only need
  553. * to check that cdlp is 1 or 2.
  554. */
  555. cdlp = (buf[1] & 0x18) >> 3;
  556. return cdlp == 0x01 || cdlp == 0x02;
  557. }
  558. /**
  559. * scsi_cdl_check - Check if a SCSI device supports Command Duration Limits
  560. * @sdev: The device to check
  561. */
  562. void scsi_cdl_check(struct scsi_device *sdev)
  563. {
  564. bool cdl_supported;
  565. unsigned char *buf;
  566. /*
  567. * Support for CDL was defined in SPC-5. Ignore devices reporting an
  568. * lower SPC version. This also avoids problems with old drives choking
  569. * on MAINTENANCE_IN / MI_REPORT_SUPPORTED_OPERATION_CODES with a
  570. * service action specified, as done in scsi_cdl_check_cmd().
  571. */
  572. if (sdev->scsi_level < SCSI_SPC_5) {
  573. sdev->cdl_supported = 0;
  574. return;
  575. }
  576. buf = kmalloc(SCSI_CDL_CHECK_BUF_LEN, GFP_KERNEL);
  577. if (!buf) {
  578. sdev->cdl_supported = 0;
  579. return;
  580. }
  581. /* Check support for READ_16, WRITE_16, READ_32 and WRITE_32 commands */
  582. cdl_supported =
  583. scsi_cdl_check_cmd(sdev, READ_16, 0, buf) ||
  584. scsi_cdl_check_cmd(sdev, WRITE_16, 0, buf) ||
  585. scsi_cdl_check_cmd(sdev, VARIABLE_LENGTH_CMD, READ_32, buf) ||
  586. scsi_cdl_check_cmd(sdev, VARIABLE_LENGTH_CMD, WRITE_32, buf);
  587. if (cdl_supported) {
  588. /*
  589. * We have CDL support: force the use of READ16/WRITE16.
  590. * READ32 and WRITE32 will be used for devices that support
  591. * the T10_PI_TYPE2_PROTECTION protection type.
  592. */
  593. sdev->use_16_for_rw = 1;
  594. sdev->use_10_for_rw = 0;
  595. sdev->cdl_supported = 1;
  596. /*
  597. * If the device supports CDL, make sure that the current drive
  598. * feature status is consistent with the user controlled
  599. * cdl_enable state.
  600. */
  601. scsi_cdl_enable(sdev, sdev->cdl_enable);
  602. } else {
  603. sdev->cdl_supported = 0;
  604. }
  605. kfree(buf);
  606. }
  607. /**
  608. * scsi_cdl_enable - Enable or disable a SCSI device supports for Command
  609. * Duration Limits
  610. * @sdev: The target device
  611. * @enable: the target state
  612. */
  613. int scsi_cdl_enable(struct scsi_device *sdev, bool enable)
  614. {
  615. struct scsi_mode_data data;
  616. struct scsi_sense_hdr sshdr;
  617. struct scsi_vpd *vpd;
  618. bool is_ata = false;
  619. char buf[64];
  620. int ret;
  621. if (!sdev->cdl_supported)
  622. return -EOPNOTSUPP;
  623. rcu_read_lock();
  624. vpd = rcu_dereference(sdev->vpd_pg89);
  625. if (vpd)
  626. is_ata = true;
  627. rcu_read_unlock();
  628. /*
  629. * For ATA devices, CDL needs to be enabled with a SET FEATURES command.
  630. */
  631. if (is_ata) {
  632. char *buf_data;
  633. int len;
  634. ret = scsi_mode_sense(sdev, 0x08, 0x0a, 0xf2, buf, sizeof(buf),
  635. 5 * HZ, 3, &data, NULL);
  636. if (ret)
  637. return -EINVAL;
  638. /* Enable CDL using the ATA feature page */
  639. len = min_t(size_t, sizeof(buf),
  640. data.length - data.header_length -
  641. data.block_descriptor_length);
  642. buf_data = buf + data.header_length +
  643. data.block_descriptor_length;
  644. if (enable)
  645. buf_data[4] = 0x02;
  646. else
  647. buf_data[4] = 0;
  648. ret = scsi_mode_select(sdev, 1, 0, buf_data, len, 5 * HZ, 3,
  649. &data, &sshdr);
  650. if (ret) {
  651. if (ret > 0 && scsi_sense_valid(&sshdr))
  652. scsi_print_sense_hdr(sdev,
  653. dev_name(&sdev->sdev_gendev), &sshdr);
  654. return ret;
  655. }
  656. }
  657. sdev->cdl_enable = enable;
  658. return 0;
  659. }
  660. /**
  661. * scsi_device_get - get an additional reference to a scsi_device
  662. * @sdev: device to get a reference to
  663. *
  664. * Description: Gets a reference to the scsi_device and increments the use count
  665. * of the underlying LLDD module. You must hold host_lock of the
  666. * parent Scsi_Host or already have a reference when calling this.
  667. *
  668. * This will fail if a device is deleted or cancelled, or when the LLD module
  669. * is in the process of being unloaded.
  670. */
  671. int scsi_device_get(struct scsi_device *sdev)
  672. {
  673. if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
  674. goto fail;
  675. if (!try_module_get(sdev->host->hostt->module))
  676. goto fail;
  677. if (!get_device(&sdev->sdev_gendev))
  678. goto fail_put_module;
  679. return 0;
  680. fail_put_module:
  681. module_put(sdev->host->hostt->module);
  682. fail:
  683. return -ENXIO;
  684. }
  685. EXPORT_SYMBOL(scsi_device_get);
  686. /**
  687. * scsi_device_put - release a reference to a scsi_device
  688. * @sdev: device to release a reference on.
  689. *
  690. * Description: Release a reference to the scsi_device and decrements the use
  691. * count of the underlying LLDD module. The device is freed once the last
  692. * user vanishes.
  693. */
  694. void scsi_device_put(struct scsi_device *sdev)
  695. {
  696. struct module *mod = sdev->host->hostt->module;
  697. put_device(&sdev->sdev_gendev);
  698. module_put(mod);
  699. }
  700. EXPORT_SYMBOL(scsi_device_put);
  701. /* helper for shost_for_each_device, see that for documentation */
  702. struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
  703. struct scsi_device *prev)
  704. {
  705. struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
  706. struct scsi_device *next = NULL;
  707. unsigned long flags;
  708. spin_lock_irqsave(shost->host_lock, flags);
  709. while (list->next != &shost->__devices) {
  710. next = list_entry(list->next, struct scsi_device, siblings);
  711. /* skip devices that we can't get a reference to */
  712. if (!scsi_device_get(next))
  713. break;
  714. next = NULL;
  715. list = list->next;
  716. }
  717. spin_unlock_irqrestore(shost->host_lock, flags);
  718. if (prev)
  719. scsi_device_put(prev);
  720. return next;
  721. }
  722. EXPORT_SYMBOL(__scsi_iterate_devices);
  723. /**
  724. * starget_for_each_device - helper to walk all devices of a target
  725. * @starget: target whose devices we want to iterate over.
  726. * @data: Opaque passed to each function call.
  727. * @fn: Function to call on each device
  728. *
  729. * This traverses over each device of @starget. The devices have
  730. * a reference that must be released by scsi_host_put when breaking
  731. * out of the loop.
  732. */
  733. void starget_for_each_device(struct scsi_target *starget, void *data,
  734. void (*fn)(struct scsi_device *, void *))
  735. {
  736. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  737. struct scsi_device *sdev;
  738. shost_for_each_device(sdev, shost) {
  739. if ((sdev->channel == starget->channel) &&
  740. (sdev->id == starget->id))
  741. fn(sdev, data);
  742. }
  743. }
  744. EXPORT_SYMBOL(starget_for_each_device);
  745. /**
  746. * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
  747. * @starget: target whose devices we want to iterate over.
  748. * @data: parameter for callback @fn()
  749. * @fn: callback function that is invoked for each device
  750. *
  751. * This traverses over each device of @starget. It does _not_
  752. * take a reference on the scsi_device, so the whole loop must be
  753. * protected by shost->host_lock.
  754. *
  755. * Note: The only reason why drivers would want to use this is because
  756. * they need to access the device list in irq context. Otherwise you
  757. * really want to use starget_for_each_device instead.
  758. **/
  759. void __starget_for_each_device(struct scsi_target *starget, void *data,
  760. void (*fn)(struct scsi_device *, void *))
  761. {
  762. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  763. struct scsi_device *sdev;
  764. __shost_for_each_device(sdev, shost) {
  765. if ((sdev->channel == starget->channel) &&
  766. (sdev->id == starget->id))
  767. fn(sdev, data);
  768. }
  769. }
  770. EXPORT_SYMBOL(__starget_for_each_device);
  771. /**
  772. * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
  773. * @starget: SCSI target pointer
  774. * @lun: SCSI Logical Unit Number
  775. *
  776. * Description: Looks up the scsi_device with the specified @lun for a given
  777. * @starget. The returned scsi_device does not have an additional
  778. * reference. You must hold the host's host_lock over this call and
  779. * any access to the returned scsi_device. A scsi_device in state
  780. * SDEV_DEL is skipped.
  781. *
  782. * Note: The only reason why drivers should use this is because
  783. * they need to access the device list in irq context. Otherwise you
  784. * really want to use scsi_device_lookup_by_target instead.
  785. **/
  786. struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
  787. u64 lun)
  788. {
  789. struct scsi_device *sdev;
  790. list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
  791. if (sdev->sdev_state == SDEV_DEL)
  792. continue;
  793. if (sdev->lun ==lun)
  794. return sdev;
  795. }
  796. return NULL;
  797. }
  798. EXPORT_SYMBOL(__scsi_device_lookup_by_target);
  799. /**
  800. * scsi_device_lookup_by_target - find a device given the target
  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 has an additional reference that
  806. * needs to be released with scsi_device_put once you're done with it.
  807. **/
  808. struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
  809. u64 lun)
  810. {
  811. struct scsi_device *sdev;
  812. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  813. unsigned long flags;
  814. spin_lock_irqsave(shost->host_lock, flags);
  815. sdev = __scsi_device_lookup_by_target(starget, lun);
  816. if (sdev && scsi_device_get(sdev))
  817. sdev = NULL;
  818. spin_unlock_irqrestore(shost->host_lock, flags);
  819. return sdev;
  820. }
  821. EXPORT_SYMBOL(scsi_device_lookup_by_target);
  822. /**
  823. * __scsi_device_lookup - find a device given the host (UNLOCKED)
  824. * @shost: SCSI host pointer
  825. * @channel: SCSI channel (zero if only one channel)
  826. * @id: SCSI target number (physical unit number)
  827. * @lun: SCSI Logical Unit Number
  828. *
  829. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  830. * for a given host. The returned scsi_device does not have an additional
  831. * reference. You must hold the host's host_lock over this call and any access
  832. * to the returned scsi_device.
  833. *
  834. * Note: The only reason why drivers would want to use this is because
  835. * they need to access the device list in irq context. Otherwise you
  836. * really want to use scsi_device_lookup instead.
  837. **/
  838. struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
  839. uint channel, uint id, u64 lun)
  840. {
  841. struct scsi_device *sdev;
  842. list_for_each_entry(sdev, &shost->__devices, siblings) {
  843. if (sdev->sdev_state == SDEV_DEL)
  844. continue;
  845. if (sdev->channel == channel && sdev->id == id &&
  846. sdev->lun ==lun)
  847. return sdev;
  848. }
  849. return NULL;
  850. }
  851. EXPORT_SYMBOL(__scsi_device_lookup);
  852. /**
  853. * scsi_device_lookup - find a device given the host
  854. * @shost: SCSI host pointer
  855. * @channel: SCSI channel (zero if only one channel)
  856. * @id: SCSI target number (physical unit number)
  857. * @lun: SCSI Logical Unit Number
  858. *
  859. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  860. * for a given host. The returned scsi_device has an additional reference that
  861. * needs to be released with scsi_device_put once you're done with it.
  862. **/
  863. struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
  864. uint channel, uint id, u64 lun)
  865. {
  866. struct scsi_device *sdev;
  867. unsigned long flags;
  868. spin_lock_irqsave(shost->host_lock, flags);
  869. sdev = __scsi_device_lookup(shost, channel, id, lun);
  870. if (sdev && scsi_device_get(sdev))
  871. sdev = NULL;
  872. spin_unlock_irqrestore(shost->host_lock, flags);
  873. return sdev;
  874. }
  875. EXPORT_SYMBOL(scsi_device_lookup);
  876. MODULE_DESCRIPTION("SCSI core");
  877. MODULE_LICENSE("GPL");
  878. module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
  879. MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
  880. static int __init init_scsi(void)
  881. {
  882. int error;
  883. error = scsi_init_procfs();
  884. if (error)
  885. goto cleanup_queue;
  886. error = scsi_init_devinfo();
  887. if (error)
  888. goto cleanup_procfs;
  889. error = scsi_init_hosts();
  890. if (error)
  891. goto cleanup_devlist;
  892. error = scsi_init_sysctl();
  893. if (error)
  894. goto cleanup_hosts;
  895. error = scsi_sysfs_register();
  896. if (error)
  897. goto cleanup_sysctl;
  898. scsi_netlink_init();
  899. printk(KERN_NOTICE "SCSI subsystem initialized\n");
  900. return 0;
  901. cleanup_sysctl:
  902. scsi_exit_sysctl();
  903. cleanup_hosts:
  904. scsi_exit_hosts();
  905. cleanup_devlist:
  906. scsi_exit_devinfo();
  907. cleanup_procfs:
  908. scsi_exit_procfs();
  909. cleanup_queue:
  910. scsi_exit_queue();
  911. printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
  912. -error);
  913. return error;
  914. }
  915. static void __exit exit_scsi(void)
  916. {
  917. scsi_netlink_exit();
  918. scsi_sysfs_unregister();
  919. scsi_exit_sysctl();
  920. scsi_exit_hosts();
  921. scsi_exit_devinfo();
  922. scsi_exit_procfs();
  923. scsi_exit_queue();
  924. }
  925. subsys_initcall(init_scsi);
  926. module_exit(exit_scsi);