scsi_transport_spi.c 33 KB

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