scsi_transport_spi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /*
  2. * Parallel SCSI (SPI) transport specific attributes exported to sysfs.
  3. *
  4. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  5. * Copyright (c) 2004, 2005 James Bottomley <James.Bottomley@SteelEye.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/ctype.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/blkdev.h>
  26. #include <asm/semaphore.h>
  27. #include <scsi/scsi.h>
  28. #include "scsi_priv.h"
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_request.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_transport.h>
  34. #include <scsi/scsi_transport_spi.h>
  35. #define SPI_PRINTK(x, l, f, a...) dev_printk(l, &(x)->dev, f , ##a)
  36. #define SPI_NUM_ATTRS 10 /* increase this if you add attributes */
  37. #define SPI_OTHER_ATTRS 1 /* Increase this if you add "always
  38. * on" attributes */
  39. #define SPI_HOST_ATTRS 1
  40. #define SPI_MAX_ECHO_BUFFER_SIZE 4096
  41. #define DV_LOOPS 3
  42. #define DV_TIMEOUT (10*HZ)
  43. #define DV_RETRIES 3 /* should only need at most
  44. * two cc/ua clears */
  45. /* Private data accessors (keep these out of the header file) */
  46. #define spi_dv_pending(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_pending)
  47. #define spi_dv_sem(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_sem)
  48. struct spi_internal {
  49. struct scsi_transport_template t;
  50. struct spi_function_template *f;
  51. /* The actual attributes */
  52. struct class_device_attribute private_attrs[SPI_NUM_ATTRS];
  53. /* The array of null terminated pointers to attributes
  54. * needed by scsi_sysfs.c */
  55. struct class_device_attribute *attrs[SPI_NUM_ATTRS + SPI_OTHER_ATTRS + 1];
  56. struct class_device_attribute private_host_attrs[SPI_HOST_ATTRS];
  57. struct class_device_attribute *host_attrs[SPI_HOST_ATTRS + 1];
  58. };
  59. #define to_spi_internal(tmpl) container_of(tmpl, struct spi_internal, t)
  60. static const int ppr_to_ps[] = {
  61. /* The PPR values 0-6 are reserved, fill them in when
  62. * the committee defines them */
  63. -1, /* 0x00 */
  64. -1, /* 0x01 */
  65. -1, /* 0x02 */
  66. -1, /* 0x03 */
  67. -1, /* 0x04 */
  68. -1, /* 0x05 */
  69. -1, /* 0x06 */
  70. 3125, /* 0x07 */
  71. 6250, /* 0x08 */
  72. 12500, /* 0x09 */
  73. 25000, /* 0x0a */
  74. 30300, /* 0x0b */
  75. 50000, /* 0x0c */
  76. };
  77. /* The PPR values at which you calculate the period in ns by multiplying
  78. * by 4 */
  79. #define SPI_STATIC_PPR 0x0c
  80. static int sprint_frac(char *dest, int value, int denom)
  81. {
  82. int frac = value % denom;
  83. int result = sprintf(dest, "%d", value / denom);
  84. if (frac == 0)
  85. return result;
  86. dest[result++] = '.';
  87. do {
  88. denom /= 10;
  89. sprintf(dest + result, "%d", frac / denom);
  90. result++;
  91. frac %= denom;
  92. } while (frac);
  93. dest[result++] = '\0';
  94. return result;
  95. }
  96. /* Modification of scsi_wait_req that will clear UNIT ATTENTION conditions
  97. * resulting from (likely) bus and device resets */
  98. static void spi_wait_req(struct scsi_request *sreq, const void *cmd,
  99. void *buffer, unsigned bufflen)
  100. {
  101. int i;
  102. for(i = 0; i < DV_RETRIES; i++) {
  103. sreq->sr_request->flags |= REQ_FAILFAST;
  104. scsi_wait_req(sreq, cmd, buffer, bufflen,
  105. DV_TIMEOUT, /* retries */ 1);
  106. if (sreq->sr_result & DRIVER_SENSE) {
  107. struct scsi_sense_hdr sshdr;
  108. if (scsi_request_normalize_sense(sreq, &sshdr)
  109. && sshdr.sense_key == UNIT_ATTENTION)
  110. continue;
  111. }
  112. break;
  113. }
  114. }
  115. static struct {
  116. enum spi_signal_type value;
  117. char *name;
  118. } signal_types[] = {
  119. { SPI_SIGNAL_UNKNOWN, "unknown" },
  120. { SPI_SIGNAL_SE, "SE" },
  121. { SPI_SIGNAL_LVD, "LVD" },
  122. { SPI_SIGNAL_HVD, "HVD" },
  123. };
  124. static inline const char *spi_signal_to_string(enum spi_signal_type type)
  125. {
  126. int i;
  127. for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
  128. if (type == signal_types[i].value)
  129. return signal_types[i].name;
  130. }
  131. return NULL;
  132. }
  133. static inline enum spi_signal_type spi_signal_to_value(const char *name)
  134. {
  135. int i, len;
  136. for (i = 0; i < sizeof(signal_types)/sizeof(signal_types[0]); i++) {
  137. len = strlen(signal_types[i].name);
  138. if (strncmp(name, signal_types[i].name, len) == 0 &&
  139. (name[len] == '\n' || name[len] == '\0'))
  140. return signal_types[i].value;
  141. }
  142. return SPI_SIGNAL_UNKNOWN;
  143. }
  144. static int spi_host_setup(struct device *dev)
  145. {
  146. struct Scsi_Host *shost = dev_to_shost(dev);
  147. spi_signalling(shost) = SPI_SIGNAL_UNKNOWN;
  148. return 0;
  149. }
  150. static DECLARE_TRANSPORT_CLASS(spi_host_class,
  151. "spi_host",
  152. spi_host_setup,
  153. NULL,
  154. NULL);
  155. static int spi_host_match(struct attribute_container *cont,
  156. struct device *dev)
  157. {
  158. struct Scsi_Host *shost;
  159. struct spi_internal *i;
  160. if (!scsi_is_host_device(dev))
  161. return 0;
  162. shost = dev_to_shost(dev);
  163. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  164. != &spi_host_class.class)
  165. return 0;
  166. i = to_spi_internal(shost->transportt);
  167. return &i->t.host_attrs.ac == cont;
  168. }
  169. static int spi_device_configure(struct device *dev)
  170. {
  171. struct scsi_device *sdev = to_scsi_device(dev);
  172. struct scsi_target *starget = sdev->sdev_target;
  173. /* Populate the target capability fields with the values
  174. * gleaned from the device inquiry */
  175. spi_support_sync(starget) = scsi_device_sync(sdev);
  176. spi_support_wide(starget) = scsi_device_wide(sdev);
  177. spi_support_dt(starget) = scsi_device_dt(sdev);
  178. spi_support_dt_only(starget) = scsi_device_dt_only(sdev);
  179. spi_support_ius(starget) = scsi_device_ius(sdev);
  180. spi_support_qas(starget) = scsi_device_qas(sdev);
  181. return 0;
  182. }
  183. static int spi_setup_transport_attrs(struct device *dev)
  184. {
  185. struct scsi_target *starget = to_scsi_target(dev);
  186. spi_period(starget) = -1; /* illegal value */
  187. spi_offset(starget) = 0; /* async */
  188. spi_width(starget) = 0; /* narrow */
  189. spi_iu(starget) = 0; /* no IU */
  190. spi_dt(starget) = 0; /* ST */
  191. spi_qas(starget) = 0;
  192. spi_wr_flow(starget) = 0;
  193. spi_rd_strm(starget) = 0;
  194. spi_rti(starget) = 0;
  195. spi_pcomp_en(starget) = 0;
  196. spi_dv_pending(starget) = 0;
  197. spi_initial_dv(starget) = 0;
  198. init_MUTEX(&spi_dv_sem(starget));
  199. return 0;
  200. }
  201. #define spi_transport_show_function(field, format_string) \
  202. \
  203. static ssize_t \
  204. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  205. { \
  206. struct scsi_target *starget = transport_class_to_starget(cdev); \
  207. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  208. struct spi_transport_attrs *tp; \
  209. struct spi_internal *i = to_spi_internal(shost->transportt); \
  210. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  211. if (i->f->get_##field) \
  212. i->f->get_##field(starget); \
  213. return snprintf(buf, 20, format_string, tp->field); \
  214. }
  215. #define spi_transport_store_function(field, format_string) \
  216. static ssize_t \
  217. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  218. size_t count) \
  219. { \
  220. int val; \
  221. struct scsi_target *starget = transport_class_to_starget(cdev); \
  222. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  223. struct spi_internal *i = to_spi_internal(shost->transportt); \
  224. \
  225. val = simple_strtoul(buf, NULL, 0); \
  226. i->f->set_##field(starget, val); \
  227. return count; \
  228. }
  229. #define spi_transport_rd_attr(field, format_string) \
  230. spi_transport_show_function(field, format_string) \
  231. spi_transport_store_function(field, format_string) \
  232. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  233. show_spi_transport_##field, \
  234. store_spi_transport_##field);
  235. /* The Parallel SCSI Tranport Attributes: */
  236. spi_transport_rd_attr(offset, "%d\n");
  237. spi_transport_rd_attr(width, "%d\n");
  238. spi_transport_rd_attr(iu, "%d\n");
  239. spi_transport_rd_attr(dt, "%d\n");
  240. spi_transport_rd_attr(qas, "%d\n");
  241. spi_transport_rd_attr(wr_flow, "%d\n");
  242. spi_transport_rd_attr(rd_strm, "%d\n");
  243. spi_transport_rd_attr(rti, "%d\n");
  244. spi_transport_rd_attr(pcomp_en, "%d\n");
  245. static ssize_t
  246. store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
  247. {
  248. struct scsi_target *starget = transport_class_to_starget(cdev);
  249. /* FIXME: we're relying on an awful lot of device internals
  250. * here. We really need a function to get the first available
  251. * child */
  252. struct device *dev = container_of(starget->dev.children.next, struct device, node);
  253. struct scsi_device *sdev = to_scsi_device(dev);
  254. spi_dv_device(sdev);
  255. return count;
  256. }
  257. static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  258. /* Translate the period into ns according to the current spec
  259. * for SDTR/PPR messages */
  260. static ssize_t show_spi_transport_period(struct class_device *cdev, char *buf)
  261. {
  262. struct scsi_target *starget = transport_class_to_starget(cdev);
  263. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  264. struct spi_transport_attrs *tp;
  265. int len, picosec;
  266. struct spi_internal *i = to_spi_internal(shost->transportt);
  267. tp = (struct spi_transport_attrs *)&starget->starget_data;
  268. if (i->f->get_period)
  269. i->f->get_period(starget);
  270. if (tp->period < 0 || tp->period > 0xff) {
  271. picosec = -1;
  272. } else if (tp->period <= SPI_STATIC_PPR) {
  273. picosec = ppr_to_ps[tp->period];
  274. } else {
  275. picosec = tp->period * 4000;
  276. }
  277. if (picosec == -1) {
  278. len = sprintf(buf, "reserved");
  279. } else {
  280. len = sprint_frac(buf, picosec, 1000);
  281. }
  282. buf[len++] = '\n';
  283. buf[len] = '\0';
  284. return len;
  285. }
  286. static ssize_t
  287. store_spi_transport_period(struct class_device *cdev, const char *buf,
  288. size_t count)
  289. {
  290. struct scsi_target *starget = transport_class_to_starget(cdev);
  291. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  292. struct spi_internal *i = to_spi_internal(shost->transportt);
  293. int j, picosec, period = -1;
  294. char *endp;
  295. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  296. if (*endp == '.') {
  297. int mult = 100;
  298. do {
  299. endp++;
  300. if (!isdigit(*endp))
  301. break;
  302. picosec += (*endp - '0') * mult;
  303. mult /= 10;
  304. } while (mult > 0);
  305. }
  306. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  307. if (ppr_to_ps[j] < picosec)
  308. continue;
  309. period = j;
  310. break;
  311. }
  312. if (period == -1)
  313. period = picosec / 4000;
  314. if (period > 0xff)
  315. period = 0xff;
  316. i->f->set_period(starget, period);
  317. return count;
  318. }
  319. static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR,
  320. show_spi_transport_period,
  321. store_spi_transport_period);
  322. static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
  323. {
  324. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  325. struct spi_internal *i = to_spi_internal(shost->transportt);
  326. if (i->f->get_signalling)
  327. i->f->get_signalling(shost);
  328. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  329. }
  330. static ssize_t store_spi_host_signalling(struct class_device *cdev,
  331. const char *buf, size_t count)
  332. {
  333. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  334. struct spi_internal *i = to_spi_internal(shost->transportt);
  335. enum spi_signal_type type = spi_signal_to_value(buf);
  336. if (type != SPI_SIGNAL_UNKNOWN)
  337. i->f->set_signalling(shost, type);
  338. return count;
  339. }
  340. static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR,
  341. show_spi_host_signalling,
  342. store_spi_host_signalling);
  343. #define DV_SET(x, y) \
  344. if(i->f->set_##x) \
  345. i->f->set_##x(sdev->sdev_target, y)
  346. enum spi_compare_returns {
  347. SPI_COMPARE_SUCCESS,
  348. SPI_COMPARE_FAILURE,
  349. SPI_COMPARE_SKIP_TEST,
  350. };
  351. /* This is for read/write Domain Validation: If the device supports
  352. * an echo buffer, we do read/write tests to it */
  353. static enum spi_compare_returns
  354. spi_dv_device_echo_buffer(struct scsi_request *sreq, u8 *buffer,
  355. u8 *ptr, const int retries)
  356. {
  357. struct scsi_device *sdev = sreq->sr_device;
  358. int len = ptr - buffer;
  359. int j, k, r;
  360. unsigned int pattern = 0x0000ffff;
  361. const char spi_write_buffer[] = {
  362. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  363. };
  364. const char spi_read_buffer[] = {
  365. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  366. };
  367. /* set up the pattern buffer. Doesn't matter if we spill
  368. * slightly beyond since that's where the read buffer is */
  369. for (j = 0; j < len; ) {
  370. /* fill the buffer with counting (test a) */
  371. for ( ; j < min(len, 32); j++)
  372. buffer[j] = j;
  373. k = j;
  374. /* fill the buffer with alternating words of 0x0 and
  375. * 0xffff (test b) */
  376. for ( ; j < min(len, k + 32); j += 2) {
  377. u16 *word = (u16 *)&buffer[j];
  378. *word = (j & 0x02) ? 0x0000 : 0xffff;
  379. }
  380. k = j;
  381. /* fill with crosstalk (alternating 0x5555 0xaaa)
  382. * (test c) */
  383. for ( ; j < min(len, k + 32); j += 2) {
  384. u16 *word = (u16 *)&buffer[j];
  385. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  386. }
  387. k = j;
  388. /* fill with shifting bits (test d) */
  389. for ( ; j < min(len, k + 32); j += 4) {
  390. u32 *word = (unsigned int *)&buffer[j];
  391. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  392. *word = pattern;
  393. pattern = (pattern << 1) | roll;
  394. }
  395. /* don't bother with random data (test e) */
  396. }
  397. for (r = 0; r < retries; r++) {
  398. sreq->sr_cmd_len = 0; /* wait_req to fill in */
  399. sreq->sr_data_direction = DMA_TO_DEVICE;
  400. spi_wait_req(sreq, spi_write_buffer, buffer, len);
  401. if(sreq->sr_result || !scsi_device_online(sdev)) {
  402. struct scsi_sense_hdr sshdr;
  403. scsi_device_set_state(sdev, SDEV_QUIESCE);
  404. if (scsi_request_normalize_sense(sreq, &sshdr)
  405. && sshdr.sense_key == ILLEGAL_REQUEST
  406. /* INVALID FIELD IN CDB */
  407. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  408. /* This would mean that the drive lied
  409. * to us about supporting an echo
  410. * buffer (unfortunately some Western
  411. * Digital drives do precisely this)
  412. */
  413. return SPI_COMPARE_SKIP_TEST;
  414. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Write Buffer failure %x\n", sreq->sr_result);
  415. return SPI_COMPARE_FAILURE;
  416. }
  417. memset(ptr, 0, len);
  418. sreq->sr_cmd_len = 0; /* wait_req to fill in */
  419. sreq->sr_data_direction = DMA_FROM_DEVICE;
  420. spi_wait_req(sreq, spi_read_buffer, ptr, len);
  421. scsi_device_set_state(sdev, SDEV_QUIESCE);
  422. if (memcmp(buffer, ptr, len) != 0)
  423. return SPI_COMPARE_FAILURE;
  424. }
  425. return SPI_COMPARE_SUCCESS;
  426. }
  427. /* This is for the simplest form of Domain Validation: a read test
  428. * on the inquiry data from the device */
  429. static enum spi_compare_returns
  430. spi_dv_device_compare_inquiry(struct scsi_request *sreq, u8 *buffer,
  431. u8 *ptr, const int retries)
  432. {
  433. int r;
  434. const int len = sreq->sr_device->inquiry_len;
  435. struct scsi_device *sdev = sreq->sr_device;
  436. const char spi_inquiry[] = {
  437. INQUIRY, 0, 0, 0, len, 0
  438. };
  439. for (r = 0; r < retries; r++) {
  440. sreq->sr_cmd_len = 0; /* wait_req to fill in */
  441. sreq->sr_data_direction = DMA_FROM_DEVICE;
  442. memset(ptr, 0, len);
  443. spi_wait_req(sreq, spi_inquiry, ptr, len);
  444. if(sreq->sr_result || !scsi_device_online(sdev)) {
  445. scsi_device_set_state(sdev, SDEV_QUIESCE);
  446. return SPI_COMPARE_FAILURE;
  447. }
  448. /* If we don't have the inquiry data already, the
  449. * first read gets it */
  450. if (ptr == buffer) {
  451. ptr += len;
  452. --r;
  453. continue;
  454. }
  455. if (memcmp(buffer, ptr, len) != 0)
  456. /* failure */
  457. return SPI_COMPARE_FAILURE;
  458. }
  459. return SPI_COMPARE_SUCCESS;
  460. }
  461. static enum spi_compare_returns
  462. spi_dv_retrain(struct scsi_request *sreq, u8 *buffer, u8 *ptr,
  463. enum spi_compare_returns
  464. (*compare_fn)(struct scsi_request *, u8 *, u8 *, int))
  465. {
  466. struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
  467. struct scsi_device *sdev = sreq->sr_device;
  468. int period = 0, prevperiod = 0;
  469. enum spi_compare_returns retval;
  470. for (;;) {
  471. int newperiod;
  472. retval = compare_fn(sreq, buffer, ptr, DV_LOOPS);
  473. if (retval == SPI_COMPARE_SUCCESS
  474. || retval == SPI_COMPARE_SKIP_TEST)
  475. break;
  476. /* OK, retrain, fallback */
  477. if (i->f->get_period)
  478. i->f->get_period(sdev->sdev_target);
  479. newperiod = spi_period(sdev->sdev_target);
  480. period = newperiod > period ? newperiod : period;
  481. if (period < 0x0d)
  482. period++;
  483. else
  484. period += period >> 1;
  485. if (unlikely(period > 0xff || period == prevperiod)) {
  486. /* Total failure; set to async and return */
  487. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n");
  488. DV_SET(offset, 0);
  489. return SPI_COMPARE_FAILURE;
  490. }
  491. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation detected failure, dropping back\n");
  492. DV_SET(period, period);
  493. prevperiod = period;
  494. }
  495. return retval;
  496. }
  497. static int
  498. spi_dv_device_get_echo_buffer(struct scsi_request *sreq, u8 *buffer)
  499. {
  500. int l;
  501. /* first off do a test unit ready. This can error out
  502. * because of reservations or some other reason. If it
  503. * fails, the device won't let us write to the echo buffer
  504. * so just return failure */
  505. const char spi_test_unit_ready[] = {
  506. TEST_UNIT_READY, 0, 0, 0, 0, 0
  507. };
  508. const char spi_read_buffer_descriptor[] = {
  509. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  510. };
  511. sreq->sr_cmd_len = 0;
  512. sreq->sr_data_direction = DMA_NONE;
  513. /* We send a set of three TURs to clear any outstanding
  514. * unit attention conditions if they exist (Otherwise the
  515. * buffer tests won't be happy). If the TUR still fails
  516. * (reservation conflict, device not ready, etc) just
  517. * skip the write tests */
  518. for (l = 0; ; l++) {
  519. spi_wait_req(sreq, spi_test_unit_ready, NULL, 0);
  520. if(sreq->sr_result) {
  521. if(l >= 3)
  522. return 0;
  523. } else {
  524. /* TUR succeeded */
  525. break;
  526. }
  527. }
  528. sreq->sr_cmd_len = 0;
  529. sreq->sr_data_direction = DMA_FROM_DEVICE;
  530. spi_wait_req(sreq, spi_read_buffer_descriptor, buffer, 4);
  531. if (sreq->sr_result)
  532. /* Device has no echo buffer */
  533. return 0;
  534. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  535. }
  536. static void
  537. spi_dv_device_internal(struct scsi_request *sreq, u8 *buffer)
  538. {
  539. struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
  540. struct scsi_device *sdev = sreq->sr_device;
  541. int len = sdev->inquiry_len;
  542. /* first set us up for narrow async */
  543. DV_SET(offset, 0);
  544. DV_SET(width, 0);
  545. if (spi_dv_device_compare_inquiry(sreq, buffer, buffer, DV_LOOPS)
  546. != SPI_COMPARE_SUCCESS) {
  547. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Initial Inquiry Failed\n");
  548. /* FIXME: should probably offline the device here? */
  549. return;
  550. }
  551. /* test width */
  552. if (i->f->set_width && sdev->wdtr) {
  553. i->f->set_width(sdev->sdev_target, 1);
  554. if (spi_dv_device_compare_inquiry(sreq, buffer,
  555. buffer + len,
  556. DV_LOOPS)
  557. != SPI_COMPARE_SUCCESS) {
  558. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Wide Transfers Fail\n");
  559. i->f->set_width(sdev->sdev_target, 0);
  560. }
  561. }
  562. if (!i->f->set_period)
  563. return;
  564. /* device can't handle synchronous */
  565. if(!sdev->ppr && !sdev->sdtr)
  566. return;
  567. /* see if the device has an echo buffer. If it does we can
  568. * do the SPI pattern write tests */
  569. len = 0;
  570. if (sdev->ppr)
  571. len = spi_dv_device_get_echo_buffer(sreq, buffer);
  572. retry:
  573. /* now set up to the maximum */
  574. DV_SET(offset, 255);
  575. DV_SET(period, 1);
  576. if (len == 0) {
  577. SPI_PRINTK(sdev->sdev_target, KERN_INFO, "Domain Validation skipping write tests\n");
  578. spi_dv_retrain(sreq, buffer, buffer + len,
  579. spi_dv_device_compare_inquiry);
  580. return;
  581. }
  582. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  583. SPI_PRINTK(sdev->sdev_target, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  584. len = SPI_MAX_ECHO_BUFFER_SIZE;
  585. }
  586. if (spi_dv_retrain(sreq, buffer, buffer + len,
  587. spi_dv_device_echo_buffer)
  588. == SPI_COMPARE_SKIP_TEST) {
  589. /* OK, the stupid drive can't do a write echo buffer
  590. * test after all, fall back to the read tests */
  591. len = 0;
  592. goto retry;
  593. }
  594. }
  595. /** spi_dv_device - Do Domain Validation on the device
  596. * @sdev: scsi device to validate
  597. *
  598. * Performs the domain validation on the given device in the
  599. * current execution thread. Since DV operations may sleep,
  600. * the current thread must have user context. Also no SCSI
  601. * related locks that would deadlock I/O issued by the DV may
  602. * be held.
  603. */
  604. void
  605. spi_dv_device(struct scsi_device *sdev)
  606. {
  607. struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
  608. struct scsi_target *starget = sdev->sdev_target;
  609. u8 *buffer;
  610. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  611. if (unlikely(!sreq))
  612. return;
  613. if (unlikely(scsi_device_get(sdev)))
  614. goto out_free_req;
  615. buffer = kmalloc(len, GFP_KERNEL);
  616. if (unlikely(!buffer))
  617. goto out_put;
  618. memset(buffer, 0, len);
  619. /* We need to verify that the actual device will quiesce; the
  620. * later target quiesce is just a nice to have */
  621. if (unlikely(scsi_device_quiesce(sdev)))
  622. goto out_free;
  623. scsi_target_quiesce(starget);
  624. spi_dv_pending(starget) = 1;
  625. down(&spi_dv_sem(starget));
  626. SPI_PRINTK(starget, KERN_INFO, "Beginning Domain Validation\n");
  627. spi_dv_device_internal(sreq, buffer);
  628. SPI_PRINTK(starget, KERN_INFO, "Ending Domain Validation\n");
  629. up(&spi_dv_sem(starget));
  630. spi_dv_pending(starget) = 0;
  631. scsi_target_resume(starget);
  632. spi_initial_dv(starget) = 1;
  633. out_free:
  634. kfree(buffer);
  635. out_put:
  636. scsi_device_put(sdev);
  637. out_free_req:
  638. scsi_release_request(sreq);
  639. }
  640. EXPORT_SYMBOL(spi_dv_device);
  641. struct work_queue_wrapper {
  642. struct work_struct work;
  643. struct scsi_device *sdev;
  644. };
  645. static void
  646. spi_dv_device_work_wrapper(void *data)
  647. {
  648. struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
  649. struct scsi_device *sdev = wqw->sdev;
  650. kfree(wqw);
  651. spi_dv_device(sdev);
  652. spi_dv_pending(sdev->sdev_target) = 0;
  653. scsi_device_put(sdev);
  654. }
  655. /**
  656. * spi_schedule_dv_device - schedule domain validation to occur on the device
  657. * @sdev: The device to validate
  658. *
  659. * Identical to spi_dv_device() above, except that the DV will be
  660. * scheduled to occur in a workqueue later. All memory allocations
  661. * are atomic, so may be called from any context including those holding
  662. * SCSI locks.
  663. */
  664. void
  665. spi_schedule_dv_device(struct scsi_device *sdev)
  666. {
  667. struct work_queue_wrapper *wqw =
  668. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  669. if (unlikely(!wqw))
  670. return;
  671. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  672. kfree(wqw);
  673. return;
  674. }
  675. /* Set pending early (dv_device doesn't check it, only sets it) */
  676. spi_dv_pending(sdev->sdev_target) = 1;
  677. if (unlikely(scsi_device_get(sdev))) {
  678. kfree(wqw);
  679. spi_dv_pending(sdev->sdev_target) = 0;
  680. return;
  681. }
  682. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
  683. wqw->sdev = sdev;
  684. schedule_work(&wqw->work);
  685. }
  686. EXPORT_SYMBOL(spi_schedule_dv_device);
  687. /**
  688. * spi_display_xfer_agreement - Print the current target transfer agreement
  689. * @starget: The target for which to display the agreement
  690. *
  691. * Each SPI port is required to maintain a transfer agreement for each
  692. * other port on the bus. This function prints a one-line summary of
  693. * the current agreement; more detailed information is available in sysfs.
  694. */
  695. void spi_display_xfer_agreement(struct scsi_target *starget)
  696. {
  697. struct spi_transport_attrs *tp;
  698. tp = (struct spi_transport_attrs *)&starget->starget_data;
  699. if (tp->offset > 0 && tp->period > 0) {
  700. unsigned int picosec, kb100;
  701. char *scsi = "FAST-?";
  702. char tmp[8];
  703. if (tp->period <= SPI_STATIC_PPR) {
  704. picosec = ppr_to_ps[tp->period];
  705. switch (tp->period) {
  706. case 7: scsi = "FAST-320"; break;
  707. case 8: scsi = "FAST-160"; break;
  708. case 9: scsi = "FAST-80"; break;
  709. case 10:
  710. case 11: scsi = "FAST-40"; break;
  711. case 12: scsi = "FAST-20"; break;
  712. }
  713. } else {
  714. picosec = tp->period * 4000;
  715. if (tp->period < 25)
  716. scsi = "FAST-20";
  717. else if (tp->period < 50)
  718. scsi = "FAST-10";
  719. else
  720. scsi = "FAST-5";
  721. }
  722. kb100 = (10000000 + picosec / 2) / picosec;
  723. if (tp->width)
  724. kb100 *= 2;
  725. sprint_frac(tmp, picosec, 1000);
  726. dev_info(&starget->dev,
  727. "%s %sSCSI %d.%d MB/s %s%s%s (%s ns, offset %d)\n",
  728. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  729. tp->dt ? "DT" : "ST", tp->iu ? " IU" : "",
  730. tp->qas ? " QAS" : "", tmp, tp->offset);
  731. } else {
  732. dev_info(&starget->dev, "%sasynchronous.\n",
  733. tp->width ? "wide " : "");
  734. }
  735. }
  736. EXPORT_SYMBOL(spi_display_xfer_agreement);
  737. #define SETUP_ATTRIBUTE(field) \
  738. i->private_attrs[count] = class_device_attr_##field; \
  739. if (!i->f->set_##field) { \
  740. i->private_attrs[count].attr.mode = S_IRUGO; \
  741. i->private_attrs[count].store = NULL; \
  742. } \
  743. i->attrs[count] = &i->private_attrs[count]; \
  744. if (i->f->show_##field) \
  745. count++
  746. #define SETUP_HOST_ATTRIBUTE(field) \
  747. i->private_host_attrs[count] = class_device_attr_##field; \
  748. if (!i->f->set_##field) { \
  749. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  750. i->private_host_attrs[count].store = NULL; \
  751. } \
  752. i->host_attrs[count] = &i->private_host_attrs[count]; \
  753. count++
  754. static int spi_device_match(struct attribute_container *cont,
  755. struct device *dev)
  756. {
  757. struct scsi_device *sdev;
  758. struct Scsi_Host *shost;
  759. if (!scsi_is_sdev_device(dev))
  760. return 0;
  761. sdev = to_scsi_device(dev);
  762. shost = sdev->host;
  763. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  764. != &spi_host_class.class)
  765. return 0;
  766. /* Note: this class has no device attributes, so it has
  767. * no per-HBA allocation and thus we don't need to distinguish
  768. * the attribute containers for the device */
  769. return 1;
  770. }
  771. static int spi_target_match(struct attribute_container *cont,
  772. struct device *dev)
  773. {
  774. struct Scsi_Host *shost;
  775. struct spi_internal *i;
  776. if (!scsi_is_target_device(dev))
  777. return 0;
  778. shost = dev_to_shost(dev->parent);
  779. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  780. != &spi_host_class.class)
  781. return 0;
  782. i = to_spi_internal(shost->transportt);
  783. return &i->t.target_attrs.ac == cont;
  784. }
  785. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  786. "spi_transport",
  787. spi_setup_transport_attrs,
  788. NULL,
  789. NULL);
  790. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  791. spi_device_match,
  792. spi_device_configure);
  793. struct scsi_transport_template *
  794. spi_attach_transport(struct spi_function_template *ft)
  795. {
  796. struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
  797. GFP_KERNEL);
  798. int count = 0;
  799. if (unlikely(!i))
  800. return NULL;
  801. memset(i, 0, sizeof(struct spi_internal));
  802. i->t.target_attrs.ac.class = &spi_transport_class.class;
  803. i->t.target_attrs.ac.attrs = &i->attrs[0];
  804. i->t.target_attrs.ac.match = spi_target_match;
  805. transport_container_register(&i->t.target_attrs);
  806. i->t.target_size = sizeof(struct spi_transport_attrs);
  807. i->t.host_attrs.ac.class = &spi_host_class.class;
  808. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  809. i->t.host_attrs.ac.match = spi_host_match;
  810. transport_container_register(&i->t.host_attrs);
  811. i->t.host_size = sizeof(struct spi_host_attrs);
  812. i->f = ft;
  813. SETUP_ATTRIBUTE(period);
  814. SETUP_ATTRIBUTE(offset);
  815. SETUP_ATTRIBUTE(width);
  816. SETUP_ATTRIBUTE(iu);
  817. SETUP_ATTRIBUTE(dt);
  818. SETUP_ATTRIBUTE(qas);
  819. SETUP_ATTRIBUTE(wr_flow);
  820. SETUP_ATTRIBUTE(rd_strm);
  821. SETUP_ATTRIBUTE(rti);
  822. SETUP_ATTRIBUTE(pcomp_en);
  823. /* if you add an attribute but forget to increase SPI_NUM_ATTRS
  824. * this bug will trigger */
  825. BUG_ON(count > SPI_NUM_ATTRS);
  826. i->attrs[count++] = &class_device_attr_revalidate;
  827. i->attrs[count] = NULL;
  828. count = 0;
  829. SETUP_HOST_ATTRIBUTE(signalling);
  830. BUG_ON(count > SPI_HOST_ATTRS);
  831. i->host_attrs[count] = NULL;
  832. return &i->t;
  833. }
  834. EXPORT_SYMBOL(spi_attach_transport);
  835. void spi_release_transport(struct scsi_transport_template *t)
  836. {
  837. struct spi_internal *i = to_spi_internal(t);
  838. transport_container_unregister(&i->t.target_attrs);
  839. transport_container_unregister(&i->t.host_attrs);
  840. kfree(i);
  841. }
  842. EXPORT_SYMBOL(spi_release_transport);
  843. static __init int spi_transport_init(void)
  844. {
  845. int error = transport_class_register(&spi_transport_class);
  846. if (error)
  847. return error;
  848. error = anon_transport_class_register(&spi_device_class);
  849. return transport_class_register(&spi_host_class);
  850. }
  851. static void __exit spi_transport_exit(void)
  852. {
  853. transport_class_unregister(&spi_transport_class);
  854. anon_transport_class_unregister(&spi_device_class);
  855. transport_class_unregister(&spi_host_class);
  856. }
  857. MODULE_AUTHOR("Martin Hicks");
  858. MODULE_DESCRIPTION("SPI Transport Attributes");
  859. MODULE_LICENSE("GPL");
  860. module_init(spi_transport_init);
  861. module_exit(spi_transport_exit);