scsi_transport_spi.c 38 KB

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