scsi_transport_spi.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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 13 /* 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_min_period(starget) = 0;
  188. spi_offset(starget) = 0; /* async */
  189. spi_max_offset(starget) = 255;
  190. spi_width(starget) = 0; /* narrow */
  191. spi_max_width(starget) = 1;
  192. spi_iu(starget) = 0; /* no IU */
  193. spi_dt(starget) = 0; /* ST */
  194. spi_qas(starget) = 0;
  195. spi_wr_flow(starget) = 0;
  196. spi_rd_strm(starget) = 0;
  197. spi_rti(starget) = 0;
  198. spi_pcomp_en(starget) = 0;
  199. spi_dv_pending(starget) = 0;
  200. spi_initial_dv(starget) = 0;
  201. init_MUTEX(&spi_dv_sem(starget));
  202. return 0;
  203. }
  204. #define spi_transport_show_simple(field, format_string) \
  205. \
  206. static ssize_t \
  207. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  208. { \
  209. struct scsi_target *starget = transport_class_to_starget(cdev); \
  210. struct spi_transport_attrs *tp; \
  211. \
  212. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  213. return snprintf(buf, 20, format_string, tp->field); \
  214. }
  215. #define spi_transport_store_simple(field, format_string) \
  216. \
  217. static ssize_t \
  218. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  219. size_t count) \
  220. { \
  221. int val; \
  222. struct scsi_target *starget = transport_class_to_starget(cdev); \
  223. struct spi_transport_attrs *tp; \
  224. \
  225. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  226. val = simple_strtoul(buf, NULL, 0); \
  227. tp->field = val; \
  228. return count; \
  229. }
  230. #define spi_transport_show_function(field, format_string) \
  231. \
  232. static ssize_t \
  233. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  234. { \
  235. struct scsi_target *starget = transport_class_to_starget(cdev); \
  236. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  237. struct spi_transport_attrs *tp; \
  238. struct spi_internal *i = to_spi_internal(shost->transportt); \
  239. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  240. if (i->f->get_##field) \
  241. i->f->get_##field(starget); \
  242. return snprintf(buf, 20, format_string, tp->field); \
  243. }
  244. #define spi_transport_store_function(field, format_string) \
  245. static ssize_t \
  246. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  247. size_t count) \
  248. { \
  249. int val; \
  250. struct scsi_target *starget = transport_class_to_starget(cdev); \
  251. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  252. struct spi_internal *i = to_spi_internal(shost->transportt); \
  253. \
  254. val = simple_strtoul(buf, NULL, 0); \
  255. i->f->set_##field(starget, val); \
  256. return count; \
  257. }
  258. #define spi_transport_store_max(field, format_string) \
  259. static ssize_t \
  260. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  261. size_t count) \
  262. { \
  263. int val; \
  264. struct scsi_target *starget = transport_class_to_starget(cdev); \
  265. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  266. struct spi_internal *i = to_spi_internal(shost->transportt); \
  267. struct spi_transport_attrs *tp \
  268. = (struct spi_transport_attrs *)&starget->starget_data; \
  269. \
  270. val = simple_strtoul(buf, NULL, 0); \
  271. if (val > tp->max_##field) \
  272. val = tp->max_##field; \
  273. i->f->set_##field(starget, val); \
  274. return count; \
  275. }
  276. #define spi_transport_rd_attr(field, format_string) \
  277. spi_transport_show_function(field, format_string) \
  278. spi_transport_store_function(field, format_string) \
  279. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  280. show_spi_transport_##field, \
  281. store_spi_transport_##field);
  282. #define spi_transport_simple_attr(field, format_string) \
  283. spi_transport_show_simple(field, format_string) \
  284. spi_transport_store_simple(field, format_string) \
  285. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  286. show_spi_transport_##field, \
  287. store_spi_transport_##field);
  288. #define spi_transport_max_attr(field, format_string) \
  289. spi_transport_show_function(field, format_string) \
  290. spi_transport_store_max(field, format_string) \
  291. spi_transport_simple_attr(max_##field, format_string) \
  292. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  293. show_spi_transport_##field, \
  294. store_spi_transport_##field);
  295. /* The Parallel SCSI Tranport Attributes: */
  296. spi_transport_max_attr(offset, "%d\n");
  297. spi_transport_max_attr(width, "%d\n");
  298. spi_transport_rd_attr(iu, "%d\n");
  299. spi_transport_rd_attr(dt, "%d\n");
  300. spi_transport_rd_attr(qas, "%d\n");
  301. spi_transport_rd_attr(wr_flow, "%d\n");
  302. spi_transport_rd_attr(rd_strm, "%d\n");
  303. spi_transport_rd_attr(rti, "%d\n");
  304. spi_transport_rd_attr(pcomp_en, "%d\n");
  305. static ssize_t
  306. store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
  307. {
  308. struct scsi_target *starget = transport_class_to_starget(cdev);
  309. /* FIXME: we're relying on an awful lot of device internals
  310. * here. We really need a function to get the first available
  311. * child */
  312. struct device *dev = container_of(starget->dev.children.next, struct device, node);
  313. struct scsi_device *sdev = to_scsi_device(dev);
  314. spi_dv_device(sdev);
  315. return count;
  316. }
  317. static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  318. /* Translate the period into ns according to the current spec
  319. * for SDTR/PPR messages */
  320. static ssize_t
  321. show_spi_transport_period_helper(struct class_device *cdev, char *buf,
  322. int period)
  323. {
  324. int len, picosec;
  325. if (period < 0 || period > 0xff) {
  326. picosec = -1;
  327. } else if (period <= SPI_STATIC_PPR) {
  328. picosec = ppr_to_ps[period];
  329. } else {
  330. picosec = period * 4000;
  331. }
  332. if (picosec == -1) {
  333. len = sprintf(buf, "reserved");
  334. } else {
  335. len = sprint_frac(buf, picosec, 1000);
  336. }
  337. buf[len++] = '\n';
  338. buf[len] = '\0';
  339. return len;
  340. }
  341. static ssize_t
  342. store_spi_transport_period_helper(struct class_device *cdev, const char *buf,
  343. size_t count, int *periodp)
  344. {
  345. int j, picosec, period = -1;
  346. char *endp;
  347. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  348. if (*endp == '.') {
  349. int mult = 100;
  350. do {
  351. endp++;
  352. if (!isdigit(*endp))
  353. break;
  354. picosec += (*endp - '0') * mult;
  355. mult /= 10;
  356. } while (mult > 0);
  357. }
  358. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  359. if (ppr_to_ps[j] < picosec)
  360. continue;
  361. period = j;
  362. break;
  363. }
  364. if (period == -1)
  365. period = picosec / 4000;
  366. if (period > 0xff)
  367. period = 0xff;
  368. *periodp = period;
  369. return count;
  370. }
  371. static ssize_t
  372. show_spi_transport_period(struct class_device *cdev, char *buf)
  373. {
  374. struct scsi_target *starget = transport_class_to_starget(cdev);
  375. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  376. struct spi_internal *i = to_spi_internal(shost->transportt);
  377. struct spi_transport_attrs *tp =
  378. (struct spi_transport_attrs *)&starget->starget_data;
  379. if (i->f->get_period)
  380. i->f->get_period(starget);
  381. return show_spi_transport_period_helper(cdev, buf, tp->period);
  382. }
  383. static ssize_t
  384. store_spi_transport_period(struct class_device *cdev, const char *buf,
  385. size_t count)
  386. {
  387. struct scsi_target *starget = transport_class_to_starget(cdev);
  388. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  389. struct spi_internal *i = to_spi_internal(shost->transportt);
  390. struct spi_transport_attrs *tp =
  391. (struct spi_transport_attrs *)&starget->starget_data;
  392. int period, retval;
  393. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  394. if (period < tp->min_period)
  395. period = tp->min_period;
  396. i->f->set_period(starget, period);
  397. return retval;
  398. }
  399. static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR,
  400. show_spi_transport_period,
  401. store_spi_transport_period);
  402. static ssize_t
  403. show_spi_transport_min_period(struct class_device *cdev, char *buf)
  404. {
  405. struct scsi_target *starget = transport_class_to_starget(cdev);
  406. struct spi_transport_attrs *tp =
  407. (struct spi_transport_attrs *)&starget->starget_data;
  408. return show_spi_transport_period_helper(cdev, buf, tp->min_period);
  409. }
  410. static ssize_t
  411. store_spi_transport_min_period(struct class_device *cdev, const char *buf,
  412. size_t count)
  413. {
  414. struct scsi_target *starget = transport_class_to_starget(cdev);
  415. struct spi_transport_attrs *tp =
  416. (struct spi_transport_attrs *)&starget->starget_data;
  417. return store_spi_transport_period_helper(cdev, buf, count,
  418. &tp->min_period);
  419. }
  420. static CLASS_DEVICE_ATTR(min_period, S_IRUGO | S_IWUSR,
  421. show_spi_transport_min_period,
  422. store_spi_transport_min_period);
  423. static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
  424. {
  425. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  426. struct spi_internal *i = to_spi_internal(shost->transportt);
  427. if (i->f->get_signalling)
  428. i->f->get_signalling(shost);
  429. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  430. }
  431. static ssize_t store_spi_host_signalling(struct class_device *cdev,
  432. const char *buf, size_t count)
  433. {
  434. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  435. struct spi_internal *i = to_spi_internal(shost->transportt);
  436. enum spi_signal_type type = spi_signal_to_value(buf);
  437. if (type != SPI_SIGNAL_UNKNOWN)
  438. i->f->set_signalling(shost, type);
  439. return count;
  440. }
  441. static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR,
  442. show_spi_host_signalling,
  443. store_spi_host_signalling);
  444. #define DV_SET(x, y) \
  445. if(i->f->set_##x) \
  446. i->f->set_##x(sdev->sdev_target, y)
  447. enum spi_compare_returns {
  448. SPI_COMPARE_SUCCESS,
  449. SPI_COMPARE_FAILURE,
  450. SPI_COMPARE_SKIP_TEST,
  451. };
  452. /* This is for read/write Domain Validation: If the device supports
  453. * an echo buffer, we do read/write tests to it */
  454. static enum spi_compare_returns
  455. spi_dv_device_echo_buffer(struct scsi_request *sreq, u8 *buffer,
  456. u8 *ptr, const int retries)
  457. {
  458. struct scsi_device *sdev = sreq->sr_device;
  459. int len = ptr - buffer;
  460. int j, k, r;
  461. unsigned int pattern = 0x0000ffff;
  462. const char spi_write_buffer[] = {
  463. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  464. };
  465. const char spi_read_buffer[] = {
  466. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  467. };
  468. /* set up the pattern buffer. Doesn't matter if we spill
  469. * slightly beyond since that's where the read buffer is */
  470. for (j = 0; j < len; ) {
  471. /* fill the buffer with counting (test a) */
  472. for ( ; j < min(len, 32); j++)
  473. buffer[j] = j;
  474. k = j;
  475. /* fill the buffer with alternating words of 0x0 and
  476. * 0xffff (test b) */
  477. for ( ; j < min(len, k + 32); j += 2) {
  478. u16 *word = (u16 *)&buffer[j];
  479. *word = (j & 0x02) ? 0x0000 : 0xffff;
  480. }
  481. k = j;
  482. /* fill with crosstalk (alternating 0x5555 0xaaa)
  483. * (test c) */
  484. for ( ; j < min(len, k + 32); j += 2) {
  485. u16 *word = (u16 *)&buffer[j];
  486. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  487. }
  488. k = j;
  489. /* fill with shifting bits (test d) */
  490. for ( ; j < min(len, k + 32); j += 4) {
  491. u32 *word = (unsigned int *)&buffer[j];
  492. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  493. *word = pattern;
  494. pattern = (pattern << 1) | roll;
  495. }
  496. /* don't bother with random data (test e) */
  497. }
  498. for (r = 0; r < retries; r++) {
  499. sreq->sr_cmd_len = 0; /* wait_req to fill in */
  500. sreq->sr_data_direction = DMA_TO_DEVICE;
  501. spi_wait_req(sreq, spi_write_buffer, buffer, len);
  502. if(sreq->sr_result || !scsi_device_online(sdev)) {
  503. struct scsi_sense_hdr sshdr;
  504. scsi_device_set_state(sdev, SDEV_QUIESCE);
  505. if (scsi_request_normalize_sense(sreq, &sshdr)
  506. && sshdr.sense_key == ILLEGAL_REQUEST
  507. /* INVALID FIELD IN CDB */
  508. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  509. /* This would mean that the drive lied
  510. * to us about supporting an echo
  511. * buffer (unfortunately some Western
  512. * Digital drives do precisely this)
  513. */
  514. return SPI_COMPARE_SKIP_TEST;
  515. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Write Buffer failure %x\n", sreq->sr_result);
  516. return SPI_COMPARE_FAILURE;
  517. }
  518. memset(ptr, 0, len);
  519. sreq->sr_cmd_len = 0; /* wait_req to fill in */
  520. sreq->sr_data_direction = DMA_FROM_DEVICE;
  521. spi_wait_req(sreq, spi_read_buffer, ptr, len);
  522. scsi_device_set_state(sdev, SDEV_QUIESCE);
  523. if (memcmp(buffer, ptr, len) != 0)
  524. return SPI_COMPARE_FAILURE;
  525. }
  526. return SPI_COMPARE_SUCCESS;
  527. }
  528. /* This is for the simplest form of Domain Validation: a read test
  529. * on the inquiry data from the device */
  530. static enum spi_compare_returns
  531. spi_dv_device_compare_inquiry(struct scsi_request *sreq, u8 *buffer,
  532. u8 *ptr, const int retries)
  533. {
  534. int r;
  535. const int len = sreq->sr_device->inquiry_len;
  536. struct scsi_device *sdev = sreq->sr_device;
  537. const char spi_inquiry[] = {
  538. INQUIRY, 0, 0, 0, len, 0
  539. };
  540. for (r = 0; r < retries; r++) {
  541. sreq->sr_cmd_len = 0; /* wait_req to fill in */
  542. sreq->sr_data_direction = DMA_FROM_DEVICE;
  543. memset(ptr, 0, len);
  544. spi_wait_req(sreq, spi_inquiry, ptr, len);
  545. if(sreq->sr_result || !scsi_device_online(sdev)) {
  546. scsi_device_set_state(sdev, SDEV_QUIESCE);
  547. return SPI_COMPARE_FAILURE;
  548. }
  549. /* If we don't have the inquiry data already, the
  550. * first read gets it */
  551. if (ptr == buffer) {
  552. ptr += len;
  553. --r;
  554. continue;
  555. }
  556. if (memcmp(buffer, ptr, len) != 0)
  557. /* failure */
  558. return SPI_COMPARE_FAILURE;
  559. }
  560. return SPI_COMPARE_SUCCESS;
  561. }
  562. static enum spi_compare_returns
  563. spi_dv_retrain(struct scsi_request *sreq, u8 *buffer, u8 *ptr,
  564. enum spi_compare_returns
  565. (*compare_fn)(struct scsi_request *, u8 *, u8 *, int))
  566. {
  567. struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
  568. struct scsi_device *sdev = sreq->sr_device;
  569. int period = 0, prevperiod = 0;
  570. enum spi_compare_returns retval;
  571. for (;;) {
  572. int newperiod;
  573. retval = compare_fn(sreq, buffer, ptr, DV_LOOPS);
  574. if (retval == SPI_COMPARE_SUCCESS
  575. || retval == SPI_COMPARE_SKIP_TEST)
  576. break;
  577. /* OK, retrain, fallback */
  578. if (i->f->get_period)
  579. i->f->get_period(sdev->sdev_target);
  580. newperiod = spi_period(sdev->sdev_target);
  581. period = newperiod > period ? newperiod : period;
  582. if (period < 0x0d)
  583. period++;
  584. else
  585. period += period >> 1;
  586. if (unlikely(period > 0xff || period == prevperiod)) {
  587. /* Total failure; set to async and return */
  588. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Failure, dropping back to Asynchronous\n");
  589. DV_SET(offset, 0);
  590. return SPI_COMPARE_FAILURE;
  591. }
  592. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation detected failure, dropping back\n");
  593. DV_SET(period, period);
  594. prevperiod = period;
  595. }
  596. return retval;
  597. }
  598. static int
  599. spi_dv_device_get_echo_buffer(struct scsi_request *sreq, u8 *buffer)
  600. {
  601. int l;
  602. /* first off do a test unit ready. This can error out
  603. * because of reservations or some other reason. If it
  604. * fails, the device won't let us write to the echo buffer
  605. * so just return failure */
  606. const char spi_test_unit_ready[] = {
  607. TEST_UNIT_READY, 0, 0, 0, 0, 0
  608. };
  609. const char spi_read_buffer_descriptor[] = {
  610. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  611. };
  612. sreq->sr_cmd_len = 0;
  613. sreq->sr_data_direction = DMA_NONE;
  614. /* We send a set of three TURs to clear any outstanding
  615. * unit attention conditions if they exist (Otherwise the
  616. * buffer tests won't be happy). If the TUR still fails
  617. * (reservation conflict, device not ready, etc) just
  618. * skip the write tests */
  619. for (l = 0; ; l++) {
  620. spi_wait_req(sreq, spi_test_unit_ready, NULL, 0);
  621. if(sreq->sr_result) {
  622. if(l >= 3)
  623. return 0;
  624. } else {
  625. /* TUR succeeded */
  626. break;
  627. }
  628. }
  629. sreq->sr_cmd_len = 0;
  630. sreq->sr_data_direction = DMA_FROM_DEVICE;
  631. spi_wait_req(sreq, spi_read_buffer_descriptor, buffer, 4);
  632. if (sreq->sr_result)
  633. /* Device has no echo buffer */
  634. return 0;
  635. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  636. }
  637. static void
  638. spi_dv_device_internal(struct scsi_request *sreq, u8 *buffer)
  639. {
  640. struct spi_internal *i = to_spi_internal(sreq->sr_host->transportt);
  641. struct scsi_device *sdev = sreq->sr_device;
  642. struct scsi_target *starget = sdev->sdev_target;
  643. int len = sdev->inquiry_len;
  644. /* first set us up for narrow async */
  645. DV_SET(offset, 0);
  646. DV_SET(width, 0);
  647. if (spi_dv_device_compare_inquiry(sreq, buffer, buffer, DV_LOOPS)
  648. != SPI_COMPARE_SUCCESS) {
  649. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Domain Validation Initial Inquiry Failed\n");
  650. /* FIXME: should probably offline the device here? */
  651. return;
  652. }
  653. /* test width */
  654. if (i->f->set_width && spi_max_width(starget) && sdev->wdtr) {
  655. i->f->set_width(sdev->sdev_target, 1);
  656. printk("WIDTH IS %d\n", spi_max_width(starget));
  657. if (spi_dv_device_compare_inquiry(sreq, buffer,
  658. buffer + len,
  659. DV_LOOPS)
  660. != SPI_COMPARE_SUCCESS) {
  661. SPI_PRINTK(sdev->sdev_target, KERN_ERR, "Wide Transfers Fail\n");
  662. i->f->set_width(sdev->sdev_target, 0);
  663. }
  664. }
  665. if (!i->f->set_period)
  666. return;
  667. /* device can't handle synchronous */
  668. if(!sdev->ppr && !sdev->sdtr)
  669. return;
  670. /* see if the device has an echo buffer. If it does we can
  671. * do the SPI pattern write tests */
  672. len = 0;
  673. if (sdev->ppr)
  674. len = spi_dv_device_get_echo_buffer(sreq, buffer);
  675. retry:
  676. /* now set up to the maximum */
  677. DV_SET(offset, spi_max_offset(starget));
  678. DV_SET(period, spi_min_period(starget));
  679. if (len == 0) {
  680. SPI_PRINTK(sdev->sdev_target, KERN_INFO, "Domain Validation skipping write tests\n");
  681. spi_dv_retrain(sreq, buffer, buffer + len,
  682. spi_dv_device_compare_inquiry);
  683. return;
  684. }
  685. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  686. SPI_PRINTK(sdev->sdev_target, KERN_WARNING, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  687. len = SPI_MAX_ECHO_BUFFER_SIZE;
  688. }
  689. if (spi_dv_retrain(sreq, buffer, buffer + len,
  690. spi_dv_device_echo_buffer)
  691. == SPI_COMPARE_SKIP_TEST) {
  692. /* OK, the stupid drive can't do a write echo buffer
  693. * test after all, fall back to the read tests */
  694. len = 0;
  695. goto retry;
  696. }
  697. }
  698. /** spi_dv_device - Do Domain Validation on the device
  699. * @sdev: scsi device to validate
  700. *
  701. * Performs the domain validation on the given device in the
  702. * current execution thread. Since DV operations may sleep,
  703. * the current thread must have user context. Also no SCSI
  704. * related locks that would deadlock I/O issued by the DV may
  705. * be held.
  706. */
  707. void
  708. spi_dv_device(struct scsi_device *sdev)
  709. {
  710. struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
  711. struct scsi_target *starget = sdev->sdev_target;
  712. u8 *buffer;
  713. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  714. if (unlikely(!sreq))
  715. return;
  716. if (unlikely(scsi_device_get(sdev)))
  717. goto out_free_req;
  718. buffer = kmalloc(len, GFP_KERNEL);
  719. if (unlikely(!buffer))
  720. goto out_put;
  721. memset(buffer, 0, len);
  722. /* We need to verify that the actual device will quiesce; the
  723. * later target quiesce is just a nice to have */
  724. if (unlikely(scsi_device_quiesce(sdev)))
  725. goto out_free;
  726. scsi_target_quiesce(starget);
  727. spi_dv_pending(starget) = 1;
  728. down(&spi_dv_sem(starget));
  729. SPI_PRINTK(starget, KERN_INFO, "Beginning Domain Validation\n");
  730. spi_dv_device_internal(sreq, buffer);
  731. SPI_PRINTK(starget, KERN_INFO, "Ending Domain Validation\n");
  732. up(&spi_dv_sem(starget));
  733. spi_dv_pending(starget) = 0;
  734. scsi_target_resume(starget);
  735. spi_initial_dv(starget) = 1;
  736. out_free:
  737. kfree(buffer);
  738. out_put:
  739. scsi_device_put(sdev);
  740. out_free_req:
  741. scsi_release_request(sreq);
  742. }
  743. EXPORT_SYMBOL(spi_dv_device);
  744. struct work_queue_wrapper {
  745. struct work_struct work;
  746. struct scsi_device *sdev;
  747. };
  748. static void
  749. spi_dv_device_work_wrapper(void *data)
  750. {
  751. struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
  752. struct scsi_device *sdev = wqw->sdev;
  753. kfree(wqw);
  754. spi_dv_device(sdev);
  755. spi_dv_pending(sdev->sdev_target) = 0;
  756. scsi_device_put(sdev);
  757. }
  758. /**
  759. * spi_schedule_dv_device - schedule domain validation to occur on the device
  760. * @sdev: The device to validate
  761. *
  762. * Identical to spi_dv_device() above, except that the DV will be
  763. * scheduled to occur in a workqueue later. All memory allocations
  764. * are atomic, so may be called from any context including those holding
  765. * SCSI locks.
  766. */
  767. void
  768. spi_schedule_dv_device(struct scsi_device *sdev)
  769. {
  770. struct work_queue_wrapper *wqw =
  771. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  772. if (unlikely(!wqw))
  773. return;
  774. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  775. kfree(wqw);
  776. return;
  777. }
  778. /* Set pending early (dv_device doesn't check it, only sets it) */
  779. spi_dv_pending(sdev->sdev_target) = 1;
  780. if (unlikely(scsi_device_get(sdev))) {
  781. kfree(wqw);
  782. spi_dv_pending(sdev->sdev_target) = 0;
  783. return;
  784. }
  785. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper, wqw);
  786. wqw->sdev = sdev;
  787. schedule_work(&wqw->work);
  788. }
  789. EXPORT_SYMBOL(spi_schedule_dv_device);
  790. /**
  791. * spi_display_xfer_agreement - Print the current target transfer agreement
  792. * @starget: The target for which to display the agreement
  793. *
  794. * Each SPI port is required to maintain a transfer agreement for each
  795. * other port on the bus. This function prints a one-line summary of
  796. * the current agreement; more detailed information is available in sysfs.
  797. */
  798. void spi_display_xfer_agreement(struct scsi_target *starget)
  799. {
  800. struct spi_transport_attrs *tp;
  801. tp = (struct spi_transport_attrs *)&starget->starget_data;
  802. if (tp->offset > 0 && tp->period > 0) {
  803. unsigned int picosec, kb100;
  804. char *scsi = "FAST-?";
  805. char tmp[8];
  806. if (tp->period <= SPI_STATIC_PPR) {
  807. picosec = ppr_to_ps[tp->period];
  808. switch (tp->period) {
  809. case 7: scsi = "FAST-320"; break;
  810. case 8: scsi = "FAST-160"; break;
  811. case 9: scsi = "FAST-80"; break;
  812. case 10:
  813. case 11: scsi = "FAST-40"; break;
  814. case 12: scsi = "FAST-20"; break;
  815. }
  816. } else {
  817. picosec = tp->period * 4000;
  818. if (tp->period < 25)
  819. scsi = "FAST-20";
  820. else if (tp->period < 50)
  821. scsi = "FAST-10";
  822. else
  823. scsi = "FAST-5";
  824. }
  825. kb100 = (10000000 + picosec / 2) / picosec;
  826. if (tp->width)
  827. kb100 *= 2;
  828. sprint_frac(tmp, picosec, 1000);
  829. dev_info(&starget->dev,
  830. "%s %sSCSI %d.%d MB/s %s%s%s (%s ns, offset %d)\n",
  831. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  832. tp->dt ? "DT" : "ST", tp->iu ? " IU" : "",
  833. tp->qas ? " QAS" : "", tmp, tp->offset);
  834. } else {
  835. dev_info(&starget->dev, "%sasynchronous.\n",
  836. tp->width ? "wide " : "");
  837. }
  838. }
  839. EXPORT_SYMBOL(spi_display_xfer_agreement);
  840. #define SETUP_ATTRIBUTE(field) \
  841. i->private_attrs[count] = class_device_attr_##field; \
  842. if (!i->f->set_##field) { \
  843. i->private_attrs[count].attr.mode = S_IRUGO; \
  844. i->private_attrs[count].store = NULL; \
  845. } \
  846. i->attrs[count] = &i->private_attrs[count]; \
  847. if (i->f->show_##field) \
  848. count++
  849. #define SETUP_RELATED_ATTRIBUTE(field, rel_field) \
  850. i->private_attrs[count] = class_device_attr_##field; \
  851. if (!i->f->set_##rel_field) { \
  852. i->private_attrs[count].attr.mode = S_IRUGO; \
  853. i->private_attrs[count].store = NULL; \
  854. } \
  855. i->attrs[count] = &i->private_attrs[count]; \
  856. if (i->f->show_##rel_field) \
  857. count++
  858. #define SETUP_HOST_ATTRIBUTE(field) \
  859. i->private_host_attrs[count] = class_device_attr_##field; \
  860. if (!i->f->set_##field) { \
  861. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  862. i->private_host_attrs[count].store = NULL; \
  863. } \
  864. i->host_attrs[count] = &i->private_host_attrs[count]; \
  865. count++
  866. static int spi_device_match(struct attribute_container *cont,
  867. struct device *dev)
  868. {
  869. struct scsi_device *sdev;
  870. struct Scsi_Host *shost;
  871. if (!scsi_is_sdev_device(dev))
  872. return 0;
  873. sdev = to_scsi_device(dev);
  874. shost = sdev->host;
  875. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  876. != &spi_host_class.class)
  877. return 0;
  878. /* Note: this class has no device attributes, so it has
  879. * no per-HBA allocation and thus we don't need to distinguish
  880. * the attribute containers for the device */
  881. return 1;
  882. }
  883. static int spi_target_match(struct attribute_container *cont,
  884. struct device *dev)
  885. {
  886. struct Scsi_Host *shost;
  887. struct spi_internal *i;
  888. if (!scsi_is_target_device(dev))
  889. return 0;
  890. shost = dev_to_shost(dev->parent);
  891. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  892. != &spi_host_class.class)
  893. return 0;
  894. i = to_spi_internal(shost->transportt);
  895. return &i->t.target_attrs.ac == cont;
  896. }
  897. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  898. "spi_transport",
  899. spi_setup_transport_attrs,
  900. NULL,
  901. NULL);
  902. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  903. spi_device_match,
  904. spi_device_configure);
  905. struct scsi_transport_template *
  906. spi_attach_transport(struct spi_function_template *ft)
  907. {
  908. struct spi_internal *i = kmalloc(sizeof(struct spi_internal),
  909. GFP_KERNEL);
  910. int count = 0;
  911. if (unlikely(!i))
  912. return NULL;
  913. memset(i, 0, sizeof(struct spi_internal));
  914. i->t.target_attrs.ac.class = &spi_transport_class.class;
  915. i->t.target_attrs.ac.attrs = &i->attrs[0];
  916. i->t.target_attrs.ac.match = spi_target_match;
  917. transport_container_register(&i->t.target_attrs);
  918. i->t.target_size = sizeof(struct spi_transport_attrs);
  919. i->t.host_attrs.ac.class = &spi_host_class.class;
  920. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  921. i->t.host_attrs.ac.match = spi_host_match;
  922. transport_container_register(&i->t.host_attrs);
  923. i->t.host_size = sizeof(struct spi_host_attrs);
  924. i->f = ft;
  925. SETUP_ATTRIBUTE(period);
  926. SETUP_RELATED_ATTRIBUTE(min_period, period);
  927. SETUP_ATTRIBUTE(offset);
  928. SETUP_RELATED_ATTRIBUTE(max_offset, offset);
  929. SETUP_ATTRIBUTE(width);
  930. SETUP_RELATED_ATTRIBUTE(max_width, width);
  931. SETUP_ATTRIBUTE(iu);
  932. SETUP_ATTRIBUTE(dt);
  933. SETUP_ATTRIBUTE(qas);
  934. SETUP_ATTRIBUTE(wr_flow);
  935. SETUP_ATTRIBUTE(rd_strm);
  936. SETUP_ATTRIBUTE(rti);
  937. SETUP_ATTRIBUTE(pcomp_en);
  938. /* if you add an attribute but forget to increase SPI_NUM_ATTRS
  939. * this bug will trigger */
  940. BUG_ON(count > SPI_NUM_ATTRS);
  941. i->attrs[count++] = &class_device_attr_revalidate;
  942. i->attrs[count] = NULL;
  943. count = 0;
  944. SETUP_HOST_ATTRIBUTE(signalling);
  945. BUG_ON(count > SPI_HOST_ATTRS);
  946. i->host_attrs[count] = NULL;
  947. return &i->t;
  948. }
  949. EXPORT_SYMBOL(spi_attach_transport);
  950. void spi_release_transport(struct scsi_transport_template *t)
  951. {
  952. struct spi_internal *i = to_spi_internal(t);
  953. transport_container_unregister(&i->t.target_attrs);
  954. transport_container_unregister(&i->t.host_attrs);
  955. kfree(i);
  956. }
  957. EXPORT_SYMBOL(spi_release_transport);
  958. static __init int spi_transport_init(void)
  959. {
  960. int error = transport_class_register(&spi_transport_class);
  961. if (error)
  962. return error;
  963. error = anon_transport_class_register(&spi_device_class);
  964. return transport_class_register(&spi_host_class);
  965. }
  966. static void __exit spi_transport_exit(void)
  967. {
  968. transport_class_unregister(&spi_transport_class);
  969. anon_transport_class_unregister(&spi_device_class);
  970. transport_class_unregister(&spi_host_class);
  971. }
  972. MODULE_AUTHOR("Martin Hicks");
  973. MODULE_DESCRIPTION("SPI Transport Attributes");
  974. MODULE_LICENSE("GPL");
  975. module_init(spi_transport_init);
  976. module_exit(spi_transport_exit);