scsi_transport_spi.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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 <linux/mutex.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_in_progress(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_in_progress)
  46. #define spi_dv_mutex(x) (((struct spi_transport_attrs *)&(x)->starget_data)->dv_mutex)
  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, SCSI_SENSE_BUFFERSIZE,
  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 < ARRAY_SIZE(signal_types); 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 < ARRAY_SIZE(signal_types); 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_dv_in_progress(starget) = 0;
  211. spi_initial_dv(starget) = 0;
  212. mutex_init(&spi_dv_mutex(starget));
  213. return 0;
  214. }
  215. #define spi_transport_show_simple(field, format_string) \
  216. \
  217. static ssize_t \
  218. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  219. { \
  220. struct scsi_target *starget = transport_class_to_starget(cdev); \
  221. struct spi_transport_attrs *tp; \
  222. \
  223. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  224. return snprintf(buf, 20, format_string, tp->field); \
  225. }
  226. #define spi_transport_store_simple(field, format_string) \
  227. \
  228. static ssize_t \
  229. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  230. size_t count) \
  231. { \
  232. int val; \
  233. struct scsi_target *starget = transport_class_to_starget(cdev); \
  234. struct spi_transport_attrs *tp; \
  235. \
  236. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  237. val = simple_strtoul(buf, NULL, 0); \
  238. tp->field = val; \
  239. return count; \
  240. }
  241. #define spi_transport_show_function(field, format_string) \
  242. \
  243. static ssize_t \
  244. show_spi_transport_##field(struct class_device *cdev, char *buf) \
  245. { \
  246. struct scsi_target *starget = transport_class_to_starget(cdev); \
  247. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  248. struct spi_transport_attrs *tp; \
  249. struct spi_internal *i = to_spi_internal(shost->transportt); \
  250. tp = (struct spi_transport_attrs *)&starget->starget_data; \
  251. if (i->f->get_##field) \
  252. i->f->get_##field(starget); \
  253. return snprintf(buf, 20, format_string, tp->field); \
  254. }
  255. #define spi_transport_store_function(field, format_string) \
  256. static ssize_t \
  257. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  258. size_t count) \
  259. { \
  260. int val; \
  261. struct scsi_target *starget = transport_class_to_starget(cdev); \
  262. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  263. struct spi_internal *i = to_spi_internal(shost->transportt); \
  264. \
  265. val = simple_strtoul(buf, NULL, 0); \
  266. i->f->set_##field(starget, val); \
  267. return count; \
  268. }
  269. #define spi_transport_store_max(field, format_string) \
  270. static ssize_t \
  271. store_spi_transport_##field(struct class_device *cdev, const char *buf, \
  272. size_t count) \
  273. { \
  274. int val; \
  275. struct scsi_target *starget = transport_class_to_starget(cdev); \
  276. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  277. struct spi_internal *i = to_spi_internal(shost->transportt); \
  278. struct spi_transport_attrs *tp \
  279. = (struct spi_transport_attrs *)&starget->starget_data; \
  280. \
  281. val = simple_strtoul(buf, NULL, 0); \
  282. if (val > tp->max_##field) \
  283. val = tp->max_##field; \
  284. i->f->set_##field(starget, val); \
  285. return count; \
  286. }
  287. #define spi_transport_rd_attr(field, format_string) \
  288. spi_transport_show_function(field, format_string) \
  289. spi_transport_store_function(field, format_string) \
  290. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  291. show_spi_transport_##field, \
  292. store_spi_transport_##field);
  293. #define spi_transport_simple_attr(field, format_string) \
  294. spi_transport_show_simple(field, format_string) \
  295. spi_transport_store_simple(field, format_string) \
  296. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  297. show_spi_transport_##field, \
  298. store_spi_transport_##field);
  299. #define spi_transport_max_attr(field, format_string) \
  300. spi_transport_show_function(field, format_string) \
  301. spi_transport_store_max(field, format_string) \
  302. spi_transport_simple_attr(max_##field, format_string) \
  303. static CLASS_DEVICE_ATTR(field, S_IRUGO | S_IWUSR, \
  304. show_spi_transport_##field, \
  305. store_spi_transport_##field);
  306. /* The Parallel SCSI Tranport Attributes: */
  307. spi_transport_max_attr(offset, "%d\n");
  308. spi_transport_max_attr(width, "%d\n");
  309. spi_transport_rd_attr(iu, "%d\n");
  310. spi_transport_rd_attr(dt, "%d\n");
  311. spi_transport_rd_attr(qas, "%d\n");
  312. spi_transport_rd_attr(wr_flow, "%d\n");
  313. spi_transport_rd_attr(rd_strm, "%d\n");
  314. spi_transport_rd_attr(rti, "%d\n");
  315. spi_transport_rd_attr(pcomp_en, "%d\n");
  316. spi_transport_rd_attr(hold_mcs, "%d\n");
  317. /* we only care about the first child device so we return 1 */
  318. static int child_iter(struct device *dev, void *data)
  319. {
  320. struct scsi_device *sdev = to_scsi_device(dev);
  321. spi_dv_device(sdev);
  322. return 1;
  323. }
  324. static ssize_t
  325. store_spi_revalidate(struct class_device *cdev, const char *buf, size_t count)
  326. {
  327. struct scsi_target *starget = transport_class_to_starget(cdev);
  328. device_for_each_child(&starget->dev, NULL, child_iter);
  329. return count;
  330. }
  331. static CLASS_DEVICE_ATTR(revalidate, S_IWUSR, NULL, store_spi_revalidate);
  332. /* Translate the period into ns according to the current spec
  333. * for SDTR/PPR messages */
  334. static int period_to_str(char *buf, int period)
  335. {
  336. int len, picosec;
  337. if (period < 0 || period > 0xff) {
  338. picosec = -1;
  339. } else if (period <= SPI_STATIC_PPR) {
  340. picosec = ppr_to_ps[period];
  341. } else {
  342. picosec = period * 4000;
  343. }
  344. if (picosec == -1) {
  345. len = sprintf(buf, "reserved");
  346. } else {
  347. len = sprint_frac(buf, picosec, 1000);
  348. }
  349. return len;
  350. }
  351. static ssize_t
  352. show_spi_transport_period_helper(char *buf, int period)
  353. {
  354. int len = period_to_str(buf, period);
  355. buf[len++] = '\n';
  356. buf[len] = '\0';
  357. return len;
  358. }
  359. static ssize_t
  360. store_spi_transport_period_helper(struct class_device *cdev, const char *buf,
  361. size_t count, int *periodp)
  362. {
  363. int j, picosec, period = -1;
  364. char *endp;
  365. picosec = simple_strtoul(buf, &endp, 10) * 1000;
  366. if (*endp == '.') {
  367. int mult = 100;
  368. do {
  369. endp++;
  370. if (!isdigit(*endp))
  371. break;
  372. picosec += (*endp - '0') * mult;
  373. mult /= 10;
  374. } while (mult > 0);
  375. }
  376. for (j = 0; j <= SPI_STATIC_PPR; j++) {
  377. if (ppr_to_ps[j] < picosec)
  378. continue;
  379. period = j;
  380. break;
  381. }
  382. if (period == -1)
  383. period = picosec / 4000;
  384. if (period > 0xff)
  385. period = 0xff;
  386. *periodp = period;
  387. return count;
  388. }
  389. static ssize_t
  390. show_spi_transport_period(struct class_device *cdev, char *buf)
  391. {
  392. struct scsi_target *starget = transport_class_to_starget(cdev);
  393. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  394. struct spi_internal *i = to_spi_internal(shost->transportt);
  395. struct spi_transport_attrs *tp =
  396. (struct spi_transport_attrs *)&starget->starget_data;
  397. if (i->f->get_period)
  398. i->f->get_period(starget);
  399. return show_spi_transport_period_helper(buf, tp->period);
  400. }
  401. static ssize_t
  402. store_spi_transport_period(struct class_device *cdev, const char *buf,
  403. size_t count)
  404. {
  405. struct scsi_target *starget = transport_class_to_starget(cdev);
  406. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  407. struct spi_internal *i = to_spi_internal(shost->transportt);
  408. struct spi_transport_attrs *tp =
  409. (struct spi_transport_attrs *)&starget->starget_data;
  410. int period, retval;
  411. retval = store_spi_transport_period_helper(cdev, buf, count, &period);
  412. if (period < tp->min_period)
  413. period = tp->min_period;
  414. i->f->set_period(starget, period);
  415. return retval;
  416. }
  417. static CLASS_DEVICE_ATTR(period, S_IRUGO | S_IWUSR,
  418. show_spi_transport_period,
  419. store_spi_transport_period);
  420. static ssize_t
  421. show_spi_transport_min_period(struct class_device *cdev, char *buf)
  422. {
  423. struct scsi_target *starget = transport_class_to_starget(cdev);
  424. struct spi_transport_attrs *tp =
  425. (struct spi_transport_attrs *)&starget->starget_data;
  426. return show_spi_transport_period_helper(buf, tp->min_period);
  427. }
  428. static ssize_t
  429. store_spi_transport_min_period(struct class_device *cdev, const char *buf,
  430. size_t count)
  431. {
  432. struct scsi_target *starget = transport_class_to_starget(cdev);
  433. struct spi_transport_attrs *tp =
  434. (struct spi_transport_attrs *)&starget->starget_data;
  435. return store_spi_transport_period_helper(cdev, buf, count,
  436. &tp->min_period);
  437. }
  438. static CLASS_DEVICE_ATTR(min_period, S_IRUGO | S_IWUSR,
  439. show_spi_transport_min_period,
  440. store_spi_transport_min_period);
  441. static ssize_t show_spi_host_signalling(struct class_device *cdev, char *buf)
  442. {
  443. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  444. struct spi_internal *i = to_spi_internal(shost->transportt);
  445. if (i->f->get_signalling)
  446. i->f->get_signalling(shost);
  447. return sprintf(buf, "%s\n", spi_signal_to_string(spi_signalling(shost)));
  448. }
  449. static ssize_t store_spi_host_signalling(struct class_device *cdev,
  450. const char *buf, size_t count)
  451. {
  452. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  453. struct spi_internal *i = to_spi_internal(shost->transportt);
  454. enum spi_signal_type type = spi_signal_to_value(buf);
  455. if (type != SPI_SIGNAL_UNKNOWN)
  456. i->f->set_signalling(shost, type);
  457. return count;
  458. }
  459. static CLASS_DEVICE_ATTR(signalling, S_IRUGO | S_IWUSR,
  460. show_spi_host_signalling,
  461. store_spi_host_signalling);
  462. #define DV_SET(x, y) \
  463. if(i->f->set_##x) \
  464. i->f->set_##x(sdev->sdev_target, y)
  465. enum spi_compare_returns {
  466. SPI_COMPARE_SUCCESS,
  467. SPI_COMPARE_FAILURE,
  468. SPI_COMPARE_SKIP_TEST,
  469. };
  470. /* This is for read/write Domain Validation: If the device supports
  471. * an echo buffer, we do read/write tests to it */
  472. static enum spi_compare_returns
  473. spi_dv_device_echo_buffer(struct scsi_device *sdev, u8 *buffer,
  474. u8 *ptr, const int retries)
  475. {
  476. int len = ptr - buffer;
  477. int j, k, r, result;
  478. unsigned int pattern = 0x0000ffff;
  479. struct scsi_sense_hdr sshdr;
  480. const char spi_write_buffer[] = {
  481. WRITE_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  482. };
  483. const char spi_read_buffer[] = {
  484. READ_BUFFER, 0x0a, 0, 0, 0, 0, 0, len >> 8, len & 0xff, 0
  485. };
  486. /* set up the pattern buffer. Doesn't matter if we spill
  487. * slightly beyond since that's where the read buffer is */
  488. for (j = 0; j < len; ) {
  489. /* fill the buffer with counting (test a) */
  490. for ( ; j < min(len, 32); j++)
  491. buffer[j] = j;
  492. k = j;
  493. /* fill the buffer with alternating words of 0x0 and
  494. * 0xffff (test b) */
  495. for ( ; j < min(len, k + 32); j += 2) {
  496. u16 *word = (u16 *)&buffer[j];
  497. *word = (j & 0x02) ? 0x0000 : 0xffff;
  498. }
  499. k = j;
  500. /* fill with crosstalk (alternating 0x5555 0xaaa)
  501. * (test c) */
  502. for ( ; j < min(len, k + 32); j += 2) {
  503. u16 *word = (u16 *)&buffer[j];
  504. *word = (j & 0x02) ? 0x5555 : 0xaaaa;
  505. }
  506. k = j;
  507. /* fill with shifting bits (test d) */
  508. for ( ; j < min(len, k + 32); j += 4) {
  509. u32 *word = (unsigned int *)&buffer[j];
  510. u32 roll = (pattern & 0x80000000) ? 1 : 0;
  511. *word = pattern;
  512. pattern = (pattern << 1) | roll;
  513. }
  514. /* don't bother with random data (test e) */
  515. }
  516. for (r = 0; r < retries; r++) {
  517. result = spi_execute(sdev, spi_write_buffer, DMA_TO_DEVICE,
  518. buffer, len, &sshdr);
  519. if(result || !scsi_device_online(sdev)) {
  520. scsi_device_set_state(sdev, SDEV_QUIESCE);
  521. if (scsi_sense_valid(&sshdr)
  522. && sshdr.sense_key == ILLEGAL_REQUEST
  523. /* INVALID FIELD IN CDB */
  524. && sshdr.asc == 0x24 && sshdr.ascq == 0x00)
  525. /* This would mean that the drive lied
  526. * to us about supporting an echo
  527. * buffer (unfortunately some Western
  528. * Digital drives do precisely this)
  529. */
  530. return SPI_COMPARE_SKIP_TEST;
  531. sdev_printk(KERN_ERR, sdev, "Write Buffer failure %x\n", result);
  532. return SPI_COMPARE_FAILURE;
  533. }
  534. memset(ptr, 0, len);
  535. spi_execute(sdev, spi_read_buffer, DMA_FROM_DEVICE,
  536. ptr, len, NULL);
  537. scsi_device_set_state(sdev, SDEV_QUIESCE);
  538. if (memcmp(buffer, ptr, len) != 0)
  539. return SPI_COMPARE_FAILURE;
  540. }
  541. return SPI_COMPARE_SUCCESS;
  542. }
  543. /* This is for the simplest form of Domain Validation: a read test
  544. * on the inquiry data from the device */
  545. static enum spi_compare_returns
  546. spi_dv_device_compare_inquiry(struct scsi_device *sdev, u8 *buffer,
  547. u8 *ptr, const int retries)
  548. {
  549. int r, result;
  550. const int len = sdev->inquiry_len;
  551. const char spi_inquiry[] = {
  552. INQUIRY, 0, 0, 0, len, 0
  553. };
  554. for (r = 0; r < retries; r++) {
  555. memset(ptr, 0, len);
  556. result = spi_execute(sdev, spi_inquiry, DMA_FROM_DEVICE,
  557. ptr, len, NULL);
  558. if(result || !scsi_device_online(sdev)) {
  559. scsi_device_set_state(sdev, SDEV_QUIESCE);
  560. return SPI_COMPARE_FAILURE;
  561. }
  562. /* If we don't have the inquiry data already, the
  563. * first read gets it */
  564. if (ptr == buffer) {
  565. ptr += len;
  566. --r;
  567. continue;
  568. }
  569. if (memcmp(buffer, ptr, len) != 0)
  570. /* failure */
  571. return SPI_COMPARE_FAILURE;
  572. }
  573. return SPI_COMPARE_SUCCESS;
  574. }
  575. static enum spi_compare_returns
  576. spi_dv_retrain(struct scsi_device *sdev, u8 *buffer, u8 *ptr,
  577. enum spi_compare_returns
  578. (*compare_fn)(struct scsi_device *, u8 *, u8 *, int))
  579. {
  580. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  581. struct scsi_target *starget = sdev->sdev_target;
  582. int period = 0, prevperiod = 0;
  583. enum spi_compare_returns retval;
  584. for (;;) {
  585. int newperiod;
  586. retval = compare_fn(sdev, buffer, ptr, DV_LOOPS);
  587. if (retval == SPI_COMPARE_SUCCESS
  588. || retval == SPI_COMPARE_SKIP_TEST)
  589. break;
  590. /* OK, retrain, fallback */
  591. if (i->f->get_iu)
  592. i->f->get_iu(starget);
  593. if (i->f->get_qas)
  594. i->f->get_qas(starget);
  595. if (i->f->get_period)
  596. i->f->get_period(sdev->sdev_target);
  597. /* Here's the fallback sequence; first try turning off
  598. * IU, then QAS (if we can control them), then finally
  599. * fall down the periods */
  600. if (i->f->set_iu && spi_iu(starget)) {
  601. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Information Units\n");
  602. DV_SET(iu, 0);
  603. } else if (i->f->set_qas && spi_qas(starget)) {
  604. starget_printk(KERN_ERR, starget, "Domain Validation Disabing Quick Arbitration and Selection\n");
  605. DV_SET(qas, 0);
  606. } else {
  607. newperiod = spi_period(starget);
  608. period = newperiod > period ? newperiod : period;
  609. if (period < 0x0d)
  610. period++;
  611. else
  612. period += period >> 1;
  613. if (unlikely(period > 0xff || period == prevperiod)) {
  614. /* Total failure; set to async and return */
  615. starget_printk(KERN_ERR, starget, "Domain Validation Failure, dropping back to Asynchronous\n");
  616. DV_SET(offset, 0);
  617. return SPI_COMPARE_FAILURE;
  618. }
  619. starget_printk(KERN_ERR, starget, "Domain Validation detected failure, dropping back\n");
  620. DV_SET(period, period);
  621. prevperiod = period;
  622. }
  623. }
  624. return retval;
  625. }
  626. static int
  627. spi_dv_device_get_echo_buffer(struct scsi_device *sdev, u8 *buffer)
  628. {
  629. int l, result;
  630. /* first off do a test unit ready. This can error out
  631. * because of reservations or some other reason. If it
  632. * fails, the device won't let us write to the echo buffer
  633. * so just return failure */
  634. const char spi_test_unit_ready[] = {
  635. TEST_UNIT_READY, 0, 0, 0, 0, 0
  636. };
  637. const char spi_read_buffer_descriptor[] = {
  638. READ_BUFFER, 0x0b, 0, 0, 0, 0, 0, 0, 4, 0
  639. };
  640. /* We send a set of three TURs to clear any outstanding
  641. * unit attention conditions if they exist (Otherwise the
  642. * buffer tests won't be happy). If the TUR still fails
  643. * (reservation conflict, device not ready, etc) just
  644. * skip the write tests */
  645. for (l = 0; ; l++) {
  646. result = spi_execute(sdev, spi_test_unit_ready, DMA_NONE,
  647. NULL, 0, NULL);
  648. if(result) {
  649. if(l >= 3)
  650. return 0;
  651. } else {
  652. /* TUR succeeded */
  653. break;
  654. }
  655. }
  656. result = spi_execute(sdev, spi_read_buffer_descriptor,
  657. DMA_FROM_DEVICE, buffer, 4, NULL);
  658. if (result)
  659. /* Device has no echo buffer */
  660. return 0;
  661. return buffer[3] + ((buffer[2] & 0x1f) << 8);
  662. }
  663. static void
  664. spi_dv_device_internal(struct scsi_device *sdev, u8 *buffer)
  665. {
  666. struct spi_internal *i = to_spi_internal(sdev->host->transportt);
  667. struct scsi_target *starget = sdev->sdev_target;
  668. struct Scsi_Host *shost = sdev->host;
  669. int len = sdev->inquiry_len;
  670. int min_period = spi_min_period(starget);
  671. int max_width = spi_max_width(starget);
  672. /* first set us up for narrow async */
  673. DV_SET(offset, 0);
  674. DV_SET(width, 0);
  675. if (spi_dv_device_compare_inquiry(sdev, buffer, buffer, DV_LOOPS)
  676. != SPI_COMPARE_SUCCESS) {
  677. starget_printk(KERN_ERR, starget, "Domain Validation Initial Inquiry Failed\n");
  678. /* FIXME: should probably offline the device here? */
  679. return;
  680. }
  681. if (!scsi_device_wide(sdev)) {
  682. spi_max_width(starget) = 0;
  683. max_width = 0;
  684. }
  685. /* test width */
  686. if (i->f->set_width && max_width) {
  687. i->f->set_width(starget, 1);
  688. if (spi_dv_device_compare_inquiry(sdev, buffer,
  689. buffer + len,
  690. DV_LOOPS)
  691. != SPI_COMPARE_SUCCESS) {
  692. starget_printk(KERN_ERR, starget, "Wide Transfers Fail\n");
  693. i->f->set_width(starget, 0);
  694. /* Make sure we don't force wide back on by asking
  695. * for a transfer period that requires it */
  696. max_width = 0;
  697. if (min_period < 10)
  698. min_period = 10;
  699. }
  700. }
  701. if (!i->f->set_period)
  702. return;
  703. /* device can't handle synchronous */
  704. if (!scsi_device_sync(sdev) && !scsi_device_dt(sdev))
  705. return;
  706. /* len == -1 is the signal that we need to ascertain the
  707. * presence of an echo buffer before trying to use it. len ==
  708. * 0 means we don't have an echo buffer */
  709. len = -1;
  710. retry:
  711. /* now set up to the maximum */
  712. DV_SET(offset, spi_max_offset(starget));
  713. DV_SET(period, min_period);
  714. /* try QAS requests; this should be harmless to set if the
  715. * target supports it */
  716. if (scsi_device_qas(sdev)) {
  717. DV_SET(qas, 1);
  718. } else {
  719. DV_SET(qas, 0);
  720. }
  721. if (scsi_device_ius(sdev) && min_period < 9) {
  722. /* This u320 (or u640). Set IU transfers */
  723. DV_SET(iu, 1);
  724. /* Then set the optional parameters */
  725. DV_SET(rd_strm, 1);
  726. DV_SET(wr_flow, 1);
  727. DV_SET(rti, 1);
  728. if (min_period == 8)
  729. DV_SET(pcomp_en, 1);
  730. } else {
  731. DV_SET(iu, 0);
  732. }
  733. /* now that we've done all this, actually check the bus
  734. * signal type (if known). Some devices are stupid on
  735. * a SE bus and still claim they can try LVD only settings */
  736. if (i->f->get_signalling)
  737. i->f->get_signalling(shost);
  738. if (spi_signalling(shost) == SPI_SIGNAL_SE ||
  739. spi_signalling(shost) == SPI_SIGNAL_HVD ||
  740. !scsi_device_dt(sdev)) {
  741. DV_SET(dt, 0);
  742. } else {
  743. DV_SET(dt, 1);
  744. }
  745. /* set width last because it will pull all the other
  746. * parameters down to required values */
  747. DV_SET(width, max_width);
  748. /* Do the read only INQUIRY tests */
  749. spi_dv_retrain(sdev, buffer, buffer + sdev->inquiry_len,
  750. spi_dv_device_compare_inquiry);
  751. /* See if we actually managed to negotiate and sustain DT */
  752. if (i->f->get_dt)
  753. i->f->get_dt(starget);
  754. /* see if the device has an echo buffer. If it does we can do
  755. * the SPI pattern write tests. Because of some broken
  756. * devices, we *only* try this on a device that has actually
  757. * negotiated DT */
  758. if (len == -1 && spi_dt(starget))
  759. len = spi_dv_device_get_echo_buffer(sdev, buffer);
  760. if (len <= 0) {
  761. starget_printk(KERN_INFO, starget, "Domain Validation skipping write tests\n");
  762. return;
  763. }
  764. if (len > SPI_MAX_ECHO_BUFFER_SIZE) {
  765. starget_printk(KERN_WARNING, starget, "Echo buffer size %d is too big, trimming to %d\n", len, SPI_MAX_ECHO_BUFFER_SIZE);
  766. len = SPI_MAX_ECHO_BUFFER_SIZE;
  767. }
  768. if (spi_dv_retrain(sdev, buffer, buffer + len,
  769. spi_dv_device_echo_buffer)
  770. == SPI_COMPARE_SKIP_TEST) {
  771. /* OK, the stupid drive can't do a write echo buffer
  772. * test after all, fall back to the read tests */
  773. len = 0;
  774. goto retry;
  775. }
  776. }
  777. /** spi_dv_device - Do Domain Validation on the device
  778. * @sdev: scsi device to validate
  779. *
  780. * Performs the domain validation on the given device in the
  781. * current execution thread. Since DV operations may sleep,
  782. * the current thread must have user context. Also no SCSI
  783. * related locks that would deadlock I/O issued by the DV may
  784. * be held.
  785. */
  786. void
  787. spi_dv_device(struct scsi_device *sdev)
  788. {
  789. struct scsi_target *starget = sdev->sdev_target;
  790. u8 *buffer;
  791. const int len = SPI_MAX_ECHO_BUFFER_SIZE*2;
  792. if (unlikely(scsi_device_get(sdev)))
  793. return;
  794. if (unlikely(spi_dv_in_progress(starget)))
  795. return;
  796. spi_dv_in_progress(starget) = 1;
  797. buffer = kzalloc(len, GFP_KERNEL);
  798. if (unlikely(!buffer))
  799. goto out_put;
  800. /* We need to verify that the actual device will quiesce; the
  801. * later target quiesce is just a nice to have */
  802. if (unlikely(scsi_device_quiesce(sdev)))
  803. goto out_free;
  804. scsi_target_quiesce(starget);
  805. spi_dv_pending(starget) = 1;
  806. mutex_lock(&spi_dv_mutex(starget));
  807. starget_printk(KERN_INFO, starget, "Beginning Domain Validation\n");
  808. spi_dv_device_internal(sdev, buffer);
  809. starget_printk(KERN_INFO, starget, "Ending Domain Validation\n");
  810. mutex_unlock(&spi_dv_mutex(starget));
  811. spi_dv_pending(starget) = 0;
  812. scsi_target_resume(starget);
  813. spi_initial_dv(starget) = 1;
  814. out_free:
  815. kfree(buffer);
  816. out_put:
  817. spi_dv_in_progress(starget) = 0;
  818. scsi_device_put(sdev);
  819. }
  820. EXPORT_SYMBOL(spi_dv_device);
  821. struct work_queue_wrapper {
  822. struct work_struct work;
  823. struct scsi_device *sdev;
  824. };
  825. static void
  826. spi_dv_device_work_wrapper(struct work_struct *work)
  827. {
  828. struct work_queue_wrapper *wqw =
  829. container_of(work, struct work_queue_wrapper, work);
  830. struct scsi_device *sdev = wqw->sdev;
  831. kfree(wqw);
  832. spi_dv_device(sdev);
  833. spi_dv_pending(sdev->sdev_target) = 0;
  834. scsi_device_put(sdev);
  835. }
  836. /**
  837. * spi_schedule_dv_device - schedule domain validation to occur on the device
  838. * @sdev: The device to validate
  839. *
  840. * Identical to spi_dv_device() above, except that the DV will be
  841. * scheduled to occur in a workqueue later. All memory allocations
  842. * are atomic, so may be called from any context including those holding
  843. * SCSI locks.
  844. */
  845. void
  846. spi_schedule_dv_device(struct scsi_device *sdev)
  847. {
  848. struct work_queue_wrapper *wqw =
  849. kmalloc(sizeof(struct work_queue_wrapper), GFP_ATOMIC);
  850. if (unlikely(!wqw))
  851. return;
  852. if (unlikely(spi_dv_pending(sdev->sdev_target))) {
  853. kfree(wqw);
  854. return;
  855. }
  856. /* Set pending early (dv_device doesn't check it, only sets it) */
  857. spi_dv_pending(sdev->sdev_target) = 1;
  858. if (unlikely(scsi_device_get(sdev))) {
  859. kfree(wqw);
  860. spi_dv_pending(sdev->sdev_target) = 0;
  861. return;
  862. }
  863. INIT_WORK(&wqw->work, spi_dv_device_work_wrapper);
  864. wqw->sdev = sdev;
  865. schedule_work(&wqw->work);
  866. }
  867. EXPORT_SYMBOL(spi_schedule_dv_device);
  868. /**
  869. * spi_display_xfer_agreement - Print the current target transfer agreement
  870. * @starget: The target for which to display the agreement
  871. *
  872. * Each SPI port is required to maintain a transfer agreement for each
  873. * other port on the bus. This function prints a one-line summary of
  874. * the current agreement; more detailed information is available in sysfs.
  875. */
  876. void spi_display_xfer_agreement(struct scsi_target *starget)
  877. {
  878. struct spi_transport_attrs *tp;
  879. tp = (struct spi_transport_attrs *)&starget->starget_data;
  880. if (tp->offset > 0 && tp->period > 0) {
  881. unsigned int picosec, kb100;
  882. char *scsi = "FAST-?";
  883. char tmp[8];
  884. if (tp->period <= SPI_STATIC_PPR) {
  885. picosec = ppr_to_ps[tp->period];
  886. switch (tp->period) {
  887. case 7: scsi = "FAST-320"; break;
  888. case 8: scsi = "FAST-160"; break;
  889. case 9: scsi = "FAST-80"; break;
  890. case 10:
  891. case 11: scsi = "FAST-40"; break;
  892. case 12: scsi = "FAST-20"; break;
  893. }
  894. } else {
  895. picosec = tp->period * 4000;
  896. if (tp->period < 25)
  897. scsi = "FAST-20";
  898. else if (tp->period < 50)
  899. scsi = "FAST-10";
  900. else
  901. scsi = "FAST-5";
  902. }
  903. kb100 = (10000000 + picosec / 2) / picosec;
  904. if (tp->width)
  905. kb100 *= 2;
  906. sprint_frac(tmp, picosec, 1000);
  907. dev_info(&starget->dev,
  908. "%s %sSCSI %d.%d MB/s %s%s%s%s%s%s%s%s (%s ns, offset %d)\n",
  909. scsi, tp->width ? "WIDE " : "", kb100/10, kb100 % 10,
  910. tp->dt ? "DT" : "ST",
  911. tp->iu ? " IU" : "",
  912. tp->qas ? " QAS" : "",
  913. tp->rd_strm ? " RDSTRM" : "",
  914. tp->rti ? " RTI" : "",
  915. tp->wr_flow ? " WRFLOW" : "",
  916. tp->pcomp_en ? " PCOMP" : "",
  917. tp->hold_mcs ? " HMCS" : "",
  918. tmp, tp->offset);
  919. } else {
  920. dev_info(&starget->dev, "%sasynchronous\n",
  921. tp->width ? "wide " : "");
  922. }
  923. }
  924. EXPORT_SYMBOL(spi_display_xfer_agreement);
  925. int spi_populate_width_msg(unsigned char *msg, int width)
  926. {
  927. msg[0] = EXTENDED_MESSAGE;
  928. msg[1] = 2;
  929. msg[2] = EXTENDED_WDTR;
  930. msg[3] = width;
  931. return 4;
  932. }
  933. EXPORT_SYMBOL_GPL(spi_populate_width_msg);
  934. int spi_populate_sync_msg(unsigned char *msg, int period, int offset)
  935. {
  936. msg[0] = EXTENDED_MESSAGE;
  937. msg[1] = 3;
  938. msg[2] = EXTENDED_SDTR;
  939. msg[3] = period;
  940. msg[4] = offset;
  941. return 5;
  942. }
  943. EXPORT_SYMBOL_GPL(spi_populate_sync_msg);
  944. int spi_populate_ppr_msg(unsigned char *msg, int period, int offset,
  945. int width, int options)
  946. {
  947. msg[0] = EXTENDED_MESSAGE;
  948. msg[1] = 6;
  949. msg[2] = EXTENDED_PPR;
  950. msg[3] = period;
  951. msg[4] = 0;
  952. msg[5] = offset;
  953. msg[6] = width;
  954. msg[7] = options;
  955. return 8;
  956. }
  957. EXPORT_SYMBOL_GPL(spi_populate_ppr_msg);
  958. #ifdef CONFIG_SCSI_CONSTANTS
  959. static const char * const one_byte_msgs[] = {
  960. /* 0x00 */ "Task Complete", NULL /* Extended Message */, "Save Pointers",
  961. /* 0x03 */ "Restore Pointers", "Disconnect", "Initiator Error",
  962. /* 0x06 */ "Abort Task Set", "Message Reject", "Nop", "Message Parity Error",
  963. /* 0x0a */ "Linked Command Complete", "Linked Command Complete w/flag",
  964. /* 0x0c */ "Target Reset", "Abort Task", "Clear Task Set",
  965. /* 0x0f */ "Initiate Recovery", "Release Recovery",
  966. /* 0x11 */ "Terminate Process", "Continue Task", "Target Transfer Disable",
  967. /* 0x14 */ NULL, NULL, "Clear ACA", "LUN Reset"
  968. };
  969. static const char * const two_byte_msgs[] = {
  970. /* 0x20 */ "Simple Queue Tag", "Head of Queue Tag", "Ordered Queue Tag",
  971. /* 0x23 */ "Ignore Wide Residue", "ACA"
  972. };
  973. static const char * const extended_msgs[] = {
  974. /* 0x00 */ "Modify Data Pointer", "Synchronous Data Transfer Request",
  975. /* 0x02 */ "SCSI-I Extended Identify", "Wide Data Transfer Request",
  976. /* 0x04 */ "Parallel Protocol Request", "Modify Bidirectional Data Pointer"
  977. };
  978. static void print_nego(const unsigned char *msg, int per, int off, int width)
  979. {
  980. if (per) {
  981. char buf[20];
  982. period_to_str(buf, msg[per]);
  983. printk("period = %s ns ", buf);
  984. }
  985. if (off)
  986. printk("offset = %d ", msg[off]);
  987. if (width)
  988. printk("width = %d ", 8 << msg[width]);
  989. }
  990. static void print_ptr(const unsigned char *msg, int msb, const char *desc)
  991. {
  992. int ptr = (msg[msb] << 24) | (msg[msb+1] << 16) | (msg[msb+2] << 8) |
  993. msg[msb+3];
  994. printk("%s = %d ", desc, ptr);
  995. }
  996. int spi_print_msg(const unsigned char *msg)
  997. {
  998. int len = 1, i;
  999. if (msg[0] == EXTENDED_MESSAGE) {
  1000. len = 2 + msg[1];
  1001. if (len == 2)
  1002. len += 256;
  1003. if (msg[2] < ARRAY_SIZE(extended_msgs))
  1004. printk ("%s ", extended_msgs[msg[2]]);
  1005. else
  1006. printk ("Extended Message, reserved code (0x%02x) ",
  1007. (int) msg[2]);
  1008. switch (msg[2]) {
  1009. case EXTENDED_MODIFY_DATA_POINTER:
  1010. print_ptr(msg, 3, "pointer");
  1011. break;
  1012. case EXTENDED_SDTR:
  1013. print_nego(msg, 3, 4, 0);
  1014. break;
  1015. case EXTENDED_WDTR:
  1016. print_nego(msg, 0, 0, 3);
  1017. break;
  1018. case EXTENDED_PPR:
  1019. print_nego(msg, 3, 5, 6);
  1020. break;
  1021. case EXTENDED_MODIFY_BIDI_DATA_PTR:
  1022. print_ptr(msg, 3, "out");
  1023. print_ptr(msg, 7, "in");
  1024. break;
  1025. default:
  1026. for (i = 2; i < len; ++i)
  1027. printk("%02x ", msg[i]);
  1028. }
  1029. /* Identify */
  1030. } else if (msg[0] & 0x80) {
  1031. printk("Identify disconnect %sallowed %s %d ",
  1032. (msg[0] & 0x40) ? "" : "not ",
  1033. (msg[0] & 0x20) ? "target routine" : "lun",
  1034. msg[0] & 0x7);
  1035. /* Normal One byte */
  1036. } else if (msg[0] < 0x1f) {
  1037. if (msg[0] < ARRAY_SIZE(one_byte_msgs) && one_byte_msgs[msg[0]])
  1038. printk("%s ", one_byte_msgs[msg[0]]);
  1039. else
  1040. printk("reserved (%02x) ", msg[0]);
  1041. } else if (msg[0] == 0x55) {
  1042. printk("QAS Request ");
  1043. /* Two byte */
  1044. } else if (msg[0] <= 0x2f) {
  1045. if ((msg[0] - 0x20) < ARRAY_SIZE(two_byte_msgs))
  1046. printk("%s %02x ", two_byte_msgs[msg[0] - 0x20],
  1047. msg[1]);
  1048. else
  1049. printk("reserved two byte (%02x %02x) ",
  1050. msg[0], msg[1]);
  1051. len = 2;
  1052. } else
  1053. printk("reserved ");
  1054. return len;
  1055. }
  1056. EXPORT_SYMBOL(spi_print_msg);
  1057. #else /* ifndef CONFIG_SCSI_CONSTANTS */
  1058. int spi_print_msg(const unsigned char *msg)
  1059. {
  1060. int len = 1, i;
  1061. if (msg[0] == EXTENDED_MESSAGE) {
  1062. len = 2 + msg[1];
  1063. if (len == 2)
  1064. len += 256;
  1065. for (i = 0; i < len; ++i)
  1066. printk("%02x ", msg[i]);
  1067. /* Identify */
  1068. } else if (msg[0] & 0x80) {
  1069. printk("%02x ", msg[0]);
  1070. /* Normal One byte */
  1071. } else if ((msg[0] < 0x1f) || (msg[0] == 0x55)) {
  1072. printk("%02x ", msg[0]);
  1073. /* Two byte */
  1074. } else if (msg[0] <= 0x2f) {
  1075. printk("%02x %02x", msg[0], msg[1]);
  1076. len = 2;
  1077. } else
  1078. printk("%02x ", msg[0]);
  1079. return len;
  1080. }
  1081. EXPORT_SYMBOL(spi_print_msg);
  1082. #endif /* ! CONFIG_SCSI_CONSTANTS */
  1083. #define SETUP_ATTRIBUTE(field) \
  1084. i->private_attrs[count] = class_device_attr_##field; \
  1085. if (!i->f->set_##field) { \
  1086. i->private_attrs[count].attr.mode = S_IRUGO; \
  1087. i->private_attrs[count].store = NULL; \
  1088. } \
  1089. i->attrs[count] = &i->private_attrs[count]; \
  1090. if (i->f->show_##field) \
  1091. count++
  1092. #define SETUP_RELATED_ATTRIBUTE(field, rel_field) \
  1093. i->private_attrs[count] = class_device_attr_##field; \
  1094. if (!i->f->set_##rel_field) { \
  1095. i->private_attrs[count].attr.mode = S_IRUGO; \
  1096. i->private_attrs[count].store = NULL; \
  1097. } \
  1098. i->attrs[count] = &i->private_attrs[count]; \
  1099. if (i->f->show_##rel_field) \
  1100. count++
  1101. #define SETUP_HOST_ATTRIBUTE(field) \
  1102. i->private_host_attrs[count] = class_device_attr_##field; \
  1103. if (!i->f->set_##field) { \
  1104. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1105. i->private_host_attrs[count].store = NULL; \
  1106. } \
  1107. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1108. count++
  1109. static int spi_device_match(struct attribute_container *cont,
  1110. struct device *dev)
  1111. {
  1112. struct scsi_device *sdev;
  1113. struct Scsi_Host *shost;
  1114. struct spi_internal *i;
  1115. if (!scsi_is_sdev_device(dev))
  1116. return 0;
  1117. sdev = to_scsi_device(dev);
  1118. shost = sdev->host;
  1119. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1120. != &spi_host_class.class)
  1121. return 0;
  1122. /* Note: this class has no device attributes, so it has
  1123. * no per-HBA allocation and thus we don't need to distinguish
  1124. * the attribute containers for the device */
  1125. i = to_spi_internal(shost->transportt);
  1126. if (i->f->deny_binding && i->f->deny_binding(sdev->sdev_target))
  1127. return 0;
  1128. return 1;
  1129. }
  1130. static int spi_target_match(struct attribute_container *cont,
  1131. struct device *dev)
  1132. {
  1133. struct Scsi_Host *shost;
  1134. struct scsi_target *starget;
  1135. struct spi_internal *i;
  1136. if (!scsi_is_target_device(dev))
  1137. return 0;
  1138. shost = dev_to_shost(dev->parent);
  1139. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1140. != &spi_host_class.class)
  1141. return 0;
  1142. i = to_spi_internal(shost->transportt);
  1143. starget = to_scsi_target(dev);
  1144. if (i->f->deny_binding && i->f->deny_binding(starget))
  1145. return 0;
  1146. return &i->t.target_attrs.ac == cont;
  1147. }
  1148. static DECLARE_TRANSPORT_CLASS(spi_transport_class,
  1149. "spi_transport",
  1150. spi_setup_transport_attrs,
  1151. NULL,
  1152. NULL);
  1153. static DECLARE_ANON_TRANSPORT_CLASS(spi_device_class,
  1154. spi_device_match,
  1155. spi_device_configure);
  1156. struct scsi_transport_template *
  1157. spi_attach_transport(struct spi_function_template *ft)
  1158. {
  1159. int count = 0;
  1160. struct spi_internal *i = kzalloc(sizeof(struct spi_internal),
  1161. GFP_KERNEL);
  1162. if (unlikely(!i))
  1163. return NULL;
  1164. i->t.target_attrs.ac.class = &spi_transport_class.class;
  1165. i->t.target_attrs.ac.attrs = &i->attrs[0];
  1166. i->t.target_attrs.ac.match = spi_target_match;
  1167. transport_container_register(&i->t.target_attrs);
  1168. i->t.target_size = sizeof(struct spi_transport_attrs);
  1169. i->t.host_attrs.ac.class = &spi_host_class.class;
  1170. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  1171. i->t.host_attrs.ac.match = spi_host_match;
  1172. transport_container_register(&i->t.host_attrs);
  1173. i->t.host_size = sizeof(struct spi_host_attrs);
  1174. i->f = ft;
  1175. SETUP_ATTRIBUTE(period);
  1176. SETUP_RELATED_ATTRIBUTE(min_period, period);
  1177. SETUP_ATTRIBUTE(offset);
  1178. SETUP_RELATED_ATTRIBUTE(max_offset, offset);
  1179. SETUP_ATTRIBUTE(width);
  1180. SETUP_RELATED_ATTRIBUTE(max_width, width);
  1181. SETUP_ATTRIBUTE(iu);
  1182. SETUP_ATTRIBUTE(dt);
  1183. SETUP_ATTRIBUTE(qas);
  1184. SETUP_ATTRIBUTE(wr_flow);
  1185. SETUP_ATTRIBUTE(rd_strm);
  1186. SETUP_ATTRIBUTE(rti);
  1187. SETUP_ATTRIBUTE(pcomp_en);
  1188. SETUP_ATTRIBUTE(hold_mcs);
  1189. /* if you add an attribute but forget to increase SPI_NUM_ATTRS
  1190. * this bug will trigger */
  1191. BUG_ON(count > SPI_NUM_ATTRS);
  1192. i->attrs[count++] = &class_device_attr_revalidate;
  1193. i->attrs[count] = NULL;
  1194. count = 0;
  1195. SETUP_HOST_ATTRIBUTE(signalling);
  1196. BUG_ON(count > SPI_HOST_ATTRS);
  1197. i->host_attrs[count] = NULL;
  1198. return &i->t;
  1199. }
  1200. EXPORT_SYMBOL(spi_attach_transport);
  1201. void spi_release_transport(struct scsi_transport_template *t)
  1202. {
  1203. struct spi_internal *i = to_spi_internal(t);
  1204. transport_container_unregister(&i->t.target_attrs);
  1205. transport_container_unregister(&i->t.host_attrs);
  1206. kfree(i);
  1207. }
  1208. EXPORT_SYMBOL(spi_release_transport);
  1209. static __init int spi_transport_init(void)
  1210. {
  1211. int error = transport_class_register(&spi_transport_class);
  1212. if (error)
  1213. return error;
  1214. error = anon_transport_class_register(&spi_device_class);
  1215. return transport_class_register(&spi_host_class);
  1216. }
  1217. static void __exit spi_transport_exit(void)
  1218. {
  1219. transport_class_unregister(&spi_transport_class);
  1220. anon_transport_class_unregister(&spi_device_class);
  1221. transport_class_unregister(&spi_host_class);
  1222. }
  1223. MODULE_AUTHOR("Martin Hicks");
  1224. MODULE_DESCRIPTION("SPI Transport Attributes");
  1225. MODULE_LICENSE("GPL");
  1226. module_init(spi_transport_init);
  1227. module_exit(spi_transport_exit);