scsi_transport_spi.c 39 KB

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