scsi_transport_spi.c 34 KB

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