scsi_transport_spi.c 27 KB

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