scsi_transport_spi.c 41 KB

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