/drivers/scsi/scsi_transport_spi.c

http://github.com/mirrors/linux · C · 1629 lines · 1249 code · 234 blank · 146 comment · 259 complexity · 4377a0b09b606b63d9073598e7136536 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
  4. *
  5. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  6. * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
  7. */
  8. #include <linux/ctype.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/mutex.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/slab.h>
  16. #include <linux/suspend.h>
  17. #include <scsi/scsi.h>
  18. #include "scsi_priv.h"
  19. #include <scsi/scsi_device.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi_cmnd.h>
  22. #include <scsi/scsi_eh.h>
  23. #include <scsi/scsi_tcq.h>
  24. #include <scsi/scsi_transport.h>
  25. #include <scsi/scsi_transport_spi.h>
  26. #define SPI_NUM_ATTRS 14 /* increase this if you add attributes */
  27. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  28. * on" attributes */
  29. #define SPI_HOST_ATTRS 1
  30. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  31. #define DV_LOOPS 3
  32. #define DV_TIMEOUT (10*HZ)
  33. #define DV_RETRIES 3 /* should only need at most
  34. * two cc/ua clears */
  35. /* Our blacklist flags */
  36. enum {
  37. SPI_BLIST_NOIUS = (__force blist_flags_t)0x1,
  38. };
  39. /* blacklist table, modelled on scsi_devinfo.c */
  40. static struct {
  41. char *vendor;
  42. char *model;
  43. blist_flags_t flags;
  44. } spi_static_device_list[] __initdata = {
  45. {"HP", "Ultrium 3-SCSI", SPI_BLIST_NOIUS },
  46. {"IBM", "ULTRIUM-TD3", SPI_BLIST_NOIUS },
  47. {NULL, NULL, 0}
  48. };
  49. /* Private data accessors (keep these out of the header file) */
  50. #define spi_dv_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
  51. #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
  52. struct spi_internal {
  53. struct scsi_transport_template t;
  54. struct spi_function_template *f;
  55. };
  56. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  57. static const int ppr_to_ps[] = {
  58. /* The PPR values 0-6 are reserved, fill them in when
  59. * the committee defines them */
  60. -1, /* 0x00 */
  61. -1, /* 0x01 */
  62. -1, /* 0x02 */
  63. -1, /* 0x03 */
  64. -1, /* 0x04 */
  65. -1, /* 0x05 */
  66. -1, /* 0x06 */
  67. 3125, /* 0x07 */
  68. 6250, /* 0x08 */
  69. 12500, /* 0x09 */
  70. 25000, /* 0x0a */
  71. 30300, /* 0x0b */
  72. 50000, /* 0x0c */
  73. };
  74. /* The PPR values at which you calculate the period in ns by multiplying
  75. * by 4 */
  76. #define SPI_STATIC_PPR 0x0c
  77. static int sprint_frac(char *dest, int value, int denom)
  78. {
  79. int frac = value % denom;
  80. int result = sprintf(dest, "%d", value / denom);
  81. if (frac == 0)
  82. return result;
  83. dest[result++] = '.';
  84. do {
  85. denom /= 10;
  86. sprintf(dest + result, "%d", frac / denom);
  87. result++;
  88. frac %= denom;
  89. } while (frac);
  90. dest[result++] = '\0';
  91. return result;
  92. }
  93. static int spi_execute(struct scsi_device *sdev, const void *cmd,
  94. enum dma_data_direction dir,
  95. void *buffer, unsigned bufflen,
  96. struct scsi_sense_hdr *sshdr)
  97. {
  98. int i, result;
  99. unsigned char sense[SCSI_SENSE_BUFFERSIZE];
  100. struct scsi_sense_hdr sshdr_tmp;
  101. if (!sshdr)
  102. sshdr = &sshdr_tmp;
  103. for(i = 0; i < DV_RETRIES; i++) {
  104. result = scsi_execute(sdev, cmd, dir, buffer, bufflen, sense,
  105. sshdr, DV_TIMEOUT, /* retries */ 1,
  106. REQ_FAILFAST_DEV |
  107. REQ_FAILFAST_TRANSPORT |
  108. REQ_FAILFAST_DRIVER,
  109. 0, NULL);
  110. if (driver_byte(result) != DRIVER_SENSE ||
  111. sshdr->sense_key != UNIT_ATTENTION)
  112. break;
  113. }
  114. return result;
  115. }
  116. static struct {
  117. enum spi_signal_type value;
  118. char *name;
  119. } signal_types[] = {
  120. { SPI_SIGNAL_UNKNOWN, "unknown" },
  121. { SPI_SIGNAL_SE, "SE" },
  122. { SPI_SIGNAL_LVD, "LVD" },
  123. { SPI_SIGNAL_HVD, "HVD" },
  124. };
  125. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  126. {
  127. int i;
  128. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  129. if (type == signal_types[i].value)
  130. return signal_types[i].name;
  131. }
  132. return NULL;
  133. }
  134. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  135. {
  136. int i, len;
  137. for (i = 0; i < ARRAY_SIZE(signal_types); i++) {
  138. len = strlen(signal_types[i].name);
  139. if (strncmp(name, signal_types[i].name, len) == 0 &&
  140. (name[len] == '\n' || name[len] == '\0'))
  141. return signal_types[i].value;
  142. }
  143. return SPI_SIGNAL_UNKNOWN;
  144. }
  145. static int spi_host_setup(struct transport_container *tc, struct device *dev,
  146. struct device *cdev)
  147. {
  148. struct Scsi_Host *shost = dev_to_shost(dev);
  149. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  150. return 0;
  151. }
  152. static int spi_host_configure(struct transport_container *tc,
  153. struct device *dev,
  154. struct device *cdev);
  155. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  156. "spi_host",
  157. spi_host_setup,
  158. NULL,
  159. spi_host_configure);
  160. static int spi_host_match(struct attribute_container *cont,
  161. struct device *dev)
  162. {
  163. struct Scsi_Host *shost;
  164. if (!scsi_is_host_device(dev))
  165. return 0;
  166. shost = dev_to_shost(dev);
  167. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  168. != &spi_host_class.class)
  169. return 0;
  170. return &shost->transportt->host_attrs.ac == cont;
  171. }
  172. static int spi_target_configure(struct transport_container *tc,
  173. struct device *dev,
  174. struct device *cdev);
  175. static int spi_device_configure(struct transport_container *tc,
  176. struct device *dev,
  177. struct device *cdev)
  178. {
  179. struct scsi_device *sdev = to_scsi_device(dev);
  180. struct scsi_target *starget = sdev->sdev_target;
  181. blist_flags_t bflags;
  182. bflags = scsi_get_device_flags_keyed(sdev, &sdev->inquiry[8],
  183. &sdev->inquiry[16],
  184. SCSI_DEVINFO_SPI);
  185. /* Populate the target capability fields with the values
  186. * gleaned from the device inquiry */
  187. spi_support_sync(starget) = scsi_device_sync(sdev);
  188. spi_support_wide(starget) = scsi_device_wide(sdev);
  189. spi_support_dt(starget) = scsi_device_dt(sdev);
  190. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  191. spi_support_ius(starget) = scsi_device_ius(sdev);
  192. if (bflags & SPI_BLIST_NOIUS) {
  193. dev_info(dev, "Information Units disabled by blacklist\n");
  194. spi_support_ius(starget) = 0;
  195. }
  196. spi_support_qas(starget) = scsi_device_qas(sdev);
  197. return 0;
  198. }
  199. static int spi_setup_transport_attrs(struct transport_container *tc,
  200. struct device *dev,
  201. struct device *cdev)
  202. {
  203. struct scsi_target *starget = to_scsi_target(dev);
  204. spi_period(starget) = -1; /* illegal value */
  205. spi_min_period(starget) = 0;
  206. spi_offset(starget) = 0; /* async */
  207. spi_max_offset(starget) = 255;
  208. spi_width(starget) = 0; /* narrow */
  209. spi_max_width(starget) = 1;
  210. spi_iu(starget) = 0; /* no IU */
  211. spi_max_iu(starget) = 1;
  212. spi_dt(starget) = 0; /* ST */
  213. spi_qas(starget) = 0;
  214. spi_max_qas(starget) = 1;
  215. spi_wr_flow(starget) = 0;
  216. spi_rd_strm(starget) = 0;
  217. spi_rti(starget) = 0;
  218. spi_pcomp_en(starget) = 0;
  219. spi_hold_mcs(starget) = 0;
  220. spi_dv_pending(starget) = 0;
  221. spi_dv_in_progress(starget) = 0;
  222. spi_initial_dv(starget) = 0;
  223. mutex_init(&spi_dv_mutex(starget));
  224. return 0;
  225. }
  226. #define spi_transport_show_simple(field, format_string) \
  227. \
  228. static ssize_t \
  229. show_spi_transport_##field(struct device *dev, \
  230. struct device_attribute *attr, char *buf) \
  231. { \
  232. struct scsi_target *starget = transport_class_to_starget(dev); \
  233. struct spi_transport_attrs *tp; \
  234. \
  235. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  236. return snprintf(buf, 20, format_string, tp->field); \
  237. }
  238. #define spi_transport_store_simple(field, format_string) \
  239. \
  240. static ssize_t \
  241. store_spi_transport_##field(struct device *dev, \
  242. struct device_attribute *attr, \
  243. const char *buf, size_t count) \
  244. { \
  245. int val; \
  246. struct scsi_target *starget = transport_class_to_starget(dev); \
  247. struct spi_transport_attrs *tp; \
  248. \
  249. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  250. val = simple_strtoul(buf, NULL, 0); \
  251. tp->field = val; \
  252. return count; \
  253. }
  254. #define spi_transport_show_function(field, format_string) \
  255. \
  256. static ssize_t \
  257. show_spi_transport_##field(struct device *dev, \
  258. struct device_attribute *attr, char *buf) \
  259. { \
  260. struct scsi_target *starget = transport_class_to_starget(dev); \
  261. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  262. struct spi_transport_attrs *tp; \
  263. struct spi_internal *i = to_spi_internal(shost->transportt); \
  264. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  265. if (i->f->get_##field) \
  266. i->f->get_##field(starget); \
  267. return snprintf(buf, 20, format_string, tp->field); \
  268. }
  269. #define spi_transport_store_function(field, format_string) \
  270. static ssize_t \
  271. store_spi_transport_##field(struct device *dev, \
  272. struct device_attribute *attr, \
  273. const char *buf, size_t count) \
  274. { \
  275. int val; \
  276. struct scsi_target *starget = transport_class_to_starget(dev); \
  277. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  278. struct spi_internal *i = to_spi_internal(shost->transportt); \
  279. \
  280. if (!i->f->set_##field) \
  281. return -EINVAL; \
  282. val = simple_strtoul(buf, NULL, 0); \
  283. i->f->set_##field(starget, val); \
  284. return count; \
  285. }
  286. #define spi_transport_store_max(field, format_string) \
  287. static ssize_t \
  288. store_spi_transport_##field(struct device *dev, \
  289. struct device_attribute *attr, \
  290. const char *buf, size_t count) \
  291. { \
  292. int val; \
  293. struct scsi_target *starget = transport_class_to_starget(dev); \
  294. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  295. struct spi_internal *i = to_spi_internal(shost->transportt); \
  296. struct spi_transport_attrs *tp \
  297. = (struct spi_transport_attrs *)&starget->starget_data; \
  298. \
  299. if (i->f->set_##field) \
  300. return -EINVAL; \
  301. val = simple_strtoul(buf, NULL, 0); \
  302. if (val > tp->max_##field) \
  303. val = tp->max_##field; \
  304. i->f->set_##field(starget, val); \
  305. return count; \
  306. }
  307. #define spi_transport_rd_attr(field, format_string) \
  308. spi_transport_show_function(field, format_string) \
  309. spi_transport_store_function(field, format_string) \
  310. static DEVICE_ATTR(field, S_IRUGO, \
  311. show_spi_transport_##field, \
  312. store_spi_transport_##field);
  313. #define spi_transport_simple_attr(field, format_string) \
  314. spi_transport_show_simple(field, format_string) \
  315. spi_transport_store_simple(field, format_string) \
  316. static DEVICE_ATTR(field, S_IRUGO, \
  317. show_spi_transport_##field, \
  318. store_spi_transport_##field);
  319. #define spi_transport_max_attr(field, format_string) \
  320. spi_transport_show_function(field, format_string) \
  321. spi_transport_store_max(field, format_string) \
  322. spi_transport_simple_attr(max_##field, format_string) \
  323. static DEVICE_ATTR(field, S_IRUGO, \
  324. show_spi_transport_##field, \
  325. store_spi_transport_##field);
  326. /* The Parallel SCSI Tranport Attributes: */
  327. spi_transport_max_attr(offset, "%d\n");
  328. spi_transport_max_attr(width, "%d\n");
  329. spi_transport_max_attr(iu, "%d\n");
  330. spi_transport_rd_attr(dt, "%d\n");
  331. spi_transport_max_attr(qas, "%d\n");
  332. spi_transport_rd_attr(wr_flow, "%d\n");
  333. spi_transport_rd_attr(rd_strm, "%d\n");
  334. spi_transport_rd_attr(rti, "%d\n");
  335. spi_transport_rd_attr(pcomp_en, "%d\n");
  336. spi_transport_rd_attr(hold_mcs, "%d\n");
  337. /* we only care about the first child device that's a real SCSI device
  338. * so we return 1 to terminate the iteration when we find it */
  339. static int child_iter(struct device *dev, void *data)
  340. {
  341. if (!scsi_is_sdev_device(dev))
  342. return 0;
  343. spi_dv_device(to_scsi_device(dev));
  344. return 1;
  345. }
  346. static ssize_t
  347. store_spi_revalidate(struct device *dev, struct device_attribute *attr,
  348. const char *buf, size_t count)
  349. {
  350. struct scsi_target *starget = transport_class_to_starget(dev);
  351. device_for_each_child(&starget->dev, NULL, child_iter);
  352. return count;
  353. }
  354. static DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  355. /* Translate the period into ns according to the current spec
  356. * for SDTR/PPR messages */
  357. static int period_to_str(char *buf, int period)
  358. {
  359. int len, picosec;
  360. if (period < 0 || period > 0xff) {
  361. picosec = -1;
  362. } else if (period <= SPI_STATIC_PPR) {
  363. picosec = ppr_to_ps[period];
  364. } else {
  365. picosec = period * 4000;
  366. }
  367. if (picosec == -1) {
  368. len = sprintf(buf, "reserved");
  369. } else {
  370. len = sprint_frac(buf, picosec, 1000);
  371. }
  372. return len;
  373. }
  374. static ssize_t
  375. show_spi_transport_period_helper(char *buf, int period)
  376. {
  377. int len = period_to_str(buf, period);
  378. buf[len++] = '\n';
  379. buf[len] = '\0';
  380. return len;
  381. }
  382. static ssize_t
  383. store_spi_transport_period_helper(struct device *dev, const char *buf,
  384. size_t count, int *periodp)
  385. {
  386. int j, picosec, period = -1;
  387. char *endp;
  388. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  389. if (*endp == '.') {
  390. int mult = 100;
  391. do {
  392. endp++;
  393. if (!isdigit(*endp))
  394. break;
  395. picosec += (*endp - '0') * mult;
  396. mult /= 10;
  397. } while (mult > 0);
  398. }
  399. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  400. if (ppr_to_ps[j] < picosec)
  401. continue;
  402. period = j;
  403. break;
  404. }
  405. if (period == -1)
  406. period = picosec / 4000;
  407. if (period > 0xff)
  408. period = 0xff;
  409. *periodp = period;
  410. return count;
  411. }
  412. static ssize_t
  413. show_spi_transport_period(struct device *dev,
  414. struct device_attribute *attr, char *buf)
  415. {
  416. struct scsi_target *starget = transport_class_to_starget(dev);
  417. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  418. struct spi_internal *i = to_spi_internal(shost->transportt);
  419. struct spi_transport_attrs *tp =
  420. (struct spi_transport_attrs *)&starget->starget_data;
  421. if (i->f->get_period)
  422. i->f->get_period(starget);
  423. return show_spi_transport_period_helper(buf, tp->period);
  424. }
  425. static ssize_t
  426. store_spi_transport_period(struct device *cdev, struct device_attribute *attr,
  427. const char *buf, size_t count)
  428. {
  429. struct scsi_target *starget = transport_class_to_starget(cdev);
  430. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  431. struct spi_internal *i = to_spi_internal(shost->transportt);
  432. struct spi_transport_attrs *tp =
  433. (struct spi_transport_attrs *)&starget->starget_data;
  434. int period, retval;
  435. if (!i->f->set_period)
  436. return -EINVAL;
  437. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  438. if (period < tp->min_period)
  439. period = tp->min_period;
  440. i->f->set_period(starget, period);
  441. return retval;
  442. }
  443. static DEVICE_ATTR(period, S_IRUGO,
  444. show_spi_transport_period,
  445. store_spi_transport_period);
  446. static ssize_t
  447. show_spi_transport_min_period(struct device *cdev,
  448. struct device_attribute *attr, char *buf)
  449. {
  450. struct scsi_target *starget = transport_class_to_starget(cdev);
  451. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  452. struct spi_internal *i = to_spi_internal(shost->transportt);
  453. struct spi_transport_attrs *tp =
  454. (struct spi_transport_attrs *)&starget->starget_data;
  455. if (!i->f->set_period)
  456. return -EINVAL;
  457. return show_spi_transport_period_helper(buf, tp->min_period);
  458. }
  459. static ssize_t
  460. store_spi_transport_min_period(struct device *cdev,
  461. struct device_attribute *attr,
  462. const char *buf, size_t count)
  463. {
  464. struct scsi_target *starget = transport_class_to_starget(cdev);
  465. struct spi_transport_attrs *tp =
  466. (struct spi_transport_attrs *)&starget->starget_data;
  467. return store_spi_transport_period_helper(cdev, buf, count,
  468. &tp->min_period);
  469. }
  470. static DEVICE_ATTR(min_period, S_IRUGO,
  471. show_spi_transport_min_period,
  472. store_spi_transport_min_period);
  473. static ssize_t show_spi_host_signalling(struct device *cdev,
  474. struct device_attribute *attr,
  475. char *buf)
  476. {
  477. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  478. struct spi_internal *i = to_spi_internal(shost->transportt);
  479. if (i->f->get_signalling)
  480. i->f->get_signalling(shost);
  481. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  482. }
  483. static ssize_t store_spi_host_signalling(struct device *dev,
  484. struct device_attribute *attr,
  485. const char *buf, size_t count)
  486. {
  487. struct Scsi_Host *shost = transport_class_to_shost(dev);
  488. struct spi_internal *i = to_spi_internal(shost->transportt);
  489. enum spi_signal_type type = spi_signal_to_value(buf);
  490. if (!i->f->set_signalling)
  491. return -EINVAL;
  492. if (type != SPI_SIGNAL_UNKNOWN)
  493. i->f->set_signalling(shost, type);
  494. return count;
  495. }
  496. static DEVICE_ATTR(signalling, S_IRUGO,
  497. show_spi_host_signalling,
  498. store_spi_host_signalling);
  499. static ssize_t show_spi_host_width(struct device *cdev,
  500. struct device_attribute *attr,
  501. char *buf)
  502. {
  503. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  504. return sprintf(buf, "%s\n", shost->max_id == 16 ? "wide" : "narrow");
  505. }
  506. static DEVICE_ATTR(host_width, S_IRUGO,
  507. show_spi_host_width, NULL);
  508. static ssize_t show_spi_host_hba_id(struct device *cdev,
  509. struct device_attribute *attr,
  510. char *buf)
  511. {
  512. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  513. return sprintf(buf, "%d\n", shost->this_id);
  514. }
  515. static DEVICE_ATTR(hba_id, S_IRUGO,
  516. show_spi_host_hba_id, NULL);
  517. #define DV_SET(x, y) \
  518. if(i->f->set_##x) \
  519. i->f->set_##x(sdev->sdev_target, y)
  520. enum spi_compare_returns {
  521. SPI_COMPARE_SUCCESS,
  522. SPI_COMPARE_FAILURE,
  523. SPI_COMPARE_SKIP_TEST,
  524. };
  525. /* This is for read/write Domain Validation: If the device supports
  526. * an echo buffer, we do read/write tests to it */
  527. static enum spi_compare_returns
  528. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  529. u8 *ptr, const int retries)
  530. {
  531. int len = ptr - buffer;
  532. int j, k, r, result;
  533. unsigned int pattern = 0x0000ffff;
  534. struct scsi_sense_hdr sshdr;
  535. const char spi_write_buffer[] = {
  536. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  537. };
  538. const char spi_read_buffer[] = {
  539. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  540. };
  541. /* set up the pattern buffer. Doesn't matter if we spill
  542. * slightly beyond since that's where the read buffer is */
  543. for (j = 0; j < len; ) {
  544. /* fill the buffer with counting (test a) */
  545. for ( ; j < min(len, 32); j++)
  546. buffer[j] = j;
  547. k = j;
  548. /* fill the buffer with alternating words of 0x0 and
  549. * 0xffff (test b) */
  550. for ( ; j < min(len, k + 32); j += 2) {
  551. u16 *word = (u16 *)&buffer[j];
  552. *word = (j & 0x02) ? 0x0000 : 0xffff;
  553. }
  554. k = j;
  555. /* fill with crosstalk (alternating 0x5555 0xaaa)
  556. * (test c) */
  557. for ( ; j < min(len, k + 32); j += 2) {
  558. u16 *word = (u16 *)&buffer[j];
  559. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  560. }
  561. k = j;
  562. /* fill with shifting bits (test d) */
  563. for ( ; j < min(len, k + 32); j += 4) {
  564. u32 *word = (unsigned int *)&buffer[j];
  565. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  566. *word = pattern;
  567. pattern = (pattern << 1) | roll;
  568. }
  569. /* don't bother with random data (test e) */
  570. }
  571. for (r = 0; r < retries; r++) {
  572. result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
  573. buffer, len, &sshdr);
  574. if(result || !scsi_device_online(sdev)) {
  575. scsi_device_set_state(sdev, SDEV_QUIESCE);
  576. if (scsi_sense_valid(&sshdr)
  577. && sshdr.sense_key == ILLEGAL_REQUEST
  578. /* INVALID FIELD IN CDB */
  579. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  580. /* This would mean that the drive lied
  581. * to us about supporting an echo
  582. * buffer (unfortunately some Western
  583. * Digital drives do precisely this)
  584. */
  585. return SPI_COMPARE_SKIP_TEST;
  586. sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
  587. return SPI_COMPARE_FAILURE;
  588. }
  589. memset(ptr, 0, len);
  590. spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
  591. ptr, len, NULL);
  592. scsi_device_set_state(sdev, SDEV_QUIESCE);
  593. if (memcmp(buffer, ptr, len) != 0)
  594. return SPI_COMPARE_FAILURE;
  595. }
  596. return SPI_COMPARE_SUCCESS;
  597. }
  598. /* This is for the simplest form of Domain Validation: a read test
  599. * on the inquiry data from the device */
  600. static enum spi_compare_returns
  601. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  602. u8 *ptr, const int retries)
  603. {
  604. int r, result;
  605. const int len = sdev->inquiry_len;
  606. const char spi_inquiry[] = {
  607. INQUIRY, 0, 0, 0, len, 0
  608. };
  609. for (r = 0; r < retries; r++) {
  610. memset(ptr, 0, len);
  611. result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
  612. ptr, len, NULL);
  613. if(result || !scsi_device_online(sdev)) {
  614. scsi_device_set_state(sdev, SDEV_QUIESCE);
  615. return SPI_COMPARE_FAILURE;
  616. }
  617. /* If we don't have the inquiry data already, the
  618. * first read gets it */
  619. if (ptr == buffer) {
  620. ptr += len;
  621. --r;
  622. continue;
  623. }
  624. if (memcmp(buffer, ptr, len) != 0)
  625. /* failure */
  626. return SPI_COMPARE_FAILURE;
  627. }
  628. return SPI_COMPARE_SUCCESS;
  629. }
  630. static enum spi_compare_returns
  631. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  632. enum spi_compare_returns
  633. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  634. {
  635. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  636. struct scsi_target *starget = sdev->sdev_target;
  637. int period = 0, prevperiod = 0;
  638. enum spi_compare_returns retval;
  639. for (;;) {
  640. int newperiod;
  641. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  642. if (retval == SPI_COMPARE_SUCCESS
  643. || retval == SPI_COMPARE_SKIP_TEST)
  644. break;
  645. /* OK, retrain, fallback */
  646. if (i->f->get_iu)
  647. i->f->get_iu(starget);
  648. if (i->f->get_qas)
  649. i->f->get_qas(starget);
  650. if (i->f->get_period)
  651. i->f->get_period(sdev->sdev_target);
  652. /* Here's the fallback sequence; first try turning off
  653. * IU, then QAS (if we can control them), then finally
  654. * fall down the periods */
  655. if (i->f->set_iu && spi_iu(starget)) {
  656. starget_printk(KERN_ERR, starget, "Domain Validation Disabling Information Units\n");
  657. DV_SET(iu, 0);
  658. } else if (i->f->set_qas && spi_qas(starget)) {
  659. starget_printk(KERN_ERR, starget, "Domain Validation Disabling Quick Arbitration and Selection\n");
  660. DV_SET(qas, 0);
  661. } else {
  662. newperiod = spi_period(starget);
  663. period = newperiod > period ? newperiod : period;
  664. if (period < 0x0d)
  665. period++;
  666. else
  667. period += period >> 1;
  668. if (unlikely(period > 0xff || period == prevperiod)) {
  669. /* Total failure; set to async and return */
  670. starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
  671. DV_SET(offset, 0);
  672. return SPI_COMPARE_FAILURE;
  673. }
  674. starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
  675. DV_SET(period, period);
  676. prevperiod = period;
  677. }
  678. }
  679. return retval;
  680. }
  681. static int
  682. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  683. {
  684. int l, result;
  685. /* first off do a test unit ready. This can error out
  686. * because of reservations or some other reason. If it
  687. * fails, the device won't let us write to the echo buffer
  688. * so just return failure */
  689. static const char spi_test_unit_ready[] = {
  690. TEST_UNIT_READY, 0, 0, 0, 0, 0
  691. };
  692. static const char spi_read_buffer_descriptor[] = {
  693. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  694. };
  695. /* We send a set of three TURs to clear any outstanding
  696. * unit attention conditions if they exist (Otherwise the
  697. * buffer tests won't be happy). If the TUR still fails
  698. * (reservation conflict, device not ready, etc) just
  699. * skip the write tests */
  700. for (l = 0; ; l++) {
  701. result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
  702. NULL, 0, NULL);
  703. if(result) {
  704. if(l >= 3)
  705. return 0;
  706. } else {
  707. /* TUR succeeded */
  708. break;
  709. }
  710. }
  711. result = spi_execute(sdev, spi_read_buffer_descriptor,
  712. DMA_FROM_DEVICE, buffer, 4, NULL);
  713. if (result)
  714. /* Device has no echo buffer */
  715. return 0;
  716. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  717. }
  718. static void
  719. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  720. {
  721. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  722. struct scsi_target *starget = sdev->sdev_target;
  723. struct Scsi_Host *shost = sdev->host;
  724. int len = sdev->inquiry_len;
  725. int min_period = spi_min_period(starget);
  726. int max_width = spi_max_width(starget);
  727. /* first set us up for narrow async */
  728. DV_SET(offset, 0);
  729. DV_SET(width, 0);
  730. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  731. != SPI_COMPARE_SUCCESS) {
  732. starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
  733. /* FIXME: should probably offline the device here? */
  734. return;
  735. }
  736. if (!spi_support_wide(starget)) {
  737. spi_max_width(starget) = 0;
  738. max_width = 0;
  739. }
  740. /* test width */
  741. if (i->f->set_width && max_width) {
  742. i->f->set_width(starget, 1);
  743. if (spi_dv_device_compare_inquiry(sdev, buffer,
  744. buffer + len,
  745. DV_LOOPS)
  746. != SPI_COMPARE_SUCCESS) {
  747. starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
  748. i->f->set_width(starget, 0);
  749. /* Make sure we don't force wide back on by asking
  750. * for a transfer period that requires it */
  751. max_width = 0;
  752. if (min_period < 10)
  753. min_period = 10;
  754. }
  755. }
  756. if (!i->f->set_period)
  757. return;
  758. /* device can't handle synchronous */
  759. if (!spi_support_sync(starget) && !spi_support_dt(starget))
  760. return;
  761. /* len == -1 is the signal that we need to ascertain the
  762. * presence of an echo buffer before trying to use it. len ==
  763. * 0 means we don't have an echo buffer */
  764. len = -1;
  765. retry:
  766. /* now set up to the maximum */
  767. DV_SET(offset, spi_max_offset(starget));
  768. DV_SET(period, min_period);
  769. /* try QAS requests; this should be harmless to set if the
  770. * target supports it */
  771. if (spi_support_qas(starget) && spi_max_qas(starget)) {
  772. DV_SET(qas, 1);
  773. } else {
  774. DV_SET(qas, 0);
  775. }
  776. if (spi_support_ius(starget) && spi_max_iu(starget) &&
  777. min_period < 9) {
  778. /* This u320 (or u640). Set IU transfers */
  779. DV_SET(iu, 1);
  780. /* Then set the optional parameters */
  781. DV_SET(rd_strm, 1);
  782. DV_SET(wr_flow, 1);
  783. DV_SET(rti, 1);
  784. if (min_period == 8)
  785. DV_SET(pcomp_en, 1);
  786. } else {
  787. DV_SET(iu, 0);
  788. }
  789. /* now that we've done all this, actually check the bus
  790. * signal type (if known). Some devices are stupid on
  791. * a SE bus and still claim they can try LVD only settings */
  792. if (i->f->get_signalling)
  793. i->f->get_signalling(shost);
  794. if (spi_signalling(shost) == SPI_SIGNAL_SE ||
  795. spi_signalling(shost) == SPI_SIGNAL_HVD ||
  796. !spi_support_dt(starget)) {
  797. DV_SET(dt, 0);
  798. } else {
  799. DV_SET(dt, 1);
  800. }
  801. /* set width last because it will pull all the other
  802. * parameters down to required values */
  803. DV_SET(width, max_width);
  804. /* Do the read only INQUIRY tests */
  805. spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
  806. spi_dv_device_compare_inquiry);
  807. /* See if we actually managed to negotiate and sustain DT */
  808. if (i->f->get_dt)
  809. i->f->get_dt(starget);
  810. /* see if the device has an echo buffer. If it does we can do
  811. * the SPI pattern write tests. Because of some broken
  812. * devices, we *only* try this on a device that has actually
  813. * negotiated DT */
  814. if (len == -1 && spi_dt(starget))
  815. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  816. if (len <= 0) {
  817. starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
  818. return;
  819. }
  820. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  821. starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  822. len = SPI_MAX_ECHO_BUFFER_SIZE;
  823. }
  824. if (spi_dv_retrain(sdev, buffer, buffer + len,
  825. spi_dv_device_echo_buffer)
  826. == SPI_COMPARE_SKIP_TEST) {
  827. /* OK, the stupid drive can't do a write echo buffer
  828. * test after all, fall back to the read tests */
  829. len = 0;
  830. goto retry;
  831. }
  832. }
  833. /** spi_dv_device - Do Domain Validation on the device
  834. * @sdev: scsi device to validate
  835. *
  836. * Performs the domain validation on the given device in the
  837. * current execution thread. Since DV operations may sleep,
  838. * the current thread must have user context. Also no SCSI
  839. * related locks that would deadlock I/O issued by the DV may
  840. * be held.
  841. */
  842. void
  843. spi_dv_device(struct scsi_device *sdev)
  844. {
  845. struct scsi_target *starget = sdev->sdev_target;
  846. u8 *buffer;
  847. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  848. /*
  849. * Because this function and the power management code both call
  850. * scsi_device_quiesce(), it is not safe to perform domain validation
  851. * while suspend or resume is in progress. Hence the
  852. * lock/unlock_system_sleep() calls.
  853. */
  854. lock_system_sleep();
  855. if (unlikely(spi_dv_in_progress(starget)))
  856. goto unlock;
  857. if (unlikely(scsi_device_get(sdev)))
  858. goto unlock;
  859. spi_dv_in_progress(starget) = 1;
  860. buffer = kzalloc(len, GFP_KERNEL);
  861. if (unlikely(!buffer))
  862. goto out_put;
  863. /* We need to verify that the actual device will quiesce; the
  864. * later target quiesce is just a nice to have */
  865. if (unlikely(scsi_device_quiesce(sdev)))
  866. goto out_free;
  867. scsi_target_quiesce(starget);
  868. spi_dv_pending(starget) = 1;
  869. mutex_lock(&spi_dv_mutex(starget));
  870. starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
  871. spi_dv_device_internal(sdev, buffer);
  872. starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
  873. mutex_unlock(&spi_dv_mutex(starget));
  874. spi_dv_pending(starget) = 0;
  875. scsi_target_resume(starget);
  876. spi_initial_dv(starget) = 1;
  877. out_free:
  878. kfree(buffer);
  879. out_put:
  880. spi_dv_in_progress(starget) = 0;
  881. scsi_device_put(sdev);
  882. unlock:
  883. unlock_system_sleep();
  884. }
  885. EXPORT_SYMBOL(spi_dv_device);
  886. struct work_queue_wrapper {
  887. struct work_struct work;
  888. struct scsi_device *sdev;
  889. };
  890. static void
  891. spi_dv_device_work_wrapper(struct work_struct *work)
  892. {
  893. struct work_queue_wrapper *wqw =
  894. container_of(work, struct work_queue_wrapper, work);
  895. struct scsi_device *sdev = wqw->sdev;
  896. kfree(wqw);
  897. spi_dv_device(sdev);
  898. spi_dv_pending(sdev->sdev_target) = 0;
  899. scsi_device_put(sdev);
  900. }
  901. /**
  902. * spi_schedule_dv_device - schedule domain validation to occur on the device
  903. * @sdev: The device to validate
  904. *
  905. * Identical to spi_dv_device() above, except that the DV will be
  906. * scheduled to occur in a workqueue later. All memory allocations
  907. * are atomic, so may be called from any context including those holding
  908. * SCSI locks.
  909. */
  910. void
  911. spi_schedule_dv_device(struct scsi_device *sdev)
  912. {
  913. struct work_queue_wrapper *wqw =
  914. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  915. if (unlikely(!wqw))
  916. return;
  917. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  918. kfree(wqw);
  919. return;
  920. }
  921. /* Set pending early (dv_device doesn't check it, only sets it) */
  922. spi_dv_pending(sdev->sdev_target) = 1;
  923. if (unlikely(scsi_device_get(sdev))) {
  924. kfree(wqw);
  925. spi_dv_pending(sdev->sdev_target) = 0;
  926. return;
  927. }
  928. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
  929. wqw->sdev = sdev;
  930. schedule_work(&wqw->work);
  931. }
  932. EXPORT_SYMBOL(spi_schedule_dv_device);
  933. /**
  934. * spi_display_xfer_agreement - Print the current target transfer agreement
  935. * @starget: The target for which to display the agreement
  936. *
  937. * Each SPI port is required to maintain a transfer agreement for each
  938. * other port on the bus. This function prints a one-line summary of
  939. * the current agreement; more detailed information is available in sysfs.
  940. */
  941. void spi_display_xfer_agreement(struct scsi_target *starget)
  942. {
  943. struct spi_transport_attrs *tp;
  944. tp = (struct spi_transport_attrs *)&starget->starget_data;
  945. if (tp->offset > 0 && tp->period > 0) {
  946. unsigned int picosec, kb100;
  947. char *scsi = "FAST-?";
  948. char tmp[8];
  949. if (tp->period <= SPI_STATIC_PPR) {
  950. picosec = ppr_to_ps[tp->period];
  951. switch (tp->period) {
  952. case 7: scsi = "FAST-320"; break;
  953. case 8: scsi = "FAST-160"; break;
  954. case 9: scsi = "FAST-80"; break;
  955. case 10:
  956. case 11: scsi = "FAST-40"; break;
  957. case 12: scsi = "FAST-20"; break;
  958. }
  959. } else {
  960. picosec = tp->period * 4000;
  961. if (tp->period < 25)
  962. scsi = "FAST-20";
  963. else if (tp->period < 50)
  964. scsi = "FAST-10";
  965. else
  966. scsi = "FAST-5";
  967. }
  968. kb100 = (10000000 + picosec / 2) / picosec;
  969. if (tp->width)
  970. kb100 *= 2;
  971. sprint_frac(tmp, picosec, 1000);
  972. dev_info(&starget->dev,
  973. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  974. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  975. tp->dt ? "DT" : "ST",
  976. tp->iu ? " IU" : "",
  977. tp->qas ? " QAS" : "",
  978. tp->rd_strm ? " RDSTRM" : "",
  979. tp->rti ? " RTI" : "",
  980. tp->wr_flow ? " WRFLOW" : "",
  981. tp->pcomp_en ? " PCOMP" : "",
  982. tp->hold_mcs ? " HMCS" : "",
  983. tmp, tp->offset);
  984. } else {
  985. dev_info(&starget->dev, "%sasynchronous\n",
  986. tp->width ? "wide " : "");
  987. }
  988. }
  989. EXPORT_SYMBOL(spi_display_xfer_agreement);
  990. int spi_populate_width_msg(unsigned char *msg, int width)
  991. {
  992. msg[0] = EXTENDED_MESSAGE;
  993. msg[1] = 2;
  994. msg[2] = EXTENDED_WDTR;
  995. msg[3] = width;
  996. return 4;
  997. }
  998. EXPORT_SYMBOL_GPL(spi_populate_width_msg);
  999. int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
  1000. {
  1001. msg[0] = EXTENDED_MESSAGE;
  1002. msg[1] = 3;
  1003. msg[2] = EXTENDED_SDTR;
  1004. msg[3] = period;
  1005. msg[4] = offset;
  1006. return 5;
  1007. }
  1008. EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
  1009. int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
  1010. int width, int options)
  1011. {
  1012. msg[0] = EXTENDED_MESSAGE;
  1013. msg[1] = 6;
  1014. msg[2] = EXTENDED_PPR;
  1015. msg[3] = period;
  1016. msg[4] = 0;
  1017. msg[5] = offset;
  1018. msg[6] = width;
  1019. msg[7] = options;
  1020. return 8;
  1021. }
  1022. EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
  1023. /**
  1024. * spi_populate_tag_msg - place a tag message in a buffer
  1025. * @msg: pointer to the area to place the tag
  1026. * @cmd: pointer to the scsi command for the tag
  1027. *
  1028. * Notes:
  1029. * designed to create the correct type of tag message for the
  1030. * particular request. Returns the size of the tag message.
  1031. * May return 0 if TCQ is disabled for this device.
  1032. **/
  1033. int spi_populate_tag_msg(unsigned char *msg, struct scsi_cmnd *cmd)
  1034. {
  1035. if (cmd->flags & SCMD_TAGGED) {
  1036. *msg++ = SIMPLE_QUEUE_TAG;
  1037. *msg++ = cmd->request->tag;
  1038. return 2;
  1039. }
  1040. return 0;
  1041. }
  1042. EXPORT_SYMBOL_GPL(spi_populate_tag_msg);
  1043. #ifdef CONFIG_SCSI_CONSTANTS
  1044. static const char * const one_byte_msgs[] = {
  1045. /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
  1046. /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
  1047. /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
  1048. /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
  1049. /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
  1050. /* 0x0f */ "Initiate Recovery", "Release Recovery",
  1051. /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
  1052. /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
  1053. };
  1054. static const char * const two_byte_msgs[] = {
  1055. /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
  1056. /* 0x23 */ "Ignore Wide Residue", "ACA"
  1057. };
  1058. static const char * const extended_msgs[] = {
  1059. /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
  1060. /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
  1061. /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
  1062. };
  1063. static void print_nego(const unsigned char *msg, int per, int off, int width)
  1064. {
  1065. if (per) {
  1066. char buf[20];
  1067. period_to_str(buf, msg[per]);
  1068. printk("period = %s ns ", buf);
  1069. }
  1070. if (off)
  1071. printk("offset = %d ", msg[off]);
  1072. if (width)
  1073. printk("width = %d ", 8 << msg[width]);
  1074. }
  1075. static void print_ptr(const unsigned char *msg, int msb, const char *desc)
  1076. {
  1077. int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
  1078. msg[msb+3];
  1079. printk("%s = %d ", desc, ptr);
  1080. }
  1081. int spi_print_msg(const unsigned char *msg)
  1082. {
  1083. int len = 1, i;
  1084. if (msg[0] == EXTENDED_MESSAGE) {
  1085. len = 2 + msg[1];
  1086. if (len == 2)
  1087. len += 256;
  1088. if (msg[2] < ARRAY_SIZE(extended_msgs))
  1089. printk ("%s ", extended_msgs[msg[2]]);
  1090. else
  1091. printk ("Extended Message, reserved code (0x%02x) ",
  1092. (int) msg[2]);
  1093. switch (msg[2]) {
  1094. case EXTENDED_MODIFY_DATA_POINTER:
  1095. print_ptr(msg, 3, "pointer");
  1096. break;
  1097. case EXTENDED_SDTR:
  1098. print_nego(msg, 3, 4, 0);
  1099. break;
  1100. case EXTENDED_WDTR:
  1101. print_nego(msg, 0, 0, 3);
  1102. break;
  1103. case EXTENDED_PPR:
  1104. print_nego(msg, 3, 5, 6);
  1105. break;
  1106. case EXTENDED_MODIFY_BIDI_DATA_PTR:
  1107. print_ptr(msg, 3, "out");
  1108. print_ptr(msg, 7, "in");
  1109. break;
  1110. default:
  1111. for (i = 2; i < len; ++i)
  1112. printk("%02x ", msg[i]);
  1113. }
  1114. /* Identify */
  1115. } else if (msg[0] & 0x80) {
  1116. printk("Identify disconnect %sallowed %s %d ",
  1117. (msg[0] & 0x40) ? "" : "not ",
  1118. (msg[0] & 0x20) ? "target routine" : "lun",
  1119. msg[0] & 0x7);
  1120. /* Normal One byte */
  1121. } else if (msg[0] < 0x1f) {
  1122. if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
  1123. printk("%s ", one_byte_msgs[msg[0]]);
  1124. else
  1125. printk("reserved (%02x) ", msg[0]);
  1126. } else if (msg[0] == 0x55) {
  1127. printk("QAS Request ");
  1128. /* Two byte */
  1129. } else if (msg[0] <= 0x2f) {
  1130. if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
  1131. printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
  1132. msg[1]);
  1133. else
  1134. printk("reserved two byte (%02x %02x) ",
  1135. msg[0], msg[1]);
  1136. len = 2;
  1137. } else
  1138. printk("reserved ");
  1139. return len;
  1140. }
  1141. EXPORT_SYMBOL(spi_print_msg);
  1142. #else /* ifndef CONFIG_SCSI_CONSTANTS */
  1143. int spi_print_msg(const unsigned char *msg)
  1144. {
  1145. int len = 1, i;
  1146. if (msg[0] == EXTENDED_MESSAGE) {
  1147. len = 2 + msg[1];
  1148. if (len == 2)
  1149. len += 256;
  1150. for (i = 0; i < len; ++i)
  1151. printk("%02x ", msg[i]);
  1152. /* Identify */
  1153. } else if (msg[0] & 0x80) {
  1154. printk("%02x ", msg[0]);
  1155. /* Normal One byte */
  1156. } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
  1157. printk("%02x ", msg[0]);
  1158. /* Two byte */
  1159. } else if (msg[0] <= 0x2f) {
  1160. printk("%02x %02x", msg[0], msg[1]);
  1161. len = 2;
  1162. } else
  1163. printk("%02x ", msg[0]);
  1164. return len;
  1165. }
  1166. EXPORT_SYMBOL(spi_print_msg);
  1167. #endif /* ! CONFIG_SCSI_CONSTANTS */
  1168. static int spi_device_match(struct attribute_container *cont,
  1169. struct device *dev)
  1170. {
  1171. struct scsi_device *sdev;
  1172. struct Scsi_Host *shost;
  1173. struct spi_internal *i;
  1174. if (!scsi_is_sdev_device(dev))
  1175. return 0;
  1176. sdev = to_scsi_device(dev);
  1177. shost = sdev->host;
  1178. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1179. != &spi_host_class.class)
  1180. return 0;
  1181. /* Note: this class has no device attributes, so it has
  1182. * no per-HBA allocation and thus we don't need to distinguish
  1183. * the attribute containers for the device */
  1184. i = to_spi_internal(shost->transportt);
  1185. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  1186. return 0;
  1187. return 1;
  1188. }
  1189. static int spi_target_match(struct attribute_container *cont,
  1190. struct device *dev)
  1191. {
  1192. struct Scsi_Host *shost;
  1193. struct scsi_target *starget;
  1194. struct spi_internal *i;
  1195. if (!scsi_is_target_device(dev))
  1196. return 0;
  1197. shost = dev_to_shost(dev->parent);
  1198. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1199. != &spi_host_class.class)
  1200. return 0;
  1201. i = to_spi_internal(shost->transportt);
  1202. starget = to_scsi_target(dev);
  1203. if (i->f->deny_binding && i->f->deny_binding(starget))
  1204. return 0;
  1205. return &i->t.target_attrs.ac == cont;
  1206. }
  1207. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  1208. "spi_transport",
  1209. spi_setup_transport_attrs,
  1210. NULL,
  1211. spi_target_configure);
  1212. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  1213. spi_device_match,
  1214. spi_device_configure);
  1215. static struct attribute *host_attributes[] = {
  1216. &dev_attr_signalling.attr,
  1217. &dev_attr_host_width.attr,
  1218. &dev_attr_hba_id.attr,
  1219. NULL
  1220. };
  1221. static struct attribute_group host_attribute_group = {
  1222. .attrs = host_attributes,
  1223. };
  1224. static int spi_host_configure(struct transport_container *tc,
  1225. struct device *dev,
  1226. struct device *cdev)
  1227. {
  1228. struct kobject *kobj = &cdev->kobj;
  1229. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1230. struct spi_internal *si = to_spi_internal(shost->transportt);
  1231. struct attribute *attr = &dev_attr_signalling.attr;
  1232. int rc = 0;
  1233. if (si->f->set_signalling)
  1234. rc = sysfs_chmod_file(kobj, attr, attr->mode | S_IWUSR);
  1235. return rc;
  1236. }
  1237. /* returns true if we should be showing the variable. Also
  1238. * overloads the return by setting 1<<1 if the attribute should
  1239. * be writeable */
  1240. #define TARGET_ATTRIBUTE_HELPER(name) \
  1241. (si->f->show_##name ? S_IRUGO : 0) | \
  1242. (si->f->set_##name ? S_IWUSR : 0)
  1243. static umode_t target_attribute_is_visible(struct kobject *kobj,
  1244. struct attribute *attr, int i)
  1245. {
  1246. struct device *cdev = container_of(kobj, struct device, kobj);
  1247. struct scsi_target *starget = transport_class_to_starget(cdev);
  1248. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1249. struct spi_internal *si = to_spi_internal(shost->transportt);
  1250. if (attr == &dev_attr_period.attr &&
  1251. spi_support_sync(starget))
  1252. return TARGET_ATTRIBUTE_HELPER(period);
  1253. else if (attr == &dev_attr_min_period.attr &&
  1254. spi_support_sync(starget))
  1255. return TARGET_ATTRIBUTE_HELPER(period);
  1256. else if (attr == &dev_attr_offset.attr &&
  1257. spi_support_sync(starget))
  1258. return TARGET_ATTRIBUTE_HELPER(offset);
  1259. else if (attr == &dev_attr_max_offset.attr &&
  1260. spi_support_sync(starget))
  1261. return TARGET_ATTRIBUTE_HELPER(offset);
  1262. else if (attr == &dev_attr_width.attr &&
  1263. spi_support_wide(starget))
  1264. return TARGET_ATTRIBUTE_HELPER(width);
  1265. else if (attr == &dev_attr_max_width.attr &&
  1266. spi_support_wide(starget))
  1267. return TARGET_ATTRIBUTE_HELPER(width);
  1268. else if (attr == &dev_attr_iu.attr &&
  1269. spi_support_ius(starget))
  1270. return TARGET_ATTRIBUTE_HELPER(iu);
  1271. else if (attr == &dev_attr_max_iu.attr &&
  1272. spi_support_ius(starget))
  1273. return TARGET_ATTRIBUTE_HELPER(iu);
  1274. else if (attr == &dev_attr_dt.attr &&
  1275. spi_support_dt(starget))
  1276. return TARGET_ATTRIBUTE_HELPER(dt);
  1277. else if (attr == &dev_attr_qas.attr &&
  1278. spi_support_qas(starget))
  1279. return TARGET_ATTRIBUTE_HELPER(qas);
  1280. else if (attr == &dev_attr_max_qas.attr &&
  1281. spi_support_qas(starget))
  1282. return TARGET_ATTRIBUTE_HELPER(qas);
  1283. else if (attr == &dev_attr_wr_flow.attr &&
  1284. spi_support_ius(starget))
  1285. return TARGET_ATTRIBUTE_HELPER(wr_flow);
  1286. else if (attr == &dev_attr_rd_strm.attr &&
  1287. spi_support_ius(starget))
  1288. return TARGET_ATTRIBUTE_HELPER(rd_strm);
  1289. else if (attr == &dev_attr_rti.attr &&
  1290. spi_support_ius(starget))
  1291. return TARGET_ATTRIBUTE_HELPER(rti);
  1292. else if (attr == &dev_attr_pcomp_en.attr &&
  1293. spi_support_ius(starget))
  1294. return TARGET_ATTRIBUTE_HELPER(pcomp_en);
  1295. else if (attr == &dev_attr_hold_mcs.attr &&
  1296. spi_support_ius(starget))
  1297. return TARGET_ATTRIBUTE_HELPER(hold_mcs);
  1298. else if (attr == &dev_attr_revalidate.attr)
  1299. return S_IWUSR;
  1300. return 0;
  1301. }
  1302. static struct attribute *target_attributes[] = {
  1303. &dev_attr_period.attr,
  1304. &dev_attr_min_period.attr,
  1305. &dev_attr_offset.attr,
  1306. &dev_attr_max_offset.attr,
  1307. &dev_attr_width.attr,
  1308. &dev_attr_max_width.attr,
  1309. &dev_attr_iu.attr,
  1310. &dev_attr_max_iu.attr,
  1311. &dev_attr_dt.attr,
  1312. &dev_attr_qas.attr,
  1313. &dev_attr_max_qas.attr,
  1314. &dev_attr_wr_flow.attr,
  1315. &dev_attr_rd_strm.attr,
  1316. &dev_attr_rti.attr,
  1317. &dev_attr_pcomp_en.attr,
  1318. &dev_attr_hold_mcs.attr,
  1319. &dev_attr_revalidate.attr,
  1320. NULL
  1321. };
  1322. static struct attribute_group target_attribute_group = {
  1323. .attrs = target_attributes,
  1324. .is_visible = target_attribute_is_visible,
  1325. };
  1326. static int spi_target_configure(struct transport_container *tc,
  1327. struct device *dev,
  1328. struct device *cdev)
  1329. {
  1330. struct kobject *kobj = &cdev->kobj;
  1331. /* force an update based on parameters read from the device */
  1332. sysfs_update_group(kobj, &target_attribute_group);
  1333. return 0;
  1334. }
  1335. struct scsi_transport_template *
  1336. spi_attach_transport(struct spi_function_template *ft)
  1337. {
  1338. struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
  1339. GFP_KERNEL);
  1340. if (unlikely(!i))
  1341. return NULL;
  1342. i->t.target_attrs.ac.class = &spi_transport_class.class;
  1343. i->t.target_attrs.ac.grp = &target_attribute_group;
  1344. i->t.target_attrs.ac.match = spi_target_match;
  1345. transport_container_register(&i->t.target_attrs);
  1346. i->t.target_size = sizeof(struct spi_transport_attrs);
  1347. i->t.host_attrs.ac.class = &spi_host_class.class;
  1348. i->t.host_attrs.ac.grp = &host_attribute_group;
  1349. i->t.host_attrs.ac.match = spi_host_match;
  1350. transport_container_register(&i->t.host_attrs);
  1351. i->t.host_size = sizeof(struct spi_host_attrs);
  1352. i->f = ft;
  1353. return &i->t;
  1354. }
  1355. EXPORT_SYMBOL(spi_attach_transport);
  1356. void spi_release_transport(struct scsi_transport_template *t)
  1357. {
  1358. struct spi_internal *i = to_spi_internal(t);
  1359. transport_container_unregister(&i->t.target_attrs);
  1360. transport_container_unregister(&i->t.host_attrs);
  1361. kfree(i);
  1362. }
  1363. EXPORT_SYMBOL(spi_release_transport);
  1364. static __init int spi_transport_init(void)
  1365. {
  1366. int error = scsi_dev_info_add_list(SCSI_DEVINFO_SPI,
  1367. "SCSI Parallel Transport Class");
  1368. if (!error) {
  1369. int i;
  1370. for (i = 0; spi_static_device_list[i].vendor; i++)
  1371. scsi_dev_info_list_add_keyed(1, /* compatible */
  1372. spi_static_device_list[i].vendor,
  1373. spi_static_device_list[i].model,
  1374. NULL,
  1375. spi_static_device_list[i].flags,
  1376. SCSI_DEVINFO_SPI);
  1377. }
  1378. error = transport_class_register(&spi_transport_class);
  1379. if (error)
  1380. return error;
  1381. error = anon_transport_class_register(&spi_device_class);
  1382. return transport_class_register(&spi_host_class);
  1383. }
  1384. static void __exit spi_transport_exit(void)
  1385. {
  1386. transport_class_unregister(&spi_transport_class);
  1387. anon_transport_class_unregister(&spi_device_class);
  1388. transport_class_unregister(&spi_host_class);
  1389. scsi_dev_info_remove_list(SCSI_DEVINFO_SPI);
  1390. }
  1391. MODULE_AUTHOR("Martin Hicks");
  1392. MODULE_DESCRIPTION("SPI Transport Attributes");
  1393. MODULE_LICENSE("GPL");
  1394. module_init(spi_transport_init);
  1395. module_exit(spi_transport_exit);